LuciferYang commented on code in PR #60168:
URL: https://github.com/apache/spark/pull/60168#discussion_r4573166865
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/statsEstimation/JoinEstimationSuite.scala:
##########
@@ -628,6 +628,120 @@ class JoinEstimationSuite extends StatsEstimationTestBase
{
}
}
+ test("SPARK-61002: a join key without min/max is not taken as holding only
nulls") {
Review Comment:
Added "an outer join on a key without min/max" in 0794944416e. `LeftOuter`
gives 5 rows, the outer side keeps its stats and the key without min/max takes
the joined stats. `RightOuter` gives 3 rows and the reverse. `FullOuter` gives
5 rows and keeps the stats of both sides.
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/statsEstimation/JoinEstimationSuite.scala:
##########
@@ -628,6 +628,120 @@ class JoinEstimationSuite extends StatsEstimationTestBase
{
}
}
+ test("SPARK-61002: a join key without min/max is not taken as holding only
nulls") {
+ // table1 (key-1-5 int, key-5-9 int): (1, 9), (2, 8), (3, 7), (4, 6), (5,
5)
+ // A data source can report the distinct and null counts of a column but
not its min/max.
+ val (noMinMax, noMinMaxStat) = (attr("key-no-min-max"),
+ ColumnStat(distinctCount = Some(3), min = None, max = None,
+ nullCount = Some(0), avgLen = Some(4), maxLen = Some(4)))
+ val noMinMaxTable = StatsTestPlan(
+ outputList = Seq(noMinMax),
+ rowCount = 3,
+ attributeStats = AttributeMap(Seq(noMinMax -> noMinMaxStat)))
+ val join = Join(table1, noMinMaxTable, Inner,
+ Some(EqualTo(nameToAttr("key-1-5"), noMinMax)), JoinHint.NONE)
+ // Estimated by ndv: 5 * 3 / max(5, 3) = 3. The joined keys keep the range
of key-1-5.
+ val joinedColStat = ColumnStat(distinctCount = Some(3), min = Some(1), max
= Some(5),
+ nullCount = Some(0), avgLen = Some(4), maxLen = Some(4))
+ val expectedStats = Statistics(
+ sizeInBytes = 3 * (8 + 4 * 3),
+ rowCount = Some(3),
+ attributeStats = AttributeMap(Seq(
+ nameToAttr("key-1-5") -> joinedColStat, noMinMax -> joinedColStat,
+ nameToAttr("key-5-9") ->
nameToColInfo("key-5-9")._2.copy(distinctCount = Some(3)))))
+ assert(join.stats == expectedStats)
+ }
+
+ test("SPARK-61002: join keys with histograms but without min/max are
estimated by ndv") {
+ val histogram = Histogram(height = 10, Array(
+ HistogramBin(lo = 1, hi = 5, ndv = 5), HistogramBin(lo = 5, hi = 9, ndv
= 4)))
+ val (col1, col2) = (attr("key1"), attr("key2"))
+ val withMinMax = generateJoinChild(col1, histogram, 1, 9)
+ val noMinMax = StatsTestPlan(
+ outputList = Seq(col2),
+ rowCount = 20,
+ attributeStats = AttributeMap(Seq(
+ col2 -> inferColumnStat(histogram, 1, 9).copy(min = None, max =
None))))
+ val join = Join(withMinMax, noMinMax, Inner, Some(EqualTo(col1, col2)),
JoinHint.NONE)
+ // 20 * 20 / max(9, 9), rounded up.
+ assert(join.stats.rowCount == Some(45))
+ }
+
+ test("SPARK-61002: a join key without min/max drops its histogram for the
range of the other") {
+ // The joined keys take the range [1, 5] of `known`. The histogram of
`unknown` covers only
+ // [2, 4], so it is dropped, while the histogram of `known` is kept.
+ val knownHistogram = Histogram(height = 5, Array(
+ HistogramBin(lo = 1, hi = 3, ndv = 3), HistogramBin(lo = 3, hi = 5, ndv
= 3)))
+ val unknownHistogram = Histogram(height = 5, Array(
+ HistogramBin(lo = 2, hi = 3, ndv = 2), HistogramBin(lo = 3, hi = 4, ndv
= 2)))
+ val (known, unknown) = (attr("known"), attr("unknown"))
+ def table(key: AttributeReference, stat: ColumnStat): StatsTestPlan =
+ StatsTestPlan(Seq(key), rowCount = 10, attributeStats =
AttributeMap(Seq(key -> stat)))
+ val knownTable = table(known, ColumnStat(distinctCount = Some(5), min =
Some(1),
+ max = Some(5), nullCount = Some(0), avgLen = Some(4), maxLen = Some(4),
+ histogram = Some(knownHistogram)))
+ val unknownTable = table(unknown, ColumnStat(distinctCount = Some(3), min
= None,
+ max = None, nullCount = Some(0), avgLen = Some(4), maxLen = Some(4),
+ histogram = Some(unknownHistogram)))
+ // 10 * 10 / max(5, 3), rounded up.
+ val joinedColStat = ColumnStat(distinctCount = Some(3), min = Some(1), max
= Some(5),
+ nullCount = Some(0), avgLen = Some(4), maxLen = Some(4))
+ Seq(
+ Join(knownTable, unknownTable, Inner, Some(EqualTo(known, unknown)),
JoinHint.NONE),
+ Join(unknownTable, knownTable, Inner, Some(EqualTo(unknown, known)),
JoinHint.NONE)
+ ).foreach { join =>
+ assert(join.stats.rowCount == Some(20))
+ assert(join.stats.attributeStats(known) ==
+ joinedColStat.copy(histogram = Some(knownHistogram)))
+ assert(join.stats.attributeStats(unknown) == joinedColStat)
+ // A filter estimated with the histogram of `unknown` would read it
outside of its bins.
+ assert(Filter(EqualTo(unknown, Literal(3)),
join).stats.rowCount.isDefined)
Review Comment:
Done in 0794944416e: they now assert 7 and 10 rows.
##########
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.
+ */
+ private def keyInterval(key: AttributeReference, keyStat: ColumnStat):
ValueInterval = {
Review Comment:
I kept the helper, so that this PR changes only the join path. The
`FilterEstimation` callers already check `hasMinMaxStats` before calling
`apply`, as you noted.
--
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]