Fixing bugs for building a prefix index with existing table data

This commit is contained in:
Jason Fulghum
2023-08-24 14:41:46 -07:00
parent b09149fc96
commit cc242a6f3f
4 changed files with 48 additions and 16 deletions
+1 -1
View File
@@ -331,7 +331,7 @@ func translateBlobField(ctx context.Context, ns tree.NodeStore, value types.Blob
case val.StringAddrEnc, val.BytesAddrEnc:
// common case
default:
return fmt.Errorf("unexpecte encoding for blob (%d)", b.Desc.Types[idx].Enc)
return fmt.Errorf("unexpected encoding for blob (%d)", b.Desc.Types[idx].Enc)
}
buf := make([]byte, value.Len())
@@ -31,6 +31,7 @@ func NewSecondaryKeyBuilder(sch schema.Schema, def schema.Index, idxDesc val.Tup
b.pool = p
b.nodeStore = nodeStore
b.sch = sch
b.def = def
keyless := schema.IsKeyless(sch)
if keyless {
@@ -64,6 +65,8 @@ func NewSecondaryKeyBuilder(sch schema.Schema, def schema.Index, idxDesc val.Tup
type SecondaryKeyBuilder struct {
// sch holds the schema of the table on which the secondary index is created
sch schema.Schema
// def holds the definition of the secondary index
def schema.Index
// mapping defines how to map fields from the source table's schema to this index's tuple layout
mapping val.OrdinalMapping
// split marks the index in the secondary index's key tuple that splits the main table's
@@ -96,6 +99,10 @@ func (b SecondaryKeyBuilder) SecondaryKeyFromRow(ctx context.Context, k, v val.T
return nil, err
}
if len(b.def.PrefixLengths()) > to {
value = val.TrimValueToPrefixLength(value, b.def.PrefixLengths()[to])
}
err = PutField(ctx, b.nodeStore, b.builder, to, value)
if err != nil {
return nil, err
@@ -290,21 +290,7 @@ func (m prollySecondaryIndexWriter) trimKeyPart(to int, keyPart interface{}) int
if len(m.prefixLengths) > to {
prefixLength = m.prefixLengths[to]
}
if prefixLength != 0 {
switch kp := keyPart.(type) {
case string:
if prefixLength > uint16(len(kp)) {
prefixLength = uint16(len(kp))
}
keyPart = kp[:prefixLength]
case []uint8:
if prefixLength > uint16(len(kp)) {
prefixLength = uint16(len(kp))
}
keyPart = kp[:prefixLength]
}
}
return keyPart
return val.TrimValueToPrefixLength(keyPart, prefixLength)
}
func (m prollySecondaryIndexWriter) keyFromRow(ctx context.Context, sqlRow sql.Row) (val.Tuple, error) {
+39
View File
@@ -0,0 +1,39 @@
// Copyright 2023 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package val
// TrimValueToPrefixLength trims |value| to |prefixLength| if it is longer
// and if it is either a []byte or string type. If |prefixLength| is zero,
// then |value| will be returned without being trimmed.
func TrimValueToPrefixLength(value interface{}, prefixLength uint16) interface{} {
if prefixLength == 0 {
return value
}
switch v := value.(type) {
case string:
if prefixLength > uint16(len(v)) {
prefixLength = uint16(len(v))
}
value = v[:prefixLength]
case []uint8:
if prefixLength > uint16(len(v)) {
prefixLength = uint16(len(v))
}
value = v[:prefixLength]
}
return value
}