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