Skip to content

Commit 168e3e3

Browse files
committed
(PDB-5216) query->sql: simplify before making query-id universal
Rework query->sql to simplify it a bit in preparation for making the query-id availble all the time, whether or not we log it. Move the local sql conversion code into a separate regular-query->sql function, and move the query logging inside query->sql.
1 parent 91f34f8 commit 168e3e3

File tree

1 file changed

+54
-59
lines changed

1 file changed

+54
-59
lines changed

src/puppetlabs/puppetdb/query_eng.clj

Lines changed: 54 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,44 @@
102102
:when (:queryable? projection-value)]
103103
(keyword projection-key))))
104104

105-
(defn maybe-log-sql [options context gen-sql]
106-
(if-let [log-id (:log-id context)]
107-
(let [{[query & params] :results-query :as sql} (gen-sql)]
108-
(log/infof "PDBQuery:%s:%s"
109-
log-id (-> (sorted-map :origin (:origin options)
110-
:sql query
111-
:params (vec params))
112-
json/generate-string))
113-
sql)
114-
(gen-sql)))
105+
(defn- regular-query->sql
106+
[query entity options]
107+
(let [query-rec (cond
108+
;; Move this so that we always look for
109+
;; include_facts_expiration (PDB-4586).
110+
(and (:include_facts_expiration options)
111+
(= entity :nodes))
112+
(get-in @entity-fn-idx [:nodes-with-fact-expiration :rec])
113+
114+
(and (:include_package_inventory options)
115+
(= entity :factsets))
116+
(get-in @entity-fn-idx [:factsets-with-packages :rec])
117+
118+
:else
119+
(get-in @entity-fn-idx [entity :rec]))
120+
columns (orderable-columns query-rec)]
121+
(paging/validate-order-by! columns options)
122+
(if (:add-agent-report-filter options)
123+
(let [ast (try
124+
(dr/maybe-add-agent-report-filter-to-query query-rec query)
125+
(catch ExceptionInfo e
126+
(let [data (ex-data e)
127+
msg (.getMessage e)]
128+
(when (not= ::dr/unrecognized-ast-syntax (:kind data))
129+
(log/error e (trs "Unknown exception when processing ast to add report type filter(s)."))
130+
(throw e))
131+
(log/error e msg)
132+
::failed))
133+
(catch Exception e
134+
(log/error e (trs "Unknown exception when processing ast to add report type filter(s)."))
135+
(throw e)))]
136+
(if (= ast ::failed)
137+
(throw (ex-info "AST validation failed, but was successfully converted to SQL. Please file a PuppetDB ticket at https://tickets.puppetlabs.com"
138+
{:kind ::dr/unrecognized-ast-syntax
139+
:ast query
140+
:sql (eng/compile-user-query->sql query-rec query options)}))
141+
(eng/compile-user-query->sql query-rec ast options)))
142+
(eng/compile-user-query->sql query-rec query options))))
115143

116144
(defn query->sql
117145
"Converts a vector-structured `query` to a corresponding SQL query which will
@@ -124,58 +152,25 @@
124152
(jdbc/valid-jdbc-query? (:results-query %))
125153
(or (not (:include_total options))
126154
(jdbc/valid-jdbc-query? (:count-query %)))]}
155+
(let [result (cond
156+
(= :aggregate-event-counts entity)
157+
(aggregate-event-counts/query->sql version query options)
127158

128-
(maybe-log-sql
129-
options
130-
context
131-
(fn []
132-
(cond
133-
(= :aggregate-event-counts entity)
134-
(aggregate-event-counts/query->sql version query options)
135-
136-
(= :event-counts entity)
137-
(event-counts/query->sql version query options)
138-
139-
(and (= :events entity) (:distinct_resources options))
140-
(events/legacy-query->sql false version query options)
141-
142-
:else
143-
(let [query-rec (cond
144-
;; Move this so that we always look for
145-
;; include_facts_expiration (PDB-4586).
146-
(and (:include_facts_expiration options)
147-
(= entity :nodes))
148-
(get-in @entity-fn-idx [:nodes-with-fact-expiration :rec])
159+
(= :event-counts entity)
160+
(event-counts/query->sql version query options)
149161

150-
(and (:include_package_inventory options)
151-
(= entity :factsets))
152-
(get-in @entity-fn-idx [:factsets-with-packages :rec])
162+
(and (= :events entity) (:distinct_resources options))
163+
(events/legacy-query->sql false version query options)
153164

154-
:else
155-
(get-in @entity-fn-idx [entity :rec]))
156-
columns (orderable-columns query-rec)]
157-
(paging/validate-order-by! columns options)
158-
(if (:add-agent-report-filter options)
159-
(let [ast (try
160-
(dr/maybe-add-agent-report-filter-to-query query-rec query)
161-
(catch ExceptionInfo e
162-
(let [data (ex-data e)
163-
msg (.getMessage e)]
164-
(when (not= ::dr/unrecognized-ast-syntax (:kind data))
165-
(log/error e (trs "Unknown exception when processing ast to add report type filter(s)."))
166-
(throw e))
167-
(log/error e msg)
168-
::failed))
169-
(catch Exception e
170-
(log/error e (trs "Unknown exception when processing ast to add report type filter(s)."))
171-
(throw e)))]
172-
(if (= ast ::failed)
173-
(throw (ex-info "AST validation failed, but was successfully converted to SQL. Please file a PuppetDB ticket at https://tickets.puppetlabs.com"
174-
{:kind ::dr/unrecognized-ast-syntax
175-
:ast query
176-
:sql (eng/compile-user-query->sql query-rec query options)}))
177-
(eng/compile-user-query->sql query-rec ast options)))
178-
(eng/compile-user-query->sql query-rec query options))))))))
165+
:else (regular-query->sql query entity options))]
166+
(when-let [log-id (:log-id context)]
167+
(let [{[sql & params] :results-query} result]
168+
(log/infof "PDBQuery:%s:%s"
169+
log-id (-> (sorted-map :origin (:origin options)
170+
:sql sql
171+
:params (vec params))
172+
json/generate-string))))
173+
result)))
179174

180175
(defn get-munge-fn
181176
[entity version paging-options url-prefix]

0 commit comments

Comments
 (0)