From 2af61829fe8d366b7a2c49382d9984d75b28c7ab Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Fri, 10 Jul 2015 05:57:32 +0300 Subject: [PATCH] dbms: added least and greatest functions [#METR-17233]. --- .../DB/Functions/FunctionsArithmetic.h | 97 +++++++++++-------- .../DB/Functions/FunctionsHigherOrder.h | 7 +- dbms/src/Functions/FunctionsArithmetic.cpp | 2 + .../00192_least_greatest.reference | 12 +++ .../0_stateless/00192_least_greatest.sql | 12 +++ 5 files changed, 90 insertions(+), 40 deletions(-) create mode 100644 dbms/tests/queries/0_stateless/00192_least_greatest.reference create mode 100644 dbms/tests/queries/0_stateless/00192_least_greatest.sql diff --git a/dbms/include/DB/Functions/FunctionsArithmetic.h b/dbms/include/DB/Functions/FunctionsArithmetic.h index b00b31a1158..ea38395fa63 100644 --- a/dbms/include/DB/Functions/FunctionsArithmetic.h +++ b/dbms/include/DB/Functions/FunctionsArithmetic.h @@ -262,6 +262,30 @@ struct BitShiftRightImpl } }; +template +struct LeastImpl +{ + typedef typename NumberTraits::ResultOfIf::Type ResultType; + + template + static inline Result apply(A a, B b) + { + return static_cast(a) < static_cast(b) ? static_cast(a) : static_cast(b); + } +}; + +template +struct GreatestImpl +{ + typedef typename NumberTraits::ResultOfIf::Type ResultType; + + template + static inline Result apply(A a, B b) + { + return static_cast(a) > static_cast(b) ? static_cast(a) : static_cast(b); + } +}; + template struct NegateImpl { @@ -319,6 +343,12 @@ template using Else = T; /// Used to indicate undefined operation struct InvalidType; +template <> +struct DataTypeFromFieldType +{ + using Type = InvalidType; +}; + template struct IsIntegral { static constexpr auto value = false; }; template <> struct IsIntegral { static constexpr auto value = true; }; template <> struct IsIntegral { static constexpr auto value = true; }; @@ -342,11 +372,13 @@ template struct IsDateOrDateTime { static constexpr auto val template <> struct IsDateOrDateTime { static constexpr auto value = true; }; template <> struct IsDateOrDateTime { static constexpr auto value = true; }; -/** Returns appropriate result type for binary operator on dates: +/** Returns appropriate result type for binary operator on dates (or datetimes): * Date + Integral -> Date * Integral + Date -> Date * Date - Date -> Int32 * Date - Integral -> Date + * least(Date, Date) -> Date + * greatest(Date, Date) -> Date * All other operations are not defined and return InvalidType, operations on * distinct date types are also undefined (e.g. DataTypeDate - DataTypeDateTime) */ template