|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +"""Protocols and value types for installing extensions on a session context. |
| 19 | +
|
| 20 | +An *extension* is a reusable configuration object — typically shipped by a |
| 21 | +separate compiled library — that contributes components to a |
| 22 | +:py:class:`~datafusion.context.SessionContext`. It implements |
| 23 | +:py:class:`SessionExtensionExportable` by returning a |
| 24 | +:py:class:`SessionExtensionComponents` describing what it contributes, and is |
| 25 | +installed with :py:meth:`~datafusion.context.SessionContext.with_extensions`:: |
| 26 | +
|
| 27 | + ctx = SessionContext().with_extensions(MyLibraryExtension()) |
| 28 | +
|
| 29 | +Installing through ``with_extensions`` rather than by chaining the individual |
| 30 | +``with_*`` methods matters for components that hold a task-context provider: |
| 31 | +the extension is handed the destination context so every component binds to |
| 32 | +the session that is actually returned. See the FFI extensions guide in the |
| 33 | +contributor documentation for the full rationale. |
| 34 | +""" |
| 35 | + |
| 36 | +from __future__ import annotations |
| 37 | + |
| 38 | +from dataclasses import dataclass |
| 39 | +from typing import TYPE_CHECKING, Any, Protocol |
| 40 | + |
| 41 | +if TYPE_CHECKING: |
| 42 | + from _typeshed import CapsuleType as _PyCapsule |
| 43 | + |
| 44 | + from datafusion.context import SessionContext |
| 45 | + from datafusion.user_defined import ( |
| 46 | + LogicalExtensionCodecExportable, |
| 47 | + PhysicalExtensionCodecExportable, |
| 48 | + ) |
| 49 | + |
| 50 | +__all__ = [ |
| 51 | + "QueryPlannerExportable", |
| 52 | + "SessionExtensionComponents", |
| 53 | + "SessionExtensionExportable", |
| 54 | +] |
| 55 | + |
| 56 | + |
| 57 | +class QueryPlannerExportable(Protocol): |
| 58 | + """Type hint for object that has a __datafusion_query_planner__ PyCapsule. |
| 59 | +
|
| 60 | + The method returns a PyCapsule wrapping an ``FFI_QueryPlanner``, typically |
| 61 | + produced by a separate compiled extension. ``session`` is the |
| 62 | + :py:class:`~datafusion.context.SessionContext` the planner is being |
| 63 | + installed on; take the extension codecs from it rather than building your |
| 64 | + own. |
| 65 | + """ |
| 66 | + |
| 67 | + def __datafusion_query_planner__(self, session: Any) -> object: ... # noqa: D105 |
| 68 | + |
| 69 | + |
| 70 | +@dataclass(frozen=True) |
| 71 | +class SessionExtensionComponents: |
| 72 | + """Components an extension contributes to a session context. |
| 73 | +
|
| 74 | + Returned by :py:meth:`SessionExtensionExportable.__datafusion_session_extension__` |
| 75 | + and consumed by |
| 76 | + :py:meth:`~datafusion.context.SessionContext.with_extensions`. Every |
| 77 | + component must be created against the context passed to that method; |
| 78 | + components bound to any other context hold a task-context provider for the |
| 79 | + wrong session and cannot be rebound. |
| 80 | + """ |
| 81 | + |
| 82 | + logical_extension_codecs: tuple[ |
| 83 | + LogicalExtensionCodecExportable | _PyCapsule, ... |
| 84 | + ] = () |
| 85 | + """Logical codecs to add to the session's codec chain, in declaration order.""" |
| 86 | + |
| 87 | + physical_extension_codecs: tuple[ |
| 88 | + PhysicalExtensionCodecExportable | _PyCapsule, ... |
| 89 | + ] = () |
| 90 | + """Physical codecs to add to the session's codec chain, in declaration order.""" |
| 91 | + |
| 92 | + query_planner: QueryPlannerExportable | _PyCapsule | None = None |
| 93 | + """Optional query planner. |
| 94 | +
|
| 95 | + At most one extension per |
| 96 | + :py:meth:`~datafusion.context.SessionContext.with_extensions` call may |
| 97 | + supply one. |
| 98 | + """ |
| 99 | + |
| 100 | + |
| 101 | +class SessionExtensionExportable(Protocol): |
| 102 | + """Type hint for extension bundles installable via ``with_extensions``. |
| 103 | +
|
| 104 | + Implementations are reusable configuration objects: they must not retain a |
| 105 | + :py:class:`~datafusion.context.SessionContext` and must create fresh |
| 106 | + components on every call using the context supplied by |
| 107 | + :py:meth:`~datafusion.context.SessionContext.with_extensions`. They should |
| 108 | + also avoid mutating global state during binding, since a failed |
| 109 | + installation discards the destination context. |
| 110 | + """ |
| 111 | + |
| 112 | + def __datafusion_session_extension__( # noqa: D105 |
| 113 | + self, ctx: SessionContext |
| 114 | + ) -> SessionExtensionComponents: ... |
0 commit comments