Skip to content

Commit 98e6e8e

Browse files
kyleconroyclaude
andcommitted
Add TRUNCATE DATABASE support
Parse TRUNCATE DATABASE differently from TRUNCATE TABLE and format with the correct spacing in EXPLAIN output. ClickHouse uses different spacing conventions for these two variants. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent d928cce commit 98e6e8e

File tree

4 files changed

+19
-13
lines changed

4 files changed

+19
-13
lines changed

ast/ast.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -711,13 +711,14 @@ const (
711711

712712
// TruncateQuery represents a TRUNCATE statement.
713713
type TruncateQuery struct {
714-
Position token.Position `json:"-"`
715-
Temporary bool `json:"temporary,omitempty"`
716-
IfExists bool `json:"if_exists,omitempty"`
717-
Database string `json:"database,omitempty"`
718-
Table string `json:"table"`
719-
OnCluster string `json:"on_cluster,omitempty"`
720-
Settings []*SettingExpr `json:"settings,omitempty"`
714+
Position token.Position `json:"-"`
715+
Temporary bool `json:"temporary,omitempty"`
716+
IfExists bool `json:"if_exists,omitempty"`
717+
TruncateDatabase bool `json:"truncate_database,omitempty"` // True for TRUNCATE DATABASE
718+
Database string `json:"database,omitempty"`
719+
Table string `json:"table"`
720+
OnCluster string `json:"on_cluster,omitempty"`
721+
Settings []*SettingExpr `json:"settings,omitempty"`
721722
}
722723

723724
func (t *TruncateQuery) Pos() token.Position { return t.Position }

internal/explain/statements.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2332,7 +2332,12 @@ func explainTruncateQuery(sb *strings.Builder, n *ast.TruncateQuery, indent stri
23322332
if hasSettings {
23332333
children++
23342334
}
2335-
fmt.Fprintf(sb, "%sTruncateQuery %s (children %d)\n", indent, n.Table, children)
2335+
// TRUNCATE DATABASE has different spacing than TRUNCATE TABLE
2336+
if n.TruncateDatabase {
2337+
fmt.Fprintf(sb, "%sTruncateQuery %s (children %d)\n", indent, n.Table, children)
2338+
} else {
2339+
fmt.Fprintf(sb, "%sTruncateQuery %s (children %d)\n", indent, n.Table, children)
2340+
}
23362341
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Table)
23372342
}
23382343
if hasSettings {

parser/parser.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6147,8 +6147,12 @@ func (p *Parser) parseTruncate() *ast.TruncateQuery {
61476147
p.nextToken()
61486148
}
61496149

6150+
// Handle TABLE or DATABASE keyword
61506151
if p.currentIs(token.TABLE) {
61516152
p.nextToken()
6153+
} else if p.currentIs(token.DATABASE) {
6154+
trunc.TruncateDatabase = true
6155+
p.nextToken()
61526156
}
61536157

61546158
// Handle IF EXISTS
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt33": true
4-
}
5-
}
1+
{}

0 commit comments

Comments
 (0)