[MINOR][SQL] Remove redundant second modulo in integral MathUtils.pmod - #58906
Open
david-mollitor-db wants to merge 1 commit into
Open
david-mollitor-db wants to merge 1 commit into
david-mollitor-db wants to merge 1 commit into
Conversation
The integral `MathUtils.pmod` overloads (Int, Long, Byte, Short) compute `if (r < 0) (r + n) % n else r`, where `r = a % n`. In exact integer arithmetic a negative `r` lies in `(-|n|, 0)`, so `r + n` is already in `[0, n)` and the trailing `% n` is a no-op. Drop it, leaving `if (r < 0) r + n else r`. The change is behavior-preserving: the two forms were verified equivalent over 18M random and boundary inputs (including Int.MinValue) across power-of-two and non-power-of-two divisors. The float/double overloads intentionally keep the second modulo: floating-point rounding can make `r + n` round up to exactly `n`, which `% n` folds back to `0`. `pmod` backs the `pmod` SQL function and `HashPartitioning`'s per-row partition-id expression; dropping the extra modulo also lets the JIT emit branchless, single-division code for the function. Co-authored-by: Isaac <no-reply@databricks.com>
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?
The integral
MathUtils.pmodoverloads (Int,Long,Byte,Short) compute the positiveremainder as:
For integer types the trailing
% nis redundant.a % nyields a value whose magnitude isstrictly less than
|n|, so whenr < 0it lies in(-|n|, 0)andr + nis already in[0, n). This PR removes the second modulo, leaving:The
Float/Doubleoverloads are left unchanged on purpose: floating-point rounding can roundr + nup to exactlyn, and the extra% nfolds that back to0, so there it is a realcorrectness guard rather than a no-op.
Why are the changes needed?
MathUtils.pmodbacks thepmodSQL function and the per-row partition-id expression ofHashPartitioning(Pmod(Murmur3Hash(...), numPartitions)), so it runs once per row on the mapside of every hash-partitioned shuffle. The trailing
% nis dead computation. Removing it is asmall readability and efficiency cleanup: besides skipping an operation, it lets the JIT compile
the function to branchless, single-division code (the second modulo otherwise sits in a branch
that cannot be turned into a conditional move).
Does this PR introduce any user-facing change?
No. The result is identical for all inputs; only redundant computation is removed.
How was this patch tested?
Existing
pmodcoverage (e.g.ArithmeticExpressionSuite) continues to apply. The old and newintegral forms were additionally cross-checked for exact equality over 18M random inputs plus
boundary values (including
Int.MinValue/Long.MinValue,0,+/-1,+/-n) acrosspower-of-two and non-power-of-two divisors, with zero mismatches.
Was this patch authored or co-authored using generative AI tooling?
Generated-by: Isaac
This pull request and its description were written by Isaac.