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
*: add builtin function FLOOR #2484
Conversation
502a9b4
to
fd0dea4
Compare
Good job. |
@@ -237,6 +237,7 @@ import ( | |||
events "EVENTS" | |||
fieldKwd "FIELD_KWD" | |||
findInSet "FIND_IN_SET" | |||
floor "FLOOR" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use tab to make the code align.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DONE
@@ -2684,6 +2685,10 @@ FunctionCallNonKeyword: | |||
{ | |||
$$ = &ast.FuncCallExpr{FnName: model.NewCIStr($1), Args: []ast.ExprNode{$3.(ast.ExprNode)}} | |||
} | |||
| "FLOOR" '(' Expression ')' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use tab to make the code align.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DONE
@@ -274,7 +274,7 @@ func (v *typeInferrer) handleFuncCallExpr(x *ast.FuncCallExpr) { | |||
} | |||
case "interval": | |||
tp = types.NewFieldType(mysql.TypeLonglong) | |||
case "ceil", "ceiling": | |||
case "ceil", "ceiling", "floor": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a test case in plan/typeinferer_test.go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried. But I could not find ceil in this test file. This is kinda confused me. If I add test for floor, then so does ceil and ceiling.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DONE. Let me know, if you want me to add test case for ceil and ceiling.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! But you could do this in another PR.
return args[0], nil | ||
} | ||
|
||
f, err := args[0].ToFloat64(ctx.GetSessionVars().StmtCtx) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check the result of floor("abc") in MySQL and your implementation in TiDB. It seems ToFloat64 will cause an error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DONE
a878a6d
to
12f34ef
Compare
@@ -147,6 +147,7 @@ var Funcs = map[string]Func{ | |||
ast.Abs: {builtinAbs, 1, 1}, | |||
ast.Ceil: {builtinCeil, 1, 1}, | |||
ast.Ceiling: {builtinCeil, 1, 1}, | |||
ast.Floor: {builtinFloor, 1, 1}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also add this for this line: https://github.com/pingcap/tidb/pull/2484/files#diff-cdbd511e3e5f2bbe0a3c5c173d3938c2L298
return args[0], nil | ||
} | ||
if args[0].Kind() == types.KindString { | ||
args[0].SetFloat64(0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why set args[0] to 0, instead of d ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, This is outdated commit. Already changed in lasted commit.
@XuHuaiyu Passed all test cases. Ready to merge into mater. Already rebased and applied all my commit on top of lasted master branch. |
71267ae
to
c7142d4
Compare
@shenli PTAL, is this ok? |
5daa544
to
c7142d4
Compare
sc.IgnoreTruncate = false | ||
return d, errors.Trace(err) | ||
} | ||
sc.IgnoreTruncate = false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why change this? Maybe the original value of sc.IgnoreTruncate is true.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FLOOR(-1.b2) = -1
This will cause ErrTruncated error. Hence, sc.IgnoreTruncate has false value. I need to set sc.IgnoreTruncate to true in order to avoid ErrTruncated error which is came from getValidFloatPrefix(sc, str) in https://github.com/pingcap/tidb/blob/master/util/types/convert.go#L242
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe the original IgnoreTruncate
value is true. So you must record an old IgnoreTruncate
value
@@ -274,7 +274,7 @@ func (v *typeInferrer) handleFuncCallExpr(x *ast.FuncCallExpr) { | |||
} | |||
case "interval": | |||
tp = types.NewFieldType(mysql.TypeLonglong) | |||
case "ceil", "ceiling": | |||
case "ceil", "ceiling", "floor": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! But you could do this in another PR.
c7142d4
to
ee68dba
Compare
@XuHuaiyu PTAL, is this ok to you? |
ee68dba
to
f1aa04c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rest LGTM
@@ -33,6 +33,7 @@ import ( | |||
var ( | |||
_ functionClass = &absFunctionClass{} | |||
_ functionClass = &ceilFunctionClass{} | |||
_ functionClass = &floorFunctionClass{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add definitions of floorFunctionClass and builtinFloorSig at the beginning of this file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DONE @XuHuaiyu
825505f
to
b3490f3
Compare
PTAL |
@@ -49,6 +50,7 @@ var ( | |||
var ( | |||
_ builtinFunc = &builtinAbsSig{} | |||
_ builtinFunc = &builtinCeilSig{} | |||
_ builtinFunc = &builtinFloorSig{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also add for floorFunctionClass and add it into map funcs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DONE.
8d72fea
to
c0c89a3
Compare
@@ -309,6 +310,7 @@ var funcs = map[string]functionClass{ | |||
ast.Abs: &absFunctionClass{baseFunctionClass{ast.Abs, 1, 1}}, | |||
ast.Ceil: &ceilFunctionClass{baseFunctionClass{ast.Ceil, 1, 1}}, | |||
ast.Ceiling: &ceilFunctionClass{baseFunctionClass{ast.Ceiling, 1, 1}}, | |||
ast.Floor: &ceilFunctionClass{baseFunctionClass{ast.Floor, 1, 1}}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why use ceilFunctionClass?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad. I did it too fast.
Dtbl := tblToDtbl(tbl) | ||
|
||
for _, t := range Dtbl { | ||
v, err := builtinFloor(t["Arg"], s.ctx) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test it by getting function class from funcs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DONE.
c0c89a3
to
5732755
Compare
@zhexuany fix CI |
0c0b642
to
647d8fd
Compare
@hanfei1991 fixed. |
{-1.23, -2, IsNil}, | ||
{"1.23", 1, IsNil}, | ||
{"-1.23", -2, IsNil}, | ||
// {"-1.b23", -1, NotNil}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why comment these?
5492695
to
56e3f85
Compare
@hanfei1991 passed the CI and rewrite the test cases. Shall be all good now. |
*: address the test failure *: fixed indentation issue in parser.y *: added more test cases *: fixed truncated data error in floor *: fixed test case in plan related with function floor *: added builtin func *: adding floor into funs map *: changing test case style *: fixing CI
56e3f85
to
052dffb
Compare
@hanfei1991 @XuHuaiyu @shenli PTAL |
LGTM |
return d, errors.Trace(err) | ||
} | ||
|
||
sc.IgnoreTruncate = false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original IgnoreTruncate
may be true, so you need to store the old value as a local var.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DONE
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
A function wanted by Support more math functions #2420.