juji-io / datalevin

A simple, fast and versatile Datalog database
https://github.com/juji-io/datalevin
Eclipse Public License 1.0
1.07k stars 60 forks source link

Query bug Assertion Error from 0.8.29 to 0.9.7 #262

Closed markusalbertgraf closed 5 days ago

markusalbertgraf commented 5 days ago
(ns assertion-error
  (:require
   [datalevin.core :as d]))

(def schema
  {:a/x {:db/cardinality :db.cardinality/one :valueType :db.type/ref}
   :a/b {:db/cardinality :db.cardinality/one :valueType :db.type/ref}
   :c/b {:db/cardinality :db.cardinality/one :valueType :db.type/ref}
   :d/c {:db/cardinality :db.cardinality/one :valueType :db.type/ref} ; <- ref
   :d/e {:db/cardinality :db.cardinality/one :valueType :db.type/ref}
   :f/e {:db/cardinality :db.cardinality/one :valueType :db.type/ref}
   :f/x {:db/cardinality :db.cardinality/one :valueType :db.type/ref}})

(def db-data
  [{:db/id 1 :type :x}
   {:db/id 2 :type :x}
   {:db/id 3 :type :b}
   {:db/id 4 :type :c :c/b 3}
   {:db/id 5 :type :c :c/b 3}
   {:db/id 6 :type :e}
   {:db/id 7 :type :e}
   {:db/id 8 :type :d :d/c 4 :d/e 6}
   {:db/id 9 :type :d :d/c 5 :d/e 7}
   {:db/id 10 :type :f :f/x 1 :f/e 6}
   {:db/id 11 :type :f :f/x 2 :f/e 6}
   {:db/id 12 :type :f :f/x 1 :f/e 7}
   {:db/id 13 :type :f :f/x 2 :f/e 7}
   {:db/id 14 :type :a :a/x 1 :a/b 3}])

(def conn (d/get-conn "/tmp/dldb/assetionerror" schema))

(d/transact! conn db-data)

(d/q '[:find [?f ...]
       :in $ ?a
       :where
       [?a :a/b ?b]
       [?c :c/b ?b]
       [?d :d/c ?c]
       [?d :d/e ?e]
       [?f :f/e ?e]
       [?f :f/x ?x]
       [?a :a/x ?x]]
     @conn 14)

;; => [12 10]

(d/q '[:find [?f ...]
       :in $ ?a
       :where
       [?a :a/b ?b]
       [?c :c/b ?b]
       [?d :d/c ?c]
       [?d :d/e ?e]
       [?f :f/e ?e]
       [?f :f/x ?x]
       #_[?a :a/x ?x]] ; this causes throw
     @conn 14)

;; Throws Assertion Error :d/c not a :db.type/ref
markusalbertgraf commented 5 days ago

I was being stupid. :valueType should have been :db/valueType in schema.