Skip to content

Commit 38713ba

Browse files
author
James Cor
committed
feedback
1 parent 8134064 commit 38713ba

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

sql/analyzer/replace_sort.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,32 +172,34 @@ func replaceIdxSortHelper(ctx *sql.Context, scope *plan.Scope, node sql.Node, so
172172
if errRight != nil {
173173
return nil, transform.SameTree, errRight
174174
}
175-
// Nothing replaced, so nothing to do
176-
if sameLeft && sameRight {
175+
// Neither child was converted to an IndexedTableAccess, so we can't remove the sort node
176+
leftIsSorted, rightIsSorted := !sameLeft, !sameRight
177+
if !leftIsSorted && !rightIsSorted {
177178
continue
178179
}
179180
// No need to check all SortField orders because of isValidSortFieldOrder
180181
isReversed := sortNode.SortFields[0].Order == sql.Descending
181182
// If both left and right have been replaced, no need to manually reverse any indexes as they both should be
182183
// replaced already
183-
if !sameLeft && !sameRight {
184+
if leftIsSorted && rightIsSorted {
184185
c.IsReversed = isReversed
185186
continue
186187
}
187188
if c.JoinType().IsCross() || c.JoinType().IsInner() {
188189
// For cross joins and inner joins, if the right child is sorted, we need to swap
189190
if !sameRight {
190-
// Rule eraseProjection will drop any Projections that are now unnecessary.
191-
// Rule fixExecIndexes will fix any existing Projection GetField indexes.
191+
// Swapping may mess up projections, but
192+
// eraseProjection will drop any Projections that are now unnecessary and
193+
// fixExecIndexes will fix any existing Projection GetField indexes.
192194
newLeft, newRight = newRight, newLeft
193195
}
194196
} else {
195197
// If only one side has been replaced, we need to check if the other side can be reversed
196-
if (sameLeft != sameRight) && isReversed {
198+
if (leftIsSorted != rightIsSorted) && isReversed {
197199
// If descending, then both Indexes must be reversed
198-
if sameLeft {
200+
if rightIsSorted {
199201
newLeft, same, err = buildReverseIndexedTable(ctx, newLeft)
200-
} else if sameRight {
202+
} else if leftIsSorted {
201203
newRight, same, err = buildReverseIndexedTable(ctx, newRight)
202204
}
203205
if err != nil {

0 commit comments

Comments
 (0)