diff --git a/executor/executor.go b/executor/executor.go index 8215c723a2dab..4eb6c7f9f0413 100644 --- a/executor/executor.go +++ b/executor/executor.go @@ -1706,6 +1706,10 @@ func FillVirtualColumnValue(virtualRetTypes []*types.FieldType, virtualColumnInd if err != nil { return err } + // Handle the bad null error. + if (mysql.HasNotNullFlag(columns[idx].Flag) || mysql.HasPreventNullInsertFlag(columns[idx].Flag)) && castDatum.IsNull() { + castDatum = table.GetZeroValue(columns[idx]) + } virCols.AppendDatum(i, &castDatum) } req.SetCol(idx, virCols.Column(i)) diff --git a/executor/union_scan.go b/executor/union_scan.go index 43f316b785cdd..cdc13e19c8f8b 100644 --- a/executor/union_scan.go +++ b/executor/union_scan.go @@ -19,6 +19,7 @@ import ( "runtime/trace" "github.com/pingcap/parser/model" + "github.com/pingcap/parser/mysql" "github.com/pingcap/tidb/expression" "github.com/pingcap/tidb/kv" plannercore "github.com/pingcap/tidb/planner/core" @@ -134,6 +135,10 @@ func (us *UnionScanExec) Next(ctx context.Context, req *chunk.Chunk) error { if err != nil { return err } + // Handle the bad null error. + if (mysql.HasNotNullFlag(us.columns[idx].Flag) || mysql.HasPreventNullInsertFlag(us.columns[idx].Flag)) && castDatum.IsNull() { + castDatum = table.GetZeroValue(us.columns[idx]) + } mutableRow.SetDatum(idx, castDatum) } diff --git a/expression/integration_test.go b/expression/integration_test.go index 54fed7610f487..a54be29493365 100755 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -7330,6 +7330,21 @@ func (s *testIntegrationSerialSuite) TestIssue18949(c *C) { c.Assert(result, Matches, `(?s).*enum\('a','b ',' c'\).*set\('a','b ',' c'\).*`) } +func (s *testIntegrationSuite) TestIssue17767(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t0;") + tk.MustExec("CREATE TABLE t0(c0 INTEGER AS (NULL) NOT NULL, c1 INT);") + tk.MustExec("CREATE INDEX i0 ON t0(c0, c1);") + tk.MustExec("INSERT IGNORE INTO t0(c1) VALUES (0);") + tk.MustQuery("SELECT * FROM t0").Check(testkit.Rows("0 0")) + + tk.MustExec("begin") + tk.MustExec("INSERT IGNORE INTO t0(c1) VALUES (0);") + tk.MustQuery("SELECT * FROM t0").Check(testkit.Rows("0 0", "0 0")) + tk.MustExec("rollback") +} + func (s *testIntegrationSuite) TestIssue19596(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test")