2016-07-12 13:02:52 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <DB/AggregateFunctions/IAggregateFunction.h>
|
|
|
|
#include <DB/Columns/ColumnNullable.h>
|
|
|
|
#include <DB/DataTypes/DataTypeNullable.h>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
|
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-10 19:12:29 +00:00
|
|
|
/// This class implements a wrapper around an aggregate function. Despite its name,
|
|
|
|
/// this is an adapter. It is used to handle aggregate functions that are called with
|
|
|
|
/// at least one nullable argument. It implements the logic according to which any
|
|
|
|
/// row that contains at least one NULL is skipped.
|
2016-07-12 13:02:52 +00:00
|
|
|
class AggregateFunctionNull : public IAggregateFunction
|
|
|
|
{
|
|
|
|
public:
|
2016-07-12 13:07:39 +00:00
|
|
|
AggregateFunctionNull(AggregateFunctionPtr nested_function_)
|
|
|
|
: nested_function{nested_function_}
|
2016-07-12 13:02:52 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
2016-08-15 11:14:29 +00:00
|
|
|
return nested_function->getName();
|
2016-07-12 13:02:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void setArguments(const DataTypes & arguments) override
|
|
|
|
{
|
|
|
|
argument_count = arguments.size();
|
|
|
|
is_nullable.reserve(arguments.size());
|
|
|
|
|
|
|
|
for (const auto & arg : arguments)
|
|
|
|
{
|
2016-08-15 11:14:29 +00:00
|
|
|
bool res = arg->isNullable();
|
2016-07-12 13:02:52 +00:00
|
|
|
is_nullable.push_back(res);
|
|
|
|
}
|
|
|
|
|
2016-08-10 19:12:29 +00:00
|
|
|
DataTypes new_args;
|
|
|
|
new_args.reserve(arguments.size());
|
2016-07-12 13:02:52 +00:00
|
|
|
|
2016-08-10 19:12:29 +00:00
|
|
|
for (const auto & arg : arguments)
|
|
|
|
{
|
2016-08-15 11:14:29 +00:00
|
|
|
if (arg->isNullable())
|
2016-07-12 13:02:52 +00:00
|
|
|
{
|
2016-08-10 19:12:29 +00:00
|
|
|
const DataTypeNullable & nullable_type = static_cast<const DataTypeNullable &>(*arg);
|
|
|
|
const DataTypePtr & nested_type = nullable_type.getNestedType();
|
|
|
|
new_args.push_back(nested_type);
|
2016-07-12 13:02:52 +00:00
|
|
|
}
|
2016-08-10 19:12:29 +00:00
|
|
|
else
|
|
|
|
new_args.push_back(arg);
|
2016-07-12 13:02:52 +00:00
|
|
|
}
|
2016-08-10 19:12:29 +00:00
|
|
|
|
2016-08-15 11:14:29 +00:00
|
|
|
nested_function->setArguments(new_args);
|
2016-07-12 13:02:52 +00:00
|
|
|
}
|
|
|
|
|
2016-09-11 10:06:34 +00:00
|
|
|
void setParameters(const Array & params) override
|
2016-07-12 13:02:52 +00:00
|
|
|
{
|
2016-08-15 11:14:29 +00:00
|
|
|
nested_function->setParameters(params);
|
2016-07-12 13:02:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DataTypePtr getReturnType() const override
|
|
|
|
{
|
2016-08-15 11:14:29 +00:00
|
|
|
return nested_function->getReturnType();
|
2016-07-12 13:02:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void create(AggregateDataPtr place) const override
|
|
|
|
{
|
2016-08-15 11:14:29 +00:00
|
|
|
nested_function->create(place);
|
2016-07-12 13:02:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void destroy(AggregateDataPtr place) const noexcept override
|
|
|
|
{
|
2016-08-15 11:14:29 +00:00
|
|
|
nested_function->destroy(place);
|
2016-07-12 13:02:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool hasTrivialDestructor() const override
|
|
|
|
{
|
2016-08-15 11:14:29 +00:00
|
|
|
return nested_function->hasTrivialDestructor();
|
2016-07-12 13:02:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t sizeOfData() const override
|
|
|
|
{
|
2016-08-15 11:14:29 +00:00
|
|
|
return nested_function->sizeOfData();
|
2016-07-12 13:02:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t alignOfData() const override
|
|
|
|
{
|
2016-08-15 11:14:29 +00:00
|
|
|
return nested_function->alignOfData();
|
2016-07-12 13:02:52 +00:00
|
|
|
}
|
|
|
|
|
2016-10-19 15:00:56 +00:00
|
|
|
void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena * arena) const override
|
2016-07-12 13:02:52 +00:00
|
|
|
{
|
|
|
|
/// This container stores the columns we really pass to the nested function.
|
2016-08-10 19:12:29 +00:00
|
|
|
const IColumn * passed_columns[argument_count];
|
2016-07-12 13:02:52 +00:00
|
|
|
|
2016-08-10 19:12:29 +00:00
|
|
|
for (size_t i = 0; i < argument_count; ++i)
|
2016-07-12 13:02:52 +00:00
|
|
|
{
|
2016-08-10 19:12:29 +00:00
|
|
|
if (is_nullable[i])
|
2016-07-12 13:02:52 +00:00
|
|
|
{
|
2016-08-10 19:12:29 +00:00
|
|
|
const ColumnNullable & nullable_col = static_cast<const ColumnNullable &>(*columns[i]);
|
|
|
|
if (nullable_col.isNullAt(row_num))
|
2016-07-12 13:02:52 +00:00
|
|
|
{
|
2016-08-10 19:12:29 +00:00
|
|
|
/// If at least one column has a null value in the current row,
|
|
|
|
/// we don't process this row.
|
|
|
|
return;
|
2016-07-12 13:02:52 +00:00
|
|
|
}
|
2016-08-10 19:12:29 +00:00
|
|
|
passed_columns[i] = nullable_col.getNestedColumn().get();
|
2016-07-12 13:02:52 +00:00
|
|
|
}
|
2016-08-10 19:12:29 +00:00
|
|
|
else
|
|
|
|
passed_columns[i] = columns[i];
|
2016-07-12 13:02:52 +00:00
|
|
|
}
|
2016-08-10 19:12:29 +00:00
|
|
|
|
2016-10-19 15:00:56 +00:00
|
|
|
nested_function->add(place, passed_columns, row_num, arena);
|
2016-07-12 13:02:52 +00:00
|
|
|
}
|
|
|
|
|
2016-10-19 15:00:56 +00:00
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena * arena) const override
|
2016-07-12 13:02:52 +00:00
|
|
|
{
|
2016-10-19 15:00:56 +00:00
|
|
|
nested_function->merge(place, rhs, arena);
|
2016-07-12 13:02:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
|
|
|
|
{
|
2016-08-15 11:14:29 +00:00
|
|
|
nested_function->serialize(place, buf);
|
2016-07-12 13:02:52 +00:00
|
|
|
}
|
|
|
|
|
2016-10-19 15:00:56 +00:00
|
|
|
void deserialize(AggregateDataPtr place, ReadBuffer & buf, Arena * arena) const override
|
2016-07-12 13:02:52 +00:00
|
|
|
{
|
2016-10-19 15:00:56 +00:00
|
|
|
nested_function->deserialize(place, buf, arena);
|
2016-07-12 13:02:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
|
|
|
|
{
|
2016-08-15 11:14:29 +00:00
|
|
|
nested_function->insertResultInto(place, to);
|
2016-07-12 13:02:52 +00:00
|
|
|
}
|
|
|
|
|
2016-10-19 15:00:56 +00:00
|
|
|
static void addFree(const IAggregateFunction * that, AggregateDataPtr place,
|
|
|
|
const IColumn ** columns, size_t row_num, Arena * arena)
|
2016-07-12 13:02:52 +00:00
|
|
|
{
|
2016-10-19 15:00:56 +00:00
|
|
|
return static_cast<const AggregateFunctionNull &>(*that).add(place, columns, row_num, arena);
|
2016-07-12 13:02:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AddFunc getAddressOfAddFunction() const override
|
|
|
|
{
|
|
|
|
return &addFree;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-07-12 13:07:39 +00:00
|
|
|
AggregateFunctionPtr nested_function;
|
2016-07-12 13:02:52 +00:00
|
|
|
std::vector<bool> is_nullable;
|
|
|
|
size_t argument_count = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|