Skip to content

[SPARK-56355][SQL] Improve join stats estimation when equi-join keys lack column statistics#55195

Open
wecharyu wants to merge 2 commits intoapache:masterfrom
wecharyu:enhance_join_estimate
Open

[SPARK-56355][SQL] Improve join stats estimation when equi-join keys lack column statistics#55195
wecharyu wants to merge 2 commits intoapache:masterfrom
wecharyu:enhance_join_estimate

Conversation

@wecharyu
Copy link
Copy Markdown
Contributor

@wecharyu wecharyu commented Apr 4, 2026

What changes were proposed in this pull request?

Improve join estimation in case where equal-join's join key column stats are missing. Currently, the estimator could fall back to a Cartesian product T(A) * T(B), which often leads to severe overestimation.

We can instead use max(T(A), T(B)) as a more accurate estimate.

Why are the changes needed?

To improve the reliability of join estimation especially in AQE re-optimize.

Does this PR introduce any user-facing change?

No.

How was this patch tested?

Add unit test, and test on the prod Spark job.

TPC-DS benchmark shows no performance regression:

  • Before
[info] TPCDS:                                    Best Time(ms)   Avg Time(ms)   Stdev(ms)    Rate(M/s)   Per Row(ns)   Relative
[info] ------------------------------------------------------------------------------------------------------------------------
[info] q24a                                             237404         237715         440          1.3         775.7       1.0X
  • After
[info] Intel(R) Xeon(R) Gold 6248 CPU @ 2.50GHz
[info] TPCDS:                                    Best Time(ms)   Avg Time(ms)   Stdev(ms)    Rate(M/s)   Per Row(ns)   Relative
[info] ------------------------------------------------------------------------------------------------------------------------
[info] q24a                                             236725         237440        1011          1.3         773.5       1.0X

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

Cursor / Opus 4.6

@wecharyu
Copy link
Copy Markdown
Contributor Author

wecharyu commented Apr 5, 2026

cc @cloud-fan @wangyum

import org.apache.spark.sql.catalyst.dsl.plans._
import org.apache.spark.sql.catalyst.expressions.{Ascending, Attribute, AttributeMap, AttributeReference, Literal, SortOrder}
import org.apache.spark.sql.catalyst.plans.{Inner, PlanTest}
import org.apache.spark.sql.catalyst.plans.{Inner, LeftOuter, PlanTest}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

why leftouter not inner

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

test for both inner and leftouter

@wecharyu
Copy link
Copy Markdown
Contributor Author

gentle ping @cloud-fan , could you help take a look? The original over-estimation could lead Gluten choose wrong build side and cause OOM, details in apache/gluten#11774

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.

2 participants