I recently had to troubleshoot some Apache Druid code which was giving difficulty. I am presenting a manufactured example of the issue here, with a synthetic data set, in case it proves useful to anyone else.
The Problem
A data analyst noticed that for some dates, results from a druid database were reporting a higher number of unique visitors than visits. Since clearly every visitor has at least one visit, this should be impossible. The data analyst was confused because looking at the underlying data, the data looked correct.

The answer lay in the way Druid is configured. By default COUNT DISTINCT in Druid is actually an approximate count. When the number of visits and unique visitors is close, this approximation can lead to unique visitors appearing to outnumber visits.
So how can we fix this. Well there are several options each with their pros and cons. Lets take a look at them
Option One – Count Exactly
We can count exactly by sending a context for our query as follows
{
"useApproximateCountDistinct": false
}
To experiment with this directly in the Druid interface you can click on the engine button and then select “Edit query context” and paste in the above. This is a nice simple approach, however it can make the query run much slower. In one test, this increased query time from 0.04 seconds to 4.03 seconds. Performance has dropped by a factor of 100. For larger queries this performance hit may well be unacceptable.
For scenarios where a precise distinct count is required, this may be the only option, however for many dashboard purposes a close approximation may be acceptable. Which leads us onto the other possibilities.
Option Two – Use a Better Approximation
When useApproximateCountDistinct is true, druid uses APPROX_COUNT_DISTINCT_BUILTIN which is a built in version of a HyperLogLog algorithm with errors of up to 4%. Fortunately for us Druid offers a couple of better approximations via Apache DataSketches.
APPROX_COUNT_DISTINCT_DS_HLL uses the HyperLogLog approximation from the Apache DataSketches library. Higher K values passed to this function return more accurate results with an error <0.5% for K=16. In tests this ran in 0.07 seconds, a much more acceptable performance penalty.
APPROX_COUNT_DISTINCT_DS_THETA uses the Theta Sketch algorithm from the Apache DataSketches library. Once again this function has a parameter K. K must be a power of 2 and counts up to K will be exact, beyond that the worse case error is <0.8% for K = 16384. In tests this ran in 0.06 seconds, an acceptable performance penalty of 50%.
Option Three – Use a Better Approximation and Apply a Ceiling
Even using better approximations the possibility exists for contradictions to occur between unique visitors and visits in our example. This can lead to a loss of trust in a dashboard or a constant need to draw attention to the approximation. To avoid this, we can apply a ceiling with LEAST to ensure that the visitorCount never exceeds visits. This is really a cosmetic correction, but it imposes a minimal performance penalty (<0.01s in my tests) and can forestall questions and bug reports.
LEAST(
COALESCE(APPROX_COUNT_DISTINCT_DS_THETA("fullVisitorId", 16384), 0),
COALESCE(COUNT("visitId"), 0)
) AS "uniqueVisitors"
Conclusion
This image demonstrates the various approaches in action:

Using exact counts of distinct values can impose a major performance penalty on Apache Druid. In practice I tend to use a combination of LEAST and APPROX_COUNT_DISTINCT_DS_THETA in these situations to deliver good and intuitive approximations while maintaining query efficiency.