Skip to content

Commit 259d663

Browse files
committed
Fix GROUPING SETS tuple unwrapping in EXPLAIN output
When outputting GROUPING SETS elements, unwrap tuple literals to output their elements directly with correct child counts instead of wrapping in ExpressionList(children 1) and outputting as Function tuple. Fixes tests for GROUPING SETS queries across 12 test files.
1 parent 7ff6679 commit 259d663

File tree

13 files changed

+29
-74
lines changed

13 files changed

+29
-74
lines changed

internal/explain/select.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,23 @@ func explainSelectQuery(sb *strings.Builder, n *ast.SelectQuery, indent string,
102102
for _, g := range n.GroupBy {
103103
if n.GroupingSets {
104104
// Each grouping set is wrapped in an ExpressionList
105-
fmt.Fprintf(sb, "%s ExpressionList (children 1)\n", indent)
106-
Node(sb, g, depth+3)
105+
// but we need to unwrap tuples and output elements directly
106+
if lit, ok := g.(*ast.Literal); ok && lit.Type == ast.LiteralTuple {
107+
if elements, ok := lit.Value.([]ast.Expression); ok {
108+
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(elements))
109+
for _, elem := range elements {
110+
Node(sb, elem, depth+3)
111+
}
112+
} else {
113+
// Fallback for unexpected tuple value type
114+
fmt.Fprintf(sb, "%s ExpressionList (children 1)\n", indent)
115+
Node(sb, g, depth+3)
116+
}
117+
} else {
118+
// Single expression grouping set
119+
fmt.Fprintf(sb, "%s ExpressionList (children 1)\n", indent)
120+
Node(sb, g, depth+3)
121+
}
107122
} else {
108123
Node(sb, g, depth+2)
109124
}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt4": true
4-
}
5-
}
1+
{}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt1": true
4-
}
5-
}
1+
{}
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt4": true,
4-
"stmt5": true,
5-
"stmt6": true
6-
}
7-
}
1+
{}
Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt10": true,
4-
"stmt13": true,
5-
"stmt16": true,
6-
"stmt17": true,
7-
"stmt6": true,
8-
"stmt9": true
9-
}
10-
}
1+
{}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt1": true
4-
}
5-
}
1+
{}
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt2": true,
4-
"stmt3": true,
5-
"stmt4": true
6-
}
7-
}
1+
{}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt4": true
4-
}
5-
}
1+
{}
Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt10": true,
4-
"stmt11": true,
5-
"stmt5": true,
6-
"stmt6": true,
7-
"stmt7": true,
8-
"stmt8": true
9-
}
10-
}
1+
{}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt1": true
4-
}
5-
}
1+
{}

0 commit comments

Comments
 (0)