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

160 lines
3.5 KiB
C
Raw Normal View History

2013-10-25 14:56:47 +00:00
#pragma once
#include <DB/IO/WriteHelpers.h>
#include <DB/IO/ReadHelpers.h>
#include <DB/AggregateFunctions/IAggregateFunction.h>
namespace DB
{
struct AggregateFunctionArgMinTraits
2013-10-25 14:56:47 +00:00
{
static bool better(const Field & lhs, const Field & rhs) { return lhs < rhs; }
static String name() { return "argMin"; }
2013-10-25 14:56:47 +00:00
};
struct AggregateFunctionArgMaxTraits
2013-10-25 14:56:47 +00:00
{
static bool better(const Field & lhs, const Field & rhs) { return lhs > rhs; }
static String name() { return "argMax"; }
2013-10-25 14:56:47 +00:00
};
struct AggregateFunctionsArgMinMaxData
2013-10-25 14:56:47 +00:00
{
2014-02-01 01:40:53 +00:00
Field result; // аргумент, при котором достигается минимальное/максимальное значение value.
Field value; // значение, для которого считается минимум/максимум.
2013-10-25 14:56:47 +00:00
};
2014-02-01 01:40:53 +00:00
/// Возвращает первое попавшееся значение arg для минимального/максимального value. Пример: argMax(arg, value).
2013-10-25 14:56:47 +00:00
template <typename Traits>
class AggregateFunctionsArgMinMax final : public IAggregateFunctionHelper<AggregateFunctionsArgMinMaxData>
2013-10-25 14:56:47 +00:00
{
private:
DataTypePtr type_res;
DataTypePtr type_val;
2013-10-25 14:56:47 +00:00
public:
String getName() const { return Traits::name(); }
DataTypePtr getReturnType() const
{
return type_res;
2013-10-25 14:56:47 +00:00
}
void setArguments(const DataTypes & arguments)
{
if (arguments.size() != 2)
throw Exception("Aggregate function " + getName() + " requires exactly two arguments.", ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
type_res = arguments[0];
type_val = arguments[1];
2013-10-25 14:56:47 +00:00
}
void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num) const
{
2014-02-01 01:40:53 +00:00
Field result;
Field value;
2013-10-25 14:56:47 +00:00
columns[0]->get(row_num, result);
columns[1]->get(row_num, value);
Data & d = data(place);
if (!d.value.isNull())
{
if (Traits::better(value, d.value))
{
2013-10-25 14:56:47 +00:00
d.result = result;
2014-02-01 01:40:53 +00:00
d.value = value;
2013-10-25 14:56:47 +00:00
}
2014-02-01 01:40:53 +00:00
}
else
{
2013-10-25 14:56:47 +00:00
d.result = result;
2014-02-01 01:40:53 +00:00
d.value = value;
2013-10-25 14:56:47 +00:00
}
}
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs) const
{
Data & d = data(place);
const Data & d_rhs = data(rhs);
if (!d.value.isNull())
{
if (Traits::better(d_rhs.value, d.value))
{
2013-10-25 14:56:47 +00:00
d.result = d_rhs.result;
2014-02-01 01:40:53 +00:00
d.value = d_rhs.value;
2013-10-25 14:56:47 +00:00
}
2014-02-01 01:40:53 +00:00
}
else
{
2013-10-25 14:56:47 +00:00
d.result = d_rhs.result;
2014-02-01 01:40:53 +00:00
d.value = d_rhs.value;
2013-10-25 14:56:47 +00:00
}
}
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const
{
const Data & d = data(place);
if (unlikely(d.result.isNull()))
{
writeBinary(false, buf);
}
else
{
writeBinary(true, buf);
type_res->serializeBinary(d.result, buf);
type_val->serializeBinary(d.value, buf);
}
2013-10-25 14:56:47 +00:00
}
void deserializeMerge(AggregateDataPtr place, ReadBuffer & buf) const
{
Data & d = data(place);
bool is_not_null = false;
readBinary(is_not_null, buf);
if (is_not_null)
2013-10-25 14:56:47 +00:00
{
if (!d.value.isNull())
{
Field result_;
Field value_;
2014-02-01 01:40:53 +00:00
type_res->deserializeBinary(result_, buf);
type_val->deserializeBinary(value_, buf);
2014-02-01 01:40:53 +00:00
if (Traits::better(value_, d.value))
{
d.result = result_;
d.value = value_;
}
}
else
{
type_res->deserializeBinary(d.result, buf);
type_val->deserializeBinary(d.value, buf);
2013-10-25 14:56:47 +00:00
}
2014-02-01 01:40:53 +00:00
}
2013-10-25 14:56:47 +00:00
}
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const
{
if (unlikely(data(place).value.isNull()))
to.insertDefault();
else
to.insert(data(place).result);
}
};
typedef AggregateFunctionsArgMinMax<AggregateFunctionArgMinTraits> AggregateFunctionArgMin;
typedef AggregateFunctionsArgMinMax<AggregateFunctionArgMaxTraits> AggregateFunctionArgMax;
2013-10-25 14:56:47 +00:00
}