Skip to content

Fix an error reading an Iceberg table whose current-snapshot-id is JSON null - #121460

Open
tiandiwonder wants to merge 4 commits into
masterfrom
reland-iceberg-null-current-snapshot-id
Open

tiandiwonder wants to merge 4 commits into
masterfrom
reland-iceberg-null-current-snapshot-id

Conversation

@tiandiwonder

@tiandiwonder tiandiwonder commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Related: #114465
Related: #121443

Re-lands #114465, which was reverted by #121443. The fix itself is unchanged; only the two tests
that call OPTIMIZE are.

Why it was reverted

On a cloud build IcebergMetadata::optimize is gated on IcebergCompactionMetadataGenerator, which
the background scheduler creates lazily. Both OPTIMIZE tests re-attach the table immediately
before calling the command — to pick up the hand-edited current-snapshot-id — so on a cloud build
the generator did not exist yet and OPTIMIZE raised
Logical error: Background compaction is not initialized. This is a bug.

The tests passed: their cloud branch classified that exception as acceptable. But the harness does
not only read test output — ci/jobs/scripts/log_parser.py extracts Logical error.* from the
server log and the functional-test results report it against whatever job shares that server.
Hence the "absolute havoc in the private CI" the revert describes. The public CI has no cloud build,
so nothing showed there.

What changed

The command is no longer issued on a cloud build at all, instead of being issued and then excused:

  • the code paths these tests cover (getHistory and the compaction guard behind it) exist only in
    the open-source branch, so there is nothing for them to assert on cloud;
  • everything printed is build-independent — the table still reads as empty, and the row counts
    around the rewrite — and a successful OPTIMIZE now prints nothing, so the reference no longer
    contains a line a cloud build can produce without having run anything.

Measured on this branch: with the cloud branch forced, 0 OPTIMIZE queries reach the server and
both tests still match their references; the open-source path issues 3. The three tests pass on an
open-source build.

The sibling 04846_iceberg_null_current_snapshot_id is untouched: it exercises the readers that
behave the same on both builds and never calls OPTIMIZE.

Follow-up, not fixed here

OPTIMIZE on a freshly attached Iceberg table raising a LOGICAL_ERROR that says "This is a bug"
is a real defect of the cloud path — a user-reachable window, not a test artifact. It is being fixed
separately on the cloud side; these tests simply stop walking into it.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed an error when reading an Iceberg table whose current-snapshot-id is JSON null, which some writers emit for a table with no current snapshot.


Workflow [PR]
Sync PR [sync-upstream/pr/121460]

tiandiwonder and others added 2 commits September 21, 2026 23:30
This is why the first attempt was reverted (#121443): on a cloud build `IcebergMetadata::optimize`
is gated on `IcebergCompactionMetadataGenerator`, which the background scheduler creates lazily, and
both tests re-attach the table right before calling `OPTIMIZE` - so the generator did not exist yet
and the command raised `Logical error: Background compaction is not initialized. This is a bug`.
The tests themselves passed, because their cloud branch classified that outcome as acceptable, but
the harness reads `Logical error` out of the *server log* (`ci/jobs/scripts/log_parser.py`) and
reports it against whatever job shares that server, which is the "havoc in the private CI" the
revert describes.

Excusing the outcome was the wrong shape. The command is now not issued at all on a cloud build:
the paths these tests cover (`getHistory`, and the compaction guard behind it) exist only in the
open-source branch, and the surrounding assertions - the table still reads as empty, the row counts
around the rewrite - hold on both builds and are the only things printed. A successful `OPTIMIZE`
now prints nothing, so the reference no longer carries a line that a cloud build produces without
having run anything.

Measured on this branch: forcing the cloud branch issues **0** `OPTIMIZE` queries (both tests still
match their references), against 3 on the open-source path, so the log line that caused the revert
cannot be produced. The three tests pass on an open-source build.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [1fe18e8]

Summary: ✅


AI Review

Summary

This PR teaches the Iceberg paths that read current-snapshot-id to treat JSON null as "no current snapshot" and adds focused functional coverage for read, mutation, and OPTIMIZE flows. The conversion-error fixes look mostly sound, but the expire_snapshots change still broadens behavior beyond that contract: it now returns before consulting refs, so metadata that still advertises a live main branch is treated as snapshotless.

Findings

⚠️ Majors

  • [src/Storages/ObjectStorage/DataLakes/Iceberg/ExpireSnapshotsExecute.cpp:712] expire_snapshots already derives retained/protected snapshots from refs (applyRetentionPolicy / partitionSnapshotsByIds below), so returning here on JSON null is only correct when the metadata is actually snapshotless. The new fixture only nulls current-snapshot-id (tests/queries/0_stateless/04846_iceberg_null_current_snapshot_id.sh), leaving the live snapshots and refs.main.snapshot-id in place, which turns ALTER TABLE ... EXECUTE expire_snapshots into a silent no-op on metadata that still names a current branch. Either normalize refs.main alongside current-snapshot-id in the fixture, or keep following refs here and short-circuit only when both carriers say there is no current snapshot.
Final Verdict

Needs changes before merge: the expire_snapshots early return still changes behavior on metadata that is not actually snapshotless.

LLVM Coverage Report

Measured on commit 1fe18e8.

Metric Baseline Current Δ
Lines 88.10% 88.10% +0.00%
Functions 91.70% 91.70% +0.00%
Branches 79.30% 79.40% +0.10%

Changed lines: Changed C/C++ lines covered: 36/38 (94.74%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 22, 2026

if (!metadata->has(Iceberg::f_current_snapshot_id))
/// `has` is true for a JSON null, and `getValue<Int64>` throws on one; null means "no current snapshot".
if (!metadata->has(Iceberg::f_current_snapshot_id) || metadata->isNull(Iceberg::f_current_snapshot_id))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expire_snapshots already derives protected snapshots from refs (applyRetentionPolicy / partitionSnapshotsByIds below), so returning here on JSON null is only correct if the metadata is actually snapshotless. The new fixtures do not put it in that state: they null only current-snapshot-id, while refs.main.snapshot-id still points at the live snapshot. All of our in-tree snapshotless metadata either clears refs or sets refs.main.snapshot-id = -1 (for example tests/queries/0_stateless/data_minio/simple_usage_test/v0.metadata.json and tests/queries/0_stateless/data_minio/issue87414/test/t0/metadata/v1.metadata.json).

With the PR as written, expire_snapshots now becomes a silent no-op on metadata that still advertises a live main branch, instead of just avoiding the getValue<Int64> conversion error. Please either normalize refs.main in the fixture too, or keep walking refs here and short-circuit only when both carriers say there is no current snapshot.

@tiandiwonder tiandiwonder changed the title Fix an error reading an Iceberg table whose current-snapshot-id is JSON null (re-land) Fix an error reading an Iceberg table whose current-snapshot-id is JSON null Sep 22, 2026
…it printed

The sibling `04846_iceberg_optimize_snapshotless_position_delete` already does this; this test was
left deciding on "stderr is non-empty", which is not the same question. The harness sets
`CLICKHOUSE_CLIENT_SERVER_LOGS_LEVEL` to `warning` and injects `--send_logs_level`, so any unrelated
server-log line the client prints would have been reported as `FAIL: OPTIMIZE failed: ...`, failing
the test for something that is not a failure. The exit status answers the question the test is
actually asking, and `--send_logs_level=fatal` keeps a real exception's report to three lines.

The conversion-error check stays text-based: `Can not convert empty value` is the signature of the
regression this test exists for, and the client still prints the exception with logs at `fatal`.

Verified on this branch: the test passes; with `allow_experimental_iceberg_compaction` dropped so
`OPTIMIZE` genuinely fails, it reports `FAIL: OPTIMIZE failed: ... Code: 36 ... (BAD_ARGUMENTS)` and
mismatches, so the assertion still catches a broken `OPTIMIZE`; and with the cloud branch forced it
issues no `OPTIMIZE` at all.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh clickhouse-gh Bot added the comp-datalake Data lake table formats (Iceberg/Delta/Hudi) integration. label Sep 22, 2026
@clickhouse-gh clickhouse-gh Bot added comp-iceberg Apache Iceberg tables: reading and writing Iceberg metadata (snapshots, manifests, schema evoluti... and removed comp-datalake Data lake table formats (Iceberg/Delta/Hudi) integration. labels Nov 19, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-iceberg Apache Iceberg tables: reading and writing Iceberg metadata (snapshots, manifests, schema evoluti... pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants