From ba695c59c18f10e4a10d01c1f4908e24cc24fed5 Mon Sep 17 00:00:00 2001 From: Chris Truter Date: Mon, 2 Sep 2024 09:56:13 +0200 Subject: [PATCH] Suppress columns coming from unknown tables (#97) * Add failing test for phantom field from generated table * Suppress columns from unknown tables --- src/macaw/collect.clj | 11 +++++++++++ .../resources/acceptance/generate_series.analysis.edn | 2 ++ test/resources/acceptance/generate_series.sql | 2 ++ 3 files changed, 15 insertions(+) create mode 100644 test/resources/acceptance/generate_series.analysis.edn create mode 100644 test/resources/acceptance/generate_series.sql diff --git a/src/macaw/collect.clj b/src/macaw/collect.clj index 81ef043..fcb2db7 100644 --- a/src/macaw/collect.clj +++ b/src/macaw/collect.clj @@ -170,6 +170,16 @@ :column)) unqualified))) +(defn- remove-phantom-table-columns + "If a column reference is qualified by a table that we don't know about, its probably + not a real field. + See the `:generate_series` fixture." + [table-map columns] + (let [known-table-name? (into #{} (map (comp :table :component val)) table-map)] + (filter (fn [{s :schema, t :table}] + (or (nil? t) (some? s) (known-table-name? t))) + columns))) + (defn- infer-table-schema [columns node] (update node :component #(let [{:keys [schema table] :as element} %] @@ -220,6 +230,7 @@ source-columns (->> (map :component all-columns) (remove-redundant-columns alias?) (remove literal?) + (remove-phantom-table-columns table-map) (into #{} (comp (remove (comp pseudo-table-names :table)) (remove :internal?) diff --git a/test/resources/acceptance/generate_series.analysis.edn b/test/resources/acceptance/generate_series.analysis.edn new file mode 100644 index 0000000..973fef9 --- /dev/null +++ b/test/resources/acceptance/generate_series.analysis.edn @@ -0,0 +1,2 @@ +{:tables [] + :source-columns []} diff --git a/test/resources/acceptance/generate_series.sql b/test/resources/acceptance/generate_series.sql new file mode 100644 index 0000000..def9d5c --- /dev/null +++ b/test/resources/acceptance/generate_series.sql @@ -0,0 +1,2 @@ +SELECT t.day::date AS date +FROM generate_series(timestamp '2021-01-01', now(), interval '1 day') AS t(day)