[SERVER-69675] Simplify window_function_exec_linear_fill.cpp's interpolate function Created: 14/Sep/22  Updated: 29/Oct/23  Resolved: 15/Sep/22

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

Type: Improvement Priority: Major - P3
Reporter: Billy Donahue Assignee: Billy Donahue
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
is depended on by SERVER-69704 Remove unused StatusWith::andThen, St... Closed
Backwards Compatibility: Fully Compatible
Sprint: Service Arch 2022-09-19
Participants:

 Description   

I happened across this while considering replacing StatusWith::andThen.
This is the only code that uses it, and it's pretty gnarly code.
I have a much cleaner way to write this without StatusWith::andThen.
I'll send off as a PR.

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);
                                });
                        });
                });
        });
    }));

Becomes (after defining operators in a private namespace):

Value interpolate(Value x1, Value y1, Value x2, Value y2, Value x) {
    using namespace value_arithmetic_operators;
    return y1 + (x - x1) * ((y2 - y1) / (x2 - x1));
}



 Comments   
Comment by Githook User [ 15/Sep/22 ]

Author:

{'name': 'Billy Donahue', 'email': 'billy.donahue@mongodb.com', 'username': 'BillyDonahue'}

Message: SERVER-69675 Simplify window_function_exec_linear_fill interpolate
Branch: master
https://github.com/mongodb/mongo/commit/05d1b5a4f2118d76d9ed2ba78e2547b3d392105c

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