github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/binary/instr.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/instr"
    22  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    23  )
    24  
    25  func Instr(vecs []*vector.Vector, proc *process.Process) (ret *vector.Vector, err error) {
    26  	defer func() {
    27  		if err != nil && ret != nil {
    28  			ret.Free(proc.Mp())
    29  		}
    30  	}()
    31  	v1, v2 := vecs[0], vecs[1]
    32  	maxLen := v1.Length()
    33  	if v2.Length() > v1.Length() {
    34  		maxLen = v2.Length()
    35  	}
    36  	resultType := types.T_int64.ToType()
    37  	if v1.IsScalarNull() || v2.IsScalarNull() {
    38  		ret = proc.AllocConstNullVector(resultType, maxLen)
    39  		return
    40  	}
    41  	s1, s2 := vector.MustStrCols(v1), vector.MustStrCols(v2)
    42  	if v1.IsScalar() && v2.IsScalar() {
    43  		ret = proc.AllocScalarVector(resultType)
    44  		str, substr := s1[0], s2[0]
    45  		err = ret.Append(instr.Single(str, substr), false, proc.Mp())
    46  		return
    47  	}
    48  	ret, err = proc.AllocVectorOfRows(resultType, int64(maxLen), nil)
    49  	if err != nil {
    50  		return
    51  	}
    52  	rs := vector.MustTCols[int64](ret)
    53  	instr.Instr(s1, s2, []*nulls.Nulls{v1.Nsp, v2.Nsp}, rs, ret.Nsp)
    54  	return
    55  }