Is it hard to support Julia UDFs in DuckDB?

Update: I opened a thread to gather ideas how this can be implemented properly. The main problem seems to be that @cfunction only accepts objects in global scope (at leat to my understanding).

Not sure if a simple API like

fun = @create_scalar_function my_sum(::Int64, ::Int64)::Int64

is possible due to this limitation (which might be a bummer :confused: )

macro _create_scalar_function_new(func_expr)

    func, parameters, return_type = _udf_parse_function_expr(func_expr)
    func_esc = esc(func)
    func_name = string(func)
    parameter_names = [p[1] for p in parameters]
    parameter_types = [p[2] for p in parameters]
    parameter_types_vec = Expr(:vect, parameter_types...) # create a vector expression, e.g. [Int, Int]
    wrapper_expr = _udf_generate_wrapper(func_expr)

    quote
        local wrapper = $(wrapper_expr)
        local fun = ScalarFunction($func_name, $parameter_types_vec, $return_type, $func_esc, wrapper)

        wrapper(C_NULL, C_NULL, C_NULL) # works
        fun.wrapper(C_NULL, C_NULL, C_NULL) # works

        # Everything below only works in GLOBAL scope in the REPL
        # ptr = @cfunction(fun.wrapper, Cvoid, (duckdb_function_info, duckdb_data_chunk, duckdb_vector))
        # ccall(ptr, Cvoid, (duckdb_function_info, duckdb_data_chunk, duckdb_vector), C_NULL, C_NULL, C_NULL) # doesnt work
        # duckdb_scalar_function_set_function(fun.handle, ptr)
        
        fun
    end

end

2 Likes