github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/sqle/index/key_builder_test.go (about) 1 // Copyright 2023 Dolthub, Inc. 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 index 16 17 import ( 18 "testing" 19 20 "github.com/stretchr/testify/require" 21 22 "github.com/dolthub/dolt/go/libraries/doltcore/schema" 23 "github.com/dolthub/dolt/go/store/types" 24 "github.com/dolthub/dolt/go/store/val" 25 ) 26 27 func TestGetIndexKeyMapping(t *testing.T) { 28 tests := []struct { 29 Name string 30 AllCols []schema.Column 31 IdxCols []string 32 Split int 33 Mapping val.OrdinalMapping 34 }{ 35 { 36 Name: "basic", 37 AllCols: []schema.Column{ 38 schema.NewColumn("col1", 0, types.IntKind, true), 39 schema.NewColumn("col2", 1, types.IntKind, false), 40 }, 41 IdxCols: []string{"col2"}, 42 Split: 1, 43 Mapping: []int{1, 0}, 44 }, 45 { 46 Name: "basic, pk not first", 47 AllCols: []schema.Column{ 48 schema.NewColumn("col1", 0, types.IntKind, false), 49 schema.NewColumn("col2", 1, types.IntKind, true), 50 }, 51 IdxCols: []string{"col1"}, 52 Split: 1, 53 Mapping: []int{1, 0}, 54 }, 55 { 56 Name: "compound index", 57 AllCols: []schema.Column{ 58 schema.NewColumn("col1", 0, types.IntKind, true), 59 schema.NewColumn("col2", 1, types.IntKind, false), 60 schema.NewColumn("col3", 2, types.IntKind, false), 61 }, 62 IdxCols: []string{"col2", "col3"}, 63 Split: 1, 64 Mapping: []int{1, 2, 0}, 65 }, 66 { 67 Name: "compound index reverse", 68 AllCols: []schema.Column{ 69 schema.NewColumn("col1", 0, types.IntKind, true), 70 schema.NewColumn("col2", 1, types.IntKind, false), 71 schema.NewColumn("col3", 2, types.IntKind, false), 72 }, 73 IdxCols: []string{"col3", "col2"}, 74 Split: 1, 75 Mapping: []int{2, 1, 0}, 76 }, 77 { 78 Name: "compound index, pk not first", 79 AllCols: []schema.Column{ 80 schema.NewColumn("col1", 0, types.IntKind, false), 81 schema.NewColumn("col2", 1, types.IntKind, true), 82 schema.NewColumn("col3", 2, types.IntKind, false), 83 }, 84 IdxCols: []string{"col1", "col3"}, 85 Split: 1, 86 Mapping: []int{1, 2, 0}, 87 }, 88 { 89 Name: "compound index, pk not first, reverse", 90 AllCols: []schema.Column{ 91 schema.NewColumn("col1", 0, types.IntKind, false), 92 schema.NewColumn("col2", 1, types.IntKind, true), 93 schema.NewColumn("col3", 2, types.IntKind, false), 94 }, 95 IdxCols: []string{"col3", "col1"}, 96 Split: 1, 97 Mapping: []int{2, 1, 0}, 98 }, 99 { 100 Name: "keyless", 101 AllCols: []schema.Column{ 102 schema.NewColumn("col1", 0, types.IntKind, false), 103 schema.NewColumn("col2", 1, types.IntKind, false), 104 }, 105 IdxCols: []string{"col1"}, 106 Split: 1, 107 // Mapping should skip over cardinality 108 Mapping: []int{2, 0}, 109 }, 110 { 111 Name: "keyless other", 112 AllCols: []schema.Column{ 113 schema.NewColumn("col1", 0, types.IntKind, false), 114 schema.NewColumn("col2", 1, types.IntKind, false), 115 }, 116 IdxCols: []string{"col2"}, 117 Split: 1, 118 // Mapping should skip over cardinality 119 Mapping: []int{3, 0}, 120 }, 121 { 122 Name: "compound keyless", 123 AllCols: []schema.Column{ 124 schema.NewColumn("col1", 0, types.IntKind, false), 125 schema.NewColumn("col2", 1, types.IntKind, false), 126 schema.NewColumn("col3", 2, types.IntKind, false), 127 }, 128 IdxCols: []string{"col2", "col3"}, 129 Split: 1, 130 // Mapping should skip over cardinality 131 Mapping: []int{3, 4, 0}, 132 }, 133 { 134 Name: "compound keyless reverse", 135 AllCols: []schema.Column{ 136 schema.NewColumn("col1", 0, types.IntKind, false), 137 schema.NewColumn("col2", 1, types.IntKind, false), 138 schema.NewColumn("col3", 2, types.IntKind, false), 139 }, 140 IdxCols: []string{"col3", "col2"}, 141 Split: 1, 142 // Mapping should skip over cardinality 143 Mapping: []int{4, 3, 0}, 144 }, 145 } 146 147 for _, tt := range tests { 148 t.Run(tt.Name, func(t *testing.T) { 149 sch := schema.MustSchemaFromCols(schema.NewColCollection(tt.AllCols...)) 150 151 idxTags := make([]uint64, len(tt.IdxCols)) 152 for i, name := range tt.IdxCols { 153 col := sch.GetAllCols().NameToCol[name] 154 idxTags[i] = col.Tag 155 } 156 allTags := append(idxTags, sch.GetPKCols().Tags...) 157 idx := schema.NewIndex("test_idx", idxTags, allTags, nil, schema.IndexProperties{}) 158 b, err := NewSecondaryKeyBuilder(nil, "", sch, idx, val.TupleDesc{}, nil, nil) 159 require.NoError(t, err) 160 require.Equal(t, tt.Split, b.split) 161 require.Equal(t, tt.Mapping, b.mapping) 162 }) 163 } 164 }