-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
MongoDB contrib : Next replaced with for loop to avoid propagation of StopIteration #2221
Conversation
…void inaccuracy with sharded cluster
… mongodb count target.
@kritchie, thanks for your PR! By analyzing the history of the files in this pull request, we identified @MathiasDesch to be a potential reviewer. |
Anything on this ? |
@MathiasDesch As another MongoDB contributor, could you take a look here? |
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.
I don't use MongoDB, but based on description this makes sense to me.
One comment about unnecessary (but not incorrect) final return statement.
return next(self.get_collection().aggregate([{'$group': {'_id': None, 'count': {'$sum': 1}}}])).get('count') | ||
for res in self.get_collection().aggregate([{'$group': {'_id': None, 'count': {'$sum': 1}}}]): | ||
return res.get('count', None) | ||
return None |
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.
Not sure this last line is necessary. If the for
loop isn't entered, the method by default return None
.
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.
Yes that's true, I just wanted to be as explicit as possible and since we use the None value for comparison in the exist() method I though it made sense to add that line but I could be wrong.
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.
Cool. Yeah, that's fine. Just commenting on it; not saying it was wrong.
Description
Replaced next with for loop in MongoDB count target.
Also fixed comment in the function's documentation.
Motivation and Context
A specific usage caused a StopIteration to be propagated which cause the "output" to be always true even if the exist condition was not met in the MongoDB count target.
This line in the complete method always return true if a StopIteration is raised in the exist method :
return all(map(lambda output: output.exists(), outputs))
Because the all method of python returns True in case of an empty array.
(Maybe this is something that could also be adjusted? Just wondering)
Have you tested this? If so, how?
Using the following code, I've tested multiple runs with conditions :
In all of the above, the pipeline worked and produced the desired result.
(Running when the count doesn't match or the collection doesn't exist, and not running if the count matches)
Following this, here's a simple example of what was happening :
This will always result with :