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-38155][SQL] Disallow distinct aggregate in lateral subqueries with unsupported predicates #35469

Closed

Conversation

allisonwang-db
Copy link
Contributor

@allisonwang-db allisonwang-db commented Feb 9, 2022

What changes were proposed in this pull request?

This PR blocks lateral subqueries that contain DISTINCT aggregate and correlated non-equality predicates in CheckAnalysis.

Why are the changes needed?

To avoid incorrect results. DISTINCT will be rewritten into Aggregate during the optimization phase and only correlated equality predicates are supported with Aggregate.

Note, we only need to block this pattern for lateral subqueries because 1) Scalar subqueries are either aggregated or can only contain one row, so they will not encounter this issue 2) IN/EXISTS subqueries can potentially have the same issue but it won't impact the results of these subqueries, for example, SELECT * FROM t1 WHERE c1 IN (SELECT DISTINCT c1 FROM t2 WHERE t1.c1 > t2.c1).

Does this PR introduce any user-facing change?

Yes. Queries with lateral joins that contain DISTINCT aggregate will be blocked:

SELECT * FROM t1 JOIN LATERAL (SELECT DISTINCT c2 FROM t2 WHERE c1 > t1.c1)

AnalysisException: Correlated column is not allowed in predicate

How was this patch tested?

Unit tests.

@github-actions github-actions bot added the SQL label Feb 9, 2022
@allisonwang-db
Copy link
Contributor Author

cc @cloud-fan

@cloud-fan
Copy link
Contributor

thanks, merging to master!

@cloud-fan cloud-fan closed this in e6c5687 Feb 22, 2022
@allisonwang-db allisonwang-db deleted the spark-38155-distinct branch January 19, 2024 01:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants