github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/split_part/split_part_test.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  	"github.com/matrixorigin/matrixone/pkg/container/types"
    20  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    21  	"github.com/matrixorigin/matrixone/pkg/testutil"
    22  	"testing"
    23  )
    24  
    25  var (
    26  	mp         = testutil.TestUtilMp
    27  	v1         = testutil.NewStringVector(10, types.T_varchar.ToType(), mp, false, []string{"a,b,c", "a,b,c", "a,b,c", "a,b,c", "a,b,c", "a,b,c", "a,b,c", "a,b,c", "a,b,c", "a,b,c"})
    28  	v2         = testutil.NewStringVector(10, types.T_varchar.ToType(), mp, false, []string{",", ",", ",", ",", ",", ",", ",", ",", ",", ","})
    29  	v3         = testutil.NewUInt32Vector(10, types.T_uint32.ToType(), mp, false, []uint32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
    30  	s1, s2, s3 = vector.MustStrCols(v1), vector.MustStrCols(v2), vector.MustTCols[uint32](v3)
    31  )
    32  
    33  func TestSplitSingle(t *testing.T) {
    34  	kases := []struct {
    35  		str    string
    36  		sep    string
    37  		cnt    uint32
    38  		res    string
    39  		isnull bool
    40  	}{
    41  		{"a,b,c", ",", 1, "a", false},
    42  		{"a,b,c", ",", 2, "b", false},
    43  		{"a,b,c", ",", 3, "c", false},
    44  		{"a,b,c", ",", 4, "", true},
    45  		{"axbxc", "x", 1, "a", false},
    46  		{"axbxc", "bxc", 2, "", true},
    47  		{"axbxc", "bxc", 1, "ax", false},
    48  		{"axbxc", "bxcd", 1, "axbxc", false},
    49  		{"axbxc", "bxcd", 2, "", true},
    50  		{"啊啊啊x", "x", 1, "啊啊啊", false},
    51  		{"啊啊啊x", "x", 2, "", true},
    52  		{"1啊啊x啊x", "啊啊", 1, "1", false},
    53  		{"1啊啊x啊x", "啊啊", 2, "x啊x", false},
    54  		{"axbcd", "a", 2, "xbcd", false},
    55  	}
    56  	for _, k := range kases {
    57  		res, isNull := SplitSingle(k.str, k.sep, k.cnt)
    58  		if res != k.res || isNull != k.isnull {
    59  			t.Errorf("expected %v, %v, got %v, %v", k.res, k.isnull, res, isNull)
    60  		}
    61  	}
    62  }
    63  
    64  func TestSplitPart(t *testing.T) {
    65  	rs := make([]string, len(s1))
    66  	nsp := nulls.NewWithSize(len(s1))
    67  	SplitPart1(s1, s2, s3, []*nulls.Nulls{v1.Nsp, v2.Nsp, v3.Nsp}, rs, nsp)
    68  	SplitPart2(s1, s2, s3, []*nulls.Nulls{v1.Nsp, v2.Nsp, v3.Nsp}, rs, nsp)
    69  	SplitPart3(s1, s2, s3, []*nulls.Nulls{v1.Nsp, v2.Nsp, v3.Nsp}, rs, nsp)
    70  	SplitPart4(s1, s2, s3, []*nulls.Nulls{v1.Nsp, v2.Nsp, v3.Nsp}, rs, nsp)
    71  	SplitPart5(s1, s2, s3, []*nulls.Nulls{v1.Nsp, v2.Nsp, v3.Nsp}, rs, nsp)
    72  	SplitPart6(s1, s2, s3, []*nulls.Nulls{v1.Nsp, v2.Nsp, v3.Nsp}, rs, nsp)
    73  	SplitPart7(s1, s2, s3, []*nulls.Nulls{v1.Nsp, v2.Nsp, v3.Nsp}, rs, nsp)
    74  	v1.Nsp.Set(0)
    75  	v2.Nsp.Set(0)
    76  	v3.Nsp.Set(0)
    77  	v1.Nsp.Set(5)
    78  	v2.Nsp.Set(5)
    79  	v3.Nsp.Set(5)
    80  	SplitPart1(s1, s2, s3, []*nulls.Nulls{v1.Nsp, v2.Nsp, v3.Nsp}, rs, nsp)
    81  	SplitPart2(s1, s2, s3, []*nulls.Nulls{v1.Nsp, v2.Nsp, v3.Nsp}, rs, nsp)
    82  	SplitPart3(s1, s2, s3, []*nulls.Nulls{v1.Nsp, v2.Nsp, v3.Nsp}, rs, nsp)
    83  	SplitPart4(s1, s2, s3, []*nulls.Nulls{v1.Nsp, v2.Nsp, v3.Nsp}, rs, nsp)
    84  	SplitPart5(s1, s2, s3, []*nulls.Nulls{v1.Nsp, v2.Nsp, v3.Nsp}, rs, nsp)
    85  	SplitPart6(s1, s2, s3, []*nulls.Nulls{v1.Nsp, v2.Nsp, v3.Nsp}, rs, nsp)
    86  	SplitPart7(s1, s2, s3, []*nulls.Nulls{v1.Nsp, v2.Nsp, v3.Nsp}, rs, nsp)
    87  }