Skip to content

Commit 0beceeb

Browse files
authored
Add database-qualified name support in EXPLAIN AST output (#103)
1 parent f2b49f7 commit 0beceeb

File tree

2 files changed

+39
-51
lines changed

2 files changed

+39
-51
lines changed

internal/explain/statements.go

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ func explainInsertQuery(sb *strings.Builder, n *ast.InsertQuery, indent string,
2020
children++
2121
} else if n.Table != "" {
2222
children++ // Table identifier
23+
if n.Database != "" {
24+
children++ // Database identifier (separate from table)
25+
}
2326
}
2427
if len(n.Columns) > 0 {
2528
children++ // Column list
@@ -45,11 +48,13 @@ func explainInsertQuery(sb *strings.Builder, n *ast.InsertQuery, indent string,
4548
if n.Function != nil {
4649
Node(sb, n.Function, depth+1)
4750
} else if n.Table != "" {
48-
name := n.Table
4951
if n.Database != "" {
50-
name = n.Database + "." + n.Table
52+
// Database-qualified: output separate identifiers
53+
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Database)
54+
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Table)
55+
} else {
56+
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Table)
5157
}
52-
fmt.Fprintf(sb, "%s Identifier %s\n", indent, name)
5358
}
5459

5560
// Column list
@@ -125,8 +130,13 @@ func explainCreateQuery(sb *strings.Builder, n *ast.CreateQuery, indent string,
125130
if n.CreateDatabase {
126131
name = n.Database
127132
}
133+
// Check for database-qualified table/view name
134+
hasDatabase := n.Database != "" && !n.CreateDatabase && (n.Table != "" || n.View != "")
128135
// Count children: name + columns + engine/storage
129136
children := 1 // name identifier
137+
if hasDatabase {
138+
children++ // additional identifier for database
139+
}
130140
if len(n.Columns) > 0 || len(n.Indexes) > 0 || len(n.Constraints) > 0 {
131141
children++
132142
}
@@ -142,10 +152,16 @@ func explainCreateQuery(sb *strings.Builder, n *ast.CreateQuery, indent string,
142152
// ClickHouse adds an extra space before (children N) for CREATE DATABASE
143153
if n.CreateDatabase {
144154
fmt.Fprintf(sb, "%sCreateQuery %s (children %d)\n", indent, name, children)
155+
fmt.Fprintf(sb, "%s Identifier %s\n", indent, name)
156+
} else if hasDatabase {
157+
// Database-qualified: CreateQuery db table (children N)
158+
fmt.Fprintf(sb, "%sCreateQuery %s %s (children %d)\n", indent, n.Database, name, children)
159+
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Database)
160+
fmt.Fprintf(sb, "%s Identifier %s\n", indent, name)
145161
} else {
146162
fmt.Fprintf(sb, "%sCreateQuery %s (children %d)\n", indent, name, children)
163+
fmt.Fprintf(sb, "%s Identifier %s\n", indent, name)
147164
}
148-
fmt.Fprintf(sb, "%s Identifier %s\n", indent, name)
149165
if len(n.Columns) > 0 || len(n.Indexes) > 0 || len(n.Constraints) > 0 {
150166
childrenCount := 0
151167
if len(n.Columns) > 0 {
@@ -690,6 +706,15 @@ func explainParameter(sb *strings.Builder, n *ast.Parameter, indent string) {
690706
}
691707

692708
func explainDetachQuery(sb *strings.Builder, n *ast.DetachQuery, indent string) {
709+
// Check for database-qualified table name
710+
if n.Database != "" && n.Table != "" {
711+
// Database-qualified: DetachQuery db table (children 2)
712+
fmt.Fprintf(sb, "%sDetachQuery %s %s (children 2)\n", indent, n.Database, n.Table)
713+
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Database)
714+
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Table)
715+
return
716+
}
717+
// Single name (table only or database only for DETACH DATABASE)
693718
name := n.Table
694719
if name == "" {
695720
name = n.Database
@@ -703,6 +728,15 @@ func explainDetachQuery(sb *strings.Builder, n *ast.DetachQuery, indent string)
703728
}
704729

705730
func explainAttachQuery(sb *strings.Builder, n *ast.AttachQuery, indent string) {
731+
// Check for database-qualified table name
732+
if n.Database != "" && n.Table != "" {
733+
// Database-qualified: AttachQuery db table (children 2)
734+
fmt.Fprintf(sb, "%sAttachQuery %s %s (children 2)\n", indent, n.Database, n.Table)
735+
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Database)
736+
fmt.Fprintf(sb, "%s Identifier %s\n", indent, n.Table)
737+
return
738+
}
739+
// Single name (table only or database only for ATTACH DATABASE)
706740
name := n.Table
707741
if name == "" {
708742
name = n.Database
Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,23 @@
11
{
22
"explain_todo": {
3-
"stmt100": true,
4-
"stmt102": true,
5-
"stmt104": true,
6-
"stmt105": true,
7-
"stmt108": true,
8-
"stmt11": true,
93
"stmt112": true,
104
"stmt114": true,
11-
"stmt115": true,
12-
"stmt117": true,
13-
"stmt118": true,
14-
"stmt12": true,
15-
"stmt120": true,
165
"stmt121": true,
176
"stmt122": true,
18-
"stmt125": true,
197
"stmt126": true,
208
"stmt127": true,
21-
"stmt130": true,
22-
"stmt132": true,
23-
"stmt134": true,
24-
"stmt137": true,
25-
"stmt15": true,
269
"stmt18": true,
2710
"stmt20": true,
2811
"stmt22": true,
29-
"stmt24": true,
30-
"stmt26": true,
31-
"stmt28": true,
3212
"stmt29": true,
3313
"stmt30": true,
34-
"stmt34": true,
3514
"stmt35": true,
3615
"stmt36": true,
37-
"stmt39": true,
38-
"stmt41": true,
39-
"stmt43": true,
40-
"stmt5": true,
41-
"stmt51": true,
42-
"stmt52": true,
43-
"stmt53": true,
44-
"stmt55": true,
45-
"stmt57": true,
46-
"stmt58": true,
47-
"stmt6": true,
48-
"stmt61": true,
4916
"stmt65": true,
5017
"stmt67": true,
51-
"stmt68": true,
52-
"stmt7": true,
53-
"stmt70": true,
54-
"stmt71": true,
55-
"stmt73": true,
5618
"stmt74": true,
5719
"stmt75": true,
58-
"stmt78": true,
5920
"stmt79": true,
60-
"stmt80": true,
61-
"stmt83": true,
62-
"stmt85": true,
63-
"stmt87": true,
64-
"stmt9": true,
65-
"stmt90": true,
66-
"stmt98": true,
67-
"stmt99": true
21+
"stmt80": true
6822
}
6923
}

0 commit comments

Comments
 (0)