Changeset: 93b72f35534c for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=93b72f35534c
Modified Files:
        gdk/gdk_analytic_func.c
        sql/backends/monet5/sql_rank.c
        sql/backends/monet5/sql_upgrades.c
        sql/common/sql_types.c
        sql/server/rel_select.c
        sql/test/analytics/Tests/analytics00.stable.out
        sql/test/analytics/Tests/analytics01.sql
        sql/test/analytics/Tests/analytics01.stable.err
        sql/test/analytics/Tests/analytics01.stable.out
        sql/test/analytics/Tests/analytics14.stable.err
Branch: statistics-analytics
Log Message:

Fixed bug in quantile window function bug, and cleaned argument compilation for 
window functions


diffs (truncated from 1061 to 300 lines):

diff --git a/gdk/gdk_analytic_func.c b/gdk/gdk_analytic_func.c
--- a/gdk/gdk_analytic_func.c
+++ b/gdk/gdk_analytic_func.c
@@ -1805,7 +1805,7 @@ GDK_ANALYTICAL_STDEV_VARIANCE(variance_p
                                ss = (BUN) start[i]; \
                                ee = (BUN) end[i]; \
                                f = (ee - ss - 1) * qua; \
-                               qindex = ss + ee - (BUN) (ee + qua - f); \
+                               qindex = ss + ee - (BUN) (ee + 0.5f - f); \
                                assert(qindex >= ss && qindex < ee); \
                                v = bp[qindex]; \
                                *rb = v; \
@@ -1826,7 +1826,7 @@ GDK_ANALYTICAL_STDEV_VARIANCE(variance_p
                                ss = (BUN) start[i]; \
                                ee = (BUN) end[i]; \
                                f = (ee - ss - 1) * qua; \
-                               qindex = ss + ee - (BUN) (ee + qua - f); \
+                               qindex = ss + ee - (BUN) (ee + 0.5f - f); \
                                assert(qindex >= ss && qindex < ee); \
                                v = bp[qindex]; \
                                *rb = v; \
@@ -1862,7 +1862,7 @@ GDK_ANALYTICAL_STDEV_VARIANCE(variance_p
                                ss = (BUN) start[i]; \
                                ee = (BUN) end[i]; \
                                f = (ee - ss - 1) * qua; \
-                               qindex = ss + ee - (BUN) (ee + qua - f); \
+                               qindex = ss + ee - (BUN) (ee + 0.5f - f); \
                                assert(qindex >= ss && qindex < ee); \
                                curval = BUNtail(bpi, qindex); \
                                has_nils |= atomcmp(curval, nil) == 0;  \
@@ -1938,7 +1938,7 @@ GDKanalytical_quantile(BAT *r, BAT *b, B
                                        ss = (BUN) start[i];
                                        ee = (BUN) end[i];
                                        f = (ee - ss - 1) * qua;
-                                       qindex = ss + ee - (BUN) (ee + qua - f);
+                                       qindex = ss + ee - (BUN) (ee + 0.5f - 
f);
                                        assert(qindex >= ss && qindex < ee);
                                        curval = BUNtail(bpi, qindex);
                                        if (BUNappend(r, curval, false) != 
GDK_SUCCEED)
diff --git a/sql/backends/monet5/sql_rank.c b/sql/backends/monet5/sql_rank.c
--- a/sql/backends/monet5/sql_rank.c
+++ b/sql/backends/monet5/sql_rank.c
@@ -1101,7 +1101,16 @@ SQLnth_value(Client cntxt, MalBlkPtr mb,
 
 #define CHECK_L_VALUE(TPE) \
        do { \
-               TPE rval = *getArgReference_##TPE(stk, pci, 2); \
+               TPE rval; \
+               if (tp2_is_a_bat) { \
+                       if (!(l = BATdescriptor(*getArgReference_bat(stk, pci, 
2)))) { \
+                               msg = createException(SQL, op, SQLSTATE(HY005) 
"Cannot access column descriptor"); \
+                               goto bailout; \
+                       } \
+                       rval = ((TPE*)Tloc(l, 0))[0]; \
+               } else { \
+                       rval = *getArgReference_##TPE(stk, pci, 2); \
+               } \
                if (!is_##TPE##_nil(rval) && rval < 0) { \
                        gdk_call = dual; \
                        rval *= -1; \
@@ -1115,9 +1124,12 @@ do_lead_lag(Client cntxt, MalBlkPtr mb, 
                        gdk_return (*dual)(BAT *, BAT *, BAT *, BUN, const 
void* restrict, int))
 {
        int tp1, tp2, tp3, base = 2;
-       BUN l_value = 1;
+       BUN l_value = 1, cnt;
        const void *restrict default_value;
        gdk_return (*gdk_call)(BAT *, BAT *, BAT *, BUN, const void* restrict, 
int) = func;
+       BAT *b = NULL, *l = NULL, *d = NULL, *p = NULL, *r = NULL;
+       bool tp2_is_a_bat;
+       str msg = MAL_SUCCEED;
 
        (void)cntxt;
        if (pci->argc < 4 || pci->argc > 6)
@@ -1127,8 +1139,10 @@ do_lead_lag(Client cntxt, MalBlkPtr mb, 
 
        if (pci->argc > 4) { //contains (lag or lead) value;
                tp2 = getArgType(mb, pci, 2);
-               if (isaBatType(tp2))
-                       throw(SQL, op, SQLSTATE(42000) "%s second argument must 
be a single atom", desc);
+               tp2_is_a_bat = isaBatType(tp2);
+               if (tp2_is_a_bat)
+                       tp2 = getBatType(tp2);
+
                switch (tp2) {
                        case TYPE_bte:
                                CHECK_L_VALUE(bte)
@@ -1154,11 +1168,21 @@ do_lead_lag(Client cntxt, MalBlkPtr mb, 
        }
 
        if (pci->argc > 5) { //contains default value;
-               ValRecord *vin = &(stk)->stk[(pci)->argv[3]];
                tp3 = getArgType(mb, pci, 3);
-               if (isaBatType(tp3))
-                       throw(SQL, op, SQLSTATE(42000) "%s third argument must 
be a single atom", desc);
-               default_value = VALget(vin);
+               if (isaBatType(tp3)) {
+                       BATiter bpi;
+
+                       tp3 = getBatType(tp3);
+                       if (!(d = BATdescriptor(*getArgReference_bat(stk, pci, 
3)))) {
+                               msg = createException(SQL, op, SQLSTATE(HY005) 
"Cannot access column descriptor");
+                               goto bailout;
+                       }
+                       bpi = bat_iterator(d);
+                       default_value = BUNtail(bpi, 0);
+               } else {
+                       ValRecord *vin = &(stk)->stk[(pci)->argv[3]];
+                       default_value = VALget(vin);
+               }
                base = 4;
        } else {
                int tpe = tp1;
@@ -1170,43 +1194,56 @@ do_lead_lag(Client cntxt, MalBlkPtr mb, 
        assert(default_value); //default value must be set
 
        if (isaBatType(tp1)) {
-               BUN cnt;
                bat *res = getArgReference_bat(stk, pci, 0);
-               BAT *b = BATdescriptor(*getArgReference_bat(stk, pci, 1)), *p = 
NULL, *r;
-               if (!b)
-                       throw(SQL, op, SQLSTATE(HY005) "Cannot access column 
descriptor");
-               cnt = BATcount(b);
+               b = BATdescriptor(*getArgReference_bat(stk, pci, 1));
+               if (!b) {
+                       msg = createException(SQL, op, SQLSTATE(HY005) "Cannot 
access column descriptor");
+                       goto bailout;
+               }
                gdk_return gdk_code;
 
+               cnt = BATcount(b);
                tp1 = getBatType(tp1);
                voidresultBAT(r, tp1, cnt, b, op);
                if (isaBatType(getArgType(mb, pci, base))) {
                        p = BATdescriptor(*getArgReference_bat(stk, pci, base));
                        if (!p) {
-                               BBPunfix(b->batCacheid);
-                               throw(SQL, op, SQLSTATE(HY005) "Cannot access 
column descriptor");
+                               msg = createException(SQL, op, SQLSTATE(HY005) 
"Cannot access column descriptor");
+                               goto bailout;
                        }
                }
 
                gdk_code = gdk_call(r, b, p, l_value, default_value, tp1);
 
                BATsetcount(r, cnt);
-               BBPunfix(b->batCacheid);
                if (gdk_code == GDK_SUCCEED)
                        BBPkeepref(*res = r->batCacheid);
-               else
-                       throw(SQL, op, GDK_EXCEPTION);
+               else {
+                       msg = createException(SQL, op, GDK_EXCEPTION);
+                       goto bailout;
+               }
        } else {
                ValRecord *res = &(stk)->stk[(pci)->argv[0]];
                ValRecord *vin = &(stk)->stk[(pci)->argv[1]];
-               if(l_value == 0) {
-                       if(!VALcopy(res, vin))
-                               throw(SQL, op, SQLSTATE(HY013) MAL_MALLOC_FAIL);
+
+               if (l_value == 0) {
+                       if (!VALcopy(res, vin)) {
+                               msg = createException(SQL, op, SQLSTATE(HY013) 
MAL_MALLOC_FAIL);
+                               goto bailout;
+                       }
                } else {
                        VALset(res, tp1, (ptr) default_value);
                }
        }
-       return MAL_SUCCEED;
+
+bailout:
+       if (b) BBPunfix(b->batCacheid);
+       if (p) BBPunfix(p->batCacheid);
+       if (l) BBPunfix(l->batCacheid);
+       if (d) BBPunfix(d->batCacheid);
+       if (msg && r)
+               BBPreclaim(r);
+       return msg;
 }
 
 str
diff --git a/sql/backends/monet5/sql_upgrades.c 
b/sql/backends/monet5/sql_upgrades.c
--- a/sql/backends/monet5/sql_upgrades.c
+++ b/sql/backends/monet5/sql_upgrades.c
@@ -3168,7 +3168,8 @@ SQLupgrades(Client c, mvc *m)
                }
        }
 
-       if (!res && !sql_find_func_by_name(m->sa, s, "stddev_samp", 1, 
F_ANALYTIC)) {
+       sql_find_subtype(&tp, "tinyint", 0, 0);
+       if (!res && !sql_bind_func(m->sa, s, "stddev_samp", &tp, NULL, 
F_ANALYTIC)) {
                if ((err = sql_update_analytics(c, m, prev_schema, 
&systabfixed)) != NULL) {
                        fprintf(stderr, "!%s\n", err);
                        freeException(err);
diff --git a/sql/common/sql_types.c b/sql/common/sql_types.c
--- a/sql/common/sql_types.c
+++ b/sql/common/sql_types.c
@@ -775,29 +775,6 @@ func_cmp(sql_allocator *sa, sql_func *f,
 }
 
 sql_subfunc *
-sql_find_func_by_name(sql_allocator *sa, sql_schema *s, const char *name, int 
nrargs, sql_ftype type)
-{
-       if (s && s->funcs.set)
-               for (node *n=s->funcs.set->h; n; n = n->next) {
-                       sql_func *f = n->data;
-
-                       if ((f->type != type || !f->res || list_length(f->ops) 
!= nrargs))
-                               continue;
-                       if (strcmp(f->base.name, name) == 0)
-                               return sql_dup_subfunc(sa, f, NULL, NULL);
-               }
-       for (node *n=funcs->h; n; n = n->next) {
-               sql_func *f = n->data;
-
-               if ((f->type != type || !f->res || list_length(f->ops) != 
nrargs))
-                       continue;
-               if (strcmp(f->base.name, name) == 0)
-                       return sql_dup_subfunc(sa, f, NULL, NULL);
-       }
-       return NULL;
-}
-
-sql_subfunc *
 sql_find_func(sql_allocator *sa, sql_schema *s, const char *sqlfname, int 
nrargs, sql_ftype type, sql_subfunc *prev)
 {
        sql_subfunc *fres;
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
@@ -4582,7 +4582,7 @@ rel_rankop(sql_query *query, sql_rel **r
                                        exp_label(sql->sa, in, ++sql->label);
                                in = exp_ref(sql->sa, in);
                        }
-                       if(!in)
+                       if (!in)
                                return NULL;
                        append(fargs, in);
                        nfargs++;
@@ -4590,52 +4590,49 @@ rel_rankop(sql_query *query, sql_rel **r
                if (dl)
                        for (dargs = dl->h ; dargs ; dargs = dargs->next) { /* 
the last dnode is the distinct flag */
                                exp_kind ek = {type_value, card_column, FALSE};
+                               sql_subtype *empty = 
sql_bind_localtype("void"), *bte = sql_bind_localtype("bte");
 
                                in = rel_value_exp2(query, &p, dargs->data.sym, 
f | sql_window, ek);
                                if (!in)
                                        return NULL;
-                               if(is_ntile && nfargs == 1) { /* ntile first 
argument null handling case */
-                                       sql_subtype *empty = 
sql_bind_localtype("void");
-                                       if(subtype_cmp(&(in->tpe), empty) == 0) 
{
-                                               sql_subtype *to = 
sql_bind_localtype("bte");
-                                               in = exp_convert(sql->sa, in, 
empty, to);
-                                       }
-                               } else if(is_nth_value && nfargs == 1) { /* 
nth_value second argument null handling case */
-                                       sql_subtype *empty = 
sql_bind_localtype("void");
-                                       if(subtype_cmp(&(in->tpe), empty) == 0) 
{
-                                               sql_exp *ep = 
rel_first_column(sql, p);
-                                               in = exp_convert(sql->sa, in, 
empty, &(ep->tpe));
-                                       }
-                               } else if((is_lag || is_lead) && nfargs == 2) { 
/* lag and lead 3rd arg must have same type as 1st arg */
+
+                               /* corner case, if the argument is null convert 
it into something countable such as bte */
+                               if (subtype_cmp(&(in->tpe), empty) == 0)
+                                       in = exp_convert(sql->sa, in, empty, 
bte);
+                               if ((is_lag || is_lead) && nfargs == 2) { /* 
lag and lead 3rd arg must have same type as 1st arg */
                                        sql_exp *first = (sql_exp*) 
fargs->h->data;
-                                       if(!(in = rel_check_type(sql, 
&first->tpe, p, in, type_equal)))
+                                       if (!(in = rel_check_type(sql, 
&first->tpe, p, in, type_equal)))
                                                return NULL;
                                }
                                if (!in)
                                        return NULL;
+
                                append(fargs, in);
+                               in = exp_ref_save(sql, in);
                                nfargs++;
                        }
                dargs = dn->next->next;
        } else { /* aggregation function call */
                for (dargs = dn->next ; dargs->next && dargs->data.sym ; dargs 
= dargs->next) { /* the last dnode is the distinct flag */
                        exp_kind ek = {type_value, card_column, FALSE};
+                       sql_subtype *empty = sql_bind_localtype("void"), *bte = 
sql_bind_localtype("bte");
 
                        in = rel_value_exp2(query, &p, dargs->data.sym, f | 
sql_window, ek);
                        if (!in)
                                return NULL;
 
+                       /* corner case, if the argument is null convert it into 
something countable such as bte */
+                       if (subtype_cmp(&(in->tpe), empty) == 0)
+                               in = exp_convert(sql->sa, in, empty, bte);
+                       if (!in)
+                               return NULL;
+
                        append(fargs, in);
+                       in = exp_ref_save(sql, in);
                        nfargs++;
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to