Skip to content

Commit

Permalink
Fix issue with passing unpinned values
Browse files Browse the repository at this point in the history
  • Loading branch information
JAicewizard committed Jun 18, 2024
1 parent 601d3cd commit f3d23ae
Showing 1 changed file with 2 additions and 13 deletions.
15 changes: 2 additions & 13 deletions udtf.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import "C"

import (
"database/sql"
"fmt"
"reflect"
"runtime"
"runtime/cgo"
Expand Down Expand Up @@ -160,7 +159,6 @@ func (pv pinnedValue[T]) unpin() {
//export udf_destroy_data
func udf_destroy_data(data unsafe.Pointer) {
h := *(*cgo.Handle)(data)
fmt.Printf("DELETE: %p: %v\n", (*cgo.Handle)(data), h)
h.Value().(unpinner).unpin()
h.Delete()
}
Expand All @@ -177,7 +175,6 @@ func udf_bind_chunk(info C.duckdb_bind_info) {

func udfBindTyped[T tableSource](info C.duckdb_bind_info) {
extra_info := C.duckdb_bind_get_extra_info(info)
fmt.Printf("GET BIND HANDLE6: %p: %v\n", (*cgo.Handle)(C.duckdb_bind_get_extra_info(info)), extra_info)
h := *(*cgo.Handle)(extra_info)

tfunc := h.Value().(pinnedValue[tableFunction[T]]).value
Expand Down Expand Up @@ -256,14 +253,12 @@ func udfBindTyped[T tableSource](info C.duckdb_bind_info) {

handle := cgo.NewHandle(pinnedInstanceData)
pinnedInstanceData.pinner.Pin(&handle)
fmt.Printf("CREATE BIND HANDLE: %p: %v\n", &handle, handle)
C.duckdb_bind_set_bind_data(info, unsafe.Pointer(&handle), C.duckdb_delete_callback_t(C.udf_destroy_data))
}

//export udf_init
func udf_init(info C.duckdb_init_info) {
h := *(*cgo.Handle)(C.duckdb_init_get_bind_data(info))
fmt.Printf("GET BIND HANDLE5: %p: %v\n", (*cgo.Handle)(C.duckdb_init_get_bind_data(info)), h)
instance := h.Value().(pinnedValue[tableFunctionData]).value

columnCount := C.duckdb_init_get_column_count(info)
Expand All @@ -277,7 +272,6 @@ func udf_init(info C.duckdb_init_info) {
//export udf_init_threaded
func udf_init_threaded(info C.duckdb_init_info) {
h := *(*cgo.Handle)(C.duckdb_init_get_bind_data(info))
fmt.Printf("GET BIND HANDLE2: %p: %v\n", (*cgo.Handle)(C.duckdb_init_get_bind_data(info)), h)
instance := h.Value().(pinnedValue[tableFunctionData]).value

columnCount := C.duckdb_init_get_column_count(info)
Expand All @@ -293,7 +287,6 @@ func udf_init_threaded(info C.duckdb_init_info) {
//export udf_local_init
func udf_local_init(info C.duckdb_init_info) {
h := *(*cgo.Handle)(C.duckdb_init_get_bind_data(info))
fmt.Printf("GET BIND HANDLE3: %p: %v\n", (*cgo.Handle)(C.duckdb_init_get_bind_data(info)), h)
instance := h.Value().(pinnedValue[tableFunctionData]).value
localState := pinnedValue[any]{
pinner: &runtime.Pinner{},
Expand All @@ -307,7 +300,6 @@ func udf_local_init(info C.duckdb_init_info) {
//export udf_row_callback
func udf_row_callback(info C.duckdb_function_info, output C.duckdb_data_chunk) {
h := *(*cgo.Handle)(C.duckdb_function_get_bind_data(info))
fmt.Printf("GET BIND HANDLE4: %p: %v\n", (*cgo.Handle)(C.duckdb_function_get_bind_data(info)), h)
instance := h.Value().(pinnedValue[tableFunctionData]).value

var chunk DataChunk
Expand All @@ -317,15 +309,13 @@ func udf_row_callback(info C.duckdb_function_info, output C.duckdb_data_chunk) {
defer C.free(unsafe.Pointer(errstr))
C.duckdb_function_set_error(info, errstr)
}
fmt.Printf("GET BIND HANDLE4_ALT: %p: %v\n", (*cgo.Handle)(C.duckdb_function_get_bind_data(info)), *(*cgo.Handle)(C.duckdb_function_get_bind_data(info)))

row := Row{
chunk: chunk,
projection: instance.projection,
}

maxSize := C.duckdb_vector_size()
fmt.Printf("GET BIND HANDLE4_ALT: %p: %v\n", (*cgo.Handle)(C.duckdb_function_get_bind_data(info)), *(*cgo.Handle)(C.duckdb_function_get_bind_data(info)))

switch fun := instance.fun.(type) {
case RowTableSource:
Expand Down Expand Up @@ -358,17 +348,16 @@ func udf_row_callback(info C.duckdb_function_info, output C.duckdb_data_chunk) {
}
}
}
fmt.Printf("GET BIND HANDLE4_ALT: %p: %v\n", (*cgo.Handle)(C.duckdb_function_get_bind_data(info)), *(*cgo.Handle)(C.duckdb_function_get_bind_data(info)))

// since row.r points to one past the last value, it is also the size
C.duckdb_data_chunk_set_size(output, row.r)
fmt.Printf("GET BIND HANDLE4_ALT: %p: %v\n", (*cgo.Handle)(C.duckdb_function_get_bind_data(info)), *(*cgo.Handle)(C.duckdb_function_get_bind_data(info)))
}

//export udf_chunk_callback
func udf_chunk_callback(info C.duckdb_function_info, output C.duckdb_data_chunk) {
h := *(*cgo.Handle)(C.duckdb_function_get_bind_data(info))
fmt.Printf("GET BIND HANDLE1: %p: %v\n", (*cgo.Handle)(C.duckdb_function_get_bind_data(info)), h)
instance := h.Value().(pinnedValue[tableFunctionData]).value

var chunk DataChunk
err := chunk.initFromChunk(output)
if err != nil {
Expand Down

0 comments on commit f3d23ae

Please sign in to comment.