2016-07-12 13:02:52 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-02-12 11:40:47 +00:00
|
|
|
#include <array>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <AggregateFunctions/IAggregateFunction.h>
|
|
|
|
#include <Columns/ColumnNullable.h>
|
|
|
|
#include <DataTypes/DataTypeNullable.h>
|
|
|
|
#include <IO/ReadHelpers.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
2017-02-10 05:03:42 +00:00
|
|
|
|
2016-07-12 13:02:52 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int LOGICAL_ERROR;
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
2016-07-12 13:02:52 +00:00
|
|
|
}
|
|
|
|
|
2017-02-10 05:03:42 +00:00
|
|
|
|
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.
|
2017-02-10 05:03:42 +00:00
|
|
|
|
2017-12-08 09:07:52 +00:00
|
|
|
/// If all rows had NULL, the behaviour is determined by "result_is_nullable" template parameter.
|
|
|
|
/// true - return NULL; false - return value from empty aggregation state of nested function.
|
|
|
|
|
2017-12-20 07:36:30 +00:00
|
|
|
template <bool result_is_nullable, typename Derived>
|
|
|
|
class AggregateFunctionNullBase : public IAggregateFunctionHelper<Derived>
|
2016-07-12 13:02:52 +00:00
|
|
|
{
|
2017-02-10 05:03:42 +00:00
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
AggregateFunctionPtr nested_function;
|
|
|
|
|
|
|
|
/** In addition to data for nested aggregate function, we keep a flag
|
|
|
|
* indicating - was there at least one non-NULL value accumulated.
|
|
|
|
* In case of no not-NULL values, the function will return NULL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static AggregateDataPtr nestedPlace(AggregateDataPtr place) noexcept
|
|
|
|
{
|
2017-12-08 09:07:52 +00:00
|
|
|
return place + (result_is_nullable ? 1 : 0);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static ConstAggregateDataPtr nestedPlace(ConstAggregateDataPtr place) noexcept
|
|
|
|
{
|
2017-12-08 09:07:52 +00:00
|
|
|
return place + (result_is_nullable ? 1 : 0);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void initFlag(AggregateDataPtr place) noexcept
|
|
|
|
{
|
2017-12-08 09:07:52 +00:00
|
|
|
if (result_is_nullable)
|
|
|
|
place[0] = 0;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void setFlag(AggregateDataPtr place) noexcept
|
|
|
|
{
|
2017-12-08 09:07:52 +00:00
|
|
|
if (result_is_nullable)
|
|
|
|
place[0] = 1;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool getFlag(ConstAggregateDataPtr place) noexcept
|
|
|
|
{
|
2017-12-08 09:07:52 +00:00
|
|
|
return result_is_nullable ? place[0] : 1;
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2016-07-12 13:02:52 +00:00
|
|
|
|
2017-02-10 05:03:42 +00:00
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
AggregateFunctionNullBase(AggregateFunctionPtr nested_function_)
|
|
|
|
: nested_function{nested_function_}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
/// This is just a wrapper. The function for Nullable arguments is named the same as the nested function itself.
|
|
|
|
return nested_function->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
DataTypePtr getReturnType() const override
|
|
|
|
{
|
2017-12-08 09:07:52 +00:00
|
|
|
return result_is_nullable
|
2017-12-10 22:44:04 +00:00
|
|
|
? makeNullable(nested_function->getReturnType())
|
2017-12-08 09:07:52 +00:00
|
|
|
: nested_function->getReturnType();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void create(AggregateDataPtr place) const override
|
|
|
|
{
|
|
|
|
initFlag(place);
|
|
|
|
nested_function->create(nestedPlace(place));
|
|
|
|
}
|
|
|
|
|
|
|
|
void destroy(AggregateDataPtr place) const noexcept override
|
|
|
|
{
|
|
|
|
nested_function->destroy(nestedPlace(place));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasTrivialDestructor() const override
|
|
|
|
{
|
|
|
|
return nested_function->hasTrivialDestructor();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t sizeOfData() const override
|
|
|
|
{
|
|
|
|
return 1 + nested_function->sizeOfData();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t alignOfData() const override
|
|
|
|
{
|
2017-12-20 07:36:30 +00:00
|
|
|
return 1; /// NOTE This works fine on x86_64 and ok on AArch64. Doesn't work under UBSan.
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void merge(AggregateDataPtr place, ConstAggregateDataPtr rhs, Arena * arena) const override
|
|
|
|
{
|
2017-12-08 09:07:52 +00:00
|
|
|
if (result_is_nullable && getFlag(rhs))
|
2017-04-01 07:20:54 +00:00
|
|
|
setFlag(place);
|
|
|
|
|
|
|
|
nested_function->merge(nestedPlace(place), nestedPlace(rhs), arena);
|
|
|
|
}
|
|
|
|
|
|
|
|
void serialize(ConstAggregateDataPtr place, WriteBuffer & buf) const override
|
|
|
|
{
|
|
|
|
bool flag = getFlag(place);
|
2017-12-08 09:07:52 +00:00
|
|
|
if (result_is_nullable)
|
|
|
|
writeBinary(flag, buf);
|
2017-04-01 07:20:54 +00:00
|
|
|
if (flag)
|
|
|
|
nested_function->serialize(nestedPlace(place), buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void deserialize(AggregateDataPtr place, ReadBuffer & buf, Arena * arena) const override
|
|
|
|
{
|
2017-12-08 09:07:52 +00:00
|
|
|
bool flag = 1;
|
|
|
|
if (result_is_nullable)
|
|
|
|
readBinary(flag, buf);
|
2017-04-01 07:20:54 +00:00
|
|
|
if (flag)
|
|
|
|
{
|
|
|
|
setFlag(place);
|
|
|
|
nested_function->deserialize(nestedPlace(place), buf, arena);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void insertResultInto(ConstAggregateDataPtr place, IColumn & to) const override
|
|
|
|
{
|
2017-12-08 09:07:52 +00:00
|
|
|
if (result_is_nullable)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-12-08 09:07:52 +00:00
|
|
|
ColumnNullable & to_concrete = static_cast<ColumnNullable &>(to);
|
|
|
|
if (getFlag(place))
|
|
|
|
{
|
2017-12-14 03:56:56 +00:00
|
|
|
nested_function->insertResultInto(nestedPlace(place), to_concrete.getNestedColumn());
|
|
|
|
to_concrete.getNullMapData().push_back(0);
|
2017-12-08 09:07:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
to_concrete.insertDefault();
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-08 09:07:52 +00:00
|
|
|
nested_function->insertResultInto(nestedPlace(place), to);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool allocatesMemoryInArena() const override
|
|
|
|
{
|
|
|
|
return nested_function->allocatesMemoryInArena();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isState() const override
|
|
|
|
{
|
|
|
|
return nested_function->isState();
|
|
|
|
}
|
2017-09-17 20:22:39 +00:00
|
|
|
|
|
|
|
const char * getHeaderFilePath() const override { return __FILE__; }
|
2017-02-10 05:03:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/** There are two cases: for single argument and variadic.
|
|
|
|
* Code for single argument is much more efficient.
|
|
|
|
*/
|
2017-12-08 09:07:52 +00:00
|
|
|
template <bool result_is_nullable>
|
2017-12-20 07:36:30 +00:00
|
|
|
class AggregateFunctionNullUnary final : public AggregateFunctionNullBase<result_is_nullable, AggregateFunctionNullUnary<result_is_nullable>>
|
2017-02-10 05:03:42 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-12-20 07:36:30 +00:00
|
|
|
AggregateFunctionNullUnary(AggregateFunctionPtr nested_function)
|
|
|
|
: AggregateFunctionNullBase(nested_function)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena * arena) const override
|
|
|
|
{
|
|
|
|
const ColumnNullable * column = static_cast<const ColumnNullable *>(columns[0]);
|
|
|
|
if (!column->isNullAt(row_num))
|
|
|
|
{
|
2017-12-08 09:07:52 +00:00
|
|
|
this->setFlag(place);
|
2017-12-14 03:56:56 +00:00
|
|
|
const IColumn * nested_column = &column->getNestedColumn();
|
2017-12-08 09:07:52 +00:00
|
|
|
this->nested_function->add(this->nestedPlace(place), &nested_column, row_num, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-10 05:03:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-12-08 09:07:52 +00:00
|
|
|
template <bool result_is_nullable>
|
2017-12-20 07:36:30 +00:00
|
|
|
class AggregateFunctionNullVariadic final : public AggregateFunctionNullBase<result_is_nullable, AggregateFunctionNullVariadic<result_is_nullable>>
|
2017-02-10 05:03:42 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-12-20 07:36:30 +00:00
|
|
|
AggregateFunctionNullVariadic(AggregateFunctionPtr nested_function, const size_t number_of_arguments)
|
|
|
|
: AggregateFunctionNullBase(nested_function), number_of_arguments(number_of_arguments)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void add(AggregateDataPtr place, const IColumn ** columns, size_t row_num, Arena * arena) const override
|
|
|
|
{
|
|
|
|
/// This container stores the columns we really pass to the nested function.
|
|
|
|
const IColumn * nested_columns[number_of_arguments];
|
|
|
|
|
|
|
|
for (size_t i = 0; i < number_of_arguments; ++i)
|
|
|
|
{
|
|
|
|
if (is_nullable[i])
|
|
|
|
{
|
|
|
|
const ColumnNullable & nullable_col = static_cast<const ColumnNullable &>(*columns[i]);
|
|
|
|
if (nullable_col.isNullAt(row_num))
|
|
|
|
{
|
|
|
|
/// If at least one column has a null value in the current row,
|
|
|
|
/// we don't process this row.
|
|
|
|
return;
|
|
|
|
}
|
2017-12-14 03:56:56 +00:00
|
|
|
nested_columns[i] = &nullable_col.getNestedColumn();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
nested_columns[i] = columns[i];
|
|
|
|
}
|
|
|
|
|
2017-12-08 09:07:52 +00:00
|
|
|
this->setFlag(place);
|
|
|
|
this->nested_function->add(this->nestedPlace(place), nested_columns, row_num, arena);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool allocatesMemoryInArena() const override
|
|
|
|
{
|
2017-12-08 09:07:52 +00:00
|
|
|
return this->nested_function->allocatesMemoryInArena();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
|
2016-07-12 13:02:52 +00:00
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
enum { MAX_ARGS = 8 };
|
|
|
|
size_t number_of_arguments = 0;
|
|
|
|
std::array<char, MAX_ARGS> is_nullable; /// Plain array is better than std::vector due to one indirection less.
|
2016-07-12 13:02:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|