aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/expression/fixedwidthbucketfunctionnode.cpp
blob: 9a927e448d16fd28774709c891f869cbb928543b (plain) (blame)
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "fixedwidthbucketfunctionnode.h"
#include "integerresultnode.h"
#include "floatresultnode.h"
#include "integerbucketresultnode.h"
#include "floatbucketresultnode.h"
#include "resultvector.h"
#include <vespa/vespalib/util/stringfmt.h>
#include <stdexcept>
#include <cmath>
#include <limits>

namespace search::expression {

IMPLEMENT_EXPRESSIONNODE(FixedWidthBucketFunctionNode, UnaryFunctionNode);

void
FixedWidthBucketFunctionNode::IntegerBucketHandler::update(ResultNode &result, const ResultNode &value) const
{
    IntegerBucketResultNode &bucket = (IntegerBucketResultNode &)result;
    int64_t n = value.getInteger();
    int64_t from = n;
    int64_t to = n;
    if (width > 0) {
        if (n >= 0) {
            from = (n/width) * width;
            if (from >= (std::numeric_limits<int64_t>::max() - width)) {
                to = std::numeric_limits<int64_t>::max();
            } else {
                to = from + width;
            }
        } else {
            to = ((n+1)/width) * width;
            if (to <= (std::numeric_limits<int64_t>::min() + width)) {
                from = std::numeric_limits<int64_t>::min();
            } else {
                from = to - width;
            }
        }
    }
    bucket.setRange(from, to);
}

void
FixedWidthBucketFunctionNode::IntegerVectorBucketHandler::update(ResultNode &result, const ResultNode &value) const
{
    const IntegerResultNodeVector::Vector & v(static_cast<const IntegerResultNodeVector &>(value).getVector());
    IntegerBucketResultNodeVector::Vector & r(static_cast<IntegerBucketResultNodeVector &>(result).getVector());
    r.resize(v.size());
    for (size_t i(0), m(v.size()); i < m; i++) {
        IntegerBucketHandler::update(r[i], v[i]);
    }
}

void
FixedWidthBucketFunctionNode::FloatVectorBucketHandler::update(ResultNode &result, const ResultNode &value) const
{
    const FloatResultNodeVector::Vector & v(static_cast<const FloatResultNodeVector &>(value).getVector());
    FloatBucketResultNodeVector::Vector & r(static_cast<FloatBucketResultNodeVector &>(result).getVector());
    r.resize(v.size());
    for (size_t i(0), m(v.size()); i < m; i++) {
        FloatBucketHandler::update(r[i], v[i]);
    }
}

void
FixedWidthBucketFunctionNode::FloatBucketHandler::update(ResultNode &result, const ResultNode &value) const
{
    FloatBucketResultNode &bucket = (FloatBucketResultNode &)result;
    double n = value.getFloat();
    double from = n;
    double to = n;
    if (width > 0.0) {
        double tmp = std::floor(n/width);
        from = tmp * width;
        to = (tmp+1) * width;
    }
    bucket.setRange(from, to);
}

FixedWidthBucketFunctionNode::~FixedWidthBucketFunctionNode() = default;

void
FixedWidthBucketFunctionNode::onPrepareResult()
{
    const ExpressionNode &child = getArg();
    const ResultNode     &input = *child.getResult();
    if (input.getClass().inherits(IntegerResultNode::classId)) {
        setResultType(std::make_unique<IntegerBucketResultNode>());
        _bucketHandler.reset(new IntegerBucketHandler(_width->getInteger()));
    } else if (input.getClass().inherits(FloatResultNode::classId)) {
        setResultType(std::make_unique<FloatBucketResultNode>());
        _bucketHandler.reset(new FloatBucketHandler(_width->getFloat()));
    } else if (input.getClass().inherits(IntegerResultNodeVector::classId)) {
        setResultType(std::make_unique<IntegerBucketResultNodeVector>());
        _bucketHandler.reset(new IntegerVectorBucketHandler(_width->getInteger()));
    } else if (input.getClass().inherits(FloatResultNodeVector::classId)) {
        setResultType(std::make_unique<FloatBucketResultNodeVector>());
        _bucketHandler.reset(new FloatVectorBucketHandler(_width->getFloat()));
    } else {
        throw std::runtime_error(vespalib::make_string("cannot create appropriate bucket for type '%s'", input.getClass().name()));
    }
}

bool
FixedWidthBucketFunctionNode::onExecute() const
{
    getArg().execute();
    _bucketHandler->update(updateResult(), *getArg().getResult());
    return true;
}

vespalib::Serializer &
FixedWidthBucketFunctionNode::onSerialize(vespalib::Serializer &os) const
{
    UnaryFunctionNode::onSerialize(os);
    return os << _width;
}

vespalib::Deserializer &
FixedWidthBucketFunctionNode::onDeserialize(vespalib::Deserializer &is)
{
    UnaryFunctionNode::onDeserialize(is);
    return is >> _width;
}

}

// this function was added by ../../forcelink.sh
void forcelink_file_searchlib_expression_fixedwidthbucketfunctionnode() {}