[MDEV-11338] bit(n) data type gets confused when used with IF(), IFNULL()... Created: 2016-11-23 Updated: 2022-11-29 |
|
| Status: | Stalled |
| Project: | MariaDB Server |
| Component/s: | N/A |
| Affects Version/s: | 10.1.19, 10.2.9 |
| Fix Version/s: | 10.1, 10.3 |
| Type: | Bug | Priority: | Major |
| Reporter: | Dean Trower | Assignee: | Alexander Barkov |
| Resolution: | Unresolved | Votes: | 0 |
| Labels: | bit, ifnull, upstream | ||
| Environment: |
Tested using Linux & phpMyAdmin / mysqli connector |
||
| Description |
|
Columns of
type should be treated consistently as numeric data. However, VERY strange results can be obtained if you use this datatype in an IFNULL(a,b) function or similar. Consider this:
The SQL below returns a 6-column result set. All 6 columns SHOULD contain the same NUMERIC data. However, column 2 returns a string of bits (1's and 0's) corresponding to the ASCII encoding of the result interpreted as a string! That is, if the correct result is 25, it actually returns either 11001000110101 (in phpMyAdmin) or 12853 (the decimal equivalent of that binary number, when using mysqli from PHP directly))! Note that: ASCII('2') = 50 = 110010 binary, and 12853 = 50*256+53 ...which is where those values come from. Needless to say, this is completely fubar! Also, using mysqli to retrieve the data yeilds a "Malformed packet" error after the first row containing a double-digit number. Also completely fubar! If you're writing SQL, a simple workaround is just to add a "+0" or a CAST to coerce the result into numeric form. However, if you've (for example) just converted a bool column to a bit(1) and expect things to work correctly as before, this bug may bite you! MySQL (tested v5.6 and v5.7) suffers a very similar (though possibly not quite identical) problem; see MySQL bug #83923. |
| Comments |
| Comment by Elena Stepanova [ 2016-11-27 ] | ||||||||||||||||||||||||||||
|
Thanks for the report. | ||||||||||||||||||||||||||||
| Comment by Dean Trower [ 2017-10-14 ] | ||||||||||||||||||||||||||||
|
Still happening in v10.2.9. | ||||||||||||||||||||||||||||
| Comment by Dean Trower [ 2019-07-10 ] | ||||||||||||||||||||||||||||
|
Still happening in v10.3.16 | ||||||||||||||||||||||||||||
| Comment by Alice Sherepa [ 2022-11-29 ] | ||||||||||||||||||||||||||||
|
currently on 10.3 4e9206736c403206915c0 -10.10 the result is:
| ||||||||||||||||||||||||||||
| Comment by Dean Trower [ 2022-11-29 ] | ||||||||||||||||||||||||||||
|
Alice, I'm now using v 10.3.37, and I'm still getting decimal numbers in the IFNULL(a,a) column that correspond to (ASCII encoding of result as a string) => (concatenated bits of that encoding) => (interpreted as decimal), so e.g. "12848" instead of "20". I get this both using the mysqli connector in PHP, and in cPanel's phpMyAdmin web interface. I also tested it and verified the same problem on version: 10.9.2-MariaDB-1:10.9.2+maria~deb11 The discrepancy seems to be because you used the command-line client to output the results directly, rather than a connector. |