github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/binary/findinset.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/nulls"
    19  	"github.com/matrixorigin/matrixone/pkg/container/types"
    20  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    21  	"github.com/matrixorigin/matrixone/pkg/vectorize/findinset"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    23  )
    24  
    25  func FindInSet(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    26  	left, right := vectors[0], vectors[1]
    27  	resultType := types.Type{Oid: types.T_uint64, Size: 8}
    28  	leftValues, rightValues := vector.MustStrCols(left), vector.MustStrCols(right)
    29  	switch {
    30  	case left.IsScalar() && right.IsScalar():
    31  		if left.ConstVectorIsNull() || right.ConstVectorIsNull() {
    32  			return proc.AllocScalarNullVector(resultType), nil
    33  		}
    34  		resultVector := vector.NewConst(resultType, 1)
    35  		resultValues := vector.MustTCols[uint64](resultVector)
    36  		findinset.FindInSetWithAllConst(leftValues[0], rightValues[0], resultValues)
    37  		return resultVector, nil
    38  	case left.IsScalar() && !right.IsScalar():
    39  		if left.ConstVectorIsNull() {
    40  			return proc.AllocScalarNullVector(resultType), nil
    41  		}
    42  		rlen := len(rightValues)
    43  		resultVector := vector.PreAllocType(resultType, rlen, rlen, proc.Mp())
    44  		resultValues := vector.MustTCols[uint64](resultVector)
    45  		nulls.Set(resultVector.Nsp, right.Nsp)
    46  		findinset.FindInSetWithLeftConst(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  		resLen := len(leftValues)
    53  		resultVector := vector.PreAllocType(resultType, resLen, resLen, proc.Mp())
    54  		resultValues := vector.MustTCols[uint64](resultVector)
    55  		nulls.Set(resultVector.Nsp, left.Nsp)
    56  		findinset.FindInSetWithRightConst(leftValues, rightValues[0], resultValues)
    57  		return resultVector, nil
    58  	}
    59  	resLen := len(leftValues)
    60  	resultVector := vector.PreAllocType(resultType, resLen, resLen, proc.Mp())
    61  	resultValues := vector.MustTCols[uint64](resultVector)
    62  	nulls.Or(left.Nsp, right.Nsp, resultVector.Nsp)
    63  	vector.SetCol(resultVector, findinset.FindInSet(leftValues, rightValues, resultValues))
    64  	return resultVector, nil
    65  }