Repository navigation
Fix the byte width of Iceberg manifest bounds for int and date columns - #117571
PedroTadim merged 7 commits into
Conversation
`ClickHouse` wrote the Iceberg manifest `lower_bounds` and `upper_bounds` of
`int` and `date` columns as 8 bytes, where Appendix D of the Iceberg
specification requires 4. `dumpValue` sizes its output as `sizeof(T)` with `T`
deduced from its argument, and `Field::safeGet<Int32>` returns
`NearestFieldType<Int32>`, which is `Int64`, so the branch asked for an `Int32`
and got an 8-byte value. `Date` is `UInt16` internally and therefore
`UInt64`-tagged; it reaches the same branch because `Field::safeGet` explicitly
permits the `UInt64`/`Int64` pair, reinterpreting the same 8-byte slot.
`Date32` is `Int32`-backed and so behaves exactly like `Int32`.
Narrow the value before `dumpValue` sees it, with the same `static_cast<Int32>`
the neighbouring small-integer branch already uses for types that map to the
same Iceberg `int`.
The `ClickHouse` and Java readers consume only the leading `sizeof(type)` bytes,
so `ClickHouse`-only round trips passed and hid this. `pyiceberg` parses at the
spec width and aborts scan planning with `unpack requires a buffer of 4 bytes`
for any filter on such a column, while an unfiltered scan succeeds because it
never consults bounds. Verified with `pyiceberg` 0.11.1: the reported failure
reproduces before this change and both filters scan cleanly after.
The same line also corrects the manifest-list partition summary written by
`OPTIMIZE ... MANIFEST` for an `int` or `date` partition column
(`0700000000000000` to `07000000`, verified by hand).
No reader change and no format version are needed in either direction:
`ColumnVector::insertData` ignores the buffer length and does
`unalignedLoad<T>(pos)`, so an existing 8-byte bound still decodes, and a
4-byte bound is at least `sizeof(type)` for all three carriers. Measured across
{old, new binary} x {8-byte, 4-byte table}: identical correct results.
The existing bounds coverage could not catch this because it decodes with
`reinterpretAsInt32`, which reads its own width and returns the right value from
a bound of any length. The new test asserts the raw bytes instead, and carries an
`Int64` and a `String` bound in the same manifest as controls.
`Float32` bounds have the same latent defect, but `canDumpIcebergStats` rejects
`Float32` at both call sites, so no input reaches it; left unchanged rather than
shipping a change no test can back.
Closes: ClickHouse#117072
The fixture pinned the width of the narrowed write but not its sign. Every bound
in it was non-negative, and for a non-negative value that fits in 32 bits
`static_cast<Int32>` of the `Field`'s `Int64` slot is value-preserving identity,
so nothing in the test would have noticed a narrowing that mishandled the sign.
`Date32` is the only type in the changed `case` label able to supply a negative
bound, because `Date` is `UInt16`-backed and cannot be negative.
Append `d32 Date32` as the last column, which leaves field ids 1 to 4 and their
expected bytes unchanged. Measured on both arms: `toDate32('1950-01-01')` is day
-7305 and is written as `77E3FFFF` with the fix, and as the sign-extended
`77E3FFFFFFFFFFFF` without it. All five field ids are present in both bound
arrays, which is what shows the added column was accepted rather than silently
emptying the statistics, since `canWriteStatistics` is all or nothing across an
entry's columns.
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex). Two passes: a full pass, which raised
Severity: ❌ blocker / |
|
Workflow [PR], commit [3515fd9] Summary: ✅
AI ReviewSummaryThis PR fixes the 8-byte Findings❌ Blockers
Final VerdictChanges are headed in the right direction and the new interoperability test is valuable, but the fix is incomplete: LLVM Coverage ReportMeasured on commit 3515fd9.
Changed lines: Changed C/C++ lines covered: 6/7 (85.71%) · Uncovered code |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-41:20260901-175600 |
| return dumpValue(field.safeGet<Int32>()); | ||
| /// Iceberg writes `int` and `date` as 4 bytes, while a `Field` keeps them in its 8-byte | ||
| /// slot; `dumpValue` takes the width from its argument type, so narrow before dumping. | ||
| return dumpValue(static_cast<Int32>(field.safeGet<Int32>())); |
There was a problem hiding this comment.
static_cast<Int32> fixes the width, but this still goes through dumpValue, which is a raw memcpy of the host representation. On s390x that makes this path write 0000002A here instead of Iceberg's required little-endian 2A000000, so ClickHouse-written manifest bounds remain non-conformant on a supported big-endian target and the new test is only correct on little-endian hosts. Please serialize Iceberg numeric bounds explicitly in little-endian instead of relying on native endianness.
There was a problem hiding this comment.
Agreed on the mechanism, and out of scope for this PR.
dumpValue has been a host-representation memcpy since 4b551c7f8016131 (2025-08-20), and the line before my change was dumpValue(field.safeGet<Int32>()), which is the same host-endian write at 8 bytes. My change narrows the width, not the byte order.
Fixing only this branch would not help s390x anyway. canDumpIcebergStats (IcebergWrites.cpp:130-140) also admits Int64 and DateTime64, which reach the same dumpValue at :269 and :279, so the reported table's long and timestamp bounds stay reversed either way. On a big-endian host neither form is conformant: today pyiceberg fails on the width, with a little-endian writer it would read a reversed 4-byte value.
The writer-only remedy would also make big-endian worse. The read path is symmetric: ManifestFileIterator.cpp:130-132 states that the binary representation "matches our internal representation" and calls insertData, which is a host-endian unalignedLoad<T> (ColumnVector.h:85-88). A little-endian writer against that reader means ClickHouse on s390x misreads the bounds it wrote itself and prunes its own files wrongly. Only the decimal branches are explicit about byte order today (:195-196 and deserializeDecimalBound), so both sides have to move together. I will not do that blind: the s390x build is compile-only (ENABLE_TESTS=0, ci/jobs/build_clickhouse.py:46) and CH_S390X is only produced, never required by a test job, so no big-endian target runs any test and I have no big-endian host to measure on.
The new test's hex assertions do hold on little-endian only, as you say. So does 04652_iceberg_insert_per_file_statistics.sh next to it, which decodes the same bounds with reinterpretAsInt32 and records that assumption in its own comment. With no big-endian test job to gate on, I am not adding a guard for a configuration nothing runs.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 19 s compile time in total, 7 of them have a recent master baseline. |
CI finish ledger - f4e5bf3Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
I have deliberately NOT merged master to pick
Session id: cron:our-pr-ci-monitor:20260902-013000 |
| rm -rf "${BOUND_PATH}" | ||
|
|
||
| # One row, so there is exactly one data file and one manifest whatever the randomized block sizes are. | ||
| ${CLICKHOUSE_CLIENT} --query " |
There was a problem hiding this comment.
@groeneai is it possible to change this into an integration test and show PyIceberg can read Int32 and Date32 columns?
There was a problem hiding this comment.
Done in 67cfc6b. The stateless test is deleted and the coverage is now test_ch_write_pyiceberg_read_bound_width in tests/integration/test_storage_iceberg_interoperability_local/test_interoperability.py: ClickHouse creates the table and inserts one row, then pyiceberg 0.11.1 opens it with StaticTable.from_metadata and scans it. I picked that package because its external_dirs mount already gives the server and the test process one absolute path for the table, so pyiceberg reads exactly what ClickHouse wrote, the way the reporter did.
What it shows:
- filters on
i32 Int32,d Dateandd32 Date32each return the row, and filters outside the bounds return nothing, so the narrowed bounds are usable and not only parseable; Int64,Stringand an unfiltered scan are controls, since those worked before the fix too;- the raw
lower_boundsandupper_boundsare still asserted per field id, which keeps the width, the value, the byte order and the sign of the pre-epochDate32pinned.
Both arms measured in the integration harness. With the fix it passes. Against a pristine master binary it fails on the bound bytes (2A00000000000000, A34D000000000000, 77E3FFFFFFFFFFFF, while the long and string bounds are identical), and the three filtered scans raise unpack requires a buffer of 4 bytes.
Issue ClickHouse#117072 is about an external reader: `pyiceberg` cannot plan a filtered scan of a `ClickHouse`-written table whose `int` or `date` bound is 8 bytes wide. A stateless test can assert the bytes in the manifest, but nothing in `0_stateless` can show that `pyiceberg` then accepts them, which is the property the fix exists for. Replace `05055_iceberg_manifest_bound_width_117072` with `test_ch_write_pyiceberg_read_bound_width` in the local interoperability suite. That package already mounts the table at one absolute path for both `ClickHouse` (container) and the test process (host) through `external_dirs`, so `pyiceberg` opens exactly what `ClickHouse` wrote, the way the reporter did with `StaticTable.from_metadata`. The byte-level oracle is kept rather than traded away: the test reads the raw `lower_bounds` and `upper_bounds` per field id, which pins width, value, byte order, and the sign of the pre-epoch `Date32`. On top of that it asserts what the stateless test could not reach: filters on `Int32`, `Date` and `Date32` return the row, filters outside the bounds return nothing, and the `Int64`, `String` and unfiltered cases stay green because they were never affected. Measured on both arms in the integration harness. With the fix the test passes; against a pristine master binary it fails on the bound bytes (`2A00000000000000`, `A34D000000000000`, `77E3FFFFFFFFFFFF` instead of `2A000000`, `A34D0000`, `77E3FFFF`) while the `long` and `string` bounds are identical, and the three filtered scans raise `unpack requires a buffer of 4 bytes`. Requested by PedroTadim while reviewing ClickHouse#117571.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
CI finish ledger - f9db59dEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260902-193000 |
…int-date-bound-width
…int-date-bound-width
… interop test The manifest bound width test opened a table's newest `metadata/vN.metadata.json` by comparing `last-updated-ms`. `CREATE TABLE` and the first `INSERT` each stamp that field from `system_clock::now()` truncated to milliseconds (`Utils.cpp:1063` and `MetadataGenerator.cpp:216`), so the two documents can carry the same value. Python's `max` returns the first maximal element and `glob.glob` lists `v1` before `v2`, so on a tie the helper returned the pre-INSERT document, whose `current-snapshot-id` is -1, and the next line raised `AttributeError` on a `None` snapshot instead of reading the manifest. Ordering by `N` takes the timestamp out of the decision. It also agrees with how ClickHouse resolves the table itself: `iceberg_recent_metadata_file_by_last_updated_ms_field` defaults to false, which selects `BY_METADATA_FILE_VERSION` (`Utils.cpp:1189`). `test_manifest_compaction.py` tie-breaks on the same parsed version, and `test_full_path_scheme.py` in this package orders by it too. Measured: 40 of 40 `CREATE TABLE` plus `INSERT` pairs left a gap of 4 ms or more, so no tie was observed on this fixture. With the tie forced, the previous helper selected `v1.metadata.json` and this one selects `v2.metadata.json`; both select `v2.metadata.json` when the timestamps differ.
Head branch was pushed to by a user without write access
`test_writes_manifest_list_partition_summary` asserts the raw bytes of the manifest-list partition summary, and it is the first test to do so, which is why it fails on this branch for every parametrisation while it is green on master: At index 1 diff: (False, None, b'\x0b\x00\x00\x00\x00\x00\x00\x00', ...) != (False, None, b'\x0b\x00\x00\x00', ...) The `Int32` partition bound is written as 8 bytes where Appendix D of the Iceberg specification requires 4. `dumpValue` sizes its output as `sizeof(T)` with `T` deduced from its argument, and `Field::safeGet<Int32>` returns `NearestFieldType<Int32>`, which is `Int64`, so the branch asks for an `Int32` and gets an 8-byte value. Narrow before `dumpValue` sees it, with the same `static_cast<Int32>` the neighbouring small-integer branch already uses. This is the source half of `3dc6b4f4ef32` (#117571, `Closes #117072`), which never got a backport label; this branch needs it because the test it adds pins the spec width. That test is the coverage here, so the stateless test of the original commit is left out rather than duplicated. The data-file bounds are corrected by the same line, so this branch stops writing manifests that `pyiceberg` refuses to plan a filtered scan over (`unpack requires a buffer of 4 bytes`). No reader change is needed in either direction: `ColumnVector::insertData` ignores the buffer length and does `unalignedLoad<T>(pos)`, so an existing 8-byte bound still decodes. Related: #117571 Related: #117072
`test_writes_manifest_list_partition_summary` asserts the raw bytes of the manifest-list partition summary, and it is the first test to do so, which is why it fails on this branch for every parametrisation while it is green on master: At index 1 diff: (False, None, b'\x0b\x00\x00\x00\x00\x00\x00\x00', ...) != (False, None, b'\x0b\x00\x00\x00', ...) The `Int32` partition bound is written as 8 bytes where Appendix D of the Iceberg specification requires 4. `dumpValue` sizes its output as `sizeof(T)` with `T` deduced from its argument, and `Field::safeGet<Int32>` returns `NearestFieldType<Int32>`, which is `Int64`, so the branch asks for an `Int32` and gets an 8-byte value. Narrow before `dumpValue` sees it, with the same `static_cast<Int32>` the neighbouring small-integer branch already uses. This is the source half of `3dc6b4f4ef32` (#117571, `Closes #117072`), which never got a backport label; this branch needs it because the test it adds pins the spec width. That test is the coverage here, so the stateless test of the original commit is left out rather than duplicated. The data-file bounds are corrected by the same line, so this branch stops writing manifests that `pyiceberg` refuses to plan a filtered scan over (`unpack requires a buffer of 4 bytes`). No reader change is needed in either direction: `ColumnVector::insertData` ignores the buffer length and does `unalignedLoad<T>(pos)`, so an existing 8-byte bound still decodes. Related: #117571 Related: #117072
|
Backporting to merge these backports #118796 |
Cherry pick #117571 to 26.6: Fix the byte width of Iceberg manifest bounds for `int` and `date` columns
Both conflicts are master-only changes around the PR, not divergence in the fix. Master moved ICEBERG_DIR_* onto a new iceberg_local_interop_dir() helper, and appended test_spark_gzip_metadata_ch_write_spark_read (issue #109801, a different PR) just before this PR's additions. Kept 26.6's own import line and dropped the gzip test; the 100 added lines are byte-identical to the original. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…ds for `int` and `date` columns
Cherry pick #117571 to 26.3: Fix the byte width of Iceberg manifest bounds for `int` and `date` columns
…ds for `int` and `date` columns
…ds for `int` and `date` columns
Backport #117571 to 26.7: Fix the byte width of Iceberg manifest bounds for `int` and `date` columns
…ds for `int` and `date` columns
Backport #117571 to 26.8: Fix the byte width of Iceberg manifest bounds for `int` and `date` columns
Backport #117571 to 26.6: Fix the byte width of Iceberg manifest bounds for `int` and `date` columns
Backport #117571 to 26.3: Fix the byte width of Iceberg manifest bounds for `int` and `date` columns
Closes: #117072
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed the byte width of the Iceberg manifest
lower_boundsandupper_boundswritten forintanddatecolumns: they were 8 bytes wide where the Iceberg specification requires 4. Spec-conformant readers such aspyicebergfailed withunpack requires a buffer of 4 byteswhile planning any scan carrying a filter on such a column.Description
ClickHouse wrote the Iceberg manifest
lower_boundsandupper_boundsofintanddatecolumns as 8 bytes, where Appendix D of the Iceberg specification requires 4. ClickHouse's own
reader takes only the leading
sizeof(type)bytes, so ClickHouse round trips pass;pyicebergparses at the spec width and aborts scan planning with
unpack requires a buffer of 4 bytes.dumpValuetakes its width fromsizeof(T), withTdeduced from its argument, andField::safeGet<Int32>returnsNearestFieldType<Int32>, which isInt64, so the branch asked forInt32and got 8 bytes.Datereaches the same branch through theUInt64/Int64allowance inField::safeGet. The fix narrows before dumping, the samestatic_cast<Int32>idiom theneighbouring small-integer branch already uses. The same line corrects the manifest-list
partition summary
OPTIMIZE ... MANIFESTwrites for anintordatepartition column.A new integration test writes the table with
ClickHouseand scans it withpyiceberg0.11.1: theintanddatebounds go from2A00000000000000andA34D000000000000to2A000000andA34D0000, filters onInt32,DateandDate32return the row, and filters outside them returnnothing, with
Int64,Stringand unfiltered scans as controls. It fails on a pristine masterbinary. The previous coverage decoded with
reinterpretAsInt32, which reads its own width andcannot see it.
No reader change or format version is needed:
ColumnVector::insertDataignores the buffer length,so an existing 8-byte bound still decodes, and 4 bytes is at least
sizeof(type)for all threecarriers. Manifests written earlier keep their 8-byte bounds: they are immutable, and a
manifest-only rewrite copies the bytes verbatim, so an existing table is corrected only as its data
files are rewritten.
Float32bounds carry the same latent defect, butcanDumpIcebergStatsrejectsFloat32at bothcall sites, so no input reaches it; I left it unchanged rather than ship a change no test can back.
Workflow [PR]
Sync PR [sync-upstream/pr/117571]
Version info
26.9.1.637(included in26.9and later)26.8.5.12,26.7.9.13,26.6.7.20,26.3.33.87