| 1 | #include <DataTypes/DataTypeDateTime.h> |
|---|---|
| 2 | #include <DataTypes/DataTypeDateTime64.h> |
| 3 | |
| 4 | #include <Core/Field.h> |
| 5 | |
| 6 | #include <Functions/IFunctionImpl.h> |
| 7 | #include <Functions/FunctionFactory.h> |
| 8 | #include <Functions/extractTimeZoneFromFunctionArguments.h> |
| 9 | |
| 10 | #include <IO/WriteHelpers.h> |
| 11 | #include <Common/assert_cast.h> |
| 12 | |
| 13 | |
| 14 | namespace DB |
| 15 | { |
| 16 | |
| 17 | namespace ErrorCodes |
| 18 | { |
| 19 | extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH; |
| 20 | extern const int ILLEGAL_TYPE_OF_ARGUMENT; |
| 21 | } |
| 22 | |
| 23 | /// Just changes time zone information for data type. The calculation is free. |
| 24 | class FunctionToTimeZone : public IFunction |
| 25 | { |
| 26 | public: |
| 27 | static constexpr auto name = "toTimeZone"; |
| 28 | static FunctionPtr create(const Context &) { return std::make_shared<FunctionToTimeZone>(); } |
| 29 | |
| 30 | String getName() const override |
| 31 | { |
| 32 | return name; |
| 33 | } |
| 34 | |
| 35 | size_t getNumberOfArguments() const override { return 2; } |
| 36 | |
| 37 | DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override |
| 38 | { |
| 39 | if (arguments.size() != 2) |
| 40 | throw Exception("Number of arguments for function "+ getName() + " doesn't match: passed " |
| 41 | + toString(arguments.size()) + ", should be 2", |
| 42 | ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH); |
| 43 | |
| 44 | const auto which_type = WhichDataType(arguments[0].type); |
| 45 | if (!which_type.isDateTime() && !which_type.isDateTime64()) |
| 46 | throw Exception{"Illegal type "+ arguments[0].type->getName() + " of argument of function "+ getName() + |
| 47 | ". Should be DateTime or DateTime64", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT}; |
| 48 | |
| 49 | String time_zone_name = extractTimeZoneNameFromFunctionArguments(arguments, 1, 0); |
| 50 | if (which_type.isDateTime()) |
| 51 | return std::make_shared<DataTypeDateTime>(time_zone_name); |
| 52 | |
| 53 | const auto * date_time64 = assert_cast<const DataTypeDateTime64 *>(arguments[0].type.get()); |
| 54 | return std::make_shared<DataTypeDateTime64>(date_time64->getScale(), time_zone_name); |
| 55 | } |
| 56 | |
| 57 | void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t /*input_rows_count*/) override |
| 58 | { |
| 59 | block.getByPosition(result).column = block.getByPosition(arguments[0]).column; |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | void registerFunctionToTimeZone(FunctionFactory & factory) |
| 64 | { |
| 65 | factory.registerFunction<FunctionToTimeZone>(); |
| 66 | } |
| 67 | |
| 68 | } |
| 69 |