0lai0 opened a new pull request, #5050:
URL: https://github.com/apache/datafusion-comet/pull/5050
## Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases. You can
link an issue to this PR using the GitHub syntax. For example `Closes #123`
indicates that this PR will close issue #123.
-->
Closes #5013
## Rationale for this change
With `spark.sql.legacy.allowNegativeScaleOfDecimal=true`, Spark allows
`DecimalType(p, s)` with `s < 0`. Comet's native cast and decimal arithmetic
kernels scale-align values by computing `10^|scale|` (e.g. `10_i128.pow(scale
as u32)`). When `scale` is negative, casting it to `u32` produces a huge value,
so the power computation overflows: it panics (`attempt to multiply/subtract
with overflow`) in debug builds and silently wraps to an incorrect value in
release builds (the native workspace sets `overflow-checks = false` for
release).
This is reachable from two places:
- Casting an integer type (`Byte`/`Short`/`Integer`/`Long`) to/from a
negative-scale `DecimalType`, or casting a negative-scale `DecimalType` to
`TimestampType`, both hit the same `10^|scale|` computation in `numeric.rs` /
`cast_decimal_to_timestamp`.
- Arithmetic on negative-scale decimals
(`Add`/`Subtract`/`Divide`/`IntegralDivide`/`Remainder`), which scale-aligns
operands the same way.
`CometCast` already special-cased negative-scale decimals for the `Decimal
-> String` direction, but that guard was unreachable in practice because
producing the negative-scale value in the first place would panic before a `->
String` cast could ever run.
Since `allowNegativeScaleOfDecimal` is a rarely-used legacy flag (default
`false`), and the native kernels don't implement negative-scale-safe rescaling,
the fix makes Comet detect these cases at planning time and fall back to Spark
instead of attempting them natively.
## What changes are included in this PR?
- `CometCast.scala`: added `negativeScaleDecimalCastReason` and marked as
`Unsupported`:
- `int -> Decimal(neg)` via `canCastFromByte`/`Short`/`Int`/`Long`
- `Decimal(neg) -> int` and `Decimal(neg) -> Timestamp` via
`canCastFromDecimal` (now takes the source `DecimalType` so it can check
`fromType.scale`)
All other cast directions to/from negative-scale decimals (e.g. `String
<-> Decimal(neg)`, `Decimal(neg) -> Float/Double/String/Boolean/wider-Decimal`)
don't perform integer scale-alignment and are unaffected, they keep running
natively.
- `arithmetic.scala`: added `negScaleDecimalRejection(expr:
BinaryArithmetic)` in `MathBase`, returning `Unsupported` when the left
operand, right operand, or result type is a negative-scale `DecimalType`. Wired
into `CometAdd`, `CometSubtract`, `CometDivide`, `CometIntegralDivide`,
`CometRemainder`.
`CometMultiply` and `UnaryMinus` are intentionally left unguarded, neither
scale-aligns, so they stay native
## How are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example, are
they covered by existing tests)?
-->
Added tests to `CometCastSuite` and `CometExpressionSuite`
`make core`
` ./mvnw -pl spark test -Dsuites='org.apache.comet.CometCastSuite'`
`./mvnw -pl spark test -Dsuites='org.apache.comet.CometExpressionSuite'`
**Note**: the `TimestampType` case is not called out in the linked issue's
reproducer, it was found while auditing the same `10^|scale|` code path during
this fix and is included here since it shares the exact same root cause and fix.
--
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]