Skip to content

Commit

Permalink
fix(duckdb): deduplicate results from list_schemas()
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Aug 2, 2023
1 parent 2039b1e commit 172520e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
22 changes: 20 additions & 2 deletions ibis/backends/duckdb/__init__.py
Expand Up @@ -28,7 +28,8 @@
import ibis.expr.schema as sch
import ibis.expr.types as ir
from ibis import util
from ibis.backends.base.sql.alchemy import AlchemyCanCreateSchema, BaseAlchemyBackend
from ibis.backends.base import CanCreateSchema
from ibis.backends.base.sql.alchemy import BaseAlchemyBackend
from ibis.backends.duckdb.compiler import DuckDBSQLCompiler
from ibis.backends.duckdb.datatypes import DuckDBType, parse
from ibis.expr.operations.relations import PandasDataFrameProxy
Expand Down Expand Up @@ -71,7 +72,7 @@ def _format_kwargs(kwargs: Mapping[str, Any]):
}


class Backend(BaseAlchemyBackend, AlchemyCanCreateSchema):
class Backend(BaseAlchemyBackend, CanCreateSchema):
name = "duckdb"
compiler = DuckDBSQLCompiler
supports_create_or_replace = True
Expand All @@ -82,6 +83,23 @@ def current_database(self) -> str:
with self.begin() as con:
return con.execute(query).scalar()

def list_schemas(self, like: str | None = None) -> list[str]:
s = sa.table(
"schemata",
sa.column("catalog_name", sa.TEXT()),
sa.column("schema_name", sa.TEXT()),
schema="information_schema",
)

where = s.c.catalog_name == sa.func.current_database()

if like is not None:
where &= s.c.schema_name.like(like)

query = sa.select(s.c.schema_name).select_from(s).where(where)
with self.begin() as con:
return list(con.execute(query).scalars())

@staticmethod
def _convert_kwargs(kwargs: MutableMapping) -> None:
read_only = str(kwargs.pop("read_only", "False")).capitalize()
Expand Down
3 changes: 2 additions & 1 deletion ibis/backends/tests/test_client.py
Expand Up @@ -1342,7 +1342,8 @@ def test_create_schema(con_create_schema):


def test_list_schemas(con_create_schema):
assert con_create_schema.list_schemas()
schemas = con_create_schema.list_schemas()
assert len(schemas) == len(set(schemas))


@pytest.mark.notyet(["datafusion"], reason="cannot list or drop databases")
Expand Down

0 comments on commit 172520e

Please sign in to comment.