@@ -3511,17 +3511,39 @@ func buildIndexRangeForEachPartition(ctx sessionctx.Context, usedPartitions []ta
3511
3511
return nextRange , nil
3512
3512
}
3513
3513
3514
- func keyColumnsIncludeAllPartitionColumns (keyColumns []int , pe * tables.PartitionExpr ) bool {
3515
- tmp := make (map [int ]struct {}, len (keyColumns ))
3516
- for _ , offset := range keyColumns {
3517
- tmp [offset ] = struct {}{}
3514
+ func getPartitionKeyColOffsets (keyColIDs []int64 , pt table.PartitionedTable ) []int {
3515
+ keyColOffsets := make ([]int , len (keyColIDs ))
3516
+ for i , colID := range keyColIDs {
3517
+ offset := - 1
3518
+ for j , col := range pt .Cols () {
3519
+ if colID == col .ID {
3520
+ offset = j
3521
+ break
3522
+ }
3523
+ }
3524
+ if offset == - 1 {
3525
+ return nil
3526
+ }
3527
+ keyColOffsets [i ] = offset
3528
+ }
3529
+
3530
+ pe , err := pt .(interface {
3531
+ PartitionExpr () (* tables.PartitionExpr , error )
3532
+ }).PartitionExpr ()
3533
+ if err != nil {
3534
+ return nil
3535
+ }
3536
+
3537
+ offsetMap := make (map [int ]struct {})
3538
+ for _ , offset := range keyColOffsets {
3539
+ offsetMap [offset ] = struct {}{}
3518
3540
}
3519
3541
for _ , offset := range pe .ColumnOffset {
3520
- if _ , ok := tmp [offset ]; ! ok {
3521
- return false
3542
+ if _ , ok := offsetMap [offset ]; ! ok {
3543
+ return nil
3522
3544
}
3523
3545
}
3524
- return true
3546
+ return keyColOffsets
3525
3547
}
3526
3548
3527
3549
func (builder * dataReaderBuilder ) prunePartitionForInnerExecutor (tbl table.Table , schema * expression.Schema , partitionInfo * plannercore.PartitionInfo ,
@@ -3536,45 +3558,16 @@ func (builder *dataReaderBuilder) prunePartitionForInnerExecutor(tbl table.Table
3536
3558
return nil , false , nil , err
3537
3559
}
3538
3560
3539
- // check whether can runtime prune.
3540
- type partitionExpr interface {
3541
- PartitionExpr () (* tables.PartitionExpr , error )
3542
- }
3543
- pe , err := tbl .(partitionExpr ).PartitionExpr ()
3544
- if err != nil {
3545
- return nil , false , nil , err
3546
- }
3547
-
3548
3561
// recalculate key column offsets
3549
3562
if len (lookUpContent ) == 0 {
3550
3563
return nil , false , nil , nil
3551
3564
}
3552
3565
if lookUpContent [0 ].keyColIDs == nil {
3553
3566
return nil , false , nil , plannercore .ErrInternal .GenWithStack ("cannot get column IDs when dynamic pruning" )
3554
3567
}
3555
- keyColOffsets := make ([]int , len (lookUpContent [0 ].keyColIDs ))
3556
- for i , colID := range lookUpContent [0 ].keyColIDs {
3557
- offset := - 1
3558
- for j , col := range partitionTbl .Cols () {
3559
- if colID == col .ID {
3560
- offset = j
3561
- break
3562
- }
3563
- }
3564
- if offset == - 1 {
3565
- return nil , false , nil , plannercore .ErrInternal .GenWithStack ("invalid column offset when dynamic pruning" )
3566
- }
3567
- keyColOffsets [i ] = offset
3568
- }
3569
-
3570
- offsetMap := make (map [int ]bool )
3571
- for _ , offset := range keyColOffsets {
3572
- offsetMap [offset ] = true
3573
- }
3574
- for _ , offset := range pe .ColumnOffset {
3575
- if _ , ok := offsetMap [offset ]; ! ok {
3576
- return condPruneResult , false , nil , nil
3577
- }
3568
+ keyColOffsets := getPartitionKeyColOffsets (lookUpContent [0 ].keyColIDs , partitionTbl )
3569
+ if len (keyColOffsets ) == 0 {
3570
+ return condPruneResult , false , nil , nil
3578
3571
}
3579
3572
3580
3573
locateKey := make ([]types.Datum , len (partitionTbl .Cols ()))
@@ -4149,12 +4142,6 @@ func (builder *dataReaderBuilder) buildTableReaderForIndexJoin(ctx context.Conte
4149
4142
}
4150
4143
tbl , _ := builder .is .TableByID (tbInfo .ID )
4151
4144
pt := tbl .(table.PartitionedTable )
4152
- pe , err := tbl .(interface {
4153
- PartitionExpr () (* tables.PartitionExpr , error )
4154
- }).PartitionExpr ()
4155
- if err != nil {
4156
- return nil , err
4157
- }
4158
4145
partitionInfo := & v .PartitionInfo
4159
4146
usedPartitionList , err := builder .partitionPruning (pt , partitionInfo .PruningConds , partitionInfo .PartitionNames , partitionInfo .Columns , partitionInfo .ColumnNames )
4160
4147
if err != nil {
@@ -4165,8 +4152,12 @@ func (builder *dataReaderBuilder) buildTableReaderForIndexJoin(ctx context.Conte
4165
4152
usedPartitions [p .GetPhysicalID ()] = p
4166
4153
}
4167
4154
var kvRanges []kv.KeyRange
4155
+ var keyColOffsets []int
4156
+ if len (lookUpContents ) > 0 {
4157
+ keyColOffsets = getPartitionKeyColOffsets (lookUpContents [0 ].keyColIDs , pt )
4158
+ }
4168
4159
if v .IsCommonHandle {
4169
- if len (lookUpContents ) > 0 && keyColumnsIncludeAllPartitionColumns ( lookUpContents [ 0 ]. keyCols , pe ) {
4160
+ if len (keyColOffsets ) > 0 {
4170
4161
locateKey := make ([]types.Datum , e .Schema ().Len ())
4171
4162
kvRanges = make ([]kv.KeyRange , 0 , len (lookUpContents ))
4172
4163
// lookUpContentsByPID groups lookUpContents by pid(partition) so that kv ranges for same partition can be merged.
@@ -4212,7 +4203,7 @@ func (builder *dataReaderBuilder) buildTableReaderForIndexJoin(ctx context.Conte
4212
4203
4213
4204
handles , lookUpContents := dedupHandles (lookUpContents )
4214
4205
4215
- if len (lookUpContents ) > 0 && keyColumnsIncludeAllPartitionColumns ( lookUpContents [ 0 ]. keyCols , pe ) {
4206
+ if len (keyColOffsets ) > 0 {
4216
4207
locateKey := make ([]types.Datum , e .Schema ().Len ())
4217
4208
kvRanges = make ([]kv.KeyRange , 0 , len (lookUpContents ))
4218
4209
for _ , content := range lookUpContents {
0 commit comments