Bitwise and bitmap aggregations
Perform bitwise operations and bitmap aggregations.
ClickHouse function reference
groupBitAnd
Applies bitwise AND operation to a series of numbers.
Syntax:
Arguments:
expr
(UInt*
orInt*
): The expression to aggregate.
Returns:
- The result of bitwise AND operation on all input values, with the same data type as the input.
Example:
Result:
In this example, assuming taco_code
represents ingredients as bit flags:
The result 4
(binary 00000100
) indicates that only the third bit is common across all taco recipes, which might represent a shared ingredient like “tortilla”.
This function is useful for finding common elements or properties across multiple items when those properties are represented as bit flags.
groupBitmap
Calculates the bitmap from an unsigned integer column and returns the cardinality (number of unique elements).
Syntax:
Arguments:
expr
(UInt*
): An expression that results in an unsigned integer type.
Returns:
- The cardinality of the bitmap as a
UInt64
value. - If the
-State
suffix is added (e.g.,groupBitmapState
), it will return a bitmap object instead of the cardinality.
Example:
Result:
In this example, groupBitmap
calculates the number of unique taco IDs in the taco_orders
rows. The result shows that there are 5 unique types of tacos ordered.
groupBitmapAnd
Calculates the bitwise AND of a bitmap column and returns the cardinality of the result.
Syntax:
Arguments:
expr
(AggregateFunction(groupBitmap, UInt*)
): An expression that results inAggregateFunction(groupBitmap, UInt*)
type.
Returns:
- The cardinality of the resulting bitmap as
UInt64
. - If the
-State
suffix is added (e.g.,groupBitmapAndState
), it returns a bitmap object.
Example:
Result:
In this example:
- Each taco’s ingredients are represented as a bitmap.
groupBitmapAnd
calculates the number of ingredients common to all tacos.groupBitmapAndState
combined withbitmapToArray
andarraySort
shows the actual common ingredients.
The groupBitmapAnd
function is useful for finding intersections in sets represented as bitmaps, which can be more efficient than using arrays for large datasets.
groupBitmapOr
Calculates the bitwise OR of a bitmap column and returns the cardinality of the result.
Syntax:
Arguments:
expr
(AggregateFunction(groupBitmap, UInt*)
): An expression that results inAggregateFunction(groupBitmap, UInt*)
type.
Returns:
- The cardinality of the resulting bitmap as
UInt64
. - If the
-State
suffix is added (e.g.,groupBitmapOrState
), it returns a bitmap object. Example:
Result:
In this example, groupBitmapOr
calculates the total number of unique taco IDs across all customers and groupBitmapOrState
returns the actual bitmap state.
groupBitmapOr
is equivalent to groupBitmapMerge
in functionality.
groupBitmapXor
Calculates the XOR (exclusive OR) of a bitmap column and returns the cardinality of the result.
Syntax:
Arguments:
expr
(AggregateFunction(groupBitmap, UInt*)
): An expression that results inAggregateFunction(groupBitmap, UInt*)
type.
Returns:
- The cardinality of the XOR result as
UInt64
.
Example:
unique_taco_preferences | xor_taco_preferences |
---|---|
5 | [2,5,6,8,9] |
Arguments:
expr
(UInt*
orInt*
): An expression that results inUInt*
orInt*
type.
Returns:
The result of bitwise OR operation, with the same type as the input (UInt*
or Int*
).
Example:
Let’s say we have a table taco_orders
with a column spice_level
representing the spiciness of each taco order in binary format:
Result:
In this example:
- Each
spice_level
represents different combinations of spices (each bit could represent a specific spice). groupBitOr
combines all spice levels, resulting in a value that represents all spices used across all orders.- The result
125
(binary1111101
) indicates that almost all spices were used in at least one order, except for the second least significant bit.
This function can be useful for combining flags or properties represented as bit fields across multiple rows.
groupBitXor
Applies bitwise XOR operation to a series of numbers.
Syntax:
Arguments:
expr
(UInt*
orInt*
): An expression that results inUInt*
orInt*
type.
Returns:
The result of bitwise XOR operation. Type is the same as the input (UInt*
or Int*
).
Example:
Let’s consider a table taco_sales
with a column daily_sales
representing daily taco sales encoded as bit flags:
Result:
In this example:
- The
groupBitXor
function performs a bitwise XOR across all these values. - The result 104 (01101000 in binary) represents the combined sales flags for the week.
This function can be useful for combining bit flags or performing certain types of data analysis where XOR operations are relevant.
Was this page helpful?