Changeset: a17d12702b82 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/a17d12702b82
Modified Files:
        sql/backends/monet5/sql.c
        sql/backends/monet5/sql_statement.c
        sql/test/nested/Tests/basic.test
Branch: nested
Log Message:

allow for array values and union of 2 array (multiset) tables.


diffs (235 lines):

diff --git a/sql/backends/monet5/sql.c b/sql/backends/monet5/sql.c
--- a/sql/backends/monet5/sql.c
+++ b/sql/backends/monet5/sql.c
@@ -1195,6 +1195,45 @@ mvc_next_value_ms(Client cntxt, MalBlkPt
        throw(SQL, "sql.next_value", SQLSTATE(HY050) "Cannot generate next 
sequence value %s.%s", sname, seqname);
 }
 
+static str
+mvc_next_value_ms_cntr(bat *res, int *rcntr, bat *in, int *icntr)
+{
+       BAT *b = BATdescriptor(*in);
+       if (!b)
+               return createException(SQL, "sql.next_value", SQLSTATE(HY002) 
RUNTIME_OBJECT_MISSING);
+       BUN card = BATcount(b);
+       BAT *r = NULL;
+
+       if (!(r = COLnew(b->hseqbase, TYPE_int, card, TRANSIENT)))
+               return createException(SQL, "sql.get_value", SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
+       int *ip = Tloc(b,0);
+       int *op = Tloc(r, 0);
+       int sn = *icntr, osn = sn;
+       bool nil_val = false;
+       for (BUN i = 0; i < card; i++) {
+               if (ip[i] < 0) {
+                       if (ip[i] == int_nil)
+                               nil_val = true;
+                       op[i] = ip[i];
+               } else {
+                       op[i] = sn++;
+               }
+       }
+       *rcntr = sn;
+       BATsetcount(r, card);
+       r->tnonil = !nil_val;
+       r->tnil = nil_val;
+       r->trevsorted = false;
+       if (((sn-osn) - (*icntr)) == 0)
+               r->tsorted = r->tkey = true;
+       else
+               r->tsorted = r->tkey = false;
+       *res = r->batCacheid;
+       BBPkeepref(r);
+       BBPreclaim(b);
+       return MAL_SUCCEED;
+}
+
 /* str mvc_get_value(lng *res, str *sname, str *seqname); */
 str
 mvc_get_value(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
@@ -6172,7 +6211,8 @@ static mel_func sql_init_funcs[] = {
  pattern("sql", "next_value_ms", mvc_next_value_ms, false, "return the next 
value of the sequence", args(1,4, arg("",int),argany("card",1), 
arg("sname",str),arg("sequence",str))),
  pattern("batsql", "next_value", mvc_next_value_bulk, true, "return the next 
value of the sequence", args(1,4, batarg("",lng),arg("card",lng), 
arg("sname",str),arg("sequence",str))),
  pattern("batsql", "next_value_ms", mvc_next_value_ms, false, "return the next 
value of the sequence", args(1,4, batarg("",int),batargany("card",1), 
arg("sname",str),arg("sequence",str))),
- //pattern("batsql", "next_value_ms", mvc_next_value_ms, false, "return the 
next value of the sequence", args(1,5, batarg("",lng),batargany("card",1), 
arg("sname",str),arg("sequence",str), batarg("cand",oid))),
+ //pattern("batsql", "next_value_ms", mvc_next_value_ms, false, "return the 
next value of the sequence", args(1,5, batarg("",lng),batargany("in",1), 
arg("sname",str),arg("sequence",str), batarg("cand",oid))),
+ command("batsql", "next_value_ms", mvc_next_value_ms_cntr, false, "return the 
next value", args(2,4, batarg("",int), arg("cntr", int), batargany("in",1), 
arg("icntr",int))),
  pattern("sql", "get_value", mvc_get_value, false, "return the current value 
of the sequence (ie the next to be used value)", args(1,3, 
arg("",lng),arg("sname",str),arg("sequence",str))),
  pattern("batsql", "get_value", mvc_get_value_bulk, false, "return the current 
value of the sequence (ie the next to be used value)", args(1,3, 
batarg("",lng),batarg("sname",str),batarg("sequence",str))),
  pattern("batsql", "get_value", mvc_get_value_bulk, false, "return the current 
value of the sequence (ie the next to be used value)", args(1,5, 
batarg("",lng),batarg("sname",str),batarg("sequence",str),batarg("s1",oid),batarg("s2",oid))),
diff --git a/sql/backends/monet5/sql_statement.c 
b/sql/backends/monet5/sql_statement.c
--- a/sql/backends/monet5/sql_statement.c
+++ b/sql/backends/monet5/sql_statement.c
@@ -3663,21 +3663,90 @@ stmt_append_bulk(backend *be, stmt *c, l
        return NULL;
 }
 
+static int
+renumber(backend *be, stmt *p, stmt *c)
+{
+       list *ms = unnest_stmt(c);
+       int len = list_length(ms) - 1;
+
+       /* next_value_ms + renumber */
+       stmt *rowids = ms->t->data;
+
+       /* call next_value_ms */
+       InstrPtr r = newStmt(be->mb, batsqlRef, "next_value_ms");
+       r = pushReturn(be->mb, r, newTmpVariable(be->mb, TYPE_int));
+       r = pushArgument(be->mb, r, rowids->nr);
+       if (p)
+               r = pushArgument(be->mb, r, p->flag);
+       else
+               r = pushInt(be->mb, r, 0); /* start from 1 */
+       pushInstruction(be->mb, r);
+
+       len--;
+       if (c->subtype.multiset == MS_ARRAY)
+               len--;
+       stmt *msid = list_fetch(ms, len);
+
+       /* call renumber */
+       InstrPtr q = newStmt(be->mb, batsqlRef, "renumber");
+       q = pushArgument(be->mb, q, msid->nr);
+       q = pushArgument(be->mb, q, rowids->nr);
+       q = pushArgument(be->mb, q, getArg(r, 0));
+       pushInstruction(be->mb, q);
+
+       /* now update msid and rowids */
+       rowids->nr = getArg(r, 0);
+       msid->nr = getArg(q, 0);
+       return getArg(r, 1);
+}
+
 static stmt *
-stmt_packn(backend *be, stmt *c, stmt *n)
+stmt_packn(backend *be, stmt *c, stmt *nr)
 {
        MalBlkPtr mb = be->mb;
        InstrPtr q = NULL;
 
-       if (c->nested)
-               return stmt_nest(be, c, n, &stmt_packn);
+       if (c->nested) {
+               int ctnr = 0;
+               if (c->subtype.multiset)
+                       ctnr = renumber(be, NULL, c);
+
+               list *ops = unnest_stmt(c);
+               list *nops = sa_list(be->mvc->sa);
+               sql_subtype *st = tail_type(c);
+               for(node *n = ops->h; n; n = n->next) {
+                       stmt *i = n->data, *oi = i;
+
+                       i = stmt_packn(be, oi, nr);
+                       i->nested = oi->nested;
+                       i->subtype = *tail_type(oi);
+                       i->tname = oi->tname;
+                       i->cname = oi->cname;
+                       i->label = oi->label;
+                       append(nops, i);
+               }
+               stmt *r = stmt_list(be, nops);
+               if (r == NULL)
+                       return NULL;
+               r->nested = true;
+               r->subtype = *st;
+               r->tname = c->tname;
+               r->cname = c->cname;
+               r->label = c->label;
+               if (c->type == st_alias)
+                       r = stmt_alias(be, r, c->label, c->tname, c->cname);
+
+               if (r && ctnr)
+                       r->flag = ctnr;
+               return r;
+       }
        if (c == NULL || c->nr < 0)
                goto bailout;
        q = newStmtArgs(mb, matRef, packIncrementRef, 3);
        if (q == NULL)
                goto bailout;
        q = pushArgument(mb, q, c->nr);
-       q = pushArgument(mb, q, n->nr);
+       q = pushArgument(mb, q, nr->nr);
        bool enabled = be->mvc->sa->eb.enabled;
        be->mvc->sa->eb.enabled = false;
        stmt *s = stmt_create(be->mvc->sa, st_append);
@@ -3687,7 +3756,7 @@ stmt_packn(backend *be, stmt *c, stmt *n
                goto bailout;
        }
        s->op1 = c;
-       s->op2 = n;
+       s->op2 = nr;
        s->nrcols = c->nrcols;
        s->key = c->key;
        s->nr = getDestVar(q);
@@ -3704,8 +3773,6 @@ stmt_packn(backend *be, stmt *c, stmt *n
 stmt *
 stmt_pack(backend *be, stmt *c, int n)
 {
-       if (c->nested)
-               return stmt_nest(be, c, stmt_atom_int(be, n), &stmt_packn);
        return stmt_packn(be, c, stmt_atom_int(be, n));
 }
 
@@ -3715,8 +3782,39 @@ stmt_pack_add(backend *be, stmt *c, stmt
        MalBlkPtr mb = be->mb;
        InstrPtr q = NULL;
 
-       if (c->nested)
-               return stmt_nest(be, c, a, &stmt_pack_add);
+       if (c->nested) {
+               int ctnr = 0;
+               if (c->subtype.multiset)
+                       ctnr = renumber(be, c, a);
+               list *ops1 = unnest_stmt(c);
+               list *ops2 = unnest_stmt(a);
+               list *nops = sa_list(be->mvc->sa);
+               sql_subtype *st = tail_type(a);
+               for(node *n = ops1->h, *m = ops2->h; n && m; n = n->next, m = 
m->next) {
+                       stmt *i1 = n->data, *i2 = m->data;
+                       stmt *i = i2, *oi = i;
+                       i = stmt_pack_add(be, i1, oi);
+                       i->nested = oi->nested;
+                       i->subtype = *tail_type(oi);
+                       i->tname = oi->tname;
+                       i->cname = oi->cname;
+                       i->label = oi->label;
+                       append(nops, i);
+               }
+               stmt *r = stmt_list(be, nops);
+               if (r == NULL)
+                       return NULL;
+               r->nested = true;
+               r->subtype = *st;
+               r->tname = a->tname;
+               r->cname = a->cname;
+               r->label = a->label;
+               if (a->type == st_alias)
+                       r = stmt_alias(be, r, a->label, a->tname, a->cname);
+               if (r && ctnr)
+                       r->flag = ctnr;
+               return r;
+       }
 
        if (c == NULL || a == NULL || c->nr < 0 || a->nr < 0)
                goto bailout;
diff --git a/sql/test/nested/Tests/basic.test b/sql/test/nested/Tests/basic.test
--- a/sql/test/nested/Tests/basic.test
+++ b/sql/test/nested/Tests/basic.test
@@ -44,3 +44,14 @@ select a from unnest(Array[1,2]) a(a);
 ----
 1
 2
+
+query T nosort
+select array[1,2,3]
+----
+{1,2,3}
+
+query T nosort
+select array[1,2,3] union all select array[4,5,6,7]
+----
+{1,2,3}
+{4,5,6,7}
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to