Skip to content

Commit ac8c541

Browse files
qw4990ti-chi-bot
authored andcommitted
This is an automated cherry-pick of pingcap#53981
Signed-off-by: ti-chi-bot <[email protected]>
1 parent 4f5d583 commit ac8c541

File tree

3 files changed

+68
-9
lines changed

3 files changed

+68
-9
lines changed

pkg/executor/union_scan_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,54 @@ func TestUnionScanForMemBufferReader(t *testing.T) {
183183
}
184184
}
185185

186+
func TestIssue53951(t *testing.T) {
187+
store := testkit.CreateMockStore(t)
188+
tk := testkit.NewTestKit(t, store)
189+
tk.MustExec("use test")
190+
tk.MustExec(`CREATE TABLE gholla_dummy1 (
191+
id varchar(10) NOT NULL,
192+
mark int,
193+
deleted_at datetime(3) NOT NULL DEFAULT '1970-01-01 01:00:01.000',
194+
account_id varchar(10) NOT NULL,
195+
metastore_id varchar(10) NOT NULL,
196+
is_deleted tinyint(1) GENERATED ALWAYS AS ((deleted_at > _utf8mb4'1970-01-01 01:00:01.000')) VIRTUAL NOT NULL,
197+
PRIMARY KEY (account_id,metastore_id,id),
198+
KEY isDeleted_accountId_metastoreId (is_deleted,account_id,metastore_id)
199+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;`)
200+
tk.MustExec(`CREATE TABLE gholla_dummy2 (
201+
id varchar(10) NOT NULL,
202+
mark int,
203+
deleted_at datetime(3) NOT NULL DEFAULT '1970-01-01 01:00:01.000',
204+
account_id varchar(10) NOT NULL,
205+
metastore_id varchar(10) NOT NULL,
206+
is_deleted tinyint(1) GENERATED ALWAYS AS ((deleted_at > _utf8mb4'1970-01-01 01:00:01.000')) VIRTUAL NOT NULL,
207+
PRIMARY KEY (account_id,metastore_id,id),
208+
KEY isDeleted_accountId_metastoreId (is_deleted,account_id,metastore_id)
209+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; `)
210+
tk.MustExec(`INSERT INTO gholla_dummy1 (id,mark,deleted_at,account_id,metastore_id) VALUES ('ABC', 1, '1970-01-01 01:00:01.000', 'ABC', 'ABC');`)
211+
tk.MustExec(`INSERT INTO gholla_dummy2 (id,mark,deleted_at,account_id,metastore_id) VALUES ('ABC', 1, '1970-01-01 01:00:01.000', 'ABC', 'ABC');`)
212+
tk.MustExec(`start transaction;`)
213+
tk.MustExec(`update gholla_dummy2 set deleted_at = NOW(), mark=2 where account_id = 'ABC' and metastore_id = 'ABC' and id = 'ABC';`)
214+
tk.MustQuery(`select
215+
/*+ INL_JOIN(g1, g2) */
216+
g1.account_id,
217+
g2.mark
218+
from
219+
gholla_dummy1 g1 FORCE INDEX(isDeleted_accountId_metastoreId)
220+
STRAIGHT_JOIN
221+
gholla_dummy2 g2 FORCE INDEX (PRIMARY)
222+
ON
223+
g1.account_id = g2.account_id AND
224+
g1.metastore_id = g2.metastore_id AND
225+
g1.id = g2.id
226+
WHERE
227+
g1.account_id = 'ABC' AND
228+
g1.metastore_id = 'ABC' AND
229+
g1.is_deleted = FALSE AND
230+
g2.is_deleted = FALSE;`).Check(testkit.Rows()) // empty result, no error
231+
tk.MustExec(`rollback`)
232+
}
233+
186234
func TestIssue28073(t *testing.T) {
187235
store := testkit.CreateMockStore(t)
188236
tk := testkit.NewTestKit(t, store)

pkg/planner/core/rule_predicate_push_down.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,20 @@ func (p *LogicalSelection) PredicatePushDown(predicates []expression.Expression,
122122
return nil, child
123123
}
124124

125+
<<<<<<< HEAD
125126
// PredicatePushDown implements LogicalPlan PredicatePushDown interface.
126127
func (p *LogicalUnionScan) PredicatePushDown(predicates []expression.Expression, opt *logicalOptimizeOp) ([]expression.Expression, LogicalPlan) {
127128
retainedPredicates, _ := p.children[0].PredicatePushDown(predicates, opt)
129+
=======
130+
// PredicatePushDown implements base.LogicalPlan PredicatePushDown interface.
131+
func (p *LogicalUnionScan) PredicatePushDown(predicates []expression.Expression, opt *optimizetrace.LogicalOptimizeOp) ([]expression.Expression, base.LogicalPlan) {
132+
if expression.ContainVirtualColumn(predicates) {
133+
// predicates with virtual columns can't be pushed down to TiKV/TiFlash so they'll be put into a Projection
134+
// below the UnionScan, but the current UnionScan doesn't support placing Projection below it, see #53951.
135+
return predicates, p
136+
}
137+
retainedPredicates, _ := p.Children()[0].PredicatePushDown(predicates, opt)
138+
>>>>>>> d53b34412f0 (planner: prevent pushing Projection with virtual columns down to UnionScan (#53981))
128139
p.conditions = make([]expression.Expression, 0, len(predicates))
129140
p.conditions = append(p.conditions, predicates...)
130141
// The conditions in UnionScan is only used for added rows, so parent Selection should not be removed.

tests/integrationtest/r/explain_generate_column_substitute.result

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -591,15 +591,15 @@ begin;
591591
delete from t2 where c_decimal > c_double/2 order by c_int, c_str, c_double, c_decimal limit 1;
592592
desc format='brief' select t2.c_enum from t2,t1 where t1.c_int - 1 = t2.c_int - 1 order by t2.c_enum;
593593
id estRows task access object operator info
594-
Sort 12487.50 root explain_generate_column_substitute.t2.c_enum
595-
└─HashJoin 12487.50 root inner join, equal:[eq(minus(explain_generate_column_substitute.t1.c_int, 1), minus(explain_generate_column_substitute.t2.c_int, 1))]
596-
├─IndexReader(Build) 9990.00 root index:IndexFullScan
597-
│ └─IndexFullScan 9990.00 cop[tikv] table:t1, index:expression_index_2(`c_int` - 1) keep order:false, stats:pseudo
598-
└─Projection(Probe) 10000.00 root explain_generate_column_substitute.t2.c_enum, minus(explain_generate_column_substitute.t2.c_int, 1), explain_generate_column_substitute.t2._tidb_rowid
599-
└─UnionScan 8000.00 root not(isnull(minus(explain_generate_column_substitute.t2.c_int, 1)))
600-
└─Selection 8000.00 root not(isnull(minus(explain_generate_column_substitute.t2.c_int, 1)))
601-
└─TableReader 10000.00 root data:TableFullScan
602-
└─TableFullScan 10000.00 cop[tikv] table:t2 keep order:false, stats:pseudo
594+
Sort 10000.00 root explain_generate_column_substitute.t2.c_enum
595+
└─HashJoin 10000.00 root inner join, equal:[eq(minus(explain_generate_column_substitute.t1.c_int, 1), minus(explain_generate_column_substitute.t2.c_int, 1))]
596+
├─Selection(Build) 8000.00 root not(isnull(minus(explain_generate_column_substitute.t2.c_int, 1)))
597+
│ └─Projection 10000.00 root explain_generate_column_substitute.t2.c_enum, minus(explain_generate_column_substitute.t2.c_int, 1), explain_generate_column_substitute.t2._tidb_rowid
598+
│ └─UnionScan 10000.00 root
599+
│ └─TableReader 10000.00 root data:TableFullScan
600+
└─TableFullScan 10000.00 cop[tikv] table:t2 keep order:false, stats:pseudo
601+
└─IndexReader(Probe) 9990.00 root index:IndexFullScan
602+
└─IndexFullScan 9990.00 cop[tikv] table:t1, index:expression_index_2(`c_int` - 1) keep order:false, stats:pseudo
603603
select t2.c_enum from t2,t1 where t1.c_int - 1 = t2.c_int - 1 order by t2.c_enum;
604604
c_enum
605605
orange

0 commit comments

Comments
 (0)