LuciferYang commented on code in PR #60168:
URL: https://github.com/apache/spark/pull/60168#discussion_r4573166458
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/JoinEstimation.scala:
##########
@@ -228,6 +236,19 @@ case class JoinEstimation(join: Join) extends Logging {
(joinCard, AttributeMap(keyStatsAfterJoin))
}
+ /**
+ * Returns the value range of a join key. A key with distinct values but no
min/max (a data
+ * source may not report them, and a union drops them for some types) has an
unknown range, not
+ * only nulls, so it overlaps the other key unless that key holds only nulls.
Review Comment:
Done in 0794944416e. `keyInterval` now checks the distinct count first, so a
key with a distinct count of 0 is a `NullValueInterval` whatever its type, and
`noValues` is gone. The Scaladoc and the description say so. The null-only test
now runs on INT and STRING keys.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/ValueInterval.scala:
##########
@@ -80,9 +81,14 @@ object ValueInterval {
*/
def intersect(r1: ValueInterval, r2: ValueInterval, dt: DataType):
(Option[Any], Option[Any]) = {
(r1, r2) match {
- case (_, _: DefaultValueInterval) | (_: DefaultValueInterval, _) =>
- // binary/string types don't support intersecting.
+ case (_: DefaultValueInterval, _: DefaultValueInterval) =>
Review Comment:
Done in 0794944416e. Any pair with an unknown range other than a
known/unknown pair returns `(None, None)` again, as before this PR, and the doc
says that `isIntersected` checks the overlap first. The null-only test also
calls `intersect` on such a pair.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/JoinEstimation.scala:
##########
@@ -196,16 +196,22 @@ case class JoinEstimation(join: Join) extends Logging {
// Check if the two sides are disjoint
val leftKeyStat = leftStats.attributeStats(leftKey)
val rightKeyStat = rightStats.attributeStats(rightKey)
- val lInterval = ValueInterval(leftKeyStat.min, leftKeyStat.max,
leftKey.dataType)
- val rInterval = ValueInterval(rightKeyStat.min, rightKeyStat.max,
rightKey.dataType)
+ val lInterval = keyInterval(leftKey, leftKeyStat)
+ val rInterval = keyInterval(rightKey, rightKeyStat)
// A distinct count of 0 on both sides means neither has a non-null key
value.
val noValues = leftKeyStat.distinctCount.contains(BigInt(0)) &&
rightKeyStat.distinctCount.contains(BigInt(0))
if (!noValues && ValueInterval.isIntersected(lInterval, rInterval)) {
val (newMin, newMax) = ValueInterval.intersect(lInterval, rInterval,
leftKey.dataType)
+ // If only one key has a known range, the joined keys take it. The
histogram of the other
+ // key may not cover that range, so it is neither used nor propagated.
+ def keepsHistogram(interval: ValueInterval): Boolean =
+ newMin.isEmpty || interval.isInstanceOf[NumericValueInterval]
Review Comment:
Added "join keys that both lack min/max are estimated by ndv" in
0794944416e, with and without histograms: 10 * 10 / max(5, 4) = 20 rows, and
both keys get distinct count 4, no min/max, and their histogram as it was.
--
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]