3

The JSON_OBJECT function strangely returns different data types depending on the presence of an key index in the join-table in MariaDB 10.5.12.

I've been trying to get post comments in a json array. And noticed that the output of the aggregate function changes strangely.

While it always gives the same right results, the packaging varies. Sometimes it's a escaped string requiring additional json parsing, sometimes it's a ready-made json array.

Gradually I was able to get close to the cause. Here's a minimally working piece of a query that shows the bug.

SELECT 
    JSON_ARRAYAGG(DISTINCT JSON_OBJECT(
        'comment_id', comment.id, 
              'text', comment.text
    ) ORDER BY comment.id) AS comments
FROM post
LEFT JOIN comment ON comment.post_id = post.id
LEFT JOIN vote ON vote.user_id = 1 AND vote.post_id = post.id
GROUP BY post.id

The prerequisite is that the key index user_id exists in the vote table.

If the join occurs by the user_id field condition, the output is escaped string:

LEFT JOIN vote ON vote.user_id = 123 AND vote.post_id = post.id 

["{\"tag_id\": 1, \"name\": \"conubia\"}","{\"tag_id\": 21, \"name\": \"convallis\"}"]
["{\"tag_id\": 40, \"name\": \"amet\"}","{\"tag_id\": 41, \"name\": \"neque\"}"]

If the merge does not occur by the user_id field condition, the output is json array:

LEFT JOIN vote ON vote.user_id = -1 AND vote.post_id = post.id 

[{"tag_id": 1, "name": "conubia"},{"tag_id": 21, "name": "convallis"}]
[{"tag_id": 40, "name": "amet"},{"tag_id": 41, "name": "neque"}]

If there is no key index user_id, under all join conditions, the output is escaped string:

["{\"tag_id\": 1, \"name\": \"conubia\"}","{\"tag_id\": 21, \"name\": \"convallis\"}"]
["{\"tag_id\": 40, \"name\": \"amet\"}","{\"tag_id\": 41, \"name\": \"neque\"}"]

Vote DDL

CREATE TABLE `vote` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `user_id` INT(11) NULL DEFAULT NULL,
    `post_id` INT(11) NULL DEFAULT NULL,
    PRIMARY KEY (`id`) USING BTREE,
    INDEX `user_id` (`user_id`) USING BTREE
)
ENGINE=InnoDB
AUTO_INCREMENT=1

Question

Is it possible to prevent accidental changes to the function output by means of the database, without removing the index?

Assumption

I think I could use CAST, but it didn't work for me.

3
  • have you thought about reporting this, but to reproduce it youi must add table and data aswell as create table and INsert into
    – nbk
    Commented Nov 7, 2021 at 12:02
  • Yeah, I was thinking about that, too. But I'm still in the process of researching and wanted feedback from the community. And to start a post in case anyone in the future encounters such a problem.
    – vatavale
    Commented Nov 7, 2021 at 12:07
  • This problem is no longer present in version 10.6.5
    – vatavale
    Commented Jan 14, 2022 at 7:14

1 Answer 1

1

Looks like this was connected with the bugs in JSON escaping.

The problem has been fixed in the new releases. Tested in MariaDB 10.6.5.

1
  • 1
    Actually upgrading to MariaDB:10.6.5 didn't resolve my issue, but you pointed to the right solution. I upgraded to MariaDB:10.7.8 and it solved the issue. Many thanks. Commented Feb 21, 2023 at 21:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.