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
bc33f40f
Commit
bc33f40f
authored
9 years ago
by
Cam Saul
Browse files
Options
Downloads
Patches
Plain Diff
Cast values that filter against mongo _id columns to bson.ObjectId when applicable
parent
ee8b084a
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/metabase/driver/mongo/query_processor.clj
+29
-20
29 additions, 20 deletions
src/metabase/driver/mongo/query_processor.clj
with
29 additions
and
20 deletions
src/metabase/driver/mongo/query_processor.clj
+
29
−
20
View file @
bc33f40f
...
...
@@ -19,7 +19,8 @@
[
metabase.util
:as
u
])
(
:import
(
com.mongodb
CommandResult
DBApiLayer
)
(
clojure.lang
PersistentArrayMap
)))
(
clojure.lang
PersistentArrayMap
)
(
org.bson.types
ObjectId
)))
(
declare
apply-clause
annotate-native-results
...
...
@@ -300,17 +301,25 @@
(
defclause
:fields
field-ids
`
[(
fields
~
(
mapv
field-id->kw
field-ids
))])
(
def
^
:private
field-id-is-date-field?
(
memoize
(
fn
[
field-id
]
(
contains?
#
{
:DateField
:DateTimeField
}
(
sel
:one
:field
[
Field
:base_type
]
:id
field-id
)))))
(
defn-
parse-date-if-needed
"Dates come back from the frontend as `YYYY-MM-DD` strings, so convert them to `java.util.Date` if needed."
[
field-id
value
]
(
if
(
field-id-is-date-field?
field-id
)
(
u/parse-date-yyyy-mm-dd
value
)
value
))
(
def
^
:private
field-id->casting-fn
"Return a fn that should be used to cast values that match/filter against `Field` with FIELD-ID."
(
let
[
->ObjectId
(
fn
[
^
String
value
]
`
(
ObjectId.
~
value
))]
(
memoize
(
fn
[
field-id
]
(
let
[{
base-type
:base_type,
field-name
:name
}
(
sel
:one
[
Field
:base_type
:name
]
:id
field-id
)]
(
cond
(
contains?
#
{
:DateField
:DateTimeField
}
base-type
)
u/parse-date-yyyy-mm-dd
(
and
(
=
field-name
"_id"
)
(
=
base-type
:UnknownField
))
->ObjectId
:else
identity
))))))
(
defn-
cast-value-if-needed
"* Convert dates (which come back as `YYYY-MM-DD` strings) to `java.util.Date`
* Convert ID strings to `ObjectId`
* Return other values as-is"
[
field-id
^
String
value
]
((
field-id->casting-fn
field-id
)
value
))
;; ### filter
;; !!! SPECIAL CASE - the results of this clause are bound to *constraints*, which is used differently
...
...
@@ -328,26 +337,26 @@
{(
field-id->kw
field-id
)
{
$exists
true
}})
(
defclause
:filter
[
"BETWEEN"
field-id
min
max
]
{(
field-id->kw
field-id
)
{
$gte
(
parse-dat
e-if-needed
field-id
min
)
$lte
(
parse-dat
e-if-needed
field-id
max
)}})
{(
field-id->kw
field-id
)
{
$gte
(
cast-valu
e-if-needed
field-id
min
)
$lte
(
cast-valu
e-if-needed
field-id
max
)}})
(
defclause
:filter
[
"="
field-id
value
]
{(
field-id->kw
field-id
)
(
parse-dat
e-if-needed
field-id
value
)})
{(
field-id->kw
field-id
)
(
cast-valu
e-if-needed
field-id
value
)})
(
defclause
:filter
[
"!="
field-id
value
]
{(
field-id->kw
field-id
)
{
$ne
(
parse-dat
e-if-needed
field-id
value
)}})
{(
field-id->kw
field-id
)
{
$ne
(
cast-valu
e-if-needed
field-id
value
)}})
(
defclause
:filter
[
"<"
field-id
value
]
{(
field-id->kw
field-id
)
{
$lt
(
parse-dat
e-if-needed
field-id
value
)}})
{(
field-id->kw
field-id
)
{
$lt
(
cast-valu
e-if-needed
field-id
value
)}})
(
defclause
:filter
[
">"
field-id
value
]
{(
field-id->kw
field-id
)
{
$gt
(
parse-dat
e-if-needed
field-id
value
)}})
{(
field-id->kw
field-id
)
{
$gt
(
cast-valu
e-if-needed
field-id
value
)}})
(
defclause
:filter
[
"<="
field-id
value
]
{(
field-id->kw
field-id
)
{
$lte
(
parse-dat
e-if-needed
field-id
value
)}})
{(
field-id->kw
field-id
)
{
$lte
(
cast-valu
e-if-needed
field-id
value
)}})
(
defclause
:filter
[
">="
field-id
value
]
{(
field-id->kw
field-id
)
{
$gte
(
parse-dat
e-if-needed
field-id
value
)}})
{(
field-id->kw
field-id
)
{
$gte
(
cast-valu
e-if-needed
field-id
value
)}})
(
defclause
:filter
[
"AND"
&
subclauses
]
{
$and
(
mapv
#
(
apply-clause
[
:filter
%
])
subclauses
)})
...
...
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