Skip to content
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
25 changes: 12 additions & 13 deletions pkg/planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package core

import (
"cmp"
"context"
"fmt"
"maps"
Expand Down Expand Up @@ -5269,6 +5270,11 @@ func (t *TblColPosInfo) MemoryUsage() (sum int64) {
return
}

// Cmp compares two TblColPosInfo by their Start field.
func (t *TblColPosInfo) Cmp(a TblColPosInfo) int {
return cmp.Compare(t.Start, a.Start)
}

// TblColPosInfoSlice attaches the methods of sort.Interface to []TblColPosInfos sorting in increasing order.
type TblColPosInfoSlice []TblColPosInfo

Expand All @@ -5277,16 +5283,6 @@ func (c TblColPosInfoSlice) Len() int {
return len(c)
}

// Swap implements sort.Interface#Swap.
func (c TblColPosInfoSlice) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}

// Less implements sort.Interface#Less.
func (c TblColPosInfoSlice) Less(i, j int) bool {
return c[i].Start < c[j].Start
}

// FindTblIdx finds the ordinal of the corresponding access column.
func (c TblColPosInfoSlice) FindTblIdx(colOrdinal int) (int, bool) {
if len(c) == 0 {
Expand Down Expand Up @@ -5321,7 +5317,9 @@ func buildColumns2HandleWithWrtiableColumns(
cols2Handles = append(cols2Handles, TblColPosInfo{TblID: tblID, Start: offset, End: end, HandleCols: handleCol})
}
}
sort.Sort(cols2Handles)
slices.SortFunc(cols2Handles, func(a, b TblColPosInfo) int {
return a.Cmp(b)
})
return cols2Handles, nil
}

Expand Down Expand Up @@ -5363,8 +5361,9 @@ func pruneAndBuildColPositionInfoForDelete(
}
}
// Sort by start position. To do the later column pruning.
// TODO: `sort`` package has a rather worse performance. We should replace it with the new `slice` package.
sort.Sort(cols2PosInfos)
slices.SortFunc(cols2PosInfos, func(a, b TblColPosInfo) int {
return a.Cmp(b)
})
prunedColCnt := 0
var err error
for i := range cols2PosInfos {
Expand Down
21 changes: 9 additions & 12 deletions pkg/planner/core/rule_partition_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,10 @@ type partitionRange struct {
end int
}

func (p *partitionRange) Cmp(a partitionRange) int {
return cmp.Compare(p.start, a.start)
}

// partitionRangeOR represents OR(range1, range2, ...)
type partitionRangeOR []partitionRange

Expand Down Expand Up @@ -958,14 +962,6 @@ func (or partitionRangeOR) Len() int {
return len(or)
}

func (or partitionRangeOR) Less(i, j int) bool {
return or[i].start < or[j].start
}

func (or partitionRangeOR) Swap(i, j int) {
or[i], or[j] = or[j], or[i]
}

func (or partitionRangeOR) union(x partitionRangeOR) partitionRangeOR {
or = append(or, x...)
return or.simplify()
Expand All @@ -977,13 +973,14 @@ func (or partitionRangeOR) simplify() partitionRangeOR {
return or
}
// Make the ranges order by start.
sort.Sort(or)
sorted := or
slices.SortFunc(or, func(i, j partitionRange) int {
return i.Cmp(j)
})

// Iterate the sorted ranges, merge the adjacent two when their range overlap.
// For example, [0, 1), [2, 7), [3, 5), ... => [0, 1), [2, 7) ...
res := sorted[:1]
for _, curr := range sorted[1:] {
res := or[:1]
for _, curr := range or[1:] {
last := &res[len(res)-1]
if curr.start > last.end {
res = append(res, curr)
Expand Down