Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

executor: fix uint type overflow on generated column not compatible with mysql | tidb-test=pr/2088 #40157

Merged
merged 19 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions executor/insert_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ func (e *InsertValues) evalRow(ctx context.Context, list []expression.Expression
e.evalBuffer.SetDatum(offset, val1)
}
// Row may lack of generated column, autoIncrement column, empty column here.
return e.fillRow(ctx, row, hasValue)
return e.fillRow(ctx, row, hasValue, rowIdx)
}

var emptyRow chunk.Row
Expand Down Expand Up @@ -422,7 +422,7 @@ func (e *InsertValues) fastEvalRow(ctx context.Context, list []expression.Expres
offset := e.insertColumns[i].Offset
row[offset], hasValue[offset] = val1, true
}
return e.fillRow(ctx, row, hasValue)
return e.fillRow(ctx, row, hasValue, rowIdx)
}

// setValueForRefColumn set some default values for the row to eval the row value with other columns,
Expand Down Expand Up @@ -562,7 +562,7 @@ func (e *InsertValues) getRow(ctx context.Context, vals []types.Datum) ([]types.
hasValue[offset] = true
}

return e.fillRow(ctx, row, hasValue)
return e.fillRow(ctx, row, hasValue, 0)
}

// getColDefaultValue gets the column default value.
Expand Down Expand Up @@ -647,7 +647,7 @@ func (e *InsertValues) fillColValue(ctx context.Context, datum types.Datum, idx
// `insert|replace values` can guarantee consecutive autoID in a batch.
// Other statements like `insert select from` don't guarantee consecutive autoID.
// https://dev.mysql.com/doc/refman/8.0/en/innodb-auto-increment-handling.html
func (e *InsertValues) fillRow(ctx context.Context, row []types.Datum, hasValue []bool) ([]types.Datum, error) {
func (e *InsertValues) fillRow(ctx context.Context, row []types.Datum, hasValue []bool, rowIdx int) ([]types.Datum, error) {
gCols := make([]*table.Column, 0)
tCols := e.Table.Cols()
if e.hasExtraHandle {
Expand Down Expand Up @@ -690,16 +690,25 @@ func (e *InsertValues) fillRow(ctx context.Context, row []types.Datum, hasValue
return nil, err
}
}
sc := e.ctx.GetSessionVars().StmtCtx
warnCnt := int(sc.WarningCount())
for i, gCol := range gCols {
colIdx := gCol.ColumnInfo.Offset
val, err := e.GenExprs[i].Eval(chunk.MutRowFromDatums(row).ToRow())
if e.ctx.GetSessionVars().StmtCtx.HandleTruncate(err) != nil {
return nil, err
}
row[colIdx], err = table.CastValue(e.ctx, val, gCol.ToInfo(), false, false)
if err != nil {
if err = e.handleErr(gCol, &val, rowIdx, err); err != nil {
return nil, err
}
if newWarnings := sc.TruncateWarnings(warnCnt); len(newWarnings) > 0 {
for k := range newWarnings {
newWarnings[k].Err = completeInsertErr(gCol.ColumnInfo, &val, rowIdx, newWarnings[k].Err)
}
sc.AppendWarnings(newWarnings)
warnCnt += len(newWarnings)
}
// Handle the bad null error.
if err = gCol.HandleBadNull(&row[colIdx], e.ctx.GetSessionVars().StmtCtx); err != nil {
return nil, err
Expand Down
16 changes: 16 additions & 0 deletions executor/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4299,3 +4299,19 @@ func TestIssueInsertPrefixIndexForNonUTF8Collation(t *testing.T) {
tk.MustExec("insert into t3 select 'abc '")
tk.MustGetErrCode("insert into t3 select 'abc d'", 1062)
}

func TestIssue40066(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add more tests, for example, type char, decimal

store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t;")
tk.MustExec(`create table t(column1 int, column2 int unsigned generated always as(column1-100));`)
tk.MustExec("set @orig_sql_mode = @@sql_mode; set @@sql_mode = 'TRADITIONAL';")
tk.MustGetErrMsg("insert into t(column1) values (99);", "[types:1264]Out of range value for column 'column2' at row 1")
tk.MustExec("set @@sql_mode = '';")
tk.MustExec("insert into t(column1) values (99);")
tk.MustQuery("show warnings;").Check(testkit.Rows("Warning 1264 Out of range value for column 'column2' at row 1"))
tk.MustQuery("select * from t;").Check(testkit.Rows("99 0"))
tk.MustExec("set @@sql_mode = @orig_sql_mode;")
tk.MustExec("drop table if exists t;")
}
3 changes: 2 additions & 1 deletion sessionctx/stmtctx/stmtctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ type StatementContext struct {
InDeleteStmt bool
InSelectStmt bool
InLoadDataStmt bool
InFillVirtualColumnValue bool
InExplainStmt bool
InCreateOrAlterStmt bool
InSetSessionStatesStmt bool
Expand Down Expand Up @@ -1002,7 +1003,7 @@ func (sc *StatementContext) GetExecDetails() execdetails.ExecDetails {
// This is the case for `insert`, `update`, `alter table`, `create table` and `load data infile` statements, when not in strict SQL mode.
// see https://dev.mysql.com/doc/refman/5.7/en/out-of-range-and-overflow.html
func (sc *StatementContext) ShouldClipToZero() bool {
return sc.InInsertStmt || sc.InLoadDataStmt || sc.InUpdateStmt || sc.InCreateOrAlterStmt || sc.IsDDLJobInQueue
return sc.InInsertStmt || sc.InLoadDataStmt || sc.InUpdateStmt || sc.InCreateOrAlterStmt || sc.IsDDLJobInQueue || sc.InFillVirtualColumnValue
}

// ShouldIgnoreOverflowError indicates whether we should ignore the error when type conversion overflows,
Expand Down
2 changes: 2 additions & 0 deletions table/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,9 @@ func FillVirtualColumnValue(virtualRetTypes []*types.FieldType, virtualColumnInd
}
// Because the expression might return different type from
// the generated column, we should wrap a CAST on the result.
sctx.GetSessionVars().StmtCtx.InFillVirtualColumnValue = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting it here may cause a data race. FillVirtualColumnValue() can be called concurrently.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi @wjhuang2016 , Can you give me some advice on how to fix it? The core reason is the "ShouldClipToZero",but i have no idea how to solve it properly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid there is not a clean way to do it. But we can add a particular check after CastValue() in FillVirtualColumnValue. We can check its type and value, then clip it to 0 if necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wjhuang2016 i'm not very sure how to check it as you mentioned. After CastValue(),it should be a valiad destination type value already,how can judge it properly?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can know its type before CastValue(), and validate its value after CastValue(). If it doesn't obey the ShouldClipToZero, then ClipToZero.

castDatum, err := CastValue(sctx, datum, colInfos[idx], false, true)
sctx.GetSessionVars().StmtCtx.InFillVirtualColumnValue = false
if err != nil {
return err
}
Expand Down