[SPARK-56913][SQL] Simplify BinaryArithmetic byte/short codegen under ANSI mode#55938
Open
gengliangwang wants to merge 1 commit into
Open
[SPARK-56913][SQL] Simplify BinaryArithmetic byte/short codegen under ANSI mode#55938gengliangwang wants to merge 1 commit into
gengliangwang wants to merge 1 commit into
Conversation
This was referenced May 17, 2026
Member
Author
Stack overview (SPARK-56908 umbrella)This PR is part of a stack of 8 PRs against SPARK-56908. Order:
PRs 1-4 are linearly stacked on each other (each branch is based on the previous one). PR 5 (decimal arithmetic) is stacked on top of PR 3 (cast decimal) since it uses |
This was referenced May 17, 2026
… ANSI mode ### What changes were proposed in this pull request? Introduce `ArithmeticUtils.java` with six static helpers (`byteAddExact`, `byteSubtractExact`, `byteMultiplyExact`, `shortAddExact`, `shortSubtractExact`, `shortMultiplyExact`) and use them from `BinaryArithmetic.doGenCode` and from `Add` / `Subtract` / `Multiply.nullSafeEval`. The `Byte`/`Short` ANSI overflow-check branch of `BinaryArithmetic.doGenCode` previously emitted ~7 lines per call site (int tmpResult + overflow check + cast back). After this PR it emits a single `ArithmeticUtils.<type><Op>Exact(...)` call. The eval-path counterparts for Add/Subtract/Multiply also delegate to the helpers under ANSI mode, replacing the previous fall-through to `numeric.plus`/`minus`/`times` (which threw a generic `ArithmeticException`) -- the eval path now produces the same SQL-formatted `BINARY_ARITHMETIC_OVERFLOW` error as the codegen path. Primitive `int`/`long`/`float`/`double` branches are intentionally left inline (single bytecode op; routing through a static method would be a runtime regression). ### Why are the changes needed? Part of SPARK-56908 (umbrella). The Byte/Short ANSI branch is the largest single inline body in `BinaryArithmetic.doGenCode`. ### Does this PR introduce _any_ user-facing change? No. Compiled behavior is identical; the eval path now produces a SQL-formatted overflow error matching the codegen path (the previous generic `ArithmeticException` was an inconsistency). ### How was this patch tested? ``` build/sbt "catalyst/testOnly *ArithmeticExpressionSuite" ``` 35/35 pass. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Cursor 1.x
7344920 to
b38e73a
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.
Title: [SPARK-56913][SQL] Refactor BinaryArithmetic byte/short codegen under ANSI mode
Base: master (independent)
Head: gengliangwang:SPARK-56913-arithmetic-byte-short
What changes were proposed in this pull request?
Introduce
ArithmeticUtils.javawith six static helpers (byteAddExact,byteSubtractExact,byteMultiplyExact,shortAddExact,shortSubtractExact,shortMultiplyExact) and use them fromBinaryArithmetic.doGenCodeand fromAdd/Subtract/Multiply.nullSafeEval.The
Byte/ShortANSI overflow-check branch ofBinaryArithmetic.doGenCodepreviously emitted ~7 lines per call site (int tmpResult + overflow check + cast back). After this PR it emits a singleArithmeticUtils.<type><Op>Exact(...)call.The eval-path counterparts for Add/Subtract/Multiply also delegate to the helpers under ANSI mode, replacing the previous fall-through to
numeric.plus/minus/times(which threw a genericArithmeticException) -- the eval path now produces the same SQL-formattedBINARY_ARITHMETIC_OVERFLOWerror as the codegen path.Primitive
int/long/float/doublebranches are intentionally left inline (single bytecode op; routing through a static method would be a runtime regression).Why are the changes needed?
Part of SPARK-56908 (umbrella). The Byte/Short ANSI branch is the largest single inline body in
BinaryArithmetic.doGenCode.Does this PR introduce any user-facing change?
No. Compiled behavior is identical; the eval path now produces a SQL-formatted overflow error matching the codegen path (the previous generic
ArithmeticExceptionwas an inconsistency).How was this patch tested?
35/35 pass.
Was this patch authored or co-authored using generative AI tooling?
Generated-by: Cursor 1.x