diff --git a/mysql-test/r/func_json.result b/mysql-test/r/func_json.result index e3eb8328deb..0c2aa964e48 100644 --- a/mysql-test/r/func_json.result +++ b/mysql-test/r/func_json.result @@ -277,3 +277,9 @@ NULL select json_depth(cast(NULL as JSON)); json_depth(cast(NULL as JSON)) NULL +select json_depth('[[], {}]'); +json_depth('[[], {}]') +2 +select json_depth('[[[1,2,3],"s"], {}, []]'); +json_depth('[[[1,2,3],"s"], {}, []]') +4 diff --git a/mysql-test/t/func_json.test b/mysql-test/t/func_json.test index 894e16809ec..2a23bedfbd4 100644 --- a/mysql-test/t/func_json.test +++ b/mysql-test/t/func_json.test @@ -123,4 +123,6 @@ select json_object("a", cast('{"b": "abcd"}' as json)); select cast(NULL AS JSON); select json_depth(cast(NULL as JSON)); +select json_depth('[[], {}]'); +select json_depth('[[[1,2,3],"s"], {}, []]'); diff --git a/sql/item_jsonfunc.cc b/sql/item_jsonfunc.cc index e39e8118028..f4894a2573b 100644 --- a/sql/item_jsonfunc.cc +++ b/sql/item_jsonfunc.cc @@ -1235,7 +1235,7 @@ longlong Item_func_json_depth::val_int() { String *js= args[0]->val_str(&tmp_js); json_engine_t je; - uint depth= 0; + uint depth= 0, c_depth= 0; bool inc_depth= TRUE; if ((null_value= args[0]->null_value)) @@ -1252,14 +1252,22 @@ longlong Item_func_json_depth::val_int() case JST_VALUE: if (inc_depth) { - depth++; + c_depth++; inc_depth= FALSE; + if (c_depth > depth) + depth= c_depth; } break; case JST_OBJ_START: case JST_ARRAY_START: inc_depth= TRUE; break; + case JST_OBJ_END: + case JST_ARRAY_END: + if (!inc_depth) + c_depth--; + inc_depth= FALSE; + break; default: break; }