Skip to content

Commit

Permalink
Merge pull request #514 from fluree/fix/group-by-vars
Browse files Browse the repository at this point in the history
Fix/group by vars
  • Loading branch information
zonotope authored Jun 19, 2023
2 parents 2e17159 + 7209466 commit b354f3d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ package-lock.json
/js-packages/browser/flureedb.js
/js-packages/nodejs/flureenjs.js
/js-packages/webworker/flureeworker.js
/.shadow-cljs/
13 changes: 10 additions & 3 deletions src/fluree/db/query/analytical.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -623,10 +623,17 @@


(defn calculate-aggregate
[tuples aggregate-fn-map]
[{:keys [headers vars tuples]} aggregate-fn-map]
(let [{:keys [variable as function]} aggregate-fn-map
agg-params (flatten (select-from-tuples [variable] tuples))
agg-result (function agg-params)]
var (or (as-variable variable)
(:variable variable))
value (if-let [var-idx (util/index-of headers var)]
(map #(nth % var-idx) tuples)
(if-let [val (get vars var)]
(map (constantly val) tuples)
(throw (ex-info (str "Invalid aggregate variable:" var)
{:status 400 :error :db/invalid-query}))))
agg-result (function value)]
[as agg-result]))

(defn add-aggregate-cols
Expand Down
26 changes: 17 additions & 9 deletions src/fluree/db/query/fql.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -324,22 +324,30 @@
(<? (pipeline-expandmaps-result select pp-keys single-result? db fuel max-fuel opts 8 result))
result))))))


(defn ad-hoc-group-by
[{:keys [headers tuples] :as res} groupBy]
[{:keys [headers vars tuples] :as res} groupBy]
(log/info "Result passed to group-by:" res)
(let [[inVector? groupBy] (cond (vector? groupBy) [true (map symbol groupBy)]
(string? groupBy) [false [(symbol groupBy)]]
:else (throw (ex-info
(str "Invalid groupBy clause, must be a string or vector. Provided: " groupBy)
{:status 400 :error :db/invalid-query})))
group-idxs (map #(util/index-of headers %) groupBy)
_ (when (some nil? group-idxs)
(throw (ex-info
(str "Invalid groupBy clause - are all groupBy vars declared in the where clause. Provided: " groupBy)
{:status 400 :error :db/invalid-query})))]
group-idxs (map (fn [group-var]
(if-let [group-idx (util/index-of headers group-var)]
{::idx group-idx}
(if-let [group-val (get vars group-var)]
{::value group-val}
(throw (ex-info
(str "Invalid groupBy clause - are all groupBy vars declared in the where clause. Provided: " groupBy)
{:status 400 :error :db/invalid-query})))))
groupBy)]
(reduce
(fn [res tuple]
(let [k (map #(nth tuple %) group-idxs)
(let [k (map (fn [val-spec]
(if-let [idx (::idx val-spec)]
(nth tuple idx)
(::value val-spec)))
group-idxs)
k' (if inVector? (into [] k) (first k))
v tuple]
(assoc res k' (conj (get res k' []) v))))
Expand Down Expand Up @@ -432,7 +440,7 @@
(defn relationship-binding
[{:keys [vars] :as opts}]
(async/go-loop [[next-vars & rest-vars] vars
acc []]
acc (if (:groupBy opts) {} [])]
(if next-vars
(let [opts' (assoc opts :vars next-vars)
res (<? (process-ad-hoc-query opts'))]
Expand Down

0 comments on commit b354f3d

Please sign in to comment.