wkok
26 January 2024 12:56
1
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
refset
26 January 2024 14:03
2
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
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!
1 Like
wkok
26 January 2024 16:02
3
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
1 Like
refset
26 January 2024 17:27
4
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
wkok
30 January 2024 10:17
5
Thanks, this works like a charm
1 Like