2020-12-17 18:32:25 +00:00
|
|
|
#include <Interpreters/castColumn.h>
|
|
|
|
|
|
|
|
#include <Functions/FunctionsConversion.h>
|
2021-08-11 19:09:51 +00:00
|
|
|
#include <Functions/CastOverloadResolver.h>
|
2020-12-17 18:32:25 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
template <CastType cast_type = CastType::nonAccurate>
|
2023-10-23 11:31:44 +00:00
|
|
|
static ColumnPtr castColumn(const ColumnWithTypeAndName & arg, const DataTypePtr & type, InternalCastFunctionCache * cache = nullptr)
|
2020-12-17 18:32:25 +00:00
|
|
|
{
|
2021-04-13 22:23:42 +00:00
|
|
|
if (arg.type->equals(*type) && cast_type != CastType::accurateOrNull)
|
2020-12-17 18:32:25 +00:00
|
|
|
return arg.column;
|
|
|
|
|
2023-10-23 11:31:44 +00:00
|
|
|
const auto from_name = arg.type->getName();
|
|
|
|
const auto to_name = type->getName();
|
2020-12-17 18:32:25 +00:00
|
|
|
ColumnsWithTypeAndName arguments
|
|
|
|
{
|
|
|
|
arg,
|
|
|
|
{
|
2023-10-23 11:31:44 +00:00
|
|
|
DataTypeString().createColumnConst(arg.column->size(), to_name),
|
2020-12-17 18:32:25 +00:00
|
|
|
std::make_shared<DataTypeString>(),
|
|
|
|
""
|
|
|
|
}
|
|
|
|
};
|
2023-10-23 11:31:44 +00:00
|
|
|
auto get_cast_func = [&arguments]
|
|
|
|
{
|
|
|
|
FunctionOverloadResolverPtr func_builder_cast = CastInternalOverloadResolver<cast_type>::createImpl();
|
|
|
|
return func_builder_cast->build(arguments);
|
|
|
|
};
|
2020-12-17 18:32:25 +00:00
|
|
|
|
2023-10-23 11:31:44 +00:00
|
|
|
FunctionBasePtr func_cast = cache ? cache->getOrSet(cast_type, from_name, to_name, std::move(get_cast_func)) : get_cast_func();
|
2020-12-17 18:32:25 +00:00
|
|
|
|
|
|
|
if constexpr (cast_type == CastType::accurateOrNull)
|
|
|
|
{
|
|
|
|
return func_cast->execute(arguments, makeNullable(type), arg.column->size());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return func_cast->execute(arguments, type, arg.column->size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-23 11:31:44 +00:00
|
|
|
ColumnPtr castColumn(const ColumnWithTypeAndName & arg, const DataTypePtr & type, InternalCastFunctionCache * cache)
|
2020-12-17 18:32:25 +00:00
|
|
|
{
|
2023-10-23 11:31:44 +00:00
|
|
|
return castColumn<CastType::nonAccurate>(arg, type, cache);
|
2020-12-17 18:32:25 +00:00
|
|
|
}
|
|
|
|
|
2023-10-23 11:31:44 +00:00
|
|
|
ColumnPtr castColumnAccurate(const ColumnWithTypeAndName & arg, const DataTypePtr & type, InternalCastFunctionCache * cache)
|
2020-12-17 18:32:25 +00:00
|
|
|
{
|
2023-10-23 11:31:44 +00:00
|
|
|
return castColumn<CastType::accurate>(arg, type, cache);
|
2020-12-17 18:32:25 +00:00
|
|
|
}
|
|
|
|
|
2023-10-23 11:31:44 +00:00
|
|
|
ColumnPtr castColumnAccurateOrNull(const ColumnWithTypeAndName & arg, const DataTypePtr & type, InternalCastFunctionCache * cache)
|
2020-12-17 18:32:25 +00:00
|
|
|
{
|
2023-10-23 11:31:44 +00:00
|
|
|
return castColumn<CastType::accurateOrNull>(arg, type, cache);
|
2020-12-17 18:32:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|