Skip to content

Commit 1d0e586

Browse files
authored
planner: push necessary predicates without virtual column down through UnionScan (#54985) (#55152)
close #54870
1 parent b89e951 commit 1d0e586

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

executor/union_scan_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,20 @@ func TestUnionScanForMemBufferReader(t *testing.T) {
306306
tk.MustExec("admin check table t1;")
307307
}
308308

309+
func TestIssue54870(t *testing.T) {
310+
store := testkit.CreateMockStore(t)
311+
tk := testkit.NewTestKit(t, store)
312+
313+
tk.MustExec("use test")
314+
tk.MustExec(`create table t (id int,
315+
deleted_at datetime(3) NOT NULL DEFAULT '1970-01-01 01:00:01.000',
316+
is_deleted tinyint(1) GENERATED ALWAYS AS ((deleted_at > _utf8mb4'1970-01-01 01:00:01.000')) VIRTUAL NOT NULL,
317+
key k(id, is_deleted))`)
318+
tk.MustExec(`begin`)
319+
tk.MustExec(`insert into t (id, deleted_at) values (1, now())`)
320+
tk.MustHavePlan(`select 1 from t where id=1 and is_deleted=true`, "IndexRangeScan")
321+
}
322+
309323
func TestIssue53951(t *testing.T) {
310324
store := testkit.CreateMockStore(t)
311325
tk := testkit.NewTestKit(t, store)

planner/core/rule_predicate_push_down.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,22 @@ func (p *LogicalSelection) PredicatePushDown(predicates []expression.Expression,
123123

124124
// PredicatePushDown implements LogicalPlan PredicatePushDown interface.
125125
func (p *LogicalUnionScan) PredicatePushDown(predicates []expression.Expression, opt *logicalOptimizeOp) ([]expression.Expression, LogicalPlan) {
126-
if expression.ContainVirtualColumn(predicates) {
127-
// predicates with virtual columns can't be pushed down to TiKV/TiFlash so they'll be put into a Projection
128-
// below the UnionScan, but the current UnionScan doesn't support placing Projection below it, see #53951.
129-
return predicates, p
126+
var predicatesWithVCol, predicatesWithoutVCol []expression.Expression
127+
// predicates with virtual columns can't be pushed down to TiKV/TiFlash so they'll be put into a Projection
128+
// below the UnionScan, but the current UnionScan doesn't support placing Projection below it, see #53951.
129+
for _, expr := range predicates {
130+
if expression.ContainVirtualColumn([]expression.Expression{expr}) {
131+
predicatesWithVCol = append(predicatesWithVCol, expr)
132+
} else {
133+
predicatesWithoutVCol = append(predicatesWithoutVCol, expr)
134+
}
130135
}
131-
136+
predicates = predicatesWithoutVCol
132137
retainedPredicates, _ := p.children[0].PredicatePushDown(predicates, opt)
133138
p.conditions = make([]expression.Expression, 0, len(predicates))
134139
p.conditions = append(p.conditions, predicates...)
135140
// The conditions in UnionScan is only used for added rows, so parent Selection should not be removed.
141+
retainedPredicates = append(retainedPredicates, predicatesWithVCol...)
136142
return retainedPredicates, p
137143
}
138144

0 commit comments

Comments
 (0)