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


##########
native/core/src/execution/planner.rs:
##########
@@ -1400,10 +1400,13 @@ impl PhysicalPlanner {
                 DataFusionOperator::Plus | DataFusionOperator::Minus | 
DataFusionOperator::Multiply,
                 Ok(DataType::Decimal128(p1, s1)),
                 Ok(DataType::Decimal128(p2, s2)),
-            ) if ((op == DataFusionOperator::Plus || op == 
DataFusionOperator::Minus)
-                && decimal128_add_sub_width(p1, s1, p2, s2) >= 
DECIMAL128_MAX_PRECISION as i16)
-                || (op == DataFusionOperator::Multiply
-                    && p1 as i16 + p2 as i16 >= DECIMAL128_MAX_PRECISION as 
i16) =>
+            ) if ((op == DataFusionOperator::Plus || op == 
DataFusionOperator::Minus) && {
+                let width = decimal128_add_sub_width(p1, s1, p2, s2);
+                // Arrow subtracts scale from precision in i8 before adding 
the scale back.
+                let integer_digits = width - i16::from(s1.max(s2));
+                width >= i16::from(DECIMAL128_MAX_PRECISION) || integer_digits 
> i16::from(i8::MAX)

Review Comment:
   Copying the new planner condition into `create_modulo_expr` wouldn't fix 
this. Its wide branch casts to `Decimal256` but still calls Arrow's `rem`, and 
`decimal_op` computes `*p1 as i8 - s1` for every decimal width. 
`DECIMAL(3,-125) % DECIMAL(3,-125)` has width 3, so it takes the narrow branch 
anyway. Could `CometRemainder.getSupportLevel` return `Unsupported` when an 
operand has `precision - scale > 127`? If that belongs in another PR, could you 
file an issue and link it here?
   



##########
spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala:
##########
@@ -1782,6 +1782,28 @@ class CometExpressionSuite extends CometTestBase with 
AdaptiveSparkPlanHelper {
     }
   }
 
+  test("decimal addition and subtraction align extreme negative scales 
natively") {
+    withSQLConf(
+      "spark.sql.legacy.allowNegativeScaleOfDecimal" -> "true",
+      SQLConf.ANSI_ENABLED.key -> "true",
+      CometConf.COMET_SCALA_UDF_CODEGEN_ENABLED.key -> "false") {
+      withTable("decimal_align") {
+        sql("CREATE TABLE decimal_align(flag BOOLEAN, n INT) USING parquet")
+        sql("INSERT INTO decimal_align VALUES (true, 7), (false, 7), (false, 
NULL)")
+        val coarseType = DecimalType(19, -128)

Review Comment:
   +1. CI builds the native library with `overflow-checks = false`, so the 
debug panic wouldn't show up there. Reading `decimal_op`, a release build 
returns the type `Decimal128(38,-125)` instead of `(4,-125)` for 
`DECIMAL(3,-125) + DECIMAL(3,-125)`. A Spark comparison would show whether that 
difference was ever visible. Could the query also include `(38,-1) + (38,38)` 
with the default `allowPrecisionLoss`? That is the only shape where the BigInt 
path rounds, and right now its expected values are worked out by hand in the 
Rust test instead of compared against Spark.
   



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