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

Difference in pull query output between 1.0.1 and 1.3.11 #430

Closed Kauko closed 2 years ago

Kauko commented 2 years ago

Posting this for a colleague who does not have a GitHub account.

For the following pull query, Datascript 1.0.1 includes the :order/amount 20, whereas 1.3.11 does not.

(ns test-datascript
    (:require [datascript.core :as d]))

  (let [schema
        {:client/id #:db{:unique :db.unique/identity},
         :client/order #:db {:cardinality :db.cardinality/many,
                             :valueType :db.type/ref,
                             :isComponent true}
         :order/delivery-address #:db{:cardinality :db.cardinality/one,
                                      :valueType :db.type/ref,
                                      :isComponent true}}
        db-conn (d/create-conn schema)]

    (d/transact! db-conn
                 [{:client/id "Optimal Frontier"
                   :client/order
                   [{:order/amount 20
                     :order/delivery-address {:address/street :x}}]}])
    ;; Issue: Datascript 1.0.1 will include the :order/amount 20 as output for
    ;; this pull query (which is correct/expected), whereas Datascript 1.3.11 does
    ;; not (=issue/bug):
    (d/pull
     @db-conn
     [{:client/order
       [{:order/delivery-address
         [:address/street
          {:order/_delivery-address [:order/amount]}]}]}]
     [:client/id "Optimal Frontier"])
    ;; of course, if I were really interested in the :order/amount I would use a
    ;; different pull pattern but this is just to have a reproduction scenario
    ;; including the reverse relation traversal in the pull query
    )
tonsky commented 2 years ago

Current behaviour is in line with Datomic: if you don’t specify pattern for component, everything is pulled. If you do specify pattern, only what you’ve specified is pulled. If you want everything still, add * to the pattern.

tonsky commented 2 years ago

Looks like I misunderstood where the problem lies. The problem is that after reverse lookup :order/amount is not present. This is probably caused by “Infinite loops prevention”. Looking into it

tonsky commented 2 years ago

Should be fixed in 1.3.12, commit 5aeb57f26b9ad26e2ffbd0e97b004a10402c5521