Skip to content

Commit

Permalink
cherry pick pingcap#19797 to release-4.0
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
fzhedu authored and ti-srebot committed Nov 13, 2020
1 parent d4a0757 commit 60c0b2f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
10 changes: 7 additions & 3 deletions expression/function_traits.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,13 @@ var DisableFoldFunctions = map[string]struct{}{
// otherwise, the child functions do not fold constant.
// Note: the function itself should fold constant.
var TryFoldFunctions = map[string]struct{}{
ast.If: {},
ast.Ifnull: {},
ast.Case: {},
ast.If: {},
ast.Ifnull: {},
ast.Case: {},
ast.LogicAnd: {},
ast.LogicOr: {},
ast.Coalesce: {},
ast.Interval: {},
}

// IllegalFunctions4GeneratedColumns stores functions that is illegal for generated columns.
Expand Down
9 changes: 9 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2838,6 +2838,15 @@ func (s *testIntegrationSuite2) TestBuiltin(c *C) {
tk.MustQuery("select 1 or b/0 from t")
tk.MustQuery("show warnings").Check(testkit.Rows())

tk.MustQuery("select 1 or 1/0")
tk.MustQuery("show warnings").Check(testkit.Rows())
tk.MustQuery("select 0 and 1/0")
tk.MustQuery("show warnings").Check(testkit.Rows())
tk.MustQuery("select COALESCE(1, 1/0)")
tk.MustQuery("show warnings").Check(testkit.Rows())
tk.MustQuery("select interval(1,0,1,2,1/0)")
tk.MustQuery("show warnings").Check(testkit.Rows())

tk.MustQuery("select case 2.0 when 2.0 then 3.0 when 3.0 then 2.0 end").Check(testkit.Rows("3.0"))
tk.MustQuery("select case 2.0 when 3.0 then 2.0 when 4.0 then 3.0 else 5.0 end").Check(testkit.Rows("5.0"))
tk.MustQuery("select case cast('2011-01-01' as date) when cast('2011-01-01' as date) then cast('2011-02-02' as date) end").Check(testkit.Rows("2011-02-02"))
Expand Down
10 changes: 10 additions & 0 deletions planner/core/expression_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,19 +400,26 @@ func (er *expressionRewriter) Enter(inNode ast.Node) (ast.Node, bool) {
er.ctxStackAppend(er.schema.Columns[index], er.names[index])
return inNode, true
case *ast.FuncCallExpr:
er.asScalar = true
if _, ok := expression.DisableFoldFunctions[v.FnName.L]; ok {
er.disableFoldCounter++
}
if _, ok := expression.TryFoldFunctions[v.FnName.L]; ok {
er.tryFoldCounter++
}
case *ast.CaseExpr:
er.asScalar = true
if _, ok := expression.DisableFoldFunctions["case"]; ok {
er.disableFoldCounter++
}
if _, ok := expression.TryFoldFunctions["case"]; ok {
er.tryFoldCounter++
}
case *ast.BinaryOperationExpr:
er.asScalar = true
if v.Op == opcode.LogicAnd || v.Op == opcode.LogicOr {
er.tryFoldCounter++
}
case *ast.SetCollationExpr:
// Do nothing
default:
Expand Down Expand Up @@ -972,6 +979,9 @@ func (er *expressionRewriter) Leave(originInNode ast.Node) (retNode ast.Node, ok
case *ast.UnaryOperationExpr:
er.unaryOpToExpression(v)
case *ast.BinaryOperationExpr:
if v.Op == opcode.LogicAnd || v.Op == opcode.LogicOr {
er.tryFoldCounter--
}
er.binaryOpToExpression(v)
case *ast.BetweenExpr:
er.betweenToExpression(v)
Expand Down

0 comments on commit 60c0b2f

Please sign in to comment.