Repository navigation
Fix crash when reading Iceberg table through materialized view - #101577
Conversation
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]>
|
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 |
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 timeb) Root cause explained? c) Fix matches root cause? Yes — two complementary fixes:
d) New tests added? Yes — 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 ( |
|
Workflow [PR], commit [0e2dfe6] Summary: ⏳
AI ReviewSummaryThis PR fixes a real ClickHouse Rules
Final Verdict
|
| 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. |
There was a problem hiding this comment.
Aren't we fixed the same problem with two different methods? Isn't it enough to call storage->updateExternalDynamicMetadataIfExists(context); in storage materialized view?
There was a problem hiding this comment.
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.
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.
LLVM Coverage Report
Changed lines: 100.00% (5/5) · Uncovered code |
|
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. |
be3373d
|
Hi @groeneai @alesapin — the changelog category for this PR might need a look. Current: 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. |
|
Hi — this PR may need backporting to release branches 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 |
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.
Antalya 26.3 Backport of ClickHouse#101577 - Fix crash when reading Iceberg table through materialized view
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.
…sh-93278 Fix crash when reading Iceberg table through materialized view
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.
Changelog category (leave one):
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 theIcebergLocalengine, the server crashes with:Root cause:
StorageMaterializedView::read()delegates to the target tablewithout calling
updateExternalDynamicMetadataIfExists()first. This call isnormally 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 hasdatalake_table_stateset, triggeringthe
LOGICAL_ERRORassertion.Fixes (defense in depth):
StorageMaterializedView::read()— callupdateExternalDynamicMetadataIfExists()on the target table beforeobtaining its metadata snapshot. This ensures proper snapshot pinning for
datalake target tables, consistent with how the analyzer handles direct table
references.
IcebergMetadata::iterate()/isDataSortedBySortingKey()— instead ofcrashing when
datalake_table_stateis absent, fall back to the currenttable state via
getRelevantState(). This guards against any other code paththat might skip the metadata update.
Closes #93278
Test plan
04077_iceberg_mv_select_crashthat reproduces theexact crash from Logical error: Can't extract iceberg table state from storage snapshot for table location #93278
empty SELECT and SELECT after INSERT both work correctly
🤖 Generated with Claude Code
Version info
26.4.1.584