comphead commented on code in PR #7806:
URL: https://github.com/apache/datafusion-comet/pull/7806#discussion_r4762657820
##########
native/spark-expr/src/conversion_funcs/numeric.rs:
##########
@@ -605,18 +604,32 @@ macro_rules! cast_decimal_to_int32_up {
let output_array: $dest_array_type = match $eval_mode {
EvalMode::Ansi => cast_array.try_unary(|value| {
let truncated = value / divisor;
- let is_overflow = truncated.abs() > $max_dest_val.into();
- if is_overflow {
- return Err(cast_overflow(
+ // Keep ordinary INT values off the rounding path. INT_MIN
needs the check below.
+ if <$rust_dest_type>::BITS == 32
+ && truncated >= -i128::from(<$rust_dest_type>::MAX)
+ && truncated <= i128::from(<$rust_dest_type>::MAX)
+ {
+ return Ok(truncated as $rust_dest_type);
+ }
+ // Before SPARK-60843, an expanded decimal can round to
INT_MIN - 1
+ // via DOUBLE. Arrow loses Spark's compact/expanded
distinction, so
+ // preserve overflow within half an ulp (2^-22, first
reachable at scale 7).
+ // The negative remainder plus the divisor is the distance in
unscaled units.
+ let rounds_past_min = <$rust_dest_type>::BITS == 32
+ && truncated == i128::from(i32::MIN)
+ && $scale >= 7
+ && divisor + value % divisor <= divisor / (1 << 22);
Review Comment:
Spark only takes the `DOUBLE` route for expanded decimals. A column of
precision 18 or less is compact on both sides. Spark's `Decimal.setOrNull`
stores it as a long and `CometVector.getDecimal`
(`spark/src/main/java/org/apache/comet/vector/CometVector.java:97`) builds it
from the long too. So a `DECIMAL(18,7)` value of `-2147483648.9999999` returns
`INT_MIN` in Spark 3.5 to 4.1 (`Decimal.roundToNumeric` is identical in v3.5.8,
v4.0.1 and v4.1.1), and with this guard Comet raises `CAST_OVERFLOW`. The
description lists this as a known gap. Adding `&& $precision > 18` to
`rounds_past_min` closes it for scan-sourced columns and leaves the expanded
behavior unchanged. A row in a `DECIMAL(18,7)` column would pin it in the
fixture. Separately, this guard and the fast path above exist only to reproduce
a Spark bug that SPARK-60843 removes, and your table shows the guard costs
about 6% before the fast path (10.25 vs 10.88 ns/row). Please name the Spark
version that drops it in the comment
so it can be deleted then. I did not run this.
##########
spark/src/test/resources/sql-tests/expressions/cast/cast_decimal_to_primitive.sql:
##########
@@ -173,3 +173,33 @@ query
SELECT cast(cast(1 as decimal(5,0)) as boolean),
cast(cast(0 as decimal(5,0)) as boolean),
cast(cast(NULL as decimal(5,0)) as boolean)
+
+statement
+SET spark.sql.ansi.enabled=true
Review Comment:
Everything else in this file runs with ANSI off, and the other ANSI fixtures
are separate files with `-- Config: spark.sql.ansi.enabled=true` (for example
`cast_boolean_to_decimal_ansi.sql`). A new `cast_decimal_to_primitive_ansi.sql`
with the same `-- ConfigMatrix: parquet.enable.dictionary=false,true` header
avoids a mid-file `SET` that stays on for any query appended later.
##########
spark/src/test/resources/sql-tests/expressions/cast/cast_decimal_to_primitive.sql:
##########
@@ -173,3 +173,33 @@ query
SELECT cast(cast(1 as decimal(5,0)) as boolean),
cast(cast(0 as decimal(5,0)) as boolean),
cast(cast(NULL as decimal(5,0)) as boolean)
+
+statement
+SET spark.sql.ansi.enabled=true
+
+statement
+CREATE TABLE test_cast_decimal_min(i decimal(11,1), b decimal(20,1), w
decimal(32,22)) USING parquet
+
+statement
+INSERT INTO test_cast_decimal_min VALUES
+ (-2147483648.0, -9223372036854775808.0, NULL),
+ (-2147483648.5, -9223372036854775808.5, -2147483648.9999997615814208984374)
Review Comment:
Coverage is missing around the boundary this fix is about. There is no
`INT_MAX`, `INT_MAX + 1`, `BIGINT_MAX` or `BIGINT_MAX + 1` row, so the new `<=
MAX` fast-path bound is not pinned on the positive side. There is no non-ANSI
query and no `try_cast` over these rows. `EvalMode::Try` is excluded from this
kernel (`native/spark-expr/src/conversion_funcs/cast.rs:420`), so `try_cast(i
as int)` for `-2147483648.0` and `-2147483649.0` runs DataFusion's cast and is
worth a line. SMALLINT and TINYINT have no rows either.
`cast_decimal_to_int16_down` checks against `i32` first, so `-32768` and `-128`
pass today, and one row each would keep it that way.
--
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]