github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/binary/startswith.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/startswith"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    23  )
    24  
    25  func Startswith(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    26  	left, right := vectors[0], vectors[1]
    27  	resultType := types.Type{Oid: types.T_uint8, Size: 1}
    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[uint8](resultVector)
    36  		startswith.StartsWithAllConst(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  		resultVector, err := proc.AllocVectorOfRows(resultType, int64(len(rightValues)), right.Nsp)
    43  		if err != nil {
    44  			return nil, err
    45  		}
    46  		resultValues := vector.MustTCols[uint8](resultVector)
    47  		startswith.StartsWithLeftConst(leftValues[0], rightValues, resultValues)
    48  		return resultVector, nil
    49  	case !left.IsScalar() && right.IsScalar():
    50  		if right.ConstVectorIsNull() {
    51  			return proc.AllocScalarNullVector(resultType), nil
    52  		}
    53  		resultVector, err := proc.AllocVectorOfRows(resultType, int64(len(leftValues)), left.Nsp)
    54  		if err != nil {
    55  			return nil, err
    56  		}
    57  		resultValues := vector.MustTCols[uint8](resultVector)
    58  		startswith.StartsWithRightConst(leftValues, rightValues[0], resultValues)
    59  		return resultVector, nil
    60  	}
    61  	resultVector, err := proc.AllocVectorOfRows(resultType, int64(len(leftValues)), nil)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  	resultValues := vector.MustTCols[uint8](resultVector)
    66  	nulls.Or(left.Nsp, right.Nsp, resultVector.Nsp)
    67  	startswith.StartsWith(leftValues, rightValues, resultValues)
    68  	return resultVector, nil
    69  }