Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Metabase
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Engineering Digital Service
Metabase
Commits
468bed10
Unverified
Commit
468bed10
authored
8 months ago
by
Chris Truter
Committed by
GitHub
8 months ago
Browse files
Options
Downloads
Patches
Plain Diff
Strip model cache tables from query analysis (#47063)
parent
a6116b85
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/metabase/query_analysis/native_query_analyzer.clj
+7
-2
7 additions, 2 deletions
src/metabase/query_analysis/native_query_analyzer.clj
test/metabase/query_analysis_test.clj
+29
-1
29 additions, 1 deletion
test/metabase/query_analysis_test.clj
with
36 additions
and
3 deletions
src/metabase/query_analysis/native_query_analyzer.clj
+
7
−
2
View file @
468bed10
...
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
test/metabase/query_analysis_test.clj
+
29
−
1
View file @
468bed10
...
...
@@ -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"
})}]
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment