ganeshashree opened a new pull request, #57368:
URL: https://github.com/apache/spark/pull/57368
…elimination shortcut
### What changes were proposed in this pull request?
`EquivalentExpressions.skipForShortcut` (used when
`spark.sql.subexpressionElimination.skipForShortcutExpr` is enabled) stripped
only a single And/Or operand. This PR makes it peel leading And/Or operands
recursively, down to the single always-evaluated leaf of the chain:
```
expr match {
case and: And => skipForShortcut(and.left)
case or: Or => skipForShortcut(or.left)
case other => other
}
```
The change is scoped to the existing skipForShortcutExpr code path and does
not change its default (false).
### Why are the changes needed?
And/Or short-circuit, so only the leftmost operand of a chain is guaranteed
to be evaluated. A chained predicate a AND b AND c is parsed left-deep as
And(And(a, b), c), so peeling only one level leaves the analysis recursing into
b (and, at the outer level, c) — treating conditionally-evaluated operands as
always-evaluated. A subexpression shared with such an operand can then be
hoisted into the common-subexpression set and evaluated eagerly, defeating
short-circuit semantics and raising a spurious error for a row where that
operand should never have run.
For example, with subexpression elimination on:
```
SELECT id != 0 AND 1 / id > 0 AND 1 / id < 1 FROM range(0, 1, 1, 1);
```
For id = 0, id != 0 is false, so short-circuit should stop before 1 / id is
ever computed and the query should return false. Instead, the shared 1 / id
subexpression was hoisted and evaluated eagerly, raising [DIVIDE_BY_ZERO]. The
same mechanism produces a NullPointerException when the eagerly-evaluated
operand dereferences a null (the originally reported case). The pre-existing
one-level peel handled the two-operand case but still failed once there were
three or more operands.
If/CaseWhen already model this correctly via the ConditionalExpression trait
(recursing only alwaysEvaluatedInputs); this brings And/Or shortcut handling in
line for chains of arbitrary length.
**Note to reviewers:**
This PR deliberately keeps
`spark.sql.subexpressionElimination.skipForShortcutExpr` set to `false`, so the
fix only takes effect for users who opt in. Could reviewers weigh in on whether
the default should be flipped to `true`? With this fix, `true` is the
correct-by-default behavior (it never produces incorrect results and can only
eliminate spurious short-circuit errors), but it can miss
subexpression-elimination opportunities on wide AND/OR chains that repeat an
expensive subexpression. In a deliberately pathological microbenchmark, an
N-way OR chain where every operand shares one from_json over a wide row and
none short-circuits, I measured a ~3.5x slowdown at 50 operands and ~10x at
500. That worst case is narrow (it requires an expensive subexpression repeated
across many operands that rarely short-circuit), and the config remains
available as an opt-out, but the trade-off (hard failure vs. a potential CSE
regression) seemed worth surfacing before changing a default. Happy t
o flip it here or send a follow-up, per reviewer preference.
### Does this PR introduce _any_ user-facing change?
No. The behavior only changes when
`spark.sql.subexpressionElimination.skipForShortcutExpr` is enabled (default
false), where it fixes incorrect eager evaluation for chains of three or more
AND/OR operands.
### How was this patch tested?
<!--
If tests were added, say they were added here. Please make sure to add some
test cases that check the changes thoroughly including negative and positive
cases if possible.
If it was tested in a way different from regular unit tests, please clarify
how you tested step by step, ideally copy and paste-able, so that other
reviewers can test and check, and descendants can verify in the future.
If tests were not added, please describe why they were not added and/or why
it was difficult to add.
If benchmark tests were added, please run the benchmarks in GitHub Actions
for the consistent environment, and the instructions could accord to:
https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
-->
- SubexpressionEliminationSuite — a unit test asserting that a subexpression
in an operand past the first of a chained/deeply-nested (4-way)/mixed AND/OR
tree is not collected as a common subexpression, while the always-evaluated
leftmost leaf still is (guarding against over-conservatism).
- SQLQuerySuite — an end-to-end regression test running SELECT id != 0 AND 1
/ id > 0 AND 1 / id < 1 FROM range(0, 1, 1, 1) with skipForShortcutExpr=true,
asserting it returns false with no error.
Also ran the surrounding suites to check for regressions:
SubexpressionEliminationSuite (21/21), WholeStageCodegenSuite (55/55), and
DataFrameFunctionsSuite (161/161) all pass.
New tests, both failing before the change and passing after:
- SubexpressionEliminationSuite — a unit test asserting that a subexpression
in an operand past the first of a chained/deeply-nested (4-way)/mixed AND/OR
tree is not collected as a common subexpression, while the always-evaluated
leftmost leaf still is (guarding against over-conservatism).
- SQLQuerySuite — an end-to-end regression test running SELECT id != 0 AND 1
/ id > 0 AND 1 / id < 1 FROM range(0, 1, 1, 1) with skipForShortcutExpr=true,
asserting it returns false with no error.
Also ran the surrounding suites to check for regressions:
SubexpressionEliminationSuite (21/21), WholeStageCodegenSuite (55/55), and
DataFrameFunctionsSuite (161/161) all pass.
### Was this patch authored or co-authored using generative AI tooling?
<!--
If generative AI tooling has been used in the process of authoring this
patch, please include the
phrase: 'Generated-by: ' followed by the name of the tool and its version.
If no, write 'No'.
Please refer to the [ASF Generative Tooling
Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
-->
Generated-by: Claude Code (Opus 4.8)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]