Skip to content

Commit a63a397

Browse files
yu34pozz-jason
authored andcommitted
support _tidb_rowid for table scan range (pingcap#8047)
1 parent e3a9c6d commit a63a397

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

cmd/explaintest/r/explain_easy.result

+24-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ Sort_13 2.00 root a:asc
390390
└─HashAgg_26 1.00 root group by:a, funcs:firstrow(a), firstrow(a)
391391
└─Projection_27 1.00 root 1
392392
└─TableDual_28 1.00 root rows:1
393-
explain SELECT 0 AS a FROM dual UNION (SELECT 1 AS a FROM dual ORDER BY a)
393+
explain SELECT 0 AS a FROM dual UNION (SELECT 1 AS a FROM dual ORDER BY a);
394394
id count task operator info
395395
HashAgg_15 2.00 root group by:a, funcs:firstrow(join_agg_0)
396396
└─Union_16 2.00 root
@@ -400,3 +400,26 @@ HashAgg_15 2.00 root group by:a, funcs:firstrow(join_agg_0)
400400
└─StreamAgg_26 1.00 root group by:a, funcs:firstrow(a), firstrow(a)
401401
└─Projection_31 1.00 root 1
402402
└─TableDual_32 1.00 root rows:1
403+
drop table if exists t;
404+
create table t(a int);
405+
explain select * from t where _tidb_rowid = 0;
406+
id count task operator info
407+
Projection_4 8000.00 root test.t.a
408+
└─TableReader_6 10000.00 root data:TableScan_5
409+
└─TableScan_5 10000.00 cop table:t, range:[0,0], keep order:false, stats:pseudo
410+
explain select * from t where _tidb_rowid > 0;
411+
id count task operator info
412+
Projection_4 8000.00 root test.t.a
413+
└─TableReader_6 10000.00 root data:TableScan_5
414+
└─TableScan_5 10000.00 cop table:t, range:(0,+inf], keep order:false, stats:pseudo
415+
explain select a, _tidb_rowid from t where a > 0;
416+
id count task operator info
417+
TableReader_7 3333.33 root data:Selection_6
418+
└─Selection_6 3333.33 cop gt(test.t.a, 0)
419+
└─TableScan_5 10000.00 cop table:t, range:[-inf,+inf], keep order:false, stats:pseudo
420+
explain select * from t where _tidb_rowid > 0 and a > 0;
421+
id count task operator info
422+
Projection_4 2666.67 root test.t.a
423+
└─TableReader_7 2666.67 root data:Selection_6
424+
└─Selection_6 2666.67 cop gt(test.t.a, 0)
425+
└─TableScan_5 3333.33 cop table:t, range:(0,+inf], keep order:false, stats:pseudo

cmd/explaintest/t/explain_easy.test

+8-1
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,11 @@ explain select * from ta where a = 1;
8282
rollback;
8383

8484
explain SELECT 0 AS a FROM dual UNION SELECT 1 AS a FROM dual ORDER BY a;
85-
explain SELECT 0 AS a FROM dual UNION (SELECT 1 AS a FROM dual ORDER BY a)
85+
explain SELECT 0 AS a FROM dual UNION (SELECT 1 AS a FROM dual ORDER BY a);
86+
87+
drop table if exists t;
88+
create table t(a int);
89+
explain select * from t where _tidb_rowid = 0;
90+
explain select * from t where _tidb_rowid > 0;
91+
explain select a, _tidb_rowid from t where a > 0;
92+
explain select * from t where _tidb_rowid > 0 and a > 0;

planner/core/logical_plans.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,10 @@ func (ds *DataSource) deriveTablePathStats(path *accessPath) (bool, error) {
340340
path.countAfterAccess = float64(ds.statisticTable.Count)
341341
path.tableFilters = ds.pushedDownConds
342342
var pkCol *expression.Column
343-
if ds.tableInfo.PKIsHandle {
343+
columnLen := len(ds.schema.Columns)
344+
if columnLen > 0 && ds.schema.Columns[columnLen-1].ID == model.ExtraHandleID {
345+
pkCol = ds.schema.Columns[columnLen-1]
346+
} else if ds.tableInfo.PKIsHandle {
344347
if pkColInfo := ds.tableInfo.GetPkColInfo(); pkColInfo != nil {
345348
pkCol = expression.ColInfo2Col(ds.schema.Columns, pkColInfo)
346349
}
@@ -349,6 +352,7 @@ func (ds *DataSource) deriveTablePathStats(path *accessPath) (bool, error) {
349352
path.ranges = ranger.FullIntRange(false)
350353
return false, nil
351354
}
355+
352356
path.ranges = ranger.FullIntRange(mysql.HasUnsignedFlag(pkCol.RetType.Flag))
353357
if len(ds.pushedDownConds) == 0 {
354358
return false, nil

0 commit comments

Comments
 (0)