LuciferYang opened a new pull request, #60168:
URL: https://github.com/apache/spark/pull/60168

   ### What changes were proposed in this pull request?
   
   With CBO enabled, `JoinEstimation` estimates an equi-join as empty when a 
join key has distinct and null counts but no min/max. `ValueInterval` turns a 
missing min or max into a `NullValueInterval`, which is meant for a column 
holding only nulls, and a `NullValueInterval` intersects nothing.
   
   This PR makes `JoinEstimation` treat such a key as having an unknown range, 
as it already does for a string key, so the join is estimated by distinct 
counts. A key with no distinct values still holds only nulls and still makes 
the join empty. Two related changes:
   
   - `ValueInterval.isIntersected` checks `NullValueInterval` before 
`DefaultValueInterval`, so a key holding only nulls still matches nothing when 
the other key's range is unknown.
   - Histograms are used only when both keys have min/max, because 
`computeByHistogram` bounds them by the intersected min/max.
   
   ### Why are the changes needed?
   
   A 0-row estimate lets the planner broadcast that side of a later join 
however large it is. SPARK-56485 fixed the same problem in `FilterEstimation`.
   
   Such statistics come from:
   
   - a DSv2 source that reports distinct and null counts but no min/max;
   - `UnionEstimation`, which keeps the distinct and null counts of every 
column but min/max only for some types. For example, with all three tables 
analyzed and `k` a boolean column, `(SELECT * FROM a1 UNION ALL SELECT * FROM 
a2) u JOIN b ON u.k = b.k` was estimated at 0 rows and returns 500000.
   
   ### Does this PR introduce _any_ user-facing change?
   
   With CBO enabled, these joins get a non-zero row estimate, so the join 
strategy or join order chosen for them can change. Query results do not change.
   
   ### How was this patch tested?
   
   New tests in `JoinEstimationSuite`:
   
   - a key with distinct and null counts but no min/max is estimated by 
distinct counts, and the joined keys have no min/max;
   - the same when both keys have histograms;
   - a boolean key coming out of a `Union`;
   - a key holding only nulls makes the join empty against a key without 
min/max, on either side.
   
   Without this change, the first three estimate 0 rows. The last one passes 
either way, and fails if `isIntersected` keeps the old order.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Opus 5.5
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to