Skip to content

Commit 1ae2a37

Browse files
authored
refactor(table_loc): return consistent object from catalog.db parsing (#9743)
1 parent e13af72 commit 1ae2a37

File tree

7 files changed

+23
-34
lines changed

7 files changed

+23
-34
lines changed

ibis/backends/bigquery/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -587,19 +587,19 @@ def table(
587587

588588
# Default `catalog` to None unless we've parsed it out of the database/schema kwargs
589589
# Raise if there are path specifications in both the name and as a kwarg
590-
catalog = None if table_loc is None else table_loc.catalog
590+
catalog = table_loc.args["catalog"] # args access will return None, not ''
591591
if table.catalog:
592-
if table_loc is not None and table_loc.catalog:
592+
if table_loc.catalog:
593593
raise com.IbisInputError(
594594
"Cannot specify catalog both in the table name and as an argument"
595595
)
596596
else:
597597
catalog = table.catalog
598598

599599
# Default `db` to None unless we've parsed it out of the database/schema kwargs
600-
db = None if table_loc is None else table_loc.db
600+
db = table_loc.args["db"] # args access will return None, not ''
601601
if table.db:
602-
if table_loc is not None and table_loc.db:
602+
if table_loc.db:
603603
raise com.IbisInputError(
604604
"Cannot specify database both in the table name and as an argument"
605605
)

ibis/backends/duckdb/__init__.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,8 @@ def create_table(
172172
"Don't specify a catalog to enable temp table creation."
173173
)
174174

175-
catalog = self.current_catalog
176-
database = self.current_database
177-
if table_loc is not None:
178-
catalog = table_loc.catalog or catalog
179-
database = table_loc.db or database
175+
catalog = table_loc.catalog or self.current_catalog
176+
database = table_loc.db or self.current_database
180177

181178
if obj is None and schema is None:
182179
raise ValueError("Either `obj` or `schema` must be specified")
@@ -302,10 +299,9 @@ def table(
302299
"""
303300
table_loc = self._warn_and_create_table_loc(database, schema)
304301

305-
catalog, database = None, None
306-
if table_loc is not None:
307-
catalog = table_loc.catalog or None
308-
database = table_loc.db or None
302+
# TODO: set these to better defaults
303+
catalog = table_loc.catalog or None
304+
database = table_loc.db or None
309305

310306
table_schema = self.get_schema(name, catalog=catalog, database=database)
311307
# load geospatial only if geo columns
@@ -1022,11 +1018,8 @@ def list_tables(
10221018
"""
10231019
table_loc = self._warn_and_create_table_loc(database, schema)
10241020

1025-
catalog = self.current_catalog
1026-
database = self.current_database
1027-
if table_loc is not None:
1028-
catalog = table_loc.catalog or catalog
1029-
database = table_loc.db or database
1021+
catalog = table_loc.catalog or self.current_catalog
1022+
database = table_loc.db or self.current_database
10301023

10311024
col = "table_name"
10321025
sql = (

ibis/backends/mssql/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ def list_tables(
489489
catalog, db = self._to_catalog_db_tuple(table_loc)
490490
conditions = []
491491

492-
if table_loc is not None:
492+
if db:
493493
conditions.append(C.table_schema.eq(sge.convert(db)))
494494

495495
sql = (

ibis/backends/mysql/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -343,11 +343,11 @@ def list_tables(
343343

344344
conditions = [TRUE]
345345

346-
if table_loc is not None:
347-
if (sg_cat := table_loc.args["catalog"]) is not None:
348-
sg_cat.args["quoted"] = False
349-
if (sg_db := table_loc.args["db"]) is not None:
350-
sg_db.args["quoted"] = False
346+
if (sg_cat := table_loc.args["catalog"]) is not None:
347+
sg_cat.args["quoted"] = False
348+
if (sg_db := table_loc.args["db"]) is not None:
349+
sg_db.args["quoted"] = False
350+
if table_loc.catalog or table_loc.db:
351351
conditions = [C.table_schema.eq(sge.convert(table_loc.sql(self.name)))]
352352

353353
col = "table_name"

ibis/backends/snowflake/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ def list_tables(
734734
tables_query = "SHOW TABLES"
735735
views_query = "SHOW VIEWS"
736736

737-
if table_loc is not None:
737+
if table_loc.catalog or table_loc.db:
738738
tables_query += f" IN {table_loc}"
739739
views_query += f" IN {table_loc}"
740740

ibis/backends/sql/__init__.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,8 @@ def table(
132132
"""
133133
table_loc = self._warn_and_create_table_loc(database, schema)
134134

135-
catalog, database = None, None
136-
if table_loc is not None:
137-
catalog = table_loc.catalog or None
138-
database = table_loc.db or None
135+
catalog = table_loc.catalog or None
136+
database = table_loc.db or None
139137

140138
table_schema = self.get_schema(name, catalog=catalog, database=database)
141139
return ops.DatabaseTable(
@@ -589,9 +587,6 @@ def _compile_pandas_udf(self, udf_node: ops.ScalarUDF) -> str:
589587
)
590588

591589
def _to_catalog_db_tuple(self, table_loc: sge.Table):
592-
if table_loc is None or table_loc == (None, None):
593-
return None, None
594-
595590
if (sg_cat := table_loc.args["catalog"]) is not None:
596591
sg_cat.args["quoted"] = False
597592
sg_cat = sg_cat.sql(self.name)
@@ -603,7 +598,8 @@ def _to_catalog_db_tuple(self, table_loc: sge.Table):
603598

604599
def _to_sqlglot_table(self, database):
605600
if database is None:
606-
return None
601+
# Create "table" with empty catalog and db
602+
database = sg.exp.Table(catalog=None, db=None)
607603
elif isinstance(database, (list, tuple)):
608604
if len(database) > 2:
609605
raise ValueError(

ibis/backends/trino/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def list_tables(
236236

237237
query = "SHOW TABLES"
238238

239-
if table_loc is not None:
239+
if table_loc.catalog or table_loc.db:
240240
table_loc = table_loc.sql(dialect=self.dialect)
241241
query += f" IN {table_loc}"
242242

0 commit comments

Comments
 (0)