github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/multi/substr_index.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 multi 16 17 import ( 18 "math" 19 20 "github.com/matrixorigin/matrixone/pkg/container/nulls" 21 "github.com/matrixorigin/matrixone/pkg/container/types" 22 "github.com/matrixorigin/matrixone/pkg/container/vector" 23 substrindex "github.com/matrixorigin/matrixone/pkg/vectorize/subStrIndex" 24 "github.com/matrixorigin/matrixone/pkg/vm/process" 25 ) 26 27 func SubStrIndex(vecs []*vector.Vector, proc *process.Process) (*vector.Vector, error) { 28 if vecs[0].IsScalarNull() || vecs[1].IsScalarNull() || vecs[2].IsScalarNull() { 29 return proc.AllocScalarNullVector(vecs[0].Typ), nil 30 } 31 //get the first arg str 32 sourceCols := vector.MustStrCols(vecs[0]) 33 //get the second arg delim 34 delimCols := vector.MustStrCols(vecs[1]) 35 //get the third arg count 36 countCols := getCount(vecs[2]) 37 38 //calcute rows 39 rowCount := vector.Length(vecs[0]) 40 41 var resultVec *vector.Vector = nil 42 resultValues := make([]string, rowCount) 43 resultNsp := nulls.NewWithSize(rowCount) 44 45 // set null row 46 nulls.Or(vecs[0].Nsp, vecs[1].Nsp, resultNsp) 47 nulls.Or(vecs[2].Nsp, resultNsp, resultNsp) 48 49 constVectors := []bool{vecs[0].IsScalar(), vecs[1].IsScalar(), vecs[2].IsScalar()} 50 //get result values 51 substrindex.SubStrIndex(sourceCols, delimCols, countCols, rowCount, constVectors, resultValues) 52 resultVec = vector.NewWithStrings(types.T_varchar.ToType(), resultValues, resultNsp, proc.Mp()) 53 54 return resultVec, nil 55 } 56 57 func getCount(vec *vector.Vector) []int64 { 58 switch vec.GetType().Oid { 59 case types.T_float64: 60 vs := vector.MustTCols[float64](vec) 61 res := make([]int64, 0, len(vs)) 62 for _, v := range vs { 63 if v > float64(math.MaxInt64) { 64 res = append(res, math.MaxInt64) 65 } else if v < float64(math.MinInt64) { 66 res = append(res, math.MinInt64) 67 } else { 68 res = append(res, int64(v)) 69 } 70 } 71 return res 72 case types.T_uint64: 73 vs := vector.MustTCols[uint64](vec) 74 res := make([]int64, 0, len(vs)) 75 for _, v := range vs { 76 if v > uint64(math.MaxInt64) { 77 res = append(res, math.MaxInt64) 78 } else { 79 res = append(res, int64(v)) 80 } 81 } 82 return res 83 default: 84 return castTVecAsInt64(vec) 85 } 86 }