Handle non-where-generating bindings.

This commit is contained in:
Richard Newman 2016-07-12 18:46:46 -07:00
parent 0832396071
commit 0149bdcd77

View file

@ -83,12 +83,13 @@
TODO: experiment; it might be the case that producing more TODO: experiment; it might be the case that producing more
pairwise equalities we get better or worse performance." pairwise equalities we get better or worse performance."
[bindings] [bindings]
(cons :and (let [clauses (mapcat (fn [[_ vs]]
(mapcat (fn [[k vs]] (when (> (count vs) 1)
(when (> (count vs) 1) (let [root (first vs)]
(let [root (first vs)] (map (fn [v] [:= root v]) (rest vs)))))
(map (fn [v] [:= root v]) (rest vs))))) bindings)]
bindings))) (when-not (empty? clauses)
(cons :and clauses))))
(defn patterns->body [patterns] (defn patterns->body [patterns]
(let [clauses (let [clauses
@ -140,15 +141,16 @@
(let [{:keys [from where bindings]} ; 'where' here is SQL. (let [{:keys [from where bindings]} ; 'where' here is SQL.
(patterns->body where) ; 'where' here is the Datalog :where clause. (patterns->body where) ; 'where' here is the Datalog :where clause.
variable-lookup #(or (first (%1 bindings)) variable-lookup #(or (first (%1 bindings))
(raise (str "Couldn't find variable " %1)))] (raise (str "Couldn't find variable " %1)))
where-from-bindings (bindings->where bindings)]
;; Now we expand the :where clause to also include any ;; Now we expand the :where clause to also include any
;; repeated variable usage, as noted in `bindings`. ;; repeated variable usage, as noted in `bindings`.
{:select (elements->sql-projection (:elements find) variable-lookup) {:select (elements->sql-projection (:elements find) variable-lookup)
:from from :from from
:where (list :and :where (if where-from-bindings
where (list :and where where-from-bindings)
(bindings->where bindings))}))) where)})))
(defn find->sql-string (defn find->sql-string
"Take a parsed `find` expression and turn it into SQL." "Take a parsed `find` expression and turn it into SQL."