Skip to content
Snippets Groups Projects
Unverified Commit 468bed10 authored by Chris Truter's avatar Chris Truter Committed by GitHub
Browse files

Strip model cache tables from query analysis (#47063)

parent a6116b85
Branches
Tags
No related merge requests found
......@@ -259,6 +259,11 @@
(defn- mark-reference [refs explicit?]
(map #(assoc % :explicit-reference explicit?) refs))
(defn- strip-model-refs [refs]
(remove (fn [{s :schema}]
(and s (str/starts-with? s "metabase_cache_")))
refs))
(defn- references-for-sql
"Returns a `{:explicit #{...} :implicit #{...}}` map with field IDs that (may) be referenced in the given card's
query. Errs on the side of optimism: i.e., it may return fields that are *not* in the query, and is unlikely to fail
......@@ -277,8 +282,8 @@
(set/difference explicit-refs))
field-refs (concat (mark-reference explicit-refs true)
(mark-reference implicit-refs false))]
{:tables table-refs
:fields field-refs}))
{:tables (strip-model-refs table-refs)
:fields (strip-model-refs field-refs)}))
(defn references-for-native
"Returns a `{:explicit #{...} :implicit #{...}}` map with field IDs that (may) be referenced in the given card's
......
......@@ -5,10 +5,12 @@
[metabase.lib.metadata :as lib.metadata]
[metabase.lib.metadata.jvm :as lib.metadata.jvm]
[metabase.models :refer [Card]]
[metabase.models.persisted-info :as persisted-info]
[metabase.public-settings :as public-settings]
[metabase.query-analysis :as query-analysis]
[metabase.test :as mt]
[metabase.util :as u]
[toucan2.core :as t2]
[toucan2.tools.with-temp :as t2.with-temp]))
(deftest native-query-enabled-test
......@@ -35,8 +37,9 @@
(->> refs
;; lowercase names to avoid tests being driver-dependent
(map #(-> %
(u/update-if-exists :schema u/lower-case-en)
(update :table u/lower-case-en)
(update :column u/lower-case-en)))
(u/update-if-exists :column u/lower-case-en)))
(sort-by (juxt :table :column)))))))
(deftest parse-mbql-test
......@@ -74,6 +77,31 @@
:explicit-reference true}]
(:fields (field-id-references mlv2-query)))))))
(deftest parse-native-test
(testing "Parsing Native queries that reference models do not return cache tables"
(mt/with-temp [Card c1 {:type :model
:dataset_query (mt/mbql-query venues
{:aggregation [[:distinct $name]
[:distinct $price]]
:limit 5})}
Card c2 {:dataset_query (let [tag-name (str "#" (:id c1) "-some-card")]
(mt/native-query {:query (format "SELECT * FROM t JOIN {{%s}} ON true" tag-name)
:template-tags {tag-name {:name tag-name
:display-name tag-name
:type "card"
:card-id (:id c1)}}}))}]
;; TODO extract model persistence logic from the task, so that we can use the module API for this
(let [pi (persisted-info/turn-on-model! (t2/select-one-pk :model/User) c1)]
(t2/update! :model/PersistedInfo (:id pi) {:active true
:state "persisted"
:query_hash (persisted-info/query-hash (:dataset_query c1))
:definition (persisted-info/metadata->definition (:result_metadata c1) (:table_name pi))
:state_change_at :%now
:refresh_end :%now}))
(is (= [{:table "t"}]
(:tables (field-id-references c2)))))))
(deftest replace-fields-and-tables!-test
(testing "fields and tables in a native card can be replaced"
(t2.with-temp/with-temp [:model/Card card {:dataset_query (mt/native-query {:query "SELECT TOTAL FROM ORDERS"})}]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment