Skip to content

Cherry pick #117571 to 26.6: Fix the byte width of Iceberg manifest bounds for int and date columns - #120145

Merged
robot-clickhouse merged 9 commits into
backport/26.6/117571from
cherrypick/26.6/117571
Sep 15, 2026
Merged

robot-clickhouse merged 9 commits into
backport/26.6/117571from
cherrypick/26.6/117571

Conversation

@robot-ch-test-poll2

Copy link
Copy Markdown
Contributor

Original pull-request #117571

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

groeneai and others added 8 commits September 1, 2026 17:09
`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: #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.
Issue #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 #117571.
… 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.
…bound-width

Fix the byte width of Iceberg manifest bounds for `int` and `date` columns
@robot-ch-test-poll2 robot-ch-test-poll2 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels Sep 15, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants