Skip to content

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

refactor: remove unnecessary pointer usage in catalog state structures (#18107)

Danny Xubeede457fc84
diff --git a/backend/plugin/advisor/catalog/state.go b/backend/plugin/advisor/catalog/state.go
index 4dceb56043..1e1dc4f37d 100644
--- a/backend/plugin/advisor/catalog/state.go
+++ b/backend/plugin/advisor/catalog/state.go
@@ -31,7 +31,7 @@ func NewDatabaseState(d *storepb.DatabaseSchemaMetadata, ignoreCaseSensitive boo
for _, view := range schema.Views {
for _, dependencyColumn := range view.DependencyColumns {
if schemaState, exist := database.schemaSet[dependencyColumn.Schema]; exist {
- if tableState, exist := schemaState.getTable(dependencyColumn.Table); exist {
+ if tableState, exist := mysqlGetTable(schemaState, dependencyColumn.Table); exist {
tableState.dependencyView[fmt.Sprintf("%q.%q", schema.Name, view.Name)] = true
if columnState, exist := tableState.columnSet[dependencyColumn.Column]; exist {
columnState.dependencyView[fmt.Sprintf("%q.%q", schema.Name, view.Name)] = true
@@ -76,17 +76,17 @@ func newSchemaState(s *storepb.SchemaMetadata, ignoreCaseSensitive bool, engineT
func newViewState(v *storepb.ViewMetadata) *ViewState {
return &ViewState{
name: v.Name,
- definition: newStringPointer(v.Definition),
- comment: newStringPointer(v.Comment),
+ definition: v.Definition,
+ comment: v.Comment,
}
}
func newTableState(t *storepb.TableMetadata, engineType storepb.Engine) *TableState {
table := &TableState{
name: t.Name,
- engine: newStringPointer(t.Engine),
- collation: newStringPointer(t.Collation),
- comment: newStringPointer(t.Comment),
+ engine: t.Engine,
+ collation: t.Collation,
+ comment: t.Comment,
columnSet: make(columnStateMap),
indexSet: make(IndexStateMap),
dependencyView: make(map[string]bool),
@@ -118,19 +118,15 @@ func newTableState(t *storepb.TableMetadata, engineType storepb.Engine) *TableSt
}
func newColumnState(c *storepb.ColumnMetadata, position int) *ColumnState {
- defaultValue := (*string)(nil)
- if c.Default != "" {
- defaultValue = copyStringPointer(&c.Default)
- }
return &ColumnState{
name: c.Name,
- position: newIntPointer(position),
- defaultValue: defaultValue,
- nullable: newBoolPointer(c.Nullable),
- columnType: newStringPointer(c.Type),
- characterSet: newStringPointer(c.CharacterSet),
- collation: newStringPointer(c.Collation),
- comment: newStringPointer(c.Comment),
+ position: position,
+ defaultValue: c.Default,
+ nullable: c.Nullable,
+ columnType: c.Type,
+ characterSet: c.CharacterSet,
+ collation: c.Collation,
+ comment: c.Comment,
dependencyView: make(map[string]bool),
}
}
@@ -138,15 +134,15 @@ func newColumnState(c *storepb.ColumnMetadata, position int) *ColumnState {
func newIndexState(i *storepb.IndexMetadata) *IndexState {
index := &IndexState{
name: i.Name,
- indexType: newStringPointer(i.Type),
- unique: newBoolPointer(i.Unique),
- primary: newBoolPointer(i.Primary),
- visible: newBoolPointer(i.Visible),
- comment: newStringPointer(i.Comment),
+ indexType: i.Type,
+ unique: i.Unique,
+ primary: i.Primary,
+ visible: i.Visible,
+ comment: i.Comment,
expressionList: copyStringSlice(i.Expressions),
// We rudely think that pk and uk are constraints here.
// But in fact, we can create uk by CREATE UNIQUE INDEX statements.
- isConstraint: *newBoolPointer(i.Primary || i.Unique),
+ isConstraint: i.Primary || i.Unique,
}
return index
}
@@ -212,7 +208,7 @@ func (d *DatabaseState) GetIndex(schemaName string, tableName string, indexName
if !exists {
return "", nil
}
- table, exists := schema.getTable(tableName)
+ table, exists := mysqlGetTable(schema, tableName)
if !exists {
return "", nil
}
@@ -249,7 +245,7 @@ func (d *DatabaseState) GetPrimaryKey(schemaName string, tableName string) *Inde
continue
}
for _, index := range table.indexSet {
- if index.primary != nil && *index.primary {
+ if index.primary {
return index
}
}
@@ -269,13 +265,13 @@ func (d *DatabaseState) CountColumn(schemaName string, tableName string, columnT
if !exists {
return 0
}
- table, exists := schema.getTable(tableName)
+ table, exists := mysqlGetTable(schema, tableName)
if !exists {
return 0
}
res := 0
for _, column := range table.columnSet {
- if column.columnType != nil && strings.EqualFold(*column.columnType, columnType) {
+ if strings.EqualFold(column.columnType, columnType) {
res++
}
}
@@ -294,7 +290,7 @@ func (d *DatabaseState) GetColumn(schemaName string, tableName string, columnNam
if !exists {
return nil
}
- table, exists := schema.getTable(tableName)
+ table, exists := mysqlGetTable(schema, tableName)
if !exists {
return nil
}
@@ -311,7 +307,7 @@ func (d *DatabaseState) GetTable(schemaName string, tableName string) *TableStat
if !exists {
return nil
}
- table, exists := schema.getTable(tableName)
+ table, exists := mysqlGetTable(schema, tableName)
if !exists {
return nil
}
@@ -333,7 +329,7 @@ type SchemaState struct {
}
func (s *SchemaState) Index(tableName string) *IndexStateMap {
- table, exists := s.getTable(tableName)
+ table, exists := mysqlGetTable(s, tableName)
if !exists {
return nil
}
@@ -346,11 +342,11 @@ type schemaStateMap map[string]*SchemaState
type TableState struct {
name string
// engine isn't supported for Postgres, Snowflake, SQLite.
- engine *string
+ engine string
// collation isn't supported for Postgres, ClickHouse, Snowflake, SQLite.
- collation *string
+ collation string
// comment isn't supported for SQLite.
- comment *string
+ comment string
columnSet columnStateMap
// indexSet isn't supported for ClickHouse, Snowflake.
indexSet IndexStateMap
@@ -382,9 +378,9 @@ func (t *TableState) Index() *IndexStateMap {
func (t *TableState) copy() *TableState {
return &TableState{
name: t.name,
- engine: copyStringPointer(t.engine),
- collation: copyStringPointer(t.collation),
- comment: copyStringPointer(t.comment),
+ engine: t.engine,
+ collation: t.collation,
+ comment: t.comment,
columnSet: t.columnSet.copy(),
indexSet: t.indexSet.copy(),
}
@@ -398,13 +394,13 @@ type IndexState struct {
// This could refer to a column or an expression.
expressionList []string
// Type isn't supported for SQLite.
- indexType *string
- unique *bool
- primary *bool
+ indexType string
+ unique bool
+ primary bool
// Visible isn't supported for Postgres, SQLite.
- visible *bool
+ visible bool
// Comment isn't supported for SQLite.
- comment *string
+ comment string
// PostgreSQL specific fields.
@@ -416,29 +412,23 @@ func (idx *IndexState) copy() *IndexState {
return &IndexState{
name: idx.name,
expressionList: copyStringSlice(idx.expressionList),
- indexType: copyStringPointer(idx.indexType),
- unique: copyBoolPointer(idx.unique),
- primary: copyBoolPointer(idx.primary),
- visible: copyBoolPointer(idx.visible),
- comment: copyStringPointer(idx.comment),
+ indexType: idx.indexType,
+ unique: idx.unique,
+ primary: idx.primary,
+ visible: idx.visible,
+ comment: idx.comment,
isConstraint: idx.isConstraint,
}
}
// Unique returns the unique for the index.
func (idx *IndexState) Unique() bool {
- if idx.unique != nil {
- return *idx.unique
- }
- return false
+ return idx.unique
}
// Primary returns the primary for the index.
func (idx *IndexState) Primary() bool {
- if idx.primary != nil {
- return *idx.primary
- }
- return false
+ return idx.primary
}
// ExpressionList returns the expression list for the index.
@@ -459,17 +449,17 @@ func (m IndexStateMap) copy() IndexStateMap {
// ColumnState is the state for walk-through.
type ColumnState struct {
name string
- position *int
- defaultValue *string
+ position int
+ defaultValue string
// nullable isn't supported for ClickHouse.
- nullable *bool
- columnType *string
+ nullable bool
+ columnType string
// characterSet isn't supported for Postgres, ClickHouse, SQLite.
- characterSet *string
+ characterSet string
// collation isn't supported for ClickHouse, SQLite.
- collation *string
+ collation string
// comment isn't supported for SQLite.
- comment *string
+ comment string
// dependencyView is used to record the dependency view for the column.
// Used to check if the column is used by any view.
@@ -479,27 +469,24 @@ type ColumnState struct {
func (col *ColumnState) copy() *ColumnState {
return &ColumnState{
name: col.name,
- position: copyIntPointer(col.position),
- defaultValue: copyStringPointer(col.defaultValue),
- nullable: copyBoolPointer(col.nullable),
- columnType: copyStringPointer(col.columnType),
- characterSet: copyStringPointer(col.characterSet),
- collation: copyStringPointer(col.collation),
- comment: copyStringPointer(col.comment),
+ position: col.position,
+ defaultValue: col.defaultValue,
+ nullable: col.nullable,
+ columnType: col.columnType,
+ characterSet: col.characterSet,
+ collation: col.collation,
+ comment: col.comment,
}
}
// Nullable returns nullable for the column.
func (col *ColumnState) Nullable() bool {
- return col.nullable != nil && *col.nullable
+ return col.nullable
}
// Type returns type for the column.
func (col *ColumnState) Type() string {
- if col.columnType != nil {
- return *col.columnType
- }
- return ""
+ return col.columnType
}
// HasDefault returns if column has default value.
@@ -508,9 +495,9 @@ func (col *ColumnState) HasDefault() bool {
case "serial", "smallserial", "bigserial":
return true
default:
- // For other column types, check if defaultValue is not nil
+ // For other column types, check if defaultValue is not empty
}
- return col.defaultValue != nil
+ return col.defaultValue != ""
}
type columnStateMap map[string]*ColumnState
@@ -526,72 +513,21 @@ func (m columnStateMap) copy() columnStateMap {
// ViewState is the state for walk-through.
type ViewState struct {
name string
- definition *string
- comment *string
+ definition string
+ comment string
}
type viewStateMap map[string]*ViewState
-func copyStringPointer(p *string) *string {
- if p == nil {
- return nil
- }
- v := *p
- return &v
-}
-
-func copyBoolPointer(p *bool) *bool {
- if p == nil {
- return nil
- }
- v := *p
- return &v
-}
-
-func copyIntPointer(p *int) *int {
- if p == nil {
- return nil
- }
- v := *p
- return &v
-}
-
func copyStringSlice(in []string) []string {
var res []string
res = append(res, in...)
return res
}
-func newEmptyStringPointer() *string {
- res := ""
- return &res
-}
-
-func newStringPointer(v string) *string {
- return &v
-}
-
-func newIntPointer(v int) *int {
- return &v
-}
-
-func newTruePointer() *bool {
- v := true
- return &v
-}
-
-func newFalsePointer() *bool {
- v := false
- return &v
-}
-
-func newBoolPointer(v bool) *bool {
- return &v
-}
-
// Schema-level operations.
-// createSchemaState creates a new schema state with the given name.
-func (d *DatabaseState) createSchemaState(schemaName string) *SchemaState {
+// CreateSchema creates a new schema state with the given name.
+func (d *DatabaseState) CreateSchema(schemaName string) *SchemaState {
schema := &SchemaState{
ignoreCaseSensitive: d.ignoreCaseSensitive,
name: schemaName,
@@ -603,31 +539,15 @@ func (d *DatabaseState) createSchemaState(schemaName string) *SchemaState {
return schema
}
-// GetOrCreateSchema gets an existing schema or creates a new one if it doesn't exist.
-func (d *DatabaseState) GetOrCreateSchema(schemaName string) *SchemaState {
- if schema, exists := d.schemaSet[schemaName]; exists {
- return schema
- }
- return d.createSchemaState(schemaName)
-}
-
// GetSchema gets an existing schema.
-//
-//nolint:revive
func (d *DatabaseState) GetSchema(schemaName string) (*SchemaState, *WalkThroughError) {
- if schemaName == "" {
- schemaName = publicSchemaName
- }
if schema, exists := d.schemaSet[schemaName]; exists {
return schema, nil
}
- if schemaName != publicSchemaName {
- return nil, &WalkThroughError{
- Code: code.SchemaNotExists,
- Content: fmt.Sprintf("The schema %q doesn't exist", schemaName),
- }
+ return nil, &WalkThroughError{
+ Code: code.SchemaNotExists,
+ Content: fmt.Sprintf("The schema %q doesn't exist", schemaName),
}
- return d.createSchemaState(publicSchemaName), nil
}
// DropSchema drops a schema.
@@ -657,15 +577,15 @@ func (d *DatabaseState) DropSchema(schemaName string) *WalkThroughError {
// CreateTable creates a new table in the schema.
func (s *SchemaState) CreateTable(tableName string) (*TableState, *WalkThroughError) {
- if _, exists := s.getTable(tableName); exists {
+ if _, exists := mysqlGetTable(s, tableName); exists {
return nil, NewTableExistsError(tableName)
}
table := &TableState{
name: tableName,
- engine: newEmptyStringPointer(),
- collation: newEmptyStringPointer(),
- comment: newEmptyStringPointer(),
+ engine: "",
+ collation: "",
+ comment: "",
columnSet: make(columnStateMap),
indexSet: make(IndexStateMap),
dependencyView: make(map[string]bool),
@@ -681,8 +601,6 @@ func (s *SchemaState) CreateTable(tableName string) (*TableState, *WalkThroughEr
}
// GetTable gets a table from the schema.
-//
-//nolint:revive
func (s *SchemaState) GetTable(tableName string) (*TableState, *WalkThroughError) {
table, exists := s.tableSet[tableName]
if !exists {
@@ -728,19 +646,17 @@ func (s *SchemaState) DropTable(tableName string, checkViewDependency func(*Tabl
}
// RenameTable renames a table in the schema.
-//
-//nolint:revive
func (s *SchemaState) RenameTable(oldName string, newName string) *WalkThroughError {
if oldName == newName {
return nil
}
- table, exists := s.getTable(oldName)
+ table, exists := mysqlGetTable(s, oldName)
if !exists {
return NewTableNotExistsError(oldName)
}
- if _, exists := s.getTable(newName); exists {
+ if _, exists := mysqlGetTable(s, newName); exists {
return NewTableExistsError(newName)
}
@@ -762,9 +678,7 @@ func (s *SchemaState) RenameTable(oldName string, newName string) *WalkThroughEr
// Column-level operations.
// CreateColumn creates a new column in the table.
-//
-//nolint:revive
-func (t *TableState) CreateColumn(columnName string, columnType *string, nullable *bool, defaultValue *string, position *int, characterSet *string, collation *string, comment *string) *WalkThroughError {
+func (t *TableState) CreateColumn(columnName string, columnType string, nullable bool, defaultValue string, position int, characterSet string, collation string, comment string) *WalkThroughError {
if _, exists := t.columnSet[strings.ToLower(columnName)]; exists {
return &WalkThroughError{
Code: code.ColumnExists,
@@ -773,13 +687,13 @@ func (t *TableState) CreateColumn(columnName string, columnType *string, nullabl
}
pos := len(t.columnSet) + 1
- if position != nil {
- pos = *position
+ if position != 0 {
+ pos = position
}
col := &ColumnState{
name: columnName,
- position: &pos,
+ position: pos,
defaultValue: defaultValue,
nullable: nullable,
columnType: columnType,
@@ -793,8 +707,6 @@ func (t *TableState) CreateColumn(columnName string, columnType *string, nullabl
}
// GetColumn gets a column from the table.
-//
-//nolint:revive
func (t *TableState) GetColumn(columnName string) (*ColumnState, *WalkThroughError) {
column, exists := t.columnSet[strings.ToLower(columnName)]
if !exists {
@@ -804,8 +716,6 @@ func (t *TableState) GetColumn(columnName string) (*ColumnState, *WalkThroughErr
}
// DropColumn drops a column from the table.
-//
-//nolint:revive
func (t *TableState) DropColumn(columnName string, checkViewDependency func(*ColumnState) ([]string, *WalkThroughError)) *WalkThroughError {
column, exists := t.columnSet[strings.ToLower(columnName)]
if !exists {
@@ -836,8 +746,8 @@ func (t *TableState) DropColumn(columnName string, checkViewDependency func(*Col
// Modify the column position
for _, col := range t.columnSet {
- if *col.position > *column.position {
- *col.position--
+ if col.position > column.position {
+ col.position--
}
}
@@ -863,8 +773,6 @@ func (t *TableState) DropColumn(columnName string, checkViewDependency func(*Col
}
// RenameColumn renames a column in the table.
-//
-//nolint:revive
func (t *TableState) RenameColumn(oldName string, newName string) *WalkThroughError {
if strings.EqualFold(oldName, newName) {
return nil
@@ -900,9 +808,7 @@ func (t *TableState) RenameColumn(oldName string, newName string) *WalkThroughEr
// Index-level operations.
// CreateIndex creates a new index in the table.
-//
-//nolint:revive
-func (t *TableState) CreateIndex(indexName string, expressionList []string, unique bool, indexType string, primary bool, visible *bool, comment *string, isConstraint bool, identifierMap identifierMap) *WalkThroughError {
+func (t *TableState) CreateIndex(indexName string, expressionList []string, unique bool, indexType string, primary bool, visible bool, comment string, isConstraint bool, identifierMap identifierMap) *WalkThroughError {
if len(expressionList) == 0 {
return &WalkThroughError{
Code: code.IndexEmptyKeys,
@@ -938,19 +844,12 @@ func (t *TableState) CreateIndex(indexName string, expressionList []string, uniq
}
}
- if visible == nil {
- visible = newTruePointer()
- }
- if comment == nil {
- comment = newEmptyStringPointer()
- }
-
index := &IndexState{
name: indexName,
expressionList: expressionList,
- indexType: &indexType,
- unique: &unique,
- primary: &primary,
+ indexType: indexType,
+ unique: unique,
+ primary: primary,
visible: visible,
comment: comment,
isConstraint: isConstraint,
@@ -966,8 +865,6 @@ func (t *TableState) CreateIndex(indexName string, expressionList []string, uniq
}
// CreatePrimaryKey creates a primary key index in the table.
-//
-//nolint:revive
func (t *TableState) CreatePrimaryKey(keyList []string, indexType string, identifierMap identifierMap) *WalkThroughError {
pkName := PrimaryKeyName
@@ -988,11 +885,11 @@ func (t *TableState) CreatePrimaryKey(keyList []string, indexType string, identi
pk := &IndexState{
name: pkName,
expressionList: keyList,
- indexType: &indexType,
- unique: newTruePointer(),
- primary: newTruePointer(),
- visible: newTruePointer(),
- comment: newEmptyStringPointer(),
+ indexType: indexType,
+ unique: true,
+ primary: true,
+ visible: true,
+ comment: "",
isConstraint: true,
}
t.indexSet[strings.ToLower(pkName)] = pk
@@ -1015,8 +912,6 @@ func (t *TableState) GetIndex(indexName string) (*IndexState, *WalkThroughError)
}
// DropIndex drops an index from the table.
-//
-//nolint:revive
func (t *TableState) DropIndex(indexName string, identifierMap identifierMap) *WalkThroughError {
if _, exists := t.indexSet[strings.ToLower(indexName)]; !exists {
if strings.EqualFold(indexName, PrimaryKeyName) {
@@ -1038,8 +933,6 @@ func (t *TableState) DropIndex(indexName string, identifierMap identifierMap) *W
}
// RenameIndex renames an index in the table.
-//
-//nolint:revive
func (t *TableState) RenameIndex(oldName string, newName string, identifierMap identifierMap) *WalkThroughError {
// For MySQL, the primary key has a special name 'PRIMARY'.
if strings.ToUpper(oldName) == PrimaryKeyName || strings.ToUpper(newName) == PrimaryKeyName {
@@ -1080,7 +973,7 @@ func (t *TableState) RenameIndex(oldName string, newName string, identifierMap i
// View-level operations.
// CreateView creates a new view in the schema.
-func (s *SchemaState) CreateView(viewName string, definition *string, comment *string) (*ViewState, *WalkThroughError) {
+func (s *SchemaState) CreateView(viewName string, definition string, comment string) (*ViewState, *WalkThroughError) {
view := &ViewState{
name: viewName,
definition: definition,
@@ -1131,35 +1024,35 @@ func (d *DatabaseState) SetCollation(collation string) {
// SetTableEngine sets the engine of the table.
func (t *TableState) SetEngine(engine string) {
- t.engine = newStringPointer(engine)
+ t.engine = engine
}
// SetTableCollation sets the collation of the table.
func (t *TableState) SetCollation(collation string) {
- t.collation = newStringPointer(collation)
+ t.collation = collation
}
// SetTableComment sets the comment of the table.
func (t *TableState) SetComment(comment string) {
- t.comment = newStringPointer(comment)
+ t.comment = comment
}
// SetColumnNullable sets the nullable property of the column.
func (col *ColumnState) SetNullable(nullable bool) {
- col.nullable = newBoolPointer(nullable)
+ col.nullable = nullable
}
// SetColumnDefault sets the default value of the column.
-func (col *ColumnState) SetDefault(defaultValue *string) {
+func (col *ColumnState) SetDefault(defaultValue string) {
col.defaultValue = defaultValue
}
// SetColumnType sets the type of the column.
func (col *ColumnState) SetType(columnType string) {
- col.columnType = newStringPointer(columnType)
+ col.columnType = columnType
}
// SetIndexVisibility sets the visibility of the index.
func (idx *IndexState) SetVisibility(visible bool) {
- idx.visible = newBoolPointer(visible)
+ idx.visible = visible
}
diff --git a/backend/plugin/advisor/catalog/walk_through.go b/backend/plugin/advisor/catalog/walk_through.go
index 4932a87b33..4fde6e32fd 100644
--- a/backend/plugin/advisor/catalog/walk_through.go
+++ b/backend/plugin/advisor/catalog/walk_through.go
@@ -119,11 +119,9 @@ func (d *DatabaseState) isCurrentDatabase(database string) bool {
return compareIdentifier(d.name, database, d.ignoreCaseSensitive)
}
-// getTable returns the table with the given name if it exists in the schema.
-// TODO(zp): It's used for MySQL, we should refactor the package to make it more clear.
-//
-//nolint:revive
-func (s *SchemaState) getTable(table string) (*TableState, bool) {
+// mysqlGetTable returns the table with the given name if it exists in the schema.
+// It performs case-insensitive comparison for MySQL/TiDB.
+func mysqlGetTable(s *SchemaState, table string) (*TableState, bool) {
for k, v := range s.tableSet {
if compareIdentifier(k, table, s.ignoreCaseSensitive) {
return v, true
@@ -133,13 +131,12 @@ func (s *SchemaState) getTable(table string) (*TableState, bool) {
return nil, false
}
-//nolint:revive
-func (s *SchemaState) renameTable(oldName string, newName string) *WalkThroughError {
+func mysqlRenameTable(s *SchemaState, oldName string, newName string) *WalkThroughError {
if oldName == newName {
return nil
}
- table, exists := s.getTable(oldName)
+ table, exists := mysqlGetTable(s, oldName)
if !exists {
return &WalkThroughError{
Code: code.TableNotExists,
@@ -147,7 +144,7 @@ func (s *SchemaState) renameTable(oldName string, newName string) *WalkThroughEr
}
}
- if _, exists := s.getTable(newName); exists {
+ if _, exists := mysqlGetTable(s, newName); exists {
return &WalkThroughError{
Code: code.TableExists,
Content: fmt.Sprintf("Table `%s` already exists", newName),
@@ -160,18 +157,6 @@ func (s *SchemaState) renameTable(oldName string, newName string) *WalkThroughEr
return nil
}
-func (d *DatabaseState) createSchema() *SchemaState {
- schema := &SchemaState{
- ignoreCaseSensitive: d.ignoreCaseSensitive,
- name: "",
- tableSet: make(tableStateMap),
- viewSet: make(viewStateMap),
- }
-
- d.schemaSet[""] = schema
- return schema
-}
-
// PostgreSQL-specific helper methods.
func parseViewName(viewName string) (string, string, error) {
@@ -222,52 +207,22 @@ func (s *SchemaState) pgGeneratePrimaryKeyName(tableName string) string {
}
}
-//nolint:revive
-func (d *DatabaseState) getSchema(schemaName string) (*SchemaState, *WalkThroughError) {
+func (d *DatabaseState) getOrCreatePublicSchema(schemaName string) (*SchemaState, *WalkThroughError) {
if schemaName == "" {
schemaName = publicSchemaName
}
- schema, exists := d.schemaSet[schemaName]
- if !exists {
- if schemaName != publicSchemaName {
- return nil, &WalkThroughError{
- Code: code.SchemaNotExists,
- Content: fmt.Sprintf("The schema %q doesn't exist", schemaName),
- }
- }
- schema = &SchemaState{
- ignoreCaseSensitive: d.ignoreCaseSensitive,
- name: publicSchemaName,
- tableSet: make(tableStateMap),
- viewSet: make(viewStateMap),
- identifierMap: make(identifierMap),
- }
- d.schemaSet[publicSchemaName] = schema
- }
- return schema, nil
-}
-//nolint:revive
-func (t *TableState) getColumn(columnName string) (*ColumnState, *WalkThroughError) {
- column, exists := t.columnSet[columnName]
- if !exists {
- return nil, &WalkThroughError{
- Code: code.ColumnNotExists,
- Content: fmt.Sprintf("The column %q does not exist in the table %q", columnName, t.name),
- }
+ schema, err := d.GetSchema(schemaName)
+ if err == nil {
+ return schema, nil
}
- return column, nil
-}
-func (s *SchemaState) pgGetTable(tableName string) (*TableState, *WalkThroughError) {
- table, exists := s.tableSet[tableName]
- if !exists {
- return nil, &WalkThroughError{
- Code: code.TableNotExists,
- Content: fmt.Sprintf("The table %q does not exist in schema %q", tableName, s.name),
- }
+ // Only auto-create the "public" schema
+ if schemaName != publicSchemaName {
+ return nil, err
}
- return table, nil
+
+ return d.CreateSchema(publicSchemaName), nil
}
func (s *SchemaState) getIndex(indexName string) (*TableState, *IndexState, *WalkThroughError) {
diff --git a/backend/plugin/advisor/catalog/walk_through_for_mysql.go b/backend/plugin/advisor/catalog/walk_through_for_mysql.go
index 789300a03d..336427ab54 100644
--- a/backend/plugin/advisor/catalog/walk_through_for_mysql.go
+++ b/backend/plugin/advisor/catalog/walk_through_for_mysql.go
@@ -19,7 +19,9 @@ func MySQLWalkThrough(d *DatabaseState, ast any) error {
// We define the Catalog as Database -> Schema -> Table. The Schema is only for PostgreSQL.
// So we use a Schema whose name is empty for other engines, such as MySQL.
// If there is no empty-string-name schema, create it to avoid corner cases.
- d.GetOrCreateSchema("")
+ if !d.HasSchema("") {
+ d.CreateSchema("")
+ }
nodeList, ok := ast.([]*mysqlparser.ParseResult)
if !ok {
@@ -97,8 +99,12 @@ func (l *mysqlListener) EnterCreateTable(ctx *mysql.CreateTableContext) {
return
}
- schema := l.databaseState.GetOrCreateSchema("")
- if _, exists := schema.getTable(tableName); exists {
+ schema, err := l.databaseState.GetSchema("")
+ if err != nil {
+ l.err = err
+ return
+ }
+ if _, exists := mysqlGetTable(schema, tableName); exists {
if ctx.IfNotExists() != nil {
return
}
@@ -187,9 +193,13 @@ func (l *mysqlListener) EnterDropTable(ctx *mysql.DropTableContext) {
}
}
- schema := l.databaseState.GetOrCreateSchema("")
+ schema, err := l.databaseState.GetSchema("")
+ if err != nil {
+ l.err = err
+ return
+ }
- table, exists := schema.getTable(tableName)
+ table, exists := mysqlGetTable(schema, tableName)
if !exists {
if ctx.IfExists() != nil {
return
@@ -368,7 +378,11 @@ func (l *mysqlListener) EnterAlterTable(ctx *mysql.AlterTableContext) {
// rename table.
case item.RENAME_SYMBOL() != nil && item.TableName() != nil:
_, newTableName := mysqlparser.NormalizeMySQLTableName(item.TableName())
- schema := l.databaseState.GetOrCreateSchema("")
+ schema, err := l.databaseState.GetSchema("")
+ if err != nil {
+ l.err = err
+ return
+ }
if err := schema.RenameTable(table.name, newTableName); err != nil {
l.err = err
return
@@ -537,7 +551,11 @@ func (l *mysqlListener) EnterRenameTableStatement(ctx *mysql.RenameTableStatemen
return
}
for _, pair := range ctx.AllRenamePair() {
- schema := l.databaseState.GetOrCreateSchema("")
+ schema, err := l.databaseState.GetSchema("")
+ if err != nil {
+ l.err = err
+ return
+ }
_, oldTableName := mysqlparser.NormalizeMySQLTableRef(pair.TableRef())
_, newTableName := mysqlparser.NormalizeMySQLTableName(pair.TableName())
@@ -546,12 +564,12 @@ func (l *mysqlListener) EnterRenameTableStatement(ctx *mysql.RenameTableStatemen
if compareIdentifier(oldTableName, newTableName, l.databaseState.ignoreCaseSensitive) {
return
}
- table, exists := schema.getTable(oldTableName)
+ table, exists := mysqlGetTable(schema, oldTableName)
if !exists {
l.err = NewTableNotExistsError(oldTableName)
return
}
- if _, exists := schema.getTable(newTableName); exists {
+ if _, exists := mysqlGetTable(schema, newTableName); exists {
l.err = NewTableExistsError(newTableName)
return
}
@@ -559,7 +577,7 @@ func (l *mysqlListener) EnterRenameTableStatement(ctx *mysql.RenameTableStatemen
table.name = newTableName
schema.tableSet[table.name] = table
} else if l.databaseState.mysqlMoveToOtherDatabase(pair) {
- _, exists := schema.getTable(oldTableName)
+ _, exists := mysqlGetTable(schema, oldTableName)
if !exists {
l.err = NewTableNotExistsError(oldTableName)
return
@@ -649,9 +667,9 @@ func (t *TableState) mysqlChangeIndexVisibility(indexName string, visibility mys
}
switch {
case visibility.VISIBLE_SYMBOL() != nil:
- index.visible = newTruePointer()
+ index.visible = true
case visibility.INVISIBLE_SYMBOL() != nil:
- index.visible = newFalsePointer()
+ index.visible = false
default:
// No visibility specified
}
@@ -675,8 +693,8 @@ func (t *TableState) mysqlAlterColumn(itemDef mysql.IAlterListItemContext) *Walk
// SET DEFAULT.
case itemDef.DEFAULT_SYMBOL() != nil:
if itemDef.SignedLiteral() != nil && itemDef.SignedLiteral().Literal() != nil && itemDef.SignedLiteral().Literal().NullLiteral() == nil {
- if colState.columnType != nil {
- switch strings.ToLower(*colState.columnType) {
+ if colState.columnType != "" {
+ switch strings.ToLower(colState.columnType) {
case "blob", "tinyblob", "mediumblob", "longblob",
"text", "tinytext", "mediumtext", "longtext",
"json",
@@ -701,9 +719,9 @@ func (t *TableState) mysqlAlterColumn(itemDef mysql.IAlterListItemContext) *Walk
// No default value expression
}
- colState.defaultValue = &defaultValue
+ colState.defaultValue = defaultValue
} else {
- if colState.nullable != nil && !*colState.nullable {
+ if !colState.nullable {
return &WalkThroughError{
Code: code.SetNullDefaultForNotNullColumn,
// Content comes from MySQL Error content.
@@ -711,14 +729,14 @@ func (t *TableState) mysqlAlterColumn(itemDef mysql.IAlterListItemContext) *Walk
}
}
- colState.defaultValue = nil
+ colState.defaultValue = ""
}
// SET VISIBLE/INVISIBLE.
default:
}
case itemDef.DROP_SYMBOL() != nil && itemDef.DEFAULT_SYMBOL() != nil:
// DROP DEFAULT.
- colState.defaultValue = nil
+ colState.defaultValue = ""
default:
// Other ALTER operations
}
@@ -740,7 +758,7 @@ func (t *TableState) mysqlCompleteTableChangeColumn(oldColumnName string, newCol
return NewColumnNotExistsError(t.name, oldColumnName)
}
- pos := *column.position
+ pos := column.position
if position == nil {
position = &mysqlColumnPosition{
@@ -752,7 +770,7 @@ func (t *TableState) mysqlCompleteTableChangeColumn(oldColumnName string, newCol
position.tp = ColumnPositionFirst
} else {
for _, col := range t.columnSet {
- if *col.position == pos-1 {
+ if col.position == pos-1 {
position.tp = ColumnPositionAfter
position.relativeColumn = col.name
break
@@ -763,8 +781,8 @@ func (t *TableState) mysqlCompleteTableChangeColumn(oldColumnName string, newCol
// drop column from columnSet.
for _, col := range t.columnSet {
- if *col.position > pos {
- *col.position--
+ if col.position > pos {
+ col.position--
}
}
delete(t.columnSet, strings.ToLower(column.name))
@@ -816,7 +834,10 @@ func (d *DatabaseState) mysqlCopyTable(databaseName, tableName, referTable strin
return err
}
- schema := d.GetOrCreateSchema("")
+ schema, err := d.GetSchema("")
+ if err != nil {
+ return err
+ }
table := targetTable.copy()
table.name = tableName
schema.tableSet[table.name] = table
@@ -828,9 +849,12 @@ func (d *DatabaseState) mysqlFindTableState(databaseName, tableName string) (*Ta
return nil, NewAccessOtherDatabaseError(d.name, databaseName)
}
- schema := d.GetOrCreateSchema("")
+ schema, err := d.GetSchema("")
+ if err != nil {
+ return nil, err
+ }
- table, exists := schema.getTable(tableName)
+ table, exists := mysqlGetTable(schema, tableName)
if !exists {
return nil, NewTableNotExistsError(tableName)
}
@@ -946,9 +970,9 @@ func (t *TableState) mysqlValidateColumnList(columnList []string, primary bool,
return NewColumnNotExistsError(t.name, columnName)
}
if primary {
- column.nullable = newFalsePointer()
+ column.nullable = false
}
- if isSpatial && column.nullable != nil && *column.nullable {
+ if isSpatial && column.nullable {
return &WalkThroughError{
Code: code.SpatialIndexKeyNullable,
// The error content comes from MySQL.
@@ -970,9 +994,9 @@ func (t *TableState) mysqlValidateExpressionList(expressionList []string, primar
}
if primary {
- column.nullable = newFalsePointer()
+ column.nullable = false
}
- if isSpatial && column.nullable != nil && *column.nullable {
+ if isSpatial && column.nullable {
return &WalkThroughError{
Code: code.SpatialIndexKeyNullable,
// The error content comes from MySQL.
@@ -1051,13 +1075,13 @@ func (t *TableState) mysqlCreateColumn(columnName string, fieldDef mysql.IFieldD
col := &ColumnState{
name: columnName,
- position: &pos,
- defaultValue: nil,
- nullable: newTruePointer(),
- columnType: newStringPointer(columnType),
- characterSet: newStringPointer(characterSet),
- collation: newStringPointer(collation),
- comment: newEmptyStringPointer(),
+ position: pos,
+ defaultValue: "",
+ nullable: true,
+ columnType: columnType,
+ characterSet: characterSet,
+ collation: collation,
+ comment: "",
}
setNullDefault := false
@@ -1071,7 +1095,7 @@ func (t *TableState) mysqlCreateColumn(columnName string, fieldDef mysql.IFieldD
}
// not null.
if attribute.NullLiteral() != nil && attribute.NOT_SYMBOL() != nil {
- col.nullable = newFalsePointer()
+ col.nullable = false
}
if attribute.GetValue() != nil {
switch attribute.GetValue().GetTokenType() {
@@ -1090,14 +1114,14 @@ func (t *TableState) mysqlCreateColumn(columnName string, fieldDef mysql.IFieldD
}
// handle default 'null' etc.
defaultValue := mysqlparser.NormalizeMySQLSignedLiteral(attribute.SignedLiteral())
- col.defaultValue = &defaultValue
+ col.defaultValue = defaultValue
// comment.
case mysql.MySQLParserCOMMENT_SYMBOL:
if attribute.TextLiteral() == nil {
continue
}
comment := mysqlparser.NormalizeMySQLTextLiteral(attribute.TextLiteral())
- col.comment = &comment
+ col.comment = comment
// on update now().
case mysql.MySQLParserON_SYMBOL:
if attribute.UPDATE_SYMBOL() == nil || attribute.NOW_SYMBOL() == nil {
@@ -1112,7 +1136,7 @@ func (t *TableState) mysqlCreateColumn(columnName string, fieldDef mysql.IFieldD
// primary key.
case mysql.MySQLParserKEY_SYMBOL:
// the key attribute for in a column meaning primary key.
- col.nullable = newFalsePointer()
+ col.nullable = false
// we need to check the key type which generated by tidb parser.
if err := t.mysqlCreatePrimaryKey([]string{strings.ToLower(col.name)}, "BTREE"); err != nil {
return err
@@ -1138,7 +1162,7 @@ func (t *TableState) mysqlCreateColumn(columnName string, fieldDef mysql.IFieldD
}
}
- if col.nullable != nil && !*col.nullable && setNullDefault {
+ if !col.nullable && setNullDefault {
return &WalkThroughError{
Code: code.SetNullDefaultForNotNullColumn,
// Content comes from MySQL Error content.
@@ -1157,7 +1181,7 @@ func (t *TableState) mysqlReorderColumn(position *mysqlColumnPosition) (int, *Wa
return len(t.columnSet) + 1, nil
case ColumnPositionFirst:
for _, column := range t.columnSet {
- *column.position++
+ column.position++
}
return 1, nil
case ColumnPositionAfter:
@@ -1167,11 +1191,11 @@ func (t *TableState) mysqlReorderColumn(position *mysqlColumnPosition) (int, *Wa
return 0, NewColumnNotExistsError(t.name, columnName)
}
for _, col := range t.columnSet {
- if *col.position > *column.position {
- *col.position++
+ if col.position > column.position {
+ col.position++
}
}
- return *column.position + 1, nil
+ return column.position + 1, nil
default:
return 0, &WalkThroughError{
Code: code.Unsupported,
@@ -1209,11 +1233,11 @@ func (t *TableState) mysqlCreateIndex(name string, keyList []string, unique bool
index := &IndexState{
name: name,
expressionList: keyList,
- indexType: &tp,
- unique: &unique,
- primary: newFalsePointer(),
- visible: newTruePointer(),
- comment: newEmptyStringPointer(),
+ indexType: tp,
+ unique: unique,
+ primary: false,
+ visible: true,
+ comment: "",
}
// need to check the visibility of index.
@@ -1229,7 +1253,7 @@ func (t *TableState) mysqlCreateIndex(name string, keyList []string, unique bool
continue
}
if attribute.CommonIndexOption().Visibility() != nil && attribute.CommonIndexOption().Visibility().INVISIBLE_SYMBOL() != nil {
- index.visible = newFalsePointer()
+ index.visible = false
}
}
@@ -1239,7 +1263,7 @@ func (t *TableState) mysqlCreateIndex(name string, keyList []string, unique bool
continue
}
if attribute.CommonIndexOption().Visibility() != nil && attribute.CommonIndexOption().Visibility().INVISIBLE_SYMBOL() != nil {
- index.visible = newFalsePointer()
+ index.visible = false
}
}
@@ -1250,7 +1274,7 @@ func (t *TableState) mysqlCreateIndex(name string, keyList []string, unique bool
continue
}
if attribute.CommonIndexOption().Visibility() != nil && attribute.CommonIndexOption().Visibility().INVISIBLE_SYMBOL() != nil {
- index.visible = newFalsePointer()
+ index.visible = false
}
}
@@ -1260,7 +1284,7 @@ func (t *TableState) mysqlCreateIndex(name string, keyList []string, unique bool
continue
}
if attribute.CommonIndexOption().Visibility() != nil && attribute.CommonIndexOption().Visibility().INVISIBLE_SYMBOL() != nil {
- index.visible = newFalsePointer()
+ index.visible = false
}
}
@@ -1271,7 +1295,7 @@ func (t *TableState) mysqlCreateIndex(name string, keyList []string, unique bool
continue
}
if attribute.CommonIndexOption().Visibility() != nil && attribute.CommonIndexOption().Visibility().INVISIBLE_SYMBOL() != nil {
- index.visible = newFalsePointer()
+ index.visible = false
}
}
@@ -1281,7 +1305,7 @@ func (t *TableState) mysqlCreateIndex(name string, keyList []string, unique bool
continue
}
if attribute.CommonIndexOption().Visibility() != nil && attribute.CommonIndexOption().Visibility().INVISIBLE_SYMBOL() != nil {
- index.visible = newFalsePointer()
+ index.visible = false
}
}
@@ -1300,11 +1324,11 @@ func (t *TableState) mysqlCreatePrimaryKey(keys []string, tp string) *WalkThroug
pk := &IndexState{
name: PrimaryKeyName,
expressionList: keys,
- indexType: &tp,
- unique: newTruePointer(),
- primary: newTruePointer(),
- visible: newTruePointer(),
- comment: newEmptyStringPointer(),
+ indexType: tp,
+ unique: true,
+ primary: true,
+ visible: true,
+ comment: "",
}
t.indexSet[strings.ToLower(pk.name)] = pk
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 be7fd61d3f..f283d70bc0 100644
--- a/backend/plugin/advisor/catalog/walk_through_for_pg.go
+++ b/backend/plugin/advisor/catalog/walk_through_for_pg.go
@@ -99,7 +99,7 @@ func (l *pgCatalogListener) EnterCreatestmt(ctx *parser.CreatestmtContext) {
}
// Get or create schema
- schema, err := l.databaseState.getSchema(schemaName)
+ schema, err := l.databaseState.getOrCreatePublicSchema(schemaName)
if err != nil {
l.setError(err)
return
@@ -133,7 +133,7 @@ func (l *pgCatalogListener) EnterCreatestmt(ctx *parser.CreatestmtContext) {
for _, elem := range allElements {
// Handle column definitions
if elem.ColumnDef() != nil {
- if err := createColumn(schema, table, elem.ColumnDef()); err != nil {
+ if err := pgCreateColumn(schema, table, elem.ColumnDef()); err != nil {
l.setError(err)
return
}
@@ -150,7 +150,7 @@ func (l *pgCatalogListener) EnterCreatestmt(ctx *parser.CreatestmtContext) {
}
// createColumn creates a column in the table.
-func createColumn(schema *SchemaState, table *TableState, columnDef parser.IColumnDefContext) *WalkThroughError {
+func pgCreateColumn(schema *SchemaState, table *TableState, columnDef parser.IColumnDefContext) *WalkThroughError {
if columnDef == nil {
return nil
}
@@ -182,11 +182,11 @@ func createColumn(schema *SchemaState, table *TableState, columnDef parser.IColu
pos := len(table.columnSet) + 1
columnState := &ColumnState{
name: columnName,
- position: &pos,
- defaultValue: nil,
- nullable: newTruePointer(),
- columnType: &columnType,
- collation: nil,
+ position: pos,
+ defaultValue: "",
+ nullable: true,
+ columnType: columnType,
+ collation: "",
}
table.columnSet[columnState.name] = columnState
@@ -201,7 +201,7 @@ func createColumn(schema *SchemaState, table *TableState, columnDef parser.IColu
// Handle NOT NULL
if elem.NOT() != nil && elem.NULL_P() != nil {
- columnState.nullable = newFalsePointer()
+ columnState.nullable = false
}
// Handle DEFAULT
@@ -209,7 +209,7 @@ func createColumn(schema *SchemaState, table *TableState, columnDef parser.IColu
// Extract default expression from B_expr
if elem.B_expr() != nil {
defaultValue := elem.B_expr().GetText()
- columnState.defaultValue = &defaultValue
+ columnState.defaultValue = defaultValue
}
}
@@ -224,15 +224,15 @@ func createColumn(schema *SchemaState, table *TableState, columnDef parser.IColu
}
// Set column as NOT NULL
- columnState.nullable = newFalsePointer()
+ columnState.nullable = false
// Create primary key index
index := &IndexState{
name: constraintName,
expressionList: []string{columnName},
- indexType: newStringPointer("btree"),
- unique: newTruePointer(),
- primary: newTruePointer(),
+ indexType: "btree",
+ unique: true,
+ primary: true,
isConstraint: true,
}
table.indexSet[index.name] = index
@@ -254,9 +254,9 @@ func createColumn(schema *SchemaState, table *TableState, columnDef parser.IColu
index := &IndexState{
name: constraintName,
expressionList: []string{columnName},
- indexType: newStringPointer("btree"),
- unique: newTruePointer(),
- primary: newFalsePointer(),
+ indexType: "btree",
+ unique: true,
+ primary: false,
isConstraint: true,
}
table.indexSet[index.name] = index
@@ -298,7 +298,7 @@ func createTableConstraint(schema *SchemaState, table *TableState, constraint pa
// Set all PK columns as NOT NULL
for _, colName := range columnList {
if column, exists := table.columnSet[colName]; exists {
- column.nullable = newFalsePointer()
+ column.nullable = false
} else {
return NewColumnNotExistsError(table.name, colName)
}
@@ -319,9 +319,9 @@ func createTableConstraint(schema *SchemaState, table *TableState, constraint pa
index := &IndexState{
name: pkName,
expressionList: columnList,
- indexType: newStringPointer("btree"),
- unique: newTruePointer(),
- primary: newTruePointer(),
+ indexType: "btree",
+ unique: true,
+ primary: true,
isConstraint: true,
}
table.indexSet[index.name] = index
@@ -363,9 +363,9 @@ func createTableConstraint(schema *SchemaState, table *TableState, constraint pa
index := &IndexState{
name: indexName,
expressionList: columnList,
- indexType: newStringPointer("btree"),
- unique: newTruePointer(),
- primary: newFalsePointer(),
+ indexType: "btree",
+ unique: true,
+ primary: false,
isConstraint: true,
}
table.indexSet[index.name] = index
@@ -398,7 +398,7 @@ func (l *pgCatalogListener) EnterIndexstmt(ctx *parser.IndexstmtContext) {
tableName := extractTableName(relationExpr.Qualified_name())
schemaName := extractSchemaName(relationExpr.Qualified_name())
- schema, err := l.databaseState.getSchema(schemaName)
+ schema, err := l.databaseState.getOrCreatePublicSchema(schemaName)
if err != nil {
l.setError(err)
return
@@ -484,9 +484,9 @@ func (l *pgCatalogListener) EnterIndexstmt(ctx *parser.IndexstmtContext) {
index := &IndexState{
name: indexName,
expressionList: columnList,
- indexType: newStringPointer(indexType),
- unique: newBoolPointer(isUnique),
- primary: newFalsePointer(),
+ indexType: indexType,
+ unique: isUnique,
+ primary: false,
isConstraint: false,
}
@@ -545,13 +545,13 @@ func (l *pgCatalogListener) EnterAltertablestmt(ctx *parser.AltertablestmtContex
}
// Get schema and table
- schema, err := l.databaseState.getSchema(schemaName)
+ schema, err := l.databaseState.getOrCreatePublicSchema(schemaName)
if err != nil {
l.setError(err)
return
}
- table, err := schema.pgGetTable(tableName)
+ table, err := schema.GetTable(tableName)
if err != nil {
l.setError(err)
return
@@ -703,7 +703,7 @@ func (l *pgCatalogListener) alterTableDropColumn(schema *SchemaState, table *Tab
// alterTableAlterColumnType handles ALTER COLUMN TYPE command.
func (l *pgCatalogListener) alterTableAlterColumnType(schema *SchemaState, table *TableState, columnName string, typeString string) {
- column, err := table.getColumn(columnName)
+ column, err := table.GetColumn(columnName)
if err != nil {
l.setError(err)
return
@@ -727,7 +727,7 @@ func (l *pgCatalogListener) alterTableAlterColumnType(schema *SchemaState, table
}
// Update column type
- column.columnType = &typeString
+ column.columnType = typeString
}
// alterTableAddColumn handles ADD COLUMN command.
@@ -762,10 +762,10 @@ func (l *pgCatalogListener) alterTableAddColumn(schema *SchemaState, table *Tabl
// Create column state
columnState := &ColumnState{
name: columnName,
- position: &pos,
- nullable: newTruePointer(),
- columnType: &typeString,
- defaultValue: nil,
+ position: pos,
+ nullable: true,
+ columnType: typeString,
+ defaultValue: "",
}
table.columnSet[columnName] = columnState
@@ -780,14 +780,14 @@ func (l *pgCatalogListener) alterTableAddColumn(schema *SchemaState, table *Tabl
// Handle NOT NULL
if elem.NOT() != nil && elem.NULL_P() != nil {
- columnState.nullable = newFalsePointer()
+ columnState.nullable = false
}
// Handle DEFAULT
if elem.DEFAULT() != nil {
if elem.B_expr() != nil {
defaultValue := elem.B_expr().GetText()
- columnState.defaultValue = &defaultValue
+ columnState.defaultValue = defaultValue
}
}
@@ -808,9 +808,9 @@ func (l *pgCatalogListener) alterTableAddColumn(schema *SchemaState, table *Tabl
index := &IndexState{
name: constraintName,
expressionList: []string{columnName},
- indexType: newStringPointer("btree"),
- unique: newTruePointer(),
- primary: newFalsePointer(),
+ indexType: "btree",
+ unique: true,
+ primary: false,
isConstraint: true,
}
table.indexSet[index.name] = index
@@ -831,14 +831,14 @@ func (l *pgCatalogListener) alterTableAddColumn(schema *SchemaState, table *Tabl
l.setError(NewRelationExistsError(constraintName, schema.name))
return
}
- columnState.nullable = newFalsePointer()
+ columnState.nullable = false
// Create primary key index
index := &IndexState{
name: constraintName,
expressionList: []string{columnName},
- indexType: newStringPointer("btree"),
- unique: newTruePointer(),
- primary: newTruePointer(),
+ indexType: "btree",
+ unique: true,
+ primary: true,
isConstraint: true,
}
table.indexSet[index.name] = index
@@ -880,39 +880,39 @@ func (l *pgCatalogListener) alterTableDropConstraint(schema *SchemaState, table
// alterTableSetDefault handles ALTER COLUMN SET DEFAULT command.
func (l *pgCatalogListener) alterTableSetDefault(table *TableState, columnName string, defaultValue string) {
- column, err := table.getColumn(columnName)
+ column, err := table.GetColumn(columnName)
if err != nil {
l.setError(err)
return
}
- column.defaultValue = &defaultValue
+ column.defaultValue = defaultValue
}
// alterTableDropDefault handles ALTER COLUMN DROP DEFAULT command.
func (l *pgCatalogListener) alterTableDropDefault(table *TableState, columnName string) {
- column, err := table.getColumn(columnName)
+ column, err := table.GetColumn(columnName)
if err != nil {
l.setError(err)
return
}
- column.defaultValue = nil
+ column.defaultValue = ""
}
// alterTableSetNotNull handles ALTER COLUMN SET NOT NULL command.
func (l *pgCatalogListener) alterTableSetNotNull(table *TableState, columnName string) {
- column, err := table.getColumn(columnName)
+ column, err := table.GetColumn(columnName)
if err != nil {
l.setError(err)
return
}
- column.nullable = newFalsePointer()
+ column.nullable = false
}
// renameTable handles RENAME TO for tables.
-func (*pgCatalogListener) renameTable(schema *SchemaState, table *TableState, newName string) *WalkThroughError {
+func (*pgCatalogListener) pgRenameTable(schema *SchemaState, table *TableState, newName string) *WalkThroughError {
// Check if new name already exists
if _, exists := schema.identifierMap[newName]; exists {
return NewRelationExistsError(newName, schema.name)
@@ -934,7 +934,7 @@ func (*pgCatalogListener) renameTable(schema *SchemaState, table *TableState, ne
// renameColumn handles RENAME COLUMN.
func (*pgCatalogListener) renameColumn(table *TableState, oldName string, newName string) *WalkThroughError {
- column, err := table.getColumn(oldName)
+ column, err := table.GetColumn(oldName)
if err != nil {
return err
}
@@ -1108,7 +1108,7 @@ func (l *pgCatalogListener) dropTable(anyName parser.IAny_nameContext, ifExists
tableName = parts[1]
}
- schema, err := l.databaseState.getSchema(schemaName)
+ schema, err := l.databaseState.getOrCreatePublicSchema(schemaName)
if err != nil {
if ifExists {
return nil
@@ -1116,7 +1116,7 @@ func (l *pgCatalogListener) dropTable(anyName parser.IAny_nameContext, ifExists
return err
}
- table, err := schema.pgGetTable(tableName)
+ table, err := schema.GetTable(tableName)
if err != nil {
if ifExists {
return nil
@@ -1164,7 +1164,7 @@ func (l *pgCatalogListener) dropView(anyName parser.IAny_nameContext, ifExists b
viewName = parts[1]
}
- schema, err := l.databaseState.getSchema(schemaName)
+ schema, err := l.databaseState.getOrCreatePublicSchema(schemaName)
if err != nil {
if ifExists {
return nil
@@ -1192,7 +1192,7 @@ func (l *pgCatalogListener) dropIndex(anyName parser.IAny_nameContext, ifExists
indexName = parts[1]
}
- schema, err := l.databaseState.getSchema(schemaName)
+ schema, err := l.databaseState.getOrCreatePublicSchema(schemaName)
if err != nil {
if ifExists {
return nil
@@ -1292,7 +1292,7 @@ func (l *pgCatalogListener) EnterRenamestmt(ctx *parser.RenamestmtContext) {
schemaName := extractSchemaName(ctx.Qualified_name())
newName := pgparser.NormalizePostgreSQLName(ctx.AllName()[0])
- schema, err := l.databaseState.getSchema(schemaName)
+ schema, err := l.databaseState.getOrCreatePublicSchema(schemaName)
if err != nil {
l.setError(err)
return
@@ -1328,7 +1328,7 @@ func (l *pgCatalogListener) EnterRenamestmt(ctx *parser.RenamestmtContext) {
schemaName = extractSchemaName(ctx.Relation_expr().Qualified_name())
}
- schema, err := l.databaseState.getSchema(schemaName)
+ schema, err := l.databaseState.getOrCreatePublicSchema(schemaName)
if err != nil {
l.setError(err)
return
@@ -1343,7 +1343,7 @@ func (l *pgCatalogListener) EnterRenamestmt(ctx *parser.RenamestmtContext) {
oldName := pgparser.NormalizePostgreSQLName(allNames[0])
newName := pgparser.NormalizePostgreSQLName(allNames[1])
- table, err := schema.pgGetTable(tableName)
+ table, err := schema.GetTable(tableName)
if err != nil {
l.setError(err)
return
@@ -1364,7 +1364,7 @@ func (l *pgCatalogListener) EnterRenamestmt(ctx *parser.RenamestmtContext) {
oldName := pgparser.NormalizePostgreSQLName(allNames[0])
newName := pgparser.NormalizePostgreSQLName(allNames[1])
- table, err := schema.pgGetTable(tableName)
+ table, err := schema.GetTable(tableName)
if err != nil {
l.setError(err)
return
@@ -1381,13 +1381,13 @@ func (l *pgCatalogListener) EnterRenamestmt(ctx *parser.RenamestmtContext) {
if tableName != "" && ctx.AllName() != nil && len(ctx.AllName()) > 0 {
newName := pgparser.NormalizePostgreSQLName(ctx.AllName()[0])
- table, err := schema.pgGetTable(tableName)
+ table, err := schema.GetTable(tableName)
if err != nil {
l.setError(err)
return
}
- if err := l.renameTable(schema, table, newName); err != nil {
+ if err := l.pgRenameTable(schema, table, newName); err != nil {
l.setError(err)
}
return
@@ -1424,7 +1424,7 @@ func (l *pgCatalogListener) EnterViewstmt(ctx *parser.ViewstmtContext) {
return
}
- schema, err := l.databaseState.getSchema(schemaName)
+ schema, err := l.databaseState.getOrCreatePublicSchema(schemaName)
if err != nil {
l.setError(err)
return
diff --git a/backend/plugin/advisor/catalog/walk_through_for_tidb.go b/backend/plugin/advisor/catalog/walk_through_for_tidb.go
index bfd06a5f90..74a5506470 100644
--- a/backend/plugin/advisor/catalog/walk_through_for_tidb.go
+++ b/backend/plugin/advisor/catalog/walk_through_for_tidb.go
@@ -18,7 +18,9 @@ func TiDBWalkThrough(d *DatabaseState, ast any) error {
// We define the Catalog as Database -> Schema -> Table. The Schema is only for PostgreSQL.
// So we use a Schema whose name is empty for other engines, such as MySQL.
// If there is no empty-string-name schema, create it to avoid corner cases.
- d.GetOrCreateSchema("")
+ if !d.HasSchema("") {
+ d.CreateSchema("")
+ }
nodeList, ok := ast.([]tidbast.StmtNode)
if !ok {
@@ -57,7 +59,7 @@ func (d *DatabaseState) changeState(in tidbast.StmtNode) (err *WalkThroughError)
case *tidbast.AlterTableStmt:
return d.alterTable(node)
case *tidbast.CreateIndexStmt:
- return d.createIndex(node)
+ return d.tidbHandleCreateIndex(node)
case *tidbast.DropIndexStmt:
return d.dropIndex(node)
case *tidbast.AlterDatabaseStmt:
@@ -67,17 +69,17 @@ func (d *DatabaseState) changeState(in tidbast.StmtNode) (err *WalkThroughError)
case *tidbast.CreateDatabaseStmt:
return NewAccessOtherDatabaseError(d.name, node.Name.O)
case *tidbast.RenameTableStmt:
- return d.renameTable(node)
+ return d.tidbRenameTable(node)
default:
return nil
}
}
-func (d *DatabaseState) renameTable(node *tidbast.RenameTableStmt) *WalkThroughError {
+func (d *DatabaseState) tidbRenameTable(node *tidbast.RenameTableStmt) *WalkThroughError {
for _, tableToTable := range node.TableToTables {
schema, exists := d.schemaSet[""]
if !exists {
- schema = d.createSchema()
+ schema = d.CreateSchema("")
}
oldTableName := tableToTable.OldTable.Name.O
newTableName := tableToTable.NewTable.Name.O
@@ -85,18 +87,18 @@ func (d *DatabaseState) renameTable(node *tidbast.RenameTableStmt) *WalkThroughE
if compareIdentifier(oldTableName, newTableName, d.ignoreCaseSensitive) {
return nil
}
- table, exists := schema.getTable(oldTableName)
+ table, exists := mysqlGetTable(schema, oldTableName)
if !exists {
return NewTableNotExistsError(oldTableName)
}
- if _, exists := schema.getTable(newTableName); exists {
+ if _, exists := mysqlGetTable(schema, newTableName); exists {
return NewTableExistsError(newTableName)
}
delete(schema.tableSet, table.name)
table.name = newTableName
schema.tableSet[table.name] = table
} else if d.moveToOtherDatabase(tableToTable) {
- _, exists := schema.getTable(tableToTable.OldTable.Name.O)
+ _, exists := mysqlGetTable(schema, tableToTable.OldTable.Name.O)
if !exists {
return NewTableNotExistsError(tableToTable.OldTable.Name.O)
}
@@ -166,10 +168,10 @@ func (d *DatabaseState) findTableState(tableName *tidbast.TableName) (*TableStat
schema, exists := d.schemaSet[""]
if !exists {
- schema = d.createSchema()
+ schema = d.CreateSchema("")
}
- table, exists := schema.getTable(tableName.Name.O)
+ table, exists := mysqlGetTable(schema, tableName.Name.O)
if !exists {
return nil, NewTableNotExistsError(tableName.Name.O)
}
@@ -186,7 +188,7 @@ func (d *DatabaseState) dropIndex(node *tidbast.DropIndexStmt) *WalkThroughError
return table.DropIndex(node.IndexName, nil)
}
-func (d *DatabaseState) createIndex(node *tidbast.CreateIndexStmt) *WalkThroughError {
+func (d *DatabaseState) tidbHandleCreateIndex(node *tidbast.CreateIndexStmt) *WalkThroughError {
table, err := d.findTableState(node.Table)
if err != nil {
return err
@@ -214,7 +216,7 @@ func (d *DatabaseState) createIndex(node *tidbast.CreateIndexStmt) *WalkThroughE
return err
}
- return table.createIndex(node.IndexName, keyList, unique, tp, node.IndexOption)
+ return table.tidbCreateIndex(node.IndexName, keyList, unique, tp, node.IndexOption)
}
func (d *DatabaseState) alterTable(node *tidbast.AlterTableStmt) *WalkThroughError {
@@ -229,11 +231,11 @@ func (d *DatabaseState) alterTable(node *tidbast.AlterTableStmt) *WalkThroughErr
for _, option := range spec.Options {
switch option.Tp {
case tidbast.TableOptionCollate:
- table.collation = newStringPointer(option.StrValue)
+ table.collation = option.StrValue
case tidbast.TableOptionComment:
- table.comment = newStringPointer(option.StrValue)
+ table.comment = option.StrValue
case tidbast.TableOptionEngine:
- table.engine = newStringPointer(option.StrValue)
+ table.engine = option.StrValue
default:
// Other table options
}
@@ -244,7 +246,7 @@ func (d *DatabaseState) alterTable(node *tidbast.AlterTableStmt) *WalkThroughErr
if len(spec.NewColumns) == 1 {
pos = spec.Position
}
- if err := table.createColumn(column, pos); err != nil {
+ if err := table.tidbCreateColumn(column, pos); err != nil {
return err
}
}
@@ -286,7 +288,7 @@ func (d *DatabaseState) alterTable(node *tidbast.AlterTableStmt) *WalkThroughErr
}
case tidbast.AlterTableRenameTable:
schema := d.schemaSet[""]
- if err := schema.renameTable(table.name, spec.NewTable.Name.O); err != nil {
+ if err := mysqlRenameTable(schema, table.name, spec.NewTable.Name.O); err != nil {
return err
}
case tidbast.AlterTableAlterColumn:
@@ -316,9 +318,9 @@ func (t *TableState) changeIndexVisibility(indexName string, visibility tidbast.
}
switch visibility {
case tidbast.IndexVisibilityVisible:
- index.visible = newTruePointer()
+ index.visible = true
case tidbast.IndexVisibilityInvisible:
- index.visible = newFalsePointer()
+ index.visible = false
default:
// Keep current visibility
}
@@ -335,8 +337,8 @@ func (t *TableState) changeColumnDefault(column *tidbast.ColumnDef) *WalkThrough
if len(column.Options) == 1 {
// SET DEFAULT
if column.Options[0].Expr.GetType().GetType() != mysql.TypeNull {
- if colState.columnType != nil {
- switch strings.ToLower(*colState.columnType) {
+ if colState.columnType != "" {
+ switch strings.ToLower(colState.columnType) {
case "blob", "tinyblob", "mediumblob", "longblob",
"text", "tinytext", "mediumtext", "longtext",
"json",
@@ -358,20 +360,20 @@ func (t *TableState) changeColumnDefault(column *tidbast.ColumnDef) *WalkThrough
Content: fmt.Sprintf("Failed to deparse default value: %v", err),
}
}
- colState.defaultValue = &defaultValue
+ colState.defaultValue = defaultValue
} else {
- if colState.nullable != nil && !*colState.nullable {
+ if !colState.nullable {
return &WalkThroughError{
Code: code.SetNullDefaultForNotNullColumn,
// Content comes from MySQL Error content.
Content: fmt.Sprintf("Invalid default value for column `%s`", columnName),
}
}
- colState.defaultValue = nil
+ colState.defaultValue = ""
}
} else {
// DROP DEFAULT
- colState.defaultValue = nil
+ colState.defaultValue = ""
}
return nil
}
@@ -400,7 +402,7 @@ func (t *TableState) completeTableChangeColumn(oldName string, newColumn *tidbas
return NewColumnNotExistsError(t.name, oldName)
}
- pos := *column.position
+ pos := column.position
// generate Position struct for creating new column
// Create a local copy to avoid modifying the input parameter
@@ -420,7 +422,7 @@ func (t *TableState) completeTableChangeColumn(oldName string, newColumn *tidbas
localPosition.Tp = tidbast.ColumnPositionFirst
} else {
for _, col := range t.columnSet {
- if *col.position == pos-1 {
+ if col.position == pos-1 {
localPosition.Tp = tidbast.ColumnPositionAfter
localPosition.RelativeColumn = &tidbast.ColumnName{Name: tidbast.NewCIStr(col.name)}
break
@@ -432,8 +434,8 @@ func (t *TableState) completeTableChangeColumn(oldName string, newColumn *tidbas
// drop column from columnSet
for _, col := range t.columnSet {
- if *col.position > pos {
- *col.position--
+ if col.position > pos {
+ col.position--
}
}
delete(t.columnSet, strings.ToLower(column.name))
@@ -442,7 +444,7 @@ func (t *TableState) completeTableChangeColumn(oldName string, newColumn *tidbas
t.renameColumnInIndexKey(oldName, newColumn.Name.Name.O)
// create a new column in columnSet
- return t.createColumn(newColumn, position)
+ return t.tidbCreateColumn(newColumn, position)
}
func (t *TableState) changeColumn(oldName string, newColumn *tidbast.ColumnDef, position *tidbast.ColumnPosition) *WalkThroughError {
@@ -456,7 +458,7 @@ func (t *TableState) reorderColumn(position *tidbast.ColumnPosition) (int, *Walk
return len(t.columnSet) + 1, nil
case tidbast.ColumnPositionFirst:
for _, column := range t.columnSet {
- *column.position++
+ column.position++
}
return 1, nil
case tidbast.ColumnPositionAfter:
@@ -466,11 +468,11 @@ func (t *TableState) reorderColumn(position *tidbast.ColumnPosition) (int, *Walk
return 0, NewColumnNotExistsError(t.name, columnName)
}
for _, col := range t.columnSet {
- if *col.position > *column.position {
- *col.position++
+ if col.position > column.position {
+ col.position++
}
}
- return *column.position + 1, nil
+ return column.position + 1, nil
default:
return 0, &WalkThroughError{
Code: code.Unsupported,
@@ -492,10 +494,10 @@ func (d *DatabaseState) dropTable(node *tidbast.DropTableStmt) *WalkThroughError
schema, exists := d.schemaSet[""]
if !exists {
- schema = d.createSchema()
+ schema = d.CreateSchema("")
}
- table, exists := schema.getTable(name.Name.O)
+ table, exists := mysqlGetTable(schema, name.Name.O)
if !exists {
if node.IfExists {
return nil
@@ -540,10 +542,10 @@ func (d *DatabaseState) createTable(node *tidbast.CreateTableStmt) *WalkThroughE
schema, exists := d.schemaSet[""]
if !exists {
- schema = d.createSchema()
+ schema = d.CreateSchema("")
}
- if _, exists = schema.getTable(node.Table.Name.O); exists {
+ if _, exists = mysqlGetTable(schema, node.Table.Name.O); exists {
if node.IfNotExists {
return nil
}
@@ -566,9 +568,9 @@ func (d *DatabaseState) createTable(node *tidbast.CreateTableStmt) *WalkThroughE
table := &TableState{
name: node.Table.Name.O,
- engine: newEmptyStringPointer(),
- collation: newEmptyStringPointer(),
- comment: newEmptyStringPointer(),
+ engine: "",
+ collation: "",
+ comment: "",
columnSet: make(columnStateMap),
indexSet: make(IndexStateMap),
}
@@ -586,7 +588,7 @@ func (d *DatabaseState) createTable(node *tidbast.CreateTableStmt) *WalkThroughE
}
hasAutoIncrement = true
}
- if err := table.createColumn(column, nil /* position */); err != nil {
+ if err := table.tidbCreateColumn(column, nil /* position */); err != nil {
err.Line = column.OriginTextPosition()
return err
}
@@ -609,7 +611,7 @@ func (t *TableState) createConstraint(constraint *tidbast.Constraint) *WalkThrou
if err != nil {
return err
}
- if err := t.createPrimaryKey(keyList, getIndexType(constraint.Option)); err != nil {
+ if err := t.tidbCreatePrimaryKey(keyList, getIndexType(constraint.Option)); err != nil {
return err
}
case tidbast.ConstraintKey, tidbast.ConstraintIndex:
@@ -617,7 +619,7 @@ func (t *TableState) createConstraint(constraint *tidbast.Constraint) *WalkThrou
if err != nil {
return err
}
- if err := t.createIndex(constraint.Name, keyList, false /* unique */, getIndexType(constraint.Option), constraint.Option); err != nil {
+ if err := t.tidbCreateIndex(constraint.Name, keyList, false /* unique */, getIndexType(constraint.Option), constraint.Option); err != nil {
return err
}
case tidbast.ConstraintUniq, tidbast.ConstraintUniqKey, tidbast.ConstraintUniqIndex:
@@ -625,7 +627,7 @@ func (t *TableState) createConstraint(constraint *tidbast.Constraint) *WalkThrou
if err != nil {
return err
}
- if err := t.createIndex(constraint.Name, keyList, true /* unique */, getIndexType(constraint.Option), constraint.Option); err != nil {
+ if err := t.tidbCreateIndex(constraint.Name, keyList, true /* unique */, getIndexType(constraint.Option), constraint.Option); err != nil {
return err
}
case tidbast.ConstraintForeignKey:
@@ -635,7 +637,7 @@ func (t *TableState) createConstraint(constraint *tidbast.Constraint) *WalkThrou
if err != nil {
return err
}
- if err := t.createIndex(constraint.Name, keyList, false /* unique */, FullTextName, constraint.Option); err != nil {
+ if err := t.tidbCreateIndex(constraint.Name, keyList, false /* unique */, FullTextName, constraint.Option); err != nil {
return err
}
case tidbast.ConstraintCheck:
@@ -666,9 +668,9 @@ func (t *TableState) validateAndGetKeyStringList(keyList []*tidbast.IndexPartSpe
return nil, NewColumnNotExistsError(t.name, columnName)
}
if primary {
- column.nullable = newFalsePointer()
+ column.nullable = false
}
- if isSpatial && column.nullable != nil && *column.nullable {
+ if isSpatial && column.nullable {
return nil, &WalkThroughError{
Code: code.SpatialIndexKeyNullable,
// The error content comes from MySQL.
@@ -717,8 +719,7 @@ func checkDefault(columnName string, columnType *types.FieldType, value tidbast.
return nil
}
-//nolint:revive
-func (t *TableState) createColumn(column *tidbast.ColumnDef, position *tidbast.ColumnPosition) *WalkThroughError {
+func (t *TableState) tidbCreateColumn(column *tidbast.ColumnDef, position *tidbast.ColumnPosition) *WalkThroughError {
if _, exists := t.columnSet[column.Name.Name.L]; exists {
return &WalkThroughError{
Code: code.ColumnExists,
@@ -735,28 +736,27 @@ func (t *TableState) createColumn(column *tidbast.ColumnDef, position *tidbast.C
}
}
- vTrue := true
col := &ColumnState{
name: column.Name.Name.L,
- position: &pos,
- defaultValue: nil,
- nullable: &vTrue,
- columnType: newStringPointer(column.Tp.CompactStr()),
- characterSet: newStringPointer(column.Tp.GetCharset()),
- collation: newStringPointer(column.Tp.GetCollate()),
- comment: newEmptyStringPointer(),
+ position: pos,
+ defaultValue: "",
+ nullable: true,
+ columnType: column.Tp.CompactStr(),
+ characterSet: column.Tp.GetCharset(),
+ collation: column.Tp.GetCollate(),
+ comment: "",
}
setNullDefault := false
for _, option := range column.Options {
switch option.Tp {
case tidbast.ColumnOptionPrimaryKey:
- col.nullable = newFalsePointer()
- if err := t.createPrimaryKey([]string{col.name}, tidbast.IndexTypeBtree.String()); err != nil {
+ col.nullable = false
+ if err := t.tidbCreatePrimaryKey([]string{col.name}, tidbast.IndexTypeBtree.String()); err != nil {
return err
}
case tidbast.ColumnOptionNotNull:
- col.nullable = newFalsePointer()
+ col.nullable = false
case tidbast.ColumnOptionAutoIncrement:
// we do not deal with AUTO-INCREMENT
case tidbast.ColumnOptionDefaultValue:
@@ -771,16 +771,16 @@ func (t *TableState) createColumn(column *tidbast.ColumnDef, position *tidbast.C
Content: fmt.Sprintf("Failed to deparse default value: %v", err),
}
}
- col.defaultValue = &defaultValue
+ col.defaultValue = defaultValue
} else {
setNullDefault = true
}
case tidbast.ColumnOptionUniqKey:
- if err := t.createIndex("", []string{col.name}, true /* unique */, tidbast.IndexTypeBtree.String(), nil); err != nil {
+ if err := t.tidbCreateIndex("", []string{col.name}, true /* unique */, tidbast.IndexTypeBtree.String(), nil); err != nil {
return err
}
case tidbast.ColumnOptionNull:
- col.nullable = newTruePointer()
+ col.nullable = true
case tidbast.ColumnOptionOnUpdate:
// we do not deal with ON UPDATE
if column.Tp.GetType() != mysql.TypeDatetime && column.Tp.GetType() != mysql.TypeTimestamp {
@@ -797,14 +797,14 @@ func (t *TableState) createColumn(column *tidbast.ColumnDef, position *tidbast.C
Content: fmt.Sprintf("Failed to deparse comment: %v", err),
}
}
- col.comment = &comment
+ col.comment = comment
case tidbast.ColumnOptionGenerated:
// we do not deal with GENERATED ALWAYS AS
case tidbast.ColumnOptionReference:
// MySQL will ignore the inline REFERENCE
// https://dev.mysql.com/doc/refman/8.0/en/create-table.html
case tidbast.ColumnOptionCollate:
- col.collation = newStringPointer(option.StrValue)
+ col.collation = option.StrValue
case tidbast.ColumnOptionCheck:
// we do not deal with CHECK constraint
case tidbast.ColumnOptionColumnFormat:
@@ -818,7 +818,7 @@ func (t *TableState) createColumn(column *tidbast.ColumnDef, position *tidbast.C
}
}
- if col.nullable != nil && !*col.nullable && setNullDefault {
+ if !col.nullable && setNullDefault {
return &WalkThroughError{
Code: code.SetNullDefaultForNotNullColumn,
// Content comes from MySQL Error content.
@@ -830,8 +830,7 @@ func (t *TableState) createColumn(column *tidbast.ColumnDef, position *tidbast.C
return nil
}
-//nolint:revive
-func (t *TableState) createIndex(name string, keyList []string, unique bool, tp string, option *tidbast.IndexOption) *WalkThroughError {
+func (t *TableState) tidbCreateIndex(name string, keyList []string, unique bool, tp string, option *tidbast.IndexOption) *WalkThroughError {
if len(keyList) == 0 {
return &WalkThroughError{
Code: code.IndexEmptyKeys,
@@ -856,26 +855,23 @@ func (t *TableState) createIndex(name string, keyList []string, unique bool, tp
}
}
+ visible := option == nil || option.Visibility != tidbast.IndexVisibilityInvisible
+
index := &IndexState{
name: name,
expressionList: keyList,
- indexType: &tp,
- unique: &unique,
- primary: newFalsePointer(),
- visible: newTruePointer(),
- comment: newEmptyStringPointer(),
- }
-
- if option != nil && option.Visibility == tidbast.IndexVisibilityInvisible {
- index.visible = newFalsePointer()
+ indexType: tp,
+ unique: unique,
+ primary: false,
+ visible: visible,
+ comment: "",
}
t.indexSet[strings.ToLower(name)] = index
return nil
}
-//nolint:revive
-func (t *TableState) createPrimaryKey(keys []string, tp string) *WalkThroughError {
+func (t *TableState) tidbCreatePrimaryKey(keys []string, tp string) *WalkThroughError {
if _, exists := t.indexSet[strings.ToLower(PrimaryKeyName)]; exists {
return &WalkThroughError{
Code: code.PrimaryKeyExists,
@@ -886,11 +882,11 @@ func (t *TableState) createPrimaryKey(keys []string, tp string) *WalkThroughErro
pk := &IndexState{
name: PrimaryKeyName,
expressionList: keys,
- indexType: &tp,
- unique: newTruePointer(),
- primary: newTruePointer(),
- visible: newTruePointer(),
- comment: newEmptyStringPointer(),
+ indexType: tp,
+ unique: true,
+ primary: true,
+ visible: true,
+ comment: "",
}
t.indexSet[strings.ToLower(pk.name)] = pk
return nil
diff --git a/backend/plugin/advisor/catalog/walk_through_test.go b/backend/plugin/advisor/catalog/walk_through_test.go
index 03be480b68..b01a45fc50 100644
--- a/backend/plugin/advisor/catalog/walk_through_test.go
+++ b/backend/plugin/advisor/catalog/walk_through_test.go
@@ -303,17 +303,9 @@ func (s *SchemaState) convertToTableMetadataList() []*storepb.TableMetadata {
ForeignKeys: []*storepb.ForeignKeyMetadata{},
}
- if table.engine != nil {
- tableMeta.Engine = *table.engine
- }
-
- if table.collation != nil {
- tableMeta.Collation = *table.collation
- }
-
- if table.comment != nil {
- tableMeta.Comment = *table.comment
- }
+ tableMeta.Engine = table.engine
+ tableMeta.Collation = table.collation
+ tableMeta.Comment = table.comment
result = append(result, tableMeta)
}
@@ -342,17 +334,9 @@ func (t *TableState) convertToIndexMetadataList() []*storepb.IndexMetadata {
Primary: index.Primary(),
}
- if index.indexType != nil {
- indexMeta.Type = *index.indexType
- }
-
- if index.visible != nil {
- indexMeta.Visible = *index.visible
- }
-
- if index.comment != nil {
- indexMeta.Comment = *index.comment
- }
+ indexMeta.Type = index.indexType
+ indexMeta.Visible = index.visible
+ indexMeta.Comment = index.comment
result = append(result, indexMeta)
}
@@ -380,25 +364,11 @@ func (t *TableState) convertToColumnMetadataList() []*storepb.ColumnMetadata {
Type: column.Type(),
}
- if column.defaultValue != nil {
- columnMeta.Default = *column.defaultValue
- }
-
- if column.characterSet != nil {
- columnMeta.CharacterSet = *column.characterSet
- }
-
- if column.collation != nil {
- columnMeta.Collation = *column.collation
- }
-
- if column.comment != nil {
- columnMeta.Comment = *column.comment
- }
-
- if column.position != nil {
- columnMeta.Position = int32(*column.position)
- }
+ columnMeta.Default = column.defaultValue
+ columnMeta.CharacterSet = column.characterSet
+ columnMeta.Collation = column.collation
+ columnMeta.Comment = column.comment
+ columnMeta.Position = int32(column.position)
result = append(result, columnMeta)
}