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 type infer of unaryMinus which should return ETDecimal if ETInt overflow int #11989

Merged
merged 3 commits into from
Sep 2, 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
7 changes: 3 additions & 4 deletions expression/builtin_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,16 +712,15 @@ func (c *unaryMinusFunctionClass) handleIntOverflow(arg *Constant) (overflow boo

// typeInfer infers unaryMinus function return type. when the arg is an int constant and overflow,
// typerInfer will infers the return type as types.ETDecimal, not types.ETInt.
func (c *unaryMinusFunctionClass) typeInfer(ctx sessionctx.Context, argExpr Expression) (types.EvalType, bool) {
func (c *unaryMinusFunctionClass) typeInfer(argExpr Expression) (types.EvalType, bool) {
tp := argExpr.GetType().EvalType()
if tp != types.ETInt && tp != types.ETDecimal {
tp = types.ETReal
}

sc := ctx.GetSessionVars().StmtCtx
overflow := false
// TODO: Handle float overflow.
if arg, ok := argExpr.(*Constant); sc.InSelectStmt && ok && tp == types.ETInt {
if arg, ok := argExpr.(*Constant); ok && tp == types.ETInt {
overflow = c.handleIntOverflow(arg)
if overflow {
tp = types.ETDecimal
Expand All @@ -736,7 +735,7 @@ func (c *unaryMinusFunctionClass) getFunction(ctx sessionctx.Context, args []Exp
}

argExpr, argExprTp := args[0], args[0].GetType()
_, intOverflow := c.typeInfer(ctx, argExpr)
_, intOverflow := c.typeInfer(argExpr)

var bf baseBuiltinFunc
switch argExprTp.EvalType() {
Expand Down
6 changes: 6 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2098,6 +2098,12 @@ func (s *testIntegrationSuite) TestOpBuiltin(c *C) {
// for unaryPlus
result = tk.MustQuery(`select +1, +0, +(-9), +(-0.001), +0.999, +null, +"aaa"`)
result.Check(testkit.Rows("1 0 -9 -0.001 0.999 <nil> aaa"))
// for unaryMinus
tk.MustExec("drop table if exists f")
tk.MustExec("create table f(a decimal(65,0))")
tk.MustExec("insert into f value (-17000000000000000000)")
result = tk.MustQuery("select a from f")
result.Check(testkit.Rows("-17000000000000000000"))
}

func (s *testIntegrationSuite) TestDatetimeOverflow(c *C) {
Expand Down