[SERVER-82078] Handle empty window in SBE window with first/last removable accumulators set to Constant Expression Created: 11/Oct/23  Updated: 13/Nov/23  Resolved: 18/Oct/23

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: 7.1.1
Fix Version/s: 7.2.0-rc0

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

Issue Links:
Backports
Depends
Backwards Compatibility: Fully Compatible
Operating System: ALL
Backport Requested:
v7.1
Sprint: QE 2023-10-16, QE 2023-10-30
Participants:
Linked BF Score: 147

 Description   

In SBE Window stage we track the first/last documents in a window in dedicated slots. We use these slots to compute the first/last accumulators. We use these slots to decide if the window is empty and what the result should be. When the expression assigned to first/last accumulator in the query is a constant we do not associate it with a slot. Thus, we do not have a way to say if the window is empty. In this case we return the constant expression even when the window is empty. This is not expected. The results when the window is empty should be null.



 Comments   
Comment by Githook User [ 18/Oct/23 ]

Author:

{'name': 'Foteini Alvanaki', 'email': 'foteini.alvanaki@mongodb.com', 'username': ''}

Message: SERVER-82078 Handle empty window for first/last in SBE window stage
Branch: master
https://github.com/mongodb/mongo/commit/7ee58e38418ae603cfdaa2bd224ca50118985443

Comment by Foteini Alvanaki [ 11/Oct/23 ]

I was able to reproduce this by running the following

  • Create a collection

 

db.cakeSalesLevels.insertMany( [
{ _id: 0, orderDate: new Date("2020-05-18T14:10:30Z"),state: "CA", dessert: {type: "chocolate", price: 13}, quantity: 120 },
{ _id: 1, orderDate: new Date("2021-03-20T11:30:05Z"),state: "WA", dessert: {type: "chocolate", price: 14}, quantity: 140 },
{ _id: 2, orderDate: new Date("2021-01-11T06:31:15Z"),state: "CA", dessert: {type: "vanilla", price: 12}, quantity: 145 },
{ _id: 3, orderDate: new Date("2020-02-08T13:13:23Z"),state: "WA", dessert: {type: "vanilla", price: 13}, quantity: 104 },
{ _id: 4, orderDate: new Date("2019-05-18T16:09:01Z"),state: "CA", dessert: {type: "strawberry", price: 41}, quantity: 162 },
{ _id: 5, orderDate: new Date("2019-01-08T06:12:03Z"),state: "WA", dessert: {type: "strawberry", price: 43}, quantity: 134 }
] )

 

  • Query the collection

 

db.cakeSalesLevels.aggregate([ { 
   $setWindowFields: { 
      sortBy: { orderDate: 1 }, 
      "output" : {"dessert.size" : {$first : {$toUpper : "automatic"}, window: { documents: [1,3] }
} } } }])

 

Results in SBE

 

[
{ _id: 5, orderDate: ISODate("2019-01-08T06:12:03.000Z"), state: 'WA', dessert: { type: 'strawberry', price: 43, size: 'AUTOMATIC' }, quantity: 134},
{ _id: 4, orderDate: ISODate("2019-05-18T16:09:01.000Z"), state: 'CA', dessert: { type: 'strawberry', price: 41, size: 'AUTOMATIC' }, quantity: 162 },
{ _id: 3, orderDate: ISODate("2020-02-08T13:13:23.000Z"), state: 'WA', dessert: { type: 'vanilla', price: 13, size: 'AUTOMATIC' }, quantity: 104 },
{ _id: 0, orderDate: ISODate("2020-05-18T14:10:30.000Z"), state: 'CA', dessert: { type: 'chocolate', price: 13, size: 'AUTOMATIC' }, quantity: 120 },
{ _id: 2, orderDate: ISODate("2021-01-11T06:31:15.000Z"), state: 'CA', dessert: { type: 'vanilla', price: 12, size: 'AUTOMATIC' }, quantity: 145 },
{ _id: 1, orderDate: ISODate("2021-03-20T11:30:05.000Z"), state: 'WA', dessert: { type: 'chocolate', price: 14, size: 'AUTOMATIC' }, quantity: 140 }
]

 

Results in Classic

[
{ _id: 5, orderDate: ISODate("2019-01-08T06:12:03.000Z"), state: 'WA', dessert: { type: 'strawberry', price: 43, size: 'AUTOMATIC' }, quantity: 134 },
{ _id: 4, orderDate: ISODate("2019-05-18T16:09:01.000Z"), state: 'CA', dessert: { type: 'strawberry', price: 41, size: 'AUTOMATIC' }, quantity: 162 },
{ _id: 3, orderDate: ISODate("2020-02-08T13:13:23.000Z"), state: 'WA', dessert: { type: 'vanilla', price: 13, size: 'AUTOMATIC' }, quantity: 104 },
{ _id: 0, orderDate: ISODate("2020-05-18T14:10:30.000Z"), state: 'CA', dessert: { type: 'chocolate', price: 13, size: 'AUTOMATIC' }, quantity: 120 },
{ _id: 2, orderDate: ISODate("2021-01-11T06:31:15.000Z"), state: 'CA', dessert: { type: 'vanilla', price: 12, size: 'AUTOMATIC' }, quantity: 145 },
{ _id: 1, orderDate: ISODate("2021-03-20T11:30:05.000Z"), state: 'WA', dessert: { type: 'chocolate', price: 14, size: null }, quantity: 140 }
]

 

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