Skip to content
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 DISTINCT/ALL for aggregate functions #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2373,6 +2373,16 @@ func (p *Parser) parsePrefix() (sqlast.Node, error) {

func (p *Parser) parseFunction(name *sqlast.ObjectName) (sqlast.Node, error) {
p.expectToken(sqltoken.LParen)

var filter *sqlast.Ident
if ok, _, _ := p.parseKeyword("DISTINCT"); ok {
p.prevToken()
filter, _ = p.parseIdentifier()
} else if ok, _, _ := p.parseKeyword("ALL"); ok {
p.prevToken()
filter, _ = p.parseIdentifier()
}

args, err := p.parseOptionalArgs()
if err != nil {
return nil, errors.Errorf("parseOptionalArgs failed: %w", err)
Expand Down Expand Up @@ -2434,6 +2444,7 @@ func (p *Parser) parseFunction(name *sqlast.ObjectName) (sqlast.Node, error) {
Args: args,
Over: over,
ArgsRParen: r.To,
Filter: filter,
}, nil
}

Expand Down
50 changes: 50 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,56 @@ FROM user WHERE id BETWEEN 1 AND 2`,
},
},
},
{
name: "count distinct",
in: "SELECT COUNT(DISTINCT email) FROM user",
out: &sqlast.QueryStmt{
Body: &sqlast.SQLSelect{
Select: sqltoken.NewPos(1, 1),
Projection: []sqlast.SQLSelectItem{
&sqlast.UnnamedSelectItem{
Node: &sqlast.Function{
Name: &sqlast.ObjectName{
Idents: []*sqlast.Ident{
{
Value: "COUNT",
From: sqltoken.NewPos(1, 8),
To: sqltoken.NewPos(1, 13),
},
},
},
Args: []sqlast.Node{
&sqlast.Ident{
Value: "email",
From: sqltoken.NewPos(1, 23),
To: sqltoken.NewPos(1, 28),
},
},
Filter: &sqlast.Ident{
Value: "DISTINCT",
From: sqltoken.NewPos(1, 14),
To: sqltoken.NewPos(1, 22),
},
ArgsRParen: sqltoken.NewPos(1, 29),
},
},
},
FromClause: []sqlast.TableReference{
&sqlast.Table{
Name: &sqlast.ObjectName{
Idents: []*sqlast.Ident{
{
Value: "user",
From: sqltoken.NewPos(1, 35),
To: sqltoken.NewPos(1, 39),
},
},
},
},
},
},
},
},
}

for _, c := range cases {
Expand Down
1 change: 1 addition & 0 deletions sqlast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ func (s *UnaryExpr) WriteTo(w io.Writer) (int64, error) {
type Function struct {
Name *ObjectName // Function Name
Args []Node
Filter *Ident
ArgsRParen sqltoken.Pos // function args RParen position
Over *WindowSpec
OverRparen sqltoken.Pos // Over RParen position (if Over is not nil)
Expand Down