func (n *scanNode) ExplainPlan(_ bool) (name, description string, children []planNode) { if n.reverse { name = "revscan" } else { name = "scan" } description = fmt.Sprintf("%s@%s %s", n.desc.Name, n.index.Name, sqlbase.PrettySpans(n.spans, 2)) return name, description, nil }
func (n *scanNode) ExplainPlan(_ bool) (name, description string, children []planNode) { if n.reverse { name = "revscan" } else { name = "scan" } var desc bytes.Buffer fmt.Fprintf(&desc, "%s@%s", n.desc.Name, n.index.Name) spans := sqlbase.PrettySpans(n.spans, 2) if spans != "" { fmt.Fprintf(&desc, " %s", spans) } if n.limitHint > 0 && !n.limitSoft { if n.limitHint == 1 { desc.WriteString(" (max 1 row)") } else { fmt.Fprintf(&desc, " (max %d rows)", n.limitHint) } } subplans := n.p.collectSubqueryPlans(n.filter, nil) return name, desc.String(), subplans }
func (n *indexJoinNode) Next() bool { // Loop looking up the next row. We either are going to pull a row from the // table or a batch of rows from the index. If we pull a batch of rows from // the index we perform another iteration of the loop looking for rows in the // table. This outer loop is necessary because a batch of rows from the index // might all be filtered when the resulting rows are read from the table. for tableLookup := (len(n.table.spans) > 0); true; tableLookup = true { // First, try to pull a row from the table. if tableLookup && n.table.Next() { if n.explain == explainDebug { n.debugVals = n.table.DebugValues() } return true } if n.err = n.table.Err(); n.err != nil { return false } // The table is out of rows. Pull primary keys from the index. n.table.scanInitialized = false n.table.spans = n.table.spans[:0] for len(n.table.spans) < joinBatchSize { if !n.index.Next() { // The index is out of rows or an error occurred. if n.err = n.index.Err(); n.err != nil { return false } if len(n.table.spans) == 0 { // The index is out of rows. return false } break } if n.explain == explainDebug { n.debugVals = n.index.DebugValues() if n.debugVals.output != debugValueRow { return true } } vals := n.index.Values() primaryIndexKey, _, err := sqlbase.EncodeIndexKey( n.table.index, n.colIDtoRowIndex, vals, n.primaryKeyPrefix) n.err = err if n.err != nil { return false } key := roachpb.Key(primaryIndexKey) n.table.spans = append(n.table.spans, sqlbase.Span{ Start: key, End: key.PrefixEnd(), }) if n.explain == explainDebug { // In debug mode, return the index information as a "partial" row. n.debugVals.output = debugValuePartial return true } } if log.V(3) { log.Infof("table scan: %s", sqlbase.PrettySpans(n.table.spans, 0)) } } return false }
func TestMakeSpans(t *testing.T) { defer leaktest.AfterTest(t)() testData := []struct { expr string columns string expectedAsc string expectedDesc string }{ {`a = 1`, `a`, `/1-/2`, `/1-/0`}, {`a != 1`, `a`, `/#-`, `-/#`}, {`a > 1`, `a`, `/2-`, `-/1`}, {`a >= 1`, `a`, `/1-`, `-/0`}, {`a < 1`, `a`, `/#-/1`, `/0-/#`}, {`a <= 1`, `a`, `/#-/2`, `/1-/#`}, {`a IS NULL`, `a`, `-/#`, `/NULL-`}, {`a IS NOT NULL`, `a`, `/#-`, `-/#`}, {`a IN (1,2,3)`, `a`, `/1-/4`, `/3-/0`}, {`a IN (1,3,5)`, `a`, `/1-/2 /3-/4 /5-/6`, `/5-/4 /3-/2 /1-/0`}, {`a IN (1,2,3) AND b = 1`, `a,b`, `/1/1-/1/2 /2/1-/2/2 /3/1-/3/2`, `/3/1-/3/0 /2/1-/2/0 /1/1-/1/0`}, {`a = 1 AND b IN (1,2,3)`, `a,b`, `/1/1-/1/4`, `/1/3-/1/0`}, {`a = 1 AND b IN (1,3,5)`, `a,b`, `/1/1-/1/2 /1/3-/1/4 /1/5-/1/6`, `/1/5-/1/4 /1/3-/1/2 /1/1-/1/0`}, {`a >= 1 AND b IN (1,2,3)`, `a,b`, `/1-`, `-/0`}, {`a <= 1 AND b IN (1,2,3)`, `a,b`, `/#-/2`, `/1-/#`}, {`(a, b) IN ((1, 2), (3, 4))`, `a,b`, `/1/2-/1/3 /3/4-/3/5`, `/3/4-/3/3 /1/2-/1/1`}, {`(b, a) IN ((1, 2), (3, 4))`, `a,b`, `/2/1-/2/2 /4/3-/4/4`, `/4/3-/4/2 /2/1-/2/0`}, {`(a, b) IN ((1, 2), (3, 4))`, `b`, `/2-/3 /4-/5`, `/4-/3 /2-/1`}, {`a = 1 AND b = 1`, `a,b`, `/1/1-/1/2`, `/1/1-/1/0`}, {`a = 1 AND b != 1`, `a,b`, `/1/#-/2`, `/1-/1/#`}, {`a = 1 AND b > 1`, `a,b`, `/1/2-/2`, `/1-/1/1`}, {`a = 1 AND b >= 1`, `a,b`, `/1/1-/2`, `/1-/1/0`}, {`a = 1 AND b < 1`, `a,b`, `/1/#-/1/1`, `/1/0-/1/#`}, {`a = 1 AND b <= 1`, `a,b`, `/1/#-/1/2`, `/1/1-/1/#`}, {`a = 1 AND b IS NULL`, `a,b`, `/1-/1/#`, `/1/NULL-/0`}, {`a = 1 AND b IS NOT NULL`, `a,b`, `/1/#-/2`, `/1-/1/#`}, {`a != 1 AND b = 1`, `a,b`, `/#-`, `-/#`}, {`a != 1 AND b != 1`, `a,b`, `/#-`, `-/#`}, {`a != 1 AND b > 1`, `a,b`, `/#-`, `-/#`}, {`a != 1 AND b >= 1`, `a,b`, `/#-`, `-/#`}, {`a != 1 AND b < 1`, `a,b`, `/#-`, `-/#`}, {`a != 1 AND b <= 1`, `a,b`, `/#-`, `-/#`}, {`a != 1 AND b IS NULL`, `a,b`, `/#-`, `-/#`}, {`a != 1 AND b IS NOT NULL`, `a,b`, `/#-`, `-/#`}, {`a > 1 AND b = 1`, `a,b`, `/2/1-`, `-/2/0`}, {`a > 1 AND b != 1`, `a,b`, `/2/#-`, `-/2/#`}, {`a > 1 AND b > 1`, `a,b`, `/2/2-`, `-/2/1`}, {`a > 1 AND b >= 1`, `a,b`, `/2/1-`, `-/2/0`}, {`a > 1 AND b < 1`, `a,b`, `/2-`, `-/1`}, {`a > 1 AND b <= 1`, `a,b`, `/2-`, `-/1`}, {`a > 1 AND b IS NULL`, `a,b`, `/2-`, `-/1`}, {`a > 1 AND b IS NOT NULL`, `a,b`, `/2/#-`, `-/2/#`}, {`a >= 1 AND b = 1`, `a,b`, `/1/1-`, `-/1/0`}, {`a >= 1 AND b != 1`, `a,b`, `/1/#-`, `-/1/#`}, {`a >= 1 AND b > 1`, `a,b`, `/1/2-`, `-/1/1`}, {`a >= 1 AND b >= 1`, `a,b`, `/1/1-`, `-/1/0`}, {`a >= 1 AND b < 1`, `a,b`, `/1-`, `-/0`}, {`a >= 1 AND b <= 1`, `a,b`, `/1-`, `-/0`}, {`a >= 1 AND b IS NULL`, `a,b`, `/1-`, `-/0`}, {`a >= 1 AND b IS NOT NULL`, `a,b`, `/1/#-`, `-/1/#`}, {`a < 1 AND b = 1`, `a,b`, `/#-/0/2`, `/0/1-/#`}, {`a < 1 AND b != 1`, `a,b`, `/#-/1`, `/0-/#`}, {`a < 1 AND b > 1`, `a,b`, `/#-/1`, `/0-/#`}, {`a < 1 AND b >= 1`, `a,b`, `/#-/1`, `/0-/#`}, {`a < 1 AND b < 1`, `a,b`, `/#-/0/1`, `/0/0-/#`}, {`a < 1 AND b <= 1`, `a,b`, `/#-/0/2`, `/0/1-/#`}, {`a < 1 AND b IS NULL`, `a,b`, `/#-/0/#`, `/0/NULL-/#`}, {`a < 1 AND b IS NOT NULL`, `a,b`, `/#-/1`, `/0-/#`}, {`a <= 1 AND b = 1`, `a,b`, `/#-/1/2`, `/1/1-/#`}, {`a <= 1 AND b != 1`, `a,b`, `/#-/2`, `/1-/#`}, {`a <= 1 AND b > 1`, `a,b`, `/#-/2`, `/1-/#`}, {`a <= 1 AND b >= 1`, `a,b`, `/#-/2`, `/1-/#`}, {`a <= 1 AND b < 1`, `a,b`, `/#-/1/1`, `/1/0-/#`}, {`a <= 1 AND b <= 1`, `a,b`, `/#-/1/2`, `/1/1-/#`}, {`a <= 1 AND b IS NULL`, `a,b`, `/#-/1/#`, `/1/NULL-/#`}, {`a <= 1 AND b IS NOT NULL`, `a,b`, `/#-/2`, `/1-/#`}, {`a IN (1) AND b = 1`, `a,b`, `/1/1-/1/2`, `/1/1-/1/0`}, {`a IN (1) AND b != 1`, `a,b`, `/1/#-/2`, `/1-/1/#`}, {`a IN (1) AND b > 1`, `a,b`, `/1/2-/2`, `/1-/1/1`}, {`a IN (1) AND b >= 1`, `a,b`, `/1/1-/2`, `/1-/1/0`}, {`a IN (1) AND b < 1`, `a,b`, `/1/#-/1/1`, `/1/0-/1/#`}, {`a IN (1) AND b <= 1`, `a,b`, `/1/#-/1/2`, `/1/1-/1/#`}, {`a IN (1) AND b IS NULL`, `a,b`, `/1-/1/#`, `/1/NULL-/0`}, {`a IN (1) AND b IS NOT NULL`, `a,b`, `/1/#-/2`, `/1-/1/#`}, {`(a, b) = (1, 2)`, `a`, `/1-/2`, `/1-/0`}, {`(a, b) = (1, 2)`, `a,b`, `/1/2-/1/3`, `/1/2-/1/1`}, {`a > 1 OR a >= 5`, `a`, `/2-`, `-/1`}, {`a < 5 OR a >= 1`, `a`, `/#-`, `-/#`}, {`a < 1 OR a >= 5`, `a`, `/#-/1 /5-`, `-/4 /0-/#`}, {`a = 1 OR a > 8`, `a`, `/1-/2 /9-`, `-/8 /1-/0`}, {`a = 8 OR a > 1`, `a`, `/2-`, `-/1`}, {`a < 1 OR a = 5 OR a > 8`, `a`, `/#-/1 /5-/6 /9-`, `-/8 /5-/4 /0-/#`}, {`a < 8 OR a = 8 OR a > 8`, `a`, `/#-`, `-/#`}, {`(a = 1 AND b = 5) OR (a = 3 AND b = 7)`, `a`, `/1-/2 /3-/4`, `/3-/2 /1-/0`}, {`(a = 1 AND b = 5) OR (a = 3 AND b = 7)`, `b`, `/5-/6 /7-/8`, `/7-/6 /5-/4`}, {`(a = 1 AND b = 5) OR (a = 3 AND b = 7)`, `a,b`, `/1/5-/1/6 /3/7-/3/8`, `/3/7-/3/6 /1/5-/1/4`}, {`(a = 1 AND b < 5) OR (a = 3 AND b > 7)`, `a`, `/1-/2 /3-/4`, `/3-/2 /1-/0`}, {`(a = 1 AND b < 5) OR (a = 3 AND b > 7)`, `b`, `/#-/5 /8-`, `-/7 /4-/#`}, {`(a = 1 AND b < 5) OR (a = 3 AND b > 7)`, `a,b`, `/1/#-/1/5 /3/8-/4`, `/3-/3/7 /1/4-/1/#`}, {`(a = 1 AND b > 5) OR (a = 3 AND b > 7)`, `a`, `/1-/2 /3-/4`, `/3-/2 /1-/0`}, {`(a = 1 AND b > 5) OR (a = 3 AND b > 7)`, `b`, `/6-`, `-/5`}, {`(a = 1 AND b > 5) OR (a = 3 AND b > 7)`, `a,b`, `/1/6-/2 /3/8-/4`, `/3-/3/7 /1-/1/5`}, {`(a = 1 AND b > 5) OR (a = 3 AND b < 7)`, `a`, `/1-/2 /3-/4`, `/3-/2 /1-/0`}, {`(a = 1 AND b > 5) OR (a = 3 AND b < 7)`, `b`, `/#-`, `-/#`}, {`(a = 1 AND b > 5) OR (a = 3 AND b < 7)`, `a,b`, `/1/6-/2 /3/#-/3/7`, `/3/6-/3/# /1-/1/5`}, {`(a < 1 AND b < 5) OR (a > 3 AND b > 7)`, `a`, `/#-/1 /4-`, `-/3 /0-/#`}, {`(a < 1 AND b < 5) OR (a > 3 AND b > 7)`, `b`, `/#-/5 /8-`, `-/7 /4-/#`}, {`(a < 1 AND b < 5) OR (a > 3 AND b > 7)`, `a,b`, `/#-/0/5 /4/8-`, `-/4/7 /0/4-/#`}, {`(a > 3 AND b < 5) OR (a < 1 AND b > 7)`, `a`, `/#-/1 /4-`, `-/3 /0-/#`}, {`(a > 3 AND b < 5) OR (a < 1 AND b > 7)`, `b`, `/#-/5 /8-`, `-/7 /4-/#`}, {`(a > 3 AND b < 5) OR (a < 1 AND b > 7)`, `a,b`, `/#-/1 /4-`, `-/3 /0-/#`}, {`(a > 1 AND b < 5) OR (a < 3 AND b > 7)`, `a`, `/#-`, `-/#`}, {`(a > 1 AND b < 5) OR (a < 3 AND b > 7)`, `b`, `/#-/5 /8-`, `-/7 /4-/#`}, {`(a > 1 AND b < 5) OR (a < 3 AND b > 7)`, `a,b`, `/#-`, `-/#`}, {`(a = 5) OR (a, b) IN ((1, 1), (3, 3))`, `a`, `/1-/2 /3-/4 /5-/6`, `/5-/4 /3-/2 /1-/0`}, {`(a = 5) OR (a, b) IN ((1, 1), (3, 3))`, `b`, `-`, `-`}, {`(a = 5) OR (a, b) IN ((1, 1), (3, 3))`, `a,b`, `/1/1-/1/2 /3/3-/3/4 /5-/6`, `/5-/4 /3/3-/3/2 /1/1-/1/0`}, // When encoding an end constraint for a maximal datum, we use // bytes.PrefixEnd() to go beyond the normal encodings of that datatype. {fmt.Sprintf(`a = %d`, math.MaxInt64), `a`, `/9223372036854775807-/<varint 9223372036854775808 overflows int64>`, `/9223372036854775807-/9223372036854775806`}, {fmt.Sprintf(`a = %d`, math.MinInt64), `a`, `/-9223372036854775808-/-9223372036854775807`, `/-9223372036854775808-/<varint 9223372036854775808 overflows int64>`}, {`(a, b) >= (1, 4)`, `a,b`, `/1/4-`, `-/1/3`}, {`(a, b) > (1, 4)`, `a,b`, `/1/5-`, `-/1/4`}, {`(a, b) < (1, 4)`, `a,b`, `/#-/1/4`, `/1/3-/#`}, {`(a, b) <= (1, 4)`, `a,b`, `/#-/1/5`, `/1/4-/#`}, {`(a, b) = (1, 4)`, `a,b`, `/1/4-/1/5`, `/1/4-/1/3`}, {`(a, b) != (1, 4)`, `a,b`, `/#-`, `-/#`}, } for _, d := range testData { for _, dir := range []encoding.Direction{encoding.Ascending, encoding.Descending} { columns := strings.Split(d.columns, ",") dirs := make([]encoding.Direction, 0, len(columns)) for range columns { dirs = append(dirs, dir) } desc, index := makeTestIndex(t, columns, dirs) constraints, _ := makeConstraints(t, d.expr, desc, index) spans := makeSpans(constraints, desc.ID, index) s := sqlbase.PrettySpans(spans, 2) var expected string if dir == encoding.Ascending { expected = d.expectedAsc } else { expected = d.expectedDesc } s = keys.MassagePrettyPrintedSpanForTest(s, indexToDirs(index)) if expected != s { t.Errorf("[index direction: %d] %s: expected %s, but found %s", dir, d.expr, expected, s) } } } // Test indexes with mixed-directions (some cols Asc, some cols Desc) and other edge cases. testData2 := []struct { expr string columns string expected string }{ {`a = 1 AND b = 5`, `a,b-,c`, `/1/5-/1/4`}, {`a = 7 AND b IN (1,2,3) AND c = false`, `a,b-,c`, `/7/3/0-/7/3/1 /7/2/0-/7/2/1 /7/1/0-/7/1/1`}, // Test different directions for te columns inside a tuple. {`(a,b,j) IN ((1,2,3), (4,5,6))`, `a-,b,j-`, `/4/5/6-/4/5/5 /1/2/3-/1/2/2`}, {`i = E'\xff'`, `i`, `/"\xff"-/"\xff\x00"`}, // Test that limits on bytes work correctly: when encoding a descending limit for bytes, // we need to go outside the bytes encoding. // "\xaa" is encoded as [bytesDescMarker, ^0xaa, <term escape sequence>] {`i = E'\xaa'`, `i-`, fmt.Sprintf("raw:%c%c\xff\xfe-%c%c\xff\xff", encoding.BytesDescMarker, ^byte(0xaa), encoding.BytesDescMarker, ^byte(0xaa))}, // Ensure tuples with differing index directions aren't constrained. // TODO(mjibson): fix this, see #6346 {`(a, b) >= (1, 4)`, `a-,b`, `-`}, {`(a, b) >= (1, 4)`, `a,b-`, `-`}, } for _, d := range testData2 { desc, index := makeTestIndexFromStr(t, d.columns) constraints, _ := makeConstraints(t, d.expr, desc, index) spans := makeSpans(constraints, desc.ID, index) var got string raw := false if strings.HasPrefix(d.expected, "raw:") { raw = true span := spans[0] d.expected = d.expected[4:] // Trim the index prefix from the span. prefix := string(sqlbase.MakeIndexKeyPrefix(desc.ID, index.ID)) got = strings.TrimPrefix(string(span.Start), prefix) + "-" + strings.TrimPrefix(string(span.End), prefix) } else { got = keys.MassagePrettyPrintedSpanForTest(sqlbase.PrettySpans(spans, 2), indexToDirs(index)) } if d.expected != got { if !raw { t.Errorf("%s: expected %s, but found %s", d.expr, d.expected, got) } else { t.Errorf("%s: expected %# x, but found %# x", d.expr, []byte(d.expected), got) } } } }