Skip to content

Commit c0d59a4

Browse files
committed
Add OPTIMIZE CLEANUP and INSERT (*) support
- Add Cleanup flag to OptimizeQuery for OPTIMIZE TABLE ... FINAL CLEANUP - Update explain output to append _cleanup to table name when Cleanup=true - Add AllColumns flag to InsertQuery for INSERT INTO table (*) syntax - Update explain output for INSERT with (*) to show Asterisk child - Update parser to handle both new syntaxes
1 parent 9f044c0 commit c0d59a4

8 files changed

Lines changed: 40 additions & 76 deletions

File tree

ast/ast.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ type InsertQuery struct {
225225
Table string `json:"table,omitempty"`
226226
Function *FunctionCall `json:"function,omitempty"` // For INSERT INTO FUNCTION syntax
227227
Columns []*Identifier `json:"columns,omitempty"`
228+
AllColumns bool `json:"all_columns,omitempty"` // For (*) syntax meaning all columns
228229
PartitionBy Expression `json:"partition_by,omitempty"` // For PARTITION BY clause
229230
Infile string `json:"infile,omitempty"` // For FROM INFILE clause
230231
Compression string `json:"compression,omitempty"` // For COMPRESSION clause
@@ -740,6 +741,7 @@ type OptimizeQuery struct {
740741
Table string `json:"table"`
741742
Partition Expression `json:"partition,omitempty"`
742743
Final bool `json:"final,omitempty"`
744+
Cleanup bool `json:"cleanup,omitempty"`
743745
Dedupe bool `json:"dedupe,omitempty"`
744746
OnCluster string `json:"on_cluster,omitempty"`
745747
}

internal/explain/statements.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func explainInsertQuery(sb *strings.Builder, n *ast.InsertQuery, indent string,
2424
children++ // Database identifier (separate from table)
2525
}
2626
}
27-
if len(n.Columns) > 0 {
27+
if len(n.Columns) > 0 || n.AllColumns {
2828
children++ // Column list
2929
}
3030
if n.Select != nil {
@@ -58,7 +58,10 @@ func explainInsertQuery(sb *strings.Builder, n *ast.InsertQuery, indent string,
5858
}
5959

6060
// Column list
61-
if len(n.Columns) > 0 {
61+
if n.AllColumns {
62+
fmt.Fprintf(sb, "%s ExpressionList (children 1)\n", indent)
63+
fmt.Fprintf(sb, "%s Asterisk\n", indent)
64+
} else if len(n.Columns) > 0 {
6265
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, len(n.Columns))
6366
for _, col := range n.Columns {
6467
fmt.Fprintf(sb, "%s Identifier %s\n", indent, col.Parts[len(col.Parts)-1])
@@ -1282,6 +1285,9 @@ func explainOptimizeQuery(sb *strings.Builder, n *ast.OptimizeQuery, indent stri
12821285
if n.Final {
12831286
name += "_final"
12841287
}
1288+
if n.Cleanup {
1289+
name += "_cleanup"
1290+
}
12851291

12861292
children := 1 // identifier
12871293
if n.Partition != nil {

parser/parser.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,19 +1179,25 @@ func (p *Parser) parseInsert() *ast.InsertQuery {
11791179
// Parse column list
11801180
if p.currentIs(token.LPAREN) {
11811181
p.nextToken()
1182-
for !p.currentIs(token.RPAREN) && !p.currentIs(token.EOF) {
1183-
pos := p.current.Pos
1184-
colName := p.parseIdentifierName()
1185-
if colName != "" {
1186-
ins.Columns = append(ins.Columns, &ast.Identifier{
1187-
Position: pos,
1188-
Parts: []string{colName},
1189-
})
1190-
}
1191-
if p.currentIs(token.COMMA) {
1192-
p.nextToken()
1193-
} else {
1194-
break
1182+
// Check for (*) meaning all columns
1183+
if p.currentIs(token.ASTERISK) {
1184+
ins.AllColumns = true
1185+
p.nextToken()
1186+
} else {
1187+
for !p.currentIs(token.RPAREN) && !p.currentIs(token.EOF) {
1188+
pos := p.current.Pos
1189+
colName := p.parseIdentifierName()
1190+
if colName != "" {
1191+
ins.Columns = append(ins.Columns, &ast.Identifier{
1192+
Position: pos,
1193+
Parts: []string{colName},
1194+
})
1195+
}
1196+
if p.currentIs(token.COMMA) {
1197+
p.nextToken()
1198+
} else {
1199+
break
1200+
}
11951201
}
11961202
}
11971203
p.expect(token.RPAREN)
@@ -4374,6 +4380,12 @@ func (p *Parser) parseOptimize() *ast.OptimizeQuery {
43744380
p.nextToken()
43754381
}
43764382

4383+
// Handle CLEANUP
4384+
if p.currentIs(token.IDENT) && strings.ToUpper(p.current.Value) == "CLEANUP" {
4385+
opt.Cleanup = true
4386+
p.nextToken()
4387+
}
4388+
43774389
// Handle DEDUPLICATE
43784390
if p.currentIs(token.IDENT) && strings.ToUpper(p.current.Value) == "DEDUPLICATE" {
43794391
opt.Dedupe = true
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
{
22
"explain_todo": {
3-
"stmt12": true,
4-
"stmt13": true,
5-
"stmt14": true,
6-
"stmt15": true,
7-
"stmt16": true,
8-
"stmt7": true
3+
"stmt15": true
94
}
105
}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt8": 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-
"stmt12": true
4-
}
5-
}
1+
{}
Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,6 @@
11
{
22
"explain_todo": {
3-
"stmt103": true,
4-
"stmt104": true,
5-
"stmt108": true,
6-
"stmt109": true,
7-
"stmt11": true,
8-
"stmt14": true,
9-
"stmt18": true,
10-
"stmt23": true,
11-
"stmt26": true,
12-
"stmt30": true,
13-
"stmt31": true,
14-
"stmt33": true,
15-
"stmt35": true,
16-
"stmt37": true,
17-
"stmt4": true,
18-
"stmt42": true,
19-
"stmt49": true,
20-
"stmt50": true,
21-
"stmt55": true,
22-
"stmt56": true,
23-
"stmt57": true,
243
"stmt58": true,
25-
"stmt59": true,
26-
"stmt64": true,
27-
"stmt65": true,
28-
"stmt7": true,
29-
"stmt71": true,
30-
"stmt75": true,
31-
"stmt77": true,
32-
"stmt81": true,
33-
"stmt83": true,
34-
"stmt88": true,
35-
"stmt89": true,
36-
"stmt93": true,
37-
"stmt94": true,
38-
"stmt98": true,
39-
"stmt99": true
4+
"stmt65": true
405
}
416
}
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt10": true,
4-
"stmt3": true,
5-
"stmt4": true,
6-
"stmt6": true,
7-
"stmt8": true
8-
}
9-
}
1+
{}

0 commit comments

Comments
 (0)