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

78 lines
1.2 KiB
C
Raw Normal View History

2011-09-26 04:00:46 +00:00
#pragma once
#include <DB/IO/WriteHelpers.h>
#include <DB/IO/ReadHelpers.h>
#include <DB/AggregateFunctions/IUnaryAggregateFunction.h>
namespace DB
{
/// Берёт первое попавшееся значение
class AggregateFunctionAny : public IUnaryAggregateFunction
{
private:
bool got;
Field value;
DataTypePtr type;
public:
AggregateFunctionAny() : got(false) {}
String getName() const { return "any"; }
String getTypeID() const { return "any"; }
2011-12-19 08:06:31 +00:00
AggregateFunctionPlainPtr cloneEmpty() const
2011-09-26 04:00:46 +00:00
{
AggregateFunctionAny * res = new AggregateFunctionAny;
res->type = type;
return res;
}
DataTypePtr getReturnType() const
{
return type;
}
void setArgument(const DataTypePtr & argument)
{
type = argument;
}
void addOne(const Field & value_)
{
if (got)
return;
got = true;
value = value_;
}
void merge(const IAggregateFunction & rhs)
{
if (!got)
2012-09-03 06:32:38 +00:00
value = static_cast<const AggregateFunctionAny &>(rhs).value;
2011-09-26 04:00:46 +00:00
}
void serialize(WriteBuffer & buf) const
{
type->serializeBinary(value, buf);
}
void deserializeMerge(ReadBuffer & buf)
{
2012-10-24 19:09:42 +00:00
Field tmp;
type->deserializeBinary(tmp, buf);
2011-09-26 04:00:46 +00:00
if (!got)
2012-10-24 19:09:42 +00:00
value = tmp;
2011-09-26 04:00:46 +00:00
}
Field getResult() const
{
return value;
}
};
}