datalog

zackteo 2021-12-01T09:03:35.012200Z

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

zackteo 2021-12-01T09:06:21.014500Z

I have a query that goes something like this

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

zackteo 2021-12-01T09:07:49.016400Z

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

zackteo 2021-12-01T09:09:00.017400Z

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

refset 2021-12-01T11:00:38.017800Z

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)]]}

refset 2021-12-01T11:01:16.018Z

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

zackteo 2021-12-01T11:23:15.018200Z

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
refset 2021-12-01T11:30:50.018500Z

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

zackteo 2021-12-01T14:35:39.018800Z

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

πŸ™ 1
zackteo 2021-12-01T14:37:01.019Z

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

☺️ 1
zackteo 2021-12-01T09:24:40.017700Z

I am using xtdb (or rather crux atm)