Skip to content

Commit 718eb86

Browse files
committed
update date function to allow for zero time
1 parent ed21f64 commit 718eb86

File tree

2 files changed

+10
-17
lines changed

2 files changed

+10
-17
lines changed

enginetest/queries/function_queries.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1839,7 +1839,6 @@ var FunctionQueryTests = []QueryTest{
18391839
ExpectedWarningsCount: 1,
18401840
},
18411841
{
1842-
// This is not a valid time string in MySQL, but we allow it
18431842
Query: "select day('0000-00-00')",
18441843
Expected: []sql.Row{{0}},
18451844
},
@@ -1897,7 +1896,6 @@ var FunctionQueryTests = []QueryTest{
18971896
ExpectedWarningsCount: 1,
18981897
},
18991898
{
1900-
// This is not a valid time string in MySQL, but we allow it
19011899
Query: "select dayofmonth('0000-00-00')",
19021900
Expected: []sql.Row{{0}},
19031901
},
@@ -1983,7 +1981,6 @@ var FunctionQueryTests = []QueryTest{
19831981
ExpectedWarningsCount: 1,
19841982
},
19851983
{
1986-
// This is not a valid time string in MySQL, but we allow it
19871984
Query: "select month('0000-00-00')",
19881985
Expected: []sql.Row{{0}},
19891986
},
@@ -2153,7 +2150,6 @@ var FunctionQueryTests = []QueryTest{
21532150
ExpectedWarningsCount: 1,
21542151
},
21552152
{
2156-
// This is not a valid time string in MySQL, but we allow it
21572153
Query: "select quarter('0000-00-00')",
21582154
Expected: []sql.Row{{0}},
21592155
},
@@ -2166,22 +2162,16 @@ var FunctionQueryTests = []QueryTest{
21662162
Expected: []sql.Row{{"0000-01-01"}},
21672163
},
21682164
{
2169-
Query: "select date('0000-00-00')",
2170-
Expected: []sql.Row{{nil}},
2171-
ExpectedWarning: mysql.ERTruncatedWrongValue,
2172-
ExpectedWarningsCount: 1,
2165+
Query: "select date('0000-00-00')",
2166+
Expected: []sql.Row{{"0000-00-00"}},
21732167
},
21742168
{
2175-
Query: "select date(0)",
2176-
Expected: []sql.Row{{nil}},
2177-
ExpectedWarning: mysql.ERTruncatedWrongValue,
2178-
ExpectedWarningsCount: 1,
2169+
Query: "select date(0)",
2170+
Expected: []sql.Row{{"0000-00-00"}},
21792171
},
21802172
{
2181-
Query: "select date(false)",
2182-
Expected: []sql.Row{{nil}},
2183-
ExpectedWarning: mysql.ERTruncatedWrongValue,
2184-
ExpectedWarningsCount: 1,
2173+
Query: "select date(false)",
2174+
Expected: []sql.Row{{"0000-00-00"}},
21852175
},
21862176
{
21872177
Query: "select date(true)",

sql/expression/function/time.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1309,10 +1309,13 @@ func (d *Date) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
13091309
}
13101310

13111311
dateTime, ok := date.(time.Time)
1312-
if !ok || dateTime.Equal(types.ZeroTime) {
1312+
if !ok {
13131313
ctx.Warn(1292, "%s", types.ErrConvertingToTime.New(dateVal).Error())
13141314
return nil, nil
13151315
}
1316+
if dateTime.Equal(types.ZeroTime) {
1317+
return types.ZeroDateStr, nil
1318+
}
13161319

13171320
return dateTime.Format("2006-01-02"), nil
13181321
}

0 commit comments

Comments
 (0)