Skip to content

Commit 28717df

Browse files
kyleconroyclaude
andcommitted
Support RENAME DATABASE statement
Add parsing and explain support for RENAME DATABASE statements. Changes: - Add RenameDatabase field to RenameQuery AST - Update parser to handle RENAME DATABASE syntax - Update explain to output correct format for database renames - Fixes 01155_rename_move_materialized_view stmt44, stmt52 - Also fixes 02096_rename_atomic_hang stmt14 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 99534b0 commit 28717df

File tree

5 files changed

+34
-19
lines changed

5 files changed

+34
-19
lines changed

ast/ast.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -981,13 +981,14 @@ type RenamePair struct {
981981

982982
// RenameQuery represents a RENAME TABLE statement.
983983
type RenameQuery struct {
984-
Position token.Position `json:"-"`
985-
Pairs []*RenamePair `json:"pairs"` // Multiple rename pairs
986-
From string `json:"from,omitempty"` // Deprecated: for backward compat
987-
To string `json:"to,omitempty"` // Deprecated: for backward compat
988-
OnCluster string `json:"on_cluster,omitempty"`
989-
Settings []*SettingExpr `json:"settings,omitempty"`
990-
IfExists bool `json:"if_exists,omitempty"` // IF EXISTS modifier
984+
Position token.Position `json:"-"`
985+
Pairs []*RenamePair `json:"pairs"` // Multiple rename pairs
986+
From string `json:"from,omitempty"` // Deprecated: for backward compat
987+
To string `json:"to,omitempty"` // Deprecated: for backward compat
988+
OnCluster string `json:"on_cluster,omitempty"`
989+
Settings []*SettingExpr `json:"settings,omitempty"`
990+
IfExists bool `json:"if_exists,omitempty"` // IF EXISTS modifier
991+
RenameDatabase bool `json:"rename_database,omitempty"` // True for RENAME DATABASE
991992
}
992993

993994
func (r *RenameQuery) Pos() token.Position { return r.Position }

internal/explain/statements.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,26 @@ func explainRenameQuery(sb *strings.Builder, n *ast.RenameQuery, indent string,
720720
fmt.Fprintf(sb, "%s*ast.RenameQuery\n", indent)
721721
return
722722
}
723+
724+
// Handle RENAME DATABASE separately - it outputs just 2 identifiers
725+
if n.RenameDatabase {
726+
children := 2 // source and target database names
727+
hasSettings := len(n.Settings) > 0
728+
if hasSettings {
729+
children++
730+
}
731+
fmt.Fprintf(sb, "%sRename (children %d)\n", indent, children)
732+
if len(n.Pairs) > 0 {
733+
// FromTable contains source database, ToTable contains target database
734+
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Pairs[0].FromTable)
735+
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Pairs[0].ToTable)
736+
}
737+
if hasSettings {
738+
fmt.Fprintf(sb, "%s Set\n", indent)
739+
}
740+
return
741+
}
742+
723743
// Count identifiers: 2 per pair if no database, 4 per pair if databases specified
724744
hasSettings := len(n.Settings) > 0
725745
children := 0

parser/parser.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6922,11 +6922,14 @@ func (p *Parser) parseRename() *ast.RenameQuery {
69226922

69236923
p.nextToken() // skip RENAME
69246924

6925-
// Handle RENAME TABLE or RENAME DICTIONARY
6925+
// Handle RENAME TABLE, RENAME DICTIONARY, or RENAME DATABASE
69266926
if p.currentIs(token.TABLE) {
69276927
p.nextToken()
69286928
} else if p.currentIs(token.IDENT) && strings.ToUpper(p.current.Value) == "DICTIONARY" {
69296929
p.nextToken()
6930+
} else if p.currentIs(token.DATABASE) {
6931+
p.nextToken()
6932+
rename.RenameDatabase = true
69306933
} else {
69316934
return nil
69326935
}
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt44": true,
4-
"stmt52": true
5-
}
6-
}
1+
{}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt14": true
4-
}
5-
}
1+
{}

0 commit comments

Comments
 (0)