Skip to content
Merged
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
4 changes: 2 additions & 2 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -992,13 +992,14 @@ type Where struct {

// GroupBy is GROUP BY clause node.
//
// GROUP BY {{.Exprs | sqlJoin ","}}
// GROUP {{.Hint | sqlOpt}} BY {{.Exprs | sqlJoin ","}}
type GroupBy struct {
// pos = Group
// end = Exprs[$].end

Group token.Pos // position of "GROUP" keyword

Hint *Hint // optional
Exprs []Expr // len(Exprs) > 0
}

Expand Down Expand Up @@ -2302,7 +2303,6 @@ type DropSchema struct {
Name *Ident
}


// CreateDatabase is CREATE DATABASE statement node.
//
// CREATE DATABASE {{.Name | sql}}
Expand Down
6 changes: 4 additions & 2 deletions ast/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func (w *Where) SQL() string {
}

func (g *GroupBy) SQL() string {
return "GROUP BY " + sqlJoin(g.Exprs, ", ")
return "GROUP " + sqlOpt("", g.Hint, " ") + "BY " + sqlJoin(g.Exprs, ", ")
}

func (h *Having) SQL() string {
Expand Down Expand Up @@ -776,7 +776,9 @@ func (s *CreateSchema) SQL() string {
return "CREATE" + strOpt(s.OrReplace, " OR REPLACE") + " SCHEMA " + strOpt(s.IfNotExists, "IF NOT EXISTS ") + s.Name.SQL()
}

func (s *DropSchema) SQL() string { return "DROP SCHEMA " + strOpt(s.IfExists, "IF EXISTS ") + s.Name.SQL() }
func (s *DropSchema) SQL() string {
return "DROP SCHEMA " + strOpt(s.IfExists, "IF EXISTS ") + s.Name.SQL()
}

func (d *AlterDatabase) SQL() string {
return "ALTER DATABASE " + d.Name.SQL() + " SET " + d.Options.SQL()
Expand Down
1 change: 1 addition & 0 deletions ast/walk_internal.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,11 +794,13 @@ func (p *Parser) tryParseGroupBy() *ast.GroupBy {
return nil
}
pos := p.expect("GROUP").Pos
hint := p.tryParseHint()
p.expect("BY")
exprs := parseCommaSeparatedList(p, p.parseExpr)

return &ast.GroupBy{
Group: pos,
Hint: hint,
Exprs: exprs,
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SELECT
FirstName, BirthDate
FROM
Singers
GROUP @{GROUP_METHOD=HASH_GROUP} BY
FirstName, BirthDate
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
--- select_singer_with_groupby_group_hints.sql
SELECT
FirstName, BirthDate
FROM
Singers
GROUP @{GROUP_METHOD=HASH_GROUP} BY
FirstName, BirthDate

--- AST
&ast.QueryStatement{
Query: &ast.Select{
Results: []ast.SelectItem{
&ast.ExprSelectItem{
Expr: &ast.Ident{
NamePos: 9,
NameEnd: 18,
Name: "FirstName",
},
},
&ast.ExprSelectItem{
Expr: &ast.Ident{
NamePos: 20,
NameEnd: 29,
Name: "BirthDate",
},
},
},
From: &ast.From{
From: 30,
Source: &ast.TableName{
Table: &ast.Ident{
NamePos: 37,
NameEnd: 44,
Name: "Singers",
},
},
},
GroupBy: &ast.GroupBy{
Group: 45,
Hint: &ast.Hint{
Atmark: 51,
Rbrace: 76,
Records: []*ast.HintRecord{
&ast.HintRecord{
Key: &ast.Path{
Idents: []*ast.Ident{
&ast.Ident{
NamePos: 53,
NameEnd: 65,
Name: "GROUP_METHOD",
},
},
},
Value: &ast.Ident{
NamePos: 66,
NameEnd: 76,
Name: "HASH_GROUP",
},
},
},
},
Exprs: []ast.Expr{
&ast.Ident{
NamePos: 83,
NameEnd: 92,
Name: "FirstName",
},
&ast.Ident{
NamePos: 94,
NameEnd: 103,
Name: "BirthDate",
},
},
},
},
}

--- SQL
SELECT FirstName, BirthDate FROM Singers GROUP @{GROUP_METHOD=HASH_GROUP} BY FirstName, BirthDate
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
--- select_singer_with_groupby_group_hints.sql
SELECT
FirstName, BirthDate
FROM
Singers
GROUP @{GROUP_METHOD=HASH_GROUP} BY
FirstName, BirthDate

--- AST
&ast.QueryStatement{
Query: &ast.Select{
Results: []ast.SelectItem{
&ast.ExprSelectItem{
Expr: &ast.Ident{
NamePos: 9,
NameEnd: 18,
Name: "FirstName",
},
},
&ast.ExprSelectItem{
Expr: &ast.Ident{
NamePos: 20,
NameEnd: 29,
Name: "BirthDate",
},
},
},
From: &ast.From{
From: 30,
Source: &ast.TableName{
Table: &ast.Ident{
NamePos: 37,
NameEnd: 44,
Name: "Singers",
},
},
},
GroupBy: &ast.GroupBy{
Group: 45,
Hint: &ast.Hint{
Atmark: 51,
Rbrace: 76,
Records: []*ast.HintRecord{
&ast.HintRecord{
Key: &ast.Path{
Idents: []*ast.Ident{
&ast.Ident{
NamePos: 53,
NameEnd: 65,
Name: "GROUP_METHOD",
},
},
},
Value: &ast.Ident{
NamePos: 66,
NameEnd: 76,
Name: "HASH_GROUP",
},
},
},
},
Exprs: []ast.Expr{
&ast.Ident{
NamePos: 83,
NameEnd: 92,
Name: "FirstName",
},
&ast.Ident{
NamePos: 94,
NameEnd: 103,
Name: "BirthDate",
},
},
},
},
}

--- SQL
SELECT FirstName, BirthDate FROM Singers GROUP @{GROUP_METHOD=HASH_GROUP} BY FirstName, BirthDate
Loading