Skip to content

Commit 53533a2

Browse files
committed
Fixed not-join not working properly with empty relations (closes #481)
1 parent 3c5d58d commit 53533a2

File tree

6 files changed

+45
-16
lines changed

6 files changed

+45
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# WIP
22

33
- Patch es6 iterators not having Symbol.iterator function #479 #249 via @kasbah
4+
- Fixed not-join not working properly with empty relations #481
45

56
# 1.7.3 - Jul 22, 2024
67

deps.edn

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@
2020
{:override-deps
2121
{org.clojure/clojure {:mvn/version "1.11.1"}}}
2222

23+
:1.12
24+
{:override-deps
25+
{org.clojure/clojure {:mvn/version "1.12.0"}}}
26+
2327
:dev
2428
{:extra-paths ["dev" "test"]
2529
:jvm-opts ["-ea" "-Ddatascript.debug" "-Dclojure.main.report=stderr"]
2630
:extra-deps
27-
{io.github.tonsky/duti {:git/sha "fc833a87a8687b67e66281e216eeee1ad6048168"}
31+
{io.github.tonsky/duti {:git/sha "e36d65296a4f9758664309ec35b00887e88c405a"}
2832
metosin/jsonista {:mvn/version "0.3.3"}
2933
cheshire/cheshire {:mvn/version "5.10.0"}
3034
com.cognitect/transit-clj {:mvn/version "1.0.324"}
@@ -37,9 +41,8 @@
3741
"-Djdk.attach.allowAttachSelf"
3842
"-XX:+DebugNonSafepoints"]
3943
:extra-deps
40-
{metosin/jsonista {:mvn/version "0.3.3"}
41-
criterium/criterium {:mvn/version "0.4.6"}
42-
com.clojure-goes-fast/clj-async-profiler {:mvn/version "1.0.0"}}}
44+
{metosin/jsonista {:mvn/version "0.3.3"}
45+
io.github.tonsky/duti {:git/sha "e36d65296a4f9758664309ec35b00887e88c405a"}}}
4346

4447
:datomic
4548
{:extra-paths ["bench_datomic" "test_datomic"]

dev/user.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(ns user
22
(:require
3-
[duti.core :as duti]))
3+
[duti.core :as duti]))
44

55
(duti/set-dirs "src" "bench" "test" #_"bench_datomic" #_"test_datomic")
66

script/repl.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
set -o errexit -o nounset -o pipefail
33
cd "`dirname $0`/.."
44

5-
clojure -A:1.11.1:dev:bench -M -m user
5+
clojure -M:1.12:dev:bench:datomic -m user

src/datascript/query.cljc

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@
4141
;; or [(Datom. 2 "Oleg" 1 55) ...]
4242
(defrecord Relation [attrs tuples])
4343

44+
#?(:clj
45+
(defmethod print-method Relation [r, ^java.io.Writer w]
46+
(.write w "#Relation{:attrs ")
47+
(.write w (pr-str (:attrs r)))
48+
(.write w ", :tuples [")
49+
(.write w (str/join " " (map seq (:tuples r))))
50+
(.write w "]}")))
51+
4452

4553
;; Utilities
4654

@@ -804,15 +812,25 @@
804812
*lookup-attrs*)]
805813
(update context :rels collapse-rels relation))))))
806814

815+
(defn short-circuit-empty-rel [context]
816+
(if (some #(empty? (:tuples %)) (:rels context))
817+
(assoc context
818+
:rels
819+
[(Relation.
820+
(zipmap (mapcat #(keys (:attrs %)) (:rels context)) (range))
821+
[])])
822+
context))
823+
807824
(defn resolve-clause [context clause]
808825
(if (->> (:rels context) (some (comp empty? :tuples)))
809826
context ; The result is empty; short-circuit processing
810-
(if (rule? context clause)
811-
(if (source? (first clause))
812-
(binding [*implicit-source* (get (:sources context) (first clause))]
813-
(resolve-clause context (next clause)))
814-
(update context :rels collapse-rels (solve-rule context clause)))
815-
(-resolve-clause context clause))))
827+
(short-circuit-empty-rel
828+
(if (rule? context clause)
829+
(if (source? (first clause))
830+
(binding [*implicit-source* (get (:sources context) (first clause))]
831+
(resolve-clause context (next clause)))
832+
(update context :rels collapse-rels (solve-rule context clause)))
833+
(-resolve-clause context clause)))))
816834

817835
(defn -q [context clauses]
818836
(binding [*implicit-source* (get (:sources context) '$)]

test/datascript/test/query_not.cljc

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848

4949
;; exclude empty set
5050
[[?e :name]
51-
(not [?e :name "Ivan"]
51+
(not
52+
[?e :name "Ivan"]
5253
[?e :name "Oleg"])]
5354
#{1 2 3 4 5 6}
5455

@@ -65,8 +66,7 @@
6566
#{2 4 6}))
6667

6768
(deftest test-not-join
68-
(are [q res] (= (d/q (concat '[:find ?e ?a :where] (quote q)) @*test-db)
69-
res)
69+
(are [q res] (= res (d/q (concat '[:find ?e ?a :where] (quote q)) @*test-db))
7070
[[?e :name]
7171
[?e :age ?a]
7272
(not-join [?e]
@@ -80,7 +80,14 @@
8080
[?e :name "Oleg"]
8181
[?e :age ?a]
8282
[?e :age 10])]
83-
#{[1 10] [5 10]}))
83+
#{[1 10] [5 10]}
84+
85+
;; issue-481
86+
[[?e :age ?a]
87+
(not-join [?a]
88+
[?e :name "Petr"]
89+
[?e :age ?a])]
90+
#{[1 10] [2 20] [3 10] [4 20] [5 10] [6 20]}))
8491

8592
(deftest test-default-source
8693
(let [db1 (d/db-with (d/empty-db)

0 commit comments

Comments
 (0)