From c54b3e280c4218412491d77446c05413fcef883e Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Thu, 29 Aug 2024 15:29:57 +0800 Subject: [PATCH] planner: push necessary predicates without virtual column down through UnionScan (#54985) (#55015) close pingcap/tidb#54870 --- planner/core/integration_test.go | 14 ++++++++++++++ planner/core/rule_predicate_push_down.go | 15 +++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go index df4ebdec44892..521e928a12edf 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -1440,6 +1440,20 @@ func TestKeepOrderHintWithBinding(t *testing.T) { require.Equal(t, len(res), 0) } +func TestIssue54870(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + + tk.MustExec("use test") + tk.MustExec(`create table t (id int, +deleted_at datetime(3) NOT NULL DEFAULT '1970-01-01 01:00:01.000', +is_deleted tinyint(1) GENERATED ALWAYS AS ((deleted_at > _utf8mb4'1970-01-01 01:00:01.000')) VIRTUAL NOT NULL, +key k(id, is_deleted))`) + tk.MustExec(`begin`) + tk.MustExec(`insert into t (id, deleted_at) values (1, now())`) + tk.MustHavePlan(`select 1 from t where id=1 and is_deleted=true`, "IndexRangeScan") +} + func TestViewHint(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) diff --git a/planner/core/rule_predicate_push_down.go b/planner/core/rule_predicate_push_down.go index c50d4c5e41a96..3f3cfc5131c9d 100644 --- a/planner/core/rule_predicate_push_down.go +++ b/planner/core/rule_predicate_push_down.go @@ -123,16 +123,23 @@ func (p *LogicalSelection) PredicatePushDown(predicates []expression.Expression, // PredicatePushDown implements LogicalPlan PredicatePushDown interface. func (p *LogicalUnionScan) PredicatePushDown(predicates []expression.Expression, opt *logicalOptimizeOp) ([]expression.Expression, LogicalPlan) { - if expression.ContainVirtualColumn(predicates) { - // predicates with virtual columns can't be pushed down to TiKV/TiFlash so they'll be put into a Projection - // below the UnionScan, but the current UnionScan doesn't support placing Projection below it, see #53951. - return predicates, p + var predicatesWithVCol, predicatesWithoutVCol []expression.Expression + // predicates with virtual columns can't be pushed down to TiKV/TiFlash so they'll be put into a Projection + // below the UnionScan, but the current UnionScan doesn't support placing Projection below it, see #53951. + for _, expr := range predicates { + if expression.ContainVirtualColumn([]expression.Expression{expr}) { + predicatesWithVCol = append(predicatesWithVCol, expr) + } else { + predicatesWithoutVCol = append(predicatesWithoutVCol, expr) + } } + predicates = predicatesWithoutVCol retainedPredicates, _ := p.children[0].PredicatePushDown(predicates, opt) p.conditions = make([]expression.Expression, 0, len(predicates)) p.conditions = append(p.conditions, predicates...) // The conditions in UnionScan is only used for added rows, so parent Selection should not be removed. + retainedPredicates = append(retainedPredicates, predicatesWithVCol...) return retainedPredicates, p }