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>
|
|
|
|
#include <common/DateLUT.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int ILLEGAL_COLUMN;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static std::string extractTimeZoneNameFromColumn(const IColumn & column)
|
|
|
|
{
|
|
|
|
const ColumnConst * time_zone_column = checkAndGetColumnConst<ColumnString>(&column);
|
|
|
|
|
|
|
|
if (!time_zone_column)
|
|
|
|
throw Exception("Illegal column " + column.getName()
|
|
|
|
+ " of time zone argument of function, must be constant string",
|
|
|
|
ErrorCodes::ILLEGAL_COLUMN);
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2020-03-08 21:40:00 +00:00
|
|
|
if (arguments.empty())
|
2018-09-26 00:31:40 +00:00
|
|
|
return {};
|
|
|
|
|
|
|
|
/// If time zone is attached to an argument of type DateTime.
|
2020-03-10 07:06:51 +00:00
|
|
|
if (const auto * type = checkAndGetDataType<DataTypeDateTime>(arguments[datetime_arg_num].type.get()))
|
2018-09-26 00:31:40 +00:00
|
|
|
return type->getTimeZone().getTimeZone();
|
2020-03-10 07:06:51 +00:00
|
|
|
if (const auto * type = checkAndGetDataType<DataTypeDateTime64>(arguments[datetime_arg_num].type.get()))
|
2019-10-09 19:53:57 +00:00
|
|
|
return type->getTimeZone().getTimeZone();
|
2018-09-26 00:31:40 +00:00
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const DateLUTImpl & extractTimeZoneFromFunctionArguments(Block & block, const ColumnNumbers & arguments, size_t time_zone_arg_num, size_t datetime_arg_num)
|
|
|
|
{
|
|
|
|
if (arguments.size() == time_zone_arg_num + 1)
|
2020-09-05 03:15:37 +00:00
|
|
|
{
|
|
|
|
std::string time_zone = extractTimeZoneNameFromColumn(*block.getByPosition(arguments[time_zone_arg_num]).column);
|
|
|
|
if (time_zone.empty())
|
|
|
|
throw Exception("Provided time zone must be non-empty and be a valid time zone", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT);
|
|
|
|
return DateLUT::instance(time_zone);
|
|
|
|
}
|
2018-09-26 00:31:40 +00:00
|
|
|
else
|
|
|
|
{
|
2020-03-08 21:40:00 +00:00
|
|
|
if (arguments.empty())
|
2018-09-26 00:31:40 +00:00
|
|
|
return DateLUT::instance();
|
|
|
|
|
|
|
|
/// If time zone is attached to an argument of type DateTime.
|
2020-03-10 07:06:51 +00:00
|
|
|
if (const auto * type = checkAndGetDataType<DataTypeDateTime>(block.getByPosition(arguments[datetime_arg_num]).type.get()))
|
|
|
|
return type->getTimeZone();
|
|
|
|
if (const auto * type = checkAndGetDataType<DataTypeDateTime64>(block.getByPosition(arguments[datetime_arg_num]).type.get()))
|
2018-09-26 00:31:40 +00:00
|
|
|
return type->getTimeZone();
|
|
|
|
|
|
|
|
return DateLUT::instance();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|