APPROX_COUNT_DISTINCT uses HyperLogLog (HLL) sketches.
Calling APPROX_COUNT_DISTINCT returns the same value as calling HLL_COUNT_DISTINCT with precision 17.
Syntax
Parameters
| Parameter | Description | Supported input types |
|---|---|---|
<expression> | Expression on which to approximate the distinct count | Any type |
<condition> | An optional boolean expression to filter rows used in aggregation | BOOL |
Return Type
BIGINT
Example
When aggregating on few distinct values,APPROX_COUNT_DISTINCT has no estimation error and returns exact results:
| approximate | exact |
|---|---|
| 1,000 | 1,000 |
NULL values do not change the result of APPROX_COUNT_DISTINCT:
| approximate | exact |
|---|---|
| 1,000 | 1,000 |
| approximate | exact |
|---|---|
| 50,160 | 50,000 |
APPROX_COUNT_DISTINCT also works for compound types:
| approximate | exact |
|---|---|
| 4 | 4 |