Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: Handle _tidb_rowid correctly in batchPointGet Plan to avoid index out of range error #58687

Merged
merged 4 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions pkg/planner/core/physical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -1063,25 +1063,36 @@ func ExpandVirtualColumn(columns []*model.ColumnInfo, schema *expression.Schema,

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

extraColumns := make([]*expression.Column, numExtraColumns)
copy(extraColumns, schema.Columns[oldNumColumns-numExtraColumns:])
schema.Columns = schema.Columns[:oldNumColumns-numExtraColumns]
extraColumnModels := make([]*model.ColumnInfo, numExtraColumns)
copy(extraColumnModels, copyColumn[len(copyColumn)-numExtraColumns:])
copyColumn = copyColumn[:len(copyColumn)-numExtraColumns]

extraColumnModels := make([]*model.ColumnInfo, numExtraColumns)
copy(extraColumnModels, copyColumn[len(copyColumn)-numExtraColumns:])
copyColumn = copyColumn[:len(copyColumn)-numExtraColumns]
copyColumn = expandVirtualColumn(schema, copyColumn, colsInfo)
schema.Columns = append(schema.Columns, extraColumns...)
copyColumn = append(copyColumn, extraColumnModels...)
return copyColumn
}
return expandVirtualColumn(schema, copyColumn, colsInfo)
}

func expandVirtualColumn(schema *expression.Schema, copyColumn []*model.ColumnInfo, colsInfo []*model.ColumnInfo) []*model.ColumnInfo {
schemaColumns := schema.Columns
for _, col := range schemaColumns {
if col.VirtualExpr == nil {
Expand All @@ -1096,9 +1107,6 @@ func ExpandVirtualColumn(columns []*model.ColumnInfo, schema *expression.Schema,
}
}
}

schema.Columns = append(schema.Columns, extraColumns...)
copyColumn = append(copyColumn, extraColumnModels...)
return copyColumn
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -765,3 +765,13 @@ KEY `idx_65` (`col_36`(5))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
with cte_192 ( col_1101,col_1102,col_1103,col_1104 ) AS ( select /*+ use_index_merge( tl6e913fb9 ) */ replace( tl6e913fb9.col_36 , tl6e913fb9.col_36 , tl6e913fb9.col_36 ) as r0 , space( 0 ) as r1 , min( distinct tl6e913fb9.col_36 ) as r2 , count( distinct tl6e913fb9.col_36 ) as r3 from tl6e913fb9 where tl6e913fb9.col_36 between 'n92ok$B%W#UU%O' and '()c=KVQ=T%-vzGJ' and tl6e913fb9.col_36 in ( 'T+kf' ,'Lvluod2H' ,'3#Omx@pC^fFkeH' ,'=b$z' ) group by tl6e913fb9.col_36 having tl6e913fb9.col_36 = 'xjV@' or IsNull( tl6e913fb9.col_36 ) ) ( select 1,col_1101,col_1102,col_1103,col_1104 from cte_192 where not( IsNull( cte_192.col_1102 ) ) order by 1,2,3,4,5 limit 72850972 );
1 col_1101 col_1102 col_1103 col_1104
drop table if exists t;
create table t (id int unique key, c int);
insert into t values (1, 10);
insert into t values (2, 20);
insert into t values (3, 30);
select _tidb_rowid from t where id in (1, 2, 3);
_tidb_rowid
1
2
3
Original file line number Diff line number Diff line change
Expand Up @@ -534,3 +534,11 @@ CREATE TABLE `tl6e913fb9` (
KEY `idx_65` (`col_36`(5))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
with cte_192 ( col_1101,col_1102,col_1103,col_1104 ) AS ( select /*+ use_index_merge( tl6e913fb9 ) */ replace( tl6e913fb9.col_36 , tl6e913fb9.col_36 , tl6e913fb9.col_36 ) as r0 , space( 0 ) as r1 , min( distinct tl6e913fb9.col_36 ) as r2 , count( distinct tl6e913fb9.col_36 ) as r3 from tl6e913fb9 where tl6e913fb9.col_36 between 'n92ok$B%W#UU%O' and '()c=KVQ=T%-vzGJ' and tl6e913fb9.col_36 in ( 'T+kf' ,'Lvluod2H' ,'3#Omx@pC^fFkeH' ,'=b$z' ) group by tl6e913fb9.col_36 having tl6e913fb9.col_36 = 'xjV@' or IsNull( tl6e913fb9.col_36 ) ) ( select 1,col_1101,col_1102,col_1103,col_1104 from cte_192 where not( IsNull( cte_192.col_1102 ) ) order by 1,2,3,4,5 limit 72850972 );

# TestIssue58581
drop table if exists t;
create table t (id int unique key, c int);
insert into t values (1, 10);
insert into t values (2, 20);
insert into t values (3, 30);
select _tidb_rowid from t where id in (1, 2, 3);