dongjoon-hyun opened a new pull request, #545:
URL: https://github.com/apache/spark-connect-swift/pull/545
### What changes were proposed in this pull request?
This PR aims to support `groupingSets` in `DataFrame`.
```swift
public func groupingSets(_ groupingSets: [[String]], _ cols: String...) ->
GroupedData
```
`GroupedData` is extended with an optional `groupingSets` stored property,
which
`buildAggregate` translates into the `Aggregate.grouping_sets` field,
mirroring how
the existing `pivot` property is handled. The `init` gains a defaulted
parameter, so
the existing `groupBy`/`rollup`/`cube`/`pivot` call sites are unchanged.
The required protobuf definitions (`GROUP_TYPE_GROUPING_SETS` and
`Aggregate.GroupingSets`) already exist, so no code generation is involved.
Example:
```swift
let df = try await spark.sql("SELECT * FROM dealer")
try await df.groupingSets([["city", "car_model"], ["city"], []], "city",
"car_model")
.agg("sum(quantity) sum").orderBy("city", "car_model").show()
```
### Why are the changes needed?
To provide a `DataFrame`-level API for multi-dimensional aggregation over an
explicit
list of group combinations, closing a gap with the other Spark clients.
Unlike `rollup` and `cube`, which derive the combinations automatically,
`groupingSets`
lets users specify arbitrary combinations, including the empty set that
aggregates over
all rows. `Dataset.groupingSets` was added in Apache Spark 4.0.0 via
SPARK-45929, and
PySpark exposes `DataFrame.groupingSets` since 4.0.0.
### Does this PR introduce _any_ user-facing change?
Yes, this adds a new public API, `DataFrame.groupingSets`. There is no
behavior change
for the existing `groupBy`, `rollup`, `cube`, and `pivot` APIs.
### How was this patch tested?
Pass the CIs with newly added test cases.
- `DataFrameTests.groupingSets` verifies the aggregation over
`[["city", "car_model"], ["city"], []]`.
- `DataFrameTests.groupingSetsSameAsSQL` compares the result with the
equivalent
`GROUP BY ... GROUPING SETS (...)` SQL query.
- `DataFrameTests.groupingSetsWithGroupingID` verifies `grouping_id()`
identifies which
grouping set each row came from.
- `DataFrameInternalTests.groupingSetsPlan` verifies the generated plan sets
`GROUP_TYPE_GROUPING_SETS` and populates `grouping_sets` and
`grouping_expressions`.
Manually tested against Apache Spark 4.0.4 and 4.2.0 Connect servers.
```
$ swift test --no-parallel -c release --filter
"DataFrameTests|DataFrameInternalTests"
Test run with 129 tests in 3 suites passed after 13.002 seconds.
```
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Opus 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]