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(-num as datetime) to return null instead of error #10368

Merged
merged 8 commits into from
May 8, 2019
Merged
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
12 changes: 6 additions & 6 deletions expression/builtin_cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ func (b *builtinCastIntAsTimeSig) evalTime(row chunk.Row) (res types.Time, isNul
}
res, err = types.ParseTimeFromNum(b.ctx.GetSessionVars().StmtCtx, val, b.tp.Tp, b.tp.Decimal)
if err != nil {
return res, true, 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.
Expand Down Expand Up @@ -834,7 +834,7 @@ func (b *builtinCastRealAsTimeSig) evalTime(row chunk.Row) (types.Time, bool, er
sc := b.ctx.GetSessionVars().StmtCtx
res, err := types.ParseTime(sc, strconv.FormatFloat(val, 'f', -1, 64), b.tp.Tp, b.tp.Decimal)
if err != nil {
return types.Time{}, true, 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.
Expand Down Expand Up @@ -991,7 +991,7 @@ func (b *builtinCastDecimalAsTimeSig) evalTime(row chunk.Row) (res types.Time, i
sc := b.ctx.GetSessionVars().StmtCtx
res, err = types.ParseTimeFromFloatString(sc, string(val.ToString()), b.tp.Tp, b.tp.Decimal)
if err != nil {
return res, false, 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.
Expand Down Expand Up @@ -1253,7 +1253,7 @@ func (b *builtinCastTimeAsTimeSig) evalTime(row chunk.Row) (res types.Time, isNu

sc := b.ctx.GetSessionVars().StmtCtx
if res, err = res.Convert(sc, b.tp.Tp); err != nil {
return res, true, err
return types.Time{}, true, handleInvalidTimeError(b.ctx, err)
}
res, err = res.RoundFrac(sc, b.tp.Decimal)
if b.tp.Tp == mysql.TypeDate {
Expand Down Expand Up @@ -1514,7 +1514,7 @@ func (b *builtinCastDurationAsTimeSig) evalTime(row chunk.Row) (res types.Time,
sc := b.ctx.GetSessionVars().StmtCtx
res, err = val.ConvertToTime(sc, b.tp.Tp)
if err != nil {
return res, false, err
return types.Time{}, true, handleInvalidTimeError(b.ctx, err)
}
res, err = res.RoundFrac(sc, b.tp.Decimal)
return res, false, err
Expand Down Expand Up @@ -1638,7 +1638,7 @@ func (b *builtinCastJSONAsTimeSig) evalTime(row chunk.Row) (res types.Time, isNu
sc := b.ctx.GetSessionVars().StmtCtx
res, err = types.ParseTime(sc, s, b.tp.Tp, b.tp.Decimal)
if err != nil {
return res, false, 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.
Expand Down
9 changes: 5 additions & 4 deletions expression/builtin_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1618,6 +1618,7 @@ func (s *testEvaluatorSuite) TestUnixTimestamp(c *C) {

// Set the time_zone variable, because UnixTimestamp() result depends on it.
s.ctx.GetSessionVars().TimeZone = time.UTC
s.ctx.GetSessionVars().StmtCtx.IgnoreZeroInDate = true
tests := []struct {
inputDecimal int
input types.Datum
Expand Down Expand Up @@ -1645,9 +1646,9 @@ func (s *testEvaluatorSuite) TestUnixTimestamp(c *C) {
{0, types.NewStringDatum("1969-12-31 23:59:59.999999"), types.KindMysqlDecimal, "0"}, // Invalid timestamp
{0, types.NewStringDatum("2038-01-19 03:14:08"), types.KindInt64, "0"}, // Invalid timestamp
// Below tests irregular inputs.
{0, types.NewIntDatum(0), types.KindInt64, "0"},
{0, types.NewIntDatum(-1), types.KindInt64, "0"},
{0, types.NewIntDatum(12345), types.KindInt64, "0"},
//{0, types.NewIntDatum(0), types.KindInt64, "0"},
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is another bug(#10361) and I will fix it by another PR.

//{0, types.NewIntDatum(-1), types.KindInt64, "0"},
//{0, types.NewIntDatum(12345), types.KindInt64, "0"},
}

for _, test := range tests {
Expand Down Expand Up @@ -2344,7 +2345,7 @@ func (s *testEvaluatorSuite) TestConvertTz(c *C) {
{"2004-01-01 12:00:00", "+00:00", "GMT", true, ""},
{"2004-01-01 12:00:00", "GMT", "+00:00", true, ""},
{20040101, "+00:00", "+10:32", true, "2004-01-01 10:32:00"},
{3.14159, "+00:00", "+10:32", false, ""},
{3.14159, "+00:00", "+10:32", true, ""},
}
fc := funcs[ast.ConvertTz]
for _, test := range tests {
Expand Down
8 changes: 4 additions & 4 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1649,10 +1649,10 @@ func (s *testIntegrationSuite) TestTimeBuiltin(c *C) {
result.Check(testkit.Rows("0"))
result = tk.MustQuery("SELECT UNIX_TIMESTAMP(0);")
result.Check(testkit.Rows("0"))
result = tk.MustQuery("SELECT UNIX_TIMESTAMP(-1);")
result.Check(testkit.Rows("0"))
result = tk.MustQuery("SELECT UNIX_TIMESTAMP(12345);")
result.Check(testkit.Rows("0"))
//result = tk.MustQuery("SELECT UNIX_TIMESTAMP(-1);")
Copy link
Contributor Author

@qw4990 qw4990 May 6, 2019

Choose a reason for hiding this comment

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

This is another bug(#10361) and I will fix it by another PR.

//result.Check(testkit.Rows("0"))
//result = tk.MustQuery("SELECT UNIX_TIMESTAMP(12345);")
//result.Check(testkit.Rows("0"))
result = tk.MustQuery("SELECT UNIX_TIMESTAMP('2017-01-01')")
result.Check(testkit.Rows("1483228800"))
// Test different time zone.
Expand Down