forked from marcboeker/go-duckdb
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathscalar_udf.go
More file actions
546 lines (472 loc) · 18.1 KB
/
Copy pathscalar_udf.go
File metadata and controls
546 lines (472 loc) · 18.1 KB
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
package duckdb
/*
void scalar_udf_callback(void *, void *, void *);
typedef void (*scalar_udf_callback_t)(void *, void *, void *);
void scalar_udf_delete_callback(void *);
typedef void (*scalar_udf_delete_callback_t)(void *);
void *scalar_udf_bind_copy_callback(void *);
typedef void *(*scalar_udf_bind_copy_callback_t)(void *);
void scalar_udf_bind_callback(void *);
typedef void (*scalar_udf_bind_callback_t)(void *);
*/
import "C"
import (
"context"
"database/sql"
"database/sql/driver"
"runtime"
"runtime/cgo"
"unsafe"
"github.com/duckdb/duckdb-go/v2/mapping"
)
// ScalarFuncConfig contains the fields to configure a user-defined scalar function.
type ScalarFuncConfig struct {
// InputTypeInfos contains Type information for each input parameter of the scalar function.
InputTypeInfos []TypeInfo
// ResultTypeInfo holds the Type information of the scalar function's result type.
ResultTypeInfo TypeInfo
// VariadicTypeInfo configures the number of input parameters.
// If this field is nil, then the input parameters match InputTypeInfos.
// Otherwise, the scalar function's input parameters are set to variadic, allowing any number of input parameters.
// The Type of the first len(InputTypeInfos) parameters is configured by InputTypeInfos, and all
// remaining parameters must match the variadic Type. To configure different variadic parameter types,
// you must set the VariadicTypeInfo's Type to TYPE_ANY.
VariadicTypeInfo TypeInfo
// Volatile sets the stability of the scalar function to volatile, if true.
// Volatile scalar functions might create a different result per row.
// E.g., random() is a volatile scalar function.
Volatile bool
// SpecialNullHandling disables the default NULL handling of scalar functions, if true.
// The default NULL handling is: NULL in, NULL out. I.e., if any input parameter is NULL, then the result is NULL.
SpecialNullHandling bool
}
// bindData holds bind data accessible during execution.
type bindData struct {
connId uint64
// We ignore the linter because we need to pass the context through C memory.
ctx context.Context //nolint:containedctx
}
// ScalarUDFArg contains scalar UDF argument metadata and the optional argument.
type ScalarUDFArg struct {
// Foldable is true, if the argument was folded into a value, else false.
Foldable bool
// Value is the folded argument value, or nil, if the argument is not foldable.
Value driver.Value
}
type (
// RowExecutorFn is the type for any row-based execution function.
// It takes the row values and returns the row execution result, or error.
RowExecutorFn func(values []driver.Value) (any, error)
// RowContextExecutorFn accepts a row-based execution function using a context.
// It takes a context and the row values, and returns the row execution result, or error.
RowContextExecutorFn func(ctx context.Context, values []driver.Value) (any, error)
// ChunkContextExecutorFn accepts a chunk-based execution function using a context.
// It takes a context and the chunk-batched input, and sets the execution result for that chunk.
// FIXME: It currently still operates row-by-row within one callback, meaning that it still fetches each row
// FIXME: via GetValue per column, and the result is also set on a per-row basis.
// FIXME: A genuinely vectorized API requires additional work on the data chunk setter and getter interfaces.
ChunkContextExecutorFn func(ctx context.Context, chunk *ChunkIteratorState) error
// ScalarBinderFn takes a (parent) context and the scalar function's arguments.
// It returns the possibly updated child context (can be the same as the parent).
// The child context can contain additional arbitrary data available during execution.
// Please ensure correct context inheritance.
ScalarBinderFn func(parentCtx context.Context, args []ScalarUDFArg) (context.Context, error)
)
// ScalarFuncExecutor contains the functions to execute a user-defined scalar function.
// It invokes its first non-nil member.
type ScalarFuncExecutor struct {
// RowExecutor accepts a row-based execution function of type RowExecutorFn.
RowExecutor RowExecutorFn
// RowContextExecutor accepts a row-based execution function of type RowContextExecutorFn.
RowContextExecutor RowContextExecutorFn
// ChunkContextExecutor accepts a chunk-based execution function of type ChunkContextExecutorFn.
ChunkContextExecutor ChunkContextExecutorFn
// Binder accepts a bind function of type ScalarBinderFn.
ScalarBinder ScalarBinderFn
}
// ScalarFunc is the user-defined scalar function interface.
// Any scalar function must implement a Config function, and an Executor function.
type ScalarFunc interface {
// Config returns ScalarFuncConfig to configure the scalar function.
Config() ScalarFuncConfig
// Executor returns ScalarFuncExecutor to execute the scalar function.
Executor() ScalarFuncExecutor
}
// scalarFuncContext wraps ScalarFunc and provides an execution context.
type scalarFuncContext struct {
f ScalarFunc
ctxStore *contextStore
}
// Config returns the ScalarFuncConfig of the scalar function.
func (s *scalarFuncContext) Config() ScalarFuncConfig {
return s.f.Config()
}
// RowExecutor returns a RowExecutorFn executing the scalar function.
// It uses the bindInfo to get the execution context.
func (s *scalarFuncContext) RowExecutor(info *bindData) (RowExecutorFn, error) {
e := s.f.Executor()
if e.RowExecutor != nil {
return e.RowExecutor, nil
}
if err := s.setCtx(info); err != nil {
return nil, err
}
return func(values []driver.Value) (any, error) {
return e.RowContextExecutor(info.ctx, values)
}, nil
}
func (s *scalarFuncContext) setCtx(info *bindData) error {
// Parent context cancellation propagates to children,
// therefore, it is enough to check the child context here.
if info.ctx == nil {
// No child context means that there is no custom bind function.
// Retrieve the parent context from the connection context store.
info.ctx = s.ctxStore.load(info.connId)
return nil
}
// Return any potential context error.
// If the error is not nil, then set it in the function info outside of this function.
return info.ctx.Err()
}
// RegisterScalarUDF registers a user-defined scalar function.
// *sql.Conn is the SQL connection on which to register the scalar function.
// name is the function name, and f is the scalar function's interface ScalarFunc.
// RegisterScalarUDF takes ownership of f, so you must pass it as a pointer.
func RegisterScalarUDF(c *sql.Conn, name string, f ScalarFunc) error {
function, err := createScalarFunc(c, name, f)
if err != nil {
return getError(errAPI, err)
}
defer mapping.DestroyScalarFunction(&function)
// Register the function on the underlying driver connection exposed by c.Raw.
err = c.Raw(func(driverConn any) error {
conn := driverConn.(*Conn)
state := mapping.RegisterScalarFunction(conn.conn, function)
if state == mapping.StateError {
return getError(errAPI, errScalarUDFCreate)
}
return nil
})
return err
}
// RegisterScalarUDFSet registers a set of user-defined scalar functions with the same name.
// This enables overloading of scalar functions.
// E.g., the function my_length() can have implementations like my_length(LIST(ANY)) and my_length(VARCHAR).
// *sql.Conn is the SQL connection on which to register the scalar function set.
// name is the function name of each function in the set.
// functions contains all ScalarFunc functions of the scalar function set.
func RegisterScalarUDFSet(c *sql.Conn, name string, functions ...ScalarFunc) error {
set := mapping.CreateScalarFunctionSet(name)
// Create each function and add it to the set.
for i, f := range functions {
function, err := createScalarFunc(c, name, f)
if err != nil {
mapping.DestroyScalarFunctionSet(&set)
return getError(errAPI, err)
}
state := mapping.AddScalarFunctionToSet(set, function)
mapping.DestroyScalarFunction(&function)
if state == mapping.StateError {
mapping.DestroyScalarFunctionSet(&set)
return getError(errAPI, addIndexToError(errScalarUDFAddToSet, i))
}
}
// Register the function set on the underlying driver connection exposed by c.Raw.
err := c.Raw(func(driverConn any) error {
conn := driverConn.(*Conn)
state := mapping.RegisterScalarFunctionSet(conn.conn, set)
mapping.DestroyScalarFunctionSet(&set)
if state == mapping.StateError {
return getError(errAPI, errScalarUDFCreateSet)
}
return nil
})
return err
}
//export scalar_udf_callback
func scalar_udf_callback(functionInfoPtr, inputPtr, outputPtr unsafe.Pointer) {
functionInfo := mapping.FunctionInfo{Ptr: functionInfoPtr}
input := mapping.DataChunk{Ptr: inputPtr}
output := mapping.Vector{Ptr: outputPtr}
// Initialize the input chunk.
var inputChunk DataChunk
if err := inputChunk.initFromDuckDataChunk(input, false); err != nil {
mapping.ScalarFunctionSetError(functionInfo, getError(errAPI, err).Error())
return
}
// Initialize the output chunk.
var outputChunk DataChunk
if err := outputChunk.initFromDuckVector(output, true); err != nil {
mapping.ScalarFunctionSetError(functionInfo, getError(errAPI, err).Error())
return
}
extraInfo := mapping.ScalarFunctionGetExtraInfo(functionInfo)
funcCtx := getPinned[*scalarFuncContext](extraInfo)
nullInNullOut := !funcCtx.Config().SpecialNullHandling
bindDataPtr := mapping.ScalarFunctionGetBindData(functionInfo)
pinnedBindData := getPinned[*bindData](bindDataPtr)
// Check if using chunk executor.
executor := funcCtx.f.Executor()
if executor.ChunkContextExecutor != nil {
executeChunk(funcCtx, pinnedBindData, &inputChunk, &outputChunk, functionInfo, nullInNullOut)
return
}
// Prepare the values.
length := len(inputChunk.columns)
values := make([]driver.Value, length)
// Execute the user-defined scalar function for each row.
f, err := funcCtx.RowExecutor(pinnedBindData)
if err != nil {
mapping.ScalarFunctionSetError(functionInfo, getError(errAPI, err).Error())
return
}
for rowIdx := range inputChunk.GetSize() {
// Get each column value.
nullRow := false
for colIdx := range length {
if values[colIdx], err = inputChunk.GetValue(colIdx, rowIdx); err != nil {
mapping.ScalarFunctionSetError(functionInfo, getError(errAPI, err).Error())
return
}
// NULL handling.
if nullInNullOut && values[colIdx] == nil {
if err = outputChunk.SetValue(0, rowIdx, nil); err != nil {
mapping.ScalarFunctionSetError(functionInfo, getError(errAPI, err).Error())
return
}
nullRow = true
break
}
}
if nullRow {
continue
}
// Execute the user-defined scalar function.
if val, e := f(values); e != nil {
mapping.ScalarFunctionSetError(functionInfo, getError(errAPI, e).Error())
return
} else {
// Write the result to the output chunk.
if err = outputChunk.SetValue(0, rowIdx, val); err != nil {
mapping.ScalarFunctionSetError(functionInfo, getError(errAPI, err).Error())
return
}
}
}
}
// executeChunk handles chunk-based execution of scalar UDFs.
func executeChunk(funcCtx *scalarFuncContext, bindInfo *bindData,
inputChunk, outputChunk *DataChunk,
functionInfo mapping.FunctionInfo, nullInNullOut bool,
) {
// Set the context.
if err := funcCtx.setCtx(bindInfo); err != nil {
mapping.ScalarFunctionSetError(functionInfo, getError(errAPI, err).Error())
return
}
// Create chunk wrapper.
// When nullInNullOut is enabled, the Rows() iterator automatically skips
// rows with NULL inputs and sets their result to NULL.
chunk := &ChunkIteratorState{
r: Row{
chunk: inputChunk,
rowIdx: mapping.IdxT(0),
},
output: &outputChunk.columns[0],
nullInNullOut: nullInNullOut,
args: make([]driver.Value, inputChunk.ColumnCount()),
}
// Execute - user iterates over rows, each row has pre-fetched Args.
if err := funcCtx.f.Executor().ChunkContextExecutor(bindInfo.ctx, chunk); err != nil {
mapping.ScalarFunctionSetError(functionInfo, getError(errAPI, err).Error())
return
}
}
//export scalar_udf_delete_callback
func scalar_udf_delete_callback(info unsafe.Pointer) {
h := (*cgo.Handle)(info)
h.Value().(unpinner).unpin()
h.Delete()
}
//export scalar_udf_bind_copy_callback
func scalar_udf_bind_copy_callback(dataPtr unsafe.Pointer) unsafe.Pointer {
// Copy and pin the bind data.
data := getPinned[*bindData](dataPtr)
dataCopy := *data
value := pinnedValue[*bindData]{
pinner: &runtime.Pinner{},
value: &dataCopy,
}
h := cgo.NewHandle(value)
value.pinner.Pin(&h)
return unsafe.Pointer(&h)
}
//export scalar_udf_bind_callback
func scalar_udf_bind_callback(bindInfoPtr unsafe.Pointer) {
bindInfo := mapping.BindInfo{Ptr: bindInfoPtr}
var clientCtx mapping.ClientContext
mapping.ScalarFunctionGetClientContext(bindInfo, &clientCtx)
defer mapping.DestroyClientContext(&clientCtx)
// We need the connId to retrieve the correct parent context.
// Then, we store the child context in data.
connId := mapping.ClientContextGetConnectionId(clientCtx)
data := bindData{connId: uint64(connId)}
extraInfo := mapping.ScalarFunctionBindGetExtraInfo(bindInfo)
funcCtx := getPinned[*scalarFuncContext](extraInfo)
// Get any custom bind data by invoking the custom bind function.
if funcCtx.f.Executor().ScalarBinder != nil {
bindCtx, err := funcCtx.bind(clientCtx, bindInfo, uint64(connId))
if err != nil {
mapping.ScalarFunctionBindSetError(bindInfo, err.Error())
return
}
data.ctx = bindCtx
}
// Set the copy callback of the bind info.
copyPtr := unsafe.Pointer(C.scalar_udf_bind_copy_callback_t(C.scalar_udf_bind_copy_callback))
mapping.ScalarFunctionSetBindDataCopy(bindInfo, copyPtr)
// Pin the bind data.
value := pinnedValue[*bindData]{
pinner: &runtime.Pinner{},
value: &data,
}
h := cgo.NewHandle(value)
value.pinner.Pin(&h)
// Set the bind data.
deleteCallbackPtr := unsafe.Pointer(C.scalar_udf_delete_callback_t(C.scalar_udf_delete_callback))
mapping.ScalarFunctionSetBindData(bindInfo, unsafe.Pointer(&h), deleteCallbackPtr)
}
func getScalarUDFArg(clientCtx mapping.ClientContext, bindInfo mapping.BindInfo, index int) (ScalarUDFArg, error) {
expr := mapping.ScalarFunctionBindGetArgument(bindInfo, mapping.IdxT(index))
defer mapping.DestroyExpression(&expr)
arg := ScalarUDFArg{
Foldable: mapping.ExpressionIsFoldable(expr),
}
if !arg.Foldable {
return arg, nil
}
// Fold the argument.
var v mapping.Value
errorData := mapping.ExpressionFold(clientCtx, expr, &v)
defer mapping.DestroyValue(&v)
err := errorDataError(errorData)
if err != nil {
return arg, err
}
// Get the mapping.Value as a driver.Value and return.
arg.Value, err = getValue(v)
if err != nil {
return arg, err
}
return arg, nil
}
func (s *scalarFuncContext) bind(clientCtx mapping.ClientContext, bindInfo mapping.BindInfo, connId uint64) (context.Context, error) {
ctx := s.ctxStore.load(connId)
argCount := mapping.ScalarFunctionBindGetArgumentCount(bindInfo)
var args []ScalarUDFArg
for i := range int(argCount) {
arg, err := getScalarUDFArg(clientCtx, bindInfo, i)
if err != nil {
return nil, err
}
args = append(args, arg)
}
bindCtx, err := s.f.Executor().ScalarBinder(ctx, args)
if err != nil {
return nil, err
}
// Propagate the parent context, if the child context is nil.
if ctx != nil && bindCtx == nil {
bindCtx = ctx
}
return bindCtx, nil
}
func registerInputParams(config ScalarFuncConfig, f mapping.ScalarFunction) error {
// Set variadic input parameters.
if config.VariadicTypeInfo != nil {
t := config.VariadicTypeInfo.logicalType()
mapping.ScalarFunctionSetVarargs(f, t)
mapping.DestroyLogicalType(&t)
}
// Early-out, if the function does not take any (non-variadic) parameters.
if config.InputTypeInfos == nil {
return nil
}
if len(config.InputTypeInfos) == 0 {
return nil
}
// Set non-variadic input parameters.
for i, info := range config.InputTypeInfos {
if info == nil {
return addIndexToError(errScalarUDFInputTypeIsNil, i)
}
t := info.logicalType()
mapping.ScalarFunctionAddParameter(f, t)
mapping.DestroyLogicalType(&t)
}
return nil
}
func registerResultParams(config ScalarFuncConfig, f mapping.ScalarFunction) error {
if config.ResultTypeInfo == nil {
return errScalarUDFResultTypeIsNil
}
if config.ResultTypeInfo.InternalType() == TYPE_ANY {
return errScalarUDFResultTypeIsANY
}
t := config.ResultTypeInfo.logicalType()
mapping.ScalarFunctionSetReturnType(f, t)
mapping.DestroyLogicalType(&t)
return nil
}
func createScalarFunc(c *sql.Conn, name string, f ScalarFunc) (mapping.ScalarFunction, error) {
if name == "" {
return mapping.ScalarFunction{}, errScalarUDFNoName
}
if f == nil {
return mapping.ScalarFunction{}, errScalarUDFIsNil
}
if f.Executor().RowExecutor == nil && f.Executor().RowContextExecutor == nil && f.Executor().ChunkContextExecutor == nil {
return mapping.ScalarFunction{}, errScalarUDFNoExecutor
}
function := mapping.CreateScalarFunction()
mapping.ScalarFunctionSetName(function, name)
// Configure the scalar function.
config := f.Config()
if err := registerInputParams(config, function); err != nil {
mapping.DestroyScalarFunction(&function)
return function, err
}
if err := registerResultParams(config, function); err != nil {
mapping.DestroyScalarFunction(&function)
return function, err
}
if config.SpecialNullHandling {
mapping.ScalarFunctionSetSpecialHandling(function)
}
if config.Volatile {
mapping.ScalarFunctionSetVolatile(function)
}
// Set the bind callback.
bindPtr := unsafe.Pointer(C.scalar_udf_bind_callback_t(C.scalar_udf_bind_callback))
mapping.ScalarFunctionSetBind(function, bindPtr)
// Set the function callback.
functionPtr := unsafe.Pointer(C.scalar_udf_callback_t(C.scalar_udf_callback))
mapping.ScalarFunctionSetFunction(function, functionPtr)
// Get the context store of the connection.
ctxStore, err := contextStoreFromConn(c)
if err != nil {
mapping.DestroyScalarFunction(&function)
return function, err
}
// Pin the ScalarFunc f.
value := pinnedValue[*scalarFuncContext]{
pinner: &runtime.Pinner{},
value: &scalarFuncContext{f: f, ctxStore: ctxStore},
}
h := cgo.NewHandle(value)
value.pinner.Pin(&h)
// Set the execution data, which is the ScalarFunc f.
deleteCallbackPtr := unsafe.Pointer(C.scalar_udf_delete_callback_t(C.scalar_udf_delete_callback))
mapping.ScalarFunctionSetExtraInfo(function, unsafe.Pointer(&h), deleteCallbackPtr)
return function, nil
}