[SERVER-68396] Regression of $add aggregation expression behavior with Date type Created: 28/Jul/22  Updated: 27/Oct/23  Resolved: 28/Jul/22

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

Type: Bug Priority: Major - P3
Reporter: Mindaugas Malinauskas Assignee: Backlog - Query Execution
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
Problem/Incident
is caused by SERVER-65735 $add operator should not use DoubleDo... Closed
Assigned Teams:
Query Execution
Operating System: ALL
Steps To Reproduce:

On 6.1 (master)

Enterprise test> db.c.insert({})
{
  acknowledged: true,
  insertedIds: { '0': ObjectId("62e2911b078470e351dec540") }
}
Enterprise test> db.c.aggregate(
...   [
...     { $project: { a: { $add: [  NumberDecimal("9.999999999999999999999999999999999E+6144"), ISODate("2019-01-15T06:18:32.992Z"), NumberDecimal("-9.999999999999999999999999999999999E+6144") ] },
..... b: { $add: [  NumberDecimal("9.999999999999999999999999999999999E+60"), ISODate("2019-01-15T06:18:32.992Z"), NumberDecimal("-9.999999999999999999999999999999999E+60") ] },
..... c: { $add: [  NumberDecimal("9.999999999999999999999999999999999E+20"), ISODate("2019-01-15T06:18:32.992Z"), NumberDecimal("-9.999999999999999999999999999999999E+20") ] } } }
...   ]
... )
[
  {
    _id: ObjectId("62e2911b078470e351dec540"),
    a: ISODate("1970-01-01T00:00:00.000Z"),
    b: ISODate("1970-01-01T00:00:00.000Z"),
    c: ISODate("2019-01-15T06:18:32.992Z")
  }
]

On 6.0.

Enterprise test> db.c.insert({})
{
  acknowledged: true,
  insertedIds: { '0': ObjectId("62e29c87078470e351dec541") }
}
Enterprise test> db.c.aggregate( [ { $project: { a: { $add: [NumberDecimal("9.999999999999999999999999999999999E+6144"), ISODate("2019-01-15T06:18:32.992Z"), NumberDecimal("-9.999999999999999999999999999999999E+6144")] }, b: { $add: [NumberDecimal("9.999999999999999999999999999999999E+60"), ISODate("2019-01-15T06:18:32.992Z"), NumberDecimal("-9.999999999999999999999999999999999E+60")] }, c: { $add: [NumberDecimal("9.999999999999999999999999999999999E+20"), ISODate("2019-01-15T06:18:32.992Z"), NumberDecimal("-9.999999999999999999999999999999999E+20")] } } }])
[
  {
    _id: ObjectId("62e29c87078470e351dec541"),
    a: ISODate("2019-01-15T06:18:32.992Z"),
    b: ISODate("2019-01-15T06:18:32.992Z"),
    c: ISODate("2019-01-15T06:18:32.992Z")
  }
]

Participants:
Linked BF Score: 135

 Description   

$add expression with parameter list [a, d, b], where d is a Date type value and a, b - numeric values, the expression evaluates as (a+d)+b. In the previous version it apparently used to evaluate as (a+b)+d. With large absolute values of a and b and due to limited precision of numeric types the results can differ.



 Comments   
Comment by Davis Haupt (Inactive) [ 28/Jul/22 ]

Hi mindaugas.malinauskas@mongodb.com, yes, this is an intended change. Arithmetic in a $add expression should always be evaluated left-to-right, the expression shouldn't rearrange operands before performing addition. BACKPORT-13141 will backport this change to 6.0, which I believe is now last-lts.

Generated at Thu Feb 08 06:10:41 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.