|
| 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 | +from typing import Any, Callable, Iterable, Iterator, Mapping, Optional, Type |
| 19 | + |
| 20 | +from nanoarrow.c_schema import CSchema, CSchemaView, c_schema_view |
| 21 | + |
| 22 | + |
| 23 | +class Extension: |
| 24 | + """Define a nanoarrow Extension |
| 25 | +
|
| 26 | + A nanoarrow extension customizes behaviour of built-in operations |
| 27 | + applicable to a specific type. This is currently implemented only |
| 28 | + for Arrow extension types but could in theory apply if one wanted |
| 29 | + to customize the conversion behaviour of a specific non-extension |
| 30 | + type. |
| 31 | +
|
| 32 | + This is currently internal and involves knowledge of other internal |
| 33 | + nanoarrow/Python structures. It is currently used only to implement |
| 34 | + canonical extensions with the anticipation of evolving to support |
| 35 | + user-defined extensions as the internal APIs on which it relies |
| 36 | + stabilize. |
| 37 | +
|
| 38 | + With the current design, an Extension subclass must be constructible |
| 39 | + with no parameters (e.g., ``Extension()``). |
| 40 | + """ |
| 41 | + |
| 42 | + def get_schema(self) -> CSchema: |
| 43 | + """Get the schema for which this extension applies. |
| 44 | +
|
| 45 | + This is used by :func:`register_extension` to ensure that it can be resolved |
| 46 | + when needed. |
| 47 | + """ |
| 48 | + raise NotImplementedError() |
| 49 | + |
| 50 | + def get_params(self, c_schema: CSchema) -> Mapping[str, Any]: |
| 51 | + """Compute a dictionary of type parameters. |
| 52 | +
|
| 53 | + These parameters are accessible via the :class:`Schema` |
| 54 | + ``extension`` attribute (e.g., ``schema.extension.param_name``). |
| 55 | + Internal parameters can also be returned but should be prefixed with |
| 56 | + an underscore. |
| 57 | +
|
| 58 | + This method should also error if the storage type or any other property |
| 59 | + of the schema is not valid. |
| 60 | + """ |
| 61 | + return {} |
| 62 | + |
| 63 | + def get_pyiter( |
| 64 | + self, |
| 65 | + py_iterator, |
| 66 | + offset: int, |
| 67 | + length: int, |
| 68 | + ) -> Optional[Iterator[Optional[bool]]]: |
| 69 | + """Compute an iterable of Python objects. |
| 70 | +
|
| 71 | + Used by ``to_pylist()`` to generate scalars for a particular type. |
| 72 | + If ``None`` is returned, the behaviour of the storage type will be |
| 73 | + used without warning. |
| 74 | +
|
| 75 | + This method is currently passed the underlying :class:`PyIterator` |
| 76 | + and returns an iterator; however, it could in the future be passed |
| 77 | + a :class:`CSchema` and return a PyIterator class once that class |
| 78 | + structure is stabilized. |
| 79 | + """ |
| 80 | + name = py_iterator._schema_view.extension_name |
| 81 | + raise NotImplementedError(f"Extension get_pyiter() for {name}") |
| 82 | + |
| 83 | + def get_sequence_converter(self, c_schema: CSchema): |
| 84 | + """Return an ArrayViewVisitor subclass used to compute a sequence from |
| 85 | + a stream of arrays. |
| 86 | +
|
| 87 | + This is currently implemented outside the null handler and may need a flag |
| 88 | + at some point to indicate that it did or did not handle its own nulls. |
| 89 | + """ |
| 90 | + schema_view = c_schema_view(c_schema) |
| 91 | + name = schema_view.extension_name |
| 92 | + raise NotImplementedError(f"Extension get_sequence_converter() for {name}") |
| 93 | + |
| 94 | + def get_buffer_appender( |
| 95 | + self, c_schema: CSchema, array_builder |
| 96 | + ) -> Optional[Callable[[Any], None]]: |
| 97 | + """Compute a function that prepares a :class:`CArrayBuilder` from a |
| 98 | + buffer. |
| 99 | +
|
| 100 | + This is used to customize the behavior of creating a CArray from an |
| 101 | + object implementing the Python buffer protocol. If ``None`` is |
| 102 | + returned, the storage will be converted without a warning. |
| 103 | +
|
| 104 | + This method is currently passed a :class:`CArrayBuilder` but in |
| 105 | + the future should perhaps be passed a :class:`CSchema` and return a |
| 106 | + CArrayBuilder class. |
| 107 | + """ |
| 108 | + schema_view = c_schema_view(c_schema) |
| 109 | + name = schema_view.extension_name |
| 110 | + raise NotImplementedError(f"Extension get_buffer_appender() for {name}") |
| 111 | + |
| 112 | + def get_iterable_appender( |
| 113 | + self, c_schema: CSchema, array_builder |
| 114 | + ) -> Optional[Callable[[Iterable], None]]: |
| 115 | + """Compute a function that prepares a :class:`CArrayBuilder` from a |
| 116 | + buffer. |
| 117 | +
|
| 118 | + This is used to customize the behavior of creating a CArray from an |
| 119 | + iterable of Python objects. |
| 120 | +
|
| 121 | + This method is currently passed a :class:`CArrayBuilder` but in |
| 122 | + the future should perhaps be passed a :class:`CSchema` and return a |
| 123 | + CArrayBuilder class. |
| 124 | + """ |
| 125 | + schema_view = c_schema_view(c_schema) |
| 126 | + name = schema_view.extension_name |
| 127 | + raise NotImplementedError(f"Extension get_iterable_appender() for {name}") |
| 128 | + |
| 129 | + |
| 130 | +_global_extension_registry = {} |
| 131 | + |
| 132 | + |
| 133 | +def resolve_extension(c_schema_view: CSchemaView) -> Optional[Extension]: |
| 134 | + """Resolve an extension instance from a :class:`CSchemaView` |
| 135 | +
|
| 136 | + Returns the registered extension instance if one applies to the passed |
| 137 | + type or ``None`` otherwise. |
| 138 | + """ |
| 139 | + extension_name = c_schema_view.extension_name |
| 140 | + if extension_name in _global_extension_registry: |
| 141 | + return _global_extension_registry[extension_name] |
| 142 | + |
| 143 | + return None |
| 144 | + |
| 145 | + |
| 146 | +def register_extension(extension: Extension) -> Optional[Extension]: |
| 147 | + """Register an :class:`Extension` instance in the global registry. |
| 148 | +
|
| 149 | + Inserts an extension into the global registry, returning the |
| 150 | + previously registered extension for that type if one exists |
| 151 | + (or ``None`` otherwise). |
| 152 | + """ |
| 153 | + global _global_extension_registry |
| 154 | + |
| 155 | + schema_view = c_schema_view(extension.get_schema()) |
| 156 | + key = schema_view.extension_name |
| 157 | + prev = resolve_extension(schema_view) |
| 158 | + _global_extension_registry[key] = extension |
| 159 | + return prev |
| 160 | + |
| 161 | + |
| 162 | +def unregister_extension(extension_name: str): |
| 163 | + """Remove an extension from the global registry by extension name. |
| 164 | +
|
| 165 | + Returns the removed extension. Raises ``KeyError`` if there was no |
| 166 | + extension registered for this extension name. |
| 167 | + """ |
| 168 | + prev = _global_extension_registry[extension_name] |
| 169 | + del _global_extension_registry[extension_name] |
| 170 | + return prev |
| 171 | + |
| 172 | + |
| 173 | +def register(extension_cls: Type[Extension]): |
| 174 | + """Decorator that registers an extension class by instantiating it |
| 175 | + and adding it to the global registry.""" |
| 176 | + register_extension(extension_cls()) |
| 177 | + return extension_cls |
0 commit comments