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-12-20 08:39:21 +00:00
|
|
|
if (typeid_cast<const DataTypeUInt8 *>(&argument_type)) return new AggregateFunctionTemplate<Data<SingleValueDataFixed<UInt8>>>(argument_type);
|
|
|
|
if (typeid_cast<const DataTypeUInt16 *>(&argument_type)) return new AggregateFunctionTemplate<Data<SingleValueDataFixed<UInt16>>>(argument_type);
|
|
|
|
if (typeid_cast<const DataTypeUInt32 *>(&argument_type)) return new AggregateFunctionTemplate<Data<SingleValueDataFixed<UInt32>>>(argument_type);
|
|
|
|
if (typeid_cast<const DataTypeUInt64 *>(&argument_type)) return new AggregateFunctionTemplate<Data<SingleValueDataFixed<UInt64>>>(argument_type);
|
|
|
|
if (typeid_cast<const DataTypeInt8 *>(&argument_type)) return new AggregateFunctionTemplate<Data<SingleValueDataFixed<Int8>>>(argument_type);
|
|
|
|
if (typeid_cast<const DataTypeInt16 *>(&argument_type)) return new AggregateFunctionTemplate<Data<SingleValueDataFixed<Int16>>>(argument_type);
|
|
|
|
if (typeid_cast<const DataTypeInt32 *>(&argument_type)) return new AggregateFunctionTemplate<Data<SingleValueDataFixed<Int32>>>(argument_type);
|
|
|
|
if (typeid_cast<const DataTypeInt64 *>(&argument_type)) return new AggregateFunctionTemplate<Data<SingleValueDataFixed<Int64>>>(argument_type);
|
|
|
|
if (typeid_cast<const DataTypeFloat32 *>(&argument_type)) return new AggregateFunctionTemplate<Data<SingleValueDataFixed<Float32>>>(argument_type);
|
|
|
|
if (typeid_cast<const DataTypeFloat64 *>(&argument_type)) return new AggregateFunctionTemplate<Data<SingleValueDataFixed<Float64>>>(argument_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
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>>;
|
2017-12-20 08:39:21 +00:00
|
|
|
|
|
|
|
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:39:21 +00:00
|
|
|
static IAggregateFunction * createAggregateFunctionArgMinMaxSecond(const IDataType * res_type, 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-12-20 08:39:21 +00:00
|
|
|
return new AggregateFunctionArgMinMax<AggregateFunctionArgMinMaxData<ResData, MinMaxData<SingleValueDataFixed<UInt8>>>>(res_type, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeUInt16 *>(val_type))
|
2017-12-20 08:39:21 +00:00
|
|
|
return new AggregateFunctionArgMinMax<AggregateFunctionArgMinMaxData<ResData, MinMaxData<SingleValueDataFixed<UInt16>>>>(res_type, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeUInt32 *>(val_type))
|
2017-12-20 08:39:21 +00:00
|
|
|
return new AggregateFunctionArgMinMax<AggregateFunctionArgMinMaxData<ResData, MinMaxData<SingleValueDataFixed<UInt32>>>>(res_type, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeUInt64 *>(val_type))
|
2017-12-20 08:39:21 +00:00
|
|
|
return new AggregateFunctionArgMinMax<AggregateFunctionArgMinMaxData<ResData, MinMaxData<SingleValueDataFixed<UInt64>>>>(res_type, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeInt8 *>(val_type))
|
2017-12-20 08:39:21 +00:00
|
|
|
return new AggregateFunctionArgMinMax<AggregateFunctionArgMinMaxData<ResData, MinMaxData<SingleValueDataFixed<Int8>>>>(res_type, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeInt16 *>(val_type))
|
2017-12-20 08:39:21 +00:00
|
|
|
return new AggregateFunctionArgMinMax<AggregateFunctionArgMinMaxData<ResData, MinMaxData<SingleValueDataFixed<Int16>>>>(res_type, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeInt32 *>(val_type))
|
2017-12-20 08:39:21 +00:00
|
|
|
return new AggregateFunctionArgMinMax<AggregateFunctionArgMinMaxData<ResData, MinMaxData<SingleValueDataFixed<Int32>>>>(res_type, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeInt64 *>(val_type))
|
2017-12-20 08:39:21 +00:00
|
|
|
return new AggregateFunctionArgMinMax<AggregateFunctionArgMinMaxData<ResData, MinMaxData<SingleValueDataFixed<Int64>>>>(res_type, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeFloat32 *>(val_type))
|
2017-12-20 08:39:21 +00:00
|
|
|
return new AggregateFunctionArgMinMax<AggregateFunctionArgMinMaxData<ResData, MinMaxData<SingleValueDataFixed<Float32>>>>(res_type, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeFloat64 *>(val_type))
|
2017-12-20 08:39:21 +00:00
|
|
|
return new AggregateFunctionArgMinMax<AggregateFunctionArgMinMaxData<ResData, MinMaxData<SingleValueDataFixed<Float64>>>>(res_type, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeDate *>(val_type))
|
2017-12-20 08:39:21 +00:00
|
|
|
return new AggregateFunctionArgMinMax<AggregateFunctionArgMinMaxData<ResData, MinMaxData<SingleValueDataFixed<DataTypeDate::FieldType>>>>(res_type, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeDateTime*>(val_type))
|
2017-12-20 08:39:21 +00:00
|
|
|
return new AggregateFunctionArgMinMax<AggregateFunctionArgMinMaxData<ResData, MinMaxData<SingleValueDataFixed<DataTypeDateTime::FieldType>>>>(res_type, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeString*>(val_type))
|
2017-12-20 08:39:21 +00:00
|
|
|
return new AggregateFunctionArgMinMax<AggregateFunctionArgMinMaxData<ResData, MinMaxData<SingleValueDataString>>>(res_type, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
|
2017-12-20 08:39:21 +00:00
|
|
|
return new AggregateFunctionArgMinMax<AggregateFunctionArgMinMaxData<ResData, MinMaxData<SingleValueDataGeneric>>>(res_type, val_type);
|
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-12-20 08:39:21 +00:00
|
|
|
return createAggregateFunctionArgMinMaxSecond<MinMaxData, SingleValueDataFixed<UInt8>>(res_type, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeUInt16 *>(&res_type))
|
2017-12-20 08:39:21 +00:00
|
|
|
return createAggregateFunctionArgMinMaxSecond<MinMaxData, SingleValueDataFixed<UInt16>>(res_type, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeUInt32 *>(&res_type))
|
2017-12-20 08:39:21 +00:00
|
|
|
return createAggregateFunctionArgMinMaxSecond<MinMaxData, SingleValueDataFixed<UInt32>>(res_type, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeUInt64 *>(&res_type))
|
2017-12-20 08:39:21 +00:00
|
|
|
return createAggregateFunctionArgMinMaxSecond<MinMaxData, SingleValueDataFixed<UInt64>>(res_type, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeInt8 *>(&res_type))
|
2017-12-20 08:39:21 +00:00
|
|
|
return createAggregateFunctionArgMinMaxSecond<MinMaxData, SingleValueDataFixed<Int8>>(res_type, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeInt16 *>(&res_type))
|
2017-12-20 08:39:21 +00:00
|
|
|
return createAggregateFunctionArgMinMaxSecond<MinMaxData, SingleValueDataFixed<Int16>>(res_type, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeInt32 *>(&res_type))
|
2017-12-20 08:39:21 +00:00
|
|
|
return createAggregateFunctionArgMinMaxSecond<MinMaxData, SingleValueDataFixed<Int32>>(res_type, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeInt64 *>(&res_type))
|
2017-12-20 08:39:21 +00:00
|
|
|
return createAggregateFunctionArgMinMaxSecond<MinMaxData, SingleValueDataFixed<Int64>>(res_type, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeFloat32 *>(&res_type))
|
2017-12-20 08:39:21 +00:00
|
|
|
return createAggregateFunctionArgMinMaxSecond<MinMaxData, SingleValueDataFixed<Float32>>(res_type, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeFloat64 *>(&res_type))
|
2017-12-20 08:39:21 +00:00
|
|
|
return createAggregateFunctionArgMinMaxSecond<MinMaxData, SingleValueDataFixed<Float64>>(res_type, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeDate *>(&res_type))
|
2017-12-20 08:39:21 +00:00
|
|
|
return createAggregateFunctionArgMinMaxSecond<MinMaxData, SingleValueDataFixed<DataTypeDate::FieldType>>(res_type, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeDateTime*>(&res_type))
|
2017-12-20 08:39:21 +00:00
|
|
|
return createAggregateFunctionArgMinMaxSecond<MinMaxData, SingleValueDataFixed<DataTypeDateTime::FieldType>>(res_type, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
if (typeid_cast<const DataTypeString*>(&res_type))
|
2017-12-20 08:39:21 +00:00
|
|
|
return createAggregateFunctionArgMinMaxSecond<MinMaxData, SingleValueDataString>(res_type, val_type);
|
2017-12-20 08:19:54 +00:00
|
|
|
|
2017-12-20 08:39:21 +00:00
|
|
|
return createAggregateFunctionArgMinMaxSecond<MinMaxData, SingleValueDataGeneric>(res_type, val_type);
|
2016-06-07 08:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|