sunchao opened a new issue, #7810:
URL: https://github.com/apache/datafusion-comet/issues/7810

   ### Describe the bug
   
   With `spark.sql.legacy.allowNegativeScaleOfDecimal=true`, decimal addition 
and subtraction can overflow an intermediate even when the input scales fit 
Arrow.
   
   * For two `DECIMAL(3,-125)` operands, Comet computes a width of 3 and picks 
the narrow kernel. [Arrow 
59.3.2](https://github.com/apache/arrow-rs/blob/59.3.2/arrow-arith/src/numeric.rs#L862-L876)
 computes `3 - (-125) = 128` in an i8 before adding the scale back. This panics 
with overflow checks enabled. At scale -124, the intermediate is 127 and fits.
   * With scales -128 and 0, the [wide 
kernel](https://github.com/apache/datafusion-comet/blob/1c0eb0b8bedbf19341dcbb5b115c0ec2235de576/native/spark-expr/src/math_funcs/wide_decimal_binary_expr.rs#L229-L254)
 needs a factor of 10^128, beyond its i256 power limit of 76. A smaller shift 
can still overflow the aligned coefficient. For example, multiplying a 38-digit 
coefficient by 10^39 can exceed signed i256 even though the power itself fits.
   
   ### Steps to reproduce
   
   On a Comet-enabled Spark session, use a nonconstant column so Spark cannot 
fold the arithmetic.
   
   ```scala
   import org.apache.spark.sql.functions._
   import org.apache.spark.sql.types.DecimalType
   
   spark.conf.set("spark.sql.legacy.allowNegativeScaleOfDecimal", "true")
   spark.sql("CREATE TABLE decimal_align(flag BOOLEAN, n INT) USING parquet")
   spark.sql("INSERT INTO decimal_align VALUES (true, 7), (false, 7), (false, 
NULL)")
   val coarseType = DecimalType(19, -128)
   val huge = lit(new java.math.BigDecimal("1E+128")).cast(coarseType)
   val coarse = when(col("flag"), huge).otherwise(lit(0).cast(coarseType))
   val fine = col("n").cast(DecimalType(3, 0))
   spark.table("decimal_align").select(try_add(coarse, fine), 
try_subtract(fine, coarse)).show()
   spark.sql("DROP TABLE decimal_align")
   ```
   
   ### Expected behavior
   
   The overflowing row returns (NULL, NULL), the zero row returns (7, 7), and 
the null-input row returns (NULL, NULL). Ordinary ANSI addition or subtraction 
should report numeric overflow for the overflowing row. No intermediate should 
wrap or panic.
   
   ### Additional context
   
   This covers scales representable in Arrow, separately from #7748. The 
[planner](https://github.com/apache/datafusion-comet/blob/1c0eb0b8bedbf19341dcbb5b115c0ec2235de576/native/core/src/execution/planner.rs#L1397-L1406)
 already computes the aligned width in i16. The remaining limits are in Arrow's 
narrow kernel and Comet's wide kernel.
   
   ### Willingness to contribute
   
   I can contribute a fix for this bug independently.
   
   


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