summaryrefslogtreecommitdiff
path: root/jstests/core/nan.js
blob: d19fc4919a4b5a39e0c1af2f6bc1caa8a2994081 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
 * Tests basic NaN handling. Note that WiredTiger indexes handle -NaN and NaN differently.
 */
(function() {
"use strict";

const coll = db.jstests_nan;
coll.drop();

assert.commandWorked(coll.insert({_id: 0, a: -Infinity}));
assert.commandWorked(coll.insert({_id: 1, a: -3}));
assert.commandWorked(coll.insert({_id: 2, a: 0}));
assert.commandWorked(coll.insert({_id: 3, a: 3}));
assert.commandWorked(coll.insert({_id: 4, a: Infinity}));
assert.commandWorked(coll.insert({_id: 5, a: NaN}));
assert.commandWorked(coll.insert({_id: 6, a: -NaN}));
assert.commandWorked(coll.insert({_id: 7, a: undefined}));
assert.commandWorked(coll.insert({_id: 8, a: null}));
assert.commandWorked(coll.insert({_id: 9, a: []}));
assert.commandWorked(coll.insert({_id: 10, a: {b: 1}}));
assert.commandWorked(coll.insert({_id: 11, a: {b: 1}}));

/**
 * Ensures correct results for EQ, LT, LTE, GT, GTE, and IN cases.
 */
function testNaNComparisons() {
    // EQ
    let cursor = coll.find({a: NaN}).sort({_id: 1});
    assert.eq(5, cursor.next()["_id"]);
    assert.eq(6, cursor.next()["_id"]);
    assert(!cursor.hasNext());

    // LT
    cursor = coll.find({a: {$lt: NaN}});
    assert(!cursor.hasNext());

    // LTE
    cursor = coll.find({a: {$lte: NaN}}).sort({_id: 1});
    assert.eq(5, cursor.next()["_id"]);
    assert.eq(6, cursor.next()["_id"]);
    assert(!cursor.hasNext());

    // GT
    cursor = coll.find({a: {$gt: NaN}});
    assert(!cursor.hasNext());

    // GTE
    cursor = coll.find({a: {$gte: NaN}}).sort({_id: 1});
    assert.eq(5, cursor.next()["_id"]);
    assert.eq(6, cursor.next()["_id"]);
    assert(!cursor.hasNext());

    // IN
    // Positive NaN should match both positive and negative NaN. Note that the second value protects
    // the $in from being optimized away.
    cursor = coll.find({a: {$in: [NaN, 1000]}}).sort({_id: 1});
    assert.eq(5, cursor.next()["_id"]);
    assert.eq(6, cursor.next()["_id"]);
    assert(!cursor.hasNext());

    // Negative NaN should match both positive and negative NaN. Note that the second value protects
    // the $in from being optimized away.
    cursor = coll.find({a: {$in: [-NaN, 1000]}}).sort({_id: 1});
    assert.eq(5, cursor.next()["_id"]);
    assert.eq(6, cursor.next()["_id"]);
    assert(!cursor.hasNext());

    // NaNs of different types should match both positive and negative NaN. Note that the second
    // value protects the $in from being optimized away.
    cursor = coll.find({a: {$in: [NumberDecimal(NaN), 1000]}}).sort({_id: 1});
    assert.eq(5, cursor.next()["_id"]);
    assert.eq(6, cursor.next()["_id"]);
    assert(!cursor.hasNext());

    cursor = coll.find({a: {$in: [NumberDecimal(-NaN), 1000]}}).sort({_id: 1});
    assert.eq(5, cursor.next()["_id"]);
    assert.eq(6, cursor.next()["_id"]);
    assert(!cursor.hasNext());
}

// Unindexed.
testNaNComparisons();

// Indexed.
assert.commandWorked(coll.createIndex({a: 1}));
testNaNComparisons();

assert(coll.drop());
assert.commandWorked(coll.insert({a: NaN}));
assert.commandWorked(coll.insert({a: -NaN}));

/**
 * Ensures that documents with NaN values do not get matched when the query has a non-NaN value.
 */
function testNonNaNQuery() {
    const queries = [{a: 1}, {a: {$lt: 1}}, {a: {$lte: 1}}, {a: {$gt: 1}}, {a: {$gte: 1}}];
    for (const query of queries) {
        const cursor = coll.find(query);
        assert(!cursor.hasNext());
    }
}

// Unindexed.
testNonNaNQuery();

// Indexed.
assert.commandWorked(coll.createIndex({a: 1}));
testNonNaNQuery();
}());