Skip to content

Commit 678daab

Browse files
authored
Fix EXPLAIN AST output for single tuple in IN expression (#36)
1 parent e17e520 commit 678daab

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

internal/explain/functions.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,37 @@ func explainInExpr(sb *strings.Builder, n *ast.InExpr, indent string, depth int)
184184
if n.Query != nil {
185185
argCount++
186186
} else {
187-
argCount += len(n.List)
187+
// Check if we have a single tuple literal that should be wrapped in Function tuple
188+
if len(n.List) == 1 {
189+
if lit, ok := n.List[0].(*ast.Literal); ok && lit.Type == ast.LiteralTuple {
190+
// Single tuple literal gets wrapped in Function tuple, so count as 1
191+
argCount++
192+
} else {
193+
argCount += len(n.List)
194+
}
195+
} else {
196+
argCount += len(n.List)
197+
}
188198
}
189199
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, argCount)
190200
Node(sb, n.Expr, depth+2)
191201
if n.Query != nil {
192202
// Subqueries in IN should be wrapped in Subquery node
193203
fmt.Fprintf(sb, "%s Subquery (children %d)\n", indent, 1)
194204
Node(sb, n.Query, depth+3)
205+
} else if len(n.List) == 1 {
206+
// Single element in the list
207+
// If it's a tuple literal, wrap it in Function tuple
208+
// Otherwise, output the element directly
209+
if lit, ok := n.List[0].(*ast.Literal); ok && lit.Type == ast.LiteralTuple {
210+
// Wrap tuple literal in Function tuple
211+
fmt.Fprintf(sb, "%s Function tuple (children %d)\n", indent, 1)
212+
fmt.Fprintf(sb, "%s ExpressionList (children %d)\n", indent, 1)
213+
Node(sb, n.List[0], depth+4)
214+
} else {
215+
// Single non-tuple element - output directly
216+
Node(sb, n.List[0], depth+2)
217+
}
195218
} else {
196219
for _, item := range n.List {
197220
Node(sb, item, depth+2)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"todo": true}
1+
{}

0 commit comments

Comments
 (0)