Skip to content

Find partition source columns the same way as the writer - #4457

Draft
rahulsmahadev wants to merge 2 commits into
apache:mainfrom
rahulsmahadev:rahul/partition-source-under-dotted-struct
Draft

rahulsmahadev wants to merge 2 commits into
apache:mainfrom
rahulsmahadev:rahul/partition-source-under-dotted-struct

Conversation

@rahulsmahadev

@rahulsmahadev rahulsmahadev commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Rationale for this change

Bug: _determine_partitions reads the partition source from the input by its full name in the current table schema, split on dots, while the writer follows the table's struct path with the input's own names (task_schema, from the Arrow field IDs or the name mapping). That gives two bugs:

  • A dot in a name on the path to a nested source fails the write. With a struct a.b and an identity partition on its child c, append raises KeyError: 'Field "a" does not exist in schema', because the split (from Fix support for writing to nested field partition #2204) looks for a column a.
  • When the input has field IDs from before a rename, the partition values come from another column than the one the writer stores, so filtered scans miss the rows:
schema = Schema(NestedField(1, "a", StringType()), NestedField(2, "b", StringType()))
spec = PartitionSpec(PartitionField(source_id=1, field_id=1000, transform=IdentityTransform(), name="a_part"))
tbl = catalog.create_table("db.t", schema=schema, partition_spec=spec)
df = pa.Table.from_pylist([{"a": "x", "b": "y"}], schema=tbl.schema().as_arrow())  # field IDs 1 and 2
for old, new in [("a", "tmp"), ("b", "a"), ("tmp", "b")]:
    with tbl.update_schema() as update:
        update.rename_column(old, new)
tbl.append(df)
tbl.scan().to_arrow().to_pylist()  # [{'b': 'x', 'a': 'y'}]
[task.file.partition for task in tbl.scan().plan_files()]  # [Record[y]], expected [Record[x]]
tbl.scan(row_filter="b = 'x'").to_arrow().to_pylist()  # [], expected the row

Fix: find the source the same way as the writer, following its struct path in the table schema with each name taken from the input schema, and pass the names to Arrow literally. How the writer matches columns is unchanged.

Bug: since #921 an input can leave out optional columns, which the writer stores as nulls, but append raises when a partition field uses such a column (ArrowInvalid for a missing struct field):

schema = Schema(NestedField(1, "id", LongType()), NestedField(2, "region", StringType()))
spec = PartitionSpec(PartitionField(source_id=2, field_id=1000, transform=IdentityTransform(), name="region"))
tbl = catalog.create_table("db.events", schema=schema, partition_spec=spec)
tbl.append(pa.table({"id": pa.array([1, 2], pa.int64())}))
# KeyError: 'Field "region" does not exist in schema'

Fix: when an optional field on the source's path is missing from the input, partition on the nulls that the writer stores for it, so the rows go to the null partition. Schema validation is unchanged. This also fixes an input that has the column under another field ID, where main took the partition from values the writer does not store.

Java: SparkWriteBuilder aligns the input schema's IDs with the table, and SparkWrite reuses a PartitionKey whose StructTransform reads each row through schema.accessorForField(sourceId) of that input schema. That StructTransform raises Cannot build accessor for field when the source is missing from the input schema; PyIceberg accepts such input since #921, so the partition follows the nulls its writer stores.

Not covered here: dynamic partition overwrite still has #4196 (open fix #4204), and the row filter converter (_ConvertToArrowExpression._get_field_name) also splits names, so delete rewrites and filtered scans of add_files files with dotted struct names can still raise ArrowInvalid.

Are these changes tested?

Yes, test_append_to_identity_partitioned_table_with_dotted_names, test_append_with_field_ids_after_swapping_column_names and test_partition_values_follow_writer_struct_path (a guard for unchanged behavior) in tests/catalog/test_catalog_behaviors.py, and test_partition_for_nested_field_with_dotted_names in tests/io/test_pyarrow.py. On main the dotted-name tests fail with the KeyError above and the swap test with partition y instead of x; all pass with this change. test_append_without_optional_partition_source_columns in tests/catalog/test_catalog_behaviors.py appends input without an optional column, a struct field and a whole struct, with identity and bucket partitions on each, and checks the rows, the null partition and an IS NULL scan. test_append_without_optional_binary_partition_source in tests/io/test_pyarrow.py does the same for a binary column with a truncate partition. Both fail on main with the KeyError or ArrowInvalid above and pass with this change.

Are there any user-facing changes?

Yes, writes to tables partitioned on a nested field with a dot in a name on its path work, partition values match the written data when the input has field IDs from before a rename, and an append without an optional partition source column puts the rows in the null partition instead of raising.

Please add the changelog label.

This pull request and its description were written by Isaac.

_determine_partitions read the partition source from the input by its full
name in the table schema, split on dots, while the writer finds each column
by following the table's struct path with the input's own names. A dotted
struct name raised KeyError, and an input with field IDs from before a
rename got its partition values from another column, so filtered scans
missed rows. Follow the writer's path and names instead.

Co-authored-by: Isaac <[email protected]>
@rahulsmahadev

Copy link
Copy Markdown
Contributor Author

@geruh, you wrote _get_field_from_arrow_table in #2204. Could you take a look when you have a chance? Thanks!

@rahulsmahadev

Copy link
Copy Markdown
Contributor Author

@rambleraptor could you take a look when you have a chance? Thanks!

Appending input without an optional column that a partition field uses failed
in _determine_partitions, although the schema check allows a missing optional
column and the writer stores nulls for it. Use those nulls as the source
values, so the rows go to the null partition.

Co-authored-by: Isaac <[email protected]>
@rahulsmahadev
rahulsmahadev marked this pull request as draft December 1, 2026 02:16

This branch has not been deployed

No deployments
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.

1 participant