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 4 STATEMENT_DISALLOW_* rules to omni AST (Phase 1.5 §1.5.3 batch 3) (#20199)
Vincent Huangbb0e73e1d7e1
diff --git a/backend/plugin/advisor/tidb/advisor_stmt_disallow_commit.go b/backend/plugin/advisor/tidb/advisor_stmt_disallow_commit.goindex fced3237b7..08bbef2af9 100644--- a/backend/plugin/advisor/tidb/advisor_stmt_disallow_commit.go+++ b/backend/plugin/advisor/tidb/advisor_stmt_disallow_commit.go@@ -1,12 +1,10 @@package tidb-// Framework code is generated by the generator.-import ("context""fmt"- "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"@@ -16,21 +14,19 @@ import (var (_ advisor.Advisor = (*StatementDisallowCommitAdvisor)(nil)- _ ast.Visitor = (*statementDisallowCommitChecker)(nil))func init() {advisor.Register(storepb.Engine_TIDB, storepb.SQLReviewRule_STATEMENT_DISALLOW_COMMIT, &StatementDisallowCommitAdvisor{})}-// StatementDisallowCommitAdvisor is the advisor checking for index type no blob.+// StatementDisallowCommitAdvisor is the advisor checking for disallow COMMIT.type StatementDisallowCommitAdvisor struct {}-// Check checks for index type no blob.+// Check checks for disallow COMMIT.func (*StatementDisallowCommitAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {- stmtList, err := getTiDBNodes(checkCtx)-+ stmts, err := getTiDBOmniNodes(checkCtx)if err != nil {return nil, err}@@ -39,44 +35,20 @@ func (*StatementDisallowCommitAdvisor) Check(_ context.Context, checkCtx advisorif err != nil {return nil, err}- checker := &statementDisallowCommitChecker{- level: level,- title: checkCtx.Rule.Type.String(),- }- for _, stmt := range stmtList {- checker.text = stmt.Text()- checker.line = stmt.OriginTextPosition()- (stmt).Accept(checker)- }-- return checker.adviceList, nil-}--type statementDisallowCommitChecker struct {- adviceList []*storepb.Advice- level storepb.Advice_Status- title string- text string- line int-}--// Enter implements the ast.Visitor interface.-func (c *statementDisallowCommitChecker) Enter(in ast.Node) (ast.Node, bool) {- if _, ok := in.(*ast.CommitStmt); ok {- c.adviceList = append(c.adviceList, &storepb.Advice{- Status: c.level,+ var adviceList []*storepb.Advice+ for _, ostmt := range stmts {+ if _, ok := ostmt.Node.(*ast.CommitStmt); !ok {+ continue+ }+ adviceList = append(adviceList, &storepb.Advice{+ Status: level,Code: code.StatementDisallowCommit.Int32(),- Title: c.title,- Content: fmt.Sprintf("Commit is not allowed, related statement: \"%s\"", c.text),- StartPosition: common.ConvertANTLRLineToPosition(c.line),+ Title: checkCtx.Rule.Type.String(),+ Content: fmt.Sprintf("Commit is not allowed, related statement: \"%s\"", ostmt.TrimmedText()),+ StartPosition: common.ConvertANTLRLineToPosition(ostmt.FirstTokenLine()),})}- return in, false-}--// Leave implements the ast.Visitor interface.-func (*statementDisallowCommitChecker) Leave(in ast.Node) (ast.Node, bool) {- return in, true+ return adviceList, nil}diff --git a/backend/plugin/advisor/tidb/advisor_stmt_disallow_limit.go b/backend/plugin/advisor/tidb/advisor_stmt_disallow_limit.goindex 8df568e5e7..a2242ccc70 100644--- a/backend/plugin/advisor/tidb/advisor_stmt_disallow_limit.go+++ b/backend/plugin/advisor/tidb/advisor_stmt_disallow_limit.go@@ -1,12 +1,10 @@package tidb-// Framework code is generated by the generator.-import ("context""fmt"- "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"@@ -16,7 +14,6 @@ import (var (_ advisor.Advisor = (*StatementDisallowLimitAdvisor)(nil)- _ ast.Visitor = (*statementDisallowLimitChecker)(nil))func init() {@@ -29,8 +26,7 @@ type StatementDisallowLimitAdvisor struct {// Check checks for no LIMIT clause in INSERT/UPDATE statement.func (*StatementDisallowLimitAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {- stmtList, err := getTiDBNodes(checkCtx)-+ stmts, err := getTiDBOmniNodes(checkCtx)if err != nil {return nil, err}@@ -39,73 +35,49 @@ func (*StatementDisallowLimitAdvisor) Check(_ context.Context, checkCtx advisor.if err != nil {return nil, err}- checker := &statementDisallowLimitChecker{- level: level,- title: checkCtx.Rule.Type.String(),- }-- for _, stmt := range stmtList {- checker.text = stmt.Text()- checker.line = stmt.OriginTextPosition()- (stmt).Accept(checker)- }-- return checker.adviceList, nil-}--type statementDisallowLimitChecker struct {- adviceList []*storepb.Advice- level storepb.Advice_Status- title string- text string- line int-}-// Enter implements the ast.Visitor interface.-func (checker *statementDisallowLimitChecker) Enter(in ast.Node) (ast.Node, bool) {- code := advisorcode.Ok- switch node := in.(type) {- case *ast.UpdateStmt:- if node.Limit != nil {- code = advisorcode.UpdateUseLimit+ var adviceList []*storepb.Advice+ for _, ostmt := range stmts {+ code := advisorcode.Ok+ switch n := ostmt.Node.(type) {+ case *ast.UpdateStmt:+ if n.Limit != nil {+ code = advisorcode.UpdateUseLimit+ }+ case *ast.DeleteStmt:+ if n.Limit != nil {+ code = advisorcode.DeleteUseLimit+ }+ case *ast.InsertStmt:+ if insertUsesLimit(n) {+ code = advisorcode.InsertUseLimit+ }+ default:}- case *ast.DeleteStmt:- if node.Limit != nil {- code = advisorcode.DeleteUseLimit- }- case *ast.InsertStmt:- if useLimit(node) {- code = advisorcode.InsertUseLimit- }- default:- }- if code != advisorcode.Ok {- checker.adviceList = append(checker.adviceList, &storepb.Advice{- Status: checker.level,- Code: code.Int32(),- Title: checker.title,- Content: fmt.Sprintf("LIMIT clause is forbidden in INSERT, UPDATE and DELETE statement, but \"%s\" uses", checker.text),- StartPosition: common.ConvertANTLRLineToPosition(checker.line),- })+ if code != advisorcode.Ok {+ adviceList = append(adviceList, &storepb.Advice{+ Status: level,+ Code: code.Int32(),+ Title: checkCtx.Rule.Type.String(),+ Content: fmt.Sprintf("LIMIT clause is forbidden in INSERT, UPDATE and DELETE statement, but \"%s\" uses", ostmt.TrimmedText()),+ StartPosition: common.ConvertANTLRLineToPosition(ostmt.FirstTokenLine()),+ })+ }}- return in, false+ return adviceList, nil}-// Leave implements the ast.Visitor interface.-func (*statementDisallowLimitChecker) Leave(in ast.Node) (ast.Node, bool) {- return in, true-}--func useLimit(node *ast.InsertStmt) bool {- if node.Select != nil {- switch stmt := node.Select.(type) {- case *ast.SelectStmt:- return stmt.Limit != nil- case *ast.SetOprStmt:- return stmt.Limit != nil- }- }- return false+// insertUsesLimit reports whether an INSERT ... SELECT carries a LIMIT+// on its inner SELECT. omni unifies the SELECT and UNION/INTERSECT/EXCEPT+// forms under a single SelectStmt with a SetOp field, so checking+// SelectStmt.Limit covers both pingcap's *SelectStmt and *SetOprStmt cases.+//+// Only the outermost LIMIT is checked. A LIMIT nested inside a+// parenthesized UNION arm (e.g.+// "INSERT INTO t (SELECT a FROM x LIMIT 3) UNION SELECT b FROM y") lives+// on Left.Limit, not on the outer .Limit; this matches pingcap parity.+func insertUsesLimit(n *ast.InsertStmt) bool {+ return n.Select != nil && n.Select.Limit != nil}diff --git a/backend/plugin/advisor/tidb/advisor_stmt_disallow_order_by.go b/backend/plugin/advisor/tidb/advisor_stmt_disallow_order_by.goindex c33fcf1b02..fbc011e00e 100644--- a/backend/plugin/advisor/tidb/advisor_stmt_disallow_order_by.go+++ b/backend/plugin/advisor/tidb/advisor_stmt_disallow_order_by.go@@ -1,12 +1,10 @@package tidb-// Framework code is generated by the generator.-import ("context""fmt"- "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"@@ -16,7 +14,6 @@ import (var (_ advisor.Advisor = (*DisallowOrderByAdvisor)(nil)- _ ast.Visitor = (*disallowOrderByChecker)(nil))func init() {@@ -29,8 +26,7 @@ type DisallowOrderByAdvisor struct {// Check checks for no ORDER BY clause in DELETE/UPDATE statements.func (*DisallowOrderByAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {- stmtList, err := getTiDBNodes(checkCtx)-+ stmts, err := getTiDBOmniNodes(checkCtx)if err != nil {return nil, err}@@ -39,56 +35,32 @@ func (*DisallowOrderByAdvisor) Check(_ context.Context, checkCtx advisor.Contextif err != nil {return nil, err}- checker := &disallowOrderByChecker{- level: level,- title: checkCtx.Rule.Type.String(),- }-- for _, stmt := range stmtList {- checker.text = stmt.Text()- checker.line = stmt.OriginTextPosition()- (stmt).Accept(checker)- }-- return checker.adviceList, nil-}-type disallowOrderByChecker struct {- adviceList []*storepb.Advice- level storepb.Advice_Status- title string- text string- line int-}--// Enter implements the ast.Visitor interface.-func (checker *disallowOrderByChecker) Enter(in ast.Node) (ast.Node, bool) {- code := advisorcode.Ok- switch node := in.(type) {- case *ast.UpdateStmt:- if node.Order != nil {- code = advisorcode.UpdateUseOrderBy- }- case *ast.DeleteStmt:- if node.Order != nil {- code = advisorcode.DeleteUseOrderBy+ var adviceList []*storepb.Advice+ for _, ostmt := range stmts {+ code := advisorcode.Ok+ switch n := ostmt.Node.(type) {+ case *ast.UpdateStmt:+ if len(n.OrderBy) > 0 {+ code = advisorcode.UpdateUseOrderBy+ }+ case *ast.DeleteStmt:+ if len(n.OrderBy) > 0 {+ code = advisorcode.DeleteUseOrderBy+ }+ default:}- default:- }- if code != advisorcode.Ok {- checker.adviceList = append(checker.adviceList, &storepb.Advice{- Status: checker.level,- Code: code.Int32(),- Title: checker.title,- Content: fmt.Sprintf("ORDER BY clause is forbidden in DELETE and UPDATE statements, but \"%s\" uses", checker.text),- StartPosition: common.ConvertANTLRLineToPosition(checker.line),- })+ if code != advisorcode.Ok {+ adviceList = append(adviceList, &storepb.Advice{+ Status: level,+ Code: code.Int32(),+ Title: checkCtx.Rule.Type.String(),+ Content: fmt.Sprintf("ORDER BY clause is forbidden in DELETE and UPDATE statements, but \"%s\" uses", ostmt.TrimmedText()),+ StartPosition: common.ConvertANTLRLineToPosition(ostmt.FirstTokenLine()),+ })+ }}- return in, false-}-// Leave implements the ast.Visitor interface.-func (*disallowOrderByChecker) Leave(in ast.Node) (ast.Node, bool) {- return in, true+ return adviceList, nil}diff --git a/backend/plugin/advisor/tidb/advisor_stmt_no_select_all.go b/backend/plugin/advisor/tidb/advisor_stmt_no_select_all.goindex e79b51a8f8..7f2b85fb54 100644--- a/backend/plugin/advisor/tidb/advisor_stmt_no_select_all.go+++ b/backend/plugin/advisor/tidb/advisor_stmt_no_select_all.go@@ -4,7 +4,7 @@ import ("context""fmt"- "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"@@ -14,7 +14,7 @@ import (var (_ advisor.Advisor = (*NoSelectAllAdvisor)(nil)- _ ast.Visitor = (*noSelectAllChecker)(nil)+ _ ast.Visitor = (*noSelectAllVisitor)(nil))func init() {@@ -25,10 +25,11 @@ func init() {type NoSelectAllAdvisor struct {}-// Check checks for no "select *".+// Check checks for no "select *". Walks the full statement tree because+// the wildcard can appear in nested subqueries (e.g. SELECT a FROM+// (SELECT * FROM t) t).func (*NoSelectAllAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {- root, err := getTiDBNodes(checkCtx)-+ stmts, err := getTiDBOmniNodes(checkCtx)if err != nil {return nil, err}@@ -37,47 +38,70 @@ func (*NoSelectAllAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([if err != nil {return nil, err}- checker := &noSelectAllChecker{- level: level,- title: checkCtx.Rule.Type.String(),- }- for _, stmtNode := range root {- checker.text = stmtNode.Text()- checker.line = stmtNode.OriginTextPosition()- (stmtNode).Accept(checker)++ var adviceList []*storepb.Advice+ for _, ostmt := range stmts {+ // Pingcap parity: every wildcard hit reports the OUTER statement's+ // first-token line, not the inner SelectStmt's line.+ v := &noSelectAllVisitor{+ level: level,+ title: checkCtx.Rule.Type.String(),+ text: ostmt.TrimmedText(),+ line: ostmt.FirstTokenLine(),+ advices: &adviceList,+ }+ ast.Walk(v, ostmt.Node)}- return checker.adviceList, nil+ return adviceList, nil}-type noSelectAllChecker struct {- adviceList []*storepb.Advice- level storepb.Advice_Status- title string- text string- line int+type noSelectAllVisitor struct {+ level storepb.Advice_Status+ title string+ text string+ line int+ advices *[]*storepb.Advice}-// Enter implements the ast.Visitor interface.-func (v *noSelectAllChecker) Enter(in ast.Node) (ast.Node, bool) {- if node, ok := in.(*ast.SelectStmt); ok {- for _, field := range node.Fields.Fields {- if field.WildCard != nil {- v.adviceList = append(v.adviceList, &storepb.Advice{- Status: v.level,- Code: code.StatementSelectAll.Int32(),- Title: v.title,- Content: fmt.Sprintf("\"%s\" uses SELECT all", v.text),- StartPosition: common.ConvertANTLRLineToPosition(v.line),- })- break- }- }+// Visit returns v to recurse into children, including SelectStmt.Left/Right+// (UNION/INTERSECT/EXCEPT arms) per omni's Walk contract.+func (v *noSelectAllVisitor) Visit(node ast.Node) ast.Visitor {+ if node == nil {+ return nil}- return in, false+ sel, ok := node.(*ast.SelectStmt)+ if !ok {+ return v+ }+ if !targetListHasStar(sel.TargetList) {+ return v+ }+ *v.advices = append(*v.advices, &storepb.Advice{+ Status: v.level,+ Code: code.StatementSelectAll.Int32(),+ Title: v.title,+ Content: fmt.Sprintf("\"%s\" uses SELECT all", v.text),+ StartPosition: common.ConvertANTLRLineToPosition(v.line),+ })+ return v}-// Leave implements the ast.Visitor interface.-func (*noSelectAllChecker) Leave(in ast.Node) (ast.Node, bool) {- return in, true+// targetListHasStar reports whether any item in a SELECT's target list is+// a wildcard. omni distinguishes:+// - bare "*" → *ast.StarExpr+// - qualified "t.*" → *ast.ColumnRef with Star: true+func targetListHasStar(targets []ast.ExprNode) bool {+ for _, expr := range targets {+ switch e := expr.(type) {+ case *ast.StarExpr:+ return true+ case *ast.ColumnRef:+ if e.Star {+ return true+ }+ default:+ }+ }+ return false}diff --git a/backend/plugin/advisor/tidb/test/statement_select_no_select_all.yaml b/backend/plugin/advisor/tidb/test/statement_select_no_select_all.yamlindex 202ab36747..1dcbec5128 100644--- a/backend/plugin/advisor/tidb/test/statement_select_no_select_all.yaml+++ b/backend/plugin/advisor/tidb/test/statement_select_no_select_all.yaml@@ -22,3 +22,51 @@line: 1column: 0endposition: null+- statement: SELECT * FROM t1 UNION SELECT a FROM t2+ changeType: 1+ want:+ - status: 2+ code: 203+ title: STATEMENT_SELECT_NO_SELECT_ALL+ content: '"SELECT * FROM t1 UNION SELECT a FROM t2" uses SELECT all'+ startposition:+ line: 1+ column: 0+ endposition: null+- statement: SELECT a FROM t1 UNION SELECT * FROM t2+ changeType: 1+ want:+ - status: 2+ code: 203+ title: STATEMENT_SELECT_NO_SELECT_ALL+ content: '"SELECT a FROM t1 UNION SELECT * FROM t2" uses SELECT all'+ startposition:+ line: 1+ column: 0+ endposition: null+- statement: SELECT a FROM t1 UNION SELECT b FROM t2+ changeType: 1+- statement: SELECT t.* FROM t+ changeType: 1+ want:+ - status: 2+ code: 203+ title: STATEMENT_SELECT_NO_SELECT_ALL+ content: '"SELECT t.* FROM t" uses SELECT all'+ startposition:+ line: 1+ column: 0+ endposition: null+- statement: SELECT a, t.* FROM t+ changeType: 1+ want:+ - status: 2+ code: 203+ title: STATEMENT_SELECT_NO_SELECT_ALL+ content: '"SELECT a, t.* FROM t" uses SELECT all'+ startposition:+ line: 1+ column: 0+ endposition: null+- statement: SELECT a, t.b FROM t+ changeType: 1diff --git a/backend/plugin/advisor/tidb/utils.go b/backend/plugin/advisor/tidb/utils.goindex 482d74de83..9c8af7bc10 100644--- a/backend/plugin/advisor/tidb/utils.go+++ b/backend/plugin/advisor/tidb/utils.go@@ -5,6 +5,7 @@ import ("log/slog""slices""strings"+ "unicode""github.com/pingcap/tidb/pkg/parser/ast""github.com/pingcap/tidb/pkg/parser/format"@@ -138,6 +139,29 @@ func (s OmniStmt) AbsoluteLine(byteOffset int) int {return s.BaseLine + int(pos.Line)}+// TrimmedText returns Text with surrounding whitespace removed. Suitable+// for embedding the statement text into advice content; raw Text may+// include leading/trailing newlines from the original multi-statement+// split.+func (s OmniStmt) TrimmedText() string {+ return strings.TrimSpace(s.Text)+}++// FirstTokenLine returns the 1-based absolute line of the first+// non-whitespace character in s.Text. Matches pingcap's+// OriginTextPosition: pingcap's lexer strips leading whitespace but+// keeps comments as part of the statement, so its reported line points+// at the first comment OR keyword. Used as the StartPosition for+// statement-level advices.+func (s OmniStmt) FirstTokenLine() int {+ for i, r := range s.Text {+ if !unicode.IsSpace(r) {+ return s.AbsoluteLine(i)+ }+ }+ return s.AbsoluteLine(0)+}+// canNull reports whether the given pingcap-AST column may have NULL values// (i.e. the column has no NOT NULL or PRIMARY KEY constraint).//