This is an automated email from the ASF dual-hosted git repository.
dongjoon-hyun pushed a commit to branch branch-4.x
in repository https://gitbox.apache.org/repos/asf/spark.git
The following commit(s) were added to refs/heads/branch-4.x by this push:
new 22e216b33122 [SPARK-61558][SQL] Fix broadcasting the large side of a
join under OFFSET when the child has no row count
22e216b33122 is described below
commit 22e216b33122259c8c17dcb42ad2a44497283125
Author: Dongjoon Hyun <[email protected]>
AuthorDate: Thu Nov 12 16:38:30 2026 -0800
[SPARK-61558][SQL] Fix broadcasting the large side of a join under OFFSET
when the child has no row count
### What changes were proposed in this pull request?
This PR aims to fix the size estimation of `Offset` when its child has no
row count. `SizeInBytesOnlyStatsPlanVisitor.visitOffset` assumed zero rows in
that case and estimated `Offset` as an empty relation (`sizeInBytes = 1` and
`rowCount = 0`), so the large side of a join could be broadcast.
Now, it estimates `Offset` like the other unary nodes (`visitUnaryNode`).
The child's size is used as an upper bound, and no row count is propagated.
When the child has a row count, the estimation is unchanged.
### Why are the changes needed?
A file source relation has no row count unless the table is analyzed and
CBO or `spark.sql.cbo.planStats.enabled` is on. CBO doesn't help either,
because `BasicStatsPlanVisitor.visitOffset` falls back to the same method.
```scala
spark.range(0, 1000000, 1, 4).selectExpr("id AS k", "md5(CAST(id AS
STRING)) AS s").write.parquet("/tmp/big")
spark.range(0, 100, 1, 1).selectExpr("id AS k", "id AS
v").write.parquet("/tmp/small")
spark.conf.set("spark.sql.adaptive.enabled", "false")
// Issue: It should broadcast `small` (100 rows), but it broadcasts `big`
(1M rows, 34.9 MiB).
spark.read.parquet("/tmp/big").offset(1).join(spark.read.parquet("/tmp/small"),
"k").explain("cost")
```
```
== Optimized Logical Plan ==
Project [k#0L, s#1, v#3L], Statistics(sizeInBytes=1292.0 B)
+- Join Inner, (k#0L = k#2L), Statistics(sizeInBytes=1528.0 B)
:- Filter isnotnull(k#0L), Statistics(sizeInBytes=1.0 B)
: +- Offset 1, Statistics(sizeInBytes=1.0 B, rowCount=0)
: +- Relation [k#0L,s#1] parquet, Statistics(sizeInBytes=34.9 MiB)
+- Filter isnotnull(k#2L), Statistics(sizeInBytes=1528.0 B)
+- Relation [k#2L,v#3L] parquet, Statistics(sizeInBytes=1528.0 B)
== Physical Plan ==
*(3) Project [k#0L, s#1, v#3L]
+- *(3) BroadcastHashJoin [k#0L], [k#2L], Inner, BuildLeft, false, false
:- BroadcastExchange HashedRelationBroadcastMode(List(input[0, bigint,
false]),false), [plan_id=1618]
: +- *(2) Filter isnotnull(k#0L)
: +- *(2) GlobalLimit -1, 1
...
```
With AQE on, the initial plan is the same. The final plan is usually
corrected because the shuffle under the offset gives runtime statistics. When
the child already has a single partition, e.g. `coalesce(1).offset(1)`, there
is no shuffle, and the large side is still broadcast.
This has been the behavior since `OFFSET` was added in Apache Spark 3.4.0
(SPARK-28330).
### Does this PR introduce _any_ user-facing change?
Yes. The query plan changes. A join no longer broadcasts a side that has
`OFFSET` over a relation without a row count based on a 1-byte estimate.
### How was this patch tested?
Pass the CIs with the newly added test case, which fails without this fix.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 5.5
Closes #60401 from dongjoon-hyun/SPARK-61558.
Authored-by: Dongjoon Hyun <[email protected]>
Signed-off-by: Dongjoon Hyun <[email protected]>
(cherry picked from commit 7a0ea90e0004c126a6bdc8b98a0be96c7e5a0f01)
Signed-off-by: Dongjoon Hyun <[email protected]>
skip-checks: true
---
.../statsEstimation/SizeInBytesOnlyStatsPlanVisitor.scala | 14 ++++++++++----
.../statsEstimation/BasicStatsEstimationSuite.scala | 7 +++++++
2 files changed, 17 insertions(+), 4 deletions(-)
diff --git
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/SizeInBytesOnlyStatsPlanVisitor.scala
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/SizeInBytesOnlyStatsPlanVisitor.scala
index 14cce02d99b7..223d081b8d14 100644
---
a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/SizeInBytesOnlyStatsPlanVisitor.scala
+++
b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/statsEstimation/SizeInBytesOnlyStatsPlanVisitor.scala
@@ -94,10 +94,16 @@ object SizeInBytesOnlyStatsPlanVisitor extends
LogicalPlanVisitor[Statistics] {
override def visitOffset(p: Offset): Statistics = {
val offset = p.offsetExpr.eval().asInstanceOf[Int]
val childStats = p.child.stats
- val rowCount: BigInt = childStats.rowCount.map(c => c -
offset).map(_.max(0)).getOrElse(0)
- Statistics(
- sizeInBytes = EstimationUtils.getOutputSize(p.output, rowCount,
childStats.attributeStats),
- rowCount = Some(rowCount))
+ childStats.rowCount.map { childRowCount =>
+ val rowCount = (childRowCount - offset).max(0)
+ Statistics(
+ sizeInBytes = EstimationUtils.getOutputSize(p.output, rowCount,
childStats.attributeStats),
+ rowCount = Some(rowCount))
+ }.getOrElse {
+ // Without the child's row count, the number of skipped rows is unknown.
Assume the same
+ // number of rows as the child, which is an upper bound.
+ visitUnaryNode(p)
+ }
}
override def visitIntersect(p: Intersect): Statistics = {
diff --git
a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/statsEstimation/BasicStatsEstimationSuite.scala
b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/statsEstimation/BasicStatsEstimationSuite.scala
index afb4618fea79..7d1e8b3082b1 100644
---
a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/statsEstimation/BasicStatsEstimationSuite.scala
+++
b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/statsEstimation/BasicStatsEstimationSuite.scala
@@ -218,6 +218,13 @@ test("range with invalid long value") {
checkStats(offset, plan.stats.copy(attributeStats = AttributeMap(Nil)))
}
+ test("SPARK-61558: offset estimation: child's rowCount is unknown") {
+ val childStats = Statistics(sizeInBytes = 120)
+ val offset = Offset(Literal(2), DummyLogicalPlan(childStats, childStats))
+ // The number of skipped rows is unknown, so the child's size is used as
an upper bound.
+ checkStats(offset, childStats)
+ }
+
test("limit estimation: limit < child's rowCount") {
val localLimit = LocalLimit(Literal(2), plan)
val globalLimit = GlobalLimit(Literal(2), plan)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]