vitess.io/vitess@v0.16.2/go/vt/vtgate/vindexes/multicol_test.go (about)

     1  /*
     2  Copyright 2021 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package vindexes
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"vitess.io/vitess/go/sqltypes"
    24  	"vitess.io/vitess/go/vt/key"
    25  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func TestMultiColMisc(t *testing.T) {
    32  	vindex, err := CreateVindex("multicol", "multicol", map[string]string{
    33  		"column_count": "3",
    34  	})
    35  	require.NoError(t, err)
    36  
    37  	multiColVdx, isMultiColVdx := vindex.(*MultiCol)
    38  	assert.True(t, isMultiColVdx)
    39  
    40  	assert.Equal(t, 3, multiColVdx.Cost())
    41  	assert.Equal(t, "multicol", multiColVdx.String())
    42  	assert.True(t, multiColVdx.IsUnique())
    43  	assert.False(t, multiColVdx.NeedsVCursor())
    44  	assert.True(t, multiColVdx.PartialVindex())
    45  }
    46  
    47  func TestMultiColMap(t *testing.T) {
    48  	vindex, err := CreateVindex("multicol", "multicol", map[string]string{
    49  		"column_count": "3",
    50  	})
    51  	require.NoError(t, err)
    52  	mutiCol := vindex.(MultiColumn)
    53  
    54  	got, err := mutiCol.Map(context.Background(), nil, [][]sqltypes.Value{{
    55  		sqltypes.NewInt64(1), sqltypes.NewInt64(1), sqltypes.NewInt64(1),
    56  	}, {
    57  		sqltypes.NewInt64(255), sqltypes.NewInt64(1), sqltypes.NewInt64(1),
    58  	}, {
    59  		sqltypes.NewInt64(256), sqltypes.NewInt64(1), sqltypes.NewInt64(1),
    60  	}, {
    61  		// only one column provided, partial column for key range mapping.
    62  		sqltypes.NewInt64(1),
    63  	}, {
    64  		// only two column provided, partial column for key range mapping.
    65  		sqltypes.NewInt64(1), sqltypes.NewInt64(1),
    66  	}, {
    67  		// Invalid column value type.
    68  		sqltypes.NewVarBinary("abcd"), sqltypes.NewInt64(256), sqltypes.NewInt64(256),
    69  	}, {
    70  		// Invalid column value type.
    71  		sqltypes.NewInt64(256), sqltypes.NewInt64(256), sqltypes.NewVarBinary("abcd"),
    72  	}})
    73  	assert.NoError(t, err)
    74  
    75  	want := []key.Destination{
    76  		key.DestinationKeyspaceID("\x16\x6b\x40\x16\x6b\x40\x16\x6b"),
    77  		key.DestinationKeyspaceID("\x25\x4e\x88\x16\x6b\x40\x16\x6b"),
    78  		key.DestinationKeyspaceID("\xdd\x7c\x0b\x16\x6b\x40\x16\x6b"),
    79  		key.DestinationKeyRange{KeyRange: &topodatapb.KeyRange{Start: []byte("\x16\x6b\x40"), End: []byte("\x16\x6b\x41")}},
    80  		key.DestinationKeyRange{KeyRange: &topodatapb.KeyRange{Start: []byte("\x16\x6b\x40\x16\x6b\x40"), End: []byte("\x16\x6b\x40\x16\x6b\x41")}},
    81  		key.DestinationNone{},
    82  		key.DestinationNone{},
    83  	}
    84  	assert.Equal(t, want, got)
    85  }