Skip to content

Commit

Permalink
Fixed empty non-queried relation not affecting results (closes #385)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Mar 2, 2021
1 parent 82487ee commit 69ac944
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# WIP

- Fixed empty non-queried relation not affecting results #385

# 1.0.4

- Implement nav/datafy for entities #325 thx @IamDrowsy
Expand Down
2 changes: 1 addition & 1 deletion script/watch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

cd "`dirname $0`/.."

clojure -A:test -m kaocha.runner --watch "$@"
clojure -A:test -M -m kaocha.runner --watch "$@"
44 changes: 27 additions & 17 deletions src/datascript/query.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -861,23 +861,33 @@
(let [rels (:rels context)]
(-collect [(da/make-array (count symbols))] rels symbols)))
([acc rels symbols]
(if-some [rel (first rels)]
(let [keep-attrs (select-keys (:attrs rel) symbols)]
(if (empty? keep-attrs)
(recur acc (next rels) symbols)
(let [copy-map (to-array (map #(get keep-attrs %) symbols))
len (count symbols)]
(recur (for [#?(:cljs t1
:clj ^{:tag "[[Ljava.lang.Object;"} t1) acc
t2 (:tuples rel)]
(let [res (aclone t1)]
(dotimes [i len]
(when-some [idx (aget copy-map i)]
(aset res i (#?(:cljs da/aget :clj get) t2 idx))))
res))
(next rels)
symbols))))
acc)))
(cond+
:let [rel (first rels)]

(nil? rel) acc

;; one empty rel means final set has to be empty
(empty? (:tuples rel)) []

:let [keep-attrs (select-keys (:attrs rel) symbols)]

(empty? keep-attrs) (recur acc (next rels) symbols)

:let [copy-map (to-array (map #(get keep-attrs %) symbols))
len (count symbols)]

:else
(recur
(for [#?(:cljs t1
:clj ^{:tag "[[Ljava.lang.Object;"} t1) acc
t2 (:tuples rel)]
(let [res (aclone t1)]
(dotimes [i len]
(when-some [idx (aget copy-map i)]
(aset res i (#?(:cljs da/aget :clj get) t2 idx))))
res))
(next rels)
symbols))))

(defn collect [context symbols]
(->> (-collect context symbols)
Expand Down
12 changes: 11 additions & 1 deletion test/datascript/test/query.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
(:import [clojure.lang ExceptionInfo])))



(deftest test-joins
(let [db (-> (d/empty-db)
(d/db-with [ { :db/id 1, :name "Ivan", :age 15 }
Expand Down Expand Up @@ -231,5 +230,16 @@
"X")
#{["abcX"] ["aXb"]})))


(deftest ^{:doc "issue-385"} test-join-unrelated
(is (= #{}
(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)))))

#_(require 'datascript.test.query :reload)
#_(clojure.test/test-ns 'datascript.test.query)

0 comments on commit 69ac944

Please sign in to comment.