This topic describes the built-in RoaringBitmap SQL functions of a Fluss catalog, including the function list, signatures, null handling, and typical usage.
Overview
RoaringBitmap SQL functions are registered by the Fluss catalog and run inside the Flink query engine. They build bitmaps, perform set operations, and count cardinality. They fit exact-distinct scenarios such as real-time UV, user retention, and audience targeting.
These functions differ from the storage-level rbm32 and rbm64 aggregators:
|
Item |
RoaringBitmap SQL functions |
rbm32 / rbm64 aggregators |
|
Where they run |
Flink query engine |
Fluss TabletServer |
|
How they are triggered |
Called explicitly in SQL |
Run automatically when primary key table records are merged |
|
Scope |
Single-row bitmap operations and cross-row bitmap aggregation |
Union of bitmap batches that share the same primary key |
The two are complementary. SQL functions produce bitmaps on the Flink side, and the rbm32 aggregator accumulates them on the server side by primary key. For more information about the merge engine, see Aggregation Merge Engine.
Prerequisites
-
A Fluss cluster is created. For more information, see Activate Streaming Storage Fluss.
-
A Fluss catalog is registered in your Flink workspace. For more information, see Register a Fluss Catalog.
Limits
-
The functions are registered with the Fluss catalog. They are not built-in functions of the Flink engine. To call them from a job that does not use the Fluss catalog as the current catalog, use fully qualified names.
-
All functions are based on 32-bit RoaringBitmaps. Bitmap values use the
BYTEStype, and the serialization format is the same as the one used by therbm32aggregator. The 64-bit RoaringBitmap data used by therbm64aggregator is not supported. Other input and return types vary by function. Refer to each function signature. -
rb_and_aggandrb_xor_agghave no server-side counterpart and run entirely in Flink. -
The functions in this topic are provided by the Fluss catalog and represent bitmaps as
BYTES. The BITMAP functions of Realtime Compute for Apache Flink are provided by the engine and use theBITMAPtype. Bitmap arguments cannot be passed directly between the two. In engine versions that support the corresponding conversion functions, useBITMAP_FROM_BYTESandBITMAP_TO_BYTESto convert explicitly between standard 32-bit RoaringBitmap bytes and theBITMAPtype. If the current catalog is not a Fluss catalog, call these functions by fully qualified name.
RoaringBitmap SQL functions are provided by the catalog implementation in the Fluss connector that the Flink job loads. If a function-not-found error occurs, first check whether the current connector version includes these functions. Then verify the current catalog, the database, and the fully qualified name of the function. Upgrading only the server side of the Fluss cluster does not guarantee that these functions are available. For more information, see Cluster Upgrade Guide.
Function list
Scalar functions
|
Function |
Signature |
Description |
|
|
|
Builds a serialized bitmap from the |
|
|
|
Returns the number of distinct integers in a bitmap. |
|
|
|
Returns whether a bitmap contains a specific integer. |
|
|
|
Converts a bitmap to an |
|
|
|
Returns the union of two bitmaps. |
|
|
|
Returns the intersection of two bitmaps. |
|
|
|
Returns the symmetric difference of two bitmaps. |
|
|
|
Returns the elements of the left bitmap that are absent from the right bitmap. |
Aggregate functions
|
Function |
Signature |
Description |
|
|
|
Aggregates an |
|
|
|
Merges bitmaps across rows by bitwise OR. |
|
|
|
Merges bitmaps across rows by bitwise AND. |
|
|
|
Merges bitmaps across rows by bitwise XOR. |
How to call the functions
After you register a Fluss catalog, its built-in functions are available right away. No CREATE TEMPORARY FUNCTION statement is required.
Use the Fluss catalog as the current catalog
If a session mainly works with Fluss objects, switch the current catalog and use the function names directly.
USE CATALOG `fluss_catalog`;
SELECT rb_cardinality(rb_build(ARRAY[1, 2, 3, 2]));
-- Output: 3
Use fully qualified names
If a job works across catalogs, keep the current catalog unchanged and call each function as <catalog>.<database>.<function>.
SELECT `fluss_catalog`.`fluss`.rb_cardinality(
`fluss_catalog`.`fluss`.rb_build(ARRAY[1, 2, 3, 2])
);
-- Output: 3
fluss is the default database of a Fluss catalog. You can replace it with another Fluss database name. That database must already exist, and the current user must have permission to access it.
Example: accumulate windowed bitmaps for distinct counts
The following example counts the cumulative UV of each page. rb_build_agg aggregates the user IDs of each processing-time window into a bitmap and writes it to a Fluss primary key table. The rbm32 aggregator merges the bitmap batches of the same primary key into a cumulative bitmap on the server side. rb_cardinality reads the cumulative distinct count at query time.
This example assumes that the Fluss catalog contains a log table named click_events with the columns page_id BIGINT, user_id INT, and proc_time AS PROCTIME().
USE CATALOG `fluss_catalog`;
-- Store one cumulative bitmap per page in Fluss.
CREATE TABLE page_uv (
page_id BIGINT,
uv_bitmap BYTES,
PRIMARY KEY (page_id) NOT ENFORCED
) WITH (
'table.merge-engine' = 'aggregation',
'fields.uv_bitmap.agg' = 'rbm32'
);
-- Build one bitmap per page every 5 seconds.
-- Each window writes a bitmap batch, and rbm32 merges it into the existing bitmap of that page.
INSERT INTO page_uv
SELECT page_id,
rb_build_agg(user_id) AS uv_bitmap
FROM TABLE(
TUMBLE(
TABLE click_events,
DESCRIPTOR(proc_time),
INTERVAL '5' SECOND
)
)
GROUP BY page_id, window_start, window_end;
-- Query the UV of a page.
SELECT page_id,
rb_cardinality(uv_bitmap) AS uv
FROM page_uv
WHERE page_id = 1;
'table.merge-engine' = 'aggregation' enables the merge engine. 'fields.uv_bitmap.agg' = 'rbm32' applies 32-bit RoaringBitmap union aggregation to the uv_bitmap field.
Scalar functions
A scalar function operates on a single row and returns a single value.
rb_build
Builds a serialized RoaringBitmap from the ARRAY<INT> of a single row.
-
Signature:
rb_build(values ARRAY<INT>) → BYTES -
Null handling: Returns
NULLif the array argument isNULL.NULLelements inside the array are ignored. An empty array or an array whose elements are allNULLreturns an empty bitmap.
SELECT rb_cardinality(rb_build(ARRAY[1, 2, 3, 2]));
-- Output: 3. The duplicate value 2 is ignored.
SELECT rb_cardinality(rb_build(ARRAY[CAST(NULL AS INT), 1, 2]));
-- Output: 2. The null element is ignored.
SELECT rb_build(CAST(NULL AS ARRAY<INT>)) IS NULL;
-- Output: TRUE
rb_cardinality
Returns the number of distinct integers in a serialized RoaringBitmap.
-
Signature:
rb_cardinality(bitmap BYTES) → BIGINT -
Null handling: Returns
NULLif the input isNULL. Returns 0 for an empty bitmap.
SELECT rb_cardinality(rb_build(ARRAY[1, 2, 3, 2]));
-- Output: 3
SELECT rb_cardinality(rb_build(ARRAY[CAST(NULL AS INT)]));
-- Output: 0. The bitmap is empty.
rb_contains
Returns whether a serialized RoaringBitmap contains a specific integer.
-
Signature:
rb_contains(bitmap BYTES, value INT) → BOOLEAN -
Null handling: Returns
NULLif either argument isNULL.
SELECT rb_contains(rb_build(ARRAY[1, 2, 3]), 2);
-- Output: TRUE
SELECT rb_contains(rb_build(ARRAY[1, 2, 3]), 5);
-- Output: FALSE
rb_to_array
Converts a serialized RoaringBitmap to an ARRAY<INT> in ascending order.
-
Signature:
rb_to_array(bitmap BYTES) → ARRAY<INT> -
Null handling: Returns
NULLif the input isNULL. Returns an empty array for an empty bitmap.
SELECT rb_to_array(rb_build(ARRAY[3, 1, 2]));
-- Output: [1, 2, 3], in ascending order
rb_or
Returns the union (bitwise OR) of two serialized RoaringBitmap values.
-
Signature:
rb_or(left BYTES, right BYTES) → BYTES -
Null handling: Returns
NULLif either argument isNULL. To union bitmaps across rows while ignoring nulls, userb_or_agg.
SELECT rb_cardinality(rb_or(rb_build(ARRAY[1, 2]), rb_build(ARRAY[2, 3])));
-- Output: 3, that is, {1, 2, 3}
rb_and
Returns the intersection (bitwise AND) of two serialized RoaringBitmap values.
-
Signature:
rb_and(left BYTES, right BYTES) → BYTES -
Null handling: Returns
NULLif either argument isNULL. Returns an empty bitmap instead ofNULLwhen the intersection is empty.
SELECT rb_cardinality(rb_and(rb_build(ARRAY[1, 2, 3]), rb_build(ARRAY[2, 3, 4])));
-- Output: 2, that is, {2, 3}
SELECT rb_cardinality(rb_and(rb_build(ARRAY[1, 2]), rb_build(ARRAY[3, 4])));
-- Output: 0. The two sets are disjoint.
rb_xor
Returns the symmetric difference (bitwise XOR) of two serialized RoaringBitmap values, that is, the elements present in exactly one of the two inputs.
-
Signature:
rb_xor(left BYTES, right BYTES) → BYTES -
Null handling: Returns
NULLif either argument isNULL. Returns an empty bitmap instead ofNULLwhen the two inputs are identical.
SELECT rb_cardinality(rb_xor(rb_build(ARRAY[1, 2, 3]), rb_build(ARRAY[2, 3, 4])));
-- Output: 2, that is, {1, 4}
SELECT rb_cardinality(rb_xor(rb_build(ARRAY[1, 2]), rb_build(ARRAY[1, 2])));
-- Output: 0. Identical inputs cancel out.
rb_andnot
Returns the elements that are present in the left bitmap but absent from the right bitmap.
-
Signature:
rb_andnot(left BYTES, right BYTES) → BYTES -
Null handling: Returns
NULLif either argument isNULL. Returns an empty bitmap instead ofNULLwhen the right bitmap is a superset of the left bitmap.
SELECT rb_cardinality(rb_andnot(rb_build(ARRAY[1, 2, 3, 4]), rb_build(ARRAY[3, 4, 5])));
-- Output: 2, that is, {1, 2}
-- Users who visited page 1 but not page 2
SELECT rb_cardinality(rb_andnot(a.uv_bitmap, b.uv_bitmap)) AS exclusive_visitors
FROM page_uv a, page_uv b
WHERE a.page_id = 1 AND b.page_id = 2;
Aggregate functions
An aggregate function reduces multiple rows into a single bitmap result.
rb_or_agg
Merges multiple serialized RoaringBitmap values across rows by bitwise OR.
-
Signature:
rb_or_agg(bitmap BYTES) → BYTES -
Null handling:
NULLand empty inputs are ignored. ReturnsNULLif all inputs areNULL.
-- Roll up daily bitmaps into a weekly UV count
SELECT rb_cardinality(rb_or_agg(daily_bitmap)) AS weekly_uv
FROM (
VALUES
(1, rb_build(ARRAY[1, 2])),
(2, rb_build(ARRAY[2, 3]))
) AS t(day_id, daily_bitmap);
-- Output: 3, that is, users {1, 2, 3} across both days
rb_and_agg
Merges multiple serialized RoaringBitmap values across rows by bitwise AND.
-
Signature:
rb_and_agg(bitmap BYTES) → BYTES -
Null handling:
NULLand empty inputs are ignored. ReturnsNULLif the intersection is empty or if all inputs areNULL.
-- Find the users who appeared on every day
SELECT rb_cardinality(rb_and_agg(daily_bitmap)) AS retained_users
FROM (
VALUES
(1, rb_build(ARRAY[1, 2])),
(2, rb_build(ARRAY[2, 3]))
) AS t(day_id, daily_bitmap);
-- Output: 1. Only user 2 appeared on both days.
rb_and_agg has no server-side counterpart and runs entirely in Flink. On append-only streams, avoid combining it with 'table.merge-engine' = 'aggregation'.
rb_xor_agg
Merges multiple serialized RoaringBitmap values across rows by bitwise XOR and returns the elements that appear in an odd number of input bitmaps.
-
Signature:
rb_xor_agg(bitmap BYTES) → BYTES -
Null handling:
NULLand empty inputs are ignored. ReturnsNULLonly when no non-NULLinput has been accumulated. As long as at least one non-NULLinput remains, inputs that cancel out return an empty bitmap.
-- Find the users who appeared on an odd number of days
SELECT rb_cardinality(rb_xor_agg(daily_bitmap)) AS changed_users
FROM (
VALUES
(1, rb_build(ARRAY[1, 2])),
(2, rb_build(ARRAY[2, 3]))
) AS t(day_id, daily_bitmap);
-- Output: 2. Users {1, 3} each appeared on exactly one day.
rb_xor_agg has no server-side counterpart and runs entirely in Flink. Unlike rb_and_agg, it supports retraction on retractable streams because XOR is self-inverse.