Skip to content

Commit f0aac10

Browse files
committed
Fix database-qualified dictionary names in DETACH/ATTACH statements
The parser was not handling database-qualified dictionary names like `db.dict` in DETACH DICTIONARY and ATTACH DICTIONARY statements. Parser changes: - parseDetach: Allow qualified names for DICTIONARY (database.dict) - parseAttach: Allow qualified names for DICTIONARY (database.dict) Explain changes: - explainDetachQuery: Handle Database + Dictionary case - explainAttachQuery: Handle Database + Dictionary case Fixes tests: - 01110_dictionary_layout_without_arguments (stmt7, stmt8) - 01575_disable_detach_table_of_dictionary (stmt7, stmt9) - 01018_ddl_dictionaries_create (stmt17, stmt22)
1 parent e61ed87 commit f0aac10

5 files changed

Lines changed: 34 additions & 27 deletions

File tree

internal/explain/statements.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,8 +1255,16 @@ func explainDetachQuery(sb *strings.Builder, n *ast.DetachQuery, indent string)
12551255
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Table)
12561256
return
12571257
}
1258+
// Check for database-qualified dictionary name
1259+
if n.Database != "" && n.Dictionary != "" {
1260+
// Database-qualified: DetachQuery db dict (children 2)
1261+
fmt.Fprintf(sb, "%sDetachQuery %s %s (children 2)\n", indent, n.Database, n.Dictionary)
1262+
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Database)
1263+
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Dictionary)
1264+
return
1265+
}
12581266
// DETACH DATABASE db: Database set, Table empty -> "DetachQuery db (children 1)"
1259-
if n.Database != "" && n.Table == "" {
1267+
if n.Database != "" && n.Table == "" && n.Dictionary == "" {
12601268
fmt.Fprintf(sb, "%sDetachQuery %s (children 1)\n", indent, n.Database)
12611269
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Database)
12621270
return
@@ -1280,7 +1288,7 @@ func explainDetachQuery(sb *strings.Builder, n *ast.DetachQuery, indent string)
12801288
func explainAttachQuery(sb *strings.Builder, n *ast.AttachQuery, indent string, depth int) {
12811289
// Count children: identifier + columns definition (if any) + select query (if any) + storage/view targets (if any)
12821290
children := 1 // table/database identifier
1283-
if n.Database != "" && n.Table != "" {
1291+
if n.Database != "" && (n.Table != "" || n.Dictionary != "") {
12841292
children++ // extra identifier for database
12851293
}
12861294
hasColumns := len(n.Columns) > 0 || len(n.ColumnsPrimaryKey) > 0 || len(n.Indexes) > 0
@@ -1301,7 +1309,13 @@ func explainAttachQuery(sb *strings.Builder, n *ast.AttachQuery, indent string,
13011309
fmt.Fprintf(sb, "%sAttachQuery %s %s (children %d)\n", indent, n.Database, n.Table, children)
13021310
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Database)
13031311
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Table)
1304-
} else if n.Database != "" && n.Table == "" {
1312+
} else if n.Database != "" && n.Dictionary != "" {
1313+
// Database-qualified dictionary: ATTACH DICTIONARY db.dict
1314+
fmt.Fprintf(sb, "%sAttachQuery %s %s (children %d)\n", indent, n.Database, n.Dictionary, children)
1315+
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Database)
1316+
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Dictionary)
1317+
return // Dictionary doesn't have columns or storage
1318+
} else if n.Database != "" && n.Table == "" && n.Dictionary == "" {
13051319
fmt.Fprintf(sb, "%sAttachQuery %s (children %d)\n", indent, n.Database, children)
13061320
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Database)
13071321
} else if n.Table != "" {

parser/parser.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6990,12 +6990,16 @@ func (p *Parser) parseDetach() *ast.DetachQuery {
69906990
p.nextToken()
69916991
}
69926992

6993-
// Parse name (can be qualified: database.table for TABLE, not for DATABASE/DICTIONARY)
6993+
// Parse name (can be qualified: database.table for TABLE, database.dict for DICTIONARY)
69946994
name := p.parseIdentifierName()
6995-
if p.currentIs(token.DOT) && !isDatabase && !isDictionary {
6995+
if p.currentIs(token.DOT) && !isDatabase {
69966996
p.nextToken()
69976997
detach.Database = name
6998-
detach.Table = p.parseIdentifierName()
6998+
if isDictionary {
6999+
detach.Dictionary = p.parseIdentifierName()
7000+
} else {
7001+
detach.Table = p.parseIdentifierName()
7002+
}
69997003
} else if isDatabase {
70007004
detach.Database = name
70017005
} else if isDictionary {
@@ -7047,12 +7051,16 @@ func (p *Parser) parseAttach() *ast.AttachQuery {
70477051
}
70487052
}
70497053

7050-
// Parse name (can be qualified: database.table for TABLE, not for DATABASE/DICTIONARY)
7054+
// Parse name (can be qualified: database.table for TABLE, database.dict for DICTIONARY)
70517055
name := p.parseIdentifierName()
7052-
if p.currentIs(token.DOT) && !isDatabase && !isDictionary {
7056+
if p.currentIs(token.DOT) && !isDatabase {
70537057
p.nextToken()
70547058
attach.Database = name
7055-
attach.Table = p.parseIdentifierName()
7059+
if isDictionary {
7060+
attach.Dictionary = p.parseIdentifierName()
7061+
} else {
7062+
attach.Table = p.parseIdentifierName()
7063+
}
70567064
} else if isDatabase {
70577065
attach.Database = name
70587066
} else if isDictionary {
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt17": true,
4-
"stmt22": true
5-
}
6-
}
1+
{}
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt7": true,
4-
"stmt8": true
5-
}
6-
}
1+
{}
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt7": true,
4-
"stmt9": true
5-
}
6-
}
1+
{}

0 commit comments

Comments
 (0)