diff --git a/ibis/backends/duckdb/__init__.py b/ibis/backends/duckdb/__init__.py index f14c414d94ba8..dbe402e90b513 100644 --- a/ibis/backends/duckdb/__init__.py +++ b/ibis/backends/duckdb/__init__.py @@ -988,23 +988,25 @@ def _list_query_constructor(self, col: str, where_predicates: list) -> str: return sg_query - def _list_tables( + def _list_objects( self, - like: str | None = None, - database: tuple[str, str] | str | None = None, + like: str | None, + database: tuple[str, str] | str | None, + object_type: str, + is_temp: bool = False, ) -> list[str]: - """List physical tables.""" + """Generic method to list objects like tables or views.""" table_loc = self._warn_and_create_table_loc(database) - catalog = table_loc.catalog or self.current_catalog + catalog = table_loc.catalog or ("temp" if is_temp else self.current_catalog) database = table_loc.db or self.current_database col = "table_name" where_predicates = [ C.table_catalog.eq(sge.convert(catalog)), C.table_schema.eq(sge.convert(database)), - C.table_type.eq("BASE TABLE"), + C.table_type.eq(object_type), ] sql = self._list_query_constructor(col, where_predicates) @@ -1012,29 +1014,23 @@ def _list_tables( return self._filter_with_like(out[col].to_pylist(), like) - def _list_temp_tables( + def _list_tables( self, like: str | None = None, database: tuple[str, str] | str | None = None, ) -> list[str]: - """List temporary tables.""" - - table_loc = self._warn_and_create_table_loc(database) + """List physical tables.""" - catalog = table_loc.catalog or "temp" - database = table_loc.db or self.current_database + return self._list_objects(like, database, "BASE TABLE") - col = "table_name" - where_predicates = [ - C.table_type.eq("LOCAL TEMPORARY"), - C.table_catalog.eq(sge.convert(catalog)), - C.table_schema.eq(sge.convert(database)), - ] - - sql = self._list_query_constructor(col, where_predicates) - out = self.con.execute(sql).fetch_arrow_table() + def _list_temp_tables( + self, + like: str | None = None, + database: tuple[str, str] | str | None = None, + ) -> list[str]: + """List temporary tables.""" - return self._filter_with_like(out[col].to_pylist(), like) + return self._list_objects(like, database, "LOCAL TEMPORARY", is_temp=True) def _list_views( self, @@ -1043,46 +1039,16 @@ def _list_views( ) -> 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 - - col = "table_name" - 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) + return self._list_objects(like, database, "VIEW") def _list_temp_views( self, like: str | None = None, database: tuple[str, str] | str | None = None, ) -> list[str]: - """List views.""" + """List temporary views.""" - table_loc = self._warn_and_create_table_loc(database) - - catalog = table_loc.catalog or "temp" - database = table_loc.db or self.current_database - - col = "table_name" - 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) + return self._list_objects(like, database, "VIEW", is_temp=True) @deprecated(as_of="10.0", instead="use the con.tables") def list_tables(