This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-06-22
Channels
- # ai (1)
- # announcements (4)
- # babashka (23)
- # beginners (27)
- # biff (17)
- # calva (5)
- # clerk (6)
- # clj-commons (27)
- # clj-kondo (35)
- # clojars (12)
- # clojure (27)
- # clojure-denver (3)
- # clojure-europe (71)
- # clojure-norway (7)
- # clojure-spec (5)
- # clojure-uk (2)
- # clojurescript (45)
- # data-science (9)
- # datomic (4)
- # dev-tooling (2)
- # devcards (1)
- # hoplon (2)
- # hyperfiddle (36)
- # introduce-yourself (3)
- # malli (11)
- # missionary (2)
- # off-topic (63)
- # polylith (5)
- # rdf (2)
- # reagent (12)
- # schema (1)
- # shadow-cljs (11)
- # sql (6)
- # tools-deps (23)
- # xtdb (6)
I am trying to perform a "join-aggregate" Let's say I have these set of documents inserted :
[{:xt/id :farm/a
:farm/id :farm/a}
{:xt/id :animal/cow
:animal/farm :farm/a}
{:xt/id :animal/donkey
:animal/farm :farm/a}]
I am able to perform the join by adding these clauses :
[[?farm :farm/id ?fid]
[?animal :animal/farm ?fid]]
Which gives me :
[{:farm/id :farm/a
:animal/id :animal/cow}
{:farm/id :farm/a
:animal/id :animal/donkey}]
But what I want to achieve is :
{:xt/id :farm/a
:farm/animals [{:xt/id :animal/cow
:animal/farm :farm/a}
{:xt/id :animal/donkey
:animal/farm :farm/a}]}
I can achieve this using reduce
but is there a way to do this in XTDB using https://docs.xtdb.com/language-reference/1.23.2/datalog-queries/#find-aggregate or https://docs.xtdb.com/language-reference/1.23.2/datalog-queries/#_custom_aggregates?not with that key afaict, but
user> (xt/pull (xt/db node) '[:farm/id {:animal/_farm [*]}] :farm/a)
{:farm/id :farm/a,
:animal/_farm
({:animal/farm :farm/a, :xt/id :animal/cow}
{:animal/farm :farm/a, :xt/id :animal/donkey})}
❤️ 2
Exactly what I needed. Thank you!
Out of curiosity, how do we compare the performance of joins using this syntax vs using :where
?
I wouldn’t expect there to be a difference, it is likely using the same attribute index
Gotcha. We can also rename the attribute using :as
which is very useful
https://docs.xtdb.com/language-reference/1.23.2/datalog-queries/#where:~:text=param%20%3Avalue%2C%20%E2%80%A6%E2%80%8B%7D).-,%3Aas,-%2D%20to%20rename%20attributes