-
Notifications
You must be signed in to change notification settings - Fork 4
/
expr.cljc
359 lines (282 loc) · 11.4 KB
/
expr.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
(ns com.yetanalytics.flint.spec.expr
(:require [clojure.spec.alpha :as s]
[clojure.set :as cset]
[com.yetanalytics.flint.spec.axiom :as ax])
#?(:cljs (:require-macros
[com.yetanalytics.flint.spec.expr :refer [defexprspecs
keyword-args]])))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Terminals
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def expr-terminal-spec
(s/and
(comp not list?)
(s/or :ax/iri ax/iri-spec
:ax/prefix-iri ax/prefix-iri-spec
:ax/var ax/variable-spec
:ax/literal ax/literal-spec)))
(def var-terminal-spec
(s/or :expr/terminal (s/or :ax/var ax/variable-spec)))
(def wildcard-terminal-spec
(s/or :expr/terminal (s/or :ax/wildcard ax/wildcard-spec)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Keyword Argument Specs and Helpers
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(s/def ::distinct? boolean?)
;; This will conform to `[:separator string-literal]`, which will be formatted
;; as a plain string literal (i.e. does not get passed to `p/-format-literal`).
(s/def ::separator (s/and string? ax/literal-spec))
(defn- kvs->map [kvs]
(reduce (fn [m {k :expr/k v :expr/v}] (assoc m k v))
{}
kvs))
(defmacro keyword-args
"Given `kspecs` (e.g. `(keyword-args ::distinct? ::selector)`), return
a regex spec that can be used to spec a sequence of `:keyword value`
pairs (e.g. `(:distinct? true :selector \";\")`)."
[& kspecs]
`(s/& (s/* (s/cat :expr/k keyword? :expr/v any?))
(s/conformer kvs->map)
(s/keys :opt-un ~kspecs)
#(every? ~(->> kspecs (map (comp keyword name)) set) (keys %))
(s/conformer #(into [] %))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Operation Symbols
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def nilary-ops
#{'rand 'now 'uuid 'struuid})
(def nilary-or-unary-ops
#{'bnode})
(def unary-ops
#{'not
'str 'strlen 'ucase 'lcase
'lang 'datatype 'blank? 'literal? 'numeric?
'iri 'uri 'iri? 'uri? 'encode-for-uri
'abs 'ceil 'floor 'round
'year 'month 'day
'hours 'minutes 'seconds
'timezone 'tz
'md5 'sha1 'sha256 'sha384 'sha512})
(def unary-var-ops
#{'bound})
(def unary-where-ops
#{'exists 'not-exists})
(def binary-ops
#{'lang-matches 'contains 'strlang 'strdt
'strstarts 'strends 'strbefore 'strafter
'sameterm
'regex 'substr
'= 'not= '< '> '<= '>=})
(def binary-plus-ops
#{'and 'or
'+ '- '* '/
'in 'not-in})
(def binary-or-ternary-ops
#{'regex 'substr})
(def ternary-ops
#{'if})
(def ternary-or-fourary-ops
#{'replace})
(def varardic-ops
#{'concat 'coalesce})
;; Aggregates
(def aggregate-expr-ops
#{'sum 'min 'max 'avg 'sample})
(def aggregate-expr-or-wild-ops
#{'count})
(def aggregate-expr-with-sep-ops
#{'group-concat})
(def aggregate-ops
"A set of all operations used in aggregate expressions."
(cset/union aggregate-expr-ops
aggregate-expr-or-wild-ops
aggregate-expr-with-sep-ops))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Branch Specs
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Built-in Expressions
(def nilary-spec
(s/cat :expr/op nilary-ops))
(defn- nilary-or-unary-spec* [expr-spec]
(s/cat :expr/op nilary-or-unary-ops
:expr/arg-1 (s/? expr-spec)))
(def nilary-or-unary-spec (nilary-or-unary-spec* ::expr))
(def nilary-or-unary-agg-spec (nilary-or-unary-spec* ::agg-expr))
(defn- unary-spec* [expr-spec]
(s/cat :expr/op unary-ops
:expr/arg-1 expr-spec))
(def unary-spec (unary-spec* ::expr))
(def unary-agg-spec (unary-spec* ::agg-expr))
(def unary-var-spec
(s/cat :expr/op unary-var-ops
:expr/arg-1 var-terminal-spec))
(def unary-where-spec
(s/cat :expr/op unary-where-ops
;; Fully qualify ns to avoid mutually recursive require
:expr/arg-1 :com.yetanalytics.flint.spec.where/where))
(defn- binary-spec* [expr-spec]
(s/cat :expr/op binary-ops
:expr/arg-1 expr-spec
:expr/arg-2 expr-spec))
(def binary-spec (binary-spec* ::expr))
(def binary-agg-spec (binary-spec* ::agg-expr))
(defn- binary-plus-spec* [expr-spec]
(s/cat :expr/op binary-plus-ops
:expr/arg-1 expr-spec
:expr/vargs (s/+ expr-spec)))
(def binary-plus-spec (binary-plus-spec* ::expr))
(def binary-plus-agg-spec (binary-plus-spec* ::agg-expr))
(defn- binary-or-ternary-spec* [expr-spec]
(s/cat :expr/op binary-or-ternary-ops
:expr/arg-1 expr-spec
:expr/arg-2 expr-spec
:expr/arg-3 (s/? expr-spec)))
(def binary-or-ternary-spec (binary-or-ternary-spec* ::expr))
(def binary-or-ternary-agg-spec (binary-or-ternary-spec* ::agg-expr))
(defn- ternary-spec* [expr-spec]
(s/cat :expr/op ternary-ops
:expr/arg-1 expr-spec
:expr/arg-2 expr-spec
:expr/arg-3 expr-spec))
(def ternary-spec (ternary-spec* ::expr))
(def ternary-agg-spec (ternary-spec* ::agg-expr))
(defn- ternary-or-fourary-spec* [expr-spec]
(s/cat :expr/op ternary-or-fourary-ops
:expr/arg-1 expr-spec
:expr/arg-2 expr-spec
:expr/arg-3 expr-spec
:expr/arg-4 (s/? expr-spec)))
(def ternary-or-fourary-spec (ternary-or-fourary-spec* ::expr))
(def ternary-or-fourary-agg-spec (ternary-or-fourary-spec* ::agg-expr))
(defn- varardic-spec* [expr-spec]
(s/cat :expr/op varardic-ops
:expr/vargs (s/* expr-spec)))
(def varardic-spec (varardic-spec* ::expr))
(def varardic-agg-spec (varardic-spec* ::agg-expr))
;; Aggregate Expressions (as opposed to regular exprs with aggregates)
;; NOTE: Aggregates inside other aggregates are not allowed in Apache Jena,
;; but are technically allowed by the grammar, so it may be allowed by other
;; implementations. After all, these two are equivalent:
;;
;; SELECT (MAX(AVG(?x)) AS ?maxAvg) => Banned by Jena
;; SELECT (AVG(?x) AS ?avg) (MAX(?avg) AS ?maxAvg) => Allowed by Jena
(def aggregate-spec
(s/cat :expr/op aggregate-ops
:expr/arg-1 ::agg-expr
:expr/kwargs (keyword-args ::distinct?)))
(def aggregate-wildcard-spec
(s/cat :expr/op aggregate-expr-or-wild-ops
:expr/arg-1 (s/& (s/alt :expr ::agg-expr
:wildcard wildcard-terminal-spec)
(s/conformer second))
:expr/kwargs (keyword-args ::distinct?)))
(def aggregate-separator-spec
(s/cat :expr/op aggregate-expr-with-sep-ops
:expr/arg-1 ::agg-expr
:expr/kwargs (keyword-args ::distinct?
::separator)))
;; Custom Expressions
(def custom-fn-spec ; non-aggregates
(s/cat :expr/op ax/iri-or-prefixed-spec
:expr/vargs (s/* ::expr)))
(def aggregate-custom-fn-spec
(s/cat :expr/op ax/iri-or-prefixed-spec
:expr/vargs (s/* ::agg-expr)
:expr/kwargs (keyword-args ::distinct?)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Mutli-specs
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmacro defexprspecs
"Macro to repeatedly define `(defmethod mm-name 'sym [_] expr-spec)`."
[mm-name syms expr-spec]
`(do ~@(map (fn [sym] `(defmethod ~mm-name (quote ~sym) [~'_] ~expr-spec))
(eval syms))))
(defn- expr-spec-dispatch
[expr-list]
(let [op (first expr-list)]
(cond
(symbol? op) op
(s/valid? ax/iri-or-prefixed-spec op) :custom)))
;; No Aggregate Expressions
(defmulti expr-spec-mm
"Given a Flint expr list, returns a spec determined by its first arg."
expr-spec-dispatch)
(defexprspecs expr-spec-mm nilary-ops nilary-spec)
(defexprspecs expr-spec-mm nilary-or-unary-ops nilary-or-unary-spec)
(defexprspecs expr-spec-mm unary-ops unary-spec)
(defexprspecs expr-spec-mm unary-var-ops unary-var-spec)
(defexprspecs expr-spec-mm unary-where-ops unary-where-spec)
(defexprspecs expr-spec-mm binary-ops binary-spec)
(defexprspecs expr-spec-mm binary-plus-ops binary-plus-spec)
(defexprspecs expr-spec-mm binary-or-ternary-ops binary-or-ternary-spec)
(defexprspecs expr-spec-mm ternary-ops ternary-spec)
(defexprspecs expr-spec-mm ternary-or-fourary-ops ternary-or-fourary-spec)
(defexprspecs expr-spec-mm varardic-ops varardic-spec)
(defmethod expr-spec-mm :custom [_] custom-fn-spec)
;; We're not gentesting so retag is irrelevant
(def expr-multi-spec
(s/multi-spec expr-spec-mm first))
;; Aggregate Expressions
(defmulti agg-expr-spec-mm
"Like `expr-spec-mm`, but covers also covers aggregate expressions."
expr-spec-dispatch)
(defexprspecs agg-expr-spec-mm nilary-ops nilary-spec)
(defexprspecs agg-expr-spec-mm nilary-or-unary-ops nilary-or-unary-agg-spec)
(defexprspecs agg-expr-spec-mm unary-ops unary-agg-spec)
(defexprspecs agg-expr-spec-mm unary-var-ops unary-var-spec)
(defexprspecs agg-expr-spec-mm unary-where-ops unary-where-spec)
(defexprspecs agg-expr-spec-mm binary-ops binary-agg-spec)
(defexprspecs agg-expr-spec-mm binary-plus-ops binary-plus-agg-spec)
(defexprspecs agg-expr-spec-mm binary-or-ternary-ops binary-or-ternary-agg-spec)
(defexprspecs agg-expr-spec-mm ternary-ops ternary-agg-spec)
(defexprspecs agg-expr-spec-mm ternary-or-fourary-ops ternary-or-fourary-agg-spec)
(defexprspecs agg-expr-spec-mm varardic-ops varardic-agg-spec)
(defexprspecs agg-expr-spec-mm aggregate-ops aggregate-spec)
(defexprspecs agg-expr-spec-mm aggregate-expr-or-wild-ops aggregate-wildcard-spec)
(defexprspecs agg-expr-spec-mm aggregate-expr-with-sep-ops aggregate-separator-spec)
(defmethod agg-expr-spec-mm :custom [_] aggregate-custom-fn-spec)
(def agg-expr-multi-spec
(s/multi-spec agg-expr-spec-mm first))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Expression Specs
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn- conform-expr
"Conforms the map arg into a
`[[:expr/op op] [:expr/args args] [:expr/kwargs kwargs]]` vector."
[{op :expr/op
arg-1 :expr/arg-1
arg-2 :expr/arg-2
arg-3 :expr/arg-3
arg-4 :expr/arg-4
vargs :expr/vargs
kwargs :expr/kwargs}]
(cond-> [[:expr/op op]
[:expr/args (cond-> []
arg-1 (conj arg-1)
arg-2 (conj arg-2)
arg-3 (conj arg-3)
arg-4 (conj arg-4)
vargs (concat vargs)
true vec)]]
kwargs (conj [:expr/kwargs kwargs])))
(def expr-branch-spec
(s/and list?
expr-multi-spec
(s/conformer conform-expr)))
(def agg-expr-branch-spec
(s/and list?
agg-expr-multi-spec
(s/conformer conform-expr)))
(s/def ::expr
(s/or :expr/terminal expr-terminal-spec
:expr/branch expr-branch-spec))
(s/def ::agg-expr
(s/or :expr/terminal expr-terminal-spec
:expr/branch agg-expr-branch-spec))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Expression AS Variable Specs
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(s/def ::expr-as-var
(s/or :expr/as-var (s/tuple ::expr (s/or :ax/var ax/variable-spec))))
(s/def ::agg-expr-as-var
(s/or :expr/as-var (s/tuple ::agg-expr (s/or :ax/var ax/variable-spec))))