Skip to content

Commit 564a0ca

Browse files
hawkingreiti-chi-bot
authored andcommitted
This is an automated cherry-pick of pingcap#63829
Signed-off-by: ti-chi-bot <[email protected]>
1 parent 9611112 commit 564a0ca

File tree

4 files changed

+3435
-0
lines changed

4 files changed

+3435
-0
lines changed

expression/util.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,70 @@ func ExtractCorColumns(expr Expression) (cols []*CorrelatedColumn) {
149149
// To avoid allocation for cols that not need.
150150
func ExtractColumnsFromExpressions(result []*Column, exprs []Expression, filter func(*Column) bool) []*Column {
151151
for _, expr := range exprs {
152+
<<<<<<< HEAD:expression/util.go
152153
result = extractColumns(result, expr, filter)
154+
=======
155+
extractColumns(m, expr, filter)
156+
}
157+
result := slices.Collect(maps.Values(m))
158+
// The keys in a map are unordered, so to ensure stability, we need to sort them here.
159+
slices.SortFunc(result, func(a, b *Column) int {
160+
return cmp.Compare(a.UniqueID, b.UniqueID)
161+
})
162+
return result
163+
}
164+
165+
// ExtractColumnsMapFromExpressions it the same as ExtractColumnsFromExpressions, but return a map
166+
func ExtractColumnsMapFromExpressions(filter func(*Column) bool, exprs ...Expression) map[int64]*Column {
167+
if len(exprs) == 0 {
168+
return nil
169+
}
170+
m := make(map[int64]*Column, len(exprs))
171+
for _, expr := range exprs {
172+
extractColumns(m, expr, filter)
173+
}
174+
return m
175+
}
176+
177+
// ExtractColumnsMapFromExpressionsWithReusedMap is the same as ExtractColumnsFromExpressions, but map can be reused.
178+
func ExtractColumnsMapFromExpressionsWithReusedMap(m map[int64]*Column, filter func(*Column) bool, exprs ...Expression) {
179+
if len(exprs) == 0 {
180+
return
181+
}
182+
if m == nil {
183+
m = make(map[int64]*Column, len(exprs))
184+
}
185+
for _, expr := range exprs {
186+
extractColumns(m, expr, filter)
187+
}
188+
}
189+
190+
// ExtractAllColumnsFromExpressionsInUsedSlices is the same as ExtractColumns. but it can reuse the memory.
191+
func ExtractAllColumnsFromExpressionsInUsedSlices(reuse []*Column, filter func(*Column) bool, exprs ...Expression) []*Column {
192+
if len(exprs) == 0 {
193+
return nil
194+
}
195+
for _, expr := range exprs {
196+
reuse = extractColumnsSlices(reuse, expr, filter)
197+
}
198+
slices.SortFunc(reuse, func(a, b *Column) int {
199+
return cmp.Compare(a.UniqueID, b.UniqueID)
200+
})
201+
reuse = slices.CompactFunc(reuse, func(a, b *Column) bool {
202+
return a.UniqueID == b.UniqueID
203+
})
204+
return reuse
205+
}
206+
207+
// ExtractAllColumnsFromExpressions is the same as ExtractColumnsFromExpressions. But this will not remove duplicates.
208+
func ExtractAllColumnsFromExpressions(exprs []Expression, filter func(*Column) bool) []*Column {
209+
if len(exprs) == 0 {
210+
return nil
211+
}
212+
result := make([]*Column, 0, 8)
213+
for _, expr := range exprs {
214+
result = extractColumnsSlices(result, expr, filter)
215+
>>>>>>> 3a54eaa3ffb (planner: fix LogicalProjection.DeriveStats allocate too many memories (#63829)):pkg/expression/util.go
153216
}
154217
return result
155218
}

0 commit comments

Comments
 (0)