github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/operator/like_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 operator
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/common/mpool"
    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/matrixorigin/matrixone/pkg/vm/process"
    25  	"github.com/stretchr/testify/require"
    26  )
    27  
    28  func TestLikeVarchar(t *testing.T) {
    29  	cases := []struct {
    30  		name      string
    31  		vecs      []*vector.Vector
    32  		proc      *process.Process
    33  		wantBytes []bool
    34  	}{
    35  		{
    36  			name:      "TEST01",
    37  			vecs:      makeLikeVectors("RUNOOB.COM", "%COM", true, true),
    38  			proc:      testutil.NewProcessWithMPool(mpool.MustNewZero()),
    39  			wantBytes: []bool{true},
    40  		},
    41  		{
    42  			name:      "TEST02",
    43  			vecs:      makeLikeVectors("aaa", "aaa", true, true),
    44  			proc:      testutil.NewProcessWithMPool(mpool.MustNewZero()),
    45  			wantBytes: []bool{true},
    46  		},
    47  		{
    48  			name:      "TEST03",
    49  			vecs:      makeLikeVectors("123", "1%", true, true),
    50  			proc:      testutil.NewProcessWithMPool(mpool.MustNewZero()),
    51  			wantBytes: []bool{true},
    52  		},
    53  		{
    54  			name:      "TEST04",
    55  			vecs:      makeLikeVectors("SALESMAN", "%SAL%", true, true),
    56  			proc:      testutil.NewProcessWithMPool(mpool.MustNewZero()),
    57  			wantBytes: []bool{true},
    58  		},
    59  		{
    60  			name:      "TEST05",
    61  			vecs:      makeLikeVectors("MANAGER@@@", "MAN_", true, true),
    62  			proc:      testutil.NewProcessWithMPool(mpool.MustNewZero()),
    63  			wantBytes: []bool{false},
    64  		},
    65  		{
    66  			name:      "TEST06",
    67  			vecs:      makeLikeVectors("MANAGER@@@", "_", true, true),
    68  			proc:      testutil.NewProcessWithMPool(mpool.MustNewZero()),
    69  			wantBytes: []bool{false},
    70  		},
    71  		{
    72  			name:      "TEST07",
    73  			vecs:      makeLikeVectors("hello@world", "hello_world", true, true),
    74  			proc:      testutil.NewProcessWithMPool(mpool.MustNewZero()),
    75  			wantBytes: []bool{true},
    76  		},
    77  		{
    78  			name:      "TEST08",
    79  			vecs:      makeLikeVectors("**", "*", true, true),
    80  			proc:      testutil.NewProcessWithMPool(mpool.MustNewZero()),
    81  			wantBytes: []bool{false},
    82  		},
    83  		{
    84  			name:      "TEST09",
    85  			vecs:      makeLikeVectors("*", "*", true, true),
    86  			proc:      testutil.NewProcessWithMPool(mpool.MustNewZero()),
    87  			wantBytes: []bool{true},
    88  		},
    89  	}
    90  
    91  	for _, c := range cases {
    92  		t.Run(c.name, func(t *testing.T) {
    93  			likeRes, err := Like(c.vecs, c.proc)
    94  			if err != nil {
    95  				t.Fatal(err)
    96  			}
    97  			require.Equal(t, c.wantBytes, likeRes.Col.([]bool))
    98  		})
    99  	}
   100  }
   101  
   102  func TestLikeVarchar2(t *testing.T) {
   103  	procs := testutil.NewProc()
   104  	cases := []struct {
   105  		name       string
   106  		vecs       []*vector.Vector
   107  		proc       *process.Process
   108  		wantBytes  []bool
   109  		wantScalar bool
   110  	}{
   111  		{
   112  			name:       "TEST01",
   113  			vecs:       makeLikeVectors("RUNOOB.COM", "%COM", false, true),
   114  			proc:       procs,
   115  			wantBytes:  []bool{true},
   116  			wantScalar: false,
   117  		},
   118  		{
   119  			name:       "TEST02",
   120  			vecs:       makeLikeVectors("aaa", "aaa", false, true),
   121  			proc:       procs,
   122  			wantBytes:  []bool{true},
   123  			wantScalar: false,
   124  		},
   125  		{
   126  			name:       "TEST03",
   127  			vecs:       makeLikeVectors("123", "1%", false, true),
   128  			proc:       procs,
   129  			wantBytes:  []bool{true},
   130  			wantScalar: false,
   131  		},
   132  		{
   133  			name:       "TEST04",
   134  			vecs:       makeLikeVectors("SALESMAN", "%SAL%", false, true),
   135  			proc:       procs,
   136  			wantBytes:  []bool{true},
   137  			wantScalar: false,
   138  		},
   139  		{
   140  			name:       "TEST05",
   141  			vecs:       makeLikeVectors("MANAGER@@@", "MAN_", false, true),
   142  			proc:       procs,
   143  			wantBytes:  []bool{false},
   144  			wantScalar: false,
   145  		},
   146  		{
   147  			name:       "TEST06",
   148  			vecs:       makeLikeVectors("MANAGER@@@", "_", false, true),
   149  			proc:       procs,
   150  			wantBytes:  []bool{false},
   151  			wantScalar: false,
   152  		},
   153  		{
   154  			name:       "TEST07",
   155  			vecs:       makeLikeVectors("hello@world", "hello_world", false, true),
   156  			proc:       procs,
   157  			wantBytes:  []bool{true},
   158  			wantScalar: false,
   159  		},
   160  		{
   161  			name:       "TEST08",
   162  			vecs:       makeLikeVectors("a", "a", false, true),
   163  			proc:       procs,
   164  			wantBytes:  []bool{true},
   165  			wantScalar: false,
   166  		},
   167  		{
   168  			name:       "TEST09",
   169  			vecs:       makeLikeVectors("*", "*", false, true),
   170  			proc:       procs,
   171  			wantBytes:  []bool{true},
   172  			wantScalar: false,
   173  		},
   174  		{
   175  			name:       "TEST10",
   176  			vecs:       makeLikeVectors("*/", "*", false, true),
   177  			proc:       procs,
   178  			wantBytes:  []bool{false},
   179  			wantScalar: false,
   180  		},
   181  	}
   182  
   183  	for _, c := range cases {
   184  		t.Run(c.name, func(t *testing.T) {
   185  			likeRes, err := Like(c.vecs, c.proc)
   186  			if err != nil {
   187  				t.Fatal(err)
   188  			}
   189  			require.Equal(t, c.wantBytes, likeRes.Col.([]bool))
   190  			require.Equal(t, c.wantScalar, likeRes.IsScalar())
   191  		})
   192  	}
   193  }
   194  
   195  func makeStrVec(s string, isConst bool, n int) *vector.Vector {
   196  	if isConst {
   197  		return vector.NewConstString(types.T_varchar.ToType(), n, s, testutil.TestUtilMp)
   198  	} else {
   199  		return vector.NewWithStrings(types.T_varchar.ToType(), []string{s}, nil, testutil.TestUtilMp)
   200  	}
   201  }
   202  
   203  func makeLikeVectors(src string, pat string, isSrcConst bool, isPatConst bool) []*vector.Vector {
   204  	resVectors := make([]*vector.Vector, 2)
   205  	resVectors[0] = makeStrVec(src, isSrcConst, 10)
   206  	resVectors[1] = makeStrVec(pat, isPatConst, 10)
   207  	return resVectors
   208  }