20
20
from __future__ import annotations
21
21
22
22
from abc import ABC , abstractmethod
23
- from typing import TYPE_CHECKING , Protocol
23
+ from typing import TYPE_CHECKING , Any , Protocol
24
24
25
25
import datafusion ._internal as df_internal
26
26
27
27
if TYPE_CHECKING :
28
28
import pyarrow as pa
29
29
30
+ from datafusion import DataFrame
31
+ from datafusion .context import TableProviderExportable
32
+
30
33
try :
31
34
from warnings import deprecated # Python 3.13+
32
35
except ImportError :
@@ -82,7 +85,11 @@ def database(self, name: str = "public") -> Schema:
82
85
"""Returns the database with the given ``name`` from this catalog."""
83
86
return self .schema (name )
84
87
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 :
86
93
"""Register a schema with this catalog."""
87
94
if isinstance (schema , Schema ):
88
95
return self .catalog .register_schema (name , schema ._raw_schema )
@@ -122,10 +129,12 @@ def table(self, name: str) -> Table:
122
129
"""Return the table with the given ``name`` from this schema."""
123
130
return Table (self ._raw_schema .table (name ))
124
131
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."""
129
138
return self ._raw_schema .register_table (name , table )
130
139
131
140
def deregister_table (self , name : str ) -> None :
@@ -139,30 +148,45 @@ class Database(Schema):
139
148
140
149
141
150
class Table :
142
- """DataFusion table."""
151
+ """A DataFusion table.
143
152
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 )
147
170
148
171
def __repr__ (self ) -> str :
149
172
"""Print a string representation of the table."""
150
- return self .table . __repr__ ( )
173
+ return repr ( self ._inner )
151
174
152
175
@staticmethod
176
+ @deprecated ("Use Table() constructor instead." )
153
177
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 )
156
180
157
181
@property
158
182
def schema (self ) -> pa .Schema :
159
183
"""Returns the schema associated with this table."""
160
- return self .table .schema
184
+ return self ._inner .schema
161
185
162
186
@property
163
187
def kind (self ) -> str :
164
188
"""Returns the kind of table."""
165
- return self .table .kind
189
+ return self ._inner .kind
166
190
167
191
168
192
class CatalogProvider (ABC ):
@@ -219,14 +243,16 @@ def table(self, name: str) -> Table | None:
219
243
"""Retrieve a specific table from this schema."""
220
244
...
221
245
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.
224
250
225
251
This method is optional. If your schema provides a fixed list of tables, you do
226
252
not need to implement this method.
227
253
"""
228
254
229
- def deregister_table (self , name , cascade : bool ) -> None : # noqa: B027
255
+ def deregister_table (self , name : str , cascade : bool ) -> None : # noqa: B027
230
256
"""Remove a table from this schema.
231
257
232
258
This method is optional. If your schema provides a fixed list of tables, you do
0 commit comments