datascript icon indicating copy to clipboard operation
datascript copied to clipboard

Function Input not Evaluated in Query Rules

Open MyriaCore opened this issue 6 years ago • 4 comments

I could easily be missing something, but to my current understanding, the intended behavior between the two code snippets below should be identical.

(d/q '[:find ?int
       :in ?intersection
       :where [(?intersection #{:a :b :c} #{:c :d :e}) ?int]]
     clojure.set/intersection))
;; => #{[#{:c}]}

(d/q '[:find ?int
       :in % ?intersection
       :where (intersection-rule #{:a :b :c} #{:c :d :e} ?int)]
  '[[(intersection-rule ?s ?t ?int)
     [(?intersection ?s ?t) ?int]]]
  clojure.set/intersection))
;; => Execution error (ExceptionInfo) at datascript.query/bind-by-fn (query.cljc:546). 
;;    Unknown function '?intersection__auto__1 in [(?intersection__auto__1 #{:c :b :a} #{:e :c :d}) 

MyriaCore avatar Aug 05 '19 21:08 MyriaCore

Yes, seems like a bug here https://github.com/tonsky/datascript/blob/f18db4e595692603e7c0904b7d0ef96837d586da/src/datascript/query.cljc#L580-L586

It tries to rename rule vars based on if symbol starts with a ?, not on actual arguments list

tonsky avatar Aug 06 '19 10:08 tonsky

Wow, thanks for the quick response! I thought I was just doing something wrong.

Is there a workaround for the time being?

MyriaCore avatar Aug 06 '19 15:08 MyriaCore

you can try explicitly pass fn as an argument to the rule. Might work, might not

tonsky avatar Aug 06 '19 17:08 tonsky

Alright, that seems to work. Thanks!

(d/q '[:find ?int
       :in ?intersection
       :where [(?intersection #{:a :b :c} #{:c :d :e}) ?int]]
     clojure.set/intersection))
;; => #{[#{:c}]}


(d/q '[:find ?int
       :in % ?intersection
       :where (intersection-rule ?intersection #{:a :b :c} #{:c :d :e} ?int)]
  '[[(intersection-rule ?f ?s ?t ?int)
     [(?f ?s ?t) ?int]]]
  clojure.set/intersection))
;; => #{[#{:c}]}

MyriaCore avatar Aug 07 '19 16:08 MyriaCore