Skip to content

Commit 62b80a0

Browse files
committed
Fixed query fn results unification (closes #490)
1 parent f38fbb7 commit 62b80a0

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# WIP
2+
3+
- Fixed query fn results unification #490
4+
15
# 1.7.5 - May 4, 2025
26

37
- Enable direct-linking on JVM #487 via @raspasov

src/datascript/query.cljc

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@
167167
(sum-rel b))))))
168168

169169
(defn prod-rel
170-
([] (Relation. {} [(da/make-array 0)]))
170+
([]
171+
(Relation. {} [(da/make-array 0)]))
171172
([rel1 rel2]
172173
(let [attrs1 (keys (:attrs rel1))
173174
attrs2 (keys (:attrs rel2))
@@ -544,12 +545,18 @@
544545
rels (for [tuple (:tuples production)
545546
:let [val (tuple-fn tuple)]
546547
:when (not (nil? val))]
547-
(prod-rel (Relation. (:attrs production) [tuple])
548-
(in->rel binding val)))]
548+
(reduce prod-rel
549+
(collapse-rels
550+
[(Relation. (:attrs production) [tuple])]
551+
(in->rel binding val))))]
549552
(if (empty? rels)
550-
(prod-rel production (empty-rel binding))
553+
(prod-rel
554+
production
555+
(empty-rel binding))
551556
(reduce sum-rel rels)))
552-
(prod-rel (assoc production :tuples []) (empty-rel binding)))]
557+
(prod-rel
558+
(assoc production :tuples [])
559+
(empty-rel binding)))]
553560
(update context :rels collapse-rels new-rel)))
554561

555562
;;; RULES

test/datascript/test/query_fns.cljc

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@
7979
(is (= (d/q '[:find ?a1
8080
:where [_ :age ?a1]
8181
[(>= ?a1 22)]] db)
82-
#{[22] [37]}))
82+
#{[22] [37]}))
83+
8384
(testing "compare values of different types"
8485
(is (= (d/q '[:find ?e
8586
:where [?e]
@@ -137,20 +138,23 @@
137138

138139
(testing "Two conflicting function values for one binding."
139140
(is (= (d/q '[:find ?n
140-
:where [(identity 1) ?n]
141+
:where
142+
[(identity 1) ?n]
141143
[(identity 2) ?n]])
142144
#{})))
143145

144146
(testing "Destructured conflicting function values for two bindings."
145147
(is (= (d/q '[:find ?n ?x
146-
:where [(identity [3 4]) [?n ?x]]
148+
:where
149+
[(identity [3 4]) [?n ?x]]
147150
[(identity [1 2]) [?n ?x]]])
148151
#{})))
149152

150153
(testing "Rule bindings interacting with function binding. (fn, rule)"
151154
(is (= (d/q '[:find ?n
152155
:in $ %
153-
:where [(identity 2) ?n]
156+
:where
157+
[(identity 2) ?n]
154158
(my-vals ?n)]
155159
db
156160
'[[(my-vals ?x)
@@ -229,6 +233,17 @@
229233
[])
230234
#{})))))
231235

236+
;; issue-490
237+
(deftest test-fn-call-results-unification
238+
(is (= #{[[:a :a] :a]}
239+
(d/q '[:find ?p ?x
240+
:where
241+
[_ :pair ?p]
242+
[(clojure.core/first ?p) ?x]
243+
[(clojure.core/second ?p) ?x]]
244+
[[1 :pair [:a :a]]
245+
[2 :pair [:b :c]]]))))
246+
232247
(deftest test-predicates
233248
(let [entities [{:db/id 1 :name "Ivan" :age 10}
234249
{:db/id 2 :name "Ivan" :age 20}

0 commit comments

Comments
 (0)