1
0
mirror of https://github.com/mariadb-corporation/mariadb-columnstore-engine.git synced 2025-04-21 19:45:56 +03:00
mariadb-columnstore-engine/utils/funcexp/func_json_array_insert.cpp
Ziy1-Tan cdd41f05f3 MCOL-785 Implement DISTRIBUTED JSON functions
The following functions are created:
Create function JSON_VALID and test cases
Create function JSON_DEPTH and test cases
Create function JSON_LENGTH and test cases
Create function JSON_EQUALS and test cases
Create function JSON_NORMALIZE and test cases
Create function JSON_TYPE and test cases
Create function JSON_OBJECT and test cases
Create function JSON_ARRAY and test cases
Create function JSON_KEYS and test cases
Create function JSON_EXISTS and test cases
Create function JSON_QUOTE/JSON_UNQUOTE and test cases
Create function JSON_COMPACT/DETAILED/LOOSE and test cases
Create function JSON_MERGE and test cases
Create function JSON_MERGE_PATCH and test cases
Create function JSON_VALUE and test cases
Create function JSON_QUERY and test cases
Create function JSON_CONTAINS and test cases
Create function JSON_ARRAY_APPEND and test cases
Create function JSON_ARRAY_INSERT and test cases
Create function JSON_INSERT/REPLACE/SET and test cases
Create function JSON_REMOVE and test cases
Create function JSON_CONTAINS_PATH and test cases
Create function JSON_OVERLAPS and test cases
Create function JSON_EXTRACT and test cases
Create function JSON_SEARCH and test cases

Note:
Some functions output differs from MDB because session variables that affects functions output,e.g JSON_QUOTE/JSON_UNQUOTE
This depends on MCOL-5212
2022-08-30 22:22:23 +08:00

143 lines
3.3 KiB
C++

#include "functor_json.h"
#include "functioncolumn.h"
#include "constantcolumn.h"
using namespace execplan;
#include "rowgroup.h"
using namespace rowgroup;
#include "joblisttypes.h"
using namespace joblist;
#include "jsonhelpers.h"
using namespace funcexp::helpers;
namespace funcexp
{
CalpontSystemCatalog::ColType Func_json_array_insert::operationType(FunctionParm& fp,
CalpontSystemCatalog::ColType& resultType)
{
return fp[0]->data()->resultType();
}
string Func_json_array_insert::getStrVal(rowgroup::Row& row, FunctionParm& fp, bool& isNull,
execplan::CalpontSystemCatalog::ColType& type)
{
const string_view js = fp[0]->data()->getStrVal(row, isNull);
if (isNull)
return "";
const CHARSET_INFO* cs = getCharset(fp[0]);
json_engine_t jsEg;
string retJS;
retJS.reserve(js.size() + 8);
initJSPaths(paths, fp, 1, 2);
string tmpJS{js};
for (size_t i = 1, j = 0; i < fp.size(); i += 2, j++)
{
const char* rawJS = tmpJS.data();
const size_t jsLen = tmpJS.size();
JSONPath& path = paths[j];
if (!path.parsed)
{
if (parseJSPath(path, row, fp[i]) || path.p.last_step - 1 < path.p.steps ||
path.p.last_step->type != JSON_PATH_ARRAY)
{
if (path.p.s.error == 0)
path.p.s.error = SHOULD_END_WITH_ARRAY;
goto error;
}
path.p.last_step--;
}
initJSEngine(jsEg, cs, tmpJS);
path.currStep = path.p.steps;
int jsErr = 0;
if (locateJSPath(jsEg, path, &jsErr))
{
if (jsErr)
goto error;
// Can't find the array to insert.
continue;
}
if (json_read_value(&jsEg))
goto error;
if (jsEg.value_type != JSON_VALUE_ARRAY)
{
/* Must be an array. */
continue;
}
const char* itemPos = 0;
IntType itemSize = 0;
while (json_scan_next(&jsEg) == 0 && jsEg.state != JST_ARRAY_END)
{
DBUG_ASSERT(jsEg.state == JST_VALUE);
if (itemSize == path.p.last_step[1].n_item)
{
itemPos = (const char*)jsEg.s.c_str;
break;
}
itemSize++;
if (json_read_value(&jsEg) || (!json_value_scalar(&jsEg) && json_skip_level(&jsEg)))
goto error;
}
if (unlikely(jsEg.s.error || *jsEg.killed_ptr))
goto error;
if (itemPos)
{
retJS.append(rawJS, itemPos - rawJS);
if (itemSize > 0)
retJS.append(" ");
if (appendJSValue(retJS, cs, row, fp[i + 1]))
goto error;
retJS.append(",");
if (itemSize == 0)
retJS.append(" ");
retJS.append(itemPos, rawJS + jsLen - itemPos);
}
else
{
/* Insert position wasn't found - append to the array. */
DBUG_ASSERT(jsEg.state == JST_ARRAY_END);
itemPos = (const char*)(jsEg.s.c_str - jsEg.sav_c_len);
retJS.append(rawJS, itemPos - rawJS);
if (itemSize > 0)
retJS.append(", ");
if (appendJSValue(retJS, cs, row, fp[i + 1]))
goto error;
retJS.append(itemPos, rawJS + jsLen - itemPos);
}
// tmpJS save the json string for next loop
tmpJS.swap(retJS);
retJS.clear();
}
initJSEngine(jsEg, cs, tmpJS);
retJS.clear();
if (doFormat(&jsEg, retJS, Func_json_format::LOOSE))
goto error;
isNull = false;
return retJS;
error:
isNull = true;
return "";
}
} // namespace funcexp