Repository navigation
Conversation
…uery context To find the tables a new table or view depends on, `DDLDependencyVisitor` evaluates a non-literal argument (the cluster name of `cluster`, the database or table of `Buffer`/`Distributed`, the name in `dictGet`/`joinGet`) with `evaluateConstantExpressionOrIdentifierAsLiteral` on a copy of the global context, and the analyzer executes a scalar subquery inside such an argument. That copy was never made a query context, and `Context::makeQueryContext` is the only place that creates the per-query join runtime filter lookup, so the copy had none. A `JOIN` in the subquery registers its runtime filter for index analysis on the probe-side `ReadFromMergeTree`, whose read-time predicate builder dereferences the lookup of its own step context, a descendant of that copy: SIGSEGV in `RuntimeFilterLookup::find` (STID 2771-4f2b, found by the stress test on a fuzzed `cluster(view(SELECT ... JOIN ...), '<cluster>')`). The build side and `__applyFilter` take the lookup of the thread's query context (the CREATE query), which is why only the probe read crashed. Reachable on 26.9 with `enable_join_runtime_filters_index_analysis = 1` in the default profile, and on master with stock settings since ClickHouse#121859. Make the copy a query context, as `ClickHouseDictionarySource` and SQL SECURITY DEFINER views already do for the queries they run on a copy of the global context. Which arguments are evaluated does not change: a view whose `cluster` name is computed by a scalar subquery keeps its dependency, and the new test checks it. The dropped comment claimed subqueries are not executed here, which was not true. The scalar subquery of a `TTL ... WHERE` predicate runs on another copy of the global context that is not a query context either (see ClickHouse#121418), and a `JOIN` there still reaches the same read. That path is not changed here. Inside the evaluated subquery the probe read now looks the filter up in the new, empty lookup and skips the pruning, which does not change results. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 2 findings over a full pass and a delta recheck) plus my own review; 1 fix round.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-10:20260926-045600 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-40:20260926-015800 |
|
Workflow [PR], commit [0ad8ea1] Summary: ❌
AI ReviewSummaryThis PR fixes the Missing context / blind spots
Final Verdict✅ No new blocking or major issues found in the current patch. LLVM Coverage ReportMeasured on commit 0ad8ea1.
Changed lines: Changed C/C++ lines covered: 6/6 (100.00%) · Uncovered code |
CI finish ledger - 0ad8ea1Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
All 176 check runs completed (133 pass, 39 not selected, 4 red) and Session id: cron:our-pr-ci-monitor:20260926-143040 |
The previous commit fixed one producer of a query plan whose read step has no join runtime filter lookup, the scalar subquery of a `TTL` or `CHECK` expression. The analyzer's own scalar subquery evaluation has the same problem whenever `evaluateConstantExpression` runs on the global context, for example for the arguments of the table function of `CREATE TABLE ... AS <table function>` (`ITableFunction::execute` with `use_global_context`) and in the dependency analysis of `CREATE` (ClickHouse#122390). This crashes the server in `buildRuntimeRangePredicate` (`p` is a MergeTree table ordered by `k`): CREATE TABLE t AS numbers(assumeNotNull((SELECT count() FROM p, b WHERE p.k = b.k))); The two halves of a join runtime filter disagreed on where they meet: the build side (`BuildRuntimeFilterStep`), `__applyFilter` and `HashJoin` use the lookup of the thread's query context, while the index analysis of the probe side used the lookup of its read step's context, which is null when the plan was built with a context that never went through `Context::makeQueryContext`. Fix it in the runtime filter code instead of in each producer: * `ReadFromMergeTree` takes the lookup from the thread's query context too, and skips the read-time pruning if the thread has none; * join runtime filters are not added to a plan built on a thread without a query context (table loading, the replication queue), where the build side and `__applyFilter` cannot work at all. Each part is needed: without the first the `CREATE` of the `TTL` table crashes again, without the second loading the tables aborts with `Query context is not available for __applyFilter`. This replaces the change of the previous commit in `ExecuteScalarSubqueriesVisitor`, which is reverted. The analysis contexts are still not query contexts, so a table function in such a subquery is still rejected with `THERE_IS_NO_QUERY`. The test gets two more arms: `CREATE TABLE ... AS numbers(...)`, read again after the database is re-attached, and a view reading through `cluster` with the cluster name computed by such a subquery, whose dependency on the table it reads is checked. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Superseded by #122795: it fixes this case in the join runtime filter code (the read step takes the lookup from the thread's query context), without making the dependency analysis context a query context, and its test has the same |
… JOIN
A scalar subquery with a `JOIN` in a `TTL` expression crashes the server on `CREATE`
(`p` is a MergeTree table ordered by `k`):
CREATE TABLE t (d DateTime, x UInt64) ENGINE = MergeTree ORDER BY tuple()
TTL d + INTERVAL 1 YEAR WHERE x < (SELECT count() FROM db.p AS p, db.b AS b WHERE p.k = b.k);
with a null read in `buildRuntimeRangePredicate`. The same subquery in a `CHECK` constraint is
created fine, but loading the table afterwards (a restart or `ATTACH DATABASE`) aborts with
`Logical error: 'Query context is not available for __applyFilter'`, and so does a replica
applying `ALTER ... MODIFY TTL` of a `ReplicatedMergeTree`, from `ReplicatedMergeTreeQueue`.
Since ClickHouse#121418 `ExecuteScalarSubqueriesVisitor` plans such a subquery with
`InterpreterSelectQueryAnalyzer`, so its `JOIN` gets join runtime filters. They rendezvous through
a per-query `IRuntimeFilterLookup` that only `Context::makeQueryContext` creates: the index analysis
of the probe side reads the lookup of its read step's context, the build side and `__applyFilter`
read the query context of the current thread. A `TTL` expression (`registerStorageMergeTree`, and
`ReplicatedMergeTreeTableMetadata::Diff::getNewMetadata` on a replica) and a `CHECK` constraint of a
table being loaded (`DatabaseOnDisk`) are analysed with a copy of the global context, which is not a
query context, and the table loader and replication threads run no query. A `CHECK` constraint at
`CREATE` is fine because it is analysed with the context of the `CREATE` query.
The subquery keeps the context it is analysed with and gets what the join runtime filters need:
* a thread with no thread group is attached to a query by a `QueryScope`: the query of that
context if it has one, otherwise a copy of the context made a query context. The scope is
created before the interpreter, because the analyzer already executes nested scalar subqueries
in its constructor, and `getQueryInterpreter` returns that query together with the interpreter,
because every other reference to a query context is weak (`Context::query_context`,
`ThreadGroup`) and it has to outlive the pipeline;
* it takes the filter lookup of the current thread's query context, the one the build side and
`__applyFilter` use.
Each part is needed: without the lookup the `CREATE` crashes again, without the scope or without
keeping its query alive the load aborts again, and with the scope created only after the
interpreter a `JOIN` in a nested scalar subquery still aborts the load.
The analysis context itself is not made a query context: that would also make table functions
resolve in the subquery (`QueryAnalyzer::resolveTableFunction` needs a query context), and a table
function in such a subquery is still rejected with `THERE_IS_NO_QUERY`, as before.
26.8 and 26.9 run these subqueries with the old interpreter, without join runtime filters, so this
is master-only. Found in the review of ClickHouse#122390, which fixes the other producer of a plan without a
lookup, the dependency analysis of `CREATE`.
The new test has one arm per carrier: `TTL ... WHERE` at `CREATE`, `CHECK` at load, a `JOIN` only in
a nested scalar subquery of a `CHECK`, and a replicated `MODIFY TTL` in an `Atomic` and in a
`Replicated` database. Each arm alone kills or aborts the unfixed server. Two more arms check that a
table function in such a subquery is still rejected, and that at `CREATE` the `TTL` subquery built a
join runtime filter and the index analysis of its read used it (`RuntimeFiltersCreated`,
`RuntimeFilterGranulesConsidered`). There is no `TTL ... GROUP BY ... SET` arm: query parameters are
not substituted in its assignments, and a TTL is not analysed in the database of the statement.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #96844
Related: #121859
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a server crash on
CREATEorATTACHof a table or view when an argument that the table dependency analysis evaluates, such as the cluster name ofcluster, the database or table ofBufferor the dictionary name ofdictGet, is a subquery with aJOINandenable_join_runtime_filters_index_analysisis enabled in the default settings profile or in the subquery's ownSETTINGS.Description
On 26.9 with
enable_join_runtime_filters_index_analysis = 1in the default settings profile or in the subquery's ownSETTINGS(and on master with stock settings, since #121859 turned it on), creating a view that reads throughcluster()with a cluster name computed by a scalar subquery with aJOINcrashes the server (ais a MergeTree table ordered byk):Found by
Stress test (arm_release)(job, report), where the AST fuzzer swapped the arguments ofcluster(view(SELECT ... JOIN ...), '<cluster>').To find the tables a new table depends on,
DDLDependencyVisitorevaluates such arguments on a copy of the global context, and the analyzer executes a scalar subquery inside them. That copy is not a query context, so it has no join runtime filter lookup (onlyContext::makeQueryContextcreates one). TheJOINregisters its runtime filter for index analysis on the probe-sideReadFromMergeTree, whose read-time predicate builder dereferences the lookup of its own context: null.The copy is now made a query context, like other places that run a
SELECTon a copy of the global context (SQL SECURITY DEFINER views, ClickHouse dictionary sources). Every argument evaluation of the dependency analysis goes through this one place, and which arguments are evaluated does not change. AJOINin the scalar subquery of aTTL ... WHEREpredicate reaches the same read through another context without a lookup (see #121418) and is not covered here.The new test crashes the server without the fix and passes with it (50 randomized runs, 20 more with a
Replicateddatabase). The existing dependency tests andtest_backup_restore_new::test_tables_dependencypass.Workflow [PR]
Sync PR [sync-upstream/pr/122390]