[MDEV-32005] JSON functions do not work with BIT fields Created: 2023-08-24  Updated: 2023-11-28

Status: Confirmed
Project: MariaDB Server
Component/s: JSON
Affects Version/s: 10.6.15, 10.11.5
Fix Version/s: 10.6, 10.11, 11.0, 11.1

Type: Bug Priority: Minor
Reporter: Walter van der Geest Assignee: Rucha Deodhar
Resolution: Unresolved Votes: 0
Labels: None
Environment:

Ubuntu



 Description   

If a BIT(1) field is put into various JSON-functions, e.g. JSON_OBJECT, the charset of the JSON is inconsequent and it returns invalid JSON.

Example script:

CREATE TABLE t1(text_field VARCHAR(50), bool_field BIT(1) NOT NULL DEFAULT 0);
INSERT INTO t1(text_field, bool_field) VALUES ('Some Umlauts with TRUE: äöü', 1), ('Also some Umlauts, but with FALSE: äöü', 0);
SELECT JSON_OBJECT('text_field', text_field, 'bool_field', bool_field) FROM t1;

Output:

 
+--------------------------------------------------------------------------------+
| json_object('text_field', text_field, 'bool_field', bool_field)                |
+--------------------------------------------------------------------------------+
| {"text_field": "Some Umlauts with TRUE: ���", "bool_field": \u0001}               |
| {"text_field": "Also some Umlauts, but with FALSE: ���", "bool_field": \u0000}    |
+--------------------------------------------------------------------------------+

Workaround

SELECT JSON_OBJECT('text_field', text_field, 'bool_field', CASE WHEN bool_field THEN true ELSE false END) FROM t1;

Output:

+----------------------------------------------------------------------------------------------------+
| json_object('text_field', text_field, 'bool_field', CASE WHEN bool_field THEN true ELSE false END) |
+----------------------------------------------------------------------------------------------------+
| {"text_field": "Some Umlauts with TRUE: äöü", "bool_field": true}                                  |
| {"text_field": "Also some Umlauts, but with FALSE: äöü", "bool_field": false}                      |
+----------------------------------------------------------------------------------------------------+
2 rows in set (0.001 sec)



 Comments   
Comment by Sergei Golubchik [ 2023-09-01 ]

BIT(1) field isn't a boolean with a true/false value. It's either an integer or a string, depending on the context. The result is still incorrect, a correct result could be either {"bool_field": 1} or {"bool_field": "\u0001"}.

Generated at Thu Feb 08 10:28:05 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.