[SERVER-79219] [CQF] $lt, $lte should not match NaN Created: 21/Jul/23  Updated: 29/Oct/23  Resolved: 02/Aug/23

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

Type: Bug Priority: Major - P3
Reporter: David Percy Assignee: Militsa Sotirova
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-67818 [CQF] NaN $eq NaN should be true Closed
Assigned Teams:
Query Optimization
Backwards Compatibility: Fully Compatible
Operating System: ALL
Sprint: QO 2023-08-07
Participants:

 Description   

The fuzzer found this:

> db.c.drop()
true
> db.c.insert({_id:1, n:NaN})
WriteResult({ "nInserted" : 1 })
> db.c.find({n: {$lt: 0}})
{ "_id" : 1, "n" : NaN }
> db.c.find({n: {$lt: 0}})
{ "_id" : 1, "n" : NaN }
> db.c.find({n: {$lte: 0}})
{ "_id" : 1, "n" : NaN }
> db.c.find({n: {$lt: -Infinity}})
{ "_id" : 1, "n" : NaN }
> db.c.find({n: {$lte: -Infinity}})
{ "_id" : 1, "n" : NaN }

All of these predicates should be false because comparisons with NaN should be false.

I think the simplest place to fix this is in ABT translation. Here's the initial ABT before optimization:

explain : Root [{p0}]
Filter []
|   EvalFilter []
|   |   Variable [p0]
|   PathGet [n] PathTraverse [1] PathComposeM []
|   |   PathCompare [Gte] Const [nan]
|   PathCompare [Lt] Const [-inf]
Scan [c_d0fd46ae-4b46-4fc0-9552-4201aef6ee7f, {p0}]

When translating MQL to ABT we introduce an explicit lower bound so that the query only matches numbers. That way Lt / Gte can be consistent with intervals / index bounds / a total order. If we made the lower bound `Gt nan` or `Gte -inf` that would fix the bug.

This seems closely related to SERVER-67818 but not exactly the same. It looks like the type-bracketing rules for numbers will have to treat NaN differently than all other numbers.



 Comments   
Comment by Githook User [ 03/Aug/23 ]

Author:

{'name': 'Militsa Sotirova', 'email': 'militsa.sotirova@mongodb.com', 'username': 'militsasotirova'}

Message: SERVER-79219 lt and lte should not match NaN
Branch: minh.luu-no_compile_sys-perf
https://github.com/mongodb/mongo/commit/bb28e590a38efee33784045b65667f9562cdf26b

Comment by Githook User [ 02/Aug/23 ]

Author:

{'name': 'Militsa Sotirova', 'email': 'militsa.sotirova@mongodb.com', 'username': 'militsasotirova'}

Message: SERVER-79219 lt and lte should not match NaN
Branch: master
https://github.com/mongodb/mongo/commit/bb28e590a38efee33784045b65667f9562cdf26b

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