2013-10-25 14:56:47 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
2015-03-01 01:06:49 +00:00
|
|
|
|
#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
|
|
|
|
|
{
|
|
|
|
|
|
2013-10-28 14:15:56 +00:00
|
|
|
|
|
2017-03-09 00:56:38 +00:00
|
|
|
|
/// For possible values for template parameters, see AggregateFunctionsMinMaxAny.h
|
2015-03-01 01:06:49 +00:00
|
|
|
|
template <typename ResultData, typename ValueData>
|
2013-10-28 14:15:56 +00:00
|
|
|
|
struct AggregateFunctionsArgMinMaxData
|
2013-10-25 14:56:47 +00:00
|
|
|
|
{
|
2015-09-22 21:16:00 +00:00
|
|
|
|
using ResultData_t = ResultData;
|
|
|
|
|
using ValueData_t = ValueData;
|
|
|
|
|
|
2017-03-09 00:56:38 +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>
|
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:
|
2013-10-28 14:15:56 +00:00
|
|
|
|
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
|
|
|
|
{
|
2013-10-28 14:15:56 +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
|
|
|
|
{
|
2013-10-28 14:15:56 +00:00
|
|
|
|
type_res = arguments[0];
|
|
|
|
|
type_val = arguments[1];
|
2013-10-25 14:56:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-19 22:30:40 +00:00
|
|
|
|
void addImpl(AggregateDataPtr place, const IColumn & column_arg, const IColumn & column_max, size_t row_num, Arena *) const
|
2013-10-25 14:56:47 +00:00
|
|
|
|
{
|
2015-11-21 19:52:31 +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
|
|
|
|
}
|
|
|
|
|
|
2016-09-23 23:33:17 +00:00
|
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena * arena) const override
|
2013-10-25 14:56:47 +00:00
|
|
|
|
{
|
2015-03-01 01:06:49 +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
|
|
|
|
{
|
2015-03-01 01:06:49 +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-09-22 23:26:08 +00:00
|
|
|
|
void deserialize(AggregateDataPtr place, ReadBuffer & buf, Arena *) 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
|
|
|
|
{
|
2015-03-01 01:06:49 +00:00
|
|
|
|
this->data(place).result.insertResultInto(to);
|
2013-10-25 14:56:47 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|