Skip to content

Keep version-hint.text in sync when any writer advances an Iceberg table - #105682

Merged
PedroTadim merged 3 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-105660-iceberg-version-hint
Jun 9, 2026
Merged

PedroTadim merged 3 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-105660-iceberg-version-hint

Conversation

@groeneai

@groeneai groeneai commented May 22, 2026 •

Copy link
Copy Markdown
Collaborator

Once any writer has created version-hint.text for an Iceberg table, every subsequent writer must keep it in sync. Otherwise a reader using iceberg_use_version_hint = 1 observes stale data when a writer that does not have the setting enabled advances the table.

Repro from #105660:

SET allow_insert_into_iceberg = 1;
CREATE TABLE t0 (c0 Int)
    ENGINE = IcebergLocal('/path/to/t0', 'Parquet')
    SETTINGS iceberg_use_version_hint = 1;

INSERT INTO t0 VALUES (1);

INSERT INTO TABLE FUNCTION
    icebergLocal('/path/to/t0', 'Parquet', 'c0 Int') (c0) VALUES (2);

SELECT * FROM t0; -- expected: 1, 2 (was: 1)

The table function does not carry iceberg_use_version_hint, so its writer wrote v3.metadata.json but skipped the version-hint.text update. The named-table reader then resolved the hint to v2.metadata.json and missed row 2.

writeMetadataFileAndVersionHint now runs the compare-and-set loop whenever version-hint.text exists, and only skips it when the file does not exist and the writer was not asked to create it. This matches the Iceberg-spec invariant.

Regression test 04275_105660_iceberg_version_hint_writer_keeps_in_sync covers the two-writer scenario and was confirmed to fail without the fix and pass 50/50 with it.

Closes #105660

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):

Fix wrong results when reading an Iceberg table with iceberg_use_version_hint = 1 after another writer (such as the icebergLocal/icebergS3 table function) without the setting advances the table. version-hint.text is now kept in sync by every writer once the file exists, so subsequent readers using the hint see the latest snapshot.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Note

Medium Risk
Changes Iceberg commit behavior to update version-hint.text even when a writer isn’t configured to create it, affecting concurrent write/commit paths and object-store compare-and-set logic.

Overview
Fixes a stale-read bug for Iceberg tables using iceberg_use_version_hint by ensuring every metadata writer keeps metadata/version-hint.text in sync once the file exists, even if the current writer wasn’t configured to create the hint.

writeMetadataFileAndVersionHint now always enters the compare-and-set retry loop when version-hint.text is present, and only skips hint updates when the file is missing and try_write_version_hint is false. Adds a stateless regression test covering the two-writer scenario (named table creates the hint; table-function writer advances the table) to verify readers using the hint see the latest snapshot.

Reviewed by Cursor Bugbot for commit 20ab0ba. Bugbot is set up for automated code reviews on this repo. Configure here.

Version info

  • Merged into: 26.6.1.545

When an Iceberg table is written through two paths with different values
of `iceberg_use_version_hint` (for example, a named table that sets the
setting and a `icebergLocal`/`icebergS3` table function that does not),
the writer that does not have the setting enabled previously skipped the
update of `version-hint.text`. A subsequent reader using
`iceberg_use_version_hint = 1` then observed stale data because the hint
still pointed at the previous metadata version.

`writeMetadataFileAndVersionHint` now runs the version-hint compare-and-set
loop whenever the file already exists, and only skips it when the file
does not exist and the writer was not asked to create it. This matches
the Iceberg spec invariant that once `version-hint.text` exists, every
writer must keep it in sync.

Closes ClickHouse#105660
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

(a) Deterministic repro? Yes. The fiddle sequence from #105660 reproduces on every run with a fresh table directory: CREATE with iceberg_use_version_hint = 1, INSERT via the named table, then INSERT via the icebergLocal table function pointing at the same path. SELECT from the named table then returns the row from the named-table INSERT and skips the row from the function INSERT.

(b) Root cause explained? Yes. writeMetadataFileAndVersionHint only ran its CAS loop over version-hint.text when its caller passed try_write_version_hint = true, which is gated on iceberg_use_version_hint. A writer whose call site does not set the setting (the icebergLocal/icebergS3 table function default) wrote the new metadata.json but never touched version-hint.text. A reader with the setting enabled then resolved the stale hint and skipped the new snapshot.

(c) Fix matches root cause? Yes. The CAS loop now runs whenever the file already exists. A writer without the setting falls into the loop, observes the existing hint, and advances it to the new metadata version. The try_write_version_hint = false + file-missing branch still skips the write (preserves prior behavior for tables that never had a hint).

(d) Test intent preserved? Yes. New test 04275_105660_iceberg_version_hint_writer_keeps_in_sync exercises the two-writer scenario. Existing 04079_iceberg_write_retry_explicit_metadata still passes against the fixed binary.

(e) Both directions demonstrated? Yes. Built locally, stashed the fix and rebuilt to confirm:

--- expected (with fix) ---
After INSERT via table function, version-hint.text: 3
SELECT via named table (uses version-hint):
1
2

--- actual (without fix) ---
After INSERT via table function, version-hint.text: 2
SELECT via named table (uses version-hint):
1

Diff matches the reference file. Restored the fix and ran the test 50 times back to back: 50/50 pass.

(f) Fix is general, not a narrow patch? Yes. writeMetadataFileAndVersionHint is the single function that writes both the metadata file and the version hint. There is no sibling implementation. The fix moves the gate from "only writers with the setting maintain the hint" to "any writer maintains the hint once it exists", which is the Iceberg-spec invariant for version-hint.text, not a defensive patch at the read site.

Session id: cron:clickhouse-ci-task-worker:20260522-214500

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @PedroTadim @alesapin @Michicosun — could you review? Keeps version-hint.text in sync across writers so a reader with iceberg_use_version_hint = 1 does not see stale data when a second writer to the same table (e.g. the icebergLocal table function) advances it without the setting. Closes #105660.

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label May 23, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 23, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ed12c48]

Summary: ✅


AI Review

Summary
  • This PR keeps Iceberg version-hint.text synchronized once it exists, so writers that do not have iceberg_use_version_hint enabled no longer leave readers on a stale metadata version. The current code also fixes the previously reported cached object storage issue by delegating CachedObjectStorage::readSmallObjectAndGetObjectMetadata to the underlying storage so S3/Azure etag metadata is preserved for conditional hint updates. I found no remaining correctness blockers or majors.
Final Verdict
  • Status: ✅ Approve

@PedroTadim

Copy link
Copy Markdown
Member

@groeneai can you check the CI and maybe with master?

Refreshes the branch from 1724 commits behind to pick up recent CI fixes and
re-trigger CI on PR ClickHouse#105682.
@groeneai

groeneai commented Jun 1, 2026

Copy link
Copy Markdown
Collaborator Author

@PedroTadim Done.

Master merged: 93779ec92a8f (was 1724 commits behind). Iceberg Utils.cpp fix preserved (HEAD vs origin/master shows only the original 3 files: Iceberg/Utils.cpp, the new 04300_105660_iceberg_version_hint_writer_keeps_in_sync.sh test, and its .reference). No conflicts.

Pre-merge CI triage (commit 20ab0bad):

Failing check Test 30d hits / PRs / master Verdict
Unit tests (asan_ubsan, function_prop_fuzzer) FunctionsStress.stress "1 threads got stuck" 302 / 211 / 15 chronic per-iteration cancellation flake
Integration tests (amd_msan, 3/6) test_keeper_reconfig_replace_leader_in_one_command kazoo retry 13 / 12 / 0 chronic Keeper integration flake
CH Inc sync (no URL) infra infra
Finish Workflow / Mergeable Check / PR aggregate of above meta meta

All passing otherwise (110+ checks green: every Build, every AST fuzzer + BuzzHouse variant, all Stateless variants, all Integration shards except the one msan flake, all Performance Comparison, SQLLogic, etc.).

Neither failing test touches Iceberg version-hint.text. New CI run is in flight on the merge commit.

`Utils::writeMetadataFileAndVersionHint` calls `readSmallObjectAndGetObjectMetadata`
on the table's `ObjectStorage` to read the current `version-hint.text` and the
object's `etag` for the conditional `If-Match` write that follows. When the
table is on cached S3/Azure storage, the call dispatches to
`CachedObjectStorage`. That class did not override
`readSmallObjectAndGetObjectMetadata`, so it fell through to
`IObjectStorage::readSmallObjectAndGetObjectMetadata`, which only fills the
bytes and leaves `ObjectMetadata` empty. The Iceberg CAS loop then read an
empty `etag`, cleared `If-None-Match`, and sent an empty `If-Match` to the
write, i.e. an unconditional overwrite. A delayed writer can then stomp a
newer hint published by another writer, defeating the CAS purpose and
re-introducing the stale-read this PR is intended to fix on cached remote
storage.

Override the method in `CachedObjectStorage` so it delegates to the wrapped
storage, mirroring the existing pattern used by `readObject`,
`getObjectMetadata`, and `tryGetObjectMetadata`. The S3 and Azure overrides
populate `etag` from the underlying read buffer, so the CAS path now behaves
correctly through the cache wrapper.

Reported in inline review on PR ClickHouse#105682.
@clickhouse-gh

clickhouse-gh Bot commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.40% 84.50% +0.10%
Functions 92.40% 92.40% +0.00%
Branches 77.00% 77.00% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 31/37 (83.78%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@groeneai

groeneai commented Jun 4, 2026

Copy link
Copy Markdown
Collaborator Author

CI ledger — covered SHA: ed12c48414d8150162b2912cbbde258bb0e024f6

CI fully finished (Mergeable Check completed; 0 pending checks). No CIDB-recorded test failures in 2d.

No PR-caused failures. Awaiting human review.

Session: cron:our-pr-ci-monitor:20260604-213000

@PedroTadim

Copy link
Copy Markdown
Member

The PR looks good to me, @scanhex12 can you give a final look?

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

Labels

can be tested Allows running workflows for external contributors pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

iceberg_use_version_hint table wrong count after insert into path

4 participants