github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/binary/endswith.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/endswith"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    23  )
    24  
    25  func Endswith(vectors []*vector.Vector, proc *process.Process) (*vector.Vector, error) {
    26  	left, right := vectors[0], vectors[1]
    27  	// XXX Why result type is uint8, not bool?
    28  	resultType := types.Type{Oid: types.T_uint8, Size: 1}
    29  	leftValues, rightValues := vector.MustStrCols(left), vector.MustStrCols(right)
    30  	switch {
    31  	case left.IsScalar() && right.IsScalar():
    32  		if left.ConstVectorIsNull() || right.ConstVectorIsNull() {
    33  			return proc.AllocScalarNullVector(resultType), nil
    34  		}
    35  		resultVector := vector.NewConst(resultType, 1)
    36  		resultValues := make([]uint8, 1)
    37  		endswith.EndsWithAllConst(leftValues, rightValues, resultValues)
    38  		vector.SetCol(resultVector, resultValues)
    39  		return resultVector, nil
    40  	case left.IsScalar() && !right.IsScalar():
    41  		if left.ConstVectorIsNull() {
    42  			return proc.AllocScalarNullVector(resultType), nil
    43  		}
    44  		resultVector, err := proc.AllocVectorOfRows(resultType, int64(len(rightValues)), right.Nsp)
    45  		if err != nil {
    46  			return nil, err
    47  		}
    48  		resultValues := vector.MustTCols[uint8](resultVector)
    49  		endswith.EndsWithLeftConst(leftValues, rightValues, resultValues)
    50  		return resultVector, nil
    51  	case !left.IsScalar() && right.IsScalar():
    52  		if right.ConstVectorIsNull() {
    53  			return proc.AllocScalarNullVector(resultType), nil
    54  		}
    55  		resultVector, err := proc.AllocVectorOfRows(resultType, int64(len(leftValues)), left.Nsp)
    56  		if err != nil {
    57  			return nil, err
    58  		}
    59  		resultValues := vector.MustTCols[uint8](resultVector)
    60  		endswith.EndsWithRightConst(leftValues, rightValues, resultValues)
    61  		return resultVector, nil
    62  	}
    63  
    64  	resultVector, err := proc.AllocVectorOfRows(resultType, int64(len(rightValues)), nil)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	nulls.Or(left.Nsp, right.Nsp, resultVector.Nsp)
    69  	resultValues := vector.MustTCols[uint8](resultVector)
    70  	endswith.EndsWith(leftValues, rightValues, resultValues)
    71  	return resultVector, nil
    72  }