Skip to content

Commit

Permalink
refactor(trino): support better cross-db/cross-schema table list
Browse files Browse the repository at this point in the history
`database` (project) and `schema` (dataset) should be specified as
separate keyword arguments (or as a dotted string passed to `schema`.
Specifying only `database` is now deprecated).
  • Loading branch information
gforsyth authored and cpcloud committed Oct 22, 2023
1 parent 95be62f commit d2cf1c9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
39 changes: 38 additions & 1 deletion ibis/backends/trino/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import pandas as pd
import sqlalchemy as sa
import sqlglot as sg
import toolz
from trino.sqlalchemy.datatype import ROW as _ROW
from trino.sqlalchemy.dialect import TrinoDialect
Expand Down Expand Up @@ -73,10 +74,46 @@ def current_schema(self) -> str:
return self._scalar_query(sa.select(sa.literal_column("current_schema")))

def list_tables(
self, like: str | None = None, database: str | None = None
self,
like: str | None = None,
database: str | None = None,
schema: str | None = None,
) -> list[str]:
"""List the tables in the database.
Parameters
----------
like
A pattern to use for listing tables.
database
The database (catalog) to perform the list against.
schema
The schema inside `database` to perform the list against.
::: {.callout-warning}
## `schema` refers to database hierarchy
The `schema` parameter does **not** refer to the column names and
types of `table`.
:::
"""
query = "SHOW TABLES"

if database is not None and schema is None:
util.warn_deprecated(
"database",
instead=(
f"{self.name} cannot list tables only using `database` specifier. "
"Include a `schema` argument."
),
as_of="7.1",
removed_in="8.0",
)
database = sg.parse_one(database, into=sg.exp.Table).sql(dialect=self.name)
elif database is None and schema is not None:
database = sg.parse_one(schema, into=sg.exp.Table).sql(dialect=self.name)
else:
database = sg.table(schema, db=database).sql(dialect=self.name) or None
if database is not None:
query += f" IN {database}"

Expand Down
2 changes: 1 addition & 1 deletion ibis/backends/trino/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def load_tpch(self) -> None:
data_schema = self._tpch_data_schema
database, schema = query_schema.split(".")

tables = con.list_tables(database=self._tpch_data_schema)
tables = con.list_tables(schema=self._tpch_data_schema)
con.create_schema(schema, database=database, force=True)

prefixes = {"partsupp": "ps"}
Expand Down

0 comments on commit d2cf1c9

Please sign in to comment.