mbutrovich commented on code in PR #7674:
URL: https://github.com/apache/datafusion-comet/pull/7674#discussion_r4607689007


##########
docs/source/contributor-guide/expression-audits/agg_funcs.md:
##########
@@ -44,6 +44,7 @@
 - Spark 3.4.3 (2026-05-26)
 - Spark 3.5.8 (2026-05-26): aggregate logic identical to 3.4.3
 - Spark 4.0.1 (2026-05-26): aggregate logic identical to 3.5.8; only 
`QueryContext` import path differs. `YearMonthIntervalType` and 
`DayTimeIntervalType` inputs (supported by Spark) fall back to Spark in Comet.
+- Performance (tuned 2026-11-17): the grouped decimal accumulator loads the 
sum's precision bounds once and compares against them on every update and 
merge, instead of calling `Decimal128Type::is_valid_decimal_precision`, which 
release builds keep out of line. Bench builds already inline that call: 2% to 
5% faster on most shapes, the 4-group shape within 1%. Benchmark: 
`benches/decimal_groups.rs`.

Review Comment:
   Could we drop the AVG percentages here? DataFusion's `sum`, which this PR 
doesn't touch, moved -1.8% and +0.5% in the same runs. The AVG shapes moved 
between +1.1% and -4.6%, which is within about twice that noise floor on a 
laptop with a load average of 4. The change this entry can state is the call 
that release builds no longer make. The other entries in this file also link 
their PR.
   
   ```suggestion
   - Performance (tuned 2026-11-17, PR 
[#7674](https://github.com/apache/datafusion-comet/pull/7674)): the grouped 
decimal accumulator loads the sum's precision bounds once and compares against 
them on every update and merge, instead of calling 
`Decimal128Type::is_valid_decimal_precision`, which release builds keep out of 
line. Benchmark: `benches/decimal_groups.rs`.
   ```



##########
docs/source/contributor-guide/expression-audits/agg_funcs.md:
##########
@@ -118,4 +119,8 @@
 - Spark 4.2.0 (audited 2026-10-12): the `TreeMap` is chosen for every pivot 
column type without proper equals (`!TypeUtils.typeWithProperEquals`), which 
adds binary and strings with a non-binary-equality collation, so binary pivot 
values now match by content. A `findPivotIndex` wrapper returns `-1` for a null 
key on the `TreeMap` path instead of throwing.
 - `CometPivotFirst` (in 
`spark/src/main/scala/org/apache/comet/serde/aggregates.scala`) forwards the 
aggregate to the native `SparkPivotFirst` UDAF 
(`native/spark-expr/src/agg_funcs/pivot_first.rs`) when the value type is in 
the supported set and the pivot column is a Boolean, Byte, Short, Int, Long, 
Float, Double, Decimal, UTF8_BINARY String, Date, Timestamp or TimestampNTZ. 
Those are the types Spark keys by value equality on every version, which the 
native lookup reproduces, including the float rules. Other pivot columns fall 
back, as does a list where `indexSize` is smaller than the list (duplicates, 
including `0.0` next to `-0.0`). State layout matches Spark's 
`aggBufferAttributes` (one scalar column per pivot slot) so the shuffle schema 
between Partial and Final stays consistent. The result reassembles the slots 
into a `ListArray` matching `PivotFirst.dataType = ArrayType(valueDataType)`.
 
+## sum
+
+- Performance (tuned 2026-11-17): the grouped decimal accumulator loads the 
result's precision bounds once and compares against them on every update, 
merge, and evaluate, instead of calling 
`Decimal128Type::is_valid_decimal_precision`, which release builds keep out of 
line. 9% to 12% faster on most shapes even in bench builds, which inline that 
call. Benchmark: `benches/decimal_groups.rs`.

Review Comment:
   The 9% to 12% comes from bench builds on an Apple M-series laptop. Comet 
deploys on x86-64 servers, and Apple silicon results don't predict those. Could 
you rerun `decimal_groups` on an x86-64 machine before putting a number in the 
audit, or drop it as in the AVG entry? Here is the version without it, with the 
PR link the other entries carry:
   
   ```suggestion
   - Performance (tuned 2026-11-17, PR 
[#7674](https://github.com/apache/datafusion-comet/pull/7674)): the grouped 
decimal accumulator loads the result's precision bounds once and compares 
against them on every update, merge, and evaluate, instead of calling 
`Decimal128Type::is_valid_decimal_precision`, which release builds keep out of 
line. Benchmark: `benches/decimal_groups.rs`.
   ```



##########
native/spark-expr/src/agg_funcs/avg_decimal.rs:
##########
@@ -398,7 +398,7 @@ impl AvgDecimalGroupsAccumulator {
             target_precision,
             target_scale,
             sum_data_type: sum_data_type.clone(),
-            sum_precision,
+            sum_bounds: Decimal128Bounds::new(sum_precision),

Review Comment:
   Could we add a test that checks the latch below precision 38? Every grouped 
AVG test uses precision 38 for both the sum and the result, so swapping 
`sum_precision` for `target_precision` here would pass them all. The SQL tests 
can't cover it either, because inputs within their declared precision need more 
than 10^10 rows to reach a sum precision below 38. This test fails if the line 
uses `target_precision`, and passes as written:
   
   ```rust
       #[test]
       fn grouped_latches_at_the_sum_precision_below_38() -> Result<()> {
           // A DECIMAL(15,2) input sums as DECIMAL(25,2) and averages as 
DECIMAL(19,6), so a latch
           // that used the result precision would also null group 0.
           let max = 10_i128.pow(25) - 1;
           let new_acc = || {
               AvgDecimalGroupsAccumulator::new(
                   &DataType::Decimal128(19, 6),
                   &DataType::Decimal128(25, 2),
                   19,
                   6,
                   25,
                   2,
                   EvalMode::Legacy,
                   None,
                   crate::create_query_context_map(),
               )
           };
           let sums = || Decimal128Array::from(vec![max, max, 
1]).with_precision_and_scale(25, 2);
           let mut updated = new_acc();
           updated.update_batch(&[Arc::new(sums()?)], &[0, 1, 1], None, 2)?;
           let mut merged = new_acc();
           merged.merge_batch(
               &[Arc::new(sums()?), Arc::new(Int64Array::from(vec![1; 3]))],
               &[0, 1, 1],
               2,
           )?;
           for mut acc in [updated, merged] {
               let state = acc.state(EmitTo::All)?;
               let sums = state[0].as_primitive::<Decimal128Type>();
               assert_eq!(sums.iter().collect::<Vec<_>>(), vec![Some(max), 
None]);
           }
           Ok(())
       }
   ```



##########
native/spark-expr/benches/decimal_groups.rs:
##########
@@ -0,0 +1,235 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+//! Grouped decimal SUM and AVG accumulators, driven directly through
+//! `GroupsAccumulator::update_batch` so the numbers cover the accumulators 
and not grouping or
+//! the execution framework.
+
+use arrow::array::{ArrayRef, Decimal128Array};
+use arrow::datatypes::{DataType, Field, Schema};
+use criterion::{criterion_group, criterion_main, Criterion};
+use datafusion::functions_aggregate::sum::sum_udaf;
+use datafusion::logical_expr::function::AccumulatorArgs;
+use datafusion::logical_expr::{AggregateUDFImpl, EmitTo, GroupsAccumulator};
+use datafusion_comet_spark_expr::{create_query_context_map, AvgDecimal, 
EvalMode, SumDecimal};
+use std::hint::black_box;
+use std::sync::Arc;
+
+const ROWS: usize = 8192;
+const BATCHES: usize = 10;
+
+/// TPC-H Q1 grouping: 4 groups in random order, weighted 25%, 0.6%, 50% and 
25%.
+fn q1_groups() -> Vec<usize> {
+    let mut x: u64 = 0x9E37_79B9_7F4A_7C15;
+    (0..ROWS)
+        .map(|_| {
+            x = x
+                .wrapping_mul(6364136223846793005)
+                .wrapping_add(1442695040888963407);
+            match (x >> 33) % 1000 {
+                0..=249 => 0,
+                250..=255 => 1,
+                256..=755 => 2,
+                _ => 3,
+            }
+        })
+        .collect()
+}
+
+fn decimals(precision: u8, scale: i8, value: impl Fn(usize) -> Option<i128>) 
-> ArrayRef {
+    Arc::new(
+        (0..ROWS)
+            .map(value)
+            .collect::<Decimal128Array>()
+            .with_precision_and_scale(precision, scale)
+            .unwrap(),
+    )
+}
+
+/// One input shape: values, group ids, and the (precision, scale) of the 
input and of the sum.
+struct Shape<'a> {
+    name: &'a str,
+    values: &'a ArrayRef,
+    groups: &'a [usize],
+    num_groups: usize,
+    types: ((u8, i8), (u8, i8)),
+}
+
+impl<'a> Shape<'a> {
+    fn new(
+        name: &'a str,
+        values: &'a ArrayRef,
+        groups: &'a [usize],
+        num_groups: usize,
+        types: ((u8, i8), (u8, i8)),
+    ) -> Self {
+        Self {
+            name,
+            values,
+            groups,
+            num_groups,
+            types,
+        }
+    }
+}
+
+fn bench(
+    c: &mut Criterion,
+    name: &str,
+    make: &dyn Fn() -> Box<dyn GroupsAccumulator>,
+    shape: &Shape,
+) {
+    let values = [Arc::clone(shape.values)];
+    c.bench_function(name, |b| {
+        b.iter(|| {
+            let mut acc = make();
+            for _ in 0..BATCHES {
+                acc.update_batch(&values, shape.groups, None, shape.num_groups)
+                    .unwrap();
+            }
+            black_box(acc.evaluate(EmitTo::All).unwrap())
+        })
+    });
+}
+
+fn criterion_benchmark(c: &mut Criterion) {
+    let q1 = q1_groups();
+    let cyclic: Vec<usize> = (0..ROWS).map(|i| i % 1024).collect();
+    // A bijection on 0..65536, so each batch touches 8192 scattered groups.
+    let scattered: Vec<usize> = (0..ROWS)
+        .map(|i| (i as u64 * 2654435761 % 65536) as usize)
+        .collect();
+
+    // Q1's `l_quantity`: DECIMAL(15,2) values 1.00..50.00 summed as 
DECIMAL(25,2).
+    let quantity = decimals(15, 2, |i| Some(((i % 50) as i128 + 1) * 100));
+    let quantity_nulls = decimals(15, 2, |i| {
+        (i % 10 != 0).then(|| ((i % 50) as i128 + 1) * 100)
+    });
+    // A DECIMAL(38,6) input keeps the sum at the capped precision 38. Values 
near 1e5 stay far
+    // from it. Values of 1e27 to 5e28 come within a few batches of it without 
overflowing.

Review Comment:
   This comment doesn't match what the `near limit` shape does. The groups are 
`i % 1024` and the sign alternates on `i % 2`. Because 1024 is even, every row 
of a group has the same sign, so nothing cancels. Each group adds 8 rows per 
batch, and after the 10 batches the largest running sum is 2.8e30 units, 2.8% 
of the 1e32 limit. No row reaches the latch, so in this PR the shape runs the 
same path as `(38,6), 1024 groups`.
   
   What the shape does guarantee is that one batch's absolute values sum to 
2.1e32 units, past the limit. That makes a bound on the batch's total, like 
item 1 in the #7660 plan, fall back to the per-row loop, which is the 
regression gate the plan describes. Could the comment say that?
   
   ```suggestion
       // A DECIMAL(38,6) input keeps the sum at the capped precision 38. 
Values near 1e5 stay far
       // from it. Values of 1e27 to 5e28 have absolute values that sum to 
2.1e32 per batch, past
       // the 1e32 limit, so a bound on a batch's total can't skip the per-row 
check. Each group's
       // rows share one sign, and no running sum passes 3% of the limit, so no 
group latches.
   ```



##########
native/spark-expr/src/agg_funcs/sum_decimal.rs:
##########
@@ -384,7 +386,7 @@ impl SumDecimalGroupsAccumulator {
             sum: Vec::new(),
             is_empty: Vec::new(),
             result_type,
-            precision,
+            bounds: Decimal128Bounds::new(precision),

Review Comment:
   Could we add the same check for grouped SUM? Its only grouped tests use 
`Decimal128(10, 2)` sums that stay small, and the overflow tests cover the 
ungrouped accumulator at precision 38. This test runs update and merge in both 
eval modes, and it fails if the bounds come from another precision:
   
   ```rust
       #[test]
       fn grouped_latches_at_the_result_precision_below_38() {
           use datafusion::logical_expr::{EmitTo, GroupsAccumulator};
   
           // Inputs within their declared precision need more than 10^10 rows 
to reach a sum
           // precision below 38, so only a unit test can check that the latch 
uses the result
           // precision there.
           let data_type = DataType::Decimal128(25, 2);
           let max = 10_i128.pow(25) - 1;
           let decimals = |values: Vec<i128>| -> ArrayRef {
               
Arc::new(Decimal128Array::from(values).with_data_type(data_type.clone()))
           };
           for eval_mode in [EvalMode::Legacy, EvalMode::Ansi] {
               let new_acc = || {
                   SumDecimalGroupsAccumulator::new(
                       data_type.clone(),
                       25,
                       eval_mode,
                       None,
                       crate::create_query_context_map(),
                   )
               };
               // Group 0 reaches the largest value that fits and group 1 goes 
one past it.
               let mut updated = new_acc();
               let update = updated.update_batch(&[decimals(vec![max, max, 
1])], &[0, 1, 1], None, 2);
               let mut merged = new_acc();
               let is_empty: ArrayRef = Arc::new(BooleanArray::from(vec![false; 
3]));
               let merge = merged.merge_batch(&[decimals(vec![max, max, 1]), 
is_empty], &[0, 1, 1], 2);
               for (result, mut acc) in [(update, updated), (merge, merged)] {
                   if eval_mode == EvalMode::Ansi {
                       assert!(result.is_err());
                   } else {
                       result.unwrap();
                       let sums = acc.evaluate(EmitTo::All).unwrap();
                       let sums = sums.as_primitive::<Decimal128Type>();
                       assert_eq!(sums.iter().collect::<Vec<_>>(), 
vec![Some(max), None]);
                   }
               }
           }
       }
   ```



##########
native/spark-expr/src/utils.rs:
##########
@@ -322,6 +324,33 @@ fn pre_timestamp_cast(array: ArrayRef, timezone: String) 
-> Result<ArrayRef, Arr
     }
 }
 
+/// The values that `Decimal128Type::is_valid_decimal_precision` accepts for 
one precision, looked
+/// up once so that a per-row check is two inline comparisons. Arrow does not 
mark that method
+/// `#[inline]`, and release builds call it out of line on every row.

Review Comment:
   Could you file an arrow-rs issue to mark `is_valid_decimal_precision` 
`#[inline]`, and link it from this comment? The [`Decimal128Type` 
impl](https://github.com/apache/arrow-rs/blob/e28432bd5cfd834a08aedbc31bb6a3766362b44d/arrow-array/src/types.rs#L1557-L1559)
 still lacks it on arrow-rs `main`. The published builds show the same 
out-of-line call as your macOS table. In the 1.2.0 jar, whose `sum_decimal.rs`, 
`avg_decimal.rs`, and `utils.rs` match this PR's base, both 
`linux/amd64/libcomet.so` (x86-64-v3) and `linux/aarch64/libcomet.so` 
(neoverse-n1) call it from `update_single` once, AVG `update_batch` twice, both 
`merge_batch`es once, and SUM `evaluate` twice. On amd64 the call goes through 
the GOT:
   
   ```asm
   movzx  edx, byte ptr [r14 + 0x61]    # precision
   mov    rdi, r12                      # new_sum, low half
   mov    rsi, r13                      # new_sum, high half
   call   qword ptr [rip + 0x4da27d4]   # GOT slot 0x6d64218 -> 
Decimal128Type::is_valid_decimal_precision
   ```
   
   arrow-cast's decimal casts make the same call from another crate 
([`decimal.rs` 
L269](https://github.com/apache/arrow-rs/blob/e28432bd5cfd834a08aedbc31bb6a3766362b44d/arrow-cast/src/cast/decimal.rs#L269)),
 so other arrow-rs users would get the change too. The #7660 plan lists it for 
later, and an issue keeps it from getting lost.
   
   `Decimal128Bounds` stays useful after that lands, since your bench builds 
inline the call and still show SUM faster. The comment can say both reasons, so 
it doesn't go stale when arrow adds `#[inline]`:
   
   ```suggestion
   /// The values that `Decimal128Type::is_valid_decimal_precision` accepts for 
one precision, looked
   /// up once so that a per-row check is two comparisons, with no precision 
guard or table lookup.
   /// Arrow does not mark that method `#[inline]` (<arrow-rs issue link>), so 
release builds also
   /// call it out of line on every row.
   ```



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