Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(UBSan): convertion NaN to unsigned int in Reservoir Sampler #43103

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 7 additions & 21 deletions src/AggregateFunctions/ReservoirSampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,6 @@ namespace ReservoirSamplerOnEmpty
};
}

template <typename ResultType, bool is_float>
struct NanLikeValueConstructor
{
static ResultType getValue()
{
return std::numeric_limits<ResultType>::quiet_NaN();
}
};
template <typename ResultType>
struct NanLikeValueConstructor<ResultType, false>
{
static ResultType getValue()
{
return ResultType();
}
};

template <typename T, ReservoirSamplerOnEmpty::Enum OnEmpty = ReservoirSamplerOnEmpty::THROW, typename Comparer = std::less<T>>
class ReservoirSampler
{
Expand Down Expand Up @@ -123,9 +106,12 @@ class ReservoirSampler
{
if (samples.empty())
{
if (DB::is_decimal<T>)
/// call onEmpty() first to check OnEmpty policy, - throw or return default value
[[maybe_unused]] auto nan = onEmpty<T>();
if constexpr (DB::is_decimal<T>)
return 0;
return onEmpty<double>();
else
return nan;
}
sortIfNeeded();

Expand Down Expand Up @@ -262,9 +248,9 @@ class ReservoirSampler
template <typename ResultType>
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<ResultType, std::is_floating_point_v<ResultType>>::getValue();
return std::numeric_limits<ResultType>::quiet_NaN();
}
};
13 changes: 10 additions & 3 deletions src/AggregateFunctions/ReservoirSamplerDeterministic.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,14 @@ class ReservoirSamplerDeterministic
double quantileInterpolated(double level)
{
if (samples.empty())
return onEmpty<double>();
{
/// call onEmpty() first to check OnEmpty policy, - throw or return default value
[[maybe_unused]] auto nan = onEmpty<T>();
if constexpr (DB::is_decimal<T>)
return 0;
else
return nan;
}

sortIfNeeded();

Expand Down Expand Up @@ -259,9 +266,9 @@ class ReservoirSamplerDeterministic
template <typename ResultType>
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<ResultType, std::is_floating_point_v<ResultType>>::getValue();
return std::numeric_limits<ResultType>::quiet_NaN();
}
};