ClickHouse/dbms/include/DB/AggregateFunctions/AggregateFunctionsArgMinMax.h

75 lines
2.3 KiB
C++
Raw Normal View History

2013-10-25 14:56:47 +00:00
#pragma once
#include <DB/AggregateFunctions/AggregateFunctionsMinMaxAny.h>
2015-11-21 18:56:54 +00:00
#include <DB/AggregateFunctions/IBinaryAggregateFunction.h>
2013-10-25 14:56:47 +00:00
namespace DB
{
/// Возможные значения параметров шаблонов см. в AggregateFunctionsMinMaxAny.h
template <typename ResultData, typename ValueData>
struct AggregateFunctionsArgMinMaxData
2013-10-25 14:56:47 +00:00
{
using ResultData_t = ResultData;
using ValueData_t = ValueData;
ResultData result; // аргумент, при котором достигается минимальное/максимальное значение value.
ValueData value; // значение, для которого считается минимум/максимум.
2013-10-25 14:56:47 +00:00
};
2014-02-01 01:40:53 +00:00
/// Возвращает первое попавшееся значение arg для минимального/максимального value. Пример: argMax(arg, value).
template <typename Data>
2015-11-21 18:56:54 +00:00
class AggregateFunctionsArgMinMax final : public IBinaryAggregateFunction<Data, AggregateFunctionsArgMinMax<Data>>
2013-10-25 14:56:47 +00:00
{
private:
DataTypePtr type_res;
DataTypePtr type_val;
2013-10-25 14:56:47 +00:00
public:
2015-11-11 02:04:23 +00:00
String getName() const override { return (0 == strcmp(Data::ValueData_t::name(), "min")) ? "argMin" : "argMax"; }
2013-10-25 14:56:47 +00:00
2015-11-11 02:04:23 +00:00
DataTypePtr getReturnType() const override
2013-10-25 14:56:47 +00:00
{
return type_res;
2013-10-25 14:56:47 +00:00
}
2015-11-21 18:56:54 +00:00
void setArgumentsImpl(const DataTypes & arguments)
2013-10-25 14:56:47 +00:00
{
type_res = arguments[0];
type_val = arguments[1];
2013-10-25 14:56:47 +00:00
}
void addImpl(AggregateDataPtr place, const IColumn & column_arg, const IColumn & column_max, size_t row_num) const
2013-10-25 14:56:47 +00:00
{
if (this->data(place).value.changeIfBetter(column_max, row_num))
this->data(place).result.change(column_arg, row_num);
2013-10-25 14:56:47 +00:00
}
2015-11-11 02:04:23 +00:00
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs) const override
2013-10-25 14:56:47 +00:00
{
if (this->data(place).value.changeIfBetter(this->data(rhs).value))
this->data(place).result.change(this->data(rhs).result);
2013-10-25 14:56:47 +00:00
}
2015-11-11 02:04:23 +00:00
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
2013-10-25 14:56:47 +00:00
{
this->data(place).result.write(buf, *type_res.get());
this->data(place).value.write(buf, *type_val.get());
2013-10-25 14:56:47 +00:00
}
2016-03-12 04:01:03 +00:00
void deserialize(AggregateDataPtr place, ReadBuffer & buf) const override
2013-10-25 14:56:47 +00:00
{
2016-03-12 04:01:03 +00:00
this->data(place).result.read(buf, *type_res.get());
this->data(place).value.read(buf, *type_val.get());
2013-10-25 14:56:47 +00:00
}
2015-11-11 02:04:23 +00:00
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
2013-10-25 14:56:47 +00:00
{
this->data(place).result.insertResultInto(to);
2013-10-25 14:56:47 +00:00
}
};
}