Skip to content

Commit f3baa1e

Browse files
authored
Re-open DuckDB context between iterations (take 2) (#4362)
Reverts #4355 and re-attempts #4346 while fixing the segfault issue --------- Signed-off-by: Will Manning <[email protected]>
1 parent 9490568 commit f3baa1e

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ jobs:
305305
- name: Run TPC-H
306306
if: ${{ matrix.suite == 'tpc-h' }}
307307
run: |
308-
cargo run --bin query_bench -- tpch -i1 --scale-factor 0.1
308+
cargo run --bin query_bench -- tpch -i2 --scale-factor 0.1
309309
- name: Run FFI Example
310310
if: ${{ matrix.suite == 'ffi' }}
311311
run: |

bench-vortex/src/benchmark_driver.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ fn execute_queries<B: Benchmark>(
188188
let mut row_count = None;
189189

190190
for _ in 0..iterations {
191+
// Ensure we reopen the database to clear caches between runs.
192+
ctx.reopen()?;
193+
191194
let (duration, current_row_count) =
192195
ctx.execute_query(query_string).unwrap_or_else(|err| {
193196
vortex_panic!("query: {query_idx} failed with: {err}")

bench-vortex/src/engines/ddb/mod.rs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

4+
use std::path::PathBuf;
45
use std::time::{Duration, Instant};
56

67
use anyhow::Result;
@@ -31,6 +32,7 @@ impl DuckDBObject {
3132
pub struct DuckDBCtx {
3233
pub db: Database,
3334
pub connection: Connection,
35+
pub db_path: Option<PathBuf>,
3436
}
3537

3638
impl DuckDBCtx {
@@ -55,18 +57,51 @@ impl DuckDBCtx {
5557
if delete_database {
5658
std::fs::remove_file(&db_path)?;
5759
}
58-
let db = Database::open(db_path)?;
60+
let db = Database::open(&db_path)?;
5961
let connection = db.connect()?;
6062
vortex_duckdb::register_table_functions(&connection)?;
61-
Ok(Self { db, connection })
63+
64+
Ok(Self {
65+
db,
66+
connection,
67+
db_path: Some(db_path),
68+
})
69+
}
70+
71+
pub fn reopen(&mut self) -> Result<()> {
72+
// take ownership of the connection & database
73+
let mut connection = unsafe { Connection::borrow(self.connection.as_ptr()) };
74+
std::mem::swap(&mut self.connection, &mut connection);
75+
let mut db = unsafe { Database::borrow(self.db.as_ptr()) };
76+
std::mem::swap(&mut self.db, &mut db);
77+
78+
// drop the connection, then the database (order might be important?)
79+
// NB: self.db and self.connection will be dangling pointers, which we'll fix below
80+
drop(connection);
81+
drop(db);
82+
83+
let mut db = match &self.db_path {
84+
Some(path) => Database::open(path),
85+
None => Database::open_in_memory(),
86+
}?;
87+
let mut connection = db.connect()?;
88+
vortex_duckdb::register_table_functions(&connection)?;
89+
90+
std::mem::swap(&mut self.connection, &mut connection);
91+
std::mem::swap(&mut self.db, &mut db);
92+
93+
Ok(())
6294
}
6395

6496
pub fn new_in_memory() -> Result<Self> {
6597
let db = Database::open_in_memory()?;
6698
let connection = db.connect()?;
6799
vortex_duckdb::register_table_functions(&connection)?;
68-
69-
Ok(Self { db, connection })
100+
Ok(Self {
101+
db,
102+
connection,
103+
db_path: None,
104+
})
70105
}
71106

72107
/// Execute DuckDB queries for benchmarks using the internal connection

0 commit comments

Comments
 (0)