Skip to content

Commit ed2e73d

Browse files
authored
Implement group hints (#341)
1 parent a868d89 commit ed2e73d

File tree

7 files changed

+173
-4
lines changed

7 files changed

+173
-4
lines changed

ast/ast.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -992,13 +992,14 @@ type Where struct {
992992

993993
// GroupBy is GROUP BY clause node.
994994
//
995-
// GROUP BY {{.Exprs | sqlJoin ","}}
995+
// GROUP {{.Hint | sqlOpt}} BY {{.Exprs | sqlJoin ","}}
996996
type GroupBy struct {
997997
// pos = Group
998998
// end = Exprs[$].end
999999

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

1002+
Hint *Hint // optional
10021003
Exprs []Expr // len(Exprs) > 0
10031004
}
10041005

@@ -2302,7 +2303,6 @@ type DropSchema struct {
23022303
Name *Ident
23032304
}
23042305

2305-
23062306
// CreateDatabase is CREATE DATABASE statement node.
23072307
//
23082308
// CREATE DATABASE {{.Name | sql}}

ast/sql.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ func (w *Where) SQL() string {
281281
}
282282

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

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

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

781783
func (d *AlterDatabase) SQL() string {
782784
return "ALTER DATABASE " + d.Name.SQL() + " SET " + d.Options.SQL()

ast/walk_internal.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

parser.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,11 +794,13 @@ func (p *Parser) tryParseGroupBy() *ast.GroupBy {
794794
return nil
795795
}
796796
pos := p.expect("GROUP").Pos
797+
hint := p.tryParseHint()
797798
p.expect("BY")
798799
exprs := parseCommaSeparatedList(p, p.parseExpr)
799800

800801
return &ast.GroupBy{
801802
Group: pos,
803+
Hint: hint,
802804
Exprs: exprs,
803805
}
804806
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
SELECT
2+
FirstName, BirthDate
3+
FROM
4+
Singers
5+
GROUP @{GROUP_METHOD=HASH_GROUP} BY
6+
FirstName, BirthDate
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
--- select_singer_with_groupby_group_hints.sql
2+
SELECT
3+
FirstName, BirthDate
4+
FROM
5+
Singers
6+
GROUP @{GROUP_METHOD=HASH_GROUP} BY
7+
FirstName, BirthDate
8+
9+
--- AST
10+
&ast.QueryStatement{
11+
Query: &ast.Select{
12+
Results: []ast.SelectItem{
13+
&ast.ExprSelectItem{
14+
Expr: &ast.Ident{
15+
NamePos: 9,
16+
NameEnd: 18,
17+
Name: "FirstName",
18+
},
19+
},
20+
&ast.ExprSelectItem{
21+
Expr: &ast.Ident{
22+
NamePos: 20,
23+
NameEnd: 29,
24+
Name: "BirthDate",
25+
},
26+
},
27+
},
28+
From: &ast.From{
29+
From: 30,
30+
Source: &ast.TableName{
31+
Table: &ast.Ident{
32+
NamePos: 37,
33+
NameEnd: 44,
34+
Name: "Singers",
35+
},
36+
},
37+
},
38+
GroupBy: &ast.GroupBy{
39+
Group: 45,
40+
Hint: &ast.Hint{
41+
Atmark: 51,
42+
Rbrace: 76,
43+
Records: []*ast.HintRecord{
44+
&ast.HintRecord{
45+
Key: &ast.Path{
46+
Idents: []*ast.Ident{
47+
&ast.Ident{
48+
NamePos: 53,
49+
NameEnd: 65,
50+
Name: "GROUP_METHOD",
51+
},
52+
},
53+
},
54+
Value: &ast.Ident{
55+
NamePos: 66,
56+
NameEnd: 76,
57+
Name: "HASH_GROUP",
58+
},
59+
},
60+
},
61+
},
62+
Exprs: []ast.Expr{
63+
&ast.Ident{
64+
NamePos: 83,
65+
NameEnd: 92,
66+
Name: "FirstName",
67+
},
68+
&ast.Ident{
69+
NamePos: 94,
70+
NameEnd: 103,
71+
Name: "BirthDate",
72+
},
73+
},
74+
},
75+
},
76+
}
77+
78+
--- SQL
79+
SELECT FirstName, BirthDate FROM Singers GROUP @{GROUP_METHOD=HASH_GROUP} BY FirstName, BirthDate
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
--- select_singer_with_groupby_group_hints.sql
2+
SELECT
3+
FirstName, BirthDate
4+
FROM
5+
Singers
6+
GROUP @{GROUP_METHOD=HASH_GROUP} BY
7+
FirstName, BirthDate
8+
9+
--- AST
10+
&ast.QueryStatement{
11+
Query: &ast.Select{
12+
Results: []ast.SelectItem{
13+
&ast.ExprSelectItem{
14+
Expr: &ast.Ident{
15+
NamePos: 9,
16+
NameEnd: 18,
17+
Name: "FirstName",
18+
},
19+
},
20+
&ast.ExprSelectItem{
21+
Expr: &ast.Ident{
22+
NamePos: 20,
23+
NameEnd: 29,
24+
Name: "BirthDate",
25+
},
26+
},
27+
},
28+
From: &ast.From{
29+
From: 30,
30+
Source: &ast.TableName{
31+
Table: &ast.Ident{
32+
NamePos: 37,
33+
NameEnd: 44,
34+
Name: "Singers",
35+
},
36+
},
37+
},
38+
GroupBy: &ast.GroupBy{
39+
Group: 45,
40+
Hint: &ast.Hint{
41+
Atmark: 51,
42+
Rbrace: 76,
43+
Records: []*ast.HintRecord{
44+
&ast.HintRecord{
45+
Key: &ast.Path{
46+
Idents: []*ast.Ident{
47+
&ast.Ident{
48+
NamePos: 53,
49+
NameEnd: 65,
50+
Name: "GROUP_METHOD",
51+
},
52+
},
53+
},
54+
Value: &ast.Ident{
55+
NamePos: 66,
56+
NameEnd: 76,
57+
Name: "HASH_GROUP",
58+
},
59+
},
60+
},
61+
},
62+
Exprs: []ast.Expr{
63+
&ast.Ident{
64+
NamePos: 83,
65+
NameEnd: 92,
66+
Name: "FirstName",
67+
},
68+
&ast.Ident{
69+
NamePos: 94,
70+
NameEnd: 103,
71+
Name: "BirthDate",
72+
},
73+
},
74+
},
75+
},
76+
}
77+
78+
--- SQL
79+
SELECT FirstName, BirthDate FROM Singers GROUP @{GROUP_METHOD=HASH_GROUP} BY FirstName, BirthDate

0 commit comments

Comments
 (0)