summaryrefslogtreecommitdiff
path: root/src/mongo/db/pipeline/window_function/window_function_exec_linear_fill.cpp
blob: 208c89f87d235cd1fc4a08a4ad4d5a7ea3e8226d (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
 *    Copyright (C) 2021-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#include "mongo/db/pipeline/window_function/window_function_exec_linear_fill.h"

namespace {
mongo::Value interpolate(
    mongo::Value x1, mongo::Value y1, mongo::Value x2, mongo::Value y2, mongo::Value xCoord) {
    // Given two known points (x1, y1) and (x2, y2) and a value x that lies between those two
    // points, we solve (or fill) for y with the following formula: y = y1 + (x - x1) * ((y2 -
    // y1)/(x2 - x1))
    return uassertStatusOK(mongo::ExpressionSubtract::apply(y2, y1).andThen([&](auto&& numerator) {
        return mongo::ExpressionSubtract::apply(x2, x1).andThen([&](auto&& denominator) {
            return mongo::ExpressionDivide::apply(numerator, denominator)
                .andThen([&](auto&& quotient) {
                    return mongo::ExpressionSubtract::apply(xCoord, x1)
                        .andThen([&](auto&& difference) {
                            return mongo::ExpressionMultiply::apply(quotient, difference)
                                .andThen([&](auto&& product) {
                                    return mongo::ExpressionAdd::apply(y1, product);
                                });
                        });
                });
        });
    }));
}
}  // namespace

namespace mongo {

boost::optional<Value> WindowFunctionExecLinearFill::evaluateInput(const Document& doc) {
    Value fillFieldValue = _input->evaluate(doc, &_input->getExpressionContext()->variables);
    if (!fillFieldValue.nullish()) {
        return boost::optional<Value>(fillFieldValue);
    }
    return boost::none;
}

boost::optional<std::pair<Value, Value>> WindowFunctionExecLinearFill::findX2Y2() {
    auto index = 1;
    while (const auto doc = _iter[index]) {
        if (const auto fillFieldValue = evaluateInput(*doc)) {
            Value sortFieldValue =
                _sortBy->evaluate(*doc, &_sortBy->getExpressionContext()->variables);
            if (!sortFieldValue.nullish()) {
                _prevX2Y2 = boost::optional<std::pair<Value, Value>>(
                    std::make_pair(sortFieldValue, *fillFieldValue));
                return _prevX2Y2;
            }
        }
        index++;
    }
    return boost::none;
}

Value WindowFunctionExecLinearFill::getNext() {
    const auto currentDoc = *_iter[0];
    Value fillFieldValue = _input->evaluate(currentDoc, &_input->getExpressionContext()->variables);
    uassert(ErrorCodes::TypeMismatch,
            str::stream() << "Value to be filled must be numeric or nullish, but found "
                          << fillFieldValue.getType(),
            fillFieldValue.numeric() || fillFieldValue.nullish());
    Value sortFieldValue =
        _sortBy->evaluate(currentDoc, &_sortBy->getExpressionContext()->variables);
    uassert(ErrorCodes::TypeMismatch,
            str::stream() << "Value of the sortBy field must be numeric or a date, but found "
                          << sortFieldValue.getType(),
            sortFieldValue.numeric() || sortFieldValue.coercibleToDate());

    // We do not allow repeated sort field values. This is because if we have the following
    // collection that has a repeated sort value and different fill values, eg [(10, 100), (10,
    // -100), (20, null), (30, 50)], it is unclear if the left value, (X1, Y1), should be (10, 100)
    // or (10, -100) when we interpolate on the third document.

    uassert(6050106,
            "There can be no repeated values in the sort field",
            ValueComparator{}.evaluate(sortFieldValue != _lastSeenElement));
    if (!_lastSeenElement.missing())
        // Throw an error If the sort value was previously of type numeric, but we've just found a
        // date (or vice versa).
        uassert(ErrorCodes::TypeMismatch,
                str::stream() << "Conflicting sort value types, previously received type "
                              << _lastSeenElement.getType() << ", but found "
                              << sortFieldValue.getType(),
                (sortFieldValue.coercibleToDate() && _lastSeenElement.coercibleToDate()) ||
                    (sortFieldValue.numeric() && _lastSeenElement.numeric()));
    _lastSeenElement = sortFieldValue;

    // We have found either (x1, y1) or (x2, y2).
    if (!fillFieldValue.nullish()) {
        // We can expire all documents before the current document in the cache. We don't want to
        // expire the current document, because it may become our *first* non-null, ie (x1, x2), for
        // the next set of null documents.
        _iter.manualExpireUpTo(-1);
        // If (x2, y2) is known, it becomes our (x1, y1) for the next sequence of null documents.
        // Otherwise the current document becomes (x1, y1).
        _prevX1Y1 = _prevX2Y2 ? _prevX2Y2
                              : boost::optional<std::pair<Value, Value>>(
                                    std::make_pair(sortFieldValue, fillFieldValue));
        _prevX2Y2 = boost::none;
        return fillFieldValue;
    }
    // Interpolation requires that the documents with null values for the field we are filling,
    // be bookended with documents with non-null sort and input field numeric values.
    // To do this, we store the previous known coordinates so that we don't scan the same null
    // documents multiple times as we search for a non-null value.
    auto firstCoord = _prevX1Y1;
    if (!firstCoord) {
        return kDefault;
    }

    auto secondCoord = _prevX2Y2 ? _prevX2Y2 : findX2Y2();
    if (!secondCoord) {
        return kDefault;
    }

    return interpolate(firstCoord->first,
                       firstCoord->second,
                       secondCoord->first,
                       secondCoord->second,
                       sortFieldValue);
}
}  // namespace mongo