-
Notifications
You must be signed in to change notification settings - Fork 572
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
RFC: selective aggregates #3506
Comments
+1 for this. By the way,
The "alternative approach" doesn't seem to work perfectly. |
Background
This is a useful feature according to some articles. This feature is supported by PostgreSQL and we can use it to implement distinct aggregation.
Design
Syntax:
AGG(<expression>) FILTER(WHERE <condition>)
, e.g.sum(v1) FILTER(WHERE v1 < 5)
.The
FILTER
clause extends aggregate functions (sum
,avg
,count
, …) by an additionalWHERE
clause. The result of the aggregate is built from only the rows that satisfy the additionalWHERE
clause too.In frontend, we can add
Condition
forPlanAggCall
. As for the backend, is it easy to implement this feature? cc @StrikeWDiscussions
An alternative approach is using
CASE .. WHEN ..
to wrap aggregate's argument. Generally, we just need to useCASE .. WHEN ..
to wrap the arguments and compute it inLogicalProject
belowLogicalAgg
. However, we have to insert an extraLogicalProject
when we want use this alternative approach to implement distinct aggregation, which might bring more performance cost.In conclusion, selective aggregates are clearer and more convenient.
Reference
PostgreSQL document
Modern SQL
The text was updated successfully, but these errors were encountered: