Skip to content

Commit

Permalink
Nested subqueries
Browse files Browse the repository at this point in the history
  • Loading branch information
bplatz committed Jun 5, 2024
1 parent 74b2454 commit adf0282
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/clj/fluree/db/query/exec.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,11 @@
;; end of subqueries search... if result-chans extract as initial soln to where, else execute where
(if (seq result-chans)
;; found subqueries, collect them into initial values solution and the execute
(->> (collect-subqueries ds fuel-tracker q error-ch result-chans)
(execute ds fuel-tracker (assoc q :where (not-empty where*)) error-ch))
(let [q* (assoc q :where (not-empty where*))
subquery-son (collect-subqueries ds fuel-tracker q error-ch result-chans)]
(if subquery?
(execute-subquery ds fuel-tracker q* error-ch subquery-son)
(execute ds fuel-tracker q* error-ch subquery-son)))
;; no sub-queries, just execute
(let [q* (assoc q :where (not-empty where*))] ;; removed subqueries from where clause
(if subquery?
Expand Down
29 changes: 28 additions & 1 deletion test/fluree/db/query/subquery_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,31 @@
"where" {"schema:age" "?age"}}]
["subquery" {"select" ["?favNums"]
"where" {"ex:favNums" "?favNums"}}]]
"orderBy" ["?age" "?favNums"]})))))))
"orderBy" ["?age" "?favNums"]}))))

(testing "with nested subqueries"
(is (= [["Alice" "alice@example.org" 13] ["Alice" "alice@example.org" 34] ["Alice" "alice@example.org" 50]
["Alice" "brian@example.org" 13] ["Alice" "brian@example.org" 34] ["Alice" "brian@example.org" 50]
["Alice" "cam@example.org" 13] ["Alice" "cam@example.org" 34] ["Alice" "cam@example.org" 50]
["Alice" "liam@example.org" 13] ["Alice" "liam@example.org" 34] ["Alice" "liam@example.org" 50]
["Brian" "alice@example.org" 13] ["Brian" "alice@example.org" 34] ["Brian" "alice@example.org" 50]
["Brian" "brian@example.org" 13] ["Brian" "brian@example.org" 34] ["Brian" "brian@example.org" 50]
["Brian" "cam@example.org" 13] ["Brian" "cam@example.org" 34] ["Brian" "cam@example.org" 50]
["Brian" "liam@example.org" 13] ["Brian" "liam@example.org" 34] ["Brian" "liam@example.org" 50]
["Cam" "alice@example.org" 13] ["Cam" "alice@example.org" 34] ["Cam" "alice@example.org" 50]
["Cam" "brian@example.org" 13] ["Cam" "brian@example.org" 34] ["Cam" "brian@example.org" 50]
["Cam" "cam@example.org" 13] ["Cam" "cam@example.org" 34] ["Cam" "cam@example.org" 50]
["Cam" "liam@example.org" 13] ["Cam" "liam@example.org" 34] ["Cam" "liam@example.org" 50]
["Liam" "alice@example.org" 13] ["Liam" "alice@example.org" 34] ["Liam" "alice@example.org" 50]
["Liam" "brian@example.org" 13] ["Liam" "brian@example.org" 34] ["Liam" "brian@example.org" 50]
["Liam" "cam@example.org" 13] ["Liam" "cam@example.org" 34] ["Liam" "cam@example.org" 50]
["Liam" "liam@example.org" 13] ["Liam" "liam@example.org" 34] ["Liam" "liam@example.org" 50]]
@(fluree/query db {"@context" {"schema" "http://schema.org/"
"ex" "http://example.org/ns/"}
"select" ["?name" "?email" "?age"]
"where" [{"schema:name" "?name"}
["subquery" {"selectDistinct" ["?age" "?email"]
"where" [{"schema:age" "?age"}
["subquery" {"select" ["?email"]
"where" {"schema:email" "?email"}}]]}]]
"orderBy" ["?name" "?email" "?age"]})))))))

0 comments on commit adf0282

Please sign in to comment.