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: correct overflow check on function MINUS #4266

Merged
merged 5 commits into from Aug 22, 2017
Jump to file or symbol
Failed to load files and symbols.
+55 −29
Diff settings

Always

Just for now

@@ -39,7 +39,6 @@ var (
_ builtinFunc = &builtinArithmeticPlusIntSig{}
_ builtinFunc = &builtinArithmeticMinusRealSig{}
_ builtinFunc = &builtinArithmeticMinusDecimalSig{}
_ builtinFunc = &builtinArithmeticMinusIntUnsignedSig{}
_ builtinFunc = &builtinArithmeticMinusIntSig{}
_ builtinFunc = &builtinArithmeticDivideRealSig{}
_ builtinFunc = &builtinArithmeticDivideDecimalSig{}
@@ -283,22 +282,18 @@ func (c *arithmeticMinusFunctionClass) getFunction(args []Expression, ctx contex
if err != nil {
return nil, errors.Trace(err)
}
if mysql.HasUnsignedFlag(tpA.Flag) || mysql.HasUnsignedFlag(tpB.Flag) {
setFlenDecimal4Int(bf.tp, args[0].GetType(), args[1].GetType())
if mysql.HasUnsignedFlag(args[0].GetType().Flag) || mysql.HasUnsignedFlag(args[1].GetType().Flag) {
bf.tp.Flag |= mysql.UnsignedFlag
setFlenDecimal4Int(bf.tp, args[0].GetType(), args[1].GetType())
sig := &builtinArithmeticMinusIntUnsignedSig{baseIntBuiltinFunc{bf}}
return sig.setSelf(sig), nil
}
setFlenDecimal4Int(bf.tp, args[0].GetType(), args[1].GetType())
sig := &builtinArithmeticMinusIntSig{baseIntBuiltinFunc{bf}}
sig := &builtinArithmeticMinusIntSig{baseIntBuiltinFunc: baseIntBuiltinFunc{bf}}
return sig.setSelf(sig), nil
}
}
type builtinArithmeticMinusRealSig struct{ baseRealBuiltinFunc }
type builtinArithmeticMinusDecimalSig struct{ baseDecimalBuiltinFunc }
type builtinArithmeticMinusIntUnsignedSig struct{ baseIntBuiltinFunc }
type builtinArithmeticMinusIntSig struct{ baseIntBuiltinFunc }
type builtinArithmeticMinusRealSig struct {
baseRealBuiltinFunc
}
func (s *builtinArithmeticMinusRealSig) evalReal(row []types.Datum) (float64, bool, error) {
sc := s.ctx.GetSessionVars().StmtCtx
@@ -316,6 +311,10 @@ func (s *builtinArithmeticMinusRealSig) evalReal(row []types.Datum) (float64, bo
return a - b, false, nil
}
type builtinArithmeticMinusDecimalSig struct {
baseDecimalBuiltinFunc
}
func (s *builtinArithmeticMinusDecimalSig) evalDecimal(row []types.Datum) (*types.MyDecimal, bool, error) {
sc := s.ctx.GetSessionVars().StmtCtx
a, isNull, err := s.args[0].EvalDecimal(row, sc)
@@ -334,37 +333,48 @@ func (s *builtinArithmeticMinusDecimalSig) evalDecimal(row []types.Datum) (*type
return c, false, nil
}
func (s *builtinArithmeticMinusIntUnsignedSig) evalInt(row []types.Datum) (val int64, isNull bool, err error) {
sc := s.ctx.GetSessionVars().StmtCtx
a, isNull, err := s.args[0].EvalInt(row, sc)
if isNull || err != nil {
return 0, isNull, errors.Trace(err)
}
unsignedA := uint64(a)
b, isNull, err := s.args[1].EvalInt(row, sc)
if isNull || err != nil {
return 0, isNull, errors.Trace(err)
}
unsignedB := uint64(b)
if unsignedA < unsignedB {
return 0, true, types.ErrOverflow.GenByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%s - %s)", s.args[0].String(), s.args[1].String()))
}
return a - b, false, nil
type builtinArithmeticMinusIntSig struct {
baseIntBuiltinFunc
}
func (s *builtinArithmeticMinusIntSig) evalInt(row []types.Datum) (val int64, isNull bool, err error) {
sc := s.ctx.GetSessionVars().StmtCtx
a, isNull, err := s.args[0].EvalInt(row, sc)
if isNull || err != nil {
return 0, isNull, errors.Trace(err)
}
b, isNull, err := s.args[1].EvalInt(row, sc)
if isNull || err != nil {
return 0, isNull, errors.Trace(err)
}
if (a > 0 && -b > math.MaxInt64-a) || (a < 0 && -b < math.MinInt64-a) {
return 0, true, types.ErrOverflow.GenByArgs("BIGINT", fmt.Sprintf("(%s - %s)", s.args[0].String(), s.args[1].String()))
isLHSUnsigned := mysql.HasUnsignedFlag(s.args[0].GetType().Flag)
isRHSUnsigned := mysql.HasUnsignedFlag(s.args[1].GetType().Flag)
switch {
case isLHSUnsigned && isRHSUnsigned:
if uint64(a) < uint64(b) {
return 0, true, types.ErrOverflow.GenByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%s - %s)", s.args[0].String(), s.args[1].String()))
}
case isLHSUnsigned && !isRHSUnsigned:
if b >= 0 && uint64(a) < uint64(b) {
return 0, true, types.ErrOverflow.GenByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%s - %s)", s.args[0].String(), s.args[1].String()))
}
if b < 0 && uint64(a) > math.MaxUint64-uint64(-b) {
return 0, true, types.ErrOverflow.GenByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%s - %s)", s.args[0].String(), s.args[1].String()))
}
case !isLHSUnsigned && isRHSUnsigned:
if uint64(a-math.MinInt64) < uint64(b) {
return 0, true, types.ErrOverflow.GenByArgs("BIGINT UNSIGNED", fmt.Sprintf("(%s - %s)", s.args[0].String(), s.args[1].String()))
}
case !isLHSUnsigned && !isRHSUnsigned:
if (a > 0 && -b > math.MaxInt64-a) || (a < 0 && -b < math.MinInt64-a) {
return 0, true, types.ErrOverflow.GenByArgs("BIGINT", fmt.Sprintf("(%s - %s)", s.args[0].String(), s.args[1].String()))
}
}
return a - b, false, nil
}
@@ -1578,6 +1578,20 @@ func (s *testIntegrationSuite) TestArithmeticBuiltin(c *C) {
c.Assert(rows, IsNil)
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[types:1690]BIGINT UNSIGNED value is out of range in '(test.t.a - test.t.b)'")
rs, err = tk.Exec("select cast(-1 as signed) - cast(-1 as unsigned);")
c.Assert(errors.ErrorStack(err), Equals, "")
c.Assert(rs, NotNil)
rows, err = tidb.GetRows(rs)
c.Assert(rows, IsNil)
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[types:1690]BIGINT UNSIGNED value is out of range in '(-1 - 18446744073709551615)'")
rs, err = tk.Exec("select cast(-1 as unsigned) - cast(-1 as signed);")

This comment has been minimized.

@XuHuaiyu

XuHuaiyu Aug 21, 2017

Contributor

add test for
select cast(-3 as unsigned) - cast(-1 as signed);

@XuHuaiyu

XuHuaiyu Aug 21, 2017

Contributor

add test for
select cast(-3 as unsigned) - cast(-1 as signed);

c.Assert(errors.ErrorStack(err), Equals, "")
c.Assert(rs, NotNil)
rows, err = tidb.GetRows(rs)
c.Assert(rows, IsNil)
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[types:1690]BIGINT UNSIGNED value is out of range in '(18446744073709551615 - -1)'")
tk.MustQuery("select 1234567890 * 1234567890").Check(testkit.Rows("1524157875019052100"))
rs, err = tk.Exec("select 1234567890 * 12345671890")
@@ -1601,6 +1615,8 @@ func (s *testIntegrationSuite) TestArithmeticBuiltin(c *C) {
c.Assert(err, IsNil)
_, err = tidb.GetRows(rs)
c.Assert(terror.ErrorEqual(err, types.ErrOverflow), IsTrue)
result = tk.MustQuery(`select cast(-3 as unsigned) - cast(-1 as signed);`)
result.Check(testkit.Rows("18446744073709551614"))
tk.MustExec("DROP TABLE IF EXISTS t;")
tk.MustExec("CREATE TABLE t(a DECIMAL(4, 2), b DECIMAL(5, 3));")
ProTip! Use n and p to navigate between commits in a pull request.