github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/operator/is_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/container/types"
    21  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    22  	"github.com/matrixorigin/matrixone/pkg/testutil"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func TestIs(t *testing.T) {
    28  	procs := testutil.NewProc()
    29  	cases := []struct {
    30  		name     string
    31  		proc     *process.Process
    32  		left     []bool
    33  		nsp      []uint64
    34  		right    bool
    35  		isScalar bool
    36  		expected []bool
    37  	}{
    38  		{
    39  			name:     "01 - normal test",
    40  			proc:     procs,
    41  			left:     []bool{true, false, true, false, false, true},
    42  			right:    true,
    43  			expected: []bool{true, false, true, false, false, true},
    44  			isScalar: false,
    45  		},
    46  		{
    47  			name:     "02 - normal test",
    48  			proc:     procs,
    49  			left:     []bool{true, false, true, false, false, true},
    50  			right:    false,
    51  			expected: []bool{false, true, false, true, true, false},
    52  			isScalar: false,
    53  		},
    54  		{
    55  			name:     "03 - scalar test",
    56  			proc:     procs,
    57  			left:     []bool{true},
    58  			right:    false,
    59  			expected: []bool{false},
    60  			isScalar: true,
    61  		},
    62  		{
    63  			name:     "04 - null test",
    64  			proc:     procs,
    65  			right:    true,
    66  			expected: []bool{false},
    67  			isScalar: true,
    68  		},
    69  	}
    70  
    71  	for _, c := range cases {
    72  		t.Run(c.name, func(t *testing.T) {
    73  			vecs := makeIsAndIsNotTestVectors(c.left, c.right, c.isScalar)
    74  			result, err := Is(vecs, c.proc)
    75  			if err != nil {
    76  				t.Fatal(err)
    77  			}
    78  			col := result.Col.([]bool)
    79  			require.Equal(t, c.expected, col)
    80  			require.Equal(t, c.isScalar, result.IsScalar())
    81  		})
    82  	}
    83  }
    84  
    85  func makeIsAndIsNotTestVectors(left []bool, right bool, isScalar bool) []*vector.Vector {
    86  	vec := make([]*vector.Vector, 2)
    87  	if left != nil {
    88  		vec[0] = testutil.MakeBoolVector(left)
    89  		if isScalar {
    90  			vec[0].MakeScalar(1)
    91  		}
    92  	} else {
    93  		vec[0] = testutil.MakeScalarNull(types.T_bool, 0)
    94  	}
    95  
    96  	vec[1] = testutil.MakeScalarBool(right, len(left))
    97  
    98  	return vec
    99  }