From 39200d08bfe80d92d48850d17665913520c62b56 Mon Sep 17 00:00:00 2001 From: hulk Date: Thu, 26 Nov 2026 10:29:42 +0800 Subject: [PATCH] Fix source span for bare EXCEPT column modifiers ## Problem `FunctionExpr.End()` always advances past the closing parenthesis. For a bare `EXCEPT` modifier, the parser stores the column's already exclusive end in that position, so the span includes one extra byte. ## Reproduction ```sql SELECT t.* EXCEPT b, c FROM t ``` The modifier's source slice is `EXCEPT b,` instead of `EXCEPT b`. ## Fix Use the column's exclusive end for bare modifiers. Parenthesized modifiers still include the closing parenthesis, and SQL formatting is unchanged. ## Test `TestSourceSpansCoverReportedNodes` checks both modifier forms. Follow-up to #349. Assistant-By OpenAI Codex --- parser/ast_expr.go | 8 +++++++- parser/position_test.go | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/parser/ast_expr.go b/parser/ast_expr.go index 8b736321..a9dc367c 100644 --- a/parser/ast_expr.go +++ b/parser/ast_expr.go @@ -215,7 +215,13 @@ func (f *FunctionExpr) Pos() Pos { } func (f *FunctionExpr) End() Pos { - return f.Params.RightParenPos + 1 + params := f.Params + // Bare EXCEPT col uses the column's bounds as its synthetic paren + // positions, so the right position is already exclusive. + if items := params.Items; items != nil && params.LeftParenPos == items.Pos() && params.RightParenPos == items.End() { + return params.RightParenPos + } + return params.RightParenPos + 1 } func (f *FunctionExpr) Accept(visitor ASTVisitor) error { diff --git a/parser/position_test.go b/parser/position_test.go index 62899756..4200bd5c 100644 --- a/parser/position_test.go +++ b/parser/position_test.go @@ -135,6 +135,8 @@ func TestSourceSpansCoverReportedNodes(t *testing.T) { {"SELECT ?", "?", func(e Expr) Expr { return e.(*SelectQuery).SelectItems[0].Expr }}, {"SELECT 'abc'", "'abc'", func(e Expr) Expr { return e.(*SelectQuery).SelectItems[0].Expr }}, {"SELECT f(1)", "f(1)", func(e Expr) Expr { return e.(*SelectQuery).SelectItems[0].Expr }}, + {"SELECT t.* EXCEPT b, c FROM t", "EXCEPT b", func(e Expr) Expr { return e.(*SelectQuery).SelectItems[0].Modifiers[0] }}, + {"SELECT t.* EXCEPT (b), c FROM t", "EXCEPT (b)", func(e Expr) Expr { return e.(*SelectQuery).SelectItems[0].Modifiers[0] }}, } { t.Run(tc.sql, func(t *testing.T) { stmt := parseOneStmt(t, tc.sql)