Repository navigation
Fix a crash when an IN subquery of a standalone expression reads a Distributed table - #121639
Conversation
…e analyzer A standalone expression - a row policy, a `CHECK` constraint, a `TTL` expression - is compiled by `ExpressionAnalyzer`, and `ActionsMatcher::makeSet` built the plan of an `IN` subquery in it with the interpreter that preceded the analyzer. `StorageDistributed` resolves its read through a query tree and a planner context that interpreter never builds, and dereferences them unconditionally, so reading a `Distributed` table from such a subquery crashed the server. `IInterpreterUnionOrSelectQuery` used to set `allow_experimental_analyzer = 0` on the interpreter's own context, and the storages read under it took their AST arm. #120311 removed the assignment together with the arms it protected, which is what uncovered the dereference. It is the same defect as #121464, reached through a second producer: #121418 replaces the one in `ExecuteScalarSubqueriesVisitor`, which accounts for 27 of the 29 occurrences of the two sibling STIDs in the CI database over the last five days; the other two come through `interpretSubquery`, and the STID does not tell them apart because it is computed from the top of the stack, which both share. `makeSet` now builds the plan with `InterpreterSelectQueryAnalyzer`. What `interpretSubquery` does around it - a table name or a table function becomes `SELECT <columns> FROM it`, and the subquery gets its own context with the whole-query result limits lifted - is shared as `prepareSubquery` rather than duplicated. `makeSet` already built a query tree of the same subquery for its set key, so the analyzer was on this path before this change. Report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=e96f004b984ced9a97237e99b3e744599a917870&name_0=MasterCI&name_1=Stress%20test%20%28arm_tsan%29 Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [2a710f7] Summary: ✅
AI ReviewSummaryThis PR correctly moves standalone-expression Findings
Final Verdict
LLVM Coverage ReportMeasured on commit 2a710f7.
Changed lines: Changed C/C++ lines covered: 30/34 (88.24%) · Uncovered code |
Build profile diff (arm_release)Commit See the job log for details. |
…queries-via-analyzer
…constraint `ActionsMatcher::makeSet` reroutes both an explicit subquery and a bare table name (`x IN in_subquery_dist`, which stands for `SELECT * FROM` it) through the analyzer; the test covered only the former. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
🕵 Both |
…queries-via-analyzer
…tributed` table The `TTL` builder is another standalone-expression caller that builds `IN` sets through `ExpressionAnalyzer`; add a smoke case that applies such a `TTL` in a merge. The subquery of a `TTL` is resolved outside of the current database (same on 26.7), so the table is qualified. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…queries-via-analyzer Brings in #121418, which fixes the `AST fuzzer (amd_debug, targeted)` red `Segmentation fault (STID: 3422-39ed)` on 983463d (the scalar-subquery producer through `ExecuteScalarSubqueriesVisitor`): https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=121639&sha=983463d6ae29e47625c100c134e9f40c822a41e8&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29 PR: #121639 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
🕵 The only red on 983463d, |
…queries-via-analyzer
…on with parallel replicas `makeSet` now plans the subquery with `InterpreterSelectQueryAnalyzer`, which treats it as a query of its own and applies parallel replicas when the default profile enables them. A background merge that evaluates `TTL ... WHERE x IN (SELECT ...)` then read the set's subquery through parallel replicas, so the set was no longer built on the merge's own thread and `05215_ttl_delete_vertical_merge_set_built_once` read `SetsBuiltFromSubquery = 0` in the `ParallelReplicas` stateless job. The interpreter never used parallel replicas for such a subquery; disable them, as `ExecuteScalarSubqueriesVisitor` already does. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=121639&sha=e6477cfc10015c7076122c316fa9890ef911ac81&name_0=PR&name_1=Stateless%20tests%20%28amd_llvm_coverage%2C%20ParallelReplicas%2C%20s3%20storage%2C%20parallel%29 #121639 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…yzer-built `IN` subquery `InterpreterSelectQueryAnalyzer` re-applies a subquery's own `SETTINGS` clause over the context it is handed, so a persisted `TTL ... WHERE x IN (SELECT ... SETTINGS enable_parallel_replicas = 1)` turned parallel replicas back on and built the set off the merge thread. Strip both setting names from a clone of the subquery AST, as `ExecuteScalarSubqueriesVisitor::getQueryInterpreter` does. Addresses review feedback on #121639 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
📊 Cloud Performance Report ✅ AI verdict: No performance change can be attributed to this PR. The only server code it changes is how the older expression machinery builds the set for an IN subquery: it now uses the analyzer, which matters for CHECK constraints, TTL expressions and row policies that read Distributed or Merge tables. ClickBench Q30 (+30.8%) and Q31 (+27.8%) are GROUP BY aggregations with no IN subquery, so they never run that code. Their CPU time is flat and both medians fall inside master's recent range, so marking them as within the normal range is correct and they should not be treated as regressions. clickbenchFlagged queries (2 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 No significant changes Debug info
|
|
🕵 Merged master (88e4e48) to clear the |
|
🕵 The only red on 88e4e48, |
… subquery `interpretSubqueryWithAnalyzer` now passes `getViewSource` to `InterpreterSelectQueryAnalyzer`, so under a materialized view the subquery reads the inserted block instead of the catalog table, as `ExecuteScalarSubqueriesVisitor` does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…er a materialized view A row policy with `x IN (SELECT x FROM src)` is applied while a `Merge` table reads its child inside the query of a materialized view over `src`. The source is a `Null` table, so the set contains the inserted rows only if the subquery reads the view source rather than the catalog table. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The flush context of an asynchronous insert has no current database, so the unqualified table in the row policy was resolved in the default database and the test failed in `Stateless tests (amd_llvm_coverage, AsyncInsert, s3 storage, parallel)`: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=121639&sha=latest&name_0=PR&name_1=Stateless%20tests%20(amd_llvm_coverage%2C%20AsyncInsert%2C%20s3%20storage%2C%20parallel) #121639 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
🕵 The only red on 164b97c was the new test |
…te in PREWHERE `EXPLAIN AST optimize = 1` analyses the query with the interpreter that preceded the analyzer, and `appendPrewhere` builds the set of an `IN` subquery in `PREWHERE`. Before ClickHouse#121639 that set was planned without a query tree, so reading `remote` through `StorageDistributed::read` dereferenced a null `PlannerContext`. None of ClickHouse#121639's tests goes through `EXPLAIN`, so this adds the minimal case from ClickHouse#117253. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117253&sha=546cd65f39602b759209f1649d527c047d1bc250&name_0=PR&name_1=AST%20fuzzer%20%28amd_release%2C%20oracle%29 Related: ClickHouse#121639 Related: ClickHouse#117253 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…t table The sharding key is compiled once in the StorageDistributed constructor. An IN over a subquery or over a non-Set table registers a FutureSetFromSubquery there, and nothing ever builds it for table metadata, so every consumer of the key sees an unready set: an INSERT fails in DistributedSink::createSelector with "Not-ready Set is passed as the second argument for function 'in'" (LOGICAL_ERROR), and the optimize_skip_unused_shards path folds the key to a wrong constant and prunes to the wrong shard (26.8 and 26.3 return 0 instead of 1 for a point query on such a table). Reject such a key with BAD_ARGUMENTS right after it is built. A tuple or Set-table IN is a ready set and keeps working. As for arrayJoin (be38893), only a definition stated now is checked (is_fresh_definition): stored tables still load and ALTER does not revalidate. The check runs after the build so a missing table in the key still reports UNKNOWN_TABLE (02111_global_context_temporary_tables). Found by BuzzHouse (amd_tsan) on ClickHouse#117004, where the INSERT into remote() with c0 GLOBAL NOT IN <Alias of a Remote table> segfaulted before ClickHouse#121639 and fails with this LOGICAL_ERROR after it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #121464
Related: #121418
Related: #120311
Closes: #121956
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Description
Stress test (arm_tsan)on master,Segmentation fault (STID: 1635-49b2):report.
This is the second producer of the null dereference reported in #121464. #121418 fixes the one that
reaches it through
ExecuteScalarSubqueriesVisitor- 27 of the 29 occurrences of the two sibling STIDsin the CI database over the last five days. The two remaining ones, including the report above, come
through
interpretSubqueryinstead, which that PR does not touch: the STID is computed from the top ofthe stack, which both producers share, so the two are not distinguishable by it.
An
INsubquery of a standalone expression - a row policy, aCHECKconstraint - is compiled byExpressionAnalyzer, whoseActionsMatcher::makeSetbuilt the subquery's plan with the interpreter thatpreceded the analyzer.
StorageDistributedresolves its read through a query tree and a planner contextthat interpreter never builds, and dereferences them unconditionally.
Until #120311 this was hidden rather than absent:
IInterpreterUnionOrSelectQuery's constructor setallow_experimental_analyzer = 0on the interpreter's own context, and the storages read under it tooktheir AST arm. That PR removed the assignment together with the arms it protected. The defect is
therefore master-only: the statement in the test below reaches the shard read on 26.8 and 26.9 and
segfaults on master, which is why there is no changelog entry.
makeSetnow builds the plan withInterpreterSelectQueryAnalyzer. WhatinterpretSubquerydoes aroundthat - a table name or a table function becomes
SELECT <columns> FROM it, the subquery gets its owncontext with the whole-query result limits lifted - is shared rather than duplicated, as
prepareSubquery.makeSetalready built a query tree of the same subquery for its set key, so the analyzer was on thispath before this change.
With this and #121418, nothing hands the interpreter that preceded the analyzer a nested
SELECTof auser's query anymore.
interpretSubquery's two other callers -buildJoinedPlanandGlobalSubqueriesVisitor- are reached fromInterpreterSelectQueryonly, and the one construction siteof that interpreter left outside its own recursion is
ProjectionsDescription, which is exactly what#120311 kept it for: a synthetic
SELECTover a source built in place.The
CHECKconstraint half of the new test segfaults the build of this commit without the change(
clickhouse local, exit 139); the row-policy half is the stack of the report above. 425 tests ofthe local stateless families that reach
makeSet- row policies, constraints,GLOBAL IN, preparedsets, mutations,
Mergetables - pass with the change; the 13 that fail on my machine all reach theneighbouring server on this host through a port or a path the test hardcodes, or need object storage.
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/121639]
Version info
26.10.1.1795-master(included in26.10and later)