Skip to content

Commit

Permalink
Merge branch 'release-3.0' into automated-cherry-pick-of-pingcap#11176-…
Browse files Browse the repository at this point in the history
…upstream-release-3.0
  • Loading branch information
SunRunAway authored Jul 22, 2019
2 parents 29ecd9d + 2348c75 commit 8420801
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
7 changes: 5 additions & 2 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2793,7 +2793,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 @@ -2820,8 +2823,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 @@ -2891,6 +2893,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
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ github.com/blacktear23/go-proxyprotocol v0.0.0-20180807104634-af7a81e8dd0d/go.mo
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20171208011716-f6d7a1f6fbf3/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/client9/misspell v0.3.4 h1:ta993UF76GwbvJcIo3Y68y/M3WxlpEHPWIGDkJYwzJI=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd h1:qMd81Ts1T2OTKmB4acZcyKaMtRnY5Y44NuXGX2GFJ1w=
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
Expand Down
14 changes: 11 additions & 3 deletions types/mydecimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,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 @@ -1556,7 +1564,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 @@ -1911,7 +1919,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 @@ -2010,7 +2018,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

0 comments on commit 8420801

Please sign in to comment.