Skip to content

Commit

Permalink
- Add new Json UDF's Json_Array_Add, Json_Array_Grp and Json_Object_Grp.
Browse files Browse the repository at this point in the history
  Handle longjmp's raised during json processing.
modified:
  storage/connect/global.h
  storage/connect/ha_connect.cc
  storage/connect/json.cpp
  storage/connect/jsonudf.cpp

- Fix wrong references to the suppressed g->Trace variables.
modified:
  storage/connect/global.h
  storage/connect/plugutil.c
  storage/connect/tabjson.cpp
  storage/connect/tabodbc.cpp
  • Loading branch information
Buggynours committed Feb 22, 2015
1 parent d9175f3 commit a736e63
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 23 deletions.
2 changes: 1 addition & 1 deletion storage/connect/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ typedef struct _global { /* Global structure */
void *Xchk; /* indexes in create/alter */
short Alchecked; /* Checked for ALTER */
short Mrr; /* True when doing mrr */
short Trace;
int N; /* Utility */
int jump_level;
jmp_buf jumper[MAX_JUMP + 2];
} GLOBAL;
Expand Down
16 changes: 13 additions & 3 deletions storage/connect/ha_connect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,10 @@
/***********************************************************************/
/* Initialize the ha_connect static members. */
/***********************************************************************/
#define SZCONV 8192
#define SZWORK 67108864 // Default work area size 64M
#define SZWMIN 4194304 // Minimum work area size 4M
#define SZCONV 8192
#define SZWORK 67108864 // Default work area size 64M
#define SZWMIN 4194304 // Minimum work area size 4M
#define JSONMAX 10 // JSON Default max grp size

extern "C" {
char version[]= "Version 1.03.0006 February 06, 2015";
Expand Down Expand Up @@ -217,6 +218,7 @@ bool ExactInfo(void);
USETEMP UseTemp(void);
int GetConvSize(void);
TYPCONV GetTypeConv(void);
uint GetJsonGrpSize(void);
uint GetWorkSize(void);
void SetWorkSize(uint);
extern "C" const char *msglang(void);
Expand Down Expand Up @@ -323,6 +325,12 @@ static MYSQL_THDVAR_ENUM(
0, // def (no)
&xconv_typelib); // typelib

// Estimate max number of rows for JSON aggregate functions
static MYSQL_THDVAR_UINT(json_grp_size,
PLUGIN_VAR_RQCMDARG, // opt
"max number of rows for JSON aggregate functions.",
NULL, NULL, JSONMAX, 1, INT_MAX, 1);

#if defined(XMSG) || defined(NEWMSG)
const char *language_names[]=
{
Expand Down Expand Up @@ -353,6 +361,7 @@ bool ExactInfo(void) {return THDVAR(current_thd, exact_info);}
USETEMP UseTemp(void) {return (USETEMP)THDVAR(current_thd, use_tempfile);}
int GetConvSize(void) {return THDVAR(current_thd, conv_size);}
TYPCONV GetTypeConv(void) {return (TYPCONV)THDVAR(current_thd, type_conv);}
uint GetJsonGrpSize(void) {return THDVAR(current_thd, json_grp_size);}
uint GetWorkSize(void) {return THDVAR(current_thd, work_size);}
void SetWorkSize(uint n)
{
Expand Down Expand Up @@ -6516,6 +6525,7 @@ static struct st_mysql_sys_var* connect_system_variables[]= {
#if defined(XMSG)
MYSQL_SYSVAR(errmsg_dir_path),
#endif // XMSG
MYSQL_SYSVAR(json_grp_size),
NULL
};

Expand Down
6 changes: 6 additions & 0 deletions storage/connect/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ PJSON ParseJson(PGLOBAL g, char *s, int len, int pretty, bool *comma)
goto err;
} else if (!(jsp = ParseObject(g, ++i, src)))
goto err;

break;
case ' ':
case '\t':
Expand All @@ -90,6 +91,11 @@ PJSON ParseJson(PGLOBAL g, char *s, int len, int pretty, bool *comma)

sprintf(g->Message, "Unexpected ',' (pretty=%d)", pretty);
goto err;
case '"':
if (!(jsp = ParseValue(g, i, src)))
goto err;

break;
case '(':
b = true;
break;
Expand Down
112 changes: 100 additions & 12 deletions storage/connect/jsonudf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@
/* Include relevant sections of the MariaDB header file. */
/***********************************************************************/
#include <my_global.h>
#include <mysqld.h>
#include <mysql.h>
#include <sql_error.h>

#include "global.h"
#include "plgdbsem.h"
#include "json.h"

#define MEMFIX 512

uint GetJsonGrpSize(void);

extern "C" {
DllExport my_bool Json_Value_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char *Json_Value(UDF_INIT*, UDF_ARGS*, char*,
Expand All @@ -23,6 +29,10 @@ DllExport my_bool Json_Array_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char *Json_Array(UDF_INIT*, UDF_ARGS*, char*,
unsigned long*, char *, char *);
DllExport void Json_Array_deinit(UDF_INIT*);
DllExport my_bool Json_Array_Add_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char *Json_Array_Add(UDF_INIT*, UDF_ARGS*, char*,
unsigned long*, char *, char *);
DllExport void Json_Array_Add_deinit(UDF_INIT*);
DllExport my_bool Json_Object_init(UDF_INIT*, UDF_ARGS*, char*);
DllExport char *Json_Object(UDF_INIT*, UDF_ARGS*, char*,
unsigned long*, char *, char *);
Expand All @@ -44,8 +54,8 @@ DllExport void Json_Object_Grp_deinit(UDF_INIT*);
/***********************************************************************/
/* Allocate and initialise the memory area. */
/***********************************************************************/
static my_bool JsonInit(UDF_INIT *initid, char *message, unsigned long reslen,
unsigned long memlen)
static my_bool JsonInit(UDF_INIT *initid, char *message,
unsigned long reslen, unsigned long memlen)
{
PGLOBAL g = PlugInit(NULL, memlen);

Expand Down Expand Up @@ -119,7 +129,7 @@ static my_bool CalcLen(UDF_ARGS *args, my_bool obj,
} // endfor i

// Calculate the amount of memory needed
memlen = 1024 + sizeof(JOUTSTR) + reslen;
memlen = MEMFIX + sizeof(JOUTSTR) + reslen;

for (i = 0; i < args->arg_count; i++) {
memlen += (args->lengths[i] + sizeof(JVALUE));
Expand Down Expand Up @@ -219,14 +229,23 @@ static PSZ MakeKey(PGLOBAL g, UDF_ARGS *args, int i)
static PJVAL MakeValue(PGLOBAL g, UDF_ARGS *args, int i)
{
char *sap = args->args[i];
PJSON jsp;
PJVAL jvp = new(g) JVALUE;

if (sap) switch (args->arg_type[i]) {
case STRING_RESULT:
if (args->lengths[i]) {
if (IsJson(args, i))
jvp->SetValue(ParseJson(g, sap, args->lengths[i], 0));
else
if (IsJson(args, i)) {
if (!(jsp = ParseJson(g, sap, args->lengths[i], 0)))
push_warning(current_thd, Sql_condition::WARN_LEVEL_WARN, 0,
g->Message);

if (jsp && jsp->GetType() == TYPE_JVAL)
jvp = (PJVAL)jsp;
else
jvp->SetValue(jsp);

} else
jvp->SetString(g, MakePSZ(g, args, i));

} // endif str
Expand Down Expand Up @@ -328,6 +347,59 @@ void Json_Array_deinit(UDF_INIT* initid)
PlugExit((PGLOBAL)initid->ptr);
} // end of Json_Array_deinit

/***********************************************************************/
/* Add values to a Json array. */
/***********************************************************************/
my_bool Json_Array_Add_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
unsigned long reslen, memlen;

if (args->arg_count < 2) {
strcpy(message, "Json_Value_Add must have at least 2 arguments");
return true;
} else if (!IsJson(args, 0)) {
strcpy(message, "Json_Value_Add first argument must be a json array");
return true;
} else
CalcLen(args, false, reslen, memlen);

return JsonInit(initid, message, reslen, memlen);
} // end of Json_Array_Add_init

char *Json_Array_Add(UDF_INIT *initid, UDF_ARGS *args, char *result,
unsigned long *res_length, char *is_null, char *error)
{
char *str;
PJVAL jvp;
PJAR arp;
PGLOBAL g = (PGLOBAL)initid->ptr;

PlugSubSet(g, g->Sarea, g->Sarea_Size);
jvp = MakeValue(g, args, 0);

if (jvp->GetValType() != TYPE_JAR) {
arp = new(g) JARRAY;
arp->AddValue(g, jvp);
} else
arp = jvp->GetArray();

for (uint i = 1; i < args->arg_count; i++)
arp->AddValue(g, MakeValue(g, args, i));

arp->InitArray(g);

if (!(str = Serialize(g, arp, NULL, 0)))
str = strcpy(result, g->Message);

*res_length = strlen(str);
return str;
} // end of Json_Array_Add

void Json_Array_Add_deinit(UDF_INIT* initid)
{
PlugExit((PGLOBAL)initid->ptr);
} // end of Json_Array_Add_deinit

/***********************************************************************/
/* Make a Json Oject containing all the parameters. */
/***********************************************************************/
Expand Down Expand Up @@ -370,7 +442,7 @@ void Json_Object_deinit(UDF_INIT* initid)
/***********************************************************************/
my_bool Json_Array_Grp_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
unsigned long reslen, memlen, n = 10;
unsigned long reslen, memlen, n = GetJsonGrpSize();

if (args->arg_count != 1) {
strcpy(message, "Json_Array_Grp can only accept 1 argument");
Expand All @@ -379,7 +451,7 @@ my_bool Json_Array_Grp_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
CalcLen(args, false, reslen, memlen);

reslen *= n;
memlen *= n;
memlen += ((memlen - MEMFIX) * (n - 1));

if (JsonInit(initid, message, reslen, memlen))
return true;
Expand All @@ -388,6 +460,7 @@ my_bool Json_Array_Grp_init(UDF_INIT *initid, UDF_ARGS *args, char *message)

PlugSubSet(g, g->Sarea, g->Sarea_Size);
g->Activityp = (PACTIVITY)new(g) JARRAY;
g->N = (int)n;
return false;
} // end of Json_Array_Grp_init

Expand All @@ -397,7 +470,9 @@ void Json_Array_Grp_add(UDF_INIT *initid, UDF_ARGS *args,
PGLOBAL g = (PGLOBAL)initid->ptr;
PJAR arp = (PJAR)g->Activityp;

arp->AddValue(g, MakeValue(g, args, 0));
if (g->N-- > 0)
arp->AddValue(g, MakeValue(g, args, 0));

} // end of Json_Array_Grp_add

char *Json_Array_Grp(UDF_INIT *initid, UDF_ARGS *args, char *result,
Expand All @@ -407,6 +482,10 @@ char *Json_Array_Grp(UDF_INIT *initid, UDF_ARGS *args, char *result,
PGLOBAL g = (PGLOBAL)initid->ptr;
PJAR arp = (PJAR)g->Activityp;

if (g->N < 0)
push_warning(current_thd, Sql_condition::WARN_LEVEL_WARN, 0,
"Result truncated to json_grp_size values");

arp->InitArray(g);

if (!(str = Serialize(g, arp, NULL, 0)))
Expand All @@ -422,6 +501,7 @@ void Json_Array_Grp_clear(UDF_INIT *initid, char *is_null, char *error)

PlugSubSet(g, g->Sarea, g->Sarea_Size);
g->Activityp = (PACTIVITY)new(g) JARRAY;
g->N = GetJsonGrpSize();
} // end of Json_Array_Grp_clear

void Json_Array_Grp_deinit(UDF_INIT* initid)
Expand All @@ -434,7 +514,7 @@ void Json_Array_Grp_deinit(UDF_INIT* initid)
/***********************************************************************/
my_bool Json_Object_Grp_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
unsigned long reslen, memlen, n = 10;
unsigned long reslen, memlen, n = GetJsonGrpSize();

if (args->arg_count != 2) {
strcpy(message, "Json_Array_Grp can only accept 2 argument");
Expand All @@ -443,7 +523,7 @@ my_bool Json_Object_Grp_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
CalcLen(args, true, reslen, memlen);

reslen *= n;
memlen *= n;
memlen += ((memlen - MEMFIX) * (n - 1));

if (JsonInit(initid, message, reslen, memlen))
return true;
Expand All @@ -452,6 +532,7 @@ my_bool Json_Object_Grp_init(UDF_INIT *initid, UDF_ARGS *args, char *message)

PlugSubSet(g, g->Sarea, g->Sarea_Size);
g->Activityp = (PACTIVITY)new(g) JOBJECT;
g->N = (int)n;
return false;
} // end of Json_Object_Grp_init

Expand All @@ -461,7 +542,9 @@ void Json_Object_Grp_add(UDF_INIT *initid, UDF_ARGS *args,
PGLOBAL g = (PGLOBAL)initid->ptr;
PJOB objp = (PJOB)g->Activityp;

objp->SetValue(g, MakeValue(g, args, 0), MakePSZ(g, args, 1));
if (g->N-- > 0)
objp->SetValue(g, MakeValue(g, args, 0), MakePSZ(g, args, 1));

} // end of Json_Object_Grp_add

char *Json_Object_Grp(UDF_INIT *initid, UDF_ARGS *args, char *result,
Expand All @@ -471,6 +554,10 @@ char *Json_Object_Grp(UDF_INIT *initid, UDF_ARGS *args, char *result,
PGLOBAL g = (PGLOBAL)initid->ptr;
PJOB objp = (PJOB)g->Activityp;

if (g->N < 0)
push_warning(current_thd, Sql_condition::WARN_LEVEL_WARN, 0,
"Result truncated to json_grp_size values");

if (!(str = Serialize(g, objp, NULL, 0)))
str = strcpy(result, g->Message);

Expand All @@ -484,6 +571,7 @@ void Json_Object_Grp_clear(UDF_INIT *initid, char *is_null, char *error)

PlugSubSet(g, g->Sarea, g->Sarea_Size);
g->Activityp = (PACTIVITY)new(g) JOBJECT;
g->N = GetJsonGrpSize();
} // end of Json_Object_Grp_clear

void Json_Object_Grp_deinit(UDF_INIT* initid)
Expand Down
2 changes: 1 addition & 1 deletion storage/connect/plgdbutl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ PQRYRES PlgAllocResult(PGLOBAL g, int ncol, int maxres, int ids,
else
crp->Kdata = NULL;

if (g->Trace)
if (trace)
htrc("Column(%d) %s type=%d len=%d value=%p\n",
crp->Ncol, crp->Name, crp->Type, crp->Length, crp->Kdata);

Expand Down
2 changes: 1 addition & 1 deletion storage/connect/plugutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ PGLOBAL PlugInit(LPCSTR Language, uint worksize)
return NULL;
} else {
g->Sarea_Size = worksize;
g->Trace = 0;
g->Createas = 0;
g->Alchecked = 0;
g->Mrr = 0;
g->Activityp = g->ActivityStart = NULL;
g->Xchk = NULL;
g->N = 0;
strcpy(g->Message, "");

/*******************************************************************/
Expand Down
6 changes: 5 additions & 1 deletion storage/connect/tabjson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,11 @@ void JSONCOL::WriteColumn(PGLOBAL g)
case TYPE_STRING:
if (Nodes[Nod-1].Op == OP_XX) {
s = Value->GetCharValue();
jsp = ParseJson(g, s, (int)strlen(s), 0);

if (!(jsp = ParseJson(g, s, (int)strlen(s), 0))) {
strcpy(g->Message, s);
longjmp(g->jumper[g->jump_level], 666);
} // endif jsp

if (arp) {
if (Nod > 1 && Nodes[Nod-2].Rank)
Expand Down
8 changes: 4 additions & 4 deletions storage/connect/tabodbc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ bool TDBODBC::OpenDB(PGLOBAL g)
{
bool rc = true;

if (g->Trace)
if (trace)
htrc("ODBC OpenDB: tdbp=%p tdb=R%d use=%dmode=%d\n",
this, Tdb_No, Use, Mode);

Expand Down Expand Up @@ -1185,12 +1185,12 @@ void ODBCCOL::ReadColumn(PGLOBAL g)

} // endif Buf_Type

if (g->Trace) {
if (trace) {
char buf[64];

htrc("ODBC Column %s: rows=%d buf=%p type=%d value=%s\n",
Name, tdbp->Rows, Bufp, Buf_Type, Value->GetCharString(buf));
} // endif Trace
} // endif trace

put:
if (tdbp->Memory != 2)
Expand Down Expand Up @@ -1424,7 +1424,7 @@ bool TDBXDBC::OpenDB(PGLOBAL g)
{
bool rc = false;

if (g->Trace)
if (trace)
htrc("ODBC OpenDB: tdbp=%p tdb=R%d use=%dmode=%d\n",
this, Tdb_No, Use, Mode);

Expand Down

0 comments on commit a736e63

Please sign in to comment.