Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sqle/driver/mysql/util/parser_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions sqle/driver/mysql/util/parser_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,35 @@ func testFingerprint(t *testing.T, input, expect string) {
}
assert.Equal(t, expect, actual)
}

// 需求完整 SQL(缩写形态):外层 WHERE rn=1 + 内层 ROW_NUMBER() OVER,涉及三张业务表。
const requirementWindowFuncSQL = `
SELECT *
FROM (
SELECT a.line_code,
a.lasttpoint_passdate,
ROW_NUMBER() OVER (
PARTITION BY a.line_code
ORDER BY a.lasttpoint_passdate DESC
) AS rn
FROM WIP_BILL_WORKORDER a
JOIN mes_trace_productinfo b ON a.id = b.workorder_id
JOIN INT_MDP_02 c ON a.code = c.code
) t
WHERE rn = 1
`

const minimalWindowFuncSQL = `SELECT id, ROW_NUMBER() OVER (PARTITION BY dept ORDER BY salary) AS rn FROM emp`

func TestParseOneSql_WindowFunction(t *testing.T) {
for _, sql := range []string{requirementWindowFuncSQL, minimalWindowFuncSQL} {
stmt, err := ParseOneSql(sql)
assert.NoError(t, err)
assert.NotNil(t, stmt)
_, isSelect := stmt.(*ast.SelectStmt)
assert.True(t, isSelect, "expected SelectStmt, got %T", stmt)
_, isUnparsed := stmt.(*ast.UnparsedStmt)
assert.False(t, isUnparsed)
}
}

Loading