github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/split_part/split_part.go (about)

     1  // Copyright 2021 - 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 split_part
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/container/nulls"
    19  	"strings"
    20  )
    21  
    22  func SplitSingle(str, sep string, cnt uint32) (string, bool) {
    23  	expectedLen := int(cnt + 1)
    24  	strSlice := strings.SplitN(str, sep, expectedLen)
    25  	if len(strSlice) < int(cnt) || strSlice[cnt-1] == "" {
    26  		return "", true
    27  	}
    28  	return strSlice[cnt-1], false
    29  }
    30  
    31  func compute(s1, s2 string, s3 uint32, rs []string, idx int, nsp *nulls.Nulls) {
    32  	res, isNull := SplitSingle(s1, s2, s3)
    33  	if isNull {
    34  		nsp.Set(uint64(idx))
    35  		return
    36  	}
    37  	rs[idx] = res
    38  }
    39  
    40  // SplitPart1 is the implementation of split_part(string, string, uint) when the 3rd arguments are not scalar.
    41  func SplitPart1(s1, s2 []string, s3 []uint32, nsps []*nulls.Nulls, rs []string, rnsp *nulls.Nulls) {
    42  	str, sep := s1[0], s2[0]
    43  	for i := 0; i < len(s3); i++ {
    44  		if nsps[2].Contains(uint64(i)) {
    45  			rnsp.Set(uint64(i))
    46  			continue
    47  		}
    48  		count := s3[i]
    49  		compute(str, sep, count, rs, i, rnsp)
    50  	}
    51  }
    52  
    53  // SplitPart2 is the implementation of split_part(string, string, uint) when the 2nd argument is not scalar.
    54  func SplitPart2(s1, s2 []string, s3 []uint32, nsps []*nulls.Nulls, rs []string, rnsp *nulls.Nulls) {
    55  	str, count := s1[0], s3[0]
    56  	for i := 0; i < len(s2); i++ {
    57  		if nsps[1].Contains(uint64(i)) {
    58  			rnsp.Set(uint64(i))
    59  			continue
    60  		}
    61  		sep := s2[i]
    62  		compute(str, sep, count, rs, i, rnsp)
    63  	}
    64  }
    65  
    66  // SplitPart3 is the implementation of split_part(string, string, uint) when the 2nd and 3rd arguments are not scalar.
    67  func SplitPart3(s1, s2 []string, s3 []uint32, nsps []*nulls.Nulls, rs []string, rnsp *nulls.Nulls) {
    68  	str := s1[0]
    69  	for i := 0; i < len(s2); i++ {
    70  		if nsps[1].Contains(uint64(i)) || nsps[2].Contains(uint64(i)) {
    71  			rnsp.Set(uint64(i))
    72  			continue
    73  		}
    74  		sep, count := s2[i], s3[i]
    75  		compute(str, sep, count, rs, i, rnsp)
    76  	}
    77  }
    78  
    79  // SplitPart4 is the implementation of split_part(string, string, uint) when the 1st arguments is not scalar.
    80  func SplitPart4(s1, s2 []string, s3 []uint32, nsps []*nulls.Nulls, rs []string, rnsp *nulls.Nulls) {
    81  	sep, count := s2[0], s3[0]
    82  	for i := 0; i < len(s1); i++ {
    83  		if nsps[0].Contains(uint64(i)) {
    84  			rnsp.Set(uint64(i))
    85  			continue
    86  		}
    87  		str := s1[i]
    88  		compute(str, sep, count, rs, i, rnsp)
    89  	}
    90  }
    91  
    92  // SplitPart5 is the implementation of split_part(string, string, uint) when the 1st and 3rd arguments are not scalar.
    93  func SplitPart5(s1, s2 []string, s3 []uint32, nsps []*nulls.Nulls, rs []string, rnsp *nulls.Nulls) {
    94  	sep := s2[0]
    95  	for i := 0; i < len(s1); i++ {
    96  		if nsps[0].Contains(uint64(i)) || nsps[2].Contains(uint64(i)) {
    97  			rnsp.Set(uint64(i))
    98  			continue
    99  		}
   100  		str, count := s1[i], s3[i]
   101  		compute(str, sep, count, rs, i, rnsp)
   102  	}
   103  }
   104  
   105  // SplitPart6 is the implementation of split_part(string, string, uint) when the 1st and 2nd arguments are not scalar.
   106  func SplitPart6(s1, s2 []string, s3 []uint32, nsps []*nulls.Nulls, rs []string, rnsp *nulls.Nulls) {
   107  	count := s3[0]
   108  	for i := 0; i < len(s1); i++ {
   109  		if nsps[0].Contains(uint64(i)) || nsps[1].Contains(uint64(i)) {
   110  			rnsp.Set(uint64(i))
   111  			continue
   112  		}
   113  		str, sep := s1[i], s2[i]
   114  		compute(str, sep, count, rs, i, rnsp)
   115  	}
   116  }
   117  
   118  // SplitPart7 is the implementation of split_part(string, string, uint) when the 1st, 2nd and 3rd arguments are not scalar.
   119  func SplitPart7(s1, s2 []string, s3 []uint32, nsps []*nulls.Nulls, rs []string, rnsp *nulls.Nulls) {
   120  	for i := 0; i < len(s1); i++ {
   121  		if nsps[0].Contains(uint64(i)) || nsps[1].Contains(uint64(i)) || nsps[2].Contains(uint64(i)) {
   122  			rnsp.Set(uint64(i))
   123  			continue
   124  		}
   125  		str, sep, count := s1[i], s2[i], s3[i]
   126  		compute(str, sep, count, rs, i, rnsp)
   127  	}
   128  }