Skip to content

Commit

Permalink
expression: fix wrong caseWhen function for enum type (#29454) (#29508)
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-srebot committed Nov 24, 2021
1 parent 0b69362 commit 820d3bb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
8 changes: 8 additions & 0 deletions expression/builtin_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,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 @@ -139,7 +139,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
}
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 @@ -8549,4 +8549,17 @@ func (s *testIntegrationSuite) TestControlFunctionWithEnumOrSet(c *C) {
tk.MustQuery("select elt(1,s) = 'a' from s").Check(testkit.Rows("1"))
tk.MustQuery("select elt(1,s) = 4 from s").Check(testkit.Rows("1"))
tk.MustQuery("select s from s where elt(1,s)").Check(testkit.Rows("a"))

tk.MustExec("use test")
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"))
}

0 comments on commit 820d3bb

Please sign in to comment.