Skip to content

Commit

Permalink
planner: push necessary predicates without virtual column down throug…
Browse files Browse the repository at this point in the history
…h UnionScan (pingcap#54985)

close pingcap#54870
  • Loading branch information
qw4990 authored and hawkingrei committed Aug 1, 2024
1 parent f853157 commit 4cbfd7a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
14 changes: 14 additions & 0 deletions pkg/planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2237,6 +2237,20 @@ func TestIssue54213(t *testing.T) {
" └─IndexRangeScan_14 0.10 cop[tikv] table:tb, index:ab(a, b) range:[1 1,1 1], keep order:false, stats:pseudo"))
}

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 TestIssue52472(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
Expand Down
15 changes: 11 additions & 4 deletions pkg/planner/core/logical_union_scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,22 @@ func (p *LogicalUnionScan) ExplainInfo() string {

// PredicatePushDown implements base.LogicalPlan.<1st> interface.
func (p *LogicalUnionScan) PredicatePushDown(predicates []expression.Expression, opt *optimizetrace.LogicalOptimizeOp) ([]expression.Expression, base.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
}

Expand Down

0 comments on commit 4cbfd7a

Please sign in to comment.