github.com/matrixorigin/matrixone@v1.2.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  	"testing"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/catalog"
    22  	"github.com/matrixorigin/matrixone/pkg/container/nulls"
    23  	"github.com/matrixorigin/matrixone/pkg/container/types"
    24  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    25  	"github.com/matrixorigin/matrixone/pkg/sql/plan/function"
    26  	"github.com/matrixorigin/matrixone/pkg/testutil"
    27  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    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  			vecs: []*vector.Vector{
    87  				testutil.NewVector(3, types.T_array_float32.ToType(), proc.Mp(), false, [][]float32{{1, 1, 1}, {2, 2, 2}, {3, 3, 3}}),
    88  				testutil.NewVector(3, types.T_array_float32.ToType(), proc.Mp(), false, [][]float32{{1, 1, 1}, {2, 2, 2}, {3, 3, 3}}),
    89  				testutil.NewVector(3, types.T_array_float32.ToType(), proc.Mp(), false, [][]float32{{1, 1, 1}, {2, 2, 2}, {3, 3, 3}}),
    90  			},
    91  			attrs: []string{"a", "b", "c"},
    92  			parts: []string{"a", "b", "c"},
    93  			proc:  proc,
    94  		},
    95  		{
    96  			vecs: []*vector.Vector{
    97  				testutil.NewVector(3, types.T_array_float32.ToType(), proc.Mp(), false, [][]float32{{1, 1, 1}, {2, 2, 2}, {3, 3, 3}}),
    98  				testutil.NewVector(3, types.T_array_float32.ToType(), proc.Mp(), false, [][]float32{{1, 1, 1}, {2, 2, 2}, {3, 3, 3}}),
    99  				testutil.NewVector(3, types.T_array_float32.ToType(), proc.Mp(), false, [][]float32{{1, 1, 1}, {2, 2, 2}, {3, 3, 3}}),
   100  			},
   101  			attrs: []string{"a", "b", "c"},
   102  			parts: []string{"a"},
   103  			proc:  proc,
   104  		},
   105  	}
   106  	for _, test := range tests {
   107  		packers := PackerList{}
   108  		if len(test.parts) >= 2 {
   109  			vec, _ := function.RunFunctionDirectly(proc, function.SerialFunctionEncodeID, test.vecs, test.vecs[0].Length())
   110  			b, _, err := BuildUniqueKeyBatch(test.vecs, test.attrs, test.parts, "", test.proc, &packers)
   111  			require.NoError(t, err)
   112  			require.Equal(t, vec.UnsafeGetRawData(), b.Vecs[0].UnsafeGetRawData())
   113  		} else {
   114  			b, _, err := BuildUniqueKeyBatch(test.vecs, test.attrs, test.parts, "", test.proc, &packers)
   115  			require.NoError(t, err)
   116  			require.Equal(t, test.vecs[0].UnsafeGetRawData(), b.Vecs[0].UnsafeGetRawData())
   117  		}
   118  		for _, p := range packers.ps {
   119  			p.FreeMem()
   120  		}
   121  	}
   122  }
   123  
   124  func TestCompactUniqueKeyBatch(t *testing.T) {
   125  	proc := testutil.NewProcess()
   126  	tests := []struct {
   127  		vecs  []*vector.Vector
   128  		attrs []string
   129  		parts []string
   130  		proc  *process.Process
   131  	}{
   132  		{
   133  			vecs: []*vector.Vector{
   134  				testutil.NewVector(3, types.T_int64.ToType(), proc.Mp(), false, []int64{1, 2, 3}),
   135  				testutil.NewVector(3, types.T_int64.ToType(), proc.Mp(), false, []int64{1, 2, 3}),
   136  				testutil.NewVector(3, types.T_int64.ToType(), proc.Mp(), false, []int64{1, 2, 3}),
   137  			},
   138  			attrs: []string{"a", "b", "c"},
   139  			parts: []string{"a", "b", "c"},
   140  			proc:  proc,
   141  		},
   142  		{
   143  			vecs: []*vector.Vector{
   144  				testutil.NewVector(3, types.T_int64.ToType(), proc.Mp(), false, []int64{1, 2, 3}),
   145  				testutil.NewVector(3, types.T_int64.ToType(), proc.Mp(), false, []int64{1, 2, 3}),
   146  				testutil.NewVector(3, types.T_int64.ToType(), proc.Mp(), false, []int64{1, 2, 3}),
   147  			},
   148  			attrs: []string{"a", "b", "c"},
   149  			parts: []string{"b"},
   150  			proc:  proc,
   151  		},
   152  		{
   153  			vecs: []*vector.Vector{
   154  				testutil.NewVector(3, types.T_array_float32.ToType(), proc.Mp(), false, [][]float32{{1, 1, 1}, {2, 2, 2}, {3, 3, 3}}),
   155  				testutil.NewVector(3, types.T_array_float32.ToType(), proc.Mp(), false, [][]float32{{1, 1, 1}, {2, 2, 2}, {3, 3, 3}}),
   156  				testutil.NewVector(3, types.T_array_float32.ToType(), proc.Mp(), false, [][]float32{{1, 1, 1}, {2, 2, 2}, {3, 3, 3}}),
   157  			},
   158  			attrs: []string{"a", "b", "c"},
   159  			parts: []string{"a", "b", "c"},
   160  			proc:  proc,
   161  		},
   162  		{
   163  			vecs: []*vector.Vector{
   164  				testutil.NewVector(3, types.T_array_float32.ToType(), proc.Mp(), false, [][]float32{{1, 1, 1}, {2, 2, 2}, {3, 3, 3}}),
   165  				testutil.NewVector(3, types.T_array_float32.ToType(), proc.Mp(), false, [][]float32{{1, 1, 1}, {2, 2, 2}, {3, 3, 3}}),
   166  				testutil.NewVector(3, types.T_array_float32.ToType(), proc.Mp(), false, [][]float32{{1, 1, 1}, {2, 2, 2}, {3, 3, 3}}),
   167  			},
   168  			attrs: []string{"a", "b", "c"},
   169  			parts: []string{"b"},
   170  			proc:  proc,
   171  		},
   172  	}
   173  	for _, test := range tests {
   174  		nulls.Add(test.vecs[1].GetNulls(), 1)
   175  		//if JudgeIsCompositeIndexColumn(test.f) {
   176  		packers := PackerList{}
   177  		if len(test.parts) >= 2 {
   178  			//b, _ := BuildUniqueKeyBatch(test.vecs, test.attrs, test.f.Parts, "", test.proc)
   179  			b, _, err := BuildUniqueKeyBatch(test.vecs, test.attrs, test.parts, "", test.proc, &packers)
   180  			require.NoError(t, err)
   181  			require.Equal(t, 2, b.Vecs[0].Length())
   182  		} else {
   183  			//b, _ := BuildUniqueKeyBatch(test.vecs, test.attrs, test.f.Parts, "", test.proc)
   184  			b, _, err := BuildUniqueKeyBatch(test.vecs, test.attrs, test.parts, "", test.proc, &packers)
   185  			require.NoError(t, err)
   186  			require.Equal(t, 2, b.Vecs[0].Length())
   187  		}
   188  		for _, p := range packers.ps {
   189  			p.FreeMem()
   190  		}
   191  	}
   192  }