Skip to content

Commit

Permalink
Merge pull request cockroachdb#15185 from RaduBerinde/fix-encode-part…
Browse files Browse the repository at this point in the history
…ial-index-key

sql: fix EncodePartialIndexKey function
  • Loading branch information
RaduBerinde authored Apr 20, 2017
2 parents 9e0d4c7 + d5c9466 commit e722e30
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
19 changes: 16 additions & 3 deletions pkg/sql/sqlbase/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ func EncodePartialIndexKey(
) (key []byte, containsNull bool, err error) {
colIDs := index.ColumnIDs[:numCols]
key = keyPrefix
dirs := directions(index.ColumnDirections)
dirs := directions(index.ColumnDirections)[:numCols]

if len(index.Interleave.Ancestors) > 0 {
for i, ancestor := range index.Interleave.Ancestors {
Expand All @@ -255,15 +255,25 @@ func EncodePartialIndexKey(
key = encoding.EncodeUvarintAscending(key, uint64(ancestor.IndexID))
}

partial := false
length := int(ancestor.SharedPrefixLen)
if length > len(colIDs) {
length = len(colIDs)
partial = true
}
var n bool
key, n, err = EncodeColumns(colIDs[:length], dirs[:length], colMap, values, key)
if err != nil {
return key, containsNull, err
}
colIDs, dirs = colIDs[length:], dirs[length:]
containsNull = containsNull || n

if partial {
// Early stop. Note that if we had exactly SharedPrefixLen columns
// remaining, we want to append the next tableID/indexID pair because
// that results in a more specific key.
return key, containsNull, nil
}
colIDs, dirs = colIDs[length:], dirs[length:]
// We reuse NotNullDescending (0xfe) as the interleave sentinel.
key = encoding.EncodeNotNullDescending(key)
}
Expand Down Expand Up @@ -296,6 +306,9 @@ func EncodeColumns(
values []parser.Datum,
keyPrefix []byte,
) (key []byte, containsNull bool, err error) {
if len(columnIDs) == 0 {
return keyPrefix, false, nil
}
// We know we will append to the key which will cause the capacity to grow
// so make it bigger from the get-go.
key = make([]byte, len(keyPrefix), 2*len(keyPrefix))
Expand Down
25 changes: 22 additions & 3 deletions pkg/sql/testdata/logic_test/ranges
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,22 @@ NULL /1 {1} 1
/7/8/#/52/1/9 /10 {1,2,4} 4
/10 NULL {1} 1

statement ok
ALTER TABLE t0 SPLIT AT VALUES (11)

query TTTI colnames
SHOW TESTING_RANGES FROM TABLE t0
----
Start Key End Key Replicas Lease Holder
NULL /1 {1} 1
/1 /5/1 {3,4} 3
/5/1 /5/2 {1,2,3} 1
/5/2 /5/3 {2,3,5} 5
/5/3 /7/8/#/52/1/9 {1,2,4} 4
/7/8/#/52/1/9 /10 {1,2,4} 4
/10 /11 {1} 1
/11 NULL {1} 1

query TTTI colnames
SHOW TESTING_RANGES FROM TABLE t
----
Expand All @@ -165,7 +181,8 @@ NULL /1 {1} 1
/5/2 /5/3 {2,3,5} 5
/5/3 /7/8/#/52/1/9 {1,2,4} 4
/7/8/#/52/1/9 /10 {1,2,4} 4
/10 NULL {1} 1
/10 /11 {1} 1
/11 NULL {1} 1

statement ok
CREATE TABLE t1 (k INT PRIMARY KEY, v1 INT, v2 INT, v3 INT)
Expand All @@ -184,7 +201,8 @@ NULL /1 {1} 1
/5/2 /5/3 {2,3,5} 5
/5/3 /7/8/#/52/1/9 {1,2,4} 4
/7/8/#/52/1/9 /10 {1,2,4} 4
/10 NULL {1} 1
/10 /11 {1} 1
/11 NULL {1} 1

statement ok
ALTER INDEX t1@idx SPLIT AT VALUES (15,16)
Expand All @@ -199,7 +217,8 @@ NULL /1 {1} 1
/5/2 /5/3 {2,3,5} 5
/5/3 /7/8/#/52/1/9 {1,2,4} 4
/7/8/#/52/1/9 /10 {1,2,4} 4
/10 /15/16/#/53/2 {1} 1
/10 /11 {1} 1
/11 /15/16/#/53/2 {1} 1
/15/16/#/53/2 NULL {1} 1

statement error too many columns in SPLIT AT data
Expand Down

0 comments on commit e722e30

Please sign in to comment.