Skip to content

Commit

Permalink
cp #19686 to main (#19719)
Browse files Browse the repository at this point in the history
cp #19686 to main

Approved by: @m-schen, @aunjgr
  • Loading branch information
zengyan1 authored Nov 4, 2024
1 parent c130dac commit b6f9866
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 68 deletions.
23 changes: 4 additions & 19 deletions pkg/container/vector/vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -778,26 +778,11 @@ func (v *Vector) UnmarshalBinaryWithCopy(data []byte, mp *mpool.MPool) error {
return nil
}

func (v *Vector) ToConst(row, length int, mp *mpool.MPool) *Vector {
w := NewConstNull(v.typ, length, mp)
if v.IsConstNull() || v.nsp.Contains(uint64(row)) {
return w
func (v *Vector) ToConst() {
if v.nsp.Contains(0) {
v.data = v.data[:0]
}

if v.IsConst() {
row = 0
}

sz := v.typ.TypeSize()
w.data = v.data[row*sz : (row+1)*sz]
w.setupFromData()
if v.typ.IsVarlen() {
w.area = v.area
}
w.cantFreeData = true
w.cantFreeArea = true

return w
v.class = CONSTANT
}

// PreExtend use to expand the capacity of the vector
Expand Down
8 changes: 0 additions & 8 deletions pkg/sql/colexec/debugTools.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,6 @@ func printFunctionExpressionExecutor(buffer *bytes.Buffer, expr *FunctionExpress

buffer.WriteString(fmt.Sprintf("%s needFoldingCheck: %v\n", prefix, expr.folded.needFoldingCheck))
buffer.WriteString(fmt.Sprintf("%s canFold: %v\n", prefix, expr.folded.canFold))
if expr.folded.foldVector != nil {
if expr.folded.foldVector.GetType() != nil {
buffer.WriteString(fmt.Sprintf("%s foldVector.typ: %s\n", prefix, expr.folded.foldVector.GetType().String()))
}
buffer.WriteString(fmt.Sprintf("%s foldVector.value: %s\n", prefix, expr.folded.foldVector.String()))
} else {
buffer.WriteString(fmt.Sprintf("%s foldVector is nil\n", prefix))
}

// selectList1, selectList2
buffer.WriteString(fmt.Sprintf("%s selectList1: %v\n", prefix, expr.selectList1))
Expand Down
7 changes: 6 additions & 1 deletion pkg/sql/colexec/evalExpression.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,12 @@ type ParamExpressionExecutor struct {
}

func (expr *ParamExpressionExecutor) Eval(proc *process.Process, _ []*batch.Batch, _ []bool) (*vector.Vector, error) {
if expr.vec != nil {
return expr.vec, nil
}
if expr.null != nil {
return expr.null, nil
}
val, err := proc.GetPrepareParamsAt(expr.pos)
if err != nil {
return nil, err
Expand Down Expand Up @@ -644,7 +650,6 @@ func (expr *FunctionExpressionExecutor) Free() {
expr.resultVector.Free()
expr.resultVector = nil
}
expr.folded.reset(expr.m)

for _, p := range expr.parameterExecutor {
if p != nil {
Expand Down
1 change: 0 additions & 1 deletion pkg/sql/colexec/evalExpressionOpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func modifyResultOwnerToOuter(executor ExpressionExecutor) (succeed bool) {
// function expression.
if f, ok := executor.(*FunctionExpressionExecutor); ok {
f.resultVector = nil
f.folded.foldVector = nil
return true
}

Expand Down
53 changes: 22 additions & 31 deletions pkg/sql/colexec/evalExpressionReset.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,11 @@ func (expr *FixedVectorExpressionExecutor) ResetForNextQuery() {
type functionFolding struct {
needFoldingCheck bool
canFold bool
foldVector *vector.Vector
}

func (fF *functionFolding) reset(m *mpool.MPool) {
if fF.foldVector != nil {
fF.foldVector.Free(m)
}
func (fF *functionFolding) reset(_ *mpool.MPool) {
fF.needFoldingCheck = true
fF.canFold = false
fF.foldVector = nil
}

type functionInformationForEval struct {
Expand Down Expand Up @@ -100,10 +95,11 @@ func (expr *FunctionExpressionExecutor) ResetForNextQuery() {
}

func (expr *FunctionExpressionExecutor) getFoldedVector(requiredLength int) *vector.Vector {
if expr.folded.foldVector.IsConst() {
expr.folded.foldVector.SetLength(requiredLength)
rv := expr.resultVector.GetResultVector()
if rv.IsConst() {
rv.SetLength(requiredLength)
}
return expr.folded.foldVector
return rv
}

func (expr *FunctionExpressionExecutor) doFold(proc *process.Process, atRuntime bool) (err error) {
Expand Down Expand Up @@ -163,34 +159,29 @@ func (expr *FunctionExpressionExecutor) doFold(proc *process.Process, atRuntime
}

// fold the function.
if execLen > 1 {
if err = expr.resultVector.PreExtendAndReset(execLen); err != nil {
return err
}
if err = expr.evalFn(expr.parameterResults, expr.resultVector, proc, execLen, nil); err != nil {
return err
}
expr.folded.foldVector = expr.resultVector.GetResultVector()
expr.resultVector.SetResultVector(nil)

} else {
if err = expr.resultVector.PreExtendAndReset(1); err != nil {
return err
}
if err = expr.evalFn(expr.parameterResults, expr.resultVector, proc, 1, nil); err != nil {
return err
}
if expr.folded.foldVector, err = expr.resultVector.GetResultVector().ToConst(0, 1, proc.Mp()).Dup(proc.Mp()); err != nil {
return err
}
expr.resultVector.Free()
if err = expr.resultVector.PreExtendAndReset(execLen); err != nil {
return err
}
if err = expr.evalFn(expr.parameterResults, expr.resultVector, proc, execLen, nil); err != nil {
return err
}
if execLen == 1 {
expr.resultVector.GetResultVector().ToConst()
}

expr.folded.canFold = true
return nil
}

func (expr *ParamExpressionExecutor) ResetForNextQuery() {
// do nothing.
if expr.null != nil {
expr.null.Free(expr.mp)
expr.null = nil
}
if expr.vec != nil {
expr.vec.Free(expr.mp)
expr.vec = nil
}
}

func (expr *VarExpressionExecutor) ResetForNextQuery() {
Expand Down
10 changes: 2 additions & 8 deletions pkg/sql/plan/function/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,8 @@ func RunFunctionDirectly(proc *process.Process, overloadID int64, inputs []*vect

vec := result.GetResultVector()
if fold {
// ToConst is a confused method. it just returns a new pointer to the same memory.
// so we need to duplicate it.
cvec, er := vec.ToConst(0, length, mp).Dup(mp)
result.Free()
if er != nil {
return nil, er
}
return cvec, nil
vec.ToConst()
vec.SetLength(length)
}
return vec, nil
}
Expand Down

0 comments on commit b6f9866

Please sign in to comment.