chdb-rust 2.2.0

chDB FFI bindings for Rust(Experimental)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//! What the process-wide engine record says, and when it refuses.
//!
//! These tests need the engine to themselves, so each takes [`engine`] first
//! and leaves the engine idle afterwards.

use chdb_rust::connection::Connection;
use chdb_rust::error::Error;
use chdb_rust::session::SessionBuilder;
use chdb_rust::{active_engine_path, active_engine_refs};

mod common;

/// Takes the engine for one test.
///
/// The engine is process-wide, so these tests cannot overlap: each asserts it
/// starts from an idle engine. Serializing here rather than relying on
/// `--test-threads=1` means a plain `cargo test` reports real failures instead
/// of collisions.
fn engine() -> std::sync::MutexGuard<'static, ()> {
    static ENGINE: std::sync::Mutex<()> = std::sync::Mutex::new(());
    ENGINE
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
}

#[test]
fn the_record_counts_every_open_handle() {
    let _engine = engine();
    assert_eq!(
        active_engine_refs(),
        0,
        "a previous test left a handle open"
    );
    assert_eq!(active_engine_path(), None);

    let dir = common::tempdir();
    let first = SessionBuilder::new()
        .with_data_path(dir.path())
        .build()
        .expect("first session");
    assert_eq!(active_engine_refs(), 1);
    let bound = active_engine_path().expect("a path once something is open");

    let second = SessionBuilder::new()
        .with_data_path(dir.path())
        .build()
        .expect("a second session on the same path");
    assert_eq!(active_engine_refs(), 2);
    assert_eq!(active_engine_path().as_deref(), Some(bound.as_str()));

    drop(second);
    assert_eq!(active_engine_refs(), 1);
    assert_eq!(
        active_engine_path().as_deref(),
        Some(bound.as_str()),
        "the engine stays bound while a handle remains"
    );

    drop(first);
    assert_eq!(active_engine_refs(), 0);
    assert_eq!(
        active_engine_path(),
        None,
        "the last handle releases the path"
    );
}

#[test]
fn a_second_data_path_is_refused_by_name() {
    let _engine = engine();
    assert_eq!(active_engine_refs(), 0);
    let held = common::tempdir();
    let other = common::tempdir();

    let session = SessionBuilder::new()
        .with_data_path(held.path())
        .build()
        .expect("session");
    let bound = active_engine_path().expect("bound");

    let refused = SessionBuilder::new()
        .with_data_path(other.path())
        .build()
        .expect_err("a second data path cannot be opened");

    match &refused {
        Error::PathConflict { active, requested } => {
            assert_eq!(active, &bound);
            assert!(
                requested.ends_with(
                    other
                        .path()
                        .file_name()
                        .and_then(|name| name.to_str())
                        .expect("temp dir name")
                ),
                "the refusal should name what was asked for, got {requested}"
            );
        }
        other => panic!("expected PathConflict, got {other}"),
    }

    // The reason has to survive being turned into a message.
    let message = refused.to_string();
    assert!(message.contains(&bound), "{message}");

    drop(session);
    assert_eq!(active_engine_refs(), 0);
}

/// An in-memory connection is a data path of its own, so it cannot join a
/// session on disk. That is what makes `execute` refuse while one is open.
#[test]
fn an_in_memory_connection_is_a_data_path() {
    let _engine = engine();
    assert_eq!(active_engine_refs(), 0);
    let dir = common::tempdir();
    let session = SessionBuilder::new()
        .with_data_path(dir.path())
        .build()
        .expect("session");

    for refused in [
        Connection::open_in_memory().expect_err("connection refused"),
        chdb_rust::execute("SELECT 1", None).expect_err("execute refused"),
    ] {
        match refused {
            Error::PathConflict { requested, .. } => assert_eq!(requested, ":memory:"),
            other => panic!("expected PathConflict, got {other}"),
        }
    }

    drop(session);

    // And the other way round, once the engine is idle.
    let memory = Connection::open_in_memory().expect("in-memory once idle");
    assert_eq!(active_engine_path().as_deref(), Some(":memory:"));
    let refused = SessionBuilder::new()
        .with_data_path(dir.path())
        .build()
        .expect_err("on-disk cannot join an in-memory engine");
    assert!(matches!(refused, Error::PathConflict { .. }));
    drop(memory);
    assert_eq!(active_engine_refs(), 0);
}

#[test]
fn the_next_path_is_free_once_the_last_handle_goes() {
    let _engine = engine();
    assert_eq!(active_engine_refs(), 0);
    let first = common::tempdir();
    let second = common::tempdir();

    let session = SessionBuilder::new()
        .with_data_path(first.path())
        .build()
        .expect("first path");
    drop(session);

    let session = SessionBuilder::new()
        .with_data_path(second.path())
        .build()
        .expect("a different path once the engine is idle");
    assert_eq!(
        session
            .execute("SELECT 1", None)
            .expect("query")
            .data_utf8_lossy()
            .trim(),
        "1"
    );
    drop(session);
    assert_eq!(active_engine_refs(), 0);
}

/// Two spellings of one directory converge before the engine sees them, so the
/// second session attaches instead of being refused.
#[test]
fn one_directory_named_two_ways_shares_the_engine() {
    let _engine = engine();
    assert_eq!(active_engine_refs(), 0);
    let dir = common::tempdir();
    let plain = dir.path().to_path_buf();
    let trailing_slash = format!("{}/", plain.display());

    let first = SessionBuilder::new()
        .with_data_path(&plain)
        .build()
        .expect("first spelling");
    let second = SessionBuilder::new()
        .with_data_path(&trailing_slash)
        .build()
        .expect("a trailing slash names the same directory");
    assert_eq!(active_engine_refs(), 2);

    // One database, not two that coexist.
    first
        .execute(
            "CREATE TABLE shared (x UInt32) ENGINE = MergeTree ORDER BY x",
            None,
        )
        .expect("create");
    first
        .execute("INSERT INTO shared VALUES (1)", None)
        .expect("insert");
    assert_eq!(
        second
            .execute("SELECT count() FROM shared", None)
            .expect("read back")
            .data_utf8_lossy()
            .trim(),
        "1"
    );

    drop(second);
    drop(first);
    assert_eq!(active_engine_refs(), 0);
}

/// A session with auto-cleanup removes its directory only as the last handle,
/// so a sibling's data survives.
#[test]
fn auto_cleanup_leaves_a_shared_path_alone() {
    let _engine = engine();
    assert_eq!(active_engine_refs(), 0);
    let dir = common::tempdir();
    let path = dir.path().to_path_buf();

    let keeper = SessionBuilder::new()
        .with_data_path(&path)
        .build()
        .expect("keeper");
    keeper
        .execute(
            "CREATE TABLE kept (x UInt32) ENGINE = MergeTree ORDER BY x",
            None,
        )
        .expect("create");
    keeper
        .execute("INSERT INTO kept VALUES (1)", None)
        .expect("insert");

    {
        let transient = SessionBuilder::new()
            .with_data_path(&path)
            .with_auto_cleanup(true)
            .build()
            .expect("transient");
        assert_eq!(active_engine_refs(), 2);
        assert_eq!(
            transient
                .execute("SELECT count() FROM kept", None)
                .expect("shared read")
                .data_utf8_lossy()
                .trim(),
            "1"
        );
    }

    assert!(
        keeper.path().exists(),
        "a session that is not the last handle must not remove the directory"
    );
    assert_eq!(
        keeper
            .execute("SELECT count() FROM kept", None)
            .expect("still readable")
            .data_utf8_lossy()
            .trim(),
        "1"
    );

    // And the last handle, which does carry the flag, does remove it.
    let path_again = keeper.path().to_path_buf();
    drop(keeper);
    assert_eq!(active_engine_refs(), 0);
    assert!(
        path_again.exists(),
        "the last handle here had no flag, so nothing should have been removed"
    );

    let solo = SessionBuilder::new()
        .with_data_path(&path_again)
        .with_auto_cleanup(true)
        .build()
        .expect("solo");
    drop(solo);
    assert!(
        !path_again.exists(),
        "the only handle carried the flag, so the directory should be gone"
    );
}

/// `cleanup` ignores the flag but still yields to siblings.
#[test]
fn cleanup_is_explicit_and_still_yields_to_siblings() {
    let _engine = engine();
    assert_eq!(active_engine_refs(), 0);
    let dir = common::tempdir();
    let path = dir.path().to_path_buf();

    let keeper = SessionBuilder::new()
        .with_data_path(&path)
        .build()
        .expect("keeper");
    let other = SessionBuilder::new()
        .with_data_path(&path)
        .build()
        .expect("other");

    other.cleanup();
    assert!(
        path.exists(),
        "cleanup must not delete from under a sibling"
    );
    assert_eq!(active_engine_refs(), 1);

    keeper.cleanup();
    assert!(
        !path.exists(),
        "the last handle's cleanup removes the directory"
    );
    assert_eq!(active_engine_refs(), 0);
}

/// Several sessions on one path, querying at the same time, each owned by the
/// thread using it.
#[test]
fn sessions_on_one_path_query_from_several_threads() {
    let _engine = engine();
    assert_eq!(active_engine_refs(), 0);
    let dir = common::tempdir();
    let path = dir.path().to_path_buf();

    let writer = SessionBuilder::new()
        .with_data_path(&path)
        .build()
        .expect("writer");
    writer
        .execute(
            "CREATE TABLE t (x UInt32) ENGINE = MergeTree ORDER BY x",
            None,
        )
        .expect("create");
    writer
        .execute("INSERT INTO t SELECT number FROM numbers(1000)", None)
        .expect("insert");

    let readers: Vec<_> = (0..4)
        .map(|id| {
            let path = path.clone();
            std::thread::spawn(move || -> Result<String, String> {
                let session = SessionBuilder::new()
                    .with_data_path(&path)
                    .build()
                    .map_err(|e| format!("thread {id} could not attach: {e}"))?;
                let mut last = String::new();
                for _ in 0..5 {
                    last = session
                        .execute("SELECT count(), sum(x) FROM t", None)
                        .map_err(|e| format!("thread {id} query: {e}"))?
                        .data_utf8_lossy()
                        .trim()
                        .to_string();
                }
                Ok(last)
            })
        })
        .collect();

    for reader in readers {
        let answer = reader.join().expect("thread").expect("query");
        assert_eq!(answer, "1000\t499500");
    }

    assert_eq!(
        active_engine_refs(),
        1,
        "every reader should have released its handle"
    );
    drop(writer);
    assert_eq!(active_engine_refs(), 0);
}

/// A symlinked data path is not followed when the directory is removed: the
/// link goes, the directory it pointed at stays.
#[test]
fn auto_cleanup_does_not_follow_a_symlink() {
    let _engine = engine();
    assert_eq!(active_engine_refs(), 0);
    let real = common::tempdir();
    let target = real.path().join("target");
    std::fs::create_dir(&target).expect("target");
    std::fs::write(target.join("keep-me"), b"x").expect("marker");

    let link = real.path().join("link");
    std::os::unix::fs::symlink(&target, &link).expect("symlink");

    let session = SessionBuilder::new()
        .with_data_path(&link)
        .with_auto_cleanup(true)
        .build()
        .expect("session on a symlink");
    drop(session);

    assert!(
        target.join("keep-me").exists(),
        "the directory the link pointed at must survive"
    );
}

/// Two sessions releasing at the same time: exactly one of them is last, and
/// the directory goes with it.
#[test]
fn concurrent_drops_still_remove_the_directory() {
    let _engine = engine();
    assert_eq!(active_engine_refs(), 0);
    let dir = common::tempdir();
    let path = dir.path().to_path_buf();

    let mut threads = Vec::new();
    for _ in 0..2 {
        let path = path.clone();
        threads.push(std::thread::spawn(move || {
            let session = SessionBuilder::new()
                .with_data_path(&path)
                .with_auto_cleanup(true)
                .build()
                .expect("session");
            session.execute("SELECT 1", None).expect("query");
            // Both release at roughly the same moment, which is the point.
            drop(session);
        }));
    }
    for thread in threads {
        thread.join().expect("thread");
    }

    assert_eq!(active_engine_refs(), 0);
    assert!(
        !path.exists(),
        "the last handle to go should have removed the directory"
    );
}

/// Cleanup cannot delete the directory out from under a connection that
/// attaches while it is releasing.
#[test]
fn cleanup_does_not_race_a_new_connection() {
    let _engine = engine();
    assert_eq!(active_engine_refs(), 0);
    let dir = common::tempdir();
    let path = dir.path().to_path_buf();

    let holder = SessionBuilder::new()
        .with_data_path(&path)
        .build()
        .expect("holder");
    let leaving = SessionBuilder::new()
        .with_data_path(&path)
        .build()
        .expect("leaving");

    leaving.cleanup();

    assert!(path.exists(), "a sibling was still holding the path");
    assert_eq!(
        holder
            .execute("SELECT 1", None)
            .expect("still usable")
            .data_utf8_lossy()
            .trim(),
        "1"
    );
    drop(holder);
    assert_eq!(active_engine_refs(), 0);
}