Skip to content

Commit 779ba7a

Browse files
committed
UNPICK changes to review
1 parent 2ada108 commit 779ba7a

File tree

17 files changed

+155
-374
lines changed

17 files changed

+155
-374
lines changed

docs/source/contributor-guide/ffi.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ as performant as possible and to utilize the features of DataFusion, you may dec
3434
your source in Rust and then expose it through `PyO3 <https://pyo3.rs>`_ as a Python library.
3535

3636
At first glance, it may appear the best way to do this is to add the ``datafusion-python``
37-
crate as a dependency, produce a DataFusion table in Rust, and then register it with the
37+
crate as a dependency, provide a ``PyTable``, and then to register it with the
3838
``SessionContext``. Unfortunately, this will not work.
3939

4040
When you produce your code as a Python library and it needs to interact with the DataFusion

docs/source/user-guide/data-sources.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,9 @@ as Delta Lake. This will require a recent version of
152152
.. code-block:: python
153153
154154
from deltalake import DeltaTable
155-
from datafusion import TableProvider
156155
157156
delta_table = DeltaTable("path_to_table")
158-
provider = TableProvider.from_capsule(delta_table.__datafusion_table_provider__())
159-
ctx.register_table("my_delta_table", provider)
157+
ctx.register_table_provider("my_delta_table", delta_table)
160158
df = ctx.table("my_delta_table")
161159
df.show()
162160

docs/source/user-guide/io/table_provider.rst

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,33 +47,12 @@ A complete example can be found in the `examples folder <https://github.com/apac
4747
}
4848
}
4949
50-
Once you have this library available, you can construct a
51-
:py:class:`~datafusion.TableProvider` in Python and register it with the
52-
``SessionContext``. Table providers can be created either from the PyCapsule exposed by
53-
your Rust provider or from an existing :py:class:`~datafusion.dataframe.DataFrame`.
54-
Call the provider's ``__datafusion_table_provider__()`` method to obtain the capsule
55-
before constructing a ``TableProvider``. The ``TableProvider.from_view()`` helper is
56-
deprecated; instead use ``TableProvider.from_dataframe()`` or ``DataFrame.into_view()``.
50+
Once you have this library available, in python you can register your table provider
51+
to the ``SessionContext``.
5752

5853
.. code-block:: python
5954
60-
from datafusion import SessionContext, TableProvider
61-
62-
ctx = SessionContext()
6355
provider = MyTableProvider()
56+
ctx.register_table_provider("my_table", provider)
6457
65-
capsule = provider.__datafusion_table_provider__()
66-
capsule_provider = TableProvider.from_capsule(capsule)
67-
68-
df = ctx.from_pydict({"a": [1]})
69-
view_provider = TableProvider.from_dataframe(df)
70-
# or: view_provider = df.into_view()
71-
72-
ctx.register_table("capsule_table", capsule_provider)
73-
ctx.register_table("view_table", view_provider)
74-
75-
ctx.table("capsule_table").show()
76-
ctx.table("view_table").show()
77-
78-
Both ``TableProvider.from_capsule()`` and ``TableProvider.from_dataframe()`` create
79-
table providers that can be registered with the SessionContext using ``register_table()``.
58+
ctx.table("my_table").show()

examples/datafusion-ffi-example/python/tests/_test_table_function.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def test_ffi_table_function_call_directly():
5353
table_udtf = udtf(table_func, "my_table_func")
5454

5555
my_table = table_udtf()
56-
ctx.register_table("t", my_table)
56+
ctx.register_table_provider("t", my_table)
5757
result = ctx.table("t").collect()
5858

5959
assert len(result) == 2

examples/datafusion-ffi-example/python/tests/_test_table_provider.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@
1818
from __future__ import annotations
1919

2020
import pyarrow as pa
21-
from datafusion import SessionContext, TableProvider
21+
from datafusion import SessionContext
2222
from datafusion_ffi_example import MyTableProvider
2323

2424

2525
def test_table_loading():
2626
ctx = SessionContext()
2727
table = MyTableProvider(3, 2, 4)
28-
ctx.register_table(
29-
"t", TableProvider.from_capsule(table.__datafusion_table_provider__())
30-
)
28+
ctx.register_table_provider("t", table)
3129
result = ctx.table("t").collect()
3230

3331
assert len(result) == 4

python/datafusion/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from . import functions, object_store, substrait, unparser
3434

3535
# The following imports are okay to remain as opaque to the user.
36-
from ._internal import Config, TableProvider
36+
from ._internal import Config
3737
from .catalog import Catalog, Database, Table
3838
from .col import col, column
3939
from .common import (
@@ -90,7 +90,6 @@
9090
"SessionContext",
9191
"Table",
9292
"TableFunction",
93-
"TableProvider",
9493
"WindowFrame",
9594
"WindowUDF",
9695
"catalog",

python/datafusion/catalog.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
if TYPE_CHECKING:
2828
import pyarrow as pa
2929

30-
from datafusion import TableProvider
31-
3230
try:
3331
from warnings import deprecated # Python 3.13+
3432
except ImportError:
@@ -124,8 +122,8 @@ def table(self, name: str) -> Table:
124122
"""Return the table with the given ``name`` from this schema."""
125123
return Table(self._raw_schema.table(name))
126124

127-
def register_table(self, name, table: Table | TableProvider) -> None:
128-
"""Register a table or table provider in this schema."""
125+
def register_table(self, name, table) -> None:
126+
"""Register a table provider in this schema."""
129127
if isinstance(table, Table):
130128
return self._raw_schema.register_table(name, table.table)
131129
return self._raw_schema.register_table(name, table)
@@ -221,8 +219,8 @@ def table(self, name: str) -> Table | None:
221219
"""Retrieve a specific table from this schema."""
222220
...
223221

224-
def register_table(self, name: str, table: Table | TableProvider) -> None: # noqa: B027
225-
"""Add a table to this schema.
222+
def register_table(self, name: str, table: Table) -> None: # noqa: B027
223+
"""Add a table from this schema.
226224
227225
This method is optional. If your schema provides a fixed list of tables, you do
228226
not need to implement this method.

python/datafusion/context.py

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import pandas as pd
4747
import polars as pl
4848

49-
from datafusion import TableProvider
5049
from datafusion.plan import ExecutionPlan, LogicalPlan
5150

5251

@@ -735,7 +734,7 @@ def from_polars(self, data: pl.DataFrame, name: str | None = None) -> DataFrame:
735734
# https://github.com/apache/datafusion-python/pull/1016#discussion_r1983239116
736735
# is the discussion on how we arrived at adding register_view
737736
def register_view(self, name: str, df: DataFrame) -> None:
738-
"""Register a :py:class:`~datafusion.dataframe.DataFrame` as a view.
737+
"""Register a :py:class: `~datafusion.detaframe.DataFrame` as a view.
739738
740739
Args:
741740
name (str): The name to register the view under.
@@ -744,25 +743,16 @@ def register_view(self, name: str, df: DataFrame) -> None:
744743
view = df.into_view()
745744
self.ctx.register_table(name, view)
746745

747-
def register_table(self, name: str, table: Table | TableProvider) -> None:
748-
"""Register a :py:class:`~datafusion.catalog.Table` or ``TableProvider``.
746+
def register_table(self, name: str, table: Table) -> None:
747+
"""Register a :py:class: `~datafusion.catalog.Table` as a table.
749748
750-
The registered table can be referenced from SQL statements executed against
751-
this context.
752-
753-
Plain :py:class:`~datafusion.dataframe.DataFrame` objects are not supported;
754-
convert them first with :meth:`datafusion.dataframe.DataFrame.into_view` or
755-
:meth:`datafusion.catalog.TableProvider.from_dataframe`.
749+
The registered table can be referenced from SQL statement executed against.
756750
757751
Args:
758752
name: Name of the resultant table.
759-
table: DataFusion :class:`Table` or :class:`TableProvider` to add to the
760-
session context.
753+
table: DataFusion table to add to the session context.
761754
"""
762-
if isinstance(table, Table):
763-
self.ctx.register_table(name, table.table)
764-
else:
765-
self.ctx.register_table(name, table)
755+
self.ctx.register_table(name, table.table)
766756

767757
def deregister_table(self, name: str) -> None:
768758
"""Remove a table from the session."""
@@ -782,18 +772,14 @@ def register_catalog_provider(
782772
self.ctx.register_catalog_provider(name, provider)
783773

784774
def register_table_provider(
785-
self, name: str, provider: TableProviderExportable | TableProvider
775+
self, name: str, provider: TableProviderExportable
786776
) -> None:
787777
"""Register a table provider.
788778
789-
Deprecated: use :meth:`register_table` instead.
779+
This table provider must have a method called ``__datafusion_table_provider__``
780+
which returns a PyCapsule that exposes a ``FFI_TableProvider``.
790781
"""
791-
warnings.warn(
792-
"register_table_provider is deprecated; use register_table",
793-
DeprecationWarning,
794-
stacklevel=2,
795-
)
796-
self.register_table(name, provider)
782+
self.ctx.register_table_provider(name, provider)
797783

798784
def register_udtf(self, func: TableFunction) -> None:
799785
"""Register a user defined table function."""

python/datafusion/dataframe.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
import polars as pl
5353
import pyarrow as pa
5454

55-
from datafusion._internal import TableProvider
5655
from datafusion._internal import expr as expr_internal
5756

5857
from enum import Enum
@@ -308,14 +307,8 @@ def __init__(self, df: DataFrameInternal) -> None:
308307
"""
309308
self.df = df
310309

311-
def into_view(self) -> TableProvider:
312-
"""Convert ``DataFrame`` into a ``TableProvider`` view for registration.
313-
314-
This is the preferred way to obtain a view for
315-
:py:meth:`~datafusion.context.SessionContext.register_table`.
316-
``TableProvider.from_dataframe`` calls this method under the hood,
317-
and the older ``TableProvider.from_view`` helper is deprecated.
318-
"""
310+
def into_view(self) -> pa.Table:
311+
"""Convert DataFrame as a ViewTable which can be used in register_table."""
319312
return self.df.into_view()
320313

321314
def __getitem__(self, key: str | list[str]) -> DataFrame:

python/tests/test_context.py

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
SessionConfig,
2828
SessionContext,
2929
SQLOptions,
30-
TableProvider,
3130
column,
3231
literal,
3332
)
@@ -331,40 +330,6 @@ def test_deregister_table(ctx, database):
331330
assert public.names() == {"csv1", "csv2"}
332331

333332

334-
def test_register_table_from_dataframe_into_view(ctx):
335-
df = ctx.from_pydict({"a": [1, 2]})
336-
provider = df.into_view()
337-
ctx.register_table("view_tbl", provider)
338-
result = ctx.sql("SELECT * FROM view_tbl").collect()
339-
assert [b.to_pydict() for b in result] == [{"a": [1, 2]}]
340-
341-
342-
def test_table_provider_from_capsule(ctx):
343-
df = ctx.from_pydict({"a": [1, 2]})
344-
provider = df.into_view()
345-
capsule = provider.__datafusion_table_provider__()
346-
provider2 = TableProvider.from_capsule(capsule)
347-
ctx.register_table("capsule_tbl", provider2)
348-
result = ctx.sql("SELECT * FROM capsule_tbl").collect()
349-
assert [b.to_pydict() for b in result] == [{"a": [1, 2]}]
350-
351-
352-
def test_table_provider_from_capsule_invalid():
353-
with pytest.raises(Exception): # noqa: B017
354-
TableProvider.from_capsule(object())
355-
356-
357-
def test_register_table_with_dataframe_errors(ctx):
358-
df = ctx.from_pydict({"a": [1]})
359-
with pytest.raises(Exception) as exc_info: # noqa: B017
360-
ctx.register_table("bad", df)
361-
362-
assert (
363-
str(exc_info.value)
364-
== 'Expected a Table or TableProvider. Convert DataFrames with "DataFrame.into_view()" or "TableProvider.from_dataframe()".'
365-
)
366-
367-
368333
def test_register_dataset(ctx):
369334
# create a RecordBatch and register it as a pyarrow.dataset.Dataset
370335
batch = pa.RecordBatch.from_arrays(

0 commit comments

Comments
 (0)