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

Cherry pick #44067 to 22.3: Fix undefined behavior in quantiles function #44140

4 changes: 2 additions & 2 deletions src/AggregateFunctions/AggregateFunctionQuantile.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,15 @@ class AggregateFunctionQuantile final : public IAggregateFunctionDataHelper<Data
{
auto & data_to = assert_cast<ColumnVector<FloatReturnType> &>(arr_to.getData()).getData();
size_t old_size = data_to.size();
data_to.resize(data_to.size() + size);
data_to.resize(old_size + size);

data.getManyFloat(levels.levels.data(), levels.permutation.data(), size, data_to.data() + old_size);
}
else
{
auto & data_to = static_cast<ColVecType &>(arr_to.getData()).getData();
size_t old_size = data_to.size();
data_to.resize(data_to.size() + size);
data_to.resize(old_size + size);

data.getMany(levels.levels.data(), levels.permutation.data(), size, data_to.data() + old_size);
}
Expand Down
24 changes: 22 additions & 2 deletions src/AggregateFunctions/QuantileReservoirSampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,35 @@ struct QuantileReservoirSampler
/// Get the value of the `level` quantile. The level must be between 0 and 1.
Value get(Float64 level)
{
return Value(data.quantileInterpolated(level));
if (data.empty())
return {};

if constexpr (is_decimal<Value>)
return Value(static_cast<typename Value::NativeType>(data.quantileInterpolated(level)));
else
return static_cast<Value>(data.quantileInterpolated(level));
}

/// Get the `size` values of `levels` quantiles. Write `size` results starting with `result` address.
/// indices - an array of index levels such that the corresponding elements will go in ascending order.
void getMany(const Float64 * levels, const size_t * indices, size_t size, Value * result)
{
bool is_empty = data.empty();

for (size_t i = 0; i < size; ++i)
result[indices[i]] = Value(data.quantileInterpolated(levels[indices[i]]));
{
if (is_empty)
{
result[i] = Value{};
}
else
{
if constexpr (is_decimal<Value>)
result[indices[i]] = Value(static_cast<typename Value::NativeType>(data.quantileInterpolated(levels[indices[i]])));
else
result[indices[i]] = Value(data.quantileInterpolated(levels[indices[i]]));
}
}
}

/// The same, but in the case of an empty state, NaN is returned.
Expand Down
24 changes: 22 additions & 2 deletions src/AggregateFunctions/QuantileReservoirSamplerDeterministic.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,35 @@ struct QuantileReservoirSamplerDeterministic
/// Get the value of the `level` quantile. The level must be between 0 and 1.
Value get(Float64 level)
{
return data.quantileInterpolated(level);
if (data.empty())
return {};

if constexpr (is_decimal<Value>)
return static_cast<typename Value::NativeType>(data.quantileInterpolated(level));
else
return static_cast<Value>(data.quantileInterpolated(level));
}

/// Get the `size` values of `levels` quantiles. Write `size` results starting with `result` address.
/// indices - an array of index levels such that the corresponding elements will go in ascending order.
void getMany(const Float64 * levels, const size_t * indices, size_t size, Value * result)
{
bool is_empty = data.empty();

for (size_t i = 0; i < size; ++i)
result[indices[i]] = data.quantileInterpolated(levels[indices[i]]);
{
if (is_empty)
{
result[i] = Value{};
}
else
{
if constexpr (is_decimal<Value>)
result[indices[i]] = static_cast<typename Value::NativeType>(data.quantileInterpolated(levels[indices[i]]));
else
result[indices[i]] = static_cast<Value>(data.quantileInterpolated(levels[indices[i]]));
}
}
}

/// The same, but in the case of an empty state, NaN is returned.
Expand Down
5 changes: 5 additions & 0 deletions src/AggregateFunctions/ReservoirSampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ class ReservoirSampler
return total_values;
}

bool empty() const
{
return samples.empty();
}

T quantileNearest(double level)
{
if (samples.empty())
Expand Down
5 changes: 5 additions & 0 deletions src/AggregateFunctions/ReservoirSamplerDeterministic.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ class ReservoirSamplerDeterministic
return total_values;
}

bool empty() const
{
return samples.empty();
}

T quantileNearest(double level)
{
if (samples.empty())
Expand Down
25 changes: 25 additions & 0 deletions tests/queries/0_stateless/02499_quantile_nan_ubsan_msan.reference
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
['1970-01-01 00:00:00']
['1970-01-01 00:00:00']

['1970-01-01 00:00:00']
['1970-01-01 00:00:00']
['1970-01-01 00:00:00','1970-01-01 00:00:00','1970-01-01 00:00:00','1970-01-01 00:00:00','1970-01-01 00:00:00','1970-01-01 00:00:00','1970-01-01 00:00:00','1970-01-01 00:00:00','1970-01-01 00:00:00']

['1970-01-01 00:00:00','1970-01-01 00:00:00','1970-01-01 00:00:00','1970-01-01 00:00:00','1970-01-01 00:00:00','1970-01-01 00:00:00','1970-01-01 00:00:00','1970-01-01 00:00:00','1970-01-01 00:00:00']
['1970-01-01 00:00:00']
['1970-01-01 00:00:00','1970-01-01 00:00:00','1970-01-01 00:00:00','1970-01-01 00:00:00']
[18446744073709552000]
['1970-01-01 00:00:00']
['1970-01-01 00:00:00','1970-01-01 00:00:00','1970-01-01 00:00:00','1970-01-01 00:00:00']
[1.157920892373162e77]
[nan]
1970-01-01 00:00:00
1970-01-01 00:00:00

1970-01-01 00:00:00
1970-01-01 00:00:00
1970-01-01 00:00:00
18446744073709552000
1970-01-01 00:00:00
1.157920892373162e77
nan
22 changes: 22 additions & 0 deletions tests/queries/0_stateless/02499_quantile_nan_ubsan_msan.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
SELECT quantiles(0.5)(now()::DateTime('UTC')) WHERE 0;
SELECT quantiles(0.5)(now()::DateTime('UTC')) WHERE 0 WITH TOTALS;
SELECT arrayReduce('quantiles(0.5)', []::Array(DateTime('UTC')));
SELECT quantiles(0.5, 1.1754943508222875e-38, 0.0001, -0., 0.0001, -0., 0.0001, 0., 0.5)(now()::DateTime('UTC')) WHERE 0 WITH TOTALS;

SELECT DISTINCT arrayReduce('quantiles(0.5)', materialize([]::Array(DateTime('UTC')))) FROM numbers(1000) LIMIT 10;
SELECT DISTINCT arrayReduce('quantiles(0, 0.5, 0.9, 1)', materialize([]::Array(DateTime('UTC')))) FROM numbers(1000) LIMIT 10;
SELECT DISTINCT arrayReduce('quantiles(0.5)', [0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFE]) FROM numbers(1000) LIMIT 10;
SELECT DISTINCT arrayReduce('quantilesDeterministic(0.5)', materialize([]::Array(DateTime('UTC'))), []::Array(UInt64)) FROM numbers(1000) LIMIT 10;
SELECT DISTINCT arrayReduce('quantilesDeterministic(0, 0.5, 0.9, 1)', materialize([]::Array(DateTime('UTC'))), []::Array(UInt64)) FROM numbers(1000) LIMIT 10;
SELECT DISTINCT arrayReduce('quantiles(0.5)', [CAST(-1, 'UInt256'), CAST(-2, 'UInt256')]) FROM numbers(1000) LIMIT 10;
SELECT DISTINCT arrayReduce('quantiles(0.5)', []::Array(Float64)) FROM numbers(1000) LIMIT 10;

SELECT quantile(0.5)(now()::DateTime('UTC')) WHERE 0;
SELECT quantile(0.5)(now()::DateTime('UTC')) WHERE 0 WITH TOTALS;
SELECT arrayReduce('quantile(0.5)', []::Array(DateTime('UTC')));

SELECT DISTINCT arrayReduce('quantile(0.5)', materialize([]::Array(DateTime('UTC')))) FROM numbers(1000) LIMIT 10;
SELECT DISTINCT arrayReduce('quantile(0.5)', [0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFE]) FROM numbers(1000) LIMIT 10;
SELECT DISTINCT arrayReduce('quantileDeterministic(0.5)', materialize([]::Array(DateTime('UTC'))), []::Array(UInt64)) FROM numbers(1000) LIMIT 10;
SELECT DISTINCT arrayReduce('quantile(0.5)', [CAST(-1, 'UInt256'), CAST(-2, 'UInt256')]) FROM numbers(1000) LIMIT 10;
SELECT DISTINCT arrayReduce('quantile(0.5)', []::Array(Float64)) FROM numbers(1000) LIMIT 10;