zikangh opened a new pull request, #60436:
URL: https://github.com/apache/spark/pull/60436

   ### What changes were proposed in this pull request?
   
   A streaming query builds its DSv2 `Scan` once, when the query starts, and 
`StreamingDataSourceV2ScanRelation.computeStats` asks that scan for its 
statistics. So every micro-batch of a DSv2 source reports the statistics of the 
whole source, or `spark.sql.defaultSizeInBytes` when the scan reports none.
   
   This PR adds the mix-in `SupportsReportMicroBatchStatistics` for 
`MicroBatchStream`, with one method, `estimateStatistics(Offset start, Offset 
end)`. When the stream of a micro-batch implements it, `computeStats` returns 
the statistics of the micro-batch's offset range. Otherwise it keeps using the 
scan's statistics.
   
   The method is on the stream, not the scan, because the stream owns the 
offsets.
   
   ### Why are the changes needed?
   
   The optimizer plans each micro-batch with these statistics. With the 
statistics of the whole source, a small micro-batch of a large table is never 
broadcast in a stream-static join, a sort-merge join shuffles both sides 
instead, and cost-based estimates use the row count of the whole table. A V1 
streaming source does not have this problem, because it builds a new relation 
for each micro-batch.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, for connector developers: a new `@Evolving` DSv2 interface. Queries 
change only for sources that implement it.
   
   ### How was this patch tested?
   
   New `StreamingMicroBatchStatisticsSuite`: with a fake source whose scan 
reports the whole source, a micro-batch reports its offset range's statistics 
and is broadcast in a stream-static join only when its stream implements the 
interface.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Isaac (Claude)
   
   This pull request and its description were written by Isaac.
   


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