This is an automated email from the ASF dual-hosted git repository. yiguolei pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push: new 016e47b857 [refactor](date function) simplify function template arguments (#14814) 016e47b857 is described below commit 016e47b857a21d6d0b77fd3ad0d3364a59a67d0d Author: Gabriel <gabrielleeb...@gmail.com> AuthorDate: Mon Dec 5 17:59:47 2022 +0800 [refactor](date function) simplify function template arguments (#14814) --- .../function_date_or_datetime_computation.cpp | 99 ++++----- .../function_date_or_datetime_computation.h | 224 +++++++++++---------- .../function_date_or_datetime_computation_v2.cpp | 174 +++++++--------- 3 files changed, 231 insertions(+), 266 deletions(-) diff --git a/be/src/vec/functions/function_date_or_datetime_computation.cpp b/be/src/vec/functions/function_date_or_datetime_computation.cpp index 02dd86b58e..abaef68c4c 100644 --- a/be/src/vec/functions/function_date_or_datetime_computation.cpp +++ b/be/src/vec/functions/function_date_or_datetime_computation.cpp @@ -21,63 +21,48 @@ namespace doris::vectorized { -using FunctionAddSeconds = FunctionDateOrDateTimeComputation< - AddSecondsImpl<DataTypeDateTime, Int64, DataTypeDateTime>>; -using FunctionAddMinutes = FunctionDateOrDateTimeComputation< - AddMinutesImpl<DataTypeDateTime, Int64, DataTypeDateTime>>; -using FunctionAddHours = - FunctionDateOrDateTimeComputation<AddHoursImpl<DataTypeDateTime, Int64, DataTypeDateTime>>; -using FunctionAddDays = - FunctionDateOrDateTimeComputation<AddDaysImpl<DataTypeDateTime, Int64, DataTypeDateTime>>; -using FunctionAddWeeks = - FunctionDateOrDateTimeComputation<AddWeeksImpl<DataTypeDateTime, Int64, DataTypeDateTime>>; -using FunctionAddMonths = - FunctionDateOrDateTimeComputation<AddMonthsImpl<DataTypeDateTime, Int64, DataTypeDateTime>>; -using FunctionAddQuarters = FunctionDateOrDateTimeComputation< - AddQuartersImpl<DataTypeDateTime, Int64, DataTypeDateTime>>; -using FunctionAddYears = - FunctionDateOrDateTimeComputation<AddYearsImpl<DataTypeDateTime, Int64, DataTypeDateTime>>; - -using FunctionSubSeconds = FunctionDateOrDateTimeComputation< - SubtractSecondsImpl<DataTypeDateTime, Int64, DataTypeDateTime>>; -using FunctionSubMinutes = FunctionDateOrDateTimeComputation< - SubtractMinutesImpl<DataTypeDateTime, Int64, DataTypeDateTime>>; -using FunctionSubHours = FunctionDateOrDateTimeComputation< - SubtractHoursImpl<DataTypeDateTime, Int64, DataTypeDateTime>>; -using FunctionSubDays = FunctionDateOrDateTimeComputation< - SubtractDaysImpl<DataTypeDateTime, Int64, DataTypeDateTime>>; -using FunctionSubWeeks = FunctionDateOrDateTimeComputation< - SubtractWeeksImpl<DataTypeDateTime, Int64, DataTypeDateTime>>; -using FunctionSubMonths = FunctionDateOrDateTimeComputation< - SubtractMonthsImpl<DataTypeDateTime, Int64, DataTypeDateTime>>; -using FunctionSubQuarters = FunctionDateOrDateTimeComputation< - SubtractQuartersImpl<DataTypeDateTime, Int64, DataTypeDateTime>>; -using FunctionSubYears = FunctionDateOrDateTimeComputation< - SubtractYearsImpl<DataTypeDateTime, Int64, DataTypeDateTime>>; - -using FunctionDateDiff = FunctionDateOrDateTimeComputation<DateDiffImpl< - VecDateTimeValue, VecDateTimeValue, DataTypeDateTime, DataTypeDateTime, Int64, Int64>>; -using FunctionTimeDiff = FunctionDateOrDateTimeComputation<TimeDiffImpl< - VecDateTimeValue, VecDateTimeValue, DataTypeDateTime, DataTypeDateTime, Int64, Int64>>; -using FunctionYearsDiff = FunctionDateOrDateTimeComputation<YearsDiffImpl< - VecDateTimeValue, VecDateTimeValue, DataTypeDateTime, DataTypeDateTime, Int64, Int64>>; -using FunctionMonthsDiff = FunctionDateOrDateTimeComputation<MonthsDiffImpl< - VecDateTimeValue, VecDateTimeValue, DataTypeDateTime, DataTypeDateTime, Int64, Int64>>; -using FunctionDaysDiff = FunctionDateOrDateTimeComputation<DaysDiffImpl< - VecDateTimeValue, VecDateTimeValue, DataTypeDateTime, DataTypeDateTime, Int64, Int64>>; -using FunctionWeeksDiff = FunctionDateOrDateTimeComputation<WeeksDiffImpl< - VecDateTimeValue, VecDateTimeValue, DataTypeDateTime, DataTypeDateTime, Int64, Int64>>; -using FunctionHoursDiff = FunctionDateOrDateTimeComputation<HoursDiffImpl< - VecDateTimeValue, VecDateTimeValue, DataTypeDateTime, DataTypeDateTime, Int64, Int64>>; -using FunctionMinutesDiff = FunctionDateOrDateTimeComputation<MintueSDiffImpl< - VecDateTimeValue, VecDateTimeValue, DataTypeDateTime, DataTypeDateTime, Int64, Int64>>; -using FunctionSecondsDiff = FunctionDateOrDateTimeComputation<SecondsDiffImpl< - VecDateTimeValue, VecDateTimeValue, DataTypeDateTime, DataTypeDateTime, Int64, Int64>>; - -using FunctionToYearWeekTwoArgs = FunctionDateOrDateTimeComputation< - ToYearWeekTwoArgsImpl<VecDateTimeValue, DataTypeDateTime, Int64>>; -using FunctionToWeekTwoArgs = FunctionDateOrDateTimeComputation< - ToWeekTwoArgsImpl<VecDateTimeValue, DataTypeDateTime, Int64>>; +using FunctionAddSeconds = FunctionDateOrDateTimeComputation<AddSecondsImpl<DataTypeDateTime>>; +using FunctionAddMinutes = FunctionDateOrDateTimeComputation<AddMinutesImpl<DataTypeDateTime>>; +using FunctionAddHours = FunctionDateOrDateTimeComputation<AddHoursImpl<DataTypeDateTime>>; +using FunctionAddDays = FunctionDateOrDateTimeComputation<AddDaysImpl<DataTypeDateTime>>; +using FunctionAddWeeks = FunctionDateOrDateTimeComputation<AddWeeksImpl<DataTypeDateTime>>; +using FunctionAddMonths = FunctionDateOrDateTimeComputation<AddMonthsImpl<DataTypeDateTime>>; +using FunctionAddQuarters = FunctionDateOrDateTimeComputation<AddQuartersImpl<DataTypeDateTime>>; +using FunctionAddYears = FunctionDateOrDateTimeComputation<AddYearsImpl<DataTypeDateTime>>; + +using FunctionSubSeconds = FunctionDateOrDateTimeComputation<SubtractSecondsImpl<DataTypeDateTime>>; +using FunctionSubMinutes = FunctionDateOrDateTimeComputation<SubtractMinutesImpl<DataTypeDateTime>>; +using FunctionSubHours = FunctionDateOrDateTimeComputation<SubtractHoursImpl<DataTypeDateTime>>; +using FunctionSubDays = FunctionDateOrDateTimeComputation<SubtractDaysImpl<DataTypeDateTime>>; +using FunctionSubWeeks = FunctionDateOrDateTimeComputation<SubtractWeeksImpl<DataTypeDateTime>>; +using FunctionSubMonths = FunctionDateOrDateTimeComputation<SubtractMonthsImpl<DataTypeDateTime>>; +using FunctionSubQuarters = + FunctionDateOrDateTimeComputation<SubtractQuartersImpl<DataTypeDateTime>>; +using FunctionSubYears = FunctionDateOrDateTimeComputation<SubtractYearsImpl<DataTypeDateTime>>; + +using FunctionDateDiff = + FunctionDateOrDateTimeComputation<DateDiffImpl<DataTypeDateTime, DataTypeDateTime>>; +using FunctionTimeDiff = + FunctionDateOrDateTimeComputation<TimeDiffImpl<DataTypeDateTime, DataTypeDateTime>>; +using FunctionYearsDiff = + FunctionDateOrDateTimeComputation<YearsDiffImpl<DataTypeDateTime, DataTypeDateTime>>; +using FunctionMonthsDiff = + FunctionDateOrDateTimeComputation<MonthsDiffImpl<DataTypeDateTime, DataTypeDateTime>>; +using FunctionDaysDiff = + FunctionDateOrDateTimeComputation<DaysDiffImpl<DataTypeDateTime, DataTypeDateTime>>; +using FunctionWeeksDiff = + FunctionDateOrDateTimeComputation<WeeksDiffImpl<DataTypeDateTime, DataTypeDateTime>>; +using FunctionHoursDiff = + FunctionDateOrDateTimeComputation<HoursDiffImpl<DataTypeDateTime, DataTypeDateTime>>; +using FunctionMinutesDiff = + FunctionDateOrDateTimeComputation<MintueSDiffImpl<DataTypeDateTime, DataTypeDateTime>>; +using FunctionSecondsDiff = + FunctionDateOrDateTimeComputation<SecondsDiffImpl<DataTypeDateTime, DataTypeDateTime>>; + +using FunctionToYearWeekTwoArgs = + FunctionDateOrDateTimeComputation<ToYearWeekTwoArgsImpl<DataTypeDateTime>>; +using FunctionToWeekTwoArgs = + FunctionDateOrDateTimeComputation<ToWeekTwoArgsImpl<DataTypeDateTime>>; struct NowFunctionName { static constexpr auto name = "now"; diff --git a/be/src/vec/functions/function_date_or_datetime_computation.h b/be/src/vec/functions/function_date_or_datetime_computation.h index a22bc1a357..4900f1a48a 100644 --- a/be/src/vec/functions/function_date_or_datetime_computation.h +++ b/be/src/vec/functions/function_date_or_datetime_computation.h @@ -32,8 +32,8 @@ #include "vec/runtime/vdatetime_value.h" namespace doris::vectorized { -template <TimeUnit unit, typename Arg, typename DateValueType, typename ResultDateValueType, - typename ResultType> +template <TimeUnit unit, typename DateValueType, typename ResultDateValueType, typename ResultType, + typename Arg> extern ResultType date_time_add(const Arg& t, Int64 delta, bool& is_null) { auto ts_value = binary_cast<Arg, DateValueType>(t); TimeInterval interval(unit, delta, false); @@ -51,9 +51,20 @@ extern ResultType date_time_add(const Arg& t, Int64 delta, bool& is_null) { } #define ADD_TIME_FUNCTION_IMPL(CLASS, NAME, UNIT) \ - template <typename DateType, typename ArgType, typename ResultType> \ + template <typename DateType> \ struct CLASS { \ - using ReturnType = ResultType; \ + using ReturnType = std::conditional_t< \ + std::is_same_v<DateType, DataTypeDate> || \ + std::is_same_v<DateType, DataTypeDateTime>, \ + DataTypeDateTime, \ + std::conditional_t< \ + std::is_same_v<DateType, DataTypeDateV2>, \ + std::conditional_t<TimeUnit::UNIT == TimeUnit::HOUR || \ + TimeUnit::UNIT == TimeUnit::MINUTE || \ + TimeUnit::UNIT == TimeUnit::SECOND || \ + TimeUnit::UNIT == TimeUnit::SECOND_MICROSECOND, \ + DataTypeDateTimeV2, DataTypeDateV2>, \ + DataTypeDateTimeV2>>; \ using ReturnNativeType = std::conditional_t< \ std::is_same_v<DateType, DataTypeDate> || \ std::is_same_v<DateType, DataTypeDateTime>, \ @@ -66,12 +77,18 @@ extern ResultType date_time_add(const Arg& t, Int64 delta, bool& is_null) { TimeUnit::UNIT == TimeUnit::SECOND_MICROSECOND, \ UInt64, UInt32>, \ UInt64>>; \ + using InputNativeType = std::conditional_t< \ + std::is_same_v<DateType, DataTypeDate> || \ + std::is_same_v<DateType, DataTypeDateTime>, \ + Int64, \ + std::conditional_t<std::is_same_v<DateType, DataTypeDateV2>, UInt32, UInt64>>; \ static constexpr auto name = #NAME; \ static constexpr auto is_nullable = true; \ - static inline ReturnNativeType execute(const ArgType& t, Int64 delta, bool& is_null) { \ + static inline ReturnNativeType execute(const InputNativeType& t, Int64 delta, \ + bool& is_null) { \ if constexpr (std::is_same_v<DateType, DataTypeDate> || \ std::is_same_v<DateType, DataTypeDateTime>) { \ - return date_time_add<TimeUnit::UNIT, ArgType, doris::vectorized::VecDateTimeValue, \ + return date_time_add<TimeUnit::UNIT, doris::vectorized::VecDateTimeValue, \ doris::vectorized::VecDateTimeValue, ReturnNativeType>( \ t, delta, is_null); \ } else if constexpr (std::is_same_v<DateType, DataTypeDateV2>) { \ @@ -79,17 +96,17 @@ extern ResultType date_time_add(const Arg& t, Int64 delta, bool& is_null) { TimeUnit::UNIT == TimeUnit::MINUTE || \ TimeUnit::UNIT == TimeUnit::SECOND || \ TimeUnit::UNIT == TimeUnit::SECOND_MICROSECOND) { \ - return date_time_add<TimeUnit::UNIT, ArgType, DateV2Value<DateV2ValueType>, \ + return date_time_add<TimeUnit::UNIT, DateV2Value<DateV2ValueType>, \ DateV2Value<DateTimeV2ValueType>, ReturnNativeType>( \ t, delta, is_null); \ } else { \ - return date_time_add<TimeUnit::UNIT, ArgType, DateV2Value<DateV2ValueType>, \ + return date_time_add<TimeUnit::UNIT, DateV2Value<DateV2ValueType>, \ DateV2Value<DateV2ValueType>, ReturnNativeType>(t, delta, \ is_null); \ } \ \ } else { \ - return date_time_add<TimeUnit::UNIT, ArgType, DateV2Value<DateTimeV2ValueType>, \ + return date_time_add<TimeUnit::UNIT, DateV2Value<DateTimeV2ValueType>, \ DateV2Value<DateTimeV2ValueType>, ReturnNativeType>(t, delta, \ is_null); \ } \ @@ -108,25 +125,33 @@ ADD_TIME_FUNCTION_IMPL(AddWeeksImpl, weeks_add, WEEK); ADD_TIME_FUNCTION_IMPL(AddMonthsImpl, months_add, MONTH); ADD_TIME_FUNCTION_IMPL(AddYearsImpl, years_add, YEAR); -template <typename DateType, typename ArgType, typename ResultType> +template <typename DateType> struct AddQuartersImpl { - using ReturnType = ResultType; + using ReturnType = + std::conditional_t<std::is_same_v<DateType, DataTypeDate> || + std::is_same_v<DateType, DataTypeDateTime>, + DataTypeDateTime, + std::conditional_t<std::is_same_v<DateType, DataTypeDateV2>, + DataTypeDateV2, DataTypeDateTimeV2>>; + using InputNativeType = std::conditional_t< + std::is_same_v<DateType, DataTypeDate> || std::is_same_v<DateType, DataTypeDateTime>, + Int64, std::conditional_t<std::is_same_v<DateType, DataTypeDateV2>, UInt32, UInt64>>; using ReturnNativeType = std::conditional_t< std::is_same_v<DateType, DataTypeDate> || std::is_same_v<DateType, DataTypeDateTime>, Int64, std::conditional_t<std::is_same_v<DateType, DataTypeDateV2>, UInt32, UInt64>>; static constexpr auto name = "quarters_add"; static constexpr auto is_nullable = true; - static inline ReturnNativeType execute(const ArgType& t, Int64 delta, bool& is_null) { + static inline ReturnNativeType execute(const InputNativeType& t, Int64 delta, bool& is_null) { if constexpr (std::is_same_v<DateType, DataTypeDate> || std::is_same_v<DateType, DataTypeDateTime>) { - return date_time_add<TimeUnit::MONTH, ArgType, doris::vectorized::VecDateTimeValue, + return date_time_add<TimeUnit::MONTH, doris::vectorized::VecDateTimeValue, doris::vectorized::VecDateTimeValue, ReturnNativeType>(t, delta, is_null); } else if constexpr (std::is_same_v<DateType, DataTypeDateV2>) { - return date_time_add<TimeUnit::MONTH, ArgType, DateV2Value<DateV2ValueType>, + return date_time_add<TimeUnit::MONTH, DateV2Value<DateV2ValueType>, DateV2Value<DateV2ValueType>, ReturnNativeType>(t, delta, is_null); } else { - return date_time_add<TimeUnit::MONTH, ArgType, DateV2Value<DateTimeV2ValueType>, + return date_time_add<TimeUnit::MONTH, DateV2Value<DateTimeV2ValueType>, DateV2Value<DateTimeV2ValueType>, ReturnNativeType>(t, delta, is_null); } @@ -135,11 +160,12 @@ struct AddQuartersImpl { static DataTypes get_variadic_argument_types() { return {std::make_shared<DateType>()}; } }; -template <typename Transform, typename DateType, typename ArgType, typename ResultType> +template <typename Transform, typename DateType> struct SubtractIntervalImpl { - using ReturnType = ResultType; + using ReturnType = typename Transform::ReturnType; + using InputNativeType = typename Transform::InputNativeType; static constexpr auto is_nullable = true; - static inline Int64 execute(const ArgType& t, Int64 delta, bool& is_null) { + static inline Int64 execute(const InputNativeType& t, Int64 delta, bool& is_null) { return Transform::execute(t, -delta, is_null); } @@ -148,108 +174,81 @@ struct SubtractIntervalImpl { } }; -template <typename DateType, typename ArgType, typename ResultType> -struct SubtractSecondsImpl : SubtractIntervalImpl<AddSecondsImpl<DateType, ArgType, ResultType>, - DateType, ArgType, ResultType> { +template <typename DateType> +struct SubtractSecondsImpl : SubtractIntervalImpl<AddSecondsImpl<DateType>, DateType> { static constexpr auto name = "seconds_sub"; }; -template <typename DateType, typename ArgType, typename ResultType> -struct SubtractMinutesImpl : SubtractIntervalImpl<AddMinutesImpl<DateType, ArgType, ResultType>, - DateType, ArgType, ResultType> { +template <typename DateType> +struct SubtractMinutesImpl : SubtractIntervalImpl<AddMinutesImpl<DateType>, DateType> { static constexpr auto name = "minutes_sub"; }; -template <typename DateType, typename ArgType, typename ResultType> -struct SubtractHoursImpl : SubtractIntervalImpl<AddHoursImpl<DateType, ArgType, ResultType>, - DateType, ArgType, ResultType> { +template <typename DateType> +struct SubtractHoursImpl : SubtractIntervalImpl<AddHoursImpl<DateType>, DateType> { static constexpr auto name = "hours_sub"; }; -template <typename DateType, typename ArgType, typename ResultType> -struct SubtractDaysImpl : SubtractIntervalImpl<AddDaysImpl<DateType, ArgType, ResultType>, DateType, - ArgType, ResultType> { +template <typename DateType> +struct SubtractDaysImpl : SubtractIntervalImpl<AddDaysImpl<DateType>, DateType> { static constexpr auto name = "days_sub"; }; -template <typename DateType, typename ArgType, typename ResultType> -struct SubtractWeeksImpl : SubtractIntervalImpl<AddWeeksImpl<DateType, ArgType, ResultType>, - DateType, ArgType, ResultType> { +template <typename DateType> +struct SubtractWeeksImpl : SubtractIntervalImpl<AddWeeksImpl<DateType>, DateType> { static constexpr auto name = "weeks_sub"; }; -template <typename DateType, typename ArgType, typename ResultType> -struct SubtractMonthsImpl : SubtractIntervalImpl<AddMonthsImpl<DateType, ArgType, ResultType>, - DateType, ArgType, ResultType> { +template <typename DateType> +struct SubtractMonthsImpl : SubtractIntervalImpl<AddMonthsImpl<DateType>, DateType> { static constexpr auto name = "months_sub"; }; -template <typename DateType, typename ArgType, typename ResultType> -struct SubtractQuartersImpl : SubtractIntervalImpl<AddQuartersImpl<DateType, ArgType, ResultType>, - DateType, ArgType, ResultType> { +template <typename DateType> +struct SubtractQuartersImpl : SubtractIntervalImpl<AddQuartersImpl<DateType>, DateType> { static constexpr auto name = "quarters_sub"; }; -template <typename DateType, typename ArgType, typename ResultType> -struct SubtractYearsImpl : SubtractIntervalImpl<AddYearsImpl<DateType, ArgType, ResultType>, - DateType, ArgType, ResultType> { +template <typename DateType> +struct SubtractYearsImpl : SubtractIntervalImpl<AddYearsImpl<DateType>, DateType> { static constexpr auto name = "years_sub"; }; -template <typename DateValueType1, typename DateValueType2, typename DateType1, typename DateType2, - typename ArgType1, typename ArgType2> -struct DateDiffImpl { - using ReturnType = DataTypeInt32; - static constexpr auto name = "datediff"; - static constexpr auto is_nullable = false; - static inline Int32 execute(const ArgType1& t0, const ArgType2& t1, bool& is_null) { - const auto& ts0 = reinterpret_cast<const DateValueType1&>(t0); - const auto& ts1 = reinterpret_cast<const DateValueType2&>(t1); - is_null = !ts0.is_valid_date() || !ts1.is_valid_date(); - return ts0.daynr() - ts1.daynr(); - } - - static DataTypes get_variadic_argument_types() { - return {std::make_shared<DateType1>(), std::make_shared<DateType2>()}; - } -}; - -template <typename DateValueType1, typename DateValueType2, typename DateType1, typename DateType2, - typename ArgType1, typename ArgType2> -struct TimeDiffImpl { - using ReturnType = DataTypeFloat64; - static constexpr auto name = "timediff"; - static constexpr auto is_nullable = false; - static inline double execute(const ArgType1& t0, const ArgType2& t1, bool& is_null) { - const auto& ts0 = reinterpret_cast<const DateValueType1&>(t0); - const auto& ts1 = reinterpret_cast<const DateValueType2&>(t1); - is_null = !ts0.is_valid_date() || !ts1.is_valid_date(); - return ts0.second_diff(ts1); - } - - static DataTypes get_variadic_argument_types() { - return {std::make_shared<DateType1>(), std::make_shared<DateType2>()}; - } -}; +#define DECLARE_DATE_FUNCTIONS(NAME, FN_NAME, RETURN_TYPE, STMT) \ + template <typename DateType1, typename DateType2> \ + struct NAME { \ + using ArgType1 = std::conditional_t< \ + std::is_same_v<DateType1, DataTypeDateV2>, UInt32, \ + std::conditional_t<std::is_same_v<DateType1, DataTypeDateTimeV2>, UInt64, Int64>>; \ + using ArgType2 = std::conditional_t< \ + std::is_same_v<DateType2, DataTypeDateV2>, UInt32, \ + std::conditional_t<std::is_same_v<DateType2, DataTypeDateTimeV2>, UInt64, Int64>>; \ + using DateValueType1 = std::conditional_t< \ + std::is_same_v<DateType1, DataTypeDateV2>, DateV2Value<DateV2ValueType>, \ + std::conditional_t<std::is_same_v<DateType1, DataTypeDateTimeV2>, \ + DateV2Value<DateTimeV2ValueType>, VecDateTimeValue>>; \ + using DateValueType2 = std::conditional_t< \ + std::is_same_v<DateType2, DataTypeDateV2>, DateV2Value<DateV2ValueType>, \ + std::conditional_t<std::is_same_v<DateType2, DataTypeDateTimeV2>, \ + DateV2Value<DateTimeV2ValueType>, VecDateTimeValue>>; \ + using ReturnType = RETURN_TYPE; \ + static constexpr auto name = #FN_NAME; \ + static constexpr auto is_nullable = false; \ + static inline Int32 execute(const ArgType1& t0, const ArgType2& t1, bool& is_null) { \ + const auto& ts0 = reinterpret_cast<const DateValueType1&>(t0); \ + const auto& ts1 = reinterpret_cast<const DateValueType2&>(t1); \ + is_null = !ts0.is_valid_date() || !ts1.is_valid_date(); \ + return STMT; \ + } \ + static DataTypes get_variadic_argument_types() { \ + return {std::make_shared<DateType1>(), std::make_shared<DateType2>()}; \ + } \ + }; +DECLARE_DATE_FUNCTIONS(DateDiffImpl, datediff, DataTypeInt32, (ts0.daynr() - ts1.daynr())); +DECLARE_DATE_FUNCTIONS(TimeDiffImpl, timediff, DataTypeFloat64, ts0.second_diff(ts1)); -#define TIME_DIFF_FUNCTION_IMPL(CLASS, NAME, UNIT) \ - template <typename DateValueType1, typename DateValueType2, typename DateType1, \ - typename DateType2, typename ArgType1, typename ArgType2> \ - struct CLASS { \ - using ReturnType = DataTypeInt64; \ - static constexpr auto name = #NAME; \ - static constexpr auto is_nullable = false; \ - static inline Int64 execute(const ArgType1& t0, const ArgType2& t1, bool& is_null) { \ - const auto& ts0 = reinterpret_cast<const DateValueType1&>(t0); \ - const auto& ts1 = reinterpret_cast<const DateValueType2&>(t1); \ - is_null = !ts0.is_valid_date() || !ts1.is_valid_date(); \ - return datetime_diff<TimeUnit::UNIT>(ts1, ts0); \ - } \ - \ - static DataTypes get_variadic_argument_types() { \ - return {std::make_shared<DateType1>(), std::make_shared<DateType2>()}; \ - } \ - } +#define TIME_DIFF_FUNCTION_IMPL(CLASS, NAME, UNIT) \ + DECLARE_DATE_FUNCTIONS(CLASS, NAME, DataTypeInt64, datetime_diff<TimeUnit::UNIT>(ts1, ts0)) TIME_DIFF_FUNCTION_IMPL(YearsDiffImpl, years_diff, YEAR); TIME_DIFF_FUNCTION_IMPL(MonthsDiffImpl, months_diff, MONTH); @@ -259,20 +258,27 @@ TIME_DIFF_FUNCTION_IMPL(HoursDiffImpl, hours_diff, HOUR); TIME_DIFF_FUNCTION_IMPL(MintueSDiffImpl, minutes_diff, MINUTE); TIME_DIFF_FUNCTION_IMPL(SecondsDiffImpl, seconds_diff, SECOND); -#define TIME_FUNCTION_TWO_ARGS_IMPL(CLASS, NAME, FUNCTION) \ - template <typename DateValueType, typename DateType, typename ArgType> \ - struct CLASS { \ - using ReturnType = DataTypeInt32; \ - static constexpr auto name = #NAME; \ - static constexpr auto is_nullable = false; \ - static inline int64_t execute(const ArgType& t0, const Int32 mode, bool& is_null) { \ - const auto& ts0 = reinterpret_cast<const DateValueType&>(t0); \ - is_null = !ts0.is_valid_date(); \ - return ts0.FUNCTION; \ - } \ - static DataTypes get_variadic_argument_types() { \ - return {std::make_shared<DateType>(), std::make_shared<DataTypeInt32>()}; \ - } \ +#define TIME_FUNCTION_TWO_ARGS_IMPL(CLASS, NAME, FUNCTION) \ + template <typename DateType> \ + struct CLASS { \ + using ArgType = std::conditional_t< \ + std::is_same_v<DateType, DataTypeDateV2>, UInt32, \ + std::conditional_t<std::is_same_v<DateType, DataTypeDateTimeV2>, UInt64, Int64>>; \ + using DateValueType = std::conditional_t< \ + std::is_same_v<DateType, DataTypeDateV2>, DateV2Value<DateV2ValueType>, \ + std::conditional_t<std::is_same_v<DateType, DataTypeDateTimeV2>, \ + DateV2Value<DateTimeV2ValueType>, VecDateTimeValue>>; \ + using ReturnType = DataTypeInt32; \ + static constexpr auto name = #NAME; \ + static constexpr auto is_nullable = false; \ + static inline int64_t execute(const ArgType& t0, const Int32 mode, bool& is_null) { \ + const auto& ts0 = reinterpret_cast<const DateValueType&>(t0); \ + is_null = !ts0.is_valid_date(); \ + return ts0.FUNCTION; \ + } \ + static DataTypes get_variadic_argument_types() { \ + return {std::make_shared<DateType>(), std::make_shared<DataTypeInt32>()}; \ + } \ } TIME_FUNCTION_TWO_ARGS_IMPL(ToYearWeekTwoArgsImpl, yearweek, year_week(mysql_week_mode(mode))); diff --git a/be/src/vec/functions/function_date_or_datetime_computation_v2.cpp b/be/src/vec/functions/function_date_or_datetime_computation_v2.cpp index ced2b2d70c..23d7f296dc 100644 --- a/be/src/vec/functions/function_date_or_datetime_computation_v2.cpp +++ b/be/src/vec/functions/function_date_or_datetime_computation_v2.cpp @@ -20,102 +20,76 @@ namespace doris::vectorized { -using FunctionAddSecondsV2 = FunctionDateOrDateTimeComputation< - AddSecondsImpl<DataTypeDateV2, UInt32, DataTypeDateTimeV2>>; -using FunctionAddMinutesV2 = FunctionDateOrDateTimeComputation< - AddMinutesImpl<DataTypeDateV2, UInt32, DataTypeDateTimeV2>>; -using FunctionAddHoursV2 = - FunctionDateOrDateTimeComputation<AddHoursImpl<DataTypeDateV2, UInt32, DataTypeDateTimeV2>>; -using FunctionAddDaysV2 = - FunctionDateOrDateTimeComputation<AddDaysImpl<DataTypeDateV2, UInt32, DataTypeDateV2>>; -using FunctionAddWeeksV2 = - FunctionDateOrDateTimeComputation<AddWeeksImpl<DataTypeDateV2, UInt32, DataTypeDateV2>>; -using FunctionAddMonthsV2 = - FunctionDateOrDateTimeComputation<AddMonthsImpl<DataTypeDateV2, UInt32, DataTypeDateV2>>; -using FunctionAddQuartersV2 = - FunctionDateOrDateTimeComputation<AddQuartersImpl<DataTypeDateV2, UInt32, DataTypeDateV2>>; -using FunctionAddYearsV2 = - FunctionDateOrDateTimeComputation<AddYearsImpl<DataTypeDateV2, UInt32, DataTypeDateV2>>; - -using FunctionSubSecondsV2 = FunctionDateOrDateTimeComputation< - SubtractSecondsImpl<DataTypeDateV2, UInt32, DataTypeDateTimeV2>>; -using FunctionSubMinutesV2 = FunctionDateOrDateTimeComputation< - SubtractMinutesImpl<DataTypeDateV2, UInt32, DataTypeDateTimeV2>>; -using FunctionSubHoursV2 = FunctionDateOrDateTimeComputation< - SubtractHoursImpl<DataTypeDateV2, UInt32, DataTypeDateTimeV2>>; -using FunctionSubDaysV2 = - FunctionDateOrDateTimeComputation<SubtractDaysImpl<DataTypeDateV2, UInt32, DataTypeDateV2>>; -using FunctionSubWeeksV2 = FunctionDateOrDateTimeComputation< - SubtractWeeksImpl<DataTypeDateV2, UInt32, DataTypeDateV2>>; -using FunctionSubMonthsV2 = FunctionDateOrDateTimeComputation< - SubtractMonthsImpl<DataTypeDateV2, UInt32, DataTypeDateV2>>; -using FunctionSubQuartersV2 = FunctionDateOrDateTimeComputation< - SubtractQuartersImpl<DataTypeDateV2, UInt32, DataTypeDateV2>>; -using FunctionSubYearsV2 = FunctionDateOrDateTimeComputation< - SubtractYearsImpl<DataTypeDateV2, UInt32, DataTypeDateV2>>; - -using FunctionToYearWeekTwoArgsV2 = FunctionDateOrDateTimeComputation< - ToYearWeekTwoArgsImpl<DateV2Value<DateV2ValueType>, DataTypeDateV2, UInt32>>; -using FunctionToWeekTwoArgsV2 = FunctionDateOrDateTimeComputation< - ToWeekTwoArgsImpl<DateV2Value<DateV2ValueType>, DataTypeDateV2, UInt32>>; - -using FunctionDatetimeV2AddSeconds = FunctionDateOrDateTimeComputation< - AddSecondsImpl<DataTypeDateTimeV2, UInt64, DataTypeDateTimeV2>>; -using FunctionDatetimeV2AddMinutes = FunctionDateOrDateTimeComputation< - AddMinutesImpl<DataTypeDateTimeV2, UInt64, DataTypeDateTimeV2>>; -using FunctionDatetimeV2AddHours = FunctionDateOrDateTimeComputation< - AddHoursImpl<DataTypeDateTimeV2, UInt64, DataTypeDateTimeV2>>; -using FunctionDatetimeV2AddDays = FunctionDateOrDateTimeComputation< - AddDaysImpl<DataTypeDateTimeV2, UInt64, DataTypeDateTimeV2>>; -using FunctionDatetimeV2AddWeeks = FunctionDateOrDateTimeComputation< - AddWeeksImpl<DataTypeDateTimeV2, UInt64, DataTypeDateTimeV2>>; -using FunctionDatetimeV2AddMonths = FunctionDateOrDateTimeComputation< - AddMonthsImpl<DataTypeDateTimeV2, UInt64, DataTypeDateTimeV2>>; -using FunctionDatetimeV2AddQuarters = FunctionDateOrDateTimeComputation< - AddQuartersImpl<DataTypeDateTimeV2, UInt64, DataTypeDateTimeV2>>; -using FunctionDatetimeV2AddYears = FunctionDateOrDateTimeComputation< - AddYearsImpl<DataTypeDateTimeV2, UInt64, DataTypeDateTimeV2>>; - -using FunctionDatetimeV2SubSeconds = FunctionDateOrDateTimeComputation< - SubtractSecondsImpl<DataTypeDateTimeV2, UInt64, DataTypeDateTimeV2>>; -using FunctionDatetimeV2SubMinutes = FunctionDateOrDateTimeComputation< - SubtractMinutesImpl<DataTypeDateTimeV2, UInt64, DataTypeDateTimeV2>>; -using FunctionDatetimeV2SubHours = FunctionDateOrDateTimeComputation< - SubtractHoursImpl<DataTypeDateTimeV2, UInt64, DataTypeDateTimeV2>>; -using FunctionDatetimeV2SubDays = FunctionDateOrDateTimeComputation< - SubtractDaysImpl<DataTypeDateTimeV2, UInt64, DataTypeDateTimeV2>>; -using FunctionDatetimeV2SubWeeks = FunctionDateOrDateTimeComputation< - SubtractWeeksImpl<DataTypeDateTimeV2, UInt64, DataTypeDateTimeV2>>; -using FunctionDatetimeV2SubMonths = FunctionDateOrDateTimeComputation< - SubtractMonthsImpl<DataTypeDateTimeV2, UInt64, DataTypeDateTimeV2>>; -using FunctionDatetimeV2SubQuarters = FunctionDateOrDateTimeComputation< - SubtractQuartersImpl<DataTypeDateTimeV2, UInt64, DataTypeDateTimeV2>>; -using FunctionDatetimeV2SubYears = FunctionDateOrDateTimeComputation< - SubtractYearsImpl<DataTypeDateTimeV2, UInt64, DataTypeDateTimeV2>>; - -#define FUNCTION_DATEV2_WITH_TWO_ARGS(NAME, IMPL, TYPE1, TYPE2, ARG1, ARG2, DATE_VALUE1, \ - DATE_VALUE2) \ - using NAME##_##TYPE1##_##TYPE2 = FunctionDateOrDateTimeComputation< \ - IMPL<DATE_VALUE1, DATE_VALUE2, TYPE1, TYPE2, ARG1, ARG2>>; - -#define ALL_FUNCTION_DATEV2_WITH_TWO_ARGS(NAME, IMPL) \ - FUNCTION_DATEV2_WITH_TWO_ARGS(NAME, IMPL, DataTypeDateTimeV2, DataTypeDateTimeV2, UInt64, \ - UInt64, DateV2Value<DateTimeV2ValueType>, \ - DateV2Value<DateTimeV2ValueType>) \ - FUNCTION_DATEV2_WITH_TWO_ARGS(NAME, IMPL, DataTypeDateTimeV2, DataTypeDateV2, UInt64, UInt32, \ - DateV2Value<DateTimeV2ValueType>, DateV2Value<DateV2ValueType>) \ - FUNCTION_DATEV2_WITH_TWO_ARGS(NAME, IMPL, DataTypeDateV2, DataTypeDateTimeV2, UInt32, UInt64, \ - DateV2Value<DateV2ValueType>, DateV2Value<DateTimeV2ValueType>) \ - FUNCTION_DATEV2_WITH_TWO_ARGS(NAME, IMPL, DataTypeDateTimeV2, DataTypeDateTime, UInt64, Int64, \ - DateV2Value<DateTimeV2ValueType>, VecDateTimeValue) \ - FUNCTION_DATEV2_WITH_TWO_ARGS(NAME, IMPL, DataTypeDateTime, DataTypeDateTimeV2, Int64, UInt64, \ - VecDateTimeValue, DateV2Value<DateTimeV2ValueType>) \ - FUNCTION_DATEV2_WITH_TWO_ARGS(NAME, IMPL, DataTypeDateTime, DataTypeDateV2, Int64, UInt32, \ - VecDateTimeValue, DateV2Value<DateV2ValueType>) \ - FUNCTION_DATEV2_WITH_TWO_ARGS(NAME, IMPL, DataTypeDateV2, DataTypeDateTime, UInt32, Int64, \ - DateV2Value<DateV2ValueType>, VecDateTimeValue) \ - FUNCTION_DATEV2_WITH_TWO_ARGS(NAME, IMPL, DataTypeDateV2, DataTypeDateV2, UInt32, UInt32, \ - DateV2Value<DateV2ValueType>, DateV2Value<DateV2ValueType>) +using FunctionAddSecondsV2 = FunctionDateOrDateTimeComputation<AddSecondsImpl<DataTypeDateV2>>; +using FunctionAddMinutesV2 = FunctionDateOrDateTimeComputation<AddMinutesImpl<DataTypeDateV2>>; +using FunctionAddHoursV2 = FunctionDateOrDateTimeComputation<AddHoursImpl<DataTypeDateV2>>; +using FunctionAddDaysV2 = FunctionDateOrDateTimeComputation<AddDaysImpl<DataTypeDateV2>>; +using FunctionAddWeeksV2 = FunctionDateOrDateTimeComputation<AddWeeksImpl<DataTypeDateV2>>; +using FunctionAddMonthsV2 = FunctionDateOrDateTimeComputation<AddMonthsImpl<DataTypeDateV2>>; +using FunctionAddQuartersV2 = FunctionDateOrDateTimeComputation<AddQuartersImpl<DataTypeDateV2>>; +using FunctionAddYearsV2 = FunctionDateOrDateTimeComputation<AddYearsImpl<DataTypeDateV2>>; + +using FunctionSubSecondsV2 = FunctionDateOrDateTimeComputation<SubtractSecondsImpl<DataTypeDateV2>>; +using FunctionSubMinutesV2 = FunctionDateOrDateTimeComputation<SubtractMinutesImpl<DataTypeDateV2>>; +using FunctionSubHoursV2 = FunctionDateOrDateTimeComputation<SubtractHoursImpl<DataTypeDateV2>>; +using FunctionSubDaysV2 = FunctionDateOrDateTimeComputation<SubtractDaysImpl<DataTypeDateV2>>; +using FunctionSubWeeksV2 = FunctionDateOrDateTimeComputation<SubtractWeeksImpl<DataTypeDateV2>>; +using FunctionSubMonthsV2 = FunctionDateOrDateTimeComputation<SubtractMonthsImpl<DataTypeDateV2>>; +using FunctionSubQuartersV2 = + FunctionDateOrDateTimeComputation<SubtractQuartersImpl<DataTypeDateV2>>; +using FunctionSubYearsV2 = FunctionDateOrDateTimeComputation<SubtractYearsImpl<DataTypeDateV2>>; + +using FunctionToYearWeekTwoArgsV2 = + FunctionDateOrDateTimeComputation<ToYearWeekTwoArgsImpl<DataTypeDateV2>>; +using FunctionToWeekTwoArgsV2 = + FunctionDateOrDateTimeComputation<ToWeekTwoArgsImpl<DataTypeDateV2>>; + +using FunctionDatetimeV2AddSeconds = + FunctionDateOrDateTimeComputation<AddSecondsImpl<DataTypeDateTimeV2>>; +using FunctionDatetimeV2AddMinutes = + FunctionDateOrDateTimeComputation<AddMinutesImpl<DataTypeDateTimeV2>>; +using FunctionDatetimeV2AddHours = + FunctionDateOrDateTimeComputation<AddHoursImpl<DataTypeDateTimeV2>>; +using FunctionDatetimeV2AddDays = + FunctionDateOrDateTimeComputation<AddDaysImpl<DataTypeDateTimeV2>>; +using FunctionDatetimeV2AddWeeks = + FunctionDateOrDateTimeComputation<AddWeeksImpl<DataTypeDateTimeV2>>; +using FunctionDatetimeV2AddMonths = + FunctionDateOrDateTimeComputation<AddMonthsImpl<DataTypeDateTimeV2>>; +using FunctionDatetimeV2AddQuarters = + FunctionDateOrDateTimeComputation<AddQuartersImpl<DataTypeDateTimeV2>>; +using FunctionDatetimeV2AddYears = + FunctionDateOrDateTimeComputation<AddYearsImpl<DataTypeDateTimeV2>>; + +using FunctionDatetimeV2SubSeconds = + FunctionDateOrDateTimeComputation<SubtractSecondsImpl<DataTypeDateTimeV2>>; +using FunctionDatetimeV2SubMinutes = + FunctionDateOrDateTimeComputation<SubtractMinutesImpl<DataTypeDateTimeV2>>; +using FunctionDatetimeV2SubHours = + FunctionDateOrDateTimeComputation<SubtractHoursImpl<DataTypeDateTimeV2>>; +using FunctionDatetimeV2SubDays = + FunctionDateOrDateTimeComputation<SubtractDaysImpl<DataTypeDateTimeV2>>; +using FunctionDatetimeV2SubWeeks = + FunctionDateOrDateTimeComputation<SubtractWeeksImpl<DataTypeDateTimeV2>>; +using FunctionDatetimeV2SubMonths = + FunctionDateOrDateTimeComputation<SubtractMonthsImpl<DataTypeDateTimeV2>>; +using FunctionDatetimeV2SubQuarters = + FunctionDateOrDateTimeComputation<SubtractQuartersImpl<DataTypeDateTimeV2>>; +using FunctionDatetimeV2SubYears = + FunctionDateOrDateTimeComputation<SubtractYearsImpl<DataTypeDateTimeV2>>; + +#define FUNCTION_DATEV2_WITH_TWO_ARGS(NAME, IMPL, TYPE1, TYPE2) \ + using NAME##_##TYPE1##_##TYPE2 = FunctionDateOrDateTimeComputation<IMPL<TYPE1, TYPE2>>; + +#define ALL_FUNCTION_DATEV2_WITH_TWO_ARGS(NAME, IMPL) \ + FUNCTION_DATEV2_WITH_TWO_ARGS(NAME, IMPL, DataTypeDateTimeV2, DataTypeDateTimeV2) \ + FUNCTION_DATEV2_WITH_TWO_ARGS(NAME, IMPL, DataTypeDateTimeV2, DataTypeDateV2) \ + FUNCTION_DATEV2_WITH_TWO_ARGS(NAME, IMPL, DataTypeDateV2, DataTypeDateTimeV2) \ + FUNCTION_DATEV2_WITH_TWO_ARGS(NAME, IMPL, DataTypeDateTimeV2, DataTypeDateTime) \ + FUNCTION_DATEV2_WITH_TWO_ARGS(NAME, IMPL, DataTypeDateTime, DataTypeDateTimeV2) \ + FUNCTION_DATEV2_WITH_TWO_ARGS(NAME, IMPL, DataTypeDateTime, DataTypeDateV2) \ + FUNCTION_DATEV2_WITH_TWO_ARGS(NAME, IMPL, DataTypeDateV2, DataTypeDateTime) \ + FUNCTION_DATEV2_WITH_TWO_ARGS(NAME, IMPL, DataTypeDateV2, DataTypeDateV2) ALL_FUNCTION_DATEV2_WITH_TWO_ARGS(FunctionDatetimeV2DateDiff, DateDiffImpl) ALL_FUNCTION_DATEV2_WITH_TWO_ARGS(FunctionDatetimeV2TimeDiff, TimeDiffImpl) @@ -127,10 +101,10 @@ ALL_FUNCTION_DATEV2_WITH_TWO_ARGS(FunctionDatetimeV2MinutesDiff, MintueSDiffImpl ALL_FUNCTION_DATEV2_WITH_TWO_ARGS(FunctionDatetimeV2SecondsDiff, SecondsDiffImpl) ALL_FUNCTION_DATEV2_WITH_TWO_ARGS(FunctionDatetimeV2DaysDiff, DaysDiffImpl) -using FunctionDatetimeV2ToYearWeekTwoArgs = FunctionDateOrDateTimeComputation< - ToYearWeekTwoArgsImpl<DateV2Value<DateTimeV2ValueType>, DataTypeDateTimeV2, UInt64>>; -using FunctionDatetimeV2ToWeekTwoArgs = FunctionDateOrDateTimeComputation< - ToWeekTwoArgsImpl<DateV2Value<DateTimeV2ValueType>, DataTypeDateTimeV2, UInt64>>; +using FunctionDatetimeV2ToYearWeekTwoArgs = + FunctionDateOrDateTimeComputation<ToYearWeekTwoArgsImpl<DataTypeDateTimeV2>>; +using FunctionDatetimeV2ToWeekTwoArgs = + FunctionDateOrDateTimeComputation<ToWeekTwoArgsImpl<DataTypeDateTimeV2>>; void register_function_date_time_computation_v2(SimpleFunctionFactory& factory) { factory.register_function<FunctionAddSecondsV2>(); --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org