furqan-khan07 opened a new pull request, #60605:
URL: https://github.com/apache/spark/pull/60605

   ### What changes were proposed in this pull request?
   
   An aggregation without grouping keys that uses grouped-agg `pandas_udf` / 
`arrow_udf` now returns one row for an
   empty input.
   
   - `ArrowAggregatePythonExec` skips an empty partition only when there are 
grouping expressions.
   - `GroupedPythonArrowInput` sends a group with no rows as a single Arrow 
batch with no rows. Before, it opened the
     Arrow stream of the group and never ended it. Groups with rows are written 
as before.
   - The Python worker of `SQL_GROUPED_AGG_ARROW_UDF` takes the schema of an 
empty group from that batch.
   - `udaf/udaf-group-by.sql.out` is regenerated: `SELECT udaf(1) FROM testData 
WHERE false` returns `0`, as
     `SELECT COUNT(1) FROM testData WHERE false` does in `group-by.sql.out`.
   
   ### Why are the changes needed?
   
   An aggregation without grouping keys returns exactly one row, also for an 
empty input. Built-in aggregates do,
   and so do the incremental Python aggregators (SPARK-58736). Grouped-agg 
pandas / Arrow UDFs returned no row:
   
       @pandas_udf("long")
       def cnt(v: pd.Series) -> int:
           return int(v.notna().sum())
   
       df = spark.range(3).filter("id > 5")
       df.agg(F.count("id")).show()   # one row: 0
       df.agg(cnt("id")).show()       # no row
   
   `ArrowAggregatePythonExec.doExecute` returned an empty partition as is, 
before it created the single group of an
   aggregation without grouping expressions. The skip was added in SPARK-28128 
and is right for an aggregation with
   grouping keys.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. An aggregation without grouping keys over an empty input returns one 
row where it returned no row, with
   grouped-agg `pandas_udf` and `arrow_udf`, in the DataFrame API and in SQL. 
The UDFs are called with empty input
   (an empty `pd.Series` / `pa.Array` of the input type; one empty batch for 
the iterator variants), so a UDF that
   fails on empty input now fails the query. An aggregation with grouping keys 
is unchanged.
   
   ### How was this patch tested?
   
   New tests, which fail without the change:
   - `test_grouped_without_group_by_clause_empty_input` in 
`test_pandas_udf_grouped_agg.py` and
     `test_arrow_udf_grouped_agg.py` (also run by the Spark Connect parity 
suites)
   - "SPARK-60826: Global aggregation of Pandas UDF returns one row for an 
empty input" in `PythonUDFSuite`
   
   Existing tests:
   - `build/sbt -Phive "sql/testOnly org.apache.spark.sql.execution.python.*"`
   - `build/sbt -Phive "sql/testOnly org.apache.spark.sql.SQLQueryTestSuite -- 
-z udaf"`
   - `python/run-tests --testnames` for the grouped agg, window, grouped map, 
cogrouped map and Python aggregator
     modules under `pyspark.sql.tests.pandas`, `pyspark.sql.tests.arrow` and 
their Connect parity modules
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Co-authored by: Claude (Fable 5.2)
   
   I reviewed the change and ran the tests listed above.


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