2018-09-26 00:31:40 +00:00
|
|
|
#include <DataTypes/DataTypeDateTime.h>
|
2019-11-04 14:06:22 +00:00
|
|
|
#include <DataTypes/DataTypeDateTime64.h>
|
2018-09-26 00:31:40 +00:00
|
|
|
|
2019-10-04 17:46:36 +00:00
|
|
|
#include <Core/Field.h>
|
|
|
|
|
2019-12-09 13:12:54 +00:00
|
|
|
#include <Functions/IFunctionImpl.h>
|
2018-09-26 00:31:40 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/extractTimeZoneFromFunctionArguments.h>
|
|
|
|
|
|
|
|
#include <IO/WriteHelpers.h>
|
2019-10-07 07:45:59 +00:00
|
|
|
#include <Common/assert_cast.h>
|
2018-09-26 00:31:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Just changes time zone information for data type. The calculation is free.
|
|
|
|
class FunctionToTimeZone : public IFunction
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static constexpr auto name = "toTimeZone";
|
|
|
|
static FunctionPtr create(const Context &) { return std::make_shared<FunctionToTimeZone>(); }
|
|
|
|
|
|
|
|
String getName() const override
|
|
|
|
{
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getNumberOfArguments() const override { return 2; }
|
|
|
|
|
|
|
|
DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override
|
|
|
|
{
|
|
|
|
if (arguments.size() != 2)
|
|
|
|
throw Exception("Number of arguments for function " + getName() + " doesn't match: passed "
|
|
|
|
+ toString(arguments.size()) + ", should be 2",
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
2019-10-07 07:45:59 +00:00
|
|
|
const auto which_type = WhichDataType(arguments[0].type);
|
2019-10-08 04:50:13 +00:00
|
|
|
if (!which_type.isDateTime() && !which_type.isDateTime64())
|
2018-09-26 00:31:40 +00:00
|
|
|
throw Exception{"Illegal type " + arguments[0].type->getName() + " of argument of function " + getName() +
|
2019-10-07 07:45:59 +00:00
|
|
|
". Should be DateTime or DateTime64", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT};
|
2018-09-26 00:31:40 +00:00
|
|
|
|
|
|
|
String time_zone_name = extractTimeZoneNameFromFunctionArguments(arguments, 1, 0);
|
2019-10-07 07:45:59 +00:00
|
|
|
if (which_type.isDateTime())
|
|
|
|
return std::make_shared<DataTypeDateTime>(time_zone_name);
|
|
|
|
|
2019-10-08 04:50:13 +00:00
|
|
|
const auto * date_time64 = assert_cast<const DataTypeDateTime64 *>(arguments[0].type.get());
|
2019-10-07 07:45:59 +00:00
|
|
|
return std::make_shared<DataTypeDateTime64>(date_time64->getScale(), time_zone_name);
|
2018-09-26 00:31:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) override
|
|
|
|
{
|
|
|
|
block.getByPosition(result).column = block.getByPosition(arguments[0]).column;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void registerFunctionToTimeZone(FunctionFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerFunction<FunctionToTimeZone>();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|