From cd9c65aad2567a89a677edf2c17876f57b3044f2 Mon Sep 17 00:00:00 2001 From: "Zhuomin(Charming) Liu" Date: Thu, 16 Jan 2020 16:20:22 +0800 Subject: [PATCH] planner: fix some select condition do not filter by tiflash (#14502) --- planner/core/find_best_task.go | 5 ++++ planner/core/integration_test.go | 38 ++++++++++++++++++++++++ planner/core/rule_predicate_push_down.go | 4 --- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/planner/core/find_best_task.go b/planner/core/find_best_task.go index 092fec4544..2142156993 100644 --- a/planner/core/find_best_task.go +++ b/planner/core/find_best_task.go @@ -1048,6 +1048,11 @@ func (ds *DataSource) convertToTableScan(prop *property.PhysicalProperty, candid func (ts *PhysicalTableScan) addPushedDownSelection(copTask *copTask, stats *property.StatsInfo) { ts.filterCondition, copTask.rootTaskConds = splitSelCondsWithVirtualColumn(ts.filterCondition) + if ts.StoreType == kv.TiFlash { + var newRootConds []expression.Expression + ts.filterCondition, newRootConds = expression.CheckExprPushFlash(ts.filterCondition) + copTask.rootTaskConds = append(copTask.rootTaskConds, newRootConds...) + } // Add filter condition to table plan now. sessVars := ts.ctx.GetSessionVars() diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go index 5eac3a29b0..ccd7a5a533 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -16,6 +16,7 @@ package core_test import ( . "github.com/pingcap/check" "github.com/pingcap/errors" + "github.com/pingcap/parser/model" "github.com/pingcap/parser/mysql" "github.com/pingcap/tidb/domain" "github.com/pingcap/tidb/kv" @@ -265,6 +266,43 @@ func (s *testIntegrationSuite) TestNoneAccessPathsFoundByIsolationRead(c *C) { c.Assert(err.Error(), Equals, "[planner:1815]Internal : Can not find access path matching 'tidb_isolation_read_engines'(value: 'tiflash'). Available values are 'tikv'.") } +func (s *testIntegrationSuite) TestSelPushDownTiFlash(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t") + tk.MustExec("create table t(a int primary key, b varchar(20))") + + // Create virtual tiflash replica info. + dom := domain.GetDomain(tk.Se) + is := dom.InfoSchema() + db, exists := is.SchemaByName(model.NewCIStr("test")) + c.Assert(exists, IsTrue) + for _, tblInfo := range db.Tables { + if tblInfo.Name.L == "t" { + tblInfo.TiFlashReplica = &model.TiFlashReplicaInfo{ + Count: 1, + Available: true, + } + } + } + + tk.MustExec("set @@session.tidb_isolation_read_engines = 'tiflash'") + + // All conditions should push tiflash. + tk.MustQuery(`explain select * from t where t.a > 1 and t.b = "flash" or t.a + 3 * t.a = 5`).Check(testkit.Rows( + "TableReader_7 8000.00 root data:Selection_6", + "└─Selection_6 8000.00 cop[tiflash] or(and(gt(test.t.a, 1), eq(test.t.b, \"flash\")), eq(plus(test.t.a, mul(3, test.t.a)), 5))", + " └─TableScan_5 10000.00 cop[tiflash] table:t, range:[-inf,+inf], keep order:false, stats:pseudo", + )) + + // Part of conditions should push tiflash. + tk.MustQuery(`explain select * from t where cast(t.a as float) + 3 = 5.1`).Check(testkit.Rows( + "Selection_7 10000.00 root eq(plus(cast(test.t.a), 3), 5.1)", + "└─TableReader_6 10000.00 root data:TableScan_5", + " └─TableScan_5 10000.00 cop[tiflash] table:t, range:[-inf,+inf], keep order:false, stats:pseudo", + )) +} + func (s *testIntegrationSuite) TestPartitionTableStats(c *C) { tk := testkit.NewTestKit(c, s.store) diff --git a/planner/core/rule_predicate_push_down.go b/planner/core/rule_predicate_push_down.go index 2e6e99d551..e8ae85e4ed 100644 --- a/planner/core/rule_predicate_push_down.go +++ b/planner/core/rule_predicate_push_down.go @@ -99,10 +99,6 @@ func (p *LogicalUnionScan) PredicatePushDown(predicates []expression.Expression) // PredicatePushDown implements LogicalPlan PredicatePushDown interface. func (ds *DataSource) PredicatePushDown(predicates []expression.Expression) ([]expression.Expression, LogicalPlan) { ds.allConds = predicates - if ds.preferStoreType&preferTiFlash != 0 { - ds.pushedDownConds, predicates = expression.CheckExprPushFlash(predicates) - return predicates, ds - } _, ds.pushedDownConds, predicates = expression.ExpressionsToPB(ds.ctx.GetSessionVars().StmtCtx, predicates, ds.ctx.GetClient()) return predicates, ds }