BITMAP
The BITMAP type is commonly used for exact deduplication, user profile tags, and similar scenarios. This document describes the table design rules for BITMAP columns in VeloDB Cloud and demonstrates two loading approaches, single-value and multi-element, through Stream Load.
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.
BITMAP type constraints
Before loading BITMAP data, review the following constraints:
| Constraint | Description |
|---|---|
| Supported table models | Duplicate table, Unique table, Aggregate table |
| Column position | Can only be used as a Value column, not as a Key column |
| Aggregate table requirement | In an Aggregate table, the BITMAP column must use the aggregate type BITMAP_UNION |
| Length and default value | Do not specify a length. The stored bitmap size depends on its contents. Omit the default to use the engine-provided empty bitmap. |
For more details about the type, see BITMAP Data Type.
Scenario 1: Load single-value BITMAP data
This applies when each row of source data contains only a single integer value. Use the TO_BITMAP() function to convert the integer into a BITMAP.
Step 1: Prepare the data
Create a CSV file test_bitmap.csv where each row contains a single integer value:
1|koga|17723
2|nijg|146285
3|lojn|347890
4|lofn|489871
5|jfin|545679
6|kon|676724
7|nhga|767689
8|nfubg|879878
9|huang|969798
10|buag|97997
Step 2: Create the target table
Create an Aggregate table with a BITMAP column in the testdb database:
CREATE TABLE testdb.test_bitmap(
typ_id BIGINT NULL COMMENT "ID",
hou VARCHAR(10) NULL COMMENT "one",
arr BITMAP BITMAP_UNION NOT NULL COMMENT "two"
)
AGGREGATE KEY(typ_id,hou)
DISTRIBUTED BY HASH(typ_id,hou) BUCKETS 10;
Step 3: Run Stream Load
Use the TO_BITMAP() function to convert the integer column into a BITMAP:
curl --location-trusted -u "<username>" \
-H "column_separator:|" \
-H "columns:typ_id,hou,arr,arr=to_bitmap(arr)" \
-T test_bitmap.csv \
-XPUT "<endpoint>/api/testdb/test_bitmap/_stream_load"
Step 4: Verify the load result
Use BITMAP_TO_STRING() to convert the BITMAP back into a string and view the loaded data:
mysql> select typ_id,hou,bitmap_to_string(arr) from testdb.test_bitmap;
+--------+-------+-----------------------+
| typ_id | hou | bitmap_to_string(arr) |
+--------+-------+-----------------------+
| 4 | lofn | 489871 |
| 6 | kon | 676724 |
| 9 | huang | 969798 |
| 3 | lojn | 347890 |
| 8 | nfubg | 879878 |
| 7 | nhga | 767689 |
| 1 | koga | 17723 |
| 2 | nijg | 146285 |
| 5 | jfin | 545679 |
| 10 | buag | 97997 |
+--------+-------+-----------------------+
10 rows in set (0.07 sec)
Scenario 2: Load multi-element BITMAP data
When each row in the source data contains multiple integers (for example, multiple user IDs), choose one of the following two methods based on the format of the source file. The differences between the two methods are as follows:
| Method | Source file format | Requires cast conversion | Notes |
|---|---|---|---|
BITMAP_FROM_STRING() | Comma-separated, square brackets not allowed | No | Square brackets are treated as a data quality error |
BITMAP_FROM_ARRAY() | Comma-separated, square brackets allowed | Must cast to array<int> | Without the cast, loading fails because the function signature does not match |
Method A: Use BITMAP_FROM_STRING()
Data format requirements
The arr column in the source file uses comma separators and must not contain square brackets:
1|koga|17,723
2|nijg|146,285
3|lojn|347,890
4|lofn|489,871
5|jfin|545,679
6|kon|676,724
7|nhga|767,689
8|nfubg|879,878
9|huang|969,798
10|buag|97,997
Stream Load command
curl --location-trusted -u "<username>" \
-H "column_separator:|" \
-H "columns:typ_id,hou,arr,arr=bitmap_from_string(arr)" \
-T test_bitmap.csv \
-XPUT "<endpoint>/api/testdb/test_bitmap/_stream_load"
Method B: Use BITMAP_FROM_ARRAY()
Data format requirements
The arr column in the source file is allowed to contain square brackets:
1|koga|[17,723]
2|nijg|[146,285]
3|lojn|[347,890]
4|lofn|[489,871]
5|jfin|[545,679]
6|kon|[676,724]
7|nhga|[767,689]
8|nfubg|[879,878]
9|huang|[969,798]
10|buag|[97,997]
Stream Load command
In Stream Load, you must first cast the string to array<int> and then convert it with BITMAP_FROM_ARRAY():
curl --location-trusted -u "<username>" \
-H "column_separator:|" \
-H "columns:typ_id,hou,arr_str,arr=bitmap_from_array(cast(arr_str as array<int>))" \
-T test_bitmap.csv \
-XPUT "<endpoint>/api/testdb/test_bitmap/_stream_load"
Troubleshooting
Function does not exist error with BITMAP_FROM_ARRAY()
Example error:
[ANALYSIS_ERROR]TStatus: errCode = 2, detailMessage = Does not support non-builtin functions, or function does not exist: bitmap_from_array(<slot 8>)
- Cause: The string was not explicitly cast to
array<int>, so the function signature forBITMAP_FROM_ARRAY()cannot be matched. - Solution: In the
columnsparameter of Stream Load, usecast(arr_str as array<int>)to perform an explicit conversion.
View BITMAP column contents
BITMAP is a binary aggregate type and cannot be viewed directly with SELECT. You need to convert it through functions:
bitmap_to_string(col): Converts the BITMAP into a comma-separated string.bitmap_count(col): Returns the number of distinct elements in the BITMAP.
Why BITMAP cannot be used as a key column
BITMAP is an aggregate binary type. It does not support being used as a Key column for sorting or deduplication, and can only be stored as a Value column to hold aggregate results.