Skip to content

Commit 2348c75

Browse files
SunRunAwayzz-jason
authored andcommitted
expression, types: fix Mod(%), Multiple(*), Minus(-) operators… (#11354)
1 parent ffe55b8 commit 2348c75

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

expression/integration_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2786,7 +2786,10 @@ func (s *testIntegrationSuite) TestArithmeticBuiltin(c *C) {
27862786
c.Assert(err, NotNil)
27872787
c.Assert(err.Error(), Equals, "[types:1690]BIGINT UNSIGNED value is out of range in '(18446744073709551615 - -1)'")
27882788
c.Assert(rs.Close(), IsNil)
2789+
tk.MustQuery(`select cast(-3 as unsigned) - cast(-1 as signed);`).Check(testkit.Rows("18446744073709551614"))
2790+
tk.MustQuery("select 1.11 - 1.11;").Check(testkit.Rows("0.00"))
27892791

2792+
// for multiply
27902793
tk.MustQuery("select 1234567890 * 1234567890").Check(testkit.Rows("1524157875019052100"))
27912794
rs, err = tk.Exec("select 1234567890 * 12345671890")
27922795
c.Assert(err, IsNil)
@@ -2813,8 +2816,7 @@ func (s *testIntegrationSuite) TestArithmeticBuiltin(c *C) {
28132816
_, err = session.GetRows4Test(ctx, tk.Se, rs)
28142817
c.Assert(terror.ErrorEqual(err, types.ErrOverflow), IsTrue)
28152818
c.Assert(rs.Close(), IsNil)
2816-
result = tk.MustQuery(`select cast(-3 as unsigned) - cast(-1 as signed);`)
2817-
result.Check(testkit.Rows("18446744073709551614"))
2819+
tk.MustQuery("select 0.0 * -1;").Check(testkit.Rows("0.0"))
28182820

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

28882891
_, err = tk.Exec("INSERT INTO t VALUE(12 MOD 0);")
28892892
c.Assert(terror.ErrorEqual(err, expression.ErrDivisionByZero), IsTrue)

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ github.com/blacktear23/go-proxyprotocol v0.0.0-20180807104634-af7a81e8dd0d/go.mo
1313
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
1414
github.com/chzyer/readline v0.0.0-20171208011716-f6d7a1f6fbf3/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
1515
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
16+
github.com/client9/misspell v0.3.4 h1:ta993UF76GwbvJcIo3Y68y/M3WxlpEHPWIGDkJYwzJI=
1617
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
1718
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd h1:qMd81Ts1T2OTKmB4acZcyKaMtRnY5Y44NuXGX2GFJ1w=
1819
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=

types/mydecimal.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ var (
107107
zeroMyDecimal = MyDecimal{}
108108
)
109109

110+
// get the zero of MyDecimal with the specified result fraction digits
111+
func zeroMyDecimalWithFrac(frac int8) MyDecimal {
112+
zero := MyDecimal{}
113+
zero.digitsFrac = frac
114+
zero.resultFrac = frac
115+
return zero
116+
}
117+
110118
// add adds a and b and carry, returns the sum and new carry.
111119
func add(a, b, carry int32) (int32, int32) {
112120
sum := a + b + carry
@@ -1556,7 +1564,7 @@ func doSub(from1, from2, to *MyDecimal) (cmp int, err error) {
15561564
if to == nil {
15571565
return 0, nil
15581566
}
1559-
*to = zeroMyDecimal
1567+
*to = zeroMyDecimalWithFrac(to.resultFrac)
15601568
return 0, nil
15611569
}
15621570
}
@@ -1911,7 +1919,7 @@ func DecimalMul(from1, from2, to *MyDecimal) error {
19111919
idx++
19121920
/* We got decimal zero */
19131921
if idx == end {
1914-
*to = zeroMyDecimal
1922+
*to = zeroMyDecimalWithFrac(to.resultFrac)
19151923
break
19161924
}
19171925
}
@@ -2010,7 +2018,7 @@ func doDivMod(from1, from2, to, mod *MyDecimal, fracIncr int) error {
20102018
}
20112019
if prec1 <= 0 {
20122020
/* short-circuit everything: from1 == 0 */
2013-
*to = zeroMyDecimal
2021+
*to = zeroMyDecimalWithFrac(to.resultFrac)
20142022
return nil
20152023
}
20162024
prec1 -= countLeadingZeroes((prec1-1)%digitsPerWord, from1.wordBuf[idx1])

types/mydecimal_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,7 @@ func (s *testMyDecimalSuite) TestAdd(c *C) {
694694
{"-123.45", "12345", "12221.55", nil},
695695
{"5", "-6.0", "-1.0", nil},
696696
{"2" + strings.Repeat("1", 71), strings.Repeat("8", 81), "8888888890" + strings.Repeat("9", 71), nil},
697+
{"-1234.1234", "1234.1234", "0.0000", nil},
697698
}
698699
for _, tt := range tests {
699700
a := NewDecFromStringForTest(tt.a)
@@ -718,7 +719,7 @@ func (s *testMyDecimalSuite) TestSub(c *C) {
718719
{"1234500009876.5", ".00012345000098765", "1234500009876.49987654999901235", nil},
719720
{"9999900000000.5", ".555", "9999899999999.945", nil},
720721
{"1111.5551", "1111.555", "0.0001", nil},
721-
{".555", ".555", "0", nil},
722+
{".555", ".555", "0.000", nil},
722723
{"10000000", "1", "9999999", nil},
723724
{"1000001000", ".1", "1000000999.9", nil},
724725
{"1000000000", ".1", "999999999.9", nil},
@@ -728,6 +729,7 @@ func (s *testMyDecimalSuite) TestSub(c *C) {
728729
{"-123.45", "-12345", "12221.55", nil},
729730
{"-12345", "123.45", "-12468.45", nil},
730731
{"12345", "-123.45", "12468.45", nil},
732+
{"12.12", "12.12", "0.00", nil},
731733
}
732734
for _, tt := range tests {
733735
var a, b, sum MyDecimal
@@ -759,6 +761,7 @@ func (s *testMyDecimalSuite) TestMul(c *C) {
759761
{"1" + strings.Repeat("0", 60), "1" + strings.Repeat("0", 60), "0", ErrOverflow},
760762
{"0.5999991229316", "0.918755041726043", "0.5512522192246113614062276588", nil},
761763
{"0.5999991229317", "0.918755041726042", "0.5512522192247026369112773314", nil},
764+
{"0.000", "-1", "0.000", nil},
762765
}
763766
for _, tt := range tests {
764767
var a, b, product MyDecimal
@@ -786,7 +789,7 @@ func (s *testMyDecimalSuite) TestDivMod(c *C) {
786789
{"0", "0", "", ErrDivByZero},
787790
{"-12193185.1853376", "98765.4321", "-123.456000000000000000", nil},
788791
{"121931851853376", "987654321", "123456.000000000", nil},
789-
{"0", "987", "0", nil},
792+
{"0", "987", "0.00000", nil},
790793
{"1", "3", "0.333333333", nil},
791794
{"1.000000000000", "3", "0.333333333333333333", nil},
792795
{"1", "1", "1.000000000", nil},
@@ -799,7 +802,7 @@ func (s *testMyDecimalSuite) TestDivMod(c *C) {
799802
var a, b, to MyDecimal
800803
a.FromString([]byte(tt.a))
801804
b.FromString([]byte(tt.b))
802-
err := doDivMod(&a, &b, &to, nil, 5)
805+
err := DecimalDiv(&a, &b, &to, 5)
803806
c.Check(err, Equals, tt.err)
804807
if tt.err == ErrDivByZero {
805808
continue
@@ -816,12 +819,13 @@ func (s *testMyDecimalSuite) TestDivMod(c *C) {
816819
{"99999999999999999999999999999999999999", "3", "0", nil},
817820
{"51", "0.003430", "0.002760", nil},
818821
{"0.0000000001", "1.0", "0.0000000001", nil},
822+
{"0.000", "0.1", "0.000", nil},
819823
}
820824
for _, tt := range tests {
821825
var a, b, to MyDecimal
822826
a.FromString([]byte(tt.a))
823827
b.FromString([]byte(tt.b))
824-
ec := doDivMod(&a, &b, nil, &to, 0)
828+
ec := DecimalMod(&a, &b, &to)
825829
c.Check(ec, Equals, tt.err)
826830
if tt.err == ErrDivByZero {
827831
continue
@@ -836,6 +840,7 @@ func (s *testMyDecimalSuite) TestDivMod(c *C) {
836840
{"1", "1.000", "1.0000", nil},
837841
{"2", "3", "0.6667", nil},
838842
{"51", "0.003430", "14868.8047", nil},
843+
{"0.000", "0.1", "0.0000000", nil},
839844
}
840845
for _, tt := range tests {
841846
var a, b, to MyDecimal

0 commit comments

Comments
 (0)