Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor(advisor/tidb): migrate statement family pair to omni AST (Phase 1.5 §1.5.3 batch 15) (#20320)
Vincent Huang1811b7e193f8
diff --git a/backend/plugin/advisor/tidb/advisor_statement_maximum_limit_value.go b/backend/plugin/advisor/tidb/advisor_statement_maximum_limit_value.goindex a1b588cd76..c58f9d5982 100644--- a/backend/plugin/advisor/tidb/advisor_statement_maximum_limit_value.go+++ b/backend/plugin/advisor/tidb/advisor_statement_maximum_limit_value.go@@ -1,13 +1,10 @@package tidb-// Framework code is generated by the generator.-import ("context""fmt"- "github.com/pingcap/tidb/pkg/parser/ast"- driver "github.com/pingcap/tidb/pkg/types/parser_driver"+ "github.com/bytebase/omni/tidb/ast""github.com/pkg/errors""github.com/bytebase/bytebase/backend/common"@@ -18,21 +15,59 @@ import (var (_ advisor.Advisor = (*StatementMaximumLimitValueAdvisor)(nil)- _ ast.Visitor = (*statementMaximumLimitValueChecker)(nil)+ _ ast.Visitor = (*maxLimitChecker)(nil))func init() {advisor.Register(storepb.Engine_TIDB, storepb.SQLReviewRule_STATEMENT_MAXIMUM_LIMIT_VALUE, &StatementMaximumLimitValueAdvisor{})}-// StatementMaximumLimitValueAdvisor is the advisor checking for LIMIT maximum value.-type StatementMaximumLimitValueAdvisor struct {-}--// Check checks for LIMIT maximum value in SELECT statements.+// StatementMaximumLimitValueAdvisor flags SELECT statements whose LIMIT+// count exceeds the configured maximum.+type StatementMaximumLimitValueAdvisor struct{}++// Check fires on every SelectStmt (top-level and nested) with+// Limit.Count > maximum. Recipe B (ast.Walk) recursion covers:+// subqueries in FROM, WHERE IN clauses, UNION arms, CTEs — matching+// pingcap-tidb's Accept-based traversal at the inner-SelectStmt+// boundaries. Omni's `ast.Walk` recurses into+// SelectStmt.{CTEs, TargetList, From, Where, GroupBy, Having,+// OrderBy, Limit, Left, Right} per walk_generated.go:720.+//+// Cumulative #26 — silent UX improvement at the UNION-root boundary:+// pingcap represents `SELECT ... UNION SELECT ... LIMIT n` (LIMIT+// without parens — attaches to the OUTER UNION result) as+// `*ast.SetOprStmt{Limit: ...}` with the UNION arms as inner+// `*ast.SelectStmt`s with nil Limits. The pre-omni rule's Enter+// matched only `*ast.SelectStmt` so the outer LIMIT lived on a+// concrete type the rule never inspected — silently skipped.+// Omni unifies UNION-root under `*ast.SelectStmt{SetOp: !=None,+// Limit: ...}` (same struct, set-op metadata), so the Walk visits+// the outer SelectStmt and reads the outer-UNION Limit directly.+// Rule now fires on the outer-UNION LIMIT case. Same structural+// shape as cumulative #24 (UNION outer-ORDER-BY on+// insert_disallow_order_by_rand). NOT a regression — pre-omni miss+// was an accidental artifact of pingcap's distinct SetOprStmt type+// being filtered out by the rule's narrow type-assert, not the+// rule's intent.+//+// Scope preservation per invariant #7:+// - Only `Limit.Count` is checked. `Limit.Offset` is NOT (mysql+// analog also checks Offset; tidb-omni preserves the narrower+// pingcap-tidb scope).+// - Non-IntLit Count values (expressions, placeholders if/when+// omni grammar accepts them) are silently skipped — matches the+// pre-omni `_, ok := node.Limit.Count.(*driver.ValueExpr)` cast+// which would also have failed for non-literal counts.+// - Strict-greater (`>`, not `>=`) — preserved.+// - Every advice (including those fired on nested SelectStmts in+// subqueries OR on UNION-root outer Limits) uses the TOP-LEVEL+// statement's first-token line. Pre-omni rule wrote+// `checker.line = stmt.OriginTextPosition()` once per top-level+// and reused it for every advice it emitted during that+// statement's walk.func (*StatementMaximumLimitValueAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {- stmtList, err := getTiDBNodes(checkCtx)-+ stmts, err := getTiDBOmniNodes(checkCtx)if err != nil {return nil, err}@@ -45,52 +80,57 @@ func (*StatementMaximumLimitValueAdvisor) Check(_ context.Context, checkCtx adviif numberPayload == nil {return nil, errors.New("number_payload is required for maximum limit value rule")}-- checker := &statementMaximumLimitValueChecker{- level: level,- title: checkCtx.Rule.Type.String(),- limitMaxValue: int(numberPayload.Number),- }-- for _, stmt := range stmtList {- checker.text = stmt.Text()- checker.line = stmt.OriginTextPosition()- (stmt).Accept(checker)+ maximum := int64(numberPayload.Number)+ title := checkCtx.Rule.Type.String()++ var adviceList []*storepb.Advice+ for _, ostmt := range stmts {+ c := &maxLimitChecker{+ level: level,+ title: title,+ maximum: maximum,+ line: ostmt.FirstTokenLine(),+ }+ ast.Walk(c, ostmt.Node)+ adviceList = append(adviceList, c.advices...)}-- return checker.adviceList, nil+ return adviceList, nil}-type statementMaximumLimitValueChecker struct {- adviceList []*storepb.Advice- level storepb.Advice_Status- title string- text string- line int- limitMaxValue int+type maxLimitChecker struct {+ level storepb.Advice_Status+ title string+ maximum int64+ line int+ advices []*storepb.Advice}-// Enter implements the ast.Visitor interface.-func (checker *statementMaximumLimitValueChecker) Enter(in ast.Node) (ast.Node, bool) {- node, ok := in.(*ast.SelectStmt)- if ok && node.Limit != nil {- if ve, ok := node.Limit.Count.(*driver.ValueExpr); ok {- limitVal := ve.GetInt64()- if limitVal > int64(checker.limitMaxValue) {- checker.adviceList = append(checker.adviceList, &storepb.Advice{- Status: checker.level,- Code: code.StatementExceedMaximumLimitValue.Int32(),- Title: checker.title,- Content: fmt.Sprintf("The limit value %d exceeds the maximum allowed value %d", limitVal, checker.limitMaxValue),- StartPosition: common.ConvertANTLRLineToPosition(checker.line),- })- }- }+// Visit implements ast.Visitor. Returns self to continue recursion;+// `Visit(nil)` is the post-order signal — we handle it with an early+// return at the top of the method.+func (c *maxLimitChecker) Visit(n ast.Node) ast.Visitor {+ if n == nil {+ return c}- return in, false-}--// Leave implements the ast.Visitor interface.-func (*statementMaximumLimitValueChecker) Leave(in ast.Node) (ast.Node, bool) {- return in, true+ sel, ok := n.(*ast.SelectStmt)+ if !ok {+ return c+ }+ if sel.Limit == nil || sel.Limit.Count == nil {+ return c+ }+ lit, ok := sel.Limit.Count.(*ast.IntLit)+ if !ok {+ return c+ }+ if lit.Value > c.maximum {+ c.advices = append(c.advices, &storepb.Advice{+ Status: c.level,+ Code: code.StatementExceedMaximumLimitValue.Int32(),+ Title: c.title,+ Content: fmt.Sprintf("The limit value %d exceeds the maximum allowed value %d", lit.Value, c.maximum),+ StartPosition: common.ConvertANTLRLineToPosition(c.line),+ })+ }+ return c}diff --git a/backend/plugin/advisor/tidb/advisor_statement_merge_alter_table.go b/backend/plugin/advisor/tidb/advisor_statement_merge_alter_table.goindex fd4856bf49..f33a443b8b 100644--- a/backend/plugin/advisor/tidb/advisor_statement_merge_alter_table.go+++ b/backend/plugin/advisor/tidb/advisor_statement_merge_alter_table.go@@ -1,13 +1,11 @@package tidb-// Framework code is generated by the generator.-import ("context""fmt""slices"- "github.com/pingcap/tidb/pkg/parser/ast"+ "github.com/bytebase/omni/tidb/ast""github.com/bytebase/bytebase/backend/common"storepb "github.com/bytebase/bytebase/backend/generated-go/store"@@ -17,21 +15,25 @@ import (var (_ advisor.Advisor = (*StatementMergeAlterTableAdvisor)(nil)- _ ast.Visitor = (*statementMergeAlterTableChecker)(nil))func init() {advisor.Register(storepb.Engine_TIDB, storepb.SQLReviewRule_STATEMENT_MERGE_ALTER_TABLE, &StatementMergeAlterTableAdvisor{})}-// StatementMergeAlterTableAdvisor is the advisor checking for merging ALTER TABLE statements.-type StatementMergeAlterTableAdvisor struct {-}--// Check checks for merging ALTER TABLE statements.+// StatementMergeAlterTableAdvisor flags multiple ALTER TABLE statements+// on the same table that could be merged into one. The pre-omni rule+// accumulated per-table {count, lastLine} across statements, sorted+// tables by lastLine, and emitted one advice per table with count>1.+// Pure aggregator pattern (Recipe A); no sub-walks. Preserves pingcap-+// tidb's "CREATE TABLE counts as 1 modification on that table" framing+// per fixture line 16-30: CREATE on t followed by 1 ALTER on t emits+// the same "2 statements to modify table" advice as 2 ALTERs.+type StatementMergeAlterTableAdvisor struct{}++// Check flags tables modified more than once across the reviewed statements.func (*StatementMergeAlterTableAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {- stmtList, err := getTiDBNodes(checkCtx)-+ stmts, err := getTiDBOmniNodes(checkCtx)if err != nil {return nil, err}@@ -40,94 +42,80 @@ func (*StatementMergeAlterTableAdvisor) Check(_ context.Context, checkCtx advisoif err != nil {return nil, err}- checker := &statementMergeAlterTableChecker{- level: level,- title: checkCtx.Rule.Type.String(),- tableMap: make(map[string]tableStatement),- }+ title := checkCtx.Rule.Type.String()- for _, stmt := range stmtList {- checker.text = stmt.Text()- checker.line = stmt.OriginTextPosition()- (stmt).Accept(checker)+ type tableState struct {+ name string+ count int+ lastLine int}-- return checker.generateAdvice(), nil-}--type statementMergeAlterTableChecker struct {- adviceList []*storepb.Advice- level storepb.Advice_Status- title string- text string- line int- tableMap map[string]tableStatement-}--type tableStatement struct {- name string- count int- lastLine int-}--// Enter implements the ast.Visitor interface.-func (checker *statementMergeAlterTableChecker) Enter(in ast.Node) (ast.Node, bool) {- switch node := in.(type) {- case *ast.CreateTableStmt:- data := tableStatement{- name: node.Table.Name.O,- count: 1,- lastLine: checker.line,+ tableMap := make(map[string]*tableState)+ // touchAlter INCREMENTS the per-table {count, lastLine}. Only ALTER+ // uses this path — CREATE has reset semantics (see below).+ touchAlter := func(name string, line int) {+ entry := tableMap[name]+ if entry == nil {+ entry = &tableState{name: name}+ tableMap[name] = entry}- checker.tableMap[node.Table.Name.O] = data- case *ast.AlterTableStmt:- data, ok := checker.tableMap[node.Table.Name.O]- if !ok {- data = tableStatement{- name: node.Table.Name.O,- count: 0,+ entry.count+++ entry.lastLine = line+ }++ for _, ostmt := range stmts {+ switch n := ostmt.Node.(type) {+ case *ast.CreateTableStmt:+ // Cumulative #25: CREATE TABLE RESETS the per-table state to+ // {count: 1, lastLine}, mirroring pre-omni semantics. A second+ // CREATE on the same name (e.g. after a DROP) starts a fresh+ // window of modifications rather than carrying over the prior+ // count — otherwise `CREATE t; ALTER t; CREATE t; ALTER t`+ // would report "4 statements" instead of "2", merging+ // modifications across table incarnations that cannot+ // actually be merged. The pre-omni rule wrote the map entry+ // unconditionally; mechanical port via a single touch()+ // helper loses the reset semantic.+ if n.Table != nil {+ tableMap[n.Table.Name] = &tableState{+ name: n.Table.Name,+ count: 1,+ lastLine: ostmt.FirstTokenLine(),+ }}+ case *ast.AlterTableStmt:+ if n.Table != nil {+ touchAlter(n.Table.Name, ostmt.FirstTokenLine())+ }+ default:}- data.count++- data.lastLine = checker.line- checker.tableMap[node.Table.Name.O] = data- default:}- return in, false-}--// Leave implements the ast.Visitor interface.-func (*statementMergeAlterTableChecker) Leave(in ast.Node) (ast.Node, bool) {- return in, true-}--func (checker *statementMergeAlterTableChecker) generateAdvice() []*storepb.Advice {- var tableList []tableStatement- for _, table := range checker.tableMap {- tableList = append(tableList, table)+ tableList := make([]*tableState, 0, len(tableMap))+ for _, t := range tableMap {+ tableList = append(tableList, t)}- slices.SortFunc(tableList, func(i, j tableStatement) int {- if i.lastLine < j.lastLine {+ slices.SortFunc(tableList, func(i, j *tableState) int {+ switch {+ case i.lastLine < j.lastLine:return -1- }- if i.lastLine > j.lastLine {+ case i.lastLine > j.lastLine:return 1+ default:+ return 0}- return 0})- for _, table := range tableList {- if table.count > 1 {- checker.adviceList = append(checker.adviceList, &storepb.Advice{- Status: checker.level,+ var adviceList []*storepb.Advice+ for _, t := range tableList {+ if t.count > 1 {+ adviceList = append(adviceList, &storepb.Advice{+ Status: level,Code: code.StatementRedundantAlterTable.Int32(),- Title: checker.title,- Content: fmt.Sprintf("There are %d statements to modify table `%s`", table.count, table.name),- StartPosition: common.ConvertANTLRLineToPosition(table.lastLine),+ Title: title,+ Content: fmt.Sprintf("There are %d statements to modify table `%s`", t.count, t.name),+ StartPosition: common.ConvertANTLRLineToPosition(t.lastLine),})}}-- return checker.adviceList+ return adviceList, nil}diff --git a/backend/plugin/advisor/tidb/test/statement_maximum_limit_value.yaml b/backend/plugin/advisor/tidb/test/statement_maximum_limit_value.yamlindex ec1bc9b686..a2cc65c97e 100644--- a/backend/plugin/advisor/tidb/test/statement_maximum_limit_value.yaml+++ b/backend/plugin/advisor/tidb/test/statement_maximum_limit_value.yaml@@ -10,3 +10,99 @@line: 1column: 0endposition: null+# Batch 15: nested-subquery in FROM — exercises ast.Walk recursion into+# SelectStmt.From. Pre-omni pingcap's Accept(checker) auto-recursed into+# subqueries via the same path; omni's Walk recurses via walk_generated+# (verified empirically). Both the outer SELECT and the inner subquery+# get visited; the inner has LIMIT 10000 > max=1000 → fires.+- statement: SELECT * FROM (SELECT * FROM employee LIMIT 10000) AS sub+ changeType: 1+ want:+ - status: 2+ code: 222+ title: STATEMENT_MAXIMUM_LIMIT_VALUE+ content: The limit value 10000 exceeds the maximum allowed value 1000+ startposition:+ line: 1+ column: 0+ endposition: null+# Batch 15: UNION-arm LIMIT — exercises ast.Walk recursion into+# SelectStmt.Left / SelectStmt.Right (the UNION arms). Pingcap-tidb+# Accept-traversal visited both arms; omni Walk does likewise. The left+# arm has LIMIT 10000 → fires.+- statement: SELECT a FROM employee LIMIT 10000 UNION SELECT b FROM employee+ changeType: 1+ want:+ - status: 2+ code: 222+ title: STATEMENT_MAXIMUM_LIMIT_VALUE+ content: The limit value 10000 exceeds the maximum allowed value 1000+ startposition:+ line: 1+ column: 0+ endposition: null+# Batch 15: WHERE-IN subquery — exercises Walk recursion into WHERE+# expressions and their nested SelectStmts. Pingcap Accept covered this+# via interface dispatch on Expr.Accept; omni Walk does via expression-+# tree recursion.+- statement: SELECT * FROM employee WHERE id IN (SELECT id FROM employee LIMIT 10000)+ changeType: 1+ want:+ - status: 2+ code: 222+ title: STATEMENT_MAXIMUM_LIMIT_VALUE+ content: The limit value 10000 exceeds the maximum allowed value 1000+ startposition:+ line: 1+ column: 0+ endposition: null+# Batch 15: strict-greater pin — LIMIT exactly at maximum (1000) does+# NOT fire. Preserves pingcap-tidb's `limitVal > int64(...)` strict+# inequality. Without this pin, a future refactor to `>=` would silently+# break customer workflows that set the limit AT the threshold.+- statement: SELECT * FROM employee LIMIT 1000+ changeType: 1+# Batch 15: no LIMIT — does NOT fire (baseline negative).+- statement: SELECT * FROM employee+ changeType: 1+# Batch 15: LIMIT offset, count form — pingcap-tidb's pre-omni rule+# accessed `node.Limit.Count` which for `LIMIT 50, 10000` is the COUNT+# (10000), NOT the offset (50). Omni's `Limit.Count` field is also the+# count (verified empirically: parse `LIMIT 50, 10000` → Count IntLit+# Value=10000). Rule fires on count=10000 > 1000.+- statement: SELECT * FROM employee LIMIT 50, 10000+ changeType: 1+ want:+ - status: 2+ code: 222+ title: STATEMENT_MAXIMUM_LIMIT_VALUE+ content: The limit value 10000 exceeds the maximum allowed value 1000+ startposition:+ line: 1+ column: 0+ endposition: null+# Cumulative #26 pin (batch 15, Codex P2 catch pre-merge): pre-omni+# pingcap represented `SELECT ... UNION SELECT ... LIMIT N` as+# `*ast.SetOprStmt{Limit: ...}` — distinct concrete type with the+# outer LIMIT on the SetOprStmt itself; inner SelectStmt arms have+# nil Limit. The pre-omni rule's Enter matched only `*ast.SelectStmt`+# so this outer LIMIT was silently skipped — pre-omni did NOT fire+# despite the rule's stated intent ("limit value exceeds maximum").+# Omni unifies UNION-root under `*ast.SelectStmt{SetOp:Union, Limit:+# ...}` (same struct, set-op metadata); ast.Walk visits the outer+# SelectStmt and reads the outer-UNION Limit. Rule now fires —+# silent UX improvement matching rule intent. Same structural shape+# as cumulative #24 (UNION outer-ORDER-BY on+# insert_disallow_order_by_rand) — second occurrence of UNION-root+# unification as a divergence axis across consecutive batches (14/15).+- statement: SELECT * FROM employee UNION SELECT * FROM employee LIMIT 1001+ changeType: 1+ want:+ - status: 2+ code: 222+ title: STATEMENT_MAXIMUM_LIMIT_VALUE+ content: The limit value 1001 exceeds the maximum allowed value 1000+ startposition:+ line: 1+ column: 0+ endposition: nulldiff --git a/backend/plugin/advisor/tidb/test/statement_merge_alter_table.yaml b/backend/plugin/advisor/tidb/test/statement_merge_alter_table.yamlindex bb7394e8f8..52cd7628fc 100644--- a/backend/plugin/advisor/tidb/test/statement_merge_alter_table.yaml+++ b/backend/plugin/advisor/tidb/test/statement_merge_alter_table.yaml@@ -59,3 +59,49 @@line: 4column: 0endposition: null+# Batch 15: count=3 pin — verifies the count grows past 2. Three+# touches on the same table emit "There are 3 statements" not just "2".+# Lock the count-accuracy contract.+- statement: |-+ ALTER TABLE tech_book ADD COLUMN a int;+ ALTER TABLE tech_book ADD COLUMN b int;+ ALTER TABLE tech_book ADD COLUMN c int;+ changeType: 1+ want:+ - status: 2+ code: 207+ title: STATEMENT_MERGE_ALTER_TABLE+ content: There are 3 statements to modify table `tech_book`+ startposition:+ line: 3+ column: 0+ endposition: null+# Batch 15: per-table isolation pin — two ALTERs on DIFFERENT tables+# do NOT trigger. Each table's count is 1 individually, neither > 1.+# Pins the per-table-keyed counting (vs accidental cross-table count).+- statement: |-+ ALTER TABLE a ADD COLUMN x int;+ ALTER TABLE b ADD COLUMN y int;+ changeType: 1+# Cumulative #25 pin (batch 15, Codex P2 catch pre-merge): CREATE TABLE+# RESETS the per-table state to {count: 1, lastLine}, mirroring pre-omni+# semantics. A second CREATE on the same name starts a fresh window of+# modifications rather than carrying over the prior count. Initial+# batch 15 migration unified CREATE + ALTER behind a single touch()+# helper that always incremented — would have reported "4 statements+# to modify table `t`" for this input instead of "2".+- statement: |-+ CREATE TABLE t(a int);+ ALTER TABLE t ADD COLUMN a int;+ CREATE TABLE t(b int);+ ALTER TABLE t ADD COLUMN c int;+ changeType: 1+ want:+ - status: 2+ code: 207+ title: STATEMENT_MERGE_ALTER_TABLE+ content: There are 2 statements to modify table `t`+ startposition:+ line: 4+ column: 0+ endposition: null