MaxGekk opened a new pull request, #60220: URL: https://github.com/apache/spark/pull/60220
### What changes were proposed in this pull request? `Rint` now returns the canonical `Double.NaN` (`0x7ff8000000000000`) whenever its result is NaN. The rule lives in one place, a new `MathUtils.rint(a: Double)`, which `Rint` passes to `UnaryMathExpression` as its function and which its generated code calls, as the generated code of the exact arithmetic calls `MathUtils.negateExact` and the like. So `eval`, `f` and codegen cannot drift apart. Every non-NaN result is `Math.rint`'s, bit for bit, including `-0.0` and the infinities, and C2 still compiles the `Math.rint` call inside the helper to its intrinsic. ### Why are the changes needed? `rint` calls `java.lang.Math.rint`, and on HotSpot the bits of a NaN result depend on which JIT tier runs that call. The interpreter and C1 execute the Java implementation, `StrictMath.rint`, which takes the absolute value and multiplies the sign back, so a NaN comes out with its sign cleared. C2 replaces the call with an intrinsic (`roundsd` on x86), which returns the input NaN with its sign. Measured on JDK 25.0.4.1, x86_64 (AMD Zen 5), the raw bits of `Math.rint(x)`: | input | `-Xint` and `-XX:TieredStopAtLevel=1` | `-XX:-TieredCompilation` (C2) | | :--- | :--- | :--- | | `fff8000000000000` | `7ff8000000000000` | `fff8000000000000` | | `fff0000000000001` | `7ff8000000000001` | `fff8000000000001` | | `fff4000000000000` | `7ffc000000000000` | `fffc000000000000` | | `7ff0000000000001` | `7ff8000000000001` | `7ff8000000000001` | `-XX:+PrintIntrinsics` shows `Math::rint` compiled as an intrinsic under C2. Every tier keeps the payload and quiets a signalling NaN; only the sign differs. So the same query can return a NaN with a different sign from one run to another, or between rows of one task as the code warms up. Spark stores the double as is (`UnsafeRowWriter.write(double)` and `UnsafeRow.setDouble` are `Platform.putDouble`), so the difference reaches user code through `collect()`, typed Datasets, Scala/Java/Python UDF arguments and the Arrow paths, where `Double.doubleToRawLongBits`, `Math.copySign` or `numpy.signbit` see it. Comparisons, grouping, joins, sorting, hashing and string output treat all NaNs alike, and the Parquet and ORC writers canonicalize NaN through `Double.doubleToLongBits`, so SQL results and those files do not show it. Returning the canonical NaN makes the result independent of the tier at the cost of one predictable branch, and keeps C2's intrinsic for every other value. Calling `StrictMath.rint` instead would also be deterministic, but gives up the intrinsic for every row. ### Does this PR introduce _any_ user-facing change? Yes, for NaN results only. `rint` of a NaN now always returns `Double.NaN` (`0x7ff8000000000000`). Before, the result kept the input's payload on every tier and its sign on C2 only, so `rint` of `0xfff8000000000000` returned `0x7ff8000000000000` or `0xfff8000000000000` depending on the tier, and `rint` of `0x7ff8000000000001` returned `0x7ff8000000000001` on every tier. The payload is dropped too, so the canonical NaN is the one answer whatever the input. NaN still compares, groups, sorts and prints the same, so SQL results do not change; the raw bits change only where user code reads them. `round` and `bround` are left as they are: they return a NaN input unchanged and are not JIT-tier dependent. ### How was this patch tested? A new test in `MathExpressionsSuite`, "SPARK-61175: rint returns the canonical NaN", evaluates `rint` over a bound column holding four NaNs: one with a payload (`0x7ff8000000000001`), a signalling one (`0x7ff0000000000001`), and the two with the sign bit set. `checkEvaluation` compares the result `UnsafeRow` byte for byte with the canonical NaN's row in both the `CODEGEN_ONLY` and `NO_CODEGEN` factory modes, and the test also checks the raw bits of the interpreted `eval`. Every tier keeps the payload of the first two inputs, so they fail without the fix wherever the test runs; the two signed inputs check the sign when the code under test has been compiled by C2, since the interpreter and C1 already clear it. Ten non-NaN inputs, the signed zeros and the infinities among them, keep `Math.rint`'s exact result. `build/sbt 'catalyst/testOnly *MathExpressionsSuite'` passes (57 tests), and with the change to `Rint` and `MathUtils` reverted the new test fails. `sql-api`, `catalyst` and catalyst's test scalastyle report no errors or warnings. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Code (Opus 5.5) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
