Repository navigation
Fix LOGICAL_ERROR crash with subqueries in ROW POLICY USING clause - #101263
Conversation
Pre-PR Validation GateSession: a) Deterministic repro: CREATE ROW POLICY p ON t USING (SELECT 1) TO ALL;
SELECT 1 FROM t; -- crashes with LOGICAL_ERROR every timeb) Root cause: c) Fix matches root cause: d) New test added: e) Both directions:
|
|
Workflow [PR], commit [f8619e7] Summary: ✅ AI ReviewSummaryThis PR fixes a Missing context
Findings
Tests
ClickHouse Rules
Final Verdict
|
| SELECT count() FROM t_04073; | ||
| DROP ROW POLICY p_04073 ON t_04073; | ||
|
|
||
| -- Test 3: Mixed expression with scalar subquery — c0 > (SELECT 1) |
There was a problem hiding this comment.
The code change updates both QueryTreeNodeType::QUERY and QueryTreeNodeType::UNION, but this test file exercises only QUERY-based scalar subqueries.
Please add one ROW POLICY USING case with a scalar UNION subquery to cover the new UNION branch in QueryAnalyzer::resolve (for example, a scalar UNION ALL wrapped to return one row). Without it, this path can regress unnoticed.
There was a problem hiding this comment.
Good catch — the UNION branch was not only untested, it was actually broken. The buildFilterInfo() path in Utils.cpp skips the NormalizeSelectWithUnionQueryVisitor that runs in the normal executeQuery() path, so UnionNode constructor rejected the un-normalized UNION_DEFAULT mode.
Fixed in 791de84:
- Added
NormalizeSelectWithUnionQueryVisitorinbuildFilterInfo()beforebuildQueryTree()— matches howexecuteQuery.cpphandles it - Added 3 new test cases:
USING (SELECT 1 UNION DISTINCT SELECT 1)— exercises the UNION branchUSING c0 > (SELECT 0 UNION DISTINCT SELECT 0)— mixed expression with UNIONUSING (SELECT 1 UNION ALL SELECT 1)— verifies proper error (not crash) for multi-row result
50/50 passes with full randomization, all existing row policy tests pass.
| { | ||
| /// The node is a scalar subquery used in an expression context | ||
| /// (e.g. ROW POLICY USING (SELECT 1)). Evaluate it as a scalar | ||
| /// subquery — the result replaces the node with a ConstantNode. | ||
| evaluateScalarSubqueryIfNeeded(node, scope); | ||
| break; | ||
| } |
There was a problem hiding this comment.
Agreed — reverted. The assertions are restored. The fix now lives entirely in buildFilterInfo() where the subquery is wrapped with equals(1, <subquery>) before reaching the analyzer.
| /// Normalize UNION modes in the filter expression AST before building | ||
| /// the query tree. Without this, a ROW POLICY USING clause containing | ||
| /// a UNION subquery (e.g. `USING (SELECT 1 UNION ALL SELECT 1)`) would | ||
| /// hit UnionNode's constructor check that rejects UNION_DEFAULT mode. | ||
| { | ||
| const auto & settings = query_context->getSettingsRef(); | ||
| NormalizeSelectWithUnionQueryVisitor::Data data{settings[Setting::union_default_mode]}; | ||
| NormalizeSelectWithUnionQueryVisitor{data}.visit(filter_expression); | ||
| } |
There was a problem hiding this comment.
It is unclear how it would help.
There was a problem hiding this comment.
A more correct approach would be to wrap with equals(1, <subquery>) the filter if the filter expression is a Query or Union node.
There was a problem hiding this comment.
Removed the UNION normalization. Replaced with the equals(1, <subquery>) wrapping as you suggested — it's cleaner and handles the core case directly. The UNION-in-USING test cases were removed since they're out of scope for this fix.
791de84 to
e4850b5
Compare
|
@novikd — reworked in e4850b5 per your review. Here's what changed: Reverted QueryAnalyzer.cpp — the assertions are restored. No more Utils.cpp — equals(1, <subquery>) wrapping: In Removed UNION normalization — the Test coverage: 3 cases remain — Session: cron:clickhouse-ci-task-worker:20260331-131500 |
| if (filter_expression->as<ASTSubquery>() | ||
| || filter_expression->as<ASTSelectWithUnionQuery>()) | ||
| { | ||
| filter_expression = makeASTFunction("equals", |
There was a problem hiding this comment.
❌ This wrapper changes boolean semantics for standalone scalar subqueries.
USING filters are interpreted as boolean expressions where non-zero numeric values are treated as true (e.g. USING 2 behaves as true). Rewriting USING (SELECT x) to equals(1, (SELECT x)) makes only 1 true and turns values like 2 or -1 into false.
That is a user-visible behavior change unrelated to the exception fix. A safer approach is to resolve standalone QUERY/UNION nodes as scalar subqueries (as in resolveConstantExpression) and keep normal boolean conversion semantics.
Please add a regression case like USING (SELECT 2) to ensure this remains true.
There was a problem hiding this comment.
Done in 5f6e7bb. Two changes:
-
Fixed boolean semantics: Replaced
equals(1, <subquery>)withnotEquals(<subquery>, 0). This correctly treats any non-zero value as truthy —USING (SELECT 2)now allows rows instead of denying them. Also handles edge cases:SELECT -1(truthy),SELECT 256(truthy, would fail with UInt8 cast),SELECT 0(falsy). -
Added test case (Test 3):
USING (SELECT 2)— verifies all rows are visible when the subquery returns a non-zero value other than 1.
e4850b5 to
5f6e7bb
Compare
|
@novikd — updated in 5f6e7bb: Fixed boolean semantics — changed The
Added test case for 50/50 passes with full randomization. All existing row policy tests pass. |
5f6e7bb to
12812fc
Compare
|
The test |
|
The MSan stress test failure (MemorySanitizer: use-of-uninitialized-value, STID 4179-5154 or 4148-3044) is a known pre-existing issue unrelated to this PR. Fix: #102158 |
|
The |
|
@groeneai can you check the CI failures? I think this PR can be merged. |
When ROW POLICY USING contains a standalone subquery like (SELECT 1), buildFilterInfo() in Utils.cpp passes the raw AST to buildQueryTree() which produces a top-level QueryNode/UnionNode. QueryAnalysisPass then calls QueryAnalyzer::resolve() with table_expression set, but that function asserts table_expression must be empty for top-level QUERY/UNION nodes — resulting in a LOGICAL_ERROR crash. Fix: in buildFilterInfo(), detect standalone subquery filter expressions (ASTSubquery or ASTSelectWithUnionQuery) and wrap them with notEquals(<subquery>, 0) before passing to buildQueryTree(). This produces a FunctionNode at the top level, so the subquery is resolved through the normal scalar-subquery evaluation path as a function argument. We use notEquals(x, 0) rather than equals(1, x) to preserve boolean semantics: any non-zero value is truthy (e.g. USING (SELECT 2) allows rows, not just USING (SELECT 1)). Closes ClickHouse#100695 Co-Authored-By: Claude Opus 4.6 <[email protected]>
1ba009d to
f8619e7
Compare
|
@PedroTadim — investigated CI on PR #101263. Both failures from the 2026-04-09 run are chronic trunk bugs unrelated to this PR. Failure analysis
Test Action taken
The fix in Once fresh CI completes, the chronic issues may or may not re-appear at random — they're not blockers for merging this PR. |
LLVM Coverage Report
Changed lines: 100.00% (8/8) · Uncovered code |
|
Hi @groeneai @novikd — the changelog category for this PR might need a look. Current: Why: The PR fixes a Could you verify this is correct? Ignore if the current category is intentional. |
When a ROW POLICY uses a scalar subquery as its filter expression (e.g.
USING (SELECT 1)), the query tree builder creates a QUERY node.QueryAnalyzer::resolve()previously rejected QUERY/UNION nodes when atable_expressioncontext was present, throwing a LOGICAL_ERROR crash:The fix evaluates such nodes as scalar subqueries via
evaluateScalarSubqueryIfNeeded(), matching the established pattern inresolveConstantExpression(). The scalar subquery is executed and replaced with a ConstantNode, which the rest of the filter pipeline handles correctly.Verified locally:
USING (SELECT 1)(all rows visible),USING (SELECT 0)(no rows visible), andUSING c0 > (SELECT 1)(expression with embedded subquery) all work correctly. Existing row policy tests pass without regression.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a LOGICAL_ERROR crash in
QueryAnalyzer::resolve()when a ROW POLICY uses a scalar subquery in itsUSINGclause (e.g.USING (SELECT 1)). Closes #100695.Documentation entry for user-facing changes
Version info
26.5.1.410