Skip to content

Commit 045942b

Browse files
committed
Revert "Merge pull request #3510 from rbrw/pdb-5161-parse-ast-query-fields"
Temporarily revert this work so that we can fix a regression (PDB-5214) with lower risk. We plan to restore it right after. This reverts commit 957f533, reversing changes made to f89c4c6.
1 parent 529909d commit 045942b

File tree

9 files changed

+229
-523
lines changed

9 files changed

+229
-523
lines changed

src/puppetlabs/puppetdb/jdbc.clj

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
(:import
1414
(com.zaxxer.hikari HikariDataSource HikariConfig)
1515
(java.sql Connection SQLException SQLTransientConnectionException)
16-
(java.util.concurrent TimeUnit)
17-
(org.postgresql.util PGobject)))
16+
(java.util.concurrent TimeUnit)))
1817

1918
(def ^:dynamic *db* nil)
2019

@@ -393,26 +392,6 @@
393392
[s]
394393
(str "\"" (escape-double-quotes s) "\""))
395394

396-
(defn string->text-array-literal
397-
"Escape string s for inclusion in a postgres text[] literal,
398-
e.g. \"foo\\\"bar\" becomes the \"foo\\\\\\\"bar\" in
399-
'{\"foo\\\\\\\"bar\"}'"
400-
;; https://www.postgresql.org/docs/11/arrays.html#ARRAYS-INPUT
401-
;; https://www.postgresql.org/docs/11/sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS
402-
[s]
403-
(assert (string? s))
404-
(str \" (str/replace s "\\" "\\\\") \"))
405-
406-
(defn strs->db-array
407-
[strs]
408-
(assert (every? string? strs))
409-
;; https://www.postgresql.org/docs/11/arrays.html#ARRAYS-INPUT
410-
(let [quoted (map string->text-array-literal strs)]
411-
(doto (PGobject.)
412-
(.setType "text[]")
413-
(.setValue (str \{ (str/join \, quoted) \})))))
414-
415-
;; Q: move/replace?
416395
(defn create-json-path-extraction
417396
"Given a base json field and a path of keys to traverse, construct the proper
418397
SQL query of the form base->'key'->'key2'..."

src/puppetlabs/puppetdb/query_eng/engine.clj

Lines changed: 109 additions & 202 deletions
Large diffs are not rendered by default.

src/puppetlabs/puppetdb/query_eng/parse.clj

Lines changed: 0 additions & 166 deletions
This file was deleted.

src/puppetlabs/puppetdb/scf/storage_utils.clj

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,12 @@
244244
(hcore/raw
245245
(format "%s = ANY(?)" (first (hfmt/format column)))))
246246

247+
(defn json-contains
248+
[field array-in-path]
249+
(if array-in-path
250+
(hcore/raw (format "%s #> ? = ?" field))
251+
(hcore/raw (format "%s @> ?" field))))
252+
247253
(defn jsonb-path-binary-expression
248254
"Produce a predicate that compares against nested value with op and checks the
249255
existence of a (presumably) top-level value. The existence check is necessary
@@ -376,3 +382,42 @@
376382
(log/info (trs "Analyzing small tables"))
377383
(apply jdbc/do-commands-outside-txn
378384
(map #(str "analyze " %) small-tables)))
385+
386+
(defn handle-quoted-path-segment
387+
[v]
388+
(loop [result []
389+
[s & splits] v]
390+
(let [s-len (count s)]
391+
(if (and (str/ends-with? s "\"")
392+
(not (= s-len 1))
393+
(or (<= s-len 2) (not (= (nth s (- s-len 2)) \\))))
394+
[(str/join "." (conj result s)) splits]
395+
(recur (conj result s) splits)))))
396+
397+
(defn dotted-query->path
398+
[string]
399+
(loop [[s & splits :as v] (str/split string #"\.")
400+
result []]
401+
(if (nil? s)
402+
result
403+
(let [s-len (count s)]
404+
(if (and (str/starts-with? s "\"")
405+
(or (= s-len 1)
406+
(or (not (str/ends-with? s "\""))
407+
(and (str/ends-with? s "\"")
408+
(>= s-len 2)
409+
(= (nth s (- s-len 2)) \\)))))
410+
(let [[x xs] (handle-quoted-path-segment v)]
411+
(recur xs (conj result x)))
412+
(recur splits (conj result s)))))))
413+
414+
(defn expand-array-access-in-path
415+
"Given a path like [\"a\" \"b[0]\" \"c\"], expand the [0] to get
416+
[\"a\" \"b\" 0 \"c\"]"
417+
[path]
418+
(mapcat (fn [el]
419+
(let [[[_ field index-str]] (re-seq #"^(.*)\[(\d+)\]$" el)]
420+
(if index-str
421+
[field (Integer/parseInt index-str)]
422+
[el])))
423+
path))

src/puppetlabs/puppetdb/utils.clj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,24 @@
285285
(apply assoc m new-kvs)
286286
m)))
287287

288+
(defn parse-matchfields
289+
[s]
290+
(str/replace s #"match\((\".*\")\)" "$1"))
291+
292+
(defn parse-indexing
293+
[s]
294+
(str/replace s #"\[(\d+)\]" ".$1"))
295+
296+
(defn split-indexing
297+
[path]
298+
(flatten
299+
(for [s path]
300+
(if (re-find #"\[\d+\]$" s)
301+
(-> s
302+
(str/split #"(?=\[\d+\]$)")
303+
(update 1 #(Integer/parseInt (subs % 1 (dec (count %))))))
304+
s))))
305+
288306
(defn regex-quote
289307
[s]
290308
(when (and (string? s) (re-find #"\\E" s))

src/puppetlabs/puppetdb/utils/string_formatter.clj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717
(interpose "\\E\\\\E\\Q"))
1818
["\\E"])))
1919

20+
(defn maybe-strip-escaped-quotes
21+
[s]
22+
(if (and (> (count s) 1)
23+
(string/starts-with? s "\"")
24+
(string/ends-with? s "\""))
25+
(subs s 1 (dec (count s)))
26+
s))
27+
2028
(defn quoted
2129
[s]
2230
(str "'" s "'"))

test/puppetlabs/puppetdb/pql/parser_test.clj

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
(ns puppetlabs.puppetdb.pql.parser-test
2-
(:require
3-
[clojure.string :as str]
4-
[clojure.test :refer :all]
5-
[instaparse.core :as insta]
6-
[puppetlabs.puppetdb.pql :refer [parse]]))
2+
(:require [clojure.test :refer :all]
3+
[clojure.string :as str]
4+
[instaparse.core :as insta]
5+
[puppetlabs.puppetdb.scf.storage-utils :as sutils]
6+
[puppetlabs.puppetdb.pql :refer [parse]]))
77

88
;; These tests are ordered the same as in the EBNF file, so one can
99
;; develop the expressions and tests side-by-side.
@@ -563,18 +563,18 @@
563563
""))
564564

565565
(testing "field"
566-
(doseq [[field expected]
567-
[["certname" [:field "certname"]]
568-
["value" [:field "value"]]
569-
["field_underscore" [:field "field_underscore"]]
570-
["facts.operatingsystem.κόσμε" [:field "facts" "operatingsystem" "κόσμε"]]
571-
["facts.\"quoted field\".foo" [:field "facts" "\"quoted field\"" "foo"]]
572-
["facts.\"field.with.dot\".foo" [:field "facts" "\"field.with.dot\"" "foo"]]
573-
["facts.\"field-with-dash\".foo" [:field "facts" "\"field-with-dash\"" "foo"]]
574-
["trusted.authenticated" [:field "trusted" "authenticated"]]
575-
["parameters.😁" [:field "parameters" "😁"]]
576-
["latest_report?" [:field "latest_report?"]]]]
577-
(is (= expected (parse field :start :field))))
566+
(are [in] (= (parse in :start :field)
567+
(vec (concat [:field] (sutils/dotted-query->path in))))
568+
"certname"
569+
"value"
570+
"field_underscore"
571+
"facts.operatingsystem.κόσμε"
572+
"facts.\"quoted field\".foo"
573+
"facts.\"field.with.dot\".foo"
574+
"facts.\"field-with-dash\".foo"
575+
"trusted.authenticated"
576+
"parameters.😁"
577+
"latest_report?")
578578

579579
(are [in] (insta/failure? (insta/parse parse in :start :field))
580580
"'asdf'"

0 commit comments

Comments
 (0)