diff --git a/ibis/backends/snowflake/__init__.py b/ibis/backends/snowflake/__init__.py index f05e1c11b5fc..aaaeba5ac568 100644 --- a/ibis/backends/snowflake/__init__.py +++ b/ibis/backends/snowflake/__init__.py @@ -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 @@ -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