dongjoon-hyun opened a new pull request, #60401:
URL: https://github.com/apache/spark/pull/60401

   ### 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
   


-- 
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