-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Support UDAF to align Builtin aggregate function #10493
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,9 @@ use datafusion_expr::expr::{ | |
AggregateFunction, AggregateFunctionDefinition, WindowFunction, | ||
}; | ||
use datafusion_expr::utils::COUNT_STAR_EXPANSION; | ||
use datafusion_expr::{lit, Expr, LogicalPlan, WindowFunctionDefinition}; | ||
use datafusion_expr::{ | ||
aggregate_function, lit, Expr, LogicalPlan, WindowFunctionDefinition, | ||
}; | ||
|
||
/// Rewrite `Count(Expr:Wildcard)` to `Count(Expr:Literal)`. | ||
/// | ||
|
@@ -54,23 +56,37 @@ fn is_wildcard(expr: &Expr) -> bool { | |
} | ||
|
||
fn is_count_star_aggregate(aggregate_function: &AggregateFunction) -> bool { | ||
matches!( | ||
&aggregate_function.func_def, | ||
AggregateFunctionDefinition::BuiltIn( | ||
datafusion_expr::aggregate_function::AggregateFunction::Count, | ||
) | ||
) && aggregate_function.args.len() == 1 | ||
&& is_wildcard(&aggregate_function.args[0]) | ||
match aggregate_function { | ||
AggregateFunction { | ||
func_def: AggregateFunctionDefinition::UDF(udf), | ||
args, | ||
.. | ||
} if udf.name() == "COUNT" && args.len() == 1 && is_wildcard(&args[0]) => true, | ||
AggregateFunction { | ||
func_def: | ||
AggregateFunctionDefinition::BuiltIn( | ||
datafusion_expr::aggregate_function::AggregateFunction::Count, | ||
), | ||
args, | ||
.. | ||
} if args.len() == 1 && is_wildcard(&args[0]) => true, | ||
_ => false, | ||
} | ||
} | ||
|
||
fn is_count_star_window_aggregate(window_function: &WindowFunction) -> bool { | ||
matches!( | ||
&window_function.fun, | ||
let args = &window_function.args; | ||
match window_function.fun { | ||
WindowFunctionDefinition::AggregateFunction( | ||
datafusion_expr::aggregate_function::AggregateFunction::Count, | ||
) | ||
) && window_function.args.len() == 1 | ||
&& is_wildcard(&window_function.args[0]) | ||
aggregate_function::AggregateFunction::Count, | ||
) if args.len() == 1 && is_wildcard(&args[0]) => true, | ||
WindowFunctionDefinition::AggregateUDF(ref udaf) | ||
if udaf.name() == "COUNT" && args.len() == 1 && is_wildcard(&args[0]) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we should document the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The name should follow what is defined in |
||
{ | ||
true | ||
} | ||
_ => false, | ||
} | ||
} | ||
|
||
fn analyze_internal(plan: LogicalPlan) -> Result<Transformed<LogicalPlan>> { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -229,12 +229,15 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> { | |
)?; | ||
let order_by = (!order_by.is_empty()).then_some(order_by); | ||
let args = self.function_args_to_expr(args, schema, planner_context)?; | ||
// TODO: Support filter and distinct for UDAFs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice |
||
let filter: Option<Box<Expr>> = filter | ||
.map(|e| self.sql_expr_to_logical_expr(*e, schema, planner_context)) | ||
.transpose()? | ||
.map(Box::new); | ||
return Ok(Expr::AggregateFunction(expr::AggregateFunction::new_udf( | ||
fm, | ||
args, | ||
false, | ||
None, | ||
distinct, | ||
filter, | ||
order_by, | ||
null_treatment, | ||
))); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://docs.rs/datafusion/latest/datafusion/physical_expr/trait.PhysicalExpr.html# seems to imply it is possible to to compare PhysicalExprs (perhaps
expr.eq(other_expr.as_any())
🤔 )I don't know
PhysicalExpr
doesn't implementPartialEq
directlyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had not take a look carefully to the comment here, since they are not needed after removing builtin function