Skip to content

Commit

Permalink
expression: Add warning info for exprs that can not be pushed to stor…
Browse files Browse the repository at this point in the history
…age layer (#22713)
  • Loading branch information
windtalker authored Feb 20, 2021
1 parent 381ac81 commit 3ad7f40
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 10 deletions.
10 changes: 10 additions & 0 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ func (s *testSuite5) TestShowErrors(c *C) {
tk.MustQuery("show errors").Check(testutil.RowsWithSep("|", "Error|1050|Table 'test.show_errors' already exists"))
}

func (s *testSuite5) TestShowWarningsForExprPushdown(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
testSQL := `create table if not exists show_warnings_expr_pushdown (a int, value date)`
tk.MustExec(testSQL)
tk.MustExec("explain select * from show_warnings_expr_pushdown where date_add(value, interval 1 day) = '2020-01-01'")
c.Assert(tk.Se.GetSessionVars().StmtCtx.WarningCount(), Equals, uint16(1))
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1105|Scalar function 'date_add'(signature: AddDateDatetimeInt) can not be pushed to tikv"))
}

func (s *testSuite5) TestShowGrantsPrivilege(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("create user show_grants")
Expand Down
10 changes: 10 additions & 0 deletions expression/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,13 @@ func canScalarFuncPushDown(scalarFunc *ScalarFunction, pc PbConverter, storeType

// Check whether this function can be pushed.
if !canFuncBePushed(scalarFunc, storeType) {
if pc.sc.InExplainStmt {
storageName := storeType.Name()
if storeType == kv.UnSpecified {
storageName = "storage layer"
}
pc.sc.AppendWarning(errors.New("Scalar function '" + scalarFunc.FuncName.L + "'(signature: " + scalarFunc.Function.PbCode().String() + ") can not be pushed to " + storageName))
}
return false
}

Expand All @@ -1196,6 +1203,9 @@ func canScalarFuncPushDown(scalarFunc *ScalarFunction, pc PbConverter, storeType

func canExprPushDown(expr Expression, pc PbConverter, storeType kv.StoreType) bool {
if storeType == kv.TiFlash && expr.GetType().Tp == mysql.TypeDuration {
if pc.sc.InExplainStmt {
pc.sc.AppendWarning(errors.New("Expr '" + expr.String() + "' can not be pushed to TiFlash because it contains Duration type"))
}
return false
}
switch x := expr.(type) {
Expand Down
17 changes: 11 additions & 6 deletions planner/core/physical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,7 @@ func (s *testPlanSuite) TestLimitToCopHint(c *C) {
output []struct {
SQL string
Plan []string
Warning string
Warning []string
}
)

Expand All @@ -991,15 +991,20 @@ func (s *testPlanSuite) TestLimitToCopHint(c *C) {
warnings := tk.Se.GetSessionVars().StmtCtx.GetWarnings()
s.testData.OnRecord(func() {
if len(warnings) > 0 {
output[i].Warning = warnings[0].Err.Error()
output[i].Warning = make([]string, len(warnings))
for j, warning := range warnings {
output[i].Warning[j] = warning.Err.Error()
}
}
})
if output[i].Warning == "" {
if len(output[i].Warning) == 0 {
c.Assert(len(warnings), Equals, 0, comment)
} else {
c.Assert(len(warnings), Equals, 1, comment)
c.Assert(warnings[0].Level, Equals, stmtctx.WarnLevelWarning, comment)
c.Assert(warnings[0].Err.Error(), Equals, output[i].Warning, comment)
c.Assert(len(warnings), Equals, len(output[i].Warning), comment)
for j, warning := range warnings {
c.Assert(warning.Level, Equals, stmtctx.WarnLevelWarning, comment)
c.Assert(warning.Err.Error(), Equals, output[i].Warning[j], comment)
}
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions planner/core/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package core
import (
"math"

"github.com/pingcap/errors"
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/charset"
"github.com/pingcap/parser/mysql"
Expand Down Expand Up @@ -1114,6 +1115,13 @@ func CheckAggCanPushCop(sctx sessionctx.Context, aggFuncs []*aggregation.AggFunc
return false
}
if !aggregation.CheckAggPushDown(aggFunc, storeType) {
if sc.InExplainStmt {
storageName := storeType.Name()
if storeType == kv.UnSpecified {
storageName = "storage layer"
}
sc.AppendWarning(errors.New("Agg function '" + aggFunc.Name + "' can not be pushed to " + storageName))
}
return false
}
if !expression.CanExprsPushDown(sc, aggFunc.Args, client, storeType) {
Expand Down
11 changes: 7 additions & 4 deletions planner/core/testdata/plan_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -1487,7 +1487,7 @@
" └─Selection_13 0.83 cop[tikv] gt(test.tn.c, 50)",
" └─IndexRangeScan_12 2.50 cop[tikv] table:tn, index:a(a, b, c, d) range:(1 10,1 20), keep order:false, stats:pseudo"
],
"Warning": ""
"Warning": null
},
{
"SQL": "select * from tn where a = 1 and b > 10 and b < 20 and c > 50 order by d limit 1",
Expand All @@ -1497,7 +1497,7 @@
" └─Selection_19 0.83 cop[tikv] gt(test.tn.c, 50)",
" └─IndexRangeScan_18 2.50 cop[tikv] table:tn, index:a(a, b, c, d) range:(1 10,1 20), keep order:false, stats:pseudo"
],
"Warning": ""
"Warning": null
},
{
"SQL": "select /*+ LIMIT_TO_COP() */ a from tn where mod(a, 2) order by a limit 1",
Expand All @@ -1507,7 +1507,10 @@
" └─IndexReader_21 1.00 root index:IndexFullScan_20",
" └─IndexFullScan_20 1.00 cop[tikv] table:tn, index:a(a, b, c, d) keep order:true, stats:pseudo"
],
"Warning": "[planner:1815]Optimizer Hint LIMIT_TO_COP is inapplicable"
"Warning": [
"Scalar function 'mod'(signature: ModInt) can not be pushed to storage layer",
"[planner:1815]Optimizer Hint LIMIT_TO_COP is inapplicable"
]
},
{
"SQL": "select /*+ LIMIT_TO_COP() */ a from tn where a > 10 limit 1",
Expand All @@ -1517,7 +1520,7 @@
" └─Limit_11 1.00 cop[tikv] offset:0, count:1",
" └─IndexRangeScan_10 1.00 cop[tikv] table:tn, index:a(a, b, c, d) range:(10,+inf], keep order:false, stats:pseudo"
],
"Warning": ""
"Warning": null
}
]
},
Expand Down

0 comments on commit 3ad7f40

Please sign in to comment.