Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expression: fix cast invalid string to datetime #26726

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion expression/builtin_cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,7 @@ 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, int8(b.tp.Decimal))
if err != nil {
return types.ZeroTime, true, handleInvalidTimeError(b.ctx, err)
return types.ZeroTime, b.ctx.GetSessionVars().StrictSQLMode, handleInvalidTimeError(b.ctx, err)
}
if b.tp.Tp == mysql.TypeDate {
// Truncate hh:mm:ss part if the type is Date.
Expand Down
2 changes: 0 additions & 2 deletions expression/builtin_cast_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -1696,8 +1696,6 @@ func (b *builtinCastStringAsTimeSig) vecEvalTime(input *chunk.Chunk, result *chu
if err = handleInvalidTimeError(b.ctx, err); err != nil {
return err
}
result.SetNull(i, true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use b.ctx.GetSessionVars().StrictSQLMode?

continue
}
times[i] = tm
if b.tp.Tp == mysql.TypeDate {
Expand Down
16 changes: 14 additions & 2 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1977,8 +1977,7 @@ func (s *testIntegrationSuite2) 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');")
// FIXME: MySQL returns 0 here.
result.Check(testkit.Rows("<nil>"))
result.Check(testkit.Rows("0"))
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 Expand Up @@ -2632,6 +2631,8 @@ func (s *testIntegrationSuite2) TestBuiltin(c *C) {
result.Check(testkit.Rows("2017-01-01 00:00:00.00"))
result = tk.MustQuery(`select cast(20170118.999 as datetime);`)
result.Check(testkit.Rows("2017-01-18 00:00:00"))
result = tk.MustQuery(`select cast('e' as date);`)
result.Check(testkit.Rows("<nil>"))
tk.MustQuery(`select convert(a2.a, unsigned int) from (select cast('"9223372036854775808"' as json) as a) as a2;`)

tk.MustExec(`create table tb5(a bigint(64) unsigned, b double);`)
Expand Down Expand Up @@ -10147,3 +10148,14 @@ func (s *testIntegrationSuite) TestIssue26958(c *C) {
tk.MustQuery("select \n(select count(distinct c_int) from t2 where c_int >= t1.c_int) c1, \n(select count(distinct c_int) from t2 where c_int >= t1.c_int) c2\nfrom t1 group by c_int;\n").
Check(testkit.Rows("3 3", "2 2", "1 1"))
}

func (s *testIntegrationSuite) TestIssue24997(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(c1 varbinary(100));")
tk.MustExec("insert into t1 values('e');")
tk.MustExec("create table t2(c1 date);")
tk.MustExec("insert into t2 values('2019-11-10');")
tk.MustQuery("select * from t1 inner join t2 on t1.c1 <= t2.c1;").Check(testkit.Rows("e 2019-11-10"))
}