HyukjinKwon commented on code in PR #60542:
URL: https://github.com/apache/spark/pull/60542#discussion_r4581893048


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/SparkStrategies.scala:
##########
@@ -751,6 +751,14 @@ abstract class SparkStrategies extends 
QueryPlanner[SparkPlan] {
 
       case PhysicalAggregation(groupingExpressions, aggExpressions, 
resultExpressions, child)
           if 
aggExpressions.forall(_.aggregateFunction.isInstanceOf[PythonAggregate]) =>
+        // A batch session window groups by one provisional session per input 
row and relies on
+        // the aggregate operator to merge the overlapping ones (see 
`UpdatingSessionsExec`). The
+        // incremental operators group by the exact session value and never 
merge sessions, so
+        // they would silently return the unmerged sessions; reject the query 
instead.
+        if 
(groupingExpressions.exists(_.metadata.contains(SessionWindow.marker))) {

Review Comment:
   Thanks, done in 1f3526b. When a grouping key is a session window, 
`PythonIncrementalAggregateExec.plan` now puts `UpdatingSessionsExec` 
(`AggUtils.mayAppendUpdatingSessionExec`) below the PARTIAL stage, so both 
stages group by the merged session. The new error condition is removed. With 
another grouping key, the FINAL stage reuses the `hashpartitioning(k)` below 
`UpdatingSessionsExec`, so no extra shuffle is added.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/SparkStrategies.scala:
##########
@@ -751,6 +751,14 @@ abstract class SparkStrategies extends 
QueryPlanner[SparkPlan] {
 
       case PhysicalAggregation(groupingExpressions, aggExpressions, 
resultExpressions, child)
           if 
aggExpressions.forall(_.aggregateFunction.isInstanceOf[PythonAggregate]) =>
+        // A batch session window groups by one provisional session per input 
row and relies on
+        // the aggregate operator to merge the overlapping ones (see 
`UpdatingSessionsExec`). The
+        // incremental operators group by the exact session value and never 
merge sessions, so
+        // they would silently return the unmerged sessions; reject the query 
instead.
+        if 
(groupingExpressions.exists(_.metadata.contains(SessionWindow.marker))) {
+          throw QueryCompilationErrors.pythonAggregateWithSessionWindowError(

Review Comment:
   This is moot now: 1f3526b merges the sessions instead of rejecting the 
query, so there is no new error to raise earlier.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/SparkStrategies.scala:
##########
@@ -751,6 +751,14 @@ abstract class SparkStrategies extends 
QueryPlanner[SparkPlan] {
 
       case PhysicalAggregation(groupingExpressions, aggExpressions, 
resultExpressions, child)
           if 
aggExpressions.forall(_.aggregateFunction.isInstanceOf[PythonAggregate]) =>

Review Comment:
   Moot as well with 1f3526b, since there is no session window error anymore. 
The mixed query, `SELECT session_mean(v), count(*) FROM session_input GROUP BY 
session_window(ts, '5 minutes')`, still fails with 
`INVALID_PYTHON_UDF_PLACEMENT`, and without `count(*)` it now returns the 
merged sessions.



##########
python/pyspark/sql/tests/arrow/test_arrow_python_aggregator.py:
##########
@@ -347,6 +348,44 @@ def test_pivot_rejected(self):
         with self.assertRaises(AnalysisException):
             df.groupBy("k").pivot("p").agg(udaf(Mean())(sf.col("v"))).collect()
 
+    def test_session_window_rejected(self):
+        # A batch session window groups by one provisional session per row and 
relies on the
+        # aggregate operator to merge the overlapping ones. The incremental 
operators do not
+        # merge sessions, so grouping by a session window is rejected rather 
than returning one
+        # row per unmerged session. A tumbling window needs no merging and 
still works.
+        df = self.spark.createDataFrame(
+            [
+                (datetime.datetime(2026, 1, 1, 10, 0), 1.0),
+                (datetime.datetime(2026, 1, 1, 10, 2), 3.0),
+            ],
+            "ts timestamp, v double",
+        )
+        mean = udaf(Mean())
+        df.createOrReplaceTempView("session_input")
+        self.spark.udf.register("session_mean", mean)
+        queries = {
+            "dataframe": lambda: df.groupBy(sf.session_window("ts", "5 
minutes")).agg(
+                mean(sf.col("v"))
+            ),
+            "sql": lambda: self.spark.sql(
+                "SELECT session_mean(v) FROM session_input GROUP BY 
session_window(ts, '5 minutes')"
+            ),
+        }
+        for name, query in queries.items():
+            with self.subTest(query=name):
+                with self.assertRaises(AnalysisException) as ctx:
+                    query().collect()
+                self.assertEqual(
+                    ctx.exception.getCondition(),
+                    
"UNSUPPORTED_FEATURE.PYTHON_AGGREGATOR_WITH_SESSION_WINDOW",
+                )
+
+        result = (
+            df.groupBy(sf.window("ts", "5 
minutes")).agg(mean(sf.col("v")).alias("m")).collect()

Review Comment:
   Done in 1f3526b. The data now has rows in several windows and sessions, and 
both sides are sorted before comparing. `test_time_window` covers a tumbling 
window and a sliding window, `sf.window("ts", "5 minutes", "1 minute")`.



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