Skip to content

Commit

Permalink
sqlparser: normalize IndexInfo
Browse files Browse the repository at this point in the history
Signed-off-by: Vicent Marti <[email protected]>
  • Loading branch information
vmg committed Oct 4, 2023
1 parent 456296d commit a853bed
Show file tree
Hide file tree
Showing 15 changed files with 178 additions and 4,669 deletions.
2 changes: 1 addition & 1 deletion go/test/endtoend/vreplication/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ func confirmTablesHaveSecondaryKeys(t *testing.T, tablets []*cluster.VttabletPro
require.NotNil(t, createTable)
require.NotNil(t, createTable.GetTableSpec())
for _, index := range createTable.GetTableSpec().Indexes {
if !index.Info.Primary {
if index.Info.Type != sqlparser.IndexTypePrimary {
secondaryKeys++
}
}
Expand Down
24 changes: 7 additions & 17 deletions go/vt/schemadiff/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,9 +583,6 @@ func (c *CreateTableEntity) normalizeColumnOptions() {

func (c *CreateTableEntity) normalizeIndexOptions() {
for _, idx := range c.CreateTable.TableSpec.Indexes {
// This name is taking straight from the input string
// so we want to normalize this to always lowercase.
idx.Info.Type = strings.ToLower(idx.Info.Type)
for _, opt := range idx.Options {
opt.Name = strings.ToLower(opt.Name)
opt.String = strings.ToLower(opt.String)
Expand Down Expand Up @@ -617,10 +614,8 @@ func (c *CreateTableEntity) normalizePartitionOptions() {
func newPrimaryKeyIndexDefinitionSingleColumn(name sqlparser.IdentifierCI) *sqlparser.IndexDefinition {
index := &sqlparser.IndexDefinition{
Info: &sqlparser.IndexInfo{
Name: sqlparser.NewIdentifierCI("PRIMARY"),
Type: "PRIMARY KEY",
Primary: true,
Unique: true,
Name: sqlparser.NewIdentifierCI("PRIMARY"),
Type: sqlparser.IndexTypePrimary,
},
Columns: []*sqlparser.IndexColumn{{Column: name}},
}
Expand Down Expand Up @@ -653,10 +648,6 @@ func (c *CreateTableEntity) normalizeKeys() {
}
}
for _, key := range c.CreateTable.TableSpec.Indexes {
// Normalize to KEY which matches MySQL behavior for the type.
if key.Info.Type == sqlparser.KeywordString(sqlparser.INDEX) {
key.Info.Type = sqlparser.KeywordString(sqlparser.KEY)
}
// now, let's look at keys that do not have names, and assign them new names
if name := key.Info.Name.String(); name == "" {
// we know there must be at least one column covered by this key
Expand Down Expand Up @@ -747,7 +738,6 @@ func (c *CreateTableEntity) normalizeForeignKeyIndexes() {
// - or, a standard auto-generated index name, if the constraint name is not provided
indexDefinition := &sqlparser.IndexDefinition{
Info: &sqlparser.IndexInfo{
Type: "key",
Name: constraint.Name, // if name is empty, then the name is later auto populated
},
}
Expand Down Expand Up @@ -1366,7 +1356,7 @@ func (c *CreateTableEntity) diffKeys(alterTable *sqlparser.AlterTable,

dropKeyStatement := func(info *sqlparser.IndexInfo) *sqlparser.DropKey {
dropKey := &sqlparser.DropKey{}
if strings.EqualFold(info.Type, sqlparser.PrimaryKeyTypeStr) {
if info.Type == sqlparser.IndexTypePrimary {
dropKey.Type = sqlparser.PrimaryKeyType
} else {
dropKey.Type = sqlparser.NormalKeyType
Expand Down Expand Up @@ -1417,7 +1407,7 @@ func (c *CreateTableEntity) diffKeys(alterTable *sqlparser.AlterTable,
IndexDefinition: t2Key,
}
addedAsSuperfluousStatement := false
if t2Key.Info.Fulltext {
if t2Key.Info.Type == sqlparser.IndexTypeFullText {
if addedFulltextKeys > 0 && hints.FullTextKeyStrategy == FullTextKeyDistinctStatements {
// Special case: MySQL does not support multiple ADD FULLTEXT KEY statements in a single ALTER
superfluousFulltextKeys = append(superfluousFulltextKeys, addKey)
Expand Down Expand Up @@ -1703,7 +1693,7 @@ func heuristicallyDetectColumnRenames(
// a PRIMARY KEY
func (c *CreateTableEntity) primaryKeyColumns() []*sqlparser.IndexColumn {
for _, existingIndex := range c.CreateTable.TableSpec.Indexes {
if existingIndex.Info.Primary {
if existingIndex.Info.Type == sqlparser.IndexTypePrimary {
return existingIndex.Columns
}
}
Expand Down Expand Up @@ -1874,7 +1864,7 @@ func (c *CreateTableEntity) apply(diff *AlterTableEntityDiff) error {
switch opt.Type {
case sqlparser.PrimaryKeyType:
for i, idx := range c.TableSpec.Indexes {
if strings.EqualFold(idx.Info.Type, sqlparser.PrimaryKeyTypeStr) {
if idx.Info.Type == sqlparser.IndexTypePrimary {
found = true
c.TableSpec.Indexes = append(c.TableSpec.Indexes[0:i], c.TableSpec.Indexes[i+1:]...)
break
Expand Down Expand Up @@ -2435,7 +2425,7 @@ func (c *CreateTableEntity) validate() error {

// Validate all unique keys include this column:
for _, key := range c.CreateTable.TableSpec.Indexes {
if !key.Info.Unique {
if !key.Info.IsUnique() {
continue
}
colFound := false
Expand Down
18 changes: 13 additions & 5 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,9 @@ type (
Type KillType
ProcesslistID uint64
}

// IndexType is the type of index in a DDL statement
IndexType int8
)

func (*Union) iStatement() {}
Expand Down Expand Up @@ -1885,13 +1888,18 @@ type IndexDefinition struct {

// IndexInfo describes the name and type of an index in a CREATE TABLE statement
type IndexInfo struct {
Type string
Type IndexType
Name IdentifierCI
ConstraintName IdentifierCI
Primary bool
Spatial bool
Fulltext bool
Unique bool
}

func (ii *IndexInfo) IsUnique() bool {
switch ii.Type {
case IndexTypePrimary, IndexTypeUnique:
return true
default:
return false
}
}

// VindexSpec defines a vindex for a CREATE VINDEX or DROP VINDEX statement
Expand Down
4 changes: 0 additions & 4 deletions go/vt/sqlparser/ast_equals.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 15 additions & 7 deletions go/vt/sqlparser/ast_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -831,13 +831,21 @@ func (ii *IndexInfo) Format(buf *TrackedBuffer) {
if !ii.ConstraintName.IsEmpty() {
buf.astPrintf(ii, "constraint %v ", ii.ConstraintName)
}
if ii.Primary {
buf.astPrintf(ii, "%s", ii.Type)
} else {
buf.astPrintf(ii, "%s", ii.Type)
if !ii.Name.IsEmpty() {
buf.astPrintf(ii, " %v", ii.Name)
}
switch ii.Type {
case IndexTypePrimary:
buf.astPrintf(ii, "%s %s", keywordStrings[PRIMARY], keywordStrings[KEY])
return
case IndexTypeDefault:
buf.astPrintf(ii, "%s", keywordStrings[INDEX])
case IndexTypeUnique:
buf.astPrintf(ii, "%s %s", keywordStrings[UNIQUE], keywordStrings[INDEX])
case IndexTypeSpatial:
buf.astPrintf(ii, "%s %s", keywordStrings[SPATIAL], keywordStrings[INDEX])
case IndexTypeFullText:
buf.astPrintf(ii, "%s %s", keywordStrings[FULLTEXT], keywordStrings[INDEX])
}
if !ii.Name.IsEmpty() {
buf.astPrintf(ii, " %v", ii.Name)
}
}

Expand Down
32 changes: 24 additions & 8 deletions go/vt/sqlparser/ast_format_fast.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a853bed

Please sign in to comment.