mbravenboer commented on PR #58939:
URL: https://github.com/apache/spark/pull/58939#issuecomment-6862818633
We hit this from another side: for integral inputs at a scale >= 0,
whole-stage generated code
already returns the input `codegenForIntegralType`: `${ev.value} =
${ce.value};}})`, and only the
interpreted path computes `BigDecimal(x).setScale(scale, mode)`. So the same
expression returns a
value or fails depending on whether Spark interprets it (constant folding,
NO_CODEGEN, the fallback
after a compile failure), and the interpreted path is slow well before it
fails: it builds
10^scale in a BigInteger.
On 4.3.0 (its Maven jars in a local[2] SparkSession, JDK 21), and the same
on master's source:
```
SELECT round(id, 1000000000) FROM range(3); -- 0, 1, 2 (generated code)
SELECT round(7, 1000000000); -- [ARITHMETIC_OVERFLOW]
BigInteger would overflow supported range
SET spark.sql.ansi.enabled=false;
SELECT round(7, 1000000000); --
java.lang.ArithmeticException, no error class
SET spark.sql.ansi.enabled=true;
SELECT round(id, 10000000) FROM range(3); -- 0, 1, 2 in 0.07 s
SELECT round(7, 10000000); -- 7 after 4.8 to 5.0 s
SET spark.sql.codegen.wholeStage=false;
SET spark.sql.codegen.factoryMode=NO_CODEGEN;
SELECT round(id, 1000000000) FROM range(3); -- [ARITHMETIC_OVERFLOW]
```
Interpreted cost by scale: 0.35 s at 10^6, 7 s at 10^7, over 15 s at 10^8 to
5*10^8, and
ArithmeticException above about 6.46*10^8, where 10^scale exceeds
BigInteger's 2^31-1 bits.
The clamp in #58939 fixes both (`setScale(1100)` is cheap and exact for
integers). It may be worth
also returning the input in the integral branches of `nullSafeEval` for
`scale >= 0`, as the generated
code does, so the two paths agree by construction; and testing both paths at
10^7 as well as
`Int.MaxValue`.
--
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]