/
query.cljc
922 lines (837 loc) · 39.6 KB
/
query.cljc
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
(ns ^{:doc "Implements query operations based on data accessible through the Graph protocol."
:author "Paula Gearon"}
asami.query
(:require [asami.graph :as gr]
[asami.internal :as internal]
[asami.planner :as planner :refer [Bindings PatternOrBindings Aggregate HasVars get-vars]]
[asami.projection :as projection :refer [EPVPattern Results Value Var epv-pattern? vartest?]]
[zuko.logging :as log :include-macros true]
[zuko.sandbox :as sandbox]
[zuko.schema :refer [FilterPattern Pattern EvalPattern eval-pattern?
filter-pattern? op-pattern?]]
[zuko.util :refer [fn-for]]
[clojure.set :as set]
[schema.core :as s :include-macros true]
#?(:clj [clojure.edn :as edn]
:cljs [cljs.reader :as edn])))
(def ^:dynamic *env* {})
(def ^:dynamic *select-distinct* distinct)
(def ^:dynamic *override-restrictions* false)
(def ^:const identity-binding (with-meta [[]] {:cols []}))
(def ^:const null-value nil)
(defn vars [s] (filter vartest? s))
(defn plain-var [v]
(let [n (name v)]
(if (#{\* \+} (last n))
(symbol (namespace v) (subs n 0 (dec (count n))))
v)))
(defn plain-vars [v] (map plain-var (vars v)))
(defn find-vars [f] (set (plain-vars f)))
(defn op-error
[pattern]
(throw (ex-info "Unknown operator" {:op (first pattern)
:args (rest pattern)})))
(defn pattern-error
[pattern]
(throw (ex-info (str "Unknown pattern type in query: " pattern) {:pattern pattern})))
(declare operators operand-vars)
(extend-protocol HasVars
#?(:clj Object :cljs object)
(get-vars
[pattern]
(cond
;; bindings will pass the epv-pattern? test, so must check for this first
(planner/bindings? pattern) (set (:cols (meta pattern)))
(epv-pattern? pattern) (find-vars pattern)
(filter-pattern? pattern) (or (:vars (meta pattern)) (find-vars (first pattern)))
(op-pattern? pattern) (if (operators (first pattern))
(operand-vars pattern)
(op-error pattern))
(eval-pattern? pattern) (let [[expr _] pattern]
(filter vartest? expr))
:default (pattern-error pattern)))
nil
(get-vars [pattern] nil))
(defn operand-vars
[o]
(first
(reduce (fn [[acc seen? :as s] v]
(if (seen? v)
s
[(conj acc v) (conj seen? v)]))
[[] #{}]
(mapcat get-vars (rest o)))))
(s/defn without :- [s/Any]
"Returns a sequence minus a specific element"
[e :- s/Any
s :- [s/Any]]
(remove (partial = e) s))
(s/defn modify-pattern :- [s/Any]
"Creates a new EPVPattern from an existing one, based on existing bindings.
Uses the mapping to copy from columns in 'existing' to overwrite variables in 'pattern'.
The variable locations have already been found and are in the 'mapping' argument"
[existing :- [Value]
mapping :- {s/Num s/Num}
pattern :- EPVPattern]
;; TODO: this is in an inner loop. Is it faster to:
;; (reduce (fn [p [f t]] (assoc p f t)) pattern mapping)
(map-indexed (fn [n v]
(if-let [x (mapping n)]
(nth existing x)
v))
pattern))
(s/defn pattern-left-join :- Results
"Takes a partial result, and does an inner join against the resolution of a pattern.
Iterates over the partial result, using the bindings to update the pattern to search the index.
Each row in the partial result is then repeated to match the rows returned from the index lookup.
If no variables match, then the result will be an outer product of the partial result, and
the rows returned from an index lookup of the unmodified pattern.
The final result has metadata with the columns from the partial result,
and all new vars from the pattern."
[graph
part :- Results
pattern :- EPVPattern]
(let [cols (:cols (meta part))
total-cols (->> (plain-vars pattern)
(remove (set cols))
(concat cols)
(into []))
pattern->left (projection/matching-vars pattern cols)]
;; iterate over part, lookup pattern
(with-meta
(for [lrow part
:let [lookup (modify-pattern lrow pattern->left pattern)]
rrow (gr/resolve-pattern graph lookup)]
(concat lrow rrow))
{:cols total-cols})))
(defn vconj
"Used to update a value to be a vector with the new element conj'ed.
If the value starts as nil, then create a new vector to hold the element."
[c v]
(if c (conj c v) [v]))
(s/defn prebound-left-join :- Results
"Takes a bindings (Results) and joins on the current results. This is similar to the
pattern-left-join but instead of taking a pattern to be applied to an indexed graph,
it takes unindexed data that is already bound. The join is done by indexing the bindings
and using the same algorithm that joins rows in pattern-left-join"
[part :- Results
bindings :- Results]
(let [lcols (:cols (meta part))
rcols (:cols (meta bindings))
total-cols (->> rcols
(remove (set lcols))
(concat lcols)
(into []))
left->binding (projection/matching-vars lcols rcols)]
(with-meta
(if (seq left->binding)
(let [key-indices (sort (keys left->binding))
select-key (fn [row] (map #(nth row %) key-indices))
val-set (set (vals left->binding))
width (count rcols)
split-row (fn [row] (reduce (fn [[k v] n]
(let [rowval (nth row n)]
(if (val-set n)
[(conj k (nth row n)) v]
[k (conj v (nth row n))])))
[[] []]
(range width)))
local-index (reduce (fn [idx row]
(let [[shared new] (split-row row)]
(update idx shared vconj new)))
{} bindings)]
(for [lrow part
rrow (local-index (select-key lrow))]
(concat lrow rrow)))
(for [row-l part row-r bindings]
(concat row-l row-r)))
{:cols total-cols})))
(def VarIndex
{Var s/Num})
(s/defn index-vars :- VarIndex
{:private true}
[vars :- [Var]]
(into {} (map-indexed (fn [n c] [c n]) vars)))
(s/defn missing :- (s/maybe Var)
"Returns a value when it is a var that does not appear in the var
index var-index."
[var-index :- VarIndex
arg :- s/Any]
(when (and (vartest? arg)
(not (var-index arg)))
arg))
(def Fcn (s/pred #(or (fn? %) (var? %))))
(s/defn resolve-op :- (s/maybe Fcn)
"Resolves a symbol to an associated function. Symbols without a namespace are presumed to be in clojure.core"
[s :- s/Symbol]
(when (or *override-restrictions*
(if-let [n (namespace s)]
(and (#{"clojure.core" "cljs.core" "clojure.string"} n)
(sandbox/allowed-fns (symbol (name s))))
(sandbox/allowed-fns s)))
(fn-for s)))
(s/defn retrieve-op :- Fcn
"Retrieves a function for a provided operation. An op can be a
variable, a function, a symbol for a function, or a string."
[op
var-index :- VarIndex
part]
(or
(cond
(vartest? op) (retrieve-op (nth (first part) (var-index op)) var-index part) ;; assuming operation is constant
(fn? op) op
(symbol? op) (or (get *env* op) (resolve-op op))
(string? op) (resolve-op (symbol op))
:default (throw (ex-info (str "Unknown operation type" op) {:op op :type (type op)})))
(throw (ex-info (str "Unsupported operation: " op) {:op op :type (type op)}))))
(s/defn filter-join
"Uses row bindings in a partial result as arguments to a function whose parameters are defined by those names.
Those rows whose bindings return true/truthy are kept and the remainder are removed."
[graph
part :- Results
[[op & args :as fltr]] :- FilterPattern]
(let [m (meta part)
var-index (index-vars (:cols m))
arg-indexes (map-indexed
(fn [i arg]
(if-let [j (var-index arg)]
j
(constantly (nth args i))))
args)
filter-fn (if (vartest? op)
(if-let [op-idx (var-index op)]
(fn [a]
(let [callable-op (retrieve-op (nth a op-idx) var-index part)]
(apply callable-op (map (fn [f] (if (fn? f) (f) (nth a f))) arg-indexes))))
(throw (ex-info (str "Unknown variable: " op) {:op op})))
(let [callable-op (retrieve-op op var-index part)]
(fn [a]
(apply callable-op (map (fn [f] (if (fn? f) (f) (nth a f))) arg-indexes)))))]
(try
(with-meta (filter filter-fn part) m)
(catch #?(:clj Throwable :cljs :default) e
(throw (if-let [ev (some (partial missing var-index) args)]
(ex-info (str "Unknown variable in filter: " (name ev)) {:vars (keys var-index) :filter-var ev})
(ex-info (str "Error executing filter: " e) {:error e})))))))
(s/defn binding-join
"Uses row bindings as arguments for an expression that uses the names in that binding.
Binds a new var to the result of the expression and adds it to the complete results."
[graph
part :- Results
[[op & args :as expr] bnd-var] :- EvalPattern]
(let [cols (vec (:cols (meta part)))
new-cols (conj cols bnd-var)
var-index (index-vars cols)
arg-indexes (keep-indexed #(when-not (zero? %1) (var-index %2 (- %1))) expr)
expr (vec expr)
binding-fn (if (vartest? op)
(if-let [op-idx (var-index op)]
(fn [row]
(let [o (retrieve-op (nth row op-idx) var-index part)]
(concat row
[(apply o
(map
#(if (neg? %) (nth expr (- %)) (nth row %))
arg-indexes))])))
(throw (ex-info (str "Unknown variable: " op) {:op op})))
(let [callable-op (retrieve-op op var-index part)]
(fn [row]
(concat row
[(apply callable-op
(map
#(if (neg? %) (nth expr (- %)) (nth row %))
arg-indexes))]))))]
(with-meta
(map binding-fn part)
{:cols new-cols})))
(def ^:dynamic *plan-options* [:min])
(declare left-join)
(s/defn minus
"Removes matches.
For each line in the current results, performs a subquery with NOT clause.
When the NOT clause returns data then that line in the results should be removed."
[graph
part :- Results
[_ & [fpattern :as patterns]]] ;; 'not symbol, then the pattern arguments
(let [ljoin #(left-join %2 %1 graph)
col-meta (meta part)]
(with-meta
(remove
(if (epv-pattern? fpattern) ;; this test is an optimization, to avoid matching-vars in a tight loop
(let [cols (:cols col-meta) ;; existing bound column names
;; map the first pattern's vars into the existing binding columns. Used for the first resolution.
pattern->left (projection/matching-vars fpattern cols)
;; find all bound vars that will be needed for the entire subquery
vars (reduce #(into %1 (map plain-var (get-vars %2))) #{} patterns)
;; get the required bound column names, in order
pre-bound (keep vars cols)
;; the required bound column indexes
pattern-val-idxs (set (keep-indexed (fn [n col] (when (vars col) n)) cols))
;; the columns about to get bound from the first pattern of the subquery
un-bound (keep-indexed #(when (and (vartest? %2) (not (pattern->left %1))) %2) fpattern)
;; all the columns resulting from resolving the first pattern of the subquery
first-cols {:cols (vec (concat pre-bound un-bound))}]
(fn [part-line]
;; update the first pattern of the subquery to include the current bindings
(let [lookup (modify-pattern part-line pattern->left fpattern)
;; start the bindings with the known bound values. Based on column number, not string comparison
bound-cols (vec (keep-indexed #(when (pattern-val-idxs %1) %2) part-line))]
;; Perform the subquery.
;; Start by resolving the first pattern with the contextual bindings
;; and then left-join through the rest of the subquery.
;; seq returns truthy when results are found, and falsey when there are no results.
(seq
(reduce ljoin
(with-meta
(map (partial into bound-cols) (gr/resolve-pattern graph lookup))
first-cols)
(rest patterns))))))
;; general subquery operation when the first element is not a graph resolution pattern
(fn [part-line]
(seq (reduce ljoin (with-meta [part-line] col-meta) patterns))))
part)
col-meta)))
(defn index
{:private true}
[xs]
(zipmap xs (range)))
(s/defn disjunction
"Implements an OR operation by repeating a join across each arm of the operation,
and concatenating the results"
[graph
part :- Results
[_ & patterns]] ;; Discard the first element, since it is just the OR operator
(let [spread (map #(left-join % part graph) patterns)
;; Column information of each sequence of results.
spread-cols (into [] (map (comp :cols meta)) spread)
;; Combine all distinct column information from left to right
;; to produce new column information for the result set.
result-cols (into [] (comp cat (distinct)) spread-cols)]
(with-meta
;; Does distinct create a scaling issue?
(*select-distinct*
;; For each result sequence `results` of `spread` with columns
;; that are not equal to result-cols, reorganize the elements
;; of `results` such that they align with `result-cols`.
(sequence
(comp (map (fn [results cols]
(if (= cols result-cols)
results
(let [cols-index (index cols)
;; Build a function which maps each
;; element of a `result` to its location in
;; `result-cols`.
;; For any column that appears in `result-cols`
;; but not the `results`, pad with a `nil`.
reorganize (apply juxt (map (fn [col]
(if-some [i (get cols-index col)]
(fn [result] (nth result i))
(constantly nil)))
result-cols))]
(map reorganize results)))))
cat)
spread
spread-cols))
{:cols result-cols})))
(s/defn conjunction
"Iterates over the arguments to perform a left-join on each"
[graph
part :- Results
[_ & patterns]]
(reduce (fn [result pattern] (left-join pattern result graph)) part patterns))
(s/defn optional
"Performs a left-outer-join, similarly to a conjunction"
[graph
part :- Results
[_ & patterns :as operation]]
(let [col-meta (meta part)
cols (:cols col-meta)
new-cols (->> (get-vars operation)
(remove (set cols)))
empties (repeat (count new-cols) null-value)
ljoin #(left-join %2 %1 graph)]
(with-meta
(mapcat (fn [lrow]
(or (seq (reduce ljoin (with-meta [lrow] col-meta) patterns))
[(concat lrow empties)]))
part)
{:cols (vec (concat cols new-cols))})))
(def operators
{'not {:left-join minus}
'NOT {:left-join minus}
'or {:left-join disjunction}
'OR {:left-join disjunction}
'and {:left-join conjunction}
'AND {:left-join conjunction}
'optional {:left-join optional}
'OPTIONAL {:left-join optional}})
(defn left-join
"Joins a partial result (on the left) to a pattern (on the right).
The pattern type will determine dispatch."
[pattern results graph]
(cond
;; bindings will pass the epv-pattern? test, so must check for this first
(planner/bindings? pattern) (prebound-left-join results pattern)
(epv-pattern? pattern) (pattern-left-join graph results pattern)
(filter-pattern? pattern) (filter-join graph results pattern)
(eval-pattern? pattern) (binding-join graph results pattern)
(op-pattern? pattern) (if-let [{:keys [left-join]} (operators (first pattern))]
(left-join graph results pattern)
(op-error pattern))
:default (pattern-error pattern)))
(s/def InSpec (s/conditional #(= '$ %) s/Symbol
#(and (symbol? %) (= \? (first (name %)))) s/Symbol
#(and sequential? (sequential? (first %))) [[s/Symbol]]
:else [s/Symbol]))
(s/defn outer-product :- Bindings
"Creates an outer product between 2 sets of bindings"
[leftb :- Bindings
rightb :- Bindings]
(let [namesl (:cols (meta leftb))
namesr (:cols (meta rightb))]
(when-let [n (seq (filter (set namesl) namesr))]
(throw (ex-info "Outer product between bindings should have distinct names" {:duplicate-names n})))
(with-meta
(for [row-l leftb row-r rightb]
(vec (concat row-l row-r)))
{:cols (into namesl namesr)})))
(s/defn symb?
"Similar to symbol? but excludes the special ... form"
[s]
(and (symbol? s) (not= s '...)))
(def empty-bindings (with-meta [] {:cols []}))
(s/defn create-binding :- Bindings
"Creates a bindings between a name and a set of values.
If the name is singular, then that name is bound to the singular element found in values.
If the name is a seq, then each name in the seq is bound to the corresponding offset in each value."
[nm :- InSpec values]
(cond
(symbol? nm) (with-meta [[values]] {:cols [nm]})
(sequential? nm)
(let [[a & r] nm]
(cond
(and (sequential? a) (nil? r) (every? symb? a)) ; relation
(if (every? #(= (count a) (count %)) values)
(with-meta values {:cols (vec a)})
(throw (ex-info "Data does not match relation binding form" {:form nm :data values})))
(and (symb? a) (= '... (first r)) (= 2 (count nm))) ; collection
(if (coll? values)
(with-meta (map vector values) {:cols [a]})
(throw (ex-info "Tuples data does not match collection binding form" {:form nm :data values})))
(and (every? symb? nm)) ; tuple
(if (and (sequential? values) (= (count nm) (count values)))
(with-meta [values] {:cols (vec nm)})
(throw (ex-info "Tuples data does not match tuple binding form" {:form nm :data values})))
:default (throw (ex-info "Unrecognized binding form" {:form nm}))))
:default (ex-info "Illegal scalar binding form" {:form nm})))
(s/defn create-bindings :- [(s/one (s/maybe Bindings) "The bindings for other params")
(s/one (s/maybe s/Any) "The default graph")]
"Converts user provided data for a query into bindings"
[in :- [InSpec]
values ;; :- (s/cond-pre (s/protocol Storage) s/Any)
]
(if-not (seq in)
[empty-bindings (first values)]
(let [[default :as defaults] (remove nil? (map (fn [n v] (when (= '$ n) v)) in values))]
(when (< 1 (count defaults))
(throw (ex-info "Only one default data source permitted" {:defaults defaults})))
(when-not (<= (count in) (count values))
(throw (ex-info "In clause must not be more than the number of sources" {:in in :sources values})))
(let [bindings (->> (map (fn [n v] (when-not (= '$ n) (create-binding n v))) in values)
(filter identity))
bindings (when (seq bindings)
(reduce outer-product bindings))]
[bindings default]))))
(defn conforms? [t d]
(try
(s/validate t d)
(catch #?(:clj Exception :cljs :default) e (str ">>>" (.getMessage e) "<<<"))))
(s/defn select-planner
"Selects a query planner function, based on user-selected options"
[{:keys [planner] :as options}]
(case planner
:user planner/user-plan
:min planner/plan-path
planner/plan-path))
(s/defn run-simple-query
[graph
[fpattern & patterns :as all-patterns] :- [PatternOrBindings]]
;; if provided with bindings, then join the entire path to them,
;; otherwise, start with the first item in the path, and join the remainder
(let [ ;; execute the plan by joining left-to-right
;; left-join has back-to-front params for dispatch reasons
ljoin #(left-join %2 %1 graph)
;; resolve the initial part of the query, and get the remaining patterns to join
[part-result proc-patterns] (cond
;; the first element is already a partial result
(planner/bindings? fpattern) [fpattern patterns]
;; the first element is a pattern lookup
(epv-pattern? fpattern) [(with-meta
(gr/resolve-pattern graph fpattern)
{:cols (vec (plain-vars fpattern))})
patterns]
;; the first element is an operation,
;; start with an empty result and process all the patterns
:default [identity-binding all-patterns])]
;; process the remaining query
(reduce ljoin part-result proc-patterns)))
(s/defn gate-fn
"Returns a function that allows data through it or not,
based on the results of a series of NOT operations.
If any operation returns results, then nothing may get through."
[graph
constraints :- [Pattern]]
(if-not (seq constraints)
identity
(loop [[[_ & patterns :as constraint] & remaining] constraints]
(if-not constraint
identity
(if (seq (run-simple-query graph patterns))
(constantly [])
(recur remaining))))))
(def Plan {s/Keyword [s/Any]})
(def AuditableResults (s/if map? Plan Results))
(s/defn join-patterns :- AuditableResults
"Joins the resolutions for a series of patterns into a single result.
If options contains :path-plan then returns the plan and does not execute"
[graph
patterns :- [Pattern]
bindings :- (s/maybe Bindings)
{:keys [query-plan] :as options}]
(let [all-patterns (if (seq bindings) (cons bindings patterns) patterns)
path-planner (select-planner options)
[fpath & rpath :as path] (path-planner graph all-patterns options)]
(if query-plan
{:plan path}
(if-not rpath
;; single path element - executed separately as an optimization
(cond
(planner/bindings? fpath) fpath
(epv-pattern? fpath) (with-meta
(gr/resolve-pattern graph fpath)
{:cols (vec (plain-vars fpath))})
:default (run-simple-query graph [fpath]))
;; normal operation
(let [ ;; if the plan begins with a negation, then it's not bound to the rest of
;; the plan, and it creates a "gate" for the result
result-gate (gate-fn graph (take-while planner/not-operation? path))
path' (drop-while planner/not-operation? path)]
(-> (run-simple-query graph path')
result-gate))))))
(def query-keys #{:find :in :with :where})
(def extended-query-keys (into query-keys [:all :distinct]))
(s/defn query-map
"Parses a query into it's main components.
Queries can be a sequence, a map, or an EDN string. These are based on Datomic-style queries.
The return map contains:
:find - The elements to be projected from a query.
:all - true if duplicates are to be returned, false otherwise. Defaults to false.
:in - A list of data sources. Optional.
:with - list of variables for grouping.
:where - A sequence of constraints for the query."
[query]
(let [{find :find :as qmap}
(cond
(map? query) (update query :where seq)
(string? query) (query-map (edn/read-string query))
(sequential? query) (->> query
(partition-by query-keys)
(partition 2)
(map (fn [[[k] v]] [k v]))
(into {})))
[find' all] (if (= :all (first find))
[(rest find) true]
[(remove #{:distinct} find) false])]
(assoc qmap :find find' :all all)))
(s/defn newl :- s/Str
[s :- (s/maybe s/Str)
& remaining]
(if s (apply str s "\n" remaining) (apply str remaining)))
(defn safe-get-vars
"Like `get-vars` but will not throw an exception, and will always
return a set. This is used in `query-validator` where bad patterns
are anticipated."
{:private true}
[x]
(transduce (comp (filter vartest?) (map plain-var)) conj #{} (tree-seq coll? seq x)))
(s/defn query-validator
[{:keys [find in where] :as query} :- {s/Keyword (s/cond-pre s/Bool [s/Any])}]
(let [unknown-keys (seq (remove extended-query-keys (keys query)))
non-seq-wheres (seq (remove sequential? where))
unbound-find-vars (set/difference (safe-get-vars find)
(set/union (safe-get-vars in) (safe-get-vars where)))
err-text (cond-> nil
unknown-keys (newl "Unknown clauses: " unknown-keys)
(empty? find) (newl "Missing ':find' clause")
(empty? where) (newl "Missing ':where' clause")
non-seq-wheres (newl "Invalid ':where' statements: " non-seq-wheres)
(seq unbound-find-vars) (newl "Unbound variables in ':find' clause: " unbound-find-vars))]
(if err-text
(throw (ex-info err-text {:query query}))
query)))
(s/defn execute-query
[selection constraints bindings graph project-fn {:keys [query-plan] :as options}]
;; joins must happen across a seq that is a conjunction
(log/debug "executing selection: " (seq selection) " where: " constraints)
(log/debug "bindings: " bindings)
(let [top-conjunction (if (seq? constraints) ;; is this a list?
(let [[op & args] constraints]
(cond
(vector? op) constraints ;; Starts with top level EPV. Already in the correct form
(#{'and 'AND} op) args ;; Starts with top level AND. Use the arguments
(operators op) (list constraints) ;; top level form. Wrap as a conjunction
(seq? op) constraints ;; first form is an operation. Already in the correct form
:default (throw (ex-info "Unknown constraint format" {:constraint constraints}))))
(list constraints)) ;; a single vector, which is one constraint that needs to be wrapped. Unexpected
select-distinct (fn [xs] (if (and (coll? xs) (not (vector? xs)))
(let [m (meta xs)] (with-meta (*select-distinct* xs) m))
xs))]
(let [resolved (join-patterns graph top-conjunction bindings options)]
(log/trace "results: " resolved)
;; check if this is a query plan without results
(if query-plan
resolved
(->> resolved
(project-fn selection)
select-distinct)))))
(s/defn prepend
[element
pattern :- Pattern]
(let [[op & args] pattern]
(cond (vector? op) (cons element pattern))
(#{'and 'AND} op) (cons element args)
:default (list element pattern)))
(defn split-with*
"Same as clojure.core/split-with but only executes the predicate once on each matching element.
Returns the same as [(vec (take-while pred coll)) (drop-while pred coll)]"
[pred coll]
(loop [tw [] [r & rs :as allr] coll]
(if-not (seq allr)
[tw nil]
(if (pred r)
(recur (conj tw r) rs)
[tw allr]))))
(defn seq-group-by
"Does a group-by style of operation, but it streams over the input and provides a sequence of groups.
The input data must already be in a groupable order, meaning that once a grouping variable has stopped
appearing, the group is finished. group-select is a function for selecting the data to group by."
[group-select xs]
(letfn [(groups [[x & xs :as xa]]
(when (seq xa)
(let [g (group-select x)
gfn (fn [r] (= g (group-select r)))
[grp rmdr] (split-with* gfn xs)]
(cons (cons x grp) (lazy-seq (groups rmdr))))))]
(groups xs)))
(s/defn context-execute-query
"For each line in a context, execute a query specified by the where clause"
[graph
grouping-vars :- #{Var}
context :- Results
[op & args :as where] :- Pattern]
(let [context-cols (meta context)
colnumbers (keep-indexed (fn [n col] (when (grouping-vars col) n)) (:cols context-cols))
group-sel (fn [row] (mapv #(nth row %) colnumbers))
ljoin #(left-join %2 %1 graph)
where (if (#{'and 'AND} op) args (list where))
subquery (fn [grp] (reduce ljoin grp where))]
(->> (seq-group-by group-sel context)
(map #(with-meta % context-cols))
(map subquery))))
(def aggregate-fns
"Map of aggregate symbols to functions that accept a seq of data to be aggregated"
{'sum (partial apply +)
'count count
'count-distinct count ;; removal of duplicates happens in processing the data
'avg #(/ (apply + %) (count %))
'max (partial apply max)
'min (partial apply min)
'first first
'last last})
(s/defn agg-label :- s/Symbol
"Converts an aggregate operation on a symbol into a symbol name"
[[op v]]
(symbol (str "?" (name op) "-" (if (planner/wildcard? v) "all" (subs (name v) 1)))))
(s/defn result-label :- s/Symbol
"Convert an element from a select/find clause into an appropriate label.
Note that duplicate columns are not considered"
[e]
(cond
(vartest? e) e
(and (seq? e) (= 2 (count e))) (agg-label e)
:default (throw (ex-info "Bad selection in :find clause with aggregates" {:element e}))))
(defn distinct-fn
"Returns a function that may deduplicate results, depending on the type of operator"
[op]
(if (= 'count-distinct op)
identity
*select-distinct*))
(s/defn aggregate-over :- s/Any ;; Usually Results, but can be a seq or a value due to projection
"For each seq of results, aggregates individually, and then together"
[selection :- [s/Any]
with :- [Var]
partial-results :- [Results]]
(let [selection-count (count selection)]
(letfn [(get-selectors [idxs selected]
(map (fn [s]
(if (vartest? s)
[first #(nth % (idxs s)) *select-distinct*]
(let [[op v] s
dedup-fn (distinct-fn op)]
(if-let [afn (aggregate-fns op)]
(if (planner/wildcard? v)
[afn identity dedup-fn]
[afn #(nth % (idxs v)) dedup-fn])
(throw (ex-info (str "Unknown operation: " op) {:expr s}))))))
selected))
(project-aggregate [selected result]
(let [var-index (index-vars (:cols (meta result)))]
(for [[col-fn col-selector dfn] (get-selectors var-index selected)]
(let [col-data (map col-selector result)]
(col-fn (dfn col-data))))))]
(cond
;; check for singleton result
(and (= 2 selection-count)
(= '. (second selection)))
(let [[op v :as sel] (first selection)
result (first partial-results)
col-fn (aggregate-fns op)
dfn (distinct-fn op)]
(if (planner/wildcard? v)
(col-fn (dfn result))
(let [var-index (index-vars (:cols (meta result)))
col (var-index v)
col-data (map #(nth % col) result)]
(col-fn (dfn col-data)))))
;; check for seq result
(and (= 1 selection-count)
(vector? (first selection)))
;; don't need to check if the first part of sel is an aggregate because we couldn't
;; be here if it weren't
(let [selected (first selection)]
(if (= '... (second selected))
(let [[[op v :as sel] _] selected
col-fn (aggregate-fns op)
dfn (distinct-fn op)
single-agg (if (planner/wildcard? v)
(fn [result]
(col-fn (dfn result)))
(fn [result]
(let [var-index (index-vars (:cols (meta result)))
col (var-index v)
col-data (map #(nth % col) result)]
(col-fn (dfn col-data)))))]
(map single-agg partial-results))
(project-aggregate (first selection) (first partial-results))))
;; standard :find clause with some of the selection as aggregates
:default
(with-meta
(map (partial project-aggregate selection) partial-results)
{:cols (mapv result-label selection)})))))
(defn aggregate-query
[find bindings with where graph project-fn {:keys [query-plan] :as options}]
(log/debug "aggregate query. plan only=" query-plan)
(binding [*select-distinct* distinct]
;; flatten the query
(let [simplified (planner/simplify-algebra where)
;; ensure that it is an (or ...) expression
normalized (planner/normalize-sum-of-products simplified)
;; extract every element of the or into an outer/inner pair of queries.
;; The inner/outer -wheres zip
[outer-wheres inner-wheres agg-vars] (planner/split-aggregate-terms normalized find with)
_ (log/debug "inner query:" (seq inner-wheres))
_ (log/debug "outer query:" (seq outer-wheres))
;; outer wheres is a series of queries that make a sum (an OR operation). These all get run separately.
;; inner wheres is a matching series of queries that get run for the corresponding outer query.
;; for each outer/inner pair, get all of the vars needed to be projected from the outer query
;; also need anything that joins the outer query to the inner query
;; start with the requested vars
find-vars (filter vartest? find)
;; convert the requested vars into sets, for filtering
find-var-set (set find-vars)
with-set (set with)
;; create a function that can find everything in the outer query that the inner query needs
;; remove the columns which will already be projected from :find and :with
needed-vars (fn [outer-where inner-where]
(let [inner-var-set (set (get-vars inner-where))]
(sequence (comp
(remove find-var-set)
(remove with-set)
(remove agg-vars)
(filter inner-var-set))
(get-vars outer-where))))
;; execute the outer query if it exists. If not then return an identity binding.
outer-results (map (fn [ow iw]
(if (seq ow)
;; outer query exists, so find the terms to be projected and execute
(let [outer-terms (concat find-vars with (needed-vars ow iw))]
(execute-query outer-terms ow bindings graph project-fn options))
identity-binding))
outer-wheres inner-wheres)]
(log/debug "outer results: " (into [] outer-results))
(if query-plan
;; results are audit plans
{:type :aggregate
:outer-queries (map :plan outer-results)
:inner-queries inner-wheres}
;; execute the inner queries within the context provided by the outer queries
;; remove the empty results. This means that empty values are not counted!
(let [grouping-vars (into find-var-set with-set) ;; (set/difference (into find-var-set with-set) agg-vars)
_ (log/debug "grouping vars: " grouping-vars)
inner-results (filter seq (mapcat (partial context-execute-query graph grouping-vars) outer-results inner-wheres))]
(log/debug "inner results: " (into [] inner-results))
;; calculate the aggregates from the final results and project
(aggregate-over find with inner-results))))))
(defn ^:private fresh []
(gensym "?__"))
(defn ^:private map-epv
"In the :where sequence of query apply f to each EPV pattern."
[f {:keys [where] :as query}]
(assoc query
:where (map (fn opf [constraint]
(cond
(epv-pattern? constraint)
(f constraint)
(op-pattern? constraint)
(cons (first constraint) (map opf (rest constraint)))
:else
constraint))
where)))
(s/defn rewrite-wildcards
"In the :where sequence of query replace all occurrences of _ with
unique variables."
[{:keys [where] :as query}]
(map-epv (fn [epv]
(mapv (fn [x]
(if (= x '_) (fresh) x))
epv))
query))
(s/defn expand-shortened-pattern-constraints
"In the :where sequence of query expand EPV patterns of the form [E]
and [E P] to [E ?P ?V] and [E ?P ?V] respectively where ?P and ?V
are fresh variables."
[{:keys [where] :as query}]
(map-epv (fn [epv]
(into epv (repeatedly (- 3 (count epv)) fresh)))
query))
(s/defn parse
[x]
(-> x
query-map
query-validator
rewrite-wildcards
expand-shortened-pattern-constraints))
(s/defn query-entry
"Main entry point of user queries"
[query empty-graph inputs plan?]
(let [{:keys [find all in with where]} (parse query)
[inputs options] (if (seq in)
[(take (count in) inputs) (drop (count in) inputs)]
[[(first inputs)] (rest inputs)])
options (-> (apply hash-map options) (assoc :query-plan plan?))
[bindings default-graph] (create-bindings in inputs)
graph (or default-graph empty-graph)]
(if (seq (filter planner/aggregate-form? find))
(aggregate-query find bindings with where graph projection/project options)
(binding [*select-distinct* (if all identity distinct)]
(execute-query find where bindings graph projection/project options)))))