[SERVER-32873] Incorrect handling of NumberLong in $pow function Created: 24/Jan/18  Updated: 30/Oct/23  Resolved: 10/Feb/18

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: 3.7.2

Type: Bug Priority: Minor - P4
Reporter: Patrick Meredith Assignee: Charlie Swanson
Resolution: Fixed Votes: 0
Labels: bkp, neweng, pull-request
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Backports
Backwards Compatibility: Fully Compatible
Operating System: ALL
Backport Requested:
v3.6, v3.4, v3.2
Participants:

 Description   

$pow has issues where a NumberLong base of -1 is used with a NumberLong exponent of a negative odd value.

For example:

MongoDB Enterprise > db.v.aggregate({$project: {'a': { $pow: [ NumberLong(-1), NumberLong(-5) ]  },  'b' : { $pow: [ -1, NumberLong(-3) ]  }   }  })
{ "_id" : ObjectId("5a689c791de68d211f6234d6"), "a" : NumberLong(1), "b" : -1 }

Removing the NumberLong from either argument causes the right answer. It seems to be related purely to the return type being NumberLong, though I can confirm there are no issues with positive odd exponents.



 Comments   
Comment by Githook User [ 10/Feb/18 ]

Author:

{'email': 'KevinCybura@gmail.com', 'name': 'KevinCybura', 'username': 'KevinCybura'}

Message: SERVER-32873 Fix incorrect handling of NumberLong in $pow function

Signed-off-by: Charlie Swanson <charlie.swanson@mongodb.com>

Closes #1211
Branch: master
https://github.com/mongodb/mongo/commit/e6e8dcf826c8e25b53c8368a0d538dfbdeaee589

Comment by Charlie Swanson [ 07/Feb/18 ]

Assigning this to myself since I'm working with Kevin Cybura (a CUNY student, external contributor) to come up with a fix and pull request.

Comment by Charlie Swanson [ 31/Jan/18 ]

Looks like this is a straight-up bug in the implementation. Once we determine that the result of the expression can be represented as a long (which is correct in this case), we assume that both numbers are positive. It seems like there should be a special case for when the exponent is negative. When the exponent is negative, the only way we could have the result be a long is when the base is 0, 1, or -1, in which case computing the answer is relatively straightforward.

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