sunchao commented on code in PR #7795:
URL: https://github.com/apache/datafusion-comet/pull/7795#discussion_r4735271054


##########
spark/src/test/scala/org/apache/comet/exec/CometAggregateSuite.scala:
##########
@@ -3369,6 +3369,63 @@ class CometAggregateSuite extends CometTestBase with 
AdaptiveSparkPlanHelper {
     }
   }
 
+  test("skip partial aggregation converts decimal AVG rows to partial states") 
{
+    withTempDir { dir =>
+      val path = new Path(dir.toURI.toString, "input").toUri.toString
+      withSQLConf(CometConf.COMET_ENABLED.key -> "false") {
+        // Mostly distinct keys, with every 4th key also given to the next 
row, so groups of
+        // one and two rows both reach the final aggregate.
+        spark
+          .range(0L, 16384L, 1L, 8)
+          .selectExpr(
+            "id - CAST(id % 4 = 1 AS BIGINT) AS k",
+            "CASE WHEN id % 5 = 0 THEN NULL " +
+              "ELSE CAST(id * 1000003 % 99999999 - 50000000 AS DECIMAL(20, 2)) 
END AS d",
+            "CAST('9' || repeat('9', 37) AS DECIMAL(38, 0)) AS big")

Review Comment:
   A singleton with 38 nines already overflows the `DECIMAL(38,4)` result of 
`avg(big)`. The first probe batch also contains duplicate keys, so a sum can 
overflow before skipping starts. Could we use `0.6` as `DECIMAL(38,38)` and 
keep the probe keys distinct? Then a singleton returns `0.6`, and two rows with 
the same key after the probe overflow only when the reducer adds them.



##########
native/core/src/execution/jni_api.rs:
##########
@@ -1054,6 +1055,13 @@ fn configure_skip_partial_aggregation(config: &mut 
SessionConfig, plan: &Operato
                         && agg.agg_exprs.iter().all(|expr| {
                             matches!(&expr.expr_struct, 
Some(AggExprStruct::Count(count))
                                 if count.children.len() == 1)
+                                // A decimal result is built as `AvgDecimal`. 
Any other result is
+                                // Comet's Float64 `Avg`, which cannot convert 
rows to states.
+                                || matches!(&expr.expr_struct, 
Some(AggExprStruct::Avg(avg))
+                                    if avg.child.is_some()
+                                        && 
avg.datatype.as_ref().is_some_and(|datatype| {
+                                            datatype.type_id == 
DataTypeId::Decimal as i32

Review Comment:
   This gate changes the answer for valid `DECIMAL(38,0)` inputs. I reproduced 
it with the Rust accumulator. Let `M = 10^38 - 1`. After the probe, two map 
tasks each see `[M, 1-M]` for the same key. Normally each emits `(sum=1, 
count=2)` and the final average is `0.5000`. With skipping, the reducer adds 
`M, 1-M, M, 1-M`. The third addition is `1 + M`, and `merge_batch` marks the 
group as overflowed permanently. I got `NULL` in legacy mode and an overflow 
error in ANSI mode. Could we keep cases where regrouping can change overflow 
out of this gate and add a two-task cancellation test?



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