Dandandan commented on a change in pull request #1300: URL: https://github.com/apache/arrow-datafusion/pull/1300#discussion_r749031578
########## File path: datafusion/src/physical_plan/expressions/array_agg.rs ########## @@ -0,0 +1,244 @@ +// 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. + +//! Defines physical expressions that can evaluated at runtime during query execution + +use super::format_state_name; +use crate::error::Result; +use crate::physical_plan::{Accumulator, AggregateExpr, PhysicalExpr}; +use crate::scalar::ScalarValue; +use arrow::datatypes::{DataType, Field}; +use std::any::Any; +use std::sync::Arc; + +/// ARRAY_AGG aggregate expression +#[derive(Debug)] +pub struct ArrayAgg { + name: String, + input_data_type: DataType, + expr: Arc<dyn PhysicalExpr>, +} + +impl ArrayAgg { + /// Create a new ArrayAgg aggregate function + pub fn new( + expr: Arc<dyn PhysicalExpr>, + name: impl Into<String>, + data_type: DataType, + ) -> Self { + Self { + name: name.into(), + expr, + input_data_type: data_type, + } + } +} + +impl AggregateExpr for ArrayAgg { + fn as_any(&self) -> &dyn Any { + self + } + + fn field(&self) -> Result<Field> { + Ok(Field::new( + &self.name, + DataType::List(Box::new(Field::new( + "element", + self.input_data_type.clone(), + true, + ))), + false, + )) + } + + fn create_accumulator(&self) -> Result<Box<dyn Accumulator>> { + Ok(Box::new(ArrayAggAccumulator::try_new( + &self.input_data_type, + )?)) + } + + fn state_fields(&self) -> Result<Vec<Field>> { + Ok(vec![Field::new( + &format_state_name(&self.name, "array_agg"), + DataType::List(Box::new(Field::new( + "item", + self.input_data_type.clone(), + true, + ))), + false, + )]) + } + + fn expressions(&self) -> Vec<Arc<dyn PhysicalExpr>> { + vec![self.expr.clone()] + } +} + +#[derive(Debug)] +pub(crate) struct ArrayAggAccumulator { + array: ScalarValue, +} + +impl ArrayAggAccumulator { + /// new array_agg accumulator based on given element data type + pub fn try_new(datatype: &DataType) -> Result<Self> { + Ok(Self { + array: ScalarValue::List(Some(Box::new(vec![])), Box::new(datatype.clone())), + }) + } +} + +impl Accumulator for ArrayAggAccumulator { + fn state(&self) -> Result<Vec<ScalarValue>> { + Ok(vec![self.array.clone()]) + } + + fn update(&mut self, values: &[ScalarValue]) -> Result<()> { Review comment: Did you leave `update_batch` and `merge_batch` on purpose? I think at this point it is hard to think of a much more efficient implementation (avoiding converting every item to scalars), given that we don't have columnar storage for aggregates yet. -- 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]
