Operational Defect Database

BugZero found this defect 34 days ago.

MongoDB | 2642392

Aggregation is inconsistent in applying $sum vs. $min/$max/$avg for non-existing fields

Last update date:

4/15/2024

Affected products:

MongoDB Server

Affected releases:

No affected releases provided.

Fixed releases:

No fixed releases provided.

Description:

Info

$sum should behave consistently with other grouping operators, such as $avg, when applied against non-existing fields, but instead it manufactures bogus zero values and there is no simple way to work it around. Mongo DB also won't process $cond or $ifNull with enclosed $sum (it really should strip off conditionals to get to the accumulators, but that's another issue), and applying something like $first, with enclosed $cond, with enclosed $sum won't generate errors, but will yield bogus results instead of returning the one and only sum of the result. These aggregations mess up things like charts, where instead of missing data zeros are output, which is misleading.

Top User Comments

eric.sedor commented on Mon, 15 Apr 2024 23:33:56 +0000: I'm marking this as a duplicate of SERVER-30270, which includes some discussion on this matter. cis74633@bell.net, just to clarify: reports made to the MongoDB Feedback Engine are taken in directly by our product teams, so reporting or voting there is the best way to route pain points to their respective feature owners. Gratefully, Eric cis74633@bell.net commented on Mon, 15 Apr 2024 19:18:10 +0000: Well, if the docs describe incorrect behavior, it's still a bug, just a documented one. The only person who can say whether it's a bug or not, however illogical is the behavior, is the Product Owner. I suggest to run this work item by him/her to get some guidance. If you are the Product Owner, then I suppose it is case-closed indeed. JIRAUSER1265262 commented on Mon, 15 Apr 2024 15:47:09 +0000: If you have reason to suspect a bug in the server (especially around the second issue you were pointing out, pertaining to $cond or $ifNull, feel free to make a separate ticket). Thanks again! JIRAUSER1265262 commented on Mon, 15 Apr 2024 15:45:23 +0000: Hi cis74633@bell.net, thanks for your report! Per our docs: If used on a field that contains both numeric and non-numeric values, $sum ignores the non-numeric values and returns the sum of the numeric values. If used on a field that does not exist in any document in the collection, $sum returns 0 for that field. If all operands are non-numeric, $sum returns 0. I understand the desire for this to behave consistently with other aggregations, however, this JIRA project is for bug reports, but you can help get this on the radar by submitting a feature request to our MongoDB Feedback Engine. You may also want to search and post on the MongoDB Developer Community Forums, as it's possible there are others who have guidance on how to satisfy your use-case. Chris

Steps to Reproduce

Insert these documents: db.abc.insertMany([{a: "A", b: 1}, {a: "B"}, {a: "B"}, {a: "A"}]) , and run this pipeline: db.abc.aggregate([{$group: {_id: "$a", sum: {$sum: "$b"}, avg: {$avg: "$b"}, min: {$min: "$b"}, max: {$max: "$b"}} }]) This result is returned: [ { _id: 'B', sum: 0, avg: null, min: null, max: null }, { _id: 'A', sum: 1, avg: 1, min: 1, max: 1 } ] $sum should yield consistent results, compared to its counterparts. Take SQL for example: create table abc (a char, b int NULL); insert into abc values ('A', 1); insert into abc values ('A', NULL); insert into abc values ('B', NULL); insert into abc values ('B', NULL); , which will yield expected results - mixed input ignores NULL and NULL-only input yields NULL: a sum(b) avg(b) min(b) max(b) ----------  ----------  ----------  ----------  ---------- A 1 1.0 1 1 B

Additional Resources / Links

Share:

BugZero® Risk Score

What's this?

Coming soon

Status

Closed

Learn More

Search:

...