#pragma once #include #include #include #include namespace DB { template struct AggregateFunctionSumData { T sum; AggregateFunctionSumData() : sum(0) {} }; /// Считает сумму чисел. template class AggregateFunctionSum : public IUnaryAggregateFunction::Type>, AggregateFunctionSum > { public: String getName() const { return "sum"; } DataTypePtr getReturnType() const { return new typename DataTypeFromFieldType::Type>::Type; } void setArgument(const DataTypePtr & argument) { if (!argument->isNumeric()) throw Exception("Illegal type " + argument->getName() + " of argument for aggregate function " + getName(), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT); } void addOne(AggregateDataPtr place, const IColumn & column, size_t row_num) const { this->data(place).sum += static_cast &>(column).getData()[row_num]; } void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs) const { this->data(place).sum += this->data(rhs).sum; } void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const { writeBinary(this->data(place).sum, buf); } void deserializeMerge(AggregateDataPtr place, ReadBuffer & buf) const { typename NearestFieldType::Type tmp; readBinary(tmp, buf); this->data(place).sum += tmp; } void serializeText(ConstAggregateDataPtr place, WriteBuffer & buf) const { writeText(this->data(place).sum, buf); } void deserializeMergeText(AggregateDataPtr place, ReadBuffer & buf) const { typename NearestFieldType::Type tmp; readText(tmp, buf); this->data(place).sum += tmp; } void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const { static_cast::Type> &>(to).getData().push_back(this->data(place).sum); } }; }