ClickHouse/dbms/src/AggregateFunctions/QuantilesCommon.h

67 lines
1.7 KiB
C++
Raw Normal View History

#pragma once
#include <vector>
#include <Core/Field.h>
#include <Common/FieldVisitors.h>
namespace DB
{
namespace ErrorCodes
{
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
}
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.
*
2017-03-09 00:56:38 +00:00
* Example: quantiles(0.5, 0.99, 0.95)(x).
* levels: 0.5, 0.99, 0.95
* levels_permutation: 0, 2, 1
*/
template <typename T> /// float or double
struct QuantileLevels
{
using Levels = std::vector<T>;
using Permutation = std::vector<size_t>;
Levels levels;
Permutation permutation; /// Index of the i-th level in `levels`.
size_t size() const { return levels.size(); }
QuantileLevels(const Array & params, bool require_at_least_one_param)
{
if (params.empty())
{
if (require_at_least_one_param)
throw Exception("Aggregate function for calculation of multiple quantiles require at least one parameter",
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
/// If levels are not specified, default is 0.5 (median).
levels.push_back(0.5);
permutation.push_back(0);
return;
}
size_t size = params.size();
levels.resize(size);
permutation.resize(size);
for (size_t i = 0; i < size; ++i)
{
levels[i] = applyVisitor(FieldVisitorConvertToNumber<Float64>(), params[i]);
permutation[i] = i;
}
std::sort(permutation.begin(), permutation.end(), [this] (size_t a, size_t b) { return levels[a] < levels[b]; });
}
};
}