Repository navigation
[core-spec]: add FILTER (WHERE ...) aggregate modifier #382
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
Merged
jbonofre
merged 5 commits into
apache:main
from
christianeu-db:filter-where-aggregate-modifier
Oct 20, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2cd3d6f
feat(core-spec): add FILTER (WHERE ...) aggregate modifier
christianeu-db 8380cb1
docs(core-spec): respond to @kayemkim's feedback on FILTER (WHERE)
christianeu-db 8ba14ef
docs(core-spec): address @jbonofre's pre-vote feedback on FILTER (WHERE)
christianeu-db 8fc8367
docs(core-spec): respond to review feedback on FILTER (WHERE)
christianeu-db 9cad541
docs(core-spec): reference optional feature T612 in Standards Reference
christianeu-db File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,6 +62,8 @@ The core language is based on **ANSI SQL:2003 Core** (ISO/IEC 9075-2:2003), sele | |
| - Well-defined semantics | ||
| - Support for modern analytical features (window functions, CTEs) | ||
|
|
||
| The language also requires optional SQL:2003 feature T612, the aggregate `<filter clause>` (`FILTER (WHERE ...)`), which is not part of Core. See [Conditional Aggregations](#conditional-aggregations-required). | ||
|
|
||
| ### Namespacing and Identifier Resolution | ||
|
|
||
| The identifiers will match standard SQL identifiers: | ||
|
|
@@ -217,17 +219,45 @@ APPROX_PERCENTILE(response_time, 0.95) | |
| ### Conditional Aggregations (REQUIRED) | ||
|
|
||
| SUM / COUNT aggregation functions support `DISTINCT.` | ||
| All aggregations should support filtered aggregation: | ||
| All aggregations must support filtered aggregation: | ||
|
|
||
| ```sql | ||
| -- DISTINCT modifier | ||
| SUM(DISTINCT amount) | ||
| COUNT(DISTINCT customer_id) | ||
|
|
||
| -- Filtered aggregation via CASE | ||
| SUM(CASE WHEN status = 'completed' THEN amount ELSE 0 END) | ||
| SUM(CASE WHEN status = 'completed' THEN amount END) | ||
| COUNT(CASE WHEN status = 'completed' THEN 1 END) | ||
|
|
||
| -- Filtered aggregation via FILTER (WHERE ...) | ||
| SUM(amount) FILTER (WHERE status = 'completed') | ||
| COUNT(*) FILTER (WHERE status = 'completed') | ||
| ``` | ||
|
|
||
| #### `FILTER (WHERE ...)` (REQUIRED) | ||
|
|
||
| Every aggregate function must support a postfix `FILTER (WHERE <predicate>)` modifier: | ||
|
|
||
| ``` | ||
| <aggregate_function>(<args>) FILTER (WHERE <predicate>) | ||
| ``` | ||
|
|
||
| The clause has the semantics of the SQL:2003 `<filter clause>` (optional feature T612): the aggregate considers only the rows for which `<predicate>` succeeds. `FILTER` is applied to the aggregate's input, not as a query `WHERE`, so it never removes output groups. A converter or engine MUST NOT rewrite an intrinsic `FILTER` predicate into a query-level `WHERE` clause when doing so would change group membership. | ||
|
Contributor
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. Please add a sentence in the
Contributor
Author
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. added |
||
|
|
||
| `FILTER (WHERE ...)` is a modifier on an aggregate expression. It is not the standalone `WHERE` clause listed under [Not Supported in Expressions](#not-supported-in-expressions). The `<predicate>` must reference only fields of the same dataset as the aggregate's arguments. | ||
|
|
||
| An engine MAY evaluate `FILTER (WHERE ...)` by rewriting it to an equivalent expression, but the result MUST be identical to the result that the `<filter clause>` semantics define. | ||
|
|
||
| *Note (non-normative):* For an aggregate function that ignores `NULL` inputs, `FILTER (WHERE ...)` is equivalent to a `CASE` expression with no `ELSE`: | ||
|
|
||
| ```sql | ||
| SUM(amount) FILTER (WHERE status = 'completed') --> SUM(CASE WHEN status = 'completed' THEN amount END) | ||
| COUNT(*) FILTER (WHERE status = 'completed') --> COUNT(CASE WHEN status = 'completed' THEN 1 END) | ||
| COUNT(DISTINCT customer_id) FILTER (WHERE status = 'completed') --> COUNT(DISTINCT CASE WHEN status = 'completed' THEN customer_id END) | ||
| ``` | ||
|
|
||
| A non-matching row contributes `NULL`, which these functions ignore. An `ELSE 0` would count as a real row and would corrupt `AVG`, `MIN`, `MAX`, and `COUNT`. The identity does not hold for an aggregate function that does not ignore `NULL` inputs, such as `ARRAY_AGG`. | ||
|
|
||
| ### Decomposability Reference | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This also covers
AGG().relational_semantics.mdsection 6 saysAGG"MUST be treated syntactically and semantically as a aggregate everywhere", so with this line,AGG(m) FILTER (WHERE q)becomes REQUIRED.But nothing defines what it means 😄
For
m = SUM(a) / COUNT(b), or amthat already has its ownFILTER, engines could applyqto each inner aggregate, AND it with the existing filter, or replace that filter, and each would get a different number.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.
@christianeu-db @justint-db I can do that in a follow-up quickly.