HeartSaVioR commented on code in PR #54085: URL: https://github.com/apache/spark/pull/54085#discussion_r2767040089
########## 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: The documentation should be covered in `latestOffset()` and `getDefaultReadLimit()` in DataSourceStreamReader. Since devs have to use the built-in read limit implementations at this point, I'm going to enumerate the built-in implementations here so that they can understand which classes are available. ########## 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: You meant moving out of doc comment? I'm OK with it. ########## 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: Class methods in ReadLimit aren't something user facing ones. They are Spark internal ones. Do we still need examples? -- 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]
