alamb commented on code in PR #6837:
URL: https://github.com/apache/arrow-datafusion/pull/6837#discussion_r1253351668
##########
datafusion/physical-expr/src/aggregate/median.rs:
##########
@@ -126,11 +142,7 @@ impl Accumulator for MedianAccumulator {
let array = &values[0];
assert_eq!(array.data_type(), &self.data_type);
- self.all_values.reserve(array.len());
- for index in 0..array.len() {
- self.all_values
- .push(ScalarValue::try_from_array(array, index)?);
- }
+ self.batches.push(array.clone());
Review Comment:
```suggestion
// defer conversion to final evaluation
self.batches.push(array.clone());
```
##########
datafusion/physical-expr/src/aggregate/median.rs:
##########
@@ -111,13 +112,28 @@ impl PartialEq<dyn Any> for Median {
/// The intermediate state is represented as a List of those scalars
struct MedianAccumulator {
data_type: DataType,
+ batches: Vec<ArrayRef>,
all_values: Vec<ScalarValue>,
}
+fn to_scalar_values(arrays: &[ArrayRef]) -> Result<Vec<ScalarValue>> {
+ let num_values: usize = arrays.iter().map(|a| a.len()).sum();
+ let mut all_values = Vec::with_capacity(num_values);
+
+ for array in arrays {
+ for index in 0..array.len() {
+ all_values.push(ScalarValue::try_from_array(&array, index)?);
+ }
+ }
+
+ Ok(all_values)
+}
+
impl Accumulator for MedianAccumulator {
fn state(&self) -> Result<Vec<ScalarValue>> {
- let state =
- ScalarValue::new_list(Some(self.all_values.clone()),
self.data_type.clone());
+ let all_values = to_scalar_values(&self.batches)?;
+ let state = ScalarValue::new_list(Some(all_values),
self.data_type.clone());
Review Comment:
After thinking about this some more, I think the most performant thing to do
will be to implement a native `GroupsAccumulator` (aka #6800 ) for median. With
sufficient effort we could make median be very fast -- so I think this is a
good improvement for now
##########
datafusion/physical-expr/src/aggregate/median.rs:
##########
@@ -111,13 +112,28 @@ impl PartialEq<dyn Any> for Median {
/// The intermediate state is represented as a List of those scalars
Review Comment:
Can you please update the comments to reflect the change here (aka that the
state is is stored as ScalarValues, but the conversion is delayed if possible)
##########
datafusion/physical-expr/src/aggregate/median.rs:
##########
@@ -111,13 +112,28 @@ impl PartialEq<dyn Any> for Median {
/// The intermediate state is represented as a List of those scalars
struct MedianAccumulator {
data_type: DataType,
+ batches: Vec<ArrayRef>,
all_values: Vec<ScalarValue>,
}
+fn to_scalar_values(arrays: &[ArrayRef]) -> Result<Vec<ScalarValue>> {
+ let num_values: usize = arrays.iter().map(|a| a.len()).sum();
Review Comment:
💯 for computing the capacity up front
##########
datafusion/physical-expr/src/aggregate/median.rs:
##########
@@ -111,13 +112,28 @@ impl PartialEq<dyn Any> for Median {
/// The intermediate state is represented as a List of those scalars
struct MedianAccumulator {
data_type: DataType,
+ batches: Vec<ArrayRef>,
Review Comment:
Nit is that this is actually arrays (not batches) -- maybe `arrays` is a
better name
--
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]