Yicong-Huang commented on code in PR #58729: URL: https://github.com/apache/spark/pull/58729#discussion_r4022916029
########## python/pyspark/sql/eval_handlers/_base.py: ########## @@ -0,0 +1,129 @@ +# +# 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. +# + +"""Base classes and registry for the eval type handlers. + +Kept in a leaf module (importing only ``_typing`` and serializers) so both the +package ``__init__`` and the concrete-handler submodules can import it without a +circular import. +""" + +from abc import ABCMeta, abstractmethod +from collections.abc import Iterator +from typing import TYPE_CHECKING, Any, ClassVar, Generic, Optional, cast + +from pyspark.serializers import Serializer +from pyspark.sql.eval_handlers._typing import ( + CoGroupedBatch, + GroupedBatch, + InputBatch, + OutputBatch, +) +from pyspark.sql.pandas.serializers import ( + ArrowStreamCoGroupSerializer, + ArrowStreamGroupSerializer, + ArrowStreamSerializer, +) + +if TYPE_CHECKING: + import pyarrow as pa # noqa: F401 # only in the batch category's forward-ref subscript + + from pyspark.worker_util import EvalConf, RunnerConf + +# eval type -> handler class, populated by _EvalTypeHandlerMeta at class definition. +EVAL_TYPE_HANDLERS: "dict[int, type[EvalTypeHandler]]" = {} + + +class _EvalTypeHandlerMeta(ABCMeta): + """Registers a concrete handler under its ``eval_type`` at definition time. + + Runs after ``ABCMeta`` sets ``__abstractmethods__``, so a class that declares + an ``eval_type`` while leaving ``run``/``serializer`` abstract is rejected here + instead of failing when ``read_udfs`` instantiates it. + """ + + def __new__(mcs, name: str, bases: tuple, namespace: dict, **kwargs: Any) -> type: + cls = cast("type[EvalTypeHandler]", super().__new__(mcs, name, bases, namespace, **kwargs)) Review Comment: Done, replaced the cast with `assert issubclass(cls, EvalTypeHandler)`. It narrows the type for mypy and doubles as a guard. ########## python/pyspark/sql/eval_handlers/_base.py: ########## @@ -0,0 +1,129 @@ +# +# 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. +# + +"""Base classes and registry for the eval type handlers. + +Kept in a leaf module (importing only ``_typing`` and serializers) so both the +package ``__init__`` and the concrete-handler submodules can import it without a +circular import. +""" + +from abc import ABCMeta, abstractmethod +from collections.abc import Iterator +from typing import TYPE_CHECKING, Any, ClassVar, Generic, Optional, cast + +from pyspark.serializers import Serializer +from pyspark.sql.eval_handlers._typing import ( + CoGroupedBatch, + GroupedBatch, + InputBatch, + OutputBatch, +) +from pyspark.sql.pandas.serializers import ( + ArrowStreamCoGroupSerializer, + ArrowStreamGroupSerializer, + ArrowStreamSerializer, +) + +if TYPE_CHECKING: + import pyarrow as pa # noqa: F401 # only in the batch category's forward-ref subscript + + from pyspark.worker_util import EvalConf, RunnerConf + +# eval type -> handler class, populated by _EvalTypeHandlerMeta at class definition. +EVAL_TYPE_HANDLERS: "dict[int, type[EvalTypeHandler]]" = {} Review Comment: Good point. Renamed it to `_eval_type_handlers` and added `get_eval_type_handler(eval_type)`; `read_udfs` now goes through the function instead of touching the module variable. ########## python/pyspark/sql/eval_handlers/_base.py: ########## @@ -0,0 +1,129 @@ +# +# 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. +# + +"""Base classes and registry for the eval type handlers. + +Kept in a leaf module (importing only ``_typing`` and serializers) so both the +package ``__init__`` and the concrete-handler submodules can import it without a +circular import. +""" + +from abc import ABCMeta, abstractmethod +from collections.abc import Iterator +from typing import TYPE_CHECKING, Any, ClassVar, Generic, Optional, cast + +from pyspark.serializers import Serializer +from pyspark.sql.eval_handlers._typing import ( + CoGroupedBatch, + GroupedBatch, + InputBatch, + OutputBatch, +) +from pyspark.sql.pandas.serializers import ( + ArrowStreamCoGroupSerializer, + ArrowStreamGroupSerializer, + ArrowStreamSerializer, +) + +if TYPE_CHECKING: + import pyarrow as pa # noqa: F401 # only in the batch category's forward-ref subscript + + from pyspark.worker_util import EvalConf, RunnerConf + +# eval type -> handler class, populated by _EvalTypeHandlerMeta at class definition. +EVAL_TYPE_HANDLERS: "dict[int, type[EvalTypeHandler]]" = {} + + +class _EvalTypeHandlerMeta(ABCMeta): Review Comment: I'd like to keep the class-time self-registration. It keeps adding an eval type to a single self-contained subclass, with no separate registry file to update in lockstep, which is the main maintenance win as more handlers land. The registry is now private with a `get_eval_type_handler` accessor (per Tian's comment), so it isn't a public discovery API. Happy to revisit if an explicit table turns out clearer later. ########## python/pyspark/worker.py: ########## @@ -2053,6 +1881,18 @@ def _elementwise_result_to_arrow(result, return_type, arrow_element_type, is_pan def read_udfs(pickleSer, udf_info_list, eval_type, runner_conf, eval_conf): + # Eval types that have been migrated to a handler are dispatched here without + # walking the if/elif chain below. The handler owns the whole lifecycle, + # including serializer selection. + handler_cls = EVAL_TYPE_HANDLERS.get(eval_type) + if handler_cls is not None: + udfs = [ + read_single_udf(pickleSer, udf_info, eval_type, runner_conf, udf_index=udf_index) Review Comment: I like this, but it changes `read_single_udf`, which is shared by every eval type, so I'd rather do it as its own change once more handlers are migrated and the per-handler prepared shapes are clear. Keeping the `list[tuple[...]]` boundary for now; let's discuss it separately. ########## python/pyspark/sql/eval_handlers/_typing.py: ########## @@ -0,0 +1,37 @@ +# +# 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. +# + +"""Type aliases and variables for the eval type handlers. + +The Arrow element types are forward refs so pyarrow stays a type-checking-only +import at runtime, as elsewhere in the worker. +""" + +from collections.abc import Iterator +from typing import TYPE_CHECKING, TypeVar + +if TYPE_CHECKING: + import pyarrow as pa + +# Grouped-category input element, matching what the serializer yields: a group +# serializer yields a lazy iterator; a co-group serializer materializes each side. +GroupedBatch = Iterator["pa.RecordBatch"] Review Comment: Keeping `Iterator` here intentionally. The group serializer yields a lazy iterator, and grouped funcs consume it via `for batch in group` / `list(group)`, so the migrated handlers won't rely on iterator-only ops. The intended contract is a real iterator, so I'd rather keep the annotation as `Iterator` and tighten the pipelined path toward it than widen to `Iterable`. (CoGroupedBatch stays a list pair since its serializer materializes both sides.) -- 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]
