quack-rs 0.12.0

Production-grade Rust SDK for building DuckDB loadable extensions
Documentation
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
// SPDX-License-Identifier: MIT
// Copyright 2026 Tom F. <https://github.com/tomtom215/>
// My way of giving something small back to the open source community
// and encouraging more Rust development!

//! [`MockRegistrar`] — a [`Registrar`] implementation for testing.
//!
//! `MockRegistrar` records which functions were registered without calling any
//! `DuckDB` C API. Use it to unit-test your registration logic — verifying that
//! the right functions are registered with the right names — without a live
//! `DuckDB` instance.
//!
//! # Limitation: builders with `LogicalType` fields
//!
//! Builders that contain [`LogicalType`][crate::types::LogicalType] values (e.g.,
//! created with `.returns_logical(...)` or `.param_logical(...)`) cannot be used
//! with `MockRegistrar` in `loadable-extension` test mode. `LogicalType`'s `Drop`
//! implementation calls `duckdb_destroy_logical_type`, which panics when the
//! `DuckDB` dispatch table is uninitialized.
//!
//! Stick to [`TypeId`]-based parameter and return types
//! when building functions for use with `MockRegistrar`.
//!
//! # Example
//!
//! ```rust
//! use quack_rs::connection::Registrar;
//! use quack_rs::testing::MockRegistrar;
//! use quack_rs::scalar::ScalarFunctionBuilder;
//! use quack_rs::aggregate::AggregateFunctionBuilder;
//! use quack_rs::types::TypeId;
//! use quack_rs::error::ExtensionError;
//!
//! fn register_all(reg: &impl Registrar) -> Result<(), ExtensionError> {
//!     let scalar = ScalarFunctionBuilder::new("word_count")
//!         .param(TypeId::Varchar)
//!         .returns(TypeId::BigInt);
//!     unsafe { reg.register_scalar(scalar) }
//! }
//!
//! let mock = MockRegistrar::new();
//! register_all(&mock).unwrap();
//! assert!(mock.has_scalar("word_count"));
//! assert_eq!(mock.total_registrations(), 1);
//! ```

use std::cell::RefCell;

use crate::aggregate::{AggregateFunctionBuilder, AggregateFunctionSetBuilder};
use crate::cast::CastFunctionBuilder;
use crate::connection::Registrar;
use crate::error::ExtensionError;
use crate::scalar::{ScalarFunctionBuilder, ScalarFunctionSetBuilder};
use crate::sql_macro::SqlMacro;
use crate::table::TableFunctionBuilder;
use crate::types::TypeId;

/// A record of a single cast function registration.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CastRecord {
    /// The source type being cast from (if set via simple `TypeId`).
    pub source: Option<TypeId>,
    /// The target type being cast to (if set via simple `TypeId`).
    pub target: Option<TypeId>,
}

/// An in-memory mock implementation of [`Registrar`] for unit testing.
///
/// All `register_*` methods succeed silently (returning `Ok(())`) and record
/// the function name (or types for casts). No `DuckDB` C API is called.
///
/// # Thread safety
///
/// `MockRegistrar` uses `RefCell` for interior mutability and is **not** `Sync`.
/// Call it from a single thread within your tests.
#[derive(Debug, Default)]
pub struct MockRegistrar {
    scalar_names: RefCell<Vec<String>>,
    scalar_set_names: RefCell<Vec<String>>,
    aggregate_names: RefCell<Vec<String>>,
    aggregate_set_names: RefCell<Vec<String>>,
    table_names: RefCell<Vec<String>>,
    sql_macro_names: RefCell<Vec<String>>,
    casts: RefCell<Vec<CastRecord>>,
    #[cfg(feature = "duckdb-1-5")]
    copy_function_names: RefCell<Vec<String>>,
}

impl MockRegistrar {
    /// Creates a new, empty `MockRegistrar`.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    // ── Inspection ──────────────────────────────────────────────────────────

    /// Returns the names of all scalar functions registered so far.
    #[must_use]
    pub fn scalar_names(&self) -> Vec<String> {
        self.scalar_names.borrow().clone()
    }

    /// Returns the names of all scalar function sets registered so far.
    #[must_use]
    pub fn scalar_set_names(&self) -> Vec<String> {
        self.scalar_set_names.borrow().clone()
    }

    /// Returns the names of all aggregate functions registered so far.
    #[must_use]
    pub fn aggregate_names(&self) -> Vec<String> {
        self.aggregate_names.borrow().clone()
    }

    /// Returns the names of all aggregate function sets registered so far.
    #[must_use]
    pub fn aggregate_set_names(&self) -> Vec<String> {
        self.aggregate_set_names.borrow().clone()
    }

    /// Returns the names of all table functions registered so far.
    #[must_use]
    pub fn table_names(&self) -> Vec<String> {
        self.table_names.borrow().clone()
    }

    /// Returns the names of all SQL macros registered so far.
    #[must_use]
    pub fn sql_macro_names(&self) -> Vec<String> {
        self.sql_macro_names.borrow().clone()
    }

    /// Returns all cast registrations recorded so far.
    #[must_use]
    pub fn casts(&self) -> Vec<CastRecord> {
        self.casts.borrow().clone()
    }

    /// Returns the names of all copy functions registered so far.
    #[cfg(feature = "duckdb-1-5")]
    #[must_use]
    pub fn copy_function_names(&self) -> Vec<String> {
        self.copy_function_names.borrow().clone()
    }

    /// Returns `true` if a copy function with the given name was registered.
    #[cfg(feature = "duckdb-1-5")]
    #[must_use]
    pub fn has_copy_function(&self, name: &str) -> bool {
        self.copy_function_names.borrow().iter().any(|n| n == name)
    }

    /// Returns the total number of registrations across all types.
    #[must_use]
    pub fn total_registrations(&self) -> usize {
        let base = self.scalar_names.borrow().len()
            + self.scalar_set_names.borrow().len()
            + self.aggregate_names.borrow().len()
            + self.aggregate_set_names.borrow().len()
            + self.table_names.borrow().len()
            + self.sql_macro_names.borrow().len()
            + self.casts.borrow().len();
        #[cfg(feature = "duckdb-1-5")]
        {
            base + self.copy_function_names.borrow().len()
        }
        #[cfg(not(feature = "duckdb-1-5"))]
        {
            base
        }
    }

    // ── Convenience predicates ──────────────────────────────────────────────

    /// Returns `true` if a scalar function with the given name was registered.
    #[must_use]
    pub fn has_scalar(&self, name: &str) -> bool {
        self.scalar_names.borrow().iter().any(|n| n == name)
    }

    /// Returns `true` if a scalar function set with the given name was registered.
    #[must_use]
    pub fn has_scalar_set(&self, name: &str) -> bool {
        self.scalar_set_names.borrow().iter().any(|n| n == name)
    }

    /// Returns `true` if an aggregate function with the given name was registered.
    #[must_use]
    pub fn has_aggregate(&self, name: &str) -> bool {
        self.aggregate_names.borrow().iter().any(|n| n == name)
    }

    /// Returns `true` if an aggregate function set with the given name was registered.
    #[must_use]
    pub fn has_aggregate_set(&self, name: &str) -> bool {
        self.aggregate_set_names.borrow().iter().any(|n| n == name)
    }

    /// Returns `true` if a table function with the given name was registered.
    #[must_use]
    pub fn has_table(&self, name: &str) -> bool {
        self.table_names.borrow().iter().any(|n| n == name)
    }

    /// Returns `true` if a SQL macro with the given name was registered.
    #[must_use]
    pub fn has_sql_macro(&self, name: &str) -> bool {
        self.sql_macro_names.borrow().iter().any(|n| n == name)
    }
}

impl Registrar for MockRegistrar {
    /// Records a scalar function registration. Never calls `DuckDB` C API.
    ///
    /// # Safety
    ///
    /// This implementation is safe to call in any context — no `DuckDB`
    /// connection is required.
    unsafe fn register_scalar(&self, builder: ScalarFunctionBuilder) -> Result<(), ExtensionError> {
        self.scalar_names
            .borrow_mut()
            .push(builder.name().to_owned());
        Ok(())
    }

    /// Records a scalar function set registration. Never calls `DuckDB` C API.
    ///
    /// # Safety
    ///
    /// This implementation is safe to call in any context.
    unsafe fn register_scalar_set(
        &self,
        builder: ScalarFunctionSetBuilder,
    ) -> Result<(), ExtensionError> {
        self.scalar_set_names
            .borrow_mut()
            .push(builder.name().to_owned());
        Ok(())
    }

    /// Records an aggregate function registration. Never calls `DuckDB` C API.
    ///
    /// # Safety
    ///
    /// This implementation is safe to call in any context.
    unsafe fn register_aggregate(
        &self,
        builder: AggregateFunctionBuilder,
    ) -> Result<(), ExtensionError> {
        self.aggregate_names
            .borrow_mut()
            .push(builder.name().to_owned());
        Ok(())
    }

    /// Records an aggregate function set registration. Never calls `DuckDB` C API.
    ///
    /// # Safety
    ///
    /// This implementation is safe to call in any context.
    unsafe fn register_aggregate_set(
        &self,
        builder: AggregateFunctionSetBuilder,
    ) -> Result<(), ExtensionError> {
        self.aggregate_set_names
            .borrow_mut()
            .push(builder.name().to_owned());
        Ok(())
    }

    /// Records a table function registration. Never calls `DuckDB` C API.
    ///
    /// # Safety
    ///
    /// This implementation is safe to call in any context.
    unsafe fn register_table(&self, builder: TableFunctionBuilder) -> Result<(), ExtensionError> {
        self.table_names
            .borrow_mut()
            .push(builder.name().to_owned());
        Ok(())
    }

    /// Records a SQL macro registration. Never calls `DuckDB` C API.
    ///
    /// # Safety
    ///
    /// This implementation is safe to call in any context.
    unsafe fn register_sql_macro(&self, sql_macro: SqlMacro) -> Result<(), ExtensionError> {
        self.sql_macro_names
            .borrow_mut()
            .push(sql_macro.name().to_owned());
        Ok(())
    }

    /// Records a cast function registration. Never calls `DuckDB` C API.
    ///
    /// The source and target types are captured from the builder.
    ///
    /// # Safety
    ///
    /// This implementation is safe to call in any context.
    unsafe fn register_cast(&self, builder: CastFunctionBuilder) -> Result<(), ExtensionError> {
        self.casts.borrow_mut().push(CastRecord {
            source: builder.source(),
            target: builder.target(),
        });
        Ok(())
    }

    #[cfg(feature = "duckdb-1-5")]
    unsafe fn register_copy_function(
        &self,
        builder: crate::copy_function::CopyFunctionBuilder,
    ) -> Result<(), ExtensionError> {
        self.copy_function_names
            .borrow_mut()
            .push(builder.name().to_owned());
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::TypeId;

    #[test]
    fn mock_registrar_records_scalar() {
        let mock = MockRegistrar::new();
        let builder = ScalarFunctionBuilder::new("my_fn")
            .param(TypeId::BigInt)
            .returns(TypeId::BigInt);
        unsafe { mock.register_scalar(builder).unwrap() };
        assert!(mock.has_scalar("my_fn"));
        assert_eq!(mock.scalar_names(), vec!["my_fn"]);
        assert_eq!(mock.total_registrations(), 1);
    }

    #[test]
    fn mock_registrar_records_aggregate() {
        let mock = MockRegistrar::new();
        let builder = AggregateFunctionBuilder::new("my_agg")
            .param(TypeId::BigInt)
            .returns(TypeId::BigInt);
        unsafe { mock.register_aggregate(builder).unwrap() };
        assert!(mock.has_aggregate("my_agg"));
        assert_eq!(mock.aggregate_names(), vec!["my_agg"]);
        assert_eq!(mock.total_registrations(), 1);
    }

    #[test]
    fn mock_registrar_records_scalar_set() {
        let mock = MockRegistrar::new();
        let builder = crate::scalar::ScalarFunctionSetBuilder::new("my_set");
        unsafe { mock.register_scalar_set(builder).unwrap() };
        assert!(mock.has_scalar_set("my_set"));
        assert!(!mock.has_scalar_set("other"));
        assert_eq!(mock.scalar_set_names(), vec!["my_set"]);
        assert_eq!(mock.total_registrations(), 1);
    }

    #[test]
    fn mock_registrar_records_aggregate_set() {
        let mock = MockRegistrar::new();
        let builder = AggregateFunctionSetBuilder::new("my_agg_set");
        unsafe { mock.register_aggregate_set(builder).unwrap() };
        assert!(mock.has_aggregate_set("my_agg_set"));
        assert_eq!(mock.aggregate_set_names(), vec!["my_agg_set"]);
        assert_eq!(mock.total_registrations(), 1);
    }

    #[test]
    fn mock_registrar_records_table() {
        let mock = MockRegistrar::new();
        let builder = TableFunctionBuilder::new("my_table");
        unsafe { mock.register_table(builder).unwrap() };
        assert!(mock.has_table("my_table"));
        assert_eq!(mock.table_names(), vec!["my_table"]);
        assert_eq!(mock.total_registrations(), 1);
    }

    #[test]
    fn mock_registrar_records_sql_macro() {
        let mock = MockRegistrar::new();
        let macro_ = SqlMacro::scalar("my_macro", &["x"], "x + 1").unwrap();
        unsafe { mock.register_sql_macro(macro_).unwrap() };
        assert!(mock.has_sql_macro("my_macro"));
        assert_eq!(mock.sql_macro_names(), vec!["my_macro"]);
        assert_eq!(mock.total_registrations(), 1);
    }

    #[test]
    fn mock_registrar_records_cast() {
        let mock = MockRegistrar::new();
        let builder = CastFunctionBuilder::new(TypeId::Varchar, TypeId::Integer);
        unsafe { mock.register_cast(builder).unwrap() };
        let casts = mock.casts();
        assert_eq!(casts.len(), 1);
        assert_eq!(casts[0].source, Some(TypeId::Varchar));
        assert_eq!(casts[0].target, Some(TypeId::Integer));
        assert_eq!(mock.total_registrations(), 1);
    }

    #[test]
    fn mock_registrar_multiple_registrations() {
        let mock = MockRegistrar::new();

        let s1 = ScalarFunctionBuilder::new("fn_one")
            .param(TypeId::BigInt)
            .returns(TypeId::BigInt);
        let s2 = ScalarFunctionBuilder::new("fn_two")
            .param(TypeId::Varchar)
            .returns(TypeId::Integer);

        unsafe {
            mock.register_scalar(s1).unwrap();
            mock.register_scalar(s2).unwrap();
        }

        assert_eq!(mock.total_registrations(), 2);
        assert!(mock.has_scalar("fn_one"));
        assert!(mock.has_scalar("fn_two"));
        assert!(!mock.has_scalar("fn_three"));
    }

    #[test]
    #[cfg(feature = "duckdb-1-5")]
    fn mock_registrar_records_copy_function() {
        let mock = MockRegistrar::new();
        assert!(!mock.has_copy_function("my_format"));
        assert!(mock.copy_function_names().is_empty());

        let builder = crate::copy_function::CopyFunctionBuilder::try_new("my_format").unwrap();
        unsafe { mock.register_copy_function(builder).unwrap() };

        assert!(mock.has_copy_function("my_format"));
        assert!(!mock.has_copy_function("other_format"));
        assert_eq!(mock.copy_function_names(), vec!["my_format"]);
        assert_eq!(mock.copy_function_names().len(), 1);
        assert_eq!(mock.total_registrations(), 1);
    }

    /// Registers one scalar **and** one copy function so that
    /// `total_registrations` must add (not subtract) the copy-function count.
    /// With the `+ with -` mutation, `base(1) - copy_len(1) = 0 ≠ 2`.
    #[test]
    #[cfg(feature = "duckdb-1-5")]
    fn mock_registrar_total_registrations_scalar_plus_copy_function() {
        let mock = MockRegistrar::new();

        let scalar = ScalarFunctionBuilder::new("my_scalar")
            .param(TypeId::BigInt)
            .returns(TypeId::BigInt);
        let copy_fn = crate::copy_function::CopyFunctionBuilder::try_new("my_format").unwrap();

        unsafe {
            mock.register_scalar(scalar).unwrap();
            mock.register_copy_function(copy_fn).unwrap();
        }

        assert_eq!(mock.total_registrations(), 2);
        assert!(mock.has_scalar("my_scalar"));
        assert!(mock.has_copy_function("my_format"));
    }

    #[test]
    fn mock_registrar_has_aggregate_false_when_empty() {
        let mock = MockRegistrar::new();
        assert!(!mock.has_aggregate("x"));
    }

    #[test]
    fn mock_registrar_has_aggregate_set_false_when_empty() {
        let mock = MockRegistrar::new();
        assert!(!mock.has_aggregate_set("x"));
    }

    #[test]
    fn mock_registrar_has_table_false_when_empty() {
        let mock = MockRegistrar::new();
        assert!(!mock.has_table("x"));
    }

    #[test]
    fn mock_registrar_has_sql_macro_false_when_empty() {
        let mock = MockRegistrar::new();
        assert!(!mock.has_sql_macro("x"));
    }

    #[test]
    fn mock_registrar_has_scalar_false_when_empty() {
        let mock = MockRegistrar::new();
        assert!(!mock.has_scalar("x"));
    }

    #[test]
    fn mock_registrar_empty_total_registrations() {
        let mock = MockRegistrar::new();
        assert_eq!(mock.total_registrations(), 0);
    }

    #[test]
    fn mock_registrar_total_registrations_counts_all_types() {
        let mock = MockRegistrar::new();

        let scalar = ScalarFunctionBuilder::new("sc")
            .param(TypeId::BigInt)
            .returns(TypeId::BigInt);
        let agg = AggregateFunctionBuilder::new("ag")
            .param(TypeId::BigInt)
            .returns(TypeId::BigInt);
        let table = TableFunctionBuilder::new("tb");
        let macro_ = SqlMacro::scalar("mc", &["x"], "x + 1").unwrap();
        let cast = CastFunctionBuilder::new(TypeId::Varchar, TypeId::Integer);

        unsafe {
            mock.register_scalar(scalar).unwrap();
            mock.register_aggregate(agg).unwrap();
            mock.register_table(table).unwrap();
            mock.register_sql_macro(macro_).unwrap();
            mock.register_cast(cast).unwrap();
        }

        assert_eq!(mock.total_registrations(), 5);
    }

    #[test]
    fn mock_registrar_used_with_generic_registrar() {
        // Demonstrates using MockRegistrar where &impl Registrar is expected.
        fn register_all(reg: &impl Registrar) -> Result<(), ExtensionError> {
            let s = ScalarFunctionBuilder::new("compute")
                .param(TypeId::Integer)
                .returns(TypeId::Integer);
            unsafe { reg.register_scalar(s) }
        }

        let mock = MockRegistrar::new();
        register_all(&mock).unwrap();
        assert!(mock.has_scalar("compute"));
    }
}