This is the multi-page printable view of this section. Click here to print.

Return to the regular view of this page.

Bitmap Functions

This section provides reference information for the bitmap functions in PlaidCloud Lakehouse.

1 - BITMAP_AND

Performs a bitwise AND operation on the two bitmaps.

Analyze Syntax

func.bitmap_and( <bitmap1>, <bitmap2> )

Analyze Examples

func.bitmap_and(func.build_bitmap([1, 4, 5]), func.cast(build_bitmap([4, 5])), string)

┌────────────────────────────────────────────────────────────────────────────────────────┐
 func.bitmap_and(func.build_bitmap([1, 4, 5]), func.cast(build_bitmap([4, 5])), string) 
├────────────────────────────────────────────────────────────────────────────────────────┤
 4,5                                                                                    
└────────────────────────────────────────────────────────────────────────────────────────┘

SQL Syntax

BITMAP_AND( <bitmap1>, <bitmap2> )

SQL Examples

SELECT BITMAP_AND(BUILD_BITMAP([1,4,5]), BUILD_BITMAP([4,5]))::String;

┌───────────────────────────────────────────────────────────────────┐
 bitmap_and(build_bitmap([1, 4, 5]), build_bitmap([4, 5]))::string 
├───────────────────────────────────────────────────────────────────┤
 4,5                                                               
└───────────────────────────────────────────────────────────────────┘

2 - BITMAP_AND_COUNT

Counts the number of bits set to 1 in the bitmap by performing a logical AND operation.

Analyze Syntax

func.bitmap_and_count( <bitmap> )

Analyze Examples

func.bitmap_and_count(to_bitmap('1, 3, 5'))

┌─────────────────────────────────────────────┐
 func.bitmap_and_count(to_bitmap('1, 3, 5')) 
├─────────────────────────────────────────────┤
                                           3 
└─────────────────────────────────────────────┘

SQL Syntax

BITMAP_AND_COUNT( <bitmap> )

SQL Examples

SELECT BITMAP_AND_COUNT(TO_BITMAP('1, 3, 5'));

┌────────────────────────────────────────┐
 bitmap_and_count(to_bitmap('1, 3, 5')) 
├────────────────────────────────────────┤
                                      3 
└────────────────────────────────────────┘

3 - BITMAP_AND_NOT

Alias for BITMAP_NOT.

4 - BITMAP_CARDINALITY

Alias for BITMAP_COUNT.

5 - BITMAP_CONTAINS

Checks if the bitmap contains a specific value.

Analyze Syntax

func.bitmap_contains( <bitmap>, <value> )

Analyze Examples

func.bitmap_contains(build_bitmap([1, 4, 5]), 1)

┌───────────────────────────────────────────────────┐
 func.bitmap_contains(build_bitmap([1, 4, 5]), 1)  
├───────────────────────────────────────────────────┤
 true                                              
└───────────────────────────────────────────────────┘

SQL Syntax

BITMAP_CONTAINS( <bitmap>, <value> )

SQL Examples

SELECT BITMAP_CONTAINS(BUILD_BITMAP([1,4,5]), 1);

┌─────────────────────────────────────────────┐
 bitmap_contains(build_bitmap([1, 4, 5]), 1) 
├─────────────────────────────────────────────┤
 true                                        
└─────────────────────────────────────────────┘

6 - BITMAP_COUNT

Counts the number of bits set to 1 in the bitmap.

Analyze Syntax

func.bitmap_count( <bitmap> )

Analyze Examples

func.bitmap_count(build_bitmap([1, 4, 5]))

┌────────────────────────────────────────────┐
 func.bitmap_count(build_bitmap([1, 4, 5])) 
├────────────────────────────────────────────┤
                                          3 
└────────────────────────────────────────────┘

SQL Syntax

BITMAP_COUNT( <bitmap> )

Aliases

SQL Examples

SELECT BITMAP_COUNT(BUILD_BITMAP([1,4,5])), BITMAP_CARDINALITY(BUILD_BITMAP([1,4,5]));

┌─────────────────────────────────────────────────────────────────────────────────────┐
 bitmap_count(build_bitmap([1, 4, 5]))  bitmap_cardinality(build_bitmap([1, 4, 5])) 
├───────────────────────────────────────┼─────────────────────────────────────────────┤
                                     3                                            3 
└─────────────────────────────────────────────────────────────────────────────────────┘

7 - BITMAP_HAS_ALL

Checks if the first bitmap contains all the bits in the second bitmap.

Analyze Syntax

func.bitmap_has_all( <bitmap1>, <bitmap2> )

Analyze Examples

func.bitmap_has_all(build_bitmap([1, 4, 5]), build_bitmap([1, 2])) 

┌─────────────────────────────────────────────────────────────────────┐
 func.bitmap_has_all(build_bitmap([1, 4, 5]), build_bitmap([1, 2]))  
├─────────────────────────────────────────────────────────────────────┤
 false                                                               
└─────────────────────────────────────────────────────────────────────┘

SQL Syntax

BITMAP_HAS_ALL( <bitmap1>, <bitmap2> )

SQL Examples

SELECT BITMAP_HAS_ALL(BUILD_BITMAP([1,4,5]), BUILD_BITMAP([1,2]));

┌───────────────────────────────────────────────────────────────┐
 bitmap_has_all(build_bitmap([1, 4, 5]), build_bitmap([1, 2])) 
├───────────────────────────────────────────────────────────────┤
 false                                                         
└───────────────────────────────────────────────────────────────┘

8 - BITMAP_HAS_ANY

Checks if the first bitmap has any bit matching the bits in the second bitmap.

Analyze Syntax

func.bitmap_has_any( <bitmap1>, <bitmap2> )

Analyze Examples

func.bitmap_has_any(func.build_bitmap([1, 4, 5]), func.build_bitmap([1, 2]))

┌───────────────────────────────────────────────────────────────────────────────┐
 func.bitmap_has_any(func.build_bitmap([1, 4, 5]), func.build_bitmap([1, 2]))  
├───────────────────────────────────────────────────────────────────────────────┤
 true                                                                          
└───────────────────────────────────────────────────────────────────────────────┘

SQL Syntax

BITMAP_HAS_ANY( <bitmap1>, <bitmap2> )

SQL Examples

SELECT BITMAP_HAS_ANY(BUILD_BITMAP([1,4,5]), BUILD_BITMAP([1,2]));

┌───────────────────────────────────────────────────────────────┐
 bitmap_has_any(build_bitmap([1, 4, 5]), build_bitmap([1, 2])) 
├───────────────────────────────────────────────────────────────┤
 true                                                          
└───────────────────────────────────────────────────────────────┘

9 - BITMAP_INTERSECT

Counts the number of bits set to 1 in the bitmap by performing a logical INTERSECT operation.

Analyze Syntax

func.bitmap_intersect( <bitmap> )

Analyze Examples

func.bitmap_intersect(func.to_bitmap('1, 3, 5'))

┌──────────────────────────────────────────────────┐
 func.bitmap_intersect(func.to_bitmap('1, 3, 5')) 
├──────────────────────────────────────────────────┤
 1,3,5                                            
└──────────────────────────────────────────────────┘

SQL Syntax

BITMAP_INTERSECT( <bitmap> )

SQL Examples

SELECT BITMAP_INTERSECT(TO_BITMAP('1, 3, 5'))::String;

┌────────────────────────────────────────────────┐
 bitmap_intersect(to_bitmap('1, 3, 5'))::string 
├────────────────────────────────────────────────┤
 1,3,5                                          
└────────────────────────────────────────────────┘

10 - BITMAP_MAX

Gets the maximum value in the bitmap.

Analyze Syntax

func.bitmap_max( <bitmap> )

Analyze Examples

func.bitmap_max(func.build_bitmap([1, 4, 5]))

┌───────────────────────────────────────────────┐
 func.bitmap_max(func.build_bitmap([1, 4, 5])) 
├───────────────────────────────────────────────┤
                                             5 
└───────────────────────────────────────────────┘

SQL Syntax

BITMAP_MAX( <bitmap> )

SQL Examples

SELECT BITMAP_MAX(BUILD_BITMAP([1,4,5]));

┌─────────────────────────────────────┐
 bitmap_max(build_bitmap([1, 4, 5])) 
├─────────────────────────────────────┤
                                   5 
└─────────────────────────────────────┘

11 - BITMAP_MIN

Gets the minimum value in the bitmap.

Analyze Syntax

func.bitmap_min( <bitmap> )

Analyze Examples

func.bitmap_min(func.build_bitmap([1, 4, 5]))

┌───────────────────────────────────────────────┐
 func.bitmap_min(func.build_bitmap([1, 4, 5])) 
├───────────────────────────────────────────────┤
                                             1 
└───────────────────────────────────────────────┘

SQL Syntax

BITMAP_MIN( <bitmap> )

SQL Examples

SELECT BITMAP_MIN(BUILD_BITMAP([1,4,5]));

┌─────────────────────────────────────┐
 bitmap_min(build_bitmap([1, 4, 5])) 
├─────────────────────────────────────┤
                                   1 
└─────────────────────────────────────┘

12 - BITMAP_NOT

Generates a new bitmap with elements from the first bitmap that are not in the second one.

Analyze Syntax

func.bitmap_not( <bitmap1>, <bitmap2> )

Analyze Examples

func.bitmap_not(func.build_bitmap([1, 4, 5]), func.cast(func.build_bitmap([5, 6, 7])), Text)

┌───────────────────────────────────────────────────────────────────────────────────────────────┐
 func.bitmap_not(func.build_bitmap([1, 4, 5]), func.cast(func.build_bitmap([5, 6, 7])), Text)  
├───────────────────────────────────────────────────────────────────────────────────────────────┤
 1,4                                                                                           
└───────────────────────────────────────────────────────────────────────────────────────────────┘

SQL Syntax

BITMAP_NOT( <bitmap1>, <bitmap2> )

Aliases

SQL Examples

SELECT BITMAP_NOT(BUILD_BITMAP([1,4,5]), BUILD_BITMAP([5,6,7]))::String;

┌──────────────────────────────────────────────────────────────────────┐
 bitmap_not(build_bitmap([1, 4, 5]), build_bitmap([5, 6, 7]))::string 
├──────────────────────────────────────────────────────────────────────┤
 1,4                                                                  
└──────────────────────────────────────────────────────────────────────┘

SELECT BITMAP_AND_NOT(BUILD_BITMAP([1,4,5]), BUILD_BITMAP([5,6,7]))::String;

┌──────────────────────────────────────────────────────────────────────────┐
 bitmap_and_not(build_bitmap([1, 4, 5]), build_bitmap([5, 6, 7]))::string 
├──────────────────────────────────────────────────────────────────────────┤
 1,4                                                                      
└──────────────────────────────────────────────────────────────────────────┘

13 - BITMAP_NOT_COUNT

Counts the number of bits set to 0 in the bitmap by performing a logical NOT operation.

Analyze Syntax

func.bitmap_not_count( <bitmap> )

Analyze Examples

func.bitmap_not_count(func.to_bitmap('1, 3, 5'))

┌──────────────────────────────────────────────────┐
 func.bitmap_not_count(func.to_bitmap('1, 3, 5')) 
├──────────────────────────────────────────────────┤
                                                3 
└──────────────────────────────────────────────────┘

SQL Syntax

BITMAP_NOT_COUNT( <bitmap> )

SQL Examples

SELECT BITMAP_NOT_COUNT(TO_BITMAP('1, 3, 5'));

┌────────────────────────────────────────┐
 bitmap_not_count(to_bitmap('1, 3, 5')) 
├────────────────────────────────────────┤
                                      3 
└────────────────────────────────────────┘

14 - BITMAP_OR

Performs a bitwise OR operation on the two bitmaps.

Analyze Syntax

func.bitmap_or( <bitmap1>, <bitmap2> )

Analyze Examples

func.bitmap_or(func.build_bitmap([1, 4, 5]), func.build_bitmap([6, 7]))

┌─────────────────────────────────────────────────────────────────────────┐
 func.bitmap_or(func.build_bitmap([1, 4, 5]), func.build_bitmap([6, 7])) 
├─────────────────────────────────────────────────────────────────────────┤
 1,4,5,6,7                                                               
└─────────────────────────────────────────────────────────────────────────┘

SQL Syntax

BITMAP_OR( <bitmap1>, <bitmap2> )

SQL Examples

SELECT BITMAP_OR(BUILD_BITMAP([1,4,5]), BUILD_BITMAP([6,7]))::String;

┌──────────────────────────────────────────────────────────────────┐
 bitmap_or(build_bitmap([1, 4, 5]), build_bitmap([6, 7]))::string 
├──────────────────────────────────────────────────────────────────┤
 1,4,5,6,7                                                        
└──────────────────────────────────────────────────────────────────┘

15 - BITMAP_OR_COUNT

Counts the number of bits set to 1 in the bitmap by performing a logical OR operation.

Analyze Syntax

func.bitmap_or_count( <bitmap> )

Analyze Examples

func.bitmap_or_count(func.to_bitmap('1, 3, 5'))

┌─────────────────────────────────────────────────┐
 func.bitmap_or_count(func.to_bitmap('1, 3, 5')) 
├─────────────────────────────────────────────────┤
                                               3 
└─────────────────────────────────────────────────┘

SQL Syntax

BITMAP_OR_COUNT( <bitmap> )

SQL Examples

SELECT BITMAP_OR_COUNT(TO_BITMAP('1, 3, 5'));

┌───────────────────────────────────────┐
 bitmap_or_count(to_bitmap('1, 3, 5')) 
├───────────────────────────────────────┤
                                     3 
└───────────────────────────────────────┘

16 - BITMAP_SUBSET_IN_RANGE

Generates a sub-bitmap of the source bitmap within a specified range.

Analyze Syntax

func.bitmap_subset_in_range( <bitmap>, <start>, <end> )

Analyze Examples

func.bitmap_subset_in_range(func.build_bitmap([5, 7, 9]), 6, 9)

┌─────────────────────────────────────────────────────────────────┐
 func.bitmap_subset_in_range(func.build_bitmap([5, 7, 9]), 6, 9) 
├─────────────────────────────────────────────────────────────────┤
 7                                                               
└─────────────────────────────────────────────────────────────────┘

SQL Syntax

BITMAP_SUBSET_IN_RANGE( <bitmap>, <start>, <end> )

SQL Examples

SELECT BITMAP_SUBSET_IN_RANGE(BUILD_BITMAP([5,7,9]), 6, 9)::String;

┌───────────────────────────────────────────────────────────────┐
 bitmap_subset_in_range(build_bitmap([5, 7, 9]), 6, 9)::string 
├───────────────────────────────────────────────────────────────┤
 7                                                             
└───────────────────────────────────────────────────────────────┘

17 - BITMAP_SUBSET_LIMIT

Generates a sub-bitmap of the source bitmap, beginning with a range from the start value, with a size limit.

Analyze Syntax

func.bitmap_subset_limit( <bitmap>, <start>, <limit> )

Analyze Examples

func.bitmap_subset_limit(func.build_bitmap([1, 4, 5]), 2, 2)

┌──────────────────────────────────────────────────────────────┐
 func.bitmap_subset_limit(func.build_bitmap([1, 4, 5]), 2, 2) 
├──────────────────────────────────────────────────────────────┤
 4,5                                                          
└──────────────────────────────────────────────────────────────┘

SQL Syntax

BITMAP_SUBSET_LIMIT( <bitmap>, <start>, <limit> )

SQL Examples

SELECT BITMAP_SUBSET_LIMIT(BUILD_BITMAP([1,4,5]), 2, 2)::String;

┌────────────────────────────────────────────────────────────┐
 bitmap_subset_limit(build_bitmap([1, 4, 5]), 2, 2)::string 
├────────────────────────────────────────────────────────────┤
 4,5                                                        
└────────────────────────────────────────────────────────────┘

18 - BITMAP_UNION

Counts the number of bits set to 1 in the bitmap by performing a logical UNION operation.

Analyze Syntax

func.bitmap_union( <bitmap> )

Analyze Examples

func.bitmap_union(func.to_bitmap('1, 3, 5'))

┌──────────────────────────────────────────────┐
 func.bitmap_union(func.to_bitmap('1, 3, 5')) 
├──────────────────────────────────────────────┤
 1,3,5                                        
└──────────────────────────────────────────────┘

SQL Syntax

BITMAP_UNION( <bitmap> )

SQL Examples

SELECT BITMAP_UNION(TO_BITMAP('1, 3, 5'))::String;

┌────────────────────────────────────────────┐
 bitmap_union(to_bitmap('1, 3, 5'))::string 
├────────────────────────────────────────────┤
 1,3,5                                      
└────────────────────────────────────────────┘

19 - BITMAP_XOR

Performs a bitwise XOR (exclusive OR) operation on the two bitmaps.

Analyze Syntax

func.bitmap_xor( <bitmap1>, <bitmap2> )

Analyze Examples

func.bitmap_xor(func.build_bitmap([1, 4, 5]), func.build_bitmap([5, 6, 7]))

┌─────────────────────────────────────────────────────────────────────────────┐
 func.bitmap_xor(func.build_bitmap([1, 4, 5]), func.build_bitmap([5, 6, 7])) 
├─────────────────────────────────────────────────────────────────────────────┤
 1,4,6,7                                                                     
└─────────────────────────────────────────────────────────────────────────────┘

SQL Syntax

BITMAP_XOR( <bitmap1>, <bitmap2> )

SQL Examples

SELECT BITMAP_XOR(BUILD_BITMAP([1,4,5]), BUILD_BITMAP([5,6,7]))::String;

┌──────────────────────────────────────────────────────────────────────┐
 bitmap_xor(build_bitmap([1, 4, 5]), build_bitmap([5, 6, 7]))::string 
├──────────────────────────────────────────────────────────────────────┤
 1,4,6,7                                                              
└──────────────────────────────────────────────────────────────────────┘

20 - BITMAP_XOR_COUNT

Counts the number of bits set to 1 in the bitmap by performing a logical XOR (exclusive OR) operation.

Analyze Syntax

func.bitmap_xor_count( <bitmap> )

Analyze Examples

func.bitmap_xor_count(func.to_bitmap('1, 3, 5'))

┌──────────────────────────────────────────────────┐
 func.bitmap_xor_count(func.to_bitmap('1, 3, 5')) 
├──────────────────────────────────────────────────┤
                                                3 
└──────────────────────────────────────────────────┘

SQL Syntax

BITMAP_XOR_COUNT( <bitmap> )

SQL Examples

SELECT BITMAP_XOR_COUNT(TO_BITMAP('1, 3, 5'));

┌────────────────────────────────────────┐
 bitmap_xor_count(to_bitmap('1, 3, 5')) 
├────────────────────────────────────────┤
                                      3 
└────────────────────────────────────────┘

21 - INTERSECT_COUNT

Counts the number of intersecting bits between two bitmap columns.

Analyze Syntax

func.intersect_count(( '<bitmap1>', '<bitmap2>' ), ( <bitmap_column1>, <bitmap_column2> ))

Analyze Examples

# Given a dataset like this:

┌───────────────────────────────────────┐
        id        tag        v       
├─────────────────┼─────────────────────┤
               1    a    0, 1        
               3    b    0, 1, 2     
               2    c    1, 3, 4     
└───────────────────────────────────────┘

# This is produced
func.intersect_count(('b', 'c'), (v, tag))
┌──────────────────────────────────────────────────────────┐
        id        func.intersect_count('b', 'c')(v, tag) 
├─────────────────┼────────────────────────────────────────┤
               1                                       0 
               3                                       3 
               2                                       3 
└──────────────────────────────────────────────────────────┘

SQL Syntax

INTERSECT_COUNT( '<bitmap1>', '<bitmap2>' )( <bitmap_column1>, <bitmap_column2> )

SQL Examples

CREATE TABLE agg_bitmap_test(id Int, tag String, v Bitmap);

INSERT INTO
  agg_bitmap_test(id, tag, v)
VALUES
  (1, 'a', to_bitmap('0, 1')),
  (2, 'b', to_bitmap('0, 1, 2')),
  (3, 'c', to_bitmap('1, 3, 4'));

SELECT id, INTERSECT_COUNT('b', 'c')(v, tag) 
FROM agg_bitmap_test GROUP BY id;

┌─────────────────────────────────────────────────────┐
        id        intersect_count('b', 'c')(v, tag) 
├─────────────────┼───────────────────────────────────┤
               1                                  0 
               3                                  3 
               2                                  3 
└─────────────────────────────────────────────────────┘

22 - SUB_BITMAP

Generates a sub-bitmap of the source bitmap, beginning from the start index, with a specified size.

Analyze Syntax

func.sub_bitmap( <bitmap>, <start>, <size> )

Analyze Examples

func.sub_bitmap(func.build_bitmap([1, 2, 3, 4, 5]), 1, 3)

┌───────────────────────────────────────────────────────────┐
 func.sub_bitmap(func.build_bitmap([1, 2, 3, 4, 5]), 1, 3) 
├───────────────────────────────────────────────────────────┤
 2,3,4                                                     
└───────────────────────────────────────────────────────────┘

SQL Syntax

SUB_BITMAP( <bitmap>, <start>, <size> )

SQL Examples

SELECT SUB_BITMAP(BUILD_BITMAP([1, 2, 3, 4, 5]), 1, 3)::String;

┌─────────────────────────────────────────────────────────┐
 sub_bitmap(build_bitmap([1, 2, 3, 4, 5]), 1, 3)::string 
├─────────────────────────────────────────────────────────┤
 2,3,4                                                   
└─────────────────────────────────────────────────────────┘