vinodkc commented on code in PR #58195:
URL: https://github.com/apache/spark/pull/58195#discussion_r4634919733
##########
python/pyspark/sql/tests/pandas/test_pandas_udf_grouped_agg.py:
##########
@@ -1148,6 +1148,72 @@ def avg_age_iter(it: Iterator[pd.Series]) -> float:
expected = [Row(id=1, avg_age=27.5), Row(id=2, avg_age=37.5)]
self.assertEqual(actual, expected)
+ def test_collation_session_window_grouped_agg(self):
+ # SPARK-58928: for a session window, ArrowAggregatePythonExec runs
+ # UpdatingSessionsIterator before the grouped iterator and the UDF
input
+ # projection, and that iterator must rewrite only the session column
of each
+ # buffered row. If it also rewrote the grouping columns to the
session's first
+ # key, a pandas grouped-aggregate UDF whose argument reads the
collated grouping
+ # column would see only the first spelling of the group.
+ from datetime import datetime
+
+ df = self.spark.createDataFrame(
+ [
+ ("foo", datetime(2026, 1, 1, 10, 0, 0), 1.0),
+ ("FOO", datetime(2026, 1, 1, 10, 5, 0), 2.0),
+ ("foo", datetime(2026, 1, 1, 10, 8, 0), 3.0),
+ ],
+ "s STRING COLLATE UTF8_LCASE, ts TIMESTAMP, v DOUBLE",
+ )
+
+ @pandas_udf("string")
+ def spellings(s: pd.Series) -> str:
+ return ",".join(sorted(s))
+
+ result = (
+ df.groupby("s", sf.session_window("ts", "10 minutes"))
+ .agg(spellings(df["s"]).alias("sp"))
+ .select("sp")
+ )
+
+ # The three rows merge into one session under the collation, and the
UDF receives
+ # each row's own spelling of the grouping column.
+ assertDataFrameEqual(result, [Row(sp="FOO,foo,foo")])
+
+ def test_collation_session_window_grouped_agg_subquery(self):
+ # SPARK-58928: through a subquery, the session attribute in the
grouping list
+ # carries the subquery's qualifier while the same column of the input
schema
+ # does not. UpdatingSessionsIterator must find the session column by
expression
+ # ID when it binds the merged session into each buffered row; matching
by
+ # attribute equality leaves every row with its own per-event session.
+ from datetime import datetime
+
+ df = self.spark.createDataFrame(
+ [
+ ("foo", datetime(2026, 1, 1, 10, 0, 0), 1.0),
+ ("FOO", datetime(2026, 1, 1, 10, 5, 0), 2.0),
+ ("foo", datetime(2026, 1, 1, 10, 8, 0), 3.0),
+ ],
+ "s STRING COLLATE UTF8_LCASE, ts TIMESTAMP, v DOUBLE",
+ )
+ df.createOrReplaceTempView("t")
Review Comment:
Done (37d11888086). The subquery test now registers the view and the
function, runs the query and asserts inside with self.temp_view("t"),
self.temp_func("spellings"):, so both are dropped even when the assertion fails
and the later tests of the mixin and its Connect parity class no longer depend
on test order.
--
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]