HyukjinKwon opened a new pull request, #60542:
URL: https://github.com/apache/spark/pull/60542
### What changes were proposed in this pull request?
This is a follow-up of #57952 (SPARK-58736). An aggregation that uses only
incremental Python aggregators (`udaf(Aggregator)`) and is grouped by a session
window now fails planning with the new error
`UNSUPPORTED_FEATURE.PYTHON_AGGREGATOR_WITH_SESSION_WINDOW`, instead of
returning unmerged sessions.
### Why are the changes needed?
A batch session window groups by one provisional session per input row,
`[ts, ts + gap)`, and relies on the aggregate operator to merge the overlapping
ones: `AggUtils` adds `UpdatingSessionsExec` and `MergingSessionsExec`, and
`ArrowAggregatePythonExec` uses `UpdatingSessionsIterator`.
`PythonIncrementalAggregateExec` groups by the exact session value in both its
PARTIAL and FINAL stages and never merges sessions, so the query silently
returned one row per provisional session:
```python
import datetime
from pyspark.sql import functions as sf
from pyspark.sql.functions import udaf
# `Mean` is the Aggregator example in pyspark.sql.aggregator.
df = spark.createDataFrame(
[(datetime.datetime(2026, 1, 1, 10, 0), 1.0), (datetime.datetime(2026,
1, 1, 10, 2), 3.0)],
"ts timestamp, v double")
w = sf.session_window("ts", "5 minutes")
# Timestamps abbreviated below.
df.groupBy(w).agg(sf.avg("v")).collect()
# [Row(session_window=Row(start=10:00, end=10:07), avg(v)=2.0)]
df.groupBy(w).agg(udaf(Mean())(df.v)).collect()
# Before: [Row(session_window=Row(start=10:00, end=10:05), ...=1.0),
# Row(session_window=Row(start=10:02, end=10:07), ...=3.0)]
# After: AnalysisException:
[UNSUPPORTED_FEATURE.PYTHON_AGGREGATOR_WITH_SESSION_WINDOW] ...
```
The same happens with SQL `GROUP BY session_window(...)` and a registered
aggregator, in classic and Connect. Streaming aggregations of incremental
Python aggregators are already rejected. Tumbling and sliding windows need no
merging and keep working.
Merging sessions in the incremental operators can be a separate improvement;
this follow-up only stops the wrong results.
### Does this PR introduce _any_ user-facing change?
Yes, in unreleased code only (SPARK-58736 is not released yet). Grouping
incremental Python aggregators by a session window now fails with
`UNSUPPORTED_FEATURE.PYTHON_AGGREGATOR_WITH_SESSION_WINDOW` instead of
returning unmerged sessions.
### How was this patch tested?
Added `test_session_window_rejected`, run in classic and Connect modes. It
checks the DataFrame and SQL forms of the rejected query, and that grouping by
a tumbling window still matches `avg`.
Ran `pyspark.sql.tests.arrow.test_arrow_python_aggregator`,
`pyspark.sql.tests.connect.arrow.test_parity_arrow_python_aggregator` and
`SparkThrowableSuite` locally.
### 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]