Skip to content

Commit 0d45aba

Browse files
committed
Add implicit NULL in CASE WHEN without ELSE clause
CASE expressions without an explicit ELSE clause implicitly return NULL. Update the explain output to always include this implicit NULL value, matching ClickHouse's EXPLAIN AST format. Fixes 4 statements in 4 tests.
1 parent 7baa9ea commit 0d45aba

5 files changed

Lines changed: 9 additions & 20 deletions

File tree

internal/explain/functions.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -836,10 +836,8 @@ func explainCaseExprWithAlias(sb *strings.Builder, n *ast.CaseExpr, alias string
836836
}
837837
} else {
838838
// CASE WHEN ... form
839-
argCount := len(n.Whens) * 2
840-
if n.Else != nil {
841-
argCount++
842-
}
839+
// CASE without ELSE implicitly has NULL as the else value
840+
argCount := len(n.Whens)*2 + 1 // Always add 1 for ELSE (explicit or implicit NULL)
843841
if alias != "" {
844842
fmt.Fprintf(sb, "%sFunction multiIf (alias %s) (children %d)\n", indent, alias, 1)
845843
} else {
@@ -852,6 +850,9 @@ func explainCaseExprWithAlias(sb *strings.Builder, n *ast.CaseExpr, alias string
852850
}
853851
if n.Else != nil {
854852
Node(sb, n.Else, depth+2)
853+
} else {
854+
// Implicit NULL when no ELSE clause
855+
fmt.Fprintf(sb, "%s Literal NULL\n", indent)
855856
}
856857
}
857858
}
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt80": true
4-
}
5-
}
1+
{}
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 & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
{
2-
"explain_todo": {
3-
"stmt319": 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-
"stmt2": true
4-
}
5-
}
1+
{}

0 commit comments

Comments
 (0)