Skip to content

Commit

Permalink
Merge pull request vitessio#7096 from GuptaManan100/convert-ddl-to-in…
Browse files Browse the repository at this point in the history
…terfaces

Convert usages of DDL struct to DDLStatement interface
  • Loading branch information
systay authored Dec 3, 2020
2 parents 6454b7d + 2eb5d64 commit b38a190
Show file tree
Hide file tree
Showing 15 changed files with 138 additions and 65 deletions.
22 changes: 11 additions & 11 deletions go/vt/schemamanager/tablet_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,16 @@ func (exec *TabletExecutor) Validate(ctx context.Context, sqls []string) error {
return err
}

func (exec *TabletExecutor) parseDDLs(sqls []string) ([]*sqlparser.DDL, []sqlparser.DBDDLStatement, error) {
parsedDDLs := make([]*sqlparser.DDL, 0)
func (exec *TabletExecutor) parseDDLs(sqls []string) ([]sqlparser.DDLStatement, []sqlparser.DBDDLStatement, error) {
parsedDDLs := make([]sqlparser.DDLStatement, 0)
parsedDBDDLs := make([]sqlparser.DBDDLStatement, 0)
for _, sql := range sqls {
stat, err := sqlparser.Parse(sql)
if err != nil {
return nil, nil, fmt.Errorf("failed to parse sql: %s, got error: %v", sql, err)
}
switch ddl := stat.(type) {
case *sqlparser.DDL:
case sqlparser.DDLStatement:
parsedDDLs = append(parsedDDLs, ddl)
case sqlparser.DBDDLStatement:
parsedDBDDLs = append(parsedDBDDLs, ddl)
Expand All @@ -151,11 +151,11 @@ func (exec *TabletExecutor) parseDDLs(sqls []string) ([]*sqlparser.DDL, []sqlpar
}

// IsOnlineSchemaDDL returns true if the query is an online schema change DDL
func (exec *TabletExecutor) isOnlineSchemaDDL(ddl *sqlparser.DDL) (isOnline bool, strategy schema.DDLStrategy, options string) {
func (exec *TabletExecutor) isOnlineSchemaDDL(ddl sqlparser.DDLStatement) (isOnline bool, strategy schema.DDLStrategy, options string) {
if ddl == nil {
return false, strategy, options
}
if ddl.Action != sqlparser.AlterDDLAction {
if ddl.GetAction() != sqlparser.AlterDDLAction {
return false, strategy, options
}
strategy, options, _ = schema.ParseDDLStrategy(exec.ddlStrategy)
Expand All @@ -169,7 +169,7 @@ func (exec *TabletExecutor) isOnlineSchemaDDL(ddl *sqlparser.DDL) (isOnline bool
// to be a big schema change and will be rejected.
// 1. Alter more than 100,000 rows.
// 2. Change a table with more than 2,000,000 rows (Drops are fine).
func (exec *TabletExecutor) detectBigSchemaChanges(ctx context.Context, parsedDDLs []*sqlparser.DDL) (bool, error) {
func (exec *TabletExecutor) detectBigSchemaChanges(ctx context.Context, parsedDDLs []sqlparser.DDLStatement) (bool, error) {
// exec.tablets is guaranteed to have at least one element;
// Otherwise, Open should fail and executor should fail.
masterTabletInfo := exec.tablets[0]
Expand All @@ -188,13 +188,13 @@ func (exec *TabletExecutor) detectBigSchemaChanges(ctx context.Context, parsedDD
// Since this is an online schema change, there is no need to worry about big changes
continue
}
switch ddl.Action {
switch ddl.GetAction() {
case sqlparser.DropDDLAction, sqlparser.CreateDDLAction, sqlparser.TruncateDDLAction, sqlparser.RenameDDLAction:
continue
}
tableName := ddl.Table.Name.String()
tableName := ddl.GetTable().Name.String()
if rowCount, ok := tableWithCount[tableName]; ok {
if rowCount > 100000 && ddl.Action == sqlparser.AlterDDLAction {
if rowCount > 100000 && ddl.GetAction() == sqlparser.AlterDDLAction {
return true, fmt.Errorf(
"big schema change detected. Disable check with -allow_long_unavailability. ddl: %s alters a table with more than 100 thousand rows", sqlparser.String(ddl))
}
Expand Down Expand Up @@ -257,8 +257,8 @@ func (exec *TabletExecutor) Execute(ctx context.Context, sqls []string) *Execute
isOnlineDDL, strategy, options := exec.isOnlineSchemaDDL(nil)
tableName := ""
switch ddl := stat.(type) {
case *sqlparser.DDL:
tableName = ddl.Table.Name.String()
case sqlparser.DDLStatement:
tableName = ddl.GetTable().Name.String()
isOnlineDDL, strategy, options = exec.isOnlineSchemaDDL(ddl)
}
exec.wr.Logger().Infof("Received DDL request. strategy=%+v", strategy)
Expand Down
2 changes: 1 addition & 1 deletion go/vt/schemamanager/tablet_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func TestIsOnlineSchemaDDL(t *testing.T) {
stmt, err := sqlparser.Parse(ts.query)
assert.NoError(t, err)

ddl, ok := stmt.(*sqlparser.DDL)
ddl, ok := stmt.(sqlparser.DDLStatement)
assert.True(t, ok)

isOnlineDDL, strategy, options := e.isOnlineSchemaDDL(ddl)
Expand Down
77 changes: 77 additions & 0 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ type (
IsFullyParsed() bool
GetTable() TableName
GetAction() DDLAction
GetOptLike() *OptLike
GetTableSpec() *TableSpec
GetVindexSpec() *VindexSpec
GetFromTables() TableNames
GetToTables() TableNames
GetAutoIncSpec() *AutoIncSpec
GetVindexCols() []ColIdent
AffectedTables() TableNames
SetTable(qualifier string, name string)
Statement
Expand Down Expand Up @@ -422,6 +429,76 @@ func (node *CreateIndex) GetAction() DDLAction {
return AlterDDLAction
}

// GetOptLike implements the DDLStatement interface
func (node *DDL) GetOptLike() *OptLike {
return node.OptLike
}

// GetOptLike implements the DDLStatement interface
func (node *CreateIndex) GetOptLike() *OptLike {
return nil
}

// GetTableSpec implements the DDLStatement interface
func (node *DDL) GetTableSpec() *TableSpec {
return node.TableSpec
}

// GetTableSpec implements the DDLStatement interface
func (node *CreateIndex) GetTableSpec() *TableSpec {
return nil
}

// GetVindexSpec implements the DDLStatement interface
func (node *DDL) GetVindexSpec() *VindexSpec {
return node.VindexSpec
}

// GetVindexSpec implements the DDLStatement interface
func (node *CreateIndex) GetVindexSpec() *VindexSpec {
return nil
}

// GetFromTables implements the DDLStatement interface
func (node *DDL) GetFromTables() TableNames {
return node.FromTables
}

// GetFromTables implements the DDLStatement interface
func (node *CreateIndex) GetFromTables() TableNames {
return nil
}

// GetToTables implements the DDLStatement interface
func (node *DDL) GetToTables() TableNames {
return node.ToTables
}

// GetToTables implements the DDLStatement interface
func (node *CreateIndex) GetToTables() TableNames {
return nil
}

// GetAutoIncSpec implements the DDLStatement interface
func (node *DDL) GetAutoIncSpec() *AutoIncSpec {
return node.AutoIncSpec
}

// GetAutoIncSpec implements the DDLStatement interface
func (node *CreateIndex) GetAutoIncSpec() *AutoIncSpec {
return nil
}

// GetVindexCols implements the DDLStatement interface
func (node *DDL) GetVindexCols() []ColIdent {
return node.VindexCols
}

// GetVindexCols implements the DDLStatement interface
func (node *CreateIndex) GetVindexCols() []ColIdent {
return nil
}

// GetAction implements the DDLStatement interface
func (node *DDL) GetAction() DDLAction {
return node.Action
Expand Down
4 changes: 2 additions & 2 deletions go/vt/sqlparser/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func TestSetLimit(t *testing.T) {
func TestDDL(t *testing.T) {
testcases := []struct {
query string
output *DDL
output DDLStatement
affected []string
}{{
query: "create table a",
Expand Down Expand Up @@ -244,7 +244,7 @@ func TestDDL(t *testing.T) {
for _, t := range tcase.affected {
want = append(want, TableName{Name: NewTableIdent(t)})
}
if affected := got.(*DDL).AffectedTables(); !reflect.DeepEqual(affected, want) {
if affected := got.(DDLStatement).AffectedTables(); !reflect.DeepEqual(affected, want) {
t.Errorf("Affected(%s): %v, want %v", tcase.query, affected, want)
}
}
Expand Down
2 changes: 1 addition & 1 deletion go/vt/sqlparser/normalizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func newNormalizer(stmt Statement, bindVars map[string]*querypb.BindVariable, pr
func (nz *normalizer) WalkStatement(cursor *Cursor) bool {
switch node := cursor.Node().(type) {
// no need to normalize the statement types
case *Set, *Show, *Begin, *Commit, *Rollback, *Savepoint, *SetTransaction, *DDL, *SRollback, *Release, *OtherAdmin, *OtherRead:
case *Set, *Show, *Begin, *Commit, *Rollback, *Savepoint, *SetTransaction, DDLStatement, *SRollback, *Release, *OtherAdmin, *OtherRead:
return false
case *Select:
Rewrite(node, nz.WalkSelect, nil)
Expand Down
38 changes: 19 additions & 19 deletions go/vt/topotools/vschema_ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (

// ApplyVSchemaDDL applies the given DDL statement to the vschema
// keyspace definition and returns the modified keyspace object.
func ApplyVSchemaDDL(ksName string, ks *vschemapb.Keyspace, ddl *sqlparser.DDL) (*vschemapb.Keyspace, error) {
func ApplyVSchemaDDL(ksName string, ks *vschemapb.Keyspace, ddl sqlparser.DDLStatement) (*vschemapb.Keyspace, error) {
if ks == nil {
ks = new(vschemapb.Keyspace)
}
Expand All @@ -44,14 +44,14 @@ func ApplyVSchemaDDL(ksName string, ks *vschemapb.Keyspace, ddl *sqlparser.DDL)

var tableName string
var table *vschemapb.Table
if !ddl.Table.IsEmpty() {
tableName = ddl.Table.Name.String()
if !ddl.GetTable().IsEmpty() {
tableName = ddl.GetTable().Name.String()
table = ks.Tables[tableName]
}

switch ddl.Action {
switch ddl.GetAction() {
case sqlparser.CreateVindexDDLAction:
name := ddl.VindexSpec.Name.String()
name := ddl.GetVindexSpec().Name.String()
if _, ok := ks.Vindexes[name]; ok {
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "vindex %s already exists in keyspace %s", name, ksName)
}
Expand All @@ -62,17 +62,17 @@ func ApplyVSchemaDDL(ksName string, ks *vschemapb.Keyspace, ddl *sqlparser.DDL)
ks.Sharded = true
}

owner, params := ddl.VindexSpec.ParseParams()
owner, params := ddl.GetVindexSpec().ParseParams()
ks.Vindexes[name] = &vschemapb.Vindex{
Type: ddl.VindexSpec.Type.String(),
Type: ddl.GetVindexSpec().Type.String(),
Params: params,
Owner: owner,
}

return ks, nil

case sqlparser.DropVindexDDLAction:
name := ddl.VindexSpec.Name.String()
name := ddl.GetVindexSpec().Name.String()
if _, ok := ks.Vindexes[name]; !ok {
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "vindex %s does not exists in keyspace %s", name, ksName)
}
Expand All @@ -95,7 +95,7 @@ func ApplyVSchemaDDL(ksName string, ks *vschemapb.Keyspace, ddl *sqlparser.DDL)
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "add vschema table: unsupported on sharded keyspace %s", ksName)
}

name := ddl.Table.Name.String()
name := ddl.GetTable().Name.String()
if _, ok := ks.Tables[name]; ok {
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "vschema already contains table %s in keyspace %s", name, ksName)
}
Expand All @@ -105,7 +105,7 @@ func ApplyVSchemaDDL(ksName string, ks *vschemapb.Keyspace, ddl *sqlparser.DDL)
return ks, nil

case sqlparser.DropVschemaTableDDLAction:
name := ddl.Table.Name.String()
name := ddl.GetTable().Name.String()
if _, ok := ks.Tables[name]; !ok {
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "vschema does not contain table %s in keyspace %s", name, ksName)
}
Expand All @@ -123,7 +123,7 @@ func ApplyVSchemaDDL(ksName string, ks *vschemapb.Keyspace, ddl *sqlparser.DDL)
//
// 2. The vindex type is not specified. Make sure the vindex
// already exists.
spec := ddl.VindexSpec
spec := ddl.GetVindexSpec()
name := spec.Name.String()
if !spec.Type.IsEmpty() {
owner, params := spec.ParseParams()
Expand Down Expand Up @@ -171,8 +171,8 @@ func ApplyVSchemaDDL(ksName string, ks *vschemapb.Keyspace, ddl *sqlparser.DDL)
}
}

columns := make([]string, len(ddl.VindexCols))
for i, col := range ddl.VindexCols {
columns := make([]string, len(ddl.GetVindexCols()))
for i, col := range ddl.GetVindexCols() {
columns[i] = col.String()
}
table.ColumnVindexes = append(table.ColumnVindexes, &vschemapb.ColumnVindex{
Expand All @@ -184,7 +184,7 @@ func ApplyVSchemaDDL(ksName string, ks *vschemapb.Keyspace, ddl *sqlparser.DDL)
return ks, nil

case sqlparser.DropColVindexDDLAction:
spec := ddl.VindexSpec
spec := ddl.GetVindexSpec()
name := spec.Name.String()
if table == nil {
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "table %s.%s not defined in vschema", ksName, tableName)
Expand All @@ -206,7 +206,7 @@ func ApplyVSchemaDDL(ksName string, ks *vschemapb.Keyspace, ddl *sqlparser.DDL)
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "add sequence table: unsupported on sharded keyspace %s", ksName)
}

name := ddl.Table.Name.String()
name := ddl.GetTable().Name.String()
if _, ok := ks.Tables[name]; ok {
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "vschema already contains sequence %s in keyspace %s", name, ksName)
}
Expand All @@ -216,7 +216,7 @@ func ApplyVSchemaDDL(ksName string, ks *vschemapb.Keyspace, ddl *sqlparser.DDL)
return ks, nil

case sqlparser.AddAutoIncDDLAction:
name := ddl.Table.Name.String()
name := ddl.GetTable().Name.String()
table := ks.Tables[name]
if table == nil {
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "vschema does not contain table %s in keyspace %s", name, ksName)
Expand All @@ -226,19 +226,19 @@ func ApplyVSchemaDDL(ksName string, ks *vschemapb.Keyspace, ddl *sqlparser.DDL)
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "vschema already contains auto inc %v on table %s in keyspace %s", table.AutoIncrement, name, ksName)
}

sequence := ddl.AutoIncSpec.Sequence
sequence := ddl.GetAutoIncSpec().Sequence
sequenceFqn := sequence.Name.String()
if sequence.Qualifier.String() != "" {
sequenceFqn = fmt.Sprintf("%s.%s", sequence.Qualifier.String(), sequenceFqn)
}

table.AutoIncrement = &vschemapb.AutoIncrement{
Column: ddl.AutoIncSpec.Column.String(),
Column: ddl.GetAutoIncSpec().Column.String(),
Sequence: sequenceFqn,
}

return ks, nil
}

return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "unexpected vindex ddl operation %s", ddl.Action.ToString())
return nil, vterrors.Errorf(vtrpcpb.Code_INTERNAL, "unexpected vindex ddl operation %s", ddl.GetAction().ToString())
}
2 changes: 1 addition & 1 deletion go/vt/vtctl/vtctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -2767,7 +2767,7 @@ func commandApplyVSchema(ctx context.Context, wr *wrangler.Wrangler, subFlags *f
if err != nil {
return fmt.Errorf("error parsing vschema statement `%s`: %v", *sql, err)
}
ddl, ok := stmt.(*sqlparser.DDL)
ddl, ok := stmt.(sqlparser.DDLStatement)
if !ok {
return fmt.Errorf("error parsing vschema statement `%s`: not a ddl statement", *sql)
}
Expand Down
12 changes: 6 additions & 6 deletions go/vt/vtexplain/vtexplain.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ func Stop() {
}
}

func parseSchema(sqlSchema string, opts *Options) ([]*sqlparser.DDL, error) {
parsedDDLs := make([]*sqlparser.DDL, 0, 16)
func parseSchema(sqlSchema string, opts *Options) ([]sqlparser.DDLStatement, error) {
parsedDDLs := make([]sqlparser.DDLStatement, 0, 16)
for {
sql, rem, err := sqlparser.SplitStatement(sqlSchema)
sqlSchema = rem
Expand Down Expand Up @@ -210,16 +210,16 @@ func parseSchema(sqlSchema string, opts *Options) ([]*sqlparser.DDL, error) {
continue
}
}
ddl, ok := stmt.(*sqlparser.DDL)
ddl, ok := stmt.(sqlparser.DDLStatement)
if !ok {
log.Infof("ignoring non-DDL statement: %s", sql)
continue
}
if ddl.Action != sqlparser.CreateDDLAction {
log.Infof("ignoring %s table statement", ddl.Action.ToString())
if ddl.GetAction() != sqlparser.CreateDDLAction {
log.Infof("ignoring %s table statement", ddl.GetAction().ToString())
continue
}
if ddl.TableSpec == nil && ddl.OptLike == nil {
if ddl.GetTableSpec() == nil && ddl.GetOptLike() == nil {
log.Errorf("invalid create table statement: %s", sql)
continue
}
Expand Down
Loading

0 comments on commit b38a190

Please sign in to comment.