[MDEV-14438] JSON_OBJECT does not respect variable types INT or DECIMAL in stored procedures Created: 2017-11-18  Updated: 2023-11-28

Status: Confirmed
Project: MariaDB Server
Component/s: JSON
Affects Version/s: 10.2.10, 10.2, 10.3, 10.4, 10.5, 10.6, 10.9, 10.10, 10.11, 11.0, 11.1, 11.2
Fix Version/s: 10.4, 10.5, 10.6, 10.11, 11.0, 11.1, 11.2

Type: Bug Priority: Major
Reporter: Rich Theobald Assignee: Rucha Deodhar
Resolution: Unresolved Votes: 2
Labels: None
Environment:

Ubuntu 14.04



 Description   

Test case:

DROP PROCEDURE IF EXISTS test;
 
DELIMITER ;;
 
CREATE PROCEDURE test()
BEGIN
  DECLARE i INT UNSIGNED DEFAULT 10;
  DECLARE d DECIMAL(2,1) UNSIGNED DEFAULT 1.5;
  
  SELECT JSON_OBJECT('int_variable', i, 'int_literal', 20, 'decimal_variable', d, 'decimal_literal', 2.6 );
  
END;;
 
DELIMITER ;
CALL test;

Returns:

+---------------------------------------------------------------------------------------------------+
| JSON_OBJECT('int_variable', i, 'int_literal', 20, 'decimal_variable', d, 'decimal_literal', 2.6 ) |
+---------------------------------------------------------------------------------------------------+
| {"int_variable": "10", "int_literal": 20, "decimal_variable": "1.5", "decimal_literal": 2.6}      |
+---------------------------------------------------------------------------------------------------+



 Comments   
Comment by Rich Theobald [ 2017-11-18 ]

User defined variables work as expected:

SET @i=10, @d=1.5; SELECT JSON_OBJECT('int_variable', @i, 'int_literal', 20, 'decimal_variable', @d, 'decimal_literal', 2.6 );
Query OK, 0 rows affected (0.00 sec)
 
+-----------------------------------------------------------------------------------------------------+
| JSON_OBJECT('int_variable', @i, 'int_literal', 20, 'decimal_variable', @d, 'decimal_literal', 2.6 ) |
+-----------------------------------------------------------------------------------------------------+
| {"int_variable": 10, "int_literal": 20, "decimal_variable": 1.5, "decimal_literal": 2.6}            |
+-----------------------------------------------------------------------------------------------------+

Comment by Alice Sherepa [ 2017-11-20 ]

thanks for the report! reproducible on current MariaDB 10.2 and 10.3

Comment by Alice Sherepa [ 2019-06-17 ]

still repeatable on 10.2-10.4:

set names utf8;
DELIMITER @@;
CREATE PROCEDURE test()
BEGIN
  DECLARE d DECIMAL(2,1) UNSIGNED DEFAULT 1.5;
  SELECT JSON_OBJECT('decimal_variable', d, 'decimal_literal', 2.6); 
END@@
 
DELIMITER ;@@
CALL test;

10.2 2b660fb4c212973ba0

MariaDB [test]> CALL test;
+------------------------------------------------------------+
| JSON_OBJECT('decimal_variable', d, 'decimal_literal', 2.6) |
+------------------------------------------------------------+
| {"decimal_variable": "1.5", "decimal_literal": 2.6}        |
+------------------------------------------------------------+
1 row in set (0.001 sec)

Comment by M. M. [ 2023-07-26 ]

On MariaDB 10.4 (i.e. 10.4.31 which is the only one i've tested this behaviour on), there is a bit of a quirk with this bug.

Running

DELIMITER ;; 
CREATE OR REPLACE PROCEDURE test()
BEGIN
  DECLARE d DECIMAL(2,1) UNSIGNED DEFAULT 1.4;
  SELECT JSON_OBJECT('int_variable', @e, 'decimal_variable', d, 'int_literal', 20, 'decimal_literal', 2.6 );
END;;
DELIMITER ;
CALL test;

without having touched the @e session variable in that session returns the desired result:

+----------------------------------------------------------------------------------------------------+
| JSON_OBJECT('int_variable', @e, 'decimal_variable', d, 'int_literal', 20, 'decimal_literal', 2.6 ) |
+----------------------------------------------------------------------------------------------------+
| {"int_variable": null, "decimal_variable": 1.4, "int_literal": 20, "decimal_literal": 2.6}         |
+----------------------------------------------------------------------------------------------------+

However, if you then set @e = 1 (and even if you then set it back to null), you once again run into the bug:

+----------------------------------------------------------------------------------------------------+
| JSON_OBJECT('int_variable', @e, 'decimal_variable', d, 'int_literal', 20, 'decimal_literal', 2.6 ) |
+----------------------------------------------------------------------------------------------------+
| {"int_variable": null, "decimal_variable": "1.4", "int_literal": 20, "decimal_literal": 2.6}       |
+----------------------------------------------------------------------------------------------------+

I find this very strange.

Leaving out the session variable altogether and creating the stored procedure as

DELIMITER ;; 
CREATE OR REPLACE PROCEDURE test()
BEGIN
  DECLARE d DECIMAL(2,1) UNSIGNED DEFAULT 1.4;
  SELECT JSON_OBJECT('decimal_variable', d, 'int_literal', 20, 'decimal_literal', 2.6 );
END;;
DELIMITER ;
CALL test;

once again exhibits the original bug:

+--------------------------------------------------------------------------------+
| JSON_OBJECT('decimal_variable', d, 'int_literal', 20, 'decimal_literal', 2.6 ) |
+--------------------------------------------------------------------------------+
| {"decimal_variable": "1.4", "int_literal": 20, "decimal_literal": 2.6}         |
+--------------------------------------------------------------------------------+

Generated at Thu Feb 08 08:13:33 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.