Skip to content

Commit a899fb6

Browse files
committed
Add GROUPING SETS support for EXPLAIN AST output
Each grouping set element in GROUPING SETS clause needs to be wrapped in an ExpressionList in the EXPLAIN output. Added GroupingSets flag to SelectQuery AST to track when GROUPING SETS is used, and updated the explain code to output the wrapper when needed. Fixes 66 statements across 25 tests.
1 parent 4e5e254 commit a899fb6

File tree

28 files changed

+31
-135
lines changed

28 files changed

+31
-135
lines changed

ast/ast.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ type SelectQuery struct {
6464
PreWhere Expression `json:"prewhere,omitempty"`
6565
Where Expression `json:"where,omitempty"`
6666
GroupBy []Expression `json:"group_by,omitempty"`
67+
GroupingSets bool `json:"grouping_sets,omitempty"` // true if GROUP BY uses GROUPING SETS
6768
WithRollup bool `json:"with_rollup,omitempty"`
6869
WithCube bool `json:"with_cube,omitempty"`
6970
WithTotals bool `json:"with_totals,omitempty"`

internal/explain/select.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,13 @@ func explainSelectQuery(sb *strings.Builder, n *ast.SelectQuery, indent string,
100100
if len(n.GroupBy) > 0 {
101101
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.GroupBy))
102102
for _, g := range n.GroupBy {
103-
Node(sb, g, depth+2)
103+
if n.GroupingSets {
104+
// Each grouping set is wrapped in an ExpressionList
105+
fmt.Fprintf(sb, "%s ExpressionList (children 1)\n", indent)
106+
Node(sb, g, depth+3)
107+
} else {
108+
Node(sb, g, depth+2)
109+
}
104110
}
105111
}
106112
// HAVING

parser/parser.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,7 @@ func (p *Parser) parseSelect() *ast.SelectQuery {
534534
p.nextToken() // skip GROUPING
535535
p.nextToken() // skip SETS
536536
sel.GroupBy = p.parseGroupingSets()
537+
sel.GroupingSets = true
537538
} else if p.currentIs(token.ROLLUP) && p.peekIs(token.LPAREN) {
538539
// ROLLUP(a, b, c)
539540
p.nextToken() // skip ROLLUP

parser/testdata/01883_grouping_sets_crash/metadata.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"stmt10": true,
44
"stmt11": true,
55
"stmt12": true,
6-
"stmt13": true,
76
"stmt14": true,
87
"stmt4": true,
98
"stmt6": true,

parser/testdata/01883_with_grouping_sets/metadata.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
"stmt10": true,
44
"stmt11": true,
55
"stmt12": true,
6-
"stmt14": true,
7-
"stmt15": true,
8-
"stmt4": true,
96
"stmt6": true,
107
"stmt7": true,
118
"stmt8": true,
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
{
22
"explain_todo": {
3-
"stmt10": true,
4-
"stmt2": true,
5-
"stmt3": true,
6-
"stmt4": true,
7-
"stmt5": true,
8-
"stmt6": true,
9-
"stmt7": true,
10-
"stmt8": true,
11-
"stmt9": true
3+
"stmt7": true
124
}
135
}
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt10": true,
4-
"stmt6": true,
5-
"stmt7": true
6-
}
7-
}
1+
{}
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt6": true,
4-
"stmt7": true
5-
}
6-
}
1+
{}

parser/testdata/02481_analyzer_optimize_grouping_sets_keys/metadata.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"explain_todo": {
3-
"stmt3": true,
43
"stmt4": true,
54
"stmt5": true,
65
"stmt6": true
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt2": true
4-
}
5-
}
1+
{}

0 commit comments

Comments
 (0)