Repository navigation
Make partition_type fields optional, as Java does - #4530
Merged
Merged
Conversation
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]>
Contributor
Author
|
@sungwy, this makes |
Fokko
approved these changes
Nov 30, 2026
Fokko
left a comment
Contributor
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 avoidtransform, which always produces NULL. The manifest writer then cannot store the NULL, soappend()andoverwrite()fail while writing the manifest entry: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, thenManifestWriterusesPartitionSpec.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. Thepartition_typedocstring already says "The partition fields should be optional", andTableMetadata.specs_struct()already builds them that way.#509 made the manifest's partition struct take
requiredfrompartition_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 switchedpartition_typeto 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)withallow_incompatible_changes=True,delete()fails with the sameAttributeErrorwhile 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 onaddress.zip, append then overwrite) andtest_append_after_removing_v1_partition_field_on_required_columntotests/table/test_init.py. Both fail on main with the errors above and pass with this change.test_partition_typenow expects an optional field for the requiredbarcolumn.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 NULLon such a partition field no longer folds toAlwaysFalseduring planning.address.zip IS NULL/IS NOT NULLstill fold on the table schema until #4513 (#4067), as on unpartitioned tables, sodelete("address.zip IS NOT NULL")also removes the NULL-struct rows; with #4513 both filters return the right rows.Please add the
changeloglabel.This pull request and its description were written by Isaac.