Skip to content

Commit e4725d7

Browse files
committed
Migrate daft.catalog top-level functions
1 parent cd8838d commit e4725d7

File tree

3 files changed

+80
-33
lines changed

3 files changed

+80
-33
lines changed

daft/catalog/__init__.py

+72-17
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88
99
**Catalog**
1010
11-
Daft recognizes a default catalog which it will attempt to use when no specific catalog name is provided.
12-
1311
```python
14-
# This will hit the default catalog
15-
daft.read_table("my_db.my_namespace.my_table")
12+
# without any qualifiers, the default catalog and namespace are used.
13+
daft.read_table("my_table")
14+
15+
# with a qualified identifier (uses default catalog)
16+
daft.read_table("my_namespace.my_table")
17+
18+
# with a fully qualified identifier
19+
daft.read_table("my_catalog.my_namespace.my_table")
1620
```
1721
1822
**Named Tables**
@@ -24,11 +28,8 @@
2428
```python
2529
df = daft.from_pydict({"foo": [1, 2, 3]})
2630
27-
# TODO deprecated catalog APIs #3819
28-
daft.catalog.register_table(
29-
"my_table",
30-
df,
31-
)
31+
# Attach the DataFrame for use across other APIs.
32+
daft.attach_table(df, "my_table")
3233
3334
# Your table is now accessible from Daft-SQL, or Daft's `read_table`
3435
df1 = daft.read_table("my_table")
@@ -42,10 +43,7 @@
4243

4344
from abc import ABC, abstractmethod
4445
from collections.abc import Sequence
45-
from daft.daft import catalog as native_catalog
4646
from daft.daft import PyIdentifier, PyTable
47-
from daft.logical.builder import LogicalPlanBuilder
48-
4947
from daft.dataframe import DataFrame
5048

5149
from typing import TYPE_CHECKING
@@ -65,14 +63,46 @@
6563
"unregister_catalog",
6664
]
6765

66+
6867
# TODO deprecated catalog APIs #3819
69-
unregister_catalog = native_catalog.unregister_catalog
68+
def unregister_catalog(catalog_name: str | None) -> bool:
69+
"""Unregisters a catalog from the Daft catalog system.
70+
71+
DEPRECATED: This is deprecated and will be removed in daft >= 0.5.0; please use `daft.detach_catalog`.
72+
73+
This function removes a previously registered catalog from the Daft catalog system.
74+
75+
Args:
76+
catalog_name (Optional[str]): The name of the catalog to unregister. If None, the default catalog will be unregistered.
77+
78+
Returns:
79+
bool: True if a catalog was successfully unregistered, False otherwise.
80+
81+
Example:
82+
>>> import daft
83+
>>> daft.unregister_catalog("my_catalog")
84+
True
85+
"""
86+
from daft.session import detach_catalog
87+
88+
warnings.warn(
89+
"This is deprecated and will be removed in daft >= 0.5.0; please use `daft.detach_catalog`.",
90+
category=DeprecationWarning,
91+
)
92+
try:
93+
alias = catalog_name if catalog_name else "default"
94+
detach_catalog(alias)
95+
return True
96+
except Exception:
97+
return False
7098

7199

72100
# TODO deprecated catalog APIs #3819
73101
def read_table(name: str) -> DataFrame:
74102
"""Finds a table with the specified name and reads it as a DataFrame.
75103
104+
DEPRECATED: This is deprecated and will be removed in daft >= 0.5.0; please use `daft.read_table`.
105+
76106
The provided name can be any of the following, and Daft will return them with the following order of priority:
77107
78108
1. Name of a registered dataframe/SQL view (manually registered using `daft.register_table`): `"my_registered_table"`
@@ -85,14 +115,21 @@ def read_table(name: str) -> DataFrame:
85115
Returns:
86116
A DataFrame containing the data from the specified table.
87117
"""
88-
native_logical_plan_builder = native_catalog.read_table(name)
89-
return DataFrame(LogicalPlanBuilder(native_logical_plan_builder))
118+
from daft.session import read_table
119+
120+
warnings.warn(
121+
"This is deprecated and will be removed in daft >= 0.5.0; please use `daft.read_table`.",
122+
category=DeprecationWarning,
123+
)
124+
return read_table(name)
90125

91126

92127
# TODO deprecated catalog APIs #3819
93128
def register_table(name: str, dataframe: DataFrame) -> str:
94129
"""Register a DataFrame as a named table.
95130
131+
DEPRECATED: This is deprecated and will be removed in daft >= 0.5.0; please use `daft.attach_table`.
132+
96133
This function registers a DataFrame as a named table, making it accessible
97134
via Daft-SQL or Daft's `read_table` function.
98135
@@ -108,13 +145,22 @@ def register_table(name: str, dataframe: DataFrame) -> str:
108145
>>> daft.catalog.register_table("my_table", df)
109146
>>> daft.read_table("my_table")
110147
"""
111-
return native_catalog.register_table(name, dataframe._builder._builder)
148+
from daft.session import attach_table
149+
150+
warnings.warn(
151+
"This is deprecated and will be removed in daft >= 0.5.0; please use `daft.attach_table`.",
152+
category=DeprecationWarning,
153+
)
154+
_ = attach_table(dataframe, name)
155+
return name
112156

113157

114158
# TODO deprecated catalog APIs #3819
115159
def register_python_catalog(catalog: object, name: str | None = None) -> str:
116160
"""Registers a Python catalog with Daft.
117161
162+
DEPRECATED: This is deprecated and will be removed in daft >= 0.5.0; please use `daft.attach_catalog`.
163+
118164
Currently supports:
119165
120166
* [PyIceberg Catalogs](https://py.iceberg.apache.org/api/)
@@ -136,7 +182,16 @@ def register_python_catalog(catalog: object, name: str | None = None) -> str:
136182
>>> daft.catalog.register_python_catalog(catalog, "my_daft_catalog")
137183
138184
"""
139-
return native_catalog.register_python_catalog(Catalog._from_obj(catalog), name)
185+
from daft.session import attach_catalog
186+
187+
warnings.warn(
188+
"This is deprecated and will be removed in daft >= 0.5.0; please use `daft.attach_catalog`.",
189+
category=DeprecationWarning,
190+
)
191+
if name is None:
192+
raise ValueError("implicit catalog aliases are not supported")
193+
_ = attach_catalog(catalog, name)
194+
return name
140195

141196

142197
class Catalog(ABC):

daft/daft/catalog.pyi

-12
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
from typing import TYPE_CHECKING
2-
3-
from daft.daft import LogicalPlanBuilder as PyLogicalPlanBuilder
4-
5-
if TYPE_CHECKING:
6-
from daft.catalog.python_catalog import PythonCatalog
7-
81
class PyIdentifier:
92
def __init__(self, namespace: tuple[str, ...], name: str): ...
103
@staticmethod
@@ -13,8 +6,3 @@ class PyIdentifier:
136
def getitem(self, index: int) -> str: ...
147
def __len__(self) -> int: ...
158
def __repr__(self) -> str: ...
16-
17-
def read_table(name: str) -> PyLogicalPlanBuilder: ...
18-
def register_table(name: str, plan_builder: PyLogicalPlanBuilder) -> str: ...
19-
def register_python_catalog(catalog: PythonCatalog, catalog_name: str | None) -> str: ...
20-
def unregister_catalog(catalog_name: str | None) -> bool: ...

daft/session.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,17 @@ def _from_env() -> Session:
6565
# attach & detach
6666
###
6767

68-
def attach_catalog(self, catalog: object | Catalog, alias: str) -> Catalog:
68+
def attach_catalog(self, catalog: object | Catalog, alias: str | None = None) -> Catalog:
6969
"""Attaches an external catalog to this session."""
70+
if alias is None:
71+
raise ValueError("implicit catalog aliases are not yet supported")
7072
c = catalog if isinstance(catalog, Catalog) else Catalog._from_obj(catalog)
7173
return self._session.attach_catalog(c, alias)
7274

73-
def attach_table(self, table: object | Table, alias: str) -> Table:
75+
def attach_table(self, table: object | Table, alias: str | None = None) -> Table:
7476
"""Attaches an external table to this session."""
77+
if alias is None:
78+
raise ValueError("implicit table aliases are not yet supported")
7579
t = table if isinstance(table, Table) else Table._from_obj(table)
7680
return self._session.attach_table(t, alias)
7781

@@ -174,12 +178,12 @@ def _session() -> Session:
174178
###
175179

176180

177-
def attach_catalog(catalog: object | Catalog, alias: str) -> Catalog:
181+
def attach_catalog(catalog: object | Catalog, alias: str | None = None) -> Catalog:
178182
"""Attaches an external catalog to the current session."""
179183
return _session().attach_catalog(catalog, alias)
180184

181185

182-
def attach_table(table: object | Table, alias: str) -> Table:
186+
def attach_table(table: object | Table, alias: str | None = None) -> Table:
183187
"""Attaches an external table to the current session."""
184188
return _session().attach_table(table, alias)
185189

0 commit comments

Comments
 (0)