2019-05-15 16:16:25 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-05-15 16:27:44 +00:00
|
|
|
#include <bitset>
|
2019-05-15 16:16:25 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <map>
|
|
|
|
#include <queue>
|
2019-05-15 16:27:44 +00:00
|
|
|
#include <sstream>
|
2019-05-15 16:16:25 +00:00
|
|
|
#include <unordered_set>
|
2019-05-15 16:27:44 +00:00
|
|
|
#include <utility>
|
2019-05-15 16:16:25 +00:00
|
|
|
#include <Columns/ColumnArray.h>
|
|
|
|
#include <Columns/ColumnTuple.h>
|
2019-05-15 16:27:44 +00:00
|
|
|
#include <Columns/ColumnsNumber.h>
|
2019-05-15 16:16:25 +00:00
|
|
|
#include <DataTypes/DataTypeArray.h>
|
2019-05-15 16:27:44 +00:00
|
|
|
#include <DataTypes/DataTypeTuple.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
2019-05-15 16:16:25 +00:00
|
|
|
#include <IO/ReadHelpers.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <Common/ArenaAllocator.h>
|
|
|
|
#include <ext/range.h>
|
2019-05-15 16:27:44 +00:00
|
|
|
#include "IAggregateFunction.h"
|
2019-05-15 16:16:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2019-05-15 16:27:44 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
extern const int TOO_MANY_ARGUMENTS_FOR_FUNCTION;
|
|
|
|
}
|
|
|
|
template <bool rate>
|
|
|
|
struct AggregateFunctionTSgroupSumData
|
|
|
|
{
|
|
|
|
using DataPoint = std::pair<Int64, Float64>;
|
|
|
|
struct Points
|
2019-05-15 16:16:25 +00:00
|
|
|
{
|
2019-05-15 16:27:44 +00:00
|
|
|
using Dps = std::queue<DataPoint>;
|
|
|
|
Dps dps;
|
|
|
|
void add(Int64 t, Float64 v)
|
2019-05-15 16:16:25 +00:00
|
|
|
{
|
2019-05-15 16:27:44 +00:00
|
|
|
dps.push(std::make_pair(t, v));
|
|
|
|
if (dps.size() > 2)
|
|
|
|
dps.pop();
|
|
|
|
}
|
|
|
|
Float64 getval(Int64 t)
|
|
|
|
{
|
|
|
|
Int64 t1, t2;
|
|
|
|
Float64 v1, v2;
|
|
|
|
if (rate)
|
2019-05-15 16:16:25 +00:00
|
|
|
{
|
2019-05-15 16:27:44 +00:00
|
|
|
if (dps.size() < 2)
|
|
|
|
return 0;
|
|
|
|
t1 = dps.back().first;
|
|
|
|
t2 = dps.front().first;
|
|
|
|
v1 = dps.back().second;
|
|
|
|
v2 = dps.front().second;
|
|
|
|
return (v1 - v2) / Float64(t1 - t2);
|
2019-05-15 16:16:25 +00:00
|
|
|
}
|
2019-05-15 16:27:44 +00:00
|
|
|
else
|
2019-05-15 16:16:25 +00:00
|
|
|
{
|
2019-05-15 16:27:44 +00:00
|
|
|
if (dps.size() == 1 && t == dps.front().first)
|
|
|
|
return dps.front().second;
|
|
|
|
t1 = dps.back().first;
|
|
|
|
t2 = dps.front().first;
|
|
|
|
v1 = dps.back().second;
|
|
|
|
v2 = dps.front().second;
|
|
|
|
return v2 + ((v1 - v2) * Float64(t - t2)) / Float64(t1 - t2);
|
2019-05-15 16:16:25 +00:00
|
|
|
}
|
2019-05-15 16:27:44 +00:00
|
|
|
}
|
|
|
|
};
|
2019-05-15 16:16:25 +00:00
|
|
|
|
2019-05-15 16:27:44 +00:00
|
|
|
static constexpr size_t bytes_on_stack = 128;
|
|
|
|
typedef std::map<UInt64, Points> Series;
|
|
|
|
typedef PODArray<DataPoint, bytes_on_stack, AllocatorWithStackMemory<Allocator<false>, bytes_on_stack>> AggSeries;
|
|
|
|
Series ss;
|
|
|
|
AggSeries result;
|
2019-05-15 16:16:25 +00:00
|
|
|
|
2019-05-15 16:27:44 +00:00
|
|
|
void add(UInt64 uid, Int64 t, Float64 v)
|
|
|
|
{ //suppose t is coming asc
|
|
|
|
typename Series::iterator it_ss;
|
|
|
|
if (ss.count(uid) == 0)
|
|
|
|
{ //time series not exist, insert new one
|
|
|
|
Points tmp;
|
|
|
|
tmp.add(t, v);
|
|
|
|
ss.emplace(uid, tmp);
|
|
|
|
it_ss = ss.find(uid);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
it_ss = ss.find(uid);
|
|
|
|
it_ss->second.add(t, v);
|
|
|
|
}
|
|
|
|
if (result.size() > 0 && t < result.back().first)
|
|
|
|
throw Exception{"TSgroupSum or TSgroupRateSum must order by timestamp asc!!!", ErrorCodes::LOGICAL_ERROR};
|
|
|
|
if (result.size() > 0 && t == result.back().first)
|
|
|
|
{
|
|
|
|
//do not add new point
|
|
|
|
if (rate)
|
|
|
|
result.back().second += it_ss->second.getval(t);
|
2019-05-15 16:16:25 +00:00
|
|
|
else
|
2019-05-15 16:27:44 +00:00
|
|
|
result.back().second += v;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (rate)
|
|
|
|
result.emplace_back(std::make_pair(t, it_ss->second.getval(t)));
|
2019-05-15 16:16:25 +00:00
|
|
|
else
|
2019-05-15 16:27:44 +00:00
|
|
|
result.emplace_back(std::make_pair(t, v));
|
|
|
|
}
|
|
|
|
size_t i = result.size() - 1;
|
|
|
|
//reverse find out the index of timestamp that more than previous timestamp of t
|
|
|
|
while (result[i].first > it_ss->second.dps.front().first && i >= 0)
|
|
|
|
i--;
|
2019-05-15 16:16:25 +00:00
|
|
|
|
2019-05-15 16:27:44 +00:00
|
|
|
i++;
|
|
|
|
while (i < result.size() - 1)
|
|
|
|
{
|
|
|
|
result[i].second += it_ss->second.getval(result[i].first);
|
2019-05-15 16:16:25 +00:00
|
|
|
i++;
|
|
|
|
}
|
2019-05-15 16:27:44 +00:00
|
|
|
}
|
2019-05-15 16:16:25 +00:00
|
|
|
|
2019-05-15 16:27:44 +00:00
|
|
|
void merge(const AggregateFunctionTSgroupSumData & other)
|
|
|
|
{
|
|
|
|
//if ts has overlap, then aggregate two series by interpolation;
|
|
|
|
AggSeries tmp;
|
|
|
|
tmp.reserve(other.result.size() + result.size());
|
|
|
|
size_t i = 0, j = 0;
|
|
|
|
Int64 t1, t2;
|
|
|
|
Float64 v1, v2;
|
|
|
|
while (i < result.size() && j < other.result.size())
|
2019-05-15 16:16:25 +00:00
|
|
|
{
|
2019-05-15 16:27:44 +00:00
|
|
|
if (result[i].first < other.result[j].first)
|
2019-05-15 16:16:25 +00:00
|
|
|
{
|
2019-05-15 16:27:44 +00:00
|
|
|
if (j == 0)
|
|
|
|
{
|
|
|
|
tmp.emplace_back(result[i]);
|
|
|
|
}
|
|
|
|
else
|
2019-05-15 16:16:25 +00:00
|
|
|
{
|
2019-05-15 16:27:44 +00:00
|
|
|
t1 = other.result[j].first;
|
|
|
|
t2 = other.result[j - 1].first;
|
|
|
|
v1 = other.result[j].second;
|
|
|
|
v2 = other.result[j - 1].second;
|
|
|
|
Float64 value = result[i].second + v2 + (v1 - v2) * (Float64(result[i].first - t2)) / Float64(t1 - t2);
|
|
|
|
tmp.emplace_back(std::make_pair(result[i].first, value));
|
2019-05-15 16:16:25 +00:00
|
|
|
}
|
2019-05-15 16:27:44 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else if (result[i].first > other.result[j].first)
|
|
|
|
{
|
|
|
|
if (i == 0)
|
2019-05-15 16:16:25 +00:00
|
|
|
{
|
2019-05-15 16:27:44 +00:00
|
|
|
tmp.emplace_back(other.result[j]);
|
2019-05-15 16:16:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-05-15 16:27:44 +00:00
|
|
|
t1 = result[i].first;
|
|
|
|
t2 = result[i - 1].first;
|
|
|
|
v1 = result[i].second;
|
|
|
|
v2 = result[i - 1].second;
|
|
|
|
Float64 value = other.result[j].second + v2 + (v1 - v2) * (Float64(other.result[j].first - t2)) / Float64(t1 - t2);
|
|
|
|
tmp.emplace_back(std::make_pair(other.result[j].first, value));
|
2019-05-15 16:16:25 +00:00
|
|
|
}
|
2019-05-15 16:27:44 +00:00
|
|
|
j++;
|
2019-05-15 16:16:25 +00:00
|
|
|
}
|
2019-05-15 16:27:44 +00:00
|
|
|
else
|
2019-05-15 16:16:25 +00:00
|
|
|
{
|
2019-05-15 16:27:44 +00:00
|
|
|
tmp.emplace_back(std::make_pair(result[i].first, result[i].second + other.result[j].second));
|
2019-05-15 16:16:25 +00:00
|
|
|
i++;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 16:27:44 +00:00
|
|
|
while (i < result.size())
|
2019-05-15 16:16:25 +00:00
|
|
|
{
|
2019-05-15 16:27:44 +00:00
|
|
|
tmp.emplace_back(result[i]);
|
|
|
|
i++;
|
2019-05-15 16:16:25 +00:00
|
|
|
}
|
2019-05-15 16:27:44 +00:00
|
|
|
while (j < other.result.size())
|
2019-05-15 16:16:25 +00:00
|
|
|
{
|
2019-05-15 16:27:44 +00:00
|
|
|
tmp.push_back(other.result[j]);
|
|
|
|
j++;
|
2019-05-15 16:16:25 +00:00
|
|
|
}
|
2019-05-15 16:27:44 +00:00
|
|
|
swap(result, tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void serialize(WriteBuffer & buf) const
|
2019-05-15 16:16:25 +00:00
|
|
|
{
|
2019-05-15 16:27:44 +00:00
|
|
|
size_t size = result.size();
|
|
|
|
writeVarUInt(size, buf);
|
|
|
|
buf.write(reinterpret_cast<const char *>(result.data()), sizeof(result[0]));
|
|
|
|
}
|
2019-05-15 16:16:25 +00:00
|
|
|
|
2019-05-15 16:27:44 +00:00
|
|
|
void deserialize(ReadBuffer & buf)
|
|
|
|
{
|
|
|
|
size_t size = 0;
|
|
|
|
readVarUInt(size, buf);
|
|
|
|
result.resize(size);
|
|
|
|
buf.read(reinterpret_cast<char *>(result.data()), size * sizeof(result[0]));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template <bool rate>
|
|
|
|
class AggregateFunctionTSgroupSum final
|
|
|
|
: public IAggregateFunctionDataHelper<AggregateFunctionTSgroupSumData<rate>, AggregateFunctionTSgroupSum<rate>>
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
public:
|
|
|
|
String getName() const override { return rate ? "TSgroupRateSum" : "TSgroupSum"; }
|
2019-05-15 16:16:25 +00:00
|
|
|
|
2019-05-15 16:27:44 +00:00
|
|
|
AggregateFunctionTSgroupSum(const DataTypes & arguments)
|
|
|
|
: IAggregateFunctionDataHelper<AggregateFunctionTSgroupSumData<rate>, AggregateFunctionTSgroupSum<rate>>(arguments, {})
|
|
|
|
{
|
|
|
|
if (!WhichDataType(arguments[0].get()).isUInt64())
|
|
|
|
throw Exception{"Illegal type " + arguments[0].get()->getName() + " of argument 1 of aggregate function " + getName()
|
|
|
|
+ ", must be UInt64",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
2019-05-15 16:16:25 +00:00
|
|
|
|
2019-05-15 16:27:44 +00:00
|
|
|
if (!WhichDataType(arguments[1].get()).isInt64())
|
|
|
|
throw Exception{"Illegal type " + arguments[1].get()->getName() + " of argument 2 of aggregate function " + getName()
|
|
|
|
+ ", must be Int64",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
2019-05-15 16:16:25 +00:00
|
|
|
|
2019-05-15 16:27:44 +00:00
|
|
|
if (!WhichDataType(arguments[2].get()).isFloat64())
|
|
|
|
throw Exception{"Illegal type " + arguments[2].get()->getName() + " of argument 3 of aggregate function " + getName()
|
|
|
|
+ ", must be Float64",
|
|
|
|
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
|
|
|
}
|
2019-05-15 16:16:25 +00:00
|
|
|
|
2019-05-15 16:27:44 +00:00
|
|
|
DataTypePtr getReturnType() const override
|
|
|
|
{
|
|
|
|
auto datatypes = std::vector<DataTypePtr>();
|
|
|
|
datatypes.push_back(std::make_shared<DataTypeInt64>());
|
|
|
|
datatypes.push_back(std::make_shared<DataTypeFloat64>());
|
2019-05-15 16:16:25 +00:00
|
|
|
|
2019-05-15 16:27:44 +00:00
|
|
|
return std::make_shared<DataTypeArray>(std::make_shared<DataTypeTuple>(datatypes));
|
|
|
|
}
|
2019-05-15 16:16:25 +00:00
|
|
|
|
2019-05-15 16:27:44 +00:00
|
|
|
void add(AggregateDataPtr place, const IColumn ** columns, const size_t row_num, Arena *) const override
|
|
|
|
{
|
|
|
|
auto uid = static_cast<const ColumnVector<UInt64> *>(columns[0])->getData()[row_num];
|
|
|
|
auto ts = static_cast<const ColumnVector<Int64> *>(columns[1])->getData()[row_num];
|
|
|
|
auto val = static_cast<const ColumnVector<Float64> *>(columns[2])->getData()[row_num];
|
|
|
|
if (uid && ts && val)
|
2019-05-15 16:16:25 +00:00
|
|
|
{
|
2019-05-15 16:27:44 +00:00
|
|
|
this->data(place).add(uid, ts, val);
|
2019-05-15 16:16:25 +00:00
|
|
|
}
|
2019-05-15 16:27:44 +00:00
|
|
|
}
|
2019-05-15 16:16:25 +00:00
|
|
|
|
2019-05-15 16:27:44 +00:00
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena *) const override { this->data(place).merge(this->data(rhs)); }
|
2019-05-15 16:16:25 +00:00
|
|
|
|
2019-05-15 16:27:44 +00:00
|
|
|
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override { this->data(place).serialize(buf); }
|
2019-05-15 16:16:25 +00:00
|
|
|
|
2019-05-15 16:27:44 +00:00
|
|
|
void deserialize(AggregateDataPtr place, ReadBuffer & buf, Arena *) const override { this->data(place).deserialize(buf); }
|
2019-05-15 16:16:25 +00:00
|
|
|
|
2019-05-15 16:27:44 +00:00
|
|
|
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
|
|
|
|
{
|
|
|
|
const auto & value = this->data(place).result;
|
|
|
|
size_t size = value.size();
|
2019-05-15 16:16:25 +00:00
|
|
|
|
2019-05-15 16:27:44 +00:00
|
|
|
ColumnArray & arr_to = static_cast<ColumnArray &>(to);
|
|
|
|
ColumnArray::Offsets & offsets_to = arr_to.getOffsets();
|
|
|
|
size_t old_size = offsets_to.back();
|
2019-05-15 16:16:25 +00:00
|
|
|
|
2019-05-15 16:27:44 +00:00
|
|
|
offsets_to.push_back(offsets_to.back() + size);
|
2019-05-15 16:16:25 +00:00
|
|
|
|
2019-05-15 16:27:44 +00:00
|
|
|
if (size)
|
|
|
|
{
|
|
|
|
typename ColumnInt64::Container & ts_to
|
|
|
|
= static_cast<ColumnInt64 &>(static_cast<ColumnTuple &>(arr_to.getData()).getColumn(0)).getData();
|
|
|
|
typename ColumnFloat64::Container & val_to
|
|
|
|
= static_cast<ColumnFloat64 &>(static_cast<ColumnTuple &>(arr_to.getData()).getColumn(1)).getData();
|
|
|
|
ts_to.reserve(old_size + size);
|
|
|
|
val_to.reserve(old_size + size);
|
|
|
|
size_t i = 0;
|
|
|
|
while (i < this->data(place).result.size())
|
2019-05-15 16:16:25 +00:00
|
|
|
{
|
2019-05-15 16:27:44 +00:00
|
|
|
ts_to.push_back(this->data(place).result[i].first);
|
|
|
|
val_to.push_back(this->data(place).result[i].second);
|
|
|
|
i++;
|
2019-05-15 16:16:25 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-15 16:27:44 +00:00
|
|
|
}
|
2019-05-15 16:16:25 +00:00
|
|
|
|
2019-05-15 16:27:44 +00:00
|
|
|
bool allocatesMemoryInArena() const override { return true; }
|
2019-05-15 16:16:25 +00:00
|
|
|
|
2019-05-15 16:27:44 +00:00
|
|
|
const char * getHeaderFilePath() const override { return __FILE__; }
|
|
|
|
};
|
2019-05-15 16:16:25 +00:00
|
|
|
}
|