HyukjinKwon commented on code in PR #40586: URL: https://github.com/apache/spark/pull/40586#discussion_r1157050221
########## python/pyspark/sql/connect/streaming/query.py: ########## @@ -0,0 +1,181 @@ +# +# 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 json +import sys +from typing import TYPE_CHECKING, Any, cast, Dict, List, Optional + +from pyspark.errors import StreamingQueryException +import pyspark.sql.connect.proto as pb2 +from pyspark.sql.streaming.query import ( + StreamingQuery as PySparkStreamingQuery, +) + +__all__ = [ + "StreamingQuery", # TODO(WIP): "StreamingQueryManager" +] + +if TYPE_CHECKING: + from pyspark.sql.connect.session import SparkSession + + +class StreamingQuery: + def __init__( + self, session: "SparkSession", queryId: str, runId: str, name: Optional[str] = None + ) -> None: + self._session = session + self._query_id = queryId + self._run_id = runId + self._name = name + + @property + def id(self) -> str: + return self._query_id + + id.__doc__ = PySparkStreamingQuery.id.__doc__ + + @property + def runId(self) -> str: + return self._run_id + + runId.__doc__ = PySparkStreamingQuery.runId.__doc__ + + @property + def name(self) -> str: + return self._name + + name.__doc__ = PySparkStreamingQuery.name.__doc__ + + @property + def isActive(self) -> bool: + return self._fetch_status().is_active + + isActive.__doc__ = PySparkStreamingQuery.isActive.__doc__ + + def awaitTermination(self, timeout: Optional[int] = None) -> Optional[bool]: + raise NotImplementedError() + + awaitTermination.__doc__ = PySparkStreamingQuery.awaitTermination.__doc__ + + @property + def status(self) -> Dict[str, Any]: + proto = self._fetch_status() + return { + "message": proto.status_message, + "isDataAvailable": proto.is_data_available, + "isTriggerActive": proto.is_trigger_active, + } + + status.__doc__ = PySparkStreamingQuery.status.__doc__ + + @property + def recentProgress(self) -> List[Dict[str, Any]]: + progress = list(self._fetch_status(recent_progress_limit=10000).recent_progress_json) + # Server only keeps 100, so 10000 limit is high enough. + return [json.loads(p) for p in progress] + + recentProgress.__doc__ = PySparkStreamingQuery.recentProgress.__doc__ + + @property + def lastProgress(self) -> Optional[Dict[str, Any]]: + progress = list(self._fetch_status(recent_progress_limit=1).recent_progress_json) + if len(progress) > 0: + return json.loads(progress[-1]) + else: + return None + + lastProgress.__doc__ = PySparkStreamingQuery.lastProgress.__doc__ + + def processAllAvailable(self) -> None: + cmd = pb2.StreamingQueryCommand() + cmd.process_all_available = True + self._execute_streaming_query_cmd(cmd) + + processAllAvailable.__doc__ = PySparkStreamingQuery.processAllAvailable.__doc__ + + def stop(self) -> None: + cmd = pb2.StreamingQueryCommand() + cmd.stop = True + self._execute_streaming_query_cmd(cmd) + + stop.__doc__ = PySparkStreamingQuery.stop.__doc__ + + def explain(self, extended: bool = False) -> None: + cmd = pb2.StreamingQueryCommand() + cmd.explain.extended = extended + result = self._execute_streaming_query_cmd(cmd).explain.result + print(result) + + explain.__doc__ = PySparkStreamingQuery.explain.__doc__ + + def exception(self) -> Optional[StreamingQueryException]: + raise NotImplementedError() + + exception.__doc__ = PySparkStreamingQuery.exception.__doc__ + + def _fetch_status( + self, recent_progress_limit=0 + ) -> pb2.StreamingQueryCommandResult.StatusResult: + cmd = pb2.StreamingQueryCommand() + cmd.status.recent_progress_limit = recent_progress_limit + + return self._execute_streaming_query_cmd(cmd).status + + def _execute_streaming_query_cmd( + self, cmd: pb2.StreamingQueryCommand + ) -> pb2.StreamingQueryCommandResult: + cmd.query_id = self._query_id + cmd.run_id = self._run_id + exec_cmd = pb2.Command() + exec_cmd.streaming_query_command.CopyFrom(cmd) + (_, properties) = self._session.client.execute_command(exec_cmd) + return cast(pb2.StreamingQueryCommandResult, properties["streaming_query_command_result"]) + + +# TODO(WIP) class StreamingQueryManager: + + +def _test() -> None: + import doctest + import os + from pyspark.sql import SparkSession + import pyspark.sql.streaming.query + from py4j.protocol import Py4JError + + os.chdir(os.environ["SPARK_HOME"]) + + globs = pyspark.sql.streaming.query.__dict__.copy() + try: + spark = SparkSession._getActiveSessionOrCreate() Review Comment: BTW, you won't have to suffer from `_getActiveSessionOrCreate` here. I used that in listeners tests because it requires to use the same session (that registered the listener) but in this case I believe it's fine. -- 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]
