aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/AggregateFunctions/AggregateFunctionDeltaSumTimestamp.h
blob: 5eeb1425afb756d8dcccb0474f1fb1227c274262 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#pragma once

#include <type_traits>

#include <IO/ReadHelpers.h>
#include <IO/WriteHelpers.h>

#include <Columns/ColumnVector.h>
#include <DataTypes/DataTypesDecimal.h>
#include <DataTypes/DataTypesNumber.h>

#include <AggregateFunctions/IAggregateFunction.h>


namespace DB
{

template <typename ValueType, typename TimestampType>
struct AggregationFunctionDeltaSumTimestampData
{
    ValueType sum = 0;
    ValueType first = 0;
    ValueType last = 0;
    TimestampType first_ts = 0;
    TimestampType last_ts = 0;
    bool seen = false;
};

template <typename ValueType, typename TimestampType>
class AggregationFunctionDeltaSumTimestamp final
    : public IAggregateFunctionDataHelper<
        AggregationFunctionDeltaSumTimestampData<ValueType, TimestampType>,
        AggregationFunctionDeltaSumTimestamp<ValueType, TimestampType>
      >
{
public:
    AggregationFunctionDeltaSumTimestamp(const DataTypes & arguments, const Array & params)
        : IAggregateFunctionDataHelper<
            AggregationFunctionDeltaSumTimestampData<ValueType, TimestampType>,
            AggregationFunctionDeltaSumTimestamp<ValueType, TimestampType>
        >{arguments, params, createResultType()}
    {}

    AggregationFunctionDeltaSumTimestamp()
        : IAggregateFunctionDataHelper<
            AggregationFunctionDeltaSumTimestampData<ValueType, TimestampType>,
            AggregationFunctionDeltaSumTimestamp<ValueType, TimestampType>
        >{}
    {}

    bool allocatesMemoryInArena() const override { return false; }

    String getName() const override { return "deltaSumTimestamp"; }

    static DataTypePtr createResultType() { return std::make_shared<DataTypeNumber<ValueType>>(); }

    void NO_SANITIZE_UNDEFINED ALWAYS_INLINE add(AggregateDataPtr __restrict place, const IColumn ** columns, size_t row_num, Arena *) const override
    {
        auto value = assert_cast<const ColumnVector<ValueType> &>(*columns[0]).getData()[row_num];
        auto ts = assert_cast<const ColumnVector<TimestampType> &>(*columns[1]).getData()[row_num];

        if ((this->data(place).last < value) && this->data(place).seen)
        {
            this->data(place).sum += (value - this->data(place).last);
        }

        this->data(place).last = value;
        this->data(place).last_ts = ts;

        if (!this->data(place).seen)
        {
            this->data(place).first = value;
            this->data(place).seen = true;
            this->data(place).first_ts = ts;
        }
    }

    // before returns true if lhs is before rhs or false if it is not or can't be determined
    bool ALWAYS_INLINE before (
        const AggregationFunctionDeltaSumTimestampData<ValueType, TimestampType> * lhs,
        const AggregationFunctionDeltaSumTimestampData<ValueType, TimestampType> * rhs
    ) const
    {
        if (lhs->last_ts < rhs->first_ts)
        {
            return true;
        }
        if (lhs->last_ts == rhs->first_ts && (lhs->last_ts < rhs->last_ts || lhs->first_ts < rhs->first_ts))
        {
            return true;
        }
        return false;
    }

    void NO_SANITIZE_UNDEFINED ALWAYS_INLINE merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs, Arena *) const override
    {
        auto place_data = &this->data(place);
        auto rhs_data = &this->data(rhs);

        if (!place_data->seen && rhs_data->seen)
        {
            place_data->sum = rhs_data->sum;
            place_data->seen = true;
            place_data->first = rhs_data->first;
            place_data->first_ts = rhs_data->first_ts;
            place_data->last = rhs_data->last;
            place_data->last_ts = rhs_data->last_ts;
        }
        else if (place_data->seen && !rhs_data->seen)
            return;
        else if (before(place_data, rhs_data))
        {
            // This state came before the rhs state

            if (rhs_data->first > place_data->last)
                place_data->sum += (rhs_data->first - place_data->last);
            place_data->sum += rhs_data->sum;
            place_data->last = rhs_data->last;
            place_data->last_ts = rhs_data->last_ts;
        }
        else if (before(rhs_data, place_data))
        {
            // This state came after the rhs state

            if (place_data->first > rhs_data->last)
                place_data->sum += (place_data->first - rhs_data->last);
            place_data->sum += rhs_data->sum;
            place_data->first = rhs_data->first;
            place_data->first_ts = rhs_data->first_ts;
        }
        else
        {
            // If none of those conditions matched, it means both states we are merging have all
            // same timestamps. We have to pick either the smaller or larger value so that the
            // result is deterministic.

            if (place_data->first < rhs_data->first)
            {
                place_data->first = rhs_data->first;
                place_data->last = rhs_data->last;
            }
        }
    }

    void serialize(ConstAggregateDataPtr __restrict place, WriteBuffer & buf, std::optional<size_t> /* version */) const override
    {
        writeBinaryLittleEndian(this->data(place).sum, buf);
        writeBinaryLittleEndian(this->data(place).first, buf);
        writeBinaryLittleEndian(this->data(place).first_ts, buf);
        writeBinaryLittleEndian(this->data(place).last, buf);
        writeBinaryLittleEndian(this->data(place).last_ts, buf);
        writeBinaryLittleEndian(this->data(place).seen, buf);
    }

    void deserialize(AggregateDataPtr __restrict place, ReadBuffer & buf, std::optional<size_t> /* version */, Arena *) const override
    {
        readBinaryLittleEndian(this->data(place).sum, buf);
        readBinaryLittleEndian(this->data(place).first, buf);
        readBinaryLittleEndian(this->data(place).first_ts, buf);
        readBinaryLittleEndian(this->data(place).last, buf);
        readBinaryLittleEndian(this->data(place).last_ts, buf);
        readBinaryLittleEndian(this->data(place).seen, buf);
    }

    void insertResultInto(AggregateDataPtr __restrict place, IColumn & to, Arena *) const override
    {
        assert_cast<ColumnVector<ValueType> &>(to).getData().push_back(this->data(place).sum);
    }
};

}