Skip to content

Commit

Permalink
refactor(table_loc): return consistent object from catalog.db parsing (
Browse files Browse the repository at this point in the history
  • Loading branch information
gforsyth authored Aug 1, 2024
1 parent e13af72 commit 1ae2a37
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 34 deletions.
8 changes: 4 additions & 4 deletions ibis/backends/bigquery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,19 +587,19 @@ def table(

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

# Default `db` to None unless we've parsed it out of the database/schema kwargs
db = None if table_loc is None else table_loc.db
db = table_loc.args["db"] # args access will return None, not ''
if table.db:
if table_loc is not None and table_loc.db:
if table_loc.db:
raise com.IbisInputError(
"Cannot specify database both in the table name and as an argument"
)
Expand Down
21 changes: 7 additions & 14 deletions ibis/backends/duckdb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,8 @@ def create_table(
"Don't specify a catalog to enable temp table creation."
)

catalog = self.current_catalog
database = self.current_database
if table_loc is not None:
catalog = table_loc.catalog or catalog
database = table_loc.db or database
catalog = table_loc.catalog or self.current_catalog
database = table_loc.db or self.current_database

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

catalog, database = None, None
if table_loc is not None:
catalog = table_loc.catalog or None
database = table_loc.db or None
# TODO: set these to better defaults
catalog = table_loc.catalog or None
database = table_loc.db or None

table_schema = self.get_schema(name, catalog=catalog, database=database)
# load geospatial only if geo columns
Expand Down Expand Up @@ -1022,11 +1018,8 @@ def list_tables(
"""
table_loc = self._warn_and_create_table_loc(database, schema)

catalog = self.current_catalog
database = self.current_database
if table_loc is not None:
catalog = table_loc.catalog or catalog
database = table_loc.db or database
catalog = table_loc.catalog or self.current_catalog
database = table_loc.db or self.current_database

col = "table_name"
sql = (
Expand Down
2 changes: 1 addition & 1 deletion ibis/backends/mssql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ def list_tables(
catalog, db = self._to_catalog_db_tuple(table_loc)
conditions = []

if table_loc is not None:
if db:
conditions.append(C.table_schema.eq(sge.convert(db)))

sql = (
Expand Down
10 changes: 5 additions & 5 deletions ibis/backends/mysql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,11 @@ def list_tables(

conditions = [TRUE]

if table_loc is not None:
if (sg_cat := table_loc.args["catalog"]) is not None:
sg_cat.args["quoted"] = False
if (sg_db := table_loc.args["db"]) is not None:
sg_db.args["quoted"] = False
if (sg_cat := table_loc.args["catalog"]) is not None:
sg_cat.args["quoted"] = False
if (sg_db := table_loc.args["db"]) is not None:
sg_db.args["quoted"] = False
if table_loc.catalog or table_loc.db:
conditions = [C.table_schema.eq(sge.convert(table_loc.sql(self.name)))]

col = "table_name"
Expand Down
2 changes: 1 addition & 1 deletion ibis/backends/snowflake/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ def list_tables(
tables_query = "SHOW TABLES"
views_query = "SHOW VIEWS"

if table_loc is not None:
if table_loc.catalog or table_loc.db:
tables_query += f" IN {table_loc}"
views_query += f" IN {table_loc}"

Expand Down
12 changes: 4 additions & 8 deletions ibis/backends/sql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,8 @@ def table(
"""
table_loc = self._warn_and_create_table_loc(database, schema)

catalog, database = None, None
if table_loc is not None:
catalog = table_loc.catalog or None
database = table_loc.db or None
catalog = table_loc.catalog or None
database = table_loc.db or None

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

def _to_catalog_db_tuple(self, table_loc: sge.Table):
if table_loc is None or table_loc == (None, None):
return None, None

if (sg_cat := table_loc.args["catalog"]) is not None:
sg_cat.args["quoted"] = False
sg_cat = sg_cat.sql(self.name)
Expand All @@ -603,7 +598,8 @@ def _to_catalog_db_tuple(self, table_loc: sge.Table):

def _to_sqlglot_table(self, database):
if database is None:
return None
# Create "table" with empty catalog and db
database = sg.exp.Table(catalog=None, db=None)
elif isinstance(database, (list, tuple)):
if len(database) > 2:
raise ValueError(
Expand Down
2 changes: 1 addition & 1 deletion ibis/backends/trino/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def list_tables(

query = "SHOW TABLES"

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

Expand Down

0 comments on commit 1ae2a37

Please sign in to comment.