Skip to content

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

refactor(advisor/tidb): migrate index_no_duplicate_column to omni AST (Phase 1.5 §1.5.3 batch 18) (#20324)

Vincent Huang9a19edde3367
diff --git a/backend/plugin/advisor/tidb/advisor_index_key_number_limit.go b/backend/plugin/advisor/tidb/advisor_index_key_number_limit.go
index f88e1da520..d0c3b21128 100644
--- a/backend/plugin/advisor/tidb/advisor_index_key_number_limit.go
+++ b/backend/plugin/advisor/tidb/advisor_index_key_number_limit.go
@@ -155,24 +155,3 @@ func omniIndexKeyCount(c *ast.Constraint) int {
return 0
}
}
-
-// omniConstraintAdviceName returns the constraint name suitable for
-// embedding in advice content. Falls back to "PRIMARY" for unnamed
-// PRIMARY KEY constraints (cumulative #28: pingcap-tidb accepted the
-// non-standard `PRIMARY KEY index_name (cols)` extension and captured
-// the index_name; omni follows standard MySQL grammar where PRIMARY
-// KEY doesn't accept an index_name and silently drops it. "PRIMARY"
-// is MySQL's canonical internal name for the primary key — better UX
-// than the empty backticks the raw `c.Name` would produce).
-func omniConstraintAdviceName(c *ast.Constraint) string {
- if c == nil {
- return ""
- }
- if c.Name != "" {
- return c.Name
- }
- if c.Type == ast.ConstrPrimaryKey {
- return "PRIMARY"
- }
- return ""
-}
diff --git a/backend/plugin/advisor/tidb/advisor_index_no_duplicate_column.go b/backend/plugin/advisor/tidb/advisor_index_no_duplicate_column.go
index a156727df4..2e00968f11 100644
--- a/backend/plugin/advisor/tidb/advisor_index_no_duplicate_column.go
+++ b/backend/plugin/advisor/tidb/advisor_index_no_duplicate_column.go
@@ -1,37 +1,80 @@
package tidb
-// Framework code is generated by the generator.
-
import (
"context"
"fmt"
- "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"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
)
var (
_ advisor.Advisor = (*IndexNoDuplicateColumnAdvisor)(nil)
- _ ast.Visitor = (*indexNoDuplicateColumnChecker)(nil)
)
func init() {
advisor.Register(storepb.Engine_TIDB, storepb.SQLReviewRule_INDEX_NO_DUPLICATE_COLUMN, &IndexNoDuplicateColumnAdvisor{})
}
-// IndexNoDuplicateColumnAdvisor is the advisor checking for no duplicate columns in index.
-type IndexNoDuplicateColumnAdvisor struct {
-}
-
-// Check checks for no duplicate columns in index.
+// IndexNoDuplicateColumnAdvisor flags index/constraint declarations
+// where the same column appears twice in the key list.
+type IndexNoDuplicateColumnAdvisor struct{}
+
+// Check fires on PRIMARY KEY, UNIQUE (all 3 syntactic forms),
+// INDEX/KEY, FOREIGN KEY constraints with duplicate plain-column
+// entries. Real functional-index expressions (e.g., `((a + 1))`)
+// are skipped — pre-omni's `if key.Expr == nil` filter and the
+// omni port's `*ColumnRef`-only type-assert preserve the same
+// author-intent contract ("skip non-column expressions to avoid
+// name-based dedup on functional indexes"). Recipe A; no cross-
+// stmt state.
+//
+// Cumulative #2 coverage (verified empirically): pingcap-tidb's
+// parser produces `ConstraintUniq` (Tp=4) for ALL three UNIQUE
+// syntactic forms — bare `UNIQUE`, `UNIQUE KEY`, and `UNIQUE INDEX`.
+// `ConstraintUniqKey` (Tp=5) and `ConstraintUniqIndex` (Tp=6) are
+// defined in pingcap's enum but the parser never produces them for
+// these inputs. Pre-omni's defensive case list including all three
+// was redundant (Tp=5/6 cases were unreachable). Omni unifies under
+// `ConstrUnique`; the single-arm omni port matches pre-omni
+// behavior mechanically — NO behavior change at the UNIQUE boundary.
+// (Initial speculation framed this as a "silent UX improvement
+// fixing a pre-omni miss of UniqKey" — empirical verification per
+// invariant #9 disproved the speculation; ConstraintUniqKey is
+// dead code in pingcap-tidb for these inputs.)
+//
+// Cumulative #28: PRIMARY KEY with the non-standard `PRIMARY KEY
+// pk (cols)` syntax has empty Name in omni (parser drops it).
+// Advice content uses `omniConstraintAdviceName` (utils.go) which
+// falls back to "PRIMARY" canonical.
+//
+// Cumulative #29 (parser-quirk false-NEGATIVE silently fixed):
+// pingcap-tidb's parser treats single-paren-wrapped column refs
+// (e.g., `INDEX idx((a), (a))`) as expressions (`key.Expr != nil,
+// key.Column == nil`). The pre-omni `if key.Expr == nil` filter
+// (author intent: "skip non-column expressions") had a filter-
+// effect that ALSO skipped paren-wrapped column refs as a side-
+// effect — rule did NOT fire on `((a), (a))` despite the
+// duplicate semantic being unambiguous. Omni follows MySQL 8.0
+// spec: single-paren is grouping (flattened at parse time to
+// inner ColumnRef); double-paren `((expr))` is the functional-
+// index syntax. The `*ColumnRef`-only type-assert in
+// `omniIndexColumns` correctly skips real functional indexes
+// (`((a + 1))` → `*BinaryOperationExpr`, not ColumnRef) while
+// catching paren-wrapped column duplicates. NOT a regression;
+// inverse direction of cumulative #21 (#21 was a parser-quirk
+// false-POSITIVE silently fixed; #29 is a parser-quirk false-
+// NEGATIVE silently fixed). Both positive and negative scope-
+// bounding fixtures pinned.
+//
+// Cumulative #17 sibling-parity: ATAddConstraint + ATAddIndex
+// dual arm preserved per established convention.
func (*IndexNoDuplicateColumnAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([]*storepb.Advice, error) {
- stmtList, err := getTiDBNodes(checkCtx)
-
+ stmts, err := getTiDBOmniNodes(checkCtx)
if err != nil {
return nil, err
}
@@ -40,138 +83,165 @@ func (*IndexNoDuplicateColumnAdvisor) Check(_ context.Context, checkCtx advisor.
if err != nil {
return nil, err
}
- checker := &indexNoDuplicateColumnChecker{
- 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
-}
+ title := checkCtx.Rule.Type.String()
-type indexNoDuplicateColumnChecker struct {
- adviceList []*storepb.Advice
- level storepb.Advice_Status
- title string
- text string
- line int
-}
-
-// Enter implements the ast.Visitor interface.
-func (checker *indexNoDuplicateColumnChecker) Enter(in ast.Node) (ast.Node, bool) {
- type duplicateColumn struct {
+ type hit struct {
+ tp string
table string
index string
column string
line int
- tp string
}
- var columnList []duplicateColumn
- switch node := in.(type) {
- case *ast.CreateTableStmt:
- for _, constraint := range node.Constraints {
- switch constraint.Tp {
- case ast.ConstraintPrimaryKey,
- ast.ConstraintUniq,
- ast.ConstraintUniqIndex,
- ast.ConstraintIndex,
- ast.ConstraintForeignKey:
- if column, duplicate := hasDuplicateColumn(constraint.Keys); duplicate {
- columnList = append(columnList, duplicateColumn{
- tp: indexTypeString(constraint.Tp),
- table: node.Table.Name.O,
- index: constraint.Name,
- column: column,
- line: constraint.OriginTextPosition(),
+ var hits []hit
+
+ for _, ostmt := range stmts {
+ switch n := ostmt.Node.(type) {
+ case *ast.CreateTableStmt:
+ if n.Table == nil {
+ continue
+ }
+ for _, c := range n.Constraints {
+ if c == nil || !omniConstraintIsIndexFamily(c.Type) {
+ continue
+ }
+ if dup, found := omniHasDuplicateString(omniConstraintColumnNames(c)); found {
+ hits = append(hits, hit{
+ tp: omniIndexTypeString(c.Type),
+ table: n.Table.Name,
+ index: omniConstraintAdviceName(c),
+ column: dup,
+ line: ostmt.AbsoluteLine(c.Loc.Start),
})
}
- default:
- // Ignore other constraint types
}
- }
- case *ast.CreateIndexStmt:
- if column, duplicate := hasDuplicateColumn(node.IndexPartSpecifications); duplicate {
- columnList = append(columnList, duplicateColumn{
- tp: "INDEX",
- table: node.Table.Name.O,
- index: node.IndexName,
- column: column,
- line: checker.line,
- })
- }
- case *ast.AlterTableStmt:
- for _, spec := range node.Specs {
- if spec.Tp == ast.AlterTableAddConstraint {
- switch spec.Constraint.Tp {
- case ast.ConstraintPrimaryKey,
- ast.ConstraintUniq,
- ast.ConstraintUniqIndex,
- ast.ConstraintIndex,
- ast.ConstraintForeignKey:
- if column, duplicate := hasDuplicateColumn(spec.Constraint.Keys); duplicate {
- columnList = append(columnList, duplicateColumn{
- tp: indexTypeString(spec.Constraint.Tp),
- table: node.Table.Name.O,
- index: spec.Constraint.Name,
- column: column,
- line: checker.line,
- })
- }
- default:
+ case *ast.CreateIndexStmt:
+ if n.Table == nil {
+ continue
+ }
+ if dup, found := omniHasDuplicateString(omniIndexColumns(n.Columns)); found {
+ hits = append(hits, hit{
+ tp: "INDEX",
+ table: n.Table.Name,
+ index: n.IndexName,
+ column: dup,
+ line: ostmt.FirstTokenLine(),
+ })
+ }
+ case *ast.AlterTableStmt:
+ if n.Table == nil {
+ continue
+ }
+ stmtLine := ostmt.FirstTokenLine()
+ for _, cmd := range n.Commands {
+ if cmd == nil || cmd.Constraint == nil {
+ continue
+ }
+ // Cumulative #17 sibling-parity: ATAddIndex paired
+ // with ATAddConstraint even though tidb omni emits
+ // only the latter today.
+ if cmd.Type != ast.ATAddConstraint && cmd.Type != ast.ATAddIndex {
+ continue
+ }
+ c := cmd.Constraint
+ if !omniConstraintIsIndexFamily(c.Type) {
+ continue
+ }
+ if dup, found := omniHasDuplicateString(omniConstraintColumnNames(c)); found {
+ hits = append(hits, hit{
+ tp: omniIndexTypeString(c.Type),
+ table: n.Table.Name,
+ index: omniConstraintAdviceName(c),
+ column: dup,
+ line: stmtLine,
+ })
}
}
+ default:
}
- default:
}
- for _, column := range columnList {
- checker.adviceList = append(checker.adviceList, &storepb.Advice{
- Status: checker.level,
+ adviceList := make([]*storepb.Advice, 0, len(hits))
+ for _, h := range hits {
+ adviceList = append(adviceList, &storepb.Advice{
+ Status: level,
Code: code.DuplicateColumnInIndex.Int32(),
- Title: checker.title,
- Content: fmt.Sprintf("%s `%s` has duplicate column `%s`.`%s`", column.tp, column.index, column.table, column.column),
- StartPosition: common.ConvertANTLRLineToPosition(column.line),
+ Title: title,
+ Content: fmt.Sprintf("%s `%s` has duplicate column `%s`.`%s`", h.tp, h.index, h.table, h.column),
+ StartPosition: common.ConvertANTLRLineToPosition(h.line),
})
}
-
- return in, false
+ return adviceList, nil
}
-// Leave implements the ast.Visitor interface.
-func (*indexNoDuplicateColumnChecker) Leave(in ast.Node) (ast.Node, bool) {
- return in, true
-}
-
-func hasDuplicateColumn(keyList []*ast.IndexPartSpecification) (string, bool) {
- checker := make(map[string]bool)
- for _, key := range keyList {
- if key.Expr == nil {
- if _, exists := checker[key.Column.Name.O]; exists {
- return key.Column.Name.O, true
- }
- checker[key.Column.Name.O] = true
- }
+// omniConstraintIsIndexFamily reports whether the constraint type
+// participates in the duplicate-column check: PRIMARY KEY, UNIQUE,
+// INDEX/KEY, or FOREIGN KEY. Omni's `ConstrUnique` unifies the 3
+// pingcap UNIQUE syntactic forms (parser produces ConstraintUniq=4
+// for bare UNIQUE / UNIQUE KEY / UNIQUE INDEX; UniqKey=5 / UniqIndex=6
+// are defined enum values but parser-unreachable — see top-level
+// docstring's cumulative #2 note). Mechanical port.
+func omniConstraintIsIndexFamily(t ast.ConstraintType) bool {
+ switch t {
+ case ast.ConstrPrimaryKey, ast.ConstrUnique, ast.ConstrIndex, ast.ConstrForeignKey:
+ return true
+ default:
+ return false
}
-
- return "", false
}
-func indexTypeString(tp ast.ConstraintType) string {
- switch tp {
- case ast.ConstraintPrimaryKey:
+// omniIndexTypeString returns the display string for the given
+// constraint type, used in the duplicate-column advice content.
+// Mirrors pre-omni `indexTypeString`. Pre-omni had 3 separate UNIQUE
+// cases all mapping to "UNIQUE KEY" — omni's unified ConstrUnique
+// renders identically.
+func omniIndexTypeString(t ast.ConstraintType) string {
+ switch t {
+ case ast.ConstrPrimaryKey:
return "PRIMARY KEY"
- case ast.ConstraintUniq, ast.ConstraintUniqKey, ast.ConstraintUniqIndex:
+ case ast.ConstrUnique:
return "UNIQUE KEY"
- case ast.ConstraintForeignKey:
+ case ast.ConstrForeignKey:
return "FOREIGN KEY"
- case ast.ConstraintIndex:
+ case ast.ConstrIndex:
return "INDEX"
default:
+ return "INDEX"
+ }
+}
+
+// omniConstraintColumnNames extracts the plain-column names for the
+// duplicate-column check. Omni splits the storage by constraint type:
+// - INDEX / PK / UNIQUE store keys in `IndexColumns []*IndexColumn`;
+// each may carry an `Expr` that's either `*ColumnRef` (plain
+// column) or another expression (functional index). The pre-omni
+// filter `if key.Expr == nil` maps to "skip non-ColumnRef Exprs"
+// in omni; we reuse the existing `omniIndexColumns` helper which
+// applies that filter.
+// - FOREIGN KEY stores its local columns in `Columns []string`
+// (verified empirically against omni parser source — FK
+// `constr.Columns = cols` is the only population path; IndexColumns
+// stays nil). Return those directly.
+func omniConstraintColumnNames(c *ast.Constraint) []string {
+ if c == nil {
+ return nil
+ }
+ if c.Type == ast.ConstrForeignKey {
+ return c.Columns
}
- return "INDEX"
+ return omniIndexColumns(c.IndexColumns)
+}
+
+// omniHasDuplicateString returns the first repeating name in the
+// slice, or "" / false if no duplicates. Used by
+// index_no_duplicate_column to detect repeated column refs in index
+// key lists after omniConstraintColumnNames normalization.
+func omniHasDuplicateString(names []string) (string, bool) {
+ seen := make(map[string]bool)
+ for _, name := range names {
+ if seen[name] {
+ return name, true
+ }
+ seen[name] = true
+ }
+ return "", false
}
diff --git a/backend/plugin/advisor/tidb/test/index_no_duplicate_column.yaml b/backend/plugin/advisor/tidb/test/index_no_duplicate_column.yaml
index 0f7c8c0896..9f4899633a 100644
--- a/backend/plugin/advisor/tidb/test/index_no_duplicate_column.yaml
+++ b/backend/plugin/advisor/tidb/test/index_no_duplicate_column.yaml
@@ -40,6 +40,11 @@
line: 2
column: 0
endposition: null
+# Cumulative #28: omni follows standard MySQL grammar and drops the
+# non-standard `PRIMARY KEY pk_a (cols)` index name. Advisor falls
+# back to "PRIMARY" canonical (matching what MySQL's information_
+# schema would surface for the primary key index regardless of any
+# user-supplied non-standard name).
- statement: |-
CREATE TABLE t(a int);
ALTER TABLE t ADD PRIMARY KEY pk_a (a, a)
@@ -48,7 +53,7 @@
- status: 2
code: 812
title: INDEX_NO_DUPLICATE_COLUMN
- content: PRIMARY KEY `pk_a` has duplicate column `t`.`a`
+ content: PRIMARY KEY `PRIMARY` has duplicate column `t`.`a`
startposition:
line: 2
column: 0
@@ -79,3 +84,75 @@
line: 2
column: 0
endposition: null
+# Cumulative #2 parity pin: verify all three UNIQUE syntactic forms
+# fire identically. Pingcap empirically maps `UNIQUE`, `UNIQUE KEY`,
+# `UNIQUE INDEX` all to `ConstraintUniq` (Tp=4); `ConstraintUniqKey`
+# (Tp=5) and `ConstraintUniqIndex` (Tp=6) are defined in the enum
+# but never produced for these inputs. Omni unifies under
+# `ConstrUnique`. Both engines emit "UNIQUE KEY" in advice content
+# (per indexTypeString mapping in pre-omni / omniIndexTypeString in
+# the port). UNIQUE INDEX form pinned (the existing fixture already
+# covered UNIQUE KEY; bare UNIQUE not yet pinned).
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t ADD UNIQUE INDEX ui_a (a, a)
+ changeType: 1
+ want:
+ - status: 2
+ code: 812
+ title: INDEX_NO_DUPLICATE_COLUMN
+ content: UNIQUE KEY `ui_a` has duplicate column `t`.`a`
+ startposition:
+ line: 2
+ column: 0
+ endposition: null
+# Bare UNIQUE (no name) — exercises the empty-name path on
+# ConstrUnique. Advisor passes empty constraint name through
+# omniConstraintAdviceName, which returns "" for non-PK constraints
+# (the "PRIMARY" fallback only fires on PRIMARY KEY).
+- statement: |-
+ CREATE TABLE t(a int);
+ ALTER TABLE t ADD UNIQUE (a, a)
+ changeType: 1
+ want:
+ - status: 2
+ code: 812
+ title: INDEX_NO_DUPLICATE_COLUMN
+ content: UNIQUE KEY `` has duplicate column `t`.`a`
+ startposition:
+ line: 2
+ column: 0
+ endposition: null
+# Cumulative #29 positive pin (parser-quirk false-NEGATIVE silently
+# fixed): pingcap-tidb's parser treats single-paren-wrapped column
+# refs as expressions (key.Expr != nil), so its `if key.Expr == nil`
+# filter skipped them — pre-omni did NOT fire on `INDEX idx((a),
+# (a))` despite the duplicate semantic being unambiguous. Omni
+# follows MySQL 8.0 spec: single-paren is grouping, flattened at
+# parse time to ColumnRef. Rule now fires. Caught pre-merge by
+# Codex P1; same family as cumulative #21 (parser quirk cleaned up
+# by omni migration) but inverse direction (#21: false-positive
+# fixed; #29: false-negative fixed).
+- statement: CREATE TABLE t(a int, INDEX idx1 ((a), (a)))
+ changeType: 1
+ want:
+ - status: 2
+ code: 812
+ title: INDEX_NO_DUPLICATE_COLUMN
+ content: INDEX `idx1` has duplicate column `t`.`a`
+ startposition:
+ line: 1
+ column: 0
+ endposition: null
+# Cumulative #29 NEGATIVE scope-bounding pin: real functional-index
+# expressions (e.g. `((a + 1))`) produce non-ColumnRef Expr in omni
+# (`*BinaryOperationExpr`); `omniIndexColumns`'s `*ColumnRef`-only
+# type-assert correctly skips them. This pin locks the change scope
+# of cumulative #29 to single-paren-wrapped ColumnRef ONLY — a
+# future refactor of `omniIndexColumns` (e.g., "let's also extract
+# bare column names from expressions") could silently broaden
+# coverage into real functional indexes; this pin catches that
+# regression. The paired positive + negative fixtures define the
+# cumulative #29 contract precisely.
+- statement: CREATE TABLE t(a int, INDEX idx1 ((a + 1), (a + 1)))
+ changeType: 1
diff --git a/backend/plugin/advisor/tidb/utils.go b/backend/plugin/advisor/tidb/utils.go
index 2a5a9addfa..c47af1d49d 100644
--- a/backend/plugin/advisor/tidb/utils.go
+++ b/backend/plugin/advisor/tidb/utils.go
@@ -498,6 +498,31 @@ func omniIsIntegerType(dt *omniast.DataType) bool {
}
}
+// omniConstraintAdviceName returns the constraint name suitable for
+// embedding in advice content. Falls back to "PRIMARY" for unnamed
+// PRIMARY KEY constraints (cumulative #28: pingcap-tidb accepted the
+// non-standard `PRIMARY KEY index_name (cols)` extension and captured
+// the index_name; omni follows standard MySQL grammar where PRIMARY
+// KEY doesn't accept an index_name and silently drops it. "PRIMARY"
+// is MySQL's canonical internal name for the primary key — better UX
+// than the empty backticks the raw `c.Name` would produce).
+//
+// Promoted to utils.go in batch 18 (originally file-local in batch
+// 17's advisor_index_key_number_limit.go) when a second consumer
+// (advisor_index_no_duplicate_column) needed the same fallback.
+func omniConstraintAdviceName(c *omniast.Constraint) string {
+ if c == nil {
+ return ""
+ }
+ if c.Name != "" {
+ return c.Name
+ }
+ if c.Type == omniast.ConstrPrimaryKey {
+ return "PRIMARY"
+ }
+ return ""
+}
+
// omniDataTypeNameCompact returns a compact, lowercase type-name string
// for use in advice content + allowlist comparisons. Mirrors the mysql
// helper of the same name (mysql/utils_omni.go). Length/scale info is