Skip to content

Commit 34c8a4c

Browse files
authored
Fix parser tests with various EXPLAIN AST formatting improvements (#42)
1 parent e0cd9ed commit 34c8a4c

File tree

25 files changed

+465
-61
lines changed

25 files changed

+465
-61
lines changed

internal/explain/expressions.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,9 @@ func explainAliasedExpr(sb *strings.Builder, n *ast.AliasedExpr, depth int) {
382382
case *ast.TupleAccess:
383383
// Tuple access - ClickHouse doesn't show aliases on tupleElement in EXPLAIN AST
384384
explainTupleAccess(sb, e, indent, depth)
385+
case *ast.InExpr:
386+
// IN expressions with alias
387+
explainInExprWithAlias(sb, e, n.Alias, indent, depth)
385388
default:
386389
// For other types, recursively explain and add alias info
387390
Node(sb, n.Expr, depth)

internal/explain/format.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,28 @@ func formatArrayLiteral(val interface{}) string {
136136
return fmt.Sprintf("Array_[%s]", strings.Join(parts, ", "))
137137
}
138138

139+
// formatNumericExpr formats a numeric expression (literal or unary minus of literal)
140+
func formatNumericExpr(e ast.Expression) (string, bool) {
141+
if lit, ok := e.(*ast.Literal); ok {
142+
if lit.Type == ast.LiteralInteger || lit.Type == ast.LiteralFloat {
143+
return FormatLiteral(lit), true
144+
}
145+
}
146+
if unary, ok := e.(*ast.UnaryExpr); ok && unary.Op == "-" {
147+
if lit, ok := unary.Operand.(*ast.Literal); ok {
148+
switch val := lit.Value.(type) {
149+
case int64:
150+
return fmt.Sprintf("Int64_%d", -val), true
151+
case uint64:
152+
return fmt.Sprintf("Int64_%d", -int64(val)), true
153+
case float64:
154+
return fmt.Sprintf("Float64_%s", FormatFloat(-val)), true
155+
}
156+
}
157+
}
158+
return "", false
159+
}
160+
139161
// formatTupleLiteral formats a tuple literal for EXPLAIN AST output
140162
func formatTupleLiteral(val interface{}) string {
141163
exprs, ok := val.([]ast.Expression)
@@ -144,7 +166,9 @@ func formatTupleLiteral(val interface{}) string {
144166
}
145167
var parts []string
146168
for _, e := range exprs {
147-
if lit, ok := e.(*ast.Literal); ok {
169+
if formatted, ok := formatNumericExpr(e); ok {
170+
parts = append(parts, formatted)
171+
} else if lit, ok := e.(*ast.Literal); ok {
148172
parts = append(parts, FormatLiteral(lit))
149173
} else if ident, ok := e.(*ast.Identifier); ok {
150174
parts = append(parts, ident.Name())
@@ -159,7 +183,9 @@ func formatTupleLiteral(val interface{}) string {
159183
func formatInListAsTuple(list []ast.Expression) string {
160184
var parts []string
161185
for _, e := range list {
162-
if lit, ok := e.(*ast.Literal); ok {
186+
if formatted, ok := formatNumericExpr(e); ok {
187+
parts = append(parts, formatted)
188+
} else if lit, ok := e.(*ast.Literal); ok {
163189
parts = append(parts, FormatLiteral(lit))
164190
} else if ident, ok := e.(*ast.Identifier); ok {
165191
parts = append(parts, ident.Name())

0 commit comments

Comments
 (0)