Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query Bug: False positive results #319

Open
panterarocks49 opened this issue Mar 1, 2025 · 3 comments
Open

Query Bug: False positive results #319

panterarocks49 opened this issue Mar 1, 2025 · 3 comments
Labels
enhancement New feature or request

Comments

@panterarocks49
Copy link
Contributor

The following query produces results when I would expect it to produce none. (I'm on 0.9.20)

(require '[datalevin.core :as dl])

(def conn (dl/create-conn "/tmp/dl/test" {:block/uid  {:db/unique :db.unique/identity}
                                           :node/title {:db/unique :db.unique/identity}
                                           :block/refs {:db/valueType   :db.type/ref
                                                        :db/cardinality :db.cardinality/many}}))

(dl/transact! conn [{:db/id     -1
                     :block/uid "test"}
                    {:db/id      -2
                     :block/refs -1}])

(dl/q
 '[:find ?SBlock
   :where
   [?ParentPage :block/uid "test"]
   [?SPage :node/title "Opposed By"]
   [?ParentPage :block/uid ?some-uid]
   [?SBlock :block/refs ?SPage]
   ]
 @conn)
;; => #{[2]}

If I swap the clause order it does return what I would expect

(dl/q
 '[:find ?SBlock
   :where
   [?ParentPage :block/uid "test"]
   [?ParentPage :block/uid ?some-uid]
   [?SPage :node/title "Opposed By"]
   [?SBlock :block/refs ?SPage]
   ]
 @conn)
  ;; => #{}

A similar issue happens if I remove the :block/refs clause, but this time it returns a nil id

(dl/q
 '[:find ?SPage
   :where
   [?ParentPage :block/uid "test"]
   [?SPage :node/title "Opposed By"]
   [?ParentPage :block/uid ?some-uid]
   ]
 @conn)
;; => #{[nil]}

Thanks in advance for looking into this!

@huahaiy huahaiy added the bug Something isn't working label Mar 1, 2025
@huahaiy
Copy link
Contributor

huahaiy commented Mar 2, 2025

These queries do not seem to make sense. :block/uid is unique identity, so how is it possible to have two clauses about them?

[?ParentPage :block/uid "test"]
[?ParentPage :block/uid ?some-uid]

So ?some-uid is just "test", isn't it?

This is a pathological case, and I would consider this undefined behavior. Of course, we could check for this and make the handling of these more robust. But it would be an enhancement, not a bug fix.

@huahaiy huahaiy added enhancement New feature or request and removed bug Something isn't working labels Mar 2, 2025
@panterarocks49
Copy link
Contributor Author

This query is part of a larger query which is generated by a higher level query language so it's definitely an edge case

The full query is a bit harder to follow so I just posted the simplest case I could find which produced similar behavior

This is the full query, but it won't reproduce on this test data

[:find
 (pull ?Evidence [:block/string :node/title :block/uid])
 (pull ?Evidence [:block/uid])
 :where
 [?gLLeASJNc-ParentPage :block/uid "test"]
 [?gLLeASJNc-SPage :node/title "Opposed By"]
 [?gLLeASJNc-ParentPage :block/uid ?gLLeASJNc-v6ruZRoob-uid]
 [?v6ruZRoob :block/uid ?gLLeASJNc-v6ruZRoob-uid]
 [?gLLeASJNc-SBlock :block/refs ?gLLeASJNc-SPage]
 [?gLLeASJNc-SBlock :block/children ?gLLeASJNc-Block]
 [?gLLeASJNc-Block :block/refs ?gLLeASJNc-Page]
 [?gLLeASJNc-Page :node/title ?gLLeASJNc-Page-Title]
 [?gLLeASJNc-Page :block/uid ?gLLeASJNc-Evidence-uid]
 [?Evidence :block/uid ?gLLeASJNc-Evidence-uid]
 [?gLLeASJNc-PBlock :block/refs ?gLLeASJNc-ParentPage]
 [?gLLeASJNc-PBlock :block/children ?gLLeASJNc-SBlock]
 [(re-pattern "^\[\[EVD\]\] - (.*?) - (.*?)$") ?gLLeASJNc-EVD---regex]
 [(re-find ?gLLeASJNc-EVD---regex ?gLLeASJNc-Page-Title)]
 ]

I can observe the same behavior with a non unique identity as well like this, to me it seems to have to do with the non unifying clauses.

(dl/q
 '[:find ?SBlock
   :where
   [?ParentPage :block/refs 1]
   [?SPage :node/title "Opposed By"]
   [?ParentPage :block/refs ?some-uid]
   [?SBlock :block/refs ?SPage]
   ]
 @conn)

Anyway, if this is expected I can try to detect it in my code

@huahaiy
Copy link
Contributor

huahaiy commented Mar 2, 2025

It is an edge case that I have not considered, so it needs to be detected and handled properly. The next release should have a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants