Update of /cvsroot/monetdb/sql/src/server
In directory 23jxhf1.ch3.sourceforge.com:/tmp/cvs-serv16373/src/server

Modified Files:
        rel_bin.mx rel_optimizer.mx rel_select.mx 
Log Message:
handle topn a bit earlier (before the projection join) 

In union distinct we use TID's where possible


U rel_select.mx
Index: rel_select.mx
===================================================================
RCS file: /cvsroot/monetdb/sql/src/server/rel_select.mx,v
retrieving revision 1.109
retrieving revision 1.110
diff -u -d -r1.109 -r1.110
--- rel_select.mx       27 Dec 2008 21:24:49 -0000      1.109
+++ rel_select.mx       29 Dec 2008 21:30:08 -0000      1.110
@@ -2257,11 +2257,10 @@
                symbol *ro = sc->data.lval->h->next->data.sym;
 
                sql_rel *lr, *rr;
-               sql_exp *e = NULL;
 
-               lr = rel_add_identity(sql, rel, &e);
+               lr = rel;
                rr = rel_copy(lr);
-               (void)e;
+               //rr = rel_dup(rel_dup(lr));
 
                lr = rel_logical_exp(sql, lr, lo, f);
                rr = rel_logical_exp(sql, rr, ro, f);

U rel_optimizer.mx
Index: rel_optimizer.mx
===================================================================
RCS file: /cvsroot/monetdb/sql/src/server/rel_optimizer.mx,v
retrieving revision 1.34
retrieving revision 1.35
diff -u -d -r1.34 -r1.35
--- rel_optimizer.mx    23 Dec 2008 10:30:07 -0000      1.34
+++ rel_optimizer.mx    29 Dec 2008 21:30:08 -0000      1.35
@@ -1727,6 +1727,24 @@
        return rel;
 }
 
+
+/*
+       Rewrite if the expressions c and z are equal
+               REF
+                       R()
+               union(
+                       select(REF, [x, y, z]),
+                       select(REF, [a, b, c])
+               )
+       into
+               REF
+                       select(R(), c)
+               union(
+                       select(REF, [x, y]),
+                       select(REF, [a, b])
+               )
+ */
+
 sql_rel *
 rel_optimizer(mvc *sql, sql_rel *rel) 
 {
@@ -1754,6 +1772,8 @@
        /* TODO add optimizer which removes unions 
                (for example common rels, with only one different expression) */
 
+       /* TODO remove distinct if its not needed */
+
        /* TODO common sub relation/expression optimizer */
 
        /* push (simple renaming) projections up */

U rel_bin.mx
Index: rel_bin.mx
===================================================================
RCS file: /cvsroot/monetdb/sql/src/server/rel_bin.mx,v
retrieving revision 1.66
retrieving revision 1.67
diff -u -d -r1.66 -r1.67
--- rel_bin.mx  28 Dec 2008 14:00:24 -0000      1.66
+++ rel_bin.mx  29 Dec 2008 21:30:08 -0000      1.67
@@ -88,6 +88,24 @@
        return stmt_dup(res);
 }
 
+static list *
+bin_find_columns( stmt *sub, char *name ) 
+{
+       node *n;
+       list *l = create_stmt_list();
+
+       for (n = sub->op1.lval->h; n; n = n->next) {
+               char *nme = column_name(n->data);
+
+               if (strcmp(nme, name) == 0) 
+                       append(l, stmt_dup(n->data));
+       }
+       if (list_length(l)) 
+               return l;
+       list_destroy(l);
+       return NULL;
+}
+
 static stmt *column( stmt *val )
 {
        val = stmt_dup(val);
@@ -839,16 +857,25 @@
 {
        node *n;
        group *grp = NULL;
-       list *rl = create_stmt_list();
+       list *rl = create_stmt_list(), *tids;
 
        /* single values are unique */
        if (s->key && s->nrcols == 0)
                return s;
 
-       for (n = s->op1.lval->h; n; n = n->next) {
-               stmt *t = n->data;
+       /* TODO Change to use 'all' %TID% columns */
+       if ((tids = bin_find_columns(s, "%TID%")) != NULL) {
+               for (n = tids->h; n; n = n->next) {
+                       stmt *t = n->data;
 
-               grp = grp_create(column(t), grp, NULL);
+                       grp = grp_create(column(t), grp, NULL);
+               }
+       } else {
+               for (n = s->op1.lval->h; n; n = n->next) {
+                       stmt *t = n->data;
+
+                       grp = grp_create(column(t), grp, NULL);
+               }
        }
 
        for (n = s->op1.lval->h; n; n = n->next) {
@@ -1485,6 +1512,26 @@
 }
 
 static stmt *
+find_projection_join( stmt *s )
+{
+       while(s && (s->type == st_alias || s->type == st_order || s->type == 
st_limit)) 
+               s = s->op1.stval;       
+       if (s && s->type == st_join)
+               return s;
+       return NULL;
+}
+
+static stmt *
+find_pivot( stmt *s)
+{
+       stmt *j = find_projection_join(s);
+
+       if (j)
+               return j->op1.stval;
+       return NULL;
+}
+
+static stmt *
 rel2bin_topn( mvc *sql, sql_rel *rel, list *refs)
 {
        list *newl;
@@ -1524,7 +1571,7 @@
        newl = create_stmt_list();
 
        if (n) {
-               stmt *limit = NULL;
+               stmt *limit = NULL, *p, *j;
                int lmt = l;
 
                if (l < 0) {
@@ -1542,14 +1589,34 @@
                        }
                }
 
-               for ( ; n; n = n->next) {
-                       stmt *s;
-       
+               /* reduce pivot */
+               j = find_projection_join(limit);
+               if (j) {
+                       p = find_pivot(j);
                        if (lmt < 0)
-                               s = stmt_diff(stmt_dup(n->data), 
stmt_dup(limit));
+                               p = stmt_diff(stmt_dup(p), stmt_dup(limit));
                        else
-                               s = stmt_semijoin(stmt_dup(n->data), 
stmt_dup(limit));
-                       list_append(newl, s);
+                               p = stmt_semijoin(stmt_dup(p), stmt_dup(limit));
+                       for ( ; n; n = n->next) {
+                               stmt *s = stmt_dup(n->data);
+                               stmt *prj = find_projection_join(s);
+               
+                               /* for all but the order column we can reduce */
+                               if (prj && prj != j) 
+                                       prj->op1.stval = 
stmt_semijoin(prj->op1.stval, stmt_dup(p));
+                               list_append(newl, s);
+                       }
+                       stmt_destroy(p);
+               } else {
+                       for ( ; n; n = n->next) {
+                               stmt *s;
+               
+                               if (lmt < 0)
+                                       s = stmt_diff(stmt_dup(n->data), 
stmt_dup(limit));
+                               else
+                                       s = stmt_semijoin(stmt_dup(n->data), 
stmt_dup(limit));
+                               list_append(newl, s);
+                       }
                }
                if (order) {
                        if (lmt < 0) {


------------------------------------------------------------------------------
_______________________________________________
Monetdb-sql-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/monetdb-sql-checkins

Reply via email to