ganeshashree opened a new pull request, #60451:
URL: https://github.com/apache/spark/pull/60451
### What changes were proposed in this pull request?
Python data source writers can now tell Spark how their input rows should be
grouped and sorted. Spark then adds the needed repartition and sort before the
write, the same way it already does for Scala/Java (DSv2) sinks.
`DataSourceWriter` gets five optional methods: `requiredDistribution`,
`requiredOrdering`, `requiredNumPartitions`, `distributionStrictlyRequired` and
`advisoryPartitionSizeInBytes`. Small value classes (`ClusteredDistribution`,
`OrderedDistribution`, `UnspecifiedDistribution`, `SortOrder`, `SortDirection`,
`NullOrdering`) describe the requirements.
```python
class MyWriter(DataSourceWriter):
def requiredDistribution(self):
return ClusteredDistribution((("region",),)) # one region per task
def requiredOrdering(self):
return [SortOrder(("event_time",))] # sorted within each
task
def write(self, iterator): ...
```
Spark asks the writer once while planning the query. The feature is behind
the internal config `spark.sql.python.writeDistributionAndOrdering.enabled`,
off by default, because it starts one extra Python worker during planning. It
applies to batch writes only; streaming writes are unchanged.
### Why are the changes needed?
Without this, a Python sink gets rows in whatever layout the query happened
to produce. A sink that writes partitioned or sorted output ends up with many
small files or requests, or has to ask every user to call `repartition()` /
`sortWithinPartitions()` before writing.
### Does this PR introduce _any_ user-facing change?
Yes, new optional `DataSourceWriter` methods and value classes in
`pyspark.sql.datasource`. Existing writers behave the same, since every method
defaults to "no requirement" and the feature is off by default.
### How was this patch tested?
New Python tests check how rows reach each write task (clustering, ordering,
null ordering, nested columns, partition count, invalid requirements, the
config being off, and streaming writes). New Scala tests cover the planned
shuffle and sort and the disabled case.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Claude Opus 5.5)
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]