2015-11-23 21:33:43 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <DB/Core/Field.h>
|
|
|
|
#include <DB/Core/FieldVisitors.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-02-07 21:26:32 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
}
|
2015-11-23 21:33:43 +00:00
|
|
|
|
2017-03-09 00:56:38 +00:00
|
|
|
/** Parameters of different functions quantiles*.
|
|
|
|
* - list of levels of quantiles.
|
|
|
|
* It is also necessary to calculate an array of indices of levels that go in ascending order.
|
2015-11-23 21:33:43 +00:00
|
|
|
*
|
2017-03-09 00:56:38 +00:00
|
|
|
* Example: quantiles(0.5, 0.99, 0.95)(x).
|
2015-11-23 21:33:43 +00:00
|
|
|
* levels: 0.5, 0.99, 0.95
|
|
|
|
* levels_permutation: 0, 2, 1
|
|
|
|
*/
|
2017-03-09 00:56:38 +00:00
|
|
|
template <typename T> /// float or double
|
2015-11-23 21:33:43 +00:00
|
|
|
struct QuantileLevels
|
|
|
|
{
|
|
|
|
using Levels = std::vector<T>;
|
|
|
|
using Permutation = std::vector<size_t>;
|
|
|
|
|
|
|
|
Levels levels;
|
2017-03-09 00:56:38 +00:00
|
|
|
Permutation permutation; /// Index of the i-th level in `levels`.
|
2015-11-23 21:33:43 +00:00
|
|
|
|
|
|
|
size_t size() const { return levels.size(); }
|
|
|
|
|
|
|
|
void set(const Array & params)
|
|
|
|
{
|
|
|
|
if (params.empty())
|
|
|
|
throw Exception("Aggregate function quantiles requires at least one parameter.", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
size_t size = params.size();
|
|
|
|
levels.resize(size);
|
|
|
|
permutation.resize(size);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i)
|
|
|
|
{
|
2017-01-06 17:41:19 +00:00
|
|
|
levels[i] = applyVisitor(FieldVisitorConvertToNumber<Float64>(), params[i]);
|
2015-11-23 21:33:43 +00:00
|
|
|
permutation[i] = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::sort(permutation.begin(), permutation.end(), [this] (size_t a, size_t b) { return levels[a] < levels[b]; });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|