> For the complete documentation index, see [llms.txt](https://docs.pinot.apache.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.pinot.apache.org/functions/sketch/distinctcountull.md).

# DISTINCTCOUNTULL

This section contains reference documentation for the DISTINCTCOUNTULL function.

The [UltraLogLog](https://github.com/dynatrace-oss/hash4j/blob/main/src/main/java/com/dynatrace/hash4j/distinctcount/UltraLogLog.java) sketch enables space-efficient cardinality estimation using the hash4j library. UltraLogLog provides similar accuracy to HyperLogLog but with reduced memory consumption and faster merge operations.

For exact distinct counting, see [DISTINCTCOUNT](/functions/aggregation/distinctcount.md).

## Signature

> distinctCountULL(**\<column>, \<p>**) -> Long

* `column` (required): Name of the column to aggregate on.
* `p` (optional): The precision parameter that controls the number of registers used by the sketch. Higher values give more accurate results but use more memory. Default is `12`.

The input column can be single-value or multi-value. For a multi-value column, Pinot adds every value in each row to the sketch.

## Usage Examples

```sql
SELECT distinctCountULL(teamID) AS value
FROM baseballStats
```

| value |
| ----- |
| 150   |

```sql
SELECT distinctCountULL(teamID, 14) AS value
FROM baseballStats
```

| value |
| ----- |
| 150   |

### Related Functions

| Function                                                        | Description                                                                                          |
| --------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------- |
| [DISTINCTCOUNTULL](/functions/sketch/distinctcountull.md)       | Returns the estimated distinct count as a Long                                                       |
| [DISTINCTCOUNTRAWULL](/functions/sketch/distinctcountrawull.md) | Returns the serialized UltraLogLog sketch as a Base64-encoded String                                 |
| [DISTINCTCOUNTSMARTULL](#distinctcountsmartull)                 | Hybrid approach that uses a Set for low cardinality and converts to ULL when a threshold is exceeded |

## DISTINCTCOUNTRAWULL

Returns the serialized UltraLogLog sketch as a Base64-encoded string. The serialized sketch can be deserialized and merged with other sketches for multi-stage aggregation across tables.

### Signature

> distinctCountRawULL(**\<column>, \<p>**) -> String

* `column` (required): Name of the column to aggregate on.
* `p` (optional): The precision parameter. Default is `12`.

### Usage Example

```sql
SELECT distinctCountRawULL(teamID) AS sketchValue
FROM baseballStats
```

## DISTINCTCOUNTSMARTULL

A hybrid distinct count function that starts with exact counting using a HashSet for low-cardinality data and automatically switches to UltraLogLog estimation when the number of distinct values exceeds a configurable threshold.

### Signature

> distinctCountSmartULL(**\<column>, \<params>**) -> Integer

* `column` (required): Name of the column to aggregate on.
* `params` (optional): Semicolon-separated parameter string. Supported keys:
  * `threshold`: Number of distinct values before switching from exact Set to ULL. Default is `100000`. Set to a non-positive value to never switch.
  * `p`: Precision parameter for ULL when the switch occurs. Default is `12`.

### Usage Examples

```sql
SELECT distinctCountSmartULL(teamID) AS value
FROM baseballStats
```

```sql
SELECT distinctCountSmartULL(teamID, 'threshold=10000;p=14') AS value
FROM baseballStats
```

This function is useful when you have a mix of low-cardinality and high-cardinality columns and want exact counts for the former while still getting efficient approximate counts for the latter.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.pinot.apache.org/functions/sketch/distinctcountull.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
