Reflexive tuples block certain rule-driven inferences
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Start by running the minimal Clojure/JVM reproduction in the issue and compare its empty result with the two simplified queries and the Datomic result. Trace rule-driven Datalog inference around reflexive tuples and indirect rule expansion; done means the original query returns the four expected entity pairs without regressing the simplified cases.
Written by the indexing model from the issue text.
Description
This is a thorny bug that only seems to happen when an entity is directly related to itself via some attribute. I've only tried this on the JVM, not in cljs. Here is the most minimal example I can construct:
(let [conn
(ds/create-conn
{:a {:db/valueType :db.type/ref}
:b {:db/valueType :db.type/ref}})
[x y]
(map - (range))
_
(ds/transact! conn [{:db/id x
:a y}
{:db/id y
:a y
:b x}])]
(ds/q '{:find [?p ?q]
:in [$ %]
:where [(bar ?p ?q)]}
@conn
'[[(foo ?p ?q)
[?p :b ?q]]
[(bar ?p ?q)
(baz ?p ?r)
(baz ?q ?r)]
[(baz ?p ?q)
[?p :a ?r]
(foo ?r ?q)]]))
;; => #{}
Removing a level of indirection yields the correct result -- both entities are related to themselves and to one another through rule bar:
(let [conn
(ds/create-conn
{:a {:db/valueType :db.type/ref}
:b {:db/valueType :db.type/ref}})
[x y]
(map - (range))
_
(ds/transact! conn [{:db/id x
:a y}
{:db/id y
:a y
:b x}])]
(ds/q '{:find [?p ?q]
:in [$ %]
:where [(bar ?p ?q)]}
@conn
'[[(bar ?p ?q)
(baz ?p ?r)
(baz ?q ?r)]
[(baz ?p ?q)
[?p :a ?r]
[?r :b ?q] ;; inlining rule `foo`
]]))
;; => #{[0 0] [1 0] [1 1] [0 1]}
Likewise through this equivalent simplification:
(let [conn
(ds/create-conn
{:a {:db/valueType :db.type/ref}
:b {:db/valueType :db.type/ref}})
[x y]
(map - (range))
_
(ds/transact! conn [{:db/id x
:a y}
{:db/id y
:a y
:b x}])]
(ds/q '{:find [?p ?q]
:in [$ %]
:where [;; inlining rule `bar`
(baz ?p ?r)
(baz ?q ?r)]}
@conn
'[[(foo ?p ?q)
[?p :b ?q]]
[(baz ?p ?q)
[?p :a ?r]
(foo ?r ?q)]]))
;; => #{[0 0] [1 0] [1 1] [0 1]}
Compare the equivalent Datomic Datalog, which yields the correct result for the original query and rules (as well as for both simplified versions):
(let [uri
"datomic:mem://throwaway-db"
db
(do
(d/delete-database uri)
(d/create-database uri)
(-> uri d/connect d/db))
db1 ;; install schema first
(:db-after
(d/with db
[{:db/id (d/tempid :db.part/db)
:db/ident :a
:db/cardinality :db.cardinality/one
:db/valueType :db.type/ref}
{:db/id (d/tempid :db.part/db)
:db/ident :b
:db/cardinality :db.cardinality/one
:db/valueType :db.type/ref}]))
[x y]
(repeatedly (fn [] (d/tempid :db.part/user)))
db2
(:db-after
(d/with db1
[{:db/id x
:a y}
{:db/id y
:a y
:b x}]))]
(d/q '{:find [?p ?q]
:in [$ %]
:where [(bar ?p ?q)]}
db2
'[[(foo ?p ?q)
[?p :b ?q]]
[(bar ?p ?q)
(baz ?p ?r)
(baz ?q ?r)]
[(baz ?p ?q)
[?p :a ?r]
(foo ?r ?q)]]))
;; => #{[17592186045418 17592186045419] [17592186045419 17592186045418] [17592186045419 17592186045419] [17592186045418 17592186045418]}
- Dominant language
- Clojure
- Stars
- 5.8k
- Forks
- 318
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from tonsky/datascript
-
Difficulty 4/5 3-5 days Newbie friendliness 42/100
tonsky/datascript#498 · 1 comment ·
-
Difficulty 5/5 Over a week Newbie friendliness 10/100
tonsky/datascript#489 ·
-
Stack overflow when transacting :db.type/tupleAttrs with a :db.type/ref attr through :db.fn/call Open
Difficulty 4/5 3-5 days Newbie friendliness 35/100
tonsky/datascript#483 · 2 comments ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
tonsky/datascript#470 · 1 comment ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
tonsky/datascript#441 · 1 comment · 3 reactions ·
All issues in tonsky/datascript
Similar issues
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
-
Difficulty 1/5 Under an hour Newbie friendliness 88/100
-
.Team/Metabot Priority:P3
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
needs triage
Difficulty 1/5 Under an hour Newbie friendliness 90/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
scalar-labs/scalar-jepsen#222 · 1 comment ·