Skip to content

Commit 4ea041f

Browse files
NickCrewscpcloud
authored andcommitted
refactor(duckdb): pass temp_directory through as-is to duckdb.connect
BREAKING CHANGE: Special handling of the `temp_directory` argument passed to Ibis is removed in favor of passing the argument through directly to `duckdb.connect`. Interior nodes of directory trees must be created, e.g., using `Path.mkdir(exists_ok=True, parents=True)`, `mkdir -p`, etc.
1 parent bcbab3f commit 4ea041f

File tree

2 files changed

+10
-48
lines changed

2 files changed

+10
-48
lines changed

ibis/backends/duckdb/__init__.py

-17
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import ast
66
import contextlib
7-
import os
87
import urllib
98
import warnings
109
from operator import itemgetter
@@ -362,7 +361,6 @@ def do_connect(
362361
self,
363362
database: str | Path = ":memory:",
364363
read_only: bool = False,
365-
temp_directory: str | Path | None = None,
366364
extensions: Sequence[str] | None = None,
367365
**config: Any,
368366
) -> None:
@@ -374,9 +372,6 @@ def do_connect(
374372
Path to a duckdb database.
375373
read_only
376374
Whether the database is read-only.
377-
temp_directory
378-
Directory to use for spilling to disk. Only set by default for
379-
in-memory connections.
380375
extensions
381376
A list of duckdb extensions to install/load upon connection.
382377
config
@@ -394,19 +389,7 @@ def do_connect(
394389
("md:", "motherduck:", ":memory:")
395390
):
396391
database = Path(database).absolute()
397-
398-
if temp_directory is None:
399-
temp_directory = (
400-
Path(os.environ.get("XDG_CACHE_HOME", Path.home() / ".cache"))
401-
/ "ibis-duckdb"
402-
/ str(os.getpid())
403-
)
404-
else:
405-
Path(temp_directory).mkdir(parents=True, exist_ok=True)
406-
config["temp_directory"] = str(temp_directory)
407-
408392
self.con = duckdb.connect(str(database), config=config, read_only=read_only)
409-
410393
self._post_connect(extensions)
411394

412395
@util.experimental

ibis/backends/duckdb/tests/test_register.py

+10-31
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import os
44
import sqlite3
5-
import tempfile
65
from pathlib import Path
76

87
import duckdb
@@ -137,32 +136,6 @@ def test_read_json(con, data_dir, tmp_path):
137136
assert nrows == jst.count().execute()
138137

139138

140-
def test_temp_directory(tmp_path):
141-
query = "SELECT current_setting('temp_directory')"
142-
143-
# 1. in-memory + no temp_directory specified
144-
con = ibis.duckdb.connect()
145-
146-
value = con.raw_sql(query).fetchone()[0]
147-
assert value # we don't care what the specific value is
148-
149-
temp_directory = Path(tempfile.gettempdir()) / "duckdb"
150-
151-
# 2. in-memory + temp_directory specified
152-
con = ibis.duckdb.connect(temp_directory=temp_directory)
153-
value = con.raw_sql(query).fetchone()[0]
154-
assert value == str(temp_directory)
155-
156-
# 3. on-disk + no temp_directory specified
157-
# untested, duckdb sets the temp_directory to something implementation
158-
# defined
159-
160-
# 4. on-disk + temp_directory specified
161-
con = ibis.duckdb.connect(tmp_path / "test2.ddb", temp_directory=temp_directory)
162-
value = con.raw_sql(query).fetchone()[0]
163-
assert value == str(temp_directory)
164-
165-
166139
@pytest.fixture(scope="session")
167140
def pgurl(): # pragma: no cover
168141
pgcon = ibis.postgres.connect(
@@ -381,10 +354,16 @@ def test_memtable_with_nullable_pyarrow_not_string(con):
381354
assert len(res) == len(data)
382355

383356

384-
def test_set_temp_dir(tmp_path):
385-
path = tmp_path / "foo" / "bar"
386-
ibis.duckdb.connect(temp_directory=path)
387-
assert path.exists()
357+
@pytest.mark.parametrize(
358+
"database",
359+
[lambda parent: parent / "test.ddb", lambda _: ":memory:"],
360+
ids=["disk", "memory"],
361+
)
362+
def test_temp_dir_set(tmp_path, database):
363+
temp_directory = tmp_path / "does" / "not" / "exist"
364+
temp_directory.mkdir(parents=True, exist_ok=True)
365+
con = ibis.duckdb.connect(database(tmp_path), temp_directory=temp_directory)
366+
assert con.settings["temp_directory"] == str(temp_directory)
388367

389368

390369
@pytest.mark.xfail(

0 commit comments

Comments
 (0)