Skip to content

Conversation

@sim1984
Copy link
Contributor

@sim1984 sim1984 commented Oct 9, 2025

The BIN_AND_AGG, BIN_OR_AGG, and BIN_XOR_AGG aggregate functions perform bit operations.

NULLs are ignored. It's returned only in the case of none evaluated records having a non-null value.

The input argument must be one of the integer types (SMALLINT, INTEGER, BIGINT, or INT128).
The output result is of the same type as the input argument.

Syntax:

<bin_add agg> ::= BIN_AND_AGG(<expression>)

<bin_or agg> ::= BIN_OR_AGG(<expression>)

<bin_xor agg> ::= BIN_XOR_AGG([ALL | DISTINCT] <expression>)

The BIN_AND_AGG and BIN_OR_AGG functions do not support the DISTINCT keyword, since eliminating duplicates does
not affect the result. However, for BIN_XOR_AGG, you can specify DISTINCT to exclude duplicates from processing.

Example:

SELECT
  name,
  BIN_AND_AGG(n) AS F_AND,
  BIN_OR_AGG(n) AS F_OR,
  BIN_XOR_AGG(n) AS F_XOR,
  BIN_XOR_AGG(ALL n) AS F_XOR_A,
  BIN_XOR_AGG(DISTINCT n) AS F_XOR_D
FROM acl_masks
GROUP BY name

@sim1984
Copy link
Contributor Author

sim1984 commented Oct 9, 2025

This is related to #8175

In MySQL and PostgreSQL, these functions are called BIT_AND, BIT_OR, and BIT_XOR. But since we already have non-aggregate functions like BIN_AND, BIN_OR, and BIN_XOR, I decided to keep the names consistent.

<bin_or agg> ::= BIN_OR_AGG(<expression>)
<bin_xor agg> ::= BIN_XOR_AGG([ALL | DISTINCT] <expression>)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is a real use case for BIN_XOR_AGG?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, BIN_AND_AGG and BIN_OR_AGG would have been enough for me. But since MySQL and PostgreSQL have BIT_XOR, I decided to add a similar function to Firebird.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aggregate XOR could be used for some trivial hashing (dangerous but fast). Or (probably more useful) for combining multiple already calculated hashes -- e.g. every row is hashed independently and then row hashes are aggregated using XOR.

@sim1984
Copy link
Contributor Author

sim1984 commented Oct 10, 2025

Thanks for the review. I'll fix it.

@sim1984 sim1984 requested a review from asfernandes October 10, 2025 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BIN_AND_AGG, BIN_OR_AGG, and BIN_XOR_AGG (BIN_AND, BIN_OR and BIN_XOR as aggregate functions)

5 participants