Skip to content

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

chore: Revert PRs #18080 and #18088 (#18095)

Danny Xuf193a40ad100
diff --git a/backend/plugin/advisor/catalog/finder.go b/backend/plugin/advisor/catalog/finder.go
index 5f32d69ea4..06cba64367 100644
--- a/backend/plugin/advisor/catalog/finder.go
+++ b/backend/plugin/advisor/catalog/finder.go
@@ -44,9 +44,20 @@ 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)}
+ 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)
}
diff --git a/backend/plugin/advisor/catalog/test/mysql_walk_through.yaml b/backend/plugin/advisor/catalog/test/mysql_walk_through.yaml
index 9a91aa2d76..7cfecfe1d2 100644
--- a/backend/plugin/advisor/catalog/test/mysql_walk_through.yaml
+++ b/backend/plugin/advisor/catalog/test/mysql_walk_through.yaml
@@ -4,7 +4,11 @@
RENAME TABLE `other`.`t1` TO `other`.`t2`;
ignore_case_sensitive: false
want: ""
- err: Database `other` is not the current database `test`
+ err:
+ type: 201
+ content: Database `other` is not the current database `test`
+ line: 2
+ payload: null
- statement: |-
CREATE TABLE `test`.`t1` (a INT);
RENAME TABLE `test`.`t1` TO `other`.`t1`;
@@ -23,13 +27,21 @@
RENAME TABLE t2 TO t1;
ignore_case_sensitive: false
want: ""
- err: Table `t1` already exists
+ err:
+ type: 301
+ content: Table `t1` already exists
+ line: 3
+ payload: null
- statement: |-
CREATE TABLE t1(a INT);
RENAME TABLE t2 TO t3;
ignore_case_sensitive: false
want: ""
- err: Table `t2` does not exist
+ err:
+ type: 302
+ content: Table `t2` does not exist
+ line: 2
+ payload: null
- statement: |-
CREATE TABLE t1 (a INT);
RENAME TABLE t1 TO t2;
@@ -59,14 +71,22 @@
- statement: CREATE DATABASE another;
ignore_case_sensitive: false
want: ""
- err: Database `another` is not the current database `test`
+ err:
+ type: 201
+ content: Database `another` is not the current database `test`
+ line: 1
+ payload: null
- statement: |-
-- this is comment
DROP DATABASE test;
CREATE TABLE t(a int);
ignore_case_sensitive: false
want: ""
- err: Database `test` is deleted
+ err:
+ type: 202
+ content: Database `test` is deleted
+ line: 1
+ payload: null
- statement: |-
ALTER DATABASE CHARACTER SET = utf8mb4;
ALTER DATABASE test COLLATE utf8mb4_polish_ci;
@@ -207,7 +227,11 @@
CREATE SPATIAL INDEX sk1 ON t1 (a);
ignore_case_sensitive: false
want: ""
- err: All parts of a SPATIAL index must be NOT NULL, but `a` is nullable
+ err:
+ type: 507
+ content: All parts of a SPATIAL index must be NOT NULL, but `a` is nullable
+ line: 2
+ payload: null
- statement: |-
CREATE TABLE t1(
a INT PRIMARY KEY,
@@ -368,14 +392,22 @@
ALTER TABLE t1 ALTER COLUMN a SET DEFAULT NULL;
ignore_case_sensitive: false
want: ""
- err: Invalid default value for column `a`
+ err:
+ type: 406
+ content: Invalid default value for column `a`
+ line: 2
+ payload: null
- statement: |-
CREATE TABLE t1(a INT);
ALTER TABLE t1 CHANGE COLUMN a a BLOB;
ALTER TABLE t1 ALTER COLUMN a SET DEFAULT 'default value';
ignore_case_sensitive: false
want: ""
- err: BLOB, TEXT, GEOMETRY or JSON column `a` can't have a default value
+ err:
+ type: 407
+ content: BLOB, TEXT, GEOMETRY or JSON column `a` can't have a default value
+ line: 3
+ payload: null
- statement: |-
CREATE TABLE t1(a INT);
ALTER TABLE t1 CHANGE COLUMN a a BLOB;
@@ -528,17 +560,29 @@
);
ignore_case_sensitive: false
want: ""
- err: There can be only one auto column for table `t1`
+ err:
+ type: 404
+ content: There can be only one auto column for table `t1`
+ line: 1
+ payload: null
- statement: CREATE TABLE tvx SELECT (x,z) FROM (VALUES ROW(1,3,5), ROW(2,4,6)) AS v(x,y,z);
ignore_case_sensitive: false
want: ""
- err: Disallow the CREATE TABLE AS statement but "CREATE TABLE tvx SELECT (x,z) FROM (VALUES ROW(1,3,5), ROW(2,4,6)) AS v(x,y,z);" uses
+ err:
+ type: 303
+ content: Disallow the CREATE TABLE AS statement but "CREATE TABLE tvx SELECT (x,z) FROM (VALUES ROW(1,3,5), ROW(2,4,6)) AS v(x,y,z);" uses
+ line: 1
+ payload: null
- statement: |-
CREATE TABLE t1(a int);
CREATE TABLE t2 (b INT) SELECT a FROM t1;
ignore_case_sensitive: false
want: ""
- err: Disallow the CREATE TABLE AS statement but "CREATE TABLE t2 (b INT) SELECT a FROM t1;" uses
+ err:
+ type: 303
+ content: Disallow the CREATE TABLE AS statement but "CREATE TABLE t2 (b INT) SELECT a FROM t1;" uses
+ line: 2
+ payload: null
- statement: |-
CREATE TABLE t1(
a int
@@ -678,21 +722,33 @@
);
ignore_case_sensitive: false
want: ""
- err: Invalid default value for column `a`
+ err:
+ type: 406
+ content: Invalid default value for column `a`
+ line: 2
+ payload: null
- statement: |-
CREATE TABLE t1(
a BLOB DEFAULT 'this is a default value'
);
ignore_case_sensitive: false
want: ""
- err: BLOB, TEXT, GEOMETRY or JSON column `a` can't have a default value
+ err:
+ type: 407
+ content: BLOB, TEXT, GEOMETRY or JSON column `a` can't have a default value
+ line: 2
+ payload: null
- statement: |-
CREATE TABLE t1(
a INT ON UPDATE NOW()
);
ignore_case_sensitive: false
want: ""
- err: Column `a` use ON UPDATE but is not DATETIME or TIMESTAMP
+ err:
+ type: 405
+ content: Column `a` use ON UPDATE but is not DATETIME or TIMESTAMP
+ line: 2
+ payload: null
- statement: |-
CREATE TABLE t1(
a INT NOT NULL DEFAULT 1 PRIMARY KEY,
diff --git a/backend/plugin/advisor/catalog/test/pg_walk_through.yaml b/backend/plugin/advisor/catalog/test/pg_walk_through.yaml
index ea9621b38a..c222b9090f 100644
--- a/backend/plugin/advisor/catalog/test/pg_walk_through.yaml
+++ b/backend/plugin/advisor/catalog/test/pg_walk_through.yaml
@@ -553,15 +553,30 @@
- statement: ALTER TABLE test DROP COLUMN id;
ignore_case_sensitive: false
want: ""
- err: 'Cannot drop column "id" in table "public"."test", it''s referenced by view: "public"."v1"'
+ err:
+ type: 408
+ content: 'Cannot drop column "id" in table "public"."test", it''s referenced by view: "public"."v1"'
+ line: 1
+ payload:
+ - '"public"."v1"'
- statement: ALTER TABLE test ALTER COLUMN id TYPE varchar(20);
ignore_case_sensitive: false
want: ""
- err: 'Cannot alter type of column "id" in table "public"."test", it''s referenced by view: "public"."v1"'
+ err:
+ type: 408
+ content: 'Cannot alter type of column "id" in table "public"."test", it''s referenced by view: "public"."v1"'
+ line: 1
+ payload:
+ - '"public"."v1"'
- statement: DROP TABLE test;
ignore_case_sensitive: false
want: ""
- err: 'Cannot drop table "public"."test", it''s referenced by view: "public"."v1"'
+ err:
+ type: 304
+ content: 'Cannot drop table "public"."test", it''s referenced by view: "public"."v1"'
+ line: 1
+ payload:
+ - '"public"."v1"'
- statement: |-
DROP VIEW v1;
ALTER TABLE test DROP COLUMN id;
diff --git a/backend/plugin/advisor/catalog/test/tidb_walk_through.yaml b/backend/plugin/advisor/catalog/test/tidb_walk_through.yaml
index 894ef49a6f..691a04403f 100644
--- a/backend/plugin/advisor/catalog/test/tidb_walk_through.yaml
+++ b/backend/plugin/advisor/catalog/test/tidb_walk_through.yaml
@@ -1,67 +1,127 @@
- statement: CREATE TABLE t(a date default '');
ignore_case_sensitive: false
want: ""
- err: '[types:1292]Incorrect datetime value: '''''
+ err:
+ type: 407
+ content: '[types:1292]Incorrect datetime value: '''''
+ line: 1
+ payload: null
- statement: |-
CREATE TABLE t(a TEXT);
ALTER TABLE T ALTER COLUMN a SET DEFAULT '1';
ignore_case_sensitive: true
want: ""
- err: BLOB, TEXT, GEOMETRY or JSON column `a` can't have a default value
+ err:
+ type: 407
+ content: BLOB, TEXT, GEOMETRY or JSON column `a` can't have a default value
+ line: 2
+ payload: null
- statement: ' CREATE TABLE t(a TEXT DEFAULT ''1'')'
ignore_case_sensitive: false
want: ""
- err: BLOB, TEXT, GEOMETRY or JSON column `a` can't have a default value
+ err:
+ type: 407
+ content: BLOB, TEXT, GEOMETRY or JSON column `a` can't have a default value
+ line: 1
+ payload: null
- statement: |-
CREATE TABLE t(a INT NOT NULL);
ALTER TABLE t ALTER COLUMN a SET DEFAULT NULL;
ignore_case_sensitive: false
want: ""
- err: Invalid default value for column `a`
+ err:
+ type: 406
+ content: Invalid default value for column `a`
+ line: 2
+ payload: null
- statement: ' CREATE TABLE t(a INT NOT NULL DEFAULT NULL)'
ignore_case_sensitive: false
want: ""
- err: Invalid default value for column `a`
+ err:
+ type: 406
+ content: Invalid default value for column `a`
+ line: 1
+ payload: null
- statement: ' CREATE TABLE t(a int ON UPDATE NOW())'
ignore_case_sensitive: false
want: ""
- err: Column `a` use ON UPDATE but is not DATETIME or TIMESTAMP
+ err:
+ type: 405
+ content: Column `a` use ON UPDATE but is not DATETIME or TIMESTAMP
+ line: 1
+ payload: null
- statement: ' CREATE TABLE t(a int auto_increment, b int auto_increment);'
ignore_case_sensitive: false
want: ""
- err: There can be only one auto column for table `t`
+ err:
+ type: 404
+ content: There can be only one auto column for table `t`
+ line: 1
+ payload: null
- statement: ' CREATE TABLE t as select * from t1;'
ignore_case_sensitive: false
want: ""
- err: Disallow the CREATE TABLE AS statement but "CREATE TABLE t as select * from t1;" uses
+ err:
+ type: 303
+ content: Disallow the CREATE TABLE AS statement but "CREATE TABLE t as select * from t1;" uses
+ line: 1
+ payload: null
- statement: "\n\t\t\t\tCREATE TABLE t(a int, b int); \n\t\t\t\tCREATE INDEX idx_c on t(c);\n\t\t\t"
ignore_case_sensitive: false
want: ""
- err: Column `c` does not exist in table `t`
+ err:
+ type: 402
+ content: Column `c` does not exist in table `t`
+ line: 3
+ payload: null
- statement: "\n\t\t\t\tCREATE TABLE t(a int, b int); \n\t\t\t\tALTER TABLE t MODIFY COLUMN c int;\n\t\t\t"
ignore_case_sensitive: false
want: ""
- err: Column `c` does not exist in table `t`
+ err:
+ type: 402
+ content: Column `c` does not exist in table `t`
+ line: 3
+ payload: null
- statement: "\n\t\t\t\tCREATE TABLE t(a int, b int); \n\t\t\t\tALTER TABLE t CHANGE COLUMN c aa int;\n\t\t\t"
ignore_case_sensitive: false
want: ""
- err: Column `c` does not exist in table `t`
+ err:
+ type: 402
+ content: Column `c` does not exist in table `t`
+ line: 3
+ payload: null
- statement: "\n\t\t\t\tCREATE TABLE t(a int, b int); \n\t\t\t\tALTER TABLE t DROP COLUMN c;\n\t\t\t"
ignore_case_sensitive: false
want: ""
- err: Column `c` does not exist in table `t`
+ err:
+ type: 402
+ content: Column `c` does not exist in table `t`
+ line: 3
+ payload: null
- statement: "\n\t\t\t\tCREATE TABLE t(a int, b int); \n\t\t\t\tALTER TABLE t RENAME COLUMN c TO cc;\n\t\t\t"
ignore_case_sensitive: false
want: ""
- err: Column `c` does not exist in table `t`
+ err:
+ type: 402
+ content: Column `c` does not exist in table `t`
+ line: 3
+ payload: null
- statement: "\n\t\t\t\tCREATE TABLE t(a int, b int); \n\t\t\t\tALTER TABLE t RENAME COLUMN c TO cc;\n\t\t\t"
ignore_case_sensitive: false
want: ""
- err: Column `c` does not exist in table `t`
+ err:
+ type: 402
+ content: Column `c` does not exist in table `t`
+ line: 3
+ payload: null
- statement: "\n\t\t\t\tCREATE TABLE t(a int, b int); \n\t\t\t\tALTER TABLE t ALTER COLUMN c DROP DEFAULT;\n\t\t\t"
ignore_case_sensitive: false
want: ""
- err: Column `c` does not exist in table `t`
+ err:
+ type: 402
+ content: Column `c` does not exist in table `t`
+ line: 3
+ payload: null
- statement: |-
ALTER DATABASE CHARACTER SET = utf8mb4;
ALTER DATABASE test COLLATE utf8mb4_polish_ci;
@@ -449,7 +509,11 @@
- statement: ' DROP TABLE t1, t2'
ignore_case_sensitive: false
want: ""
- err: Table `t1` does not exist
+ err:
+ type: 302
+ content: Table `t1` does not exist
+ line: 1
+ payload: null
- statement: |-
CREATE TABLE t(a int);
RENAME TABLE t to other_db.t1
@@ -494,7 +558,11 @@
CREATE TABLE t(a int);
ignore_case_sensitive: false
want: ""
- err: Database `test` is deleted
+ err:
+ type: 202
+ content: Database `test` is deleted
+ line: 3
+ payload: null
- statement: |-
CREATE TABLE t(
a int PRIMARY KEY DEFAULT 1,
diff --git a/backend/plugin/advisor/catalog/walk_through.go b/backend/plugin/advisor/catalog/walk_through.go
index 45b13701f7..fd19fb7bb1 100644
--- a/backend/plugin/advisor/catalog/walk_through.go
+++ b/backend/plugin/advisor/catalog/walk_through.go
@@ -10,6 +10,9 @@ import (
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
)
+// WalkThroughErrorType is the type of WalkThroughError.
+type WalkThroughErrorType int
+
const (
// PrimaryKeyName is the string for PK.
PrimaryKeyName string = "PRIMARY"
@@ -19,28 +22,162 @@ const (
SpatialName string = "SPATIAL"
publicSchemaName = "public"
+
+ // ErrorTypeUnsupported is the error for unsupported cases.
+ ErrorTypeUnsupported WalkThroughErrorType = 1
+ // ErrorTypeInternal is the error for internal errors.
+ ErrorTypeInternal WalkThroughErrorType = 2
+
+ // 101 parse error type.
+
+ // ErrorTypeParseError is the error in parsing.
+ ErrorTypeParseError WalkThroughErrorType = 101
+ // ErrorTypeDeparseError is the error in deparsing.
+ ErrorTypeDeparseError WalkThroughErrorType = 102
+
+ // 201 ~ 299 database error type.
+
+ // ErrorTypeAccessOtherDatabase is the error that try to access other database.
+ ErrorTypeAccessOtherDatabase = 201
+ // ErrorTypeDatabaseIsDeleted is the error that try to access the deleted database.
+ ErrorTypeDatabaseIsDeleted = 202
+ // ErrorTypeReferenceOtherDatabase is the error that try to reference other database.
+ ErrorTypeReferenceOtherDatabase = 203
+
+ // 301 ~ 399 table error type.
+
+ // ErrorTypeTableExists is the error that table exists.
+ ErrorTypeTableExists = 301
+ // ErrorTypeTableNotExists is the error that table not exists.
+ ErrorTypeTableNotExists = 302
+ // ErrorTypeUseCreateTableAs is the error that using CREATE TABLE AS statements.
+ ErrorTypeUseCreateTableAs = 303
+ // ErrorTypeTableIsReferencedByView is the error that table is referenced by view.
+ ErrorTypeTableIsReferencedByView = 304
+
+ // 401 ~ 499 column error type.
+
+ // ErrorTypeColumnExists is the error that column exists.
+ ErrorTypeColumnExists = 401
+ // ErrorTypeColumnNotExists is the error that column not exists.
+ ErrorTypeColumnNotExists = 402
+ // ErrorTypeDropAllColumns is the error that dropping all columns in a table.
+ ErrorTypeDropAllColumns = 403
+ // ErrorTypeAutoIncrementExists is the error that auto_increment exists.
+ ErrorTypeAutoIncrementExists = 404
+ // ErrorTypeOnUpdateColumnNotDatetimeOrTimestamp is the error that the ON UPDATE column is not datetime or timestamp.
+ ErrorTypeOnUpdateColumnNotDatetimeOrTimestamp = 405
+ // ErrorTypeSetNullDefaultForNotNullColumn is the error that setting NULL default value for the NOT NULL column.
+ ErrorTypeSetNullDefaultForNotNullColumn = 406
+ // ErrorTypeInvalidColumnTypeForDefaultValue is the error that invalid column type for default value.
+ ErrorTypeInvalidColumnTypeForDefaultValue = 407
+ // ErrorTypeColumnIsReferencedByView is the error that column is referenced by view.
+ ErrorTypeColumnIsReferencedByView = 408
+
+ // 501 ~ 599 index error type.
+
+ // ErrorTypePrimaryKeyExists is the error that PK exists.
+ ErrorTypePrimaryKeyExists = 501
+ // ErrorTypeIndexExists is the error that index exists.
+ ErrorTypeIndexExists = 502
+ // ErrorTypeIndexEmptyKeys is the error that index has empty keys.
+ ErrorTypeIndexEmptyKeys = 503
+ // ErrorTypePrimaryKeyNotExists is the error that PK does not exist.
+ ErrorTypePrimaryKeyNotExists = 504
+ // ErrorTypeIndexNotExists is the error that index does not exist.
+ ErrorTypeIndexNotExists = 505
+ // ErrorTypeIncorrectIndexName is the incorrect index name error.
+ ErrorTypeIncorrectIndexName = 506
+ // ErrorTypeSpatialIndexKeyNullable is the error that keys in spatial index are nullable.
+ ErrorTypeSpatialIndexKeyNullable = 507
+
+ // 701 ~ 799 schema error type.
+
+ // ErrorTypeSchemaNotExists is the error that schema does not exist.
+ ErrorTypeSchemaNotExists = 701
+
+ // 801 ~ 899 relation error type.
+
+ // ErrorTypeRelationExists is the error that relation already exists.
+ ErrorTypeRelationExists = 801
+
+ // 901 ~ 999 constraint error type.
+
+ // ErrorTypeConstraintNotExists is the error that constraint doesn't exist.
+ ErrorTypeConstraintNotExists = 901
)
-// SchemaViolationError represents a SQL schema validation error that should be reported as advice.
-// It carries an advisor code (int32) that can be converted to advisor.Code by the caller.
-type SchemaViolationError struct {
- Code int32 // Advisor error code from backend/plugin/advisor/code.go
- Message string
+// WalkThroughError is the error for walking-through.
+type WalkThroughError struct {
+ Type WalkThroughErrorType
+ Content string
+ // TODO(zp): position
+ Line int
+
+ Payload any
}
-// Error implements the error interface.
-func (e *SchemaViolationError) Error() string {
- return e.Message
+// NewRelationExistsError returns a new ErrorTypeRelationExists.
+func NewRelationExistsError(relationName string, schemaName string) *WalkThroughError {
+ return &WalkThroughError{
+ Type: ErrorTypeRelationExists,
+ Content: fmt.Sprintf("Relation %q already exists in schema %q", relationName, schemaName),
+ }
+}
+
+// NewColumnNotExistsError returns a new ErrorTypeColumnNotExists.
+func NewColumnNotExistsError(tableName string, columnName string) *WalkThroughError {
+ return &WalkThroughError{
+ Type: ErrorTypeColumnNotExists,
+ Content: fmt.Sprintf("Column `%s` does not exist in table `%s`", columnName, tableName),
+ }
}
-// NewSchemaViolationError creates a new SchemaViolationError.
-func NewSchemaViolationError(code int32, message string) *SchemaViolationError {
- return &SchemaViolationError{
- Code: code,
- Message: message,
+// NewIndexNotExistsError returns a new ErrorTypeIndexNotExists.
+func NewIndexNotExistsError(tableName string, indexName string) *WalkThroughError {
+ return &WalkThroughError{
+ Type: ErrorTypeIndexNotExists,
+ Content: fmt.Sprintf("Index `%s` does not exist in table `%s`", indexName, tableName),
}
}
+// NewIndexExistsError returns a new ErrorTypeIndexExists.
+func NewIndexExistsError(tableName string, indexName string) *WalkThroughError {
+ return &WalkThroughError{
+ Type: ErrorTypeIndexExists,
+ Content: fmt.Sprintf("Index `%s` already exists in table `%s`", indexName, tableName),
+ }
+}
+
+// NewAccessOtherDatabaseError returns a new ErrorTypeAccessOtherDatabase.
+func NewAccessOtherDatabaseError(current string, target string) *WalkThroughError {
+ return &WalkThroughError{
+ Type: ErrorTypeAccessOtherDatabase,
+ Content: fmt.Sprintf("Database `%s` is not the current database `%s`", target, current),
+ }
+}
+
+// NewTableNotExistsError returns a new ErrorTypeTableNotExists.
+func NewTableNotExistsError(tableName string) *WalkThroughError {
+ return &WalkThroughError{
+ Type: ErrorTypeTableNotExists,
+ Content: fmt.Sprintf("Table `%s` does not exist", tableName),
+ }
+}
+
+// NewTableExistsError returns a new ErrorTypeTableExists.
+func NewTableExistsError(tableName string) *WalkThroughError {
+ return &WalkThroughError{
+ Type: ErrorTypeTableExists,
+ Content: fmt.Sprintf("Table `%s` already exists", tableName),
+ }
+}
+
+// Error implements the error interface.
+func (e *WalkThroughError) Error() string {
+ return e.Content
+}
+
// WalkThrough will collect the catalog schema in the databaseState as it walks through the stmt.
func (d *DatabaseState) WalkThrough(ast any) error {
switch d.dbType {
@@ -57,7 +194,10 @@ func (d *DatabaseState) WalkThrough(ast any) error {
}
return nil
default:
- return errors.Errorf("Walk-through doesn't support engine type: %s", d.dbType)
+ return &WalkThroughError{
+ Type: ErrorTypeUnsupported,
+ Content: fmt.Sprintf("Walk-through doesn't support engine type: %s", d.dbType),
+ }
}
}
@@ -94,7 +234,7 @@ func (t *TableState) createIncompleteIndex(name string) *IndexState {
return index
}
-func (s *SchemaState) renameTable(ctx *FinderContext, oldName string, newName string) error {
+func (s *SchemaState) renameTable(ctx *FinderContext, oldName string, newName string) *WalkThroughError {
if oldName == newName {
return nil
}
@@ -102,13 +242,19 @@ func (s *SchemaState) renameTable(ctx *FinderContext, oldName string, newName st
table, exists := s.getTable(oldName)
if !exists {
if ctx.CheckIntegrity {
- return NewSchemaViolationError(604, fmt.Sprintf("Table `%s` does not exist", oldName))
+ return &WalkThroughError{
+ Type: ErrorTypeTableNotExists,
+ Content: fmt.Sprintf("Table `%s` does not exist", oldName),
+ }
}
table = s.createIncompleteTable(oldName)
}
if _, exists := s.getTable(newName); exists {
- return NewSchemaViolationError(607, fmt.Sprintf("Table `%s` already exists", newName))
+ return &WalkThroughError{
+ Type: ErrorTypeTableExists,
+ Content: fmt.Sprintf("Table `%s` already exists", newName),
+ }
}
table.name = newName
@@ -163,12 +309,15 @@ func parseViewName(viewName string) (string, string, error) {
return match[1], match[2], nil
}
-func (d *DatabaseState) existedViewList(viewMap map[string]bool) ([]string, error) {
+func (d *DatabaseState) existedViewList(viewMap map[string]bool) ([]string, *WalkThroughError) {
var result []string
for viewName := range viewMap {
schemaName, viewName, err := parseViewName(viewName)
if err != nil {
- return nil, errors.Wrapf(err, "failed to check view dependency")
+ return nil, &WalkThroughError{
+ Type: ErrorTypeInternal,
+ Content: fmt.Sprintf("failed to check view dependency: %s", err.Error()),
+ }
}
schemaMeta, exists := d.schemaSet[schemaName]
if !exists {
@@ -197,14 +346,17 @@ func (s *SchemaState) pgGeneratePrimaryKeyName(tableName string) string {
}
}
-func (d *DatabaseState) getSchema(schemaName string) (*SchemaState, error) {
+func (d *DatabaseState) getSchema(schemaName string) (*SchemaState, *WalkThroughError) {
if schemaName == "" {
schemaName = publicSchemaName
}
schema, exists := d.schemaSet[schemaName]
if !exists {
if schemaName != publicSchemaName {
- return nil, NewSchemaViolationError(1901, fmt.Sprintf("The schema %q doesn't exist", schemaName))
+ return nil, &WalkThroughError{
+ Type: ErrorTypeSchemaNotExists,
+ Content: fmt.Sprintf("The schema %q doesn't exist", schemaName),
+ }
}
schema = &SchemaState{
ctx: d.ctx.Copy(),
@@ -218,28 +370,37 @@ func (d *DatabaseState) getSchema(schemaName string) (*SchemaState, error) {
return schema, nil
}
-func (t *TableState) getColumn(columnName string) (*ColumnState, error) {
+func (t *TableState) getColumn(columnName string) (*ColumnState, *WalkThroughError) {
column, exists := t.columnSet[columnName]
if !exists {
- return nil, NewSchemaViolationError(405, fmt.Sprintf("The column %q does not exist in the table %q", columnName, t.name))
+ return nil, &WalkThroughError{
+ Type: ErrorTypeColumnNotExists,
+ Content: fmt.Sprintf("The column %q does not exist in the table %q", columnName, t.name),
+ }
}
return column, nil
}
-func (s *SchemaState) pgGetTable(tableName string) (*TableState, error) {
+func (s *SchemaState) pgGetTable(tableName string) (*TableState, *WalkThroughError) {
table, exists := s.tableSet[tableName]
if !exists {
- return nil, NewSchemaViolationError(604, fmt.Sprintf("The table %q does not exist in schema %q", tableName, s.name))
+ return nil, &WalkThroughError{
+ Type: ErrorTypeTableNotExists,
+ Content: fmt.Sprintf("The table %q does not exist in schema %q", tableName, s.name),
+ }
}
return table, nil
}
-func (s *SchemaState) getIndex(indexName string) (*TableState, *IndexState, error) {
+func (s *SchemaState) getIndex(indexName string) (*TableState, *IndexState, *WalkThroughError) {
for _, table := range s.tableSet {
if index, exists := table.indexSet[indexName]; exists {
return table, index, nil
}
}
- return nil, nil, NewSchemaViolationError(809, fmt.Sprintf("Index %q does not exists in schema %q", indexName, s.name))
+ return nil, nil, &WalkThroughError{
+ Type: ErrorTypeIndexNotExists,
+ Content: fmt.Sprintf("Index %q does not exists in schema %q", indexName, s.name),
+ }
}
diff --git a/backend/plugin/advisor/catalog/walk_through_for_mysql.go b/backend/plugin/advisor/catalog/walk_through_for_mysql.go
index 5c1c7eb407..11b9b6447f 100644
--- a/backend/plugin/advisor/catalog/walk_through_for_mysql.go
+++ b/backend/plugin/advisor/catalog/walk_through_for_mysql.go
@@ -41,7 +41,7 @@ type mysqlListener struct {
lineNumber int
text string
databaseState *DatabaseState
- err error
+ err *WalkThroughError
}
func (l *mysqlListener) EnterQuery(ctx *mysql.QueryContext) {
@@ -49,9 +49,21 @@ func (l *mysqlListener) EnterQuery(ctx *mysql.QueryContext) {
l.lineNumber = l.baseLine + ctx.GetStart().GetLine()
}
-func (d *DatabaseState) mysqlChangeState(in *mysqlparser.ParseResult) error {
+func (d *DatabaseState) mysqlChangeState(in *mysqlparser.ParseResult) (err *WalkThroughError) {
+ defer func() {
+ if err == nil {
+ return
+ }
+ if err.Line == 0 {
+ err.Line = in.BaseLine
+ }
+ }()
+
if d.deleted {
- return NewSchemaViolationError(703, fmt.Sprintf("Database `%s` is deleted", d.name))
+ return &WalkThroughError{
+ Type: ErrorTypeDatabaseIsDeleted,
+ Content: fmt.Sprintf("Database `%s` is deleted", d.name),
+ }
}
listener := &mysqlListener{
@@ -59,7 +71,13 @@ func (d *DatabaseState) mysqlChangeState(in *mysqlparser.ParseResult) error {
databaseState: d,
}
antlr.ParseTreeWalkerDefault.Walk(listener, in.Tree)
- return listener.err
+ if listener.err != nil {
+ if listener.err.Line == 0 {
+ listener.err.Line = listener.lineNumber
+ }
+ return listener.err
+ }
+ return nil
}
// EnterCreateTable is called when production createTable is entered.
@@ -72,7 +90,10 @@ func (l *mysqlListener) EnterCreateTable(ctx *mysql.CreateTableContext) {
}
databaseName, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
if databaseName != "" && !l.databaseState.isCurrentDatabase(databaseName) {
- l.err = NewSchemaViolationError(702, fmt.Sprintf("Database `%s` is not the current database `%s`", databaseName, l.databaseState.name))
+ l.err = &WalkThroughError{
+ Type: ErrorTypeAccessOtherDatabase,
+ Content: fmt.Sprintf("Database `%s` is not the current database `%s`", databaseName, l.databaseState.name),
+ }
return
}
@@ -84,12 +105,18 @@ func (l *mysqlListener) EnterCreateTable(ctx *mysql.CreateTableContext) {
if ctx.IfNotExists() != nil {
return
}
- l.err = NewSchemaViolationError(607, fmt.Sprintf("Table `%s` already exists", tableName))
+ l.err = &WalkThroughError{
+ Type: ErrorTypeTableExists,
+ Content: fmt.Sprintf("Table `%s` already exists", tableName),
+ }
return
}
if ctx.DuplicateAsQueryExpression() != nil {
- l.err = NewSchemaViolationError(205, fmt.Sprintf("Disallow the CREATE TABLE AS statement but \"%s\" uses", l.text))
+ l.err = &WalkThroughError{
+ Type: ErrorTypeUseCreateTableAs,
+ Content: fmt.Sprintf("Disallow the CREATE TABLE AS statement but \"%s\" uses", l.text),
+ }
return
}
@@ -123,17 +150,23 @@ func (l *mysqlListener) EnterCreateTable(ctx *mysql.CreateTableContext) {
}
if mysqlparser.IsAutoIncrement(tableElement.ColumnDefinition().FieldDefinition()) {
if hasAutoIncrement {
- l.err = NewSchemaViolationError(1, fmt.Sprintf("There can be only one auto column for table `%s`", table.name))
+ l.err = &WalkThroughError{
+ Type: ErrorTypeAutoIncrementExists,
+ // The content comes from MySQL error content.
+ Content: fmt.Sprintf("There can be only one auto column for table `%s`", table.name),
+ }
}
hasAutoIncrement = true
}
_, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
if err := table.mysqlCreateColumn(l.databaseState.ctx, columnName, tableElement.ColumnDefinition().FieldDefinition(), nil /* position */); err != nil {
+ err.Line = l.baseLine + tableElement.GetStart().GetLine()
l.err = err
return
}
case tableElement.TableConstraintDef() != nil:
if err := table.mysqlCreateConstraint(l.databaseState.ctx, tableElement.TableConstraintDef()); err != nil {
+ err.Line = tableElement.GetStart().GetLine()
l.err = err
return
}
@@ -155,7 +188,10 @@ func (l *mysqlListener) EnterDropTable(ctx *mysql.DropTableContext) {
for _, tableRef := range ctx.TableRefList().AllTableRef() {
databaseName, tableName := mysqlparser.NormalizeMySQLTableRef(tableRef)
if databaseName != "" && !l.databaseState.isCurrentDatabase(databaseName) {
- l.err = NewSchemaViolationError(702, fmt.Sprintf("Database `%s` is not the current database `%s`", databaseName, tableName))
+ l.err = &WalkThroughError{
+ Type: ErrorTypeAccessOtherDatabase,
+ Content: fmt.Sprintf("Database `%s` is not the current database `%s`", databaseName, tableName),
+ }
}
schema, exists := l.databaseState.schemaSet[""]
@@ -168,7 +204,10 @@ func (l *mysqlListener) EnterDropTable(ctx *mysql.DropTableContext) {
if ctx.IfExists() != nil || !l.databaseState.ctx.CheckIntegrity {
return
}
- l.err = NewSchemaViolationError(604, fmt.Sprintf("Table `%s` does not exist", tableName))
+ l.err = &WalkThroughError{
+ Type: ErrorTypeTableNotExists,
+ Content: fmt.Sprintf("Table `%s` does not exist", tableName),
+ }
return
}
@@ -445,7 +484,7 @@ func (l *mysqlListener) EnterAlterDatabase(ctx *mysql.AlterDatabaseContext) {
if ctx.SchemaRef() != nil {
databaseName := mysqlparser.NormalizeMySQLSchemaRef(ctx.SchemaRef())
if !l.databaseState.isCurrentDatabase(databaseName) {
- l.err = NewSchemaViolationError(702, fmt.Sprintf("Database `%s` is not the current database `%s`", databaseName, l.databaseState.name))
+ l.err = NewAccessOtherDatabaseError(l.databaseState.name, databaseName)
return
}
}
@@ -479,7 +518,7 @@ func (l *mysqlListener) EnterDropDatabase(ctx *mysql.DropDatabaseContext) {
databaseName := mysqlparser.NormalizeMySQLSchemaRef(ctx.SchemaRef())
if !l.databaseState.isCurrentDatabase(databaseName) {
- l.err = NewSchemaViolationError(702, fmt.Sprintf("Database `%s` is not the current database `%s`", databaseName, l.databaseState.name))
+ l.err = NewAccessOtherDatabaseError(l.databaseState.name, databaseName)
return
}
@@ -495,7 +534,7 @@ func (l *mysqlListener) EnterCreateDatabase(ctx *mysql.CreateDatabaseContext) {
return
}
databaseName := mysqlparser.NormalizeMySQLSchemaName(ctx.SchemaName())
- l.err = NewSchemaViolationError(702, fmt.Sprintf("Database `%s` is not the current database `%s`", databaseName, l.databaseState.name))
+ l.err = NewAccessOtherDatabaseError(l.databaseState.name, databaseName)
}
// EnterRenameTableStatement is called when production renameTableStatement is entered.
@@ -519,13 +558,13 @@ func (l *mysqlListener) EnterRenameTableStatement(ctx *mysql.RenameTableStatemen
table, exists := schema.getTable(oldTableName)
if !exists {
if schema.ctx.CheckIntegrity {
- l.err = NewSchemaViolationError(604, fmt.Sprintf("Table `%s` does not exist", oldTableName))
+ l.err = NewTableNotExistsError(oldTableName)
return
}
table = schema.createIncompleteTable(oldTableName)
}
if _, exists := schema.getTable(newTableName); exists {
- l.err = NewSchemaViolationError(607, fmt.Sprintf("Table `%s` already exists", newTableName))
+ l.err = NewTableExistsError(newTableName)
return
}
delete(schema.tableSet, table.name)
@@ -534,12 +573,12 @@ func (l *mysqlListener) EnterRenameTableStatement(ctx *mysql.RenameTableStatemen
} else if l.databaseState.mysqlMoveToOtherDatabase(pair) {
_, exists := schema.getTable(oldTableName)
if !exists && schema.ctx.CheckIntegrity {
- l.err = NewSchemaViolationError(604, fmt.Sprintf("Table `%s` does not exist", oldTableName))
+ l.err = NewTableNotExistsError(oldTableName)
return
}
delete(schema.tableSet, oldTableName)
} else {
- l.err = NewSchemaViolationError(702, fmt.Sprintf("Database `%s` is not the current database `%s`", l.databaseState.mysqlTargetDatabase(pair), l.databaseState.name))
+ l.err = NewAccessOtherDatabaseError(l.databaseState.name, l.databaseState.mysqlTargetDatabase(pair))
return
}
}
@@ -615,11 +654,11 @@ func (d *DatabaseState) mysqlTheCurrentDatabase(renamePair mysql.IRenamePairCont
return true
}
-func (t *TableState) mysqlChangeIndexVisibility(ctx *FinderContext, indexName string, visibility mysql.IVisibilityContext) error {
+func (t *TableState) mysqlChangeIndexVisibility(ctx *FinderContext, indexName string, visibility mysql.IVisibilityContext) *WalkThroughError {
index, exists := t.indexSet[strings.ToLower(indexName)]
if !exists {
if ctx.CheckIntegrity {
- return NewSchemaViolationError(809, fmt.Sprintf("Index `%s` does not exist in table `%s`", indexName, t.name))
+ return NewIndexNotExistsError(t.name, indexName)
}
index = t.createIncompleteIndex(indexName)
}
@@ -634,7 +673,7 @@ func (t *TableState) mysqlChangeIndexVisibility(ctx *FinderContext, indexName st
return nil
}
-func (t *TableState) mysqlAlterColumn(ctx *FinderContext, itemDef mysql.IAlterListItemContext) error {
+func (t *TableState) mysqlAlterColumn(ctx *FinderContext, itemDef mysql.IAlterListItemContext) *WalkThroughError {
if itemDef.ColumnInternalRef() == nil {
// should not reach here.
return nil
@@ -643,7 +682,7 @@ func (t *TableState) mysqlAlterColumn(ctx *FinderContext, itemDef mysql.IAlterLi
colState, exists := t.columnSet[strings.ToLower(columnName)]
if !exists {
if ctx.CheckIntegrity {
- return NewSchemaViolationError(405, fmt.Sprintf("Column `%s` does not exist in table `%s`", columnName, t.name))
+ return NewColumnNotExistsError(t.name, columnName)
}
colState = t.createIncompleteColumn(columnName)
}
@@ -660,7 +699,11 @@ func (t *TableState) mysqlAlterColumn(ctx *FinderContext, itemDef mysql.IAlterLi
"text", "tinytext", "mediumtext", "longtext",
"json",
"geometry":
- return NewSchemaViolationError(423, fmt.Sprintf("BLOB, TEXT, GEOMETRY or JSON column `%s` can't have a default value", columnName))
+ return &WalkThroughError{
+ Type: ErrorTypeInvalidColumnTypeForDefaultValue,
+ // Content comes from MySQL Error content.
+ Content: fmt.Sprintf("BLOB, TEXT, GEOMETRY or JSON column `%s` can't have a default value", columnName),
+ }
default:
// Other column types allow default values
}
@@ -679,7 +722,11 @@ func (t *TableState) mysqlAlterColumn(ctx *FinderContext, itemDef mysql.IAlterLi
colState.defaultValue = &defaultValue
} else {
if colState.nullable != nil && !*colState.nullable {
- return errors.Errorf("Invalid default value for column `%s`", columnName)
+ return &WalkThroughError{
+ Type: ErrorTypeSetNullDefaultForNotNullColumn,
+ // Content comes from MySQL Error content.
+ Content: fmt.Sprintf("Invalid default value for column `%s`", columnName),
+ }
}
colState.defaultValue = nil
@@ -696,7 +743,7 @@ func (t *TableState) mysqlAlterColumn(ctx *FinderContext, itemDef mysql.IAlterLi
return nil
}
-func (t *TableState) mysqlChangeColumn(ctx *FinderContext, oldColumnName string, newColumnName string, fieldDef mysql.IFieldDefinitionContext, position *mysqlColumnPosition) error {
+func (t *TableState) mysqlChangeColumn(ctx *FinderContext, oldColumnName string, newColumnName string, fieldDef mysql.IFieldDefinitionContext, position *mysqlColumnPosition) *WalkThroughError {
if ctx.CheckIntegrity {
return t.mysqlCompleteTableChangeColumn(ctx, oldColumnName, newColumnName, fieldDef, position)
}
@@ -705,7 +752,7 @@ func (t *TableState) mysqlChangeColumn(ctx *FinderContext, oldColumnName string,
// mysqlIncompleteTableChangeColumn changes column definition.
// It does not maintain the position of the column.
-func (t *TableState) mysqlIncompleteTableChangeColumn(ctx *FinderContext, oldColumnName string, newColumnName string, fieldDef mysql.IFieldDefinitionContext, position *mysqlColumnPosition) error {
+func (t *TableState) mysqlIncompleteTableChangeColumn(ctx *FinderContext, oldColumnName string, newColumnName string, fieldDef mysql.IFieldDefinitionContext, position *mysqlColumnPosition) *WalkThroughError {
delete(t.columnSet, strings.ToLower(oldColumnName))
// rename column from indexSet
@@ -720,10 +767,10 @@ func (t *TableState) mysqlIncompleteTableChangeColumn(ctx *FinderContext, oldCol
// 1. drop column from tableState.columnSet, but do not drop column from indexSet.
// 2. rename column from indexSet.
// 3. create a new column in columnSet.
-func (t *TableState) mysqlCompleteTableChangeColumn(ctx *FinderContext, oldColumnName string, newColumnName string, fieldDef mysql.IFieldDefinitionContext, position *mysqlColumnPosition) error {
+func (t *TableState) mysqlCompleteTableChangeColumn(ctx *FinderContext, oldColumnName string, newColumnName string, fieldDef mysql.IFieldDefinitionContext, position *mysqlColumnPosition) *WalkThroughError {
column, exists := t.columnSet[strings.ToLower(oldColumnName)]
if !exists {
- return NewSchemaViolationError(405, fmt.Sprintf("Column `%s` does not exist in table `%s`", oldColumnName, t.name))
+ return NewColumnNotExistsError(t.name, oldColumnName)
}
pos := *column.position
@@ -796,7 +843,7 @@ func positionFromPlaceContext(place mysql.IPlaceContext) *mysqlColumnPosition {
return columnPosition
}
-func (d *DatabaseState) mysqlCopyTable(databaseName, tableName, referTable string) error {
+func (d *DatabaseState) mysqlCopyTable(databaseName, tableName, referTable string) *WalkThroughError {
targetTable, err := d.mysqlFindTableState(databaseName, referTable)
if err != nil {
return err
@@ -809,9 +856,9 @@ func (d *DatabaseState) mysqlCopyTable(databaseName, tableName, referTable strin
return nil
}
-func (d *DatabaseState) mysqlFindTableState(databaseName, tableName string) (*TableState, error) {
+func (d *DatabaseState) mysqlFindTableState(databaseName, tableName string) (*TableState, *WalkThroughError) {
if databaseName != "" && !d.isCurrentDatabase(databaseName) {
- return nil, NewSchemaViolationError(702, fmt.Sprintf("Database `%s` is not the current database `%s`", databaseName, d.name))
+ return nil, NewAccessOtherDatabaseError(d.name, databaseName)
}
schema, exists := d.schemaSet[""]
@@ -822,7 +869,7 @@ func (d *DatabaseState) mysqlFindTableState(databaseName, tableName string) (*Ta
table, exists := schema.getTable(tableName)
if !exists {
if schema.ctx.CheckIntegrity {
- return nil, NewSchemaViolationError(604, fmt.Sprintf("Table `%s` does not exist", tableName))
+ return nil, NewTableNotExistsError(tableName)
}
table = schema.createIncompleteTable(tableName)
}
@@ -830,7 +877,7 @@ func (d *DatabaseState) mysqlFindTableState(databaseName, tableName string) (*Ta
return table, nil
}
-func (t *TableState) mysqlCreateConstraint(ctx *FinderContext, constraintDef mysql.ITableConstraintDefContext) error {
+func (t *TableState) mysqlCreateConstraint(ctx *FinderContext, constraintDef mysql.ITableConstraintDefContext) *WalkThroughError {
if constraintDef.GetType_() != nil {
switch constraintDef.GetType_().GetTokenType() {
// PRIMARY KEY.
@@ -915,7 +962,7 @@ func (t *TableState) mysqlCreateConstraint(ctx *FinderContext, constraintDef mys
}
// mysqlValidateKeyListVariants validates the key list variants.
-func (t *TableState) mysqlValidateKeyListVariants(ctx *FinderContext, keyList mysql.IKeyListVariantsContext, primary bool, isSpatial bool) error {
+func (t *TableState) mysqlValidateKeyListVariants(ctx *FinderContext, keyList mysql.IKeyListVariantsContext, primary bool, isSpatial bool) *WalkThroughError {
if keyList.KeyList() != nil {
columns := mysqlparser.NormalizeKeyList(keyList.KeyList())
if err := t.mysqlValidateColumnList(ctx, columns, primary, isSpatial); err != nil {
@@ -931,19 +978,23 @@ func (t *TableState) mysqlValidateKeyListVariants(ctx *FinderContext, keyList my
return nil
}
-func (t *TableState) mysqlValidateColumnList(ctx *FinderContext, columnList []string, primary bool, isSpatial bool) error {
+func (t *TableState) mysqlValidateColumnList(ctx *FinderContext, columnList []string, primary bool, isSpatial bool) *WalkThroughError {
for _, columnName := range columnList {
column, exists := t.columnSet[strings.ToLower(columnName)]
if !exists {
if ctx.CheckIntegrity {
- return NewSchemaViolationError(405, fmt.Sprintf("Column `%s` does not exist in table `%s`", columnName, t.name))
+ return NewColumnNotExistsError(t.name, columnName)
}
} else {
if primary {
column.nullable = newFalsePointer()
}
if isSpatial && column.nullable != nil && *column.nullable {
- return errors.Errorf("All parts of a SPATIAL index must be NOT NULL, but `%s` is nullable", column.name)
+ return &WalkThroughError{
+ Type: ErrorTypeSpatialIndexKeyNullable,
+ // The error content comes from MySQL.
+ Content: fmt.Sprintf("All parts of a SPATIAL index must be NOT NULL, but `%s` is nullable", column.name),
+ }
}
}
}
@@ -952,7 +1003,7 @@ func (t *TableState) mysqlValidateColumnList(ctx *FinderContext, columnList []st
// mysqlValidateExpressionList validates the expression list.
// TODO: update expression validation.
-func (t *TableState) mysqlValidateExpressionList(_ *FinderContext, expressionList []string, primary bool, isSpatial bool) error {
+func (t *TableState) mysqlValidateExpressionList(_ *FinderContext, expressionList []string, primary bool, isSpatial bool) *WalkThroughError {
for _, expression := range expressionList {
column, exists := t.columnSet[strings.ToLower(expression)]
// If expression is not a column, we do not need to validate it.
@@ -964,7 +1015,11 @@ func (t *TableState) mysqlValidateExpressionList(_ *FinderContext, expressionLis
column.nullable = newFalsePointer()
}
if isSpatial && column.nullable != nil && *column.nullable {
- return errors.Errorf("All parts of a SPATIAL index must be NOT NULL, but `%s` is nullable", column.name)
+ return &WalkThroughError{
+ Type: ErrorTypeSpatialIndexKeyNullable,
+ // The error content comes from MySQL.
+ Content: fmt.Sprintf("All parts of a SPATIAL index must be NOT NULL, but `%s` is nullable", column.name),
+ }
}
}
return nil
@@ -1008,15 +1063,18 @@ func mysqlGetIndexType(tableConstraint mysql.ITableConstraintDefContext) string
return "BTREE"
}
-func (t *TableState) mysqlCreateColumn(ctx *FinderContext, columnName string, fieldDef mysql.IFieldDefinitionContext, position *mysqlColumnPosition) error {
+func (t *TableState) mysqlCreateColumn(ctx *FinderContext, columnName string, fieldDef mysql.IFieldDefinitionContext, position *mysqlColumnPosition) *WalkThroughError {
if _, exists := t.columnSet[strings.ToLower(columnName)]; exists {
- return NewSchemaViolationError(412, fmt.Sprintf("Column `%s` already exists in table `%s`", columnName, t.name))
+ return &WalkThroughError{
+ Type: ErrorTypeColumnExists,
+ Content: fmt.Sprintf("Column `%s` already exists in table `%s`", columnName, t.name),
+ }
}
// todo: handle position.
pos := len(t.columnSet) + 1
if position != nil && ctx.CheckIntegrity {
- var err error
+ var err *WalkThroughError
pos, err = t.mysqlReorderColumn(position)
if err != nil {
return err
@@ -1088,7 +1146,10 @@ func (t *TableState) mysqlCreateColumn(ctx *FinderContext, columnName string, fi
continue
}
if !mysqlparser.IsTimeType(fieldDef.DataType()) {
- return errors.Errorf("Column `%s` use ON UPDATE but is not DATETIME or TIMESTAMP", col.name)
+ return &WalkThroughError{
+ Type: ErrorTypeOnUpdateColumnNotDatetimeOrTimestamp,
+ Content: fmt.Sprintf("Column `%s` use ON UPDATE but is not DATETIME or TIMESTAMP", col.name),
+ }
}
// primary key.
case mysql.MySQLParserKEY_SYMBOL:
@@ -1120,7 +1181,11 @@ func (t *TableState) mysqlCreateColumn(ctx *FinderContext, columnName string, fi
}
if col.nullable != nil && !*col.nullable && setNullDefault {
- return errors.Errorf("Invalid default value for column `%s`", col.name)
+ return &WalkThroughError{
+ Type: ErrorTypeSetNullDefaultForNotNullColumn,
+ // Content comes from MySQL Error content.
+ Content: fmt.Sprintf("Invalid default value for column `%s`", col.name),
+ }
}
t.columnSet[strings.ToLower(col.name)] = col
@@ -1128,7 +1193,7 @@ func (t *TableState) mysqlCreateColumn(ctx *FinderContext, columnName string, fi
}
// reorderColumn reorders the columns for new column and returns the new column position.
-func (t *TableState) mysqlReorderColumn(position *mysqlColumnPosition) (int, error) {
+func (t *TableState) mysqlReorderColumn(position *mysqlColumnPosition) (int, *WalkThroughError) {
switch position.tp {
case ColumnPositionNone:
return len(t.columnSet) + 1, nil
@@ -1141,7 +1206,7 @@ func (t *TableState) mysqlReorderColumn(position *mysqlColumnPosition) (int, err
columnName := strings.ToLower(position.relativeColumn)
column, exist := t.columnSet[columnName]
if !exist {
- return 0, NewSchemaViolationError(405, fmt.Sprintf("Column `%s` does not exist in table `%s`", columnName, t.name))
+ return 0, NewColumnNotExistsError(t.name, columnName)
}
for _, col := range t.columnSet {
if *col.position > *column.position {
@@ -1150,18 +1215,24 @@ func (t *TableState) mysqlReorderColumn(position *mysqlColumnPosition) (int, err
}
return *column.position + 1, nil
default:
- return 0, errors.Errorf("Unsupported column position type: %d", position.tp)
+ return 0, &WalkThroughError{
+ Type: ErrorTypeUnsupported,
+ Content: fmt.Sprintf("Unsupported column position type: %d", position.tp),
+ }
}
}
-func (t *TableState) mysqlCreateIndex(name string, keyList []string, unique bool, tp string, tableConstraint mysql.ITableConstraintDefContext, createIndexDef mysql.ICreateIndexContext) error {
+func (t *TableState) mysqlCreateIndex(name string, keyList []string, unique bool, tp string, tableConstraint mysql.ITableConstraintDefContext, createIndexDef mysql.ICreateIndexContext) *WalkThroughError {
if len(keyList) == 0 {
- return errors.Errorf("Index `%s` in table `%s` has empty key", name, t.name)
+ return &WalkThroughError{
+ Type: ErrorTypeIndexEmptyKeys,
+ Content: fmt.Sprintf("Index `%s` in table `%s` has empty key", name, t.name),
+ }
}
// construct a index name if name is empty.
if name != "" {
if _, exists := t.indexSet[strings.ToLower(name)]; exists {
- return NewSchemaViolationError(805, fmt.Sprintf("Index `%s` already exists in table `%s`", name, t.name))
+ return NewIndexExistsError(t.name, name)
}
} else {
suffix := 1
@@ -1260,9 +1331,12 @@ func (t *TableState) mysqlCreateIndex(name string, keyList []string, unique bool
return nil
}
-func (t *TableState) mysqlCreatePrimaryKey(keys []string, tp string) error {
+func (t *TableState) mysqlCreatePrimaryKey(keys []string, tp string) *WalkThroughError {
if _, exists := t.indexSet[strings.ToLower(PrimaryKeyName)]; exists {
- return errors.Errorf("Primary key exists in table `%s`", t.name)
+ return &WalkThroughError{
+ Type: ErrorTypePrimaryKeyExists,
+ Content: fmt.Sprintf("Primary key exists in table `%s`", t.name),
+ }
}
pk := &IndexState{
@@ -1278,7 +1352,7 @@ func (t *TableState) mysqlCreatePrimaryKey(keys []string, tp string) error {
return nil
}
-func mysqlCheckDefault(columnName string, fieldDefinition mysql.IFieldDefinitionContext) error {
+func mysqlCheckDefault(columnName string, fieldDefinition mysql.IFieldDefinitionContext) *WalkThroughError {
if fieldDefinition.DataType() == nil || fieldDefinition.DataType().GetType_() == nil {
return nil
}
@@ -1303,7 +1377,11 @@ func mysqlCheckDefault(columnName string, fieldDefinition mysql.IFieldDefinition
mysql.MySQLParserMULTILINESTRING_SYMBOL,
mysql.MySQLParserPOLYGON_SYMBOL,
mysql.MySQLParserMULTIPOLYGON_SYMBOL:
- return NewSchemaViolationError(423, fmt.Sprintf("BLOB, TEXT, GEOMETRY or JSON column `%s` can't have a default value", columnName))
+ return &WalkThroughError{
+ Type: ErrorTypeInvalidColumnTypeForDefaultValue,
+ // Content comes from MySQL Error content.
+ Content: fmt.Sprintf("BLOB, TEXT, GEOMETRY or JSON column `%s` can't have a default value", columnName),
+ }
default:
// Other data types are allowed to have default values
}
@@ -1311,7 +1389,7 @@ func mysqlCheckDefault(columnName string, fieldDefinition mysql.IFieldDefinition
return checkDefaultConvert(columnName, fieldDefinition)
}
-func checkDefaultConvert(columnName string, fieldDefinition mysql.IFieldDefinitionContext) error {
+func checkDefaultConvert(columnName string, fieldDefinition mysql.IFieldDefinitionContext) *WalkThroughError {
if fieldDefinition == nil {
return nil
}
diff --git a/backend/plugin/advisor/catalog/walk_through_for_pg.go b/backend/plugin/advisor/catalog/walk_through_for_pg.go
index 8a11bde202..4b81c485e4 100644
--- a/backend/plugin/advisor/catalog/walk_through_for_pg.go
+++ b/backend/plugin/advisor/catalog/walk_through_for_pg.go
@@ -17,7 +17,10 @@ func (d *DatabaseState) pgWalkThrough(ast any) error {
// ANTLR-based walkthrough
parseResult, ok := ast.(*pgparser.ParseResult)
if !ok {
- return errors.Errorf("PostgreSQL walk-through expects *pgparser.ParseResult, got %T", ast)
+ return &WalkThroughError{
+ Type: ErrorTypeUnsupported,
+ Content: fmt.Sprintf("PostgreSQL walk-through expects *pgparser.ParseResult, got %T", ast),
+ }
}
root, ok := parseResult.Tree.(parser.IRootContext)
@@ -47,22 +50,28 @@ type pgCatalogListener struct {
*parser.BasePostgreSQLParserListener
databaseState *DatabaseState
- err error
+ err *WalkThroughError
currentLine int
}
// Helper method to set error with line number
-func (l *pgCatalogListener) setError(err error) {
+func (l *pgCatalogListener) setError(err *WalkThroughError) {
if l.err != nil {
return // Keep first error
}
+ if err != nil && err.Line == 0 {
+ err.Line = l.currentLine
+ }
l.err = err
}
// Helper method to check if database is deleted
func (l *pgCatalogListener) checkDatabaseNotDeleted() bool {
if l.databaseState.deleted {
- l.setError(errors.Errorf(`Database %q is deleted`, l.databaseState.name))
+ l.setError(&WalkThroughError{
+ Type: ErrorTypeDatabaseIsDeleted,
+ Content: fmt.Sprintf(`Database %q is deleted`, l.databaseState.name),
+ })
return false
}
return true
@@ -100,7 +109,10 @@ func (l *pgCatalogListener) EnterCreatestmt(ctx *parser.CreatestmtContext) {
// Check database name if specified
databaseName := extractDatabaseName(qualifiedNames[0])
if databaseName != "" && l.databaseState.name != databaseName {
- l.setError(errors.Errorf("Database %q is not the current database %q", databaseName, l.databaseState.name))
+ l.setError(&WalkThroughError{
+ Type: ErrorTypeAccessOtherDatabase,
+ Content: fmt.Sprintf("Database %q is not the current database %q", databaseName, l.databaseState.name),
+ })
return
}
@@ -118,7 +130,10 @@ func (l *pgCatalogListener) EnterCreatestmt(ctx *parser.CreatestmtContext) {
if ifNotExists {
return
}
- l.setError(errors.Errorf(`The table %q already exists in the schema %q`, tableName, schema.name))
+ l.setError(&WalkThroughError{
+ Type: ErrorTypeTableExists,
+ Content: fmt.Sprintf(`The table %q already exists in the schema %q`, tableName, schema.name),
+ })
return
}
@@ -153,7 +168,7 @@ func (l *pgCatalogListener) EnterCreatestmt(ctx *parser.CreatestmtContext) {
}
// createColumn creates a column in the table.
-func createColumn(schema *SchemaState, table *TableState, columnDef parser.IColumnDefContext) error {
+func createColumn(schema *SchemaState, table *TableState, columnDef parser.IColumnDefContext) *WalkThroughError {
if columnDef == nil {
return nil
}
@@ -169,7 +184,10 @@ func createColumn(schema *SchemaState, table *TableState, columnDef parser.IColu
// Check if column already exists
if _, exists := table.columnSet[columnName]; exists {
- return errors.Errorf("The column %q already exists in table %q", columnName, table.name)
+ return &WalkThroughError{
+ Type: ErrorTypeColumnExists,
+ Content: fmt.Sprintf("The column %q already exists in table %q", columnName, table.name),
+ }
}
// Get column type
@@ -269,7 +287,7 @@ func createColumn(schema *SchemaState, table *TableState, columnDef parser.IColu
}
// createTableConstraint creates a table-level constraint.
-func createTableConstraint(schema *SchemaState, table *TableState, constraint parser.ITableconstraintContext) error {
+func createTableConstraint(schema *SchemaState, table *TableState, constraint parser.ITableconstraintContext) *WalkThroughError {
if constraint == nil || constraint.Constraintelem() == nil {
return nil
}
@@ -300,7 +318,7 @@ func createTableConstraint(schema *SchemaState, table *TableState, constraint pa
if column, exists := table.columnSet[colName]; exists {
column.nullable = newFalsePointer()
} else {
- return NewSchemaViolationError(405, fmt.Sprintf("Column `%s` does not exist in table `%s`", colName, table.name))
+ return NewColumnNotExistsError(table.name, colName)
}
}
@@ -312,7 +330,7 @@ func createTableConstraint(schema *SchemaState, table *TableState, constraint pa
// Check if identifier already exists
if _, exists := schema.identifierMap[pkName]; exists {
- return NewSchemaViolationError(1, fmt.Sprintf("Relation %q already exists in schema %q", pkName, schema.name))
+ return NewRelationExistsError(pkName, schema.name)
}
// Create primary key index
@@ -349,13 +367,13 @@ func createTableConstraint(schema *SchemaState, table *TableState, constraint pa
// Check if identifier already exists
if _, exists := schema.identifierMap[indexName]; exists {
- return NewSchemaViolationError(1, fmt.Sprintf("Relation %q already exists in schema %q", indexName, schema.name))
+ return NewRelationExistsError(indexName, schema.name)
}
// Validate columns exist
for _, colName := range columnList {
if _, exists := table.columnSet[colName]; !exists {
- return NewSchemaViolationError(405, fmt.Sprintf("Column `%s` does not exist in table `%s`", colName, table.name))
+ return NewColumnNotExistsError(table.name, colName)
}
}
@@ -410,7 +428,7 @@ func (l *pgCatalogListener) EnterIndexstmt(ctx *parser.IndexstmtContext) {
table, exists := schema.tableSet[tableName]
if !exists {
- l.setError(NewSchemaViolationError(604, fmt.Sprintf("Table `%s` does not exist", tableName)))
+ l.setError(NewTableNotExistsError(tableName))
return
}
@@ -439,7 +457,10 @@ func (l *pgCatalogListener) EnterIndexstmt(ctx *parser.IndexstmtContext) {
}
if len(columnList) == 0 {
- l.setError(errors.Errorf("Index %q in table %q has empty key", indexName, tableName))
+ l.setError(&WalkThroughError{
+ Type: ErrorTypeIndexEmptyKeys,
+ Content: fmt.Sprintf("Index %q in table %q has empty key", indexName, tableName),
+ })
return
}
@@ -459,7 +480,7 @@ func (l *pgCatalogListener) EnterIndexstmt(ctx *parser.IndexstmtContext) {
if wasAutoGenerated {
indexName = generateUniqueIndexName(schema, tableName, columnList, isUnique)
} else {
- l.setError(NewSchemaViolationError(1, fmt.Sprintf("Relation %q already exists in schema %q", indexName, schema.name)))
+ l.setError(NewRelationExistsError(indexName, schema.name))
return
}
}
@@ -468,7 +489,7 @@ func (l *pgCatalogListener) EnterIndexstmt(ctx *parser.IndexstmtContext) {
for _, colName := range columnList {
if colName != "expr" {
if _, exists := table.columnSet[colName]; !exists {
- l.setError(NewSchemaViolationError(405, fmt.Sprintf("Column `%s` does not exist in table `%s`", colName, tableName)))
+ l.setError(NewColumnNotExistsError(tableName, colName))
return
}
}
@@ -542,7 +563,10 @@ func (l *pgCatalogListener) EnterAltertablestmt(ctx *parser.AltertablestmtContex
// Check database access
if databaseName != "" && l.databaseState.name != databaseName {
- l.setError(errors.Errorf("Database %q is not the current database %q", databaseName, l.databaseState.name))
+ l.setError(&WalkThroughError{
+ Type: ErrorTypeAccessOtherDatabase,
+ Content: fmt.Sprintf("Database %q is not the current database %q", databaseName, l.databaseState.name),
+ })
return
}
@@ -658,7 +682,7 @@ func (l *pgCatalogListener) alterTableDropColumn(schema *SchemaState, table *Tab
if ifExists {
return
}
- l.setError(NewSchemaViolationError(405, fmt.Sprintf("Column `%s` does not exist in table `%s`", columnName, table.name)))
+ l.setError(NewColumnNotExistsError(table.name, columnName))
return
}
@@ -669,7 +693,11 @@ func (l *pgCatalogListener) alterTableDropColumn(schema *SchemaState, table *Tab
return
}
if len(viewList) > 0 {
- l.setError(errors.Errorf("Cannot drop column %q in table %q.%q, it's referenced by view: %s", column.name, schema.name, table.name, strings.Join(viewList, ", ")))
+ l.setError(&WalkThroughError{
+ Type: ErrorTypeColumnIsReferencedByView,
+ Content: fmt.Sprintf("Cannot drop column %q in table %q.%q, it's referenced by view: %s", column.name, schema.name, table.name, strings.Join(viewList, ", ")),
+ Payload: viewList,
+ })
return
}
@@ -712,7 +740,11 @@ func (l *pgCatalogListener) alterTableAlterColumnType(schema *SchemaState, table
return
}
if len(viewList) > 0 {
- l.setError(errors.Errorf("Cannot alter type of column %q in table %q.%q, it's referenced by view: %s", column.name, schema.name, table.name, strings.Join(viewList, ", ")))
+ l.setError(&WalkThroughError{
+ Type: ErrorTypeColumnIsReferencedByView,
+ Content: fmt.Sprintf("Cannot alter type of column %q in table %q.%q, it's referenced by view: %s", column.name, schema.name, table.name, strings.Join(viewList, ", ")),
+ Payload: viewList,
+ })
return
}
@@ -733,7 +765,10 @@ func (l *pgCatalogListener) alterTableAddColumn(schema *SchemaState, table *Tabl
if ifNotExists {
return
}
- l.setError(errors.Errorf("The column %q already exists in table %q", columnName, table.name))
+ l.setError(&WalkThroughError{
+ Type: ErrorTypeColumnExists,
+ Content: fmt.Sprintf("The column %q already exists in table %q", columnName, table.name),
+ })
return
}
@@ -815,7 +850,7 @@ func (l *pgCatalogListener) alterTableAddColumn(schema *SchemaState, table *Tabl
}
// Check for collision
if _, exists := schema.identifierMap[constraintName]; exists {
- l.setError(NewSchemaViolationError(1, fmt.Sprintf("Relation %q already exists in schema %q", constraintName, schema.name)))
+ l.setError(NewRelationExistsError(constraintName, schema.name))
return
}
columnState.nullable = newFalsePointer()
@@ -858,7 +893,10 @@ func (l *pgCatalogListener) alterTableDropConstraint(schema *SchemaState, table
}
if !ifExists {
- l.setError(errors.Errorf("Constraint %q for table %q does not exist", constraintName, table.name))
+ l.setError(&WalkThroughError{
+ Type: ErrorTypeConstraintNotExists,
+ Content: fmt.Sprintf("Constraint %q for table %q does not exist", constraintName, table.name),
+ })
}
}
@@ -896,10 +934,10 @@ func (l *pgCatalogListener) alterTableSetNotNull(table *TableState, columnName s
}
// renameTable handles RENAME TO for tables.
-func (*pgCatalogListener) renameTable(schema *SchemaState, table *TableState, newName string) error {
+func (*pgCatalogListener) renameTable(schema *SchemaState, table *TableState, newName string) *WalkThroughError {
// Check if new name already exists
if _, exists := schema.identifierMap[newName]; exists {
- return NewSchemaViolationError(1, fmt.Sprintf("Relation %q already exists in schema %q", newName, schema.name))
+ return NewRelationExistsError(newName, schema.name)
}
// Remove old name from maps
@@ -917,7 +955,7 @@ func (*pgCatalogListener) renameTable(schema *SchemaState, table *TableState, ne
}
// renameColumn handles RENAME COLUMN.
-func (*pgCatalogListener) renameColumn(table *TableState, oldName string, newName string) error {
+func (*pgCatalogListener) renameColumn(table *TableState, oldName string, newName string) *WalkThroughError {
column, err := table.getColumn(oldName)
if err != nil {
return err
@@ -929,7 +967,10 @@ func (*pgCatalogListener) renameColumn(table *TableState, oldName string, newNam
// Check if new name already exists
if _, exists := table.columnSet[newName]; exists {
- return errors.Errorf("The column %q already exists in table %q", newName, table.name)
+ return &WalkThroughError{
+ Type: ErrorTypeColumnExists,
+ Content: fmt.Sprintf("The column %q already exists in table %q", newName, table.name),
+ }
}
// Rename column in all indexes that reference it
@@ -950,7 +991,7 @@ func (*pgCatalogListener) renameColumn(table *TableState, oldName string, newNam
}
// renameConstraint handles RENAME CONSTRAINT.
-func (*pgCatalogListener) renameConstraint(schema *SchemaState, table *TableState, oldName string, newName string) error {
+func (*pgCatalogListener) renameConstraint(schema *SchemaState, table *TableState, oldName string, newName string) *WalkThroughError {
index, exists := table.indexSet[oldName]
if !exists {
// We haven't dealt with foreign and check constraints, so skip if not exists
@@ -959,7 +1000,7 @@ func (*pgCatalogListener) renameConstraint(schema *SchemaState, table *TableStat
// Check if new name already exists
if _, exists := schema.identifierMap[newName]; exists {
- return NewSchemaViolationError(1, fmt.Sprintf("Relation %q already exists in schema %q", newName, schema.name))
+ return NewRelationExistsError(newName, schema.name)
}
// Remove old name from maps
@@ -1043,7 +1084,7 @@ func (l *pgCatalogListener) EnterDropstmt(ctx *parser.DropstmtContext) {
}
}
-func (l *pgCatalogListener) dropTable(anyName parser.IAny_nameContext, ifExists bool) error {
+func (l *pgCatalogListener) dropTable(anyName parser.IAny_nameContext, ifExists bool) *WalkThroughError {
parts := pgparser.NormalizePostgreSQLAnyName(anyName)
if len(parts) == 0 {
return nil
@@ -1080,7 +1121,11 @@ func (l *pgCatalogListener) dropTable(anyName parser.IAny_nameContext, ifExists
return err
}
if len(viewList) > 0 {
- return errors.Errorf("Cannot drop table %q.%q, it's referenced by view: %s", schema.name, table.name, strings.Join(viewList, ", "))
+ return &WalkThroughError{
+ Type: ErrorTypeTableIsReferencedByView,
+ Content: fmt.Sprintf("Cannot drop table %q.%q, it's referenced by view: %s", schema.name, table.name, strings.Join(viewList, ", ")),
+ Payload: viewList,
+ }
}
// Delete all indexes associated with the table
@@ -1093,7 +1138,7 @@ func (l *pgCatalogListener) dropTable(anyName parser.IAny_nameContext, ifExists
return nil
}
-func (l *pgCatalogListener) dropView(anyName parser.IAny_nameContext, ifExists bool) error {
+func (l *pgCatalogListener) dropView(anyName parser.IAny_nameContext, ifExists bool) *WalkThroughError {
parts := pgparser.NormalizePostgreSQLAnyName(anyName)
if len(parts) == 0 {
return nil
@@ -1121,7 +1166,7 @@ func (l *pgCatalogListener) dropView(anyName parser.IAny_nameContext, ifExists b
return nil
}
-func (l *pgCatalogListener) dropIndex(anyName parser.IAny_nameContext, ifExists bool) error {
+func (l *pgCatalogListener) dropIndex(anyName parser.IAny_nameContext, ifExists bool) *WalkThroughError {
parts := pgparser.NormalizePostgreSQLAnyName(anyName)
if len(parts) == 0 {
return nil
@@ -1157,7 +1202,7 @@ func (l *pgCatalogListener) dropIndex(anyName parser.IAny_nameContext, ifExists
return nil
}
-func (l *pgCatalogListener) dropSchema(schemaNameCtx parser.INameContext, ifExists bool) error {
+func (l *pgCatalogListener) dropSchema(schemaNameCtx parser.INameContext, ifExists bool) *WalkThroughError {
schemaName := pgparser.NormalizePostgreSQLName(schemaNameCtx)
schema, exists := l.databaseState.schemaSet[schemaName]
@@ -1165,7 +1210,10 @@ func (l *pgCatalogListener) dropSchema(schemaNameCtx parser.INameContext, ifExis
if ifExists {
return nil
}
- return errors.Errorf("Schema %q does not exist", schemaName)
+ return &WalkThroughError{
+ Type: ErrorTypeSchemaNotExists,
+ Content: fmt.Sprintf("Schema %q does not exist", schemaName),
+ }
}
// Delete all identifiers in this schema
@@ -1366,7 +1414,10 @@ func (l *pgCatalogListener) EnterViewstmt(ctx *parser.ViewstmtContext) {
// Check if accessing other database
if databaseName != "" && l.databaseState.name != databaseName {
- l.setError(errors.Errorf("Database %q is not the current database %q", databaseName, l.databaseState.name))
+ l.setError(&WalkThroughError{
+ Type: ErrorTypeAccessOtherDatabase,
+ Content: fmt.Sprintf("Database %q is not the current database %q", databaseName, l.databaseState.name),
+ })
return
}
diff --git a/backend/plugin/advisor/catalog/walk_through_for_tidb.go b/backend/plugin/advisor/catalog/walk_through_for_tidb.go
index 698c21c322..29da7d52bd 100644
--- a/backend/plugin/advisor/catalog/walk_through_for_tidb.go
+++ b/backend/plugin/advisor/catalog/walk_through_for_tidb.go
@@ -33,9 +33,20 @@ func (d *DatabaseState) tidbWalkThrough(ast any) error {
return nil
}
-func (d *DatabaseState) changeState(in tidbast.StmtNode) error {
+func (d *DatabaseState) changeState(in tidbast.StmtNode) (err *WalkThroughError) {
+ defer func() {
+ if err == nil {
+ return
+ }
+ if err.Line == 0 {
+ err.Line = in.OriginTextPosition()
+ }
+ }()
if d.deleted {
- return NewSchemaViolationError(703, fmt.Sprintf("Database `%s` is deleted", d.name))
+ return &WalkThroughError{
+ Type: ErrorTypeDatabaseIsDeleted,
+ Content: fmt.Sprintf("Database `%s` is deleted", d.name),
+ }
}
switch node := in.(type) {
case *tidbast.CreateTableStmt:
@@ -53,7 +64,7 @@ func (d *DatabaseState) changeState(in tidbast.StmtNode) error {
case *tidbast.DropDatabaseStmt:
return d.dropDatabase(node)
case *tidbast.CreateDatabaseStmt:
- return NewSchemaViolationError(702, fmt.Sprintf("Database `%s` is not the current database `%s`", node.Name.O, d.name))
+ return NewAccessOtherDatabaseError(d.name, node.Name.O)
case *tidbast.RenameTableStmt:
return d.renameTable(node)
default:
@@ -61,7 +72,7 @@ func (d *DatabaseState) changeState(in tidbast.StmtNode) error {
}
}
-func (d *DatabaseState) renameTable(node *tidbast.RenameTableStmt) error {
+func (d *DatabaseState) renameTable(node *tidbast.RenameTableStmt) *WalkThroughError {
for _, tableToTable := range node.TableToTables {
schema, exists := d.schemaSet[""]
if !exists {
@@ -76,12 +87,12 @@ func (d *DatabaseState) renameTable(node *tidbast.RenameTableStmt) error {
table, exists := schema.getTable(oldTableName)
if !exists {
if schema.ctx.CheckIntegrity {
- return NewSchemaViolationError(604, fmt.Sprintf("Table `%s` does not exist", oldTableName))
+ return NewTableNotExistsError(oldTableName)
}
table = schema.createIncompleteTable(oldTableName)
}
if _, exists := schema.getTable(newTableName); exists {
- return NewSchemaViolationError(607, fmt.Sprintf("Table `%s` already exists", newTableName))
+ return NewTableExistsError(newTableName)
}
delete(schema.tableSet, table.name)
table.name = newTableName
@@ -89,11 +100,11 @@ func (d *DatabaseState) renameTable(node *tidbast.RenameTableStmt) error {
} else if d.moveToOtherDatabase(tableToTable) {
_, exists := schema.getTable(tableToTable.OldTable.Name.O)
if !exists && schema.ctx.CheckIntegrity {
- return NewSchemaViolationError(604, fmt.Sprintf("Table `%s` does not exist", tableToTable.OldTable.Name.O))
+ return NewTableNotExistsError(tableToTable.OldTable.Name.O)
}
delete(schema.tableSet, tableToTable.OldTable.Name.O)
} else {
- return NewSchemaViolationError(702, fmt.Sprintf("Database `%s` is not the current database `%s`", d.targetDatabase(tableToTable), d.name))
+ return NewAccessOtherDatabaseError(d.name, d.targetDatabase(tableToTable))
}
}
return nil
@@ -123,18 +134,18 @@ func (d *DatabaseState) theCurrentDatabase(node *tidbast.TableToTable) bool {
return true
}
-func (d *DatabaseState) dropDatabase(node *tidbast.DropDatabaseStmt) error {
+func (d *DatabaseState) dropDatabase(node *tidbast.DropDatabaseStmt) *WalkThroughError {
if !d.isCurrentDatabase(node.Name.O) {
- return NewSchemaViolationError(702, fmt.Sprintf("Database `%s` is not the current database `%s`", node.Name.O, d.name))
+ return NewAccessOtherDatabaseError(d.name, node.Name.O)
}
d.deleted = true
return nil
}
-func (d *DatabaseState) alterDatabase(node *tidbast.AlterDatabaseStmt) error {
+func (d *DatabaseState) alterDatabase(node *tidbast.AlterDatabaseStmt) *WalkThroughError {
if !node.AlterDefaultDatabase && !d.isCurrentDatabase(node.Name.O) {
- return NewSchemaViolationError(702, fmt.Sprintf("Database `%s` is not the current database `%s`", node.Name.O, d.name))
+ return NewAccessOtherDatabaseError(d.name, node.Name.O)
}
for _, option := range node.Options {
@@ -150,9 +161,9 @@ func (d *DatabaseState) alterDatabase(node *tidbast.AlterDatabaseStmt) error {
return nil
}
-func (d *DatabaseState) findTableState(tableName *tidbast.TableName) (*TableState, error) {
+func (d *DatabaseState) findTableState(tableName *tidbast.TableName) (*TableState, *WalkThroughError) {
if tableName.Schema.O != "" && !d.isCurrentDatabase(tableName.Schema.O) {
- return nil, NewSchemaViolationError(702, fmt.Sprintf("Database `%s` is not the current database `%s`", tableName.Schema.O, d.name))
+ return nil, NewAccessOtherDatabaseError(d.name, tableName.Schema.O)
}
schema, exists := d.schemaSet[""]
@@ -163,7 +174,7 @@ func (d *DatabaseState) findTableState(tableName *tidbast.TableName) (*TableStat
table, exists := schema.getTable(tableName.Name.O)
if !exists {
if schema.ctx.CheckIntegrity {
- return nil, NewSchemaViolationError(604, fmt.Sprintf("Table `%s` does not exist", tableName.Name.O))
+ return nil, NewTableNotExistsError(tableName.Name.O)
}
table = schema.createIncompleteTable(tableName.Name.O)
}
@@ -171,7 +182,7 @@ func (d *DatabaseState) findTableState(tableName *tidbast.TableName) (*TableStat
return table, nil
}
-func (d *DatabaseState) dropIndex(node *tidbast.DropIndexStmt) error {
+func (d *DatabaseState) dropIndex(node *tidbast.DropIndexStmt) *WalkThroughError {
table, err := d.findTableState(node.Table)
if err != nil {
return err
@@ -180,7 +191,7 @@ func (d *DatabaseState) dropIndex(node *tidbast.DropIndexStmt) error {
return table.dropIndex(d.ctx, node.IndexName)
}
-func (d *DatabaseState) createIndex(node *tidbast.CreateIndexStmt) error {
+func (d *DatabaseState) createIndex(node *tidbast.CreateIndexStmt) *WalkThroughError {
table, err := d.findTableState(node.Table)
if err != nil {
return err
@@ -211,7 +222,7 @@ func (d *DatabaseState) createIndex(node *tidbast.CreateIndexStmt) error {
return table.createIndex(node.IndexName, keyList, unique, tp, node.IndexOption)
}
-func (d *DatabaseState) alterTable(node *tidbast.AlterTableStmt) error {
+func (d *DatabaseState) alterTable(node *tidbast.AlterTableStmt) *WalkThroughError {
table, err := d.findTableState(node.Table)
if err != nil {
return err
@@ -303,11 +314,11 @@ func (d *DatabaseState) alterTable(node *tidbast.AlterTableStmt) error {
return nil
}
-func (t *TableState) changeIndexVisibility(ctx *FinderContext, indexName string, visibility tidbast.IndexVisibility) error {
+func (t *TableState) changeIndexVisibility(ctx *FinderContext, indexName string, visibility tidbast.IndexVisibility) *WalkThroughError {
index, exists := t.indexSet[strings.ToLower(indexName)]
if !exists {
if ctx.CheckIntegrity {
- return NewSchemaViolationError(809, fmt.Sprintf("Index `%s` does not exist in table `%s`", indexName, t.name))
+ return NewIndexNotExistsError(t.name, indexName)
}
index = t.createIncompleteIndex(indexName)
}
@@ -322,7 +333,7 @@ func (t *TableState) changeIndexVisibility(ctx *FinderContext, indexName string,
return nil
}
-func (t *TableState) renameIndex(ctx *FinderContext, oldName string, newName string) error {
+func (t *TableState) renameIndex(ctx *FinderContext, oldName string, newName string) *WalkThroughError {
// For MySQL, the primary key has a special name 'PRIMARY'.
// And the other indexes cannot use the name which case-insensitive equals 'PRIMARY'.
if strings.ToUpper(oldName) == PrimaryKeyName || strings.ToUpper(newName) == PrimaryKeyName {
@@ -330,19 +341,22 @@ func (t *TableState) renameIndex(ctx *FinderContext, oldName string, newName str
if strings.ToUpper(oldName) != PrimaryKeyName {
incorrectName = newName
}
- return errors.Errorf("Incorrect index name `%s`", incorrectName)
+ return &WalkThroughError{
+ Type: ErrorTypeIncorrectIndexName,
+ Content: fmt.Sprintf("Incorrect index name `%s`", incorrectName),
+ }
}
index, exists := t.indexSet[strings.ToLower(oldName)]
if !exists {
if ctx.CheckIntegrity {
- return NewSchemaViolationError(809, fmt.Sprintf("Index `%s` does not exist in table `%s`", oldName, t.name))
+ return NewIndexNotExistsError(t.name, oldName)
}
index = t.createIncompleteIndex(oldName)
}
if _, exists := t.indexSet[strings.ToLower(newName)]; exists {
- return NewSchemaViolationError(805, fmt.Sprintf("Index `%s` already exists in table `%s`", newName, t.name))
+ return NewIndexExistsError(t.name, newName)
}
index.name = newName
@@ -351,12 +365,12 @@ func (t *TableState) renameIndex(ctx *FinderContext, oldName string, newName str
return nil
}
-func (t *TableState) changeColumnDefault(ctx *FinderContext, column *tidbast.ColumnDef) error {
+func (t *TableState) changeColumnDefault(ctx *FinderContext, column *tidbast.ColumnDef) *WalkThroughError {
columnName := column.Name.Name.L
colState, exists := t.columnSet[columnName]
if !exists {
if ctx.CheckIntegrity {
- return NewSchemaViolationError(405, fmt.Sprintf("Column `%s` does not exist in table `%s`", columnName, t.name))
+ return NewColumnNotExistsError(t.name, columnName)
}
colState = t.createIncompleteColumn(columnName)
}
@@ -370,7 +384,11 @@ func (t *TableState) changeColumnDefault(ctx *FinderContext, column *tidbast.Col
"text", "tinytext", "mediumtext", "longtext",
"json",
"geometry":
- return NewSchemaViolationError(423, fmt.Sprintf("BLOB, TEXT, GEOMETRY or JSON column `%s` can't have a default value", columnName))
+ return &WalkThroughError{
+ Type: ErrorTypeInvalidColumnTypeForDefaultValue,
+ // Content comes from MySQL Error content.
+ Content: fmt.Sprintf("BLOB, TEXT, GEOMETRY or JSON column `%s` can't have a default value", columnName),
+ }
default:
// Other column types allow default values
}
@@ -383,7 +401,11 @@ func (t *TableState) changeColumnDefault(ctx *FinderContext, column *tidbast.Col
colState.defaultValue = &defaultValue
} else {
if colState.nullable != nil && !*colState.nullable {
- return errors.Errorf("Invalid default value for column `%s`", columnName)
+ return &WalkThroughError{
+ Type: ErrorTypeSetNullDefaultForNotNullColumn,
+ // Content comes from MySQL Error content.
+ Content: fmt.Sprintf("Invalid default value for column `%s`", columnName),
+ }
}
colState.defaultValue = nil
}
@@ -394,7 +416,7 @@ func (t *TableState) changeColumnDefault(ctx *FinderContext, column *tidbast.Col
return nil
}
-func (t *TableState) renameColumn(ctx *FinderContext, oldName string, newName string) error {
+func (t *TableState) renameColumn(ctx *FinderContext, oldName string, newName string) *WalkThroughError {
if strings.EqualFold(oldName, newName) {
return nil
}
@@ -402,13 +424,16 @@ func (t *TableState) renameColumn(ctx *FinderContext, oldName string, newName st
column, exists := t.columnSet[strings.ToLower(oldName)]
if !exists {
if ctx.CheckIntegrity {
- return NewSchemaViolationError(405, fmt.Sprintf("Column `%s` does not exist in table `%s`", oldName, t.name))
+ return NewColumnNotExistsError(t.name, oldName)
}
column = t.createIncompleteColumn(oldName)
}
if _, exists := t.columnSet[strings.ToLower(newName)]; exists {
- return NewSchemaViolationError(412, fmt.Sprintf("Column `%s` already exists in table `%s", newName, t.name))
+ return &WalkThroughError{
+ Type: ErrorTypeColumnExists,
+ Content: fmt.Sprintf("Column `%s` already exists in table `%s", newName, t.name),
+ }
}
column.name = newName
@@ -437,10 +462,10 @@ func (t *TableState) renameColumnInIndexKey(oldName string, newName string) {
// 1. drop column from tableState.columnSet, but do not drop column from indexSet.
// 2. rename column from indexSet.
// 3. create a new column in columnSet.
-func (t *TableState) completeTableChangeColumn(ctx *FinderContext, oldName string, newColumn *tidbast.ColumnDef, position *tidbast.ColumnPosition) error {
+func (t *TableState) completeTableChangeColumn(ctx *FinderContext, oldName string, newColumn *tidbast.ColumnDef, position *tidbast.ColumnPosition) *WalkThroughError {
column, exists := t.columnSet[strings.ToLower(oldName)]
if !exists {
- return NewSchemaViolationError(405, fmt.Sprintf("Column `%s` does not exist in table `%s`", oldName, t.name))
+ return NewColumnNotExistsError(t.name, oldName)
}
pos := *column.position
@@ -490,7 +515,7 @@ func (t *TableState) completeTableChangeColumn(ctx *FinderContext, oldName strin
// incompleteTableChangeColumn changes column definition.
// It does not maintain the position of the column.
-func (t *TableState) incompleteTableChangeColumn(ctx *FinderContext, oldName string, newColumn *tidbast.ColumnDef, position *tidbast.ColumnPosition) error {
+func (t *TableState) incompleteTableChangeColumn(ctx *FinderContext, oldName string, newColumn *tidbast.ColumnDef, position *tidbast.ColumnPosition) *WalkThroughError {
delete(t.columnSet, strings.ToLower(oldName))
// rename column from indexSet
@@ -500,20 +525,23 @@ func (t *TableState) incompleteTableChangeColumn(ctx *FinderContext, oldName str
return t.createColumn(ctx, newColumn, position)
}
-func (t *TableState) changeColumn(ctx *FinderContext, oldName string, newColumn *tidbast.ColumnDef, position *tidbast.ColumnPosition) error {
+func (t *TableState) changeColumn(ctx *FinderContext, oldName string, newColumn *tidbast.ColumnDef, position *tidbast.ColumnPosition) *WalkThroughError {
if ctx.CheckIntegrity {
return t.completeTableChangeColumn(ctx, oldName, newColumn, position)
}
return t.incompleteTableChangeColumn(ctx, oldName, newColumn, position)
}
-func (t *TableState) dropIndex(ctx *FinderContext, indexName string) error {
+func (t *TableState) dropIndex(ctx *FinderContext, indexName string) *WalkThroughError {
if ctx.CheckIntegrity {
if _, exists := t.indexSet[strings.ToLower(indexName)]; !exists {
if strings.EqualFold(indexName, PrimaryKeyName) {
- return NewSchemaViolationError(808, fmt.Sprintf("Primary key does not exist in table `%s`", t.name))
+ return &WalkThroughError{
+ Type: ErrorTypePrimaryKeyNotExists,
+ Content: fmt.Sprintf("Primary key does not exist in table `%s`", t.name),
+ }
}
- return NewSchemaViolationError(809, fmt.Sprintf("Index `%s` does not exist in table `%s`", indexName, t.name))
+ return NewIndexNotExistsError(t.name, indexName)
}
}
@@ -521,15 +549,19 @@ func (t *TableState) dropIndex(ctx *FinderContext, indexName string) error {
return nil
}
-func (t *TableState) completeTableDropColumn(columnName string) error {
+func (t *TableState) completeTableDropColumn(columnName string) *WalkThroughError {
column, exists := t.columnSet[strings.ToLower(columnName)]
if !exists {
- return NewSchemaViolationError(405, fmt.Sprintf("Column `%s` does not exist in table `%s`", columnName, t.name))
+ return NewColumnNotExistsError(t.name, columnName)
}
// Cannot drop all columns in a table using ALTER TABLE DROP COLUMN.
if len(t.columnSet) == 1 {
- return errors.Errorf("Can't delete all columns with ALTER TABLE; use DROP TABLE %s instead", t.name)
+ return &WalkThroughError{
+ Type: ErrorTypeDropAllColumns,
+ // Error content comes from MySQL error content.
+ Content: fmt.Sprintf("Can't delete all columns with ALTER TABLE; use DROP TABLE %s instead", t.name),
+ }
}
// If columns are dropped from a table, the columns are also removed from any index of which they are a part.
@@ -552,7 +584,7 @@ func (t *TableState) completeTableDropColumn(columnName string) error {
return nil
}
-func (t *TableState) incompleteTableDropColumn(columnName string) error {
+func (t *TableState) incompleteTableDropColumn(columnName string) *WalkThroughError {
// If columns are dropped from a table, the columns are also removed from any index of which they are a part.
for _, index := range t.indexSet {
if len(index.expressionList) == 0 {
@@ -569,7 +601,7 @@ func (t *TableState) incompleteTableDropColumn(columnName string) error {
return nil
}
-func (t *TableState) dropColumn(ctx *FinderContext, columnName string) error {
+func (t *TableState) dropColumn(ctx *FinderContext, columnName string) *WalkThroughError {
if ctx.CheckIntegrity {
return t.completeTableDropColumn(columnName)
}
@@ -591,7 +623,7 @@ func (idx *IndexState) dropColumn(columnName string) {
}
// reorderColumn reorders the columns for new column and returns the new column position.
-func (t *TableState) reorderColumn(position *tidbast.ColumnPosition) (int, error) {
+func (t *TableState) reorderColumn(position *tidbast.ColumnPosition) (int, *WalkThroughError) {
switch position.Tp {
case tidbast.ColumnPositionNone:
return len(t.columnSet) + 1, nil
@@ -604,7 +636,7 @@ func (t *TableState) reorderColumn(position *tidbast.ColumnPosition) (int, error
columnName := position.RelativeColumn.Name.L
column, exist := t.columnSet[columnName]
if !exist {
- return 0, NewSchemaViolationError(405, fmt.Sprintf("Column `%s` does not exist in table `%s`", columnName, t.name))
+ return 0, NewColumnNotExistsError(t.name, columnName)
}
for _, col := range t.columnSet {
if *col.position > *column.position {
@@ -613,16 +645,22 @@ func (t *TableState) reorderColumn(position *tidbast.ColumnPosition) (int, error
}
return *column.position + 1, nil
default:
- return 0, errors.Errorf("Unsupported column position type: %d", position.Tp)
+ return 0, &WalkThroughError{
+ Type: ErrorTypeUnsupported,
+ Content: fmt.Sprintf("Unsupported column position type: %d", position.Tp),
+ }
}
}
-func (d *DatabaseState) dropTable(node *tidbast.DropTableStmt) error {
+func (d *DatabaseState) dropTable(node *tidbast.DropTableStmt) *WalkThroughError {
// TODO(rebelice): deal with DROP VIEW statement.
if !node.IsView {
for _, name := range node.Tables {
if name.Schema.O != "" && !d.isCurrentDatabase(name.Schema.O) {
- return NewSchemaViolationError(702, fmt.Sprintf("Database `%s` is not the current database `%s`", name.Schema.O, d.name))
+ return &WalkThroughError{
+ Type: ErrorTypeAccessOtherDatabase,
+ Content: fmt.Sprintf("Database `%s` is not the current database `%s`", name.Schema.O, d.name),
+ }
}
schema, exists := d.schemaSet[""]
@@ -635,7 +673,10 @@ func (d *DatabaseState) dropTable(node *tidbast.DropTableStmt) error {
if node.IfExists || !d.ctx.CheckIntegrity {
return nil
}
- return NewSchemaViolationError(604, fmt.Sprintf("Table `%s` does not exist", name.Name.O))
+ return &WalkThroughError{
+ Type: ErrorTypeTableNotExists,
+ Content: fmt.Sprintf("Table `%s` does not exist", name.Name.O),
+ }
}
delete(schema.tableSet, table.name)
@@ -644,11 +685,14 @@ func (d *DatabaseState) dropTable(node *tidbast.DropTableStmt) error {
return nil
}
-func (d *DatabaseState) copyTable(node *tidbast.CreateTableStmt) error {
+func (d *DatabaseState) copyTable(node *tidbast.CreateTableStmt) *WalkThroughError {
targetTable, err := d.findTableState(node.ReferTable)
if err != nil {
- if strings.Contains(err.Error(), "is not the current database") {
- return errors.Errorf("Reference table `%s` in other database `%s`, skip walkthrough", node.ReferTable.Name.O, node.ReferTable.Schema.O)
+ if err.Type == ErrorTypeAccessOtherDatabase {
+ return &WalkThroughError{
+ Type: ErrorTypeReferenceOtherDatabase,
+ Content: fmt.Sprintf("Reference table `%s` in other database `%s`, skip walkthrough", node.ReferTable.Name.O, node.ReferTable.Schema.O),
+ }
}
}
@@ -659,9 +703,12 @@ func (d *DatabaseState) copyTable(node *tidbast.CreateTableStmt) error {
return nil
}
-func (d *DatabaseState) createTable(node *tidbast.CreateTableStmt) error {
+func (d *DatabaseState) createTable(node *tidbast.CreateTableStmt) *WalkThroughError {
if node.Table.Schema.O != "" && !d.isCurrentDatabase(node.Table.Schema.O) {
- return NewSchemaViolationError(702, fmt.Sprintf("Database `%s` is not the current database `%s`", node.Table.Schema.O, d.name))
+ return &WalkThroughError{
+ Type: ErrorTypeAccessOtherDatabase,
+ Content: fmt.Sprintf("Database `%s` is not the current database `%s`", node.Table.Schema.O, d.name),
+ }
}
schema, exists := d.schemaSet[""]
@@ -673,11 +720,17 @@ func (d *DatabaseState) createTable(node *tidbast.CreateTableStmt) error {
if node.IfNotExists {
return nil
}
- return NewSchemaViolationError(607, fmt.Sprintf("Table `%s` already exists", node.Table.Name.O))
+ return &WalkThroughError{
+ Type: ErrorTypeTableExists,
+ Content: fmt.Sprintf("Table `%s` already exists", node.Table.Name.O),
+ }
}
if node.Select != nil {
- return NewSchemaViolationError(205, fmt.Sprintf("Disallow the CREATE TABLE AS statement but \"%s\" uses", node.Text()))
+ return &WalkThroughError{
+ Type: ErrorTypeUseCreateTableAs,
+ Content: fmt.Sprintf("Disallow the CREATE TABLE AS statement but \"%s\" uses", node.Text()),
+ }
}
if node.ReferTable != nil {
@@ -698,17 +751,23 @@ func (d *DatabaseState) createTable(node *tidbast.CreateTableStmt) error {
for _, column := range node.Cols {
if isAutoIncrement(column) {
if hasAutoIncrement {
- return NewSchemaViolationError(1, fmt.Sprintf("There can be only one auto column for table `%s`", table.name))
+ return &WalkThroughError{
+ Type: ErrorTypeAutoIncrementExists,
+ // The content comes from MySQL error content.
+ Content: fmt.Sprintf("There can be only one auto column for table `%s`", table.name),
+ }
}
hasAutoIncrement = true
}
if err := table.createColumn(d.ctx, column, nil /* position */); err != nil {
+ err.Line = column.OriginTextPosition()
return err
}
}
for _, constraint := range node.Constraints {
if err := table.createConstraint(d.ctx, constraint); err != nil {
+ err.Line = constraint.OriginTextPosition()
return err
}
}
@@ -716,7 +775,7 @@ func (d *DatabaseState) createTable(node *tidbast.CreateTableStmt) error {
return nil
}
-func (t *TableState) createConstraint(ctx *FinderContext, constraint *tidbast.Constraint) error {
+func (t *TableState) createConstraint(ctx *FinderContext, constraint *tidbast.Constraint) *WalkThroughError {
switch constraint.Tp {
case tidbast.ConstraintPrimaryKey:
keyList, err := t.validateAndGetKeyStringList(ctx, constraint.Keys, true /* primary */, false /* isSpatial */)
@@ -761,7 +820,7 @@ func (t *TableState) createConstraint(ctx *FinderContext, constraint *tidbast.Co
return nil
}
-func (t *TableState) validateAndGetKeyStringList(ctx *FinderContext, keyList []*tidbast.IndexPartSpecification, primary bool, isSpatial bool) ([]string, error) {
+func (t *TableState) validateAndGetKeyStringList(ctx *FinderContext, keyList []*tidbast.IndexPartSpecification, primary bool, isSpatial bool) ([]string, *WalkThroughError) {
var res []string
for _, key := range keyList {
if key.Expr != nil {
@@ -775,14 +834,18 @@ func (t *TableState) validateAndGetKeyStringList(ctx *FinderContext, keyList []*
column, exists := t.columnSet[columnName]
if !exists {
if ctx.CheckIntegrity {
- return nil, NewSchemaViolationError(405, fmt.Sprintf("Column `%s` does not exist in table `%s`", columnName, t.name))
+ return nil, NewColumnNotExistsError(t.name, columnName)
}
} else {
if primary {
column.nullable = newFalsePointer()
}
if isSpatial && column.nullable != nil && *column.nullable {
- return nil, errors.Errorf("All parts of a SPATIAL index must be NOT NULL, but `%s` is nullable", column.name)
+ return nil, &WalkThroughError{
+ Type: ErrorTypeSpatialIndexKeyNullable,
+ // The error content comes from MySQL.
+ Content: fmt.Sprintf("All parts of a SPATIAL index must be NOT NULL, but `%s` is nullable", column.name),
+ }
}
}
@@ -801,11 +864,15 @@ func isAutoIncrement(column *tidbast.ColumnDef) bool {
return false
}
-func checkDefault(columnName string, columnType *types.FieldType, value tidbast.ExprNode) error {
+func checkDefault(columnName string, columnType *types.FieldType, value tidbast.ExprNode) *WalkThroughError {
if value.GetType().GetType() != mysql.TypeNull {
switch columnType.GetType() {
case mysql.TypeBlob, mysql.TypeTinyBlob, mysql.TypeMediumBlob, mysql.TypeLongBlob, mysql.TypeJSON, mysql.TypeGeometry:
- return NewSchemaViolationError(423, fmt.Sprintf("BLOB, TEXT, GEOMETRY or JSON column `%s` can't have a default value", columnName))
+ return &WalkThroughError{
+ Type: ErrorTypeInvalidColumnTypeForDefaultValue,
+ // Content comes from MySQL Error content.
+ Content: fmt.Sprintf("BLOB, TEXT, GEOMETRY or JSON column `%s` can't have a default value", columnName),
+ }
default:
// Other column types allow default values
}
@@ -814,20 +881,26 @@ func checkDefault(columnName string, columnType *types.FieldType, value tidbast.
if valueExpr, yes := value.(tidbast.ValueExpr); yes {
datum := types.NewDatum(valueExpr.GetValue())
if _, err := datum.ConvertTo(types.Context{}, columnType); err != nil {
- return errors.Errorf("%s", err.Error())
+ return &WalkThroughError{
+ Type: ErrorTypeInvalidColumnTypeForDefaultValue,
+ Content: err.Error(),
+ }
}
}
return nil
}
-func (t *TableState) createColumn(ctx *FinderContext, column *tidbast.ColumnDef, position *tidbast.ColumnPosition) error {
+func (t *TableState) createColumn(ctx *FinderContext, column *tidbast.ColumnDef, position *tidbast.ColumnPosition) *WalkThroughError {
if _, exists := t.columnSet[column.Name.Name.L]; exists {
- return NewSchemaViolationError(412, fmt.Sprintf("Column `%s` already exists in table `%s`", column.Name.Name.O, t.name))
+ return &WalkThroughError{
+ Type: ErrorTypeColumnExists,
+ Content: fmt.Sprintf("Column `%s` already exists in table `%s`", column.Name.Name.O, t.name),
+ }
}
pos := len(t.columnSet) + 1
if position != nil && ctx.CheckIntegrity {
- var err error
+ var err *WalkThroughError
pos, err = t.reorderColumn(position)
if err != nil {
return err
@@ -880,7 +953,10 @@ func (t *TableState) createColumn(ctx *FinderContext, column *tidbast.ColumnDef,
case tidbast.ColumnOptionOnUpdate:
// we do not deal with ON UPDATE
if column.Tp.GetType() != mysql.TypeDatetime && column.Tp.GetType() != mysql.TypeTimestamp {
- return errors.Errorf("Column `%s` use ON UPDATE but is not DATETIME or TIMESTAMP", col.name)
+ return &WalkThroughError{
+ Type: ErrorTypeOnUpdateColumnNotDatetimeOrTimestamp,
+ Content: fmt.Sprintf("Column `%s` use ON UPDATE but is not DATETIME or TIMESTAMP", col.name),
+ }
}
case tidbast.ColumnOptionComment:
comment, err := restoreNode(option.Expr, format.RestoreStringWithoutCharset)
@@ -909,20 +985,27 @@ func (t *TableState) createColumn(ctx *FinderContext, column *tidbast.ColumnDef,
}
if col.nullable != nil && !*col.nullable && setNullDefault {
- return errors.Errorf("Invalid default value for column `%s`", col.name)
+ return &WalkThroughError{
+ Type: ErrorTypeSetNullDefaultForNotNullColumn,
+ // Content comes from MySQL Error content.
+ Content: fmt.Sprintf("Invalid default value for column `%s`", col.name),
+ }
}
t.columnSet[strings.ToLower(col.name)] = col
return nil
}
-func (t *TableState) createIndex(name string, keyList []string, unique bool, tp string, option *tidbast.IndexOption) error {
+func (t *TableState) createIndex(name string, keyList []string, unique bool, tp string, option *tidbast.IndexOption) *WalkThroughError {
if len(keyList) == 0 {
- return errors.Errorf("Index `%s` in table `%s` has empty key", name, t.name)
+ return &WalkThroughError{
+ Type: ErrorTypeIndexEmptyKeys,
+ Content: fmt.Sprintf("Index `%s` in table `%s` has empty key", name, t.name),
+ }
}
if name != "" {
if _, exists := t.indexSet[strings.ToLower(name)]; exists {
- return NewSchemaViolationError(805, fmt.Sprintf("Index `%s` already exists in table `%s`", name, t.name))
+ return NewIndexExistsError(t.name, name)
}
} else {
suffix := 1
@@ -956,9 +1039,12 @@ func (t *TableState) createIndex(name string, keyList []string, unique bool, tp
return nil
}
-func (t *TableState) createPrimaryKey(keys []string, tp string) error {
+func (t *TableState) createPrimaryKey(keys []string, tp string) *WalkThroughError {
if _, exists := t.indexSet[strings.ToLower(PrimaryKeyName)]; exists {
- return errors.Errorf("Primary key exists in table `%s`", t.name)
+ return &WalkThroughError{
+ Type: ErrorTypePrimaryKeyExists,
+ Content: fmt.Sprintf("Primary key exists in table `%s`", t.name),
+ }
}
pk := &IndexState{
@@ -974,11 +1060,14 @@ func (t *TableState) createPrimaryKey(keys []string, tp string) error {
return nil
}
-func restoreNode(node tidbast.Node, flag format.RestoreFlags) (string, error) {
+func restoreNode(node tidbast.Node, flag format.RestoreFlags) (string, *WalkThroughError) {
var buffer strings.Builder
ctx := format.NewRestoreCtx(flag, &buffer)
if err := node.Restore(ctx); err != nil {
- return "", errors.Errorf("%s", err.Error())
+ return "", &WalkThroughError{
+ Type: ErrorTypeDeparseError,
+ Content: err.Error(),
+ }
}
return buffer.String(), nil
}
diff --git a/backend/plugin/advisor/catalog/walk_through_test.go b/backend/plugin/advisor/catalog/walk_through_test.go
index a3198a7f05..510b47c4c2 100644
--- a/backend/plugin/advisor/catalog/walk_through_test.go
+++ b/backend/plugin/advisor/catalog/walk_through_test.go
@@ -23,7 +23,7 @@ type testData struct {
// Use custom yaml tag to avoid generate field name `ignorecasesensitive`.
IgnoreCaseSensitive bool `yaml:"ignore_case_sensitive"`
Want string
- Err string
+ Err *WalkThroughError
}
func TestTiDBWalkThrough(t *testing.T) {
@@ -172,6 +172,14 @@ func TestPostgreSQLANTLRWalkThrough(t *testing.T) {
}
}
+func convertInterfaceSliceToStringSlice(slice []any) []string {
+ var res []string
+ for _, item := range slice {
+ res = append(res, item.(string))
+ }
+ return res
+}
+
func runWalkThroughTest(t *testing.T, file string, engineType storepb.Engine, originDatabase *storepb.DatabaseSchemaMetadata) {
tests := []testData{}
filepath := filepath.Join("test", file+".yaml")
@@ -190,15 +198,26 @@ 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 := NewFinder(&storepb.DatabaseSchemaMetadata{}, &FinderContext{CheckIntegrity: false, EngineType: engineType, IgnoreCaseSensitive: test.IgnoreCaseSensitive})
+ finder := NewEmptyFinder(&FinderContext{CheckIntegrity: false, EngineType: engineType, IgnoreCaseSensitive: test.IgnoreCaseSensitive})
state = finder.Origin
}
asts, _ := sm.GetASTsForChecks(engineType, test.Statement)
err := state.WalkThrough(asts)
- if test.Err != "" {
- require.Error(t, err, test.Statement)
- require.Equal(t, test.Err, err.Error(), test.Statement)
+ if err != nil {
+ err, yes := err.(*WalkThroughError)
+ require.True(t, yes)
+ if err.Payload != nil {
+ actualPayloadText, yes := err.Payload.([]string)
+ require.True(t, yes)
+ expectedPayloadText := convertInterfaceSliceToStringSlice(test.Err.Payload.([]any))
+ err.Payload = nil
+ test.Err.Payload = nil
+ require.Equal(t, test.Err, err)
+ require.Equal(t, expectedPayloadText, actualPayloadText)
+ } else {
+ require.Equal(t, test.Err, err)
+ }
continue
}
require.NoError(t, err, test.Statement)
@@ -229,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 := NewFinder(&storepb.DatabaseSchemaMetadata{}, &FinderContext{CheckIntegrity: false, EngineType: engineType, IgnoreCaseSensitive: test.IgnoreCaseSensitive})
+ finder := NewEmptyFinder(&FinderContext{CheckIntegrity: false, EngineType: engineType, IgnoreCaseSensitive: test.IgnoreCaseSensitive})
state = finder.Origin
}
@@ -241,9 +260,20 @@ func runANTLRWalkThroughTest(t *testing.T, file string, engineType storepb.Engin
// Call WalkThrough with ANTLR tree
err := state.WalkThrough(parseResult)
- if test.Err != "" {
- require.Error(t, err, test.Statement)
- require.Equal(t, test.Err, err.Error(), test.Statement)
+ if err != nil {
+ err, yes := err.(*WalkThroughError)
+ require.True(t, yes)
+ if err.Payload != nil {
+ actualPayloadText, yes := err.Payload.([]string)
+ require.True(t, yes)
+ expectedPayloadText := convertInterfaceSliceToStringSlice(test.Err.Payload.([]any))
+ err.Payload = nil
+ test.Err.Payload = nil
+ require.Equal(t, test.Err, err)
+ require.Equal(t, expectedPayloadText, actualPayloadText)
+ } else {
+ require.Equal(t, test.Err, err)
+ }
continue
}
require.NoError(t, err, test.Statement)
diff --git a/backend/plugin/advisor/mysql/rule_column_no_null.go b/backend/plugin/advisor/mysql/rule_column_no_null.go
index 2bf0d69bfa..7aa05f6786 100644
--- a/backend/plugin/advisor/mysql/rule_column_no_null.go
+++ b/backend/plugin/advisor/mysql/rule_column_no_null.go
@@ -3,6 +3,7 @@ package mysql
import (
"context"
"fmt"
+ "slices"
"github.com/antlr4-go/antlr/v4"
"github.com/bytebase/parser/mysql"
@@ -11,6 +12,7 @@ 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"
)
@@ -41,7 +43,7 @@ func (*ColumnNoNullAdvisor) Check(_ context.Context, checkCtx advisor.Context) (
}
// Create the rule
- rule := NewColumnNoNullRule(level, string(checkCtx.Rule.Type))
+ rule := NewColumnNoNullRule(level, string(checkCtx.Rule.Type), checkCtx.Catalog)
// Create the generic checker with the rule
checker := NewGenericChecker([]Rule{rule})
@@ -52,21 +54,38 @@ 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) *ColumnNoNullRule {
+func NewColumnNoNullRule(level storepb.Advice_Status, title string, catalog *catalog.Finder) *ColumnNoNullRule {
return &ColumnNoNullRule{
BaseRule: BaseRule{
level: level,
title: title,
},
+ columnSet: make(map[string]columnName),
+ catalog: catalog,
}
}
@@ -93,6 +112,44 @@ 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
@@ -110,42 +167,19 @@ func (r *ColumnNoNullRule) checkCreateTable(ctx *mysql.CreateTableContext) {
continue
}
- _, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- fieldDef := tableElement.ColumnDefinition().FieldDefinition()
- if fieldDef == nil {
+ _, _, column := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
+ if tableElement.ColumnDefinition().FieldDefinition() == nil {
continue
}
-
- // 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
+ col := columnName{
+ tableName: tableName,
+ columnName: column,
+ line: r.baseLine + tableElement.ColumnDefinition().GetStart().GetLine(),
}
- // PRIMARY KEY implies NOT NULL
- if attribute.GetValue() != nil && attribute.GetValue().GetTokenType() == mysql.MySQLParserKEY_SYMBOL {
- return false
+ if _, exists := r.columnSet[col.name()]; !exists {
+ r.columnSet[col.name()] = col
}
}
- return true
}
func (r *ColumnNoNullRule) checkAlterTable(ctx *mysql.AlterTableContext) {
@@ -166,63 +200,41 @@ 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:
- 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()),
- })
- }
+ column := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
+ columns = append(columns, column)
case item.OPEN_PAR_SYMBOL() != nil && item.TableElementList() != nil:
for _, tableElement := range item.TableElementList().AllTableElement() {
- if tableElement.ColumnDefinition() == nil || tableElement.ColumnDefinition().FieldDefinition() == nil {
+ if tableElement.ColumnDefinition() == nil {
continue
}
- _, _, 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()),
- })
- }
+ _, _, column := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
+ columns = append(columns, column)
}
default:
}
- // 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()),
- })
+ // 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(),
}
- 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()),
- })
+ if _, exists := r.columnSet[col.name()]; !exists {
+ r.columnSet[col.name()] = col
}
- 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 e3f3214282..09465e59d2 100644
--- a/backend/plugin/advisor/mysql/rule_index_total_number_limit.go
+++ b/backend/plugin/advisor/mysql/rule_index_total_number_limit.go
@@ -3,6 +3,7 @@ package mysql
import (
"context"
"fmt"
+ "slices"
"github.com/antlr4-go/antlr/v4"
"github.com/bytebase/parser/mysql"
@@ -57,33 +58,27 @@ func (*IndexTotalNumberLimitAdvisor) Check(_ context.Context, checkCtx advisor.C
antlr.ParseTreeWalkerDefault.Walk(checker, stmt.Tree)
}
- // Check all tables after processing all statements
- rule.checkAllTables()
-
- return checker.GetAdviceList(), nil
+ return rule.generateAdvice(), nil
}
// IndexTotalNumberLimitRule checks for index total number limit.
type IndexTotalNumberLimitRule struct {
BaseRule
- 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
+ max int
+ lineForTable map[string]int
catalog *catalog.Finder
}
// NewIndexTotalNumberLimitRule creates a new IndexTotalNumberLimitRule.
-func NewIndexTotalNumberLimitRule(level storepb.Advice_Status, title string, maxIndexes int, catalogFinder *catalog.Finder) *IndexTotalNumberLimitRule {
+func NewIndexTotalNumberLimitRule(level storepb.Advice_Status, title string, maxIndexes int, catalog *catalog.Finder) *IndexTotalNumberLimitRule {
return &IndexTotalNumberLimitRule{
BaseRule: BaseRule{
level: level,
title: title,
},
max: maxIndexes,
- tableIndexes: make(map[string]map[string]bool),
- tableLines: make(map[string]int),
- catalog: catalogFinder,
+ lineForTable: make(map[string]int),
+ catalog: catalog,
}
}
@@ -111,6 +106,45 @@ 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
@@ -120,31 +154,7 @@ func (r *IndexTotalNumberLimitRule) checkCreateTable(ctx *mysql.CreateTableConte
}
_, tableName := mysqlparser.NormalizeMySQLTableName(ctx.TableName())
-
- // 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()
+ r.lineForTable[tableName] = r.baseLine + ctx.GetStart().GetLine()
}
func (r *IndexTotalNumberLimitRule) checkCreateIndex(ctx *mysql.CreateIndexContext) {
@@ -155,18 +165,7 @@ func (r *IndexTotalNumberLimitRule) checkCreateIndex(ctx *mysql.CreateIndexConte
return
}
_, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.CreateIndexTarget().TableRef())
-
- // 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()
+ r.lineForTable[tableName] = r.baseLine + ctx.GetStart().GetLine()
}
func (r *IndexTotalNumberLimitRule) checkAlterTable(ctx *mysql.AlterTableContext) {
@@ -184,12 +183,6 @@ 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
@@ -201,89 +194,52 @@ func (r *IndexTotalNumberLimitRule) checkAlterTable(ctx *mysql.AlterTableContext
switch {
// add single columns.
case item.Identifier() != nil && item.FieldDefinition() != nil:
- r.checkFieldDefinitionForIndexes(tableName, item.FieldDefinition())
+ r.checkFieldDefinitionContext(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 {
- r.checkFieldDefinitionForIndexes(tableName, tableElement.ColumnDefinition().FieldDefinition())
- }
- if tableElement.TableConstraintDef() != nil {
- r.checkTableConstraintForIndexes(tableName, tableElement.TableConstraintDef())
+ if tableElement.ColumnDefinition() == nil || tableElement.ColumnDefinition().FieldDefinition() == nil {
+ continue
}
+ r.checkFieldDefinitionContext(tableName, tableElement.ColumnDefinition().FieldDefinition())
}
// add constraint.
case item.TableConstraintDef() != nil:
- r.checkTableConstraintForIndexes(tableName, item.TableConstraintDef())
+ r.checkTableConstraintDef(tableName, item.TableConstraintDef())
default:
}
// change column.
case item.CHANGE_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.Identifier() != nil:
- r.checkFieldDefinitionForIndexes(tableName, item.FieldDefinition())
+ r.checkFieldDefinitionContext(tableName, item.FieldDefinition())
// modify column.
case item.MODIFY_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.FieldDefinition() != nil:
- r.checkFieldDefinitionForIndexes(tableName, item.FieldDefinition())
+ r.checkFieldDefinitionContext(tableName, item.FieldDefinition())
default:
continue
}
}
-
- // Track last line for this table
- r.tableLines[tableName] = ctx.GetStart().GetLine()
}
-// checkFieldDefinitionForIndexes checks if a field definition contains inline index definitions.
-func (r *IndexTotalNumberLimitRule) checkFieldDefinitionForIndexes(tableName string, ctx mysql.IFieldDefinitionContext) {
+func (r *IndexTotalNumberLimitRule) checkFieldDefinitionContext(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, 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
+ case mysql.MySQLParserPRIMARY_SYMBOL, mysql.MySQLParserUNIQUE_SYMBOL:
+ r.lineForTable[tableName] = r.baseLine + ctx.GetStart().GetLine()
default:
}
}
}
-// checkTableConstraintForIndexes checks table constraints for index definitions.
-func (r *IndexTotalNumberLimitRule) checkTableConstraintForIndexes(tableName string, ctx mysql.ITableConstraintDefContext) {
+func (r *IndexTotalNumberLimitRule) checkTableConstraintDef(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:
- // 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
+ r.lineForTable[tableName] = r.baseLine + ctx.GetStart().GetLine()
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 fc4dadc840..79c2badcb0 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,6 +15,7 @@ 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"
)
@@ -45,7 +46,7 @@ func (*TableMaximumVarcharLengthAdvisor) Check(_ context.Context, checkCtx advis
}
// Create the rule
- rule := NewTableTextFieldsTotalLengthRule(level, string(checkCtx.Rule.Type), payload.Number)
+ rule := NewTableTextFieldsTotalLengthRule(level, string(checkCtx.Rule.Type), checkCtx.Catalog, payload.Number)
// Create the generic checker with the rule
checker := NewGenericChecker([]Rule{rule})
@@ -59,29 +60,22 @@ 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, maximum int) *TableTextFieldsTotalLengthRule {
+func NewTableTextFieldsTotalLengthRule(level storepb.Advice_Status, title string, catalog *catalog.Finder, maximum int) *TableTextFieldsTotalLengthRule {
return &TableTextFieldsTotalLengthRule{
BaseRule: BaseRule{
level: level,
title: title,
},
- maximum: maximum,
- tableColumns: make(map[string]map[string]*tableColumn),
+ catalog: catalog,
+ maximum: maximum,
}
}
@@ -120,33 +114,20 @@ func (r *TableTextFieldsTotalLengthRule) checkCreateTable(ctx *mysql.CreateTable
if tableName == "" {
return
}
-
- // Initialize table columns map
- if r.tableColumns[tableName] == nil {
- r.tableColumns[tableName] = make(map[string]*tableColumn)
+ tableInfo := r.catalog.Final.FindTable(&catalog.TableFind{TableName: tableName})
+ if tableInfo == nil {
+ return
}
-
- // 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,
- }
+ 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()),
+ })
}
-
- // Check total text length
- r.checkTableTextLength(tableName, ctx.GetStart().GetLine())
}
func (r *TableTextFieldsTotalLengthRule) checkAlterTable(ctx *mysql.AlterTableContext) {
@@ -167,99 +148,31 @@ func (r *TableTextFieldsTotalLengthRule) checkAlterTable(ctx *mysql.AlterTableCo
if tableName == "" {
return
}
-
- // 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 {
+ tableInfo := r.catalog.Final.FindTable(&catalog.TableFind{TableName: tableName})
+ if tableInfo == nil {
return
}
-
- var total int64
- for _, column := range columns {
- total += getTextLength(column.columnType)
- }
-
+ total := getTotalTextLength(tableInfo)
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 + line),
+ StartPosition: common.ConvertANTLRLineToPosition(r.baseLine + ctx.GetStart().GetLine()),
})
}
}
+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 dd788cad5e..f5b4900a17 100644
--- a/backend/plugin/advisor/pg/advisor_index_total_number_limit.go
+++ b/backend/plugin/advisor/pg/advisor_index_total_number_limit.go
@@ -3,6 +3,7 @@ package pg
import (
"context"
"fmt"
+ "slices"
"github.com/antlr4-go/antlr/v4"
@@ -42,29 +43,47 @@ func (*IndexTotalNumberLimitAdvisor) Check(_ context.Context, checkCtx advisor.C
}
rule := &indexTotalNumberLimitRule{
- 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,
+ BaseRule: BaseRule{level: level, title: string(checkCtx.Rule.Type)},
+ max: payload.Number,
+ catalog: checkCtx.Catalog,
+ tableLine: make(tableLineMap),
}
- checker := NewGenericChecker([]Rule{rule})
- antlr.ParseTreeWalkerDefault.Walk(checker, tree.Tree)
+ 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
+}
- // Check all tables after processing all statements
- rule.checkAllTables()
+type tableLineMap map[string]tableLine
- return checker.GetAdviceList(), nil
+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,
+ }
}
type indexTotalNumberLimitRule struct {
BaseRule
- max int
- tableIndexes map[string]map[string]bool // tableKey -> indexName -> exists
- tableLines map[string]int // tableKey -> last line number
- catalog *catalog.Finder
+ max int
+ catalog *catalog.Finder
+ tableLine tableLineMap
}
func (*indexTotalNumberLimitRule) Name() string {
@@ -89,33 +108,34 @@ func (*indexTotalNumberLimitRule) OnExit(_ antlr.ParserRuleContext, _ string) er
return nil
}
-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()
+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
}
-
- totalCount := existingIndexes + newIndexes
- if totalCount > r.max {
- line := r.tableLines[tableKey]
+ 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 {
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", schema, table, r.max, totalCount),
+ 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()),
StartPosition: &storepb.Position{
- Line: int32(line),
+ Line: int32(table.line),
Column: 0,
},
})
@@ -139,12 +159,6 @@ 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)
@@ -154,21 +168,18 @@ func (r *indexTotalNumberLimitRule) handleCreatestmt(ctx *parser.CreatestmtConte
// Check column-level constraints
if elem.ColumnDef() != nil {
if hasIndexConstraint(elem.ColumnDef()) {
- indexName := fmt.Sprintf("__inline_index_%d__", len(r.tableIndexes[tableKey]))
- r.tableIndexes[tableKey][indexName] = true
+ r.tableLine.set(schemaName, tableName, ctx.GetStop().GetLine())
+ return
}
}
// Check table-level constraints
if elem.Tableconstraint() != nil && hasTableIndexConstraint(elem.Tableconstraint()) {
- indexName := fmt.Sprintf("__index_%d__", len(r.tableIndexes[tableKey]))
- r.tableIndexes[tableKey][indexName] = true
+ r.tableLine.set(schemaName, tableName, ctx.GetStop().GetLine())
+ return
}
}
}
-
- // Track last line for this table
- r.tableLines[makeTableKey(schemaName, tableName)] = ctx.GetStop().GetLine()
}
func (r *indexTotalNumberLimitRule) handleAltertablestmt(ctx *parser.AltertablestmtContext) {
@@ -186,12 +197,6 @@ 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 {
@@ -200,23 +205,20 @@ 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()) {
- indexName := fmt.Sprintf("__inline_index_%d__", len(r.tableIndexes[tableKey]))
- r.tableIndexes[tableKey][indexName] = true
+ r.tableLine.set(schemaName, tableName, ctx.GetStop().GetLine())
+ return
}
}
// ADD CONSTRAINT (PRIMARY KEY or UNIQUE)
if cmd.ADD_P() != nil && cmd.Tableconstraint() != nil {
if hasTableIndexConstraint(cmd.Tableconstraint()) {
- indexName := fmt.Sprintf("__index_%d__", len(r.tableIndexes[tableKey]))
- r.tableIndexes[tableKey][indexName] = true
+ r.tableLine.set(schemaName, tableName, ctx.GetStop().GetLine())
+ return
}
}
}
}
-
- // Track last line for this table
- r.tableLines[makeTableKey(schemaName, tableName)] = ctx.GetStop().GetLine()
}
func (r *indexTotalNumberLimitRule) handleIndexstmt(ctx *parser.IndexstmtContext) {
@@ -234,19 +236,7 @@ func (r *indexTotalNumberLimitRule) handleIndexstmt(ctx *parser.IndexstmtContext
}
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)
- }
-
- // 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()
+ r.tableLine.set(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 407e7fad01..7bca05a484 100644
--- a/backend/plugin/advisor/pg/advisor_table_require_pk.go
+++ b/backend/plugin/advisor/pg/advisor_table_require_pk.go
@@ -44,24 +44,22 @@ 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)
- // Validate final state after walking all statements
+ // Simple Solution: Validate final state after walking all statements
rule.validateFinalState()
return checker.GetAdviceList(), nil
}
-type tableState struct {
- hasPK bool
- pkConstraint string // Name of the PK constraint (if known)
- startLine int
- endLine int
+type tableMention struct {
+ startLine int
+ endLine int
}
type tableRequirePKRule struct {
@@ -69,8 +67,8 @@ type tableRequirePKRule struct {
statementsText string
catalog *catalog.Finder
- // Track table state: whether they have a primary key
- tableState map[string]*tableState // key: "schema.table", value: state
+ // Simple Solution: Track last mention of each table
+ tableMentions map[string]*tableMention // key: "schema.table", value: last mention info
}
// Name returns the rule name.
@@ -98,7 +96,7 @@ func (*tableRequirePKRule) OnExit(_ antlr.ParserRuleContext, _ string) error {
return nil
}
-// handleCreatestmt records CREATE TABLE statements
+// handleCreatestmt records CREATE TABLE statements (Simple Solution)
func (r *tableRequirePKRule) handleCreatestmt(ctx *parser.CreatestmtContext) {
if !isTopLevel(ctx.GetParent()) {
return
@@ -114,45 +112,15 @@ func (r *tableRequirePKRule) handleCreatestmt(ctx *parser.CreatestmtContext) {
}
}
- 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(),
+ // 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(),
}
}
-// handleAltertablestmt records ALTER TABLE statements
+// handleAltertablestmt records ALTER TABLE statements (Simple Solution)
func (r *tableRequirePKRule) handleAltertablestmt(ctx *parser.AltertablestmtContext) {
if !isTopLevel(ctx.GetParent()) {
return
@@ -167,84 +135,12 @@ func (r *tableRequirePKRule) handleAltertablestmt(ctx *parser.AltertablestmtCont
}
}
- 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
- }
- }
+ // 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(),
}
-
- // 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
@@ -258,7 +154,9 @@ func (r *tableRequirePKRule) handleDropstmt(ctx *parser.DropstmtContext) {
return
}
- // Remove all dropped tables from tracking
+ // 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
if ctx.Any_name_list() != nil {
allNames := ctx.Any_name_list().AllAny_name()
for _, anyName := range allNames {
@@ -266,26 +164,31 @@ 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.tableState, key)
+ delete(r.tableMentions, key)
}
- // For qualified names, we skip for simplicity
+ // For qualified names, we skip for simplicity - they won't cause false positives
+ // because catalog.Final won't have dropped tables
}
}
}
-// validateFinalState checks all tables for PRIMARY KEY
+// validateFinalState checks all mentioned tables against catalog.Final (Simple Solution)
func (r *tableRequirePKRule) validateFinalState() {
- for tableKey, state := range r.tableState {
+ for tableKey, mention := range r.tableMentions {
// Parse table key: "schema.table"
schemaName, tableName := parseTableKey(tableKey)
- // 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 {
+ // Check catalog.Final for PRIMARY KEY
+ hasPK := r.catalog.Final.HasPrimaryKey(&catalog.PrimaryKeyFind{
+ SchemaName: schemaName,
+ TableName: tableName,
+ })
+
+ if !hasPK {
content := fmt.Sprintf("Table %q.%q requires PRIMARY KEY", schemaName, tableName)
// Extract and include the related statement
- statement := extractStatementText(r.statementsText, state.startLine, state.endLine)
+ statement := extractStatementText(r.statementsText, mention.startLine, mention.endLine)
if statement != "" {
content = fmt.Sprintf("%s, related statement: %q", content, statement)
}
@@ -296,7 +199,7 @@ func (r *tableRequirePKRule) validateFinalState() {
Title: r.title,
Content: content,
StartPosition: &storepb.Position{
- Line: int32(state.startLine),
+ Line: int32(mention.startLine),
Column: 0,
},
})
@@ -304,136 +207,13 @@ func (r *tableRequirePKRule) validateFinalState() {
}
}
-// 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
- }
- }
+// 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:]
}
}
- 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
+ return "public", key
}
diff --git a/backend/plugin/advisor/pg/test_helper.go b/backend/plugin/advisor/pg/test_helper.go
index aa2f67e635..a42d595088 100644
--- a/backend/plugin/advisor/pg/test_helper.go
+++ b/backend/plugin/advisor/pg/test_helper.go
@@ -74,6 +74,12 @@ 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 37f16c8a6b..d2dba4d134 100644
--- a/backend/plugin/advisor/pg/utils.go
+++ b/backend/plugin/advisor/pg/utils.go
@@ -139,65 +139,3 @@ 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 7aec91288c..4a9b301ed0 100644
--- a/backend/plugin/advisor/sql_review.go
+++ b/backend/plugin/advisor/sql_review.go
@@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"encoding/json"
+ "fmt"
"regexp"
"github.com/pkg/errors"
@@ -523,6 +524,8 @@ 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)...)
@@ -533,6 +536,16 @@ 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 {
@@ -591,3 +604,193 @@ 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 233ed82bda..3c9b73f538 100644
--- a/backend/plugin/advisor/tidb/advisor_column_no_null.go
+++ b/backend/plugin/advisor/tidb/advisor_column_no_null.go
@@ -3,6 +3,7 @@ package tidb
import (
"context"
"fmt"
+ "slices"
"github.com/pingcap/tidb/pkg/parser/ast"
"github.com/pkg/errors"
@@ -10,6 +11,7 @@ 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 (
@@ -37,21 +39,75 @@ func (*ColumnNoNullAdvisor) Check(_ context.Context, checkCtx advisor.Context) (
return nil, err
}
checker := &columnNoNullChecker{
- level: level,
- title: string(checkCtx.Rule.Type),
+ level: level,
+ title: string(checkCtx.Rule.Type),
+ columnSet: make(map[string]columnName),
+ catalog: checkCtx.Catalog,
}
for _, stmtNode := range root {
(stmtNode).Accept(checker)
}
- return checker.adviceList, nil
+ return checker.generateAdvice(), 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.
@@ -60,14 +116,13 @@ func (checker *columnNoNullChecker) Enter(in ast.Node) (ast.Node, bool) {
// CREATE TABLE
case *ast.CreateTableStmt:
for _, column := range node.Cols {
- 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()),
- })
+ 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
}
}
// ALTER TABLE
@@ -77,26 +132,24 @@ func (checker *columnNoNullChecker) Enter(in ast.Node) (ast.Node, bool) {
// ADD COLUMNS
case ast.AlterTableAddColumns:
for _, column := range spec.NewColumns {
- 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()),
- })
+ 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
}
}
// CHANGE COLUMN
case ast.AlterTableChangeColumn:
- 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()),
- })
+ 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
}
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 8c09a1e3dc..90273c8687 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,12 +58,6 @@ 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 f682eab094..3b48796d09 100644
--- a/backend/plugin/advisor/tidb/advisor_index_total_number_limit.go
+++ b/backend/plugin/advisor/tidb/advisor_index_total_number_limit.go
@@ -5,6 +5,7 @@ package tidb
import (
"context"
"fmt"
+ "slices"
"github.com/pingcap/tidb/pkg/parser/ast"
"github.com/pkg/errors"
@@ -47,8 +48,7 @@ func (*IndexTotalNumberLimitAdvisor) Check(_ context.Context, checkCtx advisor.C
level: level,
title: string(checkCtx.Rule.Type),
max: payload.Number,
- tableIndexes: make(map[string]map[string]bool),
- tableLines: make(map[string]int),
+ lineForTable: make(map[string]int),
catalog: checkCtx.Catalog,
}
@@ -58,10 +58,7 @@ func (*IndexTotalNumberLimitAdvisor) Check(_ context.Context, checkCtx advisor.C
(stmt).Accept(checker)
}
- // Check all tables after processing all statements
- checker.checkAllTables()
-
- return checker.adviceList, nil
+ return checker.generateAdvice(), nil
}
type indexTotalNumberLimitChecker struct {
@@ -71,120 +68,82 @@ type indexTotalNumberLimitChecker struct {
text string
line int
max int
- tableIndexes map[string]map[string]bool // tableName -> indexName -> exists
- tableLines map[string]int // tableName -> last line number
+ lineForTable map[string]int
catalog *catalog.Finder
}
-// 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
+func (checker *indexTotalNumberLimitChecker) generateAdvice() []*storepb.Advice {
+ type tableName struct {
+ name string
+ line int
+ }
+ var tableList []tableName
- // Initialize table indexes map
- if checker.tableIndexes[tableName] == nil {
- checker.tableIndexes[tableName] = make(map[string]bool)
+ 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
}
-
- // 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
- }
+ if i.line > j.line {
+ return 1
}
+ return 0
+ })
- // 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
- }
+ 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),
+ })
}
+ }
- // Track last line for this table
- checker.tableLines[tableName] = node.OriginTextPosition()
+ return checker.adviceList
+}
+// 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) {
- indexName := fmt.Sprintf("__inline_index_%d__", len(checker.tableIndexes[tableName]))
- checker.tableIndexes[tableName][indexName] = true
+ checker.lineForTable[node.Table.Name.O] = node.OriginTextPosition()
+ break
}
}
case ast.AlterTableAddConstraint:
if createIndex(spec.Constraint) {
- indexName := fmt.Sprintf("__index_%d__", len(checker.tableIndexes[tableName]))
- checker.tableIndexes[tableName][indexName] = true
+ checker.lineForTable[node.Table.Name.O] = node.OriginTextPosition()
}
case ast.AlterTableChangeColumn, ast.AlterTableModifyColumn:
- if len(spec.NewColumns) > 0 && createIndex(spec.NewColumns[0]) {
- indexName := fmt.Sprintf("__inline_index_%d__", len(checker.tableIndexes[tableName]))
- checker.tableIndexes[tableName][indexName] = true
+ if createIndex(spec.NewColumns[0]) {
+ checker.lineForTable[node.Table.Name.O] = node.OriginTextPosition()
}
default:
}
}
-
- // Track last line for this table
- checker.tableLines[tableName] = node.OriginTextPosition()
-
case *ast.CreateIndexStmt:
- 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()
+ checker.lineForTable[node.Table.Name.O] = 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