github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/function/func_locate.go (about)

     1  // Copyright 2023 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 function
    16  
    17  import (
    18  	"bytes"
    19  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    20  	"github.com/matrixorigin/matrixone/pkg/sql/plan/function/functionUtil"
    21  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    22  	"strings"
    23  	"unicode/utf8"
    24  )
    25  
    26  // LOCATE(substr, str)
    27  func buildInLocate2Args(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
    28  	rs := vector.MustFunctionResult[int64](result)
    29  	substrVs := vector.GenerateFunctionStrParameter(parameters[0])
    30  	strVs := vector.GenerateFunctionStrParameter(parameters[1])
    31  
    32  	for i := uint64(0); i < uint64(length); i++ {
    33  		substr, null1 := substrVs.GetStrValue(i)
    34  		str, null2 := strVs.GetStrValue(i)
    35  
    36  		if null1 || null2 {
    37  			if err := rs.Append(0, true); err != nil {
    38  				return err
    39  			}
    40  		} else {
    41  			pos := Locate2Args(functionUtil.QuickBytesToStr(bytes.ToUpper(str)), functionUtil.QuickBytesToStr(bytes.ToUpper(substr)))
    42  			rs.AppendMustValue(pos)
    43  		}
    44  	}
    45  	return nil
    46  }
    47  
    48  // LOCATE(substr, str, [position])
    49  func buildInLocate3Args(parameters []*vector.Vector, result vector.FunctionResultWrapper, proc *process.Process, length int) error {
    50  	rs := vector.MustFunctionResult[int64](result)
    51  	substrVs := vector.GenerateFunctionStrParameter(parameters[0])
    52  	strVs := vector.GenerateFunctionStrParameter(parameters[1])
    53  	posVs := vector.GenerateFunctionFixedTypeParameter[int64](parameters[2])
    54  
    55  	for i := uint64(0); i < uint64(length); i++ {
    56  		substr, null1 := substrVs.GetStrValue(i)
    57  		str, null2 := strVs.GetStrValue(i)
    58  		position, null3 := posVs.GetValue(i)
    59  
    60  		if null1 || null2 || null3 {
    61  			if err := rs.Append(0, true); err != nil {
    62  				return err
    63  			}
    64  		} else {
    65  			pos := Locate3Args(functionUtil.QuickBytesToStr(bytes.ToUpper(str)), functionUtil.QuickBytesToStr(bytes.ToUpper(substr)), position)
    66  			rs.AppendMustValue(pos)
    67  		}
    68  	}
    69  	return nil
    70  }
    71  
    72  // evalate LOCATE(substr, str)
    73  func Locate2Args(str string, subStr string) int64 {
    74  	subStrLen := len(subStr)
    75  	if subStrLen == 0 {
    76  		return 1
    77  	}
    78  	ret, idx := 0, strings.Index(str, subStr)
    79  	if idx >= 0 {
    80  		prefix := str[:idx]
    81  		prefixLen := utf8.RuneCountInString(prefix)
    82  		ret = prefixLen + 1
    83  	}
    84  	//if idx != -1 {
    85  	//	ret = idx + 1
    86  	//}
    87  
    88  	return int64(ret)
    89  }
    90  
    91  // evalate LOCATE(substr, str, pos)
    92  func Locate3Args(str string, subStr string, pos int64) int64 {
    93  	// Transfer the argument which starts from 1 to real index which starts from 0.
    94  	pos--
    95  	subStrLen := len(subStr)
    96  	if pos < 0 || pos > int64(len(str)-subStrLen) {
    97  		return 0
    98  	} else if subStrLen == 0 {
    99  		return pos + 1
   100  	}
   101  
   102  	slice := getSubstring(str, int(pos))
   103  
   104  	idx := strings.Index(slice, subStr)
   105  	if idx >= 0 {
   106  		prefix := slice[:idx]
   107  		prefixLen := utf8.RuneCountInString(prefix)
   108  		return pos + int64(prefixLen) + 1
   109  	}
   110  
   111  	return 0
   112  }
   113  
   114  // getSubstring Used to obtain the starting position of a substring in characters.
   115  // takes two parameters: str is the target string, and start is the starting position (in characters) of the substring to be obtained.
   116  func getSubstring(str string, start int) string {
   117  	if start >= utf8.RuneCountInString(str) {
   118  		return ""
   119  	}
   120  
   121  	byteOffset := 0
   122  	for i := 0; i < start; i++ {
   123  		_, size := utf8.DecodeRuneInString(str[byteOffset:])
   124  		byteOffset += size
   125  	}
   126  
   127  	return str[byteOffset:]
   128  }