Skip to content

Commit b36d690

Browse files
committed
Add SYSTEM RELOAD DICTIONARY and DELETE FROM support
- SYSTEM RELOAD DICTIONARY now outputs dictionary name twice as expected - Add DeleteQuery AST type and parsing for DELETE FROM ... WHERE - Add explain output for DeleteQuery
1 parent 4b1adb3 commit b36d690

58 files changed

Lines changed: 126 additions & 268 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

ast/ast.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,18 @@ func (t *TruncateQuery) Pos() token.Position { return t.Position }
672672
func (t *TruncateQuery) End() token.Position { return t.Position }
673673
func (t *TruncateQuery) statementNode() {}
674674

675+
// DeleteQuery represents a lightweight DELETE statement.
676+
type DeleteQuery struct {
677+
Position token.Position `json:"-"`
678+
Database string `json:"database,omitempty"`
679+
Table string `json:"table"`
680+
Where Expression `json:"where,omitempty"`
681+
}
682+
683+
func (d *DeleteQuery) Pos() token.Position { return d.Position }
684+
func (d *DeleteQuery) End() token.Position { return d.Position }
685+
func (d *DeleteQuery) statementNode() {}
686+
675687
// UseQuery represents a USE statement.
676688
type UseQuery struct {
677689
Position token.Position `json:"-"`

internal/explain/explain.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ func Node(sb *strings.Builder, node interface{}, depth int) {
238238
explainOptimizeQuery(sb, n, indent, depth)
239239
case *ast.TruncateQuery:
240240
explainTruncateQuery(sb, n, indent)
241+
case *ast.DeleteQuery:
242+
explainDeleteQuery(sb, n, indent, depth)
241243
case *ast.CheckQuery:
242244
explainCheckQuery(sb, n, indent)
243245
case *ast.CreateIndexQuery:

internal/explain/statements.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,25 @@ func explainTruncateQuery(sb *strings.Builder, n *ast.TruncateQuery, indent stri
15181518
}
15191519
}
15201520

1521+
func explainDeleteQuery(sb *strings.Builder, n *ast.DeleteQuery, indent string, depth int) {
1522+
if n == nil {
1523+
fmt.Fprintf(sb, "%s*ast.DeleteQuery\n", indent)
1524+
return
1525+
}
1526+
1527+
// Count children: Where expression + table identifier
1528+
children := 1 // table identifier
1529+
if n.Where != nil {
1530+
children++
1531+
}
1532+
1533+
fmt.Fprintf(sb, "%sDeleteQuery %s (children %d)\n", indent, n.Table, children)
1534+
if n.Where != nil {
1535+
Node(sb, n.Where, depth+1)
1536+
}
1537+
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Table)
1538+
}
1539+
15211540
func explainCheckQuery(sb *strings.Builder, n *ast.CheckQuery, indent string) {
15221541
if n == nil {
15231542
fmt.Fprintf(sb, "%s*ast.CheckQuery\n", indent)

parser/parser.go

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ func (p *Parser) parseStatement() ast.Statement {
205205
return p.parseSet()
206206
case token.UPDATE:
207207
return p.parseUpdate()
208+
case token.DELETE:
209+
return p.parseDelete()
208210
case token.OPTIMIZE:
209211
return p.parseOptimize()
210212
case token.SYSTEM:
@@ -4968,6 +4970,39 @@ func (p *Parser) parseUpdate() *ast.UpdateQuery {
49684970
return update
49694971
}
49704972

4973+
func (p *Parser) parseDelete() *ast.DeleteQuery {
4974+
del := &ast.DeleteQuery{
4975+
Position: p.current.Pos,
4976+
}
4977+
4978+
p.nextToken() // skip DELETE
4979+
4980+
// Skip optional FROM
4981+
if p.currentIs(token.FROM) {
4982+
p.nextToken()
4983+
}
4984+
4985+
// Parse table name (can be database.table)
4986+
tableName := p.parseIdentifierName()
4987+
if tableName != "" {
4988+
if p.currentIs(token.DOT) {
4989+
p.nextToken()
4990+
del.Database = tableName
4991+
del.Table = p.parseIdentifierName()
4992+
} else {
4993+
del.Table = tableName
4994+
}
4995+
}
4996+
4997+
// Parse WHERE clause
4998+
if p.currentIs(token.WHERE) {
4999+
p.nextToken() // skip WHERE
5000+
del.Where = p.parseExpression(LOWEST)
5001+
}
5002+
5003+
return del
5004+
}
5005+
49715006
func (p *Parser) parseUse() *ast.UseQuery {
49725007
use := &ast.UseQuery{
49735008
Position: p.current.Pos,
@@ -5483,7 +5518,14 @@ func (p *Parser) parseSystem() *ast.SystemQuery {
54835518
p.nextToken()
54845519
}
54855520
} else {
5486-
sys.Table = tableName
5521+
// For RELOAD DICTIONARY commands, the dictionary name appears as both database and table in EXPLAIN
5522+
if strings.Contains(strings.ToUpper(sys.Command), "RELOAD DICTIONARY") ||
5523+
strings.Contains(strings.ToUpper(sys.Command), "DROP REPLICA") {
5524+
sys.Database = tableName
5525+
sys.Table = tableName
5526+
} else {
5527+
sys.Table = tableName
5528+
}
54875529
}
54885530
}
54895531

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt7": 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-
"stmt3": 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-
"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-
"stmt6": 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-
"stmt10": true
4-
}
5-
}
1+
{}
Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
{
22
"explain_todo": {
3-
"stmt11": true,
4-
"stmt16": true,
5-
"stmt26": true,
6-
"stmt30": true,
7-
"stmt45": true,
8-
"stmt52": true,
9-
"stmt6": true,
10-
"stmt67": true,
11-
"stmt72": true,
12-
"stmt74": true
3+
"stmt45": true
134
}
145
}

0 commit comments

Comments
 (0)