Skip to content

Commit

Permalink
feat(api): add ibis.set_backend function
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrist authored and cpcloud committed Jan 20, 2023
1 parent c992a42 commit e7fabaf
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/api/expressions/top_level.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ These methods and objects are available directly in the `ibis` module.
::: ibis.read_parquet
::: ibis.row_number
::: ibis.schema
::: ibis.set_backend
::: ibis.struct
::: ibis.table
::: ibis.time
Expand Down
51 changes: 51 additions & 0 deletions ibis/backends/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,57 @@ def test_get_backend(con, alltypes, monkeypatch):
assert ibis.get_backend(expr) is con


def test_set_backend(con, monkeypatch):
monkeypatch.setattr(ibis.options, "default_backend", None)
ibis.set_backend(con)
assert ibis.get_backend() is con


@pytest.mark.parametrize(
"name",
[
param("duckdb", marks=mark.duckdb, id="duckdb"),
param("polars", marks=mark.polars, id="polars"),
param("sqlite", marks=mark.sqlite, id="sqlite"),
],
)
def test_set_backend_name(name, monkeypatch):
# Don't need to test with all backends, only checking that things are
# plumbed through correctly.
monkeypatch.setattr(ibis.options, "default_backend", None)
ibis.set_backend(name)
assert ibis.get_backend().name == name


@pytest.mark.parametrize(
"url",
[
param(
"clickhouse://default@localhost:9000/ibis_testing",
marks=mark.clickhouse,
id="clickhouse",
),
param(
"mysql://ibis:ibis@localhost:3306/ibis_testing",
marks=mark.mysql,
id="mysql",
),
param(
"postgres://postgres:postgres@localhost:5432/ibis_testing",
marks=mark.postgres,
id="postgres",
),
],
)
def test_set_backend_url(url, monkeypatch):
# Don't need to test with all backends, only checking that things are
# plumbed through correctly.
monkeypatch.setattr(ibis.options, "default_backend", None)
name = url.split("://")[0]
ibis.set_backend(url)
assert ibis.get_backend().name == name


@pytest.mark.notyet(
[
"bigquery",
Expand Down
35 changes: 35 additions & 0 deletions ibis/expr/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
'schema',
'Schema',
'sequence',
'set_backend',
'show_sql',
'to_sql',
'struct',
Expand Down Expand Up @@ -871,6 +872,40 @@ def read_parquet(sources: str | Path, **kwargs: Any) -> ir.Table:
return con.read_parquet(sources, **kwargs)


def set_backend(backend: str | BaseBackend) -> None:
"""Set the default Ibis backend.
Parameters
----------
backend
May be a backend name or URL, or an existing backend instance.
Examples
--------
May pass the backend as a name:
>>> ibis.set_backend("polars")
Or as a URI:
>>> ibis.set_backend("postgres://user:password@hostname:5432")
Or as an existing backend instance:
>>> ibis.set_backend(ibis.duckdb.connect())
"""
import ibis

if isinstance(backend, str) and backend.isidentifier():
try:
backend_type = getattr(ibis, backend)
except AttributeError:
pass
else:
backend = backend_type.connect()
if isinstance(backend, str):
backend = ibis.connect(backend)

ibis.options.default_backend = backend


def get_backend(expr: Expr | None = None) -> BaseBackend:
"""Get the current Ibis backend to use for a given expression.
Expand Down

0 comments on commit e7fabaf

Please sign in to comment.