Fork me on GitHub
#xtdb
<
2023-11-18
>
emil0r13:11:14

Is it possible to write queries like this?

(xt/q (xt/db node) '{:find [?e ?k ?v]
                      :in [[?k ...]]
                      :where [[?e ?k ?v]]}
                     [:key1 :key2 :key3])

cjohansen14:11:25

Not sure how different datalog queries in xtdb are from datomic, but this works in datomic at least:

(d/q '[:find ?e ?a ?v
         :in $ [?a ...]
         :where
         [?e ?a ?v]]
       app-db
       [:key1 :key2])

emil0r14:11:43

Yeah… I get this when I run that code

cjohansen14:11:02

Your original query has too much nesting, I think

cjohansen14:11:36

Also, the brackets in your :find clause will give you only a single result.

cjohansen14:11:07

(Again, in Datomic 😅 )

emil0r14:11:09

Hmm… no. The datalog dialect is different. The query itself is correct

cjohansen14:11:21

ok, then I'm sorry for the noise

emil0r14:11:29

No worries 🙂

emil0r14:11:45

Mucho loves @U9MKYDN4Q 😛

zeitstein14:11:19

If I'm not mistaken, you cannot have the ?k variable in XT's Datalog. It needs to be specified ahead of time.

zeitstein14:11:18

You could do an or query against the :key*s

emil0r14:11:11

What would that look like?

zeitstein14:11:40

(xt/q (xt/db node)
  '{:find [?e ?v]
    :where [(or [?e :key1 ?v]
                [?e :key2 ?v]
                [?e :key3 ?v])]})

emil0r14:11:17

OK. So that still assumes you know the key ahead of time?

zeitstein14:11:37

Yep. But your original example also does. (If I'm not mistaken, your query and my query are equivalent.)

zeitstein14:11:08

It is one of the shortcomings of XT's Datalog. I've always been able to get around it, though.

emil0r14:11:41

Right. I did say in the next comment underneath the parent of this thread I don’t know the value of ?k ahead of time 🙂

emil0r14:11:48

It was just an example

emil0r15:11:34

I have another implementation that gets what I want. Just not as elegant

🙂 1
zeitstein15:11:19

For completeness, if the key is truly unknown ahead of time, there's a way through the attribute-statshttps://juxt-oss.zulipchat.com/#narrow/stream/194466-xtdb-users/topic/reverse.20references.3F/near/255741555.

emil0r15:11:52

Hmmm… I could probably generate a query then with an or clause

refset19:11:30

this was a hack, never tested thoroughly...but might be useful if you really want something dynamic https://gist.github.com/refset/93135adcbf41fccab9b641638ab10997

emil0r13:11:49

I don’t know the value of ?k in advance in the code I’m writing