fix(types): validate DecimalType scale, not just precision - #3985
Open
ArulJerald wants to merge 2 commits into
Open
ArulJerald wants to merge 2 commits into
ArulJerald wants to merge 2 commits into
Conversation
DecimalType only bounded precision (1-38) via a model validator; scale was accepted unbounded, including negative or larger than precision, and flowed unchecked into downstream type construction and binding. Add an equivalent check enforcing 0 <= scale <= precision, and fix pre-existing test fixtures across the suite that relied on scale > precision or negative scale. Fixes apache#3981
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
DecimalType only bounded precision (1-38) via a model validator; scale was accepted unbounded, including negative or larger than precision, and flowed unchecked into downstream type construction and binding. Add an equivalent check enforcing 0 <= scale <= precision, and fix pre-existing test fixtures across the suite that relied on scale > precision or negative scale.
Closes #3981
Rationale for this change
DecimalType.check_precisionboundsprecisionto[1, 38], but there was no equivalent check forscale. ADecimalTypewith a negative scale, or a scale greater than its precision, was silently accepted and could flow into downstream type construction, binding, and (de)serialization without ever being rejected.This adds a
check_scalemodel validator (mirroringcheck_precision's shape and error style) enforcing0 <= scale <= precision, matching the decimal semantics used by Parquet and Avro.Are these changes tested?
Yes. Added
test_decimal_scale_validationintests/test_types.py, covering:ValidationErrorValidationErrorscale == precisionandscale == 0boundary cases remain validA number of pre-existing fixtures elsewhere in the suite constructed
DecimalTypes with scale > precision (e.g.DecimalType(19, 25)) or negative scale (e.g.DecimalType(9, -20)), which are now invalid under this rule. Updated to use valid parameters while preserving each test's original intent:tests/avro/test_reader.py,tests/avro/test_resolver.py,tests/avro/test_writer.py,tests/table/test_partitioning.py,tests/test_conversions.py,tests/test_schema.py,tests/utils/test_schema_conversion.py.Are there any user-facing changes?
Yes. Constructing or parsing (e.g. from table metadata JSON) a
DecimalTypewith a negative scale, or a scale greater than its precision, now raisesValidationErrorimmediately instead of being silently accepted.