Hello everyone! 🪲 I have encountered some kind of bug with caching queries (i guess). Maybe that is a known issue or I misunderstand something tho... Running such code:
(d/transact! (app/db-conn) [[:db/add -1 :srv/user-name "admin"]
[:db/add -1 :srv/id 777]])
(let [db (d/db (app/db-conn))]
(clojure.pprint/pprint ["DATOMS:" (d/datoms db :eav 37)])
(println "Q1:\t" (d/q '[:find [(pull ?e [*]) ...]
:where [?e :srv/user-name]]
db))
(println "Q2:\t" (d/q '[:find [(pull ?e [*]) ...]
:where [?e :srv/id]]
db))
(println "Q3:\t" (d/q '[:find [(pull ?e [*]) ...]
:where (or [?e :srv/id])]
db))))
expectedly outputs:
│ ["DATOMS:" ([37 :srv/user-name "admin"] [37 :srv/id 777])]
│ Q1: [{:db/id 37, :srv/user-name admin, :srv/id 777}]
│ Q2: [{:db/id 37, :srv/user-name admin, :srv/id 777}]
│ Q3: [{:db/id 37, :srv/user-name admin, :srv/id 777}]
But if we add before transaction a query Q0 same as Q2 like this:
(println "Q0:\t" (d/q '[:find [(pull ?e [*]) ...]
:where [?e :srv/id]]
(d/db (app/db-conn))))
(d/transact! (app/db-conn) [[:db/add -1 :srv/user-name "admin"]
[:db/add -1 :srv/id 777]])
(let [db (d/db (app/db-conn))]
(clojure.pprint/pprint ["DATOMS:" (d/datoms db :eav 37)])
(println "Q1:\t" (d/q '[:find [(pull ?e [*]) ...]
:where [?e :srv/user-name]]
db))
(println "Q2:\t" (d/q '[:find [(pull ?e [*]) ...]
:where [?e :srv/id]]
db))
(println "Q3:\t" (d/q '[:find [(pull ?e [*]) ...]
:where (or [?e :srv/id])]
db))))
we expect Q0 returns empty collection, but it also interfere output of Q2:
│ Q0: []
│ ["DATOMS:" ([37 :srv/user-name "admin"] [37 :srv/id 777])]
│ Q1: [{:db/id 37, :srv/user-name admin, :srv/id 777}]
│ Q2: []
│ Q3: [{:db/id 37, :srv/user-name admin, :srv/id 777}]
As we can see such behaviour interfere only exactly same query. Even putting initial clause inside redundant or fixes the moment. That's why I guess that something about caching works bad...
This happen both inside with-transaction body and in common application.
So... any thought? Am I understand expected behaviour (empty Q0 but non-emty Q2) right or there is another concept? Is it known issue or I have to create one?
MAYBE IMPORTANT: I am using Babashka pod 0.9.10 , but in #babashka channel they said that no any pod requests caching implemented.If I change d/q on d/explain {:run? true}, then we have this output:
│ Q0: {:planning-time 0.131, :actual-result-size 0, :execution-time 0.026, :opt-clauses [[?e :srv/id]], :query-graph {$ {?e {:free #:srv{:id {}}}}}, :plan nil, :late-clauses [], :result []}
│ ["DATOMS:" ([37 :srv/user-name "admin"] [37 :srv/id 777])]
│ Q1: {:planning-time 0.121, :actual-result-size 1, :execution-time 0.435, :opt-clauses [[?e :srv/user-name]], :query-graph {$ {?e {:free #:srv{:user-name {}}}}}, :plan {$ [[{:steps [Initialize [?e] by :srv/user-name.], :size nil, :cost nil, :actual-size 1}]]}, :late-clauses [], :result [37]}
│ Q2: {:planning-time 0.018, :actual-result-size 0, :execution-time 0.001, :opt-clauses [[?e :srv/id]], :query-graph {$ {?e {:free #:srv{:id {}}}}}, :plan nil, :late-clauses [], :result []}
│ Q3: {:planning-time 0.030, :actual-result-size 1, :execution-time 0.523, :opt-clauses [], :query-graph {}, :plan nil, :late-clauses [(or [?e :srv/id])], :result [37]}
Where so small execution time of Q2 convince me that cache was used...Seems the same issue that raised in a PR
Oh yes, that is! Now i see..
Fixed