Repository navigation
Conversation
…written to IN `LogicalExpressionOptimizerPass::tryReplaceOrEqualsChainWithIn` appended the new `IN` (and, on every bail-out, the untouched equalities) after all other operands of the `or`. `or` is evaluated lazily from left to right, so `x = 0 OR intDiv(1, x) != 0 OR x = 2 OR x = 3` became `intDiv(1, x) != 0 OR x IN (0, 2, 3)` and failed with `Division by zero` under the default settings, while the same query with two equalities worked. The `IN` now takes the slot of the first equality of its chain, bail-outs keep the equalities in their own slots, and the empty slots are dropped. Found by `json_ast_sql_execution_fuzzer` (differential oracle). Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
Workflow [PR], commit [94a69d9] Summary: ❌
|
|
Relaying a measurement from the fuzzer author on the "later equality behind a throwing operand" concern. The So a later The "contiguous runs only" variant was tried and reverted — it only weakens the optimization ( The follow-up commit adding those test cases is on the branch ( |
The short-circuit `or` evaluates its cheap arguments eagerly and a lazily evaluated argument only on the rows where they are all false, so a later equality already protects a throwing operand; the case that broke was the `IN` appended behind the division, which made the division the first (always evaluated) argument. Document both shapes in the test. Co-Authored-By: Claude Fable 5.1 <[email protected]>
| QueryTreeNodes or_operands; | ||
| const auto & arguments = function_node.getArguments().getNodes(); | ||
|
|
||
| /// The operands keep their original positions: `or` is evaluated lazily from left to right |
There was a problem hiding this comment.
This only fixes the query-tree pass. The same OR-to-IN rewrite is still live in src/Interpreters/LogicalExpressionsOptimizer.cpp:274-364, where addInExpression still appends the merged IN after the existing operands.
That path is still reachable in supported code: ProjectionDescription::calculateByQuery and getAggregatingProjectionInfo both build an InterpreterSelectQuery over projection ASTs, and ignoreASTOptimizations() does not skip LogicalExpressionsOptimizer because TreeRewriter::analyzeSelect runs it before the ignore_ast_optimizations gate (src/Interpreters/TreeRewriter.cpp:1461-1463, 1535-1544). So a projection definition containing x = 0 OR intDiv(1, x) != 0 OR x = 2 OR x = 3 still gets reordered during projection build or projection matching and can still change short-circuit behavior or surface ILLEGAL_DIVISION.
Please either mirror the slot-preserving fix in the AST optimizer or stop applying this rewrite on those legacy entrypoints, and add a projection-based regression so the invariant is closed end-to-end.
| -- A guard written after the division does not protect it, with or without the rewrite. | ||
| SELECT (intDiv(1, number) != 0) OR (number = 0) OR (number = 2) OR (number = 3) FROM numbers(4); -- { serverError ILLEGAL_DIVISION } | ||
|
|
||
| -- A later equality behind a throwing operand: the short-circuit `or` evaluates its cheap arguments (the equalities) |
There was a problem hiding this comment.
The new test still never exercises the rebuild path where one equality chain in an or is declined while another chain in the same node is merged. That is the path keep_equals_functions is fixing, and it is also the second reproducer from issue #121024.
02702_logical_optimizer_with_nulls covers a NULL arm on the same chain, but it does not pin the multi-chain case that used to append the declined equalities behind the throwing operand. A regression there would keep 05237 green. Please add the mixed lossy/non-lossless example from the issue discussion (for example number = 0 OR intDiv(1, number) != 0 OR number = -1 OR number = -2 OR (number + 10) = 11 OR ...) or an equivalent two-chain case.
… is rewritten to IN Mirrors the analyzer fix in `LogicalExpressionOptimizerPass::tryReplaceOrEqualsChainWithIn`. `LogicalExpressionsOptimizer::addInExpression` appended the new `IN` after every other OR operand, so `x = 0 OR intDiv(1, x) != 0 OR x = 2 OR x = 3` became `intDiv(1, x) != 0 OR x IN (0, 2, 3)` and failed with `Division by zero` under the default settings. `TreeRewriter::analyzeSelect` runs `LogicalExpressionsOptimizer` BEFORE the `ignore_ast_optimizations` gate, so the bug is reachable from projection materialization (`ProjectionDescription::calculateByQuery`) even though the projection path passes `.ignoreASTOptimizations()`. The IN now takes the slot of the first equality of its chain. The other equalities keep their own slots and are erased by `cleanupOrExpressions`, preserving the relative order of all remaining operands (declined chains, non-equality operands, and the inserted `IN`s of other chains). The regression test for the analyzer side (`05237_or_chain_to_in_keeps_operand_order`) now also covers the declined-chain rebuild path (second repro from issue ClickHouse#121024), the legacy AST path with `enable_analyzer = 0`, and a projection whose WHERE forces the rewrite during `calculateByQuery`. Co-Authored-By: Claude Opus 4.7 <[email protected]>
|
Review follow-up landed as BLOCKER (legacy AST path) — fixed. MAJOR (test coverage) — fixed.
Added |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Analyzer: preserve operand order when rewriting an
ORchain of equalities intoIN, so short-circuit evaluation of neighboring operands is not broken.Documentation entry for user-facing changes
No documentation change needed.
Detailed description / Documentation
LogicalExpressionOptimizerPass::tryReplaceOrEqualsChainWithIncollected equality chains on the same expression, and, when the chain reached the threshold, appended the resultingINnode after all remaining operands of theOR. Bail-outs also appended the untouched equalities back at the end.ORis evaluated left to right with short-circuit semantics, so a query likewas rewritten as
intDiv(1, x) != 0 OR x IN (0, 2, 3)and started failing withDivision by zero, while the two-equality version worked. The rewrite must not change the relative order of an operand that was previously guarded by an earlier operand.Fix: the
INnow takes the slot of the first equality it replaces, bail-outs keep their equalities in their own slots, and the empty slots left behind are dropped. A regression test is added and one existing reference is updated to match the preserved ordering.Closes: #121024
cc @groeneai — fuzz-lab find (JSON AST fuzzer)
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/121047]