mkroll-db commented on code in PR #3256:
URL: https://github.com/apache/iceberg-rust/pull/3256#discussion_r4080640226
##########
crates/iceberg/src/spec/datatypes.rs:
##########
@@ -1326,6 +1343,79 @@ mod tests {
assert_eq!(16, Type::decimal_required_bytes(38).unwrap());
}
+ #[test]
+ fn test_reject_malformed_decimal_and_fixed_type_strings() {
+ for invalid in [
+ r#""decimal(50, 2)""#,
+ r#""decimal(0, 0)""#,
+ r#""decimal(5, 8)""#,
+ r#""decimal(decimal(5, 2)))""#,
+ r#""decimal(5, 2""#,
+ r#""decimal(5, 2)))))""#,
+ r#""decimal(5, 2, 3)""#,
+ r#""decimal(-5, 2)""#,
+ r#""decimal()""#,
+ r#""fixed[fixed[16]]]""#,
+ r#""fixed[16""#,
+ r#""fixed[16]]]""#,
+ r#""fixed[]""#,
Review Comment:
Suggestion for some more tests, in particular testing negative numbers (go
has them excluded in the regex):
```suggestion
r#""decimal(50, 2)""#,
r#""decimal(0, 0)""#,
r#""decimal(5, 8)""#,
r#""decimal(decimal(5, 2)))""#,
r#""decimal(decimal(5, 2)""#,
r#""decimal(5, 2""#,
r#""decimal(5, 2)))))""#,
r#""decimal(5, 2, 3)""#,
r#""decimal(-5, 2)""#,
r#""decimal(5, -2)""#,
r#""decimal(-5, -2)""#,
r#""decimal(-2, -5)""#,
r#""decimal((5, 2))""#,
r#""decimal[5, 2]""#,
r#""decimal()""#,
r#""fixed[fixed[16]]]""#,
r#""fixed[16""#,
r#""fixed[16]]]""#,
r#""fixed[[16]]""#,
r#""fixed[[16]""#,
r#""fixed(16)""#,
r#""fixed[]""#,
```
##########
crates/iceberg/src/spec/datatypes.rs:
##########
@@ -1326,6 +1343,79 @@ mod tests {
assert_eq!(16, Type::decimal_required_bytes(38).unwrap());
}
+ #[test]
+ fn test_reject_malformed_decimal_and_fixed_type_strings() {
+ for invalid in [
+ r#""decimal(50, 2)""#,
+ r#""decimal(0, 0)""#,
+ r#""decimal(5, 8)""#,
+ r#""decimal(decimal(5, 2)))""#,
+ r#""decimal(5, 2""#,
+ r#""decimal(5, 2)))))""#,
+ r#""decimal(5, 2, 3)""#,
+ r#""decimal(-5, 2)""#,
+ r#""decimal()""#,
+ r#""fixed[fixed[16]]]""#,
+ r#""fixed[16""#,
+ r#""fixed[16]]]""#,
+ r#""fixed[]""#,
+ ] {
+ assert!(
+ serde_json::from_str::<Type>(invalid).is_err(),
+ "expected {invalid} to be rejected"
+ );
+ }
+ }
+
+ #[test]
+ fn test_decimal_zero_precision_error() {
+ let error = serde_json::from_str::<PrimitiveType>(r#""decimal(0,
0)""#).unwrap_err();
+ assert!(
+ error
+ .to_string()
+ .contains("Decimal precision must be greater than zero"),
+ "unexpected error: {error}"
+ );
+ }
+
+ #[test]
+ fn test_accept_valid_decimal_and_fixed_type_strings() {
+ for (json, expected) in [
+ (
+ r#""decimal(9, 2)""#,
+ Type::Primitive(PrimitiveType::Decimal {
+ precision: 9,
+ scale: 2,
+ }),
+ ),
+ (
+ r#""decimal(38, 10)""#,
+ Type::Primitive(PrimitiveType::Decimal {
+ precision: 38,
+ scale: 10,
+ }),
+ ),
+ (
+ r#""decimal(5,2)""#,
+ Type::Primitive(PrimitiveType::Decimal {
+ precision: 5,
+ scale: 2,
+ }),
+ ),
Review Comment:
Some more 'corner' case tests:
```suggestion
),
(
r#""decimal(5, 0)""#,
Type::Primitive(PrimitiveType::Decimal {
precision: 5,
scale: 0,
}),
),
(
r#""decimal(5, 5)""#,
Type::Primitive(PrimitiveType::Decimal {
precision: 5,
scale: 5,
}),
),
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]