FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add typed Protocols for FFI capsule exports (part of #1577) by stantheman0128 · Pull Request #1671 · apache/datafusion-python · GitHub

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
29 changes: 27 additions & 2 deletions python/datafusion/context.py
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
Expand Up @@ -145,6 +145,29 @@ class PhysicalOptimizerRuleExportable(Protocol):
def __datafusion_physical_optimizer_rule__(self) -> object: ... # noqa: D105


class ExtensionOptionsExportable(Protocol):
"""Type hint for object that has __datafusion_extension_options__ PyCapsule.

The method returns a PyCapsule wrapping an ``FFI_ExtensionOptions``,
typically produced by a separate compiled extension and consumed by
:py:meth:`SessionConfig.with_extension`.
"""

def __datafusion_extension_options__(self) -> object: ... # noqa: D105


class TaskContextProviderExportable(Protocol):
"""Type hint for object that has __datafusion_task_context_provider__ PyCapsule.

The method returns a PyCapsule wrapping an ``FFI_TaskContextProvider``.
:py:class:`SessionContext` exposes one for its own task context; a
separate compiled extension can decode it (or one of its own) using
the matching Rust-side ``from_pycapsule`` helper.
"""

def __datafusion_task_context_provider__(self) -> object: ... # noqa: D105


class SessionConfig:
"""Session configuration options."""

Expand Down Expand Up @@ -337,12 +360,14 @@ def set(self, key: str, value: str) -> SessionConfig:
self.config_internal = self.config_internal.set(key, value)
return self

def with_extension(self, extension: Any) -> SessionConfig:
def with_extension(self, extension: ExtensionOptionsExportable) -> SessionConfig:
"""Create a new configuration using an extension.

Args:
extension: A custom configuration extension object. These are
shared from another DataFusion extension library.
shared from another DataFusion extension library. It must expose
an ``__datafusion_extension_options__`` PyCapsule, see
:py:class:`ExtensionOptionsExportable`.

Returns:
A new :py:class:`SessionConfig` object with the updated setting.
Expand Down
15 changes: 14 additions & 1 deletion python/datafusion/user_defined.py
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
Expand Up @@ -1121,6 +1121,15 @@ def adapter(*args: Any, session: Any, **kwargs: Any) -> Any:
return adapter


class TableFunctionExportable(Protocol):
"""Type hint for object that has __datafusion_table_function__ PyCapsule.

https://datafusion.apache.org/python/user-guide/io/table_provider.html
"""

def __datafusion_table_function__(self, session: Any) -> object: ... # noqa: D105


class TableFunction:
"""Class for performing user-defined table functions (UDTF).

Expand All @@ -1131,7 +1140,7 @@ class TableFunction:
def __init__(
self,
name: str,
func: Callable[..., Any],
func: Callable[..., Any] | TableFunctionExportable,
ctx: SessionContext | None = None,
*,
with_session: bool = False,
Expand Down Expand Up @@ -1190,6 +1199,10 @@ def udtf(
with_session: bool = False,
) -> TableFunction: ...

@overload
@staticmethod
def udtf(func: TableFunctionExportable, name: str) -> TableFunction: ...

@staticmethod
def udtf(*args: Any, with_session: bool = False, **kwargs: Any):
"""Create a new User-Defined Table Function (UDTF).
Expand Down

Back | FazBrowse Home | New Git URL