Repository navigation
Leave a correlated subquery alone when it is resolved from the cache - #118321
Conversation
A correlated scalar subquery is no scalar the analyzer can evaluate - the
planner decorrelates it - so where the analyzer resolves such a subquery for
the first time it skips the scalar evaluation. The path that answers from the
already-resolved-expression cache did not, and the second occurrence of one
subquery in a single expression went through it:
SELECT o.id, if(1 = 1, (SELECT count() FROM t AS i WHERE i.id = o.id),
(SELECT count() FROM t AS i WHERE i.id = o.id))
FROM t AS o SETTINGS allow_experimental_correlated_subqueries = 1;
Code: 36. Cannot evaluate correlated scalar subquery
The same subquery under a non-constant condition, or added to itself, resolves
without hitting that path and always worked. The cached path now applies the
same test as the first resolution does.
Closes: #111937
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [9d69c58] Summary: ✅
AI ReviewSummaryThis PR fixes the cache-hit path in Final Verdict✅ No blocking issues found. LLVM Coverage ReportMeasured on commit 9d69c58.
Changed lines: Changed C/C++ lines covered: 7/10 (70.00%) · Uncovered code |
Build profile diff (arm_release)Commit See the job log for details. |
…ubquery-twice-111937
Correlated subqueries exist only in the analyzer, so in the `old analyzer` job the test failed with `Missing columns: 'o.id'` from `ExecuteScalarSubqueriesVisitor`. Set `enable_analyzer` in the test, as the sibling correlated-subquery tests do, so it keeps exercising the analyzer path there instead of being skipped. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
🕵 The two AST fuzzer failures on
@groeneai, could you take a look at the oracle mismatch and provide a fix in a separate PR if the existing issue does not already cover it? Please link it here. The |
|
Acknowledged, I am investigating the One measured note so the coverage question is not left implicit: the failing statement is |
|
On the coverage question: #117075 and its fix #119367 do not cover this one. The failing statement is What I have so far on the mismatch itself, so the state is not misread as unowned: it does not reproduce on master. Replaying the fixture with the 569 non-default session settings from the job's own dump, both arms and I will open a separate PR against master and link it here. |
|
On the mismatch itself: it is a real wrong-results bug, and it is already fixed on master by your #116381, which merged about 90 minutes after this run. So no separate PR from me, and I am withdrawing the one I said I would open. What the arm actually returned, from the job's own Mechanism, since it is not the obvious one. Timing and CIDB agree with that. One thing that may be worth your time: this text-index symptom is a second, independent consequence of that root cause, and |
…ubquery-twice-111937
|
🕵 @groeneai, thanks for the thorough root cause. Agreed that it is the double- Yes, please send the test-only PR asserting the Status of this PR: the two remaining reds on |
|
Acknowledged. It will be test-only, with no |
`AST fuzzer (amd_release, oracle)` on ClickHouse#118321 reported an `Identity WHERE (NOT(NOT p))` mismatch seeded from `02346_text_index_partially_materialized.sql`: `hasPhrase(text, 'ello50 wor')` counted 30 rows while `NOT (NOT hasPhrase(text, 'ello50 wor'))` counted 0. `hasPhrase` with two arguments tokenizes with `splitByNonAlpha`, under which `'ello50 wor'` is not a phrase of `'hello50 world50'`; the index tokenizer reaches the row-level function only as a third argument injected by `processTextIndexFunction`. Because the index declares `support_phrase_search = 0`, `getDirectReadMode` returns `Hint`, the only mode that keeps the original predicate beside the index virtual column, so that injection is observable. Before ClickHouse#116381, index analysis cancelled a pair of `not` unconditionally, so `not(not(hasPhrase(...)))` canonicalised to the bare atom, the outer `not` node became the replacement key, and `replaceNodes` returned early on it without descending into its children, so the injected three-argument call never reached the executed predicate. `EXPLAIN actions = 1` shows both sides: before Filter column: __text_index_idx_hasPhrase_5d43... AND NOT NOT hasPhrase(text, 'ello50 wor') after Filter column: NOT NOT (__text_index_idx_hasPhrase_5d43... AND hasPhrase(text, 'ello50 wor', 'ngrams(3)')) ClickHouse#116381 fixed this on master by blocking only the unsound half of the cancellation, so this commit adds no source change. `05043_not_in_value_position_key_condition` pins the index-analysis consequence of that fix; the direct-read rewrite had no test of its own. `hasAnyTokens` and `hasAllTokens` take the same injected tokenizer argument, but over a plain text index `getDirectReadMode` gives them `Exact` mode, which drops the original predicate, so this seam is reachable only for a non-positional `hasPhrase` and no sibling arm is added. With the one line of ClickHouse#116381 reverted locally, `not not p` and `prewhere not not p` report 0 instead of 10 (20 of 20 randomized runs fail); on master 50 of 50 randomized runs pass. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118321&sha=518eff241122633d224affa709900640c1544c07&name_0=PR&name_1=AST%20fuzzer%20%28amd_release%2C%20oracle%29 Related: ClickHouse#118321 Related: ClickHouse#116381 Co-Authored-By: Claude Opus 5 <[email protected]>
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118321&sha=d13d44ffd9c9a4266b597efc01539ad75b751bf0&name_0=PR&name_1=Integration%20tests%20%28amd_tsan%2C%202%2F6%29 ( |
|
Investigated. This is the EINTR signature that your own #120202 fixed, observed on a tree that predates it, so there is no new PR from me.
Your master merge already moved the pin to |
A correlated scalar subquery is no scalar the analyzer can evaluate - the planner decorrelates it - so where the analyzer resolves such a subquery for the first time it skips the scalar evaluation. The path that answers from the already-resolved-expression cache did not, and the second occurrence of one subquery in a single expression went through it:
The same subquery under a non-constant condition, or added to itself, resolves without hitting that path and always worked. The cached path now applies the same test as the first resolution does.
Closes: #111937
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Fixed
Cannot evaluate correlated scalar subqueryfor a query that uses one correlated scalar subquery twice in the same expression, for example in both branches ofif(1 = 1, ..., ...).Workflow [PR]
Sync PR [sync-upstream/pr/118321]
Version info
26.10.1.736-master(included in26.10and later)