Skip to content

Commit 2dcfc23

Browse files
committed
(PDB-5214) (Re)allow dotted projections with group_by and functions
Dotted projection arguments to group_by and functions were being rejected. Fix that, and add some initial tests. ["from" "inventory" ["extract" [["function" "count"] "facts.kernel"] ["=" "node_state" "active"] ["group_by" "facts.kernel"]]]
1 parent 045942b commit 2dcfc23

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/puppetlabs/puppetdb/query_eng/engine.clj

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,12 +2175,15 @@
21752175
into its function name. Throws an IllegalArgumentException if there is an
21762176
invalid field or function"
21772177
[query-rec column-or-fn-name]
2178-
(or (get-in query-rec [:projections column-or-fn-name :field])
2179-
(if (some #{column-or-fn-name} (keys pdb-fns->pg-fns))
2180-
(keyword column-or-fn-name)
2181-
(throw (IllegalArgumentException.
2182-
(tru "{0} is niether a valid column name nor function name"
2183-
(pr-str column-or-fn-name)))))))
2178+
;; Just split on dot for now (as a hack) - we'll use the strict
2179+
;; parser once it's available (after 6.18.0 and 7.5.0)."
2180+
(let [[root] (str/split column-or-fn-name #"\." 2)]
2181+
(or (get-in query-rec [:projections root :field])
2182+
(if (some #{column-or-fn-name} (keys pdb-fns->pg-fns))
2183+
(keyword column-or-fn-name)
2184+
(throw (IllegalArgumentException.
2185+
(tru "{0} is niether a valid column name nor function name"
2186+
(pr-str column-or-fn-name))))))))
21842187

21852188
(defn group-by-entries->fields
21862189
"Convert a list of group by columns and functions to their true SQL field names."

test/puppetlabs/puppetdb/http/inventory_test.clj

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,12 @@
170170

171171
;; TODO figure out what behavior we want for queries with null w/o a dotted path
172172
["null?" "facts" true]
173-
#{})))
173+
#{}
174+
175+
["extract" [["function", "count"] "facts.domain"]
176+
["group_by" "facts.domain"]]
177+
#{{:facts.domain "testing.com" :count 1}
178+
{:facts.domain nil :count 1}})))
174179

175180
(deftest-http-app inventory-queries
176181
[[version endpoint] inventory-endpoints
@@ -213,8 +218,8 @@
213218
[["function" "count" "certname"]]
214219
["in" "facts.os.family" ["array" ["RedHat"]]]]
215220
{:keys [body status]} (query-response method endpoint query)]
216-
(is (= status 500))
217-
(is (= body "Value does not match schema: (not (map? nil))"))))
221+
(is (= 500 status))
222+
(is (= "Value does not match schema: (not (map? nil))" body))))
218223

219224
(testing "inventory queries"
220225
(testing "well-formed queries"
@@ -224,7 +229,7 @@
224229
(get-request endpoint (json/generate-string query))
225230
(get-request endpoint))
226231
{:keys [status body headers]} (*app* request)]
227-
(is (= status http/status-ok))
232+
(is (= http/status-ok status))
228233
(is (http/json-utf8-ctype? (headers "Content-Type")))
229234
(is (= (set result)
230235
(set (json/parse-string (slurp body) true)))))))))))

0 commit comments

Comments
 (0)