Changeset: a09e99a0ed3b for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/a09e99a0ed3b
Modified Files:
        monetdb5/modules/kernel/vss.c
        monetdb5/optimizer/opt_mergetable.c
        monetdb5/optimizer/opt_support.c
        sql/backends/monet5/rel_bin.c
        sql/server/rel_exp.c
        sql/server/rel_select.c
Branch: nested
Log Message:

initial code for pushing bond through mitosis/mergetable


diffs (213 lines):

diff --git a/monetdb5/modules/kernel/vss.c b/monetdb5/modules/kernel/vss.c
--- a/monetdb5/modules/kernel/vss.c
+++ b/monetdb5/modules/kernel/vss.c
@@ -47,6 +47,7 @@ typedef struct bond_collection {
        BAT **dims;        /* array of dimension BATs (not owned, just 
referenced) */
        int ndims;         /* number of dimensions */
        int bsz;                           /* block size */
+       int wsz;                           /* block size */
        BUN nvecs;         /* number of vectors (rows) */
        dbl *dim_means;    /* mean value per dimension (for dimension ordering) 
*/
        oid *candidates;
@@ -143,6 +144,9 @@ bond_create(allocator *ma, BAT **dim_bat
                        bc->dims = ma_alloc(ma, ndims * sizeof(BAT *));
                        bc->dim_means = ma_alloc(ma, ndims * sizeof(dbl));
                        bc->nvecs = BATcount(dim_bats[0]);
+                       bc->wsz = SS;
+                       if (bc->nvecs < 100000)
+                               bc->wsz = 1;
                        for (int d = 0; d < ndims; d++) {
                                BAT *b = dim_bats[d];
                                bc->dims[d] = b;
@@ -252,7 +256,7 @@ bond_upper_bound(bond_collection *bc, co
 
        // calc full distance for the sample across all dimensions
        dbl res = 0;
-       for (int nr = 0; nr < SS; nr++) {
+       for (int nr = 0; nr < bc->wsz; nr++) {
                oid base = nr * bc->bsz;
                for (int i = 0; i < bc->bsz; i++) {
                        tc[i] = base + i;
@@ -275,19 +279,19 @@ bond_upper_bound(bond_collection *bc, co
                if (nr == 0) {
                        res = topn(tc, td, bc, bc->bsz, k);
                        for(BUN i = 0; i < k; i++) {
-                               cands [i] = bc->tcand[i];
+                               cands[i] = bc->tcand[i];
                                dists[i] = bc->tdist[i];
                        }
                } else {
                        res = topn_merge(cands, dists, bc->tc, bc->td, bc->bsz, 
k);
                        for(BUN i = 0; i < k; i++) {
-                               cands [i] = bc->tcand[i];
+                               cands[i] = bc->tcand[i];
                                dists[i] = bc->tdist[i];
                        }
                }
        }
        for (int d = 0; d < bc->ndims; d++) {
-               bc->dim_means[d] /= (bc->bsz*SS);
+               bc->dim_means[d] /= (bc->bsz*bc->wsz);
        }
        return res;
 }
@@ -305,7 +309,7 @@ bond_search_fast(bond_collection *bc, co
        BUN ncands = bc->nvecs, pruned = 0;
        oid *tc = bc->tc;
        dbl *td = bc->td;
-       for (BUN z = (SS*bc->bsz); z < ncands; z+=bc->bsz) {
+       for (BUN z = (bc->wsz*bc->bsz); z < ncands; z+=bc->bsz) {
                BUN end = z+bc->bsz > ncands?ncands:z+bc->bsz, j, i = 0;
                // initialize, all vectors are candidates
 
@@ -397,13 +401,14 @@ bond_search_fast(bond_collection *bc, co
        //printf("vectors t= " LLFMT " upper %F, pruned " BUNFMT " \n", 
GDKusec() - T0, bc->kth_upper, pruned);
 
        //T0 = GDKusec();
+       oid base = bc->dims[0]->hseqbase;
        BAT *koids = COLnew(0, TYPE_oid, k, TRANSIENT);
     BAT *kdist = COLnew(0, TYPE_dbl, k, TRANSIENT);
        // Final top k
        {
                BUN write_pos = 0;
                for (; write_pos < k; write_pos++) {
-                               *(oid*) Tloc(koids, write_pos) = 
bc->candidates[write_pos];
+                               *(oid*) Tloc(koids, write_pos) = 
bc->candidates[write_pos] + base;
                                *(dbl*) Tloc(kdist, write_pos) = 
bc->dists[write_pos];
                }
                k = write_pos;
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
@@ -2092,6 +2092,63 @@ mat_pack_topn(MalBlkPtr mb, InstrPtr slc
 }
 
 static int
+mat_topn_N(allocator *ma, MalBlkPtr mb, InstrPtr p, matlist_t *ml, int matches)
+{
+       int k, m, tpe = getArgType(mb, p, 0), tp2 = (p->retc == 
2)?getArgType(mb, p, 1):-1;
+
+       /* each input is mat! */
+       m = is_a_mat(getArg(p, p->retc+1), ml);
+       if (m < 0)
+               return -1;
+
+       /* dummy mat instruction (needed to share result of p) */
+       InstrPtr pck = newInstructionArgs(mb, matRef, packRef, 
ml->v[m].mi->argc), q;
+       if (pck == NULL)
+               return -1;
+       getArg(pck, 0) = getArg(p, 0);
+
+       InstrPtr pc2 = p->retc == 2?newInstructionArgs(mb, matRef, packRef, 
ml->v[m].mi->argc):NULL;
+       if (p->retc == 2 && pc2 == NULL)
+               return -1;
+       if (p->retc == 2)
+               getArg(pc2, 0) = getArg(p, 1);
+
+       for (k = 1; mb->errors == NULL && k < ml->v[m].mi->argc; k++) {
+               if ((q = copyInstruction(mb, p)) == NULL) {
+                       freeInstruction(mb, pck);
+                       if (pc2)
+                               freeInstruction(mb, pc2);
+                       return -1;
+               }
+               getArg(q, 0) = newTmpVariable(mb, tpe);
+               if (p->retc == 2)
+                       getArg(q, 1) = newTmpVariable(mb, tp2);
+               for(int i = 0; i<matches; i++) {
+                       int mv = is_a_mat(getArg(p, p->retc+1+i), ml);
+                       getArg(q, q->retc+1+i) = getArg(ml->v[mv].mi, k);
+               }
+
+               pushInstruction(mb, q);
+               pck = pushArgument(mb, pck, getArg(q, 0));
+               if (pc2)
+                       pc2 = pushArgument(mb, pc2, getArg(q, 1));
+       }
+       if (mb->errors || mat_add_var(ma, ml, pck, p, getArg(p, 0), mat_tpn, m, 
0, 0)) {
+               freeInstruction(mb, pck);
+               if (pc2)
+                       freeInstruction(mb, pck);
+               return -1;
+       }
+       if (pc2 && (mb->errors || mat_add_var(ma, ml, pc2, p, getArg(p, 1), 
mat_tpn, m, 0, 0))) {
+               freeInstruction(mb, pck);
+               freeInstruction(mb, pc2);
+               return -1;
+       }
+       /* now project and repeat */
+       return 0;
+}
+
+static int
 mat_topn(allocator *ma, MalBlkPtr mb, InstrPtr p, matlist_t *ml, int m, int n, 
int o)
 {
        int tpe = getArgType(mb, p, 0), k, is_slice = isSlice(p), no_offset = 
-1;
@@ -2558,6 +2615,19 @@ OPTmergetableImplementation(Client ctx, 
                        actions++;
                        continue;
                }
+               if (!distinct_topn && match >= 1 && bats == match && isTopn(p) 
&& p->retc >= 1) {
+                       bool matched = true;
+                       for (int i = 0; i<match && matched; i++)
+                               matched = (is_a_mat(getArg(p, p->retc+1+i), 
&ml) >= 0);
+                       if (matched) {
+                               if (mat_topn_N(ta, mb, p, &ml, match)) {
+                                       msg = createException(MAL, 
"optimizer.mergetable", SQLSTATE(HY013) MAL_MALLOC_FAIL);
+                                       goto cleanup;
+                               }
+                               actions++;
+                               continue;
+                       }
+               }
                if (!distinct_topn && match == 3 && bats == 3 && isTopn(p)
                        && ((m = is_a_mat(getArg(p, p->retc), &ml)) >= 0)
                        && ((n = is_a_mat(getArg(p, p->retc + 1), &ml)) >= 0)
diff --git a/monetdb5/optimizer/opt_support.c b/monetdb5/optimizer/opt_support.c
--- a/monetdb5/optimizer/opt_support.c
+++ b/monetdb5/optimizer/opt_support.c
@@ -467,7 +467,8 @@ isLikeOp(InstrPtr p)
 inline int
 isTopn(InstrPtr p)
 {
-       return ((getModuleId(p) == algebraRef && getFunctionId(p) == firstnRef)
+       return ((getFunctionId(p) && strcmp(getFunctionId(p), "bond") == 0) ||
+              (getModuleId(p) == algebraRef && getFunctionId(p) == firstnRef)
                        || isSlice(p));
 }
 
diff --git a/sql/backends/monet5/rel_bin.c b/sql/backends/monet5/rel_bin.c
--- a/sql/backends/monet5/rel_bin.c
+++ b/sql/backends/monet5/rel_bin.c
@@ -2854,7 +2854,7 @@ rel2bin_basetable(backend *be, sql_rel *
        sql_column *fcol = NULL;
        sql_idx *fi = NULL;
        list *l = sa_list(sql->sa);
-       bool complex = (t->multiset || t->composite);
+       bool complex = ((t->multiset && t->multiset != MS_VECTOR) || 
t->composite);
        stmt *dels = stmt_tid(be, t, !complex?rel->flag == 
REL_PARTITION:false), *col = NULL;
        node *en, *cn = NULL;
 
diff --git a/sql/server/rel_exp.c b/sql/server/rel_exp.c
--- a/sql/server/rel_exp.c
+++ b/sql/server/rel_exp.c
@@ -2434,8 +2434,6 @@ exp_has_aggr(sql_rel *r, sql_exp *e )
        case e_aggr:
                return true;
        case e_cmp:
-               if (e->card != CARD_ATOM)
-                       return false;
                if (e->flag == cmp_filter)
                        return exps_have_aggr(r, e->l) && exps_have_aggr(r, 
e->r);
                if (e->flag == cmp_con || e->flag == cmp_dis)
diff --git a/sql/server/rel_select.c b/sql/server/rel_select.c
--- a/sql/server/rel_select.c
+++ b/sql/server/rel_select.c
@@ -4604,7 +4604,6 @@ rel_selection_ref(sql_query *query, sql_
 
                                        nl = dlist_create(sa);
                                        exp_setname(query->sql, ve, NULL, name);
-                                       reset_intern(ve);
                                        /* now we should rewrite the selection 
such that it uses the new group by column */
                                        dlist_append_string(sa, nl, 
ma_strdup(sa, name));
                                        nsym = symbol_create_list(sa, to, nl);
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to