Changeset: b74e39a680eb for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=b74e39a680eb
Modified Files:
        sql/backends/monet5/rel_bin.c
        sql/server/rel_optimizer.c
        sql/server/rel_select.c
Branch: nospare
Log Message:

more merge fun


diffs (truncated from 708 to 300 lines):

diff --git 
a/monetdb5/extras/mal_optimizer_template/Tests/opt_sql_append.stable.out.Windows
 
b/monetdb5/extras/mal_optimizer_template/Tests/opt_sql_append.stable.out.Windows
--- 
a/monetdb5/extras/mal_optimizer_template/Tests/opt_sql_append.stable.out.Windows
+++ 
b/monetdb5/extras/mal_optimizer_template/Tests/opt_sql_append.stable.out.Windows
@@ -5,13 +5,13 @@
 % %2 # name
 % varchar # type
 % 15 # length
-[ "sequential_pipe"     ]
+[ "sequential_pipe"    ]
 #select def from optimizers() where name = optimizer;
 % .%1 # table_name
 % def # name
 % clob # type
 % 626 # length
-[ 
"optimizer.inline();optimizer.remap();optimizer.costModel();optimizer.coercions();optimizer.aliases();optimizer.evaluate();optimizer.emptybind();optimizer.pushselect();optimizer.aliases();optimizer.mergetable();optimizer.deadcode();optimizer.aliases();optimizer.constants();optimizer.commonTerms();optimizer.projectionpath();optimizer.deadcode();optimizer.reorder();optimizer.matpack();optimizer.querylog();optimizer.multiplex();optimizer.generator();optimizer.profiler();optimizer.candidates();optimizer.deadcode();optimizer.postfix();optimizer.wlc();optimizer.garbageCollector();"
       ]
+[ 
"optimizer.inline();optimizer.remap();optimizer.costModel();optimizer.coercions();optimizer.aliases();optimizer.evaluate();optimizer.emptybind();optimizer.pushselect();optimizer.aliases();optimizer.mergetable();optimizer.bincopyfrom();optimizer.parappend();optimizer.deadcode();optimizer.aliases();optimizer.constants();optimizer.commonTerms();optimizer.projectionpath();optimizer.deadcode();optimizer.reorder();optimizer.matpack();optimizer.querylog();optimizer.multiplex();optimizer.generator();optimizer.profiler();optimizer.candidates();optimizer.deadcode();optimizer.postfix();optimizer.wlc();optimizer.garbageCollector();"
 ]
 #explain copy into ttt from E'\\tmp/xyz';
 % .explain # table_name
 % mal # name
@@ -99,7 +99,7 @@ end user.main;
 #start transaction;
 #create local temp table "opt_pipe_name" ("opt_pipe_name" string);
 #insert into "opt_pipe_name" values ((select optimizer));
-[ 1     ]
+[ 1    ]
 #set optimizer = substring((select def from optimizers() where name = (select 
opt_pipe_name from "opt_pipe_name")),0,
 #                length((select def from optimizers() where name = (select 
opt_pipe_name from "opt_pipe_name")))-length('optimizer.garbageCollector();')) 
|| 'optimizer.sql_append();optimizer.garbageCollector();';
 #select optimizer;
@@ -107,7 +107,7 @@ end user.main;
 % %2 # name
 % varchar # type
 % 6 # length
-[ "user_0"      ]
+[ "user_0"     ]
 #select def from optimizers() where name = optimizer;
 % .%1 # table_name
 % def # name
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
@@ -3312,7 +3312,6 @@ rel2bin_select(backend *be, sql_rel *rel
                if ((p=find_prop(e->p, PROP_HASHCOL)) != NULL) {
                        sql_idx *i = p->value;
 
-                       assert(0);
                        sel = rel2bin_hash_lookup(be, rel, sub, NULL, i, en);
                }
        }
diff --git a/sql/server/rel_optimizer.c b/sql/server/rel_optimizer.c
--- a/sql/server/rel_optimizer.c
+++ b/sql/server/rel_optimizer.c
@@ -593,10 +593,10 @@ matching_joins(sql_allocator *sa, list *
 }
 
 static int
-sql_kc_column_cmp(sql_kc *kc, sql_column *c)
+sql_column_kc_cmp(sql_column *c, sql_kc *kc)
 {
        /* return on equality */
-       return (kc->c->colnr - c->colnr);
+       return (c->colnr - kc->c->colnr);
 }
 
 static sql_idx *
@@ -610,11 +610,11 @@ find_fk_index(mvc *sql, sql_table *l, li
                        sql_idx *li = in->data;
                        if (li->type == join_idx) {
                                sql_key *rk = 
(sql_key*)os_find_id(tr->cat->objects, tr, ((sql_fkey*)li->key)->rkey);
-                               fcmp cmp = (fcmp)&sql_kc_column_cmp;
+                               fcmp cmp = (fcmp)&sql_column_kc_cmp;
 
                                if (rk->t == r &&
-                                       list_match(li->columns, lcols, cmp) == 
0 &&
-                                       list_match(rk->columns, rcols, cmp) == 
0) {
+                                       list_match(lcols, li->columns, cmp) == 
0 &&
+                                       list_match(rcols, rk->columns, cmp) == 
0) {
                                        return li;
                                }
                        }
@@ -1356,23 +1356,33 @@ exp_rename(mvc *sql, sql_exp *e, sql_rel
 }
 
 static int
-can_push_func(sql_exp *e, sql_rel *rel, int *must)
+can_push_func(sql_exp *e, sql_rel *rel, int *must, int depth)
 {
        switch(e->type) {
        case e_cmp: {
-               int mustl = 0, mustr = 0, mustf = 0;
                sql_exp *l = e->l, *r = e->r, *f = e->f;
 
-               if ((is_project(rel->op) && e->f) || e->flag == cmp_or || 
e->flag == cmp_in || e->flag == cmp_notin || e->flag == cmp_filter)
+               if (e->flag == cmp_or || e->flag == cmp_in || e->flag == 
cmp_notin || e->flag == cmp_filter)
                        return 0;
-               return ((l->type == e_column || can_push_func(l, rel, &mustl)) 
&& (*must = mustl)) ||
-                               (!f && (r->type == e_column || can_push_func(r, 
rel, &mustr)) && (*must = mustr)) ||
-                       (f &&
-                               (r->type == e_column || can_push_func(r, rel, 
&mustr)) &&
-                       (f->type == e_column || can_push_func(f, rel, &mustf)) 
&& (*must = (mustr || mustf)));
+               if (depth > 0) { /* for comparisons under the top ones, they 
become functions */
+                       int lmust = 0;
+                       int res = can_push_func(l, rel, &lmust, depth + 1) && 
can_push_func(r, rel, &lmust, depth + 1) &&
+                                       (!f || can_push_func(f, rel, &lmust, 
depth + 1));
+                       if (res && !lmust)
+                               return 1;
+                       (*must) |= lmust;
+                       return res;
+               } else {
+                       int mustl = 0, mustr = 0, mustf = 0;
+                       return ((l->type == e_column || can_push_func(l, rel, 
&mustl, depth + 1)) && (*must = mustl)) ||
+                                       (!f && (r->type == e_column || 
can_push_func(r, rel, &mustr, depth + 1)) && (*must = mustr)) ||
+                               (f &&
+                                       (r->type == e_column || 
can_push_func(r, rel, &mustr, depth + 1)) &&
+                               (f->type == e_column || can_push_func(f, rel, 
&mustf, depth + 1)) && (*must = (mustr || mustf)));
+               }
        }
        case e_convert:
-               return can_push_func(e->l, rel, must);
+               return can_push_func(e->l, rel, must, depth + 1);
        case e_aggr:
        case e_func: {
                list *l = e->l;
@@ -1381,7 +1391,7 @@ can_push_func(sql_exp *e, sql_rel *rel, 
                if (exp_unsafe(e, 0))
                        return 0;
                if (l) for (node *n = l->h; n && res; n = n->next)
-                       res &= can_push_func(n->data, rel, &lmust);
+                       res &= can_push_func(n->data, rel, &lmust, depth + 1);
                if (res && !lmust)
                        return 1;
                (*must) |= lmust;
@@ -1404,15 +1414,12 @@ exps_can_push_func(list *exps, sql_rel *
                sql_exp *e = n->data;
                int mustl = 0, mustr = 0;
 
-               if ((is_joinop(rel->op) || is_select(rel->op)) && 
((can_push_func(e, rel->l, &mustl) && mustl)))
+               if ((is_joinop(rel->op) || is_select(rel->op)) && 
((can_push_func(e, rel->l, &mustl, 0) && mustl)))
                        *push_left = true;
-
-               if (is_joinop(rel->op) && can_push_func(e, rel->r, &mustr) && 
mustr)
+               if (is_joinop(rel->op) && can_push_func(e, rel->r, &mustr, 0) 
&& mustr)
                        *push_right = true;
        }
-       if (*push_left || *push_right)
-               return 1;
-       return 0;
+       return *push_left || *push_right;
 }
 
 static int
@@ -1447,22 +1454,22 @@ exps_need_push_down( list *exps )
        return 0;
 }
 
-static sql_exp *exp_push_single_func_down(visitor *v, sql_rel *rel, sql_rel 
*l, sql_rel *r, sql_exp *e);
+static sql_exp *exp_push_single_func_down(visitor *v, sql_rel *rel, sql_rel 
*l, sql_rel *r, sql_exp *e, int depth);
 
 static list *
-exps_push_single_func_down(visitor *v, sql_rel *rel, sql_rel *l, sql_rel *r, 
list *exps)
+exps_push_single_func_down(visitor *v, sql_rel *rel, sql_rel *l, sql_rel *r, 
list *exps, int depth)
 {
        if (THRhighwater())
                return sql_error(v->sql, 10, SQLSTATE(42000) "Query too 
complex: running out of stack space");
 
        for (node *n = exps->h; n; n = n->next)
-               if ((n->data = exp_push_single_func_down(v, rel, l, r, 
n->data)) == NULL)
+               if ((n->data = exp_push_single_func_down(v, rel, l, r, n->data, 
depth)) == NULL)
                        return NULL;
        return exps;
 }
 
 static sql_exp *
-exp_push_single_func_down(visitor *v, sql_rel *rel, sql_rel *l, sql_rel *r, 
sql_exp *e)
+exp_push_single_func_down(visitor *v, sql_rel *rel, sql_rel *l, sql_rel *r, 
sql_exp *e, int depth)
 {
        if (THRhighwater())
                return sql_error(v->sql, 10, SQLSTATE(42000) "Query too 
complex: running out of stack space");
@@ -1470,26 +1477,26 @@ exp_push_single_func_down(visitor *v, sq
        switch(e->type) {
        case e_cmp: {
                if (e->flag == cmp_or || e->flag == cmp_filter) {
-                       if ((e->l = exps_push_single_func_down(v, rel, l, r, 
e->l)) == NULL)
+                       if ((e->l = exps_push_single_func_down(v, rel, l, r, 
e->l, depth + 1)) == NULL)
                                return NULL;
-                       if ((e->r = exps_push_single_func_down(v, rel, l, r, 
e->r)) == NULL)
+                       if ((e->r = exps_push_single_func_down(v, rel, l, r, 
e->r, depth + 1)) == NULL)
                                return NULL;
                } else if (e->flag == cmp_in || e->flag == cmp_notin) {
-                       if ((e->l = exp_push_single_func_down(v, rel, l, r, 
e->l)) == NULL)
+                       if ((e->l = exp_push_single_func_down(v, rel, l, r, 
e->l, depth + 1)) == NULL)
                                return NULL;
-                       if ((e->r = exps_push_single_func_down(v, rel, l, r, 
e->r)) == NULL)
+                       if ((e->r = exps_push_single_func_down(v, rel, l, r, 
e->r, depth + 1)) == NULL)
                                return NULL;
                } else {
-                       if ((e->l = exp_push_single_func_down(v, rel, l, r, 
e->l)) == NULL)
+                       if ((e->l = exp_push_single_func_down(v, rel, l, r, 
e->l, depth + 1)) == NULL)
                                return NULL;
-                       if ((e->r = exp_push_single_func_down(v, rel, l, r, 
e->r)) == NULL)
+                       if ((e->r = exp_push_single_func_down(v, rel, l, r, 
e->r, depth + 1)) == NULL)
                                return NULL;
-                       if (e->f && (e->f = exp_push_single_func_down(v, rel, 
l, r, e->f)) == NULL)
+                       if (e->f && (e->f = exp_push_single_func_down(v, rel, 
l, r, e->f, depth + 1)) == NULL)
                                return NULL;
                }
        } break;
        case e_convert:
-               if ((e->l = exp_push_single_func_down(v, rel, l, r, e->l)) == 
NULL)
+               if ((e->l = exp_push_single_func_down(v, rel, l, r, e->l, depth 
+ 1)) == NULL)
                        return NULL;
                break;
        case e_aggr:
@@ -1500,8 +1507,8 @@ exp_push_single_func_down(visitor *v, sq
                        return e;
                if (!e->l || exps_are_atoms(e->l))
                        return e;
-               if ((is_joinop(rel->op) && ((can_push_func(e, l, &mustl) && 
mustl) || (can_push_func(e, r, &mustr) && mustr))) ||
-                       (is_select(rel->op) && can_push_func(e, l, &must) && 
must)) {
+               if ((is_joinop(rel->op) && ((can_push_func(e, l, &mustl, depth 
+ 1) && mustl) || (can_push_func(e, r, &mustr, depth + 1) && mustr))) ||
+                       (is_select(rel->op) && can_push_func(e, l, &must, depth 
+ 1) && must)) {
                        exp_label(v->sql->sa, e, ++v->sql->label);
                        if (mustr)
                                append(r->exps, e);
@@ -1542,7 +1549,7 @@ rel_push_func_down(visitor *v, sql_rel *
                                rel->r = r = rel_project(v->sql->sa, r, 
rel_projections(v->sql, r, NULL, 1, 1));
                        nrel = rel_project(v->sql->sa, rel, 
rel_projections(v->sql, rel, NULL, 1, 1));
 
-                       if (!(exps = exps_push_single_func_down(&nv, rel, l, r, 
exps)))
+                       if (!(exps = exps_push_single_func_down(&nv, rel, l, r, 
exps, 0)))
                                return NULL;
                        if (nv.changes) {
                                rel = nrel;
@@ -1571,8 +1578,8 @@ rel_push_func_down(visitor *v, sql_rel *
                                sql_exp *e = n->data;
                                int mustl = 0, mustr = 0;
 
-                               if ((can_push_func(e, l, &mustl) && mustl) ||
-                                   (can_push_func(e, r, &mustr) && mustr)) {
+                               if ((can_push_func(e, l, &mustl, 0) && mustl) ||
+                                   (can_push_func(e, r, &mustr, 0) && mustr)) {
                                        if (mustl)
                                                append(l->exps, e);
                                        else
@@ -7342,7 +7349,7 @@ find_index(sql_allocator *sa, sql_rel *r
                if ((p = find_prop(e->p, PROP_HASHIDX)) != NULL) {
                        list *exps, *cols;
                        sql_idx *i = p->value;
-                       fcmp cmp = (fcmp)&sql_kc_column_cmp;
+                       fcmp cmp = (fcmp)&sql_column_kc_cmp;
 
                        /* join indices are only interesting for joins */
                        if (i->type == join_idx || list_length(i->columns) <= 1)
@@ -7351,14 +7358,14 @@ find_index(sql_allocator *sa, sql_rel *r
                        exps = list_select(rel->exps, i, (fcmp) &index_exp, 
(fdup)NULL);
                        if (list_empty(exps))
                                continue;
-                       /* now we obtain the columns, move into 
sql_kc_column_cmp! */
+                       /* now we obtain the columns, move into 
sql_column_kc_cmp! */
                        cols = list_map(exps, sub, (fmap) &sjexp_col);
 
                        /* TODO check that at most 2 relations are involved */
 
                        /* Match the index columns with the expression columns.
                           TODO, Allow partial matches ! */
-                       if (list_match(i->columns, cols, cmp) == 0) {
+                       if (list_match(cols, i->columns, cmp) == 0) {
                                /* re-order exps in index order */
                                node *n, *m;
                                list *es = sa_list(sa);
@@ -7366,7 +7373,7 @@ find_index(sql_allocator *sa, sql_rel *r
                                for(n = i->columns->h; n; n = n->next) {
                                        int i = 0;
                                        for(m = cols->h; m; m = m->next, i++) {
-                                               if (cmp(n->data, m->data) == 0){
+                                               if (cmp(m->data, n->data) == 0){
                                                        sql_exp *e = 
list_fetch(exps, i);
                                                        list_append(es, e);
                                                        break;
@@ -7400,11 +7407,13 @@ rel_use_index(visitor *v, sql_rel *rel)
 
                if (i) {
                        prop *p;
+                       node *n;
                        int single_table = 1;
                        sql_exp *re = NULL;
 
-                       for( node *n = exps->h; n && single_table; n = n->next) 
{
-                               sql_exp *e = n->data, *nre = e->r;
+                       for( n = exps->h; n && single_table; n = n->next) {
+                               sql_exp *e = n->data;
+                               sql_exp *nre = e->r;
 
                                if (is_join(rel->op) &&
                                        ((left && !rel_find_exp(rel->l, e->l)) 
||
@@ -7414,11 +7423,8 @@ rel_use_index(visitor *v, sql_rel *rel)
                                re = nre;
                        }
                        if (single_table) { /* add PROP_HASHCOL to all column 
exps */
-                               fcmp cmp = (fcmp)&sql_kc_column_cmp;
-
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to