|
38 | 38 |
|
39 | 39 | __all__ = [ |
40 | 40 | "Catalog", |
| 41 | + "CatalogList", |
41 | 42 | "CatalogProvider", |
| 43 | + "CatalogProviderList", |
42 | 44 | "Schema", |
43 | 45 | "SchemaProvider", |
44 | 46 | "Table", |
45 | 47 | ] |
46 | 48 |
|
47 | 49 |
|
| 50 | +class CatalogList: |
| 51 | + """DataFusion data catalog list.""" |
| 52 | + |
| 53 | + def __init__(self, catalog_list: df_internal.catalog.RawCatalogList) -> None: |
| 54 | + """This constructor is not typically called by the end user.""" |
| 55 | + self.catalog_list = catalog_list |
| 56 | + |
| 57 | + def __repr__(self) -> str: |
| 58 | + """Print a string representation of the catalog list.""" |
| 59 | + return self.catalog_list.__repr__() |
| 60 | + |
| 61 | + def names(self) -> set[str]: |
| 62 | + """This is an alias for `catalog_names`.""" |
| 63 | + return self.catalog_names() |
| 64 | + |
| 65 | + def catalog_names(self) -> set[str]: |
| 66 | + """Returns the list of schemas in this catalog.""" |
| 67 | + return self.catalog_list.catalog_names() |
| 68 | + |
| 69 | + @staticmethod |
| 70 | + def memory_catalog(ctx: SessionContext | None = None) -> CatalogList: |
| 71 | + """Create an in-memory catalog provider list.""" |
| 72 | + catalog_list = df_internal.catalog.RawCatalogList.memory_catalog(ctx) |
| 73 | + return CatalogList(catalog_list) |
| 74 | + |
| 75 | + def catalog(self, name: str = "datafusion") -> Schema: |
| 76 | + """Returns the catalog with the given ``name`` from this catalog.""" |
| 77 | + catalog = self.catalog_list.catalog(name) |
| 78 | + |
| 79 | + return ( |
| 80 | + Catalog(catalog) |
| 81 | + if isinstance(catalog, df_internal.catalog.RawCatalog) |
| 82 | + else catalog |
| 83 | + ) |
| 84 | + |
| 85 | + def register_catalog( |
| 86 | + self, |
| 87 | + name: str, |
| 88 | + catalog: Catalog | CatalogProvider | CatalogProviderExportable, |
| 89 | + ) -> Catalog | None: |
| 90 | + """Register a catalog with this catalog list.""" |
| 91 | + if isinstance(catalog, Catalog): |
| 92 | + return self.catalog_list.register_catalog(name, catalog.catalog) |
| 93 | + return self.catalog_list.register_catalog(name, catalog) |
| 94 | + |
| 95 | + |
48 | 96 | class Catalog: |
49 | 97 | """DataFusion data catalog.""" |
50 | 98 |
|
@@ -195,6 +243,38 @@ def kind(self) -> str: |
195 | 243 | return self._inner.kind |
196 | 244 |
|
197 | 245 |
|
| 246 | +class CatalogProviderList(ABC): |
| 247 | + """Abstract class for defining a Python based Catalog Provider List.""" |
| 248 | + |
| 249 | + @abstractmethod |
| 250 | + def catalog_names(self) -> set[str]: |
| 251 | + """Set of the names of all catalogs in this catalog list.""" |
| 252 | + ... |
| 253 | + |
| 254 | + @abstractmethod |
| 255 | + def catalog(self, name: str) -> Catalog | None: |
| 256 | + """Retrieve a specific catalog from this catalog list.""" |
| 257 | + ... |
| 258 | + |
| 259 | + def register_catalog( # noqa: B027 |
| 260 | + self, name: str, catalog: CatalogProviderExportable | CatalogProvider | Catalog |
| 261 | + ) -> None: |
| 262 | + """Add a catalog to this catalog list. |
| 263 | +
|
| 264 | + This method is optional. If your catalog provides a fixed list of catalogs, you |
| 265 | + do not need to implement this method. |
| 266 | + """ |
| 267 | + |
| 268 | + |
| 269 | +class CatalogProviderListExportable(Protocol): |
| 270 | + """Type hint for object that has __datafusion_catalog_provider_list__ PyCapsule. |
| 271 | +
|
| 272 | + https://docs.rs/datafusion/latest/datafusion/catalog/trait.CatalogProviderList.html |
| 273 | + """ |
| 274 | + |
| 275 | + def __datafusion_catalog_provider_list__(self, session: Any) -> object: ... |
| 276 | + |
| 277 | + |
198 | 278 | class CatalogProvider(ABC): |
199 | 279 | """Abstract class for defining a Python based Catalog Provider.""" |
200 | 280 |
|
@@ -229,6 +309,15 @@ def deregister_schema(self, name: str, cascade: bool) -> None: # noqa: B027 |
229 | 309 | """ |
230 | 310 |
|
231 | 311 |
|
| 312 | +class CatalogProviderExportable(Protocol): |
| 313 | + """Type hint for object that has __datafusion_catalog_provider__ PyCapsule. |
| 314 | +
|
| 315 | + https://docs.rs/datafusion/latest/datafusion/catalog/trait.CatalogProvider.html |
| 316 | + """ |
| 317 | + |
| 318 | + def __datafusion_catalog_provider__(self, session: Any) -> object: ... |
| 319 | + |
| 320 | + |
232 | 321 | class SchemaProvider(ABC): |
233 | 322 | """Abstract class for defining a Python based Schema Provider.""" |
234 | 323 |
|
|
0 commit comments