[Bug-Fix] Fix the bug of PERCENTILE_APPROX return error result nan and add PERCENTILE_APPROX UT (#5172)

This commit is contained in:
HappenLee
2021-01-03 15:45:22 +08:00
committed by GitHub
parent 9e19b6b133
commit f2cf8d2c5e
2 changed files with 28 additions and 2 deletions

View File

@ -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;
}

View File

@ -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;
}