From 644c992ab641a42069dbbd2ec794fe160cd507b8 Mon Sep 17 00:00:00 2001 From: actiontech-zihan Date: Wed, 26 Aug 2026 12:34:51 +0800 Subject: [PATCH] fix(mysql): enable window function parsing in ParseOneSql Align SQL analysis entry with audit splitter by enabling EnableWindowFunc(true) in ParseOneSql, fixing parser failures for ROW_NUMBER() OVER (PARTITION BY ...) SQL. Add TestParseOneSqlWindowFunc to verify AST contains WindowFuncExpr. Fixes actiontech/sqle-ee#3110 Co-authored-by: Cursor --- sqle/driver/mysql/util/parser_helper.go | 1 + sqle/driver/mysql/util/parser_helper_test.go | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/sqle/driver/mysql/util/parser_helper.go b/sqle/driver/mysql/util/parser_helper.go index 42336b7522..74af2f3f29 100644 --- a/sqle/driver/mysql/util/parser_helper.go +++ b/sqle/driver/mysql/util/parser_helper.go @@ -29,6 +29,7 @@ func ParseSql(sql string) ([]ast.StmtNode, error) { func ParseOneSql(sql string) (ast.StmtNode, error) { p := parser.New() + p.EnableWindowFunc(true) stmt, err := p.ParseOneStmt(sql, "", "") if err != nil { fmt.Printf("parse error: %v\nsql: %v", err, sql) diff --git a/sqle/driver/mysql/util/parser_helper_test.go b/sqle/driver/mysql/util/parser_helper_test.go index d28bc66fe0..da15c9d340 100644 --- a/sqle/driver/mysql/util/parser_helper_test.go +++ b/sqle/driver/mysql/util/parser_helper_test.go @@ -217,3 +217,23 @@ func testFingerprint(t *testing.T, input, expect string) { } assert.Equal(t, expect, actual) } + +func TestParseOneSqlWindowFunc(t *testing.T) { + const sql = `SELECT id, ROW_NUMBER() OVER (PARTITION BY dept ORDER BY salary DESC) AS rn FROM emp;` + stmt, err := ParseOneSql(sql) + assert.NoError(t, err) + + selectStmt, ok := stmt.(*ast.SelectStmt) + assert.True(t, ok) + assert.NotNil(t, selectStmt.Fields) + + foundWindow := false + for _, field := range selectStmt.Fields.Fields { + if _, ok := field.Expr.(*ast.WindowFuncExpr); ok { + foundWindow = true + break + } + } + assert.True(t, foundWindow, "expected AST to contain WindowFuncExpr") +} +