Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor(advisor/tidb): migrate partition + order-by-rand pair to omni AST + cumulative #23 (Phase 1.5 §1.5.3 batch 14) (#20318)

Vincent Huang5d70830ab736
diff --git a/backend/plugin/advisor/tidb/advisor_column_maximum_character_length.go b/backend/plugin/advisor/tidb/advisor_column_maximum_character_length.go
index 788eccd19a..0f809e8047 100644
--- a/backend/plugin/advisor/tidb/advisor_column_maximum_character_length.go
+++ b/backend/plugin/advisor/tidb/advisor_column_maximum_character_length.go
@@ -96,14 +96,29 @@ func checkStmtForCharLength(ostmt OmniStmt, maximum int, level storepb.Advice_St
}
switch cmd.Type {
case ast.ATAddColumn:
+ // Cumulative #23: pingcap-tidb's pre-omni
+ // AlterTableAddColumns inner column loop had no break;
+ // LAST violating column in the grouped form overwrites
+ // and is reported (asymmetric vs CreateTableStmt which
+ // DOES break — first-wins). Track lastViolation across
+ // the inner loop, emit once after — mirrors cumulative
+ // #15's "preserve pingcap single-advice-per-stmt
+ // cardinality with last-wins semantics" prescription
+ // on the AlterTable ADD COLUMN call-site.
+ var lastCol *ast.ColumnDef
+ var lastLen int
for _, column := range addColumnTargets(cmd) {
if column == nil {
continue
}
if charLength := omniCharLength(column.TypeName); charLength > maximum {
- return buildCharLengthAdvice(level, title, tableName, column.Name, charLength, maximum, stmtLine)
+ lastCol = column
+ lastLen = charLength
}
}
+ if lastCol != nil {
+ return buildCharLengthAdvice(level, title, tableName, lastCol.Name, lastLen, maximum, stmtLine)
+ }
case ast.ATChangeColumn, ast.ATModifyColumn:
if cmd.Column == nil {
continue
diff --git a/backend/plugin/advisor/tidb/advisor_insert_disallow_order_by_rand.go b/backend/plugin/advisor/tidb/advisor_insert_disallow_order_by_rand.go
index fd5d1eafe6..3241f73176 100644
--- a/backend/plugin/advisor/tidb/advisor_insert_disallow_order_by_rand.go
+++ b/backend/plugin/advisor/tidb/advisor_insert_disallow_order_by_rand.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 = (*InsertDisallowOrderByRandAdvisor)(nil)
- _ ast.Visitor = (*insertDisallowOrderByRandChecker)(nil)
)
func init() {
@@ -29,8 +26,7 @@ type InsertDisallowOrderByRandAdvisor struct {
// Check checks for to disallow order by rand in INSERT statements.
func (*InsertDisallowOrderByRandAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {
- stmtList, err := getTiDBNodes(checkCtx)
-
+ stmts, err := getTiDBOmniNodes(checkCtx)
if err != nil {
return nil, err
}
@@ -39,62 +35,62 @@ func (*InsertDisallowOrderByRandAdvisor) Check(_ context.Context, checkCtx advis
if err != nil {
return nil, err
}
- checker := &insertDisallowOrderByRandChecker{
- level: level,
- title: checkCtx.Rule.Type.String(),
- }
+ title := checkCtx.Rule.Type.String()
- for _, stmt := range stmtList {
- checker.text = stmt.Text()
- checker.line = stmt.OriginTextPosition()
- (stmt).Accept(checker)
+ var adviceList []*storepb.Advice
+ for _, ostmt := range stmts {
+ advice := checkStmtForOrderByRand(ostmt, level, title)
+ if advice != nil {
+ adviceList = append(adviceList, advice)
+ }
}
- return checker.adviceList, nil
+ return adviceList, nil
}
-type insertDisallowOrderByRandChecker struct {
- adviceList []*storepb.Advice
- level storepb.Advice_Status
- title string
- text string
- line int
-}
-
-// Enter implements the ast.Visitor interface.
-func (checker *insertDisallowOrderByRandChecker) Enter(in ast.Node) (ast.Node, bool) {
- code := advisorcode.Ok
- if insert, ok := in.(*ast.InsertStmt); ok {
- if insert.Select != nil {
- if selectNode, ok := insert.Select.(*ast.SelectStmt); ok {
- if selectNode.OrderBy != nil {
- for _, item := range selectNode.OrderBy.Items {
- if f, ok := item.Expr.(*ast.FuncCallExpr); ok {
- if f.FnName.L == ast.Rand {
- code = advisorcode.InsertUseOrderByRand
- break
- }
- }
- }
- }
+// checkStmtForOrderByRand returns at most ONE advice per top-level
+// statement, mirroring pingcap-typed predecessor's break-after-first-
+// RAND-match cardinality. Mysql analog emits per-item (no break);
+// tidb preserves pingcap-tidb's single-advice-per-stmt contract.
+//
+// Cumulative #1 framing: INSERT-VALUES / INSERT-SET / INSERT-TABLE
+// forms have `ins.Select == nil` and skip. Only INSERT ... SELECT can
+// have an ORDER BY; only that path is checked.
+//
+// Cumulative #24 — silent UX improvement at the UNION boundary:
+// pingcap's `InsertStmt.Select` is a `ResultSetNode` interface;
+// UNION'd inserts produce `*ast.SetOprStmt`. The pre-omni rule
+// type-asserted `insert.Select.(*ast.SelectStmt)` — the cast failed
+// for UNION'd inputs and silently skipped the whole check. Omni's
+// `InsertStmt.Select` is `*SelectStmt` (concrete) regardless of
+// SetOp, so `ins.Select.OrderBy` here IS the outer-UNION ORDER BY
+// list. The rule now fires on `INSERT ... SELECT ... UNION ...
+// ORDER BY RAND()` (outer-UNION position) — matches the rule's
+// stated intent. NOT a regression. Inner per-arm OrderBy (in
+// parenthesized UNION arms, if/when omni grammar accepts that
+// syntax in INSERT position — currently rejected) remains
+// uncovered, matching pingcap-tidb's per-arm behavior.
+func checkStmtForOrderByRand(ostmt OmniStmt, level storepb.Advice_Status, title string) *storepb.Advice {
+ ins, ok := ostmt.Node.(*ast.InsertStmt)
+ if !ok {
+ return nil
+ }
+ if ins.Select == nil {
+ return nil
+ }
+ for _, item := range ins.Select.OrderBy {
+ if item == nil {
+ continue
+ }
+ if omniIsRandFuncCall(item.Expr) {
+ return &storepb.Advice{
+ Status: level,
+ Code: advisorcode.InsertUseOrderByRand.Int32(),
+ Title: title,
+ Content: fmt.Sprintf("\"%s\" uses ORDER BY RAND in the INSERT statement", ostmt.TrimmedText()),
+ StartPosition: common.ConvertANTLRLineToPosition(ostmt.FirstTokenLine()),
}
}
}
-
- if code != advisorcode.Ok {
- checker.adviceList = append(checker.adviceList, &storepb.Advice{
- Status: checker.level,
- Code: code.Int32(),
- Title: checker.title,
- Content: fmt.Sprintf("\"%s\" uses ORDER BY RAND in the INSERT statement", checker.text),
- StartPosition: common.ConvertANTLRLineToPosition(checker.line),
- })
- }
-
- return in, false
-}
-
-// Leave implements the ast.Visitor interface.
-func (*insertDisallowOrderByRandChecker) Leave(in ast.Node) (ast.Node, bool) {
- return in, true
+ return nil
}
diff --git a/backend/plugin/advisor/tidb/advisor_table_disallow_partition.go b/backend/plugin/advisor/tidb/advisor_table_disallow_partition.go
index b594b611b6..c026d13927 100644
--- a/backend/plugin/advisor/tidb/advisor_table_disallow_partition.go
+++ b/backend/plugin/advisor/tidb/advisor_table_disallow_partition.go
@@ -1,23 +1,19 @@
package tidb
-// Framework code is generated by the generator.
-
import (
"context"
"fmt"
- advisorcode "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-
- "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"
"github.com/bytebase/bytebase/backend/plugin/advisor"
+ advisorcode "github.com/bytebase/bytebase/backend/plugin/advisor/code"
)
var (
_ advisor.Advisor = (*TableDisallowPartitionAdvisor)(nil)
- _ ast.Visitor = (*tableDisallowPartitionChecker)(nil)
)
func init() {
@@ -30,8 +26,7 @@ type TableDisallowPartitionAdvisor struct {
// Check checks for disallow table partition.
func (*TableDisallowPartitionAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {
- stmtList, err := getTiDBNodes(checkCtx)
-
+ stmts, err := getTiDBOmniNodes(checkCtx)
if err != nil {
return nil, err
}
@@ -40,60 +35,55 @@ func (*TableDisallowPartitionAdvisor) Check(_ context.Context, checkCtx advisor.
if err != nil {
return nil, err
}
- checker := &tableDisallowPartitionChecker{
- level: level,
- title: checkCtx.Rule.Type.String(),
- }
+ title := checkCtx.Rule.Type.String()
- for _, stmt := range stmtList {
- checker.text = stmt.Text()
- checker.line = stmt.OriginTextPosition()
- (stmt).Accept(checker)
+ var adviceList []*storepb.Advice
+ for _, ostmt := range stmts {
+ advice := checkStmtForPartition(ostmt, level, title)
+ if advice != nil {
+ adviceList = append(adviceList, advice)
+ }
}
- return checker.adviceList, nil
+ return adviceList, nil
}
-type tableDisallowPartitionChecker struct {
- adviceList []*storepb.Advice
- level storepb.Advice_Status
- title string
- text string
- line int
-}
-
-// Enter implements the ast.Visitor interface.
-func (checker *tableDisallowPartitionChecker) Enter(in ast.Node) (ast.Node, bool) {
- code := advisorcode.Ok
- switch node := in.(type) {
+// checkStmtForPartition returns at most ONE advice per top-level
+// statement, mirroring pingcap-typed predecessor's break-after-first-
+// match cardinality. Pre-omni pingcap matched `spec.Tp ==
+// AlterTablePartition` which is the REPARTITION form only (ALTER
+// TABLE t PARTITION BY ...) — that maps to omni's `ATPartitionBy`.
+// Partition-management forms (ADD PARTITION, DROP PARTITION, etc.)
+// are NOT covered by this rule in either era; long-standing
+// pre-omni behavior preserved per invariant #10. Mysql analog
+// (mysql/rule_table_disallow_partition.go) has identical scope.
+func checkStmtForPartition(ostmt OmniStmt, level storepb.Advice_Status, title string) *storepb.Advice {
+ text := ostmt.TrimmedText()
+ switch n := ostmt.Node.(type) {
case *ast.CreateTableStmt:
- if node.Partition != nil {
- code = advisorcode.CreateTablePartition
+ if n.Partitions != nil {
+ return buildPartitionAdvice(level, title, text, ostmt.FirstTokenLine())
}
case *ast.AlterTableStmt:
- for _, spec := range node.Specs {
- if spec.Tp == ast.AlterTablePartition {
- code = advisorcode.CreateTablePartition
- break
+ for _, cmd := range n.Commands {
+ if cmd == nil {
+ continue
+ }
+ if cmd.Type == ast.ATPartitionBy && cmd.PartitionBy != nil {
+ return buildPartitionAdvice(level, title, text, ostmt.FirstTokenLine())
}
}
default:
}
-
- if code != advisorcode.Ok {
- checker.adviceList = append(checker.adviceList, &storepb.Advice{
- Status: checker.level,
- Code: code.Int32(),
- Title: checker.title,
- Content: fmt.Sprintf("Table partition is forbidden, but \"%s\" creates", checker.text),
- StartPosition: common.ConvertANTLRLineToPosition(checker.line),
- })
- }
-
- return in, false
+ return nil
}
-// Leave implements the ast.Visitor interface.
-func (*tableDisallowPartitionChecker) Leave(in ast.Node) (ast.Node, bool) {
- return in, true
+func buildPartitionAdvice(level storepb.Advice_Status, title, text string, line int) *storepb.Advice {
+ return &storepb.Advice{
+ Status: level,
+ Code: advisorcode.CreateTablePartition.Int32(),
+ Title: title,
+ Content: fmt.Sprintf("Table partition is forbidden, but \"%s\" creates", text),
+ StartPosition: common.ConvertANTLRLineToPosition(line),
+ }
}
diff --git a/backend/plugin/advisor/tidb/test/column_maximum_character_length.yaml b/backend/plugin/advisor/tidb/test/column_maximum_character_length.yaml
index d10a0562d5..1d39bcba83 100644
--- a/backend/plugin/advisor/tidb/test/column_maximum_character_length.yaml
+++ b/backend/plugin/advisor/tidb/test/column_maximum_character_length.yaml
@@ -81,3 +81,22 @@
line: 1
column: 0
endposition: null
+# Cumulative #23 pin (batch 14, batch-13 follow-up): pingcap-tidb's
+# pre-omni AlterTableAddColumns inner column loop had NO break — the
+# LAST violating column in the grouped ADD COLUMN form overwrites and
+# is reported. Asymmetric vs CreateTableStmt which DID break (first-
+# wins). Initial batch-13 migration regressed to first-wins via early
+# return; restored via lastViolation tracker. Same shape as cumulative
+# #15 (charset_allowlist set-then-append cardinality) applied to a
+# different call-site within the same advisor file.
+- statement: ALTER TABLE tech_book ADD COLUMN (a char(225), b char(226))
+ changeType: 1
+ want:
+ - status: 2
+ code: 415
+ title: COLUMN_MAXIMUM_CHARACTER_LENGTH
+ content: The length of the CHAR column `b` is 226, bigger than 20, please use VARCHAR instead
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
diff --git a/backend/plugin/advisor/tidb/test/statement_insert_disallow_order_by_rand.yaml b/backend/plugin/advisor/tidb/test/statement_insert_disallow_order_by_rand.yaml
index 3f0d54c723..d41febcc4a 100644
--- a/backend/plugin/advisor/tidb/test/statement_insert_disallow_order_by_rand.yaml
+++ b/backend/plugin/advisor/tidb/test/statement_insert_disallow_order_by_rand.yaml
@@ -11,3 +11,51 @@
line: 1
column: 0
endposition: null
+# Batch 14: uppercase RAND positive — pingcap canonicalized to
+# lowercase via FnName.L; omni preserves user case in FuncCallExpr.Name.
+# omniIsRandFuncCall uses EqualFold to match both forms.
+- statement: INSERT INTO tech_book SELECT * FROM tech_book ORDER BY RAND()
+ changeType: 1
+ want:
+ - status: 2
+ code: 1108
+ title: STATEMENT_INSERT_DISALLOW_ORDER_BY_RAND
+ content: '"INSERT INTO tech_book SELECT * FROM tech_book ORDER BY RAND()" uses ORDER BY RAND in the INSERT statement'
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
+# Batch 14 negative: INSERT ... SELECT with ORDER BY on a column
+# (not RAND) — the rule should NOT fire. Distinguishes "any ORDER BY"
+# (would be a different rule) from "ORDER BY RAND() specifically".
+- statement: INSERT INTO tech_book SELECT * FROM tech_book ORDER BY id
+ changeType: 1
+# Batch 14 negative: INSERT ... SET form (cumulative #1 territory) —
+# ins.Select is nil; rule short-circuits before checking OrderBy.
+# Pins the "SET-form has no SELECT to inspect" semantic.
+- statement: INSERT INTO tech_book SET id = 1, name = "a"
+ changeType: 1
+# Cumulative #24 pin (batch 14, peer-review-caught pre-merge): pre-omni
+# pingcap-tidb's InsertStmt.Select was a ResultSetNode interface; for
+# UNION'd inserts the concrete type was *ast.SetOprStmt, NOT
+# *ast.SelectStmt. The pingcap Enter's `insert.Select.(*ast.SelectStmt)`
+# cast filtered out SetOprStmt entirely — pre-omni rule did NOT fire on
+# `INSERT ... SELECT ... UNION SELECT ... ORDER BY RAND()` despite the
+# rule's stated intent. Omni's InsertStmt.Select is *SelectStmt
+# (concrete) regardless of SetOp; UNION'd inputs carry the outer ORDER
+# BY at ins.Select.OrderBy. Rule now fires — silent UX improvement
+# fixing a latent pingcap accidental skip. Same UX-improvement shape
+# as cumulative #18 / #21. Inner per-arm parenthesized OrderBy remains
+# uncovered (omni rejects parenthesized UNION arms in INSERT position
+# today — separate Phase 2 grammar gap).
+- statement: INSERT INTO tech_book SELECT id FROM tech_book UNION SELECT id FROM tech_book ORDER BY RAND()
+ changeType: 1
+ want:
+ - status: 2
+ code: 1108
+ title: STATEMENT_INSERT_DISALLOW_ORDER_BY_RAND
+ content: '"INSERT INTO tech_book SELECT id FROM tech_book UNION SELECT id FROM tech_book ORDER BY RAND()" uses ORDER BY RAND in the INSERT statement'
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
diff --git a/backend/plugin/advisor/tidb/test/table_disallow_partition.yaml b/backend/plugin/advisor/tidb/test/table_disallow_partition.yaml
index bc44903ea6..a133d70e15 100644
--- a/backend/plugin/advisor/tidb/test/table_disallow_partition.yaml
+++ b/backend/plugin/advisor/tidb/test/table_disallow_partition.yaml
@@ -38,3 +38,32 @@
line: 1
column: 0
endposition: null
+# Batch 14: HASH partition positive — verifies the rule covers
+# non-RANGE partition methods too. CreateTableStmt.Partitions is
+# non-nil for any PARTITION BY ... clause regardless of method.
+- statement: CREATE TABLE t(a int) PARTITION BY HASH(a) PARTITIONS 4
+ changeType: 1
+ want:
+ - status: 2
+ code: 608
+ title: TABLE_DISALLOW_PARTITION
+ content: 'Table partition is forbidden, but "CREATE TABLE t(a int) PARTITION BY HASH(a) PARTITIONS 4" creates'
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
+# Batch 14 negative: ALTER TABLE ... ADD PARTITION is a partition-
+# management operation that the pre-omni pingcap rule did NOT cover —
+# only `spec.Tp == AlterTablePartition` (the REPARTITION form, omni's
+# ATPartitionBy) triggered. Mysql analog has the same scope. This
+# fixture pins the long-standing behavior to prevent accidental
+# scope expansion via mechanical edits. If a user reports the rule
+# should also catch ADD PARTITION, it's a feature ticket, not a
+# regression.
+- statement: ALTER TABLE tech_book ADD PARTITION (PARTITION p2 VALUES LESS THAN (20))
+ changeType: 1
+# Batch 14 negative: ALTER TABLE ... DROP PARTITION — same scope
+# pin as ADD PARTITION above. Not caught by either pingcap-tidb or
+# the omni port.
+- statement: ALTER TABLE tech_book DROP PARTITION p0
+ changeType: 1
diff --git a/backend/plugin/advisor/tidb/utils.go b/backend/plugin/advisor/tidb/utils.go
index 98fdb0966c..2a5a9addfa 100644
--- a/backend/plugin/advisor/tidb/utils.go
+++ b/backend/plugin/advisor/tidb/utils.go
@@ -410,6 +410,25 @@ func omniIsCurrentTimeFuncCall(expr omniast.ExprNode) bool {
}
}
+// omniIsRandFuncCall checks whether the given expression is a
+// function call to RAND(). Used by insert_disallow_order_by_rand.
+// Pingcap-tidb compared via `FnName.L == ast.Rand` (lowercase
+// canonicalization); omni keeps the user's original case in
+// `FuncCallExpr.Name`, so we compare case-insensitively. RAND
+// accepts an optional integer seed argument; we match regardless
+// of arity (matching pingcap's predecessor — which type-asserted
+// the function call but didn't inspect Args).
+func omniIsRandFuncCall(expr omniast.ExprNode) bool {
+ if expr == nil {
+ return false
+ }
+ fc, ok := expr.(*omniast.FuncCallExpr)
+ if !ok {
+ return false
+ }
+ return strings.EqualFold(fc.Name, "RAND")
+}
+
// omniIsCharOrBinaryType reports whether the column type is a
// fixed-length character/binary type (CHAR or BINARY). Pingcap-tidb
// dispatched on `mysql.TypeString` which covered BOTH `CHAR` and
diff --git a/backend/plugin/advisor/tidb/utils_test.go b/backend/plugin/advisor/tidb/utils_test.go
index 91d00f47e6..0ce446ecdb 100644
--- a/backend/plugin/advisor/tidb/utils_test.go
+++ b/backend/plugin/advisor/tidb/utils_test.go
@@ -344,6 +344,33 @@ func TestOmniIsCurrentTimeFuncCall(t *testing.T) {
}
}
+// TestOmniIsRandFuncCall pins the case-insensitive RAND function
+// detection contract used by insert_disallow_order_by_rand. Pingcap-tidb
+// matched via lowercase-canonical `FnName.L == ast.Rand`; omni preserves
+// the user's case in `FuncCallExpr.Name`, so we compare via EqualFold.
+// RAND accepts an optional seed arg — we match regardless of arity.
+func TestOmniIsRandFuncCall(t *testing.T) {
+ cases := []struct {
+ name string
+ expr omniast.ExprNode
+ want bool
+ }{
+ {"RAND uppercase", &omniast.FuncCallExpr{Name: "RAND"}, true},
+ {"rand lowercase", &omniast.FuncCallExpr{Name: "rand"}, true},
+ {"Rand titlecase", &omniast.FuncCallExpr{Name: "Rand"}, true},
+ {"RAND with seed arg", &omniast.FuncCallExpr{Name: "RAND", Args: []omniast.ExprNode{nil}}, true},
+ {"RANDOM (not synonym)", &omniast.FuncCallExpr{Name: "RANDOM"}, false},
+ {"NOT_RAND prefix-like", &omniast.FuncCallExpr{Name: "NOT_RAND"}, false},
+ {"unknown function", &omniast.FuncCallExpr{Name: "FOO"}, false},
+ {"nil expr", nil, false},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ require.Equal(t, tc.want, omniIsRandFuncCall(tc.expr))
+ })
+ }
+}
+
// TestOmniIsCharOrBinaryType pins cumulative #22 — pingcap's
// `mysql.TypeString` covered BOTH CHAR and BINARY via charset
// distinction. The omni port must match both names; a mechanical