All Products
Search
Document Center

Realtime Compute for Apache Flink:SQL Functions

Last Updated:Oct 23, 2026

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

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 BYTES type, and the serialization format is the same as the one used by the rbm32 aggregator. The 64-bit RoaringBitmap data used by the rbm64 aggregator is not supported. Other input and return types vary by function. Refer to each function signature.

  • rb_and_agg and rb_xor_agg have 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 the BITMAP type. Bitmap arguments cannot be passed directly between the two. In engine versions that support the corresponding conversion functions, use BITMAP_FROM_BYTES and BITMAP_TO_BYTES to convert explicitly between standard 32-bit RoaringBitmap bytes and the BITMAP type. If the current catalog is not a Fluss catalog, call these functions by fully qualified name.

Note

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

rb_build

rb_build(values ARRAY<INT>) → BYTES

Builds a serialized bitmap from the ARRAY<INT> of a single row.

rb_cardinality

rb_cardinality(bitmap BYTES) → BIGINT

Returns the number of distinct integers in a bitmap.

rb_contains

rb_contains(bitmap BYTES, value INT) → BOOLEAN

Returns whether a bitmap contains a specific integer.

rb_to_array

rb_to_array(bitmap BYTES) → ARRAY<INT>

Converts a bitmap to an ARRAY<INT> in ascending order.

rb_or

rb_or(left BYTES, right BYTES) → BYTES

Returns the union of two bitmaps.

rb_and

rb_and(left BYTES, right BYTES) → BYTES

Returns the intersection of two bitmaps.

rb_xor

rb_xor(left BYTES, right BYTES) → BYTES

Returns the symmetric difference of two bitmaps.

rb_andnot

rb_andnot(left BYTES, right BYTES) → BYTES

Returns the elements of the left bitmap that are absent from the right bitmap.

Aggregate functions

Function

Signature

Description

rb_build_agg

rb_build_agg(value INT) → BYTES

Aggregates an INT column across rows into a bitmap.

rb_or_agg

rb_or_agg(bitmap BYTES) → BYTES

Merges bitmaps across rows by bitwise OR.

rb_and_agg

rb_and_agg(bitmap BYTES) → BYTES

Merges bitmaps across rows by bitwise AND.

rb_xor_agg

rb_xor_agg(bitmap BYTES) → BYTES

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 NULL if the array argument is NULL. NULL elements inside the array are ignored. An empty array or an array whose elements are all NULL returns 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 NULL if the input is NULL. 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 NULL if either argument is NULL.

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 NULL if the input is NULL. 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 NULL if either argument is NULL. To union bitmaps across rows while ignoring nulls, use rb_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 NULL if either argument is NULL. Returns an empty bitmap instead of NULL when 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 NULL if either argument is NULL. Returns an empty bitmap instead of NULL when 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 NULL if either argument is NULL. Returns an empty bitmap instead of NULL when 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_build_agg

Aggregates an INT column across rows into a serialized RoaringBitmap.

  • Signature: rb_build_agg(value INT) → BYTES

  • Null handling: NULL inputs are ignored. Returns NULL if all inputs are NULL.

SELECT rb_cardinality(rb_build_agg(user_id)) AS uv
FROM (VALUES (1), (2), (3), (2)) AS t(user_id);
-- Output: 3, the number of distinct users

rb_or_agg

Merges multiple serialized RoaringBitmap values across rows by bitwise OR.

  • Signature: rb_or_agg(bitmap BYTES) → BYTES

  • Null handling: NULL and empty inputs are ignored. Returns NULL if all inputs are NULL.

-- 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: NULL and empty inputs are ignored. Returns NULL if the intersection is empty or if all inputs are NULL.

-- 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.
Note

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: NULL and empty inputs are ignored. Returns NULL only when no non-NULL input has been accumulated. As long as at least one non-NULL input 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.
Note

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.