This repository has been archived by the owner on Dec 5, 2022. It is now read-only.
/
Query.jl
361 lines (323 loc) · 11 KB
/
Query.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
module Query
using Data
using Match
using Base.Cartesian
# gallop cribbed from http://www.frankmcsherry.org/dataflow/relational/join/2015/04/11/genericjoin.html
function gallop{T}(column::Vector{T}, value::T, lo::Int, hi::Int, threshold)
c = -1
@inbounds if (lo < hi) && (c2 = cmp(column[lo], value); c *= c2; c2 < threshold)
step = 1
while (lo + step < hi) && (c2 = cmp(column[lo + step], value); c *= c2; c2 < threshold)
lo = lo + step
step = step << 1
end
step = step >> 1
while step > 0
if (lo + step < hi) && (c2 = cmp(column[lo + step], value); c *= c2; c2 < threshold)
lo = lo + step
end
step = step >> 1
end
lo += 1
end
(lo, c)
end
function project(need_more_results, columns, los, his, next_los, next_his, var, body)
for (column, lo, hi, next_lo, next_hi) in zip(columns, los, his, next_los, next_his)
body = quote
$next_lo, c = gallop($column, $var, $lo, $hi, 0)
if (c == 0)
$next_hi, _ = gallop($column, $var, $next_lo+1, $hi, 1)
$body
end
end
end
body
end
function intersect(need_more_results, columns, los, his, next_los, next_his, var, body)
n = length(columns)
columns_rot = [columns[1+mod(ix-2,n)] for ix in 1:n]
next_los_rot = [next_los[1+mod(ix-2,n)] for ix in 1:n]
quote
@inbounds begin
$([:($next_lo = $lo) for (next_lo, lo) in zip(next_los, los)]...)
total = 1
while $need_more_results
if total == $n
$([:(($next_hi, _) = gallop($column, $column[$next_lo], $next_lo+1, $hi, 1)) for (next_hi, column, next_lo, hi) in zip(next_his, columns, next_los, his)]...)
$var = $(columns[1])[$(next_los[1])]
$body
$([:($next_lo = $next_hi) for (next_lo, next_hi) in zip(next_los, next_his)]...)
$([:(if $next_lo >= $hi; break; end) for (next_lo, hi) in zip(next_los, his)]...)
total = 1
end
$([quote
if total < $n
$next_lo, c = gallop($column, $column_rot[$next_lo_rot], $next_lo, $hi, 0)
total = (c == 0) ? total + 1 : 1
if $next_lo >= $hi; break; end
end
end for (next_lo, column, column_rot, next_lo_rot, hi) in zip(next_los, columns, columns_rot, next_los_rot, his)]...)
end
end
end
end
function collect_vars(expr, vars)
if isa(expr, Symbol)
push!(vars, expr)
elseif isa(expr, Expr) && expr.head != :quote
for arg in expr.args
collect_vars(arg, vars)
end
end
end
function collect_vars(expr)
vars = []
collect_vars(expr, vars)
vars
end
function parse_var(expr)
@match expr begin
Expr(:(::), [var, typ], _) => var
_ => expr
end
end
function parse_typ(expr)
@match expr begin
Expr(:(::), [var, typ], _) => typ
_ => :Any
end
end
type Row; name; vars; num_keys; end
type When; expr; vars; end
type Assign; var; expr; vars; end
type In; var; expr; vars; end
type Hint; vars; end
type Return; name; vars; typs; num_keys; end
function plan_query(query)
# unwrap block
lines = @match query begin
Expr(:block, lines, _) => lines
line => [line]
end
# parse
clauses = []
for line in lines
clause = @match line begin
Expr(:line, _, _) => :line
Expr(:call, [:in, var, expr], _) => In(var, expr, collect_vars(expr))
Expr(:(=), [var, expr], _) => Assign(var, expr, collect_vars(expr))
Expr(:macrocall, [head, args...], _) => @match head begin
Symbol("@when") => When(args[1], collect_vars(args[1]))
Symbol("@hint") => Hint(args)
_ => error("Don't know what to do with $head")
end
Expr(:return, [expr], _) => begin
(name, keys, vals) = parse_relation(expr)
typed_vars = Any[keys..., vals...]
Return(name, map(parse_var, typed_vars), map(parse_typ, typed_vars), length(keys))
end
_ => begin
(name, keys, vals) = parse_relation(line)
Row(name, Any[keys..., vals...], length(keys))
end
end
if clause != :line
push!(clauses, clause)
end
end
# check all assignments are to single vars
for clause in clauses
if typeof(clause) in [In, Assign]
@assert isa(clause.var, Symbol)
end
end
# rewrite expressions nested in Row
old_clauses = clauses
clauses = []
for clause in old_clauses
if typeof(clause) in [Row, Return]
for (ix, expr) in enumerate(clause.vars)
if !isa(expr, Symbol)
var = gensym("constant")
clause.vars[ix] = var
value = @match expr begin
Expr(:$, [value], _) => value
value => value
end
insert!(clauses, 1, Assign(var, value, collect_vars(value)))
end
end
end
push!(clauses, clause)
end
# collect vars created in this query
created_vars = Set()
for clause in clauses
if typeof(clause) in [Row]
for var in clause.vars
push!(created_vars, var)
end
end
if typeof(clause) in [Assign, In]
push!(created_vars, clause.var)
end
end
delete!(created_vars, :_) # _ is a wildcard, not a real var
# collect vars mentioned in this query, in order of mention
mentioned_vars = []
for clause in clauses
if typeof(clause) in [Row, When, Assign, In, Hint]
for var in clause.vars
push!(mentioned_vars, var)
end
end
if typeof(clause) in [Assign, In]
push!(mentioned_vars, clause.var)
end
end
# use mention order to decide execution order
vars = unique((var for var in mentioned_vars if var in created_vars))
# add a return if needed
returns = [clause for clause in clauses if typeof(clause) == Return]
if length(returns) == 0
return_clause = Return((), vars, [:Any for _ in vars], length(vars))
elseif length(returns) == 1
return_clause = returns[1]
else
error("Too many returns: $returns")
end
# use types of return relation if available
if return_clause.name != ()
for (ix, var) in enumerate(return_clause.vars)
return_clause.typs[ix] = :(eltype($(return_clause.name)[$ix]))
end
end
# collect clauses that assign a value to a var before intersect
var_assigned_by = Dict()
for clause in clauses
if typeof(clause) in [Assign, In]
@assert !haskey(var_assigned_by, clause.var) # only one assignment per var
var_assigned_by[clause.var] = clause
end
end
# for each var, collect list of relation/column pairs that need to be intersected
relation_sources = Dict(var => Tuple{Int64, Int64}[] for var in vars)
for (clause_ix, clause) in enumerate(clauses)
if typeof(clause) == Row
for (var_ix, var) in enumerate(clause.vars)
if var != :_
push!(relation_sources[var], (clause_ix, var_ix))
end
end
end
end
# for each Row clause, figure out what order to sort the index in
sort_orders = Dict(clause_ix => Int64[] for clause_ix in 1:length(clauses))
for var in vars
for (clause_ix, var_ix) in relation_sources[var]
push!(sort_orders[clause_ix], var_ix)
end
end
# --- codegen ---
# for each Row clause, get the correct index and create initial ranges
index_inits = []
for (clause_ix, clause) in enumerate(clauses)
if typeof(clause) == Row
sort_order = sort_orders[clause_ix]
index_init = :(local $(Symbol("index_$clause_ix")) = index($(esc(clause.name)), $sort_order))
columns = [:(local $(Symbol("index_$(clause_ix)_$(var_ix)")) = $(Symbol("index_$clause_ix"))[$var_ix]) for var_ix in sort_order]
lo_init = :(local $(Symbol("lo_$(clause_ix)_0")) = 1)
hi_init = :(local $(Symbol("hi_$(clause_ix)_0")) = 1 + length($(Symbol("index_$(clause_ix)_$(first(sort_order))"))))
push!(index_inits, index_init, columns..., lo_init, hi_init)
end
end
# initialize arrays for storing results
results_inits = []
for (ix, typ) in enumerate(return_clause.typs)
results_init = :(local $(Symbol("results_$ix")) = Vector{$(esc(typ))}())
push!(results_inits, results_init)
end
# declare vars
var_inits = [:(local $(esc(var))) for var in vars]
# figure out at which point in the variable order each When clause can be run
whens = [[] for _ in vars]
for clause in clauses
if typeof(clause) == When
var_ix = maximum(indexin(clause.vars, vars))
push!(whens[var_ix], clause.expr)
end
end
# figure out at which point in the variable order we have all the variables we need to return
return_after = maximum(push!(indexin(return_clause.vars, vars), 0))
# store results
body = quote
$([:(push!($(Symbol("results_$ix")), $(esc(var))))
for (ix, var) in enumerate(return_clause.vars)]...)
need_more_results = false
end
# build up the main loop from the inside out
for var_ix in length(vars):-1:1
var = vars[var_ix]
# run any When clauses
for when in whens[var_ix]
body = :(if $(esc(when)); $body; end)
end
# after return_after, only need to find one solution, not all solutions
if var_ix == return_after
body = quote
need_more_results = true
$body
end
end
need_more_results = var_ix > return_after ? :need_more_results : true
# run the intersection for this variable
clause = get(var_assigned_by, var, ())
columns = [Symbol("index_$(clause_ix)_$(var_ix)") for (clause_ix, var_ix) in relation_sources[var]]
range_ixes = [(clause_ix, findfirst(sort_orders[clause_ix], var_ix)) for (clause_ix, var_ix) in relation_sources[var]]
los = [Symbol("lo_$(clause_ix)_$(range_ix-1)") for (clause_ix, range_ix) in range_ixes]
next_los = [Symbol("lo_$(clause_ix)_$(range_ix)") for (clause_ix, range_ix) in range_ixes]
his = [Symbol("hi_$(clause_ix)_$(range_ix-1)") for (clause_ix, range_ix) in range_ixes]
next_his = [Symbol("hi_$(clause_ix)_$(range_ix)") for (clause_ix, range_ix) in range_ixes]
if typeof(clause) == Assign
body = :(let
$(esc(var)) = $(esc(clause.expr))
$(project(need_more_results, columns, los, his, next_los, next_his, esc(var), body))
end)
elseif typeof(clause) == In
body = :(let
local iter = $(esc(clause.expr))
local state = start(iter)
while $need_more_results && !done(iter, state)
($(esc(var)), state) = next(iter, state)
$(project(need_more_results, columns, los, his, next_los, next_his, esc(var), body))
end
end)
else
body = intersect(need_more_results, columns, los, his, next_los, next_his, esc(var), body)
end
end
# put everything together
results_symbols = [Symbol("results_$ix") for (ix, var) in enumerate(return_clause.vars)]
result = :(Relation(tuple($(results_symbols...)), $(return_clause.num_keys)))
quote
let
$(index_inits...)
$(results_inits...)
let
$(var_inits...) # declare vars local in here so they can't shadow relation names
$body
end
$(if return_clause.name != ()
:(merge!($(esc(return_clause.name)), $result))
else
result
end)
end
end
end
macro query(query)
plan_query(query)
end
export @query
end