Resolve a bug where datasketches would not downsample sketches sufficiently
Fix a bug where MSQ datasketches would not downsample the sketches enough. This would cause the sketches to retain more memory, potentially more than the limit configured. This in some cases lead to OutOfMemory errors.
The bug should only occur with when adding keys with large weights, with a single bucket.
This PR adds a check downsamples to the limit instead, if currentBytes/2 is also larger than that the limit, which can be the case if the weight of the key added is somehow large enough.
The earlier condition would also skip downsampling the last bucket more than once since i == sortedHolders.size() - 1 || .. is always true. The condition has been changed and the last bucket will also be downsampled to 1 key if the memory retained is still larger than the limit.
To reproduce, where trips is the 3 days Taxi Cab sample dataset:
WITH
"main" AS (
SELECT
"pickup" AS "pickup",
COUNT(*) AS "Count"
FROM "trips"
WHERE TIME_IN_INTERVAL("__time", '2015-01-11/2016-01-01')
GROUP BY 1
ORDER BY "Count" DESC
LIMIT 200
),
"compare0" AS (
SELECT
"pickup" AS "pickup",
COUNT(*) AS "Count"
FROM "trips"
WHERE TIME_IN_INTERVAL(TIME_SHIFT("__time", 'P1M', 1), '2015-01-11/2016-01-01')
GROUP BY 1
)
SELECT
"main"."pickup" AS "pickup",
"main"."Count" AS "Count",
COALESCE("compare0"."Count", 0) AS "#prev: Count"
FROM "main"
LEFT JOIN "compare0" ON "main"."pickup" IS NOT DISTINCT FROM "compare0"."pickup"
This PR has:
- [ ] been self-reviewed.
- [ ] using the concurrency checklist (Remove this item if the PR doesn't have any relation to concurrency.)
- [ ] added documentation for new or modified features or behaviors.
- [ ] a release note entry in the PR description.
- [ ] added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.
- [ ] added or updated version, license, or notice information in licenses.yaml
- [ ] added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
- [ ] added unit tests or modified existing tests to cover new code paths, ensuring the threshold for code coverage is met.
- [ ] added integration tests.
- [ ] been tested in a test Druid cluster.