2017-11-24 13:55:31 +00:00
|
|
|
#include <Common/FieldVisitors.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataTypes/FieldToDataType.h>
|
|
|
|
#include <DataTypes/DataTypeTuple.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
2018-08-23 19:11:31 +00:00
|
|
|
#include <DataTypes/DataTypesDecimal.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <DataTypes/DataTypeArray.h>
|
2017-12-07 08:31:47 +00:00
|
|
|
#include <DataTypes/DataTypeNullable.h>
|
|
|
|
#include <DataTypes/DataTypeNothing.h>
|
2018-01-11 18:37:11 +00:00
|
|
|
#include <DataTypes/getLeastSupertype.h>
|
2019-02-11 13:11:52 +00:00
|
|
|
#include <DataTypes/DataTypeFactory.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Exception.h>
|
2017-06-06 17:18:32 +00:00
|
|
|
#include <ext/size.h>
|
2015-06-29 04:54:52 +00:00
|
|
|
|
2015-10-12 07:05:54 +00:00
|
|
|
|
2015-06-29 04:54:52 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
extern const int EMPTY_DATA_PASSED;
|
2018-08-03 20:25:47 +00:00
|
|
|
extern const int NOT_IMPLEMENTED;
|
2016-01-11 21:46:36 +00:00
|
|
|
}
|
|
|
|
|
2015-06-29 04:54:52 +00:00
|
|
|
|
2018-02-28 04:30:27 +00:00
|
|
|
DataTypePtr FieldToDataType::operator() (const Null &) const
|
2017-03-12 10:13:45 +00:00
|
|
|
{
|
2017-12-07 08:31:47 +00:00
|
|
|
return std::make_shared<DataTypeNullable>(std::make_shared<DataTypeNothing>());
|
2017-03-12 10:13:45 +00:00
|
|
|
}
|
|
|
|
|
2018-02-28 04:30:27 +00:00
|
|
|
DataTypePtr FieldToDataType::operator() (const UInt64 & x) const
|
2017-03-12 10:13:45 +00:00
|
|
|
{
|
2018-11-26 00:56:50 +00:00
|
|
|
if (x <= std::numeric_limits<UInt8>::max()) return std::make_shared<DataTypeUInt8>();
|
2017-12-07 03:35:19 +00:00
|
|
|
if (x <= std::numeric_limits<UInt16>::max()) return std::make_shared<DataTypeUInt16>();
|
|
|
|
if (x <= std::numeric_limits<UInt32>::max()) return std::make_shared<DataTypeUInt32>();
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_shared<DataTypeUInt64>();
|
2017-03-12 10:13:45 +00:00
|
|
|
}
|
|
|
|
|
2018-08-03 20:25:47 +00:00
|
|
|
DataTypePtr FieldToDataType::operator() (const UInt128 &) const
|
2018-07-09 21:25:56 +00:00
|
|
|
{
|
2018-08-03 20:25:47 +00:00
|
|
|
throw Exception("There are no UInt128 literals in SQL", ErrorCodes::NOT_IMPLEMENTED);
|
2018-07-09 21:25:56 +00:00
|
|
|
}
|
|
|
|
|
2018-02-28 04:30:27 +00:00
|
|
|
DataTypePtr FieldToDataType::operator() (const Int64 & x) const
|
2017-03-12 10:13:45 +00:00
|
|
|
{
|
2018-11-26 00:56:50 +00:00
|
|
|
if (x <= std::numeric_limits<Int8>::max() && x >= std::numeric_limits<Int8>::min()) return std::make_shared<DataTypeInt8>();
|
2017-12-07 03:35:19 +00:00
|
|
|
if (x <= std::numeric_limits<Int16>::max() && x >= std::numeric_limits<Int16>::min()) return std::make_shared<DataTypeInt16>();
|
|
|
|
if (x <= std::numeric_limits<Int32>::max() && x >= std::numeric_limits<Int32>::min()) return std::make_shared<DataTypeInt32>();
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_shared<DataTypeInt64>();
|
2017-03-12 10:13:45 +00:00
|
|
|
}
|
|
|
|
|
2018-02-28 04:30:27 +00:00
|
|
|
DataTypePtr FieldToDataType::operator() (const Float64 &) const
|
2017-03-12 10:13:45 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_shared<DataTypeFloat64>();
|
2017-03-12 10:13:45 +00:00
|
|
|
}
|
|
|
|
|
2018-02-28 04:30:27 +00:00
|
|
|
DataTypePtr FieldToDataType::operator() (const String &) const
|
2017-03-12 10:13:45 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_shared<DataTypeString>();
|
2017-03-12 10:13:45 +00:00
|
|
|
}
|
|
|
|
|
2018-08-23 19:11:31 +00:00
|
|
|
DataTypePtr FieldToDataType::operator() (const DecimalField<Decimal32> & x) const
|
|
|
|
{
|
|
|
|
using Type = DataTypeDecimal<Decimal32>;
|
|
|
|
return std::make_shared<Type>(Type::maxPrecision(), x.getScale());
|
|
|
|
}
|
|
|
|
|
|
|
|
DataTypePtr FieldToDataType::operator() (const DecimalField<Decimal64> & x) const
|
|
|
|
{
|
|
|
|
using Type = DataTypeDecimal<Decimal64>;
|
|
|
|
return std::make_shared<Type>(Type::maxPrecision(), x.getScale());
|
|
|
|
}
|
|
|
|
|
|
|
|
DataTypePtr FieldToDataType::operator() (const DecimalField<Decimal128> & x) const
|
|
|
|
{
|
2018-10-22 08:25:11 +00:00
|
|
|
using Type = DataTypeDecimal<Decimal128>;
|
2018-08-23 19:11:31 +00:00
|
|
|
return std::make_shared<Type>(Type::maxPrecision(), x.getScale());
|
|
|
|
}
|
|
|
|
|
2017-03-12 10:13:45 +00:00
|
|
|
|
2018-02-28 04:30:27 +00:00
|
|
|
DataTypePtr FieldToDataType::operator() (const Array & x) const
|
2015-06-29 04:54:52 +00:00
|
|
|
{
|
2017-12-07 03:35:19 +00:00
|
|
|
DataTypes element_types;
|
|
|
|
element_types.reserve(x.size());
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-02-28 04:30:27 +00:00
|
|
|
for (const Field & elem : x)
|
2017-12-07 03:35:19 +00:00
|
|
|
element_types.emplace_back(applyVisitor(FieldToDataType(), elem));
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2018-02-28 04:30:27 +00:00
|
|
|
return std::make_shared<DataTypeArray>(getLeastSupertype(element_types));
|
2015-06-29 04:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-18 15:57:05 +00:00
|
|
|
DataTypePtr FieldToDataType::operator() (const Tuple & tuple) const
|
2015-12-24 14:31:41 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (tuple.empty())
|
|
|
|
throw Exception("Cannot infer type of an empty tuple", ErrorCodes::EMPTY_DATA_PASSED);
|
2015-12-24 14:31:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
DataTypes element_types;
|
|
|
|
element_types.reserve(ext::size(tuple));
|
2015-12-24 14:31:41 +00:00
|
|
|
|
2018-02-28 04:30:27 +00:00
|
|
|
for (const auto & element : tuple)
|
|
|
|
element_types.push_back(applyVisitor(FieldToDataType(), element));
|
2015-12-24 14:31:41 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::make_shared<DataTypeTuple>(element_types);
|
2015-12-24 14:31:41 +00:00
|
|
|
}
|
|
|
|
|
2019-02-11 13:11:52 +00:00
|
|
|
DataTypePtr FieldToDataType::operator() (const AggregateFunctionStateData & x) const
|
|
|
|
{
|
|
|
|
auto & name = static_cast<const AggregateFunctionStateData &>(x).name;
|
|
|
|
return DataTypeFactory::instance().get(name);
|
|
|
|
}
|
2015-12-24 14:31:41 +00:00
|
|
|
|
2015-06-29 04:54:52 +00:00
|
|
|
}
|