Skip to content

Commit e4f775b

Browse files
committed
fix date function
1 parent 4638265 commit e4f775b

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

enginetest/queries/function_queries.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,12 +2106,28 @@ var FunctionQueryTests = []QueryTest{
21062106
Expected: []sql.Row{{"0000-01-01"}},
21072107
},
21082108
{
2109-
Query: "select date('0000-00-00')",
2110-
Expected: []sql.Row{{"0000-00-00"}},
2109+
Query: "select date('0000-00-00')",
2110+
Expected: []sql.Row{{nil}},
2111+
ExpectedWarning: mysql.ERTruncatedWrongValue,
2112+
ExpectedWarningsCount: 1,
2113+
},
2114+
{
2115+
Query: "select date(0)",
2116+
Expected: []sql.Row{{nil}},
2117+
ExpectedWarning: mysql.ERTruncatedWrongValue,
2118+
ExpectedWarningsCount: 1,
21112119
},
21122120
{
2113-
Query: "select date(0)",
2114-
Expected: []sql.Row{{"0000-00-00"}},
2121+
Query: "select date(false)",
2122+
Expected: []sql.Row{{nil}},
2123+
ExpectedWarning: mysql.ERTruncatedWrongValue,
2124+
ExpectedWarningsCount: 1,
2125+
},
2126+
{
2127+
Query: "select date(true)",
2128+
Expected: []sql.Row{{nil}},
2129+
ExpectedWarning: mysql.ERTruncatedWrongValue,
2130+
ExpectedWarningsCount: 1,
21152131
},
21162132
{
21172133
Query: "select extract(day from 0)",

sql/expression/function/time.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,20 +1295,26 @@ func (*Date) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID,
12951295

12961296
// Eval implements the Expression interface.
12971297
func (d *Date) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
1298-
return getDatePart(ctx, d.UnaryExpression, row, func(v interface{}) interface{} {
1299-
if v == nil {
1300-
return nil
1301-
}
1298+
dateVal, err := d.Child.Eval(ctx, row)
1299+
if err != nil {
1300+
return nil, err
1301+
}
13021302

1303-
date, ok := v.(time.Time)
1304-
if !ok {
1305-
return nil
1306-
}
1307-
if date.Equal(types.ZeroTime) {
1308-
return types.ZeroDateStr
1309-
}
1310-
return date.Format("2006-01-02")
1311-
})
1303+
date, err := getDate(ctx, dateVal)
1304+
if err != nil {
1305+
return nil, err
1306+
}
1307+
if date == nil {
1308+
return nil, nil
1309+
}
1310+
1311+
dateTime, ok := date.(time.Time)
1312+
if !ok || dateTime.Equal(types.ZeroTime) {
1313+
ctx.Warn(1292, "%s", types.ErrConvertingToTime.New(dateVal).Error())
1314+
return nil, nil
1315+
}
1316+
1317+
return dateTime.Format("2006-01-02"), nil
13121318
}
13131319

13141320
// WithChildren implements the Expression interface.

0 commit comments

Comments
 (0)