Skip to content

Commit

Permalink
chore: add ddl accessor to list_views
Browse files Browse the repository at this point in the history
  • Loading branch information
ncclementi committed Jul 24, 2024
1 parent 8717629 commit 1726c36
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
24 changes: 24 additions & 0 deletions ibis/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ def _ipython_key_completions_(self) -> list[str]:
return self._backend.list_tables()


class DdlAccessor:
"""ddl accessor list views."""

def __init__(self, backend: BaseBackend):
self._backend = backend

def list_views(self) -> list[str]:
"""List the names of views in the database."""
return self._backend.list_views()


class _FileIOHandler:
@staticmethod
def _import_pyarrow():
Expand Down Expand Up @@ -895,6 +906,14 @@ def _filter_with_like(values: Iterable[str], like: str | None = None) -> list[st
pattern = re.compile(like)
return sorted(filter(pattern.findall, values))

@abc.abstractmethod
def list_views(
self,
like: str | None = None,
database: str | None = None,
) -> list[str]:
"""List the names of views in the database."""

@abc.abstractmethod
def list_tables(
self, like: str | None = None, database: tuple[str, str] | str | None = None
Expand Down Expand Up @@ -983,6 +1002,11 @@ def tables(self):
"""
return TablesAccessor(self)

@functools.cached_property
def ddl(self):
"""A ddl accessor."""
return DdlAccessor(self)

@property
@abc.abstractmethod
def version(self) -> str:
Expand Down
26 changes: 26 additions & 0 deletions ibis/backends/duckdb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,32 @@ def list_tables(

return self._filter_with_like(out[col].to_pylist(), like)

def list_views(
self,
like: str | None = None,
database: tuple[str, str] | str | None = None,
schema: str | None = None,
) -> list[str]:
"""List views."""
# return ["test1", "test2"]
table_loc = self._warn_and_create_table_loc(database, schema)

database = self.compiler.f.current_schema()
if table_loc is not None:
database = table_loc.db or database

col = "view_name"
sql = (
sg.select(col)
.from_(sg.table("duckdb_views"))
.distinct()
.where(C.schema_name.eq(database))
.sql(self.name, pretty=True)
)
out = self.con.execute(sql).fetch_arrow_table()

return self._filter_with_like(out[col].to_pylist(), like)

def read_postgres(
self, uri: str, *, table_name: str | None = None, database: str = "public"
) -> ir.Table:
Expand Down
12 changes: 12 additions & 0 deletions ibis/backends/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,15 @@ def test_unbind(alltypes, expr_fn):

assert "Unbound" not in repr(expr)
assert "Unbound" in repr(expr.unbind())


###for now let's just try duckdb
def test_list_views(ddl_con, temp_view):
expr = ddl_con.table("functional_alltypes")
ddl_con.create_view(temp_view, expr)

views = ddl_con.list_views()
assert isinstance(views, list)
assert temp_view in views

assert temp_view not in ddl_con.list_tables()

0 comments on commit 1726c36

Please sign in to comment.