Skip to content
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

Merged
merged 5 commits into from
Oct 21, 2024

Conversation

phillipleblanc
Copy link
Contributor

@phillipleblanc phillipleblanc commented Oct 21, 2024

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.

@github-actions github-actions bot added the arrow Changes to the arrow crate label Oct 21, 2024
Copy link

@peasee peasee left a 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!


impl<'a> Encoder for Decimal128Encoder<'a> {
fn encode(&mut self, idx: usize, out: &mut Vec<u8>) {
let formatted = self.array.value_as_string(idx);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me try this

Copy link
Contributor Author

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?

Copy link
Contributor

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

Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrow Changes to the arrow crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement arrow_json encoder for Decimal128 & Decimal256 DataTypes
3 participants