[SPARK-58211][SQL] Peel all leading AND/OR operands in subexpression elimination shortcut#57368
Open
ganeshashree wants to merge 1 commit into
Open
[SPARK-58211][SQL] Peel all leading AND/OR operands in subexpression elimination shortcut#57368ganeshashree wants to merge 1 commit into
ganeshashree wants to merge 1 commit into
Conversation
…elimination shortcut
`EquivalentExpressions.skipForShortcut` (used when
`spark.sql.subexpressionElimination.skipForShortcutExpr` is enabled) only stripped
a single `And`/`Or` operand. This change makes it peel leading `And`/`Or` operands
recursively down to the single always-evaluated leaf.
`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 left-deep -- `And(And(a, b), c)`
-- so peeling only one level still recurses into `b` (and, at the outer level, `c`),
treating conditionally-evaluated operands as always-evaluated. A subexpression shared
with such an operand could then be hoisted and eagerly evaluated, breaking
short-circuit semantics and raising a spurious error (e.g. NullPointerException or
divide-by-zero) for a row where the operand should never have been evaluated.
For example, with subexpression elimination on:
select id != 0 and 1 / id > 0 and 1 / id < 1 from range(0, 1, 1, 1)
should return `false` for `id = 0` (short-circuit stops at `id != 0`), but the shared
`1 / id` was hoisted and evaluated eagerly, raising DIVIDE_BY_ZERO.
This scopes the change to the existing `skipForShortcutExpr` code path and does not
change its default. Flipping the default to make the correct behavior apply
out-of-the-box is left to a follow-up, since it trades a hard failure for a potential
subexpression-elimination performance regression on wide AND/OR chains.
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.
Added a unit test in `SubexpressionEliminationSuite` covering chained, deeply nested,
and mixed AND/OR trees, and an end-to-end regression test in `SQLQuerySuite`.
ganeshashree
force-pushed
the
SPARK-58211
branch
from
July 20, 2026 07:24
dcfab98 to
adda7d4
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
EquivalentExpressions.skipForShortcut(used whenspark.sql.subexpressionElimination.skipForShortcutExpris 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: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:
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.skipForShortcutExprset tofalse, so the fix only takes effect for users who opt in. Could reviewers weigh in on whether the default should be flipped totrue? With this fix,trueis 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 to 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.skipForShortcutExpris enabled (default false), where it fixes incorrect eager evaluation for chains of three or more AND/OR operands.How was this patch tested?
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:
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?
Generated-by: Claude Code (Opus 4.8)