Skip to content

Commit

Permalink
ddl: clean up createColumnInfo (pingcap#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
tangenta authored Apr 11, 2022
1 parent 0af0498 commit 8c0130a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 28 deletions.
22 changes: 4 additions & 18 deletions ddl/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,8 @@ import (
"go.uber.org/zap"
)

func createColumnInfo(tblInfo *model.TableInfo, colInfo *model.ColumnInfo, pos *ast.ColumnPosition) (*model.ColumnInfo, *ast.ColumnPosition, error) {
// Check column name duplicate.
func createColumnInfo(tblInfo *model.TableInfo, colInfo *model.ColumnInfo) *model.ColumnInfo {
cols := tblInfo.Columns
// Should initialize pos when it is nil.
if pos == nil {
pos = &ast.ColumnPosition{}
}
colInfo.ID = allocateColumnID(tblInfo)
colInfo.State = model.StateNone
// To support add column asynchronous, we should mark its offset as the last column.
Expand All @@ -67,7 +62,7 @@ func createColumnInfo(tblInfo *model.TableInfo, colInfo *model.ColumnInfo, pos *
// Append the column info to the end of the tblInfo.Columns.
// It will reorder to the right offset in "Columns" when it state change to public.
tblInfo.Columns = append(cols, colInfo)
return colInfo, pos, nil
return colInfo
}

func checkAddColumn(t *meta.Meta, job *model.Job) (*model.TableInfo, *model.ColumnInfo, *model.ColumnInfo,
Expand Down Expand Up @@ -131,11 +126,7 @@ func onAddColumn(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, err error)
return ver, errors.Trace(err)
}
if columnInfo == nil {
columnInfo, _, err = createColumnInfo(tblInfo, col, pos)
if err != nil {
job.State = model.JobStateCancelled
return ver, errors.Trace(err)
}
columnInfo = createColumnInfo(tblInfo, col)
logutil.BgLogger().Info("[ddl] run add column job", zap.String("job", job.String()), zap.Reflect("columnInfo", *columnInfo))
if err = checkAddColumnTooManyColumns(len(tblInfo.Columns)); err != nil {
job.State = model.JobStateCancelled
Expand Down Expand Up @@ -559,7 +550,6 @@ func (w *worker) onModifyColumn(d *ddlCtx, t *meta.Meta, job *model.Job) (ver in
}

if modifyInfo.changingCol == nil {
changingColPos := &ast.ColumnPosition{Tp: ast.ColumnPositionNone}
newColName := model.NewCIStr(genChangingColumnUniqueName(tblInfo, oldCol))
if mysql.HasPriKeyFlag(oldCol.Flag) {
job.State = model.JobStateCancelled
Expand All @@ -578,11 +568,7 @@ func (w *worker) onModifyColumn(d *ddlCtx, t *meta.Meta, job *model.Job) (ver in
return ver, errors.Trace(err)
}

_, _, err = createColumnInfo(tblInfo, modifyInfo.changingCol, changingColPos)
if err != nil {
job.State = model.JobStateCancelled
return ver, errors.Trace(err)
}
createColumnInfo(tblInfo, modifyInfo.changingCol)

idxInfos, offsets := findIndexesByColName(tblInfo.Indices, oldCol.Name.L)
modifyInfo.changingIdxs = make([]*model.IndexInfo, 0, len(idxInfos))
Expand Down
7 changes: 1 addition & 6 deletions ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,13 +461,8 @@ func (w *worker) onCreateIndex(d *ddlCtx, t *meta.Meta, job *model.Job, isPK boo

if indexInfo == nil {
if len(hiddenCols) > 0 {
pos := &ast.ColumnPosition{Tp: ast.ColumnPositionNone}
for _, hiddenCol := range hiddenCols {
_, _, err = createColumnInfo(tblInfo, hiddenCol, pos)
if err != nil {
job.State = model.JobStateCancelled
return ver, errors.Trace(err)
}
createColumnInfo(tblInfo, hiddenCol)
}
}
if err = checkAddColumnTooManyColumns(len(tblInfo.Columns)); err != nil {
Expand Down
8 changes: 4 additions & 4 deletions ddl/rollingback.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,16 @@ func rollingbackModifyColumn(w *worker, d *ddlCtx, t *meta.Meta, job *model.Job)
// Give the this kind of ddl one more round to run, the dbterror.ErrCancelledDDLJob should be fetched from the bottom up.
return w.onModifyColumn(d, t, job)
}
_, tblInfo, oldCol, jp, err := getModifyColumnInfo(t, job)
_, tblInfo, oldCol, modifyInfo, err := getModifyColumnInfo(t, job)
if err != nil {
return ver, err
}
if !needChangeColumnData(oldCol, jp.newCol) {
if !needChangeColumnData(oldCol, modifyInfo.newCol) {
// Normal-type rolling back
if job.SchemaState == model.StateNone {
// When change null to not null, although state is unchanged with none, the oldCol flag's has been changed to preNullInsertFlag.
// To roll back this kind of normal job, it is necessary to mark the state as JobStateRollingback to restore the old col's flag.
if jp.modifyColumnTp == mysql.TypeNull && tblInfo.Columns[oldCol.Offset].Flag|mysql.PreventNullInsertFlag != 0 {
if modifyInfo.modifyColumnTp == mysql.TypeNull && tblInfo.Columns[oldCol.Offset].Flag|mysql.PreventNullInsertFlag != 0 {
job.State = model.JobStateRollingback
return ver, dbterror.ErrCancelledDDLJob
}
Expand All @@ -141,7 +141,7 @@ func rollingbackModifyColumn(w *worker, d *ddlCtx, t *meta.Meta, job *model.Job)
return ver, nil
}
// reorg-type rolling back
if jp.changingCol == nil {
if modifyInfo.changingCol == nil {
// The job hasn't been handled and we cancel it directly.
job.State = model.JobStateCancelled
return ver, dbterror.ErrCancelledDDLJob
Expand Down

0 comments on commit 8c0130a

Please sign in to comment.