tonsky / datascript

Immutable database and Datalog query engine for Clojure, ClojureScript and JS
Eclipse Public License 1.0
5.45k stars 304 forks source link

Lookup refs using an input #137

Open atroche opened 8 years ago

atroche commented 8 years ago

Hi, I'm trying to do a query using lookup refs, using an input variable in the lookup ref, like this:

(d/q '[:find ?e
       :in $ ?client-squuid
       :where [?e :appointment/client [:thing/squuid ?client-squuid]]]
     (d/db conn)
     #uuid "5664cb92-8256-49f9-a7ff-30d3d00939c4")

When I do the above, I get:

#object[Error Error: Cannot compare 5664cb78-8010-4d4e-bfdf-b1b6416396d2 to ?client-squuid]
     cljs.core.UUID.cljs$core$IComparable$_compare$arity$2 (jar:file:/Users/alistair/.m2/repository/com/cognitect/transit-cljs/0.8.225/transit-cljs-0.8.225.jar!/cognitect/transit.cljs:42:14)
     cljs.core/-compare (jar:file:/Users/alistair/.m2/repository/org/clojure/clojurescript/1.7.170/clojurescript-1.7.170.jar!/cljs/core.cljs:701:22)
     cljs.core/compare (jar:file:/Users/alistair/.m2/repository/org/clojure/clojurescript/1.7.170/clojurescript-1.7.170.jar!/cljs/core.cljs:2080:5)
     datascript.db/cmp-val (jar:file:/Users/alistair/.m2/repository/datascript/datascript/0.13.3/datascript-0.13.3.jar!/datascript/db.cljc:285:6)
     datascript$db$cmp_datoms_avet (jar:file:/Users/alistair/.m2/repository/datascript/datascript/0.13.3/datascript-0.13.3.jar!/datascript/db.cljc:308:6)

even though this:

(d/q '[:find ?e
       :where [?e :appointment/client [:thing/squuid #uuid "5664cb92-8256-49f9-a7ff-30d3d00939c4"]]]
     (d/db conn))

works fine.

Is this expected behaviour?

Cheers!

tonsky commented 8 years ago

Well, it is in a sense that I haven’t thought about someone would use it this way. I’ll think what can be done about that. Meanwhile, you can rewrite your query like this:

(d/q '[:find ?e
       :in $ ?client-squuid
       :where [?t :thing/squuid ?client-squuid]
              [?e :appointment/client ?t]]
     (d/db conn)
     #uuid "5664cb92-8256-49f9-a7ff-30d3d00939c4")
atroche commented 8 years ago

Thanks @tonsky. It's definitely not the end of the world =)

On 11 December 2015 at 21:18, Nikita Prokopov notifications@github.com wrote:

Well, it is in a sense that I haven’t thought about someone would use it this way. I’ll think what can be done about that. Meanwhile, you can rewrite your query like this:

(d/q '[:find ?e :in $ ?client-squuid :where [?t :thing/squuid ?client-squuid] [?e :appointment/client ?t]](d/db conn)

uuid "5664cb92-8256-49f9-a7ff-30d3d00939c4")

— Reply to this email directly or view it on GitHub https://github.com/tonsky/datascript/issues/137#issuecomment-163901698.

-- Alistair

fasiha commented 8 years ago

Aha! I was wondering why this didn't work:

(d/q '[:find ?film-name (distinct ?actor)
       :in $ ?a1 ?a2
       :where
       [?film :film/name ?film-name]
       [?film :film/cast [:actor/name ?a1]]
       [?film :film/cast [:actor/name ?a2]]
       [?film :film/cast ?actor]
       ]
     @conn
     "Nancy Allen"
     "Peter Weller")
; => ClassCastException clojure.lang.Symbol cannot be cast to java.lang.String  java.lang.String.compareTo (String.java:111)

while this did:

(d/q '[:find ?film-name (distinct ?actor)
       :where
       [?film :film/name ?film-name]
       [?film :film/cast [:actor/name "Nancy Allen"]]
       [?film :film/cast [:actor/name "Peter Weller"]]
       [?film :film/cast ?actor]]
     @conn)
; => (["RoboCop" [46 47 45]])

and this is why :D luckily, I'm making my query map programmatically and can work around this that way. Thought I was going crazy because the query map worked, strings as literal arguments worked, just strings as input arguments didn't.