duckdb 1.10505.0

Ergonomic wrapper for DuckDB
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
543
544
545
546
547
548
549
550
551
552
553
use std::sync::Arc;

use arrow::{
    array::{Array, RecordBatch},
    datatypes::DataType,
};

use crate::{
    arrow_interop::{WritableVector, data_chunk_to_arrow, to_duckdb_logical_type, write_arrow_array_to_vector},
    core::DataChunkHandle,
};

use super::{ScalarFunctionSignature, ScalarParams, VScalar};

/// The possible parameters of a scalar function that accepts and returns arrow types
pub enum ArrowScalarParams {
    /// The exact parameters of the scalar function
    Exact(Vec<DataType>),
    /// The variadic parameter of the scalar function
    Variadic(DataType),
}

impl AsRef<[DataType]> for ArrowScalarParams {
    fn as_ref(&self) -> &[DataType] {
        match self {
            Self::Exact(params) => params.as_ref(),
            Self::Variadic(param) => std::slice::from_ref(param),
        }
    }
}

impl From<ArrowScalarParams> for ScalarParams {
    fn from(params: ArrowScalarParams) -> Self {
        match params {
            ArrowScalarParams::Exact(params) => Self::Exact(
                params
                    .into_iter()
                    .map(|v| to_duckdb_logical_type(&v).expect("type should be converted"))
                    .collect(),
            ),
            ArrowScalarParams::Variadic(param) => {
                Self::Variadic(to_duckdb_logical_type(&param).expect("type should be converted"))
            }
        }
    }
}

/// A signature for a scalar function that accepts and returns arrow types
pub struct ArrowFunctionSignature {
    /// The parameters of the scalar function
    pub parameters: Option<ArrowScalarParams>,
    /// The return type of the scalar function
    pub return_type: DataType,
}

impl ArrowFunctionSignature {
    /// Create an exact function signature
    pub fn exact(params: Vec<DataType>, return_type: DataType) -> Self {
        Self {
            parameters: Some(ArrowScalarParams::Exact(params)),
            return_type,
        }
    }

    /// Create a variadic function signature
    pub fn variadic(param: DataType, return_type: DataType) -> Self {
        Self {
            parameters: Some(ArrowScalarParams::Variadic(param)),
            return_type,
        }
    }
}

/// A trait for scalar functions that accept and return arrow types that can be registered with DuckDB
pub trait VArrowScalar: Sized {
    /// State set at registration time. Persists for the lifetime of the catalog entry.
    /// Shared across worker threads and invocations — must not be modified during execution.
    /// Must be `'static` as it is stored in DuckDB and may outlive the current stack frame.
    type State: Default + Sized + Send + Sync + 'static;

    /// The actual function that is called by DuckDB
    fn invoke(state: &Self::State, input: RecordBatch) -> Result<Arc<dyn Array>, Box<dyn std::error::Error>>;

    /// The possible signatures of the scalar function. These will result in DuckDB scalar function overloads.
    /// The invoke method should be able to handle all of these signatures.
    fn signatures() -> Vec<ArrowFunctionSignature>;

    /// Whether the scalar function is volatile.
    ///
    /// Volatile functions are re-evaluated for each row, even if they have no parameters.
    /// This is useful for functions that generate random or unique values, such as random
    /// number generators, UUID generators, or fake data generators.
    ///
    /// By default, DuckDB optimizes zero-argument scalar functions as constants, evaluating
    /// them only once. Returning true from this method prevents this optimization.
    ///
    /// # Default
    /// Returns `false` by default, meaning the function is not volatile.
    fn volatile() -> bool {
        false
    }
}

impl<T> VScalar for T
where
    T: VArrowScalar,
{
    type State = T::State;

    fn invoke(
        state: &Self::State,
        input: &mut DataChunkHandle,
        out: &mut dyn WritableVector,
    ) -> Result<(), Box<dyn std::error::Error>> {
        let array = T::invoke(state, data_chunk_to_arrow(input)?)?;
        write_arrow_array_to_vector(&array, out)
    }

    fn signatures() -> Vec<ScalarFunctionSignature> {
        T::signatures()
            .into_iter()
            .map(|sig| ScalarFunctionSignature {
                parameters: sig.parameters.map(Into::into),
                return_type: to_duckdb_logical_type(&sig.return_type).expect("type should be converted"),
            })
            .collect()
    }
}

#[cfg(test)]
mod test {

    use std::{error::Error, sync::Arc};

    use arrow::{
        array::{Array, AsArray, Int32Array, Int64Array, ListArray, RecordBatch, StringArray},
        datatypes::{
            ArrowPrimitiveType, DataType, TimeUnit, TimestampMicrosecondType, TimestampMillisecondType,
            TimestampNanosecondType, TimestampSecondType,
        },
    };

    use crate::{Connection, vscalar::arrow::ArrowFunctionSignature};

    use super::VArrowScalar;

    struct HelloScalarArrow {}

    impl VArrowScalar for HelloScalarArrow {
        type State = ();

        fn invoke(_: &Self::State, input: RecordBatch) -> Result<Arc<dyn Array>, Box<dyn std::error::Error>> {
            let name = input.column(0).as_any().downcast_ref::<StringArray>().unwrap();
            let result = name.iter().map(|v| format!("Hello {}", v.unwrap())).collect::<Vec<_>>();
            Ok(Arc::new(StringArray::from(result)))
        }

        fn signatures() -> Vec<ArrowFunctionSignature> {
            vec![ArrowFunctionSignature::exact(vec![DataType::Utf8], DataType::Utf8)]
        }
    }

    #[derive(Debug)]
    struct MockState {
        info: String,
    }

    impl Default for MockState {
        fn default() -> Self {
            Self {
                info: "some meta".to_string(),
            }
        }
    }

    impl Drop for MockState {
        fn drop(&mut self) {
            println!("dropped meta");
        }
    }

    struct ArrowMultiplyScalar {}

    impl VArrowScalar for ArrowMultiplyScalar {
        type State = MockState;

        fn invoke(_: &Self::State, input: RecordBatch) -> Result<Arc<dyn Array>, Box<dyn std::error::Error>> {
            let a = input
                .column(0)
                .as_any()
                .downcast_ref::<::arrow::array::Float32Array>()
                .unwrap();

            let b = input
                .column(1)
                .as_any()
                .downcast_ref::<::arrow::array::Float32Array>()
                .unwrap();

            let result = a
                .iter()
                .zip(b.iter())
                .map(|(a, b)| a.unwrap() * b.unwrap())
                .collect::<Vec<_>>();
            Ok(Arc::new(::arrow::array::Float32Array::from(result)))
        }

        fn signatures() -> Vec<ArrowFunctionSignature> {
            vec![ArrowFunctionSignature::exact(
                vec![DataType::Float32, DataType::Float32],
                DataType::Float32,
            )]
        }
    }

    // accepts a string or a number and parses to int and multiplies by 2
    struct ArrowOverloaded {}

    impl VArrowScalar for ArrowOverloaded {
        type State = MockState;

        fn invoke(state: &Self::State, input: RecordBatch) -> Result<Arc<dyn Array>, Box<dyn std::error::Error>> {
            assert_eq!("some meta", state.info);

            let a = input.column(0);
            let b = input.column(1);

            let result = match a.data_type() {
                DataType::Utf8 => {
                    let a = a
                        .as_any()
                        .downcast_ref::<::arrow::array::StringArray>()
                        .unwrap()
                        .iter()
                        .map(|v| v.unwrap().parse::<f32>().unwrap())
                        .collect::<Vec<_>>();
                    let b = b
                        .as_any()
                        .downcast_ref::<::arrow::array::Float32Array>()
                        .unwrap()
                        .iter()
                        .map(|v| v.unwrap())
                        .collect::<Vec<_>>();
                    a.iter().zip(b.iter()).map(|(a, b)| a * b).collect::<Vec<_>>()
                }
                DataType::Float32 => {
                    let a = a
                        .as_any()
                        .downcast_ref::<::arrow::array::Float32Array>()
                        .unwrap()
                        .iter()
                        .map(|v| v.unwrap())
                        .collect::<Vec<_>>();
                    let b = b
                        .as_any()
                        .downcast_ref::<::arrow::array::Float32Array>()
                        .unwrap()
                        .iter()
                        .map(|v| v.unwrap())
                        .collect::<Vec<_>>();
                    a.iter().zip(b.iter()).map(|(a, b)| a * b).collect::<Vec<_>>()
                }
                _ => panic!("unsupported type"),
            };

            Ok(Arc::new(::arrow::array::Float32Array::from(result)))
        }

        fn signatures() -> Vec<ArrowFunctionSignature> {
            vec![
                ArrowFunctionSignature::exact(vec![DataType::Utf8, DataType::Float32], DataType::Float32),
                ArrowFunctionSignature::exact(vec![DataType::Float32, DataType::Float32], DataType::Float32),
            ]
        }
    }

    #[test]
    fn test_arrow_scalar() -> Result<(), Box<dyn Error>> {
        let conn = Connection::open_in_memory()?;
        conn.register_scalar_function::<HelloScalarArrow>("hello")?;

        let batches = conn
            .prepare("select hello('foo') as hello from range(10)")?
            .query_arrow([])?
            .collect::<Vec<_>>();

        for batch in batches.iter() {
            let array = batch.column(0);
            let array = array.as_any().downcast_ref::<::arrow::array::StringArray>().unwrap();
            for i in 0..array.len() {
                assert_eq!(array.value(i), format!("Hello foo"));
            }
        }

        Ok(())
    }

    #[test]
    fn test_arrow_scalar_multiply() -> Result<(), Box<dyn Error>> {
        let conn = Connection::open_in_memory()?;
        conn.register_scalar_function::<ArrowMultiplyScalar>("multiply_udf")?;

        let batches = conn
            .prepare("select multiply_udf(3.0, 2.0) as mult_result from range(10)")?
            .query_arrow([])?
            .collect::<Vec<_>>();

        for batch in batches.iter() {
            let array = batch.column(0);
            let array = array.as_any().downcast_ref::<::arrow::array::Float32Array>().unwrap();
            for i in 0..array.len() {
                assert_eq!(array.value(i), 6.0);
            }
        }
        Ok(())
    }

    #[test]
    fn test_multiple_signatures_scalar() -> Result<(), Box<dyn Error>> {
        let conn = Connection::open_in_memory()?;
        conn.register_scalar_function::<ArrowOverloaded>("multi_sig_udf")?;

        let batches = conn
            .prepare("select multi_sig_udf('3', 5) as message from range(2)")?
            .query_arrow([])?
            .collect::<Vec<_>>();

        for batch in batches.iter() {
            let array = batch.column(0);
            let array = array.as_any().downcast_ref::<::arrow::array::Float32Array>().unwrap();
            for i in 0..array.len() {
                assert_eq!(array.value(i), 15.0);
            }
        }

        let batches = conn
            .prepare("select multi_sig_udf(12, 10) as message from range(2)")?
            .query_arrow([])?
            .collect::<Vec<_>>();

        for batch in batches.iter() {
            let array = batch.column(0);
            let array = array.as_any().downcast_ref::<::arrow::array::Float32Array>().unwrap();
            for i in 0..array.len() {
                assert_eq!(array.value(i), 120.0);
            }
        }

        Ok(())
    }

    #[test]
    fn test_split_function() -> Result<(), Box<dyn Error>> {
        struct SplitFunction {}

        impl VArrowScalar for SplitFunction {
            type State = ();

            fn invoke(_: &Self::State, input: RecordBatch) -> Result<Arc<dyn Array>, Box<dyn std::error::Error>> {
                let strings = input.column(0).as_any().downcast_ref::<StringArray>().unwrap();

                let mut builder = arrow::array::ListBuilder::new(arrow::array::StringBuilder::with_capacity(
                    strings.len(),
                    strings.len() * 10,
                ));

                for s in strings.iter() {
                    let s = s.unwrap();
                    for split_value in s.split(' ').collect::<Vec<_>>() {
                        builder.values().append_value(split_value);
                    }
                    builder.append(true);
                }

                Ok(Arc::new(builder.finish()))
            }

            fn signatures() -> Vec<ArrowFunctionSignature> {
                vec![ArrowFunctionSignature::exact(
                    vec![DataType::Utf8],
                    DataType::List(Arc::new(arrow::datatypes::Field::new("item", DataType::Utf8, true))),
                )]
            }
        }

        let conn = Connection::open_in_memory()?;
        conn.register_scalar_function::<SplitFunction>("split_string")?;

        // Test with single string
        let batches = conn
            .prepare("select split_string('hello world') as result")?
            .query_arrow([])?
            .collect::<Vec<_>>();

        let array = batches[0].column(0);
        let list_array = array.as_any().downcast_ref::<arrow::array::ListArray>().unwrap();
        let values = list_array.value(0);
        let string_values = values.as_any().downcast_ref::<StringArray>().unwrap();

        assert_eq!(string_values.value(0), "hello");
        assert_eq!(string_values.value(1), "world");

        Ok(())
    }

    #[test]
    fn test_arrow_scalar_reads_filtered_list_vectors() -> Result<(), Box<dyn Error>> {
        struct ListFirstValueFunction;

        impl VArrowScalar for ListFirstValueFunction {
            type State = ();

            fn invoke(_: &Self::State, input: RecordBatch) -> Result<Arc<dyn Array>, Box<dyn std::error::Error>> {
                let lists = input.column(0).as_any().downcast_ref::<ListArray>().unwrap();
                let first_values = lists
                    .iter()
                    .map(|value| value.map(|value| value.as_any().downcast_ref::<Int32Array>().unwrap().value(0)))
                    .collect::<Int32Array>();
                Ok(Arc::new(first_values))
            }

            fn signatures() -> Vec<ArrowFunctionSignature> {
                vec![ArrowFunctionSignature::exact(
                    vec![DataType::List(Arc::new(arrow::datatypes::Field::new(
                        "item",
                        DataType::Int32,
                        true,
                    )))],
                    DataType::Int32,
                )]
            }
        }

        let conn = Connection::open_in_memory()?;
        conn.register_scalar_function::<ListFirstValueFunction>("arrow_list_first_value")?;
        conn.execute_batch(
            "create table list_input as \
             select i::integer as id, \
                    case when i % 7 = 0 then null \
                         else [i::integer, (i + 1)::integer] end as values \
             from range(5000) t(i)",
        )?;

        let first_values = conn
            .prepare(
                "select arrow_list_first_value(values) \
                 from list_input where id % 97 = 0 order by id",
            )?
            .query_map([], |row| row.get::<_, Option<i32>>(0))?
            .collect::<Result<Vec<_>, _>>()?;

        let expected = (0..5000)
            .step_by(97)
            .map(|id| if id % 7 == 0 { None } else { Some(id) })
            .collect::<Vec<_>>();
        assert_eq!(first_values, expected);
        Ok(())
    }

    fn timestamp_value<T>(input: &RecordBatch, column: usize, expected_type: &DataType) -> Result<i64, Box<dyn Error>>
    where
        T: ArrowPrimitiveType<Native = i64>,
    {
        let array = input.column(column);
        if array.data_type() != expected_type {
            return Err(format!(
                "expected timestamp column {column} to have type {expected_type}, got {}",
                array.data_type()
            )
            .into());
        }

        array
            .as_primitive_opt::<T>()
            .map(|timestamps| timestamps.value(0))
            .ok_or_else(|| format!("timestamp column {column} has an unexpected Arrow array implementation").into())
    }

    #[test]
    fn test_arrow_scalar_reads_timestamp_carriers() -> Result<(), Box<dyn Error>> {
        struct TimestampCarrierProbe;

        impl VArrowScalar for TimestampCarrierProbe {
            type State = ();

            fn invoke(_: &Self::State, input: RecordBatch) -> Result<Arc<dyn Array>, Box<dyn std::error::Error>> {
                let timestamp_tz = DataType::Timestamp(TimeUnit::Microsecond, Some("UTC".into()));
                let actual = [
                    timestamp_value::<TimestampSecondType>(&input, 0, &DataType::Timestamp(TimeUnit::Second, None))?,
                    timestamp_value::<TimestampMillisecondType>(
                        &input,
                        1,
                        &DataType::Timestamp(TimeUnit::Millisecond, None),
                    )?,
                    timestamp_value::<TimestampMicrosecondType>(
                        &input,
                        2,
                        &DataType::Timestamp(TimeUnit::Microsecond, None),
                    )?,
                    timestamp_value::<TimestampNanosecondType>(
                        &input,
                        3,
                        &DataType::Timestamp(TimeUnit::Nanosecond, None),
                    )?,
                    timestamp_value::<TimestampMicrosecondType>(&input, 4, &timestamp_tz)?,
                ];
                let expected = [
                    1_704_067_200,
                    1_704_067_200_123,
                    1_704_067_200_123_456,
                    1_704_067_200_000_000_123,
                    1_704_067_200_123_456,
                ];
                if actual != expected {
                    return Err(format!("expected timestamp carriers {expected:?}, got {actual:?}").into());
                }

                Ok(Arc::new(Int64Array::from_iter_values([actual[3]])))
            }

            fn signatures() -> Vec<ArrowFunctionSignature> {
                vec![ArrowFunctionSignature::exact(
                    vec![
                        DataType::Timestamp(TimeUnit::Second, None),
                        DataType::Timestamp(TimeUnit::Millisecond, None),
                        DataType::Timestamp(TimeUnit::Microsecond, None),
                        DataType::Timestamp(TimeUnit::Nanosecond, None),
                        DataType::Timestamp(TimeUnit::Microsecond, Some("UTC".into())),
                    ],
                    DataType::Int64,
                )]
            }
        }

        let conn = Connection::open_in_memory()?;
        conn.register_scalar_function::<TimestampCarrierProbe>("arrow_timestamp_carriers_raw")?;

        // 2024-01-01T00:00:00Z in each carrier's resolution.
        let value = conn.query_row(
            "select arrow_timestamp_carriers_raw(\
             TIMESTAMP_S '2024-01-01 00:00:00', \
             TIMESTAMP_MS '2024-01-01 00:00:00.123', \
             TIMESTAMP '2024-01-01 00:00:00.123456', \
             TIMESTAMP_NS '2024-01-01 00:00:00.000000123', \
             TIMESTAMPTZ '2024-01-01 00:00:00.123456+00')",
            [],
            |row| row.get::<_, i64>(0),
        )?;
        assert_eq!(value, 1_704_067_200_000_000_123);

        Ok(())
    }
}