Changeset: 5773807c86db for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=5773807c86db
Modified Files:
monetdb5/optimizer/opt_mergetable.c
Branch: Jul2015
Log Message:
use a mat_list structure in the merge table optimizer. This prevents out off
bounds access. Thanks go to richard.
diffs (truncated from 933 to 300 lines):
diff --git a/monetdb5/optimizer/opt_mergetable.c
b/monetdb5/optimizer/opt_mergetable.c
--- a/monetdb5/optimizer/opt_mergetable.c
+++ b/monetdb5/optimizer/opt_mergetable.c
@@ -30,6 +30,12 @@ typedef struct mat {
int pushed;
} mat_t;
+typedef struct matlist {
+ mat_t *v;
+ int top;
+ int size;
+} matlist_t;
+
static mat_type_t
mat_type( mat_t *mat, int n)
{
@@ -40,20 +46,20 @@ mat_type( mat_t *mat, int n)
}
static int
-is_a_mat(int idx, mat_t *mat, int top){
+is_a_mat(int idx, matlist_t *ml){
int i;
- for(i =0; i<top; i++)
- if (mat[i].mv == idx)
+ for(i =0; i<ml->top; i++)
+ if (ml->v[i].mv == idx)
return i;
return -1;
}
static int
-nr_of_mats(InstrPtr p, mat_t *mat, int mtop)
+nr_of_mats(InstrPtr p, matlist_t *ml)
{
int j,cnt=0;
for(j=p->retc; j<p->argc; j++)
- if (is_a_mat(getArg(p,j), mat, mtop) >= 0)
+ if (is_a_mat(getArg(p,j), ml) >= 0)
cnt++;
return cnt;
}
@@ -68,35 +74,41 @@ nr_of_bats(MalBlkPtr mb, InstrPtr p)
return cnt;
}
-inline static int
-mat_add(mat_t *mat, int mtop, InstrPtr q, mat_type_t type, char *func)
+/* some mat's have intermediates (with intermediate result variables), therefor
+ * we pass the old output mat variable */
+inline static void
+mat_add_var(matlist_t *ml, InstrPtr q, InstrPtr p, int var, mat_type_t type,
int inputmat, int parentmat)
{
- mat[mtop].mi = q;
- mat[mtop].org = NULL;
- mat[mtop].mv = getArg(q,0);
- mat[mtop].type = type;
- mat[mtop].pm = -1;
- mat[mtop].packed = 0;
- mat[mtop].pushed = 0;
- (void)func;
- //printf (" mtop %d %s\n", mtop, func);
- return mtop+1;
+ mat_t *dst = &ml->v[ml->top];
+ if (ml->top == ml->size) {
+ int s = ml->size * 2;
+ mat_t *v = (mat_t*)GDKzalloc(s * sizeof(mat_t));
+ if (!v)
+ return; /* FIXME: error checking */
+ memcpy(v, ml->v, ml->top * sizeof(mat_t));
+ GDKfree(ml->v);
+ ml->size = s;
+ ml->v = v;
+ dst = &ml->v[ml->top];
+ }
+ dst->mi = q;
+ dst->org = p;
+ dst->mv = var;
+ dst->type = type;
+ dst->im = inputmat;
+ dst->pm = parentmat;
+ dst->packed = 0;
+ dst->pushed = 1;
+ ++ml->top;
}
-/* some mat's have intermediates (with intermediate result variables), therefor
- * we pass the old output mat variable */
-inline static int
-mat_add_var(mat_t *mat, int mtop, InstrPtr q, InstrPtr p, int var, mat_type_t
type, int inputmat, int parentmat)
+inline static void
+mat_add(matlist_t *ml, InstrPtr q, mat_type_t type, char *func)
{
- mat[mtop].mi = q;
- mat[mtop].org = p;
- mat[mtop].mv = var;
- mat[mtop].type = type;
- mat[mtop].im = inputmat;
- mat[mtop].pm = parentmat;
- mat[mtop].packed = 0;
- mat[mtop].pushed = 1;
- return mtop+1;
+ mat_add_var(ml, q, NULL, getArg(q,0), type, -1, -1);
+ ml->v[ml->top-1].pushed = 0;
+ (void)func;
+ //printf (" ml.top %d %s\n", ml.top, func);
}
static void
@@ -279,16 +291,17 @@ mat_delta(MalBlkPtr mb, InstrPtr p, mat_
static InstrPtr
-mat_apply1(MalBlkPtr mb, InstrPtr p, mat_t *mat, int mtop, int m, int var)
+mat_apply1(MalBlkPtr mb, InstrPtr p, matlist_t *ml, int m, int var)
{
int tpe, k, is_select = isSubSelect(p), is_mirror = (getFunctionId(p)
== mirrorRef);
int is_identity = (getFunctionId(p) == identityRef && getModuleId(p) ==
batcalcRef);
int ident_var = 0, is_assign = (getFunctionId(p) == NULL), n = 0;
InstrPtr r = NULL, q;
+ mat_t *mat = ml->v;
/* Find the mat we overwrite */
if (is_assign) {
- n = is_a_mat(getArg(p, 0), mat, mtop);
+ n = is_a_mat(getArg(p, 0), ml);
is_assign = (n >= 0);
}
@@ -399,11 +412,12 @@ mat_apply3(MalBlkPtr mb, InstrPtr p, mat
}
-static int
-mat_setop(MalBlkPtr mb, InstrPtr p, mat_t *mat, int mtop, int m, int n)
+static void
+mat_setop(MalBlkPtr mb, InstrPtr p, matlist_t *ml, int m, int n)
{
int tpe = getArgType(mb,p, 0), k, j;
InstrPtr r = newInstruction(mb, ASSIGNsymbol);
+ mat_t *mat = ml->v;
setModuleId(r,matRef);
setFunctionId(r,packRef);
@@ -451,15 +465,15 @@ mat_setop(MalBlkPtr mb, InstrPtr p, mat_
}
}
- mtop = mat_add(mat, mtop, r, mat_none, getFunctionId(p));
- return mtop;
+ mat_add(ml, r, mat_none, getFunctionId(p));
}
-static int
-mat_leftfetchjoin(MalBlkPtr mb, InstrPtr p, mat_t *mat, int mtop, int m, int n)
+static void
+mat_leftfetchjoin(MalBlkPtr mb, InstrPtr p, matlist_t *ml, int m, int n)
{
int tpe = getArgType(mb,p, 0), k, j;
InstrPtr r = newInstruction(mb, ASSIGNsymbol);
+ mat_t *mat = ml->v;
setModuleId(r,matRef);
setFunctionId(r,packRef);
@@ -501,16 +515,16 @@ mat_leftfetchjoin(MalBlkPtr mb, InstrPtr
}
}
- mtop = mat_add(mat, mtop, r, mat_none, getFunctionId(p));
- return mtop;
+ mat_add(ml, r, mat_none, getFunctionId(p));
}
-static int
-mat_join2(MalBlkPtr mb, InstrPtr p, mat_t *mat, int mtop, int m, int n)
+static void
+mat_join2(MalBlkPtr mb, InstrPtr p, matlist_t *ml, int m, int n)
{
int tpe = getArgType(mb,p, 0), j,k, nr = 1;
InstrPtr l = newInstruction(mb, ASSIGNsymbol);
InstrPtr r = newInstruction(mb, ASSIGNsymbol);
+ mat_t *mat = ml->v;
setModuleId(l,matRef);
setFunctionId(l,packRef);
@@ -564,17 +578,17 @@ mat_join2(MalBlkPtr mb, InstrPtr p, mat_
r = pushArgument(mb, r, getArg(q,1));
}
}
- mtop = mat_add(mat, mtop, l, mat_none, getFunctionId(p));
- mtop = mat_add(mat, mtop, r, mat_none, getFunctionId(p));
- return mtop;
+ mat_add(ml, l, mat_none, getFunctionId(p));
+ mat_add(ml, r, mat_none, getFunctionId(p));
}
-static int
-mat_join3(MalBlkPtr mb, InstrPtr p, mat_t *mat, int mtop, int m, int n, int o)
+static void
+mat_join3(MalBlkPtr mb, InstrPtr p, matlist_t *ml, int m, int n, int o)
{
int tpe = getArgType(mb,p, 0), j,k, nr = 1;
InstrPtr l = newInstruction(mb, ASSIGNsymbol);
InstrPtr r = newInstruction(mb, ASSIGNsymbol);
+ mat_t *mat = ml->v;
setModuleId(l,matRef);
setFunctionId(l,packRef);
@@ -632,9 +646,8 @@ mat_join3(MalBlkPtr mb, InstrPtr p, mat_
r = pushArgument(mb, r, getArg(q,1));
}
}
- mtop = mat_add(mat, mtop, l, mat_none, getFunctionId(p));
- mtop = mat_add(mat, mtop, r, mat_none, getFunctionId(p));
- return mtop;
+ mat_add(ml, l, mat_none, getFunctionId(p));
+ mat_add(ml, r, mat_none, getFunctionId(p));
}
@@ -799,12 +812,12 @@ walk_n_back(mat_t *mat, int g, int cnt)
}
static int
-group_by_ext(mat_t *mat, int mtop, int g)
+group_by_ext(matlist_t *ml, int g)
{
int i;
- for(i=g; i< mtop; i++){
- if (mat[i].pm == g)
+ for(i=g; i< ml->top; i++){
+ if (ml->v[i].pm == g)
return i;
}
return 0;
@@ -814,12 +827,13 @@ group_by_ext(mat_t *mat, int mtop, int g
* gext.leftfetchjoin(mat.pack(per partition ext.leftfetchjoins(x)))
*/
-static int
-mat_group_project(MalBlkPtr mb, InstrPtr p, mat_t *mat, int mtop, int e, int a)
+static void
+mat_group_project(MalBlkPtr mb, InstrPtr p, matlist_t *ml, int e, int a)
{
int tp = getArgType(mb,p,0), k;
int tail = getColumnType(tp);
InstrPtr ai1 = newInstruction(mb, ASSIGNsymbol), r;
+ mat_t *mat = ml->v;
setModuleId(ai1,matRef);
setFunctionId(ai1,packRef);
@@ -844,8 +858,7 @@ mat_group_project(MalBlkPtr mb, InstrPtr
getArg(r,2) = getArg(ai1,0);
pushInstruction(mb,r);
if (tail == TYPE_oid)
- mtop = mat_add_var(mat, mtop, ai1, r, getArg(r, 0), mat_ext,
-1, -1);
- return mtop;
+ mat_add_var(ml, ai1, r, getArg(r, 0), mat_ext, -1, -1);
}
/* Per partition aggregates are merged and aggregated together. For
@@ -988,15 +1001,16 @@ mat_group_aggr(MalBlkPtr mb, InstrPtr p,
* This is the input for the final second phase group by.
*/
static void
-mat_pack_group(MalBlkPtr mb, mat_t *mat, int mtop, int g)
+mat_pack_group(MalBlkPtr mb, matlist_t *ml, int g)
{
+ mat_t *mat = ml->v;
int cnt = chain_by_length(mat, g), i;
InstrPtr cur = NULL;
for(i=cnt-1; i>=0; i--) {
InstrPtr grp = newInstruction(mb, ASSIGNsymbol);
int ogrp = walk_n_back(mat, g, i);
- int oext = group_by_ext(mat, mtop, ogrp);
+ int oext = group_by_ext(ml, ogrp);
int attr = mat[oext].im;
setModuleId(grp,groupRef);
@@ -1019,26 +1033,26 @@ mat_pack_group(MalBlkPtr mb, mat_t *mat,
* e2.leftfetchjoin(grp.leftfetchjoin((ext.leftfetchjoin(b)))
* and one for the current group
*/
-static int
-mat_group_attr(MalBlkPtr mb, mat_t *mat, int mtop, int g, InstrPtr cext, int
push )
+static void
+mat_group_attr(MalBlkPtr mb, matlist_t *ml, int g, InstrPtr cext, int push )
{
- int cnt = chain_by_length(mat, g), i; /* number of attributes */
+ int cnt = chain_by_length(ml->v, g), i; /* number of attributes */
int ogrp = g; /* previous group */
for(i = 0; i < cnt; i++) {
- int agrp = walk_n_back(mat, ogrp, i);
- int b = mat[agrp].im;
- int aext = group_by_ext(mat, mtop, agrp);
- int a = mat[aext].im;
- int atp = getArgType(mb,mat[a].mi,0), k;
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list