Not able to get distinct count from multiple segments

I am having multiple segments using Segment Granularity = All. Now I have a requirement to get a distinct count of a specific field with some other fields from multiple segments. I am using the native Group By query and tried the below option to get Distinct Count across multiple segments.

  1. DistinctCount Aggerator - It is getting unique count from each segment and then does the sum of each segment’s unique count.
  2. Cardinality Aggregator - It is returning the unique count from multiple segments but that is an approximate count and not the exact count.

Can anyone suggest a solution to this? I want the unique count from multiple segments.

You could use a SQL query with COUNT(DISTINCT expr).
In order to get an exact count, you’ll need to turn off “Use approximate COUNT(DISTINCT)” in the query context:
image

More info here: SQL aggregation functions · Apache Druid

exact count distincts may perform poorly. One approach is to use approx_count_distinct_ds_theta. This will give you exact counts for values lesser than the size of the theta sketch. This is good enough in most situations.

My application is running on Group By Native Query and not on SQL query. So in this case code side changes need to be done to use SQL query. Is Use approximate COUNT config available for Cardinality Aggregator as well? I did not find any such type of configuration for Cardinality Aggregator.

is this configuration available for SQL query or native query?

this is druid sql. Native query will use an aggregator of type thetaSketch