Changeset: ec9fe3794c26 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/ec9fe3794c26
Modified Files:
sql/backends/monet5/rel_bin.c
sql/rel.txt
sql/server/rel_basetable.c
sql/server/rel_dump.c
sql/server/rel_exp.c
sql/server/rel_multiset.c
sql/server/rel_optimize_others.c
sql/server/rel_rel.c
sql/server/rel_select.c
sql/server/rel_updates.c
Branch: nestedtypes
Log Message:
use nested expressions: wip
diffs (truncated from 680 to 300 lines):
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
@@ -253,6 +253,15 @@ list_find_column_nid(backend *be, list *
if (s->label == label)
return s;
+ if (s->nrcols > 1) {
+ while(s->type == st_alias)
+ s = s->op1;
+ if (s->type == st_list) {
+ s = list_find_column_nid(be, s->op4.lval,
label);
+ if (s)
+ return s;
+ }
+ }
}
return NULL;
}
@@ -2469,49 +2478,68 @@ rel2bin_sql_table(backend *be, sql_table
return stmt_list(be, l);
}
-typedef struct subtable_e {
- sql_table *t;
- int nr;
- stmt *dels;
-} subtable_e;
-
-static sql_column *
-subtable_e_find(subtable_e *sts, const char *name, int nr, stmt **dels)
-{
- for(int i = 0; i < nr; i++) {
- sql_table *t = sts[i].t;
- sql_column *c = find_sql_column(t, name);
- if (c) {
- *dels = sts[i].dels;
- return c;
- }
- }
- return NULL;
+static stmt *
+rel2bin_subtable(backend *be, sql_table *t, stmt *dels, sql_column *c, list
*exps)
+{
+ mvc *sql = be->mvc;
+ list *l = sa_list(sql->sa);
+ stmt *col = NULL;
+
+ if (c->type.multiset) {
+ t = mvc_bind_table(sql, c->t->s, c->storage_type);
+ if (!t)
+ return NULL;
+ dels = stmt_tid(be, t, false);
+ }
+ for (node *en = exps->h; en; en = en->next) {
+ sql_exp *exp = en->data;
+ sql_alias *rname = exp_relname(exp)?exp_relname(exp):exp->l;
+ const char *oname = exp->r;
+ stmt *s = NULL;
+
+ assert(!is_func(exp->type));
+ if (oname[0] == '%' && strcmp(oname, TID) == 0) {
+ /* tid function sql.tid(t) */
+
+ if (col)
+ s = stmt_mirror(be, col);
+ else {
+ s = dels?dels:stmt_tid(be, t, 0);
+ dels = NULL;
+ }
+ } else if (oname[0] == '%') {
+ sql_idx *i = find_sql_idx(t, oname+1);
+
+ /* do not include empty indices in the plan */
+ if ((hash_index(i->type) && list_length(i->columns) <=
1) || !idx_has_column(i->type))
+ continue;
+ s = stmt_idx(be, i, dels, dels->partition);
+ } else {
+ sql_column *c = find_sql_column(t, oname);
+ assert(c);
+ if (exp->f && (c->type.multiset ||
c->type.type->composite)) {
+ s = rel2bin_subtable(be, t, dels, c, exp->f);
+ } else {
+ s = stmt_col(be, c, dels, dels->partition);
+ }
+ }
+ s = stmt_alias(be, s, exp->alias.label, rname, exp_name(exp));
+ list_append(l, s);
+ }
+ return stmt_list(be, l);
}
static stmt *
rel2bin_basetable(backend *be, sql_rel *rel)
{
mvc *sql = be->mvc;
- sql_table *t = rel->l, *bt = t;
+ sql_table *t = rel->l;
sql_column *fcol = NULL;
sql_idx *fi = NULL;
list *l = sa_list(sql->sa);
- stmt *dels = stmt_tid(be, t, rel->flag == REL_PARTITION), *odels =
dels, *col = NULL;
+ bool complex = (t->multiset || t->composite);
+ stmt *dels = stmt_tid(be, t, !complex?rel->flag ==
REL_PARTITION:false), *col = NULL;
node *en;
- list *subtables = rel_base_subtables(rel);
- int nr = list_length(subtables);
- subtable_e *sts = NULL;
- if (nr) {
- sts = (subtable_e*)sa_alloc(be->mvc->sa, sizeof(subtable_e)*nr);
- int i = 0;
- for(node *n = subtables->h; n; n = n->next, i++) {
- sts[i].t = n->data;
- sts[i].nr = 0;
- sts[i].dels = stmt_tid(be, sts[i].t, rel->flag ==
REL_PARTITION);
- }
- }
- bool multiset = t->multiset;
if (l == NULL || dels == NULL)
return NULL;
@@ -2530,14 +2558,14 @@ rel2bin_basetable(backend *be, sql_rel *
if ((hash_index(i->type) && list_length(i->columns) <=
1) || !idx_has_column(i->type))
continue;
fi = i;
- col = stmt_idx(be, i, multiset?dels:NULL,
dels->partition);
+ col = stmt_idx(be, i, complex?dels:NULL,
dels->partition);
} else {
sql_column *c = find_sql_column(t, oname);
if (!c || c->type.multiset || c->type.type->composite)
continue;
fcol = c;
- col = stmt_col(be, c, multiset?dels:NULL,
dels->partition);
+ col = stmt_col(be, c, complex?dels:NULL,
dels->partition);
}
}
for (en = rel->exps->h; en; en = en->next) {
@@ -2562,25 +2590,25 @@ rel2bin_basetable(backend *be, sql_rel *
/* do not include empty indices in the plan */
if ((hash_index(i->type) && list_length(i->columns) <=
1) || !idx_has_column(i->type))
continue;
- s = (i == fi) ? col : stmt_idx(be, i,
multiset?dels:NULL, dels->partition);
+ s = (i == fi) ? col : stmt_idx(be, i,
complex?dels:NULL, dels->partition);
} else {
- sql_column *c = find_sql_column(bt, oname);
- if (c) {
- dels = odels;
- } else if (sts) {
- c = subtable_e_find(sts, oname, nr, &dels);
- }
+ sql_column *c = find_sql_column(t, oname);
assert(c);
- if (!c->type.multiset && c->type.type->composite)
- continue;
-
- s = (c == fcol) ? col : stmt_col(be, c,
multiset?dels:NULL, dels->partition);
+ if (exp->f && (c->type.multiset ||
c->type.type->composite)) {
+ s = rel2bin_subtable(be, t, dels, c, exp->f);
+ if (!s)
+ return s;
+ if (s && s->type == st_list && c->type.multiset)
+ s = (c == fcol) ? col : stmt_col(be, c,
complex?dels:NULL, dels->partition);
+ } else {
+ s = (c == fcol) ? col : stmt_col(be, c,
complex?dels:NULL, dels->partition);
+ }
}
s = stmt_alias(be, s, exp->alias.label, rname, exp_name(exp));
list_append(l, s);
}
stmt *res = stmt_list(be, l);
- if (res && !multiset && dels)
+ if (res && !complex && dels)
res->cand = dels;
return res;
}
@@ -5867,6 +5895,177 @@ insert_composite(stmt **updates, sql_col
}
static stmt *
+rel2bin_insert_ms(backend *be, sql_rel *rel, list *refs)
+{
+ mvc *sql = be->mvc;
+ list *l;
+ stmt *inserts = NULL, *insert = NULL, *ddl = NULL, *pin = NULL,
**updates, *ret = NULL, *cnt = NULL, *pos = NULL, *returning = NULL;
+ int idx_ins = 0, len = 0;
+ node *n, *m, *idx_m = NULL;
+ sql_rel *tr = rel->l, *prel = rel->r;
+ sql_table *t = NULL;
+
+ if ((rel->flag&UPD_COMP)) { /* special case ! */
+ assert(0);
+ idx_ins = 1;
+ prel = rel->l;
+ rel = rel->r;
+ tr = rel->l;
+ }
+
+ if (tr->op == op_basetable) {
+ t = tr->l;
+ } else {
+ ddl = subrel_bin(be, tr, refs);
+ ddl = subrel_project(be, ddl, refs, NULL);
+ if (!ddl)
+ return NULL;
+ t = rel_ddl_table_get(tr);
+ }
+
+ if (rel->r) /* first construct the inserts relation */
+ inserts = subrel_bin(be, rel->r, refs);
+ inserts = subrel_project(be, inserts, refs, rel->r);
+
+ if (!inserts)
+ return NULL;
+
+ if (idx_ins)
+ pin = refs_find_rel(refs, prel);
+
+ for (n = ol_first_node(t->keys); n; n = n->next) {
+ sql_key * key = n->data;
+ if (key->type == ckey)
+ sql_insert_check(be, key, inserts->op4.lval);
+ }
+
+ if (!sql_insert_check_null(be, t, inserts->op4.lval))
+ return NULL;
+
+ updates = table_update_stmts(sql, t, &len);
+
+ /* now recursively insert into the sub tables (for each multiset fetch
sequence number and renumber the msids) */
+ for (n = ol_first_node(t->columns), m = inserts->op4.lval->h; n && m; )
{
+ sql_column *c = n->data;
+
+ if (c->type.multiset) {
+ sql_table *st = mvc_bind_table(sql, t->s,
c->storage_type);
+ if (!st)
+ return sql_error(sql, 10, SQLSTATE(27000)
"INSERT INTO: sub table '%s' missing", c->storage_type);
+ //n = insert_multiset();
+ (void)st;
+ } else if (c->type.type->composite && !c->type.multiset) {
+ n = insert_composite(updates, c, n, &m);
+ } else {
+ updates[c->colnr] = m->data;
+ n = n->next;
+ m = m->next;
+ }
+ }
+
+/* before */
+ if (!sql_insert_triggers(be, t, updates, 0))
+ return sql_error(sql, 10, SQLSTATE(27000) "INSERT INTO:
triggers failed for table '%s'", t->base.name);
+
+ insert = inserts->op4.lval->h?inserts->op4.lval->h->data:NULL;
+ if (!insert || insert->nrcols == 0) {
+ cnt = stmt_atom_lng(be, 1);
+ } else {
+ cnt = stmt_aggr(be, insert, NULL, NULL, sql_bind_func(sql,
"sys", "count", sql_bind_localtype("void"), NULL, F_AGGR, true, true), 1, 0, 1);
+ }
+ insert = NULL;
+
+ l = sa_list(sql->sa);
+ if (t->idxs) {
+ idx_m = m;
+ for (n = ol_first_node(t->idxs); n && m; n = n->next, m =
m->next) {
+ stmt *is = m->data;
+ sql_idx *i = n->data;
+
+ if (non_updatable_index(i->type)) /* Some indexes don't
hold delta structures */
+ continue;
+ if (hash_index(i->type) && list_length(i->columns) <= 1)
+ is = NULL;
+ if (i->key) {
+ stmt *ckeys = sql_insert_key(be,
inserts->op4.lval, i->key, is, pin);
+
+ list_append(l, ckeys);
+ }
+ if (!insert)
+ insert = is;
+ }
+ assert(!n && !m);
+ }
+
+ if (t->s) /* only not declared tables, need this */
+ pos = stmt_claim(be, t, cnt);
+
+ if (t->idxs) {
+ for (n = ol_first_node(t->idxs), m = idx_m; n && m; n =
n->next, m = m->next) {
+ stmt *is = m->data;
+ sql_idx *i = n->data;
+
+ if (non_updatable_index(i->type)) /* Some indexes don't
hold delta structures */
+ continue;
+ if (hash_index(i->type) && list_length(i->columns) <= 1)
+ is = NULL;
+ if (is)
+ is = stmt_append_idx(be, i, pos, is);
+ }
+ assert(!n && !m);
+ }
+
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]