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: deduce result type for multi-argument functions like IF wrongly in some cases #11605

Merged
merged 3 commits into from Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions expression/builtin_control.go
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)
zz-jason marked this conversation as resolved.
Show resolved Hide resolved
if evalType == types.ETInt {
resultFieldType.Decimal = 0
} else {
Expand Down
12 changes: 12 additions & 0 deletions expression/integration_test.go
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"))
XuHuaiyu marked this conversation as resolved.
Show resolved Hide resolved
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