Repository navigation
Conversation
…lumn After a partition field is removed and its source column is dropped, the old spec's partition field has the unknown type. Rewriting its manifest wrapped the Avro "null" type in another null union, which is invalid Avro and made the table unreadable. Write unknown fields as a plain "null", like Java's AvroSchemaUtil.toOption. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
772b55d to
a0c2e52
Compare
|
Thanks @hzmsh for fixing this. The underlying problem is that we should not write So we should skip over it when writing, and read it as null. |
|
Hi @Fokko, thank you very much for the review and for pointing me to the spec. That makes sense, and I'm happy to change the PR to skip the field when writing. I tried it locally first and ran into one case I wasn't sure how to handle, so I'd really appreciate your guidance. Skipping the field works for a plain scan. But when the dropped field comes before another partition field, the partition record read back from the manifest is one field short, so the fields after it shift, and filtering on them fails. Exampledata = pa.Table.from_pydict({"id": [1, 2, 3], "category": ["a", "b", "a"], "region": ["x", "y", "y"]})
tbl = catalog.create_table("default.t", schema=data.schema)
with tbl.update_spec() as u:
u.add_identity("category")
u.add_identity("region")
tbl.append(data)
with tbl.update_spec() as u:
u.remove_field("category")
with tbl.update_schema() as u:
u.delete_column("category")
tbl.delete("id == 1")
tbl.scan().to_arrow() # works either way
tbl.scan(row_filter="region == 'y'").to_arrow() # with the field skipped: IndexError in the partition evaluatorWith this PR as it is (unknown written as If I understand correctly, reading the skipped field back as null would mean giving manifest reads the spec's partition type, which they don't have today ( In case it's useful: I originally went with Would you prefer that I go ahead with skipping the field and update the manifest reads, or keep the |

Rationale for this change
If you stop partitioning by a column and then drop that column, the next
delete()breaks the table. The commit succeeds, but after that neither PyIceberg nor Spark can read the table.What goes wrong
categoryfield, but the column is gone, so its type becomesunknown.unknownis stored in Avro as"null".["null", <type>], which gives["null", "null"].IndexError, Spark withDuplicate in union:null.When it started
to_bytesforUnknownType#3899 (including the 0.12.0 release): step 3 raises an error. The delete fails, but the table is untouched and still readable.to_bytesforUnknownType#3899 (merged tomain, not released yet): step 3 succeeds but writes a broken manifest, so the whole table becomes unreadable.main. Once 0.13.0 is released, it would affect everyone who upgrades.Fix
unknown, write it as plain"null"with no union. It's changed in the 3 places that build the Avro schema and the writers.NULL(TypeToSchema.java), andNULLis never wrapped in a union (AvroSchemaUtil.java).to_bytesforUnknownType#3899 ported a different part of Java's fix for this bug (Core, Spark: Handle unknown type during deletes iceberg#14356), but not this one.Reports from users
Are these changes tested?
Yes.
tests/avro/test_file.py::test_write_optional_unknown_type: write an optional unknown field, read it back.tests/catalog/test_catalog_behaviors.py::test_delete_after_dropping_old_partition_source_column: the example above, v1 and v2, every catalog fixture.mainand pass with this change (pure Python and Cython Avro decoders).main; the difference is the 7 new tests. 9 other failures are the same on both (optional packages I don't have).rayordaft, which I don't have installed, and fail the same way onmain.main:Duplicate in union:null. This PR:[2]for v1 and v2.ruff checkandruff format --checkpass.Are there any user-facing changes?
Yes, a bug fix.
delete()andoverwrite()after dropping an old partition column now write readable manifests.overwrite()in this case still fails earlier withCould not find field with id: 2, with or without this change. That's a separate issue.["null", "null"]by an unreleasedmainbuild are not repaired.Since #3899 isn't released yet, could this go into the 0.13.0 milestone?
Written with AI assistance (Claude Code, Claude Opus 5.5). The Java references were read from
apache/icebergmain, and the tests above were run on this branch.