Fix timezone issues

This commit is contained in:
Roman Vasin 2022-09-28 16:05:59 +00:00
parent a551e8d950
commit e66e70870a
2 changed files with 7 additions and 6 deletions

View File

@ -28,15 +28,16 @@ public:
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
{
this->checkArguments(arguments, /*is_result_type_date_or_date32*/ true);
this->checkArguments(arguments, /*is_result_type_date_or_date32*/ false);
const IDataType * from_type = arguments[0].type.get();
WhichDataType which(from_type);
std::string time_zone = extractTimeZoneNameFromFunctionArguments(arguments, 1, 0);
/// If the time zone is specified but empty, throw an exception.
/// only validate the time_zone part if the number of arguments is 2.
if ((which.isDateTime() || which.isDateTime64()) && arguments.size() == 2
&& extractTimeZoneNameFromFunctionArguments(arguments, 1, 0).empty())
if (arguments.size() == 2 && time_zone.empty())
throw Exception(
"Function " + this->getName() + " supports a 2nd argument (optional) that must be non-empty and be a valid time zone",
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
@ -49,10 +50,10 @@ public:
if (const auto * dt64 = checkAndGetDataType<DataTypeDateTime64>(arguments[0].type.get()))
scale = dt64->getScale();
}
return std::make_shared<DataTypeDateTime64>(scale, extractTimeZoneNameFromFunctionArguments(arguments, 1, 0));
return std::make_shared<DataTypeDateTime64>(scale, time_zone);
}
else
return std::make_shared<DataTypeDateTime>();
return std::make_shared<DataTypeDateTime>(time_zone);
}
ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr & result_type, size_t input_rows_count) const override

View File

@ -102,7 +102,7 @@ protected:
"Function " + getName() + " supports 1 or 2 arguments. The optional 2nd argument must be "
"a constant string with a timezone name",
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
if ((isDate(arguments[0].type) || isDate32(arguments[0].type)) && is_result_type_date_or_date32)
if (isDateOrDate32(arguments[0].type) && is_result_type_date_or_date32)
throw Exception(
"The timezone argument of function " + getName() + " is allowed only when the 1st argument has the type DateTime or DateTime64",
ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);