From f4dfde69927e624c27ef5cea84c2310defd0ab2e Mon Sep 17 00:00:00 2001 From: Rucha Deodhar Date: Thu, 22 May 2025 15:42:12 +0530 Subject: [PATCH] MDEV-32854: Make JSON_DEPTH_LIMIT unlimited This patch is the columnstore-part of the task. Columnstore wanted to have previous 32 depth, so this patch aims at keeping the compatibility. --- dbcon/execplan/functioncolumn.cpp | 12 +- utils/funcexp/func_json_array_append.cpp | 11 +- utils/funcexp/func_json_array_insert.cpp | 38 +++- utils/funcexp/func_json_contains.cpp | 11 ++ utils/funcexp/func_json_contains_path.cpp | 30 +++- utils/funcexp/func_json_depth.cpp | 7 +- utils/funcexp/func_json_equals.cpp | 19 ++ utils/funcexp/func_json_exists.cpp | 8 +- utils/funcexp/func_json_extract.cpp | 23 ++- utils/funcexp/func_json_format.cpp | 8 + utils/funcexp/func_json_insert.cpp | 37 +++- utils/funcexp/func_json_keys.cpp | 8 +- utils/funcexp/func_json_length.cpp | 6 + utils/funcexp/func_json_merge.cpp | 8 + utils/funcexp/func_json_merge_patch.cpp | 12 ++ utils/funcexp/func_json_normalize.cpp | 14 ++ utils/funcexp/func_json_overlaps.cpp | 12 ++ utils/funcexp/func_json_remove.cpp | 35 +++- utils/funcexp/func_json_search.cpp | 35 +++- utils/funcexp/func_json_type.cpp | 5 + utils/funcexp/func_json_unquote.cpp | 7 + utils/funcexp/func_json_valid.cpp | 18 +- utils/funcexp/func_json_value.cpp | 31 +++- utils/funcexp/functor_json.h | 209 +++++++++++++--------- utils/funcexp/jsonhelpers.cpp | 21 +++ utils/funcexp/jsonhelpers.h | 14 ++ 26 files changed, 526 insertions(+), 113 deletions(-) diff --git a/dbcon/execplan/functioncolumn.cpp b/dbcon/execplan/functioncolumn.cpp index 720faebe2..bfed489f8 100644 --- a/dbcon/execplan/functioncolumn.cpp +++ b/dbcon/execplan/functioncolumn.cpp @@ -354,25 +354,25 @@ void FunctionColumn::unserialize(messageqcpp::ByteStream& b) fFunctor = fDynamicFunctor = new Func_json_contains(); if (dynamic_cast(fFunctor)) - fFunctor = fDynamicFunctor = new Func_json_array_append(); + fFunctor = fDynamicFunctor = new Func_json_array_append(fFunctionParms.size()); if (dynamic_cast(fFunctor)) - fFunctor = fDynamicFunctor = new Func_json_array_insert(); + fFunctor = fDynamicFunctor = new Func_json_array_insert(fFunctionParms.size()); if (auto f = dynamic_cast(fFunctor)) fFunctor = fDynamicFunctor = new Func_json_insert(f->getMode()); if (dynamic_cast(fFunctor)) - fFunctor = fDynamicFunctor = new Func_json_remove(); + fFunctor = fDynamicFunctor = new Func_json_remove(fFunctionParms.size()); if (dynamic_cast(fFunctor)) - fFunctor = fDynamicFunctor = new Func_json_contains_path(); + fFunctor = fDynamicFunctor = new Func_json_contains_path(fFunctionParms.size()); if (dynamic_cast(fFunctor)) - fFunctor = fDynamicFunctor = new Func_json_search(); + fFunctor = fDynamicFunctor = new Func_json_search(fFunctionParms.size()); if (dynamic_cast(fFunctor)) - fFunctor = fDynamicFunctor = new Func_json_extract(); + fFunctor = fDynamicFunctor = new Func_json_extract(fFunctionParms.size()); } bool FunctionColumn::operator==(const FunctionColumn& t) const diff --git a/utils/funcexp/func_json_array_append.cpp b/utils/funcexp/func_json_array_append.cpp index e038e37f1..a96e37acc 100644 --- a/utils/funcexp/func_json_array_append.cpp +++ b/utils/funcexp/func_json_array_append.cpp @@ -28,7 +28,6 @@ string Func_json_array_append::getStrVal(rowgroup::Row& row, FunctionParm& fp, b return ""; const CHARSET_INFO* cs = getCharset(fp[0]); - json_engine_t jsEg; const uchar* arrEnd; size_t strRestLen; @@ -37,6 +36,16 @@ string Func_json_array_append::getStrVal(rowgroup::Row& row, FunctionParm& fp, b initJSPaths(paths, fp, 1, 2); +#if MYSQL_VERSION_ID >= 120100 + int jsEg_stack[JSON_DEPTH_LIMIT]; + initJsonArray(&jsEg.stack, sizeof(int), &jsEg_stack); + for (size_t i=0; i= 120100 + int jsEg_stack[JSON_DEPTH_LIMIT]; + initJsonArray(&jsEg.stack, sizeof(int), &jsEg_stack); + + for (size_t i=0; i= 120100 + json_path_step_t *last_step= reinterpret_cast(mem_root_dynamic_array_get_val(&path.p.steps, path.p.last_step_idx)), + *initial_step= reinterpret_cast(path.p.steps.buffer); +#endif +#if MYSQL_VERSION_ID >= 120100 + if (parseJSPath(path, row, fp[i]) || (last_step - 1) < initial_step || + last_step->type != JSON_PATH_ARRAY) +#else + if (parseJSPath(path, row, fp[i]) || path.p.last_step - 1 < path.p.steps || path.p.last_step->type != JSON_PATH_ARRAY) +#endif { if (path.p.s.error == 0) path.p.s.error = SHOULD_END_WITH_ARRAY; goto error; } +#if MYSQL_VERSION_ID >= 120100 + path.p.last_step_idx--; +#else path.p.last_step--; +#endif } initJSEngine(jsEg, cs, tmpJS); +#if MYSQL_VERSION_ID < 120100 path.currStep = path.p.steps; +#endif int jsErr = 0; if (locateJSPath(jsEg, path, &jsErr)) @@ -82,7 +110,13 @@ string Func_json_array_insert::getStrVal(rowgroup::Row& row, FunctionParm& fp, b while (json_scan_next(&jsEg) == 0 && jsEg.state != JST_ARRAY_END) { DBUG_ASSERT(jsEg.state == JST_VALUE); +#if MYSQL_VERSION_ID >= 120100 + if (itemSize == ((reinterpret_cast + (mem_root_dynamic_array_get_val(&path.p.steps, + path.p.last_step_idx)))[1].n_item)) +#else if (itemSize == path.p.last_step[1].n_item) +#endif { itemPos = (const char*)jsEg.s.c_str; break; diff --git a/utils/funcexp/func_json_contains.cpp b/utils/funcexp/func_json_contains.cpp index f399ddc60..224f3f947 100644 --- a/utils/funcexp/func_json_contains.cpp +++ b/utils/funcexp/func_json_contains.cpp @@ -1,6 +1,7 @@ #include "functor_json.h" #include "functioncolumn.h" #include "constantcolumn.h" +#include "json_lib.h" #include "rowgroup.h" using namespace execplan; using namespace rowgroup; @@ -160,6 +161,10 @@ bool Func_json_contains::getBoolVal(Row& row, FunctionParm& fp, bool& isNull, CalpontSystemCatalog::ColType& /*type*/) { bool isNullJS = false, isNullVal = false; +#if MYSQL_VERSION_ID >= 120100 + int jsEg_stack[JSON_DEPTH_LIMIT], valEg_stack[JSON_DEPTH_LIMIT]; +#endif + const auto& js = fp[0]->data()->getStrVal(row, isNullJS); const auto& val = fp[1]->data()->getStrVal(row, isNullVal); if (isNullJS || isNullVal) @@ -182,6 +187,9 @@ bool Func_json_contains::getBoolVal(Row& row, FunctionParm& fp, bool& isNull, } json_engine_t jsEg; +#if MYSQL_VERSION_ID >= 120100 + initJsonArray(&jsEg.stack, sizeof(int), &jsEg_stack); +#endif initJSEngine(jsEg, getCharset(fp[0]), js); if (fp.size() > 2) @@ -194,6 +202,9 @@ bool Func_json_contains::getBoolVal(Row& row, FunctionParm& fp, bool& isNull, } json_engine_t valEg; +#if MYSQL_VERSION_ID >= 120100 + initJsonArray(&valEg.stack, sizeof(int), &valEg_stack); +#endif initJSEngine(valEg, getCharset(fp[1]), arg2Val); if (json_read_value(&jsEg) || json_read_value(&valEg)) diff --git a/utils/funcexp/func_json_contains_path.cpp b/utils/funcexp/func_json_contains_path.cpp index 65cd28ada..9a5a9c66f 100644 --- a/utils/funcexp/func_json_contains_path.cpp +++ b/utils/funcexp/func_json_contains_path.cpp @@ -17,7 +17,7 @@ using namespace funcexp::helpers; namespace funcexp { CalpontSystemCatalog::ColType Func_json_contains_path::operationType( - FunctionParm& fp, CalpontSystemCatalog::ColType& /*resultType*/) + FunctionParm& fp, [[maybe_unused]] CalpontSystemCatalog::ColType& resultType) { return fp[0]->data()->resultType(); } @@ -26,7 +26,7 @@ CalpontSystemCatalog::ColType Func_json_contains_path::operationType( * getBoolVal API definition */ bool Func_json_contains_path::getBoolVal(Row& row, FunctionParm& fp, bool& isNull, - CalpontSystemCatalog::ColType& /*type*/) + [[maybe_unused]] CalpontSystemCatalog::ColType& type) { const auto& js_ns = fp[0]->data()->getStrVal(row, isNull); if (isNull) @@ -62,6 +62,15 @@ bool Func_json_contains_path::getBoolVal(Row& row, FunctionParm& fp, bool& isNul } initJSPaths(paths, fp, 2, 1); + +#if MYSQL_VERSION_ID >= 120100 + for (size_t i=0; i= 120100 + int jsEg_stack[JSON_DEPTH_LIMIT]; + json_path_step_t p_steps[JSON_DEPTH_LIMIT]; + + initJsonArray(&p.steps, sizeof(json_path_step_t), &p_steps); + initJsonArray(&jsEg.stack, sizeof(int), &jsEg_stack); +#endif + json_get_path_start(&jsEg, getCharset(fp[0]), (const uchar*)js.data(), (const uchar*)js.data() + js.size(), &p); @@ -99,12 +117,20 @@ bool Func_json_contains_path::getBoolVal(Row& row, FunctionParm& fp, bool& isNul while (json_get_path_next(&jsEg, &p) == 0) { #if MYSQL_VERSION_ID >= 100900 +#if MYSQL_VERSION_ID >= 120100 + json_path_step_t *last_step= reinterpret_cast + (mem_root_dynamic_array_get_val(&p.steps, + p.last_step_idx)); + if (hasNegPath && jsEg.value_type == JSON_VALUE_ARRAY && + json_skip_array_and_count(&jsEg, arrayCounters + (last_step - reinterpret_cast(p.steps.buffer)))) +#else if (hasNegPath && jsEg.value_type == JSON_VALUE_ARRAY && json_skip_array_and_count(&jsEg, arrayCounters + (p.last_step - p.steps))) { result = true; break; } +#endif #endif for (int restSize = argSize, curr = 0; restSize > 0; restSize--, curr++) diff --git a/utils/funcexp/func_json_depth.cpp b/utils/funcexp/func_json_depth.cpp index 66bcc22c9..f45596e04 100644 --- a/utils/funcexp/func_json_depth.cpp +++ b/utils/funcexp/func_json_depth.cpp @@ -1,5 +1,6 @@ #include "functor_json.h" #include "functioncolumn.h" +#include "json_lib.h" using namespace execplan; #include "rowgroup.h" @@ -28,8 +29,12 @@ int64_t Func_json_depth::getIntVal(rowgroup::Row& row, FunctionParm& fp, bool& i int depth = 0, currDepth = 0; bool incDepth = true; - json_engine_t jsEg; + +#if MYSQL_VERSION_ID >= 120100 + int jsEg_stack[JSON_DEPTH_LIMIT]; + initJsonArray(&jsEg.stack, sizeof(int), &jsEg_stack); +#endif initJSEngine(jsEg, getCharset(fp[0]), js); do diff --git a/utils/funcexp/func_json_equals.cpp b/utils/funcexp/func_json_equals.cpp index 0e0270cc2..884e07fd2 100644 --- a/utils/funcexp/func_json_equals.cpp +++ b/utils/funcexp/func_json_equals.cpp @@ -27,6 +27,16 @@ CalpontSystemCatalog::ColType Func_json_equals::operationType(FunctionParm& fp, bool Func_json_equals::getBoolVal(Row& row, FunctionParm& fp, bool& isNull, CalpontSystemCatalog::ColType& /*type*/) { +#if MYSQL_VERSION_ID >= 120100 + json_engine_t je; + MEM_ROOT_DYNAMIC_ARRAY array; + int je_stack[JSON_DEPTH_LIMIT]; + struct json_norm_value *buffer_array[JSON_DEPTH_LIMIT]; + + initJsonArray(&je.stack, sizeof(int), &je_stack); + initJsonArray(&array, sizeof(struct json_norm_value*), buffer_array); +#endif + // auto release the DYNAMIC_STRING using DynamicString = unique_ptr; @@ -56,13 +66,22 @@ bool Func_json_equals::getBoolVal(Row& row, FunctionParm& fp, bool& isNull, const string_view js2 = js2_ns.unsafeStringRef(); bool result = false; + +#if MYSQL_VERSION_ID >= 120100 + if (json_normalize(str1.get(), js1.data(), js1.size(), getCharset(fp[0]), NULL, &je, &array)) +#else if (json_normalize(str1.get(), js1.data(), js1.size(), getCharset(fp[0]))) +#endif { isNull = true; return result; } +#if MYSQL_VERSION_ID >= 120100 + if (json_normalize(str2.get(), js2.data(), js2.size(), getCharset(fp[1]), NULL, &je, &array)) +#else if (json_normalize(str2.get(), js2.data(), js2.size(), getCharset(fp[1]))) +#endif { isNull = true; return result; diff --git a/utils/funcexp/func_json_exists.cpp b/utils/funcexp/func_json_exists.cpp index f7a3325ee..93e224503 100644 --- a/utils/funcexp/func_json_exists.cpp +++ b/utils/funcexp/func_json_exists.cpp @@ -1,6 +1,7 @@ #include "functor_json.h" #include "functioncolumn.h" #include "constantcolumn.h" +#include "json_lib.h" #include "rowgroup.h" using namespace execplan; using namespace rowgroup; @@ -30,8 +31,13 @@ bool Func_json_exists::getBoolVal(Row& row, FunctionParm& fp, bool& isNull, int jsErr = 0; json_engine_t jsEg; - initJSEngine(jsEg, getCharset(fp[0]), js); +#if MYSQL_VERSION_ID >= 120100 + int jsEg_stack[JSON_DEPTH_LIMIT]; + initJsonArray(&jsEg.stack, sizeof(int), &jsEg_stack); +#endif + + initJSEngine(jsEg, getCharset(fp[0]), js); if (!path.parsed && parseJSPath(path, row, fp[1])) goto error; diff --git a/utils/funcexp/func_json_extract.cpp b/utils/funcexp/func_json_extract.cpp index 5e6e9dfa9..bff09d1db 100644 --- a/utils/funcexp/func_json_extract.cpp +++ b/utils/funcexp/func_json_extract.cpp @@ -38,6 +38,21 @@ int Func_json_extract::doExtract(Row& row, FunctionParm& fp, json_value_types* t initJSPaths(paths, fp, 1, 1); +#if MYSQL_VERSION_ID >= 120100 + int jsEg_stack[JSON_DEPTH_LIMIT], savJSEg_stack[JSON_DEPTH_LIMIT]; + json_path_step_t p_steps[JSON_DEPTH_LIMIT]; + + initJsonArray(&p.steps, sizeof(json_path_step_t), &p_steps); + initJsonArray(&jsEg.stack, sizeof(int), &jsEg_stack); + initJsonArray(&savJSEg.stack, sizeof(int), &savJSEg_stack); + + for (size_t i=0; i= 100900 +#if MYSQL_VERSION_ID >= 120100 + json_path_step_t *last_step= reinterpret_cast(mem_root_dynamic_array_get_val(&p.steps, p.last_step_idx)); if (hasNegPath && jsEg.value_type == JSON_VALUE_ARRAY && - json_skip_array_and_count(&jsEg, arrayCounter + (p.last_step - p.steps))) + json_skip_array_and_count(&jsEg, arrayCounter + (last_step - reinterpret_cast(p.steps.buffer)))) +#else + if (hasNegPath && jsEg.value_type == JSON_VALUE_ARRAY && + json_skip_array_and_count(&jsEg, arrayCounter + (last_step - reinterpret_cast(p.steps.buffer)))) +#endif return 1; #endif diff --git a/utils/funcexp/func_json_format.cpp b/utils/funcexp/func_json_format.cpp index 73f88fdab..c26a06fb4 100644 --- a/utils/funcexp/func_json_format.cpp +++ b/utils/funcexp/func_json_format.cpp @@ -47,7 +47,15 @@ string Func_json_format::getStrVal(rowgroup::Row& row, FunctionParm& fp, bool& i } json_engine_t jsEg; + +#if MYSQL_VERSION_ID >= 120100 + int jsEg_stack [JSON_DEPTH_LIMIT]; + + initJsonArray(&jsEg.stack, sizeof(int), &jsEg_stack); +#endif + initJSEngine(jsEg, getCharset(fp[0]), js); + string ret; if (doFormat(&jsEg, ret, fmt, tabSize)) { diff --git a/utils/funcexp/func_json_insert.cpp b/utils/funcexp/func_json_insert.cpp index 1a59d974e..ab50d4656 100644 --- a/utils/funcexp/func_json_insert.cpp +++ b/utils/funcexp/func_json_insert.cpp @@ -1,6 +1,7 @@ #include "functor_json.h" #include "functioncolumn.h" #include "constantcolumn.h" +#include "json_lib.h" using namespace execplan; #include "rowgroup.h" @@ -30,6 +31,10 @@ string Func_json_insert::getStrVal(rowgroup::Row& row, FunctionParm& fp, bool& i const bool isInsertMode = mode == INSERT || mode == SET; const bool isReplaceMode = mode == REPLACE || mode == SET; +#if MYSQL_VERSION_ID >= 120100 + int jsEg_stack[JSON_DEPTH_LIMIT]; +#endif + json_engine_t jsEg; int jsErr = 0; @@ -37,6 +42,10 @@ string Func_json_insert::getStrVal(rowgroup::Row& row, FunctionParm& fp, bool& i const CHARSET_INFO* cs = getCharset(fp[0]); json_string_set_cs(&keyName, cs); +#if MYSQL_VERSION_ID >= 120100 + initJsonArray(&jsEg.stack, sizeof(int), &jsEg_stack); +#endif + initJSPaths(paths, fp, 1, 2); // Save the result of each merge and the result of the final merge separately @@ -46,8 +55,12 @@ string Func_json_insert::getStrVal(rowgroup::Row& row, FunctionParm& fp, bool& i { const char* rawJS = tmpJS.str(); const size_t jsLen = tmpJS.length(); - JSONPath& path = paths[j]; + +#if MYSQL_VERSION_ID >= 120100 + json_path_step_t *curr_last_step= nullptr; +#endif + const json_path_step_t* lastStep; const char* valEnd; @@ -56,14 +69,29 @@ string Func_json_insert::getStrVal(rowgroup::Row& row, FunctionParm& fp, bool& i if (parseJSPath(path, row, fp[i], false)) goto error; +#if MYSQL_VERSION_ID >= 120100 + path.p.last_step_idx--; +#else path.p.last_step--; +#endif } - initJSEngine(jsEg, cs, tmpJS); + +#if MYSQL_VERSION_ID >= 120100 + if ((reinterpret_cast(mem_root_dynamic_array_get_val(&path.p.steps, + path.p.last_step_idx))) < reinterpret_cast(path.p.steps.buffer)) +#else if (path.p.last_step < path.p.steps) +#endif goto v_found; +#if MYSQL_VERSION_ID >= 120100 + curr_last_step= reinterpret_cast(mem_root_dynamic_array_get_val(&path.p.steps, + path.p.last_step_idx)); + if (curr_last_step >= reinterpret_cast(path.p.steps.buffer) && locateJSPath(jsEg, path, &jsErr)) +#else if (path.p.last_step >= path.p.steps && locateJSPath(jsEg, path, &jsErr)) +#endif { if (jsErr) goto error; @@ -73,7 +101,12 @@ string Func_json_insert::getStrVal(rowgroup::Row& row, FunctionParm& fp, bool& i if (json_read_value(&jsEg)) goto error; +#if MYSQL_VERSION_ID >= 120100 + lastStep = curr_last_step + 1; +#else lastStep = path.p.last_step + 1; +#endif + if (lastStep->type & JSON_PATH_ARRAY) { IntType itemSize = 0; diff --git a/utils/funcexp/func_json_keys.cpp b/utils/funcexp/func_json_keys.cpp index 8634fce50..b79e81e3c 100644 --- a/utils/funcexp/func_json_keys.cpp +++ b/utils/funcexp/func_json_keys.cpp @@ -61,8 +61,14 @@ string Func_json_keys::getStrVal(rowgroup::Row& row, FunctionParm& fp, bool& isN IntType keySize = 0; string ret; json_engine_t jsEg; - initJSEngine(jsEg, getCharset(fp[0]), js); +#if MYSQL_VERSION_ID >= 120100 + int jsEg_stack[JSON_DEPTH_LIMIT]; + memset(&jsEg_stack[0], 0, sizeof(jsEg_stack)); + initJsonArray(&jsEg.stack, sizeof(int), &jsEg_stack); +#endif + + initJSEngine(jsEg, getCharset(fp[0]), js); if (fp.size() > 1) { if (!path.parsed && parseJSPath(path, row, fp[1], false)) diff --git a/utils/funcexp/func_json_length.cpp b/utils/funcexp/func_json_length.cpp index 474e8bbca..3628d3a1c 100644 --- a/utils/funcexp/func_json_length.cpp +++ b/utils/funcexp/func_json_length.cpp @@ -1,6 +1,7 @@ #include "functor_json.h" #include "functioncolumn.h" #include "constantcolumn.h" +#include "json_lib.h" using namespace execplan; #include "rowgroup.h" @@ -31,6 +32,11 @@ int64_t Func_json_length::getIntVal(rowgroup::Row& row, FunctionParm& fp, bool& int length = 0; int err; +#if MYSQL_VERSION_ID >= 120100 + int jsEg_stack[JSON_DEPTH_LIMIT]; + initJsonArray(&jsEg.stack, sizeof(int), &jsEg_stack); +#endif + initJSEngine(jsEg, getCharset(fp[0]), js); if (fp.size() > 1) diff --git a/utils/funcexp/func_json_merge.cpp b/utils/funcexp/func_json_merge.cpp index 3d4b241bb..95aec7bbe 100644 --- a/utils/funcexp/func_json_merge.cpp +++ b/utils/funcexp/func_json_merge.cpp @@ -1,5 +1,6 @@ #include "functor_json.h" #include "functioncolumn.h" +#include "json_lib.h" using namespace execplan; #include "rowgroup.h" @@ -225,6 +226,13 @@ string Func_json_merge::getStrVal(rowgroup::Row& row, FunctionParm& fp, bool& is json_engine_t jsEg1, jsEg2; +#if MYSQL_VERSION_ID >= 120100 + int jsEg1_stack[JSON_DEPTH_LIMIT], jsEg2_stack[JSON_DEPTH_LIMIT]; + + initJsonArray(&jsEg1.stack, sizeof(int), &jsEg1_stack); + initJsonArray(&jsEg2.stack, sizeof(int), &jsEg2_stack); +#endif + utils::NullString tmpJS(js); string retJS; diff --git a/utils/funcexp/func_json_merge_patch.cpp b/utils/funcexp/func_json_merge_patch.cpp index 250c11736..d19993be1 100644 --- a/utils/funcexp/func_json_merge_patch.cpp +++ b/utils/funcexp/func_json_merge_patch.cpp @@ -1,5 +1,6 @@ #include "functor_json.h" #include "functioncolumn.h" +#include "json_lib.h" using namespace execplan; #include "rowgroup.h" @@ -277,6 +278,9 @@ string Func_json_merge_patch::getStrVal(rowgroup::Row& row, FunctionParm& fp, bo json_engine_t jsEg1, jsEg2; jsEg1.s.error = jsEg2.s.error = 0; +#if MYSQL_VERSION_ID >= 120100 + int jsEg1_stack[JSON_DEPTH_LIMIT], jsEg2_stack[JSON_DEPTH_LIMIT]; +#endif utils::NullString tmpJS(js); string retJS; @@ -290,6 +294,10 @@ string Func_json_merge_patch::getStrVal(rowgroup::Row& row, FunctionParm& fp, bo goto next; } +#if MYSQL_VERSION_ID >= 120100 + initJsonArray(&jsEg2.stack, sizeof(int), &jsEg2_stack); +#endif + initJSEngine(jsEg2, getCharset(fp[i]), js2); if (hasNullArg) @@ -304,6 +312,10 @@ string Func_json_merge_patch::getStrVal(rowgroup::Row& row, FunctionParm& fp, bo goto next; } +#if MYSQL_VERSION_ID >= 120100 + initJsonArray(&jsEg1.stack, sizeof(int), &jsEg1_stack); +#endif + initJSEngine(jsEg1, getCharset(fp[0]), tmpJS); if (doMergePatch(retJS, &jsEg1, &jsEg2, isEmpty)) { diff --git a/utils/funcexp/func_json_normalize.cpp b/utils/funcexp/func_json_normalize.cpp index 0470f8a80..a5a973ff9 100644 --- a/utils/funcexp/func_json_normalize.cpp +++ b/utils/funcexp/func_json_normalize.cpp @@ -25,6 +25,16 @@ CalpontSystemCatalog::ColType Func_json_normalize::operationType( string Func_json_normalize::getStrVal(rowgroup::Row& row, FunctionParm& fp, bool& isNull, execplan::CalpontSystemCatalog::ColType& /*type*/) { +#if MYSQL_VERSION_ID >= 120100 + json_engine_t je; + MEM_ROOT_DYNAMIC_ARRAY array; + int je_stack[JSON_DEPTH_LIMIT]; + struct json_norm_value *buffer_array[JSON_DEPTH_LIMIT]; + + initJsonArray(&je.stack, sizeof(int), &je_stack); + initJsonArray(&array, sizeof(struct json_norm_value*), buffer_array); +#endif + const auto js_ns = fp[0]->data()->getStrVal(row, isNull); if (isNull) return ""; @@ -36,7 +46,11 @@ string Func_json_normalize::getStrVal(rowgroup::Row& row, FunctionParm& fp, bool if (init_dynamic_string(str.get(), NULL, 0, 0)) goto error; +#if MYSQL_VERSION_ID >= 120100 + if (json_normalize(str.get(), js.data(), js.size(), getCharset(fp[0]), NULL, &je, &array)) +#else if (json_normalize(str.get(), js.data(), js.size(), getCharset(fp[0]))) +#endif goto error; return str->str; diff --git a/utils/funcexp/func_json_overlaps.cpp b/utils/funcexp/func_json_overlaps.cpp index b8933a79b..365968215 100644 --- a/utils/funcexp/func_json_overlaps.cpp +++ b/utils/funcexp/func_json_overlaps.cpp @@ -1,5 +1,6 @@ #include "functor_json.h" #include "functioncolumn.h" +#include "json_lib.h" #include "rowgroup.h" using namespace execplan; using namespace rowgroup; @@ -279,12 +280,23 @@ bool Func_json_overlaps::getBoolVal(Row& row, FunctionParm& fp, bool& /*isNull*/ CalpontSystemCatalog::ColType& /*type*/) { bool isNullJS1 = false, isNullJS2 = false; + +#if MYSQL_VERSION_ID >= 120100 + int jsEg1_stack[JSON_DEPTH_LIMIT], jsEg2_stack[JSON_DEPTH_LIMIT]; +#endif + const auto js1 = fp[0]->data()->getStrVal(row, isNullJS1); const auto js2 = fp[1]->data()->getStrVal(row, isNullJS2); if (isNullJS1 || isNullJS2) return false; json_engine_t jsEg1, jsEg2; + +#if MYSQL_VERSION_ID >= 120100 + initJsonArray(&jsEg1.stack, sizeof(int), &jsEg1_stack); + initJsonArray(&jsEg2.stack, sizeof(int), &jsEg2_stack); +#endif + initJSEngine(jsEg1, getCharset(fp[0]), js1); initJSEngine(jsEg2, getCharset(fp[1]), js2); diff --git a/utils/funcexp/func_json_remove.cpp b/utils/funcexp/func_json_remove.cpp index ca9e958ea..e106f3c86 100644 --- a/utils/funcexp/func_json_remove.cpp +++ b/utils/funcexp/func_json_remove.cpp @@ -29,7 +29,6 @@ string Func_json_remove::getStrVal(rowgroup::Row& row, FunctionParm& fp, bool& i return ""; json_engine_t jsEg; - int jsErr = 0; json_string_t keyName; const CHARSET_INFO* cs = getCharset(fp[0]); @@ -37,6 +36,17 @@ string Func_json_remove::getStrVal(rowgroup::Row& row, FunctionParm& fp, bool& i initJSPaths(paths, fp, 1, 1); +#if MYSQL_VERSION_ID >= 120100 + int jsEg_stack[JSON_DEPTH_LIMIT]; + initJsonArray(&jsEg.stack, sizeof(int), &jsEg_stack); + + for (size_t i=0; i= 120100 + json_path_step_t *curr_last_step= nullptr; +#endif + if (!path.parsed) { if (parseJSPath(path, row, fp[i], false)) goto error; - path.p.last_step--; +#if MYSQL_VERSION_ID >= 120100 + path.p.last_step_idx--; + curr_last_step= reinterpret_cast + (mem_root_dynamic_array_get_val(&path.p.steps, + path.p.last_step_idx)); + if (curr_last_step < reinterpret_cast(path.p.steps.buffer)) +#else + path.p.last_step--; if (path.p.last_step < path.p.steps) +#endif { path.p.s.error = TRIVIAL_PATH_NOT_ALLOWED; goto error; @@ -64,7 +86,11 @@ string Func_json_remove::getStrVal(rowgroup::Row& row, FunctionParm& fp, bool& i initJSEngine(jsEg, cs, tmpJS); +#if MYSQL_VERSION_ID >= 120100 + if (curr_last_step < reinterpret_cast(path.p.steps.buffer)) +#else if (path.p.last_step < path.p.steps) +#endif goto v_found; if (locateJSPath(jsEg, path, &jsErr) && jsErr) @@ -73,7 +99,12 @@ string Func_json_remove::getStrVal(rowgroup::Row& row, FunctionParm& fp, bool& i if (json_read_value(&jsEg)) goto error; +#if MYSQL_VERSION_ID >= 120100 + lastStep = curr_last_step + 1; +#else lastStep = path.p.last_step + 1; +#endif + if (lastStep->type & JSON_PATH_ARRAY) { if (jsEg.value_type != JSON_VALUE_ARRAY) diff --git a/utils/funcexp/func_json_search.cpp b/utils/funcexp/func_json_search.cpp index efad86fa5..cbf1a1a80 100644 --- a/utils/funcexp/func_json_search.cpp +++ b/utils/funcexp/func_json_search.cpp @@ -20,12 +20,20 @@ namespace static bool appendJSPath(string& ret, const json_path_t* p) { const json_path_step_t* c; - +#if MYSQL_VERSION_ID >= 120100 + const json_path_step_t* last_step= (const json_path_step_t*) + (mem_root_dynamic_array_get_val((MEM_ROOT_DYNAMIC_ARRAY*)&p->steps, + (size_t)p->last_step_idx)); +#endif try { ret.append("\"$"); +#if MYSQL_VERSION_ID >= 120100 + for (c = reinterpret_cast(p->steps.buffer)+1; c <= last_step; c++) +#else for (c = p->steps + 1; c <= p->last_step; c++) +#endif { if (c->type & JSON_PATH_KEY) { @@ -144,9 +152,23 @@ string Func_json_search::getStrVal(rowgroup::Row& row, FunctionParm& fp, bool& i initJSPaths(paths, fp, 4, 1); - for (size_t i = 4; i < fp.size(); i++) +#if MYSQL_VERSION_ID >= 120100 + int jsEg_stack[JSON_DEPTH_LIMIT]; + json_path_step_t p_steps[JSON_DEPTH_LIMIT], savPath_steps[JSON_DEPTH_LIMIT]; + initJsonArray(&jsEg.stack, sizeof(int), &jsEg_stack); + initJsonArray(&savPath.steps, sizeof(json_path_step_t), &savPath_steps); + initJsonArray(&p.steps, sizeof(json_path_step_t), &p_steps); +#endif + + int n= p_steps_arr.size(); + + for (size_t i = 4; i < fp.size()+n; i++) { JSONPath& path = paths[i - 4]; +#if MYSQL_VERSION_ID >= 120100 + initJsonArray(&path.p.steps, sizeof(json_path_step_t), &p_steps_arr[i-4]); +#endif + if (!path.parsed) { if (parseJSPath(path, row, fp[i])) @@ -162,8 +184,13 @@ string Func_json_search::getStrVal(rowgroup::Row& row, FunctionParm& fp, bool& i while (json_get_path_next(&jsEg, &p) == 0) { #if MYSQL_VERSION_ID >= 100900 +#if MYSQL_VERSION_ID >= 120100 if (hasNegPath && jsEg.value_type == JSON_VALUE_ARRAY && + json_skip_array_and_count(&jsEg, arrayCounter + p.last_step_idx)) +#else + if (hasNegPath && jsEg.value_type == JSON_VALUE_ARRAY && json_skip_array_and_count(&jsEg, arrayCounter + (p.last_step - p.steps))) +#endif goto error; #endif @@ -180,7 +207,11 @@ string Func_json_search::getStrVal(rowgroup::Row& row, FunctionParm& fp, bool& i if (pathFound == 1) { savPath = p; +#if MYSQL_VERSION_ID >= 120100 + mem_root_dynamic_array_copy_values(&savPath.steps, &p.steps); +#else savPath.last_step = savPath.steps + (p.last_step - p.steps); +#endif } else { diff --git a/utils/funcexp/func_json_type.cpp b/utils/funcexp/func_json_type.cpp index be248a794..e6fe3fc36 100644 --- a/utils/funcexp/func_json_type.cpp +++ b/utils/funcexp/func_json_type.cpp @@ -29,6 +29,11 @@ string Func_json_type::getStrVal(rowgroup::Row& row, FunctionParm& fp, bool& isN json_engine_t jsEg; string result; +#if MYSQL_VERSION_ID >= 120100 + int jsEg_stack[JSON_DEPTH_LIMIT]; + initJsonArray(&jsEg.stack, sizeof(int), &jsEg_stack); +#endif + initJSEngine(jsEg, getCharset(fp[0]), js); if (json_read_value(&jsEg)) diff --git a/utils/funcexp/func_json_unquote.cpp b/utils/funcexp/func_json_unquote.cpp index 35f11694c..6314c63c8 100644 --- a/utils/funcexp/func_json_unquote.cpp +++ b/utils/funcexp/func_json_unquote.cpp @@ -1,5 +1,6 @@ #include "functor_json.h" #include "functioncolumn.h" +#include "json_lib.h" #include "jsonhelpers.h" using namespace execplan; @@ -30,6 +31,12 @@ std::string Func_json_unquote::getStrVal(rowgroup::Row& row, FunctionParm& fp, b int strLen; const CHARSET_INFO* cs = type.getCharset(); + +#if MYSQL_VERSION_ID >= 120100 + int jsEg_stack[JSON_DEPTH_LIMIT]; + initJsonArray(&jsEg.stack, sizeof(int), &jsEg_stack); +#endif + initJSEngine(jsEg, cs, js); json_read_value(&jsEg); diff --git a/utils/funcexp/func_json_valid.cpp b/utils/funcexp/func_json_valid.cpp index 8d118c75f..e21b6ece0 100644 --- a/utils/funcexp/func_json_valid.cpp +++ b/utils/funcexp/func_json_valid.cpp @@ -25,10 +25,26 @@ CalpontSystemCatalog::ColType Func_json_valid::operationType(FunctionParm& fp, bool Func_json_valid::getBoolVal(Row& row, FunctionParm& fp, bool& isNull, CalpontSystemCatalog::ColType& /*type*/) { + int result= 0; +#if MYSQL_VERSION_ID >= 120100 + json_engine_t je; + int je_stack[JSON_DEPTH_LIMIT]; + initJsonArray(&je.stack, sizeof(int), &je_stack); +#endif + const auto js = fp[0]->data()->getStrVal(row, isNull); if (isNull) return false; - return json_valid(js.unsafeStringRef().data(), js.unsafeStringRef().size(), getCharset(fp[0])); +#if MYSQL_VERSION_ID >= 120100 + result= json_valid(js.unsafeStringRef().data(), js.unsafeStringRef().size(), getCharset(fp[0]), &je); +#else + result= json_valid(js.unsafeStringRef().data(), js.unsafeStringRef().size(), getCharset(fp[0])); +#endif + +if (result == 0) + return 1; +else + return 0; } } // namespace funcexp diff --git a/utils/funcexp/func_json_value.cpp b/utils/funcexp/func_json_value.cpp index 7bc2ee4dc..2f056c437 100644 --- a/utils/funcexp/func_json_value.cpp +++ b/utils/funcexp/func_json_value.cpp @@ -1,6 +1,7 @@ #include "functor_json.h" #include "functioncolumn.h" #include "constantcolumn.h" +#include "json_lib.h" using namespace execplan; #include "rowgroup.h" @@ -63,6 +64,13 @@ bool JSONPathWrapper::extract(std::string& ret, rowgroup::Row& row, execplan::SP { bool isNullJS = false, isNullPath = false; +#if MYSQL_VERSION_ID >= 120100 + MEM_ROOT_DYNAMIC_ARRAY array; + IntType arrayCounters[JSON_DEPTH_LIMIT]; + int je_stack[JSON_DEPTH_LIMIT]; + json_path_step_t p_steps[JSON_DEPTH_LIMIT]; +#endif + const utils::NullString& js = funcParamJS->data()->getStrVal(row, isNullJS); const utils::NullString& sjsp = funcParamPath->data()->getStrVal(row, isNullPath); if (isNullJS || isNullPath) @@ -70,21 +78,38 @@ bool JSONPathWrapper::extract(std::string& ret, rowgroup::Row& row, execplan::SP int error = 0; + +#if MYSQL_VERSION_ID >= 120100 + initJsonArray(&p.steps, sizeof(json_path_step_t), &p_steps); +#endif + if (json_path_setup(&p, getCharset(funcParamPath), (const uchar*)sjsp.str(), (const uchar*)sjsp.end())) return true; - JSONEgWrapper je(getCharset(funcParamJS), reinterpret_cast(js.str()), - reinterpret_cast(js.end())); + JSONEgWrapper je(getCharset(funcParamJS), reinterpret_cast(js.str()), + reinterpret_cast(js.end()), je_stack); +#if MYSQL_VERSION_ID >= 120100 + initJsonArray(&je.stack, sizeof(int), &je_stack); + + currStep = reinterpret_cast(p.steps.buffer); + + + initJsonArray(&array, sizeof(int), &arrayCounters); +#else currStep = p.steps; +#endif do { if (error) return true; - +#if MYSQL_VERSION_ID >= 120100 + if (json_find_path(&je, &p, &currStep, &array)) +#else IntType arrayCounters[JSON_DEPTH_LIMIT]; if (json_find_path(&je, &p, &currStep, arrayCounters)) +#endif return true; if (json_read_value(&je)) diff --git a/utils/funcexp/functor_json.h b/utils/funcexp/functor_json.h index 55c4d1b5f..3a01ad29d 100644 --- a/utils/funcexp/functor_json.h +++ b/utils/funcexp/functor_json.h @@ -27,15 +27,64 @@ struct JSONPath json_path_step_t* currStep; }; +class Func_json_no_multipath +{ + protected: + JSONPath path; +#if MYSQL_VERSION_ID >= 120100 + json_path_step_t p_steps[JSON_DEPTH_LIMIT]; +#endif + +public: + Func_json_no_multipath() + { +#if MYSQL_VERSION_ID >= 120100 + initJsonPathArray(&path.p.steps, &p_steps[0]); +#endif + } +}; + +class Func_json_multipath +{ + protected: + std::vector paths; +#if MYSQL_VERSION_ID >= 120100 + std::vector> p_steps_arr; +#endif + + public: + Func_json_multipath() + { +#if MYSQL_VERSION_ID >= 120100 + p_steps_arr = std::vector>(paths.size(), std::vector(JSON_DEPTH_LIMIT)); + for (size_t i=0; i= 120100 + p_steps_arr = std::vector>(number_of_args, std::vector(JSON_DEPTH_LIMIT)); +#endif + } + ~Func_json_multipath() {} +}; + class JSONEgWrapper : public json_engine_t { public: - JSONEgWrapper(CHARSET_INFO* cs, const uchar* str, const uchar* end) + JSONEgWrapper(CHARSET_INFO* cs, const uchar* str, const uchar* end, int *buffer) { + mem_root_dynamic_array_init(NULL, PSI_INSTRUMENT_MEM, + &this->stack, sizeof(int), buffer, + JSON_DEPTH_DEFAULT, JSON_DEPTH_INC, MYF(0)); json_scan_start(this, cs, str, end); } JSONEgWrapper(const std::string& str, CHARSET_INFO* cs) - : JSONEgWrapper(cs, (const uchar*)str.data(), (const uchar*)str.data() + str.size()) + : JSONEgWrapper(cs, (const uchar*)str.data(), (const uchar*)str.data() + str.size(), NULL) { } bool checkAndGetScalar(std::string& ret, int* error); @@ -88,15 +137,11 @@ class Func_json_depth : public Func_Int /** @brief Func_json_length class */ -class Func_json_length : public Func_Int +class Func_json_length : public Func_Int, public Func_json_no_multipath { - protected: - JSONPath path; - public: - Func_json_length() : Func_Int("json_length") - { - } + Func_json_length() : Func_Int("json_length"), Func_json_no_multipath() + {} ~Func_json_length() override = default; execplan::CalpontSystemCatalog::ColType operationType( @@ -192,13 +237,10 @@ class Func_json_array : public Func_Str }; /** @brief Func_json_keys class */ -class Func_json_keys : public Func_Str +class Func_json_keys : public Func_Str, public Func_json_no_multipath { - protected: - JSONPath path; - public: - Func_json_keys() : Func_Str("json_keys") + Func_json_keys() : Func_Str("json_keys"), Func_json_no_multipath() { } ~Func_json_keys() override = default; @@ -211,15 +253,11 @@ class Func_json_keys : public Func_Str }; /** @brief Func_json_exists class */ -class Func_json_exists : public Func_Bool +class Func_json_exists : public Func_Bool, public Func_json_no_multipath { - protected: - JSONPath path; - public: - Func_json_exists() : Func_Bool("json_exists") - { - } + Func_json_exists() : Func_Bool("json_exists"), Func_json_no_multipath() + {} ~Func_json_exists() override = default; execplan::CalpontSystemCatalog::ColType operationType( @@ -231,15 +269,11 @@ class Func_json_exists : public Func_Bool /** @brief Func_json_quote class */ -class Func_json_quote : public Func_Str +class Func_json_quote : public Func_Str, public Func_json_no_multipath { - protected: - JSONPath path; - public: - Func_json_quote() : Func_Str("json_quote") - { - } + Func_json_quote() : Func_Str("json_quote"), Func_json_no_multipath() + {} ~Func_json_quote() override = default; execplan::CalpontSystemCatalog::ColType operationType( @@ -251,15 +285,11 @@ class Func_json_quote : public Func_Str /** @brief Func_json_unquote class */ -class Func_json_unquote : public Func_Str +class Func_json_unquote : public Func_Str, public Func_json_no_multipath { - protected: - JSONPath path; - public: - Func_json_unquote() : Func_Str("json_unquote") - { - } + Func_json_unquote() : Func_Str("json_unquote"), Func_json_no_multipath() + {} ~Func_json_unquote() override = default; execplan::CalpontSystemCatalog::ColType operationType( @@ -331,8 +361,7 @@ class Func_json_merge_patch : public Func_Str { public: Func_json_merge_patch() : Func_Str("json_merge_patch") - { - } + {} ~Func_json_merge_patch() override = default; execplan::CalpontSystemCatalog::ColType operationType( @@ -377,18 +406,17 @@ class Func_json_query : public Func_Str }; /** @brief Func_json_contains class */ -class Func_json_contains : public Func_Bool +class Func_json_contains : public Func_Bool, public Func_json_no_multipath { protected: - JSONPath path; bool arg2Const; bool arg2Parsed; // argument 2 is a constant or has been parsed utils::NullString arg2Val; public: - Func_json_contains() : Func_Bool("json_contains"), arg2Const(false), arg2Parsed(false), arg2Val("") - { - } + Func_json_contains() : Func_Bool("json_contains"), arg2Const(false), + arg2Parsed(false), arg2Val("") + {} ~Func_json_contains() override = default; execplan::CalpontSystemCatalog::ColType operationType( @@ -399,15 +427,14 @@ class Func_json_contains : public Func_Bool }; /** @brief Func_json_array_append class */ -class Func_json_array_append : public Func_Str +class Func_json_array_append : public Func_Str, public Func_json_multipath { - protected: - std::vector paths; - public: - Func_json_array_append() : Func_Str("json_array_append") - { - } + Func_json_array_append() : Func_Str("json_array_append"), Func_json_multipath() + {} + Func_json_array_append(size_t number_of_args) : Func_Str("json_array_append"), + Func_json_multipath(number_of_args) + {} ~Func_json_array_append() override = default; execplan::CalpontSystemCatalog::ColType operationType( @@ -421,15 +448,13 @@ class Func_json_array_append : public Func_Str }; /** @brief Func_json_array_insert class */ -class Func_json_array_insert : public Func_Str +class Func_json_array_insert : public Func_Str, public Func_json_multipath { - protected: - std::vector paths; - public: - Func_json_array_insert() : Func_Str("json_array_insert") - { - } + Func_json_array_insert() : Func_Str("json_array_insert"), Func_json_multipath() + {} + Func_json_array_insert(size_t number_of_args) : Func_Str("json_array_insert"), Func_json_multipath(number_of_args) + {} ~Func_json_array_insert() override = default; execplan::CalpontSystemCatalog::ColType operationType( @@ -441,7 +466,7 @@ class Func_json_array_insert : public Func_Str /** @brief Func_json_insert class */ -class Func_json_insert : public Func_Str +class Func_json_insert : public Func_json_multipath, public Func_Str { public: enum MODE @@ -454,12 +479,13 @@ class Func_json_insert : public Func_Str protected: MODE mode; - std::vector paths; public: Func_json_insert() : Func_Str("json_insert"), mode(INSERT) - { - } + {} + Func_json_insert(size_t number_of_args) : Func_json_multipath(number_of_args), + Func_Str("json_insert"), mode(INSERT) + {} explicit Func_json_insert(MODE m) : mode(m) { assert(m != NONE); @@ -471,6 +497,17 @@ class Func_json_insert : public Func_Str default: break; } } + explicit Func_json_insert(size_t number_of_args, MODE m) : Func_json_multipath(number_of_args), mode(m) + { + assert(m != NONE); + switch (m) + { + case INSERT: Func_Str::Func::funcName("json_insert"); break; + case REPLACE: Func_Str::Func::funcName("json_replace"); break; + case SET: Func_Str::Func::funcName("json_set"); break; + default: break; + } + } ~Func_json_insert() override = default; MODE getMode() const @@ -486,15 +523,13 @@ class Func_json_insert : public Func_Str }; /** @brief Func_json_remove class */ -class Func_json_remove : public Func_Str +class Func_json_remove : public Func_Str, public Func_json_multipath { - protected: - std::vector paths; - public: - Func_json_remove() : Func_Str("json_remove") - { - } + Func_json_remove() : Func_Str("json_remove"), Func_json_multipath() + {} + Func_json_remove(size_t number_of_args) : Func_Str("json_remove"), Func_json_multipath(number_of_args) + {} ~Func_json_remove() override = default; execplan::CalpontSystemCatalog::ColType operationType( @@ -506,10 +541,9 @@ class Func_json_remove : public Func_Str /** @brief Func_json_contains_path class */ -class Func_json_contains_path : public Func_Bool +class Func_json_contains_path : public Func_json_multipath, public Func_Bool { protected: - std::vector paths; std::vector hasFound; bool isModeOne; bool isModeConst; @@ -518,8 +552,11 @@ class Func_json_contains_path : public Func_Bool public: Func_json_contains_path() : Func_Bool("json_contains_path"), isModeOne(false), isModeConst(false), isModeParsed(false) - { - } + {} + Func_json_contains_path(size_t number_of_args) + : Func_json_multipath(number_of_args), Func_Bool("json_contains_path"), + isModeOne(false), isModeConst(false), isModeParsed(false) + {} ~Func_json_contains_path() override = default; execplan::CalpontSystemCatalog::ColType operationType( @@ -531,15 +568,11 @@ class Func_json_contains_path : public Func_Bool /** @brief Func_json_overlaps class */ -class Func_json_overlaps : public Func_Bool +class Func_json_overlaps : public Func_Bool, public Func_json_no_multipath { - protected: - JSONPath path; - public: - Func_json_overlaps() : Func_Bool("json_overlaps") - { - } + Func_json_overlaps() : Func_Bool("json_overlaps"), Func_json_no_multipath() + {} ~Func_json_overlaps() override = default; execplan::CalpontSystemCatalog::ColType operationType( @@ -550,10 +583,9 @@ class Func_json_overlaps : public Func_Bool }; /** @brief Func_json_search class */ -class Func_json_search : public Func_Str +class Func_json_search : public Func_json_multipath, public Func_Str { protected: - std::vector paths; bool isModeParsed; bool isModeConst; bool isModeOne; @@ -562,8 +594,11 @@ class Func_json_search : public Func_Str public: Func_json_search() : Func_Str("json_search"), isModeParsed(false), isModeConst(false), isModeOne(false), escape('\\') - { - } + {} + Func_json_search(size_t number_of_args) + : Func_json_multipath(number_of_args), Func_Str("json_search"), + isModeParsed(false), isModeConst(false), isModeOne(false), escape('\\') + {} ~Func_json_search() override = default; execplan::CalpontSystemCatalog::ColType operationType( @@ -577,15 +612,13 @@ class Func_json_search : public Func_Str }; /** @brief Func_json_extract_string class */ -class Func_json_extract : public Func_Str +class Func_json_extract : public Func_Str, public Func_json_multipath { - protected: - std::vector paths; - public: Func_json_extract() : Func_Str("json_extract") - { - } + {} + Func_json_extract(size_t number_of_args) : Func_Str("json_extract"), Func_json_multipath(number_of_args) + {} ~Func_json_extract() override = default; execplan::CalpontSystemCatalog::ColType operationType( diff --git a/utils/funcexp/jsonhelpers.cpp b/utils/funcexp/jsonhelpers.cpp index 760a0c3d1..6211bb0e6 100644 --- a/utils/funcexp/jsonhelpers.cpp +++ b/utils/funcexp/jsonhelpers.cpp @@ -335,7 +335,17 @@ int cmpPartJSPath(const json_path_step_t* a, const json_path_step_t* aEnd, const int cmpJSPath(const json_path_t* a, const json_path_t* b, enum json_value_types vt, const int* arraySize) { +#if MYSQL_VERSION_ID >= 120100 + json_path_step_t *a_last_step= reinterpret_cast + (mem_root_dynamic_array_get_val((MEM_ROOT_DYNAMIC_ARRAY*)&a->steps, (size_t)a->last_step_idx)); + json_path_step_t *b_last_step= reinterpret_cast + (mem_root_dynamic_array_get_val((MEM_ROOT_DYNAMIC_ARRAY*)&b->steps, (size_t)b->last_step_idx)); + return cmpPartJSPath((reinterpret_cast(a->steps.buffer)) + 1, a_last_step, + (reinterpret_cast(b->steps.buffer)) + 1, b_last_step, + vt, arraySize); +#else return cmpPartJSPath(a->steps + 1, a->last_step, b->steps + 1, b->last_step, vt, arraySize); +#endif } int parseJSPath(JSONPath& path, rowgroup::Row& row, execplan::SPTP& parm, bool wildcards) @@ -371,5 +381,16 @@ bool matchJSPath(const vector& paths, const json_path_t* p, j } return false; } + +#if MYSQL_VERSION_ID >= 120100 +void initJsonArray(MEM_ROOT_DYNAMIC_ARRAY *mem_root_array, size_t size, void *buffer) +{ + mem_root_dynamic_array_init(NULL, PSI_INSTRUMENT_MEM | MY_INIT_BUFFER_USED | MY_BUFFER_NO_RESIZE, + mem_root_array, size, buffer, + JSON_DEPTH_LIMIT, 0, MYF(0)); + return; +} +#endif + } // namespace helpers } // namespace funcexp diff --git a/utils/funcexp/jsonhelpers.h b/utils/funcexp/jsonhelpers.h index 2756925b8..75ae8260f 100644 --- a/utils/funcexp/jsonhelpers.h +++ b/utils/funcexp/jsonhelpers.h @@ -55,6 +55,10 @@ using IntType = int; using IntType = uint; #endif +#if MYSQL_VERSION_ID >= 120100 +void initJsonArray(MEM_ROOT_DYNAMIC_ARRAY *mem_root_array, size_t size, void *buffer); +#endif + /* Compatible with json_find_path function in json_lib before 10.9: uint* array_counters @@ -63,8 +67,18 @@ using IntType = uint; inline static int locateJSPath(json_engine_t& jsEg, JSONPath& path, int* jsErr = nullptr) { IntType arrayCounters[JSON_DEPTH_LIMIT]; + +#if MYSQL_VERSION_ID >= 120100 + MEM_ROOT_DYNAMIC_ARRAY array; + + initJsonArray(&array, sizeof(int), &arrayCounters); + + path.currStep = reinterpret_cast(path.p.steps.buffer); + if (json_find_path(&jsEg, &path.p, &path.currStep, &array)) +#else path.currStep = path.p.steps; if (json_find_path(&jsEg, &path.p, &path.currStep, arrayCounters)) +#endif { if (jsErr && jsEg.s.error) *jsErr = 1;