Bug #113206 BIT_OR returns erroneous values
Submitted: 24 Nov 2023 3:57 Modified: 24 Nov 2023 7:11
Reporter: Pinhan Zhao Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server: DML Severity:S2 (Serious)
Version:8.0.27, 8.0.35 OS:Any
Assigned to: CPU Architecture:Any

[24 Nov 2023 3:57] Pinhan Zhao
Description:
When one of the argument values is -1, BIT_OR evaluates to 18446744073709551615, which seems problematic.

How to repeat:
Given a table defined as below
```
CREATE TABLE R (
  x INTEGER
);

INSERT INTO R VALUES (-1);
INSERT INTO R VALUES (1);
```

the output of `SELECT BIT_OR(x) FROM R` is

```
BIT_OR(x)
18446744073709551615
```
[24 Nov 2023 5:55] MySQL Verification Team
Hello Pinhan Zhao,

Thank you for the report and feedback.

regards,
Umesh
[24 Nov 2023 7:11] Roy Lyseng
Posted by developer:
 
Not a bug. From user manual:

Numeric evaluation occurs otherwise, with argument value conversion to unsigned 64-bit integers as necessary.

Note that there is no standard that covers the semantics of BIT_OR.
Besides, MySQL has an unsigned data type that must be handled.

If you need the result as a signed integer, wrap the expression as CAST(BIT_OR(x) AS SIGNED)