Bug #106189 json_object(x,F()) returns NULL if F returns JSON and returns null
Submitted: 17 Jan 2022 22:50 Modified: 18 Jan 2022 5:55
Reporter: Dave Pullin (Basic Quality Contributor) Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: JSON Severity:S3 (Non-critical)
Version:5.7.31 OS:CentOS
Assigned to: CPU Architecture:Any

[17 Jan 2022 22:50] Dave Pullin
Description:
json_object("a",f())  wrongly returns NULL if f is a function that 
 is declared as 'returns JSON' and returns null

json_object("a",null) correctly returns {"a": null} 

json_object("a",f()) correctly returns {"a": null} if f is a function that 
 is declared as 'returns TEXT' and returns null

I think this is the root cause of https://bugs.mysql.com/?id=106180

mysql> create database if not exists bug;
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> 
mysql> drop function if exists bug.function_that_returns_null;
Query OK, 0 rows affected (0.00 sec)

mysql> drop function if exists bug.function_that_returns_null_json;
Query OK, 0 rows affected (0.00 sec)

mysql> 
mysql> create function bug.function_that_returns_null () returns text return null;
Query OK, 0 rows affected (0.00 sec)

mysql> create function bug.function_that_returns_null_json () returns json return null;
Query OK, 0 rows affected (0.00 sec)

mysql> 
mysql> select json_object("a",null) as constant_null, 
    ->        json_object("a",bug.function_that_returns_null()) as function_that_returns_null,
    ->        json_object("a",bug.function_that_returns_null_json()) as function_that_returns_null_json;
+---------------+----------------------------+---------------------------------+
| constant_null | function_that_returns_null | function_that_returns_null_json |
+---------------+----------------------------+---------------------------------+
| {"a": null}   | {"a": null}                | NULL                            |
+---------------+----------------------------+---------------------------------+
1 row in set (0.00 sec)

mysql> 

How to repeat:
create database if not exists bug;

drop function if exists bug.function_that_returns_null;
drop function if exists bug.function_that_returns_null_json;

create function bug.function_that_returns_null () returns text return null;
create function bug.function_that_returns_null_json () returns json return null;

select json_object("a",null) as constant_null, 
       json_object("a",bug.function_that_returns_null()) as function_that_returns_null,
       json_object("a",bug.function_that_returns_null_json()) as function_that_returns_null_json;
[18 Jan 2022 5:55] MySQL Verification Team
Hello Dave,

Thank you for the report and test case.
Verified as described.

regards,
Umesh