[SERVER-83272] Fix discrepancy in sorting between classic and SBE Created: 15/Nov/23  Updated: 16/Nov/23  Resolved: 16/Nov/23

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

Type: Bug Priority: Major - P3
Reporter: Foteini Alvanaki Assignee: Foteini Alvanaki
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
Operating System: ALL
Steps To Reproduce:

Create the following three collections

 

db.t01.insertMany(
[
  { _id: 0 },
  { _id: 1 },
  { _id: 2, obj: { _id: 3, obj: {} } },
  { _id: 4 },
  { _id: 5 },
  { _id: 6 }
])

 

db.t02.insertMany(
[
  { _id: 0 },
  { _id: 1 },
  { _id: 2, obj: { _id: 3, date: null, obj: {} } },
  { _id: 4 },
  { _id: 5 },
  { _id: 6 }
])

 

db.t03.insertMany(
[
  { _id: 0 },
  { _id: 1 },
  { _id: 2, obj: { _id: 3, obj: {}, date: null } },
  { _id: 4 },
  { _id: 5 },
  { _id: 6 }
])

 

Run the following queries both in Classic and in SBE

 

db.t01.aggregate([{$project: {"obj.date": new Date("2019-08-05T22:26:32.772Z"), "obj.obj": 1}},{$sort: {obj: -1}}])
db.t02.aggregate([{$project: {"obj.date": new Date("2019-08-05T22:26:32.772Z"), "obj.obj": 1}},{$sort: {obj: -1}}])
db.t03.aggregate([{$project: {"obj.date": new Date("2019-08-05T22:26:32.772Z"), "obj.obj": 1}},{$sort: {obj: -1}}])

 

Results in classic are always the same

[
  { _id: 0, obj: { date: ISODate("2019-08-05T22:26:32.772Z") } },
  { _id: 1, obj: { date: ISODate("2019-08-05T22:26:32.772Z") } },
  { _id: 4, obj: { date: ISODate("2019-08-05T22:26:32.772Z") } },
  { _id: 5, obj: { date: ISODate("2019-08-05T22:26:32.772Z") } },
  { _id: 6, obj: { date: ISODate("2019-08-05T22:26:32.772Z") } },
  {
    _id: 2,
    obj: { obj: {}, date: ISODate("2019-08-05T22:26:32.772Z") }
  }
]

 

Results in SBE for each query are 

t01

[
  { _id: 0, obj: { date: ISODate("2019-08-05T22:26:32.772Z") } },
  { _id: 1, obj: { date: ISODate("2019-08-05T22:26:32.772Z") } },
  { _id: 4, obj: { date: ISODate("2019-08-05T22:26:32.772Z") } },
  { _id: 5, obj: { date: ISODate("2019-08-05T22:26:32.772Z") } },
  { _id: 6, obj: { date: ISODate("2019-08-05T22:26:32.772Z") } },
  {
    _id: 2,
    obj: { obj: {}, date: ISODate("2019-08-05T22:26:32.772Z") }
  }
]

t02

[
  {
    _id: 2,
    obj: { date: ISODate("2019-08-05T22:26:32.772Z"), obj: {} }
  },
  { _id: 0, obj: { date: ISODate("2019-08-05T22:26:32.772Z") } },
  { _id: 1, obj: { date: ISODate("2019-08-05T22:26:32.772Z") } },
  { _id: 4, obj: { date: ISODate("2019-08-05T22:26:32.772Z") } },
  { _id: 5, obj: { date: ISODate("2019-08-05T22:26:32.772Z") } },
  { _id: 6, obj: { date: ISODate("2019-08-05T22:26:32.772Z") } }
]

 

t03

[
  { _id: 0, obj: { date: ISODate("2019-08-05T22:26:32.772Z") } },
  { _id: 1, obj: { date: ISODate("2019-08-05T22:26:32.772Z") } },
  { _id: 4, obj: { date: ISODate("2019-08-05T22:26:32.772Z") } },
  { _id: 5, obj: { date: ISODate("2019-08-05T22:26:32.772Z") } },
  { _id: 6, obj: { date: ISODate("2019-08-05T22:26:32.772Z") } },
  {
    _id: 2,
    obj: { obj: {}, date: ISODate("2019-08-05T22:26:32.772Z") }
  }
]

 

 

From my investigation this happens because in SBE when querying collections t01 and t03 we compare {

{"obj" : {}, "date" : 1565043992772}

 vs 

{"date" : 1565043992772}

but when we query t02 we compare

{"date" : 1565043992772, "obj" : {}}

 vs 

{"date" : 1565043992772}

 

The sort key in classic is in all relations 

{"obj" : {}, "date" : 1565043992772}

Participants:
Linked BF Score: 5

 Description   

When we run a query in classic with a sort stage the order of the results does not depend in the order of the fields in the documents. When we run the same queries in SBE we see that the order of the results changes when the order of the fields in the documents changes. 



 Comments   
Comment by Foteini Alvanaki [ 16/Nov/23 ]

This discrepancy is considered acceptable.

Comment by Foteini Alvanaki [ 15/Nov/23 ]

According to the test, it seems that this issue is acceptable since the fuzzer has a method `
assertAggFuzzerResultDivergenceIsAcceptable` that considers the difference in results acceptable if the pipeline contains a project and the plans have been created by different engines.

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