[SPARK-57198][SQL] Skip the divide-by-zero check in codegen when the divisor is a non-zero literal#56253
Open
gengliangwang wants to merge 4 commits into
Open
Conversation
…divisor is a non-zero literal DivModLike (Divide/Remainder/IntegralDivide) and Pmod always emit a divide-by-zero guard `if (divisor == 0) ...` in their generated code. When the divisor is a foldable, non-null, non-zero constant the guard is dead code that can never trigger, and in ANSI mode it also registers an unreachable error-context reference in the constant pool. This skips emitting the check (and its error-context reference) when the divisor is a statically non-zero constant. Behavior and results are unchanged. Across the TPC-DS queries this removes 56 dead `if (100.0D == 0) throw divideByZeroError(...)` checks. Generated-by: Claude Code (Opus 4.8)
… + eval A foldable divisor is already constant-folded to a Literal before codegen, so detect the non-zero constant divisor by matching `Literal` directly rather than checking `foldable` and evaluating the expression at codegen time. Generated-by: Claude Code (Opus 4.8)
LuciferYang
approved these changes
Jun 2, 2026
Member
Author
|
@LuciferYang Thanks for the review. Merging to master/4.x |
…-zerocheck # Conflicts: # sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala
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?
DivModLike(Divide/Remainder/IntegralDivide) andPmodalways emit a divide-by-zero guard in their generated code:When the divisor is a foldable, non-null, non-zero constant, that guard is dead code. This PR detects that case (
divisorIsNonZero) and emits only the division/remainder body, dropping the check. TheerrCtxerror-context value is also madelazyso its constant-pool reference is no longer registered when the (now-skipped) check is the only thing that would have used it.Example: for
col / 100.0in ANSI mode, the generated code goes fromto
The dead-check elimination only fires when the divisor is statically non-zero; a zero literal divisor (which always errors / returns null) and any variable divisor keep the existing check.
Why are the changes needed?
This is a sub-task of SPARK-56908 (reduce generated Java size in whole-stage codegen). Dumping the whole-stage codegen of the TPC-DS queries shows 56 occurrences of the dead
if (100.0D == 0) throw QueryExecutionErrors.divideByZeroError(...)check across 14 queries (percentage computations such asx / 100.0). Each is unreachable source plus an unreachablereferences[]/ constant-pool entry. Removing them shrinks the generated code and eases constant-pool pressure with no behavior change.Does this PR introduce any user-facing change?
No. The generated code for a non-zero constant divisor is smaller but computes the same result; behavior for zero and variable divisors is unchanged.
How was this patch tested?
ArithmeticExpressionSuitetest "SPARK-57198: skip the divide-by-zero check when the divisor is a non-zero literal", asserting the by-zero check is dropped for a non-zero literal divisor (Divide / Remainder / IntegralDivide / Pmod) and kept for a variable divisor. Existing tests (e.g. SPARK-33008) continue to cover the zero-literal-divisor error path.if (100.0D == 0)checks dropped to 0, and all generated subtrees still compile.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Opus 4.8)