Skip to content

Commit

Permalink
executor: fix data race in the buildMemTableReader (#43431)
Browse files Browse the repository at this point in the history
close #43118
  • Loading branch information
hawkingrei authored May 4, 2023
1 parent 476e104 commit 79729c3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
6 changes: 1 addition & 5 deletions executor/mem_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,7 @@ func buildMemTableReader(ctx context.Context, us *UnionScanExec, tblReader *Tabl
}

defVal := func(i int) ([]byte, error) {
sessVars := us.ctx.GetSessionVars()
originStrict := sessVars.StrictSQLMode
sessVars.StrictSQLMode = false
d, err := table.GetColOriginDefaultValue(us.ctx, us.columns[i])
sessVars.StrictSQLMode = originStrict
d, err := table.GetColOriginDefaultValueWithoutStrictSQLMode(us.ctx, us.columns[i])
if err != nil {
return nil, err
}
Expand Down
29 changes: 23 additions & 6 deletions table/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,16 +485,27 @@ func (c *Column) IsCommonHandleColumn(tbInfo *model.TableInfo) bool {
return mysql.HasPriKeyFlag(c.GetFlag()) && tbInfo.IsCommonHandle
}

type getColOriginDefaultValue struct {
StrictSQLMode bool
}

// GetColOriginDefaultValue gets default value of the column from original default value.
func GetColOriginDefaultValue(ctx sessionctx.Context, col *model.ColumnInfo) (types.Datum, error) {
return getColDefaultValue(ctx, col, col.GetOriginDefaultValue())
return getColDefaultValue(ctx, col, col.GetOriginDefaultValue(), nil)
}

// GetColOriginDefaultValueWithoutStrictSQLMode gets default value of the column from original default value with Strict SQL mode.
func GetColOriginDefaultValueWithoutStrictSQLMode(ctx sessionctx.Context, col *model.ColumnInfo) (types.Datum, error) {
return getColDefaultValue(ctx, col, col.GetOriginDefaultValue(), &getColOriginDefaultValue{
StrictSQLMode: false,
})
}

// GetColDefaultValue gets default value of the column.
func GetColDefaultValue(ctx sessionctx.Context, col *model.ColumnInfo) (types.Datum, error) {
defaultValue := col.GetDefaultValue()
if !col.DefaultIsExpr {
return getColDefaultValue(ctx, col, defaultValue)
return getColDefaultValue(ctx, col, defaultValue, nil)
}
return getColDefaultExprValue(ctx, col, defaultValue.(string))
}
Expand Down Expand Up @@ -532,9 +543,9 @@ func getColDefaultExprValue(ctx sessionctx.Context, col *model.ColumnInfo, defau
return value, nil
}

func getColDefaultValue(ctx sessionctx.Context, col *model.ColumnInfo, defaultVal interface{}) (types.Datum, error) {
func getColDefaultValue(ctx sessionctx.Context, col *model.ColumnInfo, defaultVal interface{}, args *getColOriginDefaultValue) (types.Datum, error) {
if defaultVal == nil {
return getColDefaultValueFromNil(ctx, col)
return getColDefaultValueFromNil(ctx, col, args)
}

switch col.GetType() {
Expand Down Expand Up @@ -577,7 +588,7 @@ func getColDefaultValue(ctx sessionctx.Context, col *model.ColumnInfo, defaultVa
return value, nil
}

func getColDefaultValueFromNil(ctx sessionctx.Context, col *model.ColumnInfo) (types.Datum, error) {
func getColDefaultValueFromNil(ctx sessionctx.Context, col *model.ColumnInfo, args *getColOriginDefaultValue) (types.Datum, error) {
if !mysql.HasNotNullFlag(col.GetFlag()) && !mysql.HasNoDefaultValueFlag(col.GetFlag()) {
return types.Datum{}, nil
}
Expand All @@ -599,7 +610,13 @@ func getColDefaultValueFromNil(ctx sessionctx.Context, col *model.ColumnInfo) (t
}
vars := ctx.GetSessionVars()
sc := vars.StmtCtx
if !vars.StrictSQLMode {
var strictSQLMode bool
if args != nil {
strictSQLMode = args.StrictSQLMode
} else {
strictSQLMode = vars.StrictSQLMode
}
if !strictSQLMode {
sc.AppendWarning(ErrNoDefaultValue.FastGenByArgs(col.Name))
if mysql.HasNotNullFlag(col.GetFlag()) {
return GetZeroValue(col), nil
Expand Down

0 comments on commit 79729c3

Please sign in to comment.