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

Resolve empty relation opt for join types #11066

Merged
merged 7 commits into from
Jun 25, 2024
Merged

Resolve empty relation opt for join types #11066

merged 7 commits into from
Jun 25, 2024

Conversation

LorrensP-2158466
Copy link
Contributor

Which issue does this PR close?

Almost closes #10967:

Rationale for this change

Currently the PropagateEmptyRelation relation doesn't optimize all join types
This PR closes following cases

  • For LeftAnti Join, if the right side is empty => the Join result is left side (including null)
  • For RightAnti Join, if the left side is empty => the Join result is right side (including null)
  • For Full Join, if both sides are empty => the Join result is empty

What changes are included in this PR?

Added match statements that resolve the above cases.

Are these changes tested?

Yes these changes are tested with unit tests and 3 added slt tests.

Are there any user-facing changes?

No

@github-actions github-actions bot added optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt) labels Jun 22, 2024
Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

@@ -142,13 +146,19 @@ impl OptimizerRule for PropagateEmptyRelation {
schema: join.schema.clone(),
}),
)),
JoinType::LeftAnti if right_empty => {
Copy link
Contributor

@alamb alamb Jun 23, 2024

Choose a reason for hiding this comment

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

I double checked that an ANTI join (e.g. a NOT IN) is true when there are no tuples (aka the right side is emprt)

postgres=# select * from foo;
  x
-----
   1
   2
 NaN
(3 rows)

postgres=# select * from bar;
 y
---
(0 rows)

postgres=# select x NOT IN (SELECT y from bar) from foo;
 ?column?
----------
 t
 t
 t
(3 rows)

postgres=# select x from foo where NOT EXISTS (SELECT y from bar);
  x
-----
   1
   2
 NaN
(3 rows)

Thus I think this is a valid rewrite

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice, thanks for the double check!

JoinType::RightAnti if right_empty => Ok(Transformed::yes(
LogicalPlan::EmptyRelation(EmptyRelation {
produce_one_row: false,
schema: join.schema.clone(),
}),
)),
_ => Ok(Transformed::no(LogicalPlan::Join(join.clone()))),
_ => Ok(Transformed::no(plan)),
Copy link
Member

Choose a reason for hiding this comment

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

👍

Copy link
Member

@jonahgao jonahgao left a comment

Choose a reason for hiding this comment

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

Looks good to me! Thanks @LorrensP-2158466

Copy link
Contributor

@tshauck tshauck left a comment

Choose a reason for hiding this comment

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

Nothing to add over what's been said... LGTM!

@alamb
Copy link
Contributor

alamb commented Jun 24, 2024

I merged this PR up from main and plan to merge it when CI passes

@jonahgao jonahgao merged commit 8b244ee into apache:main Jun 25, 2024
23 checks passed
@jonahgao
Copy link
Member

Merged. Thanks @LorrensP-2158466 @alamb @tshauck

@LorrensP-2158466 LorrensP-2158466 deleted the resolve-empty-relation-opt-for-join-types branch June 26, 2024 16:16
findepi pushed a commit to findepi/datafusion that referenced this pull request Jul 16, 2024
* handle left/right anti with empty join_table & added tests for those

* add tests and only resolve cases 1,2,3 of issue

* forgot to change a test

* Update datafusion/optimizer/src/propagate_empty_relation.rs

Co-authored-by: Jonah Gao <jonahgao@msn.com>

---------

Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
Co-authored-by: Jonah Gao <jonahgao@msn.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Complete Possible Join Type Handling for EmptyRelation Propagation Rule
4 participants