Skip to content

Commit

Permalink
sql: column family span generation bugfix
Browse files Browse the repository at this point in the history
PR cockroachdb#38301 inadvertently introduced a bug that unfortunately wasn't
caught by any tests - in the process of improving the tight span
generation for tables with column families, it accidentally started
throwing away all but the spans for the final constraint passed in from
the optimizer.

This small omission has serious consequences: queries that have a
disjunction of spans to examine (like
`SELECT * FROM t WHERE key IN (values...)`) will return incorrect
results if the table being queried has multiple column families (in
certain cases) - it'll look like rows are missing. This will be
problematic for mutations as well.

Note that this bug was never present in a non-alpha release.

Release note (bug fix): restore correct result generation for queries
with index disjunctions on tables with multiple column families.
Release justification: critical bugfix, low risk
  • Loading branch information
jordanlewis committed Sep 18, 2019
1 parent 98159fa commit e60d0c9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 13 additions & 0 deletions pkg/sql/opt/exec/execbuilder/testdata/select_index
Original file line number Diff line number Diff line change
Expand Up @@ -1549,3 +1549,16 @@ render · ·
└── scan · ·
· table t4@primary
· spans /10-/11

# Regression test for #40890: a point lookup on a single column family of a
# table should still work properly in the face of a constraint disjunction.
query TTT
EXPLAIN SELECT a FROM t4 WHERE a in (1, 5) and b in (1, 5)
----
· distributed false
· vectorized true
render · ·
└── scan · ·
· table t4@primary
· spans /1/1/0-/1/1/1 /1/5/0-/1/5/1 /5/1/0-/5/1/1 /5/5/0-/5/5/1
· parallel ·
2 changes: 1 addition & 1 deletion pkg/sql/opt_index_selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func appendSpansFromConstraintSpan(
s.Key.Equal(s.EndKey) {
neededFamilyIDs := sqlbase.NeededColumnFamilyIDs(tableDesc.ColumnIdxMap(), tableDesc.Families, needed)
if len(neededFamilyIDs) < len(tableDesc.Families) {
return sqlbase.SplitSpanIntoSeparateFamilies(s, neededFamilyIDs), nil
return append(spans, sqlbase.SplitSpanIntoSeparateFamilies(s, neededFamilyIDs)...), nil
}
}

Expand Down

0 comments on commit e60d0c9

Please sign in to comment.