comphead opened a new issue, #7660:
URL: https://github.com/apache/datafusion-comet/issues/7660

   ### What is the problem the feature request solves?
   
   `SumDecimalGroupsAccumulator` and `AvgDecimalGroupsAccumulator` check the 
running sum against the buffer precision on every input row ([`sum_decimal.rs` 
L411-L432](https://github.com/apache/datafusion-comet/blob/36ab96ad8a692ddd0cc0bd89bd645d74a11b1c6f/native/spark-expr/src/agg_funcs/sum_decimal.rs#L411-L432),
 [`avg_decimal.rs` 
L425-L438](https://github.com/apache/datafusion-comet/blob/36ab96ad8a692ddd0cc0bd89bd645d74a11b1c6f/native/spark-expr/src/agg_funcs/avg_decimal.rs#L425-L438)).
 `merge_batch` repeats the check for every partial row 
([L594](https://github.com/apache/datafusion-comet/blob/36ab96ad8a692ddd0cc0bd89bd645d74a11b1c6f/native/spark-expr/src/agg_funcs/sum_decimal.rs#L594),
 
[L522-L523](https://github.com/apache/datafusion-comet/blob/36ab96ad8a692ddd0cc0bd89bd645d74a11b1c6f/native/spark-expr/src/agg_funcs/avg_decimal.rs#L522-L523)).
   
   The latch itself is Spark behavior. Spark adds into the buffer with 
`DecimalAddNoOverflowCheck` ([`Sum.scala` 
L91](https://github.com/apache/spark/blob/v4.1.3/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/Sum.scala#L91),
 [`Average.scala` 
L86](https://github.com/apache/spark/blob/v4.1.3/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/aggregate/Average.scala#L86))
 and writes each update into the group's `UnsafeRow` ([`CodeGenerator.scala` 
L1763](https://github.com/apache/spark/blob/v4.1.3/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/codegen/CodeGenerator.scala#L1763)).
 For a precision above 18, `UnsafeRow.setDecimal` nulls a value that no longer 
fits ([`UnsafeRow.java` 
L286](https://github.com/apache/spark/blob/v4.1.3/sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/UnsafeRow.java#L286)).
 So an intermediate overshoot latches even when later rows bring the sum back, 
see #6002 and #604
 1. But for most inputs no running sum can leave the precision within a batch, 
and the check costs more than the add.
   
   **TPC-H Q1 profile.** SF10, `local[8]`, Spark 4.1.3, Comet `780a860`, 
async-profiler CPU sampling for 10 s (65,374 samples). Q1 has 4 groups, 4 
decimal SUMs and 3 decimal AVGs.
   
   | Frame | CPU samples |
   |---|---|
   | `SumDecimalGroupsAccumulator`, inclusive | 10.8% |
   | of which `update_single` self | 7.1% |
   | of which `Decimal128Type::is_valid_decimal_precision` | 2.9% |
   | `AvgDecimalGroupsAccumulator::update_batch` self | 2.4% |
   | `Decimal128Type::is_valid_decimal_precision` called from it | 3.4% |
   
   The last row is attributed by elimination. async-profiler drops the caller 
of a leaf function on arm64, so these samples show 
`PartialHashAggregateStream::poll_next` as their parent.
   
   **Release builds call the check out of line.** In the `libcomet.dylib` of 
`780a860` and in a local release build from 2026-10-30, both `<Decimal128Type 
as DecimalType>::is_valid_decimal_precision` and 
`SumDecimalGroupsAccumulator::update_single` are separate functions. arrow-rs 
does not mark that method `#[inline]` ([`types.rs` L1540 in 
59.3.2](https://github.com/apache/arrow-rs/blob/59.3.2/arrow-array/src/types.rs#L1540)).
 Until #5160, Comet used its own same-crate `#[inline]` copy. I have not 
confirmed that #5160 is what moved the call out of line.
   
   **Accumulator benchmark.** Calls `update_batch` directly with 10 batches of 
8192 rows, no nulls, and values that never overflow. The input is DECIMAL(15,2) 
into a DECIMAL(25,2) buffer, as in Q1's `sum(l_quantity)`. ns per row:
   
   | Accumulator | Check out of line, as in release | Check inlined | No check 
| DataFusion `sum` |
   |---|---|---|---|---|
   | SUM, 4 groups | 2.25 | 1.85 | 1.52 | 1.27 |
   | SUM, 1024 groups | 2.27 | 1.82 | 1.36 | 0.37 |
   | AVG, 4 groups | 1.25 | 1.14 | 1.27 | |
   | AVG, 1024 groups | 1.28 | 0.93 | 0.58 | |
   
   - A DECIMAL(38,6) input and buffer (precision capped) gives the same numbers 
within 4%.
   - A bench build of the current code inlines the check. The first column 
forces it out of line with `#[inline(never)]` to match release builds.
   - "No check" drops only the precision test and keeps the `i128` overflow 
test. It is a ceiling, not a valid implementation.
   - With 4 groups, consecutive rows update the same few slots, so the loop is 
likely bound by store-to-load forwarding. DataFusion's `sum` is also at 1.27 ns 
per row there, and AVG without the check gets no faster.
   - Without the check, Comet's grouped SUM is still 3.7x DataFusion's at 1024 
groups. So its `Vec<Option<i128>>` and `Vec<bool>` state costs on its own.
   
   ### Describe the potential solution
   
   1. **Skip the per-row check when a batch cannot overflow.** Keep an upper 
bound `B` on `|sum|` over all groups. Before each batch, bound its total `R = 
Σ|v|`. When the buffer precision is not capped, the input precision gives `R` 
for free (`rows × (10^p_in - 1)`). Otherwise one pass over the values gives it. 
If `B + R <= 10^p - 1`, no running sum of any group can leave the precision 
during the batch. A plain `sums[g] += v` loop then gives identical results, 
including ANSI, and `B += R`. Otherwise, recompute `B` from the sums, and if 
the batch still does not fit, run today's per-row loop. For Q1's 
`sum(l_quantity)` the static bound alone covers about 1.2 million batches 
(10^10 rows) before the first recompute. `merge_batch` can apply the same test, 
with the partial sums as `R`.
   2. **Inline the check that remains.** Load 
`MIN_DECIMAL128_FOR_EACH_PRECISION[p]` and 
`MAX_DECIMAL128_FOR_EACH_PRECISION[p]` once per batch and compare directly, as 
`CheckOverflow` does in #7658. Alternatively, mark `is_valid_decimal_precision` 
`#[inline]` in arrow-rs.
   3. **Store SUM state the way AVG does.** Use `Vec<i128>` plus validity and 
emptiness bitmaps, instead of `Vec<Option<i128>>` (32 bytes per group) and 
`Vec<bool>`.
   
   The ungrouped `AvgDecimalAccumulator::update_batch` also validates every 
row, through `iter().flatten()`. The ungrouped `SumDecimalAccumulator` already 
defers the check to `state` and `evaluate` (#6041).
   
   ### Additional context
   
   - #951 (closed 2026-05-06) tracked Comet's decimal aggregates being slower 
than DataFusion's. A comment there asked whether `update_single` needs 
`is_valid_decimal_precision` at all. #6002 and #6041 later showed that the 
grouped latch has to stay per row. Item 1 keeps that behavior and only drops 
checks that cannot fire.
   - For a buffer precision of 18 or less, Spark's compact `UnsafeRow` path 
does not check the precision, so Spark does not latch there and Comet does. 
Reaching that bound takes more than 10^10 rows of an input with 8 or fewer 
digits, so this issue leaves it alone.
   - The other `is_valid_decimal_precision` samples in the Q1 profile (5.4%) 
come from `CheckOverflow`. #7658 replaces that scan for arrays without nulls.
   - Measured on a shared Apple M-series laptop (load average 4 to 8). 
DataFusion's `sum`, which was unchanged across runs, moved by at most 3.3%.
   
   <details>
   <summary>Benchmark source (register as a <code>[[bench]]</code> in 
<code>native/spark-expr/Cargo.toml</code>)</summary>
   
   ```rust
   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;
   
   /// Q1-like group ids: 4 groups in random order with TPC-H Q1 weights (25%, 
0.6%, 50%, 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 values(precision: u8, scale: i8, scaled: impl Fn(usize) -> i128) -> 
ArrayRef {
       Arc::new(
           (0..ROWS)
               .map(|i| Some(scaled(i)))
               .collect::<Decimal128Array>()
               .with_precision_and_scale(precision, scale)
               .unwrap(),
       )
   }
   
   fn drive(
       c: &mut Criterion,
       name: &str,
       make: &dyn Fn() -> Box<dyn GroupsAccumulator>,
       arrays: &[ArrayRef],
       groups: &[usize],
       num_groups: usize,
   ) {
       c.bench_function(name, |b| {
           b.iter(|| {
               let mut acc = make();
               for _ in 0..BATCHES {
                   acc.update_batch(arrays, groups, None, 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();
   
       // Q1 `sum(l_quantity)`: DECIMAL(15,2) input, DECIMAL(25,2) buffer, 
values 1.00..50.00.
       let narrow = values(15, 2, |i| ((i % 50) as i128 + 1) * 100);
       // Q1 `sum(charge)`: DECIMAL(38,6) input and buffer (precision capped), 
values ~1e5 units.
       let wide = values(38, 6, |i| ((i % 50) as i128 + 1) * 1_000_000_000);
   
       let cases: [(&str, &ArrayRef, u8, i8, u8, i8); 2] = [
           ("p25", &narrow, 25, 2, 19, 6),
           ("p38", &wide, 38, 6, 38, 10),
       ];
   
       for (type_name, input, sum_p, sum_s, avg_p, avg_s) in cases {
           let in_type = input.data_type().clone();
           let schema = Schema::new(vec![Field::new("c0", in_type.clone(), 
false)]);
           let expr_fields = vec![Arc::new(Field::new("c0", in_type.clone(), 
false))];
           let sum_type = DataType::Decimal128(sum_p, sum_s);
           let avg_type = DataType::Decimal128(avg_p, avg_s);
           let arrays = vec![Arc::clone(input)];
   
           let args = |ret: &DataType| AccumulatorArgs {
               return_field: Arc::new(Field::new("r", ret.clone(), true)),
               schema: &schema,
               ignore_nulls: false,
               order_bys: &[],
               name: "agg",
               is_distinct: false,
               is_reversed: false,
               exprs: &[],
               expr_fields: &expr_fields,
           };
   
           for (groups_name, groups, num_groups) in
               [("4 groups", &q1, 4usize), ("1024 groups", &cyclic, 1024usize)]
           {
               let comet_sum = SumDecimal::try_new(
                   sum_type.clone(),
                   EvalMode::Legacy,
                   None,
                   create_query_context_map(),
               )
               .unwrap();
               drive(
                   c,
                   &format!("comet sum {type_name} {groups_name}"),
                   &|| 
comet_sum.create_groups_accumulator(args(&sum_type)).unwrap(),
                   &arrays,
                   groups,
                   num_groups,
               );
   
               let comet_avg = AvgDecimal::new(
                   avg_type.clone(),
                   sum_type.clone(),
                   EvalMode::Legacy,
                   None,
                   create_query_context_map(),
               );
               drive(
                   c,
                   &format!("comet avg {type_name} {groups_name}"),
                   &|| 
comet_avg.create_groups_accumulator(args(&avg_type)).unwrap(),
                   &arrays,
                   groups,
                   num_groups,
               );
   
               let df_sum = sum_udaf();
               drive(
                   c,
                   &format!("datafusion sum {type_name} {groups_name}"),
                   &|| 
df_sum.create_groups_accumulator(args(&sum_type)).unwrap(),
                   &arrays,
                   groups,
                   num_groups,
               );
           }
       }
   }
   
   criterion_group!(benches, criterion_benchmark);
   criterion_main!(benches);
   ```
   
   </details>
   


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