diff --git a/src_cpp/py_database.cpp b/src_cpp/py_database.cpp index 200200f..fcc48e9 100644 --- a/src_cpp/py_database.cpp +++ b/src_cpp/py_database.cpp @@ -62,10 +62,11 @@ PyDatabase::PyDatabase(const std::string& databasePath, std::optional // Start from the engine defaults and only apply explicitly set settings, // so engine-side default changes propagate automatically. auto systemConfig = SystemConfig(); - if (bufferPoolSize.has_value()) { + // The SystemConfig constructor reads 0 as "use the default"; assigning it here would store 0. + if (bufferPoolSize.has_value() && bufferPoolSize.value() != 0) { systemConfig.bufferPoolSize = bufferPoolSize.value(); } - if (maxNumThreads.has_value()) { + if (maxNumThreads.has_value() && maxNumThreads.value() != 0) { systemConfig.maxNumThreads = maxNumThreads.value(); } if (compression.has_value()) { diff --git a/src_py/database.py b/src_py/database.py index 101375a..d9529d1 100644 --- a/src_py/database.py +++ b/src_py/database.py @@ -50,12 +50,12 @@ def __init__( will be created in memory. buffer_pool_size : int, optional - The maximum size of buffer pool in bytes. If None (default), the - engine default (~80% of system memory) is used. + The maximum size of buffer pool in bytes. If None (default) or 0, + the engine default (~80% of system memory) is used. max_num_threads : int, optional The maximum number of threads to use for executing queries. If None - (default), the engine default is used. + (default) or 0, the engine default is used. compression : bool, optional Enable database compression. If None (default), the engine default diff --git a/test/test_database.py b/test/test_database.py index 1ac0685..b58881e 100644 --- a/test/test_database.py +++ b/test/test_database.py @@ -147,6 +147,65 @@ def test_database_checkpoint_threshold_config(tmp_path: Path) -> None: assert result.get_next()[0] == "1234" +THREADS_QUERY = "CALL current_setting('threads') RETURN *" + + +def print_settings_in_subprocess(build_dir: Path, body: str) -> list[str]: + # A subprocess turns a hang into a failure and keeps these databases out of + # the suite's address space. + code = dedent(f""" + import sys + sys.path.append(r"{build_dir!s}") + + import ladybug as lb + + + def read(query, **kwargs): + with lb.Database(":memory:", **kwargs) as db, lb.Connection(db) as conn: + return conn.execute(query).get_next()[0] + """) + dedent(body) + try: + result = subprocess.run( + [sys.executable, "-c", code], capture_output=True, text=True, timeout=60 + ) + except subprocess.TimeoutExpired: + pytest.fail("a query did not return within 60 s") + assert result.returncode == 0, result.stderr + return result.stdout.split() + + +def test_database_max_num_threads_config(build_dir: Path) -> None: + default, explicit = print_settings_in_subprocess( + build_dir, + f""" + default = int(read({THREADS_QUERY!r})) + print(default) + print(read({THREADS_QUERY!r}, max_num_threads=default + 1)) + """, + ) + assert int(explicit) == int(default) + 1 + + +@pytest.mark.parametrize( + ("setting", "query"), + [ + ("max_num_threads", THREADS_QUERY), + ("buffer_pool_size", "CALL bm_info() RETURN mem_limit"), + ], +) +def test_database_zero_setting_uses_engine_default( + setting: str, query: str, build_dir: Path +) -> None: + default, zero = print_settings_in_subprocess( + build_dir, + f""" + print(read({query!r})) + print(read({query!r}, {setting}=0)) + """, + ) + assert zero == default + + def test_database_throw_on_wal_replay_failure_config(tmp_path: Path) -> None: database_path = get_db_file_path(tmp_path) wal_file_path = str(database_path) + ".wal"