diff --git a/ibis/backends/duckdb/__init__.py b/ibis/backends/duckdb/__init__.py index f9cd846eea221..af723598fdd82 100644 --- a/ibis/backends/duckdb/__init__.py +++ b/ibis/backends/duckdb/__init__.py @@ -973,6 +973,17 @@ def read_delta( self.con.register(table_name, delta_table.to_pyarrow_dataset()) return self.table(table_name) + def _list_query_constructor(self, col: str, where_predicates: list) -> str: + """Helper function to construct sqlglot queries for _list_* methods.""" + + sg_query = ( + sg.select(col) + .from_(sg.table("tables", db="information_schema")) + .where(*where_predicates) + ).sql(self.name) + + return sg_query + def _list_tables( self, like: str | None = None, @@ -986,50 +997,33 @@ def _list_tables( database = table_loc.db or self.current_database col = "table_name" - sql = ( - sg.select(col) - .from_(sg.table("tables", db="information_schema")) - .where( - C.table_catalog.eq(sge.convert(catalog)), - C.table_schema.eq(sge.convert(database)), - C.table_type.eq("BASE TABLE"), - ) - .sql(self.name, pretty=True) - ) + where_predicates = [ + C.table_catalog.eq(sge.convert(catalog)), + C.table_schema.eq(sge.convert(database)), + C.table_type.eq("BASE TABLE"), + ] + sql = self._list_query_constructor(col, where_predicates) out = self.con.execute(sql).fetch_arrow_table() return self._filter_with_like(out[col].to_pylist(), like) - def _list_views( + def _list_temp_tables( self, like: str | None = None, database: tuple[str, str] | str | None = None, ) -> list[str]: - """List views.""" - - table_loc = self._warn_and_create_table_loc(database) - - catalog = table_loc.catalog or self.current_catalog - database = table_loc.db or self.current_database + """List temporary tables.""" col = "table_name" - sql = ( - sg.select(col) - .from_(sg.table("tables", db="information_schema")) - .where( - C.table_catalog.eq(sge.convert(catalog)), - C.table_schema.eq(sge.convert(database)), - C.table_type.eq("VIEW"), - ) - .sql(self.name, pretty=True) - ) + where_predicates = [C.table_type.eq("LOCAL TEMPORARY")] + sql = self._list_query_constructor(col, where_predicates) out = self.con.execute(sql).fetch_arrow_table() return self._filter_with_like(out[col].to_pylist(), like) - def _list_temp_views( + def _list_views( self, like: str | None = None, database: tuple[str, str] | str | None = None, @@ -1038,40 +1032,41 @@ def _list_temp_views( table_loc = self._warn_and_create_table_loc(database) - catalog = "temp" + catalog = table_loc.catalog or self.current_catalog database = table_loc.db or self.current_database col = "table_name" - sql = ( - sg.select(col) - .from_(sg.table("tables", db="information_schema")) - .where( - C.table_catalog.eq(sge.convert(catalog)), - C.table_schema.eq(sge.convert(database)), - C.table_type.eq("VIEW"), - ) - .sql(self.name, pretty=True) - ) + where_predicates = [ + C.table_catalog.eq(sge.convert(catalog)), + C.table_schema.eq(sge.convert(database)), + C.table_type.eq("VIEW"), + ] + sql = self._list_query_constructor(col, where_predicates) out = self.con.execute(sql).fetch_arrow_table() return self._filter_with_like(out[col].to_pylist(), like) - def _list_temp_tables( + def _list_temp_views( self, like: str | None = None, database: tuple[str, str] | str | None = None, ) -> list[str]: - """List temporary tables.""" + """List views.""" + + table_loc = self._warn_and_create_table_loc(database) + + catalog = "temp" + database = table_loc.db or self.current_database col = "table_name" - sql = ( - sg.select(col) - .from_(sg.table("tables", db="information_schema")) - .where(C.table_type.eq("LOCAL TEMPORARY")) - .sql(self.name, pretty=True) - ) + where_predicates = [ + C.table_catalog.eq(sge.convert(catalog)), + C.table_schema.eq(sge.convert(database)), + C.table_type.eq("VIEW"), + ] + sql = self._list_query_constructor(col, where_predicates) out = self.con.execute(sql).fetch_arrow_table() return self._filter_with_like(out[col].to_pylist(), like)