Skip to content

Commit

Permalink
(PDB-5739) inventory-query: convert stable||volatile to hsql
Browse files Browse the repository at this point in the history
  • Loading branch information
rbrw committed May 9, 2024
1 parent 8e339ea commit 6468694
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
13 changes: 7 additions & 6 deletions src/puppetlabs/puppetdb/query_eng/engine.clj
Original file line number Diff line number Diff line change
Expand Up @@ -281,14 +281,12 @@
"facts" {:type :queryable-json
:projectable-json? true
:queryable? true
:field [:raw "(fs.stable||fs.volatile)"]
:field-type :raw
:field [:nest [:|| :fs.stable :fs.volatile]]
:join-deps #{:fs}}
"trusted" {:type :queryable-json
:projectable-json? true
:queryable? true
:field [:raw "(fs.stable||fs.volatile)->'trusted'"]
:field-type :raw
:field [:-> [:|| :fs.stable :fs.volatile] [:inline "trusted"]]
:join-deps #{:fs}}}

:selection {:from [[:factsets :fs]]
Expand Down Expand Up @@ -1666,8 +1664,11 @@
JsonbPathBinaryExpression
(-plan->sql [{:keys [field value column-data operator]} _opts]
(su/jsonb-path-binary-expression operator
(if (= :raw (:field-type column-data))
(-> column-data :field second)
;; FIXME: anything other than
;; vector :fields or string
;; fields?
(if (vector? (:field column-data))
(:field column-data)
field)
value))

Expand Down
11 changes: 6 additions & 5 deletions test/puppetlabs/puppetdb/query_eng_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,19 @@

(deftest test-extract-json-subtree-compiles
(testing "with differing levels of subtrees"
(is (re-find #"SELECT \(fs.stable\|\|fs.volatile\) -> 'os' AS \"facts\.os\" .*FROM factsets"
(is (re-find #"SELECT \(fs.stable \|\| fs.volatile\) -> 'os' AS \"facts\.os\" .*FROM factsets"
(->> ["extract" "facts.os"]
(compile-user-query->sql inventory-query)
:results-query
first)))
(is (re-find #"SELECT \(fs.stable\|\|fs.volatile\) -> 'os' -> 'family' AS \"facts\.os\.family\" .*FROM factsets"
(is (re-find #"SELECT \(fs.stable \|\| fs.volatile\) -> 'os' -> 'family' AS \"facts\.os\.family\" .*FROM factsets"
(->> ["extract" "facts.os.family"]
(compile-user-query->sql inventory-query)
:results-query
first))))

(testing "when field is raw sql"
(is (re-find #"SELECT \(fs.stable\|\|fs.volatile\)->'trusted' -> 'certname' AS \"trusted\.certname\" .*FROM factsets"
(is (re-find #"SELECT \(\(fs.stable \|\| fs.volatile\) -> 'trusted'\) -> 'certname' AS \"trusted\.certname\" .*FROM factsets"
(->> ["extract" "trusted.certname"]
(compile-user-query->sql inventory-query)
:results-query
Expand All @@ -201,14 +201,15 @@
:results-query
first)))))

;; ??? (and check all other || and/or jsonb_each in tests)
(deftest index-hit-for-trusted-facts-on-inventory
(testing "facts.trusted.extensions and trusted.extensions should generate the same SQL"
(is (re-find #"WHERE \(fs.stable\|\|fs.volatile\) @> ?"
(is (re-find #"WHERE \(fs.stable \|\| fs.volatile\) @> ?"
(->> ["extract" [] ["=" "facts.trusted.extensions.foo" "bar"]]
(compile-user-query->sql inventory-query)
:results-query
first)))
(is (re-find #"WHERE \(fs.stable\|\|fs.volatile\) @> ?"
(is (re-find #"WHERE \(fs.stable \|\| fs.volatile\) @> ?"
(->> ["extract" [] ["=" "trusted.extensions.foo" "bar"]]
(compile-user-query->sql inventory-query)
:results-query
Expand Down

0 comments on commit 6468694

Please sign in to comment.