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

Multiple occurances of variable in one clause #240

Open jonasseglare opened 6 years ago

jonasseglare commented 6 years ago

Thanks for making this library public. I am currently experimenting with Datascript and came across some strange behaviour. Either I am doing something wrong, or maybe it is a bug in Datascript. The problem is best illustrated with unit test that I wrote:

(deftest strange-test
  (let [schema {:db/id {:db/unique :db.unique/identity}
                :expr/depends-on {:db/type :db.type/ref
                            :db/cardinality :db.cardinality/many}}
        conn (datascript/create-conn schema)]
    (datascript/transact!
     conn
     [{:db/id 1}
      {:db/id 2}
      {:db/id 3}
      [:db/add 3 :expr/depends-on 1]
      [:db/add 3 :expr/depends-on 2]
      [:db/add 2 :expr/depends-on 2]])

    ;; Find expressions that depend on themselves.
    ;; This test *fails* but I would expect it to pass. Is it a bug in Datascript?
    (is (= #{[2]}
           (datascript/q
            '[:find ?x
              :where
              [?x :expr/depends-on ?x]]
            @conn)))

    ;; But this, more bulky version, works. It passes, as I expect.
    (is (= #{[2]}
           (datascript/q
            '[:find ?x
              :where
              [?x :expr/depends-on ?y]
              [(= ?x ?y)]]
            @conn)))))

This first is form fails, but I would expect it to pass. The second is form passes, as I would expect. I would expect both to pass.

The Datascript version is 0.16.2. The error message in the unit test is

Fail in strange-test
expected: (=
 #{[2]}
 (datascript/q '[:find ?x :where [?x :expr/depends-on ?x]] @conn))
  actual: (not (= #{[2]} #{[2] [1]}))

Did I misunderstand something about how Datascript works, or is this a bug? Thank you.

tonsky commented 6 years ago

Hm. I remember hitting it but can’t find any code or even an issue. Let’s consider this a bug