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


##########
native/core/src/execution/jni_api.rs:
##########
@@ -1033,6 +1037,29 @@ pub extern "system" fn 
Java_org_apache_comet_Native_setOutputStream(
     })
 }
 
+/// Whether `avg` is built as `AvgDecimal` (a decimal result; any other result 
is Comet's Float64
+/// `Avg`, which cannot convert rows to states) and no order of adding its 
values can overflow the
+/// sum. Skipping changes the order in which the final aggregate adds a 
group's values, and a sum
+/// that leaves its precision stays null, so `[M, 1 - M]` in each of two tasks 
gives 1 + 1 when
+/// aggregated but overflows at 1 + M when skipped. Spark gives the sum 10 
more digits than the
+/// input up to precision 38, so a sum below 38 has all of them, and a group 
needs more than 10^10
+/// values before any partial sum can leave it.
+fn avg_decimal_cannot_overflow(avg: &Avg) -> bool {
+    let decimal_precision = |datatype: Option<&ProtoDataType>| match datatype {
+        Some(datatype) if datatype.type_id == DataTypeId::Decimal as i32 => {
+            match datatype.type_info.as_ref()?.datatype_struct.as_ref()? {
+                DatatypeStruct::Decimal(decimal) => Some(decimal.precision),
+                _ => None,
+            }
+        }
+        _ => None,
+    };
+    avg.child.is_some()
+        && decimal_precision(avg.datatype.as_ref()).is_some()
+        && decimal_precision(avg.sum_datatype.as_ref())
+            .is_some_and(|precision| precision < DECIMAL128_MAX_PRECISION as 
i32)

Review Comment:
   You're right, and I don't see a way to close this with a precision rule: the 
10^10 bound is on the values in one group, and nothing bounds the group size, 
as your seeded state shows. Making the merge overflow-safe would fix the 
skipped case but change results without skipping: the grouped merge stays NULL 
once the running sum leaves its precision, matching Spark, so a wider merge 
would return sums where Spark returns NULL or raises in ANSI mode.
   
   So I don't think decimal AVG can be admitted with the same answers as 
aggregation for every input. I see two options: drop it, which leaves 
`convert_to_state` without a caller, so I'd close this PR; or keep it with the 
limit documented (a group with more than 10^10 values can differ when skipping 
is on), since the flag is opt-in and experimental, and Spark's own result for 
such a group already depends on how the input is split into tasks. I lean 
towards dropping it unless you think the documented limit is acceptable. Which 
would you prefer?
   
   



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