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: fix wrong caseWhen function for enum type #29454

Merged
merged 3 commits into from
Nov 5, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions expression/builtin_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,14 @@ func (c *caseWhenFunctionClass) getFunction(ctx sessionctx.Context, args []Expre
return nil, err
}
bf.tp = fieldTp
if fieldTp.Tp == mysql.TypeEnum || fieldTp.Tp == mysql.TypeSet {
switch tp {
case types.ETInt:
fieldTp.Tp = mysql.TypeLonglong
case types.ETString:
fieldTp.Tp = mysql.TypeVarchar
}
}

switch tp {
case types.ETInt:
Expand Down
2 changes: 1 addition & 1 deletion expression/constant_fold.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,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
winoros marked this conversation as resolved.
Show resolved Hide resolved
}
return expr, isDeferredConst
}
Expand Down
13 changes: 13 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9937,6 +9937,19 @@ func (s *testIntegrationSuite) TestControlFunctionWithEnumOrSet(c *C) {
tk.MustExec("insert into t values(1,1,1),(2,1,1),(1,1,1),(2,1,1);")
tk.MustQuery("select if(A, null,b)=1 from t;").Check(testkit.Rows("<nil>", "<nil>", "<nil>", "<nil>"))
tk.MustQuery("select if(A, null,b)='a' from t;").Check(testkit.Rows("<nil>", "<nil>", "<nil>", "<nil>"))

// issue 29357
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t(`a` enum('y','b','Abc','null','1','2','0')) CHARSET=binary;")
tk.MustExec("insert into t values(\"1\");")
tk.MustQuery("SELECT count(*) from t where (null like 'a') = (case when cast('2015' as real) <=> round(\"1200\",\"1\") then a end);\n").Check(testkit.Rows("0"))
tk.MustQuery("SELECT (null like 'a') = (case when cast('2015' as real) <=> round(\"1200\",\"1\") then a end) from t;\n").Check(testkit.Rows("<nil>"))
tk.MustQuery("SELECT 5 = (case when 0 <=> 0 then a end) from t;").Check(testkit.Rows("1"))
tk.MustQuery("SELECT '1' = (case when 0 <=> 0 then a end) from t;").Check(testkit.Rows("1"))
tk.MustQuery("SELECT 5 = (case when 0 <=> 1 then a end) from t;").Check(testkit.Rows("<nil>"))
tk.MustQuery("SELECT '1' = (case when 0 <=> 1 then a end) from t;").Check(testkit.Rows("<nil>"))
tk.MustQuery("SELECT 5 = (case when 0 <=> 1 then a else a end) from t;").Check(testkit.Rows("1"))
tk.MustQuery("SELECT '1' = (case when 0 <=> 1 then a else a end) from t;").Check(testkit.Rows("1"))
}

func (s *testIntegrationSuite) TestComplexShowVariables(c *C) {
Expand Down