-
-
Notifications
You must be signed in to change notification settings - Fork 238
drop sorts in cross joins and inner joins #3117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -154,16 +154,16 @@ func replaceIdxSortHelper(ctx *sql.Context, scope *plan.Scope, node sql.Node, so | |
| *plan.Project, *plan.Filter, *plan.Limit, *plan.Offset, *plan.Distinct, *plan.TableAlias: | ||
| newChildren[i], same, err = replaceIdxSortHelper(ctx, scope, child, sortNode) | ||
| case *plan.JoinNode: | ||
| // TODO: is this applicable to other types of joins? | ||
| // as long as left child is already sorted and SortFields are a prefix, then it's ok? | ||
| if !c.JoinType().IsMerge() { | ||
| continue | ||
| } | ||
| // It's (probably) not possible to have Sort as child of Join without Subquery/SubqueryAlias, | ||
| // and in the case where there is a Subq/SQA it's taken care of through finalizeSubqueries | ||
| if sortNode == nil { | ||
| continue | ||
| } | ||
| // Merge Joins assume that left and right are sorted | ||
| // Cross Joins and Inner Joins are valid for sort removal if left child is sorted | ||
| if !c.JoinType().IsMerge() && !c.JoinType().IsCross() && !c.JoinType().IsInner() { | ||
| continue | ||
| } | ||
| newLeft, sameLeft, errLeft := replaceIdxSortHelper(ctx, scope, c.Left(), sortNode) | ||
| if errLeft != nil { | ||
| return nil, transform.SameTree, errLeft | ||
|
|
@@ -172,6 +172,7 @@ func replaceIdxSortHelper(ctx *sql.Context, scope *plan.Scope, node sql.Node, so | |
| if errRight != nil { | ||
| return nil, transform.SameTree, errRight | ||
| } | ||
| // Nothing replaced, so nothing to do | ||
| if sameLeft && sameRight { | ||
|
||
| continue | ||
| } | ||
|
|
@@ -183,24 +184,34 @@ func replaceIdxSortHelper(ctx *sql.Context, scope *plan.Scope, node sql.Node, so | |
| c.IsReversed = isReversed | ||
| continue | ||
| } | ||
| // If only one side has been replaced, we need to check if the other side can be reversed | ||
| if (sameLeft != sameRight) && isReversed { | ||
| // If descending, then both Indexes must be reversed | ||
| if sameLeft { | ||
| newLeft, same, err = buildReverseIndexedTable(ctx, newLeft) | ||
| } else if sameRight { | ||
| newRight, same, err = buildReverseIndexedTable(ctx, newRight) | ||
| if c.JoinType().IsCross() || c.JoinType().IsInner() { | ||
| // For cross joins and inner joins, if the right child is sorted, we need to swap | ||
| if !sameRight { | ||
| // Rule eraseProjection will drop any Projections that are now unnecessary. | ||
|
||
| // Rule fixExecIndexes will fix any existing Projection GetField indexes. | ||
| newLeft, newRight = newRight, newLeft | ||
| } | ||
| if err != nil { | ||
| return nil, transform.SameTree, err | ||
| } else { | ||
| // If only one side has been replaced, we need to check if the other side can be reversed | ||
| if (sameLeft != sameRight) && isReversed { | ||
| // If descending, then both Indexes must be reversed | ||
| if sameLeft { | ||
| newLeft, same, err = buildReverseIndexedTable(ctx, newLeft) | ||
| } else if sameRight { | ||
| newRight, same, err = buildReverseIndexedTable(ctx, newRight) | ||
| } | ||
| if err != nil { | ||
| return nil, transform.SameTree, err | ||
| } | ||
| // If we could not replace the IndexedTableAccess with a reversed one (due to lack of reversible index) | ||
| // same = true, so just continue | ||
| if same { | ||
| continue | ||
| } | ||
| c.IsReversed = true | ||
| } | ||
| // If we could not replace the IndexedTableAccess with a reversed one (due to lack of reversible index) | ||
| // same = true, so just continue | ||
| if same { | ||
| continue | ||
| } | ||
| c.IsReversed = true | ||
| } | ||
|
|
||
| newChildren[i], err = c.WithChildren(newLeft, newRight) | ||
| if err != nil { | ||
| return nil, transform.SameTree, err | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use a more specific comment, like "Neither child was converted to an IndexedTableAccess, so we can't remove the sort node"?