diff --git a/src/AggregateFunctions/ReservoirSampler.h b/src/AggregateFunctions/ReservoirSampler.h index 487cc4167f01..b26ae8c3effe 100644 --- a/src/AggregateFunctions/ReservoirSampler.h +++ b/src/AggregateFunctions/ReservoirSampler.h @@ -44,23 +44,6 @@ namespace ReservoirSamplerOnEmpty }; } -template -struct NanLikeValueConstructor -{ - static ResultType getValue() - { - return std::numeric_limits::quiet_NaN(); - } -}; -template -struct NanLikeValueConstructor -{ - static ResultType getValue() - { - return ResultType(); - } -}; - template > class ReservoirSampler { @@ -123,9 +106,12 @@ class ReservoirSampler { if (samples.empty()) { - if (DB::is_decimal) + /// call onEmpty() first to check OnEmpty policy, - throw or return default value + [[maybe_unused]] auto nan = onEmpty(); + if constexpr (DB::is_decimal) return 0; - return onEmpty(); + else + return nan; } sortIfNeeded(); @@ -262,9 +248,9 @@ class ReservoirSampler template ResultType onEmpty() const { - if (OnEmpty == ReservoirSamplerOnEmpty::THROW) + if constexpr (OnEmpty == ReservoirSamplerOnEmpty::THROW) throw DB::Exception(DB::ErrorCodes::LOGICAL_ERROR, "Quantile of empty ReservoirSampler"); else - return NanLikeValueConstructor>::getValue(); + return std::numeric_limits::quiet_NaN(); } }; diff --git a/src/AggregateFunctions/ReservoirSamplerDeterministic.h b/src/AggregateFunctions/ReservoirSamplerDeterministic.h index 557fd93a3a90..bfaac06eb7b0 100644 --- a/src/AggregateFunctions/ReservoirSamplerDeterministic.h +++ b/src/AggregateFunctions/ReservoirSamplerDeterministic.h @@ -115,7 +115,14 @@ class ReservoirSamplerDeterministic double quantileInterpolated(double level) { if (samples.empty()) - return onEmpty(); + { + /// call onEmpty() first to check OnEmpty policy, - throw or return default value + [[maybe_unused]] auto nan = onEmpty(); + if constexpr (DB::is_decimal) + return 0; + else + return nan; + } sortIfNeeded(); @@ -259,9 +266,9 @@ class ReservoirSamplerDeterministic template ResultType onEmpty() const { - if (OnEmpty == ReservoirSamplerDeterministicOnEmpty::THROW) + if constexpr (OnEmpty == ReservoirSamplerDeterministicOnEmpty::THROW) throw DB::Exception(DB::ErrorCodes::LOGICAL_ERROR, "Quantile of empty ReservoirSamplerDeterministic"); else - return NanLikeValueConstructor>::getValue(); + return std::numeric_limits::quiet_NaN(); } };