> ## Documentation Index
> Fetch the complete documentation index at: https://docs.firebolt.io/llms.txt
> Use this file to discover all available pages before exploring further.

<AgentInstructions>

## Submitting Feedback

If you encounter incorrect, outdated, or confusing documentation on this page, submit feedback:

POST https://docs.firebolt.io/feedback

```json
{
  "path": "/reference-sql/functions-reference/datasketches/apache-datasketches-hll-merge",
  "feedback": "Description of the issue"
}
```

Only submit feedback when you have something specific and actionable to report.

</AgentInstructions>

> Reference material for APACHE_DATASKETCHES_HLL_MERGE

# APACHE_DATASKETCHES_HLL_MERGE

An aggregate function.
Merges one or more [Apache DataSketches HLL sketches](https://datasketches.apache.org/docs/HLL/HllSketches.html) that were
built using the aggregate
function [APACHE\_DATASKETCHES\_HLL\_BUILD](/reference-sql/functions-reference/datasketches/apache-datasketches-hll-build) into a new sketch.

In order to get correct results each sketch must be built on the same `<expression>` datatype.
Attempts to merge sketches for different datatypes will **NOT** result in an error, and it's the user responsibility to
merge skeches with the same datatype only.
For example, you can merge a sketch built from `INTEGER` data with one built from `TEXT` data but the estimated
cardinality will be incorrect.

If sketches are with different hll precision and hll type the merged sketch will be built with lowest precision and type of all inputs.
For example, you can merge a sketch built with precision 13 and a sketch built with precision 14 and the result
precision will be 13.

## Syntax

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
APACHE_DATASKETCHES_HLL_MERGE
(<expression>)
```

## Parameters

| Parameter      | Description                                                                                                                                                                                                                                                            | Supported input types |
| :------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :-------------------- |
| `<expression>` | An [Apache DataSketches HLL sketches](https://datasketches.apache.org/docs/HLL/HllSketches.html) in a valid format, e.g. the output of the [APACHE\_DATASKETCHES\_HLL\_BUILD](/reference-sql/functions-reference/datasketches/apache-datasketches-hll-build) function. | `BYTEA`               |

## Return Type

`BYTEA`

## Error Handling

If the input expression is not a valid [Apache DataSketches HLL sketches](https://datasketches.apache.org/docs/HLL/HllSketches.html), the function will raise an error.
Ensure that the input is correctly formatted and generated by the [APACHE\_DATASKETCHES\_HLL\_BUILD](/reference-sql/functions-reference/datasketches/apache-datasketches-hll-build) function.

## Example

Following the [example](/reference-sql/functions-reference/datasketches/apache-datasketches-hll-build#example)
in [APACHE\_DATASKETCHES\_HLL\_BUILD](/reference-sql/functions-reference/datasketches/apache-datasketches-hll-build):

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
SELECT APACHE_DATASKETCHES_HLL_ESTIMATE(APACHE_DATASKETCHES_HLL_MERGE(a)) AS estimate,
       APACHE_DATASKETCHES_HLL_MERGE(a)                                   AS merged_sketch
FROM sketch_of_data_to_count;
```

| estimate BIGINT | merged\_sketch BYTEA   |
| :-------------- | :--------------------- |
| 6673219         | \x0a0107120018000a.... |
