2016-06-07 08:23:15 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-12-20 08:19:54 +00:00
|
|
|
#include <AggregateFunctions/AggregateFunctionMinMaxAny.h>
|
2017-07-12 01:16:01 +00:00
|
|
|
#include <AggregateFunctions/AggregateFunctionArgMinMax.h>
|
2017-12-20 08:19:54 +00:00
|
|
|
#include <AggregateFunctions/FactoryHelpers.h>
|
2017-07-13 20:58:19 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataTypes/DataTypeDate.h>
|
|
|
|
#include <DataTypes/DataTypeDateTime.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-12-20 08:19:54 +00:00
|
|
|
|
2016-06-07 08:23:15 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
/// min, max, any, anyLast
|
|
|
|
template <template <typename> class AggregateFunctionTemplate, template <typename> class Data>
|
2017-12-20 08:19:54 +00:00
|
|
|
static IAggregateFunction * createAggregateFunctionSingleValue(const String & name, const DataTypes & argument_types, const Array & parameters)
|
2016-06-07 08:23:15 +00:00
|
|
|
{
|
2017-12-20 08:19:54 +00:00
|
|
|
assertNoParameters(name, parameters);
|
|
|
|
assertUnary(name, argument_types);
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
const IDataType & argument_type = *argument_types[0];
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-07-12 01:16:01 +00:00
|
|
|
if (typeid_cast<const DataTypeUInt8 *>(&argument_type)) return new AggregateFunctionTemplate<Data<SingleValueDataFixed<UInt8>>>;
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeUInt16 *>(&argument_type)) return new AggregateFunctionTemplate<Data<SingleValueDataFixed<UInt16>>>;
|
|
|
|
if (typeid_cast<const DataTypeUInt32 *>(&argument_type)) return new AggregateFunctionTemplate<Data<SingleValueDataFixed<UInt32>>>;
|
|
|
|
if (typeid_cast<const DataTypeUInt64 *>(&argument_type)) return new AggregateFunctionTemplate<Data<SingleValueDataFixed<UInt64>>>;
|
|
|
|
if (typeid_cast<const DataTypeInt8 *>(&argument_type)) return new AggregateFunctionTemplate<Data<SingleValueDataFixed<Int8>>>;
|
|
|
|
if (typeid_cast<const DataTypeInt16 *>(&argument_type)) return new AggregateFunctionTemplate<Data<SingleValueDataFixed<Int16>>>;
|
|
|
|
if (typeid_cast<const DataTypeInt32 *>(&argument_type)) return new AggregateFunctionTemplate<Data<SingleValueDataFixed<Int32>>>;
|
|
|
|
if (typeid_cast<const DataTypeInt64 *>(&argument_type)) return new AggregateFunctionTemplate<Data<SingleValueDataFixed<Int64>>>;
|
|
|
|
if (typeid_cast<const DataTypeFloat32 *>(&argument_type)) return new AggregateFunctionTemplate<Data<SingleValueDataFixed<Float32>>>;
|
|
|
|
if (typeid_cast<const DataTypeFloat64 *>(&argument_type)) return new AggregateFunctionTemplate<Data<SingleValueDataFixed<Float64>>>;
|
|
|
|
if (typeid_cast<const DataTypeDate *>(&argument_type))
|
2017-04-01 07:20:54 +00:00
|
|
|
return new AggregateFunctionTemplate<Data<SingleValueDataFixed<DataTypeDate::FieldType>>>;
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeDateTime*>(&argument_type))
|
2017-04-01 07:20:54 +00:00
|
|
|
return new AggregateFunctionTemplate<Data<SingleValueDataFixed<DataTypeDateTime::FieldType>>>;
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeString*>(&argument_type))
|
2017-04-01 07:20:54 +00:00
|
|
|
return new AggregateFunctionTemplate<Data<SingleValueDataString>>;
|
|
|
|
else
|
|
|
|
return new AggregateFunctionTemplate<Data<SingleValueDataGeneric>>;
|
2016-06-07 08:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// argMin, argMax
|
|
|
|
template <template <typename> class MinMaxData, typename ResData>
|
2017-12-20 08:19:54 +00:00
|
|
|
static IAggregateFunction * createAggregateFunctionArgMinMaxSecond(const String & name, const IDataType * val_type)
|
2016-06-07 08:23:15 +00:00
|
|
|
{
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeUInt8 *>(val_type))
|
2017-07-12 01:16:01 +00:00
|
|
|
return new AggregateFunctionArgMinMax<AggregateFunctionArgMinMaxData<ResData, MinMaxData<SingleValueDataFixed<UInt8>>>>;
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeUInt16 *>(val_type))
|
2017-07-12 01:16:01 +00:00
|
|
|
return new AggregateFunctionArgMinMax<AggregateFunctionArgMinMaxData<ResData, MinMaxData<SingleValueDataFixed<UInt16>>>>;
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeUInt32 *>(val_type))
|
2017-07-12 01:16:01 +00:00
|
|
|
return new AggregateFunctionArgMinMax<AggregateFunctionArgMinMaxData<ResData, MinMaxData<SingleValueDataFixed<UInt32>>>>;
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeUInt64 *>(val_type))
|
2017-07-12 01:16:01 +00:00
|
|
|
return new AggregateFunctionArgMinMax<AggregateFunctionArgMinMaxData<ResData, MinMaxData<SingleValueDataFixed<UInt64>>>>;
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeInt8 *>(val_type))
|
2017-07-12 01:16:01 +00:00
|
|
|
return new AggregateFunctionArgMinMax<AggregateFunctionArgMinMaxData<ResData, MinMaxData<SingleValueDataFixed<Int8>>>>;
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeInt16 *>(val_type))
|
2017-07-12 01:16:01 +00:00
|
|
|
return new AggregateFunctionArgMinMax<AggregateFunctionArgMinMaxData<ResData, MinMaxData<SingleValueDataFixed<Int16>>>>;
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeInt32 *>(val_type))
|
2017-07-12 01:16:01 +00:00
|
|
|
return new AggregateFunctionArgMinMax<AggregateFunctionArgMinMaxData<ResData, MinMaxData<SingleValueDataFixed<Int32>>>>;
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeInt64 *>(val_type))
|
2017-07-12 01:16:01 +00:00
|
|
|
return new AggregateFunctionArgMinMax<AggregateFunctionArgMinMaxData<ResData, MinMaxData<SingleValueDataFixed<Int64>>>>;
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeFloat32 *>(val_type))
|
2017-07-12 01:16:01 +00:00
|
|
|
return new AggregateFunctionArgMinMax<AggregateFunctionArgMinMaxData<ResData, MinMaxData<SingleValueDataFixed<Float32>>>>;
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeFloat64 *>(val_type))
|
2017-07-12 01:16:01 +00:00
|
|
|
return new AggregateFunctionArgMinMax<AggregateFunctionArgMinMaxData<ResData, MinMaxData<SingleValueDataFixed<Float64>>>>;
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeDate *>(val_type))
|
2017-07-12 01:16:01 +00:00
|
|
|
return new AggregateFunctionArgMinMax<AggregateFunctionArgMinMaxData<ResData, MinMaxData<SingleValueDataFixed<DataTypeDate::FieldType>>>>;
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeDateTime*>(val_type))
|
2017-07-12 01:16:01 +00:00
|
|
|
return new AggregateFunctionArgMinMax<AggregateFunctionArgMinMaxData<ResData, MinMaxData<SingleValueDataFixed<DataTypeDateTime::FieldType>>>>;
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeString*>(val_type))
|
2017-07-12 01:16:01 +00:00
|
|
|
return new AggregateFunctionArgMinMax<AggregateFunctionArgMinMaxData<ResData, MinMaxData<SingleValueDataString>>>;
|
2017-12-20 08:19:54 +00:00
|
|
|
|
|
|
|
return new AggregateFunctionArgMinMax<AggregateFunctionArgMinMaxData<ResData, MinMaxData<SingleValueDataGeneric>>>;
|
2016-06-07 08:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <template <typename> class MinMaxData>
|
2017-12-20 08:19:54 +00:00
|
|
|
static IAggregateFunction * createAggregateFunctionArgMinMax(const String & name, const DataTypes & argument_types, const Array & parameters)
|
2016-06-07 08:23:15 +00:00
|
|
|
{
|
2017-12-20 08:19:54 +00:00
|
|
|
assertNoParameters(name, parameters);
|
|
|
|
assertBinary(name, argument_types);
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-12-20 08:19:54 +00:00
|
|
|
const IDataType * res_type = argument_types[0].get();
|
|
|
|
const IDataType * val_type = argument_types[1].get();
|
2016-06-07 08:23:15 +00:00
|
|
|
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeUInt8 *>(&res_type))
|
2017-04-01 07:20:54 +00:00
|
|
|
return createAggregateFunctionArgMinMaxSecond<MinMaxData, SingleValueDataFixed<UInt8>>(name, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeUInt16 *>(&res_type))
|
2017-04-01 07:20:54 +00:00
|
|
|
return createAggregateFunctionArgMinMaxSecond<MinMaxData, SingleValueDataFixed<UInt16>>(name, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeUInt32 *>(&res_type))
|
2017-04-01 07:20:54 +00:00
|
|
|
return createAggregateFunctionArgMinMaxSecond<MinMaxData, SingleValueDataFixed<UInt32>>(name, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeUInt64 *>(&res_type))
|
2017-04-01 07:20:54 +00:00
|
|
|
return createAggregateFunctionArgMinMaxSecond<MinMaxData, SingleValueDataFixed<UInt64>>(name, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeInt8 *>(&res_type))
|
2017-04-01 07:20:54 +00:00
|
|
|
return createAggregateFunctionArgMinMaxSecond<MinMaxData, SingleValueDataFixed<Int8>>(name, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeInt16 *>(&res_type))
|
2017-04-01 07:20:54 +00:00
|
|
|
return createAggregateFunctionArgMinMaxSecond<MinMaxData, SingleValueDataFixed<Int16>>(name, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeInt32 *>(&res_type))
|
2017-04-01 07:20:54 +00:00
|
|
|
return createAggregateFunctionArgMinMaxSecond<MinMaxData, SingleValueDataFixed<Int32>>(name, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeInt64 *>(&res_type))
|
2017-04-01 07:20:54 +00:00
|
|
|
return createAggregateFunctionArgMinMaxSecond<MinMaxData, SingleValueDataFixed<Int64>>(name, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeFloat32 *>(&res_type))
|
2017-04-01 07:20:54 +00:00
|
|
|
return createAggregateFunctionArgMinMaxSecond<MinMaxData, SingleValueDataFixed<Float32>>(name, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeFloat64 *>(&res_type))
|
2017-04-01 07:20:54 +00:00
|
|
|
return createAggregateFunctionArgMinMaxSecond<MinMaxData, SingleValueDataFixed<Float64>>(name, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeDate *>(&res_type))
|
2017-04-01 07:20:54 +00:00
|
|
|
return createAggregateFunctionArgMinMaxSecond<MinMaxData, SingleValueDataFixed<DataTypeDate::FieldType>>(name, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeDateTime*>(&res_type))
|
2017-04-01 07:20:54 +00:00
|
|
|
return createAggregateFunctionArgMinMaxSecond<MinMaxData, SingleValueDataFixed<DataTypeDateTime::FieldType>>(name, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeString*>(&res_type))
|
2017-04-01 07:20:54 +00:00
|
|
|
return createAggregateFunctionArgMinMaxSecond<MinMaxData, SingleValueDataString>(name, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
|
|
|
|
return createAggregateFunctionArgMinMaxSecond<MinMaxData, SingleValueDataGeneric>(name, val_type);
|
2016-06-07 08:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|