Skip to content

Commit

Permalink
refactor(api): remove schema
Browse files Browse the repository at this point in the history
  • Loading branch information
ncclementi committed Sep 17, 2024
1 parent c6008e8 commit d36a068
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 189 deletions.
51 changes: 9 additions & 42 deletions ibis/backends/bigquery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,9 +582,11 @@ def drop_database(
self.raw_sql(stmt.sql(self.name))

def table(
self, name: str, database: str | None = None, schema: str | None = None
self,
name: str,
database: str | None = None,
) -> ir.Table:
table_loc = self._warn_and_create_table_loc(database, schema)
table_loc = self._to_sqlglot_table(database)
table = sg.parse_one(f"`{name}`", into=sge.Table, read=self.name)

# Bigquery, unlike other backends, had existing support for specifying
Expand Down Expand Up @@ -722,7 +724,6 @@ def insert(
self,
table_name: str,
obj: pd.DataFrame | ir.Table | list | dict,
schema: str | None = None,
database: str | None = None,
overwrite: bool = False,
):
Expand All @@ -734,15 +735,13 @@ def insert(
The name of the table to which data needs will be inserted
obj
The source data or expression to insert
schema
The name of the schema that the table is located in
database
Name of the attached database that the table is located in.
overwrite
If `True` then replace existing contents of table
"""
table_loc = self._warn_and_create_table_loc(database, schema)
table_loc = self._to_sqlglot_table(database)
catalog, db = self._to_catalog_db_tuple(table_loc)
if catalog is None:
catalog = self.current_catalog
Expand Down Expand Up @@ -896,7 +895,6 @@ def list_tables(
self,
like: str | None = None,
database: tuple[str, str] | str | None = None,
schema: str | None = None,
) -> list[str]:
"""List the tables in the database.
Expand Down Expand Up @@ -924,10 +922,8 @@ def list_tables(
To specify a table in a separate BigQuery dataset, you can pass in the
dataset and project as a string `"dataset.project"`, or as a tuple of
strings `(dataset, project)`.
schema
[deprecated] The schema (dataset) inside `database` to perform the list against.
"""
table_loc = self._warn_and_create_table_loc(database, schema)
table_loc = self._to_sqlglot_table(database)

project, dataset = self._parse_project_and_dataset(table_loc)
dataset_ref = bq.DatasetReference(project, dataset)
Expand Down Expand Up @@ -1090,11 +1086,10 @@ def drop_table(
self,
name: str,
*,
schema: str | None = None,
database: tuple[str | str] | str | None = None,
force: bool = False,
) -> None:
table_loc = self._warn_and_create_table_loc(database, schema)
table_loc = self._to_sqlglot_table(database)
catalog, db = self._to_catalog_db_tuple(table_loc)
stmt = sge.Drop(
kind="TABLE",
Expand All @@ -1112,11 +1107,10 @@ def create_view(
name: str,
obj: ir.Table,
*,
schema: str | None = None,
database: str | None = None,
overwrite: bool = False,
) -> ir.Table:
table_loc = self._warn_and_create_table_loc(database, schema)
table_loc = self._to_sqlglot_table(database)
catalog, db = self._to_catalog_db_tuple(table_loc)

stmt = sge.Create(
Expand All @@ -1137,11 +1131,10 @@ def drop_view(
self,
name: str,
*,
schema: str | None = None,
database: str | None = None,
force: bool = False,
) -> None:
table_loc = self._warn_and_create_table_loc(database, schema)
table_loc = self._to_sqlglot_table(database)
catalog, db = self._to_catalog_db_tuple(table_loc)

stmt = sge.Drop(
Expand Down Expand Up @@ -1169,32 +1162,6 @@ def _register_udfs(self, expr: ir.Expr) -> None:
def _safe_raw_sql(self, *args, **kwargs):
yield self.raw_sql(*args, **kwargs)

# TODO: remove when the schema kwarg is removed
def _warn_and_create_table_loc(self, database=None, schema=None):
if schema is not None:
self._warn_schema()
if database is not None and schema is not None:
if isinstance(database, str):
table_loc = f"{database}.{schema}"
elif isinstance(database, tuple):
table_loc = database + schema
elif schema is not None:
table_loc = schema
elif database is not None:
table_loc = database
else:
table_loc = None

table_loc = self._to_sqlglot_table(table_loc)

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

return table_loc


def compile(expr, params=None, **kwargs):
"""Compile an expression for BigQuery."""
Expand Down
10 changes: 5 additions & 5 deletions ibis/backends/datafusion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,8 +674,10 @@ def create_table(
return self.table(name, database=database)

def truncate_table(
self, name: str, database: str | None = None, schema: str | None = None
) -> None:
self,
name: str,
database: str | None = None,
):
"""Delete all rows from a table.
Parameters
Expand All @@ -684,14 +686,12 @@ def truncate_table(
Table name
database
Database name
schema
Schema name
"""
# datafusion doesn't support `TRUNCATE TABLE` so we use `DELETE FROM`
#
# however datafusion as of 34.0.0 doesn't implement DELETE DML yet
table_loc = self._warn_and_create_table_loc(database, schema)
table_loc = self._to_sqlglot_table(database)
catalog, db = self._to_catalog_db_tuple(table_loc)

ident = sg.table(name, db=db, catalog=catalog).sql(self.dialect)
Expand Down
13 changes: 3 additions & 10 deletions ibis/backends/duckdb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,17 +232,13 @@ def create_table(

return self.table(name, database=(catalog, database))

def table(
self, name: str, schema: str | None = None, database: str | None = None
) -> ir.Table:
def table(self, name: str, database: str | None = None) -> ir.Table:
"""Construct a table expression.
Parameters
----------
name
Table name
schema
[deprecated] Schema name
database
Database name
Expand All @@ -252,7 +248,7 @@ def table(
Table expression
"""
table_loc = self._warn_and_create_table_loc(database, schema)
table_loc = self._to_sqlglot_table(database)

# TODO: set these to better defaults
catalog = table_loc.catalog or None
Expand Down Expand Up @@ -987,7 +983,6 @@ def list_tables(
self,
like: str | None = None,
database: tuple[str, str] | str | None = None,
schema: str | None = None,
) -> list[str]:
"""List tables and views.
Expand Down Expand Up @@ -1015,8 +1010,6 @@ def list_tables(
To specify a table in a separate catalog, you can pass in the
catalog and database as a string `"catalog.database"`, or as a tuple of
strings `("catalog", "database")`.
schema
[deprecated] Schema name. If not passed, uses the current schema.
Returns
-------
Expand All @@ -1042,7 +1035,7 @@ def list_tables(
['baz']
"""
table_loc = self._warn_and_create_table_loc(database, schema)
table_loc = self._to_sqlglot_table(database)

catalog = table_loc.catalog or self.current_catalog
database = table_loc.db or self.current_database
Expand Down
6 changes: 2 additions & 4 deletions ibis/backends/mssql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,6 @@ def list_tables(
self,
like: str | None = None,
database: tuple[str, str] | str | None = None,
schema: str | None = None,
) -> list[str]:
"""List the tables in the database.
Expand All @@ -552,10 +551,9 @@ def list_tables(
To specify a table in a separate catalog, you can pass in the
catalog and database as a string `"catalog.database"`, or as a tuple of
strings `("catalog", "database")`.
schema
[deprecated] The schema inside `database` to perform the list against.
"""
table_loc = self._warn_and_create_table_loc(database, schema)
table_loc = self._to_sqlglot_table(database)
catalog, db = self._to_catalog_db_tuple(table_loc)

sql = (
Expand Down
19 changes: 1 addition & 18 deletions ibis/backends/mysql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,6 @@ def raw_sql(self, query: str | sg.Expression, **kwargs: Any) -> Any:
def list_tables(
self,
like: str | None = None,
schema: str | None = None,
database: tuple[str, str] | str | None = None,
) -> list[str]:
"""List the tables in the database.
Expand All @@ -316,27 +315,11 @@ def list_tables(
----------
like
A pattern to use for listing tables.
schema
[deprecated] The schema to perform the list against.
database
Database to list tables from. Default behavior is to show tables in
the current database (`self.current_database`).
"""
if schema is not None:
self._warn_schema()

if schema is not None and database is not None:
raise ValueError(
"Using both the `schema` and `database` kwargs is not supported. "
"`schema` is deprecated and will be removed in Ibis 10.0"
"\nUse the `database` kwarg with one of the following patterns:"
'\ndatabase="database"'
'\ndatabase=("catalog", "database")'
'\ndatabase="catalog.database"',
)
elif schema is not None:
table_loc = schema
elif database is not None:
if database is not None:
table_loc = database
else:
table_loc = self.current_database
Expand Down
19 changes: 2 additions & 17 deletions ibis/backends/oracle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ def raw_sql(self, query: str | sg.Expression, **kwargs: Any) -> Any:
def list_tables(
self,
like: str | None = None,
schema: str | None = None,
database: tuple[str, str] | str | None = None,
) -> list[str]:
"""List the tables in the database.
Expand All @@ -290,31 +289,17 @@ def list_tables(
----------
like
A pattern to use for listing tables.
schema
[deprecated] The schema to perform the list against.
database
Database to list tables from. Default behavior is to show tables in
the current database.
"""
if schema is not None and database is not None:
raise exc.IbisInputError(
"Using both the `schema` and `database` kwargs is not supported. "
"`schema` is deprecated and will be removed in Ibis 10.0"
"\nUse the `database` kwarg with one of the following patterns:"
'\ndatabase="database"'
'\ndatabase=("catalog", "database")'
'\ndatabase="catalog.database"',
)
if schema is not None:
# TODO: remove _warn_schema when the schema kwarg is removed
self._warn_schema()
table_loc = schema
elif database is not None:
if database is not None:
table_loc = database
else:
table_loc = self.con.username.upper()

table_loc = self._to_sqlglot_table(table_loc)

# Deeply frustrating here where if we call `convert` on `table_loc`,
Expand Down
20 changes: 2 additions & 18 deletions ibis/backends/postgres/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ def _session_temp_db(self) -> str | None:
def list_tables(
self,
like: str | None = None,
schema: str | None = None,
database: tuple[str, str] | str | None = None,
) -> list[str]:
"""List the tables in the database.
Expand All @@ -322,27 +321,12 @@ def list_tables(
----------
like
A pattern to use for listing tables.
schema
[deprecated] The schema to perform the list against.
database
Database to list tables from. Default behavior is to show tables in
the current database.
"""
if schema is not None:
self._warn_schema()

if schema is not None and database is not None:
raise ValueError(
"Using both the `schema` and `database` kwargs is not supported. "
"`schema` is deprecated and will be removed in Ibis 10.0"
"\nUse the `database` kwarg with one of the following patterns:"
'\ndatabase="database"'
'\ndatabase=("catalog", "database")'
'\ndatabase="catalog.database"',
)
elif schema is not None:
table_loc = schema
elif database is not None:

if database is not None:
table_loc = database
else:
table_loc = (self.current_catalog, self.current_database)
Expand Down
10 changes: 2 additions & 8 deletions ibis/backends/snowflake/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,6 @@ def list_tables(
self,
like: str | None = None,
database: tuple[str, str] | str | None = None,
schema: str | None = None,
) -> list[str]:
"""List the tables in the database.
Expand All @@ -644,10 +643,8 @@ def list_tables(
To specify a table in a separate Snowflake catalog, you can pass in the
catalog and database as a string `"catalog.database"`, or as a tuple of
strings `("catalog", "database")`.
schema
[deprecated] The schema inside `database` to perform the list against.
"""
table_loc = self._warn_and_create_table_loc(database, schema)
table_loc = self._to_sqlglot_table(database)

tables_query = "SHOW TABLES"
views_query = "SHOW VIEWS"
Expand Down Expand Up @@ -1181,7 +1178,6 @@ def insert(
self,
table_name: str,
obj: pd.DataFrame | ir.Table | list | dict,
schema: str | None = None,
database: str | None = None,
overwrite: bool = False,
) -> None:
Expand All @@ -1202,8 +1198,6 @@ def insert(
The name of the table to which data needs will be inserted
obj
The source data or expression to insert
schema
[deprecated] The name of the schema that the table is located in
database
Name of the attached database that the table is located in.
Expand All @@ -1214,7 +1208,7 @@ def insert(
If `True` then replace existing contents of table
"""
table_loc = self._warn_and_create_table_loc(database, schema)
table_loc = self._to_sqlglot_table(database)
catalog, db = self._to_catalog_db_tuple(table_loc)

if not isinstance(obj, ir.Table):
Expand Down
Loading

0 comments on commit d36a068

Please sign in to comment.