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
32 changes: 32 additions & 0 deletions expression/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ import (
"github.com/pingcap/tidb/types"
driver "github.com/pingcap/tidb/types/parser_driver"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/cmp"
"github.com/pingcap/tidb/util/collate"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/sqlexec"
"go.uber.org/zap"
"golang.org/x/exp/slices"
)

// cowExprRef is a copy-on-write slice ref util using in `ColumnSubstitute`
Expand Down Expand Up @@ -122,6 +124,36 @@ func ExtractColumns(expr Expression) []*Column {
return extractColumns(result, expr, nil)
}

// ExtractAllColumnsFromExpressionsInUsedSlices is the same as ExtractColumns. but it can reuse the memory.
func ExtractAllColumnsFromExpressionsInUsedSlices(reuse []*Column, exprs ...Expression) []*Column {
if len(exprs) == 0 {
return nil
}
reuse = reuse[:0]
for _, expr := range exprs {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cols = cols[:0]

Copy link
Contributor

@AilinKid AilinKid Oct 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about move to here, after all, your func name says it will reuse...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

reuse = extractColumnsSlices(reuse, expr)
}
slices.SortFunc(reuse, func(a, b *Column) int {
return cmp.Compare(a.UniqueID, b.UniqueID)
})
reuse = slices.CompactFunc(reuse, func(a, b *Column) bool {
return a.UniqueID == b.UniqueID
})
return reuse
}

func extractColumnsSlices(result []*Column, expr Expression) []*Column {
switch v := expr.(type) {
case *Column:
result = append(result, v)
case *ScalarFunction:
for _, arg := range v.GetArgs() {
result = extractColumnsSlices(result, arg)
}
}
return result
}

// ExtractCorColumns extracts correlated column from given expression.
func ExtractCorColumns(expr Expression) (cols []*CorrelatedColumn) {
switch v := expr.(type) {
Expand Down
3 changes: 2 additions & 1 deletion planner/core/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -1127,8 +1127,9 @@ func (p *LogicalProjection) DeriveStats(childStats []*property.StatsInfo, selfSc
RowCount: childProfile.RowCount,
ColNDVs: make(map[int64]float64, len(p.Exprs)),
}
cols := make([]*expression.Column, 0, 8)
for i, expr := range p.Exprs {
cols := expression.ExtractColumns(expr)
cols = expression.ExtractAllColumnsFromExpressionsInUsedSlices(cols, expr)
p.stats.ColNDVs[selfSchema.Columns[i].UniqueID], _ = getColsNDVWithMatchedLen(cols, childSchema[0], childProfile)
}
p.stats.GroupNDVs = p.getGroupNDVs(colGroups, childProfile, selfSchema)
Expand Down