Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Implementation of window functions. #70

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/SQLite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,12 @@ type SQLiteStmt{T}
sql::T
end

sqliteprepare(db,sql,stmt,null) =
include("window.jl")
export window

sqliteprepare(db,sql,stmt,null) =
@CHECK db sqlite3_prepare_v2(db.handle,utf8(sql),stmt,null)
sqliteprepare(db::SQLiteDB{UTF16String},sql,stmt,null) =
sqliteprepare(db::SQLiteDB{UTF16String},sql,stmt,null) =
@CHECK db sqlite3_prepare16_v2(db.handle,utf16(sql),stmt,null)

function SQLiteStmt{T}(db::SQLiteDB{T},sql::AbstractString)
Expand Down Expand Up @@ -180,7 +183,7 @@ const SERIALIZATION = UInt8[0x11,0x01,0x02,0x0d,0x53,0x65,0x72,0x69,0x61,0x6c,0x
function sqldeserialize(r)
ret = ccall(:memcmp, Int32, (Ptr{UInt8},Ptr{UInt8}, UInt),
SERIALIZATION, r, min(18,length(r)))

if ret == 0
v = deserialize(IOBuffer(r))
return v.object
Expand All @@ -205,7 +208,7 @@ function query(db::SQLiteDB,sql::AbstractString, values=[])
end
while status == SQLITE_ROW
for i = 1:ncols
t = sqlite3_column_type(stmt.handle,i-1)
t = sqlite3_column_type(stmt.handle,i-1)
if t == SQLITE_INTEGER
r = sqlite3_column_int64(stmt.handle,i-1)
elseif t == SQLITE_FLOAT
Expand Down
62 changes: 62 additions & 0 deletions src/window.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
addrange(i::Integer, r::UnitRange) = (i + r.start):(i + r.stop)
addrange(i::Integer, r::StepRange) = (i + r.start):r.step:(i + r.stop)

function fetchrow(stmt::SQLiteStmt, ncols::Integer)
row = Any[]
for col in 1:ncols
t = sqlite3_column_type(stmt.handle,col-1)
if t == SQLITE_INTEGER
r = sqlite3_column_int64(stmt.handle,col-1)
elseif t == SQLITE_FLOAT
r = sqlite3_column_double(stmt.handle,col-1)
elseif t == SQLITE_TEXT
#TODO: have a way to return text16?
r = bytestring(sqlite3_column_text(stmt.handle,col-1))
elseif t == SQLITE_BLOB
blob = sqlite3_column_blob(stmt.handle,col-1)
b = sqlite3_column_bytes(stmt.handle,col-1)
buf = zeros(UInt8,b)
unsafe_copy!(pointer(buf), convert(Ptr{UInt8},blob), b)
r = sqldeserialize(buf)
else
r = NULL
end
push!(row, r)
end
status = sqlite3_step(stmt.handle)
status, row
end

function window{S<:AbstractString}(
db::SQLiteDB, cb::Base.Callable, range::OrdinalRange,
table::AbstractString, columns::Vector{S}, data=nothing,
)
@assert !isempty(columns) "you must specifiy at least one column"
nrows = query(db, string("SELECT COUNT(*) FROM ", table))[1][1]
stmt = SQLiteStmt(db, string("SELECT ", join(columns, ", "), " FROM ", table))
status = execute(stmt)
ncols = sqlite3_column_count(stmt.handle)
# TODO: we can calculate how many rows we need and do this in place
tablerows = Array{Any,1}[]
results = Any[]
latest_row = 0
for start_row in 1:(nrows + range.start - range.stop)
# TODO: we can do this in place aswell
curwindow = Array{Any,1}[]
# find relevent rows for window
for row in range
# only load rows as they are needed
while latest_row < row + start_row - 1 && status == SQLITE_ROW
status, row_values = fetchrow(stmt, ncols)
latest_row += 1
push!(tablerows, row_values)
end
status == SQLITE_ROW || status == SQLITE_DONE || sqliteerror(stmt.db)
push!(curwindow, tablerows[row])
end
push!(results, cb(curwindow, range, data))
# get rid of rows we no longer need
shift!(tablerows)
end
results
end