Skip to content

Commit 981b178

Browse files
yu34poXuHuaiyu
authored andcommitted
support _tidb_rowid for table scan range (pingcap#8047)
1 parent e3a9c6d commit 981b178

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

cmd/explaintest/r/explain_easy.result

+62
Original file line numberDiff line numberDiff line change
@@ -400,3 +400,65 @@ 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, nb int not null, nc int not null);
405+
explain select ifnull(a, 0) from t;
406+
id count task operator info
407+
Projection_3 10000.00 root ifnull(test.t.a, 0)
408+
└─TableReader_5 10000.00 root data:TableScan_4
409+
└─TableScan_4 10000.00 cop table:t, range:[-inf,+inf], keep order:false, stats:pseudo
410+
explain select ifnull(nb, 0) from t;
411+
id count task operator info
412+
Projection_3 10000.00 root ifnull(test.t.nb, 0)
413+
└─TableReader_5 10000.00 root data:TableScan_4
414+
└─TableScan_4 10000.00 cop table:t, range:[-inf,+inf], keep order:false, stats:pseudo
415+
explain select ifnull(nb, 0), ifnull(nc, 0) from t;
416+
id count task operator info
417+
Projection_3 10000.00 root ifnull(test.t.nb, 0), ifnull(test.t.nc, 0)
418+
└─TableReader_5 10000.00 root data:TableScan_4
419+
└─TableScan_4 10000.00 cop table:t, range:[-inf,+inf], keep order:false, stats:pseudo
420+
explain select ifnull(a, 0), ifnull(nb, 0) from t;
421+
id count task operator info
422+
Projection_3 10000.00 root ifnull(test.t.a, 0), ifnull(test.t.nb, 0)
423+
└─TableReader_5 10000.00 root data:TableScan_4
424+
└─TableScan_4 10000.00 cop table:t, range:[-inf,+inf], keep order:false, stats:pseudo
425+
explain select ifnull(nb, 0), ifnull(nb, 0) from t;
426+
id count task operator info
427+
Projection_3 10000.00 root ifnull(test.t.nb, 0), ifnull(test.t.nb, 0)
428+
└─TableReader_5 10000.00 root data:TableScan_4
429+
└─TableScan_4 10000.00 cop table:t, range:[-inf,+inf], keep order:false, stats:pseudo
430+
explain select 1+ifnull(nb, 0) from t;
431+
id count task operator info
432+
Projection_3 10000.00 root plus(1, ifnull(test.t.nb, 0))
433+
└─TableReader_5 10000.00 root data:TableScan_4
434+
└─TableScan_4 10000.00 cop table:t, range:[-inf,+inf], keep order:false, stats:pseudo
435+
explain select 1+ifnull(a, 0) from t;
436+
id count task operator info
437+
Projection_3 10000.00 root plus(1, ifnull(test.t.a, 0))
438+
└─TableReader_5 10000.00 root data:TableScan_4
439+
└─TableScan_4 10000.00 cop table:t, range:[-inf,+inf], keep order:false, stats:pseudo
440+
drop table if exists t;
441+
drop table if exists t;
442+
create table t(a int);
443+
explain select * from t where _tidb_rowid = 0;
444+
id count task operator info
445+
Projection_4 8000.00 root test.t.a
446+
└─TableReader_6 10000.00 root data:TableScan_5
447+
└─TableScan_5 10000.00 cop table:t, range:[0,0], keep order:false, stats:pseudo
448+
explain select * from t where _tidb_rowid > 0;
449+
id count task operator info
450+
Projection_4 8000.00 root test.t.a
451+
└─TableReader_6 10000.00 root data:TableScan_5
452+
└─TableScan_5 10000.00 cop table:t, range:(0,+inf], keep order:false, stats:pseudo
453+
explain select a, _tidb_rowid from t where a > 0;
454+
id count task operator info
455+
TableReader_7 3333.33 root data:Selection_6
456+
└─Selection_6 3333.33 cop gt(test.t.a, 0)
457+
└─TableScan_5 10000.00 cop table:t, range:[-inf,+inf], keep order:false, stats:pseudo
458+
explain select * from t where _tidb_rowid > 0 and a > 0;
459+
id count task operator info
460+
Projection_4 2666.67 root test.t.a
461+
└─TableReader_7 2666.67 root data:Selection_6
462+
└─Selection_6 2666.67 cop gt(test.t.a, 0)
463+
└─TableScan_5 3333.33 cop table:t, range:(0,+inf], keep order:false, stats:pseudo
464+
drop table if exists t;

cmd/explaintest/t/explain_easy.test

+19
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,22 @@ rollback;
8383

8484
explain SELECT 0 AS a FROM dual UNION SELECT 1 AS a FROM dual ORDER BY a;
8585
explain SELECT 0 AS a FROM dual UNION (SELECT 1 AS a FROM dual ORDER BY a)
86+
87+
# https://github.com/pingcap/tidb/issues/7918
88+
drop table if exists t;
89+
create table t(a int, nb int not null, nc int not null);
90+
explain select ifnull(a, 0) from t;
91+
explain select ifnull(nb, 0) from t;
92+
explain select ifnull(nb, 0), ifnull(nc, 0) from t;
93+
explain select ifnull(a, 0), ifnull(nb, 0) from t;
94+
explain select ifnull(nb, 0), ifnull(nb, 0) from t;
95+
explain select 1+ifnull(nb, 0) from t;
96+
explain select 1+ifnull(a, 0) from t;
97+
drop table if exists t;
98+
drop table if exists t;
99+
create table t(a int);
100+
explain select * from t where _tidb_rowid = 0;
101+
explain select * from t where _tidb_rowid > 0;
102+
explain select a, _tidb_rowid from t where a > 0;
103+
explain select * from t where _tidb_rowid > 0 and a > 0;
104+
drop table if exists t;

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)