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
bf8b88b0
Commit
bf8b88b0
authored
9 years ago
by
Cam Saul
Browse files
Options
Downloads
Patches
Plain Diff
fix ordering by aggregate fields for mongo
parent
702d05e2
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/metabase/driver/query_processor/expand.clj
+35
-24
35 additions, 24 deletions
src/metabase/driver/query_processor/expand.clj
test/metabase/driver/query_processor_test.clj
+23
-0
23 additions, 0 deletions
test/metabase/driver/query_processor_test.clj
with
58 additions
and
24 deletions
src/metabase/driver/query_processor/expand.clj
+
35
−
24
View file @
bf8b88b0
...
...
@@ -83,11 +83,25 @@
;; ## -------------------- Expansion - Impl --------------------
(
def
^
:private
^
:dynamic
*driver*
nil
)
(
def
^
:private
^
:dynamic
*field-ids*
"Bound to an atom containing a set of `Field` IDs referenced in the query being expanded."
nil
)
(
def
^
:private
^
:dynamic
*original-query-dict*
"The entire original Query dict being expanded."
nil
)
(
def
^
:private
^
:dynamic
*fk-field-ids*
"Bound to an atom containing a set of Foreign Key `Field` IDs (on the `source-table`) that we should use for joining to additional `Tables`."
nil
)
(
def
^
:private
^
:dynamic
*table-ids*
"Bound to an atom containing a set of `Table` IDs referenced by `Fields` in the query being expanded."
nil
)
(
defn-
assert-driver-supports
[
^
Keyword
feature
]
{
:pre
[
*
driver
*
]}
(
i/assert-driver-supports
*
driver
*
feature
))
{
:pre
[
(
:
driver
*original-query-dict*
)
]}
(
i/assert-driver-supports
(
:
driver
*original-query-dict*
)
feature
))
(
defn-
non-empty-clause?
[
clause
]
(
and
clause
...
...
@@ -106,18 +120,6 @@
:source_table
:source-table
})
(
m/filter-vals
non-empty-clause?
<>
))))
(
def
^
:private
^
:dynamic
*field-ids*
"Bound to an atom containing a set of `Field` IDs referenced in the query being expanded."
nil
)
(
def
^
:private
^
:dynamic
*fk-field-ids*
"Bound to an atom containing a set of Foreign Key `Field` IDs (on the `source-table`) that we should use for joining to additional `Tables`."
nil
)
(
def
^
:private
^
:dynamic
*table-ids*
"Bound to an atom containing a set of `Table` IDs referenced by `Fields` in the query being expanded."
nil
)
(
defn
rename-mb-field-keys
"Rename the keys in a Metabase `Field` to match the format of those in Query Expander `Fields`."
[
field
]
...
...
@@ -205,11 +207,11 @@
(
defn
expand
"Expand a QUERY-DICT."
[
{
:keys
[
driver
]
,
:as
query-dict
}
]
(
binding
[
*
d
ri
ver*
driver
*field-ids*
(
atom
#
{})
*fk-field-ids*
(
atom
#
{})
*table-ids*
(
atom
#
{})]
[
query-dict
]
(
binding
[
*
o
ri
ginal-query-dict*
query-dict
*field-ids*
(
atom
#
{})
*fk-field-ids*
(
atom
#
{})
*table-ids*
(
atom
#
{})]
(
some->
query-dict
parse
(
resolve-fields
@
*field-ids*
)
...
...
@@ -441,8 +443,15 @@
;; ## -------------------- Order-By --------------------
(
defrecord
OrderByAggregateField
[
^
Keyword
source
; e.g. :aggregation
^
Integer
index
])
; e.g. 0
(
defrecord
OrderByAggregateField
[
^
Keyword
source
; Name used in original query. Always :aggregation for right now
^
Integer
index
; e.g. 0
^
Aggregation
aggregation
]
; The aggregation clause being referred to
IField
(
qualified-name-components
[
_
]
;; Return something like [nil "count"]
;; nil is used where Table name would normally go
[
nil
(
name
(
:aggregation-type
aggregation
))]))
(
defrecord
OrderBySubclause
[
^
Field
field
; or aggregate Field?
^
Keyword
direction
])
; either :ascending or :descending
...
...
@@ -453,8 +462,10 @@
"descending"
:descending
))
(
defparser
parse-order-by-subclause
[[
"aggregation"
index
]
direction
]
(
->OrderBySubclause
(
->OrderByAggregateField
:aggregation
index
)
(
parse-order-by-direction
direction
))
[[
"aggregation"
index
]
direction
]
(
let
[{{
:keys
[
aggregation
]}
:query
}
*original-query-dict*
]
(
assert
aggregation
"Query does not contain an aggregation clause."
)
(
->OrderBySubclause
(
->OrderByAggregateField
:aggregation
index
(
parse-aggregation
aggregation
))
(
parse-order-by-direction
direction
)))
[(
field-id
:guard
Field?
)
direction
]
(
->OrderBySubclause
(
ph
field-id
)
(
parse-order-by-direction
direction
)))
(
defparser
parse-order-by
...
...
This diff is collapsed.
Click to expand it.
test/metabase/driver/query_processor_test.clj
+
23
−
0
View file @
bf8b88b0
...
...
@@ -1073,3 +1073,26 @@
order
id
fields
venue...name
lim
10
))
;;; Nested Field w/ ordering by aggregation
(
expect
[[
"jane"
4
]
[
"kyle"
5
]
[
"tupac"
5
]
[
"jessica"
6
]
[
"bob"
7
]
[
"lucky_pigeon"
7
]
[
"joe"
8
]
[
"mandy"
8
]
[
"amy"
9
]
[
"biggie"
9
]
[
"sameer"
9
]
[
"cam_saul"
10
]
[
"rasta_toucan"
13
]
[
nil
400
]]
(
Q
run
against
geographical-tips
using
mongo
return
:data
:rows
aggregate
count
of
tips
breakout
source...mayor
order
ag.0
))
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