From d74397ec126a04d091470bf58ae5b4a7816b6f96 Mon Sep 17 00:00:00 2001 From: Lonng Date: Mon, 2 Sep 2019 20:56:08 +0800 Subject: [PATCH 1/2] expression: fix type infer of unaryMinus which should return ETDecimal if overflow int Signed-off-by: Lonng --- expression/builtin_op.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/expression/builtin_op.go b/expression/builtin_op.go index 3b27a09aa7853..3c30642d6b3e0 100644 --- a/expression/builtin_op.go +++ b/expression/builtin_op.go @@ -642,16 +642,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 @@ -666,7 +665,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() { From ba7120c587715f1a1273e02108387b00b0efe7d0 Mon Sep 17 00:00:00 2001 From: Lonng Date: Mon, 2 Sep 2019 21:14:00 +0800 Subject: [PATCH 2/2] add integration tests Signed-off-by: Lonng --- expression/integration_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/expression/integration_test.go b/expression/integration_test.go index 86962d6de8b0f..5b489059ec1cd 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -2080,6 +2080,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 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) {