Navigation Menu

Skip to content
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

release-20.2: opt: add transformation rule to convert left join to inner join #54110

Merged
merged 1 commit into from Sep 16, 2020

Conversation

rytaft
Copy link
Collaborator

@rytaft rytaft commented Sep 9, 2020

Backport 1/1 commits from #53976.

/cc @cockroachdb/release


Release justification: bug fixes and low-risk updates to new functionality

This commit adds an exploration rule ConvertLeftToInnerJoin, which converts
a left join to an inner join with the same ON condition, and then wraps the
expression in another left join with the original left side. In order to
avoid computing the left side of the join twice, we create a With expression
for the left side, and then reference it with two WithScans. For example
(assuming x is the primary key of a):

  SELECT a.x, b.y FROM a LEFT JOIN b ON ST_Intersects(a.geom, b.geom);

is converted to:

  WITH a_buf AS (
    SELECT * FROM a
  )
  SELECT a_buf.x, inr.y FROM a_buf LEFT JOIN (
    SELECT * FROM a_buf JOIN b ON ST_Intersects(a_buf.geom, b.geom)
  ) AS inr
  ON a_buf.x = inr.x;

Note that this transformation is not desirable in the general case, but it
is useful if there is a possibility of creating an inverted join (such as in
the above example). For this reason, we only perform this transformation if
it is possible to generate an inverted join.

This transformation allows us to index-accelerate spatial left joins, which
was not possible before.

Informs #53576

Release note (performance improvement): left outer spatial joins can now
be index-accelerated, which can lead to performance improvements in some
cases.

Release justification: bug fixes and low-risk updates to new functionality

This commit adds an exploration rule ConvertLeftToInnerJoin, which converts
a left join to an inner join with the same ON condition, and then wraps the
expression in another left join with the original left side. In order to
avoid computing the left side of the join twice, we create a With expression
for the left side, and then reference it with two WithScans. For example
(assuming x is the primary key of a):

  SELECT a.x, b.y FROM a LEFT JOIN b ON ST_Intersects(a.geom, b.geom);

is converted to:

  WITH a_buf AS (
    SELECT * FROM a
  )
  SELECT a_buf.x, inr.y FROM a_buf LEFT JOIN (
    SELECT * FROM a_buf JOIN b ON ST_Intersects(a_buf.geom, b.geom)
  ) AS inr
  ON a_buf.x = inr.x;

Note that this transformation is not desirable in the general case, but it
is useful if there is a possibility of creating an inverted join (such as in
the above example). For this reason, we only perform this transformation if
it is possible to generate an inverted join.

This transformation allows us to index-accelerate spatial left joins, which
was not possible before.

Informs cockroachdb#53576

Release note (performance improvement): left outer spatial joins can now
be index-accelerated, which can lead to performance improvements in some
cases.
@rytaft rytaft requested review from RaduBerinde, sumeerbhola and a team September 9, 2020 13:02
@rytaft rytaft requested a review from a team as a code owner September 9, 2020 13:02
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@rytaft
Copy link
Collaborator Author

rytaft commented Sep 9, 2020

I added this to #53662 for approval

Copy link
Member

@RaduBerinde RaduBerinde left a comment

Choose a reason for hiding this comment

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

LGTM (pending approval from release team)

@rytaft rytaft merged commit fbdf2d7 into cockroachdb:release-20.2 Sep 16, 2020
@rytaft rytaft deleted the backport20.2-53976 branch September 16, 2020 19:43
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.

None yet

3 participants