Skip to content

Commit

Permalink
more testing
Browse files Browse the repository at this point in the history
  • Loading branch information
crisptrutski committed Oct 21, 2024
1 parent 4526e2b commit f758fa1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
8 changes: 5 additions & 3 deletions src/macaw/scope_experiments.clj
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,13 @@
#{table-name}
(scope->nested-tables scope-id))])))))

(defn- ->vec [x]
(mapv x [:schema :table :column]))

(defn fields-to-search
"Get a set of qualified columns. Where the qualification was uncertain, we enumerate all possibilities"
[f->ts]
(into (sorted-set)
(into (sorted-set-by (fn [x y] (compare (->vec x) (->vec y))))
(mapcat (fn [[[_ column-name] table-names]]
(map #(vector :table % :column column-name) table-names)))

(map #(hash-map :table % :column column-name) table-names)))
f->ts))
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns macaw.scope-experiments
(ns macaw.scope-experiments-test
(:require
[clojure.test :refer :all]
[macaw.scope-experiments :as mse]))
Expand Down Expand Up @@ -57,13 +57,29 @@
;; like source-columns, but understands scope
(is (= (mse/fields-to-search
(mse/fields->tables-in-scope "select x from t, u, v left join w on w.a = v.a where t.b = u.b and u.c = v.c limit 3"))
#{[:table "t" :column "b"]
[:table "t" :column "x"]
[:table "u" :column "b"]
[:table "u" :column "c"]
[:table "u" :column "x"]
[:table "v" :column "a"]
[:table "v" :column "c"]
[:table "v" :column "x"]
[:table "w" :column "a"]
[:table "w" :column "x"]})))
#{{:table "t" :column "b"}
{:table "t" :column "x"}
{:table "u" :column "b"}
{:table "u" :column "c"}
{:table "u" :column "x"}
{:table "v" :column "a"}
{:table "v" :column "c"}
{:table "v" :column "x"}
{:table "w" :column "a"}
{:table "w" :column "x"}}))

(is (= (mse/fields-to-search
(mse/fields->tables-in-scope
"with b as (select x, * from a),
c as (select y, * from b)
select z from c;"))
;; getting there - needs to unwrap cte aliases to the tables that they come from
#{{:table "a" :column "x"}
{:table "b" :column "y"}
{:table "c" :column "z"}}))

(is (= (mse/fields-to-search
(mse/fields->tables-in-scope
"select x, y, (select z from u) from t"))
;; totally loses x and y :-(
#{{:table "u" :column "z"}})))

0 comments on commit f758fa1

Please sign in to comment.