github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/multi/lpad_test.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  	"testing"
    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  	"github.com/matrixorigin/matrixone/pkg/testutil"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func TestLpadVarchar(t *testing.T) {
    28  	cases := []struct {
    29  		name      string
    30  		vecs      []*vector.Vector
    31  		wantBytes []byte
    32  	}{
    33  		{
    34  			name:      "TEST01",
    35  			vecs:      makeLpadVectors("hello", 1, "#", []int{1, 1, 1}),
    36  			wantBytes: []byte("h"),
    37  		},
    38  		{
    39  			name:      "TEST02",
    40  			vecs:      makeLpadVectors("hello", 10, "#", []int{1, 1, 1}),
    41  			wantBytes: []byte("#####hello"),
    42  		},
    43  		{
    44  			name:      "TEST03",
    45  			vecs:      makeLpadVectors("hello", 15, "#@&", []int{1, 1, 1}),
    46  			wantBytes: []byte("#@&#@&#@&#hello"),
    47  		},
    48  		{
    49  			name:      "TEST04",
    50  			vecs:      makeLpadVectors("12345678", 10, "abcdefgh", []int{1, 1, 1}),
    51  			wantBytes: []byte("ab12345678"),
    52  		},
    53  		{
    54  			name:      "TEST05",
    55  			vecs:      makeLpadVectors("hello", 0, "#@&", []int{1, 1, 1}),
    56  			wantBytes: []byte(""),
    57  		},
    58  		{
    59  			name:      "TEST06",
    60  			vecs:      makeLpadVectors("hello", -1, "#@&", []int{1, 1, 1}),
    61  			wantBytes: []byte(nil),
    62  		},
    63  		{
    64  			name:      "Tx",
    65  			vecs:      makeLpadVectors("hello", 1, "", []int{1, 1, 1}),
    66  			wantBytes: []byte(""),
    67  		},
    68  		{
    69  			name:      "Tx2",
    70  			vecs:      makeLpadVectors("", 5, "x", []int{1, 1, 1}),
    71  			wantBytes: []byte("xxxxx"),
    72  		},
    73  		{
    74  			name:      "Tx3",
    75  			vecs:      makeLpadVectors("你好", 10, "再见", []int{1, 1, 1}),
    76  			wantBytes: []byte("再见再见再见再见你好"),
    77  		},
    78  		{
    79  			name:      "tx4",
    80  			vecs:      makeLpadVectors("hello", -1, "#@&", []int{0, 0, 0}),
    81  			wantBytes: []byte(nil),
    82  		},
    83  		{
    84  			name:      "tx5",
    85  			vecs:      makeLpadVectors("hello", -1, "#@&", []int{0, 0, 1}),
    86  			wantBytes: []byte(nil),
    87  		},
    88  		{
    89  			name:      "tx6",
    90  			vecs:      makeLpadVectors("hello", -1, "#@&", []int{0, 1, 0}),
    91  			wantBytes: []byte(nil),
    92  		},
    93  		{
    94  			name:      "tx6",
    95  			vecs:      makeLpadVectors("hello", -1, "#@&", []int{1, 0, 0}),
    96  			wantBytes: []byte(nil),
    97  		},
    98  		{
    99  			name:      "tx6",
   100  			vecs:      makeLpadVectors("hello", -1, "#@&", []int{1, 1, 0}),
   101  			wantBytes: []byte(nil),
   102  		},
   103  		{
   104  			name:      "tx6",
   105  			vecs:      makeLpadVectors("hello", -1, "#@&", []int{1, 0, 1}),
   106  			wantBytes: []byte(nil),
   107  		},
   108  		{
   109  			name:      "tx6",
   110  			vecs:      makeLpadVectors("hello", -1, "#@&", []int{0, 1, 1}),
   111  			wantBytes: []byte(nil),
   112  		},
   113  		{
   114  			name:      "tx6",
   115  			vecs:      makeLpadVectors("hello", -1, "#@&", []int{1, 1, 1}),
   116  			wantBytes: []byte(nil),
   117  		},
   118  		{
   119  			name:      "tx6",
   120  			vecs:      makeLpadVectors("a你", 15, "见", []int{1, 1, 1}),
   121  			wantBytes: []byte("见见见见见见见见见见见见见a你"),
   122  		},
   123  		{
   124  			name:      "tx6",
   125  			vecs:      makeLpadVectors("a你a", 15, "见a", []int{1, 1, 1}),
   126  			wantBytes: []byte("见a见a见a见a见a见aa你a"),
   127  		},
   128  		{
   129  			name:      "tx6",
   130  			vecs:      makeLpadVectors("a你aa", 15, "见aa", []int{1, 1, 1}),
   131  			wantBytes: []byte("见aa见aa见aa见aa你aa"),
   132  		},
   133  		{
   134  			name:      "tx6",
   135  			vecs:      makeLpadVectors("a你aaa", 15, "见aaa", []int{1, 1, 1}),
   136  			wantBytes: []byte("见aaa见aaa见aa你aaa"),
   137  		},
   138  		{
   139  			name:      "tx6",
   140  			vecs:      makeLpadVectors("a你aaaa", 15, "见aaaa", []int{1, 1, 1}),
   141  			wantBytes: []byte("见aaaa见aaaa你aaaa"),
   142  		},
   143  		{
   144  			name:      "tx6",
   145  			vecs:      makeLpadVectors("aaaaaaaa", 4, "bbb", []int{1, 1, 1}),
   146  			wantBytes: []byte("aaaa"),
   147  		},
   148  	}
   149  
   150  	proc := testutil.NewProcess()
   151  	for _, c := range cases {
   152  		t.Run(c.name, func(t *testing.T) {
   153  			lpad, err := Lpad(c.vecs, proc)
   154  			if err != nil {
   155  				t.Fatal(err)
   156  			}
   157  			if c.wantBytes == nil {
   158  				ret := nulls.Contains(lpad.Nsp, 0)
   159  				require.Equal(t, ret, true)
   160  			} else {
   161  				require.Equal(t, c.wantBytes, lpad.GetBytes(0))
   162  			}
   163  
   164  		})
   165  	}
   166  
   167  }
   168  
   169  func makeLpadVectors(src string, length int64, pad string, nils []int) []*vector.Vector {
   170  	vec := make([]*vector.Vector, 3)
   171  	vec[0] = vector.NewConstString(types.T_varchar.ToType(), 1, src, testutil.TestUtilMp)
   172  	vec[1] = vector.NewConstFixed(types.T_int64.ToType(), 1, length, testutil.TestUtilMp)
   173  	vec[2] = vector.NewConstString(types.T_varchar.ToType(), 1, pad, testutil.TestUtilMp)
   174  	for i, n := range nils {
   175  		if n == 0 {
   176  			nulls.Add(vec[i].Nsp, uint64(i))
   177  		}
   178  	}
   179  	return vec
   180  }