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: handle ErrTruncated when dividing decimals in non-data-change stmts #14438

Merged
merged 9 commits into from
Feb 7, 2020
Merged
Show file tree
Hide file tree
Changes from 8 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
3 changes: 3 additions & 0 deletions expression/builtin_arithmetic.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,9 @@ func (s *builtinArithmeticDivideDecimalSig) evalDecimal(row chunk.Row) (*types.M
err = types.DecimalDiv(a, b, c, types.DivFracIncr)
if err == types.ErrDivByZero {
return c, true, handleDivisionByZeroError(s.ctx)
} else if err == types.ErrTruncated {
sc := s.ctx.GetSessionVars().StmtCtx
err = sc.HandleTruncate(errTruncatedWrongValue.GenWithStackByArgs("DECIMAL", c))
} else if err == nil {
_, frac := c.PrecisionAndFrac()
if frac < s.baseBuiltinFunc.tp.Decimal {
Expand Down
8 changes: 6 additions & 2 deletions expression/builtin_arithmetic_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (b *builtinArithmeticDivideDecimalSig) vecEvalDecimal(input *chunk.Chunk, r
y := buf.Decimals()
var to types.MyDecimal
var frac int
sc := b.ctx.GetSessionVars().StmtCtx
for i := 0; i < n; i++ {
if result.IsNull(i) {
continue
Expand All @@ -90,6 +91,10 @@ func (b *builtinArithmeticDivideDecimalSig) vecEvalDecimal(input *chunk.Chunk, r
}
result.SetNull(i, true)
continue
} else if err == types.ErrTruncated {
if err = sc.HandleTruncate(errTruncatedWrongValue.GenWithStackByArgs("DECIMAL", to)); err != nil {
return err
}
} else if err == nil {
_, frac = to.PrecisionAndFrac()
if frac < b.baseBuiltinFunc.tp.Decimal {
Expand Down Expand Up @@ -698,8 +703,7 @@ func (b *builtinArithmeticIntDivideDecimalSig) vecEvalInt(input *chunk.Chunk, re
}
if err == types.ErrTruncated {
err = sc.HandleTruncate(errTruncatedWrongValue.GenWithStackByArgs("DECIMAL", c))
}
if err == types.ErrOverflow {
} else if err == types.ErrOverflow {
newErr := errTruncatedWrongValue.GenWithStackByArgs("DECIMAL", c)
err = sc.HandleOverflow(newErr, newErr)
}
Expand Down
9 changes: 8 additions & 1 deletion expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3601,7 +3601,7 @@ func (s *testIntegrationSuite) TestAggregationBuiltinJSONObjectAgg(c *C) {
b varchar(100),
c decimal(3,2),
d json,
e date,
e date,
f time,
g datetime DEFAULT '2012-01-01',
h timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
Expand Down Expand Up @@ -4438,6 +4438,13 @@ func (s *testIntegrationSuite) TestDecimalMul(c *C) {
res.Check(testkit.Rows("0.55125221922461136"))
}

func (s *testIntegrationSuite) TestDecimalDiv(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustQuery("select cast(1 as decimal(60,30)) / cast(1 as decimal(60,30)) / cast(1 as decimal(60, 30))").Check(testkit.Rows("1.000000000000000000000000000000"))
tk.MustQuery("select cast(1 as decimal(60,30)) / cast(3 as decimal(60,30)) / cast(7 as decimal(60, 30))").Check(testkit.Rows("0.047619047619047619047619047619"))
tk.MustQuery("select cast(1 as decimal(60,30)) / cast(3 as decimal(60,30)) / cast(7 as decimal(60, 30)) / cast(13 as decimal(60, 30))").Check(testkit.Rows("0.003663003663003663003663003663"))
}

func (s *testIntegrationSuite) TestUnknowHintIgnore(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("USE test")
Expand Down