Changeset: 13551439b360 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=13551439b360
Modified Files:
        sql/server/rel_dump.c
        sql/server/rel_select.c
        sql/test/BugTracker-2015/Tests/crash.Bug-3736.stable.out
        sql/test/pg_regress/Tests/vacuum.stable.out
Branch: subquery
Log Message:

merged


diffs (233 lines):

diff --git a/sql/server/rel_dump.c b/sql/server/rel_dump.c
--- a/sql/server/rel_dump.c
+++ b/sql/server/rel_dump.c
@@ -44,7 +44,7 @@ print_indent(mvc *sql, stream *fout, int
 static void
 cmp_print(mvc *sql, stream *fout, int cmp) 
 {
-       char *r;
+       char *r = NULL;
 
        (void)sql;
        switch(cmp) {
@@ -71,7 +71,7 @@ cmp_print(mvc *sql, stream *fout, int cm
        case cmp_joined:                
        case cmp_left:          
        case cmp_left_project:          
-                               assert(0); r = "inner"; break;
+                               r = "inner"; break;
        }
        mnstr_printf(fout, " %s ", r);
 }
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
@@ -4568,6 +4568,72 @@ rel_projections_(mvc *sql, sql_rel *rel)
        }
 }
 
+/* exp_rewrite */
+static sql_exp * exp_rewrite(mvc *sql, sql_exp *e, sql_rel *t);
+
+static list *
+exps_rename(mvc *sql, list *l, sql_rel *r) 
+{
+       node *n;
+       list *nl = new_exp_list(sql->sa);
+
+       for(n=l->h; n; n=n->next) {
+               sql_exp *arg = n->data;
+
+               arg = exp_rewrite(sql, arg, r);
+               if (!arg) 
+                       return NULL;
+               append(nl, arg);
+       }
+       return nl;
+}
+
+static sql_exp *
+exp_rewrite(mvc *sql, sql_exp *e, sql_rel *r) 
+{
+       sql_exp *l, *ne = NULL;
+
+       switch(e->type) {
+       case e_column:
+               if (e->l) { 
+                       e = exps_bind_column2(r->exps, e->l, e->r);
+               } else {
+                       e = exps_bind_column(r->exps, e->r, NULL);
+               }
+               if (!e)
+                       return NULL;
+               return exp_column(sql->sa, e->l, e->r, exp_subtype(e), 
exp_card(e), has_nil(e), is_intern(e));
+       case e_aggr:
+       case e_cmp: 
+               return NULL;
+       case e_convert:
+               l = exp_rewrite(sql, e->l, r);
+               if (l)
+                       ne = exp_convert(sql->sa, l, exp_fromtype(e), 
exp_totype(e));
+               break;
+       case e_func: {
+               list *l = e->l, *nl = NULL;
+
+               if (!l) {
+                       return e;
+               } else {
+                       nl = exps_rename(sql, l, r);
+                       if (!nl)
+                               return NULL;
+               }
+               if (e->type == e_func)
+                       ne = exp_op(sql->sa, nl, e->f);
+               else 
+                       ne = exp_aggr(sql->sa, nl, e->f, need_distinct(e), 
need_no_nil(e), e->card, has_nil(e));
+               break;
+       }       
+       case e_atom:
+       case e_psm:
+               return e;
+       }
+       return ne;
+}
+
 /* second complex columns only */
 static sql_exp *
 rel_order_by_column_exp(sql_query *query, sql_rel **R, symbol *column_r, int f)
@@ -4579,11 +4645,8 @@ rel_order_by_column_exp(sql_query *query
        int added_project = 0;
 
        if (is_sql_orderby(f)) {
-               sql_rel *rl = r->l;
-
                assert(is_project(r->op));
-               if (!is_processed(rl)) 
-                       r = r->l;
+               r = r->l;
        }
        if (!r)
                return e;
@@ -4597,11 +4660,10 @@ rel_order_by_column_exp(sql_query *query
        if (!e) {
                e = rel_value_exp(query, &r, column_r, sql_sel | sql_orderby, 
ek);
                /* add to internal project */
-               if (e && is_processed(r)) {
+               if (e && is_processed(r) && !is_groupby(r->op)) {
                        e = rel_project_add_exp(sql, r, e);
-                       //e = rel_lastexp(sql, r);
                        e = exp_ref(sql->sa, e);
-                       if (added_project) {
+                       if (added_project || is_sql_orderby(f)) {
                                e = rel_project_add_exp(sql, *R, e);
                                e = exp_ref(sql->sa, e);
                        }
@@ -4621,12 +4683,15 @@ rel_order_by_column_exp(sql_query *query
                        e = rel_value_exp(query, &nr, column_r, sql_sel | 
sql_orderby, ek);
                        if (e) {
                                /* first rewrite e back into current column 
names */
-                               e = rel_project_add_exp(sql, r, e);
-                               //e = rel_lastexp(sql, r);
-                               e = exp_ref(sql->sa, e);
-                               if (added_project) {
-                                       e = rel_project_add_exp(sql, *R, e);
+                               e = exp_rewrite(sql, e, nr);
+                               
+                               if (!is_groupby(r->op)) {
+                                       e = rel_project_add_exp(sql, r, e);
                                        e = exp_ref(sql->sa, e);
+                                       if (added_project || is_sql_orderby(f)) 
{
+                                               e = rel_project_add_exp(sql, 
*R, e);
+                                               e = exp_ref(sql->sa, e);
+                                       }
                                }
                        }
                }
@@ -4714,9 +4779,6 @@ rel_order_by(sql_query *query, sql_rel *
                                }
                        }
 
-                       //if (or != rel)
-                       //      return NULL;
-                       //if (!e && sql->session->status != -ERR_AMBIGUOUS && 
col->token == SQL_COLUMN) {
                        if (!e && sql->session->status != -ERR_AMBIGUOUS && 
(col->token == SQL_COLUMN || col->token == SQL_IDENT)) {
                                /* reset error */
                                sql->session->status = 0;
@@ -4758,6 +4820,8 @@ rel_order_by(sql_query *query, sql_rel *
                                }
                                if (!e)
                                        e = rel_order_by_column_exp(query, 
&rel, col, f);
+                               if (!e)
+                                       e = rel_order_by_column_exp(query, 
&rel, col, sql_sel);
                                if (e && e->card != rel->card && e->card != 
CARD_ATOM)
                                        e = NULL;
                        }
@@ -4769,7 +4833,6 @@ rel_order_by(sql_query *query, sql_rel *
                        return sql_error(sql, 02, SQLSTATE(42000) "order not of 
type SQL_COLUMN");
                }
        }
-       //*R = rel;
        if (or != rel)
                or->l = rel;
        return exps;
diff --git a/sql/test/BugTracker-2015/Tests/crash.Bug-3736.stable.out 
b/sql/test/BugTracker-2015/Tests/crash.Bug-3736.stable.out
--- a/sql/test/BugTracker-2015/Tests/crash.Bug-3736.stable.out
+++ b/sql/test/BugTracker-2015/Tests/crash.Bug-3736.stable.out
@@ -70,10 +70,10 @@ Ready.
 % clob # type
 % 721 # length
 REF 1 (2)
-crossproduct (
+join (
 | table(sys.open_auctions) [ "open_auctions"."id" NOT NULL HASHCOL  as 
"o"."id", "open_auctions"."open_auction_id" NOT NULL as "o"."open_auction_id", 
"open_auctions"."initial" NOT NULL as "o"."initial", "open_auctions"."reserve" 
NOT NULL as "o"."reserve", "open_auctions"."aktuell" NOT NULL as "o"."aktuell", 
"open_auctions"."privacy" NOT NULL as "o"."privacy", "open_auctions"."itemref" 
NOT NULL as "o"."itemref", "open_auctions"."seller" NOT NULL as "o"."seller", 
"open_auctions"."quantity" NOT NULL as "o"."quantity", "open_auctions"."type" 
NOT NULL as "o"."type", "open_auctions"."start" NOT NULL as "o"."start", 
"open_auctions"."ende" NOT NULL as "o"."ende", "open_auctions"."%TID%" NOT NULL 
as "o"."%TID%" ] COUNT ,
 | table(sys.bidder) [ "bidder"."id" NOT NULL HASHCOL  as "b"."id", 
"bidder"."open_auction_id" NOT NULL as "b"."open_auction_id", "bidder"."date" 
NOT NULL as "b"."date", "bidder"."time" NOT NULL as "b"."time", 
"bidder"."personref" NOT NULL as "b"."personref", "bidder"."increase" NOT NULL 
as "b"."increase", "bidder"."%TID%" NOT NULL as "b"."%TID%" ] COUNT 
-) [  ]
+) [ "o"."open_auction_id" NOT NULL = "b"."open_auction_id" NOT NULL ]
 REF 2 (2)
 group by (
 | project (
@@ -112,11 +112,7 @@ group by (
 ) [ "o"."open_auction_id" NOT NULL ] [ "o"."open_auction_id" NOT NULL ]
 project (
 | semijoin (
-| | project (
-| | | select (
-| | | | & REF 3 
-| | | ) [ "o"."open_auction_id" NOT NULL = "b"."open_auction_id" NOT NULL ]
-| | ) [ "o"."id" NOT NULL HASHCOL , "o"."open_auction_id" NOT NULL, 
"o"."initial" NOT NULL, "o"."reserve" NOT NULL, "o"."aktuell" NOT NULL, 
"o"."privacy" NOT NULL, "o"."itemref" NOT NULL, "o"."seller" NOT NULL, 
"o"."quantity" NOT NULL, "o"."type" NOT NULL, "o"."start" NOT NULL, "o"."ende" 
NOT NULL, "o"."%TID%" NOT NULL, "b"."id" NOT NULL HASHCOL , 
"b"."open_auction_id" NOT NULL, "b"."date" NOT NULL, "b"."time" NOT NULL, 
"b"."personref" NOT NULL, "b"."increase" NOT NULL, "b"."%TID%" NOT NULL, 
"b3"."increase" NOT NULL, sys.sql_mul("b3"."increase" NOT NULL, double "2") as 
"L44"."L44" ],
+| | & REF 3 ,
 | | project (
 | | | semijoin (
 | | | | project (
@@ -134,8 +130,8 @@ project (
 | | | | | ) [ "o"."open_auction_id" NOT NULL ] [ sys.max no nil ("b2a"."id" 
NOT NULL HASHCOL ) NOT NULL as "L7"."L7", "o"."open_auction_id" NOT NULL ]
 | | | | ) [ "L7"."L7" NOT NULL, "o"."open_auction_id" NOT NULL as "L37"."L37" ]
 | | | ) [ "b2"."id" NOT NULL HASHCOL  = "L7"."L7" NOT NULL, 
"o"."open_auction_id" NOT NULL = "L37"."L37" NOT NULL ]
-| | ) [ "b2"."increase" NOT NULL, "o"."open_auction_id" NOT NULL as 
"L32"."L32", "b2"."increase" NOT NULL as "L45"."L45" ]
-| ) [ "L44"."L44" <= "L45"."L45" NOT NULL, "o"."open_auction_id" NOT NULL = 
"L32"."L32" NOT NULL ]
+| | ) [ "b2"."increase" NOT NULL, "o"."open_auction_id" NOT NULL as 
"L32"."L32" ]
+| ) [ sys.sql_mul("b3"."increase" NOT NULL, double "2") <= "b2"."increase" NOT 
NULL, "o"."open_auction_id" NOT NULL = "L32"."L32" NOT NULL ]
 ) [ "b"."id" NOT NULL HASHCOL , "b"."open_auction_id" NOT NULL, "b"."date" NOT 
NULL, "b"."time" NOT NULL, "b"."personref" NOT NULL, "b"."increase" NOT NULL ]
 #Select b.* FROM open_auctions o, bidder b WHERE (select b3.INCREASE from 
bidder b3 where b3.id = (select min (b3a.id) from bidder b3a where 
b3a.open_auction_id = o.open_auction_id)) * 2 <= (Select b2.INCREASE from 
bidder b2 where b2.id = (SELECT MAX (b2a.id) from bidder b2a where 
b2a.open_auction_id = o.open_auction_id)) AND o.open_auction_id = 
b.open_auction_id order by date, time;
 % sys.b,       sys.b,  sys.b,  sys.b,  sys.b,  sys.b # table_name
diff --git a/sql/test/pg_regress/Tests/vacuum.stable.out 
b/sql/test/pg_regress/Tests/vacuum.stable.out
--- a/sql/test/pg_regress/Tests/vacuum.stable.out
+++ b/sql/test/pg_regress/Tests/vacuum.stable.out
@@ -86,7 +86,7 @@ Ready.
 % schema,      table,  column, type,   count,  typewidth,      columnsize,     
heapsize,       hashes, imprints,       sorted # name
 % varchar,     varchar,        varchar,        varchar,        bigint, int,    
bigint, bigint, bigint, bigint, boolean # type
 % 3,   6,      1,      3,      4,      1,      4,      1,      1,      1,      
5 # length
-[ "sys",       "vactst",       "i",    "int",  2049,   4,      8196,   0,      
0,      0,      false   ]
+[ "sys",       "vactst",       "i",    "int",  2049,   4,      8196,   0,      
0,      0,      NULL    ]
 #select "schema", "table", "column", type, count, typewidth, columnsize, 
heapsize, hashes, "imprints", sorted from sys.storage(current_schema, 'vactst');
 % .L3, .L3,    .L3,    .L3,    .L3,    .L3,    .L3,    .L3,    .L3,    .L3,    
.L3 # table_name
 % schema,      table,  column, type,   count,  typewidth,      columnsize,     
heapsize,       hashes, imprints,       sorted # name
@@ -163,7 +163,7 @@ Ready.
 % schema,      table,  column, type,   count,  typewidth,      columnsize,     
heapsize,       hashes, imprints,       sorted # name
 % varchar,     varchar,        varchar,        varchar,        bigint, int,    
bigint, bigint, bigint, bigint, boolean # type
 % 3,   6,      1,      3,      7,      1,      7,      1,      1,      1,      
5 # length
-[ "sys",       "vactst",       "i",    "int",  1048577,        4,      
4194308,        0,      0,      0,      false   ]
+[ "sys",       "vactst",       "i",    "int",  1048577,        4,      
4194308,        0,      0,      0,      NULL    ]
 #select "schema", "table", "column", type, count, typewidth, columnsize, 
heapsize, hashes, "imprints", sorted from sys.storage(current_schema, 'vactst');
 % .L3, .L3,    .L3,    .L3,    .L3,    .L3,    .L3,    .L3,    .L3,    .L3,    
.L3 # table_name
 % schema,      table,  column, type,   count,  typewidth,      columnsize,     
heapsize,       hashes, imprints,       sorted # name
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to