Skip to content

Commit 58c3099

Browse files
authored
planner: Handle _tidb_rowid correctly in batchPointGet Plan to avoid index out of range error (#58687) (#60441)
close #58581
1 parent d4d834d commit 58c3099

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

pkg/planner/core/find_best_task.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1493,7 +1493,7 @@ func findBestTask4LogicalDataSource(lp base.LogicalPlan, prop *property.Physical
14931493
}
14941494
if canConvertPointGet && len(path.Ranges) > 1 {
14951495
// TODO: This is now implemented, but to decrease
1496-
// the impact of supporting plan cache for patitioning,
1496+
// the impact of supporting plan cache for partitioning,
14971497
// this is not yet enabled.
14981498
// TODO: just remove this if block and update/add tests...
14991499
// We can only build batch point get for hash partitions on a simple column now. This is

pkg/planner/core/physical_plans.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,25 +1059,36 @@ func ExpandVirtualColumn(columns []*model.ColumnInfo, schema *expression.Schema,
10591059

10601060
oldNumColumns := len(schema.Columns)
10611061
numExtraColumns := 0
1062+
ordinaryColumnExists := false
10621063
for i := oldNumColumns - 1; i >= 0; i-- {
10631064
cid := schema.Columns[i].ID
10641065
// Move extra columns to the end.
10651066
// ExtraRowChecksumID is ignored here since it's treated as an ordinary column.
10661067
// https://github.com/pingcap/tidb/blob/3c407312a986327bc4876920e70fdd6841b8365f/pkg/util/rowcodec/decoder.go#L206-L222
10671068
if cid != model.ExtraHandleID && cid != model.ExtraPhysTblID {
1069+
ordinaryColumnExists = true
10681070
break
10691071
}
10701072
numExtraColumns++
10711073
}
1074+
if ordinaryColumnExists && numExtraColumns > 0 {
1075+
extraColumns := make([]*expression.Column, numExtraColumns)
1076+
copy(extraColumns, schema.Columns[oldNumColumns-numExtraColumns:])
1077+
schema.Columns = schema.Columns[:oldNumColumns-numExtraColumns]
10721078

1073-
extraColumns := make([]*expression.Column, numExtraColumns)
1074-
copy(extraColumns, schema.Columns[oldNumColumns-numExtraColumns:])
1075-
schema.Columns = schema.Columns[:oldNumColumns-numExtraColumns]
1079+
extraColumnModels := make([]*model.ColumnInfo, numExtraColumns)
1080+
copy(extraColumnModels, copyColumn[len(copyColumn)-numExtraColumns:])
1081+
copyColumn = copyColumn[:len(copyColumn)-numExtraColumns]
10761082

1077-
extraColumnModels := make([]*model.ColumnInfo, numExtraColumns)
1078-
copy(extraColumnModels, copyColumn[len(copyColumn)-numExtraColumns:])
1079-
copyColumn = copyColumn[:len(copyColumn)-numExtraColumns]
1083+
copyColumn = expandVirtualColumn(schema, copyColumn, colsInfo)
1084+
schema.Columns = append(schema.Columns, extraColumns...)
1085+
copyColumn = append(copyColumn, extraColumnModels...)
1086+
return copyColumn
1087+
}
1088+
return expandVirtualColumn(schema, copyColumn, colsInfo)
1089+
}
10801090

1091+
func expandVirtualColumn(schema *expression.Schema, copyColumn []*model.ColumnInfo, colsInfo []*model.ColumnInfo) []*model.ColumnInfo {
10811092
schemaColumns := schema.Columns
10821093
for _, col := range schemaColumns {
10831094
if col.VirtualExpr == nil {
@@ -1092,9 +1103,6 @@ func ExpandVirtualColumn(columns []*model.ColumnInfo, schema *expression.Schema,
10921103
}
10931104
}
10941105
}
1095-
1096-
schema.Columns = append(schema.Columns, extraColumns...)
1097-
copyColumn = append(copyColumn, extraColumnModels...)
10981106
return copyColumn
10991107
}
11001108

tests/integrationtest/r/planner/core/issuetest/planner_issue.result

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,3 +815,13 @@ Projection_9 12.50 root test.t.a, test.t1.a, test.t1.b
815815
select /*+ inl_join(t1), use_index(t1, idx) */ * from t join t1 on t.a = t1.a and t1.b = 123;
816816
a a b
817817
2 2 123
818+
drop table if exists t;
819+
create table t (id int unique key, c int);
820+
insert into t values (1, 10);
821+
insert into t values (2, 20);
822+
insert into t values (3, 30);
823+
select _tidb_rowid from t where id in (1, 2, 3);
824+
_tidb_rowid
825+
1
826+
2
827+
3

tests/integrationtest/t/planner/core/issuetest/planner_issue.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,3 +553,11 @@ insert into t1 values(2, 123), (123, 2);
553553
set tidb_opt_fix_control='44855:on';
554554
explain select /*+ inl_join(t1), use_index(t1, idx) */ * from t join t1 on t.a = t1.a and t1.b = 123;
555555
select /*+ inl_join(t1), use_index(t1, idx) */ * from t join t1 on t.a = t1.a and t1.b = 123;
556+
557+
# TestIssue58581
558+
drop table if exists t;
559+
create table t (id int unique key, c int);
560+
insert into t values (1, 10);
561+
insert into t values (2, 20);
562+
insert into t values (3, 30);
563+
select _tidb_rowid from t where id in (1, 2, 3);

0 commit comments

Comments
 (0)