From f301ad2bc2dddf483ddab46829ece8193b70a71a Mon Sep 17 00:00:00 2001 From: Yiding Cui Date: Tue, 24 Nov 2020 17:00:53 +0800 Subject: [PATCH 1/3] cherry pick #21233 to release-4.0 Signed-off-by: ti-srebot --- executor/builder.go | 16 +++++++-- executor/write_test.go | 74 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 2 deletions(-) diff --git a/executor/builder.go b/executor/builder.go index 4fea31d56af9e..5d1354ea1d60c 100644 --- a/executor/builder.go +++ b/executor/builder.go @@ -2080,7 +2080,13 @@ func (b *executorBuilder) buildIndexLookUpJoin(v *plannercore.PhysicalIndexJoin) innerPlan := v.Children()[v.InnerChildIdx] innerTypes := make([]*types.FieldType, innerPlan.Schema().Len()) for i, col := range innerPlan.Schema().Columns { - innerTypes[i] = col.RetType + innerTypes[i] = col.RetType.Clone() + // The `innerTypes` would be called for `Datum.ConvertTo` when converting the columns from outer table + // to build hash map or construct lookup keys. So we need to modify its Flen otherwise there would be + // truncate error. See issue https://github.com/pingcap/tidb/issues/21232 for example. + if innerTypes[i].EvalType() == types.ETString { + innerTypes[i].Flen = types.UnspecifiedLength + } } var ( @@ -2158,7 +2164,13 @@ func (b *executorBuilder) buildIndexLookUpMergeJoin(v *plannercore.PhysicalIndex innerPlan := v.Children()[v.InnerChildIdx] innerTypes := make([]*types.FieldType, innerPlan.Schema().Len()) for i, col := range innerPlan.Schema().Columns { - innerTypes[i] = col.RetType + innerTypes[i] = col.RetType.Clone() + // The `innerTypes` would be called for `Datum.ConvertTo` when converting the columns from outer table + // to build hash map or construct lookup keys. So we need to modify its Flen otherwise there would be + // truncate error. See issue https://github.com/pingcap/tidb/issues/21232 for example. + if innerTypes[i].EvalType() == types.ETString { + innerTypes[i].Flen = types.UnspecifiedLength + } } var ( outerFilter []expression.Expression diff --git a/executor/write_test.go b/executor/write_test.go index eb4234ba4dd97..6178cec69a2a5 100644 --- a/executor/write_test.go +++ b/executor/write_test.go @@ -2872,3 +2872,77 @@ func (s *testSerialSuite1) TestIssue20724(c *C) { tk.MustQuery("select * from t1").Check(testkit.Rows("A")) tk.MustExec("drop table t1") } +<<<<<<< HEAD +======= + +func (s *testSerialSuite) TestIssue20840(c *C) { + collate.SetNewCollationEnabledForTest(true) + defer collate.SetNewCollationEnabledForTest(false) + + tk := testkit.NewTestKitWithInit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t1") + tk.MustExec("set tidb_enable_clustered_index = 0") + tk.MustExec("create table t1 (i varchar(20) unique key) collate=utf8mb4_general_ci") + tk.MustExec("insert into t1 values ('a')") + tk.MustExec("replace into t1 values ('A')") + tk.MustQuery("select * from t1").Check(testkit.Rows("A")) + tk.MustExec("drop table t1") +} + +func (s *testSuite) TestEqualDatumsAsBinary(c *C) { + tests := []struct { + a []interface{} + b []interface{} + same bool + }{ + // Positive cases + {[]interface{}{1}, []interface{}{1}, true}, + {[]interface{}{1, "aa"}, []interface{}{1, "aa"}, true}, + {[]interface{}{1, "aa", 1}, []interface{}{1, "aa", 1}, true}, + + // negative cases + {[]interface{}{1}, []interface{}{2}, false}, + {[]interface{}{1, "a"}, []interface{}{1, "aaaaaa"}, false}, + {[]interface{}{1, "aa", 3}, []interface{}{1, "aa", 2}, false}, + + // Corner cases + {[]interface{}{}, []interface{}{}, true}, + {[]interface{}{nil}, []interface{}{nil}, true}, + {[]interface{}{}, []interface{}{1}, false}, + {[]interface{}{1}, []interface{}{1, 1}, false}, + {[]interface{}{nil}, []interface{}{1}, false}, + } + for _, tt := range tests { + testEqualDatumsAsBinary(c, tt.a, tt.b, tt.same) + } +} + +func (s *testSuite) TestIssue21232(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t, t1") + tk.MustExec("create table t(a varchar(1), index idx(a))") + tk.MustExec("create table t1(a varchar(5), index idx(a))") + tk.MustExec("insert into t values('a'), ('b')") + tk.MustExec("insert into t1 values('a'), ('bbbbb')") + tk.MustExec("update /*+ INL_JOIN(t) */ t, t1 set t.a='a' where t.a=t1.a") + tk.MustQuery("show warnings").Check(testkit.Rows()) + tk.MustQuery("select * from t").Check(testkit.Rows("a", "b")) + tk.MustExec("update /*+ INL_HASH_JOIN(t) */ t, t1 set t.a='a' where t.a=t1.a") + tk.MustQuery("show warnings").Check(testkit.Rows()) + tk.MustQuery("select * from t").Check(testkit.Rows("a", "b")) + tk.MustExec("update /*+ INL_MERGE_JOIN(t) */ t, t1 set t.a='a' where t.a=t1.a") + tk.MustQuery("show warnings").Check(testkit.Rows()) + tk.MustQuery("select * from t").Check(testkit.Rows("a", "b")) +} + +func testEqualDatumsAsBinary(c *C, a []interface{}, b []interface{}, same bool) { + sc := new(stmtctx.StatementContext) + re := new(executor.ReplaceExec) + sc.IgnoreTruncate = true + res, err := re.EqualDatumsAsBinary(sc, types.MakeDatums(a...), types.MakeDatums(b...)) + c.Assert(err, IsNil) + c.Assert(res, Equals, same, Commentf("a: %v, b: %v", a, b)) +} +>>>>>>> 115c7f88e... executor: set the inner join keys' field length to unspecified (#21233) From 272a2ca52594893a4aa3fb621e7ec9c9d8226736 Mon Sep 17 00:00:00 2001 From: Yiding Cui Date: Tue, 24 Nov 2020 17:05:38 +0800 Subject: [PATCH 2/3] resolve conflicts --- executor/write_test.go | 56 +----------------------------------------- 1 file changed, 1 insertion(+), 55 deletions(-) diff --git a/executor/write_test.go b/executor/write_test.go index 6178cec69a2a5..a5b288acca525 100644 --- a/executor/write_test.go +++ b/executor/write_test.go @@ -27,6 +27,7 @@ import ( "github.com/pingcap/tidb/planner/core" "github.com/pingcap/tidb/session" "github.com/pingcap/tidb/sessionctx" + "github.com/pingcap/tidb/sessionctx/stmtctx" "github.com/pingcap/tidb/store/mockstore" "github.com/pingcap/tidb/table/tables" "github.com/pingcap/tidb/types" @@ -2872,51 +2873,6 @@ func (s *testSerialSuite1) TestIssue20724(c *C) { tk.MustQuery("select * from t1").Check(testkit.Rows("A")) tk.MustExec("drop table t1") } -<<<<<<< HEAD -======= - -func (s *testSerialSuite) TestIssue20840(c *C) { - collate.SetNewCollationEnabledForTest(true) - defer collate.SetNewCollationEnabledForTest(false) - - tk := testkit.NewTestKitWithInit(c, s.store) - tk.MustExec("use test") - tk.MustExec("drop table if exists t1") - tk.MustExec("set tidb_enable_clustered_index = 0") - tk.MustExec("create table t1 (i varchar(20) unique key) collate=utf8mb4_general_ci") - tk.MustExec("insert into t1 values ('a')") - tk.MustExec("replace into t1 values ('A')") - tk.MustQuery("select * from t1").Check(testkit.Rows("A")) - tk.MustExec("drop table t1") -} - -func (s *testSuite) TestEqualDatumsAsBinary(c *C) { - tests := []struct { - a []interface{} - b []interface{} - same bool - }{ - // Positive cases - {[]interface{}{1}, []interface{}{1}, true}, - {[]interface{}{1, "aa"}, []interface{}{1, "aa"}, true}, - {[]interface{}{1, "aa", 1}, []interface{}{1, "aa", 1}, true}, - - // negative cases - {[]interface{}{1}, []interface{}{2}, false}, - {[]interface{}{1, "a"}, []interface{}{1, "aaaaaa"}, false}, - {[]interface{}{1, "aa", 3}, []interface{}{1, "aa", 2}, false}, - - // Corner cases - {[]interface{}{}, []interface{}{}, true}, - {[]interface{}{nil}, []interface{}{nil}, true}, - {[]interface{}{}, []interface{}{1}, false}, - {[]interface{}{1}, []interface{}{1, 1}, false}, - {[]interface{}{nil}, []interface{}{1}, false}, - } - for _, tt := range tests { - testEqualDatumsAsBinary(c, tt.a, tt.b, tt.same) - } -} func (s *testSuite) TestIssue21232(c *C) { tk := testkit.NewTestKit(c, s.store) @@ -2936,13 +2892,3 @@ func (s *testSuite) TestIssue21232(c *C) { tk.MustQuery("show warnings").Check(testkit.Rows()) tk.MustQuery("select * from t").Check(testkit.Rows("a", "b")) } - -func testEqualDatumsAsBinary(c *C, a []interface{}, b []interface{}, same bool) { - sc := new(stmtctx.StatementContext) - re := new(executor.ReplaceExec) - sc.IgnoreTruncate = true - res, err := re.EqualDatumsAsBinary(sc, types.MakeDatums(a...), types.MakeDatums(b...)) - c.Assert(err, IsNil) - c.Assert(res, Equals, same, Commentf("a: %v, b: %v", a, b)) -} ->>>>>>> 115c7f88e... executor: set the inner join keys' field length to unspecified (#21233) From 272b858e4354692b1ad12f26970baf2fdad07ed1 Mon Sep 17 00:00:00 2001 From: Yiding Cui Date: Wed, 25 Nov 2020 13:35:42 +0800 Subject: [PATCH 3/3] fix test build failure --- executor/write_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/executor/write_test.go b/executor/write_test.go index a5b288acca525..1e9c95c3f336f 100644 --- a/executor/write_test.go +++ b/executor/write_test.go @@ -27,7 +27,6 @@ import ( "github.com/pingcap/tidb/planner/core" "github.com/pingcap/tidb/session" "github.com/pingcap/tidb/sessionctx" - "github.com/pingcap/tidb/sessionctx/stmtctx" "github.com/pingcap/tidb/store/mockstore" "github.com/pingcap/tidb/table/tables" "github.com/pingcap/tidb/types"