Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/asami/projection.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,32 @@
(throw (ex-info (str "Projection variables not found in the selected data: " missing)
{:missing missing :data columns})))))))

(s/defn derive-pattern-and-columns :- (s/pair [s/Any] :pattern
[s/Any] :columns)
"Given a pattern (possibly with usage of the :as syntax), return a
pair of a pattern without usage of the :as syntax and renamed
columns as described by the :as syntax.

Example:

(derive-pattern-and-columns '[?x :data :as ?z ?y])
;; => [[?x :data ?y] [?x ?z ?y]]"
Comment thread
noprompt marked this conversation as resolved.
[pattern]
(loop [pattern pattern
new-pattern []
renamed-columns []]
(if (seq pattern)
(let [[x y z :as xs] (take 3 pattern)
Comment thread
noprompt marked this conversation as resolved.
Outdated
new-pattern (conj new-pattern x)]
(if (= :as y)
(recur (drop 3 pattern)
new-pattern
(conj renamed-columns (or z x)))
Comment thread
noprompt marked this conversation as resolved.
Outdated
(recur (rest pattern)
new-pattern
(conj renamed-columns x))))
[new-pattern renamed-columns])))

(s/defn project-results :- Results
"Converts each row from a result, into just the requested columns, as per the patterns arg.
Any specified value in the patterns will be copied into that position in the projection.
Expand All @@ -216,12 +242,12 @@
pattern :- [s/Any]
columns :- [Var]
data :- Results]
(let [full-pattern (vec pattern)
(let [[full-pattern pattern-columns] (derive-pattern-and-columns pattern)
pattern->data (offset-mappings full-pattern columns data)
nodes (new-nodes pattern->data)]
(with-meta
(map #(project-row store-fns full-pattern nodes pattern->data %) data)
{:cols full-pattern})))
{:cols pattern-columns})))

(s/defn project :- s/Any
"Converts data from results, into just the requested columns, as per the patterns arg.
Expand Down
26 changes: 26 additions & 0 deletions test/asami/projection_test.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
(ns asami.projection-test
(:require [asami.projection :as projection]
#?(:clj [clojure.test :as t]
:cljs [cljs.test :as t :include-macros true])))

(t/deftest derive-pattern-and-columns-test
(t/is (= '[[] []]
(projection/derive-pattern-and-columns [])))

(t/is (= '[[?x] [?x]]
(projection/derive-pattern-and-columns '[?x])))

;; Dangling :as is ignored.
(t/is (= '[[:data] [:data]]
(projection/derive-pattern-and-columns '[:data :as])))

(t/is (= '[[:data] [:data]]
(projection/derive-pattern-and-columns '[:data])))

(t/is (= '[[:data] [?x]]
(projection/derive-pattern-and-columns '[:data :as ?x])))

(t/is (= '[[?x :data ?y] [?x ?z ?y]]
(projection/derive-pattern-and-columns '[?x :data :as ?z ?y]))))


8 changes: 8 additions & 0 deletions test/asami/query_internals_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -363,4 +363,12 @@
(is (vartest? p))
(is (vartest? v)))))

(deftest test-column-renaming
Comment thread
noprompt marked this conversation as resolved.
(is (= '{:cols [?x ?z ?y]}
(meta (q/query-entry '{:find [?x :data :as ?z ?y]
Comment thread
noprompt marked this conversation as resolved.
Outdated
:where [[?x _ ?y]]}
empty-graph
[(assert-data empty-graph [[1 :y 2]])]
false)))))

#?(:cljs (run-tests))