Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyiceberg/partitioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
38 changes: 38 additions & 0 deletions tests/table/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion tests/table/test_partitioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)


Expand Down