Discussion:
Cannot get logical disjunction to work
Eugene Trifuntov
2015-03-09 12:01:05 UTC
Permalink
Hello,

I have two valid queries:

user=> (d/q '[:find ?e :where [?e :user/username "username1"]] (d/db conn))
#{[17592186045418]}
user=> (d/q '[:find ?e :where [?e :user/username "username2"]] (d/db conn))
#{[17592186045509]}

Now I want the query that will be logical disjunction of the two above, id
est find all entities having "username1" OR "username2" in :user/username.
Following datomic docs I am building query like:

(d/q '[:find ?e :where (or [?e :user/username "username1"] [?e
:user/username "username2"])] (d/db conn))

which throws: CompilerException java.lang.RuntimeException: Unable to
resolve symbol: ?e in this context

This exception is strange by itself, because whole query is quoted and I
have no idea why clojure tries to evaluate it anyway, but it's not the
question. When I am trying to feed the query to console it also looks
strange.

1. Inside query textarea the query converted to:
[:find ?e
:where
["or" [?e :user/username "username1"] [?e :user/username "username2"]]
]

2. Following error issued: message: processing clause: ["or" [?e
:user/username "username1"] [?e :user/username "username2"]], message:
:db.error/invalid-lookup-ref Invalid list form: [?e :user/username
"username2"]



My questions are:
1. What's the proper syntax for query I'd like to build?
2. Since I'm gonna build it programmatically what is proper map form for
such query?
--
You received this message because you are subscribed to the Google Groups "Datomic" group.
To unsubscribe from this group and stop receiving emails from it, send an email to datomic+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Francis Avila
2015-03-09 16:14:29 UTC
Permalink
The idiomatic way to express your query does not use "or" at all:

[:find ?e :where [(ground ["username1" "username2"]) [?username ...]][?e :
user/username ?username]

Or since your intention is to use this programmatically, provide the
usernames as an input parameter:

(d/q '[:find ?e :in $ [?username ...] :where [?e :user/username ?username]]
(d/db conn) vector-of-usernames)

As a map:

{:find '[?e] :in '[$ [?username ...]] :where '[[?e :user/username
?username]]}

(Translation to the map form is mechanical.)

That said, I think your "or" form should work also, and don't know why it
does not.

The problems you get on the datomic console are probably bugs in the
datomic console. Its query formatter probably doesn't know about the new
clauses and turns everything to vectors and strings.
Post by Eugene Trifuntov
Hello,
user=> (d/q '[:find ?e :where [?e :user/username "username1"]] (d/db conn))
#{[17592186045418]}
user=> (d/q '[:find ?e :where [?e :user/username "username2"]] (d/db conn))
#{[17592186045509]}
Now I want the query that will be logical disjunction of the two above, id
est find all entities having "username1" OR "username2" in :user/username.
(d/q '[:find ?e :where (or [?e :user/username "username1"] [?e
:user/username "username2"])] (d/db conn))
which throws: CompilerException java.lang.RuntimeException: Unable to
resolve symbol: ?e in this context
This exception is strange by itself, because whole query is quoted and I
have no idea why clojure tries to evaluate it anyway, but it's not the
question. When I am trying to feed the query to console it also looks
strange.
[:find ?e
:where
["or" [?e :user/username "username1"] [?e :user/username "username2"]]
]
2. Following error issued: message: processing clause: ["or" [?e
:db.error/invalid-lookup-ref Invalid list form: [?e :user/username
"username2"]
1. What's the proper syntax for query I'd like to build?
2. Since I'm gonna build it programmatically what is proper map form for
such query?
--
You received this message because you are subscribed to the Google Groups "Datomic" group.
To unsubscribe from this group and stop receiving emails from it, send an email to datomic+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Stuart Sierra
2015-03-09 20:32:18 UTC
Permalink
Post by Francis Avila
The problems you get on the datomic console are probably
bugs in the datomic console. Its query formatter probably
doesn't know about the new clauses and turns everything to
vectors and strings.
Correct. The Datomic Console doesn't (yet?) support some of
the newer query syntax, including `or` clauses.
--
You received this message because you are subscribed to the Google Groups "Datomic" group.
To unsubscribe from this group and stop receiving emails from it, send an email to datomic+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Igor Ges
2017-09-14 17:06:24 UTC
Permalink
I just ran into similar problem using 'not' clause with datomic
v.0.9.5561.50
Would it be correct to guess that the aforementioned yet never came? Are
there any plans to update the console in the future?
Thanks,
I.G.
Post by Stuart Sierra
Post by Francis Avila
The problems you get on the datomic console are probably
bugs in the datomic console. Its query formatter probably
doesn't know about the new clauses and turns everything to
vectors and strings.
Correct. The Datomic Console doesn't (yet?) support some of
the newer query syntax, including `or` clauses.
--
You received this message because you are subscribed to the Google Groups "Datomic" group.
To unsubscribe from this group and stop receiving emails from it, send an email to datomic+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Loading...