Skip to content

Commit

Permalink
chore: replace repetitive code for helper
Browse files Browse the repository at this point in the history
  • Loading branch information
ncclementi committed Aug 22, 2024
1 parent 167e7c0 commit b515edb
Showing 1 changed file with 42 additions and 47 deletions.
89 changes: 42 additions & 47 deletions ibis/backends/duckdb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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)
Expand Down

0 comments on commit b515edb

Please sign in to comment.