Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
Better debugging info when expression-with-name fails
Browse files Browse the repository at this point in the history
  • Loading branch information
camsaul committed Jul 19, 2019
1 parent 6519287 commit 432197e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject metabase/mbql "1.3.2"
(defproject metabase/mbql "1.3.3-SNAPSHOT"
:description "Shared things used across several Metabase projects, such as i18n and config."
:url "https://github.com/metabase/mbql"
:min-lein-version "2.5.0"
Expand Down
22 changes: 17 additions & 5 deletions src/metabase/mbql/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,23 @@

(s/defn expression-with-name :- mbql.s/FieldOrExpressionDef
"Return the `Expression` referenced by a given `expression-name`."
[query :- mbql.s/Query, expression-name :- su/NonBlankString]
(or (get-in query, [:query :expressions (keyword expression-name)])
(when-let [source-query (get-in query [:query :source-query])]
(expression-with-name (assoc query :query source-query) expression-name))
(throw (Exception. (str (tru "No expression named ''{0}''" (name expression-name)))))))
[{inner-query :query} :- mbql.s/Query, expression-name :- (s/cond-pre s/Keyword su/NonBlankString)]
(let [allowed-names [(qualified-name expression-name) (keyword expression-name)]]
(loop [{:keys [expressions source-query]} inner-query, found #{}]
(or
;; look for either string or keyword version of `expression-name` in `expressions`
(some (partial get expressions) allowed-names)
;; otherwise, if we have a source query recursively look in that (do we allow that??)
(let [found (into found (keys expressions))]
(if source-query
(recur source-query found)
;; failing that throw an Exception with detailed info about what we tried and what the actual expressions
;; were
(throw (ex-info (str (tru "No expression named ''{0}''" (qualified-name expression-name)))
{:type :invalid-query
:expression-name expression-name
:tried allowed-names
:found found}))))))))


(s/defn aggregation-at-index :- mbql.s/Aggregation
Expand Down

0 comments on commit 432197e

Please sign in to comment.