Skip to content

Commit 4ff9272

Browse files
committed
Fix GROUPING SETS tuple unwrapping and add TOP clause support
- Fixed GROUPING SETS handling to unwrap tuple literals and output elements directly with correct child counts - Added TOP clause support to SELECT parser (use MUL_PREC to stop at *) - Added TOP clause output to EXPLAIN AST at end of SelectQuery - Updated 3 GROUPING SETS tests that now pass The TOP fix prevents `SELECT TOP 5 * FROM t` from being misparsed as `TOP (5 * FROM t)` where * was treated as multiplication.
1 parent 259d663 commit 4ff9272

File tree

5 files changed

+12
-20
lines changed

5 files changed

+12
-20
lines changed

internal/explain/select.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ func explainSelectQuery(sb *strings.Builder, n *ast.SelectQuery, indent string,
181181
if len(n.Settings) > 0 && !n.SettingsAfterFormat {
182182
fmt.Fprintf(sb, "%s Set\n", indent)
183183
}
184+
// TOP clause is output at the end
185+
if n.Top != nil {
186+
Node(sb, n.Top, depth+1)
187+
}
184188
}
185189

186190
func explainOrderByElement(sb *strings.Builder, n *ast.OrderByElement, indent string, depth int) {
@@ -355,5 +359,9 @@ func countSelectQueryChildren(n *ast.SelectQuery) int {
355359
if len(n.Settings) > 0 && !n.SettingsAfterFormat {
356360
count++
357361
}
362+
// TOP clause
363+
if n.Top != nil {
364+
count++
365+
}
358366
return count
359367
}

parser/parser.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,8 @@ func (p *Parser) parseSelect() *ast.SelectQuery {
492492
// Handle TOP
493493
if p.currentIs(token.TOP) {
494494
p.nextToken()
495-
sel.Top = p.parseExpression(LOWEST)
495+
// Use MUL_PREC to stop at * (which would be parsed as column selector, not multiplication)
496+
sel.Top = p.parseExpression(MUL_PREC)
496497
}
497498

498499
// Parse column list
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
{
22
"explain_todo": {
3-
"stmt10": true,
4-
"stmt11": true,
5-
"stmt12": true,
6-
"stmt14": true,
7-
"stmt4": true,
8-
"stmt6": true,
9-
"stmt8": true,
10-
"stmt9": true
3+
"stmt6": true
114
}
125
}
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
{
22
"explain_todo": {
3-
"stmt10": true,
4-
"stmt11": true,
5-
"stmt12": true,
6-
"stmt6": true,
7-
"stmt7": true,
8-
"stmt8": true,
93
"stmt9": true
104
}
115
}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt6": true
4-
}
5-
}
1+
{}

0 commit comments

Comments
 (0)