rahulsmahadev opened a new pull request, #4530:
URL: https://github.com/apache/iceberg-python/pull/4530

   # 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:
   
   ```python
   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.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to