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, types: fix Mod(%), Multiple(*), Minus(-) operators meets inconsistent 0 results. (#11251) #11353

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: 5 additions & 2 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2667,7 +2667,10 @@ func (s *testIntegrationSuite) TestArithmeticBuiltin(c *C) {
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[types:1690]BIGINT UNSIGNED value is out of range in '(18446744073709551615 - -1)'")
c.Assert(rs.Close(), IsNil)
tk.MustQuery(`select cast(-3 as unsigned) - cast(-1 as signed);`).Check(testkit.Rows("18446744073709551614"))
tk.MustQuery("select 1.11 - 1.11;").Check(testkit.Rows("0.00"))

// for multiply
tk.MustQuery("select 1234567890 * 1234567890").Check(testkit.Rows("1524157875019052100"))
rs, err = tk.Exec("select 1234567890 * 12345671890")
c.Assert(err, IsNil)
Expand All @@ -2694,8 +2697,7 @@ func (s *testIntegrationSuite) TestArithmeticBuiltin(c *C) {
_, err = session.GetRows4Test(ctx, tk.Se, rs)
c.Assert(terror.ErrorEqual(err, types.ErrOverflow), IsTrue)
c.Assert(rs.Close(), IsNil)
result = tk.MustQuery(`select cast(-3 as unsigned) - cast(-1 as signed);`)
result.Check(testkit.Rows("18446744073709551614"))
tk.MustQuery("select 0.0 * -1;").Check(testkit.Rows("0.0"))

tk.MustExec("DROP TABLE IF EXISTS t;")
tk.MustExec("CREATE TABLE t(a DECIMAL(4, 2), b DECIMAL(5, 3));")
Expand Down Expand Up @@ -2765,6 +2767,7 @@ func (s *testIntegrationSuite) TestArithmeticBuiltin(c *C) {
tk.MustExec("INSERT IGNORE INTO t VALUE(12 MOD 0);")
tk.MustQuery("show warnings;").Check(testkit.Rows("Warning 1365 Division by 0"))
tk.MustQuery("select v from t;").Check(testkit.Rows("<nil>"))
tk.MustQuery("select 0.000 % 0.11234500000000000000;").Check(testkit.Rows("0.00000000000000000000"))

_, err = tk.Exec("INSERT INTO t VALUE(12 MOD 0);")
c.Assert(terror.ErrorEqual(err, expression.ErrDivisionByZero), IsTrue)
Expand Down
14 changes: 11 additions & 3 deletions types/mydecimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ var (
zeroMyDecimal = MyDecimal{}
)

// get the zero of MyDecimal with the specified result fraction digits
func zeroMyDecimalWithFrac(frac int8) MyDecimal {
zero := MyDecimal{}
zero.digitsFrac = frac
zero.resultFrac = frac
return zero
}

// add adds a and b and carry, returns the sum and new carry.
func add(a, b, carry int32) (int32, int32) {
sum := a + b + carry
Expand Down Expand Up @@ -1528,7 +1536,7 @@ func doSub(from1, from2, to *MyDecimal) (cmp int, err error) {
if to == nil {
return 0, nil
}
*to = zeroMyDecimal
*to = zeroMyDecimalWithFrac(to.resultFrac)
return 0, nil
}
}
Expand Down Expand Up @@ -1883,7 +1891,7 @@ func DecimalMul(from1, from2, to *MyDecimal) error {
idx++
/* We got decimal zero */
if idx == end {
*to = zeroMyDecimal
*to = zeroMyDecimalWithFrac(to.resultFrac)
break
}
}
Expand Down Expand Up @@ -1982,7 +1990,7 @@ func doDivMod(from1, from2, to, mod *MyDecimal, fracIncr int) error {
}
if prec1 <= 0 {
/* short-circuit everything: from1 == 0 */
*to = zeroMyDecimal
*to = zeroMyDecimalWithFrac(to.resultFrac)
return nil
}
prec1 -= countLeadingZeroes((prec1-1)%digitsPerWord, from1.wordBuf[idx1])
Expand Down
13 changes: 9 additions & 4 deletions types/mydecimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,7 @@ func (s *testMyDecimalSuite) TestAdd(c *C) {
{"-123.45", "12345", "12221.55", nil},
{"5", "-6.0", "-1.0", nil},
{"2" + strings.Repeat("1", 71), strings.Repeat("8", 81), "8888888890" + strings.Repeat("9", 71), nil},
{"-1234.1234", "1234.1234", "0.0000", nil},
}
for _, tt := range tests {
a := NewDecFromStringForTest(tt.a)
Expand All @@ -718,7 +719,7 @@ func (s *testMyDecimalSuite) TestSub(c *C) {
{"1234500009876.5", ".00012345000098765", "1234500009876.49987654999901235", nil},
{"9999900000000.5", ".555", "9999899999999.945", nil},
{"1111.5551", "1111.555", "0.0001", nil},
{".555", ".555", "0", nil},
{".555", ".555", "0.000", nil},
{"10000000", "1", "9999999", nil},
{"1000001000", ".1", "1000000999.9", nil},
{"1000000000", ".1", "999999999.9", nil},
Expand All @@ -728,6 +729,7 @@ func (s *testMyDecimalSuite) TestSub(c *C) {
{"-123.45", "-12345", "12221.55", nil},
{"-12345", "123.45", "-12468.45", nil},
{"12345", "-123.45", "12468.45", nil},
{"12.12", "12.12", "0.00", nil},
}
for _, tt := range tests {
var a, b, sum MyDecimal
Expand Down Expand Up @@ -759,6 +761,7 @@ func (s *testMyDecimalSuite) TestMul(c *C) {
{"1" + strings.Repeat("0", 60), "1" + strings.Repeat("0", 60), "0", ErrOverflow},
{"0.5999991229316", "0.918755041726043", "0.5512522192246113614062276588", nil},
{"0.5999991229317", "0.918755041726042", "0.5512522192247026369112773314", nil},
{"0.000", "-1", "0.000", nil},
}
for _, tt := range tests {
var a, b, product MyDecimal
Expand Down Expand Up @@ -786,7 +789,7 @@ func (s *testMyDecimalSuite) TestDivMod(c *C) {
{"0", "0", "", ErrDivByZero},
{"-12193185.1853376", "98765.4321", "-123.456000000000000000", nil},
{"121931851853376", "987654321", "123456.000000000", nil},
{"0", "987", "0", nil},
{"0", "987", "0.00000", nil},
{"1", "3", "0.333333333", nil},
{"1.000000000000", "3", "0.333333333333333333", nil},
{"1", "1", "1.000000000", nil},
Expand All @@ -799,7 +802,7 @@ func (s *testMyDecimalSuite) TestDivMod(c *C) {
var a, b, to MyDecimal
a.FromString([]byte(tt.a))
b.FromString([]byte(tt.b))
err := doDivMod(&a, &b, &to, nil, 5)
err := DecimalDiv(&a, &b, &to, 5)
c.Check(err, Equals, tt.err)
if tt.err == ErrDivByZero {
continue
Expand All @@ -816,12 +819,13 @@ func (s *testMyDecimalSuite) TestDivMod(c *C) {
{"99999999999999999999999999999999999999", "3", "0", nil},
{"51", "0.003430", "0.002760", nil},
{"0.0000000001", "1.0", "0.0000000001", nil},
{"0.000", "0.1", "0.000", nil},
}
for _, tt := range tests {
var a, b, to MyDecimal
a.FromString([]byte(tt.a))
b.FromString([]byte(tt.b))
ec := doDivMod(&a, &b, nil, &to, 0)
ec := DecimalMod(&a, &b, &to)
c.Check(ec, Equals, tt.err)
if tt.err == ErrDivByZero {
continue
Expand All @@ -836,6 +840,7 @@ func (s *testMyDecimalSuite) TestDivMod(c *C) {
{"1", "1.000", "1.0000", nil},
{"2", "3", "0.6667", nil},
{"51", "0.003430", "14868.8047", nil},
{"0.000", "0.1", "0.0000000", nil},
}
for _, tt := range tests {
var a, b, to MyDecimal
Expand Down