Skip to content

Commit c209332

Browse files
authored
fix: raise ValidationError for decimal precision outside range (#3585)
Closes #3583 # Rationale for this change According to the Iceberg specification, decimal precision must be between 1 and 38. Previously, `pyiceberg` silently accepted invalid values (such as `precision=39` or `0`), which could lead to data corruption or crashes downstream when encoding/decoding fixed-byte decimals (matching the Java implementation's `IllegalArgumentException`). This PR adds a defensive validation guard into `DecimalType.__init__` to raise a `ValidationError` when the precision is out of bounds `[1, 38]`. Additionally, fixed an existing invalid type definition (`DecimalType(100, 2)`) inside `tests/test_schema.py` which was violating the specification bounds and causing the new validation guard to trip during the integration test suite. ## Are these changes tested? Yes, added dedicated unit tests in `tests/test_types.py`: - `test_decimal_precision_validation` covers upper bounds (`precision=39`), lower bounds (`precision=0`, `precision=-5`), and ensures valid boundary values (`precision=38`) work perfectly. Also updated existing test assertions in `tests/test_schema.py` to use a compliant precision value (`38`). ## Are there any user-facing changes? Yes, creating a `DecimalType` with an invalid precision will now explicitly raise a `pyiceberg.exceptions.ValidationError` instead of failing silently at runtime.
1 parent ea80d51 commit c209332

3 files changed

Lines changed: 24 additions & 1 deletion

File tree

‎pyiceberg/types.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,14 @@ class DecimalType(PrimitiveType):
326326
def __init__(self, precision: int, scale: int) -> None:
327327
super().__init__(root=(precision, scale))
328328

329+
@model_validator(mode="after")
330+
def check_precision(self) -> DecimalType:
331+
precision = getattr(self, "precision", None) or self.root[0]
332+
333+
if not (1 <= precision <= 38):
334+
raise ValidationError(f"Decimal precision must be between 1 and 38 (inclusive), got: {precision}")
335+
return self
336+
329337
@model_serializer
330338
def ser_model(self) -> str:
331339
"""Serialize the model to a string."""

‎tests/test_schema.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
FloatType(),
6868
DoubleType(),
6969
DecimalType(10, 2),
70-
DecimalType(100, 2),
70+
DecimalType(38, 2),
7171
StringType(),
7272
DateType(),
7373
TimeType(),

‎tests/test_types.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,3 +920,18 @@ def test_nested_field_geography_with_params_as_string() -> None:
920920
assert isinstance(field.field_type, GeographyType)
921921
assert field.field_type.crs == "EPSG:4326"
922922
assert field.field_type.algorithm == "planar"
923+
924+
925+
def test_decimal_precision_validation() -> None:
926+
"""Test that DecimalType rejects precision outside the [1, 38] range."""
927+
decimal_type = DecimalType(38, 2)
928+
assert decimal_type.precision == 38
929+
930+
with pytest.raises(ValidationError, match="Decimal precision must be between 1 and 38"):
931+
DecimalType(39, 2)
932+
933+
with pytest.raises(ValidationError, match="Decimal precision must be between 1 and 38"):
934+
DecimalType(0, 2)
935+
936+
with pytest.raises(ValidationError, match="Decimal precision must be between 1 and 38"):
937+
DecimalType(-5, 2)

0 commit comments

Comments
 (0)