diff --git a/pkg/container/vector/vector.go b/pkg/container/vector/vector.go index 39e839af4538..79e2d729230d 100644 --- a/pkg/container/vector/vector.go +++ b/pkg/container/vector/vector.go @@ -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 diff --git a/pkg/sql/colexec/debugTools.go b/pkg/sql/colexec/debugTools.go index bbb949a2d3f9..13586fd192d6 100644 --- a/pkg/sql/colexec/debugTools.go +++ b/pkg/sql/colexec/debugTools.go @@ -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)) diff --git a/pkg/sql/colexec/evalExpression.go b/pkg/sql/colexec/evalExpression.go index 23971707b0f3..ca8e844d64f3 100644 --- a/pkg/sql/colexec/evalExpression.go +++ b/pkg/sql/colexec/evalExpression.go @@ -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 @@ -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 { diff --git a/pkg/sql/colexec/evalExpressionOpt.go b/pkg/sql/colexec/evalExpressionOpt.go index 5fc6750d7fe6..5039b3c5fd5e 100644 --- a/pkg/sql/colexec/evalExpressionOpt.go +++ b/pkg/sql/colexec/evalExpressionOpt.go @@ -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 } diff --git a/pkg/sql/colexec/evalExpressionReset.go b/pkg/sql/colexec/evalExpressionReset.go index 64251089ad52..055c7188b635 100644 --- a/pkg/sql/colexec/evalExpressionReset.go +++ b/pkg/sql/colexec/evalExpressionReset.go @@ -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 { @@ -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) { @@ -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() { diff --git a/pkg/sql/plan/function/function.go b/pkg/sql/plan/function/function.go index 72d199df3c20..1ac7d7ac4c48 100644 --- a/pkg/sql/plan/function/function.go +++ b/pkg/sql/plan/function/function.go @@ -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 }