Skip to content

fix: Support deserialization of BigDecimal fields in Avro records when using the "the serde way" - #338

Merged
martin-g merged 4 commits into
apache:mainfrom
eft-prima:add-big-decimal-to-deserialize-any
Nov 23, 2025
Merged

martin-g merged 4 commits into
apache:mainfrom
eft-prima:add-big-decimal-to-deserialize-any

Conversation

@eft-prima

@eft-prima eft-prima commented Nov 21, 2025 •

Copy link
Copy Markdown
Contributor

This pull request adds support for the deserialization of BigDecimal fields in Avro records when using the "the serde way".

Currently, the following fails when wrapping the call to apache_avro::from_value(value) with the error Error { details: Failed to deserialize Avro value into value: incorrect value of type: BigDecimal }.

    #[test]
    fn test_big_decimal() {
        #[derive(Clone, Eq, PartialEq, Debug, Default, serde::Deserialize, serde::Serialize)]
        struct Test {
            big_decimal: apache_avro::BigDecimal,
        }

        let schema = {
            let value = r#"
                {
                    "name": "Test",
                    "type": "record",
                    "fields": [
                        {
                            "name": "big_decimal",
                            "type": "bytes",
                            "logicalType": "big-decimal"
                        }
                    ]
                }
            "#;
            Schema::parse_str(value).unwrap()
        };

        let test = Test::default();

        let mut writer = Writer::new(&schema, Vec::default());
        writer.append_ser(test.clone()).unwrap();

        let encoded = writer.into_inner().unwrap();

        let mut reader = Reader::with_schema(&schema, &encoded[..]).unwrap();

        if let Some(Ok(ref value)) = reader.next() {
            let decoded: Test = apache_avro::from_value(value).unwrap();
            assert_eq!(test, decoded);
        }
    }

I believe this is because there is no branch for Value::BigDecimal in fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>.

I've added one using the same via string approach that's used for Value::Uuid.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This pull request adds support for deserializing BigDecimal fields in Avro records when using serde-based deserialization (the "serde way"). Previously, attempting to deserialize a record with a BigDecimal field using apache_avro::from_value() would fail with an error about an incorrect value type.

Key Changes:

  • Added Value::BigDecimal case handling in the deserialize_any method to convert BigDecimal values to strings using to_plain_string()
  • Added a new test test_avro_3779_record_with_bg_ser to verify the serde serialization/deserialization round-trip works correctly

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
avro/src/de.rs Added Value::BigDecimal handling in the deserialize_any method to deserialize BigDecimal values as strings
avro/src/bigdecimal.rs Added from_value import and a new test case to verify BigDecimal deserialization via serde works correctly

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Kriskras99 Kriskras99 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good

Comment thread avro/src/bigdecimal.rs Outdated
}

#[test]
fn test_avro_3779_record_with_bg_ser() -> TestResult {

@Kriskras99 Kriskras99 Nov 22, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This issue number is not relevant for this test, please use avro_rs_338 so it points to this PR

Comment thread avro/src/bigdecimal.rs Resolved
@martin-g
martin-g merged commit f617b8e into apache:main Nov 23, 2025
@martin-g

Copy link
Copy Markdown
Member

Thank you, @eft-prima !

@martin-g martin-g added this to the 0.22.0 milestone Nov 23, 2025
@martin-g martin-g changed the title Support deserialization of BigDecimal fields in Avro records when using the "the serde way" fix: Support deserialization of BigDecimal fields in Avro records when using the "the serde way" Nov 24, 2025
@Kriskras99 Kriskras99 mentioned this pull request Jun 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants