2013-10-25 14:56:47 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-07-12 01:16:01 +00:00
|
|
|
#include <AggregateFunctions/AggregateFunctionMinMaxAny.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <AggregateFunctions/IBinaryAggregateFunction.h>
|
2013-10-25 14:56:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2013-10-28 14:15:56 +00:00
|
|
|
|
2017-07-12 01:16:01 +00:00
|
|
|
/// For possible values for template parameters, see AggregateFunctionMinMaxAny.h
|
2015-03-01 01:06:49 +00:00
|
|
|
template <typename ResultData, typename ValueData>
|
2017-07-12 01:16:01 +00:00
|
|
|
struct AggregateFunctionArgMinMaxData
|
2013-10-25 14:56:47 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
using ResultData_t = ResultData;
|
|
|
|
using ValueData_t = ValueData;
|
2015-09-22 21:16:00 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
ResultData result; // the argument at which the minimum/maximum value is reached.
|
|
|
|
ValueData value; // value for which the minimum/maximum is calculated.
|
2013-10-25 14:56:47 +00:00
|
|
|
};
|
|
|
|
|
2017-03-09 00:56:38 +00:00
|
|
|
/// Returns the first arg value found for the minimum/maximum value. Example: argMax(arg, value).
|
2015-03-01 01:06:49 +00:00
|
|
|
template <typename Data>
|
2017-07-12 01:16:01 +00:00
|
|
|
class AggregateFunctionArgMinMax final : public IBinaryAggregateFunction<Data, AggregateFunctionArgMinMax<Data>>
|
2013-10-25 14:56:47 +00:00
|
|
|
{
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
DataTypePtr type_res;
|
|
|
|
DataTypePtr type_val;
|
2013-10-25 14:56:47 +00:00
|
|
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
String getName() const override { return (0 == strcmp(Data::ValueData_t::name(), "min")) ? "argMin" : "argMax"; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnType() const override
|
|
|
|
{
|
|
|
|
return type_res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setArgumentsImpl(const DataTypes & arguments)
|
|
|
|
{
|
|
|
|
type_res = arguments[0];
|
|
|
|
type_val = arguments[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
void addImpl(AggregateDataPtr place, const IColumn & column_arg, const IColumn & column_max, size_t row_num, Arena *) const
|
|
|
|
{
|
|
|
|
if (this->data(place).value.changeIfBetter(column_max, row_num))
|
|
|
|
this->data(place).result.change(column_arg, row_num);
|
|
|
|
}
|
|
|
|
|
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena * arena) const override
|
|
|
|
{
|
|
|
|
if (this->data(place).value.changeIfBetter(this->data(rhs).value))
|
|
|
|
this->data(place).result.change(this->data(rhs).result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
|
|
|
|
{
|
|
|
|
this->data(place).result.write(buf, *type_res.get());
|
|
|
|
this->data(place).value.write(buf, *type_val.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
void deserialize(AggregateDataPtr place, ReadBuffer & buf, Arena *) const override
|
|
|
|
{
|
|
|
|
this->data(place).result.read(buf, *type_res.get());
|
|
|
|
this->data(place).value.read(buf, *type_val.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
|
|
|
|
{
|
|
|
|
this->data(place).result.insertResultInto(to);
|
|
|
|
}
|
2013-10-25 14:56:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|