From 9a4d300a10dd73154e720e64b8c43886a59e18ff Mon Sep 17 00:00:00 2001 From: Alexey Milovidov Date: Sun, 20 Oct 2013 03:57:28 +0000 Subject: [PATCH] dbms: aggregate function quantileTiming: added (deterministic) randomization [#CONV-2944]. --- .../AggregateFunctionQuantileTiming.h | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/dbms/include/DB/AggregateFunctions/AggregateFunctionQuantileTiming.h b/dbms/include/DB/AggregateFunctions/AggregateFunctionQuantileTiming.h index c76b9445125..0323708e002 100644 --- a/dbms/include/DB/AggregateFunctions/AggregateFunctionQuantileTiming.h +++ b/dbms/include/DB/AggregateFunctions/AggregateFunctionQuantileTiming.h @@ -12,6 +12,8 @@ #include +#include + namespace DB { @@ -136,6 +138,13 @@ namespace detail /// Число значений для каждого значения от small_threshold до big_threshold, округлённого до big_precision. UInt64 count_big[BIG_SIZE]; + /// Получить значение квантиля по индексу в массиве count_big. + static inline UInt16 indexInBigToValue(size_t i) + { + return (i * BIG_PRECISION) + SMALL_THRESHOLD + + (intHash32<0>(i) % BIG_PRECISION - (BIG_PRECISION / 2)); /// Небольшая рандомизация, чтобы не было заметно, что все значения чётные. + } + public: QuantileTimingLarge() { @@ -209,7 +218,7 @@ namespace detail } if (i < BIG_SIZE) - return (i * BIG_PRECISION) + SMALL_THRESHOLD; + return indexInBigToValue(i); return BIG_THRESHOLD; } @@ -258,7 +267,7 @@ namespace detail if (i < BIG_SIZE) { - *result = (i * BIG_PRECISION) + SMALL_THRESHOLD; + *result = indexInBigToValue(i); ++level; ++result;