diff --git a/executor/builder.go b/executor/builder.go index f1e2e689f5826..d192cc691fcfd 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 ( @@ -2169,7 +2175,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..1e9c95c3f336f 100644 --- a/executor/write_test.go +++ b/executor/write_test.go @@ -2872,3 +2872,22 @@ func (s *testSerialSuite1) TestIssue20724(c *C) { tk.MustQuery("select * from t1").Check(testkit.Rows("A")) tk.MustExec("drop table t1") } + +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")) +}