github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/unary/bin.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 unary
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    19  	"github.com/matrixorigin/matrixone/pkg/container/types"
    20  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    21  	"github.com/matrixorigin/matrixone/pkg/vectorize/bin"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    23  	"golang.org/x/exp/constraints"
    24  )
    25  
    26  func Bin[T constraints.Unsigned | constraints.Signed](vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    27  	return generalBin[T](vectors, proc, bin.Bin[T])
    28  }
    29  
    30  func BinFloat[T constraints.Float](vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    31  	return generalBin[T](vectors, proc, bin.BinFloat[T])
    32  }
    33  
    34  type binT interface {
    35  	constraints.Unsigned | constraints.Signed | constraints.Float
    36  }
    37  
    38  type binFun[T binT] func(*vector.Vector, *vector.Vector, *process.Process) error
    39  
    40  func generalBin[T binT](vectors []*vector.Vector, proc *process.Process, cb binFun[T]) (*vector.Vector, error) {
    41  	inputVector := vectors[0]
    42  	resultType := types.T_varchar.ToType()
    43  	if inputVector.IsScalar() {
    44  		if inputVector.ConstVectorIsNull() {
    45  			return proc.AllocScalarNullVector(resultType), nil
    46  		}
    47  		resultVector := proc.AllocScalarVector(resultType)
    48  		resultValues := make([]types.Varlena, 0, 1)
    49  		vector.SetCol(resultVector, resultValues)
    50  		err := cb(inputVector, resultVector, proc)
    51  		if err != nil {
    52  			return nil, moerr.NewInvalidInput(proc.Ctx, "The input value is out of range")
    53  		}
    54  		return resultVector, nil
    55  	} else {
    56  		resultVector, err := proc.AllocVectorOfRows(resultType, 0, inputVector.Nsp)
    57  		if err != nil {
    58  			return nil, err
    59  		}
    60  		err = cb(inputVector, resultVector, proc)
    61  		if err != nil {
    62  			return nil, moerr.NewInvalidInput(proc.Ctx, "The input value is out of range")
    63  		}
    64  		return resultVector, nil
    65  	}
    66  
    67  }