Skip to content

Commit 159355d

Browse files
author
Jesse Whitehouse
committed
Update has_table to honor schema selection and allow for catalog select
Closes #115 Signed-off-by: Jesse Whitehouse <[email protected]>
1 parent 1965df5 commit 159355d

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/databricks/sqlalchemy/dialect/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,17 +267,18 @@ def do_rollback(self, dbapi_connection):
267267
# Databricks SQL Does not support transactions
268268
pass
269269

270-
def has_table(self, connection, table_name, schema=None, **kwargs) -> bool:
270+
def has_table(self, connection, table_name, schema=None, catalog=None, **kwargs) -> bool:
271271
"""SQLAlchemy docstrings say dialect providers must implement this method"""
272272

273-
schema = schema or "default"
274-
273+
_schema = schema or self.schema
274+
_catalog = catalog or self.catalog
275+
275276
# DBR >12.x uses underscores in error messages
276277
DBR_LTE_12_NOT_FOUND_STRING = "Table or view not found"
277278
DBR_GT_12_NOT_FOUND_STRING = "TABLE_OR_VIEW_NOT_FOUND"
278279

279280
try:
280-
res = connection.execute(f"DESCRIBE TABLE {table_name}")
281+
res = connection.execute(f"DESCRIBE TABLE {_catalog}.{_schema}.{table_name}")
281282
return True
282283
except DatabaseError as e:
283284
if DBR_GT_12_NOT_FOUND_STRING in str(

tests/e2e/sqlalchemy/test_basic.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,34 @@ def test_get_table_names_smoke_test(samples_engine: Engine):
340340
with samples_engine.connect() as conn:
341341
_names = samples_engine.table_names(schema="nyctaxi", connection=conn)
342342
_names is not None, "get_table_names did not succeed"
343+
344+
def test_has_table_across_schemas(db_engine: Engine, samples_engine: Engine):
345+
"""For this test to pass these conditions must be met:
346+
- Table samples.nyctaxi.trips must exist
347+
- Table samples.tpch.customer must exist
348+
- The `catalog` and `schema` environment variables must be set and valid
349+
"""
350+
351+
with samples_engine.connect() as conn:
352+
353+
# Check for table within schema declared at engine creation time
354+
assert samples_engine.dialect.has_table(connection=conn, table_name="trips")
355+
356+
# Check for table within another schema in the same catalog
357+
assert samples_engine.dialect.has_table(connection=conn, table_name="customer", schema="tpch")
358+
359+
# Check for a table within a different catalog
360+
other_catalog = os.environ.get("catalog")
361+
other_schema = os.environ.get("schema")
362+
363+
# Create a table in a different catalog
364+
with db_engine.connect() as conn:
365+
conn.execute("CREATE TABLE test_has_table (numbers_are_cool INT);")
366+
367+
try:
368+
# Verify with a different engine that this table is not found in the samples catalog
369+
assert not samples_engine.dialect.has_table(connection=conn, table_name="test_has_table")
370+
# Verify with a different engine that this table is found in a separate catalog
371+
assert samples_engine.dialect.has_table(connection=conn, table_name="test_has_table", schema=other_schema, catalog=other_catalog)
372+
finally:
373+
conn.execute("DROP TABLE test_has_table;")

0 commit comments

Comments
 (0)