Skip to content

Commit

Permalink
Serialize numeric to tape (#4069) (#4073)
Browse files Browse the repository at this point in the history
  • Loading branch information
tustvold authored Apr 18, 2023
1 parent 6665512 commit 4ec95c2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
23 changes: 23 additions & 0 deletions arrow-json/src/reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ fn make_decoder(

#[cfg(test)]
mod tests {
use serde_json::json;
use std::fs::File;
use std::io::{BufReader, Cursor, Seek};
use std::sync::Arc;
Expand Down Expand Up @@ -1976,4 +1977,26 @@ mod tests {
"Json error: whilst decoding field 'a': whilst decoding field 'child': expected primitive got [123, 3465346]"
);
}

#[test]
fn test_serialize_timestamp() {
let json = vec![
json!({"timestamp": 1681319393}),
json!({"timestamp": "1970-01-01T00:00:00+02:00"}),
];
let schema = Schema::new(vec![Field::new(
"timestamp",
DataType::Timestamp(TimeUnit::Second, None),
true,
)]);
let mut decoder = ReaderBuilder::new(Arc::new(schema))
.build_decoder()
.unwrap();
decoder.serialize(&json).unwrap();
let batch = decoder.flush().unwrap().unwrap();
assert_eq!(batch.num_rows(), 2);
assert_eq!(batch.num_columns(), 1);
let values = batch.column(0).as_primitive::<TimestampSecondType>();
assert_eq!(values.values(), &[1681319393, -7200]);
}
}
10 changes: 9 additions & 1 deletion arrow-json/src/reader/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ impl<'a> TapeSerializer<'a> {
offsets,
}
}

fn serialize_number(&mut self, v: &[u8]) {
self.bytes.extend_from_slice(v);
let idx = self.offsets.len() - 1;
self.elements.push(TapeElement::Number(idx as _));
self.offsets.push(self.bytes.len());
}
}

/// The tape stores all values as strings, and so must serialize numeric types
Expand All @@ -81,7 +88,8 @@ macro_rules! serialize_numeric {
($s:ident, $t:ty, $v:ident) => {{
let mut buffer = [0_u8; <$t>::FORMATTED_SIZE];
let s = lexical_core::write($v, &mut buffer);
$s.serialize_bytes(s)
$s.serialize_number(s);
Ok(())
}};
}

Expand Down

0 comments on commit 4ec95c2

Please sign in to comment.