Repository navigation
Fix LOGICAL_ERROR on lambda over an Array(Tuple(...)) - #122345
Conversation
|
Workflow [PR], commit [73e4ef0] Summary: ✅
AI ReviewSummaryThis PR fixes the main analyzer bug behind Findings❌ Blockers
Final VerdictChanges requested — the new LLVM Coverage ReportMeasured on commit 73e4ef0.
Changed lines: Changed C/C++ lines covered: 48/51 (94.12%) · Uncovered code |
Array(Tuple(...))
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes83 object files changed (+91.44 KiB total), 0 added.
716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units1281 translation units recompiled, 7869 s compile time in total, 1281 of them have a recent master baseline. |
|
Backport labels have been automatically added based on https://github.com/clickhouse/clickhouse-core-incidents/issues/2055 Labels added:
These labels ensure backport PRs are properly created for each affected release version. The release automation will create backport PRs as needed based on these labels. |
|
I think there is a problem with this fix, it is too general and affects regular views as well, which then only see the currently pushed block. See the following pastila: https://pastila.clickhouse.com/?0565bd58/f5725cb24012675a170ff5a8d5c59081#MDg0dc6STAU7lDH0pmlsQQ==GCM |
There was a problem hiding this comment.
See my comment above. Also I'm not sure the test is very robust, since it relies on timing, but this test could be running slowly on a sanitizer run, claude has a proposal to fix it:
Fix: poll until SELECT count() FROM dst is at least 1 before running the ALTER, which guarantees the INSERT started with the old type. Also add enough blocks to leave margin after the ALTER, e.g. more rows or a longer sleepEachRow. Also
check that the test fails on master; there is no evidence of that in the PR.
Address review: resolving the view source during analysis applied to every context carrying it, including the inner query of an ordinary `VIEW` referenced from the materialized view query. Such a view then read only the inserted block instead of the whole table. Skip the early resolution when `isViewInnerQuery` is set, which matches how the inner query of a view resolved the table before. Add `05291_mv_ordinary_view_over_source_reads_table` for that case, and make `05241_mv_insert_alter_source_column_during_insert` wait until the first block has passed through the view before running the `ALTER`, with more blocks left after it, so it does not depend on timing on slow builds. Without the fix the test fails with `Cannot capture column 3 because it has incompatible type`. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
🕵 @george-larionov, thanks, both points were right. Fixed in 88420bb. Regular views. Reproduced: with an ordinary Test robustness. |
|
I will add a local fix for that, but this consideration makes the test much more complex than the initial quick fix because: The initial version bound the view source through the Context, which every nested analysis shares, so views and other storages that analyze their own query saw the block too. The binding must belong to the one analysis that received the storage. But there is not any per-analysis object carrying a storage. What I could do is to threaded through |
The `view_source` rewrites of the right-hand side of `IN` (`resolveFunction`) and of scalar subqueries (`evaluateScalarSubqueryIfNeeded`) now skip the inner query of an ordinary view (`isViewInnerQuery`), like the table resolution in `IdentifierResolver::tryResolveTableIdentifier`. Otherwise an ordinary view referenced from a materialized view query saw only the pushed block in those subqueries instead of the table. Address review: ClickHouse#122345 (comment) Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
I think this is the better fix, because currently we are playing whack-a-mole with The issue is whether the proposed QueryAnalyzer fix can be backported? |
…the table With `analyzer_inline_views = 1`, `QueryAnalyzer::inlineViewSubqueryIfNeeded` resolves the view body with `StorageView::getViewSubqueryContext`, which did not mark the context as the inner query of a view, so the body inherited `view_source` and resolved the source table of a materialized view to the inserted block. Mark that context with `setIsViewInnerQuery`, like `getViewContext` does for a view that is not inlined. `replaceStorageInQueryTree` also swapped the storage in every table reference of the join tree, including those inside an inlined view body. It no longer descends into queries of a view that is inlined into a query outside of views. Address review: ClickHouse#122345 (comment) Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
🕵 Pushed db55019 for the AI review Major ( @george-larionov, I confirmed the local shard case you suspected. On the current head, with a local server on port 19377: CREATE TABLE src (x UInt64) ENGINE = MergeTree ORDER BY x;
INSERT INTO src SELECT number + 100 FROM numbers(10);
CREATE TABLE dst (block_rows UInt64, r UInt64) ENGINE = MergeTree ORDER BY tuple();
CREATE MATERIALIZED VIEW mv TO dst AS SELECT count() AS block_rows,
(SELECT count() FROM remote('127.0.0.1:19377', currentDatabase(), src)) AS r FROM src;
INSERT INTO src VALUES (1), (2), (3);
SELECT * FROM dst; -- 3 3, the table has 13 rowsThe local replica's plan is analyzed with a copy of the context that carries |
…a materialized view query
`Context::createCopy` preserves `view_source`, so `createLocalPlan` analyzed the local shard of a
`remote` or `Distributed` read inside a materialized view query with the inserted block standing in
for the source table: `(SELECT count() FROM remote('127.0.0.1', db, src))` counted the block, not
the table. Before the early resolution in `IdentifierResolver::tryResolveTableIdentifier`, the swap
was done only on the view query's own tree, so the local shard read the table, like the remote
shards do. Clear the view source in the local shard's context.
New test `05294_mv_source_remote_local_shard_reads_table`: without the fix it prints `0` rows
for both reads, with the fix `10`.
Addresses review: ClickHouse#122345 (comment)
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
I actually agree about the per-analysis binding is better, but it is pretty big (I already have that in a local branch)
IMHO this version keeps the invariant simple:
So, What if we merge this for the incident and backport + to do the per-analysis binding as a follow-up on master, where it can replace these guards. WDYT @george-larionov and @alexey-milovidov ? |
|
@Ergus I agree with your plan, let me do a final review of the new changes. |
Backport #122345 to 26.9: Fix LOGICAL_ERROR on lambda over an `Array(Tuple(...))`
Backport #122345 to 26.8: Fix LOGICAL_ERROR on lambda over an `Array(Tuple(...))`
…an `Array(Tuple(...))`
…an `Array(Tuple(...))`
Backport #122345 to 26.7: Fix LOGICAL_ERROR on lambda over an `Array(Tuple(...))`
Cherry pick #122345 to 26.3: Fix LOGICAL_ERROR on lambda over an `Array(Tuple(...))`
Backport #122345 to 26.3: Fix LOGICAL_ERROR on lambda over an `Array(Tuple(...))`
- `ANY INNER JOIN` keeps an arbitrary one of the matching rows, so `t.v` of the "ANY join" case differed between runs, and between the runs with and without the rewrite. Do not select it; the subquery value is the same for all of them. - Since ClickHouse#122345, a materialized view resolves every reference to its source table, also in a subquery, to the inserted block. Update the reference: the results with and without the rewrite are still equal. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=121848&sha=e46d7ff8b9d57742d8755c72bbbc6c3d0e0b5abc&name_0=PR PR: ClickHouse#121848 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…an `Array(Tuple(...))`
Since ClickHouse#123514, `tryResolveTableIdentifier` resolves every reference to the source table of a materialized view, outside view inner queries, to the catalog storage with the view source metadata (columns only, no primary key), relying on `replaceStorageInQueryTree` to swap the view source in after the passes. That swap only walked the join trees, so a reference in a correlated subquery, as the right side of `IN` (`k IN src`), or in a non-leftmost table of an `IN` subquery was planned as a `MergeTree` read with that metadata, and `KeyCondition` dereferenced the missing primary key expression: an INSERT into the source table crashed the server. When the storage being swapped in is the context's view source, walk the whole query tree, including subqueries in expressions, still stopping at view inner queries and table function arguments. These references read the pushed block again, as with ClickHouse#122345 and the old analyzer. Other callers (Buffer, header computation) keep the join-tree walk. Closes ClickHouse#124647 Co-Authored-By: Claude Opus 5.5 <[email protected]>
The analyzer typed the source table of a materialized view by the view source in every analysis whose context has one (`tryResolveTableIdentifier`), but only an interpreter given the view source as storage swaps it in after the passes. Any other interpreter that re-analyzes the table under a copy of the push context planned the catalog `MergeTree` with key-less metadata and crashed in `KeyCondition`: `merge` with a `Distributed` sibling, `loop`, the input query of `executable`. `InterpreterSelectQueryAnalyzer` now marks its context with `setViewSourceSubstituted` when it is given the view source as storage, and `tryResolveTableIdentifier` takes the types from the view source only under that mark. Other analyses type the table from the catalog and read it, as before ClickHouse#122345: measured on 26.8.2.7, `merge`, `loop` and `executable` inside a view query read the table, while an IN or scalar subquery of the view query reads the inserted block even when `merge` pushes it into its child. The view source rewrite of an IN subquery now replaces every reference to the source table in it, not only the leftmost one, so a join inside such a subquery reads the block too when `merge` pushes it into its child, like it does in the view query itself. This replaces the per-site storage arguments for `merge`, `loop` and `executable`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Backport labels have been automatically added based on https://github.com/clickhouse/clickhouse-core-incidents/issues/2055 Labels added:
These labels ensure backport PRs are properly created for each affected release version. The release automation will create backport PRs as needed based on these labels. |
Related: #109747
The query of a materialized view is analyzed per pushed block. The analyzer resolved the source table through the catalog and swapped in the
StorageValuesholding the block only after the passes, so the tree was typed by the table's current metadata while the data kept the type from the start of theINSERT. AnALTER ... MODIFY COLUMNcommitted in between made a lambda capture a column of another type.IdentifierResolver::tryResolveTableIdentifiernow resolves the view source directly, so the view query is typed by the block it processes.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
LOGICAL_ERRORCannot capture column N because it has incompatible typewhen a materialized view with a lambda over anArray(Tuple(...))column receives a block while the source table's column type is being changed byALTER TABLE ... MODIFY COLUMN.Workflow [PR]
Sync PR [sync-upstream/pr/122345]
Version info
26.10.1.1164-master(included in26.10and later)26.9.8.11,26.8.15.24,26.7.20.24,26.3.41.4