threatgrid / asami

A graph store for Clojure and ClojureScript
Eclipse Public License 1.0
635 stars 29 forks source link

Subqueries #147

Open quoll opened 3 years ago

quoll commented 3 years ago

Subqueries are currently possible by piping the results of one query into the next:

(->>
  (q ‘[:find ?person (count ?child) :where [?person :children ?children] [?children :tg/contains ?child]] the-db)
  (q ‘[:find ?city
       :in $ [[?person ?count-child]]
       :where [(> ?count-child 3)] [?person :address ?address] [?address :city ?city]] the-db))

This works efficiently, as it is only 2 queries, with the bindings of the first query being lazily consumed by the second.

However, this is difficult to follow. After some discussion with @marioaquino we're thinking that the following may work as a syntax:

(q ‘[:find ?city
     :in $ [:find ?person (count ?child) :as ?cc :where [?person :children ?children] [?children :tg/contains ?child]]
     :where [(> ?cc 3)] [?person :address ?address] [?address :city ?city]] the-db)

Things to note:

quoll commented 3 years ago

Depends on issue #192