2020from __future__ import annotations
2121
2222from abc import ABC , abstractmethod
23- from typing import TYPE_CHECKING , Protocol
23+ from typing import TYPE_CHECKING , Any , Protocol
2424
2525import datafusion ._internal as df_internal
2626
2727if TYPE_CHECKING :
2828 import pyarrow as pa
2929
30+ from datafusion import DataFrame
31+ from datafusion .context import TableProviderExportable
32+
3033try :
3134 from warnings import deprecated # Python 3.13+
3235except ImportError :
@@ -82,7 +85,11 @@ def database(self, name: str = "public") -> Schema:
8285 """Returns the database with the given ``name`` from this catalog."""
8386 return self .schema (name )
8487
85- def register_schema (self , name , schema ) -> Schema | None :
88+ def register_schema (
89+ self ,
90+ name : str ,
91+ schema : Schema | SchemaProvider | SchemaProviderExportable ,
92+ ) -> Schema | None :
8693 """Register a schema with this catalog."""
8794 if isinstance (schema , Schema ):
8895 return self .catalog .register_schema (name , schema ._raw_schema )
@@ -122,10 +129,12 @@ def table(self, name: str) -> Table:
122129 """Return the table with the given ``name`` from this schema."""
123130 return Table (self ._raw_schema .table (name ))
124131
125- def register_table (self , name , table ) -> None :
126- """Register a table provider in this schema."""
127- if isinstance (table , Table ):
128- return self ._raw_schema .register_table (name , table .table )
132+ def register_table (
133+ self ,
134+ name : str ,
135+ table : Table | TableProviderExportable | DataFrame | pa .dataset .Dataset ,
136+ ) -> None :
137+ """Register a table in this schema."""
129138 return self ._raw_schema .register_table (name , table )
130139
131140 def deregister_table (self , name : str ) -> None :
@@ -139,30 +148,45 @@ class Database(Schema):
139148
140149
141150class Table :
142- """DataFusion table."""
151+ """A DataFusion table.
143152
144- def __init__ (self , table : df_internal .catalog .RawTable ) -> None :
145- """This constructor is not typically called by the end user."""
146- self .table = table
153+ Internally we currently support the following types of tables:
154+
155+ - Tables created using built-in DataFusion methods, such as
156+ reading from CSV or Parquet
157+ - pyarrow datasets
158+ - DataFusion DataFrames, which will be converted into a view
159+ - Externally provided tables implemented with the FFI PyCapsule
160+ interface (advanced)
161+ """
162+
163+ __slots__ = ("_inner" ,)
164+
165+ def __init__ (
166+ self , table : Table | TableProviderExportable | DataFrame | pa .dataset .Dataset
167+ ) -> None :
168+ """Constructor."""
169+ self ._inner = df_internal .catalog .RawTable (table )
147170
148171 def __repr__ (self ) -> str :
149172 """Print a string representation of the table."""
150- return self .table . __repr__ ( )
173+ return repr ( self ._inner )
151174
152175 @staticmethod
176+ @deprecated ("Use Table() constructor instead." )
153177 def from_dataset (dataset : pa .dataset .Dataset ) -> Table :
154- """Turn a pyarrow Dataset into a Table."""
155- return Table (df_internal . catalog . RawTable . from_dataset ( dataset ) )
178+ """Turn a :mod:` pyarrow.dataset` `` Dataset`` into a :class:` Table` ."""
179+ return Table (dataset )
156180
157181 @property
158182 def schema (self ) -> pa .Schema :
159183 """Returns the schema associated with this table."""
160- return self .table .schema
184+ return self ._inner .schema
161185
162186 @property
163187 def kind (self ) -> str :
164188 """Returns the kind of table."""
165- return self .table .kind
189+ return self ._inner .kind
166190
167191
168192class CatalogProvider (ABC ):
@@ -219,14 +243,16 @@ def table(self, name: str) -> Table | None:
219243 """Retrieve a specific table from this schema."""
220244 ...
221245
222- def register_table (self , name : str , table : Table ) -> None : # noqa: B027
223- """Add a table from this schema.
246+ def register_table ( # noqa: B027
247+ self , name : str , table : Table | TableProviderExportable | Any
248+ ) -> None :
249+ """Add a table to this schema.
224250
225251 This method is optional. If your schema provides a fixed list of tables, you do
226252 not need to implement this method.
227253 """
228254
229- def deregister_table (self , name , cascade : bool ) -> None : # noqa: B027
255+ def deregister_table (self , name : str , cascade : bool ) -> None : # noqa: B027
230256 """Remove a table from this schema.
231257
232258 This method is optional. If your schema provides a fixed list of tables, you do
0 commit comments