Skip to content

Commit bd4c132

Browse files
committed
Fix aggregates on Linux.
No arguments are ever passed to the final function so values was a void pointer causing sqlite3_value_type to fail. This simply stopped trying to collect args. Also changed the order of the function definitions to something that made more sense.
1 parent c7d205a commit bd4c132

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

src/UDF.jl

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,26 @@ sqlreturn(context, val::Vector{UInt8}) = sqlite3_result_blob(context, val)
3535
sqlreturn(context, val::Bool) = sqlreturn(context, int(val))
3636
sqlreturn(context, val) = sqlreturn(context, sqlserialize(val))
3737

38+
# Internal method for generating an SQLite scalar function from
39+
# a Julia function name
40+
function scalarfunc(func,fsym=symbol(string(func)))
41+
# check if name defined in Base so we don't clobber Base methods
42+
nm = isdefined(Base,fsym) ? :(Base.$fsym) : fsym
43+
return quote
44+
#nm needs to be a symbol or expr, i.e. :sin or :(Base.sin)
45+
function $(nm)(context::Ptr{Void}, nargs::Cint, values::Ptr{Ptr{Void}})
46+
args = [SQLite.sqlvalue(values, i) for i in 1:nargs]
47+
ret = $(func)(args...)
48+
SQLite.sqlreturn(context, ret)
49+
nothing
50+
end
51+
end
52+
end
53+
function scalarfunc(expr::Expr)
54+
f = eval(expr)
55+
return scalarfunc(f)
56+
end
57+
3858
# convert a bytearray to an int arr[1] is 256^0, arr[2] is 256^1...
3959
# TODO: would making this a method of convert needlessly pollute the Base namespace?
4060
function bytestoint(arr::Vector{UInt8})
@@ -120,9 +140,6 @@ function finalfunc(init, func, fsym=symbol(string(func)*"_final"))
120140
nm = isdefined(Base,fsym) ? :(Base.$fsym) : fsym
121141
return quote
122142
function $(nm)(context::Ptr{Void}, nargs::Cint, values::Ptr{Ptr{Void}})
123-
# TODO: I don't think arguments are ever passed to this function,
124-
# should we leave them in anyway?
125-
args = [sqlvalue(context, i) for i in 1:nargs]
126143
acptr = sqlite3_aggregate_context(context, 0)
127144
# step function wasn't run
128145
if acptr === C_NULL
@@ -145,7 +162,7 @@ function finalfunc(init, func, fsym=symbol(string(func)*"_final"))
145162
unsafe_copy!(pointer(acvalbuf), valptr, valsize)
146163

147164
acval = sqldeserialize(acvalbuf)
148-
ret = $(func)(acval, args...)
165+
ret = $(func)(acval)
149166
c_free(valptr)
150167
sqlreturn(context, ret)
151168
end
@@ -154,26 +171,6 @@ function finalfunc(init, func, fsym=symbol(string(func)*"_final"))
154171
end
155172
end
156173

157-
# Internal method for generating an SQLite scalar function from
158-
# a Julia function name
159-
function scalarfunc(func,fsym=symbol(string(func)))
160-
# check if name defined in Base so we don't clobber Base methods
161-
nm = isdefined(Base,fsym) ? :(Base.$fsym) : fsym
162-
return quote
163-
#nm needs to be a symbol or expr, i.e. :sin or :(Base.sin)
164-
function $(nm)(context::Ptr{Void}, nargs::Cint, values::Ptr{Ptr{Void}})
165-
args = [SQLite.sqlvalue(values, i) for i in 1:nargs]
166-
ret = $(func)(args...)
167-
SQLite.sqlreturn(context, ret)
168-
nothing
169-
end
170-
end
171-
end
172-
function scalarfunc(expr::Expr)
173-
f = eval(expr)
174-
return scalarfunc(f)
175-
end
176-
177174
# User-facing macro for convenience in registering a simple function
178175
# with no configurations needed
179176
macro register(db, func)

0 commit comments

Comments
 (0)