github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/binary/power.go (about)

     1  // Copyright 2022 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package binary
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/container/types"
    19  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    20  	"github.com/matrixorigin/matrixone/pkg/vectorize/power"
    21  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    22  )
    23  
    24  func Power(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    25  	left, right := vectors[0], vectors[1]
    26  	resultType := types.T_float64.ToType()
    27  	leftValues, rightValues := vector.MustTCols[float64](left), vector.MustTCols[float64](right)
    28  	switch {
    29  	case left.IsScalar() && right.IsScalar():
    30  		if left.ConstVectorIsNull() || right.ConstVectorIsNull() {
    31  			return proc.AllocScalarNullVector(resultType), nil
    32  		}
    33  		resultVector := vector.NewConst(resultType, 1)
    34  		resultValues := make([]float64, 1)
    35  		vector.SetCol(resultVector, power.Power(leftValues, rightValues, resultValues))
    36  		return resultVector, nil
    37  	case left.IsScalar() && !right.IsScalar():
    38  		if left.ConstVectorIsNull() {
    39  			return proc.AllocScalarNullVector(resultType), nil
    40  		}
    41  		resultVector, err := proc.AllocVectorOfRows(resultType, int64(len(rightValues)), right.Nsp)
    42  		if err != nil {
    43  			return nil, err
    44  		}
    45  		resultValues := vector.MustTCols[float64](resultVector)
    46  		power.PowerScalarLeftConst(leftValues[0], rightValues, resultValues)
    47  		return resultVector, nil
    48  	case !left.IsScalar() && right.IsScalar():
    49  		if right.ConstVectorIsNull() {
    50  			return proc.AllocScalarNullVector(resultType), nil
    51  		}
    52  		resultVector, err := proc.AllocVectorOfRows(resultType, int64(len(leftValues)), left.Nsp)
    53  		if err != nil {
    54  			return nil, err
    55  		}
    56  		resultValues := vector.MustTCols[float64](resultVector)
    57  		power.PowerScalarRightConst(rightValues[0], leftValues, resultValues)
    58  		return resultVector, nil
    59  	}
    60  	resultVector, err := proc.AllocVectorOfRows(resultType, int64(len(rightValues)), nil)
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  	resultValues := vector.MustTCols[float64](resultVector)
    65  	power.Power(leftValues, rightValues, resultValues)
    66  	return resultVector, nil
    67  }