From 186c5ce09375122fdd61ef5389e9bef92cdb05a3 Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Fri, 7 Feb 2020 14:02:52 +0800 Subject: [PATCH 1/2] expression: handle ErrTruncated when dividing decimals in non-data-change stmts (#14438) --- expression/builtin_arithmetic.go | 3 ++ expression/integration_test.go | 49 ++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/expression/builtin_arithmetic.go b/expression/builtin_arithmetic.go index 20bcf0c7f5261..4a1f203a28843 100644 --- a/expression/builtin_arithmetic.go +++ b/expression/builtin_arithmetic.go @@ -644,6 +644,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 { diff --git a/expression/integration_test.go b/expression/integration_test.go index c1e475e75c0a0..6ed3fa8ae3334 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -3404,6 +3404,44 @@ func (s *testIntegrationSuite) TestAggregationBuiltinGroupConcat(c *C) { tk.MustQuery("select * from d").Check(testkit.Rows("hello,h")) } +func (s *testIntegrationSuite) TestAggregationBuiltinJSONObjectAgg(c *C) { + defer s.cleanEnv(c) + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + + tk.MustExec("drop table if exists t;") + tk.MustExec(`CREATE TABLE t ( + a int(11), + b varchar(100), + c decimal(3,2), + d json, + e date, + f time, + g datetime DEFAULT '2012-01-01', + h timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + i char(36), + j text(50));`) + + tk.MustExec(`insert into t values(1, 'ab', 5.5, '{"id": 1}', '2020-01-10', '11:12:13', '2020-01-11', '0000-00-00 00:00:00', 'first', 'json_objectagg_test');`) + + result := tk.MustQuery("select json_objectagg(a, b) from t group by a order by a;") + result.Check(testkit.Rows(`{"1": "ab"}`)) + result = tk.MustQuery("select json_objectagg(b, c) from t group by b order by b;") + result.Check(testkit.Rows(`{"ab": 5.5}`)) + result = tk.MustQuery("select json_objectagg(e, f) from t group by e order by e;") + result.Check(testkit.Rows(`{"2020-01-10": "11:12:13"}`)) + result = tk.MustQuery("select json_objectagg(f, g) from t group by f order by f;") + result.Check(testkit.Rows(`{"11:12:13": "2020-01-11 00:00:00"}`)) + result = tk.MustQuery("select json_objectagg(g, h) from t group by g order by g;") + result.Check(testkit.Rows(`{"2020-01-11 00:00:00": "0000-00-00 00:00:00"}`)) + result = tk.MustQuery("select json_objectagg(h, i) from t group by h order by h;") + result.Check(testkit.Rows(`{"0000-00-00 00:00:00": "first"}`)) + result = tk.MustQuery("select json_objectagg(i, j) from t group by i order by i;") + result.Check(testkit.Rows(`{"first": "json_objectagg_test"}`)) + result = tk.MustQuery("select json_objectagg(a, null) from t group by a order by a;") + result.Check(testkit.Rows(`{"1": null}`)) +} + func (s *testIntegrationSuite) TestOtherBuiltin(c *C) { defer s.cleanEnv(c) tk := testkit.NewTestKit(c, s.store) @@ -4142,6 +4180,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") @@ -4486,9 +4531,9 @@ from (select * from t1) a left join (select bussid,date(from_unixtime(ct)) date8 from t2) b -on +on a.period_id = b.bussid -where +where datediff(b.date8, date(from_unixtime(a.starttime))) >= 0` tk.MustQuery(q) } From 7c9e1a02f6c4fa60c81fc375c77b391de12ee36f Mon Sep 17 00:00:00 2001 From: Yuanjia Zhang Date: Fri, 7 Feb 2020 15:53:19 +0800 Subject: [PATCH 2/2] fixup --- expression/integration_test.go | 38 ---------------------------------- 1 file changed, 38 deletions(-) diff --git a/expression/integration_test.go b/expression/integration_test.go index 6ed3fa8ae3334..3efda42701a78 100644 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -3404,44 +3404,6 @@ func (s *testIntegrationSuite) TestAggregationBuiltinGroupConcat(c *C) { tk.MustQuery("select * from d").Check(testkit.Rows("hello,h")) } -func (s *testIntegrationSuite) TestAggregationBuiltinJSONObjectAgg(c *C) { - defer s.cleanEnv(c) - tk := testkit.NewTestKit(c, s.store) - tk.MustExec("use test") - - tk.MustExec("drop table if exists t;") - tk.MustExec(`CREATE TABLE t ( - a int(11), - b varchar(100), - c decimal(3,2), - d json, - e date, - f time, - g datetime DEFAULT '2012-01-01', - h timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - i char(36), - j text(50));`) - - tk.MustExec(`insert into t values(1, 'ab', 5.5, '{"id": 1}', '2020-01-10', '11:12:13', '2020-01-11', '0000-00-00 00:00:00', 'first', 'json_objectagg_test');`) - - result := tk.MustQuery("select json_objectagg(a, b) from t group by a order by a;") - result.Check(testkit.Rows(`{"1": "ab"}`)) - result = tk.MustQuery("select json_objectagg(b, c) from t group by b order by b;") - result.Check(testkit.Rows(`{"ab": 5.5}`)) - result = tk.MustQuery("select json_objectagg(e, f) from t group by e order by e;") - result.Check(testkit.Rows(`{"2020-01-10": "11:12:13"}`)) - result = tk.MustQuery("select json_objectagg(f, g) from t group by f order by f;") - result.Check(testkit.Rows(`{"11:12:13": "2020-01-11 00:00:00"}`)) - result = tk.MustQuery("select json_objectagg(g, h) from t group by g order by g;") - result.Check(testkit.Rows(`{"2020-01-11 00:00:00": "0000-00-00 00:00:00"}`)) - result = tk.MustQuery("select json_objectagg(h, i) from t group by h order by h;") - result.Check(testkit.Rows(`{"0000-00-00 00:00:00": "first"}`)) - result = tk.MustQuery("select json_objectagg(i, j) from t group by i order by i;") - result.Check(testkit.Rows(`{"first": "json_objectagg_test"}`)) - result = tk.MustQuery("select json_objectagg(a, null) from t group by a order by a;") - result.Check(testkit.Rows(`{"1": null}`)) -} - func (s *testIntegrationSuite) TestOtherBuiltin(c *C) { defer s.cleanEnv(c) tk := testkit.NewTestKit(c, s.store)