[Bug-Fix] Fix the bug of PERCENTILE_APPROX return error result nan and add PERCENTILE_APPROX UT (#5172)
This commit is contained in:
@ -177,9 +177,10 @@ public:
|
||||
PercentileApproxState() : digest(new TDigest()) {}
|
||||
PercentileApproxState(double compression) : digest(new TDigest(compression)) {}
|
||||
~PercentileApproxState() { delete digest; }
|
||||
static constexpr double INIT_QUANTILE = -1.0;
|
||||
|
||||
TDigest* digest = nullptr;
|
||||
double targetQuantile = -1.0;
|
||||
double targetQuantile = INIT_QUANTILE;
|
||||
};
|
||||
|
||||
void AggregateFunctions::percentile_approx_init(FunctionContext* ctx, StringVal* dst) {
|
||||
@ -255,7 +256,12 @@ void AggregateFunctions::percentile_approx_merge(FunctionContext* ctx, const Str
|
||||
|
||||
PercentileApproxState* dst_percentile = reinterpret_cast<PercentileApproxState*>(dst->ptr);
|
||||
dst_percentile->digest->merge(src_percentile->digest);
|
||||
dst_percentile->targetQuantile = quantile;
|
||||
// dst_percentile->targetQuantile only need set once from child result
|
||||
// for example:
|
||||
// child result targetQuantile is (0.5, -1), we should set 0.5 once to make sure correct result
|
||||
if (dst_percentile->targetQuantile == PercentileApproxState::INIT_QUANTILE) {
|
||||
dst_percentile->targetQuantile = quantile;
|
||||
}
|
||||
|
||||
delete src_percentile;
|
||||
}
|
||||
|
||||
@ -88,6 +88,26 @@ TEST_F(PercentileApproxTest, testSerialize) {
|
||||
AggregateFunctions::percentile_approx_merge(context, serialized, &stringVal2);
|
||||
DoubleVal v = AggregateFunctions::percentile_approx_finalize(context, stringVal2);
|
||||
ASSERT_DOUBLE_EQ(v.val, 99900.5);
|
||||
|
||||
// merge init percentile stringVal3 should not change the correct result
|
||||
AggregateFunctions::percentile_approx_init(context, &stringVal);
|
||||
|
||||
for (int i = 1; i <= 100000; i++) {
|
||||
DoubleVal val(i);
|
||||
AggregateFunctions::percentile_approx_update(context, val, doubleQ, &stringVal);
|
||||
}
|
||||
serialized = AggregateFunctions::percentile_approx_serialize(context, stringVal);
|
||||
|
||||
StringVal stringVal3;
|
||||
AggregateFunctions::percentile_approx_init(context, &stringVal2);
|
||||
AggregateFunctions::percentile_approx_init(context, &stringVal3);
|
||||
StringVal serialized2 = AggregateFunctions::percentile_approx_serialize(context, stringVal3);
|
||||
|
||||
AggregateFunctions::percentile_approx_merge(context, serialized, &stringVal2);
|
||||
AggregateFunctions::percentile_approx_merge(context, serialized2, &stringVal2);
|
||||
v = AggregateFunctions::percentile_approx_finalize(context, stringVal2);
|
||||
ASSERT_DOUBLE_EQ(v.val, 99900.5);
|
||||
|
||||
delete futil;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user