BITMAP_ANDNOT_CARDINALITY

Description

The BITMAP_ANDNOT_CARDINALITY function is used to calculate the set difference (i.e., andnot operation) of two bitmap type parameters and returns the number of elements in the resulting bitmap. This function can efficiently perform set operations when handling large-scale datasets, making it particularly suitable for scenarios that require quick calculations of relationships between sets.

Function Syntax

bitmap_andnot_cardinality(left, right)
  • left: The first bitmap parameter.
  • right: The second bitmap parameter.

Return Type

Returns a value of bigint type, representing the number of elements in the resulting bitmap.

Usage Example

The following example demonstrates how to use the BITMAP_ANDNOT_CARDINALITY function to calculate the set difference of two bitmaps and get the number of elements in the result.

Example 1: Calculate the set difference of two simple bitmaps

SELECT bitmap_andnot_cardinality(
    bitmap_build(array(1, 2, 3)),
    bitmap_build(array(2, 3, 4))
);

Results:

1

In this example, the first bitmap contains elements 1, 2, and 3, and the second bitmap contains elements 2, 3, and 4. After the set difference operation, the resulting bitmap only contains elements that are present in the first bitmap but not in the second bitmap, which is only element 1. Therefore, the result is 1.

Example 2: Applying the BITMAP_ANDNOT_CARDINALITY function in an actual data table Suppose we have a data table named orders with the following columns: order_id, customer_id, and order_date. We want to find out the number of orders that a specific customer did not place within a specific date range.

SELECT customer_id,
       bitmap_andnot_cardinality(
           bitmap_union(
               -- Construct a bitmap of all orders within a specific date range
               SELECT bitmap_or(
                   bitmap_build(
                       -- Extract customer_id from each order
                       array_agg(DISTINCT order_id)
                   ) FROM orders
                   WHERE order_date BETWEEN '2022-01-01' AND '2022-01-31'
               )
           ),
           -- Construct a bitmap of all orders for a specific customer
           bitmap_build(
               array_agg(DISTINCT order_id)
           ) FROM orders
           WHERE customer_id = 12345
       ) AS orders_count
FROM orders
WHERE order_date BETWEEN '2022-01-01' AND '2022-01-31'
GROUP BY customer_id;

In this example, we first use the bitmap_union function to construct a bitmap of all orders within a specific date range. Then, we use the bitmap_build function to construct a bitmap of all orders for a specific customer. Finally, we use the BITMAP_ANDNOT_CARDINALITY function to calculate the set difference of these two bitmaps and return the number of elements in the result, which is the number of orders that the customer did not place within the specific date range.