Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions enginetest/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -8755,6 +8755,17 @@ from typestable`,
{"12.0000"},
},
},
{
Query: "select * from one_pk_two_idx where v1 < 4 and v2 < 2 or v2 > 3 order by v1",
Expected: []sql.Row{
{0, 0, 0},
{1, 1, 1},
{4, 4, 4},
{5, 5, 5},
{6, 6, 6},
{7, 7, 7},
},
},
}

var KeylessQueries = []QueryTest{
Expand Down
13 changes: 13 additions & 0 deletions enginetest/queries/query_plans.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions sql/analyzer/replace_sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ func replaceIdxSortHelper(ctx *sql.Context, scope *plan.Scope, node sql.Node, so
return n, transform.SameTree, nil
}

// if the resulting ranges are overlapping, we cannot drop the sort node
// it is possible we end up with blocks rows that intersect
if hasOverlapping(sfExprs, lookup.Ranges) {
return n, transform.SameTree, nil
}

// if the lookup does not need any reversing, do nothing
if sortNode.SortFields[0].Order != sql.Descending {
return n, transform.NewTree, nil
Expand Down Expand Up @@ -291,6 +297,21 @@ func isValidSortFieldOrder(sfs sql.SortFields) bool {
return true
}

// hasOverlapping checks if the ranges in a RangeCollection that are part of the sortfield exprs are overlapping
// This function assumes that the sort field exprs are a valid prefix of the index columns
func hasOverlapping(sfExprs []sql.Expression, ranges sql.RangeCollection) bool {
for si := range sfExprs {
for ri := 0; ri < len(ranges)-1; ri++ {
for rj := ri + 1; rj < len(ranges); rj++ {
if _, overlaps, _ := ranges[ri][si].Overlaps(ranges[rj][si]); overlaps {
return true
}
}
}
}
return false
}

// getPKIndex returns the primary key index of an IndexAddressableTable
func getPKIndex(ctx *sql.Context, idxTbl sql.IndexAddressableTable) (sql.Index, error) {
idxs, err := idxTbl.GetIndexes(ctx)
Expand Down