diff --git a/optimizer/typeinferer.go b/optimizer/typeinferer.go index e18b030c6a..813396f46e 100644 --- a/optimizer/typeinferer.go +++ b/optimizer/typeinferer.go @@ -258,6 +258,10 @@ func (v *typeInferrer) handleFuncCallExpr(x *ast.FuncCallExpr) { switch x.FnName.L { case "abs", "ifnull", "nullif": tp = x.Args[0].GetType() + // TODO: We should cover all types. + if x.FnName.L == "abs" && tp.Tp == mysql.TypeDatetime { + tp = types.NewFieldType(mysql.TypeDouble) + } case "pow", "power", "rand": tp = types.NewFieldType(mysql.TypeDouble) case "curdate", "current_date", "date": diff --git a/optimizer/typeinferer_test.go b/optimizer/typeinferer_test.go index 8bbd12272b..af044d6d00 100644 --- a/optimizer/typeinferer_test.go +++ b/optimizer/typeinferer_test.go @@ -82,6 +82,7 @@ func (ts *testTypeInferrerSuite) TestInferType(c *C) { {"count(c1)", mysql.TypeLonglong, charset.CharsetBin}, {"abs(1)", mysql.TypeLonglong, charset.CharsetBin}, {"abs(1.1)", mysql.TypeNewDecimal, charset.CharsetBin}, + {"abs(cast(\"20150817015609\" as DATETIME))", mysql.TypeDouble, charset.CharsetBin}, {"IF(1>2,2,3)", mysql.TypeLonglong, charset.CharsetBin}, {"IFNULL(1,0)", mysql.TypeLonglong, charset.CharsetBin}, {"POW(2,2)", mysql.TypeDouble, charset.CharsetBin},