Skip to content

Commit

Permalink
Skip conversion of contexts into vectors (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
crisptrutski authored Aug 22, 2024
1 parent d95f197 commit 4fa033b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
18 changes: 8 additions & 10 deletions src/macaw/collect.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
([key-name xf]
(fn item-conjer [results component context]
(update results key-name conj {:component (xf component)
:context (mapv
(fn [^AstWalker$Scope x]
[(keyword (.getType x)) (.getLabel x) (.getId x)])
context)}))))
:context context}))))

(defn- query->raw-components
[^Statement parsed-ast]
Expand Down Expand Up @@ -78,7 +75,7 @@
relax-case (when-not (and quotes-preserve-case? quoted)
(setting->relax-case opts))]
(cond-> s
quoted strip-quotes
quoted strip-quotes
relax-case relax-case)))))

(defn- find-table [{:keys [alias->table name->table keep-internal-tables?] :as opts} ^Table t]
Expand Down Expand Up @@ -124,22 +121,23 @@
(when (and (= (count name->table) 1) (not (alias? (.getColumnName c))))
(:component (val (first name->table))))))

(defn- scope-type [^AstWalker$Scope s] (keyword (.getType s)))

(defn- make-column [aliases opts ^Column c ctx]
(let [{:keys [schema table]} (maybe-column-table aliases opts c)]
(u/strip-nils
{:schema schema
:table table
:column (normalize-reference (.getColumnName c) opts)
:alias (let [[k y] (first ctx)]
(when (= k :alias) y))
:alias (when-let [s (first ctx)]
(when (= :alias (scope-type s))
(.getLabel ^AstWalker$Scope s)))
:instances (when (:with-instance opts) [c])})))

;;; get them together

(defn- only-query-context [ctx]
(into [] (comp (filter #(= (first %) :query))
(map (comp vec rest)))
ctx))
(filter #(= (scope-type %) :query) ctx))

(def ^:private strip-non-query-contexts
(map #(update % :context only-query-context)))
Expand Down
11 changes: 11 additions & 0 deletions src/macaw/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[macaw.collect :as collect]
[macaw.rewrite :as rewrite])
(:import
(com.metabase.macaw AstWalker$Scope)
(java.util.function Consumer)
(net.sf.jsqlparser.parser CCJSqlParser CCJSqlParserUtil)
(net.sf.jsqlparser.parser.feature Feature)))
Expand Down Expand Up @@ -54,6 +55,16 @@
(escape-keywords (:non-reserved-words opts))
(CCJSqlParserUtil/parse (->parser-fn opts))))

(defn scope-id
"A unique identifier for the given scope."
[^AstWalker$Scope s]
(.getId s))

(defn scope-label
"The type of scope we're talking about e.g., a top-level SELECT."
[^AstWalker$Scope s]
(.getLabel s))

(defn query->components
"Given a parsed query (i.e., a [subclass of] `Statement`) return a map with the elements found within it.
Expand Down
7 changes: 5 additions & 2 deletions test/macaw/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,20 @@
(walk/prewalk
(fn [x]
(if (:context x)
(update x :context (partial mapv first))
(update x :context (partial mapv m/scope-label))
x))
m))

(defn scope->vec [s]
[(m/scope-label s) (m/scope-id s)])

(defn contexts->scopes
"Replace full context stack with a reference to the local scope, only."
[m]
(walk/prewalk
(fn [x]
(if-let [context (:context x)]
(-> x (dissoc :context) (assoc :scope (first context)))
(-> x (dissoc :context) (assoc :scope (scope->vec (first context))))
x))
m))

Expand Down

0 comments on commit 4fa033b

Please sign in to comment.