spark-test commented on code in PR #60633:
URL: https://github.com/apache/spark/pull/60633#discussion_r4607588820
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/statsEstimation/BasicStatsEstimationSuite.scala:
##########
@@ -402,6 +402,18 @@ test("range with invalid long value") {
attributeStats = AttributeMap(Seq(attribute -> colStat))))
}
+ test("SPARK-61891: a left single join is estimated from its left side") {
+ // It outputs each of the 10 rows of `plan` once, with the column of
`other` added.
+ val otherKey = attr("other")
+ val other = StatsTestPlan(outputList = Seq(otherKey), rowCount = 1000,
+ attributeStats = AttributeMap(Seq(otherKey -> colStat)), size =
Some(1000 * (8 + 4)))
+ val join = Join(plan, other, LeftSingle, Some(attribute === otherKey),
JoinHint.NONE)
Review Comment:
**Nit (P3):** `plan` has size 10 * (8 + 4) = 120, exactly its row count
times its row width. At that ratio the visitor's scaled size, 120 * 16 / 12 =
160, is the same as the left semi estimate JoinEstimation would compute from
the row count and column stats, 10 * (8 + 4 + 4) = 160, with the same row count
and column stats. So the CBO-on run would still pass if `LeftSingle` were added
to `case LeftSemi | LeftAnti` in JoinEstimation.scala:44, which is the estimate
the PR description says it deliberately avoids. The SPARK-61534 test above it
(BasicStatsEstimationSuite.scala:392) doubles the left side's size so that the
two estimates differ, 320 against 160.
**Verification:**
- **Regression:** With CBO on and with CBO off, the left single join
statistics test uses a left side whose size is not its row count times its row
width, asserts the size scaled by the row widths, and fails if left single
joins are estimated like left semi joins from the left row count and column
stats.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/basicLogicalOperators.scala:
##########
@@ -769,7 +769,7 @@ case class Join(
None
}
- case LeftSemi | LeftAnti =>
+ case LeftSemi | LeftAnti | LeftSingle =>
Review Comment:
**Nit (P3):** This bound changes one plan that the PR description does not
mention. Before the subquery is rewritten, EliminateLimits
(Optimizer.scala:123, ahead of RewriteCorrelatedScalarSubquery at
Optimizer.scala:152) already sees the outer side's bound through the Project or
Filter that holds the subquery, so a root ORDER BY ... LIMIT n over an outer
side bounded by n rows loses its limit on the base branch too. The new bound
matters only for SparkOptimizer's last EliminateLimits
(SparkOptimizer.scala:134-139), which runs after the 'LocalRelation' batch has
evaluated a filter pushed onto a local outer side (Optimizer.scala:3822-3827).
Reading the optimizer and planner code, take `SELECT * FROM (SELECT a.k,
(SELECT t.v FROM t WHERE t.k = a.k) AS v FROM VALUES (1), (2), (3) AS a(k)) x
WHERE x.k > 1 ORDER BY v LIMIT 2`, with `t` a file table. The filter cuts the
list to 2 rows only in that late batch. On the base branch the join's maxRows
is still None at that point, so the limit stays and the root is
TakeOrderedAndProjectExec. With this change the bound is 2, so the limit is
dropped and the root becomes a global SortExec over a range exchange, because
the 2-row LocalTableScanExec under the join is not single-partitioned. That
adds a RangePartitioner sampling job and a shuffle over at most 2 rows. Results
are the same, and AQE does not adopt such a re-plan at runtime, so only an
outer side whose bound drops this late is affected.
No change local to `Join.maxRows` avoids this without giving up the new
bound, and keeping the limit would mean changing EliminateLimits for every
bounded input, so the cost is one to state and accept rather than to remove.
**Verification:**
- **Inspection:** The PR description states that a root ORDER BY ... LIMIT n
over a left single join whose outer side's row bound falls to n or fewer only
late in optimization, such as a VALUES list that a filter from an enclosing
query cuts to n rows or fewer, now plans a global sort with a range shuffle
instead of a single top-K job, as it already does over a left semi or anti
join, names keeping that limit in EliminateLimits as the alternative, and says
why this PR does not take it.
##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/plans/LogicalPlanSuite.scala:
##########
@@ -169,6 +170,11 @@ class LogicalPlanSuite extends SparkFunSuite {
assert(query.where(Literal.FalseLiteral).maxRowsPerPartition.contains(0))
}
+ test("SPARK-61891: a left single join outputs at most as many rows as its
left side") {
+ val join = Join(Range(0, 10, 1, 1), Range(0, 100, 1, 1), LeftSingle, None,
JoinHint.NONE)
Review Comment:
**Nit (P3):** Both sides of this join are bounded, so the test only covers
the change from the product of the bounds to the left side's bound. The change
also gives a left single join the left side's bound when the right side has no
row bound, which the base branch turned into None and which is the usual input,
a scalar subquery over a table. If the new case were narrowed to `case
LeftSingle if right.maxRows.isDefined => left.maxRows`, this test would still
pass, while that case would lose its bound again.
**Verification:**
- **Regression:** The left single join maxRows test also covers a right side
with no row bound and asserts that the join's maxRows is the left side's bound,
so it fails if the bound falls back to None for an unbounded right side.
--
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]