|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use super::{from_substrait_func_args, substrait_fun_name, SubstraitConsumer}; |
| 19 | +use datafusion::common::{not_impl_datafusion_err, plan_err, DFSchema, ScalarValue}; |
| 20 | +use datafusion::execution::FunctionRegistry; |
| 21 | +use datafusion::logical_expr::{expr, Expr, SortExpr}; |
| 22 | +use std::sync::Arc; |
| 23 | +use substrait::proto::AggregateFunction; |
| 24 | + |
| 25 | +/// Convert Substrait AggregateFunction to DataFusion Expr |
| 26 | +pub async fn from_substrait_agg_func( |
| 27 | + consumer: &impl SubstraitConsumer, |
| 28 | + f: &AggregateFunction, |
| 29 | + input_schema: &DFSchema, |
| 30 | + filter: Option<Box<Expr>>, |
| 31 | + order_by: Option<Vec<SortExpr>>, |
| 32 | + distinct: bool, |
| 33 | +) -> datafusion::common::Result<Arc<Expr>> { |
| 34 | + let Some(fn_signature) = consumer |
| 35 | + .get_extensions() |
| 36 | + .functions |
| 37 | + .get(&f.function_reference) |
| 38 | + else { |
| 39 | + return plan_err!( |
| 40 | + "Aggregate function not registered: function anchor = {:?}", |
| 41 | + f.function_reference |
| 42 | + ); |
| 43 | + }; |
| 44 | + |
| 45 | + let fn_name = substrait_fun_name(fn_signature); |
| 46 | + let udaf = consumer.get_function_registry().udaf(fn_name); |
| 47 | + let udaf = udaf.map_err(|_| { |
| 48 | + not_impl_datafusion_err!( |
| 49 | + "Aggregate function {} is not supported: function anchor = {:?}", |
| 50 | + fn_signature, |
| 51 | + f.function_reference |
| 52 | + ) |
| 53 | + })?; |
| 54 | + |
| 55 | + let args = from_substrait_func_args(consumer, &f.arguments, input_schema).await?; |
| 56 | + |
| 57 | + // Datafusion does not support aggregate functions with no arguments, so |
| 58 | + // we inject a dummy argument that does not affect the query, but allows |
| 59 | + // us to bypass this limitation. |
| 60 | + let args = if udaf.name() == "count" && args.is_empty() { |
| 61 | + vec![Expr::Literal(ScalarValue::Int64(Some(1)))] |
| 62 | + } else { |
| 63 | + args |
| 64 | + }; |
| 65 | + |
| 66 | + Ok(Arc::new(Expr::AggregateFunction( |
| 67 | + expr::AggregateFunction::new_udf(udaf, args, distinct, filter, order_by, None), |
| 68 | + ))) |
| 69 | +} |
0 commit comments