Skip to content

Commit

Permalink
sql: permit inverted indexes to return 0 keys
Browse files Browse the repository at this point in the history
This commit paves the way to having inverted indexes not return any
keys, for datums like NULL and empty arrays.

Release note: None
  • Loading branch information
jordanlewis committed Mar 1, 2020
1 parent e076076 commit 1d10843
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pkg/sql/backfill/backfill.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ func (ib *IndexBackfiller) BuildIndexEntriesChunk(
// indexes which can append entries to the end of the slice. If we don't do this, then everything
// EncodeSecondaryIndexes appends to secondaryIndexEntries for a row, would stay in the slice for
// subsequent rows and we would then have duplicates in entries on output.
buffer = buffer[:len(ib.added)]
buffer = buffer[:0]
if buffer, err = sqlbase.EncodeSecondaryIndexes(
tableDesc.TableDesc(), ib.added, ib.colIdxMap,
ib.rowVals, buffer); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/sql/row/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ func (rh *rowHelper) encodePrimaryIndex(
func (rh *rowHelper) encodeSecondaryIndexes(
colIDtoRowIndex map[sqlbase.ColumnID]int, values []tree.Datum,
) (secondaryIndexEntries []sqlbase.IndexEntry, err error) {
if len(rh.indexEntries) != len(rh.Indexes) {
rh.indexEntries = make([]sqlbase.IndexEntry, len(rh.Indexes))
if cap(rh.indexEntries) < len(rh.Indexes) {
rh.indexEntries = make([]sqlbase.IndexEntry, 0, len(rh.Indexes))
}
rh.indexEntries, err = sqlbase.EncodeSecondaryIndexes(
rh.TableDesc.TableDesc(), rh.Indexes, colIDtoRowIndex, values, rh.indexEntries)
rh.TableDesc.TableDesc(), rh.Indexes, colIDtoRowIndex, values, rh.indexEntries[:0])
if err != nil {
return nil, err
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/sql/row/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,12 @@ func (ru *Updater) UpdateRow(
if ru.Fks.checker != nil {
ru.Fks.addCheckForIndex(ru.Helper.TableDesc.PrimaryIndex.ID, ru.Helper.TableDesc.PrimaryIndex.Type)
for i := range ru.Helper.Indexes {
if ru.Helper.Indexes[i].Type == sqlbase.IndexDescriptor_INVERTED {
// We ignore FK existence checks for inverted indexes.
//
// TODO(knz): verify that this is indeed correct.
continue
}
// * We always will have at least 1 entry in the index, so indexing 0 is safe.
// * The only difference between column family 0 vs other families encodings is
// just the family key ending of the key, so if index[0] is different, the other
Expand Down
19 changes: 8 additions & 11 deletions pkg/sql/sqlbase/index_encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -831,8 +831,7 @@ func (a byID) Less(i, j int) bool { return a[i].id < a[j].id }

// EncodeInvertedIndexKeys creates a list of inverted index keys by
// concatenating keyPrefix with the encodings of the column in the
// index. Returns the key and whether any of the encoded values were
// NULLs.
// index.
func EncodeInvertedIndexKeys(
tableDesc *TableDescriptor,
index *IndexDescriptor,
Expand Down Expand Up @@ -1194,21 +1193,19 @@ func EncodeSecondaryIndexes(
values []tree.Datum,
secondaryIndexEntries []IndexEntry,
) ([]IndexEntry, error) {
if len(secondaryIndexEntries) != len(indexes) {
panic("Length of secondaryIndexEntries is not equal to the number of indexes.")
if len(secondaryIndexEntries) > 0 {
panic("Length of secondaryIndexEntries was non-zero")
}
for i := range indexes {
entries, err := EncodeSecondaryIndex(tableDesc, &indexes[i], colMap, values)
if err != nil {
return secondaryIndexEntries, err
}
secondaryIndexEntries[i] = entries[0]

// This is specifically for inverted indexes which can have more than one entry
// associated with them, or secondary indexes which store columns from
// multiple column families.
if len(entries) > 1 {
secondaryIndexEntries = append(secondaryIndexEntries, entries[1:]...)
for i := range entries {
// Normally, each index will have exactly one entry. Inverted indexes can
// have 0 or >1 entries, though, as well as secondary indexes which store
// columns from multiple column families.
secondaryIndexEntries = append(secondaryIndexEntries, entries[i])
}
}
return secondaryIndexEntries, nil
Expand Down

0 comments on commit 1d10843

Please sign in to comment.