#pragma once #include #include #include #include #include namespace DB { template struct AggregateFunctionSumData { T sum{}; }; /// Counts the sum of the numbers. template class AggregateFunctionSum final : public IUnaryAggregateFunction, AggregateFunctionSum> { public: String getName() const override { return "sum"; } DataTypePtr getReturnType() const override { return std::make_shared>(); } void setArgument(const DataTypePtr & argument) { if (!argument->isSummable()) throw Exception("Illegal type " + argument->getName() + " of argument for aggregate function " + getName(), ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT); } void addImpl(AggregateDataPtr place, const IColumn & column, size_t row_num, Arena *) const { this->data(place).sum += static_cast &>(column).getData()[row_num]; } void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena *) const override { this->data(place).sum += this->data(rhs).sum; } void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override { writeBinary(this->data(place).sum, buf); } void deserialize(AggregateDataPtr place, ReadBuffer & buf, Arena *) const override { readBinary(this->data(place).sum, buf); } void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override { static_cast &>(to).getData().push_back(this->data(place).sum); } const char * getHeaderFilePath() const override { return __FILE__; } }; }