Skip to content

Commit a4cff40

Browse files
yu34powinoros
authored andcommitted
support _tidb_rowid for table scan range (#8047)
1 parent 1f57184 commit a4cff40

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

cmd/explaintest/r/explain_easy.result

+23
Original file line numberDiff line numberDiff line change
@@ -422,3 +422,26 @@ Projection_3 10000.00 root plus(1, ifnull(test.t.a, 0))
422422
└─TableScan_4 10000.00 cop table:t, range:[-inf,+inf], keep order:false, stats:pseudo
423423
drop table if exists t;
424424
drop table if exists t;
425+
create table t(a int);
426+
explain select * from t where _tidb_rowid = 0;
427+
id count task operator info
428+
Projection_4 8000.00 root test.t.a
429+
└─TableReader_6 10000.00 root data:TableScan_5
430+
└─TableScan_5 10000.00 cop table:t, range:[0,0], keep order:false, stats:pseudo
431+
explain select * from t where _tidb_rowid > 0;
432+
id count task operator info
433+
Projection_4 8000.00 root test.t.a
434+
└─TableReader_6 10000.00 root data:TableScan_5
435+
└─TableScan_5 10000.00 cop table:t, range:(0,+inf], keep order:false, stats:pseudo
436+
explain select a, _tidb_rowid from t where a > 0;
437+
id count task operator info
438+
TableReader_7 3333.33 root data:Selection_6
439+
└─Selection_6 3333.33 cop gt(test.t.a, 0)
440+
└─TableScan_5 10000.00 cop table:t, range:[-inf,+inf], keep order:false, stats:pseudo
441+
explain select * from t where _tidb_rowid > 0 and a > 0;
442+
id count task operator info
443+
Projection_4 2666.67 root test.t.a
444+
└─TableReader_7 2666.67 root data:Selection_6
445+
└─Selection_6 2666.67 cop gt(test.t.a, 0)
446+
└─TableScan_5 3333.33 cop table:t, range:(0,+inf], keep order:false, stats:pseudo
447+
drop table if exists t;

cmd/explaintest/t/explain_easy.test

+7
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,10 @@ explain select ifnull(nb, 0), ifnull(nb, 0) from t;
9393
explain select 1+ifnull(nb, 0) from t;
9494
explain select 1+ifnull(a, 0) from t;
9595
drop table if exists t;
96+
drop table if exists t;
97+
create table t(a int);
98+
explain select * from t where _tidb_rowid = 0;
99+
explain select * from t where _tidb_rowid > 0;
100+
explain select a, _tidb_rowid from t where a > 0;
101+
explain select * from t where _tidb_rowid > 0 and a > 0;
102+
drop table if exists t;

planner/core/logical_plans.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,10 @@ func (ds *DataSource) deriveTablePathStats(path *accessPath) (bool, error) {
333333
path.countAfterAccess = float64(ds.statisticTable.Count)
334334
path.tableFilters = ds.pushedDownConds
335335
var pkCol *expression.Column
336-
if ds.tableInfo.PKIsHandle {
336+
columnLen := len(ds.schema.Columns)
337+
if columnLen > 0 && ds.schema.Columns[columnLen-1].ID == model.ExtraHandleID {
338+
pkCol = ds.schema.Columns[columnLen-1]
339+
} else if ds.tableInfo.PKIsHandle {
337340
if pkColInfo := ds.tableInfo.GetPkColInfo(); pkColInfo != nil {
338341
pkCol = expression.ColInfo2Col(ds.schema.Columns, pkColInfo)
339342
}
@@ -342,6 +345,7 @@ func (ds *DataSource) deriveTablePathStats(path *accessPath) (bool, error) {
342345
path.ranges = ranger.FullIntRange(false)
343346
return false, nil
344347
}
348+
345349
path.ranges = ranger.FullIntRange(mysql.HasUnsignedFlag(pkCol.RetType.Flag))
346350
if len(ds.pushedDownConds) == 0 {
347351
return false, nil

0 commit comments

Comments
 (0)