Skip to content

Commit

Permalink
refactor(backend-api): remove the database() API and implementation (
Browse files Browse the repository at this point in the history
…ibis-project#8406)

Remove the `BaseBackend.database()` API. This has been deprecated for a
quite a while. Depends on ibis-project#8405.
  • Loading branch information
cpcloud authored Feb 21, 2024
1 parent 5c0c6aa commit ff5d078
Show file tree
Hide file tree
Showing 8 changed files with 6 additions and 202 deletions.
4 changes: 1 addition & 3 deletions docs/backends/impala.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -1065,9 +1065,7 @@ func.register(fuzzy_equals.name, udf_db)
The object `fuzzy_equals` is callable and works with Ibis expressions:

```python
>>> db = c.database('ibis_testing')

>>> t = db.functional_alltypes
>>> t = con.tables.functional_alltypes

>>> expr = fuzzy_equals(t.float_col, t.double_col / 10)

Expand Down
152 changes: 1 addition & 151 deletions ibis/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,140 +28,7 @@
import sqlglot as sg
import torch

__all__ = ("BaseBackend", "Database", "connect")


class Database:
"""Generic Database class."""

def __init__(self, name: str, client: Any) -> None:
self.name = name
self.client = client

def __repr__(self) -> str:
"""Return type name and the name of the database."""
return f"{type(self).__name__}({self.name!r})"

def __dir__(self) -> list[str]:
"""Return the attributes and tables of the database.
Returns
-------
list[str]
A list of the attributes and tables available in the database.
"""
attrs = dir(type(self))
unqualified_tables = [self._unqualify(x) for x in self.tables]
return sorted(frozenset(attrs + unqualified_tables))

def __contains__(self, table: str) -> bool:
"""Check if the given table is available in the current database.
Parameters
----------
table
Table name
Returns
-------
bool
True if the given table is available in the current database.
"""
return table in self.tables

@property
def tables(self) -> list[str]:
"""Return a list with all available tables.
Returns
-------
list[str]
The list of tables in the database
"""
return self.list_tables()

def __getitem__(self, table: str) -> ir.Table:
"""Return a Table for the given table name.
Parameters
----------
table
Table name
Returns
-------
Table
Table expression
"""
return self.table(table)

def __getattr__(self, table: str) -> ir.Table:
"""Return a Table for the given table name.
Parameters
----------
table
Table name
Returns
-------
Table
Table expression
"""
return self.table(table)

def _qualify(self, value):
return value

def _unqualify(self, value):
return value

def drop(self, force: bool = False) -> None:
"""Drop the database.
Parameters
----------
force
If `True`, drop any objects that exist, and do not fail if the
database does not exist.
"""
self.client.drop_database(self.name, force=force)

def table(self, name: str) -> ir.Table:
"""Return a table expression referencing a table in this database.
Parameters
----------
name
The name of a table
Returns
-------
Table
Table expression
"""
qualified_name = self._qualify(name)
return self.client.table(qualified_name, self.name)

def list_tables(self, like=None, database=None):
"""List the tables in the database.
Parameters
----------
like
A pattern to use for listing tables.
database
The database to perform the list against
"""
return self.client.list_tables(like, database=database or self.name)
__all__ = ("BaseBackend", "connect")


class TablesAccessor(collections.abc.Mapping):
Expand Down Expand Up @@ -875,23 +742,6 @@ def reconnect(self) -> None:
def do_connect(self, *args, **kwargs) -> None:
"""Connect to database specified by `args` and `kwargs`."""

@util.deprecated(instead="use equivalent methods in the backend")
def database(self, name: str | None = None) -> Database:
"""Return a `Database` object for the `name` database.
Parameters
----------
name
Name of the database to return the object for.
Returns
-------
Database
A database object for the specified database.
"""
return Database(name=name or self.current_database, client=self)

@staticmethod
def _filter_with_like(values: Iterable[str], like: str | None = None) -> list[str]:
"""Filter names with a `like` pattern (regex).
Expand Down
11 changes: 1 addition & 10 deletions ibis/backends/bigquery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import ibis.expr.operations as ops
import ibis.expr.types as ir
from ibis import util
from ibis.backends import CanCreateSchema, Database
from ibis.backends import CanCreateSchema
from ibis.backends.bigquery.client import (
BigQueryCursor,
bigquery_param,
Expand Down Expand Up @@ -674,15 +674,6 @@ def current_database(self) -> str:
def current_schema(self) -> str | None:
return self.dataset

def database(self, name=None):
if name is None and not self.dataset:
raise ValueError(
"Unable to determine BigQuery dataset. Call "
"client.database('my_dataset') or set_database('my_dataset') "
"to assign your client a dataset."
)
return Database(name or self.dataset, self)

def compile(
self, expr: ir.Expr, limit: str | None = None, params=None, **kwargs: Any
):
Expand Down
5 changes: 0 additions & 5 deletions ibis/backends/clickhouse/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,6 @@ def con(tmp_path_factory, data_dir, worker_id):
return TestConf.load_data(data_dir, tmp_path_factory, worker_id).connection


@pytest.fixture(scope="session")
def db(con):
return con.database()


@pytest.fixture(scope="session")
def alltypes(con):
return con.tables.functional_alltypes
Expand Down
5 changes: 0 additions & 5 deletions ibis/backends/impala/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,6 @@ def test_sql_query_limits(con, test_data_db):
assert table.count().execute(limit=10) == 25


def test_database_default_current_database(con):
db = con.database()
assert db.name == con.current_database


def test_set_compression_codec(con):
old_opts = con.get_options()
assert old_opts["COMPRESSION_CODEC"].upper() in ("NONE", "")
Expand Down
5 changes: 1 addition & 4 deletions ibis/backends/polars/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import ibis.expr.operations as ops
import ibis.expr.schema as sch
import ibis.expr.types as ir
from ibis.backends import BaseBackend, Database, NoUrl
from ibis.backends import BaseBackend, NoUrl
from ibis.backends.pandas.rewrites import (
bind_unbound_table,
replace_parameter,
Expand Down Expand Up @@ -331,9 +331,6 @@ def read_parquet(

return self.table(table_name)

def database(self, name=None):
return Database(name, self)

def create_table(
self,
name: str,
Expand Down
17 changes: 0 additions & 17 deletions ibis/backends/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,23 +413,6 @@ def test_create_drop_view(ddl_con, temp_view):
assert set(t_expr.schema().names) == set(v_expr.schema().names)


@mark.notimpl(["postgres", "risingwave", "polars"])
@mark.notimpl(
["datafusion"],
raises=NotImplementedError,
reason="doesn't seem to have a stateful notion of 'current database'",
)
def test_separate_database(ddl_con, alternate_current_database):
current_data_db = ddl_con.current_database
# using alternate_current_database switches "con" current
# database to a temporary one until a test is over
tmp_db = ddl_con.database(alternate_current_database)
# verifying we can open another db which isn't equal to current
db = ddl_con.database(current_data_db)
assert db.name == current_data_db
assert tmp_db.name == alternate_current_database


@pytest.fixture
def employee_empty_temp_table(backend, con, test_employee_schema):
temp_table_name = gen_name("temp_employee_empty_table")
Expand Down
9 changes: 2 additions & 7 deletions ibis/backends/trino/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,8 @@ def con(tmp_path_factory, data_dir, worker_id):


@pytest.fixture(scope="module")
def db(con):
return con.database()


@pytest.fixture(scope="module")
def alltypes(db):
return db.functional_alltypes
def alltypes(con):
return con.tableds.functional_alltypes


@pytest.fixture(scope="module")
Expand Down

0 comments on commit ff5d078

Please sign in to comment.