github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/operator/isnull_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/stretchr/testify/require"
    24  )
    25  
    26  func TestIsNullNormal(t *testing.T) {
    27  	procs := testutil.NewProc()
    28  	vecs1 := []*vector.Vector{testutil.MakeInt32Vector([]int32{1, 14, 42, 7656, 4324234, 543534523}, nil)}
    29  	expected1 := []bool{false, false, false, false, false, false}
    30  	vecs2 := []*vector.Vector{testutil.MakeVarcharVector([]string{"1", "2", "3", "4"}, nil)}
    31  	expected2 := []bool{false, false, false, false}
    32  	vecs3 := []*vector.Vector{testutil.MakeTextVector([]string{"1", "2", "3", "4"}, nil)}
    33  	expected3 := []bool{false, false, false, false}
    34  
    35  	t.Run("test null normal", func(t *testing.T) {
    36  		result, err := IsNull(vecs1, procs)
    37  		if err != nil {
    38  			t.Fatal(err)
    39  		}
    40  		checkIsNullResult(t, result, expected1, false)
    41  		result, err = IsNull(vecs2, procs)
    42  		if err != nil {
    43  			t.Fatal(err)
    44  		}
    45  		checkIsNullResult(t, result, expected2, false)
    46  		result, err = IsNull(vecs3, procs)
    47  		if err != nil {
    48  			t.Fatal(err)
    49  		}
    50  		checkIsNullResult(t, result, expected3, false)
    51  	})
    52  }
    53  
    54  func TestIsNullNormalWithNull(t *testing.T) {
    55  	procs := testutil.NewProc()
    56  	vecs := []*vector.Vector{testutil.MakeInt32Vector([]int32{1, 14, 42, 7656, 4324234, 543534523}, []uint64{0, 1, 4})}
    57  	expected := []bool{true, true, false, false, true, false}
    58  	vecs2 := []*vector.Vector{testutil.MakeVarcharVector([]string{"1", "2", "3", "4"}, []uint64{1, 2})}
    59  	expected2 := []bool{false, true, true, false}
    60  	vecs3 := []*vector.Vector{testutil.MakeTextVector([]string{"1", "2", "3", "4"}, []uint64{1, 3})}
    61  	expected3 := []bool{false, true, false, true}
    62  
    63  	t.Run("test null normal with null", func(t *testing.T) {
    64  		result, err := IsNull(vecs, procs)
    65  		if err != nil {
    66  			t.Fatal(err)
    67  		}
    68  		checkIsNullResult(t, result, expected, false)
    69  		result, err = IsNull(vecs2, procs)
    70  		if err != nil {
    71  			t.Fatal(err)
    72  		}
    73  		checkIsNullResult(t, result, expected2, false)
    74  		result, err = IsNull(vecs3, procs)
    75  		if err != nil {
    76  			t.Fatal(err)
    77  		}
    78  		checkIsNullResult(t, result, expected3, false)
    79  	})
    80  }
    81  
    82  func TestIsNullScalar(t *testing.T) {
    83  	procs := testutil.NewProc()
    84  	vecs := []*vector.Vector{testutil.MakeScalarInt32(543534523, 1)}
    85  	expected := []bool{false}
    86  	vecs2 := []*vector.Vector{testutil.MakeScalarVarchar("cms", 1)}
    87  	expected2 := []bool{false}
    88  
    89  	t.Run("test null scalar", func(t *testing.T) {
    90  		result, err := IsNull(vecs, procs)
    91  		if err != nil {
    92  			t.Fatal(err)
    93  		}
    94  		checkIsNullResult(t, result, expected, true)
    95  		result, err = IsNull(vecs2, procs)
    96  		if err != nil {
    97  			t.Fatal(err)
    98  		}
    99  		checkIsNullResult(t, result, expected2, true)
   100  	})
   101  }
   102  
   103  func TestIsNullScalarNull(t *testing.T) {
   104  	procs := testutil.NewProc()
   105  	vecs := []*vector.Vector{testutil.MakeScalarNull(types.T_any, 0)}
   106  	expected := []bool{true}
   107  
   108  	t.Run("test null scalar null", func(t *testing.T) {
   109  		result, err := IsNull(vecs, procs)
   110  		if err != nil {
   111  			t.Fatal(err)
   112  		}
   113  
   114  		checkIsNullResult(t, result, expected, true)
   115  	})
   116  }
   117  
   118  func checkIsNullResult(t *testing.T, result *vector.Vector, expected []bool, isScalar bool) {
   119  	col := result.Col.([]bool)
   120  
   121  	require.Equal(t, expected, col)
   122  	require.Equal(t, isScalar, result.IsScalar())
   123  }