Fork me on GitHub
#xtdb
<
2023-07-30
>
Ferdinand Beyer10:07:54

Is it possible that (or …) does not work if we leave out the v position of the triple? I wanted to query for “has attr X or has attr Y”. This gives no results:

:where [[?e :type/attr]
        (or [?e :x] [?e :y])]
However, if I add a _ variable at the v position, the query works as expected
:where [[?e :type/attr]
        (or [?e :x _] [?e :y _])]

refset11:08:02

Hey @U922FGW59 thanks for bringing this up. What you describe looks like a bug to me but I can't repro it. Am I doing something wrong here:

(with-open [n (xt/start-node {})]
    (let [_ (xt/submit-tx n [[::xt/put {:xt/id :foo
                                        :type :thing
                                        :x 1
                                        :y 2}]])
          _ (xt/sync n)]
      (xt/q (xt/db n)
            '{:find [(pull e [*])]
              :where [[e :type]
                      (or [?e :x]
                          [?e :y])]})))
;;=> #{[{:type :thing, :x 1, :y 2, :xt/id :foo}]}

Ferdinand Beyer12:08:39

Maybe it works for you because your entity contains both :x and :y? I can try coming up with a complete reproducing example, I found this in my real code that I don’t want to reproduce here…

refset14:08:55

I also tried with just :x or :y but still couldn't repro

refset14:08:22

> I can try coming up with a complete reproducing example that would be great if you can. What kind of values sit underneath x and y?

Ferdinand Beyer14:08:59

Strings. In my case I had two URLs, a :entity/image-url and an :entity/fallback-image-url, and a “result” attribute. I need to find all entities that have no result and either URL or fallback URL. I did not get any result even though there were documents matching this criteria, and adding _ at the v position worked 🤷 But let me see if I can create a minimal example

👍 2
Ferdinand Beyer06:08:54

Alright, here is a reproducing example with version 1.24.0:

(with-open [node (xt/start-node {})]
  (xt/submit-tx node [[::xt/put {:xt/id 1
                                 :id 1
                                 :name "cat"}]
                      [::xt/put {:xt/id 2
                                 :id 2
                                 :url "dog.png"}]])
  (xt/sync node)
  (xt/q (xt/db node)
        '{:find [?a]
          :where [[?a :id]
                  (or [?a :name]
                      [?a :url])]}))
I expect to get #{[1] [2]}, but the result is #{}. I get the expected result if I change the query in one of these ways: 1. Add a _ at the v position in the (or …) 2. Remove the [?a :id] clause (however this changes the intent of the query) I further noticed that I get partial results if I only add one _:
'{:find [?a]
  :where [[?a :id]
          (or [?a :name _]
              [?a :url])]}
; => {[1]}

'{:find [?a]
  :where [[?a :id]
          (or [?a :name]
              [?a :url _])]}
; => {[2]}
@U899JBRPF — should I create a GitHub issue for this?

refset11:08:57

ahh, I made a e/`?e` typo in my example which didn't help. I have a very minimal example now: > (with-open [n (xt/start-node {})] > (let [_ (xt/submit-tx n [[::xt/put {:xt/id :foo :x 1}]]) > _ (xt/sync n)] > (xt/q (xt/db n) > '{:find [e] > :where [[e :xt/id] > #[e :x ] ;; works as expected > #(or [e :x ]) ;; works as expected > #_[e :x] ;; works as expected > (or [e :x]) ;; doesn't work > ]}))) Thanks for your help with identifying this. Please feel free to raise an issue if you have a spare 5m 🙏 (but if it's easier I can of course do so instead)