Skip to content

Conversation

@mccanne
Copy link
Collaborator

@mccanne mccanne commented Dec 23, 2025

This commit changes the "where " clause on agg functions to use the SQL syntax "filter ( )".

While we were here, we changed ast.Agg to ast.AggFunc, removed the Where field from ast.CallExpr, and cleaned up the grammar for how agg functions are integrated into expressions and shortcuts.

We also fixed a big where "count() where ..." did not previously parse, but "count() filter ( ... )" now works.

Fixes #6261

This commit changes the "where <expr>" clause on agg functions
to use the SQL syntax "filter ( <expr> )".

While we were here, we change ast.Agg to ast.AggFunc, removed
the Where field from ast.CallExpr, and cleaned up the grammar
for how agg functions are integrated into expressions and shortcuts.

We also fixed a big where "count(*) where ..." did not previously
parse, but "count(*) filter ( ... )" now works.
// upon a function of the record, e.g., count() counts up records without
// looking into them.
type Agg struct {
type AggFunc struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: While you're here, maybe move this to expr.go?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also gonna call it AggFuncExpr


FilterClause = _ FILTER __ "(" __ expr:LogicalOrExpr __ ")" { return expr, nil }

WhereClause = _ WHERE _ expr:LogicalOrExpr { return expr, nil }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Since this is now used only in FromSelect and Select, maybe move it down where OptWhereClause used to live?


func newCall(c *current, name, args, where any) ast.Expr {
func newCall(c *current, name, args any) ast.Expr {
call := &ast.CallExpr{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

Suggested change
call := &ast.CallExpr{
return &ast.CallExpr{

},
}
values := sem.NewValues(agg, sem.NewThis(agg, []string{name}))
return append(append(seq, aggregate), values)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

Suggested change
return append(append(seq, aggregate), values)
return append(seq, aggregate, values)

@mccanne mccanne merged commit 3c25564 into main Dec 23, 2025
4 checks passed
@mccanne mccanne deleted the agg-filter branch December 23, 2025 17:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQL: Can't do math between aggregate function calls with modifiers (e.g., DISTINCT)

3 participants