Skip to content

Commit 5effbea

Browse files
committed
Fix FORMAT clause handling in EXPLAIN statement output
When EXPLAIN wraps a SELECT query with FORMAT clause, the FORMAT identifier should be a child of the Explain node, not the inner SelectWithUnionQuery. Updated explainExplainQuery to extract the format from the inner SelectQuery and output it as a sibling. Fixes 17 statements across 13 tests.
1 parent a899fb6 commit 5effbea

File tree

14 files changed

+34
-58
lines changed

14 files changed

+34
-58
lines changed

internal/explain/statements.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,11 +613,30 @@ func explainExplainQuery(sb *strings.Builder, n *ast.ExplainQuery, indent string
613613
}
614614
return
615615
}
616-
// Count children: settings (if present) + statement
616+
617+
// Check if inner statement has FORMAT clause - this should be output as child of Explain
618+
var format *ast.Identifier
619+
if swu, ok := n.Statement.(*ast.SelectWithUnionQuery); ok {
620+
for _, sel := range swu.Selects {
621+
if sq, ok := sel.(*ast.SelectQuery); ok && sq.Format != nil {
622+
format = sq.Format
623+
// Temporarily nil out the format so it's not output by SelectWithUnionQuery
624+
sq.Format = nil
625+
defer func() { sq.Format = format }()
626+
break
627+
}
628+
}
629+
}
630+
631+
// Count children: settings (if present) + statement + format (if present)
617632
children := 1
618633
if n.HasSettings {
619634
children++
620635
}
636+
if format != nil {
637+
children++
638+
}
639+
621640
// At top level (depth 0), ClickHouse outputs "Explain EXPLAIN <TYPE>"
622641
// Nested in subqueries, it outputs "Explain <TYPE>"
623642
if depth == 0 {
@@ -629,6 +648,9 @@ func explainExplainQuery(sb *strings.Builder, n *ast.ExplainQuery, indent string
629648
fmt.Fprintf(sb, "%s Set\n", indent)
630649
}
631650
Node(sb, n.Statement, depth+1)
651+
if format != nil {
652+
fmt.Fprintf(sb, "%s Identifier %s\n", indent, format.Parts[len(format.Parts)-1])
653+
}
632654
}
633655

634656
func explainShowQuery(sb *strings.Builder, n *ast.ShowQuery, indent string) {
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt1": 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-
"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-
"stmt27": true
4-
}
5-
}
1+
{}
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt4": true,
4-
"stmt5": true,
5-
"stmt6": true,
6-
"stmt7": true
7-
}
8-
}
1+
{}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"explain_todo": {
3-
"stmt11": true,
43
"stmt12": true
54
}
65
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"explain_todo":{"stmt4":true}}
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 & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt12": 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-
"stmt11": true
4-
}
5-
}
1+
{}

0 commit comments

Comments
 (0)