From ea874ad1c53b4b4a27ae610eb3baf71a1488bcef Mon Sep 17 00:00:00 2001 From: Michael Kolupaev Date: Mon, 24 Dec 2012 08:16:25 +0000 Subject: [PATCH] clickhouse: added missing file [#CONV-6318]. --- dbms/include/DB/Functions/FunctionsRound.h | 99 ++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 dbms/include/DB/Functions/FunctionsRound.h diff --git a/dbms/include/DB/Functions/FunctionsRound.h b/dbms/include/DB/Functions/FunctionsRound.h new file mode 100644 index 00000000000..a487824c048 --- /dev/null +++ b/dbms/include/DB/Functions/FunctionsRound.h @@ -0,0 +1,99 @@ +#pragma once + +#include +#include // log2() + + +namespace DB +{ + + /** Функции округления: + * roundToExp2 - вниз до ближайшей степени двойки; + * roundDuration - вниз до ближайшего из: 0, 1, 10, 30, 60, 120, 180, 240, 300, 600, 1200, 1800, 3600, 7200, 18000, 36000; + * roundAge - вниз до ближайшего из: 0, 18, 25, 35, 45. + */ + + template + struct RoundToExp2Impl + { + typedef A ResultType; + + static inline A apply(A x) + { + return x <= 0 ? static_cast(0) : (static_cast(1) << static_cast(log2(static_cast(x)))); + } + }; + + template<> + struct RoundToExp2Impl + { + typedef Float32 ResultType; + + static inline Float32 apply(Float32 x) + { + return static_cast(x < 1 ? 0. : pow(2., floor(log2(x)))); + } + }; + + template<> + struct RoundToExp2Impl + { + typedef Float64 ResultType; + + static inline Float64 apply(Float64 x) + { + return x < 1 ? 0. : pow(2., floor(log2(x))); + } + }; + + template + struct RoundDurationImpl + { + typedef UInt16 ResultType; + + static inline ResultType apply(A x) + { + return x < 1 ? 0 + : (x < 10 ? 1 + : (x < 30 ? 10 + : (x < 60 ? 30 + : (x < 120 ? 60 + : (x < 180 ? 120 + : (x < 240 ? 180 + : (x < 300 ? 240 + : (x < 600 ? 300 + : (x < 1200 ? 600 + : (x < 1800 ? 1200 + : (x < 3600 ? 1800 + : (x < 7200 ? 3600 + : (x < 18000 ? 7200 + : (x < 36000 ? 18000 + : 36000)))))))))))))); + } + }; + + template + struct RoundAgeImpl + { + typedef UInt8 ResultType; + + static inline ResultType apply(A x) + { + return x < 18 ? 0 + : (x < 25 ? 18 + : (x < 35 ? 25 + : (x < 45 ? 35 + : 45))); + } + }; + + + struct NameRoundToExp2 { static const char * get() { return "roundToExp"; } }; + struct NameRoundDuration { static const char * get() { return "roundDuration"; } }; + struct NameRoundAge { static const char * get() { return "roundAge"; } }; + + typedef FunctionUnaryArithmetic FunctionRoundToExp2; + typedef FunctionUnaryArithmetic FunctionRoundDuration; + typedef FunctionUnaryArithmetic FunctionRoundAge; + +}