Details
-
Bug
-
Status: Open (View Workflow)
-
Major
-
Resolution: Unresolved
-
10.11, 11.4, 11.8, 12.3, 13.0
-
None
-
None
Description
JSON_OBJECT is documented to raise an error when a key name is NULL. It
does not: the key becomes the empty string, silently.
SELECT JSON_OBJECT(NULL, 1) AS v; |
v
|
{"": 1} |
No error, no warning, no note. The other half of the same documented
sentence is implemented, so the sentence is not dead text:
SELECT JSON_OBJECT('a'); |
ERROR 42000: Incorrect parameter count in the call to native |
function 'JSON_OBJECT' |
How to repeat
SELECT JSON_OBJECT(NULL, 1) AS literal_null; |
 |
CREATE TABLE tk (k VARCHAR(10), v INT); |
INSERT INTO tk VALUES ('a',1),(NULL,2); |
SELECT JSON_OBJECT(k, v) AS from_a_column FROM tk; |
from_a_column
|
{"a": 1} |
{"": 2} |
DROP TABLE tk; |
Root cause
The key argument is written into the object without being tested for
NULL, so `val_str()` returning NULL is composed as a zero-length key.
Contract, `scripts/fill_help_tables.sql:451` (topic 377): *"An error
will occur if there are an odd number of arguments, or any key name is
NULL."*
Why it matters
A row's identifying value is silently replaced by the empty string, and
two rows with NULL keys collide on one key name inside the same object,
so one of them is lost.
Affected versions
JSON_OBJECT was added in 10.2.3.
Verified on 10.11 only (10.11.19-MariaDB-debug, commit 1dab253482d).
Other branches were not tested.
Note
Found while auditing the JSON functions against their documented
contracts. Pinned as the deviation:
func_json_invalid.result:396-398 records JSON_OBJECT(NULL, 1) ->
. Nothing anywhere uses --error for it.
Attachments
Issue Links
- split from
-
MDEV-40642 Project "JSON Phoenix" (placeholder)
-
- Open
-