Skip to content

Commit

Permalink
fix(oracle): implement current_catalog and current_database correctly (
Browse files Browse the repository at this point in the history
…#9918)

Fixes some hacks around `current_catalog` and `current_database` for the
Oracle backend
  • Loading branch information
cpcloud authored Aug 26, 2024
1 parent 9da3c9f commit 4fdb707
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 41 deletions.
10 changes: 0 additions & 10 deletions ibis/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,11 +614,6 @@ def list_catalogs(self, like: str | None = None) -> list[str]:
"""

@property
@abc.abstractmethod
def current_catalog(self) -> str:
"""The current catalog in use."""


class CanCreateCatalog(CanListCatalog):
@abc.abstractmethod
Expand Down Expand Up @@ -705,11 +700,6 @@ def list_databases(
"""

@property
@abc.abstractmethod
def current_database(self) -> str:
"""The current database in use."""


class CanCreateDatabase(CanListDatabase):
@abc.abstractmethod
Expand Down
8 changes: 0 additions & 8 deletions ibis/backends/datafusion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,6 @@ def raw_sql(self, query: str | sge.Expression) -> Any:
self._log(query)
return self.con.sql(query)

@property
def current_catalog(self) -> str:
raise NotImplementedError()

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

def list_catalogs(self, like: str | None = None) -> list[str]:
code = (
sg.select(C.table_catalog)
Expand Down
10 changes: 9 additions & 1 deletion ibis/backends/oracle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,16 @@ def _from_url(self, url: ParseResult, **kwargs):
return self

@property
def current_database(self) -> str:
def current_catalog(self) -> str:
with self._safe_raw_sql(sg.select(STAR).from_("global_name")) as cur:
[(catalog,)] = cur.fetchall()
return catalog

@property
def current_database(self) -> str:
# databases correspond to users, other than that there's
# no notion of a database inside a catalog for oracle
with self._safe_raw_sql(sg.select("user").from_("dual")) as cur:
[(database,)] = cur.fetchall()
return database

Expand Down
6 changes: 1 addition & 5 deletions ibis/backends/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def test_version(backend):
"clickhouse",
"sqlite",
"dask",
"datafusion",
"exasol",
"pandas",
"druid",
Expand All @@ -37,11 +38,6 @@ def test_version(backend):
reason="backend does not support catalogs",
raises=AttributeError,
)
@pytest.mark.notimpl(
["datafusion"],
raises=NotImplementedError,
reason="current_catalog isn't implemented",
)
@pytest.mark.xfail_version(pyspark=["pyspark<3.4"])
def test_catalog_consistency(backend, con):
catalogs = con.list_catalogs()
Expand Down
23 changes: 6 additions & 17 deletions ibis/backends/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1753,24 +1753,13 @@ def test_cross_database_join(con_create_database, monkeypatch):
["impala", "pyspark", "trino"], reason="Default constraints are not supported"
)
def test_insert_into_table_missing_columns(con, temp_table):
try:
db = getattr(con, "current_database", None)
except NotImplementedError:
db = None

# UGH
if con.name == "oracle":
db = None

try:
catalog = getattr(con, "current_catalog", None)
except NotImplementedError:
catalog = None
db = getattr(con, "current_database", None)

raw_ident = ".".join(
sg.to_identifier(i, quoted=True).sql("duckdb")
for i in filter(None, (catalog, db, temp_table))
)
raw_ident = sg.table(
temp_table,
db=db if db is None else sg.to_identifier(db, quoted=True),
quoted=True,
).sql("duckdb")

ct_sql = f'CREATE TABLE {raw_ident} ("a" INT DEFAULT 1, "b" INT)'
sg_expr = sg.parse_one(ct_sql, read="duckdb")
Expand Down

0 comments on commit 4fdb707

Please sign in to comment.