Skip to content

Conversation

jycor
Copy link
Contributor

@jycor jycor commented Jul 24, 2025

This PR applies the sort removal optimizations over cross and inner joins.

@jycor jycor marked this pull request as draft July 24, 2025 19:55
@jycor jycor force-pushed the james/cross-join branch from 78124db to e40746c Compare July 25, 2025 23:02
@jycor jycor marked this pull request as ready for review July 29, 2025 17:21
@jycor jycor changed the title drop sorts in cross joins drop sorts in cross joins and inner joins Jul 29, 2025
if errRight != nil {
return nil, transform.SameTree, errRight
}
// Nothing replaced, so nothing to do
Copy link
Contributor

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"?

return nil, transform.SameTree, errRight
}
// Nothing replaced, so nothing to do
if sameLeft && sameRight {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have an idea that might improve readability. What if we wrote:

leftIsSorted := !sameLeft
rightIsSorted := !sameRight

And then wrote the conditionals in terms of these? It might make it more clear what each of these branches means.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense; I'll change the variable names

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these comments added here, in the part where we swap the left and right nodes?

Copy link
Contributor Author

@jycor jycor Jul 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Swapping left and right nodes opens up the opportunity for projections to be dropped and messes up the GetField indexes. These comments are just saying that both those things are handled later on in analysis.

It may also come in handy if we ever change the order of those rules

@jycor jycor merged commit 797f25f into main Jul 30, 2025
8 of 10 checks passed
@jycor jycor deleted the james/cross-join branch July 30, 2025 05:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants