Skip to content

Commit

Permalink
expression, executor: castStringAsTime returns null when sql_mode is …
Browse files Browse the repository at this point in the history
…not strict (#8516)
  • Loading branch information
XuHuaiyu authored and zz-jason committed Dec 6, 2018
1 parent e61bad0 commit fde9c72
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
28 changes: 16 additions & 12 deletions cmd/explaintest/r/partition_pruning.result
Original file line number Diff line number Diff line change
Expand Up @@ -4038,18 +4038,22 @@ INSERT INTO t1 VALUES (1, '2009-01-01'), (2, NULL);
# test with an invalid date, which lead to item->null_value is set.
EXPLAIN SELECT * FROM t1 WHERE b < CAST('2009-04-99' AS DATETIME);
id count task operator info
Selection_11 40000.00 root lt(test.t1.b, cast("2009-04-99"))
└─Union_12 50000.00 root
├─TableReader_14 10000.00 root data:TableScan_13
│ └─TableScan_13 10000.00 cop table:t1, partition:p20090401, range:[-inf,+inf], keep order:false, stats:pseudo
├─TableReader_16 10000.00 root data:TableScan_15
│ └─TableScan_15 10000.00 cop table:t1, partition:p20090402, range:[-inf,+inf], keep order:false, stats:pseudo
├─TableReader_18 10000.00 root data:TableScan_17
│ └─TableScan_17 10000.00 cop table:t1, partition:p20090403, range:[-inf,+inf], keep order:false, stats:pseudo
├─TableReader_20 10000.00 root data:TableScan_19
│ └─TableScan_19 10000.00 cop table:t1, partition:p20090404, range:[-inf,+inf], keep order:false, stats:pseudo
└─TableReader_22 10000.00 root data:TableScan_21
└─TableScan_21 10000.00 cop table:t1, partition:p20090405, range:[-inf,+inf], keep order:false, stats:pseudo
Union_11 0.00 root
├─TableReader_14 0.00 root data:Selection_13
│ └─Selection_13 0.00 cop lt(test.t1.b, NULL)
│ └─TableScan_12 10000.00 cop table:t1, partition:p20090401, range:[-inf,+inf], keep order:false, stats:pseudo
├─TableReader_17 0.00 root data:Selection_16
│ └─Selection_16 0.00 cop lt(test.t1.b, NULL)
│ └─TableScan_15 10000.00 cop table:t1, partition:p20090402, range:[-inf,+inf], keep order:false, stats:pseudo
├─TableReader_20 0.00 root data:Selection_19
│ └─Selection_19 0.00 cop lt(test.t1.b, NULL)
│ └─TableScan_18 10000.00 cop table:t1, partition:p20090403, range:[-inf,+inf], keep order:false, stats:pseudo
├─TableReader_23 0.00 root data:Selection_22
│ └─Selection_22 0.00 cop lt(test.t1.b, NULL)
│ └─TableScan_21 10000.00 cop table:t1, partition:p20090404, range:[-inf,+inf], keep order:false, stats:pseudo
└─TableReader_26 0.00 root data:Selection_25
└─Selection_25 0.00 cop lt(test.t1.b, NULL)
└─TableScan_24 10000.00 cop table:t1, partition:p20090405, range:[-inf,+inf], keep order:false, stats:pseudo
DROP TABLE t1;
CREATE TABLE t1
(a INT NOT NULL AUTO_INCREMENT,
Expand Down
11 changes: 11 additions & 0 deletions executor/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,17 @@ func (s *testSuite) TestUpdate(c *C) {
tk.MustExec("insert into t values (0.0);")
_, err = tk.Exec("update t set c1 = 2.0;")
c.Assert(types.ErrWarnDataOutOfRange.Equal(err), IsTrue)

tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a datetime not null, b datetime)")
tk.MustExec("insert into t value('1999-12-12', '1999-12-13')")
tk.MustExec(" set @orig_sql_mode=@@sql_mode; set @@sql_mode='';")
tk.MustQuery("select * from t").Check(testkit.Rows("1999-12-12 00:00:00 1999-12-13 00:00:00"))
tk.MustExec("update t set a = ''")
tk.MustQuery("select * from t").Check(testkit.Rows("0000-00-00 00:00:00 1999-12-13 00:00:00"))
tk.MustExec("update t set b = ''")
tk.MustQuery("select * from t").Check(testkit.Rows("0000-00-00 00:00:00 <nil>"))
tk.MustExec("set @@sql_mode=@orig_sql_mode;")
}

func (s *testSuite) TestPartitionedTableUpdate(c *C) {
Expand Down
4 changes: 2 additions & 2 deletions expression/builtin_cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1175,13 +1175,13 @@ func (b *builtinCastStringAsTimeSig) evalTime(row chunk.Row) (res types.Time, is
sc := b.ctx.GetSessionVars().StmtCtx
res, err = types.ParseTime(sc, val, b.tp.Tp, b.tp.Decimal)
if err != nil {
return res, false, errors.Trace(err)
return types.Time{}, true, handleInvalidTimeError(b.ctx, err)
}
if b.tp.Tp == mysql.TypeDate {
// Truncate hh:mm:ss part if the type is Date.
res.Time = types.FromDate(res.Time.Year(), res.Time.Month(), res.Time.Day(), 0, 0, 0, 0)
}
return res, false, errors.Trace(err)
return res, false, nil
}

type builtinCastStringAsDurationSig struct {
Expand Down
3 changes: 2 additions & 1 deletion expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1550,7 +1550,8 @@ func (s *testIntegrationSuite) TestTimeBuiltin(c *C) {
result = tk.MustQuery("SELECT UNIX_TIMESTAMP('1969-12-31 23:59:59');")
result.Check(testkit.Rows("0"))
result = tk.MustQuery("SELECT UNIX_TIMESTAMP('1970-13-01 00:00:00');")
result.Check(testkit.Rows("0"))
// FIXME: MySQL returns 0 here.
result.Check(testkit.Rows("<nil>"))
result = tk.MustQuery("SELECT UNIX_TIMESTAMP('2038-01-19 03:14:07.999999');")
result.Check(testkit.Rows("2147483647.999999"))
result = tk.MustQuery("SELECT UNIX_TIMESTAMP('2038-01-19 03:14:08');")
Expand Down

0 comments on commit fde9c72

Please sign in to comment.