java-topology/defects/tidb/patch/tidb-0001-merge-join-offsets-map.patch

68 lines
2.4 KiB
Diff

# UNDF: UNDF-2026-000000312
--- a/pkg/planner/core/operator/physicalop/physical_merge_join.go
+++ b/pkg/planner/core/operator/physicalop/physical_merge_join.go
@@ -150,7 +150,10 @@ func getEnforcedMergeJoin(p *logicalop.LogicalJoin, prop *property.PhysicalProp
offsets := make([]int, 0, len(leftJoinKeys))
+ // CWE-407 fix: use a map for O(1) membership test instead of
+ // slices.Contains which is O(len(offsets)) per call inside a nested loop.
+ offsetSet := make(map[int]struct{}, len(leftJoinKeys))
all, desc := prop.AllSameOrder()
if !all {
return nil
@@ -172,10 +175,9 @@ func getEnforcedMergeJoin(p *logicalop.LogicalJoin, prop *property.PhysicalProp
if key == nil {
continue
}
- if slices.Contains(offsets, joinKeyPos) {
+ if _, exists := offsetSet[joinKeyPos]; exists {
isExist = true
}
if !isExist {
offsets = append(offsets, joinKeyPos)
+ offsetSet[joinKeyPos] = struct{}{}
}
isExist = true
break
@@ -507,16 +509,22 @@ func getNewJoinKeysByOffsets(oldJoinKeys []*expression.Column, offsets []int) []
// Change JoinKeys order, by offsets array
// offsets array is generate by prop check
func getNewJoinKeysByOffsets(oldJoinKeys []*expression.Column, offsets []int) []*expression.Column {
newKeys := make([]*expression.Column, 0, len(oldJoinKeys))
for _, offset := range offsets {
newKeys = append(newKeys, oldJoinKeys[offset])
}
+ // CWE-407 fix: build a map once (O(|offsets|)) so the loop below is O(N)
+ // instead of O(N * |offsets|) with slices.Contains.
+ offsetSet := make(map[int]struct{}, len(offsets))
+ for _, o := range offsets {
+ offsetSet[o] = struct{}{}
+ }
for pos, key := range oldJoinKeys {
- isExist := slices.Contains(offsets, pos)
- if !isExist {
+ if _, exists := offsetSet[pos]; !exists {
newKeys = append(newKeys, key)
}
}
return newKeys
}
func getNewNullEQByOffsets(oldNullEQ []bool, offsets []int) []bool {
newNullEQ := make([]bool, 0, len(oldNullEQ))
for _, offset := range offsets {
newNullEQ = append(newNullEQ, oldNullEQ[offset])
}
+ // CWE-407 fix: same map pattern as getNewJoinKeysByOffsets.
+ offsetSet := make(map[int]struct{}, len(offsets))
+ for _, o := range offsets {
+ offsetSet[o] = struct{}{}
+ }
for pos, key := range oldNullEQ {
- isExist := slices.Contains(offsets, pos)
- if !isExist {
+ if _, exists := offsetSet[pos]; !exists {
newNullEQ = append(newNullEQ, key)
}
}
return newNullEQ
}