alamb commented on code in PR #3009:
URL: https://github.com/apache/arrow-datafusion/pull/3009#discussion_r937097331
##########
datafusion/expr/src/accumulator.rs:
##########
@@ -44,3 +44,27 @@ pub trait Accumulator: Send + Sync + Debug {
/// returns its value based on its current state.
fn evaluate(&self) -> Result<ScalarValue>;
}
+
+#[derive(Debug)]
+pub enum AggregateState {
+ Scalar(ScalarValue),
+ Array(ArrayRef),
+}
+
+impl AggregateState {
+ pub fn as_scalar(&self) -> Result<&ScalarValue> {
+ match &self {
+ Self::Scalar(v) => Ok(v),
+ _ => Err(DataFusionError::Execution(
+ "not a scalar aggregate".to_string(),
+ )),
Review Comment:
```suggestion
_ => Err(DataFusionError::Internal(
"AggregateState is not a scalar aggregate".to_string(),
)),
```
I think marking this `Internal` might better signal to user if they hit
this, they have seen some sort of bug with DataFusion
##########
datafusion/expr/src/accumulator.rs:
##########
@@ -44,3 +44,27 @@ pub trait Accumulator: Send + Sync + Debug {
/// returns its value based on its current state.
fn evaluate(&self) -> Result<ScalarValue>;
}
+
+#[derive(Debug)]
+pub enum AggregateState {
Review Comment:
This is a very elegant idea. Can you please add docstrings to
`AggregateState` explaining what is going on?
I think it would be worth updating the docstrings in the accumulator trait
with some discussion / examples of how to use the Array state.
##########
datafusion/physical-expr/src/aggregate/count_distinct.rs:
##########
@@ -206,7 +206,10 @@ impl Accumulator for DistinctCountAccumulator {
)
});
- Ok(cols_out)
+ Ok(cols_out
+ .iter()
+ .map(|v| AggregateState::Scalar(v.clone()))
+ .collect())
Review Comment:
You can probably avoid a copy like:
```suggestion
Ok(cols_out
.into_iter()
.map(|v| AggregateState::Scalar(v))
.collect())
```
##########
datafusion/physical-expr/src/aggregate/median.rs:
##########
@@ -0,0 +1,260 @@
+// 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.
+
+//! # Median
+
+use crate::expressions::format_state_name;
+use crate::{AggregateExpr, PhysicalExpr};
+use arrow::array::{
+ Array, ArrayRef, Float32Array, Float64Array, Int16Array, Int32Array,
Int64Array,
+ Int8Array, PrimitiveArray, PrimitiveBuilder, UInt16Array, UInt32Array,
UInt64Array,
+ UInt8Array,
+};
+use arrow::compute::sort;
+use arrow::datatypes::{ArrowPrimitiveType, DataType, Field};
+use datafusion_common::{DataFusionError, Result, ScalarValue};
+use datafusion_expr::{Accumulator, AggregateState};
+use std::any::Any;
+use std::sync::Arc;
+
+/// MEDIAN aggregate expression. This uses a lot of memory because all values
need to be
+/// stored in memory before a result can be computed. If an approximation is
sufficient
Review Comment:
I wonder if it is worth (perhaps as a follow on PR) putting a cap on the
number of values DataFusion will try to buffer to compute median and throw a
runtime error if that number is exceeded 🤔 That way we could avoid OOM kills
--
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]