Skip to content

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

refactor: eliminate catalog.Final dependency and improve code quality (#18080)

Danny Xud7a55d178aa7
diff --git a/backend/plugin/advisor/catalog/finder.go b/backend/plugin/advisor/catalog/finder.go
index 06cba64367..5f32d69ea4 100644
--- a/backend/plugin/advisor/catalog/finder.go
+++ b/backend/plugin/advisor/catalog/finder.go
@@ -44,20 +44,9 @@ func (ctx *FinderContext) Copy() *FinderContext {
// Finder is the service for finding schema information in database.
type Finder struct {
Origin *DatabaseState
- Final *DatabaseState
}
// NewFinder creates a new finder.
func NewFinder(database *storepb.DatabaseSchemaMetadata, ctx *FinderContext) *Finder {
- return &Finder{Origin: newDatabaseState(database, ctx), Final: newDatabaseState(database, ctx)}
-}
-
-// NewEmptyFinder creates a finder with empty database.
-func NewEmptyFinder(ctx *FinderContext) *Finder {
- return &Finder{Origin: newDatabaseState(&storepb.DatabaseSchemaMetadata{}, ctx), Final: newDatabaseState(&storepb.DatabaseSchemaMetadata{}, ctx)}
-}
-
-// WalkThrough does the walk through.
-func (f *Finder) WalkThrough(ast any) error {
- return f.Final.WalkThrough(ast)
+ return &Finder{Origin: newDatabaseState(database, ctx)}
}
diff --git a/backend/plugin/advisor/catalog/walk_through_test.go b/backend/plugin/advisor/catalog/walk_through_test.go
index 510b47c4c2..72b8c66ea0 100644
--- a/backend/plugin/advisor/catalog/walk_through_test.go
+++ b/backend/plugin/advisor/catalog/walk_through_test.go
@@ -198,7 +198,7 @@ func runWalkThroughTest(t *testing.T, file string, engineType storepb.Engine, or
if originDatabase != nil {
state = newDatabaseState(originDatabase, &FinderContext{CheckIntegrity: true, EngineType: engineType, IgnoreCaseSensitive: test.IgnoreCaseSensitive})
} else {
- finder := NewEmptyFinder(&FinderContext{CheckIntegrity: false, EngineType: engineType, IgnoreCaseSensitive: test.IgnoreCaseSensitive})
+ finder := NewFinder(&storepb.DatabaseSchemaMetadata{}, &FinderContext{CheckIntegrity: false, EngineType: engineType, IgnoreCaseSensitive: test.IgnoreCaseSensitive})
state = finder.Origin
}
@@ -248,7 +248,7 @@ func runANTLRWalkThroughTest(t *testing.T, file string, engineType storepb.Engin
if originDatabase != nil {
state = newDatabaseState(originDatabase, &FinderContext{CheckIntegrity: true, EngineType: engineType, IgnoreCaseSensitive: test.IgnoreCaseSensitive})
} else {
- finder := NewEmptyFinder(&FinderContext{CheckIntegrity: false, EngineType: engineType, IgnoreCaseSensitive: test.IgnoreCaseSensitive})
+ finder := NewFinder(&storepb.DatabaseSchemaMetadata{}, &FinderContext{CheckIntegrity: false, EngineType: engineType, IgnoreCaseSensitive: test.IgnoreCaseSensitive})
state = finder.Origin
}
diff --git a/backend/plugin/advisor/mysql/rule_column_no_null.go b/backend/plugin/advisor/mysql/rule_column_no_null.go
index 7aa05f6786..2bf0d69bfa 100644
--- a/backend/plugin/advisor/mysql/rule_column_no_null.go
+++ b/backend/plugin/advisor/mysql/rule_column_no_null.go
@@ -3,7 +3,6 @@ package mysql
import (
"context"
"fmt"
- "slices"
"github.com/antlr4-go/antlr/v4"
"github.com/bytebase/parser/mysql"
@@ -12,7 +11,6 @@ import (
"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/catalog"
mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
)
@@ -43,7 +41,7 @@ func (*ColumnNoNullAdvisor) Check(_ context.Context, checkCtx advisor.Context) (
}
// Create the rule
- rule := NewColumnNoNullRule(level, string(checkCtx.Rule.Type), checkCtx.Catalog)
+ rule := NewColumnNoNullRule(level, string(checkCtx.Rule.Type))
// Create the generic checker with the rule
checker := NewGenericChecker([]Rule{rule})
@@ -54,38 +52,21 @@ func (*ColumnNoNullAdvisor) Check(_ context.Context, checkCtx advisor.Context) (
antlr.ParseTreeWalkerDefault.Walk(checker, stmtNode.Tree)
}
- // Generate advice after walking
- rule.generateAdvice()
-
return checker.GetAdviceList(), nil
}
-type columnName struct {
- tableName string
- columnName string
- line int
-}
-
-func (c columnName) name() string {
- return fmt.Sprintf("%s.%s", c.tableName, c.columnName)
-}
-
// ColumnNoNullRule checks for column no NULL value.
type ColumnNoNullRule struct {
BaseRule
- columnSet map[string]columnName
- catalog *catalog.Finder
}
// NewColumnNoNullRule creates a new ColumnNoNullRule.
-func NewColumnNoNullRule(level storepb.Advice_Status, title string, catalog *catalog.Finder) *ColumnNoNullRule {
+func NewColumnNoNullRule(level storepb.Advice_Status, title string) *ColumnNoNullRule {
return &ColumnNoNullRule{
BaseRule: BaseRule{
level: level,
title: title,
},
- columnSet: make(map[string]columnName),
- catalog: catalog,
}
}
@@ -112,44 +93,6 @@ func (*ColumnNoNullRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
return nil
}
-func (r *ColumnNoNullRule) generateAdvice() {
- var columnList []columnName
- for _, column := range r.columnSet {
- columnList = append(columnList, column)
- }
- slices.SortFunc(columnList, func(a, b columnName) int {
- if a.line != b.line {
- if a.line < b.line {
- return -1
- }
- return 1
- }
- if a.columnName < b.columnName {
- return -1
- }
- if a.columnName > b.columnName {
- return 1
- }
- return 0
- })
-
- for _, column := range columnList {
- col := r.catalog.Final.FindColumn(&catalog.ColumnFind{
- TableName: column.tableName,
- ColumnName: column.columnName,
- })
- if col != nil && col.Nullable() {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: advisor.ColumnCannotNull.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("`%s`.`%s` cannot have NULL value", column.tableName, column.columnName),
- StartPosition: common.ConvertANTLRLineToPosition(column.line),
- })
- }
- }
-}
-
func (r *ColumnNoNullRule) checkCreateTable(ctx *mysql.CreateTableContext) {
if ctx.TableName() == nil {
return
@@ -167,19 +110,42 @@ func (r *ColumnNoNullRule) checkCreateTable(ctx *mysql.CreateTableContext) {
continue
}
- _, _, column := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- if tableElement.ColumnDefinition().FieldDefinition() == nil {
+ _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
+ fieldDef := tableElement.ColumnDefinition().FieldDefinition()
+ if fieldDef == nil {
continue
}
- col := columnName{
- tableName: tableName,
- columnName: column,
- line: r.baseLine + tableElement.ColumnDefinition().GetStart().GetLine(),
+
+ // Check nullability directly from AST
+ if isNullable(fieldDef) {
+ r.AddAdvice(&storepb.Advice{
+ Status: r.level,
+ Code: advisor.ColumnCannotNull.Int32(),
+ Title: r.title,
+ Content: fmt.Sprintf("`%s`.`%s` cannot have NULL value", tableName, columnName),
+ StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + tableElement.ColumnDefinition().GetStart().GetLine()),
+ })
+ }
+ }
+}
+
+// isNullable checks if a column is nullable based on its field definition.
+// Default is nullable unless explicitly marked as NOT NULL or PRIMARY KEY.
+func isNullable(fieldDef mysql.IFieldDefinitionContext) bool {
+ for _, attribute := range fieldDef.AllColumnAttribute() {
+ if attribute == nil {
+ continue
+ }
+ // NOT NULL
+ if attribute.NullLiteral() != nil && attribute.NOT_SYMBOL() != nil {
+ return false
}
- if _, exists := r.columnSet[col.name()]; !exists {
- r.columnSet[col.name()] = col
+ // PRIMARY KEY implies NOT NULL
+ if attribute.GetValue() != nil && attribute.GetValue().GetTokenType() == mysql.MySQLParserKEY_SYMBOL {
+ return false
}
}
+ return true
}
func (r *ColumnNoNullRule) checkAlterTable(ctx *mysql.AlterTableContext) {
@@ -200,41 +166,63 @@ func (r *ColumnNoNullRule) checkAlterTable(ctx *mysql.AlterTableContext) {
continue
}
- var columns []string
switch {
// add column
case item.ADD_SYMBOL() != nil:
switch {
case item.Identifier() != nil && item.FieldDefinition() != nil:
- column := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- columns = append(columns, column)
+ columnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
+ if isNullable(item.FieldDefinition()) {
+ r.AddAdvice(&storepb.Advice{
+ Status: r.level,
+ Code: advisor.ColumnCannotNull.Int32(),
+ Title: r.title,
+ Content: fmt.Sprintf("`%s`.`%s` cannot have NULL value", tableName, columnName),
+ StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + item.GetStart().GetLine()),
+ })
+ }
case item.OPEN_PAR_SYMBOL() != nil && item.TableElementList() != nil:
for _, tableElement := range item.TableElementList().AllTableElement() {
- if tableElement.ColumnDefinition() == nil {
+ if tableElement.ColumnDefinition() == nil || tableElement.ColumnDefinition().FieldDefinition() == nil {
continue
}
- _, _, column := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- columns = append(columns, column)
+ _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
+ if isNullable(tableElement.ColumnDefinition().FieldDefinition()) {
+ r.AddAdvice(&storepb.Advice{
+ Status: r.level,
+ Code: advisor.ColumnCannotNull.Int32(),
+ Title: r.title,
+ Content: fmt.Sprintf("`%s`.`%s` cannot have NULL value", tableName, columnName),
+ StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + tableElement.GetStart().GetLine()),
+ })
+ }
}
default:
}
- // change column
- case item.CHANGE_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.Identifier() != nil:
- // only care new column name.
- column := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- columns = append(columns, column)
- default:
- }
-
- for _, column := range columns {
- col := columnName{
- tableName: tableName,
- columnName: column,
- line: r.baseLine + item.GetStart().GetLine(),
+ // change column or modify column
+ case item.CHANGE_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.Identifier() != nil && item.FieldDefinition() != nil:
+ columnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
+ if isNullable(item.FieldDefinition()) {
+ r.AddAdvice(&storepb.Advice{
+ Status: r.level,
+ Code: advisor.ColumnCannotNull.Int32(),
+ Title: r.title,
+ Content: fmt.Sprintf("`%s`.`%s` cannot have NULL value", tableName, columnName),
+ StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + item.GetStart().GetLine()),
+ })
}
- if _, exists := r.columnSet[col.name()]; !exists {
- r.columnSet[col.name()] = col
+ case item.MODIFY_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.FieldDefinition() != nil:
+ columnName := mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())
+ if isNullable(item.FieldDefinition()) {
+ r.AddAdvice(&storepb.Advice{
+ Status: r.level,
+ Code: advisor.ColumnCannotNull.Int32(),
+ Title: r.title,
+ Content: fmt.Sprintf("`%s`.`%s` cannot have NULL value", tableName, columnName),
+ StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + item.GetStart().GetLine()),
+ })
}
+ default:
}
}
}
diff --git a/backend/plugin/advisor/mysql/rule_index_total_number_limit.go b/backend/plugin/advisor/mysql/rule_index_total_number_limit.go
index 09465e59d2..e3f3214282 100644
--- a/backend/plugin/advisor/mysql/rule_index_total_number_limit.go
+++ b/backend/plugin/advisor/mysql/rule_index_total_number_limit.go
@@ -3,7 +3,6 @@ package mysql
import (
"context"
"fmt"
- "slices"
"github.com/antlr4-go/antlr/v4"
"github.com/bytebase/parser/mysql"
@@ -58,27 +57,33 @@ func (*IndexTotalNumberLimitAdvisor) Check(_ context.Context, checkCtx advisor.C
antlr.ParseTreeWalkerDefault.Walk(checker, stmt.Tree)
}
- return rule.generateAdvice(), nil
+ // Check all tables after processing all statements
+ rule.checkAllTables()
+
+ return checker.GetAdviceList(), nil
}
// IndexTotalNumberLimitRule checks for index total number limit.
type IndexTotalNumberLimitRule struct {
BaseRule
- max int
- lineForTable map[string]int
+ max int
+ // tableIndexes tracks indexes for each table across all statements
+ tableIndexes map[string]map[string]bool // tableName -> indexName -> exists
+ tableLines map[string]int // tableName -> last line number
catalog *catalog.Finder
}
// NewIndexTotalNumberLimitRule creates a new IndexTotalNumberLimitRule.
-func NewIndexTotalNumberLimitRule(level storepb.Advice_Status, title string, maxIndexes int, catalog *catalog.Finder) *IndexTotalNumberLimitRule {
+func NewIndexTotalNumberLimitRule(level storepb.Advice_Status, title string, maxIndexes int, catalogFinder *catalog.Finder) *IndexTotalNumberLimitRule {
return &IndexTotalNumberLimitRule{
BaseRule: BaseRule{
level: level,
title: title,
},
max: maxIndexes,
- lineForTable: make(map[string]int),
- catalog: catalog,
+ tableIndexes: make(map[string]map[string]bool),
+ tableLines: make(map[string]int),
+ catalog: catalogFinder,
}
}
@@ -106,45 +111,6 @@ func (*IndexTotalNumberLimitRule) OnExit(_ antlr.ParserRuleContext, _ string) er
return nil
}
-func (r *IndexTotalNumberLimitRule) generateAdvice() []*storepb.Advice {
- type tableName struct {
- name string
- line int
- }
- var tableList []tableName
-
- for k, v := range r.lineForTable {
- tableList = append(tableList, tableName{
- name: k,
- line: v,
- })
- }
- slices.SortFunc(tableList, func(i, j tableName) int {
- if i.line < j.line {
- return -1
- }
- if i.line > j.line {
- return 1
- }
- return 0
- })
-
- for _, table := range tableList {
- tableInfo := r.catalog.Final.FindTable(&catalog.TableFind{TableName: table.name})
- if tableInfo != nil && tableInfo.CountIndex() > r.max {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: advisor.IndexCountExceedsLimit.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("The count of index in table `%s` should be no more than %d, but found %d", table.name, r.max, tableInfo.CountIndex()),
- StartPosition: common.ConvertANTLRLineToPosition(table.line),
- })
- }
- }
-
- return r.adviceList
-}
-
func (r *IndexTotalNumberLimitRule) checkCreateTable(ctx *mysql.CreateTableContext) {
if !mysqlparser.IsTopMySQLRule(&ctx.BaseParserRuleContext) {
return
@@ -154,7 +120,31 @@ func (r *IndexTotalNumberLimitRule) checkCreateTable(ctx *mysql.CreateTableConte
}
_, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
- r.lineForTable[tableName] = r.baseLine + ctx.GetStart().GetLine()
+
+ // Initialize table indexes map
+ if r.tableIndexes[tableName] == nil {
+ r.tableIndexes[tableName] = make(map[string]bool)
+ }
+
+ // Extract indexes from table elements
+ if ctx.TableElementList() != nil {
+ for _, tableElement := range ctx.TableElementList().AllTableElement() {
+ if tableElement == nil {
+ continue
+ }
+ // Check column definitions for inline indexes
+ if tableElement.ColumnDefinition() != nil && tableElement.ColumnDefinition().FieldDefinition() != nil {
+ r.checkFieldDefinitionForIndexes(tableName, tableElement.ColumnDefinition().FieldDefinition())
+ }
+ // Check table constraints for indexes
+ if tableElement.TableConstraintDef() != nil {
+ r.checkTableConstraintForIndexes(tableName, tableElement.TableConstraintDef())
+ }
+ }
+ }
+
+ // Track last line for this table
+ r.tableLines[tableName] = ctx.GetStart().GetLine()
}
func (r *IndexTotalNumberLimitRule) checkCreateIndex(ctx *mysql.CreateIndexContext) {
@@ -165,7 +155,18 @@ func (r *IndexTotalNumberLimitRule) checkCreateIndex(ctx *mysql.CreateIndexConte
return
}
_, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.CreateIndexTarget().TableRef())
- r.lineForTable[tableName] = r.baseLine + ctx.GetStart().GetLine()
+
+ // Initialize table indexes map
+ if r.tableIndexes[tableName] == nil {
+ r.tableIndexes[tableName] = make(map[string]bool)
+ }
+
+ // Add the index (use a generic name since we just need to count)
+ indexName := fmt.Sprintf("__index_%d__", len(r.tableIndexes[tableName]))
+ r.tableIndexes[tableName][indexName] = true
+
+ // Track last line for this table
+ r.tableLines[tableName] = ctx.GetStart().GetLine()
}
func (r *IndexTotalNumberLimitRule) checkAlterTable(ctx *mysql.AlterTableContext) {
@@ -183,6 +184,12 @@ func (r *IndexTotalNumberLimitRule) checkAlterTable(ctx *mysql.AlterTableContext
}
_, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())
+
+ // Initialize table indexes map
+ if r.tableIndexes[tableName] == nil {
+ r.tableIndexes[tableName] = make(map[string]bool)
+ }
+
for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
if item == nil {
continue
@@ -194,52 +201,89 @@ func (r *IndexTotalNumberLimitRule) checkAlterTable(ctx *mysql.AlterTableContext
switch {
// add single columns.
case item.Identifier() != nil && item.FieldDefinition() != nil:
- r.checkFieldDefinitionContext(tableName, item.FieldDefinition())
+ r.checkFieldDefinitionForIndexes(tableName, item.FieldDefinition())
// add multi columns.
case item.OPEN_PAR_SYMBOL() != nil && item.TableElementList() != nil:
for _, tableElement := range item.TableElementList().AllTableElement() {
- if tableElement.ColumnDefinition() == nil || tableElement.ColumnDefinition().FieldDefinition() == nil {
- continue
+ if tableElement.ColumnDefinition() != nil && tableElement.ColumnDefinition().FieldDefinition() != nil {
+ r.checkFieldDefinitionForIndexes(tableName, tableElement.ColumnDefinition().FieldDefinition())
+ }
+ if tableElement.TableConstraintDef() != nil {
+ r.checkTableConstraintForIndexes(tableName, tableElement.TableConstraintDef())
}
- r.checkFieldDefinitionContext(tableName, tableElement.ColumnDefinition().FieldDefinition())
}
// add constraint.
case item.TableConstraintDef() != nil:
- r.checkTableConstraintDef(tableName, item.TableConstraintDef())
+ r.checkTableConstraintForIndexes(tableName, item.TableConstraintDef())
default:
}
// change column.
case item.CHANGE_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.Identifier() != nil:
- r.checkFieldDefinitionContext(tableName, item.FieldDefinition())
+ r.checkFieldDefinitionForIndexes(tableName, item.FieldDefinition())
// modify column.
case item.MODIFY_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.FieldDefinition() != nil:
- r.checkFieldDefinitionContext(tableName, item.FieldDefinition())
+ r.checkFieldDefinitionForIndexes(tableName, item.FieldDefinition())
default:
continue
}
}
+
+ // Track last line for this table
+ r.tableLines[tableName] = ctx.GetStart().GetLine()
}
-func (r *IndexTotalNumberLimitRule) checkFieldDefinitionContext(tableName string, ctx mysql.IFieldDefinitionContext) {
+// checkFieldDefinitionForIndexes checks if a field definition contains inline index definitions.
+func (r *IndexTotalNumberLimitRule) checkFieldDefinitionForIndexes(tableName string, ctx mysql.IFieldDefinitionContext) {
for _, attr := range ctx.AllColumnAttribute() {
if attr == nil || attr.GetValue() == nil {
continue
}
switch attr.GetValue().GetTokenType() {
- case mysql.MySQLParserPRIMARY_SYMBOL, mysql.MySQLParserUNIQUE_SYMBOL:
- r.lineForTable[tableName] = r.baseLine + ctx.GetStart().GetLine()
+ case mysql.MySQLParserPRIMARY_SYMBOL, mysql.MySQLParserUNIQUE_SYMBOL, mysql.MySQLParserKEY_SYMBOL:
+ // Generate a unique index name for inline index
+ indexName := fmt.Sprintf("__inline_index_%d__", len(r.tableIndexes[tableName]))
+ r.tableIndexes[tableName][indexName] = true
default:
}
}
}
-func (r *IndexTotalNumberLimitRule) checkTableConstraintDef(tableName string, ctx mysql.ITableConstraintDefContext) {
+// checkTableConstraintForIndexes checks table constraints for index definitions.
+func (r *IndexTotalNumberLimitRule) checkTableConstraintForIndexes(tableName string, ctx mysql.ITableConstraintDefContext) {
if ctx.GetType_() == nil {
return
}
switch ctx.GetType_().GetTokenType() {
case mysql.MySQLParserPRIMARY_SYMBOL, mysql.MySQLParserUNIQUE_SYMBOL, mysql.MySQLParserKEY_SYMBOL, mysql.MySQLParserINDEX_SYMBOL, mysql.MySQLParserFULLTEXT_SYMBOL:
- r.lineForTable[tableName] = r.baseLine + ctx.GetStart().GetLine()
+ // Add the index (use a generic name since we just need to count)
+ indexName := fmt.Sprintf("__index_%d__", len(r.tableIndexes[tableName]))
+ r.tableIndexes[tableName][indexName] = true
default:
}
}
+
+// checkAllTables checks all tables' index counts after processing all statements.
+func (r *IndexTotalNumberLimitRule) checkAllTables() {
+ for tableName, indexes := range r.tableIndexes {
+ // Get the number of indexes created in these statements
+ newIndexes := len(indexes)
+
+ // Get the number of indexes that already exist in catalog.Origin
+ existingIndexes := 0
+ if table := r.catalog.Origin.FindTable(&catalog.TableFind{TableName: tableName}); table != nil {
+ existingIndexes = table.CountIndex()
+ }
+
+ totalCount := existingIndexes + newIndexes
+ if totalCount > r.max {
+ line := r.tableLines[tableName]
+ r.AddAdvice(&storepb.Advice{
+ Status: r.level,
+ Code: advisor.IndexCountExceedsLimit.Int32(),
+ Title: r.title,
+ Content: fmt.Sprintf("The count of index in table `%s` should be no more than %d, but found %d", tableName, r.max, totalCount),
+ StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + line),
+ })
+ }
+ }
+}
diff --git a/backend/plugin/advisor/mysql/rule_table_text_fields_total_length.go b/backend/plugin/advisor/mysql/rule_table_text_fields_total_length.go
index 79c2badcb0..fc4dadc840 100644
--- a/backend/plugin/advisor/mysql/rule_table_text_fields_total_length.go
+++ b/backend/plugin/advisor/mysql/rule_table_text_fields_total_length.go
@@ -15,7 +15,6 @@ import (
"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/catalog"
mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
)
@@ -46,7 +45,7 @@ func (*TableMaximumVarcharLengthAdvisor) Check(_ context.Context, checkCtx advis
}
// Create the rule
- rule := NewTableTextFieldsTotalLengthRule(level, string(checkCtx.Rule.Type), checkCtx.Catalog, payload.Number)
+ rule := NewTableTextFieldsTotalLengthRule(level, string(checkCtx.Rule.Type), payload.Number)
// Create the generic checker with the rule
checker := NewGenericChecker([]Rule{rule})
@@ -60,22 +59,29 @@ func (*TableMaximumVarcharLengthAdvisor) Check(_ context.Context, checkCtx advis
return checker.GetAdviceList(), nil
}
+// tableColumn tracks column information for text length calculation.
+type tableColumn struct {
+ name string
+ columnType string
+}
+
// TableTextFieldsTotalLengthRule checks for table text fields total length.
type TableTextFieldsTotalLengthRule struct {
BaseRule
- catalog *catalog.Finder
maximum int
+ // tableColumns tracks columns for each table across all statements
+ tableColumns map[string]map[string]*tableColumn // tableName -> columnName -> column
}
// NewTableTextFieldsTotalLengthRule creates a new TableTextFieldsTotalLengthRule.
-func NewTableTextFieldsTotalLengthRule(level storepb.Advice_Status, title string, catalog *catalog.Finder, maximum int) *TableTextFieldsTotalLengthRule {
+func NewTableTextFieldsTotalLengthRule(level storepb.Advice_Status, title string, maximum int) *TableTextFieldsTotalLengthRule {
return &TableTextFieldsTotalLengthRule{
BaseRule: BaseRule{
level: level,
title: title,
},
- catalog: catalog,
- maximum: maximum,
+ maximum: maximum,
+ tableColumns: make(map[string]map[string]*tableColumn),
}
}
@@ -114,20 +120,33 @@ func (r *TableTextFieldsTotalLengthRule) checkCreateTable(ctx *mysql.CreateTable
if tableName == "" {
return
}
- tableInfo := r.catalog.Final.FindTable(&catalog.TableFind{TableName: tableName})
- if tableInfo == nil {
- return
+
+ // Initialize table columns map
+ if r.tableColumns[tableName] == nil {
+ r.tableColumns[tableName] = make(map[string]*tableColumn)
}
- total := getTotalTextLength(tableInfo)
- if total > int64(r.maximum) {
- r.AddAdvice(&storepb.Advice{
- Status: r.level,
- Code: advisor.IndexCountExceedsLimit.Int32(),
- Title: r.title,
- Content: fmt.Sprintf("Table %q total text column length (%d) exceeds the limit (%d).", tableName, total, r.maximum),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
- })
+
+ // Extract columns from CREATE TABLE statement
+ for _, tableElement := range ctx.TableElementList().AllTableElement() {
+ if tableElement.ColumnDefinition() == nil {
+ continue
+ }
+ columnDef := tableElement.ColumnDefinition()
+ if columnDef.ColumnName() == nil || columnDef.FieldDefinition() == nil || columnDef.FieldDefinition().DataType() == nil {
+ continue
+ }
+
+ _, _, columnName := mysqlparser.NormalizeMySQLColumnName(columnDef.ColumnName())
+ columnType := mysqlparser.NormalizeMySQLDataType(columnDef.FieldDefinition().DataType(), true /* compact */)
+
+ r.tableColumns[tableName][strings.ToLower(columnName)] = &tableColumn{
+ name: columnName,
+ columnType: columnType,
+ }
}
+
+ // Check total text length
+ r.checkTableTextLength(tableName, ctx.GetStart().GetLine())
}
func (r *TableTextFieldsTotalLengthRule) checkAlterTable(ctx *mysql.AlterTableContext) {
@@ -148,31 +167,99 @@ func (r *TableTextFieldsTotalLengthRule) checkAlterTable(ctx *mysql.AlterTableCo
if tableName == "" {
return
}
- tableInfo := r.catalog.Final.FindTable(&catalog.TableFind{TableName: tableName})
- if tableInfo == nil {
+
+ // Initialize table columns map if not exists
+ if r.tableColumns[tableName] == nil {
+ r.tableColumns[tableName] = make(map[string]*tableColumn)
+ }
+
+ // Process ALTER TABLE actions
+ for _, item := range ctx.AlterTableActions().AlterCommandList().AlterList().AllAlterListItem() {
+ if item == nil {
+ continue
+ }
+
+ switch {
+ // ADD COLUMN
+ case item.ADD_SYMBOL() != nil:
+ switch {
+ case item.Identifier() != nil && item.FieldDefinition() != nil && item.FieldDefinition().DataType() != nil:
+ columnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
+ columnType := mysqlparser.NormalizeMySQLDataType(item.FieldDefinition().DataType(), true /* compact */)
+ r.tableColumns[tableName][strings.ToLower(columnName)] = &tableColumn{
+ name: columnName,
+ columnType: columnType,
+ }
+ case item.OPEN_PAR_SYMBOL() != nil && item.TableElementList() != nil:
+ for _, tableElement := range item.TableElementList().AllTableElement() {
+ if tableElement.ColumnDefinition() == nil {
+ continue
+ }
+ columnDef := tableElement.ColumnDefinition()
+ if columnDef.ColumnName() == nil || columnDef.FieldDefinition() == nil || columnDef.FieldDefinition().DataType() == nil {
+ continue
+ }
+ _, _, columnName := mysqlparser.NormalizeMySQLColumnName(columnDef.ColumnName())
+ columnType := mysqlparser.NormalizeMySQLDataType(columnDef.FieldDefinition().DataType(), true /* compact */)
+ r.tableColumns[tableName][strings.ToLower(columnName)] = &tableColumn{
+ name: columnName,
+ columnType: columnType,
+ }
+ }
+ default:
+ // Other ADD variations not relevant for column tracking
+ }
+ // CHANGE COLUMN or MODIFY COLUMN
+ case item.CHANGE_SYMBOL() != nil && item.Identifier() != nil && item.FieldDefinition() != nil && item.FieldDefinition().DataType() != nil:
+ columnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
+ columnType := mysqlparser.NormalizeMySQLDataType(item.FieldDefinition().DataType(), true /* compact */)
+ r.tableColumns[tableName][strings.ToLower(columnName)] = &tableColumn{
+ name: columnName,
+ columnType: columnType,
+ }
+ case item.MODIFY_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.FieldDefinition() != nil && item.FieldDefinition().DataType() != nil:
+ columnName := mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())
+ columnType := mysqlparser.NormalizeMySQLDataType(item.FieldDefinition().DataType(), true /* compact */)
+ r.tableColumns[tableName][strings.ToLower(columnName)] = &tableColumn{
+ name: columnName,
+ columnType: columnType,
+ }
+ // DROP COLUMN
+ case item.DROP_SYMBOL() != nil && item.ColumnInternalRef() != nil:
+ columnName := mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())
+ delete(r.tableColumns[tableName], strings.ToLower(columnName))
+ default:
+ // Other ALTER TABLE commands not relevant for column tracking
+ }
+ }
+
+ // Check total text length after alterations
+ r.checkTableTextLength(tableName, ctx.GetStart().GetLine())
+}
+
+// checkTableTextLength calculates and checks the total text field length for a table.
+func (r *TableTextFieldsTotalLengthRule) checkTableTextLength(tableName string, line int) {
+ columns, exists := r.tableColumns[tableName]
+ if !exists {
return
}
- total := getTotalTextLength(tableInfo)
+
+ var total int64
+ for _, column := range columns {
+ total += getTextLength(column.columnType)
+ }
+
if total > int64(r.maximum) {
r.AddAdvice(&storepb.Advice{
Status: r.level,
Code: advisor.TotalTextLengthExceedsLimit.Int32(),
Title: r.title,
Content: fmt.Sprintf("Table %q total text column length (%d) exceeds the limit (%d).", tableName, total, r.maximum),
- StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
+ StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + line),
})
}
}
-func getTotalTextLength(tableInfo *catalog.TableState) int64 {
- var total int64
- columns := tableInfo.ListColumns()
- for _, column := range columns {
- total += getTextLength(column.Type())
- }
- return total
-}
-
func getTextLength(s string) int64 {
s = strings.ToLower(s)
switch s {
diff --git a/backend/plugin/advisor/pg/advisor_index_total_number_limit.go b/backend/plugin/advisor/pg/advisor_index_total_number_limit.go
index f5b4900a17..dd788cad5e 100644
--- a/backend/plugin/advisor/pg/advisor_index_total_number_limit.go
+++ b/backend/plugin/advisor/pg/advisor_index_total_number_limit.go
@@ -3,7 +3,6 @@ package pg
import (
"context"
"fmt"
- "slices"
"github.com/antlr4-go/antlr/v4"
@@ -43,47 +42,29 @@ func (*IndexTotalNumberLimitAdvisor) Check(_ context.Context, checkCtx advisor.C
}
rule := &indexTotalNumberLimitRule{
- BaseRule: BaseRule{level: level, title: string(checkCtx.Rule.Type)},
- max: payload.Number,
- catalog: checkCtx.Catalog,
- tableLine: make(tableLineMap),
+ BaseRule: BaseRule{level: level, title: string(checkCtx.Rule.Type)},
+ max: payload.Number,
+ tableIndexes: make(map[string]map[string]bool),
+ tableLines: make(map[string]int),
+ catalog: checkCtx.Catalog,
}
- if rule.catalog.Final.Usable() {
- checker := NewGenericChecker([]Rule{rule})
- antlr.ParseTreeWalkerDefault.Walk(checker, tree.Tree)
- rule.generateAdvice()
- return checker.GetAdviceList(), nil
- }
-
- return nil, nil
-}
-
-type tableLine struct {
- schema string
- table string
- line int
-}
+ checker := NewGenericChecker([]Rule{rule})
+ antlr.ParseTreeWalkerDefault.Walk(checker, tree.Tree)
-type tableLineMap map[string]tableLine
+ // Check all tables after processing all statements
+ rule.checkAllTables()
-func (m tableLineMap) set(schema string, table string, line int) {
- if schema == "" {
- schema = "public"
- }
- m[fmt.Sprintf("%q.%q", schema, table)] = tableLine{
- schema: schema,
- table: table,
- line: line,
- }
+ return checker.GetAdviceList(), nil
}
type indexTotalNumberLimitRule struct {
BaseRule
- max int
- catalog *catalog.Finder
- tableLine tableLineMap
+ max int
+ tableIndexes map[string]map[string]bool // tableKey -> indexName -> exists
+ tableLines map[string]int // tableKey -> last line number
+ catalog *catalog.Finder
}
func (*indexTotalNumberLimitRule) Name() string {
@@ -108,34 +89,33 @@ func (*indexTotalNumberLimitRule) OnExit(_ antlr.ParserRuleContext, _ string) er
return nil
}
-func (r *indexTotalNumberLimitRule) generateAdvice() {
- var tableList []tableLine
- for _, table := range r.tableLine {
- tableList = append(tableList, table)
- }
- slices.SortFunc(tableList, func(i, j tableLine) int {
- if i.line < j.line {
- return -1
+func (r *indexTotalNumberLimitRule) checkAllTables() {
+ for tableKey, indexes := range r.tableIndexes {
+ // Parse table key to get schema and table name
+ schema, table := parseTableKey(tableKey)
+
+ // Get the number of indexes created in these statements
+ newIndexes := len(indexes)
+
+ // Get the number of indexes that already exist in catalog.Origin
+ existingIndexes := 0
+ if tableInfo := r.catalog.Origin.FindTable(&catalog.TableFind{
+ SchemaName: schema,
+ TableName: table,
+ }); tableInfo != nil {
+ existingIndexes = tableInfo.CountIndex()
}
- if i.line > j.line {
- return 1
- }
- return 0
- })
-
- for _, table := range tableList {
- tableInfo := r.catalog.Final.FindTable(&catalog.TableFind{
- SchemaName: table.schema,
- TableName: table.table,
- })
- if tableInfo != nil && tableInfo.CountIndex() > r.max {
+
+ totalCount := existingIndexes + newIndexes
+ if totalCount > r.max {
+ line := r.tableLines[tableKey]
r.AddAdvice(&storepb.Advice{
Status: r.level,
Code: advisor.IndexCountExceedsLimit.Int32(),
Title: r.title,
- Content: fmt.Sprintf("The count of index in table %q.%q should be no more than %d, but found %d", table.schema, table.table, r.max, tableInfo.CountIndex()),
+ Content: fmt.Sprintf("The count of index in table %q.%q should be no more than %d, but found %d", schema, table, r.max, totalCount),
StartPosition: &storepb.Position{
- Line: int32(table.line),
+ Line: int32(line),
Column: 0,
},
})
@@ -159,6 +139,12 @@ func (r *indexTotalNumberLimitRule) handleCreatestmt(ctx *parser.CreatestmtConte
}
schemaName := extractSchemaName(qualifiedNames[0])
+ tableKey := makeTableKey(schemaName, tableName)
+
+ // Initialize table indexes map
+ if r.tableIndexes[tableKey] == nil {
+ r.tableIndexes[tableKey] = make(map[string]bool)
+ }
// Check if this CREATE TABLE statement creates any indexes
// (PRIMARY KEY or UNIQUE constraints)
@@ -168,18 +154,21 @@ func (r *indexTotalNumberLimitRule) handleCreatestmt(ctx *parser.CreatestmtConte
// Check column-level constraints
if elem.ColumnDef() != nil {
if hasIndexConstraint(elem.ColumnDef()) {
- r.tableLine.set(schemaName, tableName, ctx.GetStop().GetLine())
- return
+ indexName := fmt.Sprintf("__inline_index_%d__", len(r.tableIndexes[tableKey]))
+ r.tableIndexes[tableKey][indexName] = true
}
}
// Check table-level constraints
if elem.Tableconstraint() != nil && hasTableIndexConstraint(elem.Tableconstraint()) {
- r.tableLine.set(schemaName, tableName, ctx.GetStop().GetLine())
- return
+ indexName := fmt.Sprintf("__index_%d__", len(r.tableIndexes[tableKey]))
+ r.tableIndexes[tableKey][indexName] = true
}
}
}
+
+ // Track last line for this table
+ r.tableLines[makeTableKey(schemaName, tableName)] = ctx.GetStop().GetLine()
}
func (r *indexTotalNumberLimitRule) handleAltertablestmt(ctx *parser.AltertablestmtContext) {
@@ -197,6 +186,12 @@ func (r *indexTotalNumberLimitRule) handleAltertablestmt(ctx *parser.Altertables
}
schemaName := extractSchemaName(ctx.Relation_expr().Qualified_name())
+ tableKey := makeTableKey(schemaName, tableName)
+
+ // Initialize table indexes map
+ if r.tableIndexes[tableKey] == nil {
+ r.tableIndexes[tableKey] = make(map[string]bool)
+ }
// Check ALTER TABLE commands that create indexes
if ctx.Alter_table_cmds() != nil {
@@ -205,20 +200,23 @@ func (r *indexTotalNumberLimitRule) handleAltertablestmt(ctx *parser.Altertables
// ADD COLUMN with PRIMARY KEY or UNIQUE
if cmd.ADD_P() != nil && cmd.ColumnDef() != nil {
if hasIndexConstraint(cmd.ColumnDef()) {
- r.tableLine.set(schemaName, tableName, ctx.GetStop().GetLine())
- return
+ indexName := fmt.Sprintf("__inline_index_%d__", len(r.tableIndexes[tableKey]))
+ r.tableIndexes[tableKey][indexName] = true
}
}
// ADD CONSTRAINT (PRIMARY KEY or UNIQUE)
if cmd.ADD_P() != nil && cmd.Tableconstraint() != nil {
if hasTableIndexConstraint(cmd.Tableconstraint()) {
- r.tableLine.set(schemaName, tableName, ctx.GetStop().GetLine())
- return
+ indexName := fmt.Sprintf("__index_%d__", len(r.tableIndexes[tableKey]))
+ r.tableIndexes[tableKey][indexName] = true
}
}
}
}
+
+ // Track last line for this table
+ r.tableLines[makeTableKey(schemaName, tableName)] = ctx.GetStop().GetLine()
}
func (r *indexTotalNumberLimitRule) handleIndexstmt(ctx *parser.IndexstmtContext) {
@@ -236,7 +234,19 @@ func (r *indexTotalNumberLimitRule) handleIndexstmt(ctx *parser.IndexstmtContext
}
schemaName := extractSchemaName(ctx.Relation_expr().Qualified_name())
- r.tableLine.set(schemaName, tableName, ctx.GetStop().GetLine())
+ tableKey := makeTableKey(schemaName, tableName)
+
+ // Initialize table indexes map
+ if r.tableIndexes[tableKey] == nil {
+ r.tableIndexes[tableKey] = make(map[string]bool)
+ }
+
+ // Add the index
+ indexName := fmt.Sprintf("__index_%d__", len(r.tableIndexes[tableKey]))
+ r.tableIndexes[tableKey][indexName] = true
+
+ // Track last line for this table
+ r.tableLines[makeTableKey(schemaName, tableName)] = ctx.GetStop().GetLine()
}
// hasIndexConstraint checks if a column definition has PRIMARY KEY or UNIQUE constraint
diff --git a/backend/plugin/advisor/pg/advisor_table_require_pk.go b/backend/plugin/advisor/pg/advisor_table_require_pk.go
index 7bca05a484..407e7fad01 100644
--- a/backend/plugin/advisor/pg/advisor_table_require_pk.go
+++ b/backend/plugin/advisor/pg/advisor_table_require_pk.go
@@ -44,22 +44,24 @@ func (*TableRequirePKAdvisor) Check(_ context.Context, checkCtx advisor.Context)
title: string(checkCtx.Rule.Type),
},
statementsText: checkCtx.Statements,
+ tableState: make(map[string]*tableState),
catalog: checkCtx.Catalog,
- tableMentions: make(map[string]*tableMention),
}
checker := NewGenericChecker([]Rule{rule})
antlr.ParseTreeWalkerDefault.Walk(checker, tree.Tree)
- // Simple Solution: Validate final state after walking all statements
+ // Validate final state after walking all statements
rule.validateFinalState()
return checker.GetAdviceList(), nil
}
-type tableMention struct {
- startLine int
- endLine int
+type tableState struct {
+ hasPK bool
+ pkConstraint string // Name of the PK constraint (if known)
+ startLine int
+ endLine int
}
type tableRequirePKRule struct {
@@ -67,8 +69,8 @@ type tableRequirePKRule struct {
statementsText string
catalog *catalog.Finder
- // Simple Solution: Track last mention of each table
- tableMentions map[string]*tableMention // key: "schema.table", value: last mention info
+ // Track table state: whether they have a primary key
+ tableState map[string]*tableState // key: "schema.table", value: state
}
// Name returns the rule name.
@@ -96,7 +98,7 @@ func (*tableRequirePKRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
return nil
}
-// handleCreatestmt records CREATE TABLE statements (Simple Solution)
+// handleCreatestmt records CREATE TABLE statements
func (r *tableRequirePKRule) handleCreatestmt(ctx *parser.CreatestmtContext) {
if !isTopLevel(ctx.GetParent()) {
return
@@ -112,15 +114,45 @@ func (r *tableRequirePKRule) handleCreatestmt(ctx *parser.CreatestmtContext) {
}
}
- // Simple Solution: Just record the table mention (ALWAYS update for last occurrence)
- key := fmt.Sprintf("%s.%s", schemaName, tableName)
- r.tableMentions[key] = &tableMention{
- startLine: ctx.GetStart().GetLine(),
- endLine: ctx.GetStop().GetLine(),
+ key := makeTableKey(schemaName, tableName)
+
+ // Check if this CREATE TABLE has a PRIMARY KEY
+ hasPK := false
+ pkConstraint := ""
+ if ctx.Opttableelementlist() != nil && ctx.Opttableelementlist().Tableelementlist() != nil {
+ allElements := ctx.Opttableelementlist().Tableelementlist().AllTableelement()
+ for _, elem := range allElements {
+ // Check column-level PRIMARY KEY
+ if elem.ColumnDef() != nil && hasPrimaryKeyConstraint(elem.ColumnDef()) {
+ hasPK = true
+ // PostgreSQL generates constraint name as tablename_pkey
+ pkConstraint = getDefaultPKConstraintName(tableName)
+ break
+ }
+
+ // Check table-level PRIMARY KEY
+ if elem.Tableconstraint() != nil && isTablePrimaryKey(elem.Tableconstraint()) {
+ hasPK = true
+ // Try to get explicit constraint name, or use default
+ if elem.Tableconstraint().Name() != nil && elem.Tableconstraint().Name().Colid() != nil {
+ pkConstraint = pgparser.NormalizePostgreSQLColid(elem.Tableconstraint().Name().Colid())
+ } else {
+ pkConstraint = getDefaultPKConstraintName(tableName)
+ }
+ break
+ }
+ }
+ }
+
+ r.tableState[key] = &tableState{
+ hasPK: hasPK,
+ pkConstraint: pkConstraint,
+ startLine: ctx.GetStart().GetLine(),
+ endLine: ctx.GetStop().GetLine(),
}
}
-// handleAltertablestmt records ALTER TABLE statements (Simple Solution)
+// handleAltertablestmt records ALTER TABLE statements
func (r *tableRequirePKRule) handleAltertablestmt(ctx *parser.AltertablestmtContext) {
if !isTopLevel(ctx.GetParent()) {
return
@@ -135,12 +167,84 @@ func (r *tableRequirePKRule) handleAltertablestmt(ctx *parser.AltertablestmtCont
}
}
- // Simple Solution: Just record the table mention (ALWAYS update for last occurrence)
- key := fmt.Sprintf("%s.%s", schemaName, tableName)
- r.tableMentions[key] = &tableMention{
- startLine: ctx.GetStart().GetLine(),
- endLine: ctx.GetStop().GetLine(),
+ key := makeTableKey(schemaName, tableName)
+
+ // Check if we're dropping a PRIMARY KEY (either via DROP CONSTRAINT or DROP COLUMN)
+ droppedPK := false
+ if ctx.Alter_table_cmds() != nil {
+ allCmds := ctx.Alter_table_cmds().AllAlter_table_cmd()
+ for _, cmd := range allCmds {
+ if r.isDropPKConstraint(cmd, key, schemaName, tableName) {
+ droppedPK = true
+ break
+ }
+ if r.isDropPKColumn(cmd, schemaName, tableName) {
+ droppedPK = true
+ break
+ }
+ }
}
+
+ // Check if we're already tracking this table (from CREATE TABLE)
+ // or if we just detected we're dropping the PK
+ if r.tableState[key] == nil && !droppedPK {
+ // Not tracking this table and not dropping PK, so nothing to do
+ return
+ }
+
+ // Track that we're modifying this table
+ if r.tableState[key] == nil {
+ r.tableState[key] = &tableState{
+ hasPK: false,
+ startLine: ctx.GetStart().GetLine(),
+ endLine: ctx.GetStop().GetLine(),
+ }
+ }
+
+ // If we dropped the PK, mark it and update the line to point to this ALTER statement
+ if droppedPK {
+ r.tableState[key].hasPK = false
+ r.tableState[key].startLine = ctx.GetStart().GetLine()
+ }
+
+ // Check if ALTER TABLE adds a PRIMARY KEY
+ addedPK := false
+ addedPKConstraint := ""
+ if ctx.Alter_table_cmds() != nil {
+ allCmds := ctx.Alter_table_cmds().AllAlter_table_cmd()
+ for _, cmd := range allCmds {
+ // ADD COLUMN with PRIMARY KEY
+ if cmd.ADD_P() != nil && cmd.ColumnDef() != nil {
+ if hasPrimaryKeyConstraint(cmd.ColumnDef()) {
+ addedPK = true
+ addedPKConstraint = tableName + "_pkey"
+ break
+ }
+ }
+
+ // ADD CONSTRAINT PRIMARY KEY
+ if cmd.ADD_P() != nil && cmd.Tableconstraint() != nil {
+ if isTablePrimaryKey(cmd.Tableconstraint()) {
+ addedPK = true
+ // Try to get explicit constraint name, or use default
+ if cmd.Tableconstraint().Name() != nil && cmd.Tableconstraint().Name().Colid() != nil {
+ addedPKConstraint = pgparser.NormalizePostgreSQLColid(cmd.Tableconstraint().Name().Colid())
+ } else {
+ addedPKConstraint = tableName + "_pkey"
+ }
+ break
+ }
+ }
+ }
+ }
+
+ if addedPK {
+ r.tableState[key].hasPK = true
+ r.tableState[key].pkConstraint = addedPKConstraint
+ }
+
+ // Update end line
+ r.tableState[key].endLine = ctx.GetStop().GetLine()
}
// handleDropstmt handles DROP TABLE - remove from tracking
@@ -154,9 +258,7 @@ func (r *tableRequirePKRule) handleDropstmt(ctx *parser.DropstmtContext) {
return
}
- // Remove all dropped tables from tracking (simplified - best effort)
- // Note: For the Simple Solution, not handling DROP TABLE perfectly is acceptable
- // since dropped tables won't be in catalog.Final anyway
+ // Remove all dropped tables from tracking
if ctx.Any_name_list() != nil {
allNames := ctx.Any_name_list().AllAny_name()
for _, anyName := range allNames {
@@ -164,31 +266,26 @@ func (r *tableRequirePKRule) handleDropstmt(ctx *parser.DropstmtContext) {
// Simple table name (most common case)
name := pgparser.NormalizePostgreSQLColid(anyName.Colid())
key := fmt.Sprintf("public.%s", name)
- delete(r.tableMentions, key)
+ delete(r.tableState, key)
}
- // For qualified names, we skip for simplicity - they won't cause false positives
- // because catalog.Final won't have dropped tables
+ // For qualified names, we skip for simplicity
}
}
}
-// validateFinalState checks all mentioned tables against catalog.Final (Simple Solution)
+// validateFinalState checks all tables for PRIMARY KEY
func (r *tableRequirePKRule) validateFinalState() {
- for tableKey, mention := range r.tableMentions {
+ for tableKey, state := range r.tableState {
// Parse table key: "schema.table"
schemaName, tableName := parseTableKey(tableKey)
- // Check catalog.Final for PRIMARY KEY
- hasPK := r.catalog.Final.HasPrimaryKey(&catalog.PrimaryKeyFind{
- SchemaName: schemaName,
- TableName: tableName,
- })
-
- if !hasPK {
+ // Only report error if we didn't add a PK in the statements
+ // (Tables we CREATE without PK, or tables we ALTER without adding PK are reported)
+ if !state.hasPK {
content := fmt.Sprintf("Table %q.%q requires PRIMARY KEY", schemaName, tableName)
// Extract and include the related statement
- statement := extractStatementText(r.statementsText, mention.startLine, mention.endLine)
+ statement := extractStatementText(r.statementsText, state.startLine, state.endLine)
if statement != "" {
content = fmt.Sprintf("%s, related statement: %q", content, statement)
}
@@ -199,7 +296,7 @@ func (r *tableRequirePKRule) validateFinalState() {
Title: r.title,
Content: content,
StartPosition: &storepb.Position{
- Line: int32(mention.startLine),
+ Line: int32(state.startLine),
Column: 0,
},
})
@@ -207,13 +304,136 @@ func (r *tableRequirePKRule) validateFinalState() {
}
}
-// parseTableKey splits "schema.table" into schema and table name
-func parseTableKey(key string) (string, string) {
- // Simple split on first dot
- for i := 0; i < len(key); i++ {
- if key[i] == '.' {
- return key[:i], key[i+1:]
+// getConstraintName extracts the constraint name from an ALTER TABLE command.
+// Returns empty string if no constraint name found.
+func getConstraintName(cmd parser.IAlter_table_cmdContext) string {
+ if cmd.Name() != nil && cmd.Name().Colid() != nil {
+ return pgparser.NormalizePostgreSQLColid(cmd.Name().Colid())
+ }
+ allColids := cmd.AllColid()
+ if len(allColids) > 0 {
+ return pgparser.NormalizePostgreSQLColid(allColids[0])
+ }
+ return ""
+}
+
+// isPKConstraintInCatalog checks if the given constraint name is a primary key in catalog.Origin.
+func (r *tableRequirePKRule) isPKConstraintInCatalog(schemaName, tableName, constraintName string) bool {
+ tableInfo := r.catalog.Origin.FindTable(&catalog.TableFind{
+ SchemaName: schemaName,
+ TableName: tableName,
+ })
+ if tableInfo == nil {
+ return false
+ }
+
+ indexSet := tableInfo.Index(nil)
+ if indexSet == nil {
+ return false
+ }
+
+ for indexName, indexState := range *indexSet {
+ if indexState.Primary() && indexName == constraintName {
+ return true
+ }
+ }
+ return false
+}
+
+// isColumnInPK checks if a column is part of the primary key in catalog.Origin.
+func (r *tableRequirePKRule) isColumnInPK(schemaName, tableName, columnName string) bool {
+ tableInfo := r.catalog.Origin.FindTable(&catalog.TableFind{
+ SchemaName: schemaName,
+ TableName: tableName,
+ })
+ if tableInfo == nil {
+ return false
+ }
+
+ indexSet := tableInfo.Index(nil)
+ if indexSet == nil {
+ return false
+ }
+
+ for _, indexState := range *indexSet {
+ if indexState.Primary() {
+ for _, expr := range indexState.ExpressionList() {
+ if expr == columnName {
+ return true
+ }
+ }
}
}
- return "public", key
+ return false
+}
+
+// isDropPKConstraint checks if the ALTER TABLE command drops a primary key constraint.
+func (r *tableRequirePKRule) isDropPKConstraint(cmd parser.IAlter_table_cmdContext, key, schemaName, tableName string) bool {
+ if cmd.DROP() == nil || cmd.CONSTRAINT() == nil {
+ return false
+ }
+
+ constraintName := getConstraintName(cmd)
+ if constraintName == "" {
+ return false
+ }
+
+ // First check if this table is being tracked and has a known PK constraint
+ if r.tableState[key] != nil && r.tableState[key].pkConstraint == constraintName {
+ return true
+ }
+
+ // Check if this constraint is the primary key in catalog.Origin
+ return r.isPKConstraintInCatalog(schemaName, tableName, constraintName)
+}
+
+// isDropPKColumn checks if the ALTER TABLE command drops a column that's part of the primary key.
+func (r *tableRequirePKRule) isDropPKColumn(cmd parser.IAlter_table_cmdContext, schemaName, tableName string) bool {
+ if cmd.DROP() == nil || cmd.CONSTRAINT() != nil {
+ return false
+ }
+
+ allColids := cmd.AllColid()
+ if len(allColids) == 0 {
+ return false
+ }
+
+ columnName := pgparser.NormalizePostgreSQLColid(allColids[0])
+ return r.isColumnInPK(schemaName, tableName, columnName)
+}
+
+// hasPrimaryKeyConstraint checks if a column definition has PRIMARY KEY constraint
+func hasPrimaryKeyConstraint(colDef parser.IColumnDefContext) bool {
+ if colDef.Colquallist() == nil {
+ return false
+ }
+
+ allConstraints := colDef.Colquallist().AllColconstraint()
+ for _, constraint := range allConstraints {
+ if constraint.Colconstraintelem() != nil {
+ elem := constraint.Colconstraintelem()
+ // PRIMARY KEY
+ if elem.PRIMARY() != nil && elem.KEY() != nil {
+ return true
+ }
+ }
+ }
+
+ return false
+}
+
+// isTablePrimaryKey checks if a table constraint is PRIMARY KEY
+func isTablePrimaryKey(constraint parser.ITableconstraintContext) bool {
+ if constraint == nil || constraint.Constraintelem() == nil {
+ return false
+ }
+
+ elem := constraint.Constraintelem()
+
+ // PRIMARY KEY
+ if elem.PRIMARY() != nil && elem.KEY() != nil {
+ return true
+ }
+
+ return false
}
diff --git a/backend/plugin/advisor/pg/test_helper.go b/backend/plugin/advisor/pg/test_helper.go
index a42d595088..aa2f67e635 100644
--- a/backend/plugin/advisor/pg/test_helper.go
+++ b/backend/plugin/advisor/pg/test_helper.go
@@ -74,12 +74,6 @@ func RunANTLRAdvisorRuleTest(t *testing.T, rule advisor.SQLReviewRuleType, dbTyp
tree, err := pg.ParsePostgreSQL(tc.Statement)
require.NoError(t, err, "Failed to parse SQL: %s", tc.Statement)
- // Use ANTLR tree for catalog WalkThrough
- if needMetaData {
- err = finder.WalkThrough(tree)
- require.NoError(t, err, "Failed to walk through catalog: %s", tc.Statement)
- }
-
ruleList := []*storepb.SQLReviewRule{
{
Type: string(rule),
diff --git a/backend/plugin/advisor/pg/utils.go b/backend/plugin/advisor/pg/utils.go
index d2dba4d134..37f16c8a6b 100644
--- a/backend/plugin/advisor/pg/utils.go
+++ b/backend/plugin/advisor/pg/utils.go
@@ -139,3 +139,65 @@ func normalizeSchemaName(schemaName string) string {
}
return schemaName
}
+
+// makeTableKey creates a table key in format "schema"."table" for use in tracking maps.
+// This uses %q formatting to properly quote schema and table names.
+func makeTableKey(schema, table string) string {
+ if schema == "" {
+ schema = "public"
+ }
+ return strings.Join([]string{strconv.Quote(schema), strconv.Quote(table)}, ".")
+}
+
+// parseTableKey parses a table key in format "schema"."table" or "schema.table" back to schema and table.
+// Returns (schema, table). Defaults to "public" schema if only table name is present.
+func parseTableKey(key string) (string, string) {
+ // Handle quoted format: "schema"."table"
+ if strings.Contains(key, `"`) {
+ parts := make([]string, 0, 2)
+ start := 0
+ inQuote := false
+
+ for i := 0; i < len(key); i++ {
+ if key[i] == '"' {
+ inQuote = !inQuote
+ } else if key[i] == '.' && !inQuote {
+ // Remove quotes from the part
+ part := key[start:i]
+ if len(part) > 0 && part[0] == '"' && part[len(part)-1] == '"' {
+ part = part[1 : len(part)-1]
+ }
+ parts = append(parts, part)
+ start = i + 1
+ }
+ }
+
+ // Add last part
+ if start < len(key) {
+ part := key[start:]
+ if len(part) > 0 && part[0] == '"' && part[len(part)-1] == '"' {
+ part = part[1 : len(part)-1]
+ }
+ parts = append(parts, part)
+ }
+
+ if len(parts) == 2 {
+ return parts[0], parts[1]
+ }
+ return "public", key
+ }
+
+ // Handle simple format: schema.table
+ for i := 0; i < len(key); i++ {
+ if key[i] == '.' {
+ return key[:i], key[i+1:]
+ }
+ }
+ return "public", key
+}
+
+// getDefaultPKConstraintName returns PostgreSQL's default primary key constraint name.
+// PostgreSQL uses the convention: tablename_pkey
+func getDefaultPKConstraintName(tableName string) string {
+ return tableName + "_pkey"
+}
diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.go
index 4a9b301ed0..7aec91288c 100644
--- a/backend/plugin/advisor/sql_review.go
+++ b/backend/plugin/advisor/sql_review.go
@@ -4,7 +4,6 @@ import (
"context"
"database/sql"
"encoding/json"
- "fmt"
"regexp"
"github.com/pkg/errors"
@@ -524,8 +523,6 @@ func SQLReviewCheck(
) ([]*storepb.Advice, error) {
asts, parseResult := sm.GetASTsForChecks(checkContext.DBType, statements)
- builtinOnly := len(ruleList) == 0
-
if !checkContext.NoAppendBuiltin {
// Append builtin rules to the rule list.
ruleList = append(ruleList, GetBuiltinRules(checkContext.DBType)...)
@@ -536,16 +533,6 @@ func SQLReviewCheck(
}
finder := checkContext.Catalog.GetFinder()
- if !builtinOnly {
- switch checkContext.DBType {
- case storepb.Engine_TIDB, storepb.Engine_MYSQL, storepb.Engine_MARIADB, storepb.Engine_POSTGRES, storepb.Engine_OCEANBASE:
- if err := finder.WalkThrough(asts); err != nil {
- return convertWalkThroughErrorToAdvice(err)
- }
- default:
- // Other database types don't need walkthrough
- }
- }
var errorAdvices, warningAdvices []*storepb.Advice
for _, rule := range ruleList {
@@ -604,193 +591,3 @@ func SQLReviewCheck(
advices = append(advices, warningAdvices...)
return advices, nil
}
-
-func convertWalkThroughErrorToAdvice(err error) ([]*storepb.Advice, error) {
- walkThroughError, ok := err.(*catalog.WalkThroughError)
- if !ok {
- return nil, err
- }
-
- var res []*storepb.Advice
- switch walkThroughError.Type {
- case catalog.ErrorTypeUnsupported:
- res = append(res, &storepb.Advice{
- Status: storepb.Advice_ERROR,
- Code: Unsupported.Int32(),
- Title: walkThroughError.Content,
- Content: "",
- StartPosition: common.ConvertANTLRLineToPosition(walkThroughError.Line),
- })
- case catalog.ErrorTypeParseError:
- res = append(res, &storepb.Advice{
- Status: storepb.Advice_ERROR,
- Code: StatementSyntaxError.Int32(),
- Title: SyntaxErrorTitle,
- Content: walkThroughError.Content,
- })
- case catalog.ErrorTypeDeparseError:
- res = append(res, &storepb.Advice{
- Status: storepb.Advice_ERROR,
- Code: Internal.Int32(),
- Title: "Internal error for walk-through",
- Content: walkThroughError.Content,
- StartPosition: common.ConvertANTLRLineToPosition(walkThroughError.Line),
- })
- case catalog.ErrorTypeAccessOtherDatabase:
- res = append(res, &storepb.Advice{
- Status: storepb.Advice_ERROR,
- Code: NotCurrentDatabase.Int32(),
- Title: "Access other database",
- Content: walkThroughError.Content,
- StartPosition: common.ConvertANTLRLineToPosition(walkThroughError.Line),
- })
- case catalog.ErrorTypeDatabaseIsDeleted:
- res = append(res, &storepb.Advice{
- Status: storepb.Advice_ERROR,
- Code: DatabaseIsDeleted.Int32(),
- Title: "Access deleted database",
- Content: walkThroughError.Content,
- StartPosition: common.ConvertANTLRLineToPosition(walkThroughError.Line),
- })
- case catalog.ErrorTypeReferenceOtherDatabase:
- res = append(res, &storepb.Advice{
- Status: storepb.Advice_WARNING,
- Code: ReferenceOtherDatabase.Int32(),
- Title: "Reference other database",
- Content: walkThroughError.Content,
- StartPosition: common.ConvertANTLRLineToPosition(walkThroughError.Line),
- })
- case catalog.ErrorTypeTableExists:
- res = append(res, &storepb.Advice{
- Status: storepb.Advice_ERROR,
- Code: TableExists.Int32(),
- Title: "Table already exists",
- Content: walkThroughError.Content,
- StartPosition: common.ConvertANTLRLineToPosition(walkThroughError.Line),
- })
- case catalog.ErrorTypeTableNotExists:
- res = append(res, &storepb.Advice{
- Status: storepb.Advice_ERROR,
- Code: TableNotExists.Int32(),
- Title: "Table does not exist",
- Content: walkThroughError.Content,
- StartPosition: common.ConvertANTLRLineToPosition(walkThroughError.Line),
- })
- case catalog.ErrorTypeColumnExists:
- res = append(res, &storepb.Advice{
- Status: storepb.Advice_ERROR,
- Code: ColumnExists.Int32(),
- Title: "Column already exists",
- Content: walkThroughError.Content,
- StartPosition: common.ConvertANTLRLineToPosition(walkThroughError.Line),
- })
- case catalog.ErrorTypeColumnNotExists:
- res = append(res, &storepb.Advice{
- Status: storepb.Advice_ERROR,
- Code: ColumnNotExists.Int32(),
- Title: "Column does not exist",
- Content: walkThroughError.Content,
- StartPosition: common.ConvertANTLRLineToPosition(walkThroughError.Line),
- })
- case catalog.ErrorTypeDropAllColumns:
- res = append(res, &storepb.Advice{
- Status: storepb.Advice_ERROR,
- Code: DropAllColumns.Int32(),
- Title: "Drop all columns",
- Content: walkThroughError.Content,
- StartPosition: common.ConvertANTLRLineToPosition(walkThroughError.Line),
- })
- case catalog.ErrorTypePrimaryKeyExists:
- res = append(res, &storepb.Advice{
- Status: storepb.Advice_ERROR,
- Code: PrimaryKeyExists.Int32(),
- Title: "Primary key exists",
- Content: walkThroughError.Content,
- StartPosition: common.ConvertANTLRLineToPosition(walkThroughError.Line),
- })
- case catalog.ErrorTypeIndexExists:
- res = append(res, &storepb.Advice{
- Status: storepb.Advice_ERROR,
- Code: IndexExists.Int32(),
- Title: "Index exists",
- Content: walkThroughError.Content,
- StartPosition: common.ConvertANTLRLineToPosition(walkThroughError.Line),
- })
- case catalog.ErrorTypeIndexEmptyKeys:
- res = append(res, &storepb.Advice{
- Status: storepb.Advice_ERROR,
- Code: IndexEmptyKeys.Int32(),
- Title: "Index empty keys",
- Content: walkThroughError.Content,
- StartPosition: common.ConvertANTLRLineToPosition(walkThroughError.Line),
- })
- case catalog.ErrorTypePrimaryKeyNotExists:
- res = append(res, &storepb.Advice{
- Status: storepb.Advice_ERROR,
- Code: PrimaryKeyNotExists.Int32(),
- Title: "Primary key does not exist",
- Content: walkThroughError.Content,
- StartPosition: common.ConvertANTLRLineToPosition(walkThroughError.Line),
- })
- case catalog.ErrorTypeIndexNotExists:
- res = append(res, &storepb.Advice{
- Status: storepb.Advice_ERROR,
- Code: IndexNotExists.Int32(),
- Title: "Index does not exist",
- Content: walkThroughError.Content,
- StartPosition: common.ConvertANTLRLineToPosition(walkThroughError.Line),
- })
- case catalog.ErrorTypeIncorrectIndexName:
- res = append(res, &storepb.Advice{
- Status: storepb.Advice_ERROR,
- Code: IncorrectIndexName.Int32(),
- Title: "Incorrect index name",
- Content: walkThroughError.Content,
- StartPosition: common.ConvertANTLRLineToPosition(walkThroughError.Line),
- })
- case catalog.ErrorTypeSpatialIndexKeyNullable:
- res = append(res, &storepb.Advice{
- Status: storepb.Advice_ERROR,
- Code: SpatialIndexKeyNullable.Int32(),
- Title: "Spatial index key must be NOT NULL",
- Content: walkThroughError.Content,
- StartPosition: common.ConvertANTLRLineToPosition(walkThroughError.Line),
- })
- case catalog.ErrorTypeColumnIsReferencedByView:
- details := walkThroughError.Content
- res = append(res, &storepb.Advice{
- Status: storepb.Advice_ERROR,
- Code: ColumnIsReferencedByView.Int32(),
- Title: "Column is referenced by view",
- Content: details,
- StartPosition: common.ConvertANTLRLineToPosition(walkThroughError.Line),
- })
- case catalog.ErrorTypeTableIsReferencedByView:
- details := walkThroughError.Content
- res = append(res, &storepb.Advice{
- Status: storepb.Advice_ERROR,
- Code: TableIsReferencedByView.Int32(),
- Title: "Table is referenced by view",
- Content: details,
- StartPosition: common.ConvertANTLRLineToPosition(walkThroughError.Line),
- })
- case catalog.ErrorTypeInvalidColumnTypeForDefaultValue:
- res = append(res, &storepb.Advice{
- Status: storepb.Advice_ERROR,
- Code: InvalidColumnDefault.Int32(),
- Title: "Invalid column default value",
- Content: walkThroughError.Content,
- StartPosition: common.ConvertANTLRLineToPosition(walkThroughError.Line),
- })
- default:
- res = append(res, &storepb.Advice{
- Status: storepb.Advice_ERROR,
- Code: Internal.Int32(),
- Title: fmt.Sprintf("Failed to walk-through with code %d", walkThroughError.Type),
- Content: walkThroughError.Content,
- StartPosition: common.ConvertANTLRLineToPosition(walkThroughError.Line),
- })
- }
-
- return res, nil
-}
diff --git a/backend/plugin/advisor/tidb/advisor_column_no_null.go b/backend/plugin/advisor/tidb/advisor_column_no_null.go
index 3c9b73f538..233ed82bda 100644
--- a/backend/plugin/advisor/tidb/advisor_column_no_null.go
+++ b/backend/plugin/advisor/tidb/advisor_column_no_null.go
@@ -3,7 +3,6 @@ package tidb
import (
"context"
"fmt"
- "slices"
"github.com/pingcap/tidb/pkg/parser/ast"
"github.com/pkg/errors"
@@ -11,7 +10,6 @@ import (
"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/catalog"
)
var (
@@ -39,75 +37,21 @@ func (*ColumnNoNullAdvisor) Check(_ context.Context, checkCtx advisor.Context) (
return nil, err
}
checker := &columnNoNullChecker{
- level: level,
- title: string(checkCtx.Rule.Type),
- columnSet: make(map[string]columnName),
- catalog: checkCtx.Catalog,
+ level: level,
+ title: string(checkCtx.Rule.Type),
}
for _, stmtNode := range root {
(stmtNode).Accept(checker)
}
- return checker.generateAdvice(), nil
+ return checker.adviceList, nil
}
type columnNoNullChecker struct {
adviceList []*storepb.Advice
level storepb.Advice_Status
title string
- columnSet map[string]columnName
- catalog *catalog.Finder
-}
-
-func (checker *columnNoNullChecker) generateAdvice() []*storepb.Advice {
- var columnList []columnName
- for _, column := range checker.columnSet {
- columnList = append(columnList, column)
- }
- slices.SortFunc(columnList, func(i, j columnName) int {
- if i.line != j.line {
- if i.line < j.line {
- return -1
- }
- return 1
- }
- if i.columnName < j.columnName {
- return -1
- }
- if i.columnName > j.columnName {
- return 1
- }
- return 0
- })
-
- for _, column := range columnList {
- col := checker.catalog.Final.FindColumn(&catalog.ColumnFind{
- TableName: column.tableName,
- ColumnName: column.columnName,
- })
- if col != nil && col.Nullable() {
- checker.adviceList = append(checker.adviceList, &storepb.Advice{
- Status: checker.level,
- Code: advisor.ColumnCannotNull.Int32(),
- Title: checker.title,
- Content: fmt.Sprintf("`%s`.`%s` cannot have NULL value", column.tableName, column.columnName),
- StartPosition: common.ConvertANTLRLineToPosition(column.line),
- })
- }
- }
-
- return checker.adviceList
-}
-
-type columnName struct {
- tableName string
- columnName string
- line int
-}
-
-func (c columnName) name() string {
- return fmt.Sprintf("%s.%s", c.tableName, c.columnName)
}
// Enter implements the ast.Visitor interface.
@@ -116,13 +60,14 @@ func (checker *columnNoNullChecker) Enter(in ast.Node) (ast.Node, bool) {
// CREATE TABLE
case *ast.CreateTableStmt:
for _, column := range node.Cols {
- col := columnName{
- tableName: node.Table.Name.O,
- columnName: column.Name.Name.O,
- line: column.OriginTextPosition(),
- }
- if _, exists := checker.columnSet[col.name()]; !exists {
- checker.columnSet[col.name()] = col
+ if canNull(column) {
+ checker.adviceList = append(checker.adviceList, &storepb.Advice{
+ Status: checker.level,
+ Code: advisor.ColumnCannotNull.Int32(),
+ Title: checker.title,
+ Content: fmt.Sprintf("`%s`.`%s` cannot have NULL value", node.Table.Name.O, column.Name.Name.O),
+ StartPosition: common.ConvertANTLRLineToPosition(column.OriginTextPosition()),
+ })
}
}
// ALTER TABLE
@@ -132,24 +77,26 @@ func (checker *columnNoNullChecker) Enter(in ast.Node) (ast.Node, bool) {
// ADD COLUMNS
case ast.AlterTableAddColumns:
for _, column := range spec.NewColumns {
- col := columnName{
- tableName: node.Table.Name.O,
- columnName: column.Name.Name.O,
- line: node.OriginTextPosition(),
- }
- if _, exists := checker.columnSet[col.name()]; !exists {
- checker.columnSet[col.name()] = col
+ if canNull(column) {
+ checker.adviceList = append(checker.adviceList, &storepb.Advice{
+ Status: checker.level,
+ Code: advisor.ColumnCannotNull.Int32(),
+ Title: checker.title,
+ Content: fmt.Sprintf("`%s`.`%s` cannot have NULL value", node.Table.Name.O, column.Name.Name.O),
+ StartPosition: common.ConvertANTLRLineToPosition(node.OriginTextPosition()),
+ })
}
}
// CHANGE COLUMN
case ast.AlterTableChangeColumn:
- col := columnName{
- tableName: node.Table.Name.O,
- columnName: spec.NewColumns[0].Name.Name.O,
- line: node.OriginTextPosition(),
- }
- if _, exists := checker.columnSet[col.name()]; !exists {
- checker.columnSet[col.name()] = col
+ if len(spec.NewColumns) > 0 && canNull(spec.NewColumns[0]) {
+ checker.adviceList = append(checker.adviceList, &storepb.Advice{
+ Status: checker.level,
+ Code: advisor.ColumnCannotNull.Int32(),
+ Title: checker.title,
+ Content: fmt.Sprintf("`%s`.`%s` cannot have NULL value", node.Table.Name.O, spec.NewColumns[0].Name.Name.O),
+ StartPosition: common.ConvertANTLRLineToPosition(node.OriginTextPosition()),
+ })
}
default:
// Skip other alter table specification types
diff --git a/backend/plugin/advisor/tidb/advisor_column_set_default_for_not_null.go b/backend/plugin/advisor/tidb/advisor_column_set_default_for_not_null.go
index 90273c8687..8c09a1e3dc 100644
--- a/backend/plugin/advisor/tidb/advisor_column_set_default_for_not_null.go
+++ b/backend/plugin/advisor/tidb/advisor_column_set_default_for_not_null.go
@@ -58,6 +58,12 @@ type columnSetDefaultForNotNullChecker struct {
text string
}
+type columnName struct {
+ tableName string
+ columnName string
+ line int
+}
+
// Enter implements the ast.Visitor interface.
func (checker *columnSetDefaultForNotNullChecker) Enter(in ast.Node) (ast.Node, bool) {
var notNullColumnWithNoDefault []columnName
diff --git a/backend/plugin/advisor/tidb/advisor_index_total_number_limit.go b/backend/plugin/advisor/tidb/advisor_index_total_number_limit.go
index 3b48796d09..f682eab094 100644
--- a/backend/plugin/advisor/tidb/advisor_index_total_number_limit.go
+++ b/backend/plugin/advisor/tidb/advisor_index_total_number_limit.go
@@ -5,7 +5,6 @@ package tidb
import (
"context"
"fmt"
- "slices"
"github.com/pingcap/tidb/pkg/parser/ast"
"github.com/pkg/errors"
@@ -48,7 +47,8 @@ func (*IndexTotalNumberLimitAdvisor) Check(_ context.Context, checkCtx advisor.C
level: level,
title: string(checkCtx.Rule.Type),
max: payload.Number,
- lineForTable: make(map[string]int),
+ tableIndexes: make(map[string]map[string]bool),
+ tableLines: make(map[string]int),
catalog: checkCtx.Catalog,
}
@@ -58,7 +58,10 @@ func (*IndexTotalNumberLimitAdvisor) Check(_ context.Context, checkCtx advisor.C
(stmt).Accept(checker)
}
- return checker.generateAdvice(), nil
+ // Check all tables after processing all statements
+ checker.checkAllTables()
+
+ return checker.adviceList, nil
}
type indexTotalNumberLimitChecker struct {
@@ -68,82 +71,120 @@ type indexTotalNumberLimitChecker struct {
text string
line int
max int
- lineForTable map[string]int
+ tableIndexes map[string]map[string]bool // tableName -> indexName -> exists
+ tableLines map[string]int // tableName -> last line number
catalog *catalog.Finder
}
-func (checker *indexTotalNumberLimitChecker) generateAdvice() []*storepb.Advice {
- type tableName struct {
- name string
- line int
- }
- var tableList []tableName
+// Enter implements the ast.Visitor interface.
+func (checker *indexTotalNumberLimitChecker) Enter(in ast.Node) (ast.Node, bool) {
+ switch node := in.(type) {
+ case *ast.CreateTableStmt:
+ tableName := node.Table.Name.O
- for k, v := range checker.lineForTable {
- tableList = append(tableList, tableName{
- name: k,
- line: v,
- })
- }
- slices.SortFunc(tableList, func(i, j tableName) int {
- if i.line < j.line {
- return -1
+ // Initialize table indexes map
+ if checker.tableIndexes[tableName] == nil {
+ checker.tableIndexes[tableName] = make(map[string]bool)
}
- if i.line > j.line {
- return 1
+
+ // Check inline indexes in column definitions
+ for _, column := range node.Cols {
+ if createIndex(column) {
+ indexName := fmt.Sprintf("__inline_index_%d__", len(checker.tableIndexes[tableName]))
+ checker.tableIndexes[tableName][indexName] = true
+ }
}
- return 0
- })
- for _, table := range tableList {
- tableInfo := checker.catalog.Final.FindTable(&catalog.TableFind{TableName: table.name})
- if tableInfo != nil && tableInfo.CountIndex() > checker.max {
- checker.adviceList = append(checker.adviceList, &storepb.Advice{
- Status: checker.level,
- Code: advisor.IndexCountExceedsLimit.Int32(),
- Title: checker.title,
- Content: fmt.Sprintf("The count of index in table `%s` should be no more than %d, but found %d", table.name, checker.max, tableInfo.CountIndex()),
- StartPosition: common.ConvertANTLRLineToPosition(table.line),
- })
+ // Check constraint indexes
+ for _, constraint := range node.Constraints {
+ if createIndex(constraint) {
+ indexName := fmt.Sprintf("__index_%d__", len(checker.tableIndexes[tableName]))
+ checker.tableIndexes[tableName][indexName] = true
+ }
}
- }
- return checker.adviceList
-}
+ // Track last line for this table
+ checker.tableLines[tableName] = node.OriginTextPosition()
-// Enter implements the ast.Visitor interface.
-func (checker *indexTotalNumberLimitChecker) Enter(in ast.Node) (ast.Node, bool) {
- switch node := in.(type) {
- case *ast.CreateTableStmt:
- checker.lineForTable[node.Table.Name.O] = node.OriginTextPosition()
case *ast.AlterTableStmt:
+ tableName := node.Table.Name.O
+
+ // Initialize table indexes map
+ if checker.tableIndexes[tableName] == nil {
+ checker.tableIndexes[tableName] = make(map[string]bool)
+ }
+
for _, spec := range node.Specs {
switch spec.Tp {
case ast.AlterTableAddColumns:
for _, column := range spec.NewColumns {
if createIndex(column) {
- checker.lineForTable[node.Table.Name.O] = node.OriginTextPosition()
- break
+ indexName := fmt.Sprintf("__inline_index_%d__", len(checker.tableIndexes[tableName]))
+ checker.tableIndexes[tableName][indexName] = true
}
}
case ast.AlterTableAddConstraint:
if createIndex(spec.Constraint) {
- checker.lineForTable[node.Table.Name.O] = node.OriginTextPosition()
+ indexName := fmt.Sprintf("__index_%d__", len(checker.tableIndexes[tableName]))
+ checker.tableIndexes[tableName][indexName] = true
}
case ast.AlterTableChangeColumn, ast.AlterTableModifyColumn:
- if createIndex(spec.NewColumns[0]) {
- checker.lineForTable[node.Table.Name.O] = node.OriginTextPosition()
+ if len(spec.NewColumns) > 0 && createIndex(spec.NewColumns[0]) {
+ indexName := fmt.Sprintf("__inline_index_%d__", len(checker.tableIndexes[tableName]))
+ checker.tableIndexes[tableName][indexName] = true
}
default:
}
}
+
+ // Track last line for this table
+ checker.tableLines[tableName] = node.OriginTextPosition()
+
case *ast.CreateIndexStmt:
- checker.lineForTable[node.Table.Name.O] = node.OriginTextPosition()
+ tableName := node.Table.Name.O
+
+ // Initialize table indexes map
+ if checker.tableIndexes[tableName] == nil {
+ checker.tableIndexes[tableName] = make(map[string]bool)
+ }
+
+ // Add the index
+ indexName := fmt.Sprintf("__index_%d__", len(checker.tableIndexes[tableName]))
+ checker.tableIndexes[tableName][indexName] = true
+
+ // Track last line for this table
+ checker.tableLines[tableName] = node.OriginTextPosition()
}
return in, false
}
+// checkAllTables checks all tables' index counts after processing all statements.
+func (checker *indexTotalNumberLimitChecker) checkAllTables() {
+ for tableName, indexes := range checker.tableIndexes {
+ // Get the number of indexes created in these statements
+ newIndexes := len(indexes)
+
+ // Get the number of indexes that already exist in catalog.Origin
+ existingIndexes := 0
+ if table := checker.catalog.Origin.FindTable(&catalog.TableFind{TableName: tableName}); table != nil {
+ existingIndexes = table.CountIndex()
+ }
+
+ totalCount := existingIndexes + newIndexes
+ if totalCount > checker.max {
+ line := checker.tableLines[tableName]
+ checker.adviceList = append(checker.adviceList, &storepb.Advice{
+ Status: checker.level,
+ Code: advisor.IndexCountExceedsLimit.Int32(),
+ Title: checker.title,
+ Content: fmt.Sprintf("The count of index in table `%s` should be no more than %d, but found %d", tableName, checker.max, totalCount),
+ StartPosition: common.ConvertANTLRLineToPosition(line),
+ })
+ }
+ }
+}
+
// Leave implements the ast.Visitor interface.
func (*indexTotalNumberLimitChecker) Leave(in ast.Node) (ast.Node, bool) {
return in, true