Skip to content

Commit

Permalink
AVRO-3899: [Rust] Invalid logical types should be ignored and treated…
Browse files Browse the repository at this point in the history
…as the underlying type (#2580)

* AVRO-3899: [Rust] Invalid logical types should be ignored and treated as the underlying type

Log a warning for invalid logical (decimal) schema and use the
underlying (base) schema

Signed-off-by: Martin Tzvetanov Grigorov <mgrigorov@apache.org>

* AVRO-3899: [Rust] Add a new unit test for parsing decimal schema

Assert that a log message is logged for invalid schema.
Assert that the same log message is not logged for a valid decimal
schema.

Signed-off-by: Martin Tzvetanov Grigorov <mgrigorov@apache.org>

---------

Signed-off-by: Martin Tzvetanov Grigorov <mgrigorov@apache.org>
  • Loading branch information
martin-g authored Nov 7, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent a6debf7 commit da98719
Showing 2 changed files with 64 additions and 12 deletions.
64 changes: 58 additions & 6 deletions lang/rust/avro/src/schema.rs
Original file line number Diff line number Diff line change
@@ -1324,12 +1324,17 @@ impl Parser {
parse_as_native_complex(complex, self, enclosing_namespace)?,
&[SchemaKind::Fixed, SchemaKind::Bytes],
|inner| -> AvroResult<Schema> {
let (precision, scale) = Self::parse_precision_and_scale(complex)?;
Ok(Schema::Decimal(DecimalSchema {
precision,
scale,
inner: Box::new(inner),
}))
match Self::parse_precision_and_scale(complex) {
Ok((precision, scale)) => Ok(Schema::Decimal(DecimalSchema {
precision,
scale,
inner: Box::new(inner),
})),
Err(err) => {
warn!("Ignoring invalid decimal logical type: {}", err);
Ok(inner)
}
}
},
);
}
@@ -6311,4 +6316,51 @@ mod tests {

Ok(())
}

#[test]
fn test_avro_3899_parse_decimal_type() -> TestResult {
use apache_avro_test_helper::logger::{assert_logged, assert_not_logged};

let schema = Schema::parse_str(
r#"{
"name": "InvalidDecimal",
"type": "fixed",
"size": 16,
"logicalType": "decimal",
"precision": 2,
"scale": 3
}"#,
)?;
match schema {
Schema::Fixed(fixed_schema) => {
let attrs = fixed_schema.attributes;
let precision = attrs
.get("precision")
.expect("The 'precision' attribute is missing");
let scale = attrs
.get("scale")
.expect("The 'scale' attribute is missing");
assert_logged(&format!("Ignoring invalid decimal logical type: The decimal precision ({}) must be bigger or equal to the scale ({})", precision, scale));
}
_ => unreachable!("Expected Schema::Fixed, got {:?}", schema),
}

let schema = Schema::parse_str(
r#"{
"name": "ValidDecimal",
"type": "bytes",
"logicalType": "decimal",
"precision": 3,
"scale": 2
}"#,
)?;
match schema {
Schema::Decimal(_) => {
assert_not_logged("Ignoring invalid decimal logical type: The decimal precision (2) must be bigger or equal to the scale (3)");
}
_ => unreachable!("Expected Schema::Decimal, got {:?}", schema),
}

Ok(())
}
}
12 changes: 6 additions & 6 deletions lang/rust/avro/tests/schema.rs
Original file line number Diff line number Diff line change
@@ -431,7 +431,7 @@ const DECIMAL_LOGICAL_TYPE: &[(&str, bool)] = &[
"logicalType": "decimal",
"precision": 0
}"#,
false,
true,
),
(
r#"{
@@ -449,7 +449,7 @@ const DECIMAL_LOGICAL_TYPE: &[(&str, bool)] = &[
"precision": 2,
"scale": -2
}"#,
false,
true,
),
(
r#"{
@@ -458,7 +458,7 @@ const DECIMAL_LOGICAL_TYPE: &[(&str, bool)] = &[
"precision": -2,
"scale": 2
}"#,
false,
true,
),
(
r#"{
@@ -467,7 +467,7 @@ const DECIMAL_LOGICAL_TYPE: &[(&str, bool)] = &[
"precision": 2,
"scale": 3
}"#,
false,
true,
),
(
r#"{
@@ -478,7 +478,7 @@ const DECIMAL_LOGICAL_TYPE: &[(&str, bool)] = &[
"scale": 2,
"size": 5
}"#,
false,
true,
),
(
r#"{
@@ -489,7 +489,7 @@ const DECIMAL_LOGICAL_TYPE: &[(&str, bool)] = &[
"scale": 3,
"size": 2
}"#,
false,
true,
),
(
r#"{

0 comments on commit da98719

Please sign in to comment.