2929namespace doris ::vectorized {
3030
3131template <TimeUnit unit>
32- inline Int128 date_time_add (const Int128& t, Int64 delta) {
32+ inline Int128 date_time_add (const Int128& t, Int64 delta, bool & is_null ) {
3333 auto ts_value = binary_cast<Int128, doris::DateTimeValue>(t);
3434 TimeInterval interval (unit, delta, false );
35- ts_value.date_add_interval (interval, unit);
35+ is_null = ! ts_value.date_add_interval (interval, unit);
3636
3737 return binary_cast<doris::DateTimeValue, Int128>(ts_value);
3838}
3939
40- #define ADD_TIME_FUNCTION_IMPL (CLASS, NAME, UNIT ) \
41- struct CLASS { \
42- using ReturnType = DataTypeDateTime; \
43- static constexpr auto name = #NAME ; \
44- static inline Int128 execute (const Int128& t, Int64 delta) { \
45- return date_time_add<TimeUnit::UNIT >(t, delta); \
46- } \
40+ #define ADD_TIME_FUNCTION_IMPL (CLASS, NAME, UNIT ) \
41+ struct CLASS { \
42+ using ReturnType = DataTypeDateTime; \
43+ static constexpr auto name = #NAME ; \
44+ static constexpr auto is_nullable = true ; \
45+ static inline Int128 execute (const Int128& t, Int64 delta, bool & is_null) { \
46+ return date_time_add<TimeUnit::UNIT >(t, delta, is_null); \
47+ } \
4748 }
4849
4950ADD_TIME_FUNCTION_IMPL (AddSecondsImpl, seconds_add, SECOND );
@@ -57,17 +58,18 @@ ADD_TIME_FUNCTION_IMPL(AddYearsImpl, years_add, YEAR);
5758struct AddQuartersImpl {
5859 using ReturnType = DataTypeDateTime;
5960 static constexpr auto name = " quarters_add" ;
60-
61- static inline Int128 execute (const Int128& t, Int64 delta) {
62- return date_time_add<TimeUnit::MONTH >(t, delta * 3 );
61+ static constexpr auto is_nullable = true ;
62+ static inline Int128 execute (const Int128& t, Int64 delta, bool & is_null ) {
63+ return date_time_add<TimeUnit::MONTH >(t, delta * 3 , is_null );
6364 }
6465};
6566
6667template <typename Transform>
6768struct SubtractIntervalImpl {
6869 using ReturnType = DataTypeDateTime;
69- static inline Int128 execute (const Int128& t, Int64 delta) {
70- return Transform::execute (t, -delta);
70+ static constexpr auto is_nullable = true ;
71+ static inline Int128 execute (const Int128& t, Int64 delta, bool & is_null) {
72+ return Transform::execute (t, -delta, is_null);
7173 }
7274};
7375
@@ -99,8 +101,8 @@ struct SubtractYearsImpl : SubtractIntervalImpl<AddYearsImpl> {
99101struct DateDiffImpl {
100102 using ReturnType = DataTypeInt32;
101103 static constexpr auto name = " datediff" ;
102-
103- static inline Int32 execute (const Int128& t0, const Int128& t1) {
104+ static constexpr auto is_nullable = false ;
105+ static inline Int32 execute (const Int128& t0, const Int128& t1, bool & is_null ) {
104106 const auto & ts0 = reinterpret_cast <const doris::DateTimeValue&>(t0);
105107 const auto & ts1 = reinterpret_cast <const doris::DateTimeValue&>(t1);
106108 return ts0.daynr () - ts1.daynr ();
@@ -110,23 +112,24 @@ struct DateDiffImpl {
110112struct TimeDiffImpl {
111113 using ReturnType = DataTypeFloat64;
112114 static constexpr auto name = " timediff" ;
113-
114- static inline double execute (const Int128& t0, const Int128& t1) {
115+ static constexpr auto is_nullable = false ;
116+ static inline double execute (const Int128& t0, const Int128& t1, bool & is_null ) {
115117 const auto & ts0 = reinterpret_cast <const doris::DateTimeValue&>(t0);
116118 const auto & ts1 = reinterpret_cast <const doris::DateTimeValue&>(t1);
117119 return ts0.second_diff (ts1);
118120 }
119121};
120122
121- #define TIME_DIFF_FUNCTION_IMPL (CLASS, NAME, UNIT ) \
122- struct CLASS { \
123- using ReturnType = DataTypeInt64; \
124- static constexpr auto name = #NAME ; \
125- static inline int64_t execute (const Int128& t0, const Int128& t1) { \
126- const auto & ts0 = reinterpret_cast <const doris::DateTimeValue&>(t0); \
127- const auto & ts1 = reinterpret_cast <const doris::DateTimeValue&>(t1); \
128- return DateTimeValue::datetime_diff<TimeUnit::UNIT >(ts1, ts0); \
129- } \
123+ #define TIME_DIFF_FUNCTION_IMPL (CLASS, NAME, UNIT ) \
124+ struct CLASS { \
125+ using ReturnType = DataTypeInt64; \
126+ static constexpr auto name = #NAME ; \
127+ static constexpr auto is_nullable = false ; \
128+ static inline int64_t execute (const Int128& t0, const Int128& t1, bool & is_null) { \
129+ const auto & ts0 = reinterpret_cast <const doris::DateTimeValue&>(t0); \
130+ const auto & ts1 = reinterpret_cast <const doris::DateTimeValue&>(t1); \
131+ return DateTimeValue::datetime_diff<TimeUnit::UNIT >(ts1, ts0); \
132+ } \
130133 }
131134
132135TIME_DIFF_FUNCTION_IMPL (YearsDiffImpl, years_diff, YEAR );
@@ -142,48 +145,69 @@ struct DateTimeOp {
142145 // use for (DateTime, DateTime) -> other_type
143146 static void vector_vector (const PaddedPODArray<FromType>& vec_from0,
144147 const PaddedPODArray<FromType>& vec_from1,
145- PaddedPODArray<ToType>& vec_to) {
148+ PaddedPODArray<ToType>& vec_to, NullMap& null_map ) {
146149 size_t size = vec_from0.size ();
147150 vec_to.resize (size);
148-
149- for (size_t i = 0 ; i < size; ++i)
150- vec_to[i] = Transform::execute (vec_from0[i], vec_from1[i]);
151+ null_map.resize_fill (size, false );
152+
153+ for (size_t i = 0 ; i < size; ++i) {
154+ vec_to[i] = Transform::execute (vec_from0[i], vec_from1[i],
155+ reinterpret_cast <bool &>(null_map[i]));
156+ // here reinterpret_cast is used to convert uint8& to bool&,
157+ // otherwise it will be implicitly converted to bool, causing the rvalue to fail to match the lvalue.
158+ // the same goes for the following.
159+ }
151160 }
152161
153162 // use for (DateTime, const DateTime) -> other_type
154163 static void vector_constant (const PaddedPODArray<FromType>& vec_from,
155- PaddedPODArray<ToType>& vec_to, Int128& delta) {
164+ PaddedPODArray<ToType>& vec_to, NullMap& null_map, Int128& delta) {
156165 size_t size = vec_from.size ();
157166 vec_to.resize (size);
167+ null_map.resize_fill (size, false );
158168
159- for (size_t i = 0 ; i < size; ++i) vec_to[i] = Transform::execute (vec_from[i], delta);
169+ for (size_t i = 0 ; i < size; ++i) {
170+ vec_to[i] =
171+ Transform::execute (vec_from[i], delta, reinterpret_cast <bool &>(null_map[i]));
172+ }
160173 }
161174
162175 // use for (DateTime, const ColumnNumber) -> other_type
163176 static void vector_constant (const PaddedPODArray<FromType>& vec_from,
164- PaddedPODArray<ToType>& vec_to, Int64 delta) {
177+ PaddedPODArray<ToType>& vec_to, NullMap& null_map, Int64 delta) {
165178 size_t size = vec_from.size ();
166179 vec_to.resize (size);
180+ null_map.resize_fill (size, false );
167181
168- for (size_t i = 0 ; i < size; ++i) vec_to[i] = Transform::execute (vec_from[i], delta);
182+ for (size_t i = 0 ; i < size; ++i) {
183+ vec_to[i] =
184+ Transform::execute (vec_from[i], delta, reinterpret_cast <bool &>(null_map[i]));
185+ }
169186 }
170187
171188 // use for (const DateTime, ColumnNumber) -> other_type
172189 static void constant_vector (const FromType& from, PaddedPODArray<ToType>& vec_to,
173- const IColumn& delta) {
190+ NullMap& null_map, const IColumn& delta) {
174191 size_t size = delta.size ();
175192 vec_to.resize (size);
193+ null_map.resize_fill (size, false );
176194
177- for (size_t i = 0 ; i < size; ++i) vec_to[i] = Transform::execute (from, delta.get_int (i));
195+ for (size_t i = 0 ; i < size; ++i) {
196+ vec_to[i] = Transform::execute (from, delta.get_int (i),
197+ reinterpret_cast <bool &>(null_map[i]));
198+ }
178199 }
179200
180201 // use for (const DateTime, DateTime) -> other_type
181202 static void constant_vector (const FromType& from, PaddedPODArray<ToType>& vec_to,
182- const PaddedPODArray<Int128>& delta) {
203+ NullMap& null_map, const PaddedPODArray<Int128>& delta) {
183204 size_t size = delta.size ();
184205 vec_to.resize (size);
206+ null_map.resize_fill (size, false );
185207
186- for (size_t i = 0 ; i < size; ++i) vec_to[i] = Transform::execute (from, delta[i]);
208+ for (size_t i = 0 ; i < size; ++i) {
209+ vec_to[i] = Transform::execute (from, delta[i], reinterpret_cast <bool &>(null_map[i]));
210+ }
187211 }
188212};
189213
@@ -192,44 +216,56 @@ struct DateTimeAddIntervalImpl {
192216 static Status execute (Block& block, const ColumnNumbers& arguments, size_t result) {
193217 using ToType = typename Transform::ReturnType::FieldType;
194218 using Op = DateTimeOp<FromType, ToType, Transform>;
195-
196219 // const DateLUTImpl & time_zone = extractTimeZoneFromFunctionArguments(block, arguments, 2, 0);
197220
198221 const ColumnPtr source_col = block.get_by_position (arguments[0 ]).column ;
199-
200222 if (const auto * sources = check_and_get_column<ColumnVector<FromType>>(source_col.get ())) {
201223 auto col_to = ColumnVector<ToType>::create ();
202-
224+ auto null_map = ColumnUInt8::create ();
203225 const IColumn& delta_column = *block.get_by_position (arguments[1 ]).column ;
204226
205227 if (const auto * delta_const_column = typeid_cast<const ColumnConst*>(&delta_column)) {
206228 if (delta_const_column->get_field ().get_type () == Field::Types::Int128) {
207229 Op::vector_constant (sources->get_data (), col_to->get_data (),
230+ null_map->get_data (),
208231 delta_const_column->get_field ().get <Int128>());
209232 } else {
210233 Op::vector_constant (sources->get_data (), col_to->get_data (),
234+ null_map->get_data (),
211235 delta_const_column->get_field ().get <Int64>());
212236 }
213237 } else {
214238 const auto * delta_vec_column =
215239 check_and_get_column<ColumnVector<FromType>>(delta_column);
216240 Op::vector_vector (sources->get_data (), delta_vec_column->get_data (),
217- col_to->get_data ());
241+ col_to->get_data (), null_map-> get_data () );
218242 }
219- block.replace_by_position (result, std::move (col_to));
243+
244+ if constexpr (!Transform::is_nullable)
245+ block.replace_by_position (result, std::move (col_to));
246+ else
247+ block.get_by_position (result).column =
248+ ColumnNullable::create (std::move (col_to), std::move (null_map));
220249 } else if (const auto * sources_const =
221250 check_and_get_column_const<ColumnVector<FromType>>(source_col.get ())) {
222251 auto col_to = ColumnVector<ToType>::create ();
252+ auto null_map = ColumnUInt8::create ();
253+
223254 if (const auto * delta_vec_column = check_and_get_column<ColumnVector<FromType>>(
224255 *block.get_by_position (arguments[1 ]).column )) {
225256 Op::constant_vector (sources_const->template get_value <FromType>(),
226- col_to->get_data (), delta_vec_column->get_data ());
257+ col_to->get_data (), null_map->get_data (),
258+ delta_vec_column->get_data ());
227259 } else {
228260 Op::constant_vector (sources_const->template get_value <FromType>(),
229- col_to->get_data (),
261+ col_to->get_data (), null_map-> get_data (),
230262 *block.get_by_position (arguments[1 ]).column );
231263 }
232- block.replace_by_position (result, std::move (col_to));
264+ if constexpr (!Transform::is_nullable)
265+ block.replace_by_position (result, std::move (col_to));
266+ else
267+ block.get_by_position (result).column =
268+ ColumnNullable::create (std::move (col_to), std::move (null_map));
233269 } else {
234270 return Status::RuntimeError (fmt::format (
235271 " Illegal column {} of first argument of function {}" ,
@@ -276,8 +312,11 @@ class FunctionDateOrDateTimeComputation : public IFunction {
276312 get_name ());
277313 }
278314 }
279-
280- return std::make_shared<typename Transform::ReturnType>();
315+ if constexpr (Transform::is_nullable) {
316+ return make_nullable (std::make_shared<typename Transform::ReturnType>());
317+ } else {
318+ return std::make_shared<typename Transform::ReturnType>();
319+ }
281320 }
282321
283322 bool use_default_implementation_for_constants () const override { return true ; }
0 commit comments