viirya commented on code in PR #7806:
URL: https://github.com/apache/datafusion-comet/pull/7806#discussion_r4764352206


##########
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:
   I'd keep the guard ungated. In Spark, compact vs expanded depends on how the 
value was produced, not on its precision. A string cast goes through 
`Decimal.fromStringANSI`, which builds a `BigDecimal`, and `changePrecision` 
keeps it. So `cast(cast(s AS DECIMAL(18,7)) AS INT)` with 
`'-2147483648.9999999'` overflows in Spark, and Comet runs that whole 
expression natively. A `$precision > 18` gate would return `INT_MIN` there, 
which turns an error into a wrong value. The reverse case exists too. `a + b` 
over two `DECIMAL(18,7)` columns gives a compact `DECIMAL(19,7)`. Could the 
fixture pin the string case?
   



##########
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);
+                }

Review Comment:
   Following up on this. As far as I can tell, SPARK-60843 is only on Spark 
master (5.0), so every Spark version Comet supports keeps the DOUBLE rounding. 
Besides in-range values, the same divergence shows up on the `INT_MAX` side and 
at the SMALLINT/TINYINT bounds, where Comet returns a value and Spark 
overflows. For example, `-128.99999999999999999` as `DECIMAL(20,17)` to 
TINYINT. Could you file an issue covering these and link it from the comment 
here? Once Comet supports 5.0, that issue can also track turning this guard off 
through a flag set from Scala.
   



##########
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.

Review Comment:
   The two `BITS == 32` checks put INT-only logic into a macro that BIGINT 
shares, and the arithmetic already implies `$scale >= 7`, since `divisor / (1 
<< 22)` is 0 below that. Would something like `match 
<$rust_dest_type>::try_from(truncated) { Ok(v) if v != i32::MIN || 
!rounds_past_min(value, divisor) => Ok(v), _ => Err(..) }` keep your fast-path 
numbers? Here `rounds_past_min` would be a small helper that only the INT path 
uses, and the separate fast path would go away.



-- 
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]

Reply via email to