Calculates the aggregate bitwise XOR of integer input values across a group.
Syntax
BIGINT BITWISE_XOR_AGG([DISTINCT] BIGINT|INT|SMALLINT|TINYINT value)
Parameters
| Parameter | Required | Description |
|---|---|---|
DISTINCT |
Optional | Deduplicates input values before aggregation. Each distinct value participates in the XOR calculation only once. |
value |
Required | An integer expression of type BIGINT, INT, SMALLINT, or TINYINT. NULL values are excluded from the calculation. |
Return value
Returns a BIGINT value representing the bitwise XOR of all non-NULL input values.
-
If the value is NULL, NULL is returned.
-
If
valueis not one of the supported types (BIGINT, INT, SMALLINT, or TINYINT), an error is returned.
Examples
Example 1: Basic GROUP BY
SELECT id, BITWISE_XOR_AGG(v)
FROM VALUES (1L, 2L), (1L, 1L), (2L, NULL), (1L, NULL) t(id, v)
GROUP BY id;
Result:
| id | _c1 |
|---|---|
| 1 | 3 |
| 2 | NULL |
For id=1, the non-NULL values are 2 and 1. 2 XOR 1 = 3. For id=2, all values are NULL, so the result is NULL.
Example 2: With DISTINCT
SELECT id, BITWISE_XOR_AGG(DISTINCT v)
FROM VALUES (1L, 2L), (1L, 1L), (1L, 1L), (2L, NULL), (1L, NULL) t(id, v)
GROUP BY id;
Result:
| id | _c1 |
|---|---|
| 1 | 3 |
| 2 | NULL |
For id=1, the input contains two occurrences of 1L. DISTINCT reduces these to one, leaving distinct values 2 and 1. 2 XOR 1 = 3.