---
title: hll_union_agg function
description: Learn the syntax of the hll\_union\_agg aggregate function of the SQL language in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/aws/en/sql/language-manual/functions/hll_union_agg
---

# `hll_union_agg` function

**Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime 13.3 LTS and above

This function utilizes the [HyperLogLog](https://en.wikipedia.org/wiki/HyperLogLog) algorithm to combine a group of sketches into a single one.

Queries can use the resulting buffers to compute approximate unique counts with the [hll\_sketch\_estimate](https://docs.databricks.com/aws/en/sql/language-manual/functions/hll_sketch_estimate.md) function.

The implementation uses the [Apache Datasketches library](https://datasketches.apache.org). Please see [HLL](https://datasketches.apache.org/docs/HLL/HllJavaExample.html) for more information.

## Syntax

```
hll_union_agg ( expr [, allowDifferentLgConfigK ] )
```

This function can also be invoked as a [window function](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-window-functions.md) using the `OVER` clause.

## Arguments

* `expr`: A `BINARY` expression holding a sketch generated by [hll\_sketch\_agg](https://docs.databricks.com/aws/en/sql/language-manual/functions/hll_sketch_agg.md).
* `allowDifferentLgConfigK`: A optional `BOOLEAN` [constant expression](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-expression.md#constant-expression) controlling whether to allow merging sketches with different lgConfigK values. The default value is false.

## Returns

A `BINARY` buffer containing the HyperLogLog sketch computed as a result of combining the input expressions of the same group.

When the `allowDifferentLgConfigK` parameter is true, the result sketch uses the smaller of the two provided `lgConfigK` values.

Databricks raises [HLL\_UNION\_DIFFERENT\_LG\_K](https://docs.databricks.com/aws/en/error-messages/error-classes.md#hll_union_different_lg_k) if input sketches have different `lgConfigK` values and `allowDifferentLgConfigK` is `false`.

Databricks raises [HLL\_INVALID\_INPUT\_SKETCH\_BUFFER](https://docs.databricks.com/aws/en/error-messages/error-classes.md#hll_invalid_input_sketch_buffer) if `expr` is not a valid HLL sketch buffer.

## Common error conditions

* [HLL\_INVALID\_INPUT\_SKETCH\_BUFFER](https://docs.databricks.com/aws/en/error-messages/error-classes.md#hll_invalid_input_sketch_buffer)
* [HLL\_UNION\_DIFFERENT\_LG\_K](https://docs.databricks.com/aws/en/error-messages/error-classes.md#hll_union_different_lg_k)

## Examples

```sql
> SELECT hll_sketch_estimate(hll_union_agg(sketch, true))
    FROM (SELECT hll_sketch_agg(col) as sketch
            FROM VALUES (1) AS tab(col)
          UNION ALL
          SELECT hll_sketch_agg(col, 20) as sketch
            FROM VALUES (1) AS tab(col));
  1

> SELECT hll_sketch_estimate(hll_union_agg(sketch, false))
    FROM (SELECT hll_sketch_agg(col) as sketch
            FROM VALUES (1) AS tab(col)
          UNION ALL
          SELECT hll_sketch_agg(col, 20) as sketch
            FROM VALUES (1) AS tab(col));
  Error: HLL_UNION_DIFFERENT_LG_K
```

## Related

* [`hll_sketch_agg` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/hll_sketch_agg.md)
* [`hll_sketch_estimate` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/hll_sketch_estimate.md)
* [`hll_union` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/hll_union.md)
* [`hll_union_agg` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/hll_union_agg.md#)
* [Window functions](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-window-functions.md)
