From a933153c17f1252fe1d6b2215a49b290cf4bd23d Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 30 Dec 2025 00:30:45 +0000 Subject: [PATCH] Fix negated integer literals with aliases in EXPLAIN output Remove the subquery context restriction when outputting negated integer literals with aliases. Previously, `-5 as offset` was incorrectly rendered as `Function negate (alias offset)` in regular SELECT statements, but should be rendered as `Literal Int64_-5 (alias offset)`. This fixes several failing tests in the 02154_bit_slice_for_string and 02154_bit_slice_for_fixedstring test suites. --- internal/explain/expressions.go | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/internal/explain/expressions.go b/internal/explain/expressions.go index 74604df541..1d7caf35d3 100644 --- a/internal/explain/expressions.go +++ b/internal/explain/expressions.go @@ -418,22 +418,19 @@ func explainAliasedExpr(sb *strings.Builder, n *ast.AliasedExpr, depth int) { } case *ast.UnaryExpr: // Handle negated numeric literals - output as Literal instead of Function negate - // For integers, only do this in subquery context (ClickHouse behavior) - // For floats (especially inf/nan), always do this + // When an aliased expression is a negated literal, output as negative Literal if e.Op == "-" { if lit, ok := e.Operand.(*ast.Literal); ok { switch lit.Type { case ast.LiteralInteger: - // Only convert to literal in subquery context - if inSubqueryContext { - switch val := lit.Value.(type) { - case int64: - fmt.Fprintf(sb, "%sLiteral Int64_%d (alias %s)\n", indent, -val, escapeAlias(n.Alias)) - return - case uint64: - fmt.Fprintf(sb, "%sLiteral Int64_-%d (alias %s)\n", indent, val, escapeAlias(n.Alias)) - return - } + // Convert negated integer to negative literal + switch val := lit.Value.(type) { + case int64: + fmt.Fprintf(sb, "%sLiteral Int64_%d (alias %s)\n", indent, -val, escapeAlias(n.Alias)) + return + case uint64: + fmt.Fprintf(sb, "%sLiteral Int64_-%d (alias %s)\n", indent, val, escapeAlias(n.Alias)) + return } case ast.LiteralFloat: // Always convert negated floats to literals (especially for -inf, -nan)