Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor aggregate function handling #8358

Merged
merged 8 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions datafusion/core/src/datasource/listing/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ pub fn expr_applicable_for_cols(col_names: &[String], expr: &Expr) -> bool {
// - AGGREGATE, WINDOW and SORT should not end up in filter conditions, except maybe in some edge cases
// - Can `Wildcard` be considered as a `Literal`?
// - ScalarVariable could be `applicable`, but that would require access to the context
Expr::AggregateUDF { .. }
| Expr::AggregateFunction { .. }
Expr::AggregateFunction { .. }
| Expr::Sort { .. }
| Expr::WindowFunction { .. }
| Expr::Wildcard { .. }
Expand Down
128 changes: 54 additions & 74 deletions datafusion/core/src/physical_planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ use datafusion_common::{
};
use datafusion_expr::dml::{CopyOptions, CopyTo};
use datafusion_expr::expr::{
self, AggregateFunction, AggregateUDF, Alias, Between, BinaryExpr, Cast,
GetFieldAccess, GetIndexedField, GroupingSet, InList, Like, TryCast, WindowFunction,
self, AggregateFunction, AggregateFunctionDefinition, Alias, Between, BinaryExpr,
Cast, GetFieldAccess, GetIndexedField, GroupingSet, InList, Like, TryCast,
WindowFunction,
};
use datafusion_expr::expr_rewriter::{unalias, unnormalize_cols};
use datafusion_expr::logical_plan::builder::wrap_projection_for_join_if_necessary;
Expand Down Expand Up @@ -229,30 +230,37 @@ fn create_physical_name(e: &Expr, is_first_expr: bool) -> Result<String> {
create_function_physical_name(&fun.to_string(), false, args)
}
Expr::AggregateFunction(AggregateFunction {
fun,
func_def,
distinct,
args,
..
}) => create_function_physical_name(&fun.to_string(), *distinct, args),
Expr::AggregateUDF(AggregateUDF {
fun,
args,
filter,
order_by,
}) => {
// TODO: Add support for filter and order by in AggregateUDF
if filter.is_some() {
return exec_err!("aggregate expression with filter is not supported");
}) => match func_def {
AggregateFunctionDefinition::BuiltIn(..) => {
create_function_physical_name(func_def.name(), *distinct, args)
}
if order_by.is_some() {
return exec_err!("aggregate expression with order_by is not supported");
AggregateFunctionDefinition::UDF(fun) => {
// TODO: Add support for filter and order by in AggregateUDF
if filter.is_some() {
return exec_err!(
"aggregate expression with filter is not supported"
);
}
if order_by.is_some() {
return exec_err!(
"aggregate expression with order_by is not supported"
);
}
let names = args
.iter()
.map(|e| create_physical_name(e, false))
.collect::<Result<Vec<_>>>()?;
Ok(format!("{}({})", fun.name(), names.join(",")))
}
let mut names = Vec::with_capacity(args.len());
for e in args {
names.push(create_physical_name(e, false)?);
AggregateFunctionDefinition::Name(_) => {
internal_err!("Aggregate function `Expr` with name should be resolved.")
}
Ok(format!("{}({})", fun.name(), names.join(",")))
}
},
Expr::GroupingSet(grouping_set) => match grouping_set {
GroupingSet::Rollup(exprs) => Ok(format!(
"ROLLUP ({})",
Expand Down Expand Up @@ -1705,7 +1713,7 @@ pub fn create_aggregate_expr_with_name_and_maybe_filter(
) -> Result<AggregateExprWithOptionalArgs> {
match e {
Expr::AggregateFunction(AggregateFunction {
fun,
func_def,
distinct,
args,
filter,
Expand Down Expand Up @@ -1746,63 +1754,35 @@ pub fn create_aggregate_expr_with_name_and_maybe_filter(
),
None => None,
};
let ordering_reqs = order_by.clone().unwrap_or(vec![]);
let agg_expr = aggregates::create_aggregate_expr(
fun,
*distinct,
&args,
&ordering_reqs,
physical_input_schema,
name,
)?;
Ok((agg_expr, filter, order_by))
}
Expr::AggregateUDF(AggregateUDF {
fun,
args,
filter,
order_by,
}) => {
let args = args
.iter()
.map(|e| {
create_physical_expr(
e,
logical_input_schema,
let (agg_expr, filter, order_by) = match func_def {
AggregateFunctionDefinition::BuiltIn(fun) => {
let ordering_reqs = order_by.clone().unwrap_or(vec![]);
let agg_expr = aggregates::create_aggregate_expr(
fun,
*distinct,
&args,
&ordering_reqs,
physical_input_schema,
execution_props,
name,
)?;
(agg_expr, filter, order_by)
}
AggregateFunctionDefinition::UDF(fun) => {
let agg_expr = udaf::create_aggregate_expr(
fun,
&args,
physical_input_schema,
name,
);
(agg_expr?, filter, order_by)
}
AggregateFunctionDefinition::Name(_) => {
return internal_err!(
"Aggregate function name should have been resolved"
)
})
.collect::<Result<Vec<_>>>()?;

let filter = match filter {
Some(e) => Some(create_physical_expr(
e,
logical_input_schema,
physical_input_schema,
execution_props,
)?),
None => None,
};
let order_by = match order_by {
Some(e) => Some(
e.iter()
.map(|expr| {
create_physical_sort_expr(
expr,
logical_input_schema,
physical_input_schema,
execution_props,
)
})
.collect::<Result<Vec<_>>>()?,
),
None => None,
}
};

let agg_expr =
udaf::create_aggregate_expr(fun, &args, physical_input_schema, name);
Ok((agg_expr?, filter, order_by))
Ok((agg_expr, filter, order_by))
}
other => internal_err!("Invalid aggregate expression '{other:?}'"),
}
Expand Down
2 changes: 1 addition & 1 deletion datafusion/expr/src/aggregate_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ pub enum AggregateFunction {
}

impl AggregateFunction {
fn name(&self) -> &str {
pub fn name(&self) -> &str {
use AggregateFunction::*;
match self {
Count => "COUNT",
Expand Down
112 changes: 66 additions & 46 deletions datafusion/expr/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,6 @@ pub enum Expr {
AggregateFunction(AggregateFunction),
/// Represents the call of a window function with arguments.
WindowFunction(WindowFunction),
/// aggregate function
AggregateUDF(AggregateUDF),
/// Returns whether the list contains the expr value.
InList(InList),
/// EXISTS subquery
Expand Down Expand Up @@ -484,11 +482,33 @@ impl Sort {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
/// Defines which implementation of an aggregate function DataFusion should call.
pub enum AggregateFunctionDefinition {
BuiltIn(aggregate_function::AggregateFunction),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

/// Resolved to a user defined aggregate function
UDF(Arc<crate::AggregateUDF>),
/// A aggregation function constructed with name. This variant can not be executed directly
/// and instead must be resolved to one of the other variants prior to physical planning.
Name(Arc<str>),
}

impl AggregateFunctionDefinition {
/// Function's name for display
pub fn name(&self) -> &str {
match self {
AggregateFunctionDefinition::BuiltIn(fun) => fun.name(),
AggregateFunctionDefinition::UDF(udf) => udf.name(),
AggregateFunctionDefinition::Name(func_name) => func_name.as_ref(),
}
}
}

/// Aggregate function
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct AggregateFunction {
/// Name of the function
pub fun: aggregate_function::AggregateFunction,
pub func_def: AggregateFunctionDefinition,
/// List of expressions to feed to the functions as arguments
pub args: Vec<Expr>,
/// Whether this is a DISTINCT aggregation or not
Expand All @@ -508,7 +528,24 @@ impl AggregateFunction {
order_by: Option<Vec<Expr>>,
) -> Self {
Self {
fun,
func_def: AggregateFunctionDefinition::BuiltIn(fun),
args,
distinct,
filter,
order_by,
}
}

/// Create a new AggregateFunction expression with a user-defined function (UDF)
pub fn new_udf(
udf: Arc<crate::AggregateUDF>,
args: Vec<Expr>,
distinct: bool,
filter: Option<Box<Expr>>,
order_by: Option<Vec<Expr>>,
) -> Self {
Self {
func_def: AggregateFunctionDefinition::UDF(udf),
args,
distinct,
filter,
Expand Down Expand Up @@ -736,7 +773,6 @@ impl Expr {
pub fn variant_name(&self) -> &str {
match self {
Expr::AggregateFunction { .. } => "AggregateFunction",
Expr::AggregateUDF { .. } => "AggregateUDF",
Expr::Alias(..) => "Alias",
Expr::Between { .. } => "Between",
Expr::BinaryExpr { .. } => "BinaryExpr",
Expand Down Expand Up @@ -1251,30 +1287,14 @@ impl fmt::Display for Expr {
Ok(())
}
Expr::AggregateFunction(AggregateFunction {
fun,
func_def,
distinct,
ref args,
filter,
order_by,
..
}) => {
fmt_function(f, &fun.to_string(), *distinct, args, true)?;
if let Some(fe) = filter {
write!(f, " FILTER (WHERE {fe})")?;
}
if let Some(ob) = order_by {
write!(f, " ORDER BY [{}]", expr_vec_fmt!(ob))?;
}
Ok(())
}
Expr::AggregateUDF(AggregateUDF {
fun,
ref args,
filter,
order_by,
..
}) => {
fmt_function(f, fun.name(), false, args, true)?;
fmt_function(f, func_def.name(), *distinct, args, true)?;
if let Some(fe) = filter {
write!(f, " FILTER (WHERE {fe})")?;
}
Expand Down Expand Up @@ -1579,39 +1599,39 @@ fn create_name(e: &Expr) -> Result<String> {
Ok(parts.join(" "))
}
Expr::AggregateFunction(AggregateFunction {
fun,
func_def,
distinct,
args,
filter,
order_by,
}) => {
let mut name = create_function_name(&fun.to_string(), *distinct, args)?;
if let Some(fe) = filter {
name = format!("{name} FILTER (WHERE {fe})");
};
if let Some(order_by) = order_by {
name = format!("{name} ORDER BY [{}]", expr_vec_fmt!(order_by));
let name = match func_def {
AggregateFunctionDefinition::BuiltIn(..)
| AggregateFunctionDefinition::Name(..) => {
create_function_name(func_def.name(), *distinct, args)?
}
AggregateFunctionDefinition::UDF(..) => {
let names: Vec<String> =
args.iter().map(create_name).collect::<Result<_>>()?;
names.join(",")
}
};
Ok(name)
}
Expr::AggregateUDF(AggregateUDF {
fun,
args,
filter,
order_by,
}) => {
let mut names = Vec::with_capacity(args.len());
for e in args {
names.push(create_name(e)?);
}
let mut info = String::new();
if let Some(fe) = filter {
info += &format!(" FILTER (WHERE {fe})");
};
if let Some(order_by) = order_by {
info += &format!(" ORDER BY [{}]", expr_vec_fmt!(order_by));
};
match func_def {
AggregateFunctionDefinition::BuiltIn(..)
| AggregateFunctionDefinition::Name(..) => {
Ok(format!("{}{}", name, info))
}
AggregateFunctionDefinition::UDF(fun) => {
Ok(format!("{}({}){}", fun.name(), name, info))
}
}
if let Some(ob) = order_by {
info += &format!(" ORDER BY ([{}])", expr_vec_fmt!(ob));
}
Ok(format!("{}({}){}", fun.name(), names.join(","), info))
}
Expr::GroupingSet(grouping_set) => match grouping_set {
GroupingSet::Rollup(exprs) => {
Expand Down
Loading