Skip to content

Commit

Permalink
Add regression test for bug fixed by 4eb5bff
Browse files Browse the repository at this point in the history
Commit 4eb5bff fixed the bug in tdigest_compute_quantiles, this just
adds a simple test to trigger the issue.
  • Loading branch information
tvondra committed Aug 12, 2020
1 parent 4eb5bff commit 0be29a2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
20 changes: 20 additions & 0 deletions test/expected/tdigest.out
Original file line number Diff line number Diff line change
Expand Up @@ -1277,3 +1277,23 @@ FROM (
0.99 | t |
(2 rows)

-- check that the computed percentiles are perfectly correlated (don't decrease for higher p values)
-- first test on a tiny t-digest with all centroids having count = 1
WITH
-- percentiles to compute
perc AS (SELECT array_agg((i / 100.0)::double precision) AS percentiles FROM generate_series(1,99) s(i)),
-- input data (just 15 points)
input_data AS (select i::double precision AS val FROM generate_series(1,15) s(i))
SELECT * FROM (
SELECT p, v AS v1, lag(v, 1) OVER (ORDER BY p) v2 FROM (
SELECT
unnest(perc.percentiles) p,
unnest(tdigest_percentile(input_data.val, 100, perc.percentiles)) v
FROM perc, input_data
GROUP BY perc.percentiles
) foo
) bar where v2 > v1;
p | v1 | v2
---+----+----
(0 rows)

17 changes: 17 additions & 0 deletions test/sql/tdigest.sql
Original file line number Diff line number Diff line change
Expand Up @@ -952,3 +952,20 @@ FROM (
unnest(percentile_cont(ARRAY[0.01, 0.99]) WITHIN GROUP (ORDER BY x)) AS b
FROM data
) foo;

-- check that the computed percentiles are perfectly correlated (don't decrease for higher p values)
-- first test on a tiny t-digest with all centroids having count = 1
WITH
-- percentiles to compute
perc AS (SELECT array_agg((i / 100.0)::double precision) AS percentiles FROM generate_series(1,99) s(i)),
-- input data (just 15 points)
input_data AS (select i::double precision AS val FROM generate_series(1,15) s(i))
SELECT * FROM (
SELECT p, v AS v1, lag(v, 1) OVER (ORDER BY p) v2 FROM (
SELECT
unnest(perc.percentiles) p,
unnest(tdigest_percentile(input_data.val, 100, perc.percentiles)) v
FROM perc, input_data
GROUP BY perc.percentiles
) foo
) bar where v2 > v1;

0 comments on commit 0be29a2

Please sign in to comment.