Changeset: 9698a5f83bf6 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=9698a5f83bf6
Added Files:
        sql/test/BugTracker-2014/Tests/ifthenelse.Bug-3546.stable.err
        sql/test/BugTracker-2014/Tests/ifthenelse.Bug-3546.stable.out
Modified Files:
        sql/backends/monet5/sql_gencode.c
        sql/server/rel_optimizer.c
        sql/storage/bat/bat_storage.c
        sql/test/BugTracker-2014/Tests/All
Branch: Oct2014
Log Message:

generalized div detection/rewrites which fixes bug 3546


diffs (299 lines):

diff --git a/sql/backends/monet5/sql_gencode.c 
b/sql/backends/monet5/sql_gencode.c
--- a/sql/backends/monet5/sql_gencode.c
+++ b/sql/backends/monet5/sql_gencode.c
@@ -1869,6 +1869,8 @@ static int
                                return -1;
                        mod = sql_func_mod(f->func);
                        fimp = sql_func_imp(f->func);
+                       if (s->op1 && list_length(s->op1->op4.lval) != 3 && 
strcmp(f->func->base.name, "ifthenelse") == 0)
+                               assert(0);
                        if (s->nrcols) {
                                sql_subtype *res = f->res->h->data;
                                fimp = convertMultiplexFcn(fimp);
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
@@ -2216,6 +2216,43 @@ sql_div_fixup( mvc *sql, sql_exp *e, sql
        return exp_binop(sql->sa, le, re, e->f);
 }
 
+static int 
+exp_find_func( sql_exp *e, char *name)
+{
+       if (!e)
+               return 0;
+       switch(e->type) {
+       case e_aggr:
+       case e_func: 
+               {
+                       list *l = e->l;
+                       node *n;
+                       sql_subfunc *f = e->f;
+
+                       if (!f->func->s && !strcmp(f->func->base.name, name)) 
+                               return 1;
+                       if (!l)
+                               return 0;
+                       for (n = l->h; n; n = n->next) {
+                               sql_exp *ne = n->data;
+
+                               if (exp_find_func( ne, name))
+                                       return 1;
+                       }
+               }
+       case e_convert:
+               return exp_find_func( e->l, name);
+       case e_column: 
+       case e_cmp:
+       case e_psm:
+       case e_atom:
+       default:
+               return 0;
+       }
+}
+
+static sql_exp * exp_div_fixup( mvc *sql, sql_exp *e, sql_exp *cond, int lr );
+
 static list *
 exps_case_fixup( mvc *sql, list *exps, sql_exp *cond, int lr )
 {
@@ -2236,6 +2273,10 @@ exps_case_fixup( mvc *sql, list *exps, s
                                        exp_setname(sql->sa, ne, e->rname, 
e->name );
                                        e = ne;
                                }
+                       } else if (e->type == e_convert) {
+                               sql_exp *l = exp_div_fixup(sql, e->l, cond, lr);
+                               sql_exp *ne = exp_convert(sql->sa, l, 
exp_fromtype(e), exp_totype(e));
+                               e = ne;
                        }
                        append(nexps, e);
                }
@@ -2245,6 +2286,24 @@ exps_case_fixup( mvc *sql, list *exps, s
 }
 
 static sql_exp *
+exp_div_fixup( mvc *sql, sql_exp *e, sql_exp *cond, int lr )
+{
+       if (is_func(e->type) && e->l && !is_rank_op(e) ) {
+               sql_subfunc *f = e->f;
+
+               if (!f->func->s && !strcmp(f->func->base.name, "sql_div")) {
+                       e = sql_div_fixup(sql, e, cond, lr);
+               } else {
+                       list *l = exps_case_fixup(sql, e->l, cond, lr);
+                       sql_exp *ne = exp_op(sql->sa, l, f);
+                       exp_setname(sql->sa, ne, e->rname, e->name );
+                       e = ne;
+               }
+       }
+       return e;
+}
+
+static sql_exp *
 exp_case_fixup( mvc *sql, sql_exp *e )
 {
        /* only functions need fix up */
@@ -2264,38 +2323,23 @@ exp_case_fixup( mvc *sql, sql_exp *e )
 
                /* ifthenelse with one of the sides an 'sql_div' */
                args = ne->l;
-               if (!f->func->s && !strcmp(f->func->base.name,"ifthenelse")) { 
+               if (!f->func->s && !strcmp(f->func->base.name, "ifthenelse")) { 
                        sql_exp *cond = args->h->data, *nne; 
                        sql_exp *a1 = args->h->next->data; 
                        sql_exp *a2 = args->h->next->next->data; 
-                       sql_subfunc *a1f = a1->f;
-                       sql_subfunc *a2f = a2->f;
 
                        /* rewrite right hands of div */
-                       if (a1->type == e_func && !a1f->func->s && 
-                            !strcmp(a1f->func->base.name, "sql_div")) {
-                               a1 = sql_div_fixup(sql, a1, cond, 0);
+                       if ((a1->type == e_func || a1->type == e_convert) && 
exp_find_func(a1, "sql_div")) {
+                               a1 = exp_div_fixup(sql, a1, cond, 0);
                        } else if (a1->type == e_func && a1->l) { 
                                a1->l = exps_case_fixup(sql, a1->l, cond, 0); 
-                       } else if (a1->type == e_convert) { 
-                               sql_exp *l = a1->l;
-                               sql_subfunc *f = l->f;
-
-                               if (l->type == e_func && !f->func->s && 
!strcmp(f->func->base.name, "sql_div")) 
-                                       a1->l = sql_div_fixup(sql, l, cond, 0);
                        }
-                       if  (a2->type == e_func && !a2f->func->s && 
-                            !strcmp(a2f->func->base.name, "sql_div")) { 
-                               a2 = sql_div_fixup(sql, a2, cond, 1);
+                       if  ((a2->type == e_func || a2->type == e_convert) && 
exp_find_func(a2, "sql_div")) {
+                               a2 = exp_div_fixup(sql, a2, cond, 1);
                        } else if (a2->type == e_func && a2->l) { 
                                a2->l = exps_case_fixup(sql, a2->l, cond, 1); 
-                       } else if (a2->type == e_convert) { 
-                               sql_exp *l = a2->l;
-                               sql_subfunc *f = l->f;
-
-                               if (l->type == e_func && !f->func->s && 
!strcmp(f->func->base.name, "sql_div")) 
-                                       a2->l = sql_div_fixup(sql, l, cond, 1);
                        }
+                       assert(cond && a1 && a2);
                        nne = exp_op3(sql->sa, cond, a1, a2, ne->f);
                        exp_setname(sql->sa, nne, ne->rname, ne->name );
                        ne = nne;
diff --git a/sql/storage/bat/bat_storage.c b/sql/storage/bat/bat_storage.c
--- a/sql/storage/bat/bat_storage.c
+++ b/sql/storage/bat/bat_storage.c
@@ -1384,8 +1384,12 @@ empty_col(sql_column *c)
        sql_delta *bat = c->data;
 
        assert(c->data && c->base.allocated && bat->bid == 0);
-       bat->bid = e_bat(type);
+       bat->bid = bat->ibid;
+       bat->ibid = e_bat(type);
+       if (bat->bid == bat->ibid)
+               bat->bid = copyBat(bat->ibid, type, 0);
 }
+
 static void 
 empty_idx(sql_idx *i)
 {
@@ -1393,7 +1397,10 @@ empty_idx(sql_idx *i)
        sql_delta *bat = i->data;
 
        assert(i->data && i->base.allocated && bat->bid == 0);
-       bat->bid = e_bat(type);
+       bat->bid = bat->ibid;
+       bat->ibid = e_bat(type);
+       if (bat->bid == bat->ibid) 
+               bat->bid = copyBat(bat->ibid, type, 0);
 }
 
 static BUN
@@ -1745,10 +1752,10 @@ update_table(sql_trans *tr, sql_table *f
                                for (n = tt->idxs.set->h; n; n = n->next) 
                                        (void)store_funcs.clear_idx(tr->parent, 
n->data);
                } else {
-                       for (n = tt->columns.set->h; n; n = n->next) 
+                       for (n = ft->columns.set->h; n; n = n->next) 
                                empty_col(n->data);
-                       if (tt->idxs.set) 
-                               for (n = tt->idxs.set->h; n; n = n->next) 
+                       if (ft->idxs.set) 
+                               for (n = ft->idxs.set->h; n; n = n->next) 
                                        empty_idx(n->data);
                }
        }
diff --git a/sql/test/BugTracker-2014/Tests/All 
b/sql/test/BugTracker-2014/Tests/All
--- a/sql/test/BugTracker-2014/Tests/All
+++ b/sql/test/BugTracker-2014/Tests/All
@@ -32,3 +32,4 @@ non_groupby_column.Bug-3524
 orderby_count.Bug-3526
 outer_join_using_diff_types_using.Bug-3536
 select-distinct-order-limit.Bug-3527
+ifthenelse.Bug-3546
diff --git a/sql/test/BugTracker-2014/Tests/ifthenelse.Bug-3546.stable.err 
b/sql/test/BugTracker-2014/Tests/ifthenelse.Bug-3546.stable.err
new file mode 100644
--- /dev/null
+++ b/sql/test/BugTracker-2014/Tests/ifthenelse.Bug-3546.stable.err
@@ -0,0 +1,37 @@
+stderr of test 'ifthenelse.Bug-3546` in directory 'sql/test/BugTracker-2014` 
itself:
+
+
+# 20:44:31 >  
+# 20:44:31 >  "mserver5" "--debug=10" "--set" "gdk_nr_threads=0" "--set" 
"mapi_open=true" "--set" "mapi_port=34821" "--set" 
"mapi_usock=/var/tmp/mtest-21670/.s.monetdb.34821" "--set" "monet_prompt=" 
"--forcemito" "--set" "mal_listing=2" 
"--dbpath=/home/niels/scratch/rc-monetdb/Linux-x86_64/var/MonetDB/mTests_sql_test_BugTracker-2014"
 "--set" "mal_listing=0" "--set" "embedded_r=yes"
+# 20:44:31 >  
+
+# builtin opt  gdk_dbpath = 
/home/niels/scratch/rc-monetdb/Linux-x86_64/var/monetdb5/dbfarm/demo
+# builtin opt  gdk_debug = 0
+# builtin opt  gdk_vmtrim = no
+# builtin opt  monet_prompt = >
+# builtin opt  monet_daemon = no
+# builtin opt  mapi_port = 50000
+# builtin opt  mapi_open = false
+# builtin opt  mapi_autosense = false
+# builtin opt  sql_optimizer = default_pipe
+# builtin opt  sql_debug = 0
+# cmdline opt  gdk_nr_threads = 0
+# cmdline opt  mapi_open = true
+# cmdline opt  mapi_port = 34821
+# cmdline opt  mapi_usock = /var/tmp/mtest-21670/.s.monetdb.34821
+# cmdline opt  monet_prompt = 
+# cmdline opt  mal_listing = 2
+# cmdline opt  gdk_dbpath = 
/home/niels/scratch/rc-monetdb/Linux-x86_64/var/MonetDB/mTests_sql_test_BugTracker-2014
+# cmdline opt  mal_listing = 0
+# cmdline opt  embedded_r = yes
+# cmdline opt  gdk_debug = 536870922
+
+# 20:44:31 >  
+# 20:44:31 >  "mclient" "-lsql" "-ftest" "-Eutf-8" "-i" "-e" 
"--host=/var/tmp/mtest-21670" "--port=34821"
+# 20:44:31 >  
+
+
+# 20:44:31 >  
+# 20:44:31 >  "Done."
+# 20:44:31 >  
+
diff --git a/sql/test/BugTracker-2014/Tests/ifthenelse.Bug-3546.stable.out 
b/sql/test/BugTracker-2014/Tests/ifthenelse.Bug-3546.stable.out
new file mode 100644
--- /dev/null
+++ b/sql/test/BugTracker-2014/Tests/ifthenelse.Bug-3546.stable.out
@@ -0,0 +1,61 @@
+stdout of test 'ifthenelse.Bug-3546` in directory 'sql/test/BugTracker-2014` 
itself:
+
+
+# 20:44:31 >  
+# 20:44:31 >  "mserver5" "--debug=10" "--set" "gdk_nr_threads=0" "--set" 
"mapi_open=true" "--set" "mapi_port=34821" "--set" 
"mapi_usock=/var/tmp/mtest-21670/.s.monetdb.34821" "--set" "monet_prompt=" 
"--forcemito" "--set" "mal_listing=2" 
"--dbpath=/home/niels/scratch/rc-monetdb/Linux-x86_64/var/MonetDB/mTests_sql_test_BugTracker-2014"
 "--set" "mal_listing=0" "--set" "embedded_r=yes"
+# 20:44:31 >  
+
+# MonetDB 5 server v11.19.0
+# This is an unreleased version
+# Serving database 'mTests_sql_test_BugTracker-2014', using 4 threads
+# Compiled for x86_64-unknown-linux-gnu/64bit with 64bit OIDs dynamically 
linked
+# Found 7.334 GiB available main-memory.
+# Copyright (c) 1993-July 2008 CWI.
+# Copyright (c) August 2008-2014 MonetDB B.V., all rights reserved
+# Visit http://www.monetdb.org/ for further information
+# Listening for connection requests on mapi:monetdb://localhost.nes.nl:34821/
+# Listening for UNIX domain connection requests on 
mapi:monetdb:///var/tmp/mtest-21670/.s.monetdb.34821
+# MonetDB/GIS module loaded
+# MonetDB/SQL module loaded
+# MonetDB/R   module loaded
+
+Ready.
+
+# 20:44:31 >  
+# 20:44:31 >  "mclient" "-lsql" "-ftest" "-Eutf-8" "-i" "-e" 
"--host=/var/tmp/mtest-21670" "--port=34821"
+# 20:44:31 >  
+
+#start transaction;
+#create table t(r integer,a integer);
+#insert into t values (1,42),(2,0),(3,null); 
+[ 3    ]
+#select * from t;
+% sys.t,       sys.t # table_name
+% r,   a # name
+% int, int # type
+% 1,   2 # length
+[ 1,   42      ]
+[ 2,   0       ]
+[ 3,   NULL    ]
+#select case when not (a is null) and a > 0.0 then r/a else -1 end as s from t;
+% sys.L # table_name
+% s # name
+% int # type
+% 2 # length
+[ 0    ]
+[ -1   ]
+[ -1   ]
+#select case when not (a is null) and a > 0.0 then 1.0*r/a else -1 end as r 
from t;
+% sys.L # table_name
+% r # name
+% decimal # type
+% 21 # length
+[ 0.00 ]
+[ -1.00        ]
+[ -1.00        ]
+#rollback;
+
+# 20:44:31 >  
+# 20:44:31 >  "Done."
+# 20:44:31 >  
+
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to