|
| 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 | +"""Miscellaneous helper utilities for DataFusion's Python bindings.""" |
| 18 | + |
| 19 | +from __future__ import annotations |
| 20 | + |
| 21 | +from importlib import import_module, util |
| 22 | +from typing import TYPE_CHECKING, Any |
| 23 | + |
| 24 | +from datafusion._internal import EXPECTED_PROVIDER_MSG |
| 25 | + |
| 26 | +_PYARROW_DATASET_TYPES: tuple[type[Any], ...] |
| 27 | +_dataset_spec = util.find_spec("pyarrow.dataset") |
| 28 | +if _dataset_spec is None: # pragma: no cover - optional dependency at runtime |
| 29 | + _PYARROW_DATASET_TYPES = () |
| 30 | +else: # pragma: no cover - exercised in environments with pyarrow installed |
| 31 | + _dataset_module = import_module("pyarrow.dataset") |
| 32 | + dataset_base = getattr(_dataset_module, "Dataset", None) |
| 33 | + dataset_types: set[type[Any]] = set() |
| 34 | + if isinstance(dataset_base, type): |
| 35 | + dataset_types.add(dataset_base) |
| 36 | + for value in vars(_dataset_module).values(): |
| 37 | + if isinstance(value, type) and issubclass(value, dataset_base): |
| 38 | + dataset_types.add(value) |
| 39 | + _PYARROW_DATASET_TYPES = tuple(dataset_types) |
| 40 | + |
| 41 | +if TYPE_CHECKING: # pragma: no cover - imported for typing only |
| 42 | + from datafusion import TableProvider |
| 43 | + from datafusion.catalog import Table |
| 44 | + from datafusion.context import TableProviderExportable |
| 45 | + |
| 46 | + |
| 47 | +def _normalize_table_provider( |
| 48 | + table: Table | TableProvider | TableProviderExportable, |
| 49 | +) -> Any: |
| 50 | + """Return the underlying provider for supported table inputs. |
| 51 | +
|
| 52 | + Args: |
| 53 | + table: A :class:`~datafusion.catalog.Table`, |
| 54 | + :class:`~datafusion.table_provider.TableProvider`, or object exporting a |
| 55 | + DataFusion table provider via ``__datafusion_table_provider__``. |
| 56 | +
|
| 57 | + Returns: |
| 58 | + The object expected by the Rust bindings for table registration. |
| 59 | +
|
| 60 | + Raises: |
| 61 | + TypeError: If ``table`` is not a supported table provider input. |
| 62 | + """ |
| 63 | + |
| 64 | + from datafusion.catalog import Table as _Table |
| 65 | + from datafusion.table_provider import TableProvider as _TableProvider |
| 66 | + |
| 67 | + if isinstance(table, _Table): |
| 68 | + return table.table |
| 69 | + |
| 70 | + if isinstance(table, _TableProvider): |
| 71 | + return table._table_provider |
| 72 | + |
| 73 | + if _PYARROW_DATASET_TYPES and isinstance(table, _PYARROW_DATASET_TYPES): |
| 74 | + return table |
| 75 | + |
| 76 | + provider_factory = getattr(table, "__datafusion_table_provider__", None) |
| 77 | + if callable(provider_factory): |
| 78 | + return table |
| 79 | + |
| 80 | + raise TypeError(EXPECTED_PROVIDER_MSG) |
0 commit comments