You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@auron.apache.org by ri...@apache.org on 2026/09/18 02:52:55 UTC

(auron) branch master updated: [AURON #2530] Fix incorrect results in partial merge aggregation with sliced batches (#2531)

This is an automated email from the ASF dual-hosted git repository.

richox pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/auron.git


The following commit(s) were added to refs/heads/master by this push:
     new 3d37d5408 [AURON #2530] Fix incorrect results in partial merge aggregation with sliced batches (#2531)
3d37d5408 is described below

commit 3d37d54084e0d0b02b6b55923bc71b5342e656ec
Author: Yizhong Zhang <zy...@gmail.com>
AuthorDate: Fri Sep 18 10:52:51 2026 +0800

    [AURON #2530] Fix incorrect results in partial merge aggregation with sliced batches (#2531)
    
    # Which issue does this PR close?
    
    Closes #2530
    
    # Rationale for this change
    Sorted aggregation's partial merge path produces silently wrong results
    when a batch is processed in multiple row-range slices. Two bugs
    compound:
    1. AccPrimColumn and AccBytesColumn apply array offsets twice when
    unfreezing from sliced Arrow arrays
    2. The merging accumulator table is redundantly rebuilt per slice with
    incorrect (relative instead of absolute) index ranges.
    
    # What changes are included in this PR?
    - acc.rs: Remove double-offset in AccPrimColumn::unfreeze_from_arrays
    and use raw buffer access in AccBytesColumn::unfreeze_from_arrays
    - agg_ctx.rs: Extract create_merging_acc_table() that unfreezes acc
    arrays once per batch; pass it through slices via &mut Option<AccTable>;
    use absolute
      index ranges in partial_merge
    - agg_exec.rs: Thread merging_acc_table through both sorted-agg call
    sites
      - 2 new unit tests verifying split-range and sliced-batch correctness
    
    # Are there any user-facing changes?
    No.
    
    # How was this patch tested?
    UTs.
    
    Co-authored-by: zhangyizhong <zh...@kuaishou.com>
---
 native-engine/datafusion-ext-plans/src/agg/acc.rs  |   4 +-
 .../datafusion-ext-plans/src/agg/agg_ctx.rs        | 175 ++++++++++++++++++---
 native-engine/datafusion-ext-plans/src/agg_exec.rs |   6 +
 3 files changed, 158 insertions(+), 27 deletions(-)

diff --git a/native-engine/datafusion-ext-plans/src/agg/acc.rs b/native-engine/datafusion-ext-plans/src/agg/acc.rs
index 7dbf2cc59..19eb5e5f6 100644
--- a/native-engine/datafusion-ext-plans/src/agg/acc.rs
+++ b/native-engine/datafusion-ext-plans/src/agg/acc.rs
@@ -344,7 +344,7 @@ impl<T: ArrowNativeType> AccColumn for AccPrimColumn<T> {
 
         // fill values
         let buffer = array_data.buffer::<T>(0);
-        self.values = buffer[array_data.offset()..][..array_data.len()].to_vec();
+        self.values = buffer[..array_data.len()].to_vec();
 
         // fill valids
         if let Some(nb) = array_data.nulls() {
@@ -540,7 +540,7 @@ impl AccColumn for AccBytesColumn {
 
         // fill values
         let offset_buffer = array_data.buffer::<i32>(0);
-        let data_buffer = array_data.buffer::<u8>(1);
+        let data_buffer = array_data.buffers()[1].as_slice();
         for i in 0..array_data.len() {
             if array_data.is_valid(i) {
                 let offset_begin = offset_buffer[i] as usize;
diff --git a/native-engine/datafusion-ext-plans/src/agg/agg_ctx.rs b/native-engine/datafusion-ext-plans/src/agg/agg_ctx.rs
index 00df68b5c..20308e541 100644
--- a/native-engine/datafusion-ext-plans/src/agg/agg_ctx.rs
+++ b/native-engine/datafusion-ext-plans/src/agg/agg_ctx.rs
@@ -259,7 +259,27 @@ impl AggContext {
         acc_table: &mut AccTable,
         acc_idx: IdxSelection,
     ) -> Result<()> {
-        self.update_batch_slice_to_acc_table(batch, 0, batch.num_rows(), acc_table, acc_idx)
+        self.update_batch_slice_to_acc_table(
+            batch,
+            0,
+            batch.num_rows(),
+            acc_table,
+            acc_idx,
+            &mut None,
+        )
+    }
+
+    pub fn create_merging_acc_table(&self, batch: &RecordBatch) -> Result<AccTable> {
+        let mut merging_acc_table = self.create_acc_table(0);
+        let mut acc_arrays_start = batch.num_columns() - self.input_acc_arrays_len;
+
+        for (agg_idx, agg) in &self.need_partial_merge_aggs {
+            let num_acc_arrays = agg.acc_array_data_types().len();
+            let acc_arrays = &batch.columns()[acc_arrays_start..][..num_acc_arrays];
+            acc_arrays_start += num_acc_arrays;
+            merging_acc_table.cols_mut()[*agg_idx].unfreeze_from_arrays(acc_arrays)?;
+        }
+        Ok(merging_acc_table)
     }
 
     pub fn update_batch_slice_to_acc_table(
@@ -269,6 +289,7 @@ impl AggContext {
         batch_end_idx: usize,
         acc_table: &mut AccTable,
         acc_idx: IdxSelection,
+        merging_acc_table: &mut Option<AccTable>,
     ) -> Result<()> {
         // NOTE:
         // arrow-ffi with sliced batch is buggy in older arrow-java, so we use unsliced
@@ -375,22 +396,14 @@ impl AggContext {
 
         // partial merge
         if self.need_partial_merge {
-            let mut merging_acc_table = self.create_acc_table(0);
-
-            if self.need_partial_merge {
-                let mut acc_arrays_start = batch.num_columns() - self.input_acc_arrays_len;
-                for (agg_idx, agg) in &self.need_partial_merge_aggs {
-                    let acc_arrays = &batch.columns()[acc_arrays_start..]
-                        .iter()
-                        .take(agg.acc_array_data_types().len())
-                        .map(|array| array.slice(batch_start_idx, batch_end_idx - batch_start_idx))
-                        .collect::<Vec<_>>();
-                    acc_arrays_start += agg.acc_array_data_types().len();
-                    merging_acc_table.cols_mut()[*agg_idx].unfreeze_from_arrays(&acc_arrays)?;
+            let merging_acc_table = match merging_acc_table {
+                Some(merging_acc_table) => merging_acc_table,
+                merging_acc_table => {
+                    merging_acc_table.insert(self.create_merging_acc_table(batch)?)
                 }
-            }
-            let batch_selection = IdxSelection::Range(0, batch_end_idx - batch_start_idx);
-            self.partial_merge(acc_table, acc_idx, &mut merging_acc_table, batch_selection)?;
+            };
+            let batch_selection = IdxSelection::Range(batch_start_idx, batch_end_idx);
+            self.partial_merge(acc_table, acc_idx, merging_acc_table, batch_selection)?;
         }
         Ok(())
     }
@@ -652,13 +665,13 @@ impl AggContext {
 #[cfg(test)]
 mod tests {
     use arrow::{
-        array::{Float64Array, Int64Array},
-        datatypes::DataType,
+        array::{AsArray, Float64Array, Int64Array},
+        datatypes::{DataType, Float64Type, Int64Type},
     };
     use datafusion::physical_expr::expressions::Column;
 
     use super::*;
-    use crate::agg::{AggFunction, agg::create_agg};
+    use crate::agg::{AggFunction, agg::create_agg, count::AggCount, sum::AggSum};
 
     #[test]
     fn filtered_cached_indices_preserve_slice_mapping() {
@@ -766,13 +779,13 @@ mod tests {
             .collect::<Result<Vec<_>>>()?;
         let partial = context(exec_mode, schema, AggMode::Partial, &aggs)?;
         let mut table = partial.create_acc_table(0);
-        // Group 19 has no matching rows, but must still have all accumulator slots.
         partial.update_batch_slice_to_acc_table(
             &batch,
             1,
             6,
             &mut table,
             IdxSelection::IndicesU32(&[2, 7, 2, 19, 7]).with_cached_max(),
+            &mut None,
         )?;
         assert!(table.cols().iter().all(|col| col.num_records() >= 20));
         if multiple_batches {
@@ -797,14 +810,13 @@ mod tests {
                     ])),
                 ],
             )?;
-            // Reuse the table: the selected NULL must not increment COUNT(value)
-            // or AVG's count. Group 2 accumulates 70; new group 31 is filtered out.
             partial.update_batch_slice_to_acc_table(
                 &next_batch,
                 1,
                 5,
                 &mut table,
                 IdxSelection::IndicesU32(&[2, 2, 7, 31]).with_cached_max(),
+                &mut None,
             )?;
             assert!(table.cols().iter().all(|col| col.num_records() >= 32));
         }
@@ -823,8 +835,6 @@ mod tests {
             &aggs,
         )?;
         let mut table = merging.create_acc_table(0);
-        // Merge group 2 twice and group 7 once. The second batch adds 70
-        // to group 2, so its contribution is also merged twice.
         let (expected_count, expected_sum) = if multiple_batches {
             (7, 270.)
         } else {
@@ -837,6 +847,7 @@ mod tests {
             5,
             &mut table,
             IdxSelection::Indices(&[4, 4, 4, 9]).with_cached_max(),
+            &mut None,
         )?;
         let merged_batch = RecordBatch::try_new(
             merging.output_schema.clone(),
@@ -854,6 +865,7 @@ mod tests {
             3,
             &mut table,
             IdxSelection::IndicesU32(&[8, 15]).with_cached_max(),
+            &mut None,
         )?;
         let results = final_ctx.build_agg_columns(
             &mut table,
@@ -872,7 +884,6 @@ mod tests {
             &Float64Array::from(vec![Some(expected_avg), None])
         );
 
-        // A Single destination broadcasts across the sliced partial buffers.
         let mut table = final_ctx.create_acc_table(0);
         final_ctx.update_batch_slice_to_acc_table(
             &merged_batch,
@@ -880,10 +891,124 @@ mod tests {
             3,
             &mut table,
             IdxSelection::Single(6),
+            &mut None,
         )?;
         let results = final_ctx.build_agg_columns(&mut table, IdxSelection::Single(6))?;
         assert_eq!(results[0].as_ref(), &Int64Array::from(vec![expected_count]));
         assert_eq!(results[2].as_ref(), &Float64Array::from(vec![expected_avg]));
         Ok(())
     }
+
+    fn partial_output_batch(num_rows: usize) -> RecordBatch {
+        let keys = Int64Array::from((0..num_rows as i64).map(|i| i / 3).collect::<Vec<_>>());
+        let sums = Float64Array::from((0..num_rows).map(|i| i as f64 * 1.5).collect::<Vec<_>>());
+        let counts = Int64Array::from((0..num_rows as i64).map(|i| i % 5).collect::<Vec<_>>());
+        RecordBatch::try_from_iter_with_nullable(vec![
+            ("key", Arc::new(keys) as ArrayRef, false),
+            ("sum", Arc::new(sums) as ArrayRef, true),
+            ("cnt", Arc::new(counts) as ArrayRef, false),
+        ])
+        .expect("failed to create test batch")
+    }
+
+    fn merging_agg_ctx(schema: SchemaRef) -> Result<AggContext> {
+        AggContext::try_new(
+            AggExecMode::SortAgg,
+            schema,
+            vec![GroupingExpr {
+                field_name: "key".to_string(),
+                expr: Arc::new(Column::new("key", 0)),
+            }],
+            vec![
+                AggExpr {
+                    field_name: "sum".to_string(),
+                    mode: AggMode::Final,
+                    agg: Arc::new(AggSum::try_new(
+                        Arc::new(Column::new("sum", 1)),
+                        DataType::Float64,
+                    )?),
+                    filter: None,
+                },
+                AggExpr {
+                    field_name: "cnt".to_string(),
+                    mode: AggMode::Final,
+                    agg: Arc::new(AggCount::try_new(
+                        vec![Arc::new(Column::new("cnt", 2))],
+                        DataType::Int64,
+                    )?),
+                    filter: None,
+                },
+            ],
+            false,
+            false,
+        )
+    }
+
+    fn merge_in_ranges(
+        agg_ctx: &AggContext,
+        batch: &RecordBatch,
+        split_points: &[usize],
+    ) -> Result<Vec<ArrayRef>> {
+        let num_rows = batch.num_rows();
+        let mut acc_table = agg_ctx.create_acc_table(num_rows);
+        let mut merging_acc_table = None;
+        let mut start = 0;
+
+        for &end in split_points.iter().chain(std::iter::once(&num_rows)) {
+            let acc_indices = (start..end).collect::<Vec<_>>();
+            agg_ctx.update_batch_slice_to_acc_table(
+                batch,
+                start,
+                end,
+                &mut acc_table,
+                IdxSelection::Indices(&acc_indices),
+                &mut merging_acc_table,
+            )?;
+            start = end;
+        }
+        agg_ctx.build_agg_columns(&mut acc_table, IdxSelection::Range(0, num_rows))
+    }
+
+    fn assert_columns_eq(expected: &[ArrayRef], actual: &[ArrayRef]) {
+        let expected_sums = expected[0].as_primitive::<Float64Type>();
+        let actual_sums = actual[0].as_primitive::<Float64Type>();
+        let expected_counts = expected[1].as_primitive::<Int64Type>();
+        let actual_counts = actual[1].as_primitive::<Int64Type>();
+        assert_eq!(expected_sums, actual_sums);
+        assert_eq!(expected_counts, actual_counts);
+    }
+
+    #[test]
+    fn test_partial_merge_row_ranges_match_whole_batch() -> Result<()> {
+        let batch = partial_output_batch(97);
+        let agg_ctx = merging_agg_ctx(batch.schema())?;
+
+        let whole = merge_in_ranges(&agg_ctx, &batch, &[])?;
+        let splitted = merge_in_ranges(&agg_ctx, &batch, &[13, 40, 40, 96])?;
+        assert_columns_eq(&whole, &splitted);
+        Ok(())
+    }
+
+    #[test]
+    fn test_partial_merge_accepts_sliced_input_batch() -> Result<()> {
+        let batch = partial_output_batch(97);
+        let sliced = batch.slice(11, 63);
+        let agg_ctx = merging_agg_ctx(sliced.schema())?;
+
+        let expected = merge_in_ranges(
+            &merging_agg_ctx(batch.schema())?,
+            &RecordBatch::try_new(
+                sliced.schema(),
+                sliced
+                    .columns()
+                    .iter()
+                    .map(|c| arrow::compute::concat(&[c.as_ref()]).expect("concat failed"))
+                    .collect(),
+            )?,
+            &[],
+        )?;
+        let actual = merge_in_ranges(&agg_ctx, &sliced, &[7, 30])?;
+        assert_columns_eq(&expected, &actual);
+        Ok(())
+    }
 }
diff --git a/native-engine/datafusion-ext-plans/src/agg_exec.rs b/native-engine/datafusion-ext-plans/src/agg_exec.rs
index 7ffaf295f..c637c78f5 100644
--- a/native-engine/datafusion-ext-plans/src/agg_exec.rs
+++ b/native-engine/datafusion-ext-plans/src/agg_exec.rs
@@ -366,6 +366,10 @@ fn execute_agg_sorted(
                 // compute grouping rows
                 let grouping_rows = agg_ctx.create_grouping_rows(&batch)?;
 
+                // acc arrays of the whole batch are unfrozen at most once, then
+                // shared by all partial merges of this batch
+                let mut merging_acc_table = None;
+
                 // update to current record
                 let mut batch_range_start = 0;
                 let mut batch_range_end = 0;
@@ -381,6 +385,7 @@ fn execute_agg_sorted(
                                 batch_range_end,
                                 &mut staging_acc_table,
                                 IdxSelection::Indices(&acc_indices),
+                                &mut merging_acc_table,
                             )?;
                             acc_indices.clear();
                             batch_range_start = batch_range_end;
@@ -398,6 +403,7 @@ fn execute_agg_sorted(
                     batch_range_end,
                     &mut staging_acc_table,
                     IdxSelection::Indices(&acc_indices),
+                    &mut merging_acc_table,
                 )?;
                 acc_indices.clear();
             }