diff --git a/mysql-test/main/func_json.result b/mysql-test/main/func_json.result index 14ab6f41bce..0c242a886b7 100644 --- a/mysql-test/main/func_json.result +++ b/mysql-test/main/func_json.result @@ -1366,5 +1366,21 @@ JSON_OBJECTAGG(a, e) DROP VIEW v; DROP TABLE t1; # +# MDEV-23004 When using GROUP BY with JSON_ARRAYAGG with joint table, the square brackets are not included. +# +CREATE TABLE t1(id int primary key, name varchar(50)); +CREATE TABLE t2(id int, owner_id int); +INSERT INTO t1 VALUES (1, "name1"), (2, "name2"), (3, "name3"); +INSERT INTO t2 VALUES (1, 1), (2, 1), (3, 2), (4, 3); +SELECT t1.id, JSON_ARRAYAGG(JSON_OBJECT('id',t2.id)) as materials +from t1 LEFT JOIN t2 on t1.id = t2.owner_id +GROUP BY t1.id ORDER BY id; +id materials +1 ["{\"id\": 1}","{\"id\": 2}"] +2 ["{\"id\": 3}"] +3 ["{\"id\": 4}"] +DROP TABLE t1; +DROP TABLE t2; +# # End of 10.5 tests # diff --git a/mysql-test/main/func_json.test b/mysql-test/main/func_json.test index a77f7d8bd5b..f08a72a5bce 100644 --- a/mysql-test/main/func_json.test +++ b/mysql-test/main/func_json.test @@ -856,6 +856,23 @@ SELECT * FROM v; DROP VIEW v; DROP TABLE t1; +--echo # +--echo # MDEV-23004 When using GROUP BY with JSON_ARRAYAGG with joint table, the square brackets are not included. +--echo # + +CREATE TABLE t1(id int primary key, name varchar(50)); +CREATE TABLE t2(id int, owner_id int); + +INSERT INTO t1 VALUES (1, "name1"), (2, "name2"), (3, "name3"); +INSERT INTO t2 VALUES (1, 1), (2, 1), (3, 2), (4, 3); + +SELECT t1.id, JSON_ARRAYAGG(JSON_OBJECT('id',t2.id)) as materials + from t1 LEFT JOIN t2 on t1.id = t2.owner_id + GROUP BY t1.id ORDER BY id; + +DROP TABLE t1; +DROP TABLE t2; + --echo # --echo # End of 10.5 tests --echo # diff --git a/sql/item_jsonfunc.cc b/sql/item_jsonfunc.cc index 220a3e8de92..cf00da79de9 100644 --- a/sql/item_jsonfunc.cc +++ b/sql/item_jsonfunc.cc @@ -3743,6 +3743,12 @@ void Item_func_json_arrayagg::cut_max_length(String *result, } +Item *Item_func_json_arrayagg::copy_or_same(THD* thd) +{ + return new (thd->mem_root) Item_func_json_arrayagg(thd, this); +} + + String* Item_func_json_arrayagg::val_str(String *str) { if ((str= Item_func_group_concat::val_str(str))) diff --git a/sql/item_jsonfunc.h b/sql/item_jsonfunc.h index ec6c6696001..64ee47f740a 100644 --- a/sql/item_jsonfunc.h +++ b/sql/item_jsonfunc.h @@ -560,15 +560,17 @@ public: is_separator, limit_clause, row_limit, offset_limit) { } - Item_func_json_arrayagg(THD *thd, Item_func_json_arrayagg *item); + Item_func_json_arrayagg(THD *thd, Item_func_json_arrayagg *item) : + Item_func_group_concat(thd, item) {} bool is_json_type() { return true; } const char *func_name() const { return "json_arrayagg("; } enum Sumfunctype sum_func() const {return JSON_ARRAYAGG_FUNC;} - String* val_str(String *str); + String* val_str(String *str) override; - Item *get_copy(THD *thd) + Item *copy_or_same(THD* thd) override; + Item *get_copy(THD *thd) override { return get_item_copy(thd, this); } };