Fork me on GitHub
#datalog
<
2021-12-01
>
zackteo09:12:35

Hello I'm a bit unsure of how to do recursive datalog queries and am not too sure where to start

zackteo09:12:21

I have a query that goes something like this

{:find [?dataset ?group]
 :where [[?dataset :id "..."]
         [?dataset :group ?group]]}

zackteo09:12:49

My data is such that each group may have a parent group indicated by the attribute :parent

zackteo09:12:00

how might i go about finding all groups that the dataset is a group of?

refset11:12:38

Hey πŸ™‚ looking at the example(s) in the docs for inspiration https://docs.xtdb.com/language-reference/datalog-queries/#_bound_arguments ...I'd try something similar:

{:find  [?dataset ?group ?parent]
 :where [[?dataset :xt/id "..."]
         [?dataset :group ?group]
         (group-parents ?group ?parent)]
 :rules [[(group-parents [?group] ?parent)
          [?group :parent ?parent]]
         [(group-parents [?group] ?parent)
          [?group :parent ?subparent]
          (group-parents ?subparent ?parent)]]}

refset11:12:16

adjusting the language of "parent"/"subparent" would probably make things clearer but hopefully you get the idea πŸ™‚

zackteo11:12:15

Thanks for the link 😊 was trying see where it might be in the docs. Will take a look when I get back home and let you know if I have any issues! (:

πŸ‘ 1
refset11:12:50

if you also need to return the "height" of all the nested parents, you can do it like this: https://gist.github.com/refset/57a910e2b746332ec7a0a31886f57cfb

zackteo14:12:39

I think I manged to adapt it to my problem πŸ˜„ Thanks for your help!

πŸ™ 1
zackteo14:12:01

I should definitely set aside some time to read up the docs tho

☺️ 1
zackteo09:12:40

I am using xtdb (or rather crux atm)