Refactor(optimizer): Optimize USING expansion #4115
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR is an optimization follow-up on #4113
When processing multiple joins such as
A JOIN B JOIN C USING(col)
, we gather the columns from the previous sources (A
,B
etc) to expandUSING (col)
toON c.col = <source>.col
. Previously, this operation would happen from scratch on each iteration, which can be avoided. A few key observations:ordered
list is constructed as a subset ofscope.selected_sources
; It can have[0, N]
elements before the join loopjoin_table
toordered
; It is implied thatjoin_table
is a member ofscope.selected_sources
as it participates in the scope'sJOIN
clause(s) so (1) continues to holdThe combination of (1) and (2) means that we can avoid checking if
source_name
is both inscope.selected_sources
andordered
; Besides that, (2) drives incremental construction ofcolumns
at each iteration.This PR also adds one more test case of a longer JOIN chain; This is to ensure that columns from all intermediate join tables are gathered, even if there is no
USING
clause.