forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-40435][SS][PYTHON] Add test suites for applyInPandasWithState …
…in PySpark ### What changes were proposed in this pull request? This PR adds the test suites for apache#37893, applyInPandasWithState. The new test suite mostly ports E2E test cases from existing [flatMapGroupsWithState](https://github.com/apache/spark/blob/master/sql/core/src/test/scala/org/apache/spark/sql/streaming/FlatMapGroupsWithStateSuite.scala). ### Why are the changes needed? Tests are missing in apache#37893 by intention to reduce the size of change, and this PR fills the gap. ### Does this PR introduce _any_ user-facing change? No, test only. ### How was this patch tested? New test suites. Closes apache#37894 from HeartSaVioR/SPARK-40435-on-top-of-SPARK-40434-SPARK-40433-SPARK-40432. Lead-authored-by: Jungtaek Lim <[email protected]> Co-authored-by: Hyukjin Kwon <[email protected]> Signed-off-by: Jungtaek Lim <[email protected]>
- Loading branch information
1 parent
32dd753
commit c22ddbe
Showing
9 changed files
with
1,035 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
python/pyspark/sql/tests/test_pandas_grouped_map_with_state.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You under the Apache License, Version 2.0 | ||
# (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
import unittest | ||
from typing import cast | ||
|
||
from pyspark.sql.streaming.state import GroupStateTimeout, GroupState | ||
from pyspark.sql.types import ( | ||
LongType, | ||
StringType, | ||
StructType, | ||
StructField, | ||
Row, | ||
) | ||
from pyspark.testing.sqlutils import ( | ||
ReusedSQLTestCase, | ||
have_pandas, | ||
have_pyarrow, | ||
pandas_requirement_message, | ||
pyarrow_requirement_message, | ||
) | ||
|
||
if have_pandas: | ||
import pandas as pd | ||
|
||
if have_pyarrow: | ||
import pyarrow as pa # noqa: F401 | ||
|
||
|
||
@unittest.skipIf( | ||
not have_pandas or not have_pyarrow, | ||
cast(str, pandas_requirement_message or pyarrow_requirement_message), | ||
) | ||
class GroupedMapInPandasWithStateTests(ReusedSQLTestCase): | ||
def test_apply_in_pandas_with_state_basic(self): | ||
df = self.spark.readStream.format("text").load("python/test_support/sql/streaming") | ||
|
||
for q in self.spark.streams.active: | ||
q.stop() | ||
self.assertTrue(df.isStreaming) | ||
|
||
output_type = StructType( | ||
[StructField("key", StringType()), StructField("countAsString", StringType())] | ||
) | ||
state_type = StructType([StructField("c", LongType())]) | ||
|
||
def func(key, pdf_iter, state): | ||
assert isinstance(state, GroupState) | ||
|
||
total_len = 0 | ||
for pdf in pdf_iter: | ||
total_len += len(pdf) | ||
|
||
state.update((total_len,)) | ||
assert state.get[0] == 1 | ||
yield pd.DataFrame({"key": [key[0]], "countAsString": [str(total_len)]}) | ||
|
||
def check_results(batch_df, _): | ||
self.assertEqual( | ||
set(batch_df.collect()), | ||
{Row(key="hello", countAsString="1"), Row(key="this", countAsString="1")}, | ||
) | ||
|
||
q = ( | ||
df.groupBy(df["value"]) | ||
.applyInPandasWithState( | ||
func, output_type, state_type, "Update", GroupStateTimeout.NoTimeout | ||
) | ||
.writeStream.queryName("this_query") | ||
.foreachBatch(check_results) | ||
.outputMode("update") | ||
.start() | ||
) | ||
|
||
self.assertEqual(q.name, "this_query") | ||
self.assertTrue(q.isActive) | ||
q.processAllAvailable() | ||
|
||
|
||
if __name__ == "__main__": | ||
from pyspark.sql.tests.test_pandas_grouped_map_with_state import * # noqa: F401 | ||
|
||
try: | ||
import xmlrunner # type: ignore[import] | ||
|
||
testRunner = xmlrunner.XMLTestRunner(output="target/test-reports", verbosity=2) | ||
except ImportError: | ||
testRunner = None | ||
unittest.main(testRunner=testRunner, verbosity=2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.