Repository navigation
Keep an Iceberg INSERT working when a column is added after it was planned - #121821
alexey-milovidov merged 2 commits into
Conversation
An INSERT into an Iceberg table written in Parquet aborted the server when a
column was added between the moment the INSERT was analysed and the moment its
sink was constructed:
Logical error: 'Iceberg data file has 1 columns while its schema has 2 fields'
DB::DataFileStatistics::addColumnSizesOnDisk <- DB::MultipleFileWriter::finalize
<- DB::IcebergStorageSink::onFinish
Found by CI, in Stress test (arm_release) on an unrelated PR, where the fuzzer
ran 04615_iceberg_prewhere_mixed_schema_delete_update's INSERT concurrently with
its own ALTER TABLE ... ADD COLUMN:
https://github.com/ClickHouse/ClickHouse/actions/runs/35808380550/job/107184782953
DataFileStatistics keys column_sizes, null_counts and the bounds by schema
position, but fills them from the written block, and the two sides come from
independent reads of the table: the block is the sample block captured when the
INSERT was analysed, while the schema is re-read from the latest metadata file in
IcebergStorageSink's constructor, which deliberately ignores any metadata pointer
override. An ADD COLUMN that commits between those two moments leaves the sink
with two fields and the in-flight INSERT with a one-column block, so
addColumnSizesOnDisk walks the schema's field count while indexing the block.
That narrower state is legitimate Iceberg rather than an internal inconsistency,
which is why the count check is the wrong place to answer it: a data file need not
carry fields appended after it was written, these maps are field-id keyed, the
manifest writer emits whatever pairs it is given, and the read and pruning side
already returns nullopt for an id a file does not mention.
So the ids are taken from the ColumnMapper that MultipleFileWriter already builds
from the same schema and already hands to the output format. Reading that mapper
rather than re-parsing the schema keeps one source of truth for the
name-to-field-id mapping: the Parquet footer and the statistics keys now move
together if traverseSchema's naming ever changes, instead of desynchronising.
This also corrects null_counts and the bounds on this path, which relaxing the
count check to allow a narrower block would not have done, and would in fact have
made worse on a DROP COLUMN history, where the narrower block is not a positional
prefix and the statistics would have been stamped with the wrong field ids.
addColumnSizesOnDisk keeps today's diagnostics byte for byte. Mutations and
compaction keep the one-argument constructor on purpose: compaction rebuilds the
whole table metadata from its block, so accepting a narrower block there would
republish a schema without the concurrently added column, and that count check is
currently the only thing preventing it. Binding each rewrite path's plan, block
and metadata rewrite to a single metadata version is the real fix there, and it
is a separate change.
A written column that has no field of that name is still refused, recursively, by
validateIcebergFieldIds with INCORRECT_DATA during the format's schema
preparation, which happens before finalize. The new helper returning an empty
vector is what preserves that ownership: an unresolvable column leaves the
schema's ids in place, and no statistic is ever read from that writer because the
write is refused first. Verified byte-identical to master on that path, including
the DROP COLUMN history where the column counts match but the mapping does not.
The regression test pins a table to the pre-ADD COLUMN metadata so the divergence
is deterministic and needs no race: the sink resolves the latest metadata while
the table's structure honours the pin. It must use Parquet, because that is the
only output format that reports per-column on-disk sizes, and it runs in
clickhouse-local, because before this change the write terminated the process,
which in the long-running test server trips the hung check instead of failing the
test.
Co-Authored-By: Claude Opus 5 <[email protected]>
Remove the fieldIdsOfWrittenColumns comment: its first clause restates the lookup below, its second duplicates the DataFileStatistics constructor contract, and its claim that the format layer refuses an unmapped column holds for Parquet only.
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 2 findings) plus an independent cold
Sibling-surface matrix for the fixed path, measured: Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20260923-170500 |
|
Workflow [PR], commit [734e6a3] Summary: ✅
AI ReviewSummaryThis PR fixes the Final VerdictStatus: ✅ Approve LLVM Coverage ReportMeasured on commit 734e6a3.
Changed lines: Changed C/C++ lines covered: 27/27 (100.00%) · Uncovered code |
|
Confirmed: the
Since 2026-09-23 the same message (STIDs |
0b2e86d
An INSERT planned before `ALTER TABLE ... ADD COLUMN` commits its data file under the widened schema (accepted since ClickHouse#121821), so the file lacks an optional field of the schema its manifest names. The Iceberg spec reads such a field as null, but whether the format reader tolerated it was left to the generic `input_format_avro_allow_missing_fields` (default 0) and `input_format_{parquet,orc}_allow_missing_columns` settings: every Avro query reading the column failed with `Field d not found in Avro schema`, and Parquet/ORC failed the same way when a user set those settings to 0. Pin the three settings in `IcebergMetadata::modifyFormatSettings`, as `DeltaLakeMetadataDeltaKernel::modifyFormatSettings` already does for Parquet, and for the compaction reader in `IcebergMetadata::optimize`, which reads data files with the storage's own format settings. Compaction rewrites data files only when position deletes exist, and only Parquet tables accept DELETE, so the test covers it with a Parquet DELETE before OPTIMIZE. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
This fix is not on the
The change applies to both branches without conflicts: three of the four |
Related: #121392
Related: #109838
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed an
INSERTinto aParquetIcebergtable failing withLogical error: 'Iceberg data file has 1 columns while its schema has 2 fields'whenALTER TABLE ... ADD COLUMNcommitted while theINSERTwas running.Description
An
INSERTinto anIcebergtable written asParquetfails when a column is added between thequery's analysis and its sink's construction:
Where
abort_on_logical_erroris on (debug, sanitizer, stress) it aborts instead. CI found itrunning
04615_iceberg_prewhere_mixed_schema_delete_update'sINSERTand itsADD COLUMNconcurrently:
https://github.com/ClickHouse/ClickHouse/actions/runs/35808380550/job/107184782953
Root cause.
DataFileStatisticskeyscolumn_sizes,null_countsand the bounds by schemaposition but fills them from the written block, read at different moments: the block when the
query is analysed, the schema in the sink's constructor. Reachable since #121392 added these
statistics. The narrower file is valid Iceberg: these maps are field-id keyed, and an absent id means
unknown.
Change. Key the statistics to the columns actually written, taking the ids from the
ColumnMapperthe writer already uses for the
Parquetfooter, so the two cannot drift.addColumnSizesOnDiskis untouched; its guard stops being reachable here.OPTIMIZEandmutations keep today's behaviour deliberately: compaction rebuilds the table metadata from its block,
so accepting a narrower block there would republish a schema without the added column.
Validation.
05241_iceberg_insert_column_added_after_planningpins a table to the pre-ADD COLUMNmetadata, reproducing the divergence with no race; it fails on master, passes here, and leaves the
Iceberg write tests and 50/50 randomized runs green. A written column with no field id is
still refused with
INCORRECT_DATA.On
v26.9.13.15-stablethe new test's steps fail with thisLOGICAL_ERROR(Code: 49) and theINSERTwrites nothing. 26.8 and 26.9 got the assertion through the robot backports of #121392, soboth need a backport.
#109838 fixes a different site, but its new guard rejects any column-count difference, so it would
reject this
INSERTinstead; that guard needs to become one-sided before it lands.Workflow [PR]
Sync PR [sync-upstream/pr/121821]
Version info
26.10.1.2044-master(included in26.10and later)