2013-10-25 14:56:47 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-12-20 07:36:30 +00:00
|
|
|
#include <common/StringRef.h>
|
|
|
|
#include <DataTypes/IDataType.h>
|
|
|
|
#include <AggregateFunctions/IAggregateFunction.h>
|
2017-12-27 19:26:18 +00:00
|
|
|
#include <AggregateFunctions/AggregateFunctionMinMaxAny.h> // SingleValueDataString used in embedded compiler
|
2013-10-25 14:56:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-12-20 07:36:30 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
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.
|
2019-12-26 18:54:37 +00:00
|
|
|
|
|
|
|
static bool allocatesMemoryInArena()
|
|
|
|
{
|
|
|
|
return ResultData::allocatesMemoryInArena() || ValueData::allocatesMemoryInArena();
|
|
|
|
}
|
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).
|
2019-12-26 18:54:37 +00:00
|
|
|
template <typename Data>
|
|
|
|
class AggregateFunctionArgMinMax final : public IAggregateFunctionDataHelper<Data, AggregateFunctionArgMinMax<Data>>
|
2013-10-25 14:56:47 +00:00
|
|
|
{
|
|
|
|
private:
|
2019-02-11 19:26:32 +00:00
|
|
|
const DataTypePtr & type_res;
|
|
|
|
const DataTypePtr & type_val;
|
2013-10-25 14:56:47 +00:00
|
|
|
|
|
|
|
public:
|
2019-08-03 11:02:40 +00:00
|
|
|
AggregateFunctionArgMinMax(const DataTypePtr & type_res_, const DataTypePtr & type_val_)
|
2019-12-26 18:54:37 +00:00
|
|
|
: IAggregateFunctionDataHelper<Data, AggregateFunctionArgMinMax<Data>>({type_res_, type_val_}, {}),
|
2019-02-12 09:31:20 +00:00
|
|
|
type_res(this->argument_types[0]), type_val(this->argument_types[1])
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-20 07:36:30 +00:00
|
|
|
if (!type_val->isComparable())
|
|
|
|
throw Exception("Illegal type " + type_val->getName() + " of second argument of aggregate function " + getName()
|
|
|
|
+ " because the values of that data type are not comparable", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-12-20 07:36:30 +00:00
|
|
|
String getName() const override { return StringRef(Data::ValueData_t::name()) == StringRef("min") ? "argMin" : "argMax"; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnType() const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-20 07:36:30 +00:00
|
|
|
return type_res;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena * arena) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
if (this->data(place).value.changeIfBetter(*columns[1], row_num, arena))
|
|
|
|
this->data(place).result.change(*columns[0], row_num, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena * arena) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
if (this->data(place).value.changeIfBetter(this->data(rhs).value, arena))
|
|
|
|
this->data(place).result.change(this->data(rhs).result, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
|
|
|
|
{
|
2017-12-20 07:36:30 +00:00
|
|
|
this->data(place).result.write(buf, *type_res);
|
|
|
|
this->data(place).value.write(buf, *type_val);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2017-12-26 17:58:59 +00:00
|
|
|
void deserialize(AggregateDataPtr place, ReadBuffer & buf, Arena * arena) const override
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-26 17:58:59 +00:00
|
|
|
this->data(place).result.read(buf, *type_res, arena);
|
|
|
|
this->data(place).value.read(buf, *type_val, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2019-05-18 14:15:44 +00:00
|
|
|
bool allocatesMemoryInArena() const override
|
|
|
|
{
|
2019-12-26 18:54:37 +00:00
|
|
|
return Data::allocatesMemoryInArena();
|
2019-05-18 14:15:44 +00:00
|
|
|
}
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
|
|
|
|
{
|
|
|
|
this->data(place).result.insertResultInto(to);
|
|
|
|
}
|
2013-10-25 14:56:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|