Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Predicate seems to be lazily evaluated and fails silently #385

Closed
frankiesardo opened this issue Feb 19, 2021 · 3 comments
Closed

Predicate seems to be lazily evaluated and fails silently #385

frankiesardo opened this issue Feb 19, 2021 · 3 comments

Comments

@frankiesardo
Copy link

frankiesardo commented Feb 19, 2021

Hey @tonsky

I've noticed a behaviour that is substantially different from Datomic, and might be a bug.

I'm using datascript 1.0.3 and I've tested this behaviour both on cljs and clj.

When I'm running

(d/q '[:find ?name ?result
       :in $ ?my-fn
       :where
       [?e :person/name ?name]
       [(?my-fn) ?result]
       [(< ?result 3)]]
     (d/db-with (d/empty-db) [{:person/name "Joe"}])
     (fn [] 5))

=> #{}

I correctly get an empty set (result is greater than 3)

But if I do not force the evaluation of ?result the condition is never checked

(d/q '[:find ?name
       :in $ ?my-fn
       :where
       [?e :person/name ?name]
       [(?my-fn) ?result]
       [(< ?result 3)]]
     (d/db-with (d/empty-db) [{:person/name "Joe"}])
     (fn [] 5))

=> #{["Joe"]}

The same behaviour occurs on the JVM if you call the function directly

(defn my-fn [] 5)

(d/q '[:find ?name
       :in $
       :where
       [?e :person/name ?name]
       [(user/my-fn) ?result]
       [(< ?result 3)]]
     (d/db-with (d/empty-db) [{:person/name "Joe"}]))

=> #{["Joe"]}

The equivalent code in Datomic produces an empty set instead.

(def conn (do (d/create-database "datomic:mem://test") (d/connect "datomic:mem://test")))
(defn my-fn [] 5)

(d/q '[:find ?doc
       :in $
       :where
       [?e :db/doc ?doc]
       [(user/my-fn) ?result]
       [(< ?result 3)]]
     (d/db conn))

=> #{}
@tonsky
Copy link
Owner

tonsky commented Feb 23, 2021

Looks like a bug — I’ll take a look!

@tonsky tonsky closed this as completed in 69ac944 Mar 2, 2021
@tonsky
Copy link
Owner

tonsky commented Mar 2, 2021

Pushed 1.0.5, hope it fixes the issue

frankiesardo pushed a commit to frankiesardo/minikusari that referenced this issue Mar 3, 2021
tonsky/datascript#385 fixes empty predicate so there's no need to add irrelevant metadata to the tx
@frankiesardo
Copy link
Author

It does, thanks for the quick fix! 🥇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants