-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Add logical plan support for aggregate expressions with filters (and upgrade to sqlparser 0.23) #3405
Conversation
Codecov Report
@@ Coverage Diff @@
## master #3405 +/- ##
=======================================
Coverage 85.68% 85.68%
=======================================
Files 298 298
Lines 54645 54700 +55
=======================================
+ Hits 46820 46868 +48
- Misses 7825 7832 +7
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
cc @ayushdg |
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.
Thanks @andygrove
Looks good to me -- I had some comments, but nothing that needs to be done in this PR (could be done in a follow on).
https://www.postgresql.org/docs/current/sql-expressions.html
4.2.7. Aggregate Expressions
Today I learned that postgres aggregates support all sorts of semi-wild things (like an ORDER BY clause)
@@ -231,6 +231,8 @@ pub enum Expr { | |||
args: Vec<Expr>, | |||
/// Whether this is a DISTINCT aggregation or not | |||
distinct: bool, | |||
/// Optional filter |
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.
/// Optional filter | |
/// Optional filter applied prior to aggregating |
@@ -1009,6 +1012,7 @@ mod roundtrip_tests { | |||
let test_expr = Expr::AggregateUDF { | |||
fun: Arc::new(dummy_agg.clone()), | |||
args: vec![lit(1.0_f64)], | |||
filter: Some(Box::new(lit(true))), |
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.
👍
filter: match filter { | ||
Some(e) => Some(Box::new(e.as_ref().try_into()?)), | ||
None => None, | ||
} |
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.
I think you can write this kind of thing somewhat more concisely via
filter: match filter { | |
Some(e) => Some(Box::new(e.as_ref().try_into()?)), | |
None => None, | |
} | |
filter: filter | |
.map(|filter| Box::new(e.as_ref().try_into())) | |
.transpose()? |
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.
I experimented with this locally but could not get this to work.
datafusion/sql/src/planner.rs
Outdated
Expr::AggregateFunction { | ||
fun, args, distinct, .. | ||
} => Ok(Expr::AggregateFunction { fun, args, distinct, filter: Some(Box::new(self.sql_expr_to_logical_expr(*filter, schema, ctes)?)) }), | ||
_ => Err(DataFusionError::Internal("".to_string())) |
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.
The error message here appears to be missing
Thanks for the review @alamb. I fixed the error message. |
Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
Benchmark runs are scheduled for baseline = 1d4a928 and contender = 30e8cef. 30e8cef is a master commit associated with this PR. Results will be available as each benchmark for each run completes. |
Which issue does this PR close?
Part of #2214
Rationale for this change
Adds support to the SQL query planner and logical plan for aggregate expressions with filters e.g.
aggr_expr(foo) FILTER (WHERE ...)
What changes are included in this PR?
Expr::AggregateFunction
andExpr::AggregateUDF
have a newfilter
attributeAre there any user-facing changes?
Yes,
Expr
API change