Skip to content

Commit

Permalink
feat(snowflake): support 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 d2cf1c9 commit 2071897
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions ibis/backends/snowflake/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import pyarrow as pa
import sqlalchemy as sa
import sqlglot as sg
from packaging.version import parse as vparse
from sqlalchemy.ext.compiler import compiles

Expand Down Expand Up @@ -472,6 +473,58 @@ def list_schemas(

return self._filter_with_like(schemata, like)

def list_tables(
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, quoted=True).sql(dialect=self.name)
or None
)
if database is not None:
query += f" IN {database}"

with self.begin() as con:
tables = [row["name"] for row in con.exec_driver_sql(query).mappings()]

return self._filter_with_like(tables, like=like)

def _register_in_memory_table(self, op: ops.InMemoryTable) -> None:
import pyarrow.parquet as pq

Expand Down

0 comments on commit 2071897

Please sign in to comment.