You've already forked mariadb-columnstore-engine
							
							
				mirror of
				https://github.com/mariadb-corporation/mariadb-columnstore-engine.git
				synced 2025-11-03 17:13:17 +03:00 
			
		
		
		
	This patch is the columnstore-part of the task. Columnstore wanted to have previous 32 depth, so this patch aims at keeping the compatibility.
		
			
				
	
	
		
			271 lines
		
	
	
		
			6.5 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			271 lines
		
	
	
		
			6.5 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
#include "functor_json.h"
 | 
						|
#include "functioncolumn.h"
 | 
						|
#include "constantcolumn.h"
 | 
						|
using namespace execplan;
 | 
						|
 | 
						|
#include "rowgroup.h"
 | 
						|
using namespace rowgroup;
 | 
						|
 | 
						|
#include "dataconvert.h"
 | 
						|
using namespace dataconvert;
 | 
						|
 | 
						|
#include "jsonhelpers.h"
 | 
						|
using namespace funcexp::helpers;
 | 
						|
 | 
						|
namespace funcexp
 | 
						|
{
 | 
						|
CalpontSystemCatalog::ColType Func_json_insert::operationType(FunctionParm& fp,
 | 
						|
                                                              CalpontSystemCatalog::ColType& /*resultType*/)
 | 
						|
{
 | 
						|
  return fp[0]->data()->resultType();
 | 
						|
}
 | 
						|
 | 
						|
std::string Func_json_insert::getStrVal(rowgroup::Row& row, FunctionParm& fp, bool& isNull,
 | 
						|
                                        execplan::CalpontSystemCatalog::ColType& /*type*/)
 | 
						|
{
 | 
						|
  const auto& js = fp[0]->data()->getStrVal(row, isNull);
 | 
						|
  if (isNull)
 | 
						|
    return "";
 | 
						|
 | 
						|
  const bool isInsertMode = mode == INSERT || mode == SET;
 | 
						|
  const bool isReplaceMode = mode == REPLACE || mode == SET;
 | 
						|
 | 
						|
  int jsErr = 0;
 | 
						|
  json_string_t keyName;
 | 
						|
  const CHARSET_INFO* cs = getCharset(fp[0]);
 | 
						|
  json_string_set_cs(&keyName, cs);
 | 
						|
 | 
						|
  // Save the result of each merge and the result of the final merge separately
 | 
						|
  std::string retJS;
 | 
						|
  utils::NullString tmpJS(js);
 | 
						|
  for (size_t i = 1, j = 0; i < fp.size(); i += 2, j++)
 | 
						|
  {
 | 
						|
    const char* rawJS = tmpJS.str();
 | 
						|
    const size_t jsLen = tmpJS.length();
 | 
						|
 | 
						|
    JSONPath& path = paths[j];
 | 
						|
    const json_path_step_t* lastStep;
 | 
						|
    const char* valEnd;
 | 
						|
 | 
						|
    if (!path.parsed)
 | 
						|
    {
 | 
						|
      if (parseJSPath(path, row, fp[i], false))
 | 
						|
        goto error;
 | 
						|
 | 
						|
#if MYSQL_VERSION_ID >= 120200
 | 
						|
      path.p.last_step_idx--;
 | 
						|
#else
 | 
						|
      path.p.last_step--;
 | 
						|
#endif
 | 
						|
    }
 | 
						|
 | 
						|
    initJSEngine(jsEg, cs, tmpJS);
 | 
						|
 | 
						|
#if MYSQL_VERSION_ID >= 120200
 | 
						|
    if (path.p.last_step_idx < 0)
 | 
						|
#else
 | 
						|
    if (path.p.last_step < path.p.steps)
 | 
						|
#endif
 | 
						|
      goto v_found;
 | 
						|
 | 
						|
#if MYSQL_VERSION_ID >= 120200
 | 
						|
    if (path.p.last_step_idx >= 0 && locateJSPath(jsEg, path, &jsErr))
 | 
						|
#else
 | 
						|
    if (path.p.last_step >= path.p.steps && locateJSPath(jsEg, path, &jsErr))
 | 
						|
#endif
 | 
						|
    {
 | 
						|
      if (jsErr)
 | 
						|
        goto error;
 | 
						|
      continue;
 | 
						|
    }
 | 
						|
 | 
						|
    if (json_read_value(&jsEg))
 | 
						|
      goto error;
 | 
						|
 | 
						|
#if MYSQL_VERSION_ID >= 120200
 | 
						|
    if (path.p.last_step_idx < 0)
 | 
						|
    {
 | 
						|
      lastStep= (reinterpret_cast<json_path_step_t*>
 | 
						|
                        (mem_root_dynamic_array_get_val(&path.p.steps,
 | 
						|
                             0))) + path.p.last_step_idx + 1;
 | 
						|
    }
 | 
						|
    else
 | 
						|
    {
 | 
						|
      lastStep= (reinterpret_cast<json_path_step_t*>
 | 
						|
                        (mem_root_dynamic_array_get_val(&path.p.steps,
 | 
						|
                             path.p.last_step_idx))) + 1;
 | 
						|
    }
 | 
						|
#else
 | 
						|
    lastStep = path.p.last_step + 1;
 | 
						|
#endif
 | 
						|
 | 
						|
    if (lastStep->type & JSON_PATH_ARRAY)
 | 
						|
    {
 | 
						|
      IntType itemSize = 0;
 | 
						|
 | 
						|
      if (jsEg.value_type != JSON_VALUE_ARRAY)
 | 
						|
      {
 | 
						|
        const uchar* valStart = jsEg.value_begin;
 | 
						|
        bool isArrAutoWrap;
 | 
						|
 | 
						|
        if (isInsertMode)
 | 
						|
        {
 | 
						|
          if (isReplaceMode)
 | 
						|
            isArrAutoWrap = lastStep->n_item > 0;
 | 
						|
          else
 | 
						|
          {
 | 
						|
            if (lastStep->n_item == 0)
 | 
						|
              continue;
 | 
						|
            isArrAutoWrap = true;
 | 
						|
          }
 | 
						|
        }
 | 
						|
        else
 | 
						|
        {
 | 
						|
          if (lastStep->n_item)
 | 
						|
            continue;
 | 
						|
          isArrAutoWrap = false;
 | 
						|
        }
 | 
						|
 | 
						|
        retJS.clear();
 | 
						|
        /* Wrap the value as an array. */
 | 
						|
        retJS.append(rawJS, (const char*)valStart - rawJS);
 | 
						|
        if (isArrAutoWrap)
 | 
						|
          retJS.append("[");
 | 
						|
 | 
						|
        if (jsEg.value_type == JSON_VALUE_OBJECT)
 | 
						|
        {
 | 
						|
          if (json_skip_level(&jsEg))
 | 
						|
            goto error;
 | 
						|
        }
 | 
						|
 | 
						|
        if (isArrAutoWrap)
 | 
						|
          retJS.append((const char*)valStart, jsEg.s.c_str - valStart);
 | 
						|
        retJS.append(", ");
 | 
						|
        if (appendJSValue(retJS, cs, row, fp[i + 1]))
 | 
						|
          goto error;
 | 
						|
        if (isArrAutoWrap)
 | 
						|
          retJS.append("]");
 | 
						|
        retJS.append((const char*)jsEg.s.c_str, rawJS + jsLen - (const char*)jsEg.s.c_str);
 | 
						|
 | 
						|
        goto continue_point;
 | 
						|
      }
 | 
						|
 | 
						|
      while (json_scan_next(&jsEg) == 0 && jsEg.state != JST_ARRAY_END)
 | 
						|
      {
 | 
						|
        switch (jsEg.state)
 | 
						|
        {
 | 
						|
          case JST_VALUE:
 | 
						|
            if (itemSize == lastStep->n_item)
 | 
						|
              goto v_found;
 | 
						|
            itemSize++;
 | 
						|
            if (json_skip_array_item(&jsEg))
 | 
						|
              goto error;
 | 
						|
            break;
 | 
						|
          default: break;
 | 
						|
        }
 | 
						|
      }
 | 
						|
 | 
						|
      if (unlikely(jsEg.s.error))
 | 
						|
        goto error;
 | 
						|
 | 
						|
      if (!isInsertMode)
 | 
						|
        continue;
 | 
						|
 | 
						|
      valEnd = (const char*)(jsEg.s.c_str - jsEg.sav_c_len);
 | 
						|
      retJS.clear();
 | 
						|
      retJS.append(rawJS, valEnd - rawJS);
 | 
						|
      if (itemSize > 0)
 | 
						|
        retJS.append(", ");
 | 
						|
      if (appendJSValue(retJS, cs, row, fp[i + 1]))
 | 
						|
        goto error;
 | 
						|
      retJS.append(valEnd, rawJS + jsLen - valEnd);
 | 
						|
    }
 | 
						|
    else /*JSON_PATH_KEY*/
 | 
						|
    {
 | 
						|
      IntType keySize = 0;
 | 
						|
 | 
						|
      if (jsEg.value_type != JSON_VALUE_OBJECT)
 | 
						|
        continue;
 | 
						|
 | 
						|
      while (json_scan_next(&jsEg) == 0 && jsEg.state != JST_OBJ_END)
 | 
						|
      {
 | 
						|
        switch (jsEg.state)
 | 
						|
        {
 | 
						|
          case JST_KEY:
 | 
						|
            json_string_set_str(&keyName, lastStep->key, lastStep->key_end);
 | 
						|
            if (json_key_matches(&jsEg, &keyName))
 | 
						|
              goto v_found;
 | 
						|
            keySize++;
 | 
						|
            if (json_skip_key(&jsEg))
 | 
						|
              goto error;
 | 
						|
            break;
 | 
						|
          default: break;
 | 
						|
        }
 | 
						|
      }
 | 
						|
 | 
						|
      if (unlikely(jsEg.s.error))
 | 
						|
        goto error;
 | 
						|
 | 
						|
      if (!isInsertMode)
 | 
						|
        continue;
 | 
						|
 | 
						|
      valEnd = (const char*)(jsEg.s.c_str - jsEg.sav_c_len);
 | 
						|
 | 
						|
      retJS.clear();
 | 
						|
      retJS.append(rawJS, valEnd - rawJS);
 | 
						|
 | 
						|
      if (keySize > 0)
 | 
						|
        retJS.append(", ");
 | 
						|
 | 
						|
      retJS.append("\"");
 | 
						|
      retJS.append((const char*)lastStep->key, lastStep->key_end - lastStep->key);
 | 
						|
      retJS.append("\":");
 | 
						|
 | 
						|
      if (appendJSValue(retJS, cs, row, fp[i + 1]))
 | 
						|
        goto error;
 | 
						|
      retJS.append(valEnd, rawJS + jsLen - valEnd);
 | 
						|
    }
 | 
						|
 | 
						|
    goto continue_point;
 | 
						|
 | 
						|
  v_found:
 | 
						|
 | 
						|
    if (!isReplaceMode)
 | 
						|
      continue;
 | 
						|
 | 
						|
    if (json_read_value(&jsEg))
 | 
						|
      goto error;
 | 
						|
 | 
						|
    valEnd = (const char*)jsEg.value_begin;
 | 
						|
    retJS.clear();
 | 
						|
    if (!json_value_scalar(&jsEg))
 | 
						|
    {
 | 
						|
      if (json_skip_level(&jsEg))
 | 
						|
        goto error;
 | 
						|
    }
 | 
						|
 | 
						|
    retJS.append(rawJS, valEnd - rawJS);
 | 
						|
    if (appendJSValue(retJS, cs, row, fp[i + 1]))
 | 
						|
      goto error;
 | 
						|
    retJS.append((const char*)jsEg.s.c_str, rawJS + jsLen - (const char*)jsEg.s.c_str);
 | 
						|
 | 
						|
  continue_point:
 | 
						|
    // tmpJS save the json string for next loop
 | 
						|
    tmpJS.assign(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
 |