#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 "roundToExp2"; } }; 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; }