Changeset: 9b9548ba027b for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=9b9548ba027b
Modified Files:
        gdk/gdk_analytic.h
        gdk/gdk_analytic_bounds.c
        gdk/gdk_analytic_func.c
        sql/backends/monet5/sql.c
        sql/backends/monet5/sql_rank.c
        sql/backends/monet5/sql_rank.h
        sql/server/rel_optimizer.c
        sql/server/rel_select.c
Branch: window-tunning
Log Message:

Global case for min and max functions added. Now I have to do the same for the 
remaining aggregates, but the worst is done.

Still lots to clean and test. The tests will take a long time before running 
again.
Tomorrow back to scoping.


diffs (truncated from 708 to 300 lines):

diff --git a/gdk/gdk_analytic.h b/gdk/gdk_analytic.h
--- a/gdk/gdk_analytic.h
+++ b/gdk/gdk_analytic.h
@@ -29,8 +29,8 @@ gdk_export gdk_return GDKanalyticalfirst
 gdk_export gdk_return GDKanalyticallast(BAT *r, BAT *b, BAT *s, BAT *e, int 
tpe);
 gdk_export gdk_return GDKanalyticalnthvalue(BAT *r, BAT *b, BAT *s, BAT *e, 
BAT *l, const void *restrict bound, int tp1, int tp2);
 
-gdk_export gdk_return GDKanalyticalmin(BAT *r, BAT *b, BAT *s, BAT *e, int 
tpe);
-gdk_export gdk_return GDKanalyticalmax(BAT *r, BAT *b, BAT *s, BAT *e, int 
tpe);
+gdk_export gdk_return GDKanalyticalmin(BAT *r, BAT *p, BAT *b, BAT *s, BAT *e, 
int tpe, int frame_type);
+gdk_export gdk_return GDKanalyticalmax(BAT *r, BAT *p, BAT *b, BAT *s, BAT *e, 
int tpe, int frame_type);
 gdk_export gdk_return GDKanalyticalcount(BAT *r, BAT *b, BAT *s, BAT *e, const 
bit *restrict ignore_nils, int tpe);
 gdk_export gdk_return GDKanalyticalsum(BAT *r, BAT *b, BAT *s, BAT *e, int 
tp1, int tp2);
 gdk_export gdk_return GDKanalyticalprod(BAT *r, BAT *b, BAT *s, BAT *e, int 
tp1, int tp2);
diff --git a/gdk/gdk_analytic_bounds.c b/gdk/gdk_analytic_bounds.c
--- a/gdk/gdk_analytic_bounds.c
+++ b/gdk/gdk_analytic_bounds.c
@@ -37,7 +37,6 @@
 #define ANALYTICAL_WINDOW_BOUNDS_BRANCHES_ROWS(IMP, LIMIT)             \
        do {                                                            \
                if (p) {                                                \
-                       np = (bit*)Tloc(p, 0);                  \
                        for (; i < cnt; i++) {                  \
                                if (np[i])                      \
                                        
ANALYTICAL_WINDOW_BOUNDS_ROWS##IMP(LIMIT); \
@@ -88,7 +87,6 @@
 #define ANALYTICAL_WINDOW_BOUNDS_BRANCHES_GROUPS(IMP, LIMIT)           \
        do {                                                            \
                if (p) {                                                \
-                       np = (bit*)Tloc(p, 0);                  \
                        for (; i < cnt; i++) {                  \
                                if (np[i])                      \
                                        
ANALYTICAL_WINDOW_BOUNDS_GROUPS##IMP(LIMIT); \
@@ -380,7 +378,7 @@ static gdk_return
 GDKanalyticalrowbounds(BAT *r, BAT *b, BAT *p, BAT *l, const void *restrict 
bound, int tp2, bool preceding, lng first_half)
 {
        lng cnt = (BUN) BATcount(b), nils = 0, *restrict rb = (lng *) Tloc(r, 
0), i = 0, k = 0, j = 0;
-       bit *np;
+       bit *restrict np = p ? (bit*)Tloc(p, 0) : NULL;
        int abort_on_error = 1;
 
        if (l) {                /* dynamic bounds */
@@ -486,7 +484,7 @@ GDKanalyticalrangebounds(BAT *r, BAT *b,
        bit *np = p ? (bit *) Tloc(p, 0) : NULL;
        BATiter bpi = bat_iterator(b);
        int (*atomcmp) (const void *, const void *) = ATOMcompare(tp1);
-       const void *restrict nil = ATOMnilptr(tp1);
+       const void *nil = ATOMnilptr(tp1);
        int abort_on_error = 1;
 
        if (l) {                /* dynamic bounds */
@@ -649,7 +647,7 @@ static gdk_return
 GDKanalyticalgroupsbounds(BAT *r, BAT *b, BAT *p, BAT *l, const void *restrict 
bound, int tp2, bool preceding)
 {
        lng cnt = (lng) BATcount(b), *restrict rb = (lng *) Tloc(r, 0), i = 0, 
k = 0, j = 0;
-       bit *np, *bp = (bit*) Tloc(b, 0);
+       bit *restrict np = p ? (bit*)Tloc(p, 0) : NULL, *restrict bp = (bit*) 
Tloc(b, 0);
 
        if (b->ttype != TYPE_bit) {
                GDKerror("groups frame bound type must be of type bit\n");
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
@@ -13,7 +13,7 @@
 
 #define ANALYTICAL_DIFF_IMP(TPE)                               \
        do {                                                    \
-               TPE *bp = (TPE*)Tloc(b, 0), prev = bp[0];               \
+               TPE *restrict bp = (TPE*)Tloc(b, 0), prev = bp[0];              
\
                if (np) {                                       \
                        for (; i < cnt; i++) {  \
                                TPE next = bp[i]; \
@@ -40,7 +40,7 @@
 /* We use NaN for floating point null values, which always output false on 
equality tests */
 #define ANALYTICAL_DIFF_FLOAT_IMP(TPE)                                 \
        do {                                                            \
-               TPE *bp = (TPE*)Tloc(b, 0), prev = bp[0];               \
+               TPE *restrict bp = (TPE*)Tloc(b, 0), prev = bp[0];              
\
                if (np) {                                               \
                        for (; i < cnt; i++) {          \
                                TPE next = bp[i]; \
@@ -128,7 +128,7 @@ GDKanalyticaldiff(BAT *r, BAT *b, BAT *p
                }
        }
        }
-       BATsetcount(r, cnt);
+       BATsetcount(r, (BUN) cnt);
        r->tnonil = true;
        r->tnil = false;
        return GDK_SUCCEED;
@@ -910,34 +910,176 @@ GDKanalyticallead(BAT *r, BAT *b, BAT *p
        return GDK_SUCCEED;
 }
 
-#define ANALYTICAL_MIN_MAX_CALC(TPE, OP)                               \
+#define ANALYTICAL_MIN_MAX_CALC_FIXED(TPE, OP)                         \
        do {                                                            \
-               TPE *bp = (TPE*)Tloc(b, 0), *bs, *be, v,                \
-                       curval = TPE##_nil, *restrict rb = (TPE*)Tloc(r, 0); \
-               for (; i < cnt; i++, rb++) {                            \
-                       bs = bp + start[i];                             \
-                       be = bp + end[i];                               \
-                       for (; bs < be; bs++) {                         \
-                               v = *bs;                                \
+               TPE *bp = (TPE*)Tloc(b, 0), v, curval = TPE##_nil, *restrict rb 
= (TPE*)Tloc(r, 0); \
+               switch (frame_type) {           \
+               case 3: /* unbounded until current row */       {       \
+                       for (; k < i; k++) { \
+                               v = bp[k];                              \
                                if (!is_##TPE##_nil(v)) {               \
                                        if (is_##TPE##_nil(curval))     \
-                                               curval = v;             \
+                                               curval = v;     \
+                                       else                            \
+                                               curval = OP(v, curval); \
+                               }                                       \
+                               rb[k] = curval; \
+                               has_nils |= is_##TPE##_nil(curval); \
+                       } \
+               } break;                \
+               case 4: /* current row until unbounded */       {       \
+                       for (j = i - 1; j >= k; j--) { \
+                               v = bp[j];                              \
+                               if (!is_##TPE##_nil(v)) {               \
+                                       if (is_##TPE##_nil(curval))     \
+                                               curval = v;     \
+                                       else                            \
+                                               curval = OP(v, curval); \
+                               }                                       \
+                               rb[j] = curval; \
+                               has_nils |= is_##TPE##_nil(curval); \
+                       } \
+               } break;                \
+               case 5: /* all rows */  {       \
+                       for (j = k; j < i; j++) { \
+                               v = bp[j];                              \
+                               if (!is_##TPE##_nil(v)) {               \
+                                       if (is_##TPE##_nil(curval))     \
+                                               curval = v;     \
                                        else                            \
                                                curval = OP(v, curval); \
                                }                                       \
+                       } \
+                       for (; k < i; k++) \
+                               rb[k] = curval; \
+                       has_nils |= is_##TPE##_nil(curval); \
+               } break;                \
+               case 6: /* current row */ {     \
+                       for (; k < i; k++) { \
+                               v = bp[k]; \
+                               rb[k] = v; \
+                               has_nils |= is_##TPE##_nil(v); \
+                       } \
+               } break;                \
+               default: {              \
+                       for (; k < i; k++) { \
+                               TPE *bs = bp + start[k];                        
        \
+                               TPE *be = bp + end[k];                          
\
+                               for (; bs < be; bs++) {                         
\
+                                       v = *bs;                                
\
+                                       if (!is_##TPE##_nil(v)) {               
\
+                                               if (is_##TPE##_nil(curval))     
\
+                                                       curval = v;     \
+                                               else                            
\
+                                                       curval = OP(v, curval); 
\
+                                       }                                       
\
+                               }                                               
\
+                               rb[k] = curval;                                 
\
+                               if (is_##TPE##_nil(curval))                     
\
+                                       has_nils = true;                        
\
+                               else                                            
\
+                                       curval = TPE##_nil;     /* For the next 
iteration */    \
+                       }               \
+               }               \
+               }       \
+       } while (0)
+
+#define ANALYTICAL_MIN_MAX_CALC_VARSIZED(SIGN_OP)                              
\
+       do {                                                            \
+               curval = (void*) nil; \
+               switch (frame_type) {           \
+               case 3: /* unbounded until current row */       {       \
+                       for (; k < i; k++) { \
+                               void *next = BUNtail(bpi, k);   \
+                               if (atomcmp(next, nil) != 0) {          \
+                                       if (atomcmp(curval, nil) == 0)  \
+                                               curval = next;          \
+                                       else                            \
+                                               curval = atomcmp(next, curval) 
SIGN_OP 0 ? curval : next; \
+                               }                                       \
+                               if (tfastins_nocheckVAR(r, k, curval, Tsize(r)) 
!= GDK_SUCCEED) \
+                                       return GDK_FAIL; \
+                               has_nils |= atomcmp(curval, nil) == 0;          
\
+                       } \
+               } break;                \
+               case 4: /* current row until unbounded */       {       \
+                       for (j = i - 1; j >= k; j--) { \
+                               void *next = BUNtail(bpi, j);   \
+                               if (atomcmp(next, nil) != 0) {          \
+                                       if (atomcmp(curval, nil) == 0)  \
+                                               curval = next;          \
+                                       else                            \
+                                               curval = atomcmp(next, curval) 
SIGN_OP 0 ? curval : next; \
+                               }                                       \
+                               if (tfastins_nocheckVAR(r, j, curval, Tsize(r)) 
!= GDK_SUCCEED) \
+                                       return GDK_FAIL; \
+                               has_nils |= atomcmp(curval, nil) == 0;          
\
+                       } \
+               } break;                \
+               case 5: /* all rows */  {       \
+                       for (j = k; j < i; j++) { \
+                               void *next = BUNtail(bpi, j);   \
+                               if (atomcmp(next, nil) != 0) {          \
+                                       if (atomcmp(curval, nil) == 0)  \
+                                               curval = next;          \
+                                       else                            \
+                                               curval = atomcmp(next, curval) 
SIGN_OP 0 ? curval : next; \
+                               }                                       \
+                       } \
+                       for (; k < i; k++) \
+                               if (tfastins_nocheckVAR(r, k, curval, Tsize(r)) 
!= GDK_SUCCEED) \
+                                       return GDK_FAIL; \
+                       has_nils |= atomcmp(curval, nil) == 0;          \
+               } break;                \
+               case 6: /* current row */ {     \
+                       for (; k < i; k++) { \
+                               void *next = BUNtail(bpi, k); \
+                               if (tfastins_nocheckVAR(r, k, next, Tsize(r)) 
!= GDK_SUCCEED) \
+                                       return GDK_FAIL; \
+                               has_nils |= atomcmp(next, nil) == 0;            
\
+                       } \
+               } break;                \
+               default: {              \
+                       for (; k < i; k++) { \
+                               j = start[k];                                   
\
+                               l = end[k];                                     
\
+                               curval = (void*) nil;                           
\
+                               for (;j < l; j++) {                             
\
+                                       void *next = BUNtail(bpi, j);   \
+                                       if (atomcmp(next, nil) != 0) {          
\
+                                               if (atomcmp(curval, nil) == 0)  
\
+                                                       curval = next;          
\
+                                               else                            
\
+                                                       curval = atomcmp(next, 
curval) SIGN_OP 0 ? curval : next; \
+                                       }                                       
\
+                               }                                               
\
+                               if (tfastins_nocheckVAR(r, k, curval, Tsize(r)) 
!= GDK_SUCCEED) \
+                                       return GDK_FAIL; \
+                               has_nils |= atomcmp(curval, nil) == 0;          
\
+                       }                                                       
\
+               }               \
+               }       \
+       } while (0)
+
+#define ANALYTICAL_MIN_MAX_PARTITIONS(TPE, OP)         \
+       do {                                    \
+               if (p) {                                        \
+                       for (; i < cnt; i++) {          \
+                               if (np[i])                      \
+                                       ANALYTICAL_MIN_MAX_CALC_FIXED(TPE, OP); 
\
                        }                                               \
-                       *rb = curval;                                   \
-                       if (is_##TPE##_nil(curval))                     \
-                               has_nils = true;                        \
-                       else                                            \
-                               curval = TPE##_nil;     /* For the next 
iteration */    \
+                       i = cnt;                        \
+                       ANALYTICAL_MIN_MAX_CALC_FIXED(TPE, OP); \
+               } else {                                \
+                       i = cnt;                                        \
+                       ANALYTICAL_MIN_MAX_CALC_FIXED(TPE, OP); \
                }                                                       \
        } while (0)
 
 #ifdef HAVE_HGE
 #define ANALYTICAL_MIN_MAX_LIMIT(OP)                   \
        case TYPE_hge:                                  \
-               ANALYTICAL_MIN_MAX_CALC(hge, OP);       \
+               ANALYTICAL_MIN_MAX_PARTITIONS(hge, OP); \
        break;
 #else
 #define ANALYTICAL_MIN_MAX_LIMIT(OP)
@@ -945,61 +1087,52 @@ GDKanalyticallead(BAT *r, BAT *b, BAT *p
 
 #define ANALYTICAL_MIN_MAX(OP, IMP, SIGN_OP)                           \
 gdk_return                                                             \
-GDKanalytical##OP(BAT *r, BAT *b, BAT *s, BAT *e, int tpe)             \
+GDKanalytical##OP(BAT *r, BAT *p, BAT *b, BAT *s, BAT *e, int tpe, int 
frame_type)             \
 {                                                                      \
        bool has_nils = false;                                          \
-       BUN i = 0, cnt = BATcount(b);                                   \
-       lng *restrict start, *restrict end, j = 0, l = 0;               \
-                                                                       \
-       assert(s && e);                                                 \
-       start = (lng*)Tloc(s, 0);                                       \
-       end = (lng*)Tloc(e, 0);                                         \
+       lng i = 0, j = 0, k = 0, l = 0, cnt = (lng) BATcount(b);                
                        \
+       lng *restrict start = s ? (lng*)Tloc(s, 0) : NULL, *restrict end = e ? 
(lng*)Tloc(e, 0) : NULL;         \
+       bit *restrict np = p ? Tloc(p, 0) : NULL;       \
                                                                        \
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to