Skip to content

Commit

Permalink
create udf utility file
Browse files Browse the repository at this point in the history
  • Loading branch information
taniabogatsch committed Sep 23, 2024
1 parent 5b389f0 commit 7906697
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
16 changes: 0 additions & 16 deletions pinner.go

This file was deleted.

12 changes: 2 additions & 10 deletions scalar_udf.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,6 @@ func RegisterScalarUDFSet(c *sql.Conn, name string, functions ...ScalarFunc) err
return err
}

func setFuncError(function_info C.duckdb_function_info, msg string) {
err := C.CString(msg)
C.duckdb_scalar_function_set_error(function_info, err)
C.duckdb_free(unsafe.Pointer(err))
}

//export scalar_udf_callback
func scalar_udf_callback(function_info C.duckdb_function_info, input C.duckdb_data_chunk, output C.duckdb_vector) {
extraInfo := C.duckdb_scalar_function_get_extra_info(function_info)
Expand Down Expand Up @@ -198,10 +192,8 @@ func scalar_udf_callback(function_info C.duckdb_function_info, input C.duckdb_da
}

//export scalar_udf_delete_callback
func scalar_udf_delete_callback(extraInfo unsafe.Pointer) {
h := (*cgo.Handle)(extraInfo)
h.Value().(unpinner).unpin()
h.Delete()
func scalar_udf_delete_callback(info unsafe.Pointer) {
udf_delete_callback(info)
}

func registerInputParams(config ScalarFuncConfig, f C.duckdb_scalar_function) error {
Expand Down
37 changes: 37 additions & 0 deletions udf_util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package duckdb

/*
#include <duckdb.h>
*/
import "C"

import (
"runtime"
"runtime/cgo"
"unsafe"
)

type pinnedValue[T any] struct {
pinner *runtime.Pinner
value T
}

type unpinner interface {
unpin()
}

func (v pinnedValue[T]) unpin() {
v.pinner.Unpin()
}

func setFuncError(function_info C.duckdb_function_info, msg string) {
err := C.CString(msg)
C.duckdb_scalar_function_set_error(function_info, err)
C.duckdb_free(unsafe.Pointer(err))
}

func udf_delete_callback(info unsafe.Pointer) {
h := (*cgo.Handle)(info)
h.Value().(unpinner).unpin()
h.Delete()
}

0 comments on commit 7906697

Please sign in to comment.