

 Amazon Redshift will no longer support the creation of new Python UDFs starting Patch 198. Existing Python UDFs will continue to function until June 30, 2026. For more information, see the [ blog post ](https://aws.amazon.com/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/). 

# HyperLogLog functions
<a name="hyperloglog-functions"></a>

Following, you can find descriptions for the HyperLogLog functions for SQL that Amazon Redshift supports.

**Topics**
+ [HLL function](r_HLL_function.md)
+ [HLL\$1CREATE\$1SKETCH function](r_HLL_CREATE_SKETCH.md)
+ [HLL\$1CARDINALITY function](r_HLL_CARDINALITY.md)
+ [HLL\$1COMBINE function](r_HLL_COMBINE.md)
+ [HLL\$1COMBINE\$1SKETCHES function](r_HLL_COMBINE_SKETCHES.md)

# HLL function
<a name="r_HLL_function"></a>

The HLL function returns the HyperLogLog cardinality of the input expression values. The HLL function works with any data types except the HLLSKETCH data type. The HLL function ignores NULL values. When there are no rows in a table or all rows are NULL, the resulting cardinality is 0.

## Syntax
<a name="r_HLL_function-synopsis"></a>

```
HLL (aggregate_expression)
```

## Argument
<a name="r_HLL_function-argument"></a>

 *aggregate\$1expression*   
Any valid expression that provides the value to an aggregate, such as a column name. This function supports any data type as input except HLLSKETCH, GEOMETRY, GEOGRAPHY, and VARBYTE.

## Return type
<a name="r_HLL_function-return-type"></a>

The HLL function returns a BIGINT or INT8 value.

## Examples
<a name="r_HLL_function-examples"></a>

The following example returns the cardinality of column `an_int` in table `a_table`.

```
CREATE TABLE a_table(an_int INT);
INSERT INTO a_table VALUES (1), (2), (3), (4);

SELECT hll(an_int) AS cardinality FROM a_table;
cardinality
-------------
4
```

# HLL\$1CREATE\$1SKETCH function
<a name="r_HLL_CREATE_SKETCH"></a>

The HLL\$1CREATE\$1SKETCH function returns an HLLSKETCH data type that encapsulates the input expression values. The HLL\$1CREATE\$1SKETCH function works with any data type and ignores NULL values. When there are no rows in a table or all rows are NULL, the resulting sketch has no index-value pairs such as `{"version":1,"logm":15,"sparse":{"indices":[],"values":[]}}`.

## Syntax
<a name="r_HLL_CREATE_SKETCH-synopsis"></a>

```
HLL_CREATE_SKETCH (aggregate_expression)
```

## Argument
<a name="r_HLL_CREATE_SKETCH-argument"></a>

 *aggregate\$1expression*   
Any valid expression that provides the value to an aggregate, such as a column name. NULL values are ignored. This function supports any data type as input except HLLSKETCH, GEOMETRY, GEOGRAPHY, and VARBYTE.

## Return type
<a name="r_HLL_CREATE_SKETCH-return-type"></a>

The HLL\$1CREATE\$1SKETCH function returns an HLLSKETCH value.

## Examples
<a name="r_HLL_CREATE_SKETCH-examples"></a>

The following example returns the HLLSKETCH type for column `an_int` in table `a_table`. A JSON object is used to represent a sparse HyperLogLog sketch when importing, exporting, or printing sketches. A string representation (in Base64 format) is used to represent a dense HyperLogLog sketch.

```
CREATE TABLE a_table(an_int INT);
INSERT INTO a_table VALUES (1), (2), (3), (4);

SELECT hll_create_sketch(an_int) AS sketch FROM a_table;
sketch
-------------------------------------------------------------------------------------------------------
{"version":1,"logm":15,"sparse":{"indices":[20812342,20850007,22362299,47158030],"values":[1,2,1,1]}}
(1 row)
```

# HLL\$1CARDINALITY function
<a name="r_HLL_CARDINALITY"></a>

The HLL\$1CARDINALITY function returns the cardinality of the input HLLSKETCH data type.

## Syntax
<a name="r_HLL_CARDINALITY-synopsis"></a>

```
HLL_CARDINALITY (hllsketch_expression)
```

## Argument
<a name="r_HLL_CARDINALITY-argument"></a>

 *hllsketch\$1expression*   
Any valid expression that evaluates to an HLLSKETCH type, such as a column name. The input value is the HLLSKETCH data type.

## Return type
<a name="r_HLL_CARDINALITY-return-type"></a>

The HLL\$1CARDINALITY function returns a BIGINT or INT8 value.

## Examples
<a name="r_HLL_CARDINALITY-examples"></a>

The following example returns the cardinality of column `sketch` in table `hll_table`.

```
CREATE TABLE a_table(an_int INT, b_int INT);
INSERT INTO a_table VALUES (1,1), (2,1), (3,1), (4,1), (1,2), (2,2), (3,2), (4,2), (5,2), (6,2);

CREATE TABLE hll_table (sketch HLLSKETCH);
INSERT INTO hll_table select hll_create_sketch(an_int) from a_table group by b_int;

SELECT hll_cardinality(sketch) AS cardinality FROM hll_table;
cardinality
-------------
6
4
(2 rows)
```

# HLL\$1COMBINE function
<a name="r_HLL_COMBINE"></a>

The HLL\$1COMBINE aggregate function returns an HLLSKETCH data type that combines all input HLLSKETCH values. 

The combination of two or more HyperLogLog sketches is a new HLLSKETCH that encapsulates information about the union of the distinct values that each input sketch represents. After combining sketches, Amazon Redshift extracts the cardinality of the union of two or more datasets. For more information on how to combine multiple sketches, see [Example: Return a HyperLogLog sketch from combining multiple sketches](r_HLL-examples.md#hll-examples-multiple-sketches).

## Syntax
<a name="r_HLL_COMBINE-synopsis"></a>

```
HLL_COMBINE (hllsketch_expression)
```

## Argument
<a name="r_HLL_COMBINE-argument"></a>

 *hllsketch\$1expression*   
Any valid expression that evaluates to an HLLSKETCH type, such as a column name. The input value is the HLLSKETCH data type.

## Return type
<a name="r_HLL_COMBINE-return-type"></a>

The HLL\$1COMBINE function returns an HLLSKETCH type.

## Examples
<a name="r_HLL_COMBINE-examples"></a>

The following example returns the combined HLLSKETCH values in the table `hll_table`.

```
CREATE TABLE a_table(an_int INT, b_int INT);
INSERT INTO a_table VALUES (1,1), (2,1), (3,1), (4,1), (1,2), (2,2), (3,2), (4,2), (5,2), (6,2);

CREATE TABLE hll_table (sketch HLLSKETCH);
INSERT INTO hll_table select hll_create_sketch(an_int) from a_table group by b_int;

SELECT hll_combine(sketch) AS sketches FROM hll_table;
sketches
----------------------------------------------------------------------------------------------------------------------------
{"version":1,"logm":15,"sparse":{"indices":[20812342,20850007,22362299,40314817,42650774,47158030],"values":[1,2,1,3,2,1]}}
(1 row)
```

# HLL\$1COMBINE\$1SKETCHES function
<a name="r_HLL_COMBINE_SKETCHES"></a>

The HLL\$1COMBINE\$1SKETCHES is a scalar function that takes as input two HLLSKETCH values and combines them into a single HLLSKETCH.

The combination of two or more HyperLogLog sketches is a new HLLSKETCH that encapsulates information about the union of the distinct values that each input sketch represents.

## Syntax
<a name="r_HLL_COMBINE_SKETCHES-synopsis"></a>

```
HLL_COMBINE_SKETCHES (hllsketch_expression1, hllsketch_expression2)
```

## Argument
<a name="r_HLL_COMBINE_SKETCHES-argument"></a>

 *hllsketch\$1expression1* and *hllsketch\$1expression2*   
Any valid expression that evaluates to an HLLSKETCH type, such as a column name.

## Return type
<a name="r_HLL_COMBINE_SKETCHES-return-type"></a>

The HLL\$1COMBINE\$1SKETCHES function returns an HLLSKETCH type.

## Examples
<a name="r_HLL_COMBINE_SKETCHES-examples"></a>

The following example returns the combined HLLSKETCH values in the table `hll_table`.

```
WITH tbl1(x, y)
     AS (SELECT Hll_create_sketch(1),
                Hll_create_sketch(2)
         UNION ALL
         SELECT Hll_create_sketch(3),
                Hll_create_sketch(4)
         UNION ALL
         SELECT Hll_create_sketch(5),
                Hll_create_sketch(6)
         UNION ALL
         SELECT Hll_create_sketch(7),
                Hll_create_sketch(8)),
     tbl2(x, y)
     AS (SELECT Hll_create_sketch(9),
                Hll_create_sketch(10)
         UNION ALL
         SELECT Hll_create_sketch(11),
                Hll_create_sketch(12)
         UNION ALL
         SELECT Hll_create_sketch(13),
                Hll_create_sketch(14)
         UNION ALL
         SELECT Hll_create_sketch(15),
                Hll_create_sketch(16)
         UNION ALL
         SELECT Hll_create_sketch(NULL),
                Hll_create_sketch(NULL)),
     tbl3(x, y)
     AS (SELECT *
         FROM   tbl1
         UNION ALL
         SELECT *
         FROM   tbl2)
SELECT Hll_combine_sketches(x, y)
FROM   tbl3;
```