-
Notifications
You must be signed in to change notification settings - Fork 837
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement arrow_json encoder for Decimal128 & Decimal256 #6606
Implement arrow_json encoder for Decimal128 & Decimal256 #6606
Conversation
This reverts commit dfe8edb.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good, thanks for working on this!
arrow-json/src/writer/encoder.rs
Outdated
|
||
impl<'a> Encoder for Decimal128Encoder<'a> { | ||
fn encode(&mut self, idx: usize, out: &mut Vec<u8>) { | ||
let formatted = self.array.value_as_string(idx); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW you can avoid this allocation by using https://docs.rs/arrow-cast/latest/arrow_cast/display/struct.ArrayFormatter.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me try this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried this, but it encoded them as strings not numbers, and I don't see any option that is relevant.
DataType::Decimal128(_, _) | DataType::Decimal256(_, _)=> {
let options = FormatOptions::new();
let formatter = ArrayFormatter::try_new(array, &options)?;
(Box::new(formatter) as _, array.nulls().cloned())
}
assertion `left == right` failed
left: [Some(Object {"decimal": Number(12.34)}), Some(Object {"decimal": Number(56.78)}), Some(Object {"decimal": Number(90.12)}), None]
right: [Some(Object {"decimal": String("12.34")}), Some(Object {"decimal": String("56.78")}), Some(Object {"decimal": String("90.12")}), None]
I'm not sure why though, it seems to call the same format_decimal
method that value_as_string
does?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes you will need to keep the custom encoder as a newtype as unlike the implementation for ArrayFormatter above, you don't want to write the quote characters
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took the liberty of doing this in c912fa1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Which issue does this PR close?
Closes #6605
Rationale for this change
Makes the arrow_json crate able to serialize more record batches.
What changes are included in this PR?
Implements a Decimal128Encoder and Decimal256Encoder and uses that within the
make_encoder_impl
function for the Decimal128 & Decimal256 types.Are there any user-facing changes?
More record batches will be able to be serialized to JSON, and this is backwards compatible.