Bug #119154 Incorrect BIT_COUNT value for TEXT column
Submitted: 14 Oct 2025 13:24 Modified: 13 Jan 10:22
Reporter: John Jove Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Data Types Severity:S1 (Critical)
Version:8.4.6 OS:Any
Assigned to: CPU Architecture:Any

[14 Oct 2025 13:24] John Jove
Description:
I run the following statements in case 1, where an incorrect result is returned.
The possible root cause is that when internally casting 14954857449029110191 to an unsigned number, an overflowed value is returned. 
However, the 14954857449029110191 value is able to store into the BIGINT UNSIGNED column, thus such overflow should not happen. In the PoC case, a correct value is returned.

How to repeat:
-- case 1
CREATE TABLE t0 (c1 TEXT);
INSERT INTO t0 VALUES (14954857449029110191);
SELECT BIT_COUNT(c1) FROM t0; -- actual:{63}, expected:{37}

-- PoC case
CREATE TABLE t0 (c1 BIGINT UNSIGNED);
INSERT INTO t0 VALUES (14954857449029110191);
SELECT BIT_COUNT(c1) FROM t0; -- 37
[13 Jan 10:00] Øystein Grøvlen
Thank you for your bug report.
I do not think this is a bug.

When you are storing an integer in a TEXT column it is converted to a text string.
Hence, your bit_count expression will count the bits of the corresponding ASCII values.
In order to count the bits of the original integer, you will need to convert the text to an integer:

> select bit_count(cast(c1 as unsigned))  from t0;
+---------------------------------+
| bit_count(cast(c1 as unsigned)) |
+---------------------------------+
|                              37 |
+---------------------------------+
[13 Jan 10:22] Øystein Grøvlen
On second thoughts, it seems that for smaller numbers the behavior is as expected by the filer of this bug:

mysql [localhost:9500] {msandbox} (test) > CREATE TABLE t0 (c1 TEXT);
Query OK, 0 rows affected (0.003 sec)

mysql [localhost:9500] {msandbox} (test) > INSERT INTO t0 VALUES (1), (3), (255);
Query OK, 3 rows affected (0.001 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql [localhost:9500] {msandbox} (test) > SELECT BIT_COUNT(c1) FROM t0;
+---------------+
| BIT_COUNT(c1) |
+---------------+
|             1 |
|             2 |
|             8 |
+---------------+
3 rows in set (0.000 sec)

Verified as described.