|
| 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 typing import TYPE_CHECKING, Any |
| 22 | + |
| 23 | +from datafusion._internal import EXPECTED_PROVIDER_MSG |
| 24 | + |
| 25 | +if TYPE_CHECKING: # pragma: no cover - imported for typing only |
| 26 | + from datafusion import TableProvider |
| 27 | + from datafusion.catalog import Table |
| 28 | + from datafusion.context import TableProviderExportable |
| 29 | + |
| 30 | + |
| 31 | +def _normalize_table_provider( |
| 32 | + table: Table | TableProvider | TableProviderExportable, |
| 33 | +) -> Any: |
| 34 | + """Return the underlying provider for supported table inputs. |
| 35 | +
|
| 36 | + Args: |
| 37 | + table: A :class:`~datafusion.catalog.Table`, |
| 38 | + :class:`~datafusion.table_provider.TableProvider`, or object exporting a |
| 39 | + DataFusion table provider via ``__datafusion_table_provider__``. |
| 40 | +
|
| 41 | + Returns: |
| 42 | + The object expected by the Rust bindings for table registration. |
| 43 | +
|
| 44 | + Raises: |
| 45 | + TypeError: If ``table`` is not a supported table provider input. |
| 46 | + """ |
| 47 | + |
| 48 | + from datafusion.catalog import Table as _Table |
| 49 | + from datafusion.table_provider import TableProvider as _TableProvider |
| 50 | + |
| 51 | + if isinstance(table, _Table): |
| 52 | + return table.table |
| 53 | + |
| 54 | + if isinstance(table, _TableProvider): |
| 55 | + return table._table_provider |
| 56 | + |
| 57 | + provider_factory = getattr(table, "__datafusion_table_provider__", None) |
| 58 | + if callable(provider_factory): |
| 59 | + return table |
| 60 | + |
| 61 | + raise TypeError(EXPECTED_PROVIDER_MSG) |
0 commit comments