Skip to content

[Spark-50873][SQL] Prune column for ExistsSubquery in DSV2 #50916

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Akeron-Zhu
Copy link

@Akeron-Zhu Akeron-Zhu commented May 16, 2025

What changes were proposed in this pull request?

This PR offers an optimize rule for SparkOptimizer to prune unnecessary column for DataSourceV2 (DSV2) after RewriteSubquery.
Spark 3 use V2ScanRelationPushDown rule to prune column for DSV2. However, if there are subquerys in the qeuery sql, RewriteSubery rule will be generated new predicates which can be use to prune column after executed V2ScanRelationPushDown, but Spark does not prune column again which cause lower performance.
See the issue for more detail description : SPARK-50873
A Similar issue: SPARK-51831

This PR's Solution:

This PR rewrite "SELECT *" as "SELECT 1" in WHERE EXISTS subquery, because "Select *" does not provide any project information when entering V2ScanRelationPushDown#pruneColumn function but "SELECT 1"'s project provides the necessary column.

A more general Solution:

Since EXISTS only returns True or False, it doesn't matter what is SELECTED. Therefore, an optimization rule can be added to check whether the EXISTS clause contains Project 1, If not, add a Project 1 on the EXISTS. This method can also solve the current problem and provide a wider range of optimizations. See the SPARK-52186.

Why are the changes needed?

A better performance for Spark DSV2.
For example, in 10T TPCDS test, the query16 execution time will be reduced by 50% from 2.5min to 1.3min in my cluster.

Does this PR introduce any user-facing change?

NO

How was this patch tested?

Github Actions.
Currently, only TPCDSV1_4-PLanStabilitySuite and TPCDSV1_4-PLanStabilityWithStatsSuite are unable to pass. The plan is correct, but after this rule, the positions of some columns were changed. like this:
Plan Compare

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

No

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant