Skip to content

Make partition_type fields optional, as Java does - #4530

Merged
Fokko merged 1 commit into
apache:mainfrom
rahulsmahadev:rahul/partition-fields-optional
Nov 30, 2026
Merged

Fokko merged 1 commit into
apache:mainfrom
rahulsmahadev:rahul/partition-fields-optional

Conversation

@rahulsmahadev

Copy link
Copy Markdown
Contributor

Rationale for this change

Bug: PartitionSpec.partition_type() marks a partition field required when its source column is required, but the partition value can still be NULL: the column can sit in a NULL optional struct, and a v1 table keeps a removed partition field as a void transform, which always produces NULL. The manifest writer then cannot store the NULL, so append() and overwrite() fail while writing the manifest entry:

schema = Schema(
    NestedField(1, "id", LongType(), required=True),
    NestedField(2, "address", StructType(NestedField(3, "zip", LongType(), required=True)), required=False),
)
spec = PartitionSpec(PartitionField(source_id=3, field_id=1000, transform=IdentityTransform(), name="zip"))
table = catalog.create_table("default.customers", schema=schema, partition_spec=spec)
table.append(pa.Table.from_pylist([{"id": 1, "address": None}], schema=schema.as_arrow()))
# TypeError: unsupported operand type(s) for <<: 'NoneType' and 'int'

After update_spec().remove_field() on a required column of a v1 table, subsequent non-empty appends fail (AttributeError: 'NoneType' object has no attribute 'encode' for a string column), also after upgrading the table to v2. delete() can fail the same way when it rewrites manifest entries containing such NULL partition values.

Fix: build every partition field as optional. Java's Spark writer computes values with PartitionKey, then ManifestWriter uses PartitionSpec.partitionType()'s optional fields (Types.NestedField.optional) for the Avro schema, and its v1 field removal notes that "null values are always allowed in partition data". This changes the corresponding PyIceberg schema derivation. The partition_type docstring already says "The partition fields should be optional", and TableMetadata.specs_struct() already builds them that way.

#509 made the manifest's partition struct take required from partition_type, because Avro reads optional and required fields differently (that review noted "I checked locally in tests, and there the fields are not required"), and in the same PR switched partition_type to the source column's requiredness. This keeps the first part: the manifest writer still writes the schema it is given, and readers use the schema stored in each manifest. Only the derivation goes back to optional, as in Java. Both revisions read either manifest encoding. Appends and deletes with non-NULL partition values remain compatible; main still fails when rewriting a NULL partition value for a required source column.

Not done: keeping a field required when its source and all its ancestors are required and the transform is not void. That is narrower, but it still fails where older files hold NULLs: after update_column("category", required=True) with allow_incompatible_changes=True, delete() fails with the same AttributeError while writing those entries back. Java uses one representation for all partition fields.

Are these changes tested?

Yes, added test_write_null_partition_value_of_required_field_in_null_struct (identity and bucket on address.zip, append then overwrite) and test_append_after_removing_v1_partition_field_on_required_column to tests/table/test_init.py. Both fail on main with the errors above and pass with this change. test_partition_type now expects an optional field for the required bar column.

Are there any user-facing changes?

Yes, these appends and overwrites work now. partition_type() returns optional fields for required source columns too, so new manifests store those partition fields as optional, as Java writes them. IS NULL on such a partition field no longer folds to AlwaysFalse during planning. address.zip IS NULL / IS NOT NULL still fold on the table schema until #4513 (#4067), as on unpartitioned tables, so delete("address.zip IS NOT NULL") also removes the NULL-struct rows; with #4513 both filters return the right rows.

Please add the changelog label.

This pull request and its description were written by Isaac.

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 <[email protected]>
@rahulsmahadev

Copy link
Copy Markdown
Contributor Author

@sungwy, this makes partition_type fields optional again; #509 made them follow the source column's requiredness. A required source can still give a NULL partition value (a required field under a NULL optional struct, or a v1 void field), and the manifest writer then fails. Java's PartitionSpec.partitionType() makes every partition field optional. Could you take a look when you have a chance? Thanks!

@Fokko Fokko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @rahulsmahadev for fixing this, and for the clear write-up on how #509 got us here! Manifests written by earlier versions are still read with their own schema, so this is safe for existing tables.

@Fokko
Fokko added this pull request to the merge queue Nov 30, 2026
Merged via the queue into apache:main with commit 2e4cd90 Nov 30, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants