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

[SPARK-45009][SQL] Decorrelate predicate subqueries in join condition #42725

Closed

Conversation

andylam-db
Copy link
Contributor

@andylam-db andylam-db commented Aug 29, 2023

What changes were proposed in this pull request?

Pulling up correlated subquery predicates in Joins, and re-writing them into ExistenceJoins if they are not pushed down into the join inputs.

Why are the changes needed?

This change allows correlated IN and EXISTS subqueries in join condition. This is valid SQL that is not yet supported by Spark SQL.

Does this PR introduce any user-facing change?

Yes, previously unsupported queries become supported.

How was this patch tested?

Added SQL tests for IN and EXISTS in join conditions, and crossed-check correctness with postgres (except for ANTI joins, which are not supported in postgres).

Permutations of the tests:

  1. Exists / Not exists / in / not in
  2. Subquery references left child / right child
  3. Join type: inner / left outer
  4. Transitive predicates to try invoking filter inference

@github-actions github-actions bot added the SQL label Aug 29, 2023
@andylam-db andylam-db changed the title Decorrelate predicate subqueries in join condition [SPARK-45009] Decorrelate predicate subqueries in join condition Aug 29, 2023
@andylam-db andylam-db changed the title [SPARK-45009] Decorrelate predicate subqueries in join condition [SPARK-45009][SQL] Decorrelate predicate subqueries in join condition Aug 29, 2023
@andylam-db andylam-db marked this pull request as draft August 29, 2023 20:45
Copy link
Contributor

@jchen5 jchen5 left a comment

Choose a reason for hiding this comment

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

Let's also add test cases for other join types: right, full, semi, anti

Looks pretty good overall! CC @agubichev

@andylam-db andylam-db marked this pull request as ready for review September 5, 2023 20:40
@@ -55,7 +55,8 @@ abstract class Optimizer(catalogManager: CatalogManager)
Set(
"PartitionPruning",
"RewriteSubquery",
"Extract Python UDFs")
"Extract Python UDFs",
"Infer Filters")
Copy link
Contributor Author

@andylam-db andylam-db Sep 6, 2023

Choose a reason for hiding this comment

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

"Infer Filters" is not inherently idempotent. We need to exclude it from the idempotency check so that tests will pass.

@andylam-db
Copy link
Contributor Author

Looking for reviews! @cloud-fan @sigmod

@andylam-db
Copy link
Contributor Author

Pinging for reviews! @allisonwang-db @cloud-fan

@andylam-db
Copy link
Contributor Author

andylam-db commented Oct 13, 2023

@cloud-fan I think the build is failing because of unrelated failing tests -- timeouts for KafkaSourceStressSuite and OracleIntegrationSuite.
Could you take a look and see if we can merge this?

1:

[info] KafkaSourceStressSuite:
[info] - stress test with multiple topics and partitions *** FAILED *** (1 minute, 4 seconds)
[info]   Timed out waiting for stream: The code passed to failAfter did not complete within 30 seconds.

2:

[info] OracleIntegrationSuite:
[info] org.apache.spark.sql.jdbc.v2.OracleIntegrationSuite *** ABORTED *** (8 minutes, 6 seconds)
[info]   The code passed to eventually never returned normally. Attempted 417 times over 7.011344010933333 minutes. Last failure message: ORA-12514: Cannot connect to database. Service freepdb1 is not registered with the listener at host 10.1.0.85 port 40439. (CONNECTION_ID=7qWwov5HRIGzNr+ypAdPFQ==). (DockerJDBCIntegrationSuite.scala:166)

@cloud-fan
Copy link
Contributor

yea they are unrelated, thanks, merging to master!

@cloud-fan cloud-fan closed this in 4fd2d68 Oct 16, 2023
gengliangwang pushed a commit that referenced this pull request May 23, 2024
…tion of predicate subqueries in join condition which reference both join child

### What changes were proposed in this pull request?

This is a follow up PR for #42725, which decorrelates predicate subqueries in join conditions. I forgot to add the error class definition for the case where the subquery references both join children, and test cases for it.

### Why are the changes needed?

To show a clear error message when the condition is hit.

### Does this PR introduce _any_ user-facing change?

No.

### How was this patch tested?

Added SQL test and golden files.

### Was this patch authored or co-authored using generative AI tooling?

No.

Closes #46708 from andylam-db/follow-up-decorrelate-subqueries-in-join-cond.

Authored-by: Andy Lam <andy.lam@databricks.com>
Signed-off-by: Gengliang Wang <gengliang@apache.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
5 participants