[SPARK-57181][SQL] Simplify Pmod codegen by sharing a MathUtils.pmod helper with eval#56232
Open
gengliangwang wants to merge 1 commit into
Open
[SPARK-57181][SQL] Simplify Pmod codegen by sharing a MathUtils.pmod helper with eval#56232gengliangwang wants to merge 1 commit into
gengliangwang wants to merge 1 commit into
Conversation
…helper with eval ### What changes were proposed in this pull request? `Pmod.doGenCode` emitted the positive-modulo `remainder`/adjust block inline, once for byte/short and once for the int/long/float/double case (~6-8 lines each), duplicating the algorithm already implemented by `Pmod`'s private `pmod` eval methods. This adds `MathUtils.pmod` overloads (Int, Long, Byte, Short, Float, Double) -- the exact bodies moved out of `Pmod` -- and routes both the eval dispatch (`pmodFunc`) and codegen through them. The primitive codegen cases collapse to a single `MathUtils.pmod(left, right)` call. The Decimal case (which returns null / applies `toPrecision`) is unchanged. ### Why are the changes needed? Part of SPARK-56908 (umbrella). `Pmod` over IntegerType is emitted by every `HashPartitioning` (`Pmod(Murmur3Hash(...), numPartitions)`), so collapsing the inline block shrinks the generated Java on a very common path, and the eval and codegen paths now share one implementation instead of duplicating the algorithm. ### Does this PR introduce _any_ user-facing change? No. The compiled behavior is identical; only the emitted Java source text changes. ### How was this patch tested? Existing `ArithmeticExpressionSuite."pmod"` covers all numeric types, negative operands / divisors, mod-by-zero (ANSI on/off), and `checkConsistencyBetweenInterpretedAndCodegenAllowingException` across all numeric types (which verifies eval and codegen agree -- exactly the invariant this refactor must preserve). ``` build/sbt "catalyst/testOnly *ArithmeticExpressionSuite" ``` 35/35 pass. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Code (Opus 4.8) Co-authored-by: Isaac
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?
Pmod.doGenCodeemitted the positive-moduloremainder/adjust block inline, once for byte/short and once for the int/long/float/double case (~6-8 lines each), duplicating the algorithm already implemented byPmod's privatepmodeval methods. This addsMathUtils.pmodoverloads (Int, Long, Byte, Short, Float, Double) -- the exact bodies moved out ofPmod-- and routes both the eval dispatch (pmodFunc) and codegen through them. The primitive codegen cases collapse to a singleMathUtils.pmod(left, right)call. The Decimal case (which returns null / appliestoPrecision) is unchanged.Why are the changes needed?
Part of SPARK-56908 (umbrella).
Pmodover IntegerType is emitted by everyHashPartitioning(Pmod(Murmur3Hash(...), numPartitions)), so collapsing the inline block shrinks the generated Java on a very common path, and the eval and codegen paths now share one implementation instead of duplicating the algorithm (helping with the JVM 64KB method / constant-pool limits, Janino compile time, and JIT work).Does this PR introduce any user-facing change?
No. The compiled behavior is identical; only the emitted Java source text changes.
How was this patch tested?
Existing
ArithmeticExpressionSuite."pmod"covers all numeric types, negative operands / divisors, mod-by-zero (ANSI on/off), andcheckConsistencyBetweenInterpretedAndCodegenAllowingExceptionacross all numeric types (which verifies eval and codegen agree -- exactly the invariant this refactor must preserve).35/35 pass.
Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Opus 4.8)