Skip to content

Fix crash when reading Iceberg table through materialized view - #101577

Merged
alesapin merged 4 commits into
ClickHouse:masterfrom
groeneai:fix/iceberg-mv-crash-93278
Apr 7, 2026
Merged

alesapin merged 4 commits into
ClickHouse:masterfrom
groeneai:fix/iceberg-mv-crash-93278

Conversation

@groeneai

@groeneai groeneai commented Apr 2, 2026 •

Copy link
Copy Markdown
Collaborator

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 to CHANGELOG.md):

Fix server crash (LOGICAL_ERROR) when SELECT-ing from a materialized view backed by an IcebergLocal table engine.

What this PR does

When SELECT-ing from a materialized view whose target table uses the
IcebergLocal engine, the server crashes with:

Logical error: 'Can't extract iceberg table state from storage snapshot
for table location ...'

Root cause: StorageMaterializedView::read() delegates to the target table
without calling updateExternalDynamicMetadataIfExists() first. This call is
normally made by the analyzer/interpreter on the outermost storage, but for a
materialized view the call targets the view itself (a no-op for IStorage),
not the actual datalake target. As a result the metadata snapshot passed to
IcebergMetadata::iterate() never has datalake_table_state set, triggering
the LOGICAL_ERROR assertion.

Fixes (defense in depth):

  1. StorageMaterializedView::read() — call
    updateExternalDynamicMetadataIfExists() on the target table before
    obtaining its metadata snapshot. This ensures proper snapshot pinning for
    datalake target tables, consistent with how the analyzer handles direct table
    references.

  2. IcebergMetadata::iterate() / isDataSortedBySortingKey() — instead of
    crashing when datalake_table_state is absent, fall back to the current
    table state via getRelevantState(). This guards against any other code path
    that might skip the metadata update.

Closes #93278

Test plan

🤖 Generated with Claude Code

Version info

  • Merged into: 26.4.1.584

When SELECT-ing from a materialized view whose target is an IcebergLocal
table, the server crashes with:
  Logical error: Can't extract iceberg table state from storage snapshot

Root cause: StorageMaterializedView::read() delegates to the target table
without calling updateExternalDynamicMetadataIfExists() first. This call
is normally made by the analyzer on the outermost storage, but for a
materialized view it targets the view itself (a no-op) rather than the
actual datalake target. As a result, the metadata snapshot passed to
IcebergMetadata::iterate() never has datalake_table_state set.

Two fixes (defense in depth):
1. StorageMaterializedView::read() — call updateExternalDynamicMetadataIfExists()
   on the target table before taking its metadata snapshot, ensuring proper
   snapshot pinning for datalake tables.
2. IcebergMetadata::iterate() / isDataSortedBySortingKey() — fall back to
   getRelevantState() when datalake_table_state is missing instead of
   crashing. This guards against any other code path that might skip the
   metadata update.

Closes ClickHouse#93278

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@groeneai

groeneai commented Apr 2, 2026

Copy link
Copy Markdown
Collaborator Author

cc @scanhex12 @divanik — could you review this? It fixes the server crash from #93278 where SELECT from a materialized view backed by IcebergLocal crashes with LOGICAL_ERROR: Can't extract iceberg table state. The root cause is that StorageMaterializedView::read() delegates to the target table without calling updateExternalDynamicMetadataIfExists(), so the metadata snapshot never has datalake_table_state set when IcebergMetadata::iterate() is reached.

@groeneai

groeneai commented Apr 2, 2026

Copy link
Copy Markdown
Collaborator Author

Pre-PR Validation Gate (session cron:clickhouse-ci-task-worker:20260402-024500)

a) Deterministic repro? Yes — 100% reproducible:

CREATE MATERIALIZED VIEW v REFRESH AFTER 1 MINUTE
  ENGINE = IcebergLocal('/path/to/user_files/test/')
  AS (SELECT 1::Int32 c0);
SELECT c0 FROM v;  -- crashes with LOGICAL_ERROR every time

b) Root cause explained? StorageMaterializedView::read() gets the target table and calls storage->read() on it. But it never calls updateExternalDynamicMetadataIfExists() on the target. The analyzer calls this on the outermost storage (the view itself), which is a no-op for IStorage. The target StorageObjectStorage::read() skips configuration->update() for datalake tables (line 444-448, comment: "For data lake we did update in getExternalDynamicMetadata"). So datalake_table_state is never set → extractIcebergSnapshotIdFromMetadataObject() returns nullptr → LOGICAL_ERROR.

c) Fix matches root cause? Yes — two complementary fixes:

  1. MV read path: call updateExternalDynamicMetadataIfExists() on the target before getting its metadata
  2. iterate() fallback: if datalake_table_state is missing for any reason, fall back to getRelevantState() instead of crashing

d) New tests added? Yes — 04077_iceberg_mv_select_crash regression test that reproduces the exact crash from #93278.

e) Demonstrated in both directions? Yes — without the fix, the test crashes the server deterministically. With the fix, 20/20 passes with full randomization.

f) Fix is general? Yes — checked both crash sites (iterate() and isDataSortedBySortingKey()). The MV fix also covers any future datalake engine used as a MV target (DeltaLake, Hudi).

@alesapin alesapin added the can be tested Allows running workflows for external contributors label Apr 2, 2026
@alesapin alesapin self-assigned this Apr 2, 2026
@clickhouse-gh

clickhouse-gh Bot commented Apr 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0e2dfe6]

Summary: ⏳

job_name test_name status info comment
Stateless tests (amd_msan, flaky check) failure
02845_join_on_cond_sparse FAIL cidb, issue ISSUE EXISTS
Stress test (arm_tsan) failure
Hung check failed, possible deadlock found FAIL cidb, issue ISSUE EXISTS

AI Review

Summary

This PR fixes a real LOGICAL_ERROR path when selecting from a materialized view whose target uses IcebergLocal, by calling updateExternalDynamicMetadataIfExists on the target storage before taking the snapshot in StorageMaterializedView::read. The fix is focused and aligns with how metadata pinning is expected to happen for datalake storages. The added regression test reproduces the reported failure mode and validates the read path after insert. I did not find correctness, safety, concurrency, performance, or PR-metadata issues that require changes.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Apr 2, 2026
ErrorCodes::LOGICAL_ERROR,
"Can't extract iceberg table state from storage snapshot for table location {}",
persistent_components.table_location);
/// The caller did not pin a snapshot in the metadata object.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Aren't we fixed the same problem with two different methods? Isn't it enough to call storage->updateExternalDynamicMetadataIfExists(context); in storage materialized view?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

You're right — the MV fix alone is sufficient. I've removed the fallback in IcebergMetadata and restored the original LOGICAL_ERROR assertions.

After analysis, the only code path where datalake_table_state was missing is the MaterializedView path (the analyzer calls updateExternalDynamicMetadataIfExists() on the view — a no-op — not the target). Normal SELECT queries and table functions already set up the state correctly.

The LOGICAL_ERROR is valuable as an invariant check — if a new code path is added in the future that forgets to call updateExternalDynamicMetadataIfExists(), it should crash and be fixed properly rather than silently degrade.

Verified 10/10 passes with randomization using just the MV fix.

groeneai and others added 2 commits April 2, 2026 14:56
Per @alesapin's review: the LOGICAL_ERROR assertions in iterate() and
isDataSortedBySortingKey() are valuable invariant checks. The root cause
fix in StorageMaterializedView::read() is sufficient — it ensures
updateExternalDynamicMetadataIfExists() is called on the target table
before reading, so the metadata snapshot always has datalake_table_state.

Keeping the LOGICAL_ERROR ensures that any future code path that forgets
to set up metadata will be caught immediately rather than silently
degraded.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
IcebergLocal is a non-replicated engine. Creating a REFRESH
materialized view with a non-replicated target in a replicated
database is explicitly rejected by ClickHouse (BAD_ARGUMENTS).
Skip the test in DatabaseReplicated mode.
@clickhouse-gh

clickhouse-gh Bot commented Apr 4, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% (739,529/879,748) 84.10% (739,594/879,749) +0.00% (+65)
Functions 90.90% (798,401/878,581) 90.90% (798,394/878,600) +0.00% (-7)
Branches 76.60% (239,758/312,978) 76.60% (239,798/312,978) +0.00% (+40)

Changed lines: 100.00% (5/5) · Uncovered code

Full report · Diff report

@alexey-milovidov

Copy link
Copy Markdown
Member

The Stress test (arm_msan) failure is fixed by #101239, which should be merged first. After it is merged, please update the branch to include the fix.

@alesapin
alesapin added this pull request to the merge queue Apr 7, 2026
Merged via the queue into ClickHouse:master with commit be3373d Apr 7, 2026
154 of 158 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 7, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Hi @groeneai @alesapin — the changelog category for this PR might need a look.

Current: Bug Fix (user-visible misbehavior in an official stable release)
Suggested: Critical Bug Fix (crash, data loss, RBAC) or LOGICAL_ERROR

Why: The bug produces a LOGICAL_ERROR (error code 49) crash, which falls under 'Critical Bug Fix (crash, data loss, RBAC) or LOGICAL_ERROR', not the generic 'Bug Fix' category. The issue explicitly states 'Logical error: Can’t extract iceberg table state' and the stack trace shows DB::Exception with code 49 (LOGICAL_ERROR).

Could you verify this is correct? Ignore if the current category is intentional.

@clickgapai

Copy link
Copy Markdown
Contributor

Hi — this PR may need backporting to release branches 26.3, 26.2, 26.1, 25.12, 25.11, 25.10, 25.9, 25.8, but no backport label was found.

Why: This fixes a LOGICAL_ERROR crash affecting any user who creates a materialized view backed by an IcebergLocal engine. The Iceberg storage and MaterializedView code have been present in all active release branches. The crash occurs on valid SQL with no workaround.

If this should be backported, consider adding pr-must-backport or a version-specific label (e.g. v26.3-must-backport). Ignore this if backporting is not applicable.

groeneai added a commit to groeneai/ClickHouse that referenced this pull request Apr 28, 2026
Issue ClickHouse#93278 (reopened): `IcebergMetadata::iterate` and
`IcebergMetadata::isDataSortedBySortingKey` throw `LOGICAL_ERROR`
"Can't extract iceberg table state from storage snapshot" when
`datalake_table_state` is missing from the storage metadata snapshot
(STID 2606-4a47, 11 master + 45 PR hits over 30 days).

The PR's fix in `StorageObjectStorageSource::createFileIterator` and
`ReadFromObjectStorageStep::requestReadingInOrder` re-fetches the
snapshot from the configuration when it is missing. PR ClickHouse#101577 already
covered the materialized-view-target path
(`04077_iceberg_mv_select_crash`); this new test exercises the
concurrent-normal-SELECT path that the same fix has to keep stable.

The test runs many parallel `SELECT`s (both plain and `ORDER BY c0`)
against an `IcebergLocal` table while a writer thread produces new
snapshots, then asserts that the
"Can't extract iceberg table state" `LOGICAL_ERROR` never appears in
captured stderr and that the table remains readable.
zvonand added a commit to Altinity/ClickHouse that referenced this pull request May 12, 2026
Antalya 26.3 Backport of ClickHouse#101577 - Fix crash when reading Iceberg table through materialized view
groeneai added a commit to groeneai/ClickHouse that referenced this pull request May 18, 2026
Issue ClickHouse#93278 (reopened): `IcebergMetadata::iterate` and
`IcebergMetadata::isDataSortedBySortingKey` throw `LOGICAL_ERROR`
"Can't extract iceberg table state from storage snapshot" when
`datalake_table_state` is missing from the storage metadata snapshot
(STID 2606-4a47, 11 master + 45 PR hits over 30 days).

The PR's fix in `StorageObjectStorageSource::createFileIterator` and
`ReadFromObjectStorageStep::requestReadingInOrder` re-fetches the
snapshot from the configuration when it is missing. PR ClickHouse#101577 already
covered the materialized-view-target path
(`04077_iceberg_mv_select_crash`); this new test exercises the
concurrent-normal-SELECT path that the same fix has to keep stable.

The test runs many parallel `SELECT`s (both plain and `ORDER BY c0`)
against an `IcebergLocal` table while a writer thread produces new
snapshots, then asserts that the
"Can't extract iceberg table state" `LOGICAL_ERROR` never appears in
captured stderr and that the table remains readable.
il9ue pushed a commit to il9ue/ClickHouse that referenced this pull request May 20, 2026
…sh-93278

Fix crash when reading Iceberg table through materialized view
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Issue ClickHouse#93278 (reopened): `IcebergMetadata::iterate` and
`IcebergMetadata::isDataSortedBySortingKey` throw `LOGICAL_ERROR`
"Can't extract iceberg table state from storage snapshot" when
`datalake_table_state` is missing from the storage metadata snapshot
(STID 2606-4a47, 11 master + 45 PR hits over 30 days).

The PR's fix in `StorageObjectStorageSource::createFileIterator` and
`ReadFromObjectStorageStep::requestReadingInOrder` re-fetches the
snapshot from the configuration when it is missing. PR ClickHouse#101577 already
covered the materialized-view-target path
(`04077_iceberg_mv_select_crash`); this new test exercises the
concurrent-normal-SELECT path that the same fix has to keep stable.

The test runs many parallel `SELECT`s (both plain and `ORDER BY c0`)
against an `IcebergLocal` table while a writer thread produces new
snapshots, then asserts that the
"Can't extract iceberg table state" `LOGICAL_ERROR` never appears in
captured stderr and that the table remains readable.
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.

Logical error: Can't extract iceberg table state from storage snapshot for table location

5 participants