Skip to content

Commit

Permalink
reverse keyless indexscans apply reversal (#8541)
Browse files Browse the repository at this point in the history
  • Loading branch information
max-hoffman authored Nov 14, 2024
1 parent ead97bf commit 0e34d26
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
10 changes: 6 additions & 4 deletions go/libraries/doltcore/sqle/index/index_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func RowIterForIndexLookup(ctx *sql.Context, t DoltTableable, lookup sql.IndexLo
if err != nil {
return nil, err
}
return RowIterForProllyRange(ctx, idx, prollyRanges[0], pkSch, columns, durableState)
return RowIterForProllyRange(ctx, idx, prollyRanges[0], pkSch, columns, durableState, lookup.IsReverse)
} else {
nomsRanges, err := idx.nomsRanges(ctx, mysqlRanges...)
if err != nil {
Expand All @@ -67,15 +67,15 @@ func RowIterForIndexLookup(ctx *sql.Context, t DoltTableable, lookup sql.IndexLo
}
}

func RowIterForProllyRange(ctx *sql.Context, idx DoltIndex, r prolly.Range, pkSch sql.PrimaryKeySchema, projections []uint64, durableState *durableIndexState) (sql.RowIter, error) {
func RowIterForProllyRange(ctx *sql.Context, idx DoltIndex, r prolly.Range, pkSch sql.PrimaryKeySchema, projections []uint64, durableState *durableIndexState, reverse bool) (sql.RowIter, error) {
if projections == nil {
projections = idx.Schema().GetAllCols().Tags
}

if sql.IsKeyless(pkSch.Schema) {
// in order to resolve row cardinality, keyless indexes must always perform
// an indirect lookup through the clustered index.
return newProllyKeylessIndexIter(ctx, idx, r, nil, pkSch, projections, durableState.Primary, durableState.Secondary)
return newProllyKeylessIndexIter(ctx, idx, r, nil, pkSch, projections, durableState.Primary, durableState.Secondary, reverse)
}

covers := idx.coversColumns(durableState, projections)
Expand Down Expand Up @@ -697,15 +697,17 @@ func (i *keylessMapIter) Next(ctx context.Context) (val.Tuple, val.Tuple, error)
func (ib *keylessIndexImplBuilder) NewPartitionRowIter(ctx *sql.Context, part sql.Partition) (sql.RowIter, error) {
var prollyRange prolly.Range
var doltgresRange *DoltgresRange
var reverse bool
switch p := part.(type) {
case rangePartition:
prollyRange = p.prollyRange
reverse = p.isReverse
case pointPartition:
prollyRange = p.r
case DoltgresPartition:
doltgresRange = &p.rang
}
return newProllyKeylessIndexIter(ctx, ib.idx, prollyRange, doltgresRange, ib.sch, ib.projections, ib.s.Primary, ib.s.Secondary)
return newProllyKeylessIndexIter(ctx, ib.idx, prollyRange, doltgresRange, ib.sch, ib.projections, ib.s.Primary, ib.s.Secondary, reverse)
}

func (ib *keylessIndexImplBuilder) NewSecondaryIter(strict bool, cnt int, nullSafe []bool) SecondaryLookupIterGen {
Expand Down
16 changes: 6 additions & 10 deletions go/libraries/doltcore/sqle/index/prolly_index_iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,20 +292,16 @@ type prollyKeylessIndexIter struct {

var _ sql.RowIter = prollyKeylessIndexIter{}

func newProllyKeylessIndexIter(
ctx *sql.Context,
idx DoltIndex,
rng prolly.Range,
doltgresRange *DoltgresRange,
pkSch sql.PrimaryKeySchema,
projections []uint64,
rows, dsecondary durable.Index,
) (prollyKeylessIndexIter, error) {
func newProllyKeylessIndexIter(ctx *sql.Context, idx DoltIndex, rng prolly.Range, doltgresRange *DoltgresRange, pkSch sql.PrimaryKeySchema, projections []uint64, rows, dsecondary durable.Index, reverse bool) (prollyKeylessIndexIter, error) {
secondary := durable.ProllyMapFromIndex(dsecondary)
var indexIter prolly.MapIter
var err error
if doltgresRange == nil {
indexIter, err = secondary.IterRange(ctx, rng)
if reverse {
indexIter, err = secondary.IterRangeReverse(ctx, rng)
} else {
indexIter, err = secondary.IterRange(ctx, rng)
}
if err != nil {
return prollyKeylessIndexIter{}, err
}
Expand Down

0 comments on commit 0e34d26

Please sign in to comment.