Skip to content

Commit

Permalink
chore: consolidate repetitive code
Browse files Browse the repository at this point in the history
  • Loading branch information
ncclementi committed Aug 28, 2024
1 parent fcf6fe2 commit b382a1b
Showing 1 changed file with 21 additions and 55 deletions.
76 changes: 21 additions & 55 deletions ibis/backends/duckdb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -988,53 +988,49 @@ 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)
out = self.con.execute(sql).fetch_arrow_table()

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,
Expand All @@ -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(
Expand Down

0 comments on commit b382a1b

Please sign in to comment.