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

Duplicated/un-merged results for nested optionals #256

Open
kirahowe opened this issue Oct 12, 2023 · 0 comments
Open

Duplicated/un-merged results for nested optionals #256

kirahowe opened this issue Oct 12, 2023 · 0 comments

Comments

@kirahowe
Copy link

There's a bug where matches of optional clauses seem to return duplicated or unbound vars when they're nested inside a map in a build query.

For example:

;; Given this data:
(def db (matcha/index-triples [[:s1 :p1 :o1]
                               [:s1 :p2 :o2]
                               [:o1 :po1 :oo1]
                               [:o1 :po2 :oo2]]))

;; This works as expected (whether the last clause is optional or not, i.e. both):
(matcha/build [:id ?id]
              {:p1  ?o1
               :po1 ?oo1
               :po2 ?oo2}
              [[?id :p1 ?o1]
               [?id :p2 ?o2]
               [?o1 :po1 ?oo1]
               [?o1 :po2 ?oo2]]
              db)

;; and
(matcha/build [:id ?id]
              {:p1  ?o1
               :po1 ?oo1
               :po2 ?oo2}
              [[?id :p1 ?o1]
               [?id :p2 ?o2]
               [?o1 :po1 ?oo1]
               (matcha/optional [[?o1 :po2 ?oo2]])]
              db)

;; Return, as expected: 
({:id :s1
  :p1 :o1
  :po1 :oo1
  :po2 :oo2})

;; Nesting some of the results under a separate key also works when no clauses are optional:
(matcha/build [:id ?id]
              {:p1  ?o1
               :o1 {:po1 ?oo1
                    :po2 ?oo2}}
              [[?id :p1 ?o1]
               [?id :p2 ?o2]
               [?o1 :po1 ?oo1]
               [?o1 :po2 ?oo2]]
              db)

;; Returns, as expected:
({:id :s1
  :p1 :o1
  :o1 {:po1 :oo1
       :po2 :oo2}})

;; But making one of the clauses optional breaks the result (I would expect the same result as above here, but we get a set instead of a single nested resource):
(matcha/build [:id ?id]
              {:p1  ?o1
               :o1 {:po1 ?oo1
                    :po2 ?oo2}}
              [[?id :p1 ?o1]
               [?id :p2 ?o2]
               [?o1 :po1 ?oo1]
               (matcha/optional [[?o1 :po2 ?oo2]])]
              db)

;; Returns:
({:id :s1
  :p1 :o1
  :o1 #{{:po1 :oo1, :po2 _0}
        {:po1 :oo1, :po2 :oo2}}})

These can be manually fixed/flattened of course, but I think the result given is not the ideal solution.

This may have been introduced when fixing #21.

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

No branches or pull requests

1 participant