From 316c3a87ee67555abec1d1338ce93321e490d09e Mon Sep 17 00:00:00 2001 From: Lay Patel Date: Thu, 12 Nov 2026 13:24:28 +0530 Subject: [PATCH] Fix nil-pointer panic when ORDER BY ends with AS Signed-off-by: Lay Patel --- parser/parser_table.go | 2 +- parser/parser_test.go | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/parser/parser_table.go b/parser/parser_table.go index f4f6e954..1da62a2d 100644 --- a/parser/parser_table.go +++ b/parser/parser_table.go @@ -1079,7 +1079,7 @@ func (p *Parser) parseOrderExpr(pos Pos) (*OrderExpr, error) { var alias *Ident if p.matchKeyword(KeywordAs) { // It should be a subquery instead of an order by alias if the `AS` is followed by `SELECT` keyword. - if nextToken, err := p.lexer.peekToken(); err == nil && nextToken.ToString() == KeywordSelect { + if nextToken, err := p.lexer.peekToken(); err == nil && nextToken != nil && nextToken.ToString() == KeywordSelect { return &OrderExpr{ OrderPos: pos, Expr: columnExpr, diff --git a/parser/parser_test.go b/parser/parser_test.go index 17ab8e90..a69f4801 100644 --- a/parser/parser_test.go +++ b/parser/parser_test.go @@ -288,6 +288,8 @@ func TestParser_InvalidSyntax(t *testing.T) { // ClickHouse rejects a set operator once SETTINGS is bound to a // parenthesized group "(SELECT 1) SETTINGS max_threads=1 UNION ALL SELECT 2", + // AS at the very end of an ORDER BY item leaves no token to check for SELECT + "SELECT * FROM t ORDER BY a AS", } for _, sql := range invalidSQLs { parser := NewParser(sql)