diff --git a/pkg/sql/rowenc/index_fetch.go b/pkg/sql/rowenc/index_fetch.go index edec233e02a8..86a54aa01e20 100644 --- a/pkg/sql/rowenc/index_fetch.go +++ b/pkg/sql/rowenc/index_fetch.go @@ -15,6 +15,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/sql/catalog" "github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb" "github.com/cockroachdb/cockroach/pkg/util/buildutil" + "github.com/cockroachdb/cockroach/pkg/util/encoding" "github.com/cockroachdb/errors" ) @@ -46,8 +47,9 @@ func InitIndexFetchSpec( maxKeysPerRow := table.IndexKeysPerRow(index) s.MaxKeysPerRow = uint32(maxKeysPerRow) - // TODO(radu): calculate the length without actually generating a throw-away key. - s.KeyPrefixLength = uint32(len(MakeIndexKeyPrefix(codec, table.GetID(), index.GetID()))) + s.KeyPrefixLength = uint32(len(codec.TenantPrefix()) + + encoding.EncodedLengthUvarintAscending(uint64(s.TableID)) + + encoding.EncodedLengthUvarintAscending(uint64(index.GetID()))) s.FamilyDefaultColumns = table.FamilyDefaultColumns() diff --git a/pkg/util/encoding/encoding.go b/pkg/util/encoding/encoding.go index 634989f3478a..72bda65cef8e 100644 --- a/pkg/util/encoding/encoding.go +++ b/pkg/util/encoding/encoding.go @@ -374,6 +374,31 @@ func EncodeUvarintAscending(b []byte, v uint64) []byte { } } +// EncodedLengthUvarintAscending returns the length of the variable length +// representation, i.e. the number of bytes appended by EncodeUvarintAscending. +func EncodedLengthUvarintAscending(v uint64) int { + switch { + case v <= intSmall: + return 1 + case v <= 0xff: + return 2 + case v <= 0xffff: + return 3 + case v <= 0xffffff: + return 4 + case v <= 0xffffffff: + return 5 + case v <= 0xffffffffff: + return 6 + case v <= 0xffffffffffff: + return 7 + case v <= 0xffffffffffffff: + return 8 + default: + return 9 + } +} + // EncodeUvarintDescending encodes the uint64 value so that it sorts in // reverse order, from largest to smallest. func EncodeUvarintDescending(b []byte, v uint64) []byte { diff --git a/pkg/util/encoding/encoding_test.go b/pkg/util/encoding/encoding_test.go index 926d4f2f3e93..723db2838a85 100644 --- a/pkg/util/encoding/encoding_test.go +++ b/pkg/util/encoding/encoding_test.go @@ -323,6 +323,17 @@ func TestEncodeDecodeUvarint(t *testing.T) { testCustomEncodeUint64(testCases, EncodeUvarintAscending, t) } +func TestEncodedLengthUvarintAscending(t *testing.T) { + for i := 0; i < 100; i++ { + v := rand.Uint64() + exp := len(EncodeUvarintAscending(nil, v)) + actual := EncodedLengthUvarintAscending(v) + if actual != exp { + t.Fatalf("incorrect encoded length for %d: %d (expected %d)", v, actual, exp) + } + } +} + func TestEncodeDecodeUvarintDescending(t *testing.T) { testBasicEncodeDecodeUint64(EncodeUvarintDescending, DecodeUvarintDescending, true, true, true, t) testCases := []testCaseUint64{