Skip to content

Commit

Permalink
expression: deduce result type for multi-argument functions like IF
Browse files Browse the repository at this point in the history
… wrongly in some cases (#11605)
  • Loading branch information
qw4990 authored and sre-bot committed Aug 5, 2019
1 parent 3f3ead2 commit cb4b778
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
3 changes: 1 addition & 2 deletions expression/builtin_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ func InferType4ControlFuncs(lhs, rhs *types.FieldType) *types.FieldType {
} else if rhs.Tp == mysql.TypeNull {
*resultFieldType = *lhs
} else {
var unsignedFlag uint
evalType := types.AggregateEvalType([]*types.FieldType{lhs, rhs}, &unsignedFlag)
resultFieldType = types.AggFieldType([]*types.FieldType{lhs, rhs})
evalType := types.AggregateEvalType([]*types.FieldType{lhs, rhs}, &resultFieldType.Flag)
if evalType == types.ETInt {
resultFieldType.Decimal = 0
} else {
Expand Down
12 changes: 12 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4760,6 +4760,18 @@ func (s *testIntegrationSuite) TestFuncCaseWithLeftJoin(c *C) {
tk.MustQuery("select t1.id from kankan1 t1 left join kankan2 t2 on t1.id = t2.id where (case when t1.name='b' then 'case2' when t1.name='a' then 'case1' else NULL end) = 'case1' order by t1.id").Check(testkit.Rows("1", "2"))
}

func (s *testIntegrationSuite) TestIssue11594(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec(`drop table if exists t1;`)
tk.MustExec("CREATE TABLE t1 (v bigint(20) UNSIGNED NOT NULL);")
tk.MustExec("INSERT INTO t1 VALUES (1), (2);")
tk.MustQuery("SELECT SUM(IF(v > 1, v, -v)) FROM t1;").Check(testkit.Rows("1"))
tk.MustQuery("SELECT sum(IFNULL(cast(null+rand() as unsigned), -v)) FROM t1;").Check(testkit.Rows("-3"))
tk.MustQuery("SELECT sum(COALESCE(cast(null+rand() as unsigned), -v)) FROM t1;").Check(testkit.Rows("-3"))
tk.MustQuery("SELECT sum(COALESCE(cast(null+rand() as unsigned), v)) FROM t1;").Check(testkit.Rows("3"))
}

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

0 comments on commit cb4b778

Please sign in to comment.