Rel from a value in another document

Hello,

When trying out the examples for Rel from a value in another document

this one:

(xt/q xt-node
          '(-> (from :docs [my-nested-rel])
               (rel my-nested-rel [a b])))

gives an error:

1. Unhandled xtdb.IllegalArgumentException
   Illegal argument: ':xtql/unknown-query-tail'
   {:xtdb.error/error-type :illegal-argument,
    :xtdb.error/error-key :xtql/unknown-query-tail,
    :xtdb.error/message "Illegal argument: ':xtql/unknown-query-tail'",
    :op rel}
                 error.clj:   13  xtdb.error/illegal-arg
                 error.clj:    4  xtdb.error/illegal-arg

and this one:

(xt/q xt-node
          '(unify (from :docs [my-nested-rel])
                  (rel my-nested-rel [a b])))

gives an error:

1. Unhandled xtdb.IllegalArgumentException
   Illegal second argument to `rel`
   {:xtdb.error/error-type :illegal-argument,
    :xtdb.error/error-key :xtql/rel,
    :xtdb.error/message "Illegal second argument to `rel`",
    :arg my-nested-rel}
                 error.clj:   13  xtdb.error/illegal-arg

Hey @wkok, the first error looks like it’s a mistake in the documentation. I don’t think rel can be used like that (at least not anymore) - I will clarify / make sure we fix that :slight_smile:

Your second error is a mystery to me, so I will investigate, but in the meantime you will probably have a better experience by following the examples of using the rel/unnest/unify combinations seen in the tutorial here:

Apologies for the rough edges!

Thanks @refset

What I’m trying to determine is how to filter on a nested field in a document for example this document from the example I’ve linked:

{:xt/id <id>, :my-nested-rel [{:a 1, :b 2}, ...]}

how to go about querying for everything where :a equals 1 ?

Using unnest I can successfully get to a flatter structure but not sure how to destructure/bind deeper into the result so I can say (where (= a 1))

The closest example I could find is the one I linked, I cannot see the same attempted from the fuller tutorial you linked but I might just miss it :slight_smile:

1 Like

Despite the examples in those docs, rel currently only supports literals or query parameters (and not column references / logic vars).

The workaround you can use is explicit unnest + projecting nested field access, e.g.

(xt/submit-tx node [(xt/put :foo {:xt/id :bar
                            :baz [{:a 1 :b 2}
                                  {:a 3 :b 4}]})])

(xt/q node '(-> (from :foo [xt/id baz])
                (unnest {:row baz})
                (with {:a (.. row :a)
                       :b (.. row :b)})
                (where (= a 1))))
;; => [{:row {:b 2, :a 1}, :baz [{:b 2, :a 1} {:b 4, :a 3}], :xt/id :bar, :a 1, :b 2}]
1 Like

Thanks, this works like a charm :slight_smile:

1 Like