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
aa3f1b57
Commit
aa3f1b57
authored
9 years ago
by
Cam Saul
Browse files
Options
Downloads
Patches
Plain Diff
add :table-name to expanded Fields
parent
6700e8e5
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/metabase/driver/query_processor/expand.clj
+37
-13
37 additions, 13 deletions
src/metabase/driver/query_processor/expand.clj
with
37 additions
and
13 deletions
src/metabase/driver/query_processor/expand.clj
+
37
−
13
View file @
aa3f1b57
...
...
@@ -42,7 +42,8 @@
[
medley.core
:as
m
]
[
swiss.arrows
:refer
[
-<>
]]
[
metabase.db
:refer
[
sel
]]
[
metabase.models.field
:as
field
]
(
metabase.models
[
field
:as
field
]
[
table
:refer
[
Table
]])
[
metabase.util
:as
u
])
(
:import
(
clojure.lang
Keyword
)))
...
...
@@ -55,19 +56,24 @@
;; ## -------------------- Protocols --------------------
(
defprotocol
IResolve
Field
"Methods called during `Field` resolution. Placeholder types should implement this protocol."
(
defprotocol
IResolve
"Methods called during `Field`
and `Table`
resolution. Placeholder types should implement this protocol."
(
resolve-field
[
this
field-id->fields
]
"This method is called when walking the Query after fetching `Fields`.
Placeholder objects should lookup the relevant Field in FIELD-ID->FIELDS and
return their expanded form. Other objects should just return themselves."
))
return their expanded form. Other objects should just return themselves."
)
(
resolve-table
[
this
table-id->tables
]
"Called when walking the Query after `Fields` have been resolved and `Tables` have been fetched.
Objects like `Fields` can add relevant information like the name of their `Table`."
))
;; Default impls are just identity
(
extend
Object
IResolveField
{
:resolve-field
(
fn
[
this
_
]
this
)})
IResolve
{
:resolve-field
(
fn
[
this
_
]
this
)
:resolve-table
(
fn
[
this
_
]
this
)})
(
extend
nil
IResolveField
{
:resolve-field
(
constantly
nil
)})
IResolve
{
:resolve-field
(
constantly
nil
)
:resolve-table
(
constantly
nil
)})
;; ## -------------------- Expansion - Impl --------------------
...
...
@@ -79,7 +85,8 @@
:fields
(
parse-fields
(
:fields
%
))
:filter
(
parse-filter
(
:filter
%
))
:order_by
(
parse-order-by
(
:order_by
%
)))
(
set/rename-keys
<>
{
:order_by
:order-by
})
(
set/rename-keys
<>
{
:order_by
:order-by
:source_table
:source-table
})
(
m/filter-vals
identity
<>
))))
(
def
^
:private
^
:dynamic
*field-ids*
...
...
@@ -90,15 +97,25 @@
"Resolve the `Fields` in an EXPANDED-QUERY-DICT."
[
expanded-query-dict
field-ids
]
(
if-not
(
seq
field-ids
)
expanded-query-dict
; No need to do a DB call or walk expanded-query-dict if we didn't see any Field IDs
(
let
[
fields
(
->>
(
sel
:many
:id->fields
[
field/Field
:name
:base_type
:special_type
]
:id
[
in
field-ids
])
(
let
[
fields
(
->>
(
sel
:many
:id->fields
[
field/Field
:name
:base_type
:special_type
:table_id
]
:id
[
in
field-ids
])
(
m/map-vals
#
(
set/rename-keys
%
{
:id
:field-id
:name
:field-name
:special_type
:special-type
:base_type
:base-type
})))]
:base_type
:base-type
:table_id
:table-id
})))]
;; This is performed depth-first so we don't end up walking the newly-created Field/Value objects
;; they may have nil values; this was we don't have to write an implementation of resolve-field for nil
(
walk/postwalk
#
(
resolve-field
%
fields
)
expanded-query-dict
))))
(
defn-
resolve-tables
"Resolve the `Tables` in an EXPANDED-QUERY-DICT`."
[{{
source-table-id
:source-table
}
:query,
:as
expanded-query-dict
}]
;; TODO - do we need any other information about a Table besides its name and ID?
;; TODO - this doesn't handle join tables yet
(
let
[
table
(
sel
:one
:fields
[
Table
:name
:id
]
:id
source-table-id
)]
(
->>
(
assoc-in
expanded-query-dict
[
:query
:source-table
]
table
)
(
walk/postwalk
#
(
resolve-table
%
{(
:id
table
)
table
})))))
;; ## -------------------- Public Interface --------------------
...
...
@@ -108,7 +125,8 @@
(
binding
[
*field-ids*
(
atom
#
{})]
(
some->
query-dict
parse
(
resolve-fields
@
*field-ids*
))))
(
resolve-fields
@
*field-ids*
)
resolve-tables
)))
;; ## -------------------- Field + Value --------------------
...
...
@@ -117,7 +135,13 @@
(
defrecord
Field
[
field-id
field-name
base-type
special-type
])
special-type
table-id
table-name
]
IResolve
(
resolve-table
[
this
table-id->table
]
(
cond->
this
table-id
(
assoc
:table-name
(
:name
(
table-id->table
table-id
))))))
;; Value is the expansion of a value within a QL clause
;; Information about the associated Field is included for convenience
...
...
@@ -133,7 +157,7 @@
;; Replace Field IDs with these during first pass
(
defrecord
FieldPlaceholder
[
field-id
]
IResolve
Field
IResolve
(
resolve-field
[
this
field-id->fields
]
(
->
(
:field-id
this
)
field-id->fields
...
...
@@ -155,7 +179,7 @@
;; Replace values with these during first pass over Query.
;; Include associated Field ID so appropriate the info can be found during Field resolution
(
defrecord
ValuePlaceholder
[
field-id
value
]
IResolve
Field
IResolve
(
resolve-field
[
this
field-id->fields
]
(
->
(
:field-id
this
)
field-id->fields
...
...
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