mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-12-15 10:52:30 +00:00
72b9d75a84
SQL function toTimezone() converts a Date or DateTime into another timezone. The problem is that the timezone is part of the Date / DateTime type but not part of the internal representation (value). This led to the fact that toTimeZone() wqith non-const timezones produced wrong and misleading results until #48471 (shipped with v23.4) enforced a const timezone. Unfortunately, this PR also broke existing table definitions with non-const timezones, e.g. in ALIAS expressions. So while #48471 addressed the issue appropriately, it is really backwards-incompatible. This PR adds a setting to toggle the behavior and makes it also part of the compatibility profile.
84 lines
3.0 KiB
C++
84 lines
3.0 KiB
C++
#include <Functions/extractTimeZoneFromFunctionArguments.h>
|
|
#include <Functions/FunctionHelpers.h>
|
|
#include <Core/Block.h>
|
|
#include <DataTypes/DataTypeDateTime.h>
|
|
#include <DataTypes/DataTypeDateTime64.h>
|
|
#include <Columns/ColumnString.h>
|
|
#include <Common/DateLUT.h>
|
|
|
|
|
|
namespace DB
|
|
{
|
|
|
|
namespace ErrorCodes
|
|
{
|
|
extern const int ILLEGAL_COLUMN;
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
}
|
|
|
|
|
|
std::string extractTimeZoneNameFromColumn(const IColumn * column, const String & column_name)
|
|
{
|
|
const ColumnConst * time_zone_column = checkAndGetColumnConst<ColumnString>(column);
|
|
|
|
if (!time_zone_column)
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN,
|
|
"Illegal column {} of time zone argument of function, must be a constant string",
|
|
column_name);
|
|
|
|
return time_zone_column->getValue<String>();
|
|
}
|
|
|
|
|
|
std::string extractTimeZoneNameFromFunctionArguments(const ColumnsWithTypeAndName & arguments, size_t time_zone_arg_num, size_t datetime_arg_num, bool allow_nonconst_timezone_arguments)
|
|
{
|
|
/// Explicit time zone may be passed in last argument.
|
|
if ((arguments.size() == time_zone_arg_num + 1)
|
|
&& (!allow_nonconst_timezone_arguments || arguments[time_zone_arg_num].column))
|
|
{
|
|
return extractTimeZoneNameFromColumn(arguments[time_zone_arg_num].column.get(), arguments[time_zone_arg_num].name);
|
|
}
|
|
else
|
|
{
|
|
if (arguments.size() <= datetime_arg_num)
|
|
return {};
|
|
|
|
const auto & dt_arg = arguments[datetime_arg_num].type.get();
|
|
/// If time zone is attached to an argument of type DateTime.
|
|
if (const auto * type = checkAndGetDataType<DataTypeDateTime>(dt_arg))
|
|
return type->hasExplicitTimeZone() ? type->getTimeZone().getTimeZone() : std::string();
|
|
if (const auto * type = checkAndGetDataType<DataTypeDateTime64>(dt_arg))
|
|
return type->hasExplicitTimeZone() ? type->getTimeZone().getTimeZone() : std::string();
|
|
|
|
return {};
|
|
}
|
|
}
|
|
|
|
const DateLUTImpl & extractTimeZoneFromFunctionArguments(const ColumnsWithTypeAndName & arguments, size_t time_zone_arg_num, size_t datetime_arg_num)
|
|
{
|
|
if (arguments.size() == time_zone_arg_num + 1)
|
|
{
|
|
std::string time_zone = extractTimeZoneNameFromColumn(arguments[time_zone_arg_num].column.get(), arguments[time_zone_arg_num].name);
|
|
if (time_zone.empty())
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Provided time zone must be non-empty and be a valid time zone");
|
|
return DateLUT::instance(time_zone);
|
|
}
|
|
else
|
|
{
|
|
if (arguments.size() <= datetime_arg_num)
|
|
return DateLUT::instance();
|
|
|
|
const auto & dt_arg = arguments[datetime_arg_num].type.get();
|
|
/// If time zone is attached to an argument of type DateTime.
|
|
if (const auto * type = checkAndGetDataType<DataTypeDateTime>(dt_arg))
|
|
return type->getTimeZone();
|
|
if (const auto * type = checkAndGetDataType<DataTypeDateTime64>(dt_arg))
|
|
return type->getTimeZone();
|
|
|
|
return DateLUT::instance();
|
|
}
|
|
}
|
|
|
|
}
|
|
|