View a markdown version of this page

Boolean and bitwise aggregate functions - Amazon Athena

Boolean and bitwise aggregate functions

Use these functions to test whether a condition holds for every row or for at least one row of a group. Other functions combine the values with bitwise AND, OR, or XOR, or mark which integers in a range occur in the group.

bit_and function

Combines the non-NULL values of arg with bitwise AND, so each bit of the result is 1 only if that bit is 1 in every value. NULL values are skipped. If every value is NULL, or if there are no input rows, the result is NULL.

arg can be an integer or a BIT string. To combine bits with OR or XOR, use the bit_or function or the bit_xor function. For BOOLEAN values, use the bool_and function.

Syntax

bit_and(arg)

Arguments

The bit_and function takes the following argument:

arg

The values to combine with bitwise AND, as a signed or unsigned integer, such as INTEGER, BIGINT, or UTINYINT, or a BIT string. DECIMAL, DOUBLE, and BOOLEAN values are not accepted.

Return type

The type of arg, such as INTEGER for INTEGER input or BIT for BIT input.

Examples

The following SQL query combines 12 (binary 1100) and 10 (binary 1010). Only the bit for 8 is set in both, so the result is 8.

SELECT bit_and(x) AS result FROM (VALUES (12), (10)) AS v(x)

The query returns the following result.

result

8

The following SQL query combines two BIT strings of the same length. Only the leftmost bit is 1 in both strings, so the result is 1000.

SELECT bit_and(b) AS result FROM (VALUES (CAST('1100' AS BIT)), (CAST('1010' AS BIT)) ) AS v(b)

The query returns the following result.

result

1000

Considerations

When arg is a BIT string, every value must have the same number of bits. Otherwise the query fails.

Differences from Athena engine version 3

Athena engine version 3 names this function bitwise_and_agg, and DuckDB has no bitwise_and_agg function. Replace bitwise_and_agg(x) with bit_and(x).

bit_or function

Combines the non-NULL values of arg with bitwise OR, so each bit of the result is 1 if that bit is 1 in at least one value. NULL values are skipped. If every value is NULL, or if there are no input rows, the result is NULL.

arg can be an integer or a BIT string. A common use is to combine flag values, where each bit stands for one option. For BOOLEAN values, use the bool_or function.

Syntax

bit_or(arg)

Arguments

The bit_or function takes the following argument:

arg

The values to combine with bitwise OR, as a signed or unsigned integer, such as INTEGER, BIGINT, or UTINYINT, or a BIT string. DECIMAL, DOUBLE, and BOOLEAN values are not accepted.

Return type

The type of arg, such as INTEGER for INTEGER input or BIT for BIT input.

Examples

The following SQL query combines 12 (binary 1100) and 10 (binary 1010). The result, 14, is binary 1110.

SELECT bit_or(x) AS result FROM (VALUES (12), (10)) AS v(x)

The query returns the following result.

result

14

The following SQL query combines permission flags for each user, where 1 is read, 2 is write, and 4 is delete. For bob, the flag 2 occurs twice and still sets only one bit.

SELECT user_name, bit_or(permission) AS permissions FROM (VALUES ('alice', 1), ('alice', 4), ('bob', 2), ('bob', 2) ) AS v(user_name, permission) GROUP BY user_name ORDER BY user_name

The query returns the following results.

user_name permissions

alice

5

bob

2

Considerations

When arg is a BIT string, every value must have the same number of bits. Otherwise the query fails.

Differences from Athena engine version 3

Athena engine version 3 names this function bitwise_or_agg, and DuckDB has no bitwise_or_agg function. Replace bitwise_or_agg(x) with bit_or(x).

bit_xor function

Combines the non-NULL values of arg with bitwise exclusive OR, so each bit of the result is 1 if that bit is 1 in an odd number of values. NULL values are skipped. If every value is NULL, or if there are no input rows, the result is NULL.

Because a value combined with itself gives 0, duplicate values cancel each other out. To combine each distinct value once, use bit_xor(DISTINCT arg).

Syntax

bit_xor(arg)

Arguments

The bit_xor function takes the following argument:

arg

The values to combine with bitwise exclusive OR, as a signed or unsigned integer, such as INTEGER, BIGINT, or UTINYINT, or a BIT string. DECIMAL, DOUBLE, and BOOLEAN values are not accepted.

Return type

The type of arg, such as INTEGER for INTEGER input or BIT for BIT input.

Examples

The following SQL query combines 12 (binary 1100) and 10 (binary 1010). The bits that differ give 6, binary 0110.

SELECT bit_xor(x) AS result FROM (VALUES (12), (10)) AS v(x)

The query returns the following result.

result

6

The following SQL query shows that duplicates cancel out. The two 5 values cancel, so the result over all rows is 3. With DISTINCT, the function combines 5 and 3 once each, which gives 6.

SELECT bit_xor(x) AS all_values, bit_xor(DISTINCT x) AS distinct_values FROM (VALUES (5), (5), (3)) AS v(x)

The query returns the following result.

all_values distinct_values

3

6

Considerations

When arg is a BIT string, every value must have the same number of bits. Otherwise the query fails.

Differences from Athena engine version 3

Athena engine version 3 has no aggregate function for a bitwise XOR, so bit_xor has no Athena engine version 3 counterpart.

bitstring_agg function

Returns a BIT string with one bit for each integer from min to max. A bit is 1 when its integer occurs in arg at least once, and 0 otherwise. The leftmost bit stands for min, so the result has max - min + 1 bits.

NULL values are skipped. If every value is NULL, or if there are no input rows, the result is NULL. To count the distinct values that the result marks, pass it to bit_count.

Syntax

bitstring_agg(arg [, min, max ])

Arguments

The bitstring_agg function takes the following arguments:

arg

The integers to mark in the result, of a signed or unsigned integer type, such as INTEGER or BIGINT.

min

The smallest value that the result covers, as a constant of the same integer type as arg. This argument is optional.

max

The largest value that the result covers, as a constant of the same integer type as arg. It must not be smaller than min. This argument is optional.

Return type

BIT

Examples

The following SQL query marks the values 1, 4, and 7 in a 9-bit string for the range from 1 to 9.

SELECT bitstring_agg(x, 1, 9) AS result FROM (VALUES (1), (4), (7)) AS v(x)

The query returns the following result.

result

100100100

The following SQL query shows that bitstring_agg skips the NULL and sets one bit for the repeated 2. It then uses bit_count to count the distinct values.

SELECT bitstring_agg(x, 1, 5) AS bits, bit_count(bitstring_agg(x, 1, 5)) AS distinct_values FROM (VALUES (2), (2), (5), (NULL)) AS v(x)

The query returns the following result.

bits distinct_values

01001

2

Considerations

Always pass min and max. Without them, the engine must work out the range of arg on its own, and the query can fail. For example, it can fail on inline VALUES, on a computed expression, or on a table column.

If a value of arg is outside the range from min to max, the query fails. min and max must be constants.

bool_and function

Returns true if every non-NULL value of arg is true, and false if at least one value is false. NULL values are skipped. If every value is NULL, or if there are no input rows, the result is NULL.

To test whether a condition holds for every row, pass the condition, as in bool_and(amount > 50). To test whether it holds for at least one row, use the bool_or function.

Syntax

bool_and(arg)

Arguments

The bool_and function takes the following argument:

arg

The values to test, as a BOOLEAN value or condition. Numbers and strings are not accepted.

Return type

BOOLEAN

Examples

The following SQL query returns false because one of the values is false.

SELECT bool_and(x) AS result FROM (VALUES (true), (false), (true)) AS v(x)

The query returns the following result.

result

false

The following SQL query tests, for each region, whether every order is over 50.

SELECT region, bool_and(amount > 50) AS all_over_50 FROM (VALUES ('east', 120.50), ('east', 42.25), ('west', 75.00), ('west', 60.00) ) AS v(region, amount) GROUP BY region ORDER BY region

The query returns the following results.

region all_over_50

east

false

west

true

The following SQL query shows that a NULL value is skipped, so the result is true.

SELECT bool_and(x) AS result FROM (VALUES (true), (NULL)) AS v(x)

The query returns the following result.

result

true

Considerations

With no input rows, bool_and returns NULL, not true. To return true when there are no input rows, use coalesce(bool_and(arg), true).

Differences from Athena engine version 3

Athena engine version 3 also has every, which returns the same result as bool_and. DuckDB has no every function, so replace every(x) with bool_and(x).

bool_or function

Returns true if at least one non-NULL value of arg is true, and false if every non-NULL value is false. NULL values are skipped. If every value is NULL, or if there are no input rows, the result is NULL.

To test whether a condition holds for at least one row, pass the condition, as in bool_or(amount > 100). To test whether it holds for every row, use the bool_and function.

Syntax

bool_or(arg)

Arguments

The bool_or function takes the following argument:

arg

The values to test, as a BOOLEAN value or condition. Numbers and strings are not accepted.

Return type

BOOLEAN

Examples

The following SQL query returns true because at least one of the values is true.

SELECT bool_or(x) AS result FROM (VALUES (true), (false), (true)) AS v(x)

The query returns the following result.

result

true

The following SQL query tests, for each region, whether any order is over 100.

SELECT region, bool_or(amount > 100) AS has_large_order FROM (VALUES ('east', 120.50), ('east', 42.25), ('west', 75.00), ('west', 60.00) ) AS v(region, amount) GROUP BY region ORDER BY region

The query returns the following results.

region has_large_order

east

true

west

false

The following SQL query shows that a NULL value is skipped, so the result is false, not NULL.

SELECT bool_or(x) AS result FROM (VALUES (false), (NULL)) AS v(x)

The query returns the following result.

result

false

Considerations

With no input rows, bool_or returns NULL, not false. To return false when there are no input rows, use coalesce(bool_or(arg), false).