Skip to content

Commit

Permalink
refactor the job args for add column ddl.
Browse files Browse the repository at this point in the history
Signed-off-by: joccau <[email protected]>
  • Loading branch information
joccau committed Sep 26, 2024
1 parent 8337779 commit 272174c
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 10 deletions.
8 changes: 3 additions & 5 deletions pkg/ddl/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,13 @@ func checkAddColumn(t *meta.Meta, job *model.Job) (*model.TableInfo, *model.Colu
if err != nil {
return nil, nil, nil, nil, false, errors.Trace(err)
}
col := &model.ColumnInfo{}
pos := &ast.ColumnPosition{}
offset := 0
ifNotExists := false
err = job.DecodeArgs(col, pos, &offset, &ifNotExists)

args, err := model.GetAddColumnArgs(job)
if err != nil {
job.State = model.JobStateCancelled
return nil, nil, nil, nil, false, errors.Trace(err)
}
col, pos, ifNotExists := args.Col, args.Pos, args.IfNotExists

columnInfo := model.FindColumnInfo(tblInfo.Columns, col.Name.L)
if columnInfo != nil {
Expand Down
12 changes: 9 additions & 3 deletions pkg/ddl/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2261,18 +2261,24 @@ func (e *executor) AddColumn(ctx sessionctx.Context, ti ast.Ident, spec *ast.Alt
}

job := &model.Job{
Version: model.JobVersion1,
SchemaID: schema.ID,
TableID: tbInfo.ID,
SchemaName: schema.Name.L,
TableName: tbInfo.Name.L,
Type: model.ActionAddColumn,
BinlogInfo: &model.HistoryInfo{},
Args: []any{col, spec.Position, 0, spec.IfNotExists},
CDCWriteSource: ctx.GetSessionVars().CDCWriteSource,
SQLMode: ctx.GetSessionVars().SQLMode,
}

err = e.DoDDLJob(ctx, job)
args := &model.AddColumnArgs{
Col: col.ColumnInfo,
Pos: spec.Position,
Offset: 0,
IfNotExists: spec.IfNotExists,
}
job.FillArgs(args)
err = e.doDDLJob2(ctx, job, args)
return errors.Trace(err)
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/ddl/multi_schema_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ func appendToSubJobs(m *model.MultiSchemaInfo, jobW *JobWrapper) error {
func fillMultiSchemaInfo(info *model.MultiSchemaInfo, job *JobWrapper) error {
switch job.Type {
case model.ActionAddColumn:
col := job.Args[0].(*table.Column)
pos := job.Args[1].(*ast.ColumnPosition)
args := job.JobArgs.(*model.AddColumnArgs)
col, pos := args.Col, args.Pos
info.AddColumns = append(info.AddColumns, col.Name)
for colName := range col.Dependences {
info.RelativeColumns = append(info.RelativeColumns, pmodel.CIStr{L: colName, O: colName})
Expand Down
38 changes: 38 additions & 0 deletions pkg/meta/model/job_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -1121,3 +1121,41 @@ func GetSetDefaultValueArgs(job *Job) (*SetDefaultValueArgs, error) {

return getOrDecodeArgsV2[*SetDefaultValueArgs](job)
}

// AddColumnArgs is the arguments for adding column ddl.
type AddColumnArgs struct {
Col *ColumnInfo
Pos *ast.ColumnPosition
Offset int
IfNotExists bool
}

func (a *AddColumnArgs) fillJob(job *Job) {
if job.Version == JobVersion1 {
job.Args = []any{a.Col, a.Pos, a.Offset, a.IfNotExists}
} else {
job.Args = []any{a}
}
}

func GetAddColumnArgs(job *Job) (*AddColumnArgs, error) {
if job.Version == JobVersion1 {
var (
col = &ColumnInfo{}
pos = &ast.ColumnPosition{}
offset = 0
ifNotExists = false
)
if err := job.DecodeArgs(col, pos, &offset, &ifNotExists); err != nil {
return nil, errors.Trace(err)
}
return &AddColumnArgs{
Col: col,
Pos: pos,
Offset: offset,
IfNotExists: ifNotExists,
}, nil
}

return getOrDecodeArgsV2[*AddColumnArgs](job)
}
21 changes: 21 additions & 0 deletions pkg/meta/model/job_args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,3 +657,24 @@ func TestGetSetDefaultValueArgs(t *testing.T) {
require.Equal(t, inArgs, args)
}
}

func TestGetAddColumnArgs(t *testing.T) {
inArgs := &AddColumnArgs{
Col: &ColumnInfo{
ID: 7527,
Name: model.NewCIStr("col_name"),g
},
Pos: &ast.ColumnPosition{
Tp: ast.ColumnPositionFirst,
},
Offset: 1001,
IfNotExists: true,
}
for _, v := range []JobVersion{JobVersion1, JobVersion2} {
j2 := &Job{}
require.NoError(t, j2.Decode(getJobBytes(t, inArgs, v, ActionAddColumn)))
args, err := GetAddColumnArgs(j2)
require.NoError(t, err)
require.Equal(t, inArgs, args)
}
}

0 comments on commit 272174c

Please sign in to comment.