Repository navigation
Keep the schema and partition spec when Iceberg OPTIMIZE TABLE rewrites a table - #129564
Conversation
…ioned table Data compaction (writeMetadataFiles) built the metadata it commits from createEmptyMetadataFile(..., partition_by = nullptr, ...), so it committed an empty spec 0 (no fields, last-partition-id 999) while every rewritten manifest and the manifest list kept the source table's default spec and its partition tuples. Every later writer reads the committed spec: INSERT and ALTER ... DELETE wrote `[]` tuples, so the position deletes never matched the compacted files and the deleted rows stayed visible, and the next OPTIMIZE read 0 partition columns from the committed spec while the old files still carried 1-value tuples, which generateManifestFile reports as LOGICAL_ERROR "Iceberg manifest partition arity mismatch: 0 partition columns but 1 values" (aborts debug builds). Any second data OPTIMIZE on a partitioned table hits it; the emptied partition in the issue is not needed. writeMetadataFiles now copies partition-specs, default-spec-id and last-partition-id from the source metadata, so the committed metadata defines the spec (id, field ids, transforms) the rewritten manifests are written under. Only the partition keys are copied: schema preservation interacts with how the rewritten snapshots are stamped with the current schema id and is handled with its own refusal in ClickHouse#109838. Data compaction rewrites every data file under the default spec, so a tuple of another arity cannot be written (a table whose spec another engine changed to a different number of fields, or a table already compacted by an earlier version). compactIcebergTable now checks every data file's tuple against the default spec before writing anything and refuses with NOT_IMPLEMENTED; a missing default spec is ICEBERG_SPECIFICATION_VIOLATION (master dereferenced a null spec there). generateManifestFile keeps its LOGICAL_ERROR guards, since its other callers compute tuples from the spec they pass. Tests: a stateless test with the issue's two symptoms (a DELETE after OPTIMIZE, then a second OPTIMIZE), and a Spark integration case (DROP PARTITION FIELD) that checks the error, the rows, and that the refused OPTIMIZE writes no file. Closes ClickHouse#129498 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reads apply position delete files whether or not compaction ran, so the row-count assertions of the test also passed when both OPTIMIZE calls were silent no-ops. Count the POSITION_DELETE entries of system.iceberg_files before and after each OPTIMIZE (the pattern of 04815) and require that there were some before and none after. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…a changed default spec `test_optimize_position_delete_other_partition_spec` builds a table whose default partition spec is 1 (`REPLACE PARTITION FIELD`), the one case where `partition-specs`, `default-spec-id` and `last-partition-id` all differ from what `createEmptyMetadataFile` writes. Compare the three keys before and after `OPTIMIZE`; on master all three change (default spec 1 -> 0, last partition id 1001 -> 999, two specs -> one empty spec 0). The metadata lookup moves into a helper shared with `test_optimize_refuses_partition_tuple_of_another_arity`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 4 findings over three rounds) plus my own review.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261105-180600 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-41:20261105-152800 |
|
cc @SmitaRKulkarni @scanhex12, could you review this and add the |
|
Workflow [PR], commit [8feebfc] Summary: ✅
AI ReviewSummaryThe change preserves schema field IDs and partition specifications when Iceberg data compaction rebuilds metadata, and rejects incompatible partition tuple lengths before writing files. The current code addresses the schema-binding concern raised in the discussion. No new correctness or safety findings; approve with the metadata nit below. PR MetadataThe selected ### Changelog category (leave one):
- Bug Fix (user-visible misbehavior in an official stable release)The required changelog entry is present, specific, and describes the affected operations and the new refusal. No entry replacement is needed. Missing context / blind spots
Findings💡 Nits
Final VerdictStatus: ✅ Approve. No code changes requested. Correct the changelog category before merging. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes4 object files changed (+39.55 KiB total), 0 added.
711 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units12 translation units recompiled, 97 s compile time in total, 12 of them have a recent master baseline. |
|
🕵️ @groeneai The fix for #129498 looks right to me. Copying One point about the spec being committed now. Also, #109838 carries the same copy in |
writeMetadataFiles committed the schema built by createEmptyMetadataFile, which numbers the top-level columns 1..N in their current order, while writeDataFiles writes the rewritten files with the source schema's field ids (its column mapper and the manifest statistics both come from the source current schema). When the source ids are not 1..N in column order (a column added after a Tuple column, Spark's ALTER COLUMN ... FIRST), reads after OPTIMIZE bound the file columns to other columns, and the copied partition spec's source-id named another column, so OPTIMIZE of such a partitioned table failed with BAD_GET in the manifest-list partition summary. Copy schemas, current-schema-id and last-column-id from the source metadata together with the partition keys. Remapping the spec through the column names would leave the data files and statistics on the old ids, and refusing such tables is not needed once the ids are kept. Raised in review of ClickHouse#129564. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…the schema Tables written by 26.6 and older record a last-column-id that does not count nested field ids. The copied value is raised with getLastAssignedFieldId, as ADD COLUMN already does, so a writer that trusts last-column-id cannot reuse an existing id after OPTIMIZE. The reordered-columns test now checks that OPTIMIZE removed the position delete, so it cannot pass with a no-op OPTIMIZE. Co-Authored-By: Claude Opus 5.5 <[email protected]>
OPTIMIZE TABLE rewrites a partitioned tableOPTIMIZE TABLE rewrites a table
|
Thanks. I took neither option: the compacted metadata now keeps the source schema as well (8c2af16). The renumbered schema was wrong for more than the spec: Measured on debug builds of master
Both are tests now: a case in On #109838: its |
|
@groeneai how is this not a bug fix when the server aborted in the issue? |
|
You're right. I had filed it as I switched the category to |
…g-insert-column-count-oob-109835 Conflicts with ClickHouse#129564 (keep schema and partition spec in Iceberg `OPTIMIZE TABLE`): - `compactIcebergTable`: keep master's partition-tuple arity check (`NOT_IMPLEMENTED`) first, then this PR's stricter schema-id and partition-spec guards, sharing one lookup of the default spec. - `writeMetadataFiles`: this PR copies all table-level keys, which subsumes master's list of keys; keep master's `last-column-id` recomputation after the copy. - `test_optimize_position_delete_other_partition_spec`: keep this PR's expectation that `OPTIMIZE` is refused, and check that the metadata is unchanged; keep master's new tests. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #129498
Related: #123982
Related: #109838
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
OPTIMIZE TABLEon an Iceberg table (allow_experimental_iceberg_compaction) committing an empty partition spec and a renumbered schema: a laterALTER TABLE ... DELETEleft the rows it matched visible, a secondOPTIMIZE TABLEfailed withLOGICAL_ERROR, and a table whose field ids are not in column order (for example a column added after aTuplecolumn) read other columns' values afterOPTIMIZE. A data file whose partition tuple has a different number of values than the default partition spec is now refused with aNOT_IMPLEMENTEDerror before anything is written. Closes #129498.Description
Data compaction (
writeMetadataFiles) commits metadata built bycreateEmptyMetadataFile(..., partition_by = nullptr, ...): an empty spec 0 and a schema numbered1..Nin column order. The rewritten data files, their statistics and the manifests keep the source field ids and partition spec, so:INSERTandDELETEuse the empty spec, so the position deletes never match the compacted files. On 26.10.2.103,DELETE WHERE a = 3afterOPTIMIZEof a table partitioned bypleaves the row visible, and the nextOPTIMIZEthrowsLOGICAL_ERROR(IcebergOPTIMIZE TABLEaborts the server withLOGICAL_ERROR("partition arity mismatch: 0 partition columns but 1 values") after aDELETEempties an identity partition #129498; debug builds abort).1..Nin column order (a column added after aTuplecolumn, Spark'sALTER COLUMN ... FIRST), reads afterOPTIMIZEreturn other columns' values, andOPTIMIZEof such a partitioned table fails withBAD_GET(the spec'ssource-idnames another column).The change:
writeMetadataFilescopiesschemas,current-schema-id,last-column-id,partition-specs,default-spec-idandlast-partition-idfrom the source metadata;last-column-idis raised over all field ids, asADD COLUMNdoes for tables written by 26.6 and older.compactIcebergTablechecks, before writing any file, that every data file's partition tuple has the default spec's arity, elseNOT_IMPLEMENTED. This replaces theLOGICAL_ERRORfor tables whose spec changed to another number of fields, and for tables already compacted by an earlier version.Files of an older spec with equal arity are still rewritten under the default spec (#124114). #109838 copies these keys too, among larger changes.
Validation, all red on master: the stateless test (a
DELETEafterOPTIMIZE, a secondOPTIMIZE, a column added after aTuplecolumn); Spark cases forDROP PARTITION FIELD(the error, no file written) andALTER COLUMN region FIRST(rows and partitions afterOPTIMIZE,INSERT,DELETE); the existing default-spec-1 Spark case checks that the partition keys are kept.Workflow [PR]
Sync PR [sync-upstream/pr/129564]
Version info
26.11.1.2174-master(included in26.11and later)