| 1 | #include <DataTypes/DataTypeDate.h> | 
| 2 | #include <DataTypes/DataTypeDateTime.h> | 
| 3 | #include <Functions/IFunctionImpl.h> | 
| 4 | #include <DataTypes/DataTypeDateTime64.h> | 
| 5 | #include <Functions/extractTimeZoneFromFunctionArguments.h> | 
| 6 | #include <Functions/DateTimeTransforms.h> | 
| 7 | #include <IO/WriteHelpers.h> | 
| 8 |  | 
| 9 |  | 
| 10 | namespace DB | 
| 11 | { | 
| 12 |  | 
| 13 | namespace ErrorCodes | 
| 14 | { | 
| 15 |     extern const int ILLEGAL_TYPE_OF_ARGUMENT; | 
| 16 |     extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH; | 
| 17 | } | 
| 18 |  | 
| 19 | template<class Transform> | 
| 20 | struct WithDateTime64Converter : public Transform | 
| 21 | { | 
| 22 |     UInt8 scale; | 
| 23 |     Transform transform; | 
| 24 |  | 
| 25 |     explicit WithDateTime64Converter(UInt8 scale_, Transform transform_ = {}) | 
| 26 |         : scale(scale_), | 
| 27 |           transform(std::move(transform_)) | 
| 28 |     {} | 
| 29 |  | 
| 30 |     inline auto execute(DataTypeDateTime64::FieldType t, const DateLUTImpl & time_zone) const | 
| 31 |     { | 
| 32 |         auto x = DateTime64(t); | 
| 33 |         auto res = transform.execute(static_cast<UInt32>(DecimalUtils::getWholePart(x, scale)), time_zone); | 
| 34 |         return res; | 
| 35 |     } | 
| 36 | }; | 
| 37 |  | 
| 38 |  | 
| 39 | /// See DateTimeTransforms.h | 
| 40 | template <typename ToDataType, typename Transform> | 
| 41 | class FunctionDateOrDateTimeToSomething : public IFunction | 
| 42 | { | 
| 43 | public: | 
| 44 |     static constexpr auto name = Transform::name; | 
| 45 |     static FunctionPtr create(const Context &) { return std::make_shared<FunctionDateOrDateTimeToSomething>(); } | 
| 46 |  | 
| 47 |     String getName() const override | 
| 48 |     { | 
| 49 |         return name; | 
| 50 |     } | 
| 51 |  | 
| 52 |     bool isVariadic() const override { return true; } | 
| 53 |     size_t getNumberOfArguments() const override { return 0; } | 
| 54 |  | 
| 55 |     DataTypePtr getReturnTypeImpl(const ColumnsWithTypeAndName & arguments) const override | 
| 56 |     { | 
| 57 |         if (arguments.size() == 1) | 
| 58 |         { | 
| 59 |             if (!isDateOrDateTime(arguments[0].type)) | 
| 60 |                 throw Exception( | 
| 61 |                     "Illegal type "  + arguments[0].type->getName() + " of argument of function "  + getName() | 
| 62 |                         + ". Should be a date or a date with time" , | 
| 63 |                     ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT); | 
| 64 |         } | 
| 65 |         else if (arguments.size() == 2) | 
| 66 |         { | 
| 67 |             if (!isDateOrDateTime(arguments[0].type)) | 
| 68 |                 throw Exception( | 
| 69 |                     "Illegal type "  + arguments[0].type->getName() + " of argument of function "  + getName() | 
| 70 |                         + ". Should be a date or a date with time" , | 
| 71 |                     ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT); | 
| 72 |             if (!isString(arguments[1].type)) | 
| 73 |                 throw Exception( | 
| 74 |                     "Function "  + getName() + " supports 1 or 2 arguments. The 1st argument "  | 
| 75 |                           "must be of type Date or DateTime. The 2nd argument (optional) must be "  | 
| 76 |                           "a constant string with timezone name" , | 
| 77 |                     ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT); | 
| 78 |             if (isDate(arguments[0].type) && std::is_same_v<ToDataType, DataTypeDate>) | 
| 79 |                 throw Exception( | 
| 80 |                     "The timezone argument of function "  + getName() + " is allowed only when the 1st argument has the type DateTime" , | 
| 81 |                     ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT); | 
| 82 |         } | 
| 83 |         else | 
| 84 |             throw Exception( | 
| 85 |                 "Number of arguments for function "  + getName() + " doesn't match: passed "  + toString(arguments.size()) | 
| 86 |                     + ", should be 1 or 2" , | 
| 87 |                 ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH); | 
| 88 |  | 
| 89 |         /// For DateTime, if time zone is specified, attach it to type. | 
| 90 |         if constexpr (std::is_same_v<ToDataType, DataTypeDateTime>) | 
| 91 |             return std::make_shared<ToDataType>(extractTimeZoneNameFromFunctionArguments(arguments, 1, 0)); | 
| 92 |         if constexpr (std::is_same_v<ToDataType, DataTypeDateTime64>) | 
| 93 |             return std::make_shared<ToDataType>(extractTimeZoneNameFromFunctionArguments(arguments, 1, 0)); | 
| 94 |         else | 
| 95 |             return std::make_shared<ToDataType>(); | 
| 96 |     } | 
| 97 |  | 
| 98 |     bool useDefaultImplementationForConstants() const override { return true; } | 
| 99 |     ColumnNumbers getArgumentsThatAreAlwaysConstant() const override { return {1}; } | 
| 100 |  | 
| 101 |     void executeImpl(Block & block, const ColumnNumbers & arguments, size_t result, size_t input_rows_count) override | 
| 102 |     { | 
| 103 |         const IDataType * from_type = block.getByPosition(arguments[0]).type.get(); | 
| 104 |         WhichDataType which(from_type); | 
| 105 |  | 
| 106 |         if (which.isDate()) | 
| 107 |             DateTimeTransformImpl<DataTypeDate, ToDataType, Transform>::execute(block, arguments, result, input_rows_count); | 
| 108 |         else if (which.isDateTime()) | 
| 109 |             DateTimeTransformImpl<DataTypeDateTime, ToDataType, Transform>::execute(block, arguments, result, input_rows_count); | 
| 110 |         else if (which.isDateTime64()) | 
| 111 |         { | 
| 112 |             const auto scale = static_cast<const DataTypeDateTime64 *>(from_type)->getScale(); | 
| 113 |             WithDateTime64Converter<Transform> transformer(scale); | 
| 114 |  | 
| 115 |             DateTimeTransformImpl<DataTypeDateTime64, ToDataType, WithDateTime64Converter<Transform>>::execute(block, arguments, result, input_rows_count, transformer); | 
| 116 |         } | 
| 117 |         else | 
| 118 |             throw Exception("Illegal type "  + block.getByPosition(arguments[0]).type->getName() + " of argument of function "  + getName(), | 
| 119 |                 ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT); | 
| 120 |     } | 
| 121 |  | 
| 122 |  | 
| 123 |     bool hasInformationAboutMonotonicity() const override | 
| 124 |     { | 
| 125 |         return true; | 
| 126 |     } | 
| 127 |  | 
| 128 |     Monotonicity getMonotonicityForRange(const IDataType & type, const Field & left, const Field & right) const override | 
| 129 |     { | 
| 130 |         IFunction::Monotonicity is_monotonic { true }; | 
| 131 |         IFunction::Monotonicity is_not_monotonic; | 
| 132 |  | 
| 133 |         if (std::is_same_v<typename Transform::FactorTransform, ZeroTransform>) | 
| 134 |         { | 
| 135 |             is_monotonic.is_always_monotonic = true; | 
| 136 |             return is_monotonic; | 
| 137 |         } | 
| 138 |  | 
| 139 |         /// This method is called only if the function has one argument. Therefore, we do not care about the non-local time zone. | 
| 140 |         const DateLUTImpl & date_lut = DateLUT::instance(); | 
| 141 |  | 
| 142 |         if (left.isNull() || right.isNull()) | 
| 143 |             return is_not_monotonic; | 
| 144 |  | 
| 145 |         /// The function is monotonous on the [left, right] segment, if the factor transformation returns the same values for them. | 
| 146 |  | 
| 147 |         if (checkAndGetDataType<DataTypeDate>(&type)) | 
| 148 |         { | 
| 149 |             return Transform::FactorTransform::execute(UInt16(left.get<UInt64>()), date_lut) | 
| 150 |                 == Transform::FactorTransform::execute(UInt16(right.get<UInt64>()), date_lut) | 
| 151 |                 ? is_monotonic : is_not_monotonic; | 
| 152 |         } | 
| 153 |         else | 
| 154 |         { | 
| 155 |             return Transform::FactorTransform::execute(UInt32(left.get<UInt64>()), date_lut) | 
| 156 |                 == Transform::FactorTransform::execute(UInt32(right.get<UInt64>()), date_lut) | 
| 157 |                 ? is_monotonic : is_not_monotonic; | 
| 158 |         } | 
| 159 |     } | 
| 160 | }; | 
| 161 |  | 
| 162 | } | 
| 163 |  | 
| 164 |  |