-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adjust distinct aggregation expansion on Mongo #44442
Conversation
|
(def ^:private $addFields | ||
:$addFields) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Operators are defined as strings. This removal produces consistent compilation results in modified test, ie. also $addFields
is string in compiled query.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good stuff, just some Clojure code polishing.
If there is a need for more _pre-post_ transformations (as per results of [[expand-aggregations]]), this function | ||
should be generalized." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not clear what this means. I'd either turn this into a TODO
comment that calls out a specific weakness to be fixed later, or just remove this sentence.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By pre-post transformations
I mean transformations that are necessary on :post
key of expand-aggregations
return value. Even if meaning of the comment was evident, its added value is questionable. Removed.
aggr-expr' (walk/postwalk (fn [x] | ||
(if (and (string? x) | ||
(distinct-vals x)) | ||
{$size x} | ||
x)) | ||
aggr-expr)] | ||
[aggr-expr' mappings])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for an extra let
binding here; this can be inlined into the output clause.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In context of the following function, expand-aggregations
, I've deliberately decided to have an explicit var aggr-expr'
, to signal on the first sight that returned value has a same shape as what's used in the other function. I've also decided to bind the distinct-keys
var, despite it could have been omitted by moving filter into transducer composition on the next line. I, subjectively, find this form easier on eyes of future reader.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But as I'm not the only future reader I did as suggested :)
modules/drivers/mongo/src/metabase/driver/mongo/query_processor.clj
Outdated
Show resolved
Hide resolved
(deftest ^:parallel multiple-aggregations-with-distinct-count-expression-test | ||
(mt/test-driver | ||
:mongo | ||
(testing "Should generate correct queries for `:distinct` in expressions ()" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing the bug number?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks.
…r.clj Co-authored-by: Braden Shepherdson <braden@metabase.com>
This commit adds `$size` operator wrapping to `$addToSet`. That enables use of `:distinct` clause in expressions on Mongo.
@metabase-bot backport release-x.49.x |
This commit adds `$size` operator wrapping to `$addToSet`. That enables use of `:distinct` clause in expressions on Mongo.
🚀 This should also be released by v0.50.7 |
Closes #35425
Description
This PR modifies expressions that are used in
$addFields
stage in aggregation transformation. Specifically, names that are result of:distinct
aggregation are wrapped in$size
to produce actual counts.How to verify
Checklist