github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/util/index_util_test.go (about)

     1  // Copyright 2021 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 util
    16  
    17  import (
    18  	"context"
    19  	"github.com/matrixorigin/matrixone/pkg/catalog"
    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/sql/plan/function/builtin/multi"
    24  	"github.com/matrixorigin/matrixone/pkg/testutil"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func TestBuildIndexTableName(t *testing.T) {
    32  	tests := []struct {
    33  		indexNames     string
    34  		uniques        bool
    35  		indexTableName string
    36  	}{
    37  		{
    38  			indexNames:     "a",
    39  			uniques:        true,
    40  			indexTableName: catalog.PrefixIndexTableName + "unique_",
    41  		},
    42  		{
    43  			indexNames:     "b",
    44  			uniques:        false,
    45  			indexTableName: catalog.PrefixIndexTableName + "secondary_",
    46  		},
    47  	}
    48  	for _, test := range tests {
    49  		unique := test.uniques
    50  		ctx := context.TODO()
    51  		indexTableName, err := BuildIndexTableName(ctx, unique)
    52  		require.Equal(t, indexTableName[:len(test.indexTableName)], test.indexTableName)
    53  		require.Equal(t, err, nil)
    54  	}
    55  }
    56  
    57  func TestBuildUniqueKeyBatch(t *testing.T) {
    58  	proc := testutil.NewProcess()
    59  	tests := []struct {
    60  		vecs  []*vector.Vector
    61  		attrs []string
    62  		parts []string
    63  		proc  *process.Process
    64  	}{
    65  		{
    66  			vecs: []*vector.Vector{
    67  				testutil.NewVector(3, types.T_int64.ToType(), proc.Mp(), false, []int64{1, 2, 3}),
    68  				testutil.NewVector(3, types.T_int64.ToType(), proc.Mp(), false, []int64{1, 2, 3}),
    69  				testutil.NewVector(3, types.T_int64.ToType(), proc.Mp(), false, []int64{1, 2, 3}),
    70  			},
    71  			attrs: []string{"a", "b", "c"},
    72  			parts: []string{"a", "b", "c"},
    73  			proc:  proc,
    74  		},
    75  		{
    76  			vecs: []*vector.Vector{
    77  				testutil.NewVector(3, types.T_int64.ToType(), proc.Mp(), false, []int64{1, 2, 3}),
    78  				testutil.NewVector(3, types.T_int64.ToType(), proc.Mp(), false, []int64{1, 2, 3}),
    79  				testutil.NewVector(3, types.T_int64.ToType(), proc.Mp(), false, []int64{1, 2, 3}),
    80  			},
    81  			attrs: []string{"a", "b", "c"},
    82  			parts: []string{"a"},
    83  			proc:  proc,
    84  		},
    85  	}
    86  	for _, test := range tests {
    87  		if len(test.parts) >= 2 {
    88  			vec, _ := multi.Serial(test.vecs, proc)
    89  			b, _ := BuildUniqueKeyBatch(test.vecs, test.attrs, test.parts, "", test.proc)
    90  			require.Equal(t, vec.Col, b.Vecs[0].Col)
    91  		} else {
    92  			b, _ := BuildUniqueKeyBatch(test.vecs, test.attrs, test.parts, "", test.proc)
    93  			require.Equal(t, test.vecs[0].Col, b.Vecs[0].Col)
    94  		}
    95  	}
    96  }
    97  
    98  func TestCompactUniqueKeyBatch(t *testing.T) {
    99  	proc := testutil.NewProcess()
   100  	tests := []struct {
   101  		vecs  []*vector.Vector
   102  		attrs []string
   103  		parts []string
   104  		proc  *process.Process
   105  	}{
   106  		{
   107  			vecs: []*vector.Vector{
   108  				testutil.NewVector(3, types.T_int64.ToType(), proc.Mp(), false, []int64{1, 2, 3}),
   109  				testutil.NewVector(3, types.T_int64.ToType(), proc.Mp(), false, []int64{1, 2, 3}),
   110  				testutil.NewVector(3, types.T_int64.ToType(), proc.Mp(), false, []int64{1, 2, 3}),
   111  			},
   112  			attrs: []string{"a", "b", "c"},
   113  			parts: []string{"a", "b", "c"},
   114  			proc:  proc,
   115  		},
   116  		{
   117  			vecs: []*vector.Vector{
   118  				testutil.NewVector(3, types.T_int64.ToType(), proc.Mp(), false, []int64{1, 2, 3}),
   119  				testutil.NewVector(3, types.T_int64.ToType(), proc.Mp(), false, []int64{1, 2, 3}),
   120  				testutil.NewVector(3, types.T_int64.ToType(), proc.Mp(), false, []int64{1, 2, 3}),
   121  			},
   122  			attrs: []string{"a", "b", "c"},
   123  			parts: []string{"b"},
   124  			proc:  proc,
   125  		},
   126  	}
   127  	for _, test := range tests {
   128  		nulls.Add(test.vecs[1].Nsp, 1)
   129  		//if JudgeIsCompositeIndexColumn(test.f) {
   130  		if len(test.parts) >= 2 {
   131  			//b, _ := BuildUniqueKeyBatch(test.vecs, test.attrs, test.f.Parts, "", test.proc)
   132  			b, _ := BuildUniqueKeyBatch(test.vecs, test.attrs, test.parts, "", test.proc)
   133  			require.Equal(t, 2, b.Vecs[0].Length())
   134  		} else {
   135  			//b, _ := BuildUniqueKeyBatch(test.vecs, test.attrs, test.f.Parts, "", test.proc)
   136  			b, _ := BuildUniqueKeyBatch(test.vecs, test.attrs, test.parts, "", test.proc)
   137  			require.Equal(t, 2, b.Vecs[0].Length())
   138  		}
   139  	}
   140  }