forked from duckdb/duckdb-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.rs
481 lines (443 loc) · 16.1 KB
/
function.rs
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
use super::{
ffi::{
duckdb_bind_add_result_column, duckdb_bind_get_extra_info, duckdb_bind_get_named_parameter,
duckdb_bind_get_parameter, duckdb_bind_get_parameter_count, duckdb_bind_info, duckdb_bind_set_bind_data,
duckdb_bind_set_cardinality, duckdb_bind_set_error, duckdb_create_table_function, duckdb_data_chunk,
duckdb_delete_callback_t, duckdb_destroy_table_function, duckdb_table_function,
duckdb_table_function_add_named_parameter, duckdb_table_function_add_parameter, duckdb_table_function_init_t,
duckdb_table_function_set_bind, duckdb_table_function_set_extra_info, duckdb_table_function_set_function,
duckdb_table_function_set_init, duckdb_table_function_set_local_init, duckdb_table_function_set_name,
duckdb_table_function_supports_projection_pushdown, duckdb_vector, idx_t,
},
LogicalType, Value,
};
use std::{
ffi::{c_void, CString},
os::raw::c_char,
};
/// An interface to store and retrieve data during the function bind stage
#[derive(Debug)]
pub struct BindInfo {
ptr: duckdb_bind_info,
}
impl BindInfo {
/// Adds a result column to the output of the table function.
///
/// # Arguments
/// * `name`: The name of the column
/// * `type`: The logical type of the column
pub fn add_result_column(&self, column_name: &str, column_type: LogicalType) {
let c_str = CString::new(column_name).unwrap();
unsafe {
duckdb_bind_add_result_column(self.ptr, c_str.as_ptr() as *const c_char, column_type.ptr);
}
}
/// Report that an error has occurred while calling bind.
///
/// # Arguments
/// * `error`: The error message
pub fn set_error(&self, error: &str) {
let c_str = CString::new(error).unwrap();
unsafe {
duckdb_bind_set_error(self.ptr, c_str.as_ptr() as *const c_char);
}
}
/// Sets the user-provided bind data in the bind object. This object can be retrieved again during execution.
///
/// # Arguments
/// * `extra_data`: The bind data object.
/// * `destroy`: The callback that will be called to destroy the bind data (if any)
///
/// # Safety
///
pub unsafe fn set_bind_data(&self, data: *mut c_void, free_function: Option<unsafe extern "C" fn(*mut c_void)>) {
duckdb_bind_set_bind_data(self.ptr, data, free_function);
}
/// Retrieves the number of regular (non-named) parameters to the function.
pub fn get_parameter_count(&self) -> u64 {
unsafe { duckdb_bind_get_parameter_count(self.ptr) }
}
/// Retrieves the parameter at the given index.
///
/// # Arguments
/// * `index`: The index of the parameter to get
///
/// returns: The value of the parameter
///
/// # Panics
/// If requested parameter is out of range for function definition
pub fn get_parameter(&self, param_index: u64) -> Value {
unsafe {
let ptr = duckdb_bind_get_parameter(self.ptr, param_index);
if ptr.is_null() {
panic!("{} is out of range for function definition", param_index);
} else {
Value::from(ptr)
}
}
}
/// Retrieves the named parameter with the given name.
///
/// # Arguments
/// * `name`: The name of the parameter to get
///
/// returns: The value of the parameter
pub fn get_named_parameter(&self, name: &str) -> Option<Value> {
unsafe {
let name = &CString::new(name).unwrap();
let ptr = duckdb_bind_get_named_parameter(self.ptr, name.as_ptr());
if ptr.is_null() {
None
} else {
Some(Value::from(ptr))
}
}
}
/// Sets the cardinality estimate for the table function, used for optimization.
///
/// # Arguments
/// * `cardinality`: The cardinality estimate
/// * `is_exact`: Whether or not the cardinality estimate is exact, or an approximation
pub fn set_cardinality(&self, cardinality: idx_t, is_exact: bool) {
unsafe { duckdb_bind_set_cardinality(self.ptr, cardinality, is_exact) }
}
/// Retrieves the extra info of the function as set in [`TableFunction::set_extra_info`]
///
/// # Arguments
/// * `returns`: The extra info
pub fn get_extra_info<T>(&self) -> *const T {
unsafe { duckdb_bind_get_extra_info(self.ptr).cast() }
}
}
impl From<duckdb_bind_info> for BindInfo {
fn from(ptr: duckdb_bind_info) -> Self {
Self { ptr }
}
}
use super::ffi::{
duckdb_init_get_bind_data, duckdb_init_get_column_count, duckdb_init_get_column_index, duckdb_init_get_extra_info,
duckdb_init_info, duckdb_init_set_error, duckdb_init_set_init_data, duckdb_init_set_max_threads,
};
/// An interface to store and retrieve data during the function init stage
#[derive(Debug)]
pub struct InitInfo(duckdb_init_info);
impl From<duckdb_init_info> for InitInfo {
fn from(ptr: duckdb_init_info) -> Self {
Self(ptr)
}
}
impl InitInfo {
/// # Safety
pub unsafe fn set_init_data(&self, data: *mut c_void, freeer: Option<unsafe extern "C" fn(*mut c_void)>) {
duckdb_init_set_init_data(self.0, data, freeer);
}
/// Returns the column indices of the projected columns at the specified positions.
///
/// This function must be used if projection pushdown is enabled to figure out which columns to emit.
///
/// returns: The column indices at which to get the projected column index
pub fn get_column_indices(&self) -> Vec<idx_t> {
let mut indices;
unsafe {
let column_count = duckdb_init_get_column_count(self.0);
indices = Vec::with_capacity(column_count as usize);
for i in 0..column_count {
indices.push(duckdb_init_get_column_index(self.0, i))
}
}
indices
}
/// Retrieves the extra info of the function as set in [`TableFunction::set_extra_info`]
///
/// # Arguments
/// * `returns`: The extra info
pub fn get_extra_info<T>(&self) -> *const T {
unsafe { duckdb_init_get_extra_info(self.0).cast() }
}
/// Gets the bind data set by [`BindInfo::set_bind_data`] during the bind.
///
/// Note that the bind data should be considered as read-only.
/// For tracking state, use the init data instead.
///
/// # Arguments
/// * `returns`: The bind data object
pub fn get_bind_data<T>(&self) -> *const T {
unsafe { duckdb_init_get_bind_data(self.0).cast() }
}
/// Sets how many threads can process this table function in parallel (default: 1)
///
/// # Arguments
/// * `max_threads`: The maximum amount of threads that can process this table function
pub fn set_max_threads(&self, max_threads: idx_t) {
unsafe { duckdb_init_set_max_threads(self.0, max_threads) }
}
/// Report that an error has occurred while calling init.
///
/// # Arguments
/// * `error`: The error message
pub fn set_error(&self, error: &str) {
let c_str = CString::new(error).unwrap();
unsafe { duckdb_init_set_error(self.0, c_str.as_ptr() as *const c_char) }
}
}
/// A function that returns a queryable table
#[derive(Debug)]
pub struct TableFunction {
pub(crate) ptr: duckdb_table_function,
}
impl Drop for TableFunction {
fn drop(&mut self) {
unsafe {
duckdb_destroy_table_function(&mut self.ptr);
}
}
}
impl TableFunction {
/// Sets whether or not the given table function supports projection pushdown.
///
/// If this is set to true, the system will provide a list of all required columns in the `init` stage through
/// the [`InitInfo::get_column_indices`] method.
/// If this is set to false (the default), the system will expect all columns to be projected.
///
/// # Arguments
/// * `pushdown`: True if the table function supports projection pushdown, false otherwise.
pub fn supports_pushdown(&self, supports: bool) -> &Self {
unsafe {
duckdb_table_function_supports_projection_pushdown(self.ptr, supports);
}
self
}
/// Adds a parameter to the table function.
///
/// # Arguments
/// * `logical_type`: The type of the parameter to add.
pub fn add_parameter(&self, logical_type: &LogicalType) -> &Self {
unsafe {
duckdb_table_function_add_parameter(self.ptr, logical_type.ptr);
}
self
}
/// Adds a named parameter to the table function.
///
/// # Arguments
/// * `name`: The name of the parameter to add.
/// * `logical_type`: The type of the parameter to add.
pub fn add_named_parameter(&self, name: &str, logical_type: &LogicalType) -> &Self {
unsafe {
let string = CString::new(name).unwrap();
duckdb_table_function_add_named_parameter(self.ptr, string.as_ptr(), logical_type.ptr);
}
self
}
/// Sets the main function of the table function
///
/// # Arguments
/// * `function`: The function
pub fn set_function(
&self,
func: Option<unsafe extern "C" fn(info: duckdb_function_info, output: duckdb_data_chunk)>,
) -> &Self {
unsafe {
duckdb_table_function_set_function(self.ptr, func);
}
self
}
/// Sets the init function of the table function
///
/// # Arguments
/// * `function`: The init function
pub fn set_init(&self, init_func: Option<unsafe extern "C" fn(duckdb_init_info)>) -> &Self {
unsafe {
duckdb_table_function_set_init(self.ptr, init_func);
}
self
}
/// Sets the bind function of the table function
///
/// # Arguments
/// * `function`: The bind function
pub fn set_bind(&self, bind_func: Option<unsafe extern "C" fn(duckdb_bind_info)>) -> &Self {
unsafe {
duckdb_table_function_set_bind(self.ptr, bind_func);
}
self
}
/// Creates a new empty table function.
pub fn new() -> Self {
Self {
ptr: unsafe { duckdb_create_table_function() },
}
}
/// Sets the name of the given table function.
///
/// # Arguments
/// * `name`: The name of the table function
pub fn set_name(&self, name: &str) -> &TableFunction {
unsafe {
let string = CString::from_vec_unchecked(name.as_bytes().into());
duckdb_table_function_set_name(self.ptr, string.as_ptr());
}
self
}
/// Assigns extra information to the table function that can be fetched during binding, etc.
///
/// # Arguments
/// * `extra_info`: The extra information
/// * `destroy`: The callback that will be called to destroy the bind data (if any)
///
/// # Safety
pub unsafe fn set_extra_info(&self, extra_info: *mut c_void, destroy: duckdb_delete_callback_t) {
duckdb_table_function_set_extra_info(self.ptr, extra_info, destroy);
}
/// Sets the thread-local init function of the table function
///
/// # Arguments
/// * `init`: The init function
pub fn set_local_init(&self, init: duckdb_table_function_init_t) {
unsafe { duckdb_table_function_set_local_init(self.ptr, init) };
}
}
impl Default for TableFunction {
fn default() -> Self {
Self::new()
}
}
use super::ffi::{
duckdb_function_get_bind_data, duckdb_function_get_extra_info, duckdb_function_get_init_data,
duckdb_function_get_local_init_data, duckdb_function_info, duckdb_function_set_error,
};
/// An interface to store and retrieve data during the function execution stage
#[derive(Debug)]
pub struct FunctionInfo(duckdb_function_info);
impl FunctionInfo {
/// Report that an error has occurred while executing the function.
///
/// # Arguments
/// * `error`: The error message
pub fn set_error(&self, error: &str) {
let c_str = CString::new(error).unwrap();
unsafe {
duckdb_function_set_error(self.0, c_str.as_ptr() as *const c_char);
}
}
/// Gets the bind data set by [`BindInfo::set_bind_data`] during the bind.
///
/// Note that the bind data should be considered as read-only.
/// For tracking state, use the init data instead.
///
/// # Arguments
/// * `returns`: The bind data object
pub fn get_bind_data<T>(&self) -> *mut T {
unsafe { duckdb_function_get_bind_data(self.0).cast() }
}
/// Gets the init data set by [`InitInfo::set_init_data`] during the init.
///
/// # Arguments
/// * `returns`: The init data object
pub fn get_init_data<T>(&self) -> *mut T {
unsafe { duckdb_function_get_init_data(self.0).cast() }
}
/// Retrieves the extra info of the function as set in [`TableFunction::set_extra_info`]
///
/// # Arguments
/// * `returns`: The extra info
pub fn get_extra_info<T>(&self) -> *mut T {
unsafe { duckdb_function_get_extra_info(self.0).cast() }
}
/// Gets the thread-local init data set by [`InitInfo::set_init_data`] during the local_init.
///
/// # Arguments
/// * `returns`: The init data object
pub fn get_local_init_data<T>(&self) -> *mut T {
unsafe { duckdb_function_get_local_init_data(self.0).cast() }
}
}
impl From<duckdb_function_info> for FunctionInfo {
fn from(ptr: duckdb_function_info) -> Self {
Self(ptr)
}
}
use super::ffi::{
duckdb_create_scalar_function, duckdb_destroy_scalar_function, duckdb_scalar_function,
duckdb_scalar_function_add_parameter, duckdb_scalar_function_set_extra_info, duckdb_scalar_function_set_function,
duckdb_scalar_function_set_name, duckdb_scalar_function_set_return_type,
};
/// A function that returns a queryable scalar function
#[derive(Debug)]
pub struct ScalarFunction {
pub(crate) ptr: duckdb_scalar_function,
}
impl Drop for ScalarFunction {
fn drop(&mut self) {
unsafe {
duckdb_destroy_scalar_function(&mut self.ptr);
}
}
}
impl ScalarFunction {
/// Adds a parameter to the scalar function.
///
/// # Arguments
/// * `logical_type`: The type of the parameter to add.
pub fn add_parameter(&self, logical_type: &LogicalType) -> &Self {
unsafe {
duckdb_scalar_function_add_parameter(self.ptr, logical_type.ptr);
}
self
}
/// Sets the return type of the scalar function.
///
/// # Arguments
/// * `logical_type`: The return type of the scalar function.
pub fn set_return_type(&self, logical_type: &LogicalType) -> &Self {
unsafe {
duckdb_scalar_function_set_return_type(self.ptr, logical_type.ptr);
}
self
}
/// Sets the main function of the scalar function
///
/// # Arguments
/// * `function`: The function
pub fn set_function(
&self,
func: Option<unsafe extern "C" fn(info: duckdb_function_info, input: duckdb_data_chunk, output: duckdb_vector)>,
) -> &Self {
unsafe {
duckdb_scalar_function_set_function(self.ptr, func);
}
self
}
/// Creates a new empty scalar function.
pub fn new() -> Self {
Self {
ptr: unsafe { duckdb_create_scalar_function() },
}
}
/// Sets the name of the given scalar function.
///
/// # Arguments
/// * `name`: The name of the scalar function
pub fn set_name(&self, name: &str) -> &ScalarFunction {
unsafe {
let string = CString::from_vec_unchecked(name.as_bytes().into());
duckdb_scalar_function_set_name(self.ptr, string.as_ptr());
}
self
}
/// Assigns extra information to the scalar function that can be fetched during binding, etc.
///
/// # Arguments
/// * `extra_info`: The extra information
/// * `destroy`: The callback that will be called to destroy the bind data (if any)
///
/// # Safety
pub unsafe fn set_extra_info(&self, extra_info: *mut c_void, destroy: duckdb_delete_callback_t) {
duckdb_scalar_function_set_extra_info(self.ptr, extra_info, destroy);
}
}
impl Default for ScalarFunction {
fn default() -> Self {
Self::new()
}
}