allisonwang-db commented on code in PR #54085: URL: https://github.com/apache/spark/pull/54085#discussion_r2766305732
########## python/pyspark/sql/streaming/datasource.py: ########## @@ -0,0 +1,225 @@ +# +# 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. +# + +from abc import ABC, abstractmethod + + +class ReadLimit(ABC): + """ + Specifies limits on how much data to read from a streaming source when + determining the latest offset. Review Comment: Can we add more comments and examples on how to use this class? ########## python/pyspark/sql/datasource.py: ########## @@ -714,9 +715,37 @@ def initialOffset(self) -> dict: messageParameters={"feature": "initialOffset"}, ) - def latestOffset(self) -> dict: + def latestOffset(self, start: dict, limit: ReadLimit) -> dict: """ - Returns the most recent offset available. + Returns the most recent offset available given a read limit. The start offset can be used + to figure out how much new data should be read given the limit. + + The `start` will be provided from the return value of :meth:`initialOffset()` for + the very first micro-batch, and for subsequent micro-batches, the start offset is the + ending offset from the previous micro-batch. The source can return the `start` parameter + as it is, if there is no data to process. + + :class:`ReadLimit` can be used by the source to limit the amount of data returned in this + call. The implementation should implement :meth:`getDefaultReadLimit()` to provide the + proper :class:`ReadLimit` if the source can limit the amount of data returned based on the + source options. + + The engine can still call :meth:`latestOffset()` with :class:`ReadAllAvailable` even if the + source produces the different read limit from :meth:`getDefaultReadLimit()`, to respect the + semantic of trigger. The source must always respect the given readLimit provided by the + engine; e.g. if the readLimit is :class:`ReadAllAvailable`, the source must ignore the read + limit configured through options. + + NOTE: Previous Spark versions didn't have start offset and read limit parameters for this + method. While Spark will ensure the backward compatibility for existing data sources, the + new data sources are strongly encouraged to implement this new method signature. + + Parameters + ---------- + start : dict + The start offset of the microbatch to continue reading from. + limit : :class:`ReadLimit` + The limit on the amount of data to be returned by this call. Review Comment: Let's add supported version here ########## python/pyspark/sql/datasource_internal.py: ########## @@ -79,28 +84,26 @@ class _SimpleStreamReaderWrapper(DataSourceStreamReader): def __init__(self, simple_reader: SimpleDataSourceStreamReader): self.simple_reader = simple_reader - self.initial_offset: Optional[dict] = None - self.current_offset: Optional[dict] = None self.cache: List[PrefetchedCacheEntry] = [] def initialOffset(self) -> dict: - if self.initial_offset is None: - self.initial_offset = self.simple_reader.initialOffset() - return self.initial_offset - - def latestOffset(self) -> dict: - # when query start for the first time, use initial offset as the start offset. - if self.current_offset is None: - self.current_offset = self.initialOffset() - (iter, end) = self.simple_reader.read(self.current_offset) - self.cache.append(PrefetchedCacheEntry(self.current_offset, end, iter)) - self.current_offset = end + return self.simple_reader.initialOffset() + + def getDefaultReadLimit(self) -> ReadLimit: + # We do not consider providing different read limit on simple stream reader. + return ReadAllAvailable() + + def latestOffset(self, start: dict, limit: ReadLimit) -> dict: + assert start is not None, "start offset should not be None" + assert isinstance( + limit, ReadAllAvailable + ), "simple stream reader does not support read limit" Review Comment: Does that mean we can't use availableNow with simple streaming reader? ########## python/pyspark/sql/streaming/datasource.py: ########## @@ -0,0 +1,225 @@ +# +# 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. +# + +from abc import ABC, abstractmethod + + +class ReadLimit(ABC): + """ + Specifies limits on how much data to read from a streaming source when + determining the latest offset. + """ + + @classmethod + @abstractmethod + def type_name(cls) -> str: + """ + The name of this :class:`ReadLimit` type. This is used to register the type into registry. + + Returns + ------- + str + The name of this :class:`ReadLimit` type. + """ + pass + + @classmethod + @abstractmethod + def load(cls, params: dict) -> "ReadLimit": + """ + Create an instance of :class:`ReadLimit` from parameters. + + Parameter + --------- + params : dict + The parameters to create the :class:`ReadLimit`. type name isn't included. Review Comment: ditto can we add more examples? ########## python/pyspark/sql/datasource.py: ########## @@ -714,9 +715,37 @@ def initialOffset(self) -> dict: messageParameters={"feature": "initialOffset"}, ) - def latestOffset(self) -> dict: + def latestOffset(self, start: dict, limit: ReadLimit) -> dict: """ - Returns the most recent offset available. + Returns the most recent offset available given a read limit. The start offset can be used + to figure out how much new data should be read given the limit. + + The `start` will be provided from the return value of :meth:`initialOffset()` for + the very first micro-batch, and for subsequent micro-batches, the start offset is the + ending offset from the previous micro-batch. The source can return the `start` parameter + as it is, if there is no data to process. + + :class:`ReadLimit` can be used by the source to limit the amount of data returned in this + call. The implementation should implement :meth:`getDefaultReadLimit()` to provide the + proper :class:`ReadLimit` if the source can limit the amount of data returned based on the + source options. + + The engine can still call :meth:`latestOffset()` with :class:`ReadAllAvailable` even if the + source produces the different read limit from :meth:`getDefaultReadLimit()`, to respect the + semantic of trigger. The source must always respect the given readLimit provided by the + engine; e.g. if the readLimit is :class:`ReadAllAvailable`, the source must ignore the read + limit configured through options. + + NOTE: Previous Spark versions didn't have start offset and read limit parameters for this + method. While Spark will ensure the backward compatibility for existing data sources, the + new data sources are strongly encouraged to implement this new method signature. Review Comment: Let's add this NOTE below as a docstring section? -- 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]
