Skip to content

Commit 0a7a39e

Browse files
committed
check partition and sort order
1 parent 2d6a1b9 commit 0a7a39e

File tree

7 files changed

+181
-1
lines changed

7 files changed

+181
-1
lines changed

pyiceberg/partitioning.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
model_validator,
3333
)
3434

35+
from pyiceberg.exceptions import ValidationError
3536
from pyiceberg.schema import Schema
3637
from pyiceberg.transforms import (
3738
BucketTransform,
@@ -249,6 +250,37 @@ def partition_to_path(self, data: Record, schema: Schema) -> str:
249250
path = "/".join([field_str + "=" + value_str for field_str, value_str in zip(field_strs, value_strs, strict=True)])
250251
return path
251252

253+
def check_compatible(self, schema: Schema, allow_missing_fields: bool = False) -> None:
254+
# if the underlying field is dropped, we cannot check they are compatible -- continue
255+
schema_fields = schema._lazy_id_to_field
256+
parents = schema._lazy_id_to_parent
257+
258+
for field in self.fields:
259+
source_field = schema_fields.get(field.source_id)
260+
261+
if allow_missing_fields and source_field is None:
262+
continue
263+
264+
if isinstance(field.transform, VoidTransform):
265+
continue
266+
267+
if not source_field:
268+
raise ValidationError(f"Cannot find source column for partition field: {field}")
269+
270+
source_type = source_field.field_type
271+
if not source_type.is_primitive:
272+
raise ValidationError(f"Cannot partition by non-primitive source field: {source_type}")
273+
if not field.transform.can_transform(source_type):
274+
raise ValidationError(f"Invalid source type {source_type} for transform: {field.transform}")
275+
276+
# The only valid parent types for a PartitionField are StructTypes. This must be checked recursively
277+
parent_id = parents.get(field.source_id)
278+
while parent_id:
279+
parent_type = schema.find_type(parent_id)
280+
if not parent_type.is_struct:
281+
raise ValidationError(f"Invalid partition field parent: {parent_type}")
282+
parent_id = parents.get(parent_id)
283+
252284

253285
UNPARTITIONED_PARTITION_SPEC = PartitionSpec(spec_id=0)
254286

pyiceberg/table/sorting.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
model_validator,
2828
)
2929

30+
from pyiceberg.exceptions import ValidationError
3031
from pyiceberg.schema import Schema
3132
from pyiceberg.transforms import IdentityTransform, Transform, parse_transform
3233
from pyiceberg.typedef import IcebergBaseModel
@@ -169,6 +170,17 @@ def __repr__(self) -> str:
169170
fields = f"{', '.join(repr(column) for column in self.fields)}, " if self.fields else ""
170171
return f"SortOrder({fields}order_id={self.order_id})"
171172

173+
def check_compatible(self, schema: Schema) -> None:
174+
schema_ids = schema._lazy_id_to_field
175+
for field in self.fields:
176+
if source_field := schema_ids.get(field.source_id):
177+
if not source_field.field_type.is_primitive:
178+
raise ValidationError(f"Cannot sort by non-primitive source field: {source_field}")
179+
if not field.transform.can_transform(source_field.field_type):
180+
raise ValidationError(f"Invalid source type {source_field.field_type} for transform: {field.transform}")
181+
else:
182+
raise ValidationError(f"Cannot find source column for sort field: {field}")
183+
172184

173185
UNSORTED_SORT_ORDER_ID = 0
174186
UNSORTED_SORT_ORDER = SortOrder(order_id=UNSORTED_SORT_ORDER_ID)

pyiceberg/table/update/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,12 @@ def update_table_metadata(
706706
if base_metadata.last_updated_ms == new_metadata.last_updated_ms:
707707
new_metadata = new_metadata.model_copy(update={"last_updated_ms": datetime_to_millis(datetime.now().astimezone())})
708708

709+
# Check correctness of partition spec, and sort order
710+
new_metadata.spec().check_compatible(new_metadata.schema())
711+
712+
if sort_order := new_metadata.sort_order_by_id(new_metadata.default_sort_order_id):
713+
sort_order.check_compatible(new_metadata.schema())
714+
709715
if enforce_validation:
710716
return TableMetadataUtil.parse_obj(new_metadata.model_dump())
711717
else:

tests/conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
from pyiceberg.serializers import ToOutputFile
7474
from pyiceberg.table import FileScanTask, Table
7575
from pyiceberg.table.metadata import TableMetadataV1, TableMetadataV2, TableMetadataV3
76+
from pyiceberg.table.sorting import NullOrder, SortField, SortOrder
7677
from pyiceberg.transforms import DayTransform, IdentityTransform
7778
from pyiceberg.typedef import Identifier
7879
from pyiceberg.types import (
@@ -1887,6 +1888,11 @@ def test_partition_spec() -> PartitionSpec:
18871888
)
18881889

18891890

1891+
@pytest.fixture(scope="session")
1892+
def test_sort_order() -> SortOrder:
1893+
return SortOrder(SortField(source_id=1, transform=IdentityTransform(), null_order=NullOrder.NULLS_FIRST))
1894+
1895+
18901896
@pytest.fixture(scope="session")
18911897
def generated_manifest_entry_file(
18921898
avro_schema_manifest_entry: dict[str, Any], test_schema: Schema, test_partition_spec: PartitionSpec

tests/integration/test_catalog.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
NoSuchNamespaceError,
3535
NoSuchTableError,
3636
TableAlreadyExistsError,
37+
ValidationError,
3738
)
3839
from pyiceberg.io import WAREHOUSE
3940
from pyiceberg.partitioning import PartitionField, PartitionSpec
@@ -635,3 +636,56 @@ def test_rest_custom_namespace_separator(rest_catalog: RestCatalog, table_schema
635636

636637
loaded_table = rest_catalog.load_table(identifier=full_table_identifier_tuple)
637638
assert loaded_table.name() == full_table_identifier_tuple
639+
640+
641+
@pytest.mark.integration
642+
@pytest.mark.parametrize("test_catalog", CATALOGS)
643+
def test_incompatible_partitioned_schema_evolution(
644+
test_catalog: Catalog, test_schema: Schema, test_partition_spec: PartitionSpec, database_name: str, table_name: str
645+
) -> None:
646+
if isinstance(test_catalog, HiveCatalog):
647+
pytest.skip("HiveCatalog does not support schema evolution")
648+
649+
identifier = (database_name, table_name)
650+
test_catalog.create_namespace(database_name)
651+
table = test_catalog.create_table(identifier, test_schema, partition_spec=test_partition_spec)
652+
assert test_catalog.table_exists(identifier)
653+
654+
with pytest.raises(ValidationError):
655+
with table.update_schema() as update:
656+
update.delete_column("VendorID")
657+
658+
# Assert column was not dropped
659+
assert "VendorID" in table.schema().column_names
660+
661+
with table.transaction() as transaction:
662+
with transaction.update_spec() as spec_update:
663+
spec_update.remove_field("VendorID")
664+
665+
with transaction.update_schema() as schema_update:
666+
schema_update.delete_column("VendorID")
667+
668+
assert table.spec() == PartitionSpec(PartitionField(2, 1001, DayTransform(), "tpep_pickup_day"), spec_id=1)
669+
assert table.schema() == Schema(NestedField(2, "tpep_pickup_datetime", TimestampType(), False))
670+
671+
672+
@pytest.mark.integration
673+
@pytest.mark.parametrize("test_catalog", CATALOGS)
674+
def test_incompatible_sorted_schema_evolution(
675+
test_catalog: Catalog, test_schema: Schema, test_sort_order: SortOrder, database_name: str, table_name: str
676+
) -> None:
677+
if isinstance(test_catalog, HiveCatalog):
678+
pytest.skip("HiveCatalog does not support schema evolution")
679+
680+
identifier = (database_name, table_name)
681+
test_catalog.create_namespace(database_name)
682+
table = test_catalog.create_table(identifier, test_schema, sort_order=test_sort_order)
683+
assert test_catalog.table_exists(identifier)
684+
685+
with pytest.raises(ValidationError):
686+
with table.update_schema() as update:
687+
update.delete_column("VendorID")
688+
689+
assert table.schema() == Schema(
690+
NestedField(1, "VendorID", IntegerType(), False), NestedField(2, "tpep_pickup_datetime", TimestampType(), False)
691+
)

tests/table/test_partitioning.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import pytest
2323

24+
from pyiceberg.exceptions import ValidationError
2425
from pyiceberg.partitioning import UNPARTITIONED_PARTITION_SPEC, PartitionField, PartitionSpec
2526
from pyiceberg.schema import Schema
2627
from pyiceberg.transforms import (
@@ -259,3 +260,36 @@ def test_deserialize_partition_field_v3() -> None:
259260

260261
field = PartitionField.model_validate_json(json_partition_spec)
261262
assert field == PartitionField(source_id=1, field_id=1000, transform=TruncateTransform(width=19), name="str_truncate")
263+
264+
265+
def test_incompatible_source_column_not_found() -> None:
266+
schema = Schema(NestedField(1, "foo", IntegerType()), NestedField(2, "bar", IntegerType()))
267+
268+
spec = PartitionSpec(PartitionField(3, 1000, IdentityTransform(), "some_partition"))
269+
270+
with pytest.raises(ValidationError) as exc:
271+
spec.check_compatible(schema)
272+
273+
assert "Cannot find source column for partition field: 1000: some_partition: identity(3)" in str(exc.value)
274+
275+
276+
def test_incompatible_non_primitive_type() -> None:
277+
schema = Schema(NestedField(1, "foo", StructType()), NestedField(2, "bar", IntegerType()))
278+
279+
spec = PartitionSpec(PartitionField(1, 1000, IdentityTransform(), "some_partition"))
280+
281+
with pytest.raises(ValidationError) as exc:
282+
spec.check_compatible(schema)
283+
284+
assert "Cannot partition by non-primitive source field: struct<>" in str(exc.value)
285+
286+
287+
def test_incompatible_transform_source_type() -> None:
288+
schema = Schema(NestedField(1, "foo", IntegerType()), NestedField(2, "bar", IntegerType()))
289+
290+
spec = PartitionSpec(PartitionField(1, 1000, YearTransform(), "some_partition"))
291+
292+
with pytest.raises(ValidationError) as exc:
293+
spec.check_compatible(schema)
294+
295+
assert "Invalid source type int for transform: year" in str(exc.value)

tests/table/test_sorting.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import pytest
2222

23+
from pyiceberg.exceptions import ValidationError
24+
from pyiceberg.schema import Schema
2325
from pyiceberg.table.metadata import TableMetadataUtil
2426
from pyiceberg.table.sorting import (
2527
UNSORTED_SORT_ORDER,
@@ -28,7 +30,8 @@
2830
SortField,
2931
SortOrder,
3032
)
31-
from pyiceberg.transforms import BucketTransform, IdentityTransform, VoidTransform
33+
from pyiceberg.transforms import BucketTransform, IdentityTransform, VoidTransform, YearTransform
34+
from pyiceberg.types import IntegerType, NestedField, StructType
3235

3336

3437
@pytest.fixture
@@ -114,3 +117,36 @@ def test_serialize_sort_field_v3() -> None:
114117
expected = SortField(source_id=19, transform=IdentityTransform(), null_order=NullOrder.NULLS_FIRST)
115118
payload = '{"source-ids":[19],"transform":"identity","direction":"asc","null-order":"nulls-first"}'
116119
assert SortField.model_validate_json(payload) == expected
120+
121+
122+
def test_incompatible_source_column_not_found(sort_order: SortOrder) -> None:
123+
schema = Schema(NestedField(1, "foo", IntegerType()), NestedField(2, "bar", IntegerType()))
124+
125+
with pytest.raises(ValidationError) as exc:
126+
sort_order.check_compatible(schema)
127+
128+
assert "Cannot find source column for sort field: 19 ASC NULLS FIRST" in str(exc.value)
129+
130+
131+
def test_incompatible_non_primitive_type() -> None:
132+
schema = Schema(NestedField(1, "foo", StructType()), NestedField(2, "bar", IntegerType()))
133+
134+
sort_order = SortOrder(SortField(source_id=1, transform=IdentityTransform(), null_order=NullOrder.NULLS_FIRST))
135+
136+
with pytest.raises(ValidationError) as exc:
137+
sort_order.check_compatible(schema)
138+
139+
assert "Cannot sort by non-primitive source field: 1: foo: optional struct<>" in str(exc.value)
140+
141+
142+
def test_incompatible_transform_source_type() -> None:
143+
schema = Schema(NestedField(1, "foo", IntegerType()), NestedField(2, "bar", IntegerType()))
144+
145+
sort_order = SortOrder(
146+
SortField(source_id=1, transform=YearTransform(), null_order=NullOrder.NULLS_FIRST),
147+
)
148+
149+
with pytest.raises(ValidationError) as exc:
150+
sort_order.check_compatible(schema)
151+
152+
assert "Invalid source type int for transform: year" in str(exc.value)

0 commit comments

Comments
 (0)