From 9ca92f07707fd8c8bbc0ca4123b1e1bf5452d6c4 Mon Sep 17 00:00:00 2001 From: Saqib Ansari Date: Thu, 5 Sep 2024 17:48:08 +0530 Subject: [PATCH] fix(backends): pass kwargs to _from_url() in every case (#10003) Co-authored-by: Phillip Cloud <417981+cpcloud@users.noreply.github.com> --- ibis/backends/__init__.py | 7 ++++--- ibis/backends/clickhouse/tests/test_client.py | 13 ++++++++++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/ibis/backends/__init__.py b/ibis/backends/__init__.py index 371affac47fc..afce12fbf69c 100644 --- a/ibis/backends/__init__.py +++ b/ibis/backends/__init__.py @@ -1396,11 +1396,12 @@ def connect(resource: Path | str, **kwargs: Any) -> BaseBackend: if len(value) == 1: kwargs[name] = value[0] + # Merge explicit kwargs with query string, explicit kwargs + # taking precedence + kwargs.update(orig_kwargs) + if scheme == "file": path = parsed.netloc + parsed.path - # Merge explicit kwargs with query string, explicit kwargs - # taking precedence - kwargs.update(orig_kwargs) if path.endswith(".duckdb"): return ibis.duckdb.connect(path, **kwargs) elif path.endswith((".sqlite", ".db")): diff --git a/ibis/backends/clickhouse/tests/test_client.py b/ibis/backends/clickhouse/tests/test_client.py index c679ea0e7c7b..311889aea091 100644 --- a/ibis/backends/clickhouse/tests/test_client.py +++ b/ibis/backends/clickhouse/tests/test_client.py @@ -375,7 +375,18 @@ def test_from_url(con): ) -def test_invalid_port(con): +def test_from_url_with_kwargs(con): + # since explicit kwargs take precedence, this passes, because we're passing + # `database` explicitly, even though our connection string says to use a + # random database + database = ibis.util.gen_name("clickhouse_database") + assert ibis.connect( + f"clickhouse://{CLICKHOUSE_USER}:{CLICKHOUSE_PASS}@{CLICKHOUSE_HOST}:{CLICKHOUSE_PORT}/{database}", + database=IBIS_TEST_CLICKHOUSE_DB, + ) + + +def test_invalid_port(): port = 9999 url = f"clickhouse://{CLICKHOUSE_USER}:{CLICKHOUSE_PASS}@{CLICKHOUSE_HOST}:{port}/{IBIS_TEST_CLICKHOUSE_DB}" with pytest.raises(cc.driver.exceptions.DatabaseError):