diff --git a/datafusion/expr/src/expr.rs b/datafusion/expr/src/expr.rs index 5d750083894c2..212ec1a7791b1 100644 --- a/datafusion/expr/src/expr.rs +++ b/datafusion/expr/src/expr.rs @@ -1631,6 +1631,16 @@ fn create_name(e: &Expr) -> Result { } Ok(format!("{}({}){}", fun.name(), names.join(","), info)) } + AggregateFunctionDefinition::Name(name) => { + let mut name = create_function_name(name, *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)); + }; + Ok(name) + } }, Expr::GroupingSet(grouping_set) => match grouping_set { GroupingSet::Rollup(exprs) => { diff --git a/datafusion/optimizer/src/analyzer/count_wildcard_rule.rs b/datafusion/optimizer/src/analyzer/count_wildcard_rule.rs index 16d8c72d462a3..b63f1d21116d3 100644 --- a/datafusion/optimizer/src/analyzer/count_wildcard_rule.rs +++ b/datafusion/optimizer/src/analyzer/count_wildcard_rule.rs @@ -19,7 +19,7 @@ use crate::analyzer::AnalyzerRule; use datafusion_common::config::ConfigOptions; use datafusion_common::tree_node::{Transformed, TreeNode, TreeNodeRewriter}; use datafusion_common::Result; -use datafusion_expr::expr::{AggregateFunction, InSubquery}; +use datafusion_expr::expr::{AggregateFunction, AggregateFunctionDefinition, InSubquery}; use datafusion_expr::expr_rewriter::rewrite_preserving_name; use datafusion_expr::utils::COUNT_STAR_EXPANSION; use datafusion_expr::Expr::ScalarSubquery; @@ -144,7 +144,11 @@ impl TreeNodeRewriter for CountWildcardRewriter { _ => old_expr, }, Expr::AggregateFunction(AggregateFunction { - func_def: _, + func_def: + AggregateFunctionDefinition::BuiltIn { + fun: aggregate_function::AggregateFunction::Count, + name: _, + }, args, distinct, filter, diff --git a/datafusion/optimizer/src/single_distinct_to_groupby.rs b/datafusion/optimizer/src/single_distinct_to_groupby.rs index f66ff0eae6f0e..f3e29296d7a36 100644 --- a/datafusion/optimizer/src/single_distinct_to_groupby.rs +++ b/datafusion/optimizer/src/single_distinct_to_groupby.rs @@ -71,7 +71,7 @@ fn is_single_distinct_agg(plan: &LogicalPlan) -> Result { let mut aggregate_count = 0; for expr in aggr_expr { if let Expr::AggregateFunction(AggregateFunction { - func_def, + func_def: AggregateFunctionDefinition::BuiltIn { fun, name: _ }, distinct, args, filter, @@ -86,19 +86,8 @@ fn is_single_distinct_agg(plan: &LogicalPlan) -> Result { for e in args { fields_set.insert(e.canonical_name()); } - } else { - match func_def { - AggregateFunctionDefinition::BuiltIn { fun, name: _ } => { - if !matches!(fun, Sum | Min | Max) { - return Ok(false); - } else { - return Ok(true); - } - } - _ => { - return Ok(false); - } - } + } else if !matches!(fun, Sum | Min | Max) { + return Ok(false); } } }