-
Notifications
You must be signed in to change notification settings - Fork 525
fix: reject DecimalType with precision outside [1, 38] #3584
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -324,6 +324,8 @@ class DecimalType(PrimitiveType): | |
| root: tuple[int, int] | ||
|
|
||
| def __init__(self, precision: int, scale: int) -> None: | ||
| if not (1 <= precision <= 38): | ||
| raise ValidationError(f"Precision must be between 1 and 38 (inclusive), got: {precision}") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
DecimalType(10, 2).model_copy(update={"root": (39, 0)})Could you consider using
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1. This is how we do validations elsewhere in the code as well. |
||
| super().__init__(root=(precision, scale)) | ||
|
|
||
| @model_serializer | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -521,6 +521,31 @@ def test_deserialization_decimal_failure() -> None: | |||||
| assert "Could not parse decimal(abc, def) into a DecimalType" in str(exc_info.value) | ||||||
|
|
||||||
|
|
||||||
| def test_decimal_precision_above_38_raises() -> None: | ||||||
| """Precision > 38 must be rejected per the Iceberg spec (https://iceberg.apache.org/spec/#primitive-types).""" | ||||||
| with pytest.raises(ValidationError, match="Precision must be between 1 and 38"): | ||||||
| DecimalType(39, 0) | ||||||
|
|
||||||
|
|
||||||
| def test_decimal_precision_zero_raises() -> None: | ||||||
| """Precision 0 is not valid; minimum precision is 1.""" | ||||||
| with pytest.raises(ValidationError, match="Precision must be between 1 and 38"): | ||||||
| DecimalType(0, 0) | ||||||
|
Comment on lines
+531
to
+533
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: We could remove the code comment. It's obvious when reading the code. |
||||||
|
|
||||||
|
|
||||||
| def test_decimal_precision_boundary_38_accepted() -> None: | ||||||
| """Precision == 38 is the maximum allowed and must be accepted.""" | ||||||
| d = DecimalType(38, 0) | ||||||
| assert d.precision == 38 | ||||||
| assert d.scale == 0 | ||||||
|
|
||||||
|
|
||||||
| def test_decimal_deserialization_precision_above_38_raises() -> None: | ||||||
| """Deserialization of decimal(39, 0) must also raise a ValidationError.""" | ||||||
| with pytest.raises(Exception, match="Precision must be between 1 and 38"): | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| DecimalType.model_validate_json('"decimal(39, 0)"') | ||||||
|
|
||||||
|
|
||||||
| def test_str_decimal() -> None: | ||||||
| assert str(DecimalType(19, 25)) == "decimal(19, 25)" | ||||||
|
|
||||||
|
|
||||||
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.
This looks stray. Can you remove this?