Skip to content

Commit

Permalink
fix(backends): make current_database implementation and API consist…
Browse files Browse the repository at this point in the history
…ent across all backends
  • Loading branch information
cpcloud authored and gforsyth committed Aug 2, 2023
1 parent 9f5b8fd commit eeeeee0
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 14 deletions.
3 changes: 2 additions & 1 deletion ibis/backends/base/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,8 @@ def current_database(self) -> str | None:
Returns
-------
str | None
Name of the current database.
Name of the current database or `None` if the backend supports the
concept of a database but doesn't support multiple databases.
"""

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion ibis/backends/base/sql/alchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ def schema(self, name: str) -> sch.Schema:
return self.database().schema(name)

@property
def current_database(self) -> str:
def current_database(self) -> str | None:
"""The name of the current database this client is connected to."""
return self.database_name

Expand Down
2 changes: 1 addition & 1 deletion ibis/backends/bigquery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ def raw_sql(self, query: str, results=False, params=None):
return self._execute(query, results=results, query_parameters=query_parameters)

@property
def current_database(self) -> str:
def current_database(self) -> str | None:
return self.dataset

def database(self, name=None):
Expand Down
6 changes: 4 additions & 2 deletions ibis/backends/clickhouse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,10 @@ def version(self) -> str:
return self.con.server_version

@property
def current_database(self) -> str:
return self.con.database
def current_database(self) -> str | None:
with closing(self.raw_sql("SELECT currentDatabase()")) as result:
[(db,)] = result.result_rows
return db

def list_databases(self, like: str | None = None) -> list[str]:
with closing(self.raw_sql("SELECT name FROM system.databases")) as result:
Expand Down
2 changes: 1 addition & 1 deletion ibis/backends/datafusion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def do_connect(
self.register(path, table_name=name)

@property
def current_database(self) -> str:
def current_database(self) -> str | None:
raise NotImplementedError()

def list_databases(self, like: str | None = None) -> list[str]:
Expand Down
2 changes: 1 addition & 1 deletion ibis/backends/duckdb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Backend(BaseAlchemyBackend, CanCreateSchema):
supports_create_or_replace = True

@property
def current_database(self) -> str:
def current_database(self) -> str | None:
query = sa.select(sa.func.current_database())
with self.begin() as con:
return con.execute(query).scalar()
Expand Down
2 changes: 1 addition & 1 deletion ibis/backends/impala/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ def _get_list(self, cur):
return list(map(operator.itemgetter(0), tuples))

@property
def current_database(self):
def current_database(self) -> str | None:
# XXX The parent `Client` has a generic method that calls this same
# method in the backend. But for whatever reason calling this code from
# that method doesn't seem to work. Maybe `con` is a copy?
Expand Down
2 changes: 1 addition & 1 deletion ibis/backends/pandas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def version(self) -> str:
return pd.__version__

@property
def current_database(self):
def current_database(self) -> str | None:
raise NotImplementedError('pandas backend does not support databases')

def list_tables(self, like=None, database=None):
Expand Down
3 changes: 2 additions & 1 deletion ibis/backends/polars/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ def do_connect(
def version(self) -> str:
return pl.__version__

def current_database(self):
@property
def current_database(self) -> str | None:
raise NotImplementedError('polars backend does not support databases')

def list_tables(self, like=None, database=None):
Expand Down
2 changes: 1 addition & 1 deletion ibis/backends/pyspark/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def version(self):
return pyspark.__version__

@property
def current_database(self):
def current_database(self) -> str | None:
return self._catalog.currentDatabase()

def list_databases(self, like=None):
Expand Down
9 changes: 6 additions & 3 deletions ibis/backends/trino/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@ class Backend(BaseAlchemyBackend, AlchemyCanCreateSchema):
supports_create_or_replace = False
supports_temporary_tables = False

def current_database(self) -> str:
raise NotImplementedError(type(self))

@cached_property
def version(self) -> str:
with self.begin() as con:
return con.execute(sa.select(sa.func.version())).scalar()

@property
def current_database(self) -> str | None:
query = sa.select(sa.literal_column("current_catalog"))
with self.begin() as con:
return con.execute(query).scalar()

def do_connect(
self,
user: str = "user",
Expand Down

0 comments on commit eeeeee0

Please sign in to comment.