xtdb

jf 2024-09-29T12:10:02.258549Z

I am having trouble constructing a “complex” (only that it isnt the simple “and”-ing of conditions) query. I thought I had it… until I tried the query with a different argument. It’s probably best to explain with data and a query (my exact data and query are not the same, but otherwise it’s the same):

[;; users
 {:xt/id :petr   :owner/name "Petr"  :owner/store :abc}
 {:xt/id :ivan   :owner/name "Ivan"  :owner/store :abc-branch}
 {:xt/id :sergei :ower/name "Sergei" :owner/store :abc-branch}
 {:xt/id :boris  :owner/name "Boris" :owner/store :abc-branch2}

 ;; stores
 {:xt/id :abc        :store/address "addr1"}
 {:xt/id :abc-branch  :store/address "addr2" :store/parent :abc}
 {:xt/id :abc-branch2 :store/address "addr3" :store/parent :abc}

 ;; books
 {:xt/id :b1 :book/store :abc ...}
 {;xt/id :b2 :book/store :abc-branch ...}
 {:xt/id :b3 :book/store :abc-branch2 ...}]

;; given an owner id, find all books sold at owner's store or branches of owner's store

    (xt/q db
                  '{:find [(= owner book-store-owner) (pull book [* {:book/store [:store/name :store/address]}])]
                    :where [[owner :owner/store direct-store]
                            (or-join [book direct-store owner book-store-owner]
                                     (and [book             :book/store  direct-store]
                                          [book-store-owner :owner/store direct-store]
                                          [(== book-store-owner owner)])
                                     (and [branch           :store/parent direct-store]
                                          [book             :book/store   branch]
                                          [book-store-owner :owner/store  branch]))]
                    :in [admin]}
                  :ivan)]
My initial results with the query seemed promising (I was querying with :petr), but once I started to query using the id of a branch owner instead (say :ivan) I would end up with books in sister branches as well, which is not what I want.. as well as duplicates of the books in the branch (and from investigation, it looks like because the branch has 2 owners) I did initially use an or… but I would get an error saying how the variables needed to be the same across both branches (my interpretation); but that’s not really what I want to do. Because the branches are different. One is for books sold at the store directly owned.. and the other is for books sold at branches. UPDATE: managed to resolve it with this updated query. I would have to give up pulling out book-store-owner though, but it does indeed look like that was the issue with the earlier queries I tried: trying to pull info on owners with a multi-ownership scenario.
(xt/q db
                  '{:find [(pull book [* {:book/store [:store/name :store/address]}])]
                    :where [[owner :owner/store direct-store]
                            (or-join [book direct-store]
                                     [book :book/store direct-store]
                                     (and [branch           :store/parent direct-store]
                                          [book             :book/store   branch]))]
                    :in [admin]}
                  :ivan)]

refset 2024-09-30T12:12:05.858549Z

Hey, just wanted to say thanks for confirming you'd found the solution and updating the message

👍 1
whilo 2024-09-29T22:41:54.958299Z

Hey @taylor.jeremydavid. I am looking at different relational runtimes atm. Out of curiosity, why did xtdb stop using the hyperloglog? Is it because elements cannot be removed?

jarohen 2024-09-30T01:24:57.719479Z

Hey @whilo - it'll likely make a return the next time we have a few iterations on the XT2 query planner. At the moment, we've just gone for a simpler algo that's still reasonably performant

➕ 1