Commit 576070a
committed
[SPARK-57181][SQL] Simplify Pmod codegen by sharing a MathUtils.pmod 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 (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)
Closes #56232 from gengliangwang/spark-pmod-codegen.
Authored-by: Gengliang Wang <gengliang@apache.org>
Signed-off-by: Gengliang Wang <gengliang@apache.org>1 parent 2d83237 commit 576070a
2 files changed
Lines changed: 46 additions & 54 deletions
File tree
- sql
- api/src/main/scala/org/apache/spark/sql/catalyst/util
- catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions
Lines changed: 34 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
92 | 126 | | |
93 | 127 | | |
94 | 128 | | |
| |||
Lines changed: 12 additions & 54 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1080 | 1080 | | |
1081 | 1081 | | |
1082 | 1082 | | |
1083 | | - | |
1084 | | - | |
1085 | | - | |
1086 | | - | |
1087 | | - | |
1088 | | - | |
| 1083 | + | |
| 1084 | + | |
| 1085 | + | |
| 1086 | + | |
| 1087 | + | |
| 1088 | + | |
1089 | 1089 | | |
1090 | 1090 | | |
1091 | 1091 | | |
| |||
1120 | 1120 | | |
1121 | 1121 | | |
1122 | 1122 | | |
| 1123 | + | |
1123 | 1124 | | |
1124 | 1125 | | |
1125 | 1126 | | |
| |||
1135 | 1136 | | |
1136 | 1137 | | |
1137 | 1138 | | |
1138 | | - | |
1139 | | - | |
1140 | | - | |
1141 | | - | |
1142 | | - | |
1143 | | - | |
1144 | | - | |
1145 | | - | |
1146 | | - | |
1147 | | - | |
| 1139 | + | |
| 1140 | + | |
| 1141 | + | |
| 1142 | + | |
1148 | 1143 | | |
1149 | | - | |
1150 | | - | |
1151 | | - | |
1152 | | - | |
1153 | | - | |
1154 | | - | |
1155 | | - | |
1156 | | - | |
| 1144 | + | |
1157 | 1145 | | |
1158 | 1146 | | |
1159 | 1147 | | |
| |||
1198 | 1186 | | |
1199 | 1187 | | |
1200 | 1188 | | |
1201 | | - | |
1202 | | - | |
1203 | | - | |
1204 | | - | |
1205 | | - | |
1206 | | - | |
1207 | | - | |
1208 | | - | |
1209 | | - | |
1210 | | - | |
1211 | | - | |
1212 | | - | |
1213 | | - | |
1214 | | - | |
1215 | | - | |
1216 | | - | |
1217 | | - | |
1218 | | - | |
1219 | | - | |
1220 | | - | |
1221 | | - | |
1222 | | - | |
1223 | | - | |
1224 | | - | |
1225 | | - | |
1226 | | - | |
1227 | | - | |
1228 | | - | |
1229 | | - | |
1230 | | - | |
1231 | 1189 | | |
1232 | 1190 | | |
1233 | 1191 | | |
| |||
0 commit comments