From 30c4166ea78298268271e43642a483f7c352ab58 Mon Sep 17 00:00:00 2001 From: wjHuang Date: Tue, 26 May 2020 15:48:13 +0800 Subject: [PATCH 1/2] executor: set the DDL query string instead of `execute` (#17407) --- executor/adapter.go | 1 + session/session_test.go | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/executor/adapter.go b/executor/adapter.go index 934ed8793dedc..892dcf5fe02f8 100644 --- a/executor/adapter.go +++ b/executor/adapter.go @@ -718,6 +718,7 @@ func (a *ExecStmt) buildExecutor() (Executor, error) { if err != nil { return nil, err } + a.Ctx.SetValue(sessionctx.QueryString, executorExec.stmt.Text()) a.OutputNames = executorExec.outputNames a.isPreparedStmt = true a.Plan = executorExec.plan diff --git a/session/session_test.go b/session/session_test.go index 617e5a73bb5e4..92d723a51bab2 100644 --- a/session/session_test.go +++ b/session/session_test.go @@ -314,6 +314,18 @@ func (s *testSessionSuite) TestQueryString(c *C) { c.Assert(err, IsNil) qs := tk.Se.Value(sessionctx.QueryString) c.Assert(qs.(string), Equals, "CREATE TABLE t2(id bigint PRIMARY KEY, age int)") + + // Test execution of DDL through the "Execute" interface. + _, err = tk.Se.Execute(context.Background(), "use test;") + c.Assert(err, IsNil) + _, err = tk.Se.Execute(context.Background(), "drop table t2") + c.Assert(err, IsNil) + _, err = tk.Se.Execute(context.Background(), "prepare stmt from 'CREATE TABLE t2(id bigint PRIMARY KEY, age int)'") + c.Assert(err, IsNil) + _, err = tk.Se.Execute(context.Background(), "execute stmt") + c.Assert(err, IsNil) + qs = tk.Se.Value(sessionctx.QueryString) + c.Assert(qs.(string), Equals, "CREATE TABLE t2(id bigint PRIMARY KEY, age int)") } func (s *testSessionSuite) TestAffectedRows(c *C) { From f4ab49aeb4745836ad1e216567e4cdfbc6ed2015 Mon Sep 17 00:00:00 2001 From: wjhuang2016 Date: Fri, 18 Sep 2020 15:48:16 +0800 Subject: [PATCH 2/2] 1 Signed-off-by: wjhuang2016 --- expression/constant_fold.go | 2 +- expression/integration_test.go | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/expression/constant_fold.go b/expression/constant_fold.go index 83065bae9302f..84d3d1b2abe07 100644 --- a/expression/constant_fold.go +++ b/expression/constant_fold.go @@ -102,7 +102,7 @@ func caseWhenHandler(expr *ScalarFunction) (Expression, bool) { foldedExpr.GetType().Decimal = expr.GetType().Decimal return foldedExpr, isDeferredConst } - return BuildCastFunction(expr.GetCtx(), foldedExpr, foldedExpr.GetType()), isDeferredConst + return foldedExpr, isDeferredConst } } else { hasNonConstCondition = true diff --git a/expression/integration_test.go b/expression/integration_test.go index 238e038dcaeaa..7d990378df172 100755 --- a/expression/integration_test.go +++ b/expression/integration_test.go @@ -3507,6 +3507,7 @@ func (s *testIntegrationSuite) TestAggregationBuiltin(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 decimal(7, 6))") tk.MustExec("insert into t values(1.123456), (1.123456)") result := tk.MustQuery("select avg(a) from t") @@ -3533,6 +3534,19 @@ func (s *testIntegrationSuite) TestAggregationBuiltin(c *C) { result.Check(testkit.Rows("18446744073709551615")) } +func (s *testIntegrationSuite) Test19387(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("USE test;") + + tk.MustExec("drop table if exists t;") + tk.MustExec("create table t(a decimal(16, 2));") + tk.MustExec("select sum(case when 1 then a end) from t group by a;") + res := tk.MustQuery("show create table t") + c.Assert(len(res.Rows()), Equals, 1) + str := res.Rows()[0][1].(string) + c.Assert(strings.Contains(str, "decimal(16,2)"), IsTrue) +} + func (s *testIntegrationSuite) TestAggregationBuiltinBitOr(c *C) { defer s.cleanEnv(c) tk := testkit.NewTestKit(c, s.store)