Skip to content

Commit

Permalink
executor: fix ifnull bug when arg is enum/set (#25110) (#26035)
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-srebot authored Jul 14, 2021
1 parent 831cad0 commit 7874d60
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion planner/core/expression_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1617,9 +1617,13 @@ func (er *expressionRewriter) rewriteFuncCall(v *ast.FuncCallExpr) bool {
stackLen := len(er.ctxStack)
arg1 := er.ctxStack[stackLen-2]
col, isColumn := arg1.(*expression.Column)
var isEnumSet bool
if arg1.GetType().Tp == mysql.TypeEnum || arg1.GetType().Tp == mysql.TypeSet {
isEnumSet = true
}
// if expr1 is a column and column has not null flag, then we can eliminate ifnull on
// this column.
if isColumn && mysql.HasNotNullFlag(col.RetType.Flag) {
if isColumn && !isEnumSet && mysql.HasNotNullFlag(col.RetType.Flag) {
name := er.ctxNameStk[stackLen-2]
newCol := col.Clone().(*expression.Column)
er.ctxStackPop(len(v.Args))
Expand Down
10 changes: 10 additions & 0 deletions planner/core/expression_rewriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ func (s *testExpressionRewriterSuite) TestIfNullEliminateColName(c *C) {
c.Assert(err, IsNil)
fields := rs.Fields()
c.Assert(fields[0].Column.Name.L, Equals, "ifnull(a,b)")

tk.MustExec("drop table if exists t")
tk.MustExec("create table t(e int not null, b int)")
tk.MustExec("insert into t values(1, 1)")
tk.MustExec("create table t1(e int not null, b int)")
tk.MustExec("insert into t1 values(1, 1)")
rows := tk.MustQuery("select b from t where ifnull(e, b)")
rows.Check(testkit.Rows("1"))
rows = tk.MustQuery("select b from t1 where ifnull(e, b)")
rows.Check(testkit.Rows("1"))
}

func (s *testExpressionRewriterSuite) TestBinaryOpFunction(c *C) {
Expand Down

0 comments on commit 7874d60

Please sign in to comment.