2018-09-26 00:31:40 +00:00
|
|
|
#include <Functions/extractTimeZoneFromFunctionArguments.h>
|
|
|
|
#include <Functions/FunctionHelpers.h>
|
|
|
|
#include <Core/Block.h>
|
|
|
|
#include <DataTypes/DataTypeDateTime.h>
|
2019-10-14 08:38:03 +00:00
|
|
|
#include <DataTypes/DataTypeDateTime64.h>
|
2018-09-26 00:31:40 +00:00
|
|
|
#include <Columns/ColumnString.h>
|
2021-12-21 13:41:53 +00:00
|
|
|
#include <Common/DateLUT.h>
|
2018-09-26 00:31:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
2020-09-05 03:36:58 +00:00
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-29 14:52:08 +00:00
|
|
|
std::string extractTimeZoneNameFromColumn(const IColumn & column)
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
|
|
|
const ColumnConst * time_zone_column = checkAndGetColumnConst<ColumnString>(&column);
|
|
|
|
|
|
|
|
if (!time_zone_column)
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_COLUMN,
|
|
|
|
"Illegal column {} of time zone argument of function, must be constant string",
|
|
|
|
column.getName());
|
2018-09-26 00:31:40 +00:00
|
|
|
|
|
|
|
return time_zone_column->getValue<String>();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string extractTimeZoneNameFromFunctionArguments(const ColumnsWithTypeAndName & arguments, size_t time_zone_arg_num, size_t datetime_arg_num)
|
|
|
|
{
|
|
|
|
/// Explicit time zone may be passed in last argument.
|
|
|
|
if (arguments.size() == time_zone_arg_num + 1 && arguments[time_zone_arg_num].column)
|
|
|
|
{
|
|
|
|
return extractTimeZoneNameFromColumn(*arguments[time_zone_arg_num].column);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-05-13 13:48:14 +00:00
|
|
|
if (arguments.size() <= datetime_arg_num)
|
2018-09-26 00:31:40 +00:00
|
|
|
return {};
|
|
|
|
|
2021-05-13 13:48:14 +00:00
|
|
|
const auto & dt_arg = arguments[datetime_arg_num].type.get();
|
2018-09-26 00:31:40 +00:00
|
|
|
/// If time zone is attached to an argument of type DateTime.
|
2021-05-13 13:48:14 +00:00
|
|
|
if (const auto * type = checkAndGetDataType<DataTypeDateTime>(dt_arg))
|
2021-04-20 21:08:06 +00:00
|
|
|
return type->hasExplicitTimeZone() ? type->getTimeZone().getTimeZone() : std::string();
|
2021-05-13 13:48:14 +00:00
|
|
|
if (const auto * type = checkAndGetDataType<DataTypeDateTime64>(dt_arg))
|
2021-04-20 21:08:06 +00:00
|
|
|
return type->hasExplicitTimeZone() ? type->getTimeZone().getTimeZone() : std::string();
|
2018-09-26 00:31:40 +00:00
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-17 15:32:40 +00:00
|
|
|
const DateLUTImpl & extractTimeZoneFromFunctionArguments(const ColumnsWithTypeAndName & arguments, size_t time_zone_arg_num, size_t datetime_arg_num)
|
2018-09-26 00:31:40 +00:00
|
|
|
{
|
|
|
|
if (arguments.size() == time_zone_arg_num + 1)
|
2020-09-05 03:15:37 +00:00
|
|
|
{
|
2020-10-17 14:23:37 +00:00
|
|
|
std::string time_zone = extractTimeZoneNameFromColumn(*arguments[time_zone_arg_num].column);
|
2020-09-05 03:15:37 +00:00
|
|
|
if (time_zone.empty())
|
2023-01-23 21:13:58 +00:00
|
|
|
throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Provided time zone must be non-empty and be a valid time zone");
|
2020-09-05 03:15:37 +00:00
|
|
|
return DateLUT::instance(time_zone);
|
|
|
|
}
|
2018-09-26 00:31:40 +00:00
|
|
|
else
|
|
|
|
{
|
2021-05-13 13:48:14 +00:00
|
|
|
if (arguments.size() <= datetime_arg_num)
|
2018-09-26 00:31:40 +00:00
|
|
|
return DateLUT::instance();
|
|
|
|
|
2020-04-17 13:26:44 +00:00
|
|
|
const auto & dt_arg = arguments[datetime_arg_num].type.get();
|
2018-09-26 00:31:40 +00:00
|
|
|
/// If time zone is attached to an argument of type DateTime.
|
2020-04-17 13:26:44 +00:00
|
|
|
if (const auto * type = checkAndGetDataType<DataTypeDateTime>(dt_arg))
|
2020-03-10 07:06:51 +00:00
|
|
|
return type->getTimeZone();
|
2020-04-17 13:26:44 +00:00
|
|
|
if (const auto * type = checkAndGetDataType<DataTypeDateTime64>(dt_arg))
|
2018-09-26 00:31:40 +00:00
|
|
|
return type->getTimeZone();
|
|
|
|
|
|
|
|
return DateLUT::instance();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|