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


##########
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)
+        val nonzero = lit(new java.math.BigDecimal("1E+128")).cast(coarseType)
+        val coarse = when(col("flag"), 
nonzero).otherwise(lit(0).cast(coarseType))
+        val fine = col("n").cast(DecimalType(3, 0))
+        val input = spark.table("decimal_align")
+        val query = input.select(try_add(coarse, fine), try_subtract(fine, 
coarse))

Review Comment:
   `try_add` and `try_subtract` stay in TRY mode here. Worth a plain `+` case 
for the ANSI error too?



##########
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:
   Width here is 147, so this took the wide kernel before too. Maybe add the 
issue's `DECIMAL(3,-125)` case?



##########
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:
   `DECIMAL(3,-125) % DECIMAL(3,-125)` still panics for me in Arrow's `rem` on 
a debug build. Is a follow-up issue planned?



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