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
|
|
|
|
{
|
|
|
|
|
2013-02-08 19:34:44 +00:00
|
|
|
|
|
|
|
struct AggregateFunctionAnyData
|
|
|
|
{
|
|
|
|
Field value;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-09-26 04:00:46 +00:00
|
|
|
/// Берёт первое попавшееся значение
|
2013-02-08 19:34:44 +00:00
|
|
|
class AggregateFunctionAny : public IUnaryAggregateFunction<AggregateFunctionAnyData>
|
2011-09-26 04:00:46 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
DataTypePtr type;
|
|
|
|
|
|
|
|
public:
|
|
|
|
String getName() const { return "any"; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnType() const
|
|
|
|
{
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setArgument(const DataTypePtr & argument)
|
|
|
|
{
|
|
|
|
type = argument;
|
|
|
|
}
|
|
|
|
|
2013-02-08 19:34:44 +00:00
|
|
|
|
2013-06-25 08:06:40 +00:00
|
|
|
void addOne(AggregateDataPtr place, const IColumn & column, size_t row_num) const
|
2011-09-26 04:00:46 +00:00
|
|
|
{
|
2013-02-08 19:34:44 +00:00
|
|
|
Data & d = data(place);
|
|
|
|
|
|
|
|
if (!d.value.isNull())
|
2011-09-26 04:00:46 +00:00
|
|
|
return;
|
2013-06-25 08:06:40 +00:00
|
|
|
column.get(row_num, d.value);
|
2011-09-26 04:00:46 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 19:34:44 +00:00
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs) const
|
2011-09-26 04:00:46 +00:00
|
|
|
{
|
2013-02-08 19:34:44 +00:00
|
|
|
Data & d = data(place);
|
|
|
|
|
|
|
|
if (d.value.isNull())
|
|
|
|
d.value = data(rhs).value;
|
2011-09-26 04:00:46 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 19:34:44 +00:00
|
|
|
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const
|
2011-09-26 04:00:46 +00:00
|
|
|
{
|
2013-02-08 19:34:44 +00:00
|
|
|
type->serializeBinary(data(place).value, buf);
|
2011-09-26 04:00:46 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 19:34:44 +00:00
|
|
|
void deserializeMerge(AggregateDataPtr place, ReadBuffer & buf) const
|
2011-09-26 04:00:46 +00:00
|
|
|
{
|
2013-02-08 19:34:44 +00:00
|
|
|
Data & d = data(place);
|
|
|
|
|
2012-10-24 19:09:42 +00:00
|
|
|
Field tmp;
|
|
|
|
type->deserializeBinary(tmp, buf);
|
2013-02-08 19:34:44 +00:00
|
|
|
|
|
|
|
if (d.value.isNull())
|
|
|
|
d.value = tmp;
|
2011-09-26 04:00:46 +00:00
|
|
|
}
|
|
|
|
|
2013-06-30 11:38:46 +00:00
|
|
|
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const
|
2011-09-26 04:00:46 +00:00
|
|
|
{
|
2013-06-30 11:38:46 +00:00
|
|
|
to.insert(data(place).value);
|
2011-09-26 04:00:46 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|