[SERVER-32960] $mod has inconsistent rounding/truncation behavior Created: 29/Jan/18  Updated: 29/Oct/23  Resolved: 19/Nov/20

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: None
Fix Version/s: 4.9.0, 4.0.22, 3.6.22, 4.4.3, 4.2.12

Type: Bug Priority: Major - P3
Reporter: Charlie Swanson Assignee: Nikita Lapkov (Inactive)
Resolution: Fixed Votes: 0
Labels: neweng, query-44-grooming
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Backports
Depends
is depended on by DRIVERS-874 $mod has inconsistent rounding/trunca... Closed
Documented
is documented by DOCS-13855 Investigate changes in SERVER-32960: ... Closed
Duplicate
is duplicated by SERVER-50579 $mod truncates divisor and remainder ... Closed
Problem/Incident
Related
related to SERVER-32961 Add $fmod, or some way to mod by frac... Backlog
is related to DOCS-7792 $mod silently truncates decimal argum... Closed
Backwards Compatibility: Minor Change
Operating System: ALL
Backport Requested:
v4.4, v4.2, v4.0, v3.6
Sprint: Query 2020-11-30
Participants:
Linked BF Score: 44

 Description   

See DOCS-7792 - $mod will truncate a double (e.g. 2.7 -> 2), but will round decimals, ties to even (e.g. 3.5 -> 4).

We should be consistent here, probably truncating decimals as well as doubles.

Also, $mod treats it's divisor and remainder arguments as int values. This could lead to unexpected behaviour when these arguments are larger than int. For example:

> db.createCollection('test')
> db.test.insert([

{a: 4000000000}

])
> db.test.find({a: {$mod: [4000000000, 0]}})

The last query should return the single record from the test collection, but returns nothing instead.

The suggestion is to truncate decimals and doubles consistently towards zero into long long type.



 Comments   
Comment by Githook User [ 19/Nov/20 ]

Author:

{'name': 'Nikita Lapkov', 'email': 'nikita.lapkov@mongodb.com', 'username': 'laplab'}

Message: SERVER-32960: Make $mod truncate float values consistently and use long long for its arguments
Branch: master
https://github.com/mongodb/mongo/commit/0a2bfde4719880480c442d9fd619546522162b8e

Comment by Githook User [ 19/Nov/20 ]

Author:

{'name': 'Nikita Lapkov', 'email': 'nikita.lapkov@mongodb.com', 'username': 'laplab'}

Message: SERVER-32960 Make $mod truncate float values consistently and use long long for its arguments
Branch: v4.2
https://github.com/mongodb/mongo/commit/6f7f808583de3c432458d0adc2413d3a4022c126

Comment by Githook User [ 19/Nov/20 ]

Author:

{'name': 'Nikita Lapkov', 'email': 'nikita.lapkov@mongodb.com', 'username': 'laplab'}

Message: SERVER-32960 Make $mod truncate float values consistently and use long long for its arguments
Branch: v4.0
https://github.com/mongodb/mongo/commit/af44d6124a35aec82a22693cade942c4f9d2f53a

Comment by Githook User [ 19/Nov/20 ]

Author:

{'name': 'Nikita Lapkov', 'email': 'nikita.lapkov@mongodb.com', 'username': 'laplab'}

Message: SERVER-32960 Make $mod truncate float values consistently and use long long for its arguments
Branch: v4.4
https://github.com/mongodb/mongo/commit/841dc94c12b80bafef30b9c189fc59b0ac5a8ff8

Comment by Githook User [ 19/Nov/20 ]

Author:

{'name': 'Nikita Lapkov', 'email': 'nikita.lapkov@mongodb.com', 'username': 'laplab'}

Message: SERVER-32960 Make $mod truncate float values consistently and use long long for its arguments
Branch: v3.6
https://github.com/mongodb/mongo/commit/9f73260db3bfa261b2f02880bcbf6010cbc10ea6

Comment by Githook User [ 01/Sep/20 ]

Author:

{'name': 'Nikita Lapkov', 'email': 'nikita.lapkov@mongodb.com', 'username': 'laplab'}

Message: Revert "SERVER-32960 Make $mod always truncate divisor and remainder toward zero"

This reverts commit cb131974041efdcd0b4a99fe3bd316cbb9b4db41.
Branch: master
https://github.com/mongodb/mongo/commit/fa38abd6d670f9b43dd9771b68f3b1a6441183b0

Comment by Nikita Lapkov (Inactive) [ 31/Aug/20 ]

Will revert commit because of BF-18654

Comment by Githook User [ 27/Aug/20 ]

Author:

{'name': 'Nikita Lapkov', 'email': 'nikita.lapkov@mongodb.com', 'username': 'laplab'}

Message: SERVER-32960 Make $mod always truncate divisor and remainder toward zero
Branch: master
https://github.com/mongodb/mongo/commit/cb131974041efdcd0b4a99fe3bd316cbb9b4db41

Generated at Thu Feb 08 04:31:50 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.