-
Notifications
You must be signed in to change notification settings - Fork 133
Preserve PyArrow extension metadata when chaining Python scalar UDFs #1287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kosiew
wants to merge
15
commits into
apache:main
Choose a base branch
from
kosiew:udf-1172
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d0b02a6
Implement metadata-aware PySimpleScalarUDF
kosiew 2325993
Clone pyarrow.Field objects for FFI handoff
kosiew e0bce84
Updated the _function and _decorator helpers in ScalarUDF.udf to use …
kosiew 5fb08d6
Updated the internal ScalarUDF.udf helper signatures so IDEs and type…
kosiew 6a89977
Refactor imports in udf.rs for improved organization and clarity
kosiew 5aacb41
Add shared PyArrowArray alias and refine ScalarUDFs
kosiew bae5d54
Add feature flag for pyarrow UUID helper detection
kosiew 153b5f1
Add assertions for UUID extension type in tests
kosiew 1baa2b7
Teach _wrap_extension_value to handle empty arrays
kosiew 308e774
Refactor type alias for PyArrowArray to use Union for better clarity
kosiew 16224e2
Fix Ruff errors
kosiew 7b9ced0
Enhance documentation for PyArrowArray type alias to clarify usage an…
kosiew 0f28465
Add dev dependency pyarrow >= 19 and remove UUID extension availabili…
kosiew f068caa
Update return_type to raise error for metadata source
kosiew 1ea75fd
Fix ruff errors
kosiew File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -22,15 +22,39 @@ | |
| import functools | ||
| from abc import ABCMeta, abstractmethod | ||
| from enum import Enum | ||
| from typing import TYPE_CHECKING, Any, Callable, Optional, Protocol, TypeVar, overload | ||
| from typing import ( | ||
| Any, | ||
| Callable, | ||
| Optional, | ||
| Protocol, | ||
| Sequence, | ||
| TypeVar, | ||
| Union, | ||
| cast, | ||
| overload, | ||
| ) | ||
|
|
||
| import pyarrow as pa | ||
|
|
||
| import datafusion._internal as df_internal | ||
| from datafusion.expr import Expr | ||
|
|
||
| if TYPE_CHECKING: | ||
| _R = TypeVar("_R", bound=pa.DataType) | ||
| PyArrowArray = Union[pa.Array, pa.ChunkedArray] | ||
| # Type alias for array batches exchanged with Python scalar UDFs. | ||
| # | ||
| # We need two related but different annotations here: | ||
| # - `PyArrowArray` is the concrete union type (pa.Array | pa.ChunkedArray) | ||
| # that is convenient for user-facing callables and casts. Use this when | ||
| # annotating or checking values that may be either an Array or | ||
| # a ChunkedArray. | ||
| # - `PyArrowArrayT` is a constrained `TypeVar` over the two concrete | ||
| # array flavors. Keeping a generic TypeVar allows helpers like | ||
| # `_wrap_extension_value` and `_wrap_udf_function` to remain generic | ||
| # and preserve the specific array "flavor" (Array vs ChunkedArray) | ||
| # flowing through them, rather than collapsing everything to the | ||
| # wide union. This improves type-checking and keeps return types | ||
| # precise in the wrapper logic. | ||
| PyArrowArrayT = TypeVar("PyArrowArrayT", pa.Array, pa.ChunkedArray) | ||
|
|
||
|
|
||
| class Volatility(Enum): | ||
|
|
@@ -77,6 +101,87 @@ def __str__(self) -> str: | |
| return self.name.lower() | ||
|
|
||
|
|
||
| def _clone_field(field: pa.Field) -> pa.Field: | ||
| """Return a deep copy of ``field`` including its DataType.""" | ||
| return pa.schema([field]).field(0) | ||
|
|
||
|
|
||
| def _normalize_field(value: pa.DataType | pa.Field, *, default_name: str) -> pa.Field: | ||
| if isinstance(value, pa.Field): | ||
| return _clone_field(value) | ||
| if isinstance(value, pa.DataType): | ||
| return _clone_field(pa.field(default_name, value)) | ||
| msg = "Expected a pyarrow.DataType or pyarrow.Field" | ||
| raise TypeError(msg) | ||
|
|
||
|
|
||
| def _normalize_input_fields( | ||
| values: pa.DataType | pa.Field | Sequence[pa.DataType | pa.Field], | ||
| ) -> list[pa.Field]: | ||
| if isinstance(values, (pa.DataType, pa.Field)): | ||
| sequence: Sequence[pa.DataType | pa.Field] = [values] | ||
| elif isinstance(values, Sequence) and not isinstance(values, (str, bytes)): | ||
| sequence = values | ||
| else: | ||
| msg = "input_types must be a DataType, Field, or a sequence of them" | ||
| raise TypeError(msg) | ||
|
|
||
| return [ | ||
| _normalize_field(value, default_name=f"arg_{idx}") | ||
| for idx, value in enumerate(sequence) | ||
| ] | ||
|
|
||
|
|
||
| def _normalize_return_field( | ||
| value: pa.DataType | pa.Field, | ||
| *, | ||
| name: str, | ||
| ) -> pa.Field: | ||
| default_name = f"{name}_result" if name else "result" | ||
| return _normalize_field(value, default_name=default_name) | ||
|
|
||
|
|
||
| def _wrap_extension_value( | ||
| value: PyArrowArrayT, data_type: pa.DataType | ||
| ) -> PyArrowArrayT: | ||
| storage_type = getattr(data_type, "storage_type", None) | ||
| wrap_array = getattr(data_type, "wrap_array", None) | ||
| if storage_type is None or wrap_array is None: | ||
| return value | ||
| if isinstance(value, pa.Array) and value.type.equals(storage_type): | ||
| return wrap_array(value) | ||
| if isinstance(value, pa.ChunkedArray) and value.type.equals(storage_type): | ||
| wrapped_chunks = [wrap_array(chunk) for chunk in value.chunks] | ||
| if not wrapped_chunks: | ||
| empty_storage = pa.array([], type=storage_type) | ||
| return wrap_array(empty_storage) | ||
| return pa.chunked_array(wrapped_chunks, type=data_type) | ||
| return value | ||
|
|
||
|
|
||
| def _wrap_udf_function( | ||
| func: Callable[..., PyArrowArrayT], | ||
| input_fields: Sequence[pa.Field], | ||
| return_field: pa.Field, | ||
| ) -> Callable[..., PyArrowArrayT]: | ||
| def wrapper(*args: Any, **kwargs: Any) -> PyArrowArrayT: | ||
| if args: | ||
| converted_args: list[Any] = list(args) | ||
| for idx, field in enumerate(input_fields): | ||
| if idx >= len(converted_args): | ||
| break | ||
| converted_args[idx] = _wrap_extension_value( | ||
| cast(PyArrowArray, converted_args[idx]), | ||
| field.type, | ||
| ) | ||
| else: | ||
| converted_args = [] | ||
| result = func(*converted_args, **kwargs) | ||
| return _wrap_extension_value(result, return_field.type) | ||
|
|
||
| return wrapper | ||
|
|
||
|
|
||
| class ScalarUDFExportable(Protocol): | ||
| """Type hint for object that has __datafusion_scalar_udf__ PyCapsule.""" | ||
|
|
||
|
|
@@ -93,9 +198,9 @@ class ScalarUDF: | |
| def __init__( | ||
| self, | ||
| name: str, | ||
| func: Callable[..., _R], | ||
| input_types: pa.DataType | list[pa.DataType], | ||
| return_type: _R, | ||
| func: Callable[..., PyArrowArray] | ScalarUDFExportable, | ||
| input_types: pa.DataType | pa.Field | Sequence[pa.DataType | pa.Field], | ||
| return_type: pa.DataType | pa.Field, | ||
| volatility: Volatility | str, | ||
| ) -> None: | ||
| """Instantiate a scalar user-defined function (UDF). | ||
|
|
@@ -105,10 +210,11 @@ def __init__( | |
| if hasattr(func, "__datafusion_scalar_udf__"): | ||
| self._udf = df_internal.ScalarUDF.from_pycapsule(func) | ||
| return | ||
| if isinstance(input_types, pa.DataType): | ||
| input_types = [input_types] | ||
| normalized_inputs = _normalize_input_fields(input_types) | ||
| normalized_return = _normalize_return_field(return_type, name=name) | ||
| wrapped_func = _wrap_udf_function(func, normalized_inputs, normalized_return) | ||
| self._udf = df_internal.ScalarUDF( | ||
| name, func, input_types, return_type, str(volatility) | ||
| name, wrapped_func, normalized_inputs, normalized_return, str(volatility) | ||
| ) | ||
|
|
||
| def __repr__(self) -> str: | ||
|
|
@@ -127,18 +233,18 @@ def __call__(self, *args: Expr) -> Expr: | |
| @overload | ||
| @staticmethod | ||
| def udf( | ||
| input_types: list[pa.DataType], | ||
| return_type: _R, | ||
| input_types: list[pa.DataType | pa.Field], | ||
| return_type: pa.DataType | pa.Field, | ||
| volatility: Volatility | str, | ||
| name: Optional[str] = None, | ||
| ) -> Callable[..., ScalarUDF]: ... | ||
| ) -> Callable[[Callable[..., PyArrowArray]], Callable[..., Expr]]: ... | ||
|
|
||
| @overload | ||
| @staticmethod | ||
| def udf( | ||
| func: Callable[..., _R], | ||
| input_types: list[pa.DataType], | ||
| return_type: _R, | ||
| func: Callable[..., PyArrowArray], | ||
| input_types: list[pa.DataType | pa.Field], | ||
| return_type: pa.DataType | pa.Field, | ||
| volatility: Volatility | str, | ||
| name: Optional[str] = None, | ||
| ) -> ScalarUDF: ... | ||
|
|
@@ -164,10 +270,15 @@ def udf(*args: Any, **kwargs: Any): # noqa: D417 | |
| backed ScalarUDF within a PyCapsule, you can pass this parameter | ||
| and ignore the rest. They will be determined directly from the | ||
| underlying function. See the online documentation for more information. | ||
| input_types (list[pa.DataType]): The data types of the arguments | ||
| to ``func``. This list must be of the same length as the number of | ||
| arguments. | ||
| return_type (_R): The data type of the return value from the function. | ||
| The callable should accept and return :class:`pyarrow.Array` or | ||
| :class:`pyarrow.ChunkedArray` values. | ||
| input_types (list[pa.DataType | pa.Field]): The argument types for ``func``. | ||
| This list must be of the same length as the number of arguments. Pass | ||
| :class:`pyarrow.Field` instances when you need to declare extension | ||
| metadata for an argument. | ||
| return_type (pa.DataType | pa.Field): The return type of the function. | ||
| Supply a :class:`pyarrow.Field` when the result should expose | ||
| extension metadata to downstream consumers. | ||
| volatility (Volatility | str): See `Volatility` for allowed values. | ||
| name (Optional[str]): A descriptive name for the function. | ||
|
|
||
|
|
@@ -179,8 +290,13 @@ def udf(*args: Any, **kwargs: Any): # noqa: D417 | |
|
|
||
| def double_func(x): | ||
| return x * 2 | ||
| double_udf = udf(double_func, [pa.int32()], pa.int32(), | ||
| "volatile", "double_it") | ||
| double_udf = udf( | ||
| double_func, | ||
| [pa.int32()], | ||
| pa.int32(), | ||
| "volatile", | ||
| "double_it", | ||
| ) | ||
|
|
||
| Example: Using ``udf`` as a decorator:: | ||
|
|
||
|
|
@@ -190,9 +306,9 @@ def double_udf(x): | |
| """ | ||
|
|
||
| def _function( | ||
| func: Callable[..., _R], | ||
| input_types: list[pa.DataType], | ||
| return_type: _R, | ||
| func: Callable[..., PyArrowArray], | ||
| input_types: list[pa.DataType | pa.Field], | ||
| return_type: pa.DataType | pa.Field, | ||
| volatility: Volatility | str, | ||
| name: Optional[str] = None, | ||
| ) -> ScalarUDF: | ||
|
|
@@ -213,18 +329,18 @@ def _function( | |
| ) | ||
|
|
||
| def _decorator( | ||
| input_types: list[pa.DataType], | ||
| return_type: _R, | ||
| input_types: list[pa.DataType | pa.Field], | ||
| return_type: pa.DataType | pa.Field, | ||
| volatility: Volatility | str, | ||
| name: Optional[str] = None, | ||
| ) -> Callable: | ||
| def decorator(func: Callable): | ||
| ) -> Callable[[Callable[..., PyArrowArray]], Callable[..., Expr]]: | ||
| def decorator(func: Callable[..., PyArrowArray]) -> Callable[..., Expr]: | ||
| udf_caller = ScalarUDF.udf( | ||
| func, input_types, return_type, volatility, name | ||
| ) | ||
|
|
||
| @functools.wraps(func) | ||
| def wrapper(*args: Any, **kwargs: Any): | ||
| def wrapper(*args: Any, **kwargs: Any) -> Expr: | ||
| return udf_caller(*args, **kwargs) | ||
|
|
||
| return wrapper | ||
|
|
@@ -357,10 +473,12 @@ def udaf(*args: Any, **kwargs: Any): # noqa: D417, C901 | |
| This class allows you to define an aggregate function that can be used in | ||
| data aggregation or window function calls. | ||
|
|
||
| Usage: | ||
| - As a function: ``udaf(accum, input_types, return_type, state_type, volatility, name)``. | ||
| - As a decorator: ``@udaf(input_types, return_type, state_type, volatility, name)``. | ||
| When using ``udaf`` as a decorator, do not pass ``accum`` explicitly. | ||
| Usage: | ||
| - As a function: ``udaf(accum, input_types, return_type, state_type,`` | ||
| ``volatility, name)``. | ||
| - As a decorator: ``@udaf(input_types, return_type, state_type,`` | ||
| ``volatility, name)``. | ||
| When using ``udaf`` as a decorator, do not pass ``accum`` explicitly. | ||
|
Comment on lines
+476
to
+481
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like the formatting got changed. Is this intentional? |
||
|
|
||
| Function example: | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the change added to ensure pa.uuid() is available for test_udf.py.
https://arrow.apache.org/docs/19.0/python/generated/pyarrow.uuid.html is the lowest version which contains pyarrow.uuid.
The rest are VSCode automatic formatting.