Skip to content

[SPARK-57181][SQL] Simplify Pmod codegen by sharing a MathUtils.pmod helper with eval#56232

Open
gengliangwang wants to merge 1 commit into
apache:masterfrom
gengliangwang:spark-pmod-codegen
Open

[SPARK-57181][SQL] Simplify Pmod codegen by sharing a MathUtils.pmod helper with eval#56232
gengliangwang wants to merge 1 commit into
apache:masterfrom
gengliangwang:spark-pmod-codegen

Conversation

@gengliangwang
Copy link
Copy Markdown
Member

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 (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), 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)

…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
@gengliangwang gengliangwang requested a review from LuciferYang May 31, 2026 02:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant