Skip to main content
VeloDB Cloud 26.x is based on Apache Doris 4.x. See Version Mapping.

HLL

HLL (HyperLogLog) stores a sketch for approximate distinct counting. Use it when a small estimation error is acceptable, such as estimating daily active users. Compare its accuracy and resource use with exact COUNT(DISTINCT ...) on representative data.

Because raw values cannot be written directly into an HLL column, the data must be hashed with functions such as HLL_HASH() during import. For more information about the HLL type, see HLL.

Before you start​

Create or select the target database, and use a warehouse SQL user with the required table and load permissions. Copy the Stream Load request URL from Connection Methods. Replace <endpoint> with its scheme, host, and port, and preserve HTTPS when it is required. Replace <username> with your warehouse SQL username. The curl commands prompt for your password.

Choose one input-format procedure for each sample table, or recreate the sample table before trying another procedure. The displayed rows and timings illustrate the source examples. They are not measurements from your warehouse.

Applicable scenarios​

ScenarioDescription
UV statistics on massive dataFor example, estimating daily and monthly active unique users
Distinct counting on large data volumesWorkloads where an approximate count meets the accuracy requirement
Replacing Count DistinctPreaggregate sketches when an approximate result meets your accuracy requirements

Workflow overview​

The complete HLL data import workflow consists of the following 4 steps:

  1. Prepare the source data file to import.
  2. Create the target table with an HLL column in VeloDB Cloud.
  3. Import the data through Stream Load together with the HLL_HASH() function.
  4. Verify the import result with HLL_CARDINALITY().

Example​

Step 1: Prepare the data​

Create the source data file test_hll.csv with the following content:

1001|koga
1002|nijg
1003|lojn
1004|lofn
1005|jfin
1006|kon
1007|nhga
1008|nfubg
1009|huang
1010|buag

Step 2: Create the table in the database​

In the target database, create an Aggregate model table that contains an HLL column:

CREATE TABLE testdb.test_hll(
typ_id BIGINT NULL COMMENT "ID",
typ_name VARCHAR(10) NULL COMMENT "NAME",
pv hll hll_union NOT NULL COMMENT "hll"
)
AGGREGATE KEY(typ_id,typ_name)
DISTRIBUTED BY HASH(typ_id) BUCKETS 10;

Notes:

  • In an Aggregate Key table, declare the HLL value column with HLL_UNION.
  • This example uses AGGREGATE KEY to merge sketches for matching keys. Duplicate Key and Unique Key tables can also store HLL value columns.

Step 3: Import the data​

Import the data through Stream Load. Use the columns parameter to apply HLL_HASH() to the source column typ_id and write the result into the pv column:

curl --location-trusted -u "<username>" \
-H "column_separator:|" \
-H "columns:typ_id,typ_name,pv=hll_hash(typ_id)" \
-T test_hll.csv \
-XPUT "<endpoint>/api/testdb/test_hll/_stream_load"

Key parameter descriptions:

ParameterDescription
column_separatorColumn separator in the source file. This example uses |.
columnsField mapping expression. The HLL column must be transformed by HLL_HASH().
<username>Warehouse SQL username. The command prompts for the password.
<endpoint>Warehouse Stream Load endpoint, including the scheme and port, from the generated request URL.

Step 4: Verify the imported data​

Use the HLL_CARDINALITY() function to query the distinct count result of the HLL column:

mysql> select typ_id,typ_name,hll_cardinality(pv) from testdb.test_hll;
+--------+----------+---------------------+
| typ_id | typ_name | hll_cardinality(pv) |
+--------+----------+---------------------+
| 1010 | buag | 1 |
| 1002 | nijg | 1 |
| 1001 | koga | 1 |
| 1008 | nfubg | 1 |
| 1005 | jfin | 1 |
| 1009 | huang | 1 |
| 1004 | lofn | 1 |
| 1007 | nhga | 1 |
| 1003 | lojn | 1 |
| 1006 | kon | 1 |
+--------+----------+---------------------+
10 rows in set (0.06 sec)

The result shows that the HLL column cardinality is 1 for every row, which means the data has been imported correctly and hashed by HLL_HASH().

FAQ​

Why must HLL columns use HLL_HASH()?​

HLL is a special binary aggregate type and cannot store raw values directly. The raw value must first be converted into the internal HLL representation through HLL_HASH() before being written into the HLL column.

What is the difference between HLL and count distinct?​

DimensionHLLCount Distinct
AccuracyApproximate (small margin of error)Exact
ProcessingCan merge precomputed sketchesMust retain enough distinct-value state for an exact result
StorageStores an HLL sketch per valueQuery state grows with the distinct values being counted
Applicable scenariosUV estimation and other scenarios where some error is acceptableScenarios that require exact distinct counting

What table model is required when creating an HLL column?​

HLL can be stored in value columns of Duplicate Key, Unique Key, and Aggregate Key tables. In an Aggregate Key table, use HLL_UNION to merge sketches for matching keys. HLL cannot be a key column.

Common errors​

  • Authentication or connection failure: Check the generated warehouse endpoint, TLS requirements, SQL user permissions, and network access. For remote files, also check source credentials and reachability.
  • Filtered rows or parsing errors: Check the input format, delimiters, field mappings, and target column types. Inspect the returned load error before retrying. Raising the filter tolerance can discard data.
  • Unexpected duplicate rows: These examples use independent loads. Check the table model and existing rows before rerunning a sample.

See also​