Repository navigation
Write per-file statistics into Iceberg manifest entries on INSERT - #112414
alexey-milovidov merged 6 commits into
Conversation
An INSERT that produced more than one data file per partition wrote the manifest-wide aggregate column statistics into every `data_file` entry of that manifest, so `null_value_counts`, `column_sizes`, `lower_bounds` and `upper_bounds` described the union of all files instead of the file the entry points at. The values were not merely imprecise but impossible and non-conservative: with one row per data file, each one-row entry reported `null_value_counts = 2` for a table holding two NULLs in total, and `column_sizes` were inflated by the file count. The Iceberg pruning rule `null_count == record_count` implies the column is all-NULL in that file, so an external reader evaluating `score = 10` or `score IS NOT NULL` skipped every file and returned no rows for data that was present. ClickHouse's own reader never prunes on `null_value_counts`, which is why ClickHouse-only round trips stayed correct and this went unnoticed. `MultipleFileWriter` already computed the correct per-file statistics and kept them in lockstep with the per-file row and byte counts; the sink discarded them and passed only `getResultStatistics`, so `generateManifestFile` fell into its file-index-independent branch and stamped one set of values inside the per-file loop. The fix is applied at that discard point: a defaulted per-file parameter the sink populates, selected inside the existing type-aware branch. The same serialization path runs, on true values rather than impossible ones. A size mismatch between the per-file statistics and the data files throws `LOGICAL_ERROR` rather than falling back to the aggregate, so a release build cannot silently publish mis-paired metadata. A `chassert` would compile out. Bounds become tighter, which is strictly more accurate and allows more pruning. The Avro schema is unchanged, existing manifests are never rewritten, no setting is added, and all existing callers are byte-unchanged because the new parameter defaults to null. `value_counts` is still not emitted (ClickHouse#103168). Co-Authored-By: Claude Fable 5 <[email protected]>
The pairing of per-file statistics to data files is a bare index, so a permutation of the statistics relative to `data_file_names` would attach one file's `null_value_counts` to another file's entry. That is the same non-conservative metadata the fix removes: an entry can then report `null_count == record_count` for a file that holds a matching row, and a spec-conforming external reader prunes it. Every assertion the test had was invariant under such a permutation. They were sorted multisets over `(record_count, null_value_counts[k])`, a cross-table `max(column_sizes)` inequality, or a read of the table data that was never joined to any entry's `file_path`. Reversing the per-file vector left the reference byte-identical. Two scenarios now read the manifest at the Avro level and join each entry to the Parquet file its `file_path` names. The first compares the entry's `record_count` and `null_value_counts` against that file's real row and null counts and emits a per-entry verdict ordered by the file's own `id`, so a permutation moves the output. The second uses a table with no nullable column, where every entry carries bounds, and requires each entry's decoded `lower_bounds` and `upper_bounds` to equal the single row its own file holds. Reversing the per-file vector now reddens only these two scenarios while every pre-existing assertion stays byte-identical, which is what pins the pairing contract rather than a multiset. The scenarios also document the bounds behaviour in both directions. An entry whose nullable column is entirely NULL legitimately carries no bounds at all, because `canWriteStatistics` is all-or-nothing across an entry's columns and `ColumnNullable::getExtremes` yields NULL extremes that `canDumpIcebergStats` rejects. Writing per-file statistics therefore gains an upper bound on the bounded file and drops the union's lower bound on the all-NULL ones. Conservative in both directions, and now frozen in the reference rather than rediscoverable as a regression. Bounds are held as raw little-endian bytes, decoded here with `reinterpretAsInt32`. No metadata is fabricated, both new tables are scoped to the test database, and the test stays parallel-safe. Co-Authored-By: Claude Fable 5 <[email protected]>
The pairing of per-file statistics to data files is a bare index that governs all four statistics at once, and `column_sizes` is one of the values this change makes describe a single data file. It was the only one no assertion could catch a mis-pairing of. Its one assertion compared the largest per-file size against the largest whole-manifest size, which detects the aggregate fallback but constrains no individual entry. The per-entry cross-checks projected `record_count`, `null_value_counts` and the bounds, but not `column_sizes`. Both of their fixtures also give every data file the same per-file sizes, four bytes for a one-row `Int32` and five for a one-row `Nullable(Int32)`, so reversing the per-file vector could not move a size value there. A third scenario gives each data file a `String` of a different width, which makes each entry's size distinct, and joins every entry to the Parquet file its `file_path` names. Reversing the per-file vector now attaches the twelve-byte file's twenty-byte size to the four-byte file's entry, and dropping the fix restores the manifest-wide forty-eight bytes on all three entries, which is the sum of the three per-file values. The frozen counts are in-memory `IColumn::byteSize` sums rather than Parquet file sizes, so they do not depend on the encoding, the compression codec or the build flavour. For a one-row `String` the value is the character bytes plus one offset, because `insertData` appends exactly the given length with no terminator. Co-Authored-By: Claude Fable 5 <[email protected]>
Internal second-model review (3 rounds, 13 findings, 7 AGREE / 0 DISAGREE on substance)Every change below was reviewed by a second model independently of the author, and by a reviewer Round 1 (head
|
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-46:20260729-074000 |
|
cc @arsenmuk @hanfei1991 — could you review this? Iceberg |
|
Workflow [PR], commit [3e915ae] Summary: ✅
AI ReviewSummaryThis PR fixes the Missing context / blind spots
Final Verdict✅ No new review findings. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 54/58 (93.10%) · Uncovered code |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 53/57 (92.98%) · Uncovered code |
CI finish ledger - 8ee4fdfEvery failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260731-160000 |
…erg-compaction-per-file-stats
Master added `user_defined_snapshot_id` to `generateManifestFile` in the
middle of its parameter list (the compaction manifest-lineage work). The
INSERT sink passes the trailing optional arguments positionally, so after
merging master its `/*data_file_formats=*/{}` bound to the new parameter
and every later argument shifted by one, ending in a hard compile error:
error: no matching function for call to 'generateManifestFile'
note: no known conversion from 'std::nullptr_t' to
'const std::vector<DataFileEntryLineage>' for 21st argument
The merge itself was textually clean, so this only appears at build time.
Pass the new parameter explicitly, matching the argument spelling the
Compaction.cpp and Mutations.cpp callers already use.
Verified on the merged tree: 04652 passes with the fix, and the nullptr
mutation of the per-file statistics argument still reproduces the original
defect (impossible_entries: 3, null_count 2 inside one-row files), so the
regression oracle remains load-bearing. Siblings 04203, 04201 and 04614
are green.
CI finish ledger - 3e915aeEvery failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
No test-level failure on this head: every Stateless/Integration/Stress/Bugfix-validation job concluded Session id: cron:our-pr-ci-monitor:20260809-230000 |
|
Dear @scanhex12, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
|
@groeneai @alexey-milovidov — ClickGap found the following in this PR:
Close anything that's wrong or already addressed. |
|
Answered in #117116 (comment): the finding is real, it is the compaction half this PR deliberately left to #110069 (open), and that PR already implements the per-file statistics fix the issue suggests. |
Resolves the conflict with ClickHouse#112414, which merged first and renamed the statistics source in generateManifestFile from data_file_statistics to effective_statistics. The resolution keeps this branch's per-field filterWritableStatistics mechanism and applies it to the new source, so both call sites read effective_statistics->getLowerBounds()/getUpperBounds() through the filter. ClickHouse#112414 added 04652_iceberg_insert_per_file_statistics, whose reference asserted the all-or-nothing output this branch fixes: the two data files holding an all-NULL score recorded lower=[] upper=[], dropping the bound of the id column along with it. Those two rows now carry the id bound, [(1,2)] and [(1,3)]. Its comments described that loss as pre-existing behaviour asserted rather than fixed, which no longer holds, so they now state the per-field rule instead. Renamed the new test from 04693 to 04739. Master took 04693 for five unrelated tests while this branch was open, and check_gaps_in_tests_numbers only flags gaps, never collisions, so a duplicate number ships silently under green CI. 04739 is free on master, across all 2077 open pull requests and in the fleet worktrees. Co-Authored-By: Claude Opus 5 <[email protected]>
…fest entries on INSERT
Backport #112414 to 26.8: Write per-file statistics into Iceberg manifest entries on INSERT
…fest entries on INSERT
Backport #112414 to 26.7: Write per-file statistics into Iceberg manifest entries on INSERT
Cherry pick #112414 to 26.6: Write per-file statistics into Iceberg manifest entries on INSERT
…fest entries on INSERT
Backport #112414 to 26.6: Write per-file statistics into Iceberg manifest entries on INSERT
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed Iceberg
INSERTwriting manifest-wide aggregate column statistics into everydata_fileentry when a partition produced more than one data file. Each entry'snull_value_counts,column_sizes,lower_boundsandupper_boundsnow describe only the data file it points at. Previously an entry could report more nulls than the file had rows, so a spec-conforming external reader pruned files that contained matching rows.Description
MultipleFileWriteralready computes per-file statistics in lockstep with the per-file row and byte counts. The INSERT sink passed only the manifest-wide aggregate, sogenerateManifestFiletook its file-index-independent branch and stamped the same statistics onto every entry.The written values are not merely imprecise, they are impossible and non-conservative. Inserting three rows as three one-row files,
scorenon-NULL in the first only, made every entry reportnull_value_counts = 2inside a 1-row file and inflatedcolumn_sizesthreefold. The Iceberg rulenull_count == record_countimplies the column is all-NULL in that file, so an external reader evaluatingscore = 10skipped all three files and returned no rows for data that was present. ClickHouse's own reader never prunes onnull_value_counts, which is why ClickHouse-only round trips stayed correct and this went unnoticed.The fix selects those per-file statistics inside the existing type-aware branch, via a new defaulted parameter the sink populates. No bound is widened and no check is loosened: the same serialization path runs, on true values instead of impossible ones. A size mismatch against the data files throws
LOGICAL_ERRORrather than falling back to the aggregate, so a release build cannot silently publish mis-paired metadata.Bounds become per-file too. They were never wrong before, only widened to the manifest union, so that part is an accuracy improvement rather than a correctness fix, and it is not uniformly a pruning improvement (see below). The Avro schema is unchanged, existing manifests are never rewritten, no setting is added, and all existing callers are byte-unchanged because the new parameter defaults to null.
value_countsis still not emitted; that is #103168 and out of scope. The compaction-side occurrences of this defect class are handled by #110069, which touches the same function, so whichever lands second needs a trivial rebase.Bounds trade-off for entries whose column is all-NULL in one file
canWriteStatisticsomits the whole bounds map for an entry as soon as one column's bound is not serializable. A column that is all-NULL inside one file yields an unserializable bound for that entry (ColumnNullable::getExtremesreturns NULL extremes, whichcanDumpIcebergStatsrejects), where the manifest-wide union still had a finite bound contributed by another file. Such an entry therefore ends up with no bounds at all instead of union bounds.Omitted bounds are conservative, so no reader can be misled, but that entry loses bound-based pruning. The all-or-nothing granularity of
canWriteStatisticsis pre-existing and left alone here. The regression test asserts this behaviour explicitly, in both directions, so it is documented rather than rediscovered later.Version info
26.9.1.337(included in26.9and later)26.8.3.94,26.7.8.9,26.6.6.17