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
c720218e
Commit
c720218e
authored
9 years ago
by
Cam Saul
Browse files
Options
Downloads
Patches
Plain Diff
change cum_sum implementation
parent
a6912918
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/query_processor.clj
+9
-32
9 additions, 32 deletions
src/metabase/driver/query_processor.clj
with
9 additions
and
32 deletions
src/metabase/driver/query_processor.clj
+
9
−
32
View file @
c720218e
...
...
@@ -108,10 +108,8 @@
;; Store the cumulative sum field under the key :cum_sum so we know which one to sum later
(
and
cum-sum?
has-breakout?
)
(
->
query
(
dissoc
:breakout
)
(
assoc
:cum_sum
ag-field
:aggregation
[
"rows"
]
:fields
(
distinct
(
concat
breakout-fields
[
ag-field
]))))
:aggregation
[
"sum"
ag-field
]))
;; Cumulative sum without any breakout fields should just be treated the same way as "sum". Rewrite query as such
cum-sum?
(
assoc
query
...
...
@@ -129,14 +127,15 @@
"Cumulative sum the values of the aggregate `Field` in RESULTS."
{
:arglists
'
([
query
results
])}
[{
cum-sum-field
:cum_sum,
:as
query
}
{
rows
:rows,
cols
:cols,
:as
results
}]
(
println
(
colorize.core/magenta
(
with-out-str
(
clojure.pprint/pprint
results
))))
(
if-not
cum-sum-field
results
(
let
[
;; Determine the index of the field we need to cumulative sum
cum-sum-field-index
(
->>
cols
(
map-indexed
(
fn
[
i
{
field-
id
:id
}]
(
when
(
=
field-
id
cum-sum-field
)
i
)))
(
filter
identity
)
first
)
(
map-indexed
(
fn
[
i
{
field-
name
:name
}]
(
when
(
=
field-
name
"sum"
)
i
)))
(
filter
identity
)
first
)
_
(
assert
(
integer?
cum-sum-field-index
))
;; Now make a sequence of cumulative sum values for each row
values
(
->>
rows
...
...
@@ -145,30 +144,8 @@
;; Update the values in each row
rows
(
map
(
fn
[
row
value
]
(
assoc
(
vec
row
)
cum-sum-field-index
value
))
rows
values
)
;; We only want to return a single row for each value of the breakout columns.
;; e.g.
;; 2014-04-03 3 2014-04-03 8 ; only return one row for 2014-04-03
;; 2014-04-03 5 ---> 2014-04-04 18
;; 2014-04-04 10
;;
;; We'll reverse the sequence of rows, then filter out all rows whose breakout field values were the same as the last.
;; Then we'll reverse the sequence again to restore the original order.
remove-cum-sum-value
(
fn
[
row
]
(
concat
(
subvec
row
0
cum-sum-field-index
)
(
subvec
row
(
inc
cum-sum-field-index
)
(
count
row
))))
previous-row-breakout-field-values
(
atom
nil
)
same-breakout-field-values-as-previous-row?
(
fn
[
row
]
(
let
[
breakout-values
(
remove-cum-sum-value
row
)
previous-values
@
previous-row-breakout-field-values
]
(
reset!
previous-row-breakout-field-values
breakout-values
)
(
and
(
not
(
empty?
breakout-values
))
(
=
breakout-values
previous-values
))))]
(
->>
rows
reverse
(
filter
(
complement
same-breakout-field-values-as-previous-row?
))
reverse
(
assoc
results
:rows
)))))
rows
values
)]
(
assoc
results
:rows
rows
))))
;; ### ADD-ROW-COUNT-AND-STATUS
...
...
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