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: special handling is required when an "auto id out of range" error occurs in insert ignore into ... on on duplicate ... #39847

Merged
merged 27 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
4abb091
remove unnecessary checks during inserts
Dousir9 Dec 12, 2022
9b10da2
add test case
Dousir9 Dec 13, 2022
aab8392
add IgnoreTruncate and TruncateAsWarning support for autoid.ErrAutoin…
Dousir9 Dec 13, 2022
463cfcc
Handling the special case of
Dousir9 Dec 13, 2022
fee23b1
fixed a bug that returned an incorrect err
Dousir9 Dec 20, 2022
68c27fa
Merge branch 'master' into fix-38950
Dousir9 Dec 20, 2022
90fcc44
test
Dousir9 Dec 20, 2022
e67f6fb
Merge branch 'fix-38950' of github.com:Dousir9/tidb into fix-38950
Dousir9 Dec 20, 2022
80936ea
test
Dousir9 Dec 20, 2022
daeca72
Merge branch 'master' into fix-38950
hawkingrei Dec 20, 2022
9ca314b
Merge branch 'master' into fix-38950
Dousir9 Dec 20, 2022
d532251
test
Dousir9 Dec 20, 2022
6e27ed9
Merge branch 'fix-38950' of github.com:Dousir9/tidb into fix-38950
Dousir9 Dec 20, 2022
7f7eedb
Merge branch 'master' into fix-38950
Dousir9 Dec 21, 2022
f81fb79
Merge branch 'master' into fix-38950
Dousir9 Dec 21, 2022
a6a948a
Merge branch 'master' into fix-38950
Dousir9 Dec 21, 2022
f06381f
Merge branch 'master' into fix-38950
hawkingrei Dec 22, 2022
17badfb
check if the type cast is ok to avoid panic
Dousir9 Dec 26, 2022
8b966db
Merge branch 'fix-38950' of github.com:Dousir9/tidb into fix-38950
Dousir9 Dec 26, 2022
8892c7b
Merge branch 'master' of github.com:pingcap/tidb into fix-38950
Dousir9 Dec 27, 2022
7649f61
change the name of the test case
Dousir9 Dec 27, 2022
c2b9f01
Update executor/writetest/write_test.go
Dousir9 Dec 30, 2022
b11a896
Update executor/writetest/write_test.go
Dousir9 Dec 30, 2022
cd41491
minor
Dousir9 Dec 30, 2022
69bee57
update comment
Dousir9 Dec 30, 2022
b084390
Merge branch 'master' into fix-38950
Dousir9 Dec 30, 2022
e48180a
Merge branch 'master' into fix-38950
Dousir9 Jan 3, 2023
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
12 changes: 11 additions & 1 deletion executor/insert_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/sessiontxn"
Expand Down Expand Up @@ -771,7 +772,16 @@ func setDatumAutoIDAndCast(ctx sessionctx.Context, d *types.Datum, id int64, col
var err error
*d, err = table.CastValue(ctx, *d, col.ToInfo(), false, false)
if err == nil && d.GetInt64() < id {
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 some test cases for it.

Copy link
Contributor

Choose a reason for hiding this comment

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

maybe we should add a check about core.Insert.IsReplace in this line 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, this also works, but we can put sc := ctx.GetSessionVars().StmtCtx and insertPlan, ok := sc.GetPlan().(*core.Insert) inside the if statement block to avoid more calls.

// Auto ID is out of range, the truncated ID is possible to duplicate with an existing ID.
// Auto ID is out of range.
sc := ctx.GetSessionVars().StmtCtx
insertPlan, ok := sc.GetPlan().(*core.Insert)
if ok && sc.TruncateAsWarning && len(insertPlan.OnDuplicate) > 0 {
// Fix issue #38950: AUTO_INCREMENT is incompatible with mysql
// An auto id out of range error occurs in `insert ignore into ... on duplicate ...`.
// We should allow the SQL to be executed successfully.
return nil
}
// The truncated ID is possible to duplicate with an existing ID.
// To prevent updating unrelated rows in the REPLACE statement, it is better to throw an error.
return autoid.ErrAutoincReadFailed
}
Expand Down
19 changes: 19 additions & 0 deletions executor/writetest/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,25 @@ commit;`
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1526 Table has no partition for value 3"))
}

func TestIssue38950(t *testing.T) {
store := testkit.CreateMockStore(t)
var cfg kv.InjectionConfig
tk := testkit.NewTestKit(t, kv.NewInjectedStore(store, &cfg))
tk.MustExec("use test;")
tk.MustExec("drop table if exists t; create table t (id smallint auto_increment primary key);")
tk.MustExec("alter table t add column c1 int default 1;")
tk.MustExec("insert ignore into t(id) values (194626268);")
require.Empty(t, tk.Session().LastMessage())

tk.MustQuery("select * from t").Check(testkit.Rows("32767 1"))

tk.MustExec("insert ignore into t(id) values ('*') on duplicate key update c1 = 2;")
require.Equal(t, int64(2), int64(tk.Session().AffectedRows()))
require.Empty(t, tk.Session().LastMessage())

tk.MustQuery("select * from t").Check(testkit.Rows("32767 2"))
}

func TestInsertOnDup(t *testing.T) {
store := testkit.CreateMockStore(t)
var cfg kv.InjectionConfig
Expand Down