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: avoid unnecessary warnings/errors when folding constants in shortcut-able expressions (#19797) #21040

Merged
merged 1 commit into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
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