ClickHouse
125 строк · 3.5 Кб
1#include <memory>2#include <random>3
4#include <DataTypes/DataTypesNumber.h>5#include <Common/thread_local_rng.h>6#include <IO/ReadBuffer.h>7#include <IO/WriteBuffer.h>8#include <AggregateFunctions/IAggregateFunction.h>9#include <AggregateFunctions/AggregateFunctionFactory.h>10
11
12namespace DB13{
14struct Settings;15
16namespace ErrorCodes17{
18extern const int AGGREGATE_FUNCTION_THROW;19extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;20}
21
22namespace
23{
24
25struct AggregateFunctionThrowData26{
27bool allocated;28
29AggregateFunctionThrowData() : allocated(true) {}30~AggregateFunctionThrowData()31{32volatile bool * allocated_ptr = &allocated;33
34if (*allocated_ptr)35*allocated_ptr = false;36else37abort();38}39};40
41/** Throw on creation with probability specified in parameter.
42* It will check correct destruction of the state.
43* This is intended to check for exception safety.
44*/
45class AggregateFunctionThrow final : public IAggregateFunctionDataHelper<AggregateFunctionThrowData, AggregateFunctionThrow>46{
47private:48Float64 throw_probability;49
50public:51AggregateFunctionThrow(const DataTypes & argument_types_, const Array & parameters_, Float64 throw_probability_)52: IAggregateFunctionDataHelper(argument_types_, parameters_, createResultType())53, throw_probability(throw_probability_)54{}55
56String getName() const override57{58return "aggThrow";59}60
61static DataTypePtr createResultType()62{63return std::make_shared<DataTypeUInt8>();64}65
66bool allocatesMemoryInArena() const override { return false; }67
68void create(AggregateDataPtr __restrict place) const override69{70if (std::uniform_real_distribution<>(0.0, 1.0)(thread_local_rng) <= throw_probability)71throw Exception(ErrorCodes::AGGREGATE_FUNCTION_THROW, "Aggregate function {} has thrown exception successfully", getName());72
73new (place) Data;74}75
76void destroy(AggregateDataPtr __restrict place) const noexcept override77{78data(place).~Data();79}80
81bool hasTrivialDestructor() const override { return std::is_trivially_destructible_v<Data>; }82
83void add(AggregateDataPtr __restrict, const IColumn **, size_t, Arena *) const override84{85}86
87void merge(AggregateDataPtr __restrict, ConstAggregateDataPtr, Arena *) const override88{89}90
91void serialize(ConstAggregateDataPtr __restrict, WriteBuffer & buf, std::optional<size_t> /* version */) const override92{93char c = 0;94buf.write(c);95}96
97void deserialize(AggregateDataPtr __restrict /* place */, ReadBuffer & buf, std::optional<size_t> /* version */, Arena *) const override98{99char c = 0;100buf.readStrict(c);101}102
103void insertResultInto(AggregateDataPtr __restrict, IColumn & to, Arena *) const override104{105to.insertDefault();106}107};108
109}
110
111void registerAggregateFunctionAggThrow(AggregateFunctionFactory & factory)112{
113factory.registerFunction("aggThrow", [](const std::string & name, const DataTypes & argument_types, const Array & parameters, const Settings *)114{115Float64 throw_probability = 1.0;116if (parameters.size() == 1)117throw_probability = parameters[0].safeGet<Float64>();118else if (parameters.size() > 1)119throw Exception(ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH, "Aggregate function {} cannot have more than one parameter", name);120
121return std::make_shared<AggregateFunctionThrow>(argument_types, parameters, throw_probability);122});123}
124
125}
126