Skip to content

Commit be72c6e

Browse files
committed
Only optimize ORDER BY to use vector indexes if there's also a limit.
1 parent 4dea0bb commit be72c6e

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

sql/analyzer/replace_order_by_distance.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ func replaceIdxOrderByDistance(ctx *sql.Context, a *Analyzer, n sql.Node, scope
1313
return replaceIdxOrderByDistanceHelper(ctx, scope, n, nil)
1414
}
1515

16-
func replaceIdxOrderByDistanceHelper(ctx *sql.Context, scope *plan.Scope, node sql.Node, sortNode plan.Sortable) (sql.Node, transform.TreeIdentity, error) {
16+
func replaceIdxOrderByDistanceHelper(ctx *sql.Context, scope *plan.Scope, node sql.Node, sortNode *plan.TopN) (sql.Node, transform.TreeIdentity, error) {
1717
switch n := node.(type) {
18-
case plan.Sortable:
18+
case *plan.TopN:
1919
sortNode = n // lowest parent sort node
2020
case *plan.ResolvedTable:
2121
if sortNode == nil {
@@ -44,7 +44,7 @@ func replaceIdxOrderByDistanceHelper(ctx *sql.Context, scope *plan.Scope, node s
4444

4545
// Column references have not been assigned their final indexes yet, so do that for the ORDER BY expression now.
4646
// We can safely do this because an expression that references other tables won't pass `isSortFieldsValidPrefix` below.
47-
sortNode = offsetAssignIndexes(sortNode).(plan.Sortable)
47+
sortNode = offsetAssignIndexes(sortNode).(*plan.TopN)
4848

4949
sfExprs := normalizeExpressions(tableAliases, sortNode.GetSortFields().ToExpressions()...)
5050
sfAliases := aliasedExpressionsInNode(sortNode)
@@ -91,10 +91,7 @@ func replaceIdxOrderByDistanceHelper(ctx *sql.Context, scope *plan.Scope, node s
9191
return n, transform.SameTree, nil
9292
}
9393

94-
var limit sql.Expression
95-
if topn, ok := sortNode.(*plan.TopN); ok {
96-
limit = topn.Limit
97-
}
94+
limit := sortNode.Limit
9895

9996
lookup := sql.IndexLookup{
10097
Index: idx,

0 commit comments

Comments
 (0)