2018-09-06 10:48:54 +00:00
|
|
|
#include <unordered_set>
|
|
|
|
|
2017-12-08 09:45:07 +00:00
|
|
|
#include <IO/WriteBufferFromString.h>
|
|
|
|
#include <IO/Operators.h>
|
2017-12-07 00:31:46 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
|
|
|
|
2018-01-11 18:37:11 +00:00
|
|
|
#include <DataTypes/getLeastSupertype.h>
|
2017-12-07 00:31:46 +00:00
|
|
|
|
|
|
|
#include <DataTypes/DataTypeArray.h>
|
|
|
|
#include <DataTypes/DataTypeTuple.h>
|
2021-02-24 16:45:38 +00:00
|
|
|
#include <DataTypes/DataTypeMap.h>
|
2017-12-07 00:31:46 +00:00
|
|
|
#include <DataTypes/DataTypeNullable.h>
|
2020-07-07 22:10:23 +00:00
|
|
|
#include <DataTypes/DataTypeLowCardinality.h>
|
2017-12-07 08:31:47 +00:00
|
|
|
#include <DataTypes/DataTypeNothing.h>
|
2017-12-07 00:31:46 +00:00
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <DataTypes/DataTypeDateTime.h>
|
2019-11-04 14:06:22 +00:00
|
|
|
#include <DataTypes/DataTypeDateTime64.h>
|
2020-12-18 08:10:54 +00:00
|
|
|
#include <DataTypes/DataTypeEnum.h>
|
2017-12-07 00:31:46 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
2018-09-06 10:48:54 +00:00
|
|
|
#include <DataTypes/DataTypesDecimal.h>
|
2021-06-12 15:10:25 +00:00
|
|
|
#include <DataTypes/DataTypeFactory.h>
|
2021-11-09 12:36:25 +00:00
|
|
|
#include <base/EnumReflection.h>
|
2017-12-07 00:31:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NO_COMMON_TYPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2021-06-12 15:10:25 +00:00
|
|
|
|
2021-11-09 12:36:25 +00:00
|
|
|
String typeToString(const DataTypePtr & type) { return type->getName(); }
|
|
|
|
String typeToString(const TypeIndex & type) { return String(magic_enum::enum_name(type)); }
|
2021-06-12 15:10:25 +00:00
|
|
|
|
|
|
|
template <typename DataTypes>
|
|
|
|
String getExceptionMessagePrefix(const DataTypes & types)
|
|
|
|
{
|
|
|
|
WriteBufferFromOwnString res;
|
|
|
|
res << "There is no supertype for types ";
|
|
|
|
|
|
|
|
bool first = true;
|
|
|
|
for (const auto & type : types)
|
2017-12-07 00:31:46 +00:00
|
|
|
{
|
2021-06-12 15:10:25 +00:00
|
|
|
if (!first)
|
|
|
|
res << ", ";
|
|
|
|
first = false;
|
2017-12-07 00:31:46 +00:00
|
|
|
|
2021-06-12 15:10:25 +00:00
|
|
|
res << typeToString(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.str();
|
|
|
|
}
|
|
|
|
|
2022-06-22 16:25:57 +00:00
|
|
|
template <LeastSupertypeOnError on_error, typename DataTypes>
|
|
|
|
DataTypePtr throwOrReturn(const DataTypes & types, std::string_view message_suffix, int error_code)
|
2021-06-12 15:10:25 +00:00
|
|
|
{
|
2022-06-22 16:25:57 +00:00
|
|
|
if constexpr (on_error == LeastSupertypeOnError::String)
|
|
|
|
return std::make_shared<DataTypeString>();
|
2021-06-12 15:10:25 +00:00
|
|
|
|
2022-06-22 16:25:57 +00:00
|
|
|
if constexpr (on_error == LeastSupertypeOnError::Null)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (message_suffix.empty())
|
|
|
|
throw Exception(error_code, getExceptionMessagePrefix(types));
|
2021-06-12 15:10:25 +00:00
|
|
|
|
2022-06-22 16:25:57 +00:00
|
|
|
throw Exception(error_code, "{} {}", getExceptionMessagePrefix(types), message_suffix);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <LeastSupertypeOnError on_error>
|
|
|
|
DataTypePtr getNumericType(const TypeIndexSet & types)
|
|
|
|
{
|
2021-06-12 15:10:25 +00:00
|
|
|
bool all_numbers = true;
|
|
|
|
|
|
|
|
size_t max_bits_of_signed_integer = 0;
|
|
|
|
size_t max_bits_of_unsigned_integer = 0;
|
|
|
|
size_t max_mantissa_bits_of_floating = 0;
|
|
|
|
|
|
|
|
auto maximize = [](size_t & what, size_t value)
|
|
|
|
{
|
|
|
|
if (value > what)
|
|
|
|
what = value;
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto & type : types)
|
|
|
|
{
|
|
|
|
if (type == TypeIndex::UInt8)
|
|
|
|
maximize(max_bits_of_unsigned_integer, 8);
|
|
|
|
else if (type == TypeIndex::UInt16)
|
|
|
|
maximize(max_bits_of_unsigned_integer, 16);
|
|
|
|
else if (type == TypeIndex::UInt32)
|
|
|
|
maximize(max_bits_of_unsigned_integer, 32);
|
|
|
|
else if (type == TypeIndex::UInt64)
|
|
|
|
maximize(max_bits_of_unsigned_integer, 64);
|
|
|
|
else if (type == TypeIndex::UInt128)
|
|
|
|
maximize(max_bits_of_unsigned_integer, 128);
|
|
|
|
else if (type == TypeIndex::UInt256)
|
|
|
|
maximize(max_bits_of_unsigned_integer, 256);
|
|
|
|
else if (type == TypeIndex::Int8 || type == TypeIndex::Enum8)
|
|
|
|
maximize(max_bits_of_signed_integer, 8);
|
|
|
|
else if (type == TypeIndex::Int16 || type == TypeIndex::Enum16)
|
|
|
|
maximize(max_bits_of_signed_integer, 16);
|
|
|
|
else if (type == TypeIndex::Int32)
|
|
|
|
maximize(max_bits_of_signed_integer, 32);
|
|
|
|
else if (type == TypeIndex::Int64)
|
|
|
|
maximize(max_bits_of_signed_integer, 64);
|
|
|
|
else if (type == TypeIndex::Int128)
|
|
|
|
maximize(max_bits_of_signed_integer, 128);
|
|
|
|
else if (type == TypeIndex::Int256)
|
|
|
|
maximize(max_bits_of_signed_integer, 256);
|
|
|
|
else if (type == TypeIndex::Float32)
|
|
|
|
maximize(max_mantissa_bits_of_floating, 24);
|
|
|
|
else if (type == TypeIndex::Float64)
|
|
|
|
maximize(max_mantissa_bits_of_floating, 53);
|
2022-06-22 16:25:57 +00:00
|
|
|
else if (type != TypeIndex::Nothing)
|
2021-06-12 15:10:25 +00:00
|
|
|
all_numbers = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (max_bits_of_signed_integer || max_bits_of_unsigned_integer || max_mantissa_bits_of_floating)
|
|
|
|
{
|
|
|
|
if (!all_numbers)
|
2022-06-22 16:25:57 +00:00
|
|
|
return throwOrReturn<on_error>(types, "because some of them are numbers and some of them are not", ErrorCodes::NO_COMMON_TYPE);
|
2021-06-12 15:10:25 +00:00
|
|
|
|
|
|
|
/// If there are signed and unsigned types of same bit-width, the result must be signed number with at least one more bit.
|
|
|
|
/// Example, common of Int32, UInt32 = Int64.
|
|
|
|
|
|
|
|
size_t min_bit_width_of_integer = std::max(max_bits_of_signed_integer, max_bits_of_unsigned_integer);
|
|
|
|
|
|
|
|
/// If unsigned is not covered by signed.
|
|
|
|
if (max_bits_of_signed_integer && max_bits_of_unsigned_integer >= max_bits_of_signed_integer) //-V1051
|
|
|
|
{
|
|
|
|
// Because 128 and 256 bit integers are significantly slower, we should not promote to them.
|
|
|
|
// But if we already have wide numbers, promotion is necessary.
|
|
|
|
if (min_bit_width_of_integer != 64)
|
|
|
|
++min_bit_width_of_integer;
|
|
|
|
else
|
2022-06-22 16:25:57 +00:00
|
|
|
return throwOrReturn<on_error>(types,
|
|
|
|
"because some of them are signed integers and some are unsigned integers,"
|
|
|
|
" but there is no signed integer type, that can exactly represent all required unsigned integer values",
|
2021-06-12 15:10:25 +00:00
|
|
|
ErrorCodes::NO_COMMON_TYPE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If the result must be floating.
|
|
|
|
if (max_mantissa_bits_of_floating)
|
2017-12-07 00:31:46 +00:00
|
|
|
{
|
2021-06-12 15:10:25 +00:00
|
|
|
size_t min_mantissa_bits = std::max(min_bit_width_of_integer, max_mantissa_bits_of_floating);
|
|
|
|
if (min_mantissa_bits <= 24)
|
|
|
|
return std::make_shared<DataTypeFloat32>();
|
|
|
|
else if (min_mantissa_bits <= 53)
|
|
|
|
return std::make_shared<DataTypeFloat64>();
|
|
|
|
else
|
2022-06-22 16:25:57 +00:00
|
|
|
return throwOrReturn<on_error>(types,
|
|
|
|
" because some of them are integers and some are floating point,"
|
2021-06-12 15:10:25 +00:00
|
|
|
" but there is no floating point type, that can exactly represent all required integers", ErrorCodes::NO_COMMON_TYPE);
|
|
|
|
}
|
2017-12-07 00:31:46 +00:00
|
|
|
|
2021-06-12 15:10:25 +00:00
|
|
|
/// If the result must be signed integer.
|
|
|
|
if (max_bits_of_signed_integer)
|
|
|
|
{
|
|
|
|
if (min_bit_width_of_integer <= 8)
|
|
|
|
return std::make_shared<DataTypeInt8>();
|
|
|
|
else if (min_bit_width_of_integer <= 16)
|
|
|
|
return std::make_shared<DataTypeInt16>();
|
|
|
|
else if (min_bit_width_of_integer <= 32)
|
|
|
|
return std::make_shared<DataTypeInt32>();
|
|
|
|
else if (min_bit_width_of_integer <= 64)
|
|
|
|
return std::make_shared<DataTypeInt64>();
|
|
|
|
else if (min_bit_width_of_integer <= 128)
|
|
|
|
return std::make_shared<DataTypeInt128>();
|
|
|
|
else if (min_bit_width_of_integer <= 256)
|
|
|
|
return std::make_shared<DataTypeInt256>();
|
|
|
|
else
|
2022-06-28 14:21:21 +00:00
|
|
|
return throwOrReturn<on_error>(types,
|
2022-06-22 16:25:57 +00:00
|
|
|
" because some of them are signed integers and some are unsigned integers,"
|
2021-06-12 15:10:25 +00:00
|
|
|
" but there is no signed integer type, that can exactly represent all required unsigned integer values", ErrorCodes::NO_COMMON_TYPE);
|
2017-12-07 00:31:46 +00:00
|
|
|
}
|
|
|
|
|
2021-06-12 15:10:25 +00:00
|
|
|
/// All unsigned.
|
|
|
|
{
|
|
|
|
if (min_bit_width_of_integer <= 8)
|
|
|
|
return std::make_shared<DataTypeUInt8>();
|
|
|
|
else if (min_bit_width_of_integer <= 16)
|
|
|
|
return std::make_shared<DataTypeUInt16>();
|
|
|
|
else if (min_bit_width_of_integer <= 32)
|
|
|
|
return std::make_shared<DataTypeUInt32>();
|
|
|
|
else if (min_bit_width_of_integer <= 64)
|
|
|
|
return std::make_shared<DataTypeUInt64>();
|
|
|
|
else if (min_bit_width_of_integer <= 128)
|
|
|
|
return std::make_shared<DataTypeUInt128>();
|
|
|
|
else if (min_bit_width_of_integer <= 256)
|
|
|
|
return std::make_shared<DataTypeUInt256>();
|
|
|
|
else
|
2022-06-28 14:21:21 +00:00
|
|
|
return throwOrReturn<on_error>(types,
|
2022-06-22 16:25:57 +00:00
|
|
|
" but as all data types are unsigned integers, we must have found maximum unsigned integer type", ErrorCodes::NO_COMMON_TYPE);
|
2021-06-12 15:10:25 +00:00
|
|
|
}
|
2017-12-07 00:31:46 +00:00
|
|
|
}
|
2021-06-12 15:10:25 +00:00
|
|
|
|
|
|
|
return {};
|
2017-12-07 00:31:46 +00:00
|
|
|
}
|
|
|
|
|
2021-06-12 15:10:25 +00:00
|
|
|
}
|
2017-12-07 00:31:46 +00:00
|
|
|
|
2022-06-22 16:25:57 +00:00
|
|
|
template <LeastSupertypeOnError on_error>
|
|
|
|
DataTypePtr getLeastSupertype(const DataTypes & types)
|
2017-12-07 00:31:46 +00:00
|
|
|
{
|
|
|
|
/// Trivial cases
|
|
|
|
|
|
|
|
if (types.empty())
|
2017-12-07 08:31:47 +00:00
|
|
|
return std::make_shared<DataTypeNothing>();
|
2017-12-07 00:31:46 +00:00
|
|
|
|
|
|
|
if (types.size() == 1)
|
|
|
|
return types[0];
|
|
|
|
|
|
|
|
/// All types are equal
|
|
|
|
{
|
|
|
|
bool all_equal = true;
|
|
|
|
for (size_t i = 1, size = types.size(); i < size; ++i)
|
|
|
|
{
|
|
|
|
if (!types[i]->equals(*types[0]))
|
|
|
|
{
|
|
|
|
all_equal = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (all_equal)
|
|
|
|
return types[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Recursive rules
|
|
|
|
|
2017-12-09 06:32:22 +00:00
|
|
|
/// If there are Nothing types, skip them
|
|
|
|
{
|
|
|
|
DataTypes non_nothing_types;
|
|
|
|
non_nothing_types.reserve(types.size());
|
|
|
|
|
|
|
|
for (const auto & type : types)
|
|
|
|
if (!typeid_cast<const DataTypeNothing *>(type.get()))
|
|
|
|
non_nothing_types.emplace_back(type);
|
|
|
|
|
|
|
|
if (non_nothing_types.size() < types.size())
|
2022-06-22 16:25:57 +00:00
|
|
|
return getLeastSupertype<on_error>(non_nothing_types);
|
2017-12-09 06:32:22 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 00:31:46 +00:00
|
|
|
/// For Arrays
|
|
|
|
{
|
|
|
|
bool have_array = false;
|
|
|
|
bool all_arrays = true;
|
|
|
|
|
|
|
|
DataTypes nested_types;
|
|
|
|
nested_types.reserve(types.size());
|
|
|
|
|
|
|
|
for (const auto & type : types)
|
|
|
|
{
|
|
|
|
if (const DataTypeArray * type_array = typeid_cast<const DataTypeArray *>(type.get()))
|
|
|
|
{
|
|
|
|
have_array = true;
|
|
|
|
nested_types.emplace_back(type_array->getNestedType());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
all_arrays = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (have_array)
|
|
|
|
{
|
|
|
|
if (!all_arrays)
|
2022-06-22 16:25:57 +00:00
|
|
|
return throwOrReturn<on_error>(types, "because some of them are Array and some of them are not", ErrorCodes::NO_COMMON_TYPE);
|
2017-12-07 00:31:46 +00:00
|
|
|
|
2022-07-11 14:46:34 +00:00
|
|
|
auto nested_type = getLeastSupertype<on_error>(nested_types);
|
|
|
|
/// When on_error == LeastSupertypeOnError::Null and we cannot get least supertype,
|
|
|
|
/// nested_type will be nullptr, we should return nullptr in this case.
|
|
|
|
if (!nested_type)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeArray>(nested_type);
|
2017-12-07 00:31:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// For tuples
|
|
|
|
{
|
|
|
|
bool have_tuple = false;
|
|
|
|
bool all_tuples = true;
|
|
|
|
size_t tuple_size = 0;
|
|
|
|
|
|
|
|
std::vector<DataTypes> nested_types;
|
|
|
|
|
|
|
|
for (const auto & type : types)
|
|
|
|
{
|
|
|
|
if (const DataTypeTuple * type_tuple = typeid_cast<const DataTypeTuple *>(type.get()))
|
|
|
|
{
|
|
|
|
if (!have_tuple)
|
|
|
|
{
|
|
|
|
tuple_size = type_tuple->getElements().size();
|
|
|
|
nested_types.resize(tuple_size);
|
|
|
|
for (size_t elem_idx = 0; elem_idx < tuple_size; ++elem_idx)
|
|
|
|
nested_types[elem_idx].reserve(types.size());
|
|
|
|
}
|
|
|
|
else if (tuple_size != type_tuple->getElements().size())
|
2022-06-22 16:25:57 +00:00
|
|
|
return throwOrReturn<on_error>(types, "because Tuples have different sizes", ErrorCodes::NO_COMMON_TYPE);
|
2017-12-07 00:31:46 +00:00
|
|
|
|
|
|
|
have_tuple = true;
|
|
|
|
|
|
|
|
for (size_t elem_idx = 0; elem_idx < tuple_size; ++elem_idx)
|
|
|
|
nested_types[elem_idx].emplace_back(type_tuple->getElements()[elem_idx]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
all_tuples = false;
|
2017-12-08 09:45:07 +00:00
|
|
|
}
|
2017-12-07 00:31:46 +00:00
|
|
|
|
2017-12-08 09:45:07 +00:00
|
|
|
if (have_tuple)
|
|
|
|
{
|
|
|
|
if (!all_tuples)
|
2022-06-22 16:25:57 +00:00
|
|
|
return throwOrReturn<on_error>(types, "because some of them are Tuple and some of them are not", ErrorCodes::NO_COMMON_TYPE);
|
2017-12-07 00:31:46 +00:00
|
|
|
|
2017-12-08 09:45:07 +00:00
|
|
|
DataTypes common_tuple_types(tuple_size);
|
|
|
|
for (size_t elem_idx = 0; elem_idx < tuple_size; ++elem_idx)
|
2022-07-11 14:46:34 +00:00
|
|
|
{
|
|
|
|
auto common_type = getLeastSupertype<on_error>(nested_types[elem_idx]);
|
|
|
|
/// When on_error == LeastSupertypeOnError::Null and we cannot get least supertype,
|
|
|
|
/// common_type will be nullptr, we should return nullptr in this case.
|
|
|
|
if (!common_type)
|
|
|
|
return nullptr;
|
|
|
|
common_tuple_types[elem_idx] = common_type;
|
|
|
|
}
|
2017-12-07 00:31:46 +00:00
|
|
|
|
2017-12-08 09:45:07 +00:00
|
|
|
return std::make_shared<DataTypeTuple>(common_tuple_types);
|
2017-12-07 00:31:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-24 16:45:38 +00:00
|
|
|
/// For maps
|
|
|
|
{
|
|
|
|
bool have_maps = false;
|
|
|
|
bool all_maps = true;
|
|
|
|
DataTypes key_types;
|
|
|
|
DataTypes value_types;
|
|
|
|
key_types.reserve(types.size());
|
|
|
|
value_types.reserve(types.size());
|
|
|
|
|
|
|
|
for (const auto & type : types)
|
|
|
|
{
|
|
|
|
if (const DataTypeMap * type_map = typeid_cast<const DataTypeMap *>(type.get()))
|
|
|
|
{
|
|
|
|
have_maps = true;
|
|
|
|
key_types.emplace_back(type_map->getKeyType());
|
|
|
|
value_types.emplace_back(type_map->getValueType());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
all_maps = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (have_maps)
|
|
|
|
{
|
|
|
|
if (!all_maps)
|
2022-06-22 16:25:57 +00:00
|
|
|
return throwOrReturn<on_error>(types, "because some of them are Maps and some of them are not", ErrorCodes::NO_COMMON_TYPE);
|
2021-02-24 16:45:38 +00:00
|
|
|
|
2022-07-11 14:46:34 +00:00
|
|
|
auto keys_common_type = getLeastSupertype<on_error>(key_types);
|
|
|
|
auto values_common_type = getLeastSupertype<on_error>(value_types);
|
|
|
|
/// When on_error == LeastSupertypeOnError::Null and we cannot get least supertype for keys or values,
|
|
|
|
/// keys_common_type or values_common_type will be nullptr, we should return nullptr in this case.
|
|
|
|
if (!keys_common_type || !values_common_type)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return std::make_shared<DataTypeMap>(keys_common_type, values_common_type);
|
2021-02-24 16:45:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-07 22:26:43 +00:00
|
|
|
/// For LowCardinality. This is above Nullable, because LowCardinality can contain Nullable but cannot be inside Nullable.
|
2017-12-07 00:31:46 +00:00
|
|
|
{
|
2020-07-07 22:26:43 +00:00
|
|
|
bool have_low_cardinality = false;
|
|
|
|
bool have_not_low_cardinality = false;
|
2017-12-07 00:31:46 +00:00
|
|
|
|
|
|
|
DataTypes nested_types;
|
|
|
|
nested_types.reserve(types.size());
|
|
|
|
|
|
|
|
for (const auto & type : types)
|
|
|
|
{
|
2020-07-07 22:26:43 +00:00
|
|
|
if (const DataTypeLowCardinality * type_low_cardinality = typeid_cast<const DataTypeLowCardinality *>(type.get()))
|
2017-12-07 00:31:46 +00:00
|
|
|
{
|
2020-07-07 22:26:43 +00:00
|
|
|
have_low_cardinality = true;
|
|
|
|
nested_types.emplace_back(type_low_cardinality->getDictionaryType());
|
2017-12-07 00:31:46 +00:00
|
|
|
}
|
|
|
|
else
|
2020-07-07 22:26:43 +00:00
|
|
|
{
|
|
|
|
have_not_low_cardinality = true;
|
2017-12-07 00:31:46 +00:00
|
|
|
nested_types.emplace_back(type);
|
2020-07-07 22:26:43 +00:00
|
|
|
}
|
2017-12-07 00:31:46 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 22:26:43 +00:00
|
|
|
/// All LowCardinality gives LowCardinality.
|
|
|
|
/// LowCardinality with high cardinality gives high cardinality.
|
|
|
|
if (have_low_cardinality)
|
2017-12-07 00:31:46 +00:00
|
|
|
{
|
2020-07-07 22:26:43 +00:00
|
|
|
if (have_not_low_cardinality)
|
2022-06-22 16:25:57 +00:00
|
|
|
return getLeastSupertype<on_error>(nested_types);
|
2020-07-07 22:26:43 +00:00
|
|
|
else
|
2022-07-11 14:46:34 +00:00
|
|
|
{
|
|
|
|
auto nested_type = getLeastSupertype<on_error>(nested_types);
|
|
|
|
/// When on_error == LeastSupertypeOnError::Null and we cannot get least supertype,
|
|
|
|
/// nested_type will be nullptr, we should return nullptr in this case.
|
|
|
|
if (!nested_type)
|
|
|
|
return nullptr;
|
|
|
|
return std::make_shared<DataTypeLowCardinality>(nested_type);
|
|
|
|
}
|
2017-12-07 00:31:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-07 22:26:43 +00:00
|
|
|
/// For Nullable
|
2020-07-07 22:10:23 +00:00
|
|
|
{
|
2020-07-07 22:26:43 +00:00
|
|
|
bool have_nullable = false;
|
2020-07-07 22:10:23 +00:00
|
|
|
|
|
|
|
DataTypes nested_types;
|
|
|
|
nested_types.reserve(types.size());
|
|
|
|
|
|
|
|
for (const auto & type : types)
|
|
|
|
{
|
2020-07-07 22:26:43 +00:00
|
|
|
if (const DataTypeNullable * type_nullable = typeid_cast<const DataTypeNullable *>(type.get()))
|
2020-07-07 22:10:23 +00:00
|
|
|
{
|
2020-07-07 22:26:43 +00:00
|
|
|
have_nullable = true;
|
|
|
|
|
|
|
|
if (!type_nullable->onlyNull())
|
|
|
|
nested_types.emplace_back(type_nullable->getNestedType());
|
2020-07-07 22:10:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
nested_types.emplace_back(type);
|
|
|
|
}
|
|
|
|
|
2020-07-07 22:26:43 +00:00
|
|
|
if (have_nullable)
|
2020-07-07 22:10:23 +00:00
|
|
|
{
|
2022-07-11 14:46:34 +00:00
|
|
|
auto nested_type = getLeastSupertype<on_error>(nested_types);
|
|
|
|
/// When on_error == LeastSupertypeOnError::Null and we cannot get least supertype,
|
|
|
|
/// nested_type will be nullptr, we should return nullptr in this case.
|
|
|
|
if (!nested_type)
|
|
|
|
return nullptr;
|
|
|
|
return std::make_shared<DataTypeNullable>(nested_type);
|
2020-07-07 22:10:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-07 00:31:46 +00:00
|
|
|
/// Non-recursive rules
|
|
|
|
|
2021-06-12 15:10:25 +00:00
|
|
|
TypeIndexSet type_ids;
|
2018-09-06 10:48:54 +00:00
|
|
|
for (const auto & type : types)
|
|
|
|
type_ids.insert(type->getTypeId());
|
|
|
|
|
2017-12-07 00:31:46 +00:00
|
|
|
/// For String and FixedString, or for different FixedStrings, the common type is String.
|
2017-12-09 10:14:45 +00:00
|
|
|
/// No other types are compatible with Strings. TODO Enums?
|
2017-12-07 00:31:46 +00:00
|
|
|
{
|
2022-10-07 10:46:45 +00:00
|
|
|
size_t have_string = type_ids.count(TypeIndex::String);
|
|
|
|
size_t have_fixed_string = type_ids.count(TypeIndex::FixedString);
|
2017-12-07 00:31:46 +00:00
|
|
|
|
2018-09-06 10:48:54 +00:00
|
|
|
if (have_string || have_fixed_string)
|
2017-12-07 00:31:46 +00:00
|
|
|
{
|
2018-09-06 10:48:54 +00:00
|
|
|
bool all_strings = type_ids.size() == (have_string + have_fixed_string);
|
2017-12-07 00:31:46 +00:00
|
|
|
if (!all_strings)
|
2022-06-22 16:25:57 +00:00
|
|
|
return throwOrReturn<on_error>(types, "because some of them are String/FixedString and some of them are not", ErrorCodes::NO_COMMON_TYPE);
|
2017-12-07 00:31:46 +00:00
|
|
|
|
|
|
|
return std::make_shared<DataTypeString>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 17:39:30 +00:00
|
|
|
/// For Date and DateTime/DateTime64, the common type is DateTime/DateTime64. No other types are compatible.
|
2017-12-07 00:31:46 +00:00
|
|
|
{
|
2022-10-07 10:46:45 +00:00
|
|
|
size_t have_date = type_ids.count(TypeIndex::Date);
|
|
|
|
size_t have_date32 = type_ids.count(TypeIndex::Date32);
|
|
|
|
size_t have_datetime = type_ids.count(TypeIndex::DateTime);
|
|
|
|
size_t have_datetime64 = type_ids.count(TypeIndex::DateTime64);
|
2017-12-07 00:31:46 +00:00
|
|
|
|
2021-10-15 08:09:37 +00:00
|
|
|
if (have_date || have_date32 || have_datetime || have_datetime64)
|
2017-12-07 00:31:46 +00:00
|
|
|
{
|
2021-10-15 08:09:37 +00:00
|
|
|
bool all_date_or_datetime = type_ids.size() == (have_date + have_date32 + have_datetime + have_datetime64);
|
2017-12-07 00:31:46 +00:00
|
|
|
if (!all_date_or_datetime)
|
2022-06-22 16:25:57 +00:00
|
|
|
return throwOrReturn<on_error>(types,
|
|
|
|
"because some of them are Date/Date32/DateTime/DateTime64 and some of them are not",
|
2020-06-23 17:39:30 +00:00
|
|
|
ErrorCodes::NO_COMMON_TYPE);
|
2017-12-07 00:31:46 +00:00
|
|
|
|
2021-10-15 08:09:37 +00:00
|
|
|
if (have_datetime64 == 0 && have_date32 == 0)
|
2021-05-14 20:07:08 +00:00
|
|
|
{
|
2021-06-28 16:56:15 +00:00
|
|
|
for (const auto & type : types)
|
2021-05-14 20:07:08 +00:00
|
|
|
{
|
2021-06-28 16:56:15 +00:00
|
|
|
if (isDateTime(type))
|
|
|
|
return type;
|
2021-05-14 20:07:08 +00:00
|
|
|
}
|
|
|
|
|
2019-10-16 07:24:48 +00:00
|
|
|
return std::make_shared<DataTypeDateTime>();
|
2021-05-14 20:07:08 +00:00
|
|
|
}
|
2019-10-16 07:24:48 +00:00
|
|
|
|
2021-10-18 08:53:25 +00:00
|
|
|
/// For Date and Date32, the common type is Date32
|
|
|
|
if (have_datetime == 0 && have_datetime64 == 0)
|
2021-10-15 08:09:37 +00:00
|
|
|
{
|
|
|
|
for (const auto & type : types)
|
|
|
|
{
|
|
|
|
if (isDate32(type))
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 08:53:25 +00:00
|
|
|
/// For Datetime and Date32, the common type is Datetime64
|
|
|
|
if (have_datetime == 1 && have_date32 == 1 && have_datetime64 == 0)
|
|
|
|
{
|
|
|
|
return std::make_shared<DataTypeDateTime64>(0);
|
|
|
|
}
|
|
|
|
|
2020-06-23 17:39:30 +00:00
|
|
|
UInt8 max_scale = 0;
|
2021-06-28 16:56:15 +00:00
|
|
|
size_t max_scale_date_time_index = 0;
|
2019-10-16 07:24:48 +00:00
|
|
|
|
2021-06-28 16:56:15 +00:00
|
|
|
for (size_t i = 0; i < types.size(); ++i)
|
2019-10-16 07:24:48 +00:00
|
|
|
{
|
2021-06-28 16:56:15 +00:00
|
|
|
const auto & type = types[i];
|
|
|
|
|
|
|
|
if (const auto * date_time64_type = typeid_cast<const DataTypeDateTime64 *>(type.get()))
|
2019-10-16 07:24:48 +00:00
|
|
|
{
|
2021-06-28 16:56:15 +00:00
|
|
|
const auto scale = date_time64_type->getScale();
|
2021-06-29 08:05:07 +00:00
|
|
|
if (scale >= max_scale)
|
2021-05-14 20:07:08 +00:00
|
|
|
{
|
2021-06-28 16:56:15 +00:00
|
|
|
max_scale_date_time_index = i;
|
2020-06-23 17:39:30 +00:00
|
|
|
max_scale = scale;
|
2021-05-14 20:07:08 +00:00
|
|
|
}
|
2019-10-16 07:24:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-28 16:56:15 +00:00
|
|
|
return types[max_scale_date_time_index];
|
2017-12-07 00:31:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-06 10:48:54 +00:00
|
|
|
/// Decimals
|
|
|
|
{
|
2022-10-07 10:46:45 +00:00
|
|
|
size_t have_decimal32 = type_ids.count(TypeIndex::Decimal32);
|
|
|
|
size_t have_decimal64 = type_ids.count(TypeIndex::Decimal64);
|
|
|
|
size_t have_decimal128 = type_ids.count(TypeIndex::Decimal128);
|
2018-09-06 10:48:54 +00:00
|
|
|
|
|
|
|
if (have_decimal32 || have_decimal64 || have_decimal128)
|
|
|
|
{
|
2022-10-07 10:46:45 +00:00
|
|
|
size_t num_supported = have_decimal32 + have_decimal64 + have_decimal128;
|
2018-09-26 14:59:23 +00:00
|
|
|
|
|
|
|
std::vector<TypeIndex> int_ids = {TypeIndex::Int8, TypeIndex::UInt8, TypeIndex::Int16, TypeIndex::UInt16,
|
2022-10-07 10:46:45 +00:00
|
|
|
TypeIndex::Int32, TypeIndex::UInt32, TypeIndex::Int64, TypeIndex::UInt64};
|
2018-09-26 14:59:23 +00:00
|
|
|
|
|
|
|
TypeIndex max_int = TypeIndex::Nothing;
|
2022-10-22 11:17:27 +00:00
|
|
|
for (auto int_id : int_ids)
|
2018-09-26 14:59:23 +00:00
|
|
|
{
|
2022-10-22 11:17:27 +00:00
|
|
|
size_t num = type_ids.count(int_id);
|
2018-09-26 14:59:23 +00:00
|
|
|
num_supported += num;
|
|
|
|
if (num)
|
2022-10-22 11:17:27 +00:00
|
|
|
max_int = int_id;
|
2018-09-26 14:59:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (num_supported != type_ids.size())
|
2022-06-22 16:25:57 +00:00
|
|
|
return throwOrReturn<on_error>(types, "because some of them have no lossless conversion to Decimal", ErrorCodes::NO_COMMON_TYPE);
|
2018-09-06 10:48:54 +00:00
|
|
|
|
|
|
|
UInt32 max_scale = 0;
|
|
|
|
for (const auto & type : types)
|
|
|
|
{
|
2022-07-20 15:54:12 +00:00
|
|
|
auto type_id = type->getTypeId();
|
|
|
|
if (type_id != TypeIndex::Decimal32 && type_id != TypeIndex::Decimal64 && type_id != TypeIndex::Decimal128)
|
|
|
|
continue;
|
|
|
|
|
2022-07-19 16:46:52 +00:00
|
|
|
UInt32 scale = getDecimalScale(*type);
|
2018-09-06 10:48:54 +00:00
|
|
|
if (scale > max_scale)
|
|
|
|
max_scale = scale;
|
|
|
|
}
|
|
|
|
|
2018-09-26 14:59:23 +00:00
|
|
|
UInt32 min_precision = max_scale + leastDecimalPrecisionFor(max_int);
|
|
|
|
|
|
|
|
/// special cases Int32 -> Dec32, Int64 -> Dec64
|
|
|
|
if (max_scale == 0)
|
|
|
|
{
|
|
|
|
if (max_int == TypeIndex::Int32)
|
|
|
|
min_precision = DataTypeDecimal<Decimal32>::maxPrecision();
|
|
|
|
else if (max_int == TypeIndex::Int64)
|
|
|
|
min_precision = DataTypeDecimal<Decimal64>::maxPrecision();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (min_precision > DataTypeDecimal<Decimal128>::maxPrecision())
|
2022-06-22 16:25:57 +00:00
|
|
|
return throwOrReturn<on_error>(types, "because the least supertype is Decimal("
|
2018-09-26 14:59:23 +00:00
|
|
|
+ toString(min_precision) + ',' + toString(max_scale) + ')',
|
|
|
|
ErrorCodes::NO_COMMON_TYPE);
|
|
|
|
|
|
|
|
if (have_decimal128 || min_precision > DataTypeDecimal<Decimal64>::maxPrecision())
|
2018-09-06 10:48:54 +00:00
|
|
|
return std::make_shared<DataTypeDecimal<Decimal128>>(DataTypeDecimal<Decimal128>::maxPrecision(), max_scale);
|
2018-09-26 14:59:23 +00:00
|
|
|
if (have_decimal64 || min_precision > DataTypeDecimal<Decimal32>::maxPrecision())
|
2018-09-06 10:48:54 +00:00
|
|
|
return std::make_shared<DataTypeDecimal<Decimal64>>(DataTypeDecimal<Decimal64>::maxPrecision(), max_scale);
|
|
|
|
return std::make_shared<DataTypeDecimal<Decimal32>>(DataTypeDecimal<Decimal32>::maxPrecision(), max_scale);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-07 00:31:46 +00:00
|
|
|
/// For numeric types, the most complicated part.
|
|
|
|
{
|
2022-06-22 16:25:57 +00:00
|
|
|
auto numeric_type = getNumericType<on_error>(type_ids);
|
2021-06-12 15:10:25 +00:00
|
|
|
if (numeric_type)
|
|
|
|
return numeric_type;
|
|
|
|
}
|
2017-12-07 00:31:46 +00:00
|
|
|
|
2021-06-12 15:10:25 +00:00
|
|
|
/// All other data types (UUID, AggregateFunction, Enum...) are compatible only if they are the same (checked in trivial cases).
|
2022-06-22 16:25:57 +00:00
|
|
|
return throwOrReturn<on_error>(types, "", ErrorCodes::NO_COMMON_TYPE);
|
2021-06-12 15:10:25 +00:00
|
|
|
}
|
2017-12-07 00:31:46 +00:00
|
|
|
|
2022-06-22 16:25:57 +00:00
|
|
|
DataTypePtr getLeastSupertypeOrString(const DataTypes & types)
|
2021-06-12 15:10:25 +00:00
|
|
|
{
|
2022-06-22 16:25:57 +00:00
|
|
|
return getLeastSupertype<LeastSupertypeOnError::String>(types);
|
|
|
|
}
|
2017-12-07 00:31:46 +00:00
|
|
|
|
2022-06-22 16:25:57 +00:00
|
|
|
DataTypePtr tryGetLeastSupertype(const DataTypes & types)
|
|
|
|
{
|
|
|
|
return getLeastSupertype<LeastSupertypeOnError::Null>(types);
|
|
|
|
}
|
2017-12-07 00:31:46 +00:00
|
|
|
|
2022-06-22 16:25:57 +00:00
|
|
|
template <LeastSupertypeOnError on_error>
|
|
|
|
DataTypePtr getLeastSupertype(const TypeIndexSet & types)
|
|
|
|
{
|
|
|
|
if (types.empty())
|
|
|
|
return std::make_shared<DataTypeNothing>();
|
|
|
|
|
|
|
|
if (types.size() == 1)
|
2021-06-12 15:10:25 +00:00
|
|
|
{
|
2022-06-22 16:25:57 +00:00
|
|
|
WhichDataType which(*types.begin());
|
|
|
|
if (which.isNothing())
|
|
|
|
return std::make_shared<DataTypeNothing>();
|
2017-12-07 00:31:46 +00:00
|
|
|
|
2022-06-22 16:25:57 +00:00
|
|
|
#define DISPATCH(TYPE) \
|
|
|
|
if (which.idx == TypeIndex::TYPE) \
|
2022-06-28 14:21:21 +00:00
|
|
|
return std::make_shared<DataTypeNumber<TYPE>>(); /// NOLINT
|
2017-12-08 04:09:32 +00:00
|
|
|
|
2022-06-22 16:25:57 +00:00
|
|
|
FOR_NUMERIC_TYPES(DISPATCH)
|
|
|
|
#undef DISPATCH
|
2017-12-07 00:31:46 +00:00
|
|
|
|
2022-06-22 16:25:57 +00:00
|
|
|
if (which.isString())
|
|
|
|
return std::make_shared<DataTypeString>();
|
|
|
|
|
2022-06-28 14:21:21 +00:00
|
|
|
return throwOrReturn<on_error>(types, "because cannot get common type by type indexes with non-simple types", ErrorCodes::NO_COMMON_TYPE);
|
2022-06-22 16:25:57 +00:00
|
|
|
}
|
2017-12-07 00:31:46 +00:00
|
|
|
|
2022-04-18 10:18:43 +00:00
|
|
|
if (types.contains(TypeIndex::String))
|
2021-06-12 15:10:25 +00:00
|
|
|
{
|
2022-06-22 16:25:57 +00:00
|
|
|
bool only_string = types.size() == 2 && types.contains(TypeIndex::Nothing);
|
|
|
|
if (!only_string)
|
|
|
|
return throwOrReturn<on_error>(types, "because some of them are String and some of them are not", ErrorCodes::NO_COMMON_TYPE);
|
2017-12-07 00:31:46 +00:00
|
|
|
|
2021-06-12 15:10:25 +00:00
|
|
|
return std::make_shared<DataTypeString>();
|
2017-12-07 00:31:46 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 16:25:57 +00:00
|
|
|
auto numeric_type = getNumericType<on_error>(types);
|
2021-06-12 15:10:25 +00:00
|
|
|
if (numeric_type)
|
|
|
|
return numeric_type;
|
|
|
|
|
2022-06-22 16:25:57 +00:00
|
|
|
return throwOrReturn<on_error>(types, "", ErrorCodes::NO_COMMON_TYPE);
|
2017-12-07 00:31:46 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 16:25:57 +00:00
|
|
|
DataTypePtr getLeastSupertypeOrString(const TypeIndexSet & types)
|
2022-01-20 13:56:12 +00:00
|
|
|
{
|
2022-06-22 16:25:57 +00:00
|
|
|
return getLeastSupertype<LeastSupertypeOnError::String>(types);
|
2022-01-20 13:56:12 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 16:25:57 +00:00
|
|
|
DataTypePtr tryGetLeastSupertype(const TypeIndexSet & types)
|
|
|
|
{
|
|
|
|
return getLeastSupertype<LeastSupertypeOnError::Null>(types);
|
|
|
|
}
|
|
|
|
|
|
|
|
template DataTypePtr getLeastSupertype<LeastSupertypeOnError::Throw>(const DataTypes & types);
|
|
|
|
template DataTypePtr getLeastSupertype<LeastSupertypeOnError::Throw>(const TypeIndexSet & types);
|
|
|
|
|
2017-12-07 00:31:46 +00:00
|
|
|
}
|