Skip to content

Commit

Permalink
refactor executeSQL() for single query execution
Browse files Browse the repository at this point in the history
Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com>
  • Loading branch information
shlomi-noach committed Dec 13, 2020
1 parent fbf2b60 commit 775bbcf
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions go/vt/schemamanager/tablet_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,31 @@ func (exec *TabletExecutor) preflightSchemaChanges(ctx context.Context, sqls []s
return err
}

// executeSQL executes a single SQL statement either as online DDL or synchronously on all tablets.
// In online DDL case, the query may be exploded into multiple queries during
func (exec *TabletExecutor) executeSQL(ctx context.Context, sql string, execResult *ExecuteResult) error {
stat, err := sqlparser.Parse(sql)
if err != nil {
return err
}
switch ddl := stat.(type) {
case sqlparser.DDLStatement:
if isOnlineDDL, strategy, options := exec.isOnlineSchemaDDL(ddl); isOnlineDDL {
exec.wr.Logger().Infof("Received online DDL request. strategy=%+v", strategy)
normalizedQueries, err := schema.NormalizeOnlineDDL(sql)
if err != nil {
return err
}
for _, normalized := range normalizedQueries {
exec.executeOnlineDDL(ctx, execResult, normalized.SQL, normalized.TableName.Name.String(), strategy, options)
}
return nil
}
}
exec.executeOnAllTablets(ctx, execResult, sql)
return nil
}

// Execute applies schema changes
func (exec *TabletExecutor) Execute(ctx context.Context, sqls []string) *ExecuteResult {
execResult := ExecuteResult{}
Expand Down Expand Up @@ -247,30 +272,11 @@ func (exec *TabletExecutor) Execute(ctx context.Context, sqls []string) *Execute

for index, sql := range sqls {
execResult.CurSQLIndex = index

stat, err := sqlparser.Parse(sql)
if err != nil {
if err := exec.executeSQL(ctx, sql, &execResult); err != nil {
execResult.ExecutorErr = err.Error()
return &execResult
}
isOnlineDDL, strategy, options := exec.isOnlineSchemaDDL(nil)
switch ddl := stat.(type) {
case sqlparser.DDLStatement:
isOnlineDDL, strategy, options = exec.isOnlineSchemaDDL(ddl)
}
exec.wr.Logger().Infof("Received DDL request. strategy=%+v", strategy)
if isOnlineDDL {
normalizedQueries, err := schema.NormalizeOnlineDDL(sql)
if err != nil {
execResult.ExecutorErr = err.Error()
return &execResult
}
for _, normalized := range normalizedQueries {
exec.executeOnlineDDL(ctx, &execResult, normalized.SQL, normalized.TableName.Name.String(), strategy, options)
}
} else {
exec.executeOnAllTablets(ctx, &execResult, sql)
}

if len(execResult.FailedShards) > 0 {
break
}
Expand Down

0 comments on commit 775bbcf

Please sign in to comment.