This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-09-12
Channels
- # adventofcode (1)
- # announcements (1)
- # atom-editor (4)
- # aws (4)
- # babashka (7)
- # beginners (46)
- # biff (14)
- # calva (11)
- # cljdoc (2)
- # clojure (78)
- # clojure-art (1)
- # clojure-austin (1)
- # clojure-europe (50)
- # clojure-nl (2)
- # clojure-norway (22)
- # clojure-spec (2)
- # clojure-uk (2)
- # clojurescript (72)
- # conjure (6)
- # core-typed (6)
- # eastwood (4)
- # events (1)
- # figwheel-main (11)
- # fulcro (1)
- # guix (1)
- # helix (13)
- # jobs (2)
- # jobs-discuss (4)
- # kaocha (2)
- # malli (5)
- # off-topic (7)
- # pathom (22)
- # pedestal (9)
- # re-frame (29)
- # reagent (7)
- # releases (2)
- # remote-jobs (1)
- # rewrite-clj (12)
- # shadow-cljs (44)
- # sql (13)
- # squint (2)
- # xtdb (17)
Hi again, amateur question again – How does one handle "unify this or accept an empty result" – I have a relation that might exist (if it does – I should have a (count ?knows)
– or if there are no results it should be 0
– I can do either one of those but not at the same time 😄
I tried
(or
(and [?known :core/entity ?entity]
[?known :user/id]])
[(identity nil) ?known]
)
but that is obviously not the way to do it 🙂
?entity
is bound in the where query before that clause
Heh, one way that I found out kind of works is [(identity true) ?known]
in the other leg of that or
predicate that returns N+1 matches for ones that have something and 1 for the ones that did not have anything...
so (dec (count ?known))
gives the result I expected.
Hey, I don't have time to think about this properly right now (I'll try to circle back sometime later), but this might give some ideas: https://github.com/xtdb/xtdb/blob/065f41945daed67c6742a5c480864eaa24f6e48d/test/test/xtdb/query_test.clj#L1812
On the surface it looks like that or-join
it returns N+1 as well if I adapt this to my query, adding the restriction to the other leg with (not [?known :user/id])
Thanks! It was mentioned in the docs but https://github.com/xtdb/xtdb/blob/e2f51ed99fc2716faa8ad254c0b18166c937b134/test/test/xtdb/query_test.clj#L1054 this did not open so didn't bother looking into it 😄
user> (xt/submit-tx node [[::xt/put {:xt/id "test1" :foo 1 :have-it 42}] [::xt/put {:xt/id "test2" :foo 2}] [::xt/put {:xt/id "test3" :foo 3 :have-it 666}]])
user> (xt/q (xt/db node) '[:find ?e ?foo ?it :where [?e :foo ?foo] [(get-attr ?e :have-it) [?it]]])
#{["test3" 3 666] ["test1" 1 42] ["test2" 2 nil]}
idk if this helps in your specific query, but you can use it to “get” attributes that possibly don’t exist in the entitySounds like this does not fit my current use case. I'm essentially trying to find related documents that has a link to another entity through a key. My query above "works", if it is efficient or idiomatic – that is up for debate 😄 Thanks for your input – another new shining thing in my toolbelt 🙂
> so (dec (count ?known))
gives the result I expected.
If you can share the full query (that gives the expected result) we'll be able to help more easily on the "efficient or idiomatic" front
Sure! 🙂
{
:find [?role ?group-name ?type ?name ?skill (dec (count ?knows))],
:where [[?dept :core/role ?role]
[?dept :core/type ?type]
[?group :core/parent ?dept]
[?group :core/name ?group-name]
[?group :core/type :core/group]
[?skill :core/parent ?group]
[?skill :core/name ?name]
(or (and [?knows :core/skill ?skill]
[?knows :user/id])
[(identity true) ?knows])]
}
Thanks! That doesn't seem too bad, actually, though I might change the or
to an or-join
to make it slightly more explicit (and potentially speed it up):
(or-join [?knows ?skill]
(and [?knows :core/skill ?skill]
[?knows :user/id])
[(identity true) ?knows])
Thank you again for your help. Changed my impl to or-join according to your suggestion 🙂