Skip to content

Commit

Permalink
expression: fix a bug that DML using caseWhen may cause schema change (
Browse files Browse the repository at this point in the history
…#20095)

Signed-off-by: wjhuang2016 <huangwenjun1997@gmail.com>
  • Loading branch information
wjhuang2016 committed Sep 19, 2020
1 parent 6d71e24 commit 44314ac
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion expression/constant_fold.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,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 {
// for no-const, here should return directly, because the following branches are unknown to be run or not
Expand Down
14 changes: 14 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3592,6 +3592,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")
Expand All @@ -3618,6 +3619,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)
Expand Down

0 comments on commit 44314ac

Please sign in to comment.