2017-04-01 09:19:00 +00:00
|
|
|
#include <AggregateFunctions/AggregateFunctionFactory.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionGroupUniqArray.h>
|
|
|
|
#include <AggregateFunctions/Helpers.h>
|
2015-09-24 12:40:36 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-09-13 13:24:24 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2016-12-16 10:57:44 +00:00
|
|
|
/// Substitute return type for Date and DateTime
|
|
|
|
class AggregateFunctionGroupUniqArrayDate : public AggregateFunctionGroupUniqArray<DataTypeDate::FieldType>
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
DataTypePtr getReturnType() const override { return std::make_shared<DataTypeArray>(std::make_shared<DataTypeDate>()); }
|
2016-12-16 10:57:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class AggregateFunctionGroupUniqArrayDateTime : public AggregateFunctionGroupUniqArray<DataTypeDateTime::FieldType>
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
DataTypePtr getReturnType() const override { return std::make_shared<DataTypeArray>(std::make_shared<DataTypeDateTime>()); }
|
2016-12-16 10:57:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-09-13 13:24:24 +00:00
|
|
|
static IAggregateFunction * createWithExtraTypes(const IDataType & argument_type)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (typeid_cast<const DataTypeDate *>(&argument_type)) return new AggregateFunctionGroupUniqArrayDate;
|
|
|
|
else if (typeid_cast<const DataTypeDateTime *>(&argument_type)) return new AggregateFunctionGroupUniqArrayDateTime;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// Check that we can use plain version of AggreagteFunctionGroupUniqArrayGeneric
|
|
|
|
if (typeid_cast<const DataTypeString*>(&argument_type) || typeid_cast<const DataTypeFixedString*>(&argument_type))
|
|
|
|
return new AggreagteFunctionGroupUniqArrayGeneric<true>;
|
|
|
|
|
|
|
|
auto * array_type = typeid_cast<const DataTypeArray *>(&argument_type);
|
|
|
|
if (array_type)
|
|
|
|
{
|
|
|
|
auto nested_type = array_type->getNestedType();
|
|
|
|
if (nested_type->isNumeric() || typeid_cast<DataTypeFixedString *>(nested_type.get()))
|
|
|
|
return new AggreagteFunctionGroupUniqArrayGeneric<true>;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new AggreagteFunctionGroupUniqArrayGeneric<false>;
|
|
|
|
}
|
2016-09-13 13:24:24 +00:00
|
|
|
}
|
|
|
|
|
2017-12-02 02:47:12 +00:00
|
|
|
AggregateFunctionPtr createAggregateFunctionGroupUniqArray(const std::string & name, const DataTypes & argument_types, const Array & /*parameters*/)
|
2015-09-24 12:40:36 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (argument_types.size() != 1)
|
|
|
|
throw Exception("Incorrect number of arguments for aggregate function " + name,
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
2015-09-24 12:40:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
AggregateFunctionPtr res(createWithNumericType<AggregateFunctionGroupUniqArray>(*argument_types[0]));
|
2015-09-24 12:40:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!res)
|
|
|
|
res = AggregateFunctionPtr(createWithExtraTypes(*argument_types[0]));
|
2016-09-13 13:24:24 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!res)
|
|
|
|
throw Exception("Illegal type " + argument_types[0]->getName() +
|
|
|
|
" of argument for aggregate function " + name, ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
2015-09-24 12:40:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return res;
|
2015-09-24 12:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void registerAggregateFunctionGroupUniqArray(AggregateFunctionFactory & factory)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
factory.registerFunction("groupUniqArray", createAggregateFunctionGroupUniqArray);
|
2015-09-24 12:40:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|