-
Notifications
You must be signed in to change notification settings - Fork 28.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-48755][SS][PYTHON] transformWithState pyspark base implementat…
…ion and ValueState support ### What changes were proposed in this pull request? - Base implementation for Python State V2 - Implemented ValueState Below we specifically highlight some key files/components for this change: - Python - `group_ops.py`: defines transformWithStateInPandas function and its udf. - `serializer.py`: defines how we load and dump arrow streams for data rows between the JVM and Python process. - `stateful_processor.py`: defines StatefulProcessorHandle, ValueState functionalities and StatefulProcessor interface. - `state_api_client.py` and `value_state_client.py`: contains logics to send API request in protobuf format to the server (JVM) - Scala - `TransformWithStateInPandasExec`: physical operator for `TransformWithStateInPandas`. - `TransformWithStateInPandasPythonRunner`: python runner that launches python worker that executes the udf. - `TransformWithStateInPandasStateServer`: class that handles state requests in protobuf format from python side. ### Why are the changes needed? Support Python State V2 API ### Does this PR introduce _any_ user-facing change? Yes ### How was this patch tested? Did local integration test with below command ``` import pandas as pd from pyspark.sql import Row from pyspark.sql.streaming import StatefulProcessor, StatefulProcessorHandle from pyspark.sql.types import StructType, StructField, LongType, StringType from typing import Iterator spark.conf.set("spark.sql.streaming.stateStore.providerClass","org.apache.spark.sql.execution.streaming.state.RocksDBStateStoreProvider") spark.conf.set("spark.sql.shuffle.partitions","1") output_schema = StructType([ StructField("value", LongType(), True) ]) state_schema = StructType([ StructField("id", LongType(), True), StructField("value", StringType(), True), StructField("comment", StringType(), True) ]) class SimpleStatefulProcessor(StatefulProcessor): def init(self, handle: StatefulProcessorHandle) -> None: self.value_state = handle.getValueState("testValueState", state_schema) def handleInputRows(self, key, rows) -> Iterator[pd.DataFrame]: self.value_state.update((1,"test_value","comment")) exists = self.value_state.exists() print(f"value state exists: {exists}") value = self.value_state.get() print(f"get value: {value}") print("clearing value state") self.value_state.clear() print("value state cleared") return rows def close(self) -> None: pass q = spark.readStream.format("rate").option("rowsPerSecond", "1").option("numPartitions", "1").load().groupBy("value").transformWithStateInPandas(stateful_processor = SimpleStatefulProcessor(), outputStructType=output_schema, outputMode="Update", timeMode="None").writeStream.format("console").option("checkpointLocation", "/tmp/streaming/temp_ckp").outputMode("update").start() ``` Verified from the logs that value state methods work as expected for key `11` ``` value state exists: True get value: id value comment 0 1 test_value comment clearing value state value state cleared ``` Will add unit test ### Was this patch authored or co-authored using generative AI tooling? No Closes #47133 from bogao007/state-v2-initial. Lead-authored-by: bogao007 <[email protected]> Co-authored-by: Bhuwan Sahni <[email protected]> Signed-off-by: Jungtaek Lim <[email protected]>
- Loading branch information
1 parent
b9fbdf0
commit def42d4
Showing
31 changed files
with
12,153 additions
and
5 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,3 +44,4 @@ This page gives an overview of all public Spark SQL API. | |
variant_val | ||
protobuf | ||
datasource | ||
stateful_processor |
29 changes: 29 additions & 0 deletions
29
python/docs/source/reference/pyspark.sql/stateful_processor.rst
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,29 @@ | ||
.. 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. | ||
================== | ||
Stateful Processor | ||
================== | ||
.. currentmodule:: pyspark.sql.streaming | ||
|
||
.. autosummary:: | ||
:toctree: api/ | ||
|
||
StatefulProcessor.init | ||
StatefulProcessor.handleInputRows | ||
StatefulProcessor.close |
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.