

# 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
<a name="athena-duckdb-functions-aggregate-bit-and"></a>

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](#athena-duckdb-functions-aggregate-bit-or) or the [`bit_xor` function](#athena-duckdb-functions-aggregate-bit-xor). For `BOOLEAN` values, use the [`bool_and` function](#athena-duckdb-functions-aggregate-bool-and).

### Syntax
<a name="athena-duckdb-functions-aggregate-bit-and-syntax"></a>

```sql
bit_and({{arg}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-bit-and-arguments"></a>

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
<a name="athena-duckdb-functions-aggregate-bit-and-return-type"></a>

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

### Examples
<a name="athena-duckdb-functions-aggregate-bit-and-examples"></a>

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

```sql
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`.

```sql
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
<a name="athena-duckdb-functions-aggregate-bit-and-considerations"></a>

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
<a name="athena-duckdb-functions-aggregate-bit-and-differences-from-engine-version-3"></a>

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
<a name="athena-duckdb-functions-aggregate-bit-or"></a>

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](#athena-duckdb-functions-aggregate-bool-or).

### Syntax
<a name="athena-duckdb-functions-aggregate-bit-or-syntax"></a>

```sql
bit_or({{arg}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-bit-or-arguments"></a>

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
<a name="athena-duckdb-functions-aggregate-bit-or-return-type"></a>

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

### Examples
<a name="athena-duckdb-functions-aggregate-bit-or-examples"></a>

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

```sql
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.

```sql
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
<a name="athena-duckdb-functions-aggregate-bit-or-considerations"></a>

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
<a name="athena-duckdb-functions-aggregate-bit-or-differences-from-engine-version-3"></a>

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
<a name="athena-duckdb-functions-aggregate-bit-xor"></a>

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
<a name="athena-duckdb-functions-aggregate-bit-xor-syntax"></a>

```sql
bit_xor({{arg}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-bit-xor-arguments"></a>

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
<a name="athena-duckdb-functions-aggregate-bit-xor-return-type"></a>

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

### Examples
<a name="athena-duckdb-functions-aggregate-bit-xor-examples"></a>

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

```sql
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`.

```sql
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
<a name="athena-duckdb-functions-aggregate-bit-xor-considerations"></a>

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
<a name="athena-duckdb-functions-aggregate-bit-xor-differences-from-engine-version-3"></a>

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
<a name="athena-duckdb-functions-aggregate-bitstring-agg"></a>

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
<a name="athena-duckdb-functions-aggregate-bitstring-agg-syntax"></a>

```sql
bitstring_agg({{arg}} [, {{min}}, {{max}} ])
```

### Arguments
<a name="athena-duckdb-functions-aggregate-bitstring-agg-arguments"></a>

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
<a name="athena-duckdb-functions-aggregate-bitstring-agg-return-type"></a>

`BIT`

### Examples
<a name="athena-duckdb-functions-aggregate-bitstring-agg-examples"></a>

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

```sql
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.

```sql
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
<a name="athena-duckdb-functions-aggregate-bitstring-agg-considerations"></a>

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
<a name="athena-duckdb-functions-aggregate-bool-and"></a>

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](#athena-duckdb-functions-aggregate-bool-or).

### Syntax
<a name="athena-duckdb-functions-aggregate-bool-and-syntax"></a>

```sql
bool_and({{arg}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-bool-and-arguments"></a>

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
<a name="athena-duckdb-functions-aggregate-bool-and-return-type"></a>

`BOOLEAN`

### Examples
<a name="athena-duckdb-functions-aggregate-bool-and-examples"></a>

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

```sql
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.

```sql
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`.

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

The query returns the following result.


| result | 
| --- | 
| `true` | 

### Considerations
<a name="athena-duckdb-functions-aggregate-bool-and-considerations"></a>

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
<a name="athena-duckdb-functions-aggregate-bool-and-differences-from-engine-version-3"></a>

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
<a name="athena-duckdb-functions-aggregate-bool-or"></a>

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](#athena-duckdb-functions-aggregate-bool-and).

### Syntax
<a name="athena-duckdb-functions-aggregate-bool-or-syntax"></a>

```sql
bool_or({{arg}})
```

### Arguments
<a name="athena-duckdb-functions-aggregate-bool-or-arguments"></a>

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
<a name="athena-duckdb-functions-aggregate-bool-or-return-type"></a>

`BOOLEAN`

### Examples
<a name="athena-duckdb-functions-aggregate-bool-or-examples"></a>

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

```sql
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.

```sql
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`.

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

The query returns the following result.


| result | 
| --- | 
| `false` | 

### Considerations
<a name="athena-duckdb-functions-aggregate-bool-or-considerations"></a>

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)`.