comphead commented on code in PR #10149:
URL: https://github.com/apache/datafusion/pull/10149#discussion_r1578081487


##########
datafusion/physical-expr/src/aggregate/array_agg.rs:
##########
@@ -187,17 +333,261 @@ impl Accumulator for ArrayAggAccumulator {
     }
 }
 
+struct ArrayAggGroupsAccumulator<T>
+where
+    T: ArrowPrimitiveType + Send,
+{
+    values: Vec<PrimitiveBuilder<T>>,
+    data_type: DataType,
+    null_state: NullState,
+}
+
+impl<T> ArrayAggGroupsAccumulator<T>
+where
+    T: ArrowPrimitiveType + Send,
+{
+    pub fn new(data_type: &DataType) -> Self {
+        Self {
+            values: vec![],
+            data_type: data_type.clone(),
+            null_state: NullState::new(),
+        }
+    }
+}
+
+impl<T: ArrowPrimitiveType + Send> ArrayAggGroupsAccumulator<T> {
+    fn build_list(&mut self, emit_to: EmitTo) -> Result<ArrayRef> {
+        let arrays = emit_to.take_needed(&mut self.values);
+        let nulls = self.null_state.build(emit_to);
+
+        let len = nulls.len();
+        assert_eq!(arrays.len(), len);
+
+        let mut builder = ListBuilder::with_capacity(
+            
PrimitiveBuilder::<T>::new().with_data_type(self.data_type.clone()),
+            len,
+        );
+
+        for (is_valid, mut arr) in nulls.iter().zip(arrays.into_iter()) {
+            if is_valid {
+                builder.append_value(arr.finish().into_iter());
+            } else {
+                builder.append_null();
+            }
+        }
+
+        Ok(Arc::new(builder.finish()))
+    }
+}
+
+impl<T> GroupsAccumulator for ArrayAggGroupsAccumulator<T>
+where
+    T: ArrowPrimitiveType + Send + Sync,
+{
+    fn update_batch(
+        &mut self,
+        new_values: &[ArrayRef],
+        group_indices: &[usize],
+        opt_filter: Option<&BooleanArray>,
+        total_num_groups: usize,
+    ) -> Result<()> {
+        assert_eq!(new_values.len(), 1, "single argument to update_batch");
+        let new_values = new_values[0].as_primitive::<T>();
+
+        for _ in self.values.len()..total_num_groups {
+            self.values.push(
+                
PrimitiveBuilder::<T>::new().with_data_type(self.data_type.clone()),
+            );
+        }
+
+        self.null_state.accumulate(
+            group_indices,
+            new_values,
+            opt_filter,
+            total_num_groups,
+            |group_index, new_value| {
+                self.values[group_index].append_value(new_value);
+            },
+        );
+
+        Ok(())
+    }
+
+    fn merge_batch(
+        &mut self,
+        values: &[ArrayRef],
+        group_indices: &[usize],
+        opt_filter: Option<&BooleanArray>,
+        total_num_groups: usize,
+    ) -> Result<()> {
+        assert_eq!(values.len(), 1, "single argument to merge_batch");
+        let values = values[0].as_list();
+
+        for _ in self.values.len()..total_num_groups {
+            self.values.push(
+                
PrimitiveBuilder::<T>::new().with_data_type(self.data_type.clone()),
+            );
+        }
+
+        self.null_state.accumulate_array(
+            group_indices,
+            values,
+            opt_filter,
+            total_num_groups,
+            |group_index, new_value: ArrayRef| {
+                let new_value = new_value.as_primitive::<T>();
+                self.values[group_index].extend(new_value);
+            },
+        );
+
+        Ok(())
+    }
+
+    fn evaluate(&mut self, emit_to: EmitTo) -> Result<ArrayRef> {
+        self.build_list(emit_to)
+    }
+
+    fn state(&mut self, emit_to: EmitTo) -> Result<Vec<ArrayRef>> {
+        Ok(vec![self.build_list(emit_to)?])
+    }
+
+    fn size(&self) -> usize {
+        std::mem::size_of_val(self)
+            + std::mem::size_of::<PrimitiveBuilder<T>>() * 
self.values.capacity()
+            + self.values.iter().map(|arr| arr.capacity()).sum::<usize>()
+                * std::mem::size_of::<<T as ArrowPrimitiveType>::Native>()
+            + self.null_state.size()
+    }
+}
+
+struct StringArrayAggGroupsAccumulator {
+    values: Vec<StringBuilder>,
+    null_state: NullState,
+}
+
+impl StringArrayAggGroupsAccumulator {
+    pub fn new() -> Self {
+        Self {
+            values: vec![],
+            null_state: NullState::new(),
+        }
+    }
+}
+
+impl StringArrayAggGroupsAccumulator {
+    fn build_list(&mut self, emit_to: EmitTo) -> Result<ArrayRef> {
+        let array = emit_to.take_needed(&mut self.values);
+        let nulls = self.null_state.build(emit_to);
+
+        assert_eq!(array.len(), nulls.len());
+
+        let mut builder = ListBuilder::with_capacity(StringBuilder::new(), 
nulls.len());
+        for (is_valid, mut arr) in nulls.iter().zip(array.into_iter()) {
+            if is_valid {
+                builder.append_value(arr.finish().into_iter());
+            } else {
+                builder.append_null();
+            }
+        }
+
+        Ok(Arc::new(builder.finish()))
+    }
+}
+
+impl GroupsAccumulator for StringArrayAggGroupsAccumulator {
+    fn update_batch(
+        &mut self,
+        new_values: &[ArrayRef],
+        group_indices: &[usize],
+        opt_filter: Option<&BooleanArray>,
+        total_num_groups: usize,
+    ) -> Result<()> {
+        assert_eq!(new_values.len(), 1, "single argument to update_batch");

Review Comment:
   ditto



##########
datafusion/physical-expr/src/aggregate/array_agg.rs:
##########
@@ -187,17 +333,261 @@ impl Accumulator for ArrayAggAccumulator {
     }
 }
 
+struct ArrayAggGroupsAccumulator<T>
+where
+    T: ArrowPrimitiveType + Send,
+{
+    values: Vec<PrimitiveBuilder<T>>,
+    data_type: DataType,
+    null_state: NullState,
+}
+
+impl<T> ArrayAggGroupsAccumulator<T>
+where
+    T: ArrowPrimitiveType + Send,
+{
+    pub fn new(data_type: &DataType) -> Self {
+        Self {
+            values: vec![],
+            data_type: data_type.clone(),
+            null_state: NullState::new(),
+        }
+    }
+}
+
+impl<T: ArrowPrimitiveType + Send> ArrayAggGroupsAccumulator<T> {
+    fn build_list(&mut self, emit_to: EmitTo) -> Result<ArrayRef> {
+        let arrays = emit_to.take_needed(&mut self.values);
+        let nulls = self.null_state.build(emit_to);
+
+        let len = nulls.len();
+        assert_eq!(arrays.len(), len);
+
+        let mut builder = ListBuilder::with_capacity(
+            
PrimitiveBuilder::<T>::new().with_data_type(self.data_type.clone()),
+            len,
+        );
+
+        for (is_valid, mut arr) in nulls.iter().zip(arrays.into_iter()) {
+            if is_valid {
+                builder.append_value(arr.finish().into_iter());
+            } else {
+                builder.append_null();
+            }
+        }
+
+        Ok(Arc::new(builder.finish()))
+    }
+}
+
+impl<T> GroupsAccumulator for ArrayAggGroupsAccumulator<T>
+where
+    T: ArrowPrimitiveType + Send + Sync,
+{
+    fn update_batch(
+        &mut self,
+        new_values: &[ArrayRef],
+        group_indices: &[usize],
+        opt_filter: Option<&BooleanArray>,
+        total_num_groups: usize,
+    ) -> Result<()> {
+        assert_eq!(new_values.len(), 1, "single argument to update_batch");
+        let new_values = new_values[0].as_primitive::<T>();
+
+        for _ in self.values.len()..total_num_groups {
+            self.values.push(
+                
PrimitiveBuilder::<T>::new().with_data_type(self.data_type.clone()),
+            );
+        }
+
+        self.null_state.accumulate(
+            group_indices,
+            new_values,
+            opt_filter,
+            total_num_groups,
+            |group_index, new_value| {
+                self.values[group_index].append_value(new_value);
+            },
+        );
+
+        Ok(())
+    }
+
+    fn merge_batch(
+        &mut self,
+        values: &[ArrayRef],
+        group_indices: &[usize],
+        opt_filter: Option<&BooleanArray>,
+        total_num_groups: usize,
+    ) -> Result<()> {
+        assert_eq!(values.len(), 1, "single argument to merge_batch");
+        let values = values[0].as_list();
+
+        for _ in self.values.len()..total_num_groups {
+            self.values.push(
+                
PrimitiveBuilder::<T>::new().with_data_type(self.data_type.clone()),
+            );
+        }
+
+        self.null_state.accumulate_array(
+            group_indices,
+            values,
+            opt_filter,
+            total_num_groups,
+            |group_index, new_value: ArrayRef| {
+                let new_value = new_value.as_primitive::<T>();
+                self.values[group_index].extend(new_value);
+            },
+        );
+
+        Ok(())
+    }
+
+    fn evaluate(&mut self, emit_to: EmitTo) -> Result<ArrayRef> {
+        self.build_list(emit_to)
+    }
+
+    fn state(&mut self, emit_to: EmitTo) -> Result<Vec<ArrayRef>> {
+        Ok(vec![self.build_list(emit_to)?])
+    }
+
+    fn size(&self) -> usize {
+        std::mem::size_of_val(self)
+            + std::mem::size_of::<PrimitiveBuilder<T>>() * 
self.values.capacity()
+            + self.values.iter().map(|arr| arr.capacity()).sum::<usize>()
+                * std::mem::size_of::<<T as ArrowPrimitiveType>::Native>()
+            + self.null_state.size()
+    }
+}
+
+struct StringArrayAggGroupsAccumulator {
+    values: Vec<StringBuilder>,
+    null_state: NullState,
+}
+
+impl StringArrayAggGroupsAccumulator {
+    pub fn new() -> Self {
+        Self {
+            values: vec![],
+            null_state: NullState::new(),
+        }
+    }
+}
+
+impl StringArrayAggGroupsAccumulator {
+    fn build_list(&mut self, emit_to: EmitTo) -> Result<ArrayRef> {
+        let array = emit_to.take_needed(&mut self.values);
+        let nulls = self.null_state.build(emit_to);
+
+        assert_eq!(array.len(), nulls.len());
+
+        let mut builder = ListBuilder::with_capacity(StringBuilder::new(), 
nulls.len());
+        for (is_valid, mut arr) in nulls.iter().zip(array.into_iter()) {
+            if is_valid {
+                builder.append_value(arr.finish().into_iter());
+            } else {
+                builder.append_null();
+            }
+        }
+
+        Ok(Arc::new(builder.finish()))
+    }
+}
+
+impl GroupsAccumulator for StringArrayAggGroupsAccumulator {
+    fn update_batch(
+        &mut self,
+        new_values: &[ArrayRef],
+        group_indices: &[usize],
+        opt_filter: Option<&BooleanArray>,
+        total_num_groups: usize,
+    ) -> Result<()> {
+        assert_eq!(new_values.len(), 1, "single argument to update_batch");
+        let new_values = new_values[0].as_string();
+
+        for _ in self.values.len()..total_num_groups {
+            self.values.push(StringBuilder::new());
+        }
+
+        self.null_state.accumulate_string(
+            group_indices,
+            new_values,
+            opt_filter,
+            total_num_groups,
+            |group_index, new_value| {
+                self.values[group_index].append_value(new_value);
+            },
+        );
+
+        Ok(())
+    }
+
+    fn merge_batch(
+        &mut self,
+        values: &[ArrayRef],
+        group_indices: &[usize],
+        opt_filter: Option<&BooleanArray>,
+        total_num_groups: usize,
+    ) -> Result<()> {
+        assert_eq!(values.len(), 1, "single argument to merge_batch");

Review Comment:
   ditto



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