diff --git a/executor/insert_test.go b/executor/insert_test.go index b209330dabb67..8c20916d50708 100644 --- a/executor/insert_test.go +++ b/executor/insert_test.go @@ -883,3 +883,19 @@ func (s *testSuite3) TestIssue16366(c *C) { c.Assert(err, NotNil) c.Assert(strings.Contains(err.Error(), "Duplicate entry '0' for key 'PRIMARY'"), IsTrue, Commentf("%v", err)) } + +func (s *testSuite3) TestIssue20768(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t1, t2") + tk.MustExec("create table t1(a year, primary key(a))") + tk.MustExec("insert ignore into t1 values(null)") + tk.MustExec("create table t2(a int, key(a))") + tk.MustExec("insert into t2 values(0)") + tk.MustQuery("select /*+ hash_join(t1) */ * from t1 join t2 on t1.a = t2.a").Check(testkit.Rows("0 0")) + tk.MustQuery("select /*+ inl_join(t1) */ * from t1 join t2 on t1.a = t2.a").Check(testkit.Rows("0 0")) + tk.MustQuery("select /*+ inl_join(t2) */ * from t1 join t2 on t1.a = t2.a").Check(testkit.Rows("0 0")) + tk.MustQuery("select /*+ inl_hash_join(t1) */ * from t1 join t2 on t1.a = t2.a").Check(testkit.Rows("0 0")) + tk.MustQuery("select /*+ inl_merge_join(t1) */ * from t1 join t2 on t1.a = t2.a").Check(testkit.Rows("0 0")) + tk.MustQuery("select /*+ merge_join(t1) */ * from t1 join t2 on t1.a = t2.a").Check(testkit.Rows("0 0")) +} diff --git a/table/column.go b/table/column.go index ebed7a1238d3a..4843185cc68ff 100644 --- a/table/column.go +++ b/table/column.go @@ -460,12 +460,14 @@ func getColDefaultValueFromNil(ctx sessionctx.Context, col *model.ColumnInfo) (t func GetZeroValue(col *model.ColumnInfo) types.Datum { var d types.Datum switch col.Tp { - case mysql.TypeTiny, mysql.TypeInt24, mysql.TypeShort, mysql.TypeLong, mysql.TypeLonglong, mysql.TypeYear: + case mysql.TypeTiny, mysql.TypeInt24, mysql.TypeShort, mysql.TypeLong, mysql.TypeLonglong: if mysql.HasUnsignedFlag(col.Flag) { d.SetUint64(0) } else { d.SetInt64(0) } + case mysql.TypeYear: + d.SetInt64(0) case mysql.TypeFloat: d.SetFloat32(0) case mysql.TypeDouble: