ClickHouse/src/DataTypes/FieldToDataType.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

192 lines
6.2 KiB
C++
Raw Normal View History

#include <DataTypes/FieldToDataType.h>
#include <DataTypes/DataTypeTuple.h>
2020-10-10 06:49:03 +00:00
#include <DataTypes/DataTypeMap.h>
2021-08-20 21:11:22 +00:00
#include <DataTypes/DataTypeObject.h>
#include <DataTypes/DataTypesNumber.h>
2018-08-23 19:11:31 +00:00
#include <DataTypes/DataTypesDecimal.h>
#include <DataTypes/DataTypeString.h>
#include <DataTypes/DataTypeArray.h>
#include <DataTypes/DataTypeNullable.h>
#include <DataTypes/DataTypeNothing.h>
2021-05-03 22:46:51 +00:00
#include <DataTypes/DataTypeUUID.h>
#include <DataTypes/getLeastSupertype.h>
2019-02-11 13:11:52 +00:00
#include <DataTypes/DataTypeFactory.h>
#include <Common/Exception.h>
namespace DB
{
namespace ErrorCodes
{
extern const int EMPTY_DATA_PASSED;
}
template <LeastSupertypeOnError on_error>
DataTypePtr FieldToDataType<on_error>::operator() (const Null &) const
{
return std::make_shared<DataTypeNullable>(std::make_shared<DataTypeNothing>());
}
template <LeastSupertypeOnError on_error>
DataTypePtr FieldToDataType<on_error>::operator() (const UInt64 & x) const
{
2018-11-26 00:56:50 +00:00
if (x <= std::numeric_limits<UInt8>::max()) return std::make_shared<DataTypeUInt8>();
if (x <= std::numeric_limits<UInt16>::max()) return std::make_shared<DataTypeUInt16>();
if (x <= std::numeric_limits<UInt32>::max()) return std::make_shared<DataTypeUInt32>();
return std::make_shared<DataTypeUInt64>();
}
template <LeastSupertypeOnError on_error>
DataTypePtr FieldToDataType<on_error>::operator() (const Int64 & x) const
{
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>();
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>();
return std::make_shared<DataTypeInt64>();
}
template <LeastSupertypeOnError on_error>
DataTypePtr FieldToDataType<on_error>::operator() (const Float64 &) const
2021-05-03 15:20:12 +00:00
{
return std::make_shared<DataTypeFloat64>();
}
template <LeastSupertypeOnError on_error>
DataTypePtr FieldToDataType<on_error>::operator() (const UInt128 &) const
2021-05-03 15:20:12 +00:00
{
return std::make_shared<DataTypeUInt128>();
}
template <LeastSupertypeOnError on_error>
DataTypePtr FieldToDataType<on_error>::operator() (const Int128 &) const
{
return std::make_shared<DataTypeInt128>();
}
template <LeastSupertypeOnError on_error>
DataTypePtr FieldToDataType<on_error>::operator() (const UInt256 &) const
{
2021-05-03 15:20:12 +00:00
return std::make_shared<DataTypeUInt256>();
}
template <LeastSupertypeOnError on_error>
DataTypePtr FieldToDataType<on_error>::operator() (const Int256 &) const
2021-05-03 15:20:12 +00:00
{
return std::make_shared<DataTypeInt256>();
}
template <LeastSupertypeOnError on_error>
DataTypePtr FieldToDataType<on_error>::operator() (const UUID &) const
2021-05-03 22:46:51 +00:00
{
return std::make_shared<DataTypeUUID>();
}
template <LeastSupertypeOnError on_error>
DataTypePtr FieldToDataType<on_error>::operator() (const String &) const
{
return std::make_shared<DataTypeString>();
}
template <LeastSupertypeOnError on_error>
DataTypePtr FieldToDataType<on_error>::operator() (const DecimalField<Decimal32> & x) const
2018-08-23 19:11:31 +00:00
{
using Type = DataTypeDecimal<Decimal32>;
return std::make_shared<Type>(Type::maxPrecision(), x.getScale());
}
template <LeastSupertypeOnError on_error>
DataTypePtr FieldToDataType<on_error>::operator() (const DecimalField<Decimal64> & x) const
2018-08-23 19:11:31 +00:00
{
using Type = DataTypeDecimal<Decimal64>;
return std::make_shared<Type>(Type::maxPrecision(), x.getScale());
}
template <LeastSupertypeOnError on_error>
DataTypePtr FieldToDataType<on_error>::operator() (const DecimalField<Decimal128> & x) const
2018-08-23 19:11:31 +00:00
{
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());
}
template <LeastSupertypeOnError on_error>
DataTypePtr FieldToDataType<on_error>::operator() (const DecimalField<Decimal256> & x) const
{
using Type = DataTypeDecimal<Decimal256>;
return std::make_shared<Type>(Type::maxPrecision(), x.getScale());
}
template <LeastSupertypeOnError on_error>
DataTypePtr FieldToDataType<on_error>::operator() (const Array & x) const
{
DataTypes element_types;
element_types.reserve(x.size());
2018-02-28 04:30:27 +00:00
for (const Field & elem : x)
element_types.emplace_back(applyVisitor(*this, elem));
return std::make_shared<DataTypeArray>(getLeastSupertype<on_error>(element_types));
}
template <LeastSupertypeOnError on_error>
DataTypePtr FieldToDataType<on_error>::operator() (const Tuple & tuple) const
{
if (tuple.empty())
throw Exception("Cannot infer type of an empty tuple", ErrorCodes::EMPTY_DATA_PASSED);
DataTypes element_types;
2021-06-15 19:55:21 +00:00
element_types.reserve(tuple.size());
2018-02-28 04:30:27 +00:00
for (const auto & element : tuple)
element_types.push_back(applyVisitor(*this, element));
return std::make_shared<DataTypeTuple>(element_types);
}
template <LeastSupertypeOnError on_error>
DataTypePtr FieldToDataType<on_error>::operator() (const Map & map) const
2020-10-10 06:49:03 +00:00
{
2020-12-03 03:52:41 +00:00
DataTypes key_types;
DataTypes value_types;
key_types.reserve(map.size());
value_types.reserve(map.size());
for (const auto & elem : map)
{
const auto & tuple = elem.safeGet<const Tuple &>();
assert(tuple.size() == 2);
key_types.push_back(applyVisitor(*this, tuple[0]));
value_types.push_back(applyVisitor(*this, tuple[1]));
2020-12-03 03:52:41 +00:00
}
return std::make_shared<DataTypeMap>(
getLeastSupertype<on_error>(key_types),
2022-06-28 14:21:21 +00:00
getLeastSupertype<on_error>(value_types));
2020-10-10 06:49:03 +00:00
}
template <LeastSupertypeOnError on_error>
DataTypePtr FieldToDataType<on_error>::operator() (const Object &) const
2021-08-20 21:11:22 +00:00
{
/// TODO: Do we need different parameters for type Object?
return std::make_shared<DataTypeObject>("json", false);
}
template <LeastSupertypeOnError on_error>
DataTypePtr FieldToDataType<on_error>::operator() (const AggregateFunctionStateData & x) const
2019-02-11 13:11:52 +00:00
{
2020-04-22 05:39:31 +00:00
const auto & name = static_cast<const AggregateFunctionStateData &>(x).name;
2019-02-11 13:11:52 +00:00
return DataTypeFactory::instance().get(name);
}
template <LeastSupertypeOnError on_error>
DataTypePtr FieldToDataType<on_error>::operator()(const bool &) const
{
return DataTypeFactory::instance().get("Bool");
}
template class FieldToDataType<LeastSupertypeOnError::Throw>;
template class FieldToDataType<LeastSupertypeOnError::String>;
template class FieldToDataType<LeastSupertypeOnError::Null>;
}