From a3635ba6f892030a161041ec4d8e2bfd95d8da77 Mon Sep 17 00:00:00 2001 From: rahulsmahadev Date: Mon, 30 Nov 2026 09:38:01 +0000 Subject: [PATCH] Make partition_type fields optional, as Java does A partition value can be NULL even when its source column is required: the column can sit in a NULL optional struct, and a v1 void transform always produces NULL. The required field made append and overwrite fail while writing the manifest entry. Co-authored-by: Isaac --- pyiceberg/partitioning.py | 2 +- tests/table/test_init.py | 38 ++++++++++++++++++++++++++++++++ tests/table/test_partitioning.py | 3 ++- 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/pyiceberg/partitioning.py b/pyiceberg/partitioning.py index f557d4a7e2..db6a187564 100644 --- a/pyiceberg/partitioning.py +++ b/pyiceberg/partitioning.py @@ -237,7 +237,7 @@ def partition_type(self, schema: Schema) -> StructType: for field in self.fields: if source_field := schema_ids.get(field.source_id): result_type = field.transform.result_type(source_field.field_type) - nested_fields.append(NestedField(field.field_id, field.name, result_type, required=source_field.required)) + nested_fields.append(NestedField(field.field_id, field.name, result_type, required=False)) else: # Since the source field has been drop we cannot determine the type nested_fields.append(NestedField(field.field_id, field.name, UnknownType())) diff --git a/tests/table/test_init.py b/tests/table/test_init.py index 116b578eb1..574a468a8b 100644 --- a/tests/table/test_init.py +++ b/tests/table/test_init.py @@ -2636,6 +2636,44 @@ def test_build_partition_predicate_with_nan_and_null(table_v2: Table) -> None: assert expr == And(IsNaN("a"), IsNull("b"), EqualTo("c", 1.0)) +def test_write_null_partition_value_of_required_field_in_null_struct(catalog: Catalog) -> None: + """A required field inside a NULL optional struct is NULL, and so is its partition value.""" + import pyarrow as pa + + catalog.create_namespace("default") + address = StructType(NestedField(3, "zip", LongType(), required=True)) + schema = Schema(NestedField(1, "id", LongType(), required=True), NestedField(2, "address", address, required=False)) + rows = pa.Table.from_pylist([{"id": 1, "address": {"zip": 94107}}, {"id": 2, "address": None}], schema=schema.as_arrow()) + for field in [ + PartitionField(source_id=3, field_id=1000, transform=IdentityTransform(), name="zip"), + PartitionField(source_id=3, field_id=1000, transform=BucketTransform(4), name="zip_bucket"), + ]: + table = catalog.create_table(f"default.{field.name}", schema=schema, partition_spec=PartitionSpec(field)) + + table.append(rows) + assert table.scan().to_arrow().sort_by("id").to_pylist() == rows.to_pylist(), field.name + # The overwrite also rewrites the manifest entry that holds the NULL partition value + table.overwrite(rows) + assert table.scan().to_arrow().sort_by("id").to_pylist() == rows.to_pylist(), field.name + + +def test_append_after_removing_v1_partition_field_on_required_column(catalog: Catalog) -> None: + """A v1 table keeps a removed partition field as a void transform, which always produces NULL.""" + import pyarrow as pa + + catalog.create_namespace("default") + schema = Schema(NestedField(1, "id", LongType(), required=True), NestedField(2, "category", StringType(), required=True)) + spec = PartitionSpec(PartitionField(source_id=2, field_id=1000, transform=IdentityTransform(), name="category")) + table = catalog.create_table("default.v1_void", schema=schema, partition_spec=spec, properties={"format-version": "1"}) + table.append(pa.Table.from_pylist([{"id": 1, "category": "a"}], schema=schema.as_arrow())) + with table.update_spec() as update: + update.remove_field("category") + assert isinstance(table.spec().fields[0].transform, VoidTransform) + + table.append(pa.Table.from_pylist([{"id": 2, "category": "b"}], schema=schema.as_arrow())) + assert table.scan().to_arrow().sort_by("id").to_pylist() == [{"id": 1, "category": "a"}, {"id": 2, "category": "b"}] + + def test_overwrite_delete_data_file_on_bucket_partition(catalog: Catalog) -> None: """Delete a data file without projecting its already-transformed partition value again.""" import pyarrow as pa diff --git a/tests/table/test_partitioning.py b/tests/table/test_partitioning.py index 1f24cfccd8..4840a26514 100644 --- a/tests/table/test_partitioning.py +++ b/tests/table/test_partitioning.py @@ -211,7 +211,8 @@ def test_partition_type(table_schema_simple: Schema) -> None: assert spec.partition_type(table_schema_simple) == StructType( NestedField(field_id=1000, name="str_truncate", field_type=StringType(), required=False), - NestedField(field_id=1001, name="int_bucket", field_type=IntegerType(), required=True), + # Optional although bar is required, as in Java: a void transform or a NULL parent struct can still give NULL + NestedField(field_id=1001, name="int_bucket", field_type=IntegerType(), required=False), )