Changeset: 1495c93361c0 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=1495c93361c0
Modified Files:
        sql/backends/monet5/sql_rank.c
        sql/test/analytics/Tests/analytics01.stable.out
Branch: Nov2019
Log Message:

Fixed bug at cume_dist window function where ordering was not taken in account 
for the output of the function


diffs (truncated from 2132 to 300 lines):

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
@@ -558,57 +558,90 @@ SQLcume_dist(Client cntxt, MalBlkPtr mb,
        (void)cntxt;
        if (isaBatType(getArgType(mb, pci, 1))) {
                bat *res = getArgReference_bat(stk, pci, 0);
-               BAT *b = BATdescriptor(*getArgReference_bat(stk, pci, 1)), *p, 
*r;
-               BUN cnt;
-               int j;
-               dbl *rb, *rp, *end, cnt_cast;
-               bit *np;
+               BAT *b = BATdescriptor(*getArgReference_bat(stk, pci, 1)), *p, 
*o, *r;
+               BUN ncnt, j = 0;
+               bit *np, *no, *bo1, *bo2, *end;
+               dbl *rb, *rp, cnt_cast, nres;
 
                if (!b)
                        throw(SQL, "sql.cume_dist", SQLSTATE(HY005) "Cannot 
access column descriptor");
-               cnt = BATcount(b);
-               cnt_cast = (dbl) cnt;
-               voidresultBAT(r, TYPE_dbl, cnt, b, "sql.cume_dist");
+               voidresultBAT(r, TYPE_dbl, BATcount(b), b, "sql.cume_dist");
                rb = rp = (dbl*)Tloc(r, 0);
-               end = rp + cnt;
                if (isaBatType(getArgType(mb, pci, 2))) {
                        if (isaBatType(getArgType(mb, pci, 3))) {
                                p = BATdescriptor(*getArgReference_bat(stk, 
pci, 2));
-                               if (!p) {
+                               o = BATdescriptor(*getArgReference_bat(stk, 
pci, 3));
+                               if (!p || !o) {
                                        BBPunfix(b->batCacheid);
+                                       if (p) BBPunfix(p->batCacheid);
+                                       if (o) BBPunfix(o->batCacheid);
                                        throw(SQL, "sql.cume_dist", 
SQLSTATE(HY005) "Cannot access column descriptor");
                                }
                                np = (bit*)Tloc(p, 0);
-                               for(j=0; rp<end; j++, np++, rp++) {
+                               end = np + BATcount(p);
+                               bo1 = bo2 = no = (bit*)Tloc(o, 0);
+
+                               for (; np<end; np++, no++) {
                                        if (*np) {
-                                               for(; rb<rp; rb++)
-                                                       *rb = j / cnt_cast;
+                                               ncnt = no - bo2;
+                                               cnt_cast = (dbl) ncnt;
+                                               for (; bo2<no; bo2++) {
+                                                       if (*bo2) {
+                                                               j += (bo2 - 
bo1);
+                                                               nres = j / 
cnt_cast;
+                                                               for (; bo1 < 
bo2; bo1++, rb++)
+                                                                       *rb = 
nres;
+                                                       }
+                                               }
+                                               for (; bo1 < bo2; bo1++, rb++)
+                                                       *rb = 1;
                                        }
                                }
-                               for(; rb<rp; rb++)
+                               ncnt = no - bo2;
+                               cnt_cast = (dbl) ncnt;
+                               for (; bo2<no; bo2++) {
+                                       if (*bo2) {
+                                               j += (bo2 - bo1);
+                                               nres = j / cnt_cast;
+                                               for (; bo1 < bo2; bo1++, rb++)
+                                                       *rb = nres;
+                                       }
+                               }
+                               for (; bo1 < bo2; bo1++, rb++)
                                        *rb = 1;
                        } else { /* single value, ie no ordering */
-                               p = BATdescriptor(*getArgReference_bat(stk, 
pci, 2));
-                               if (!p) {
+                               rp = rb + BATcount(b);
+                               for (; rb<rp; rb++)
+                                       *rb = 1;
+                       }
+               } else { /* single value, ie no partitions */
+                       if (isaBatType(getArgType(mb, pci, 3))) {
+                               o = BATdescriptor(*getArgReference_bat(stk, 
pci, 3));
+                               if (!o) {
                                        BBPunfix(b->batCacheid);
                                        throw(SQL, "sql.cume_dist", 
SQLSTATE(HY005) "Cannot access column descriptor");
                                }
-                               np = (bit*)Tloc(p, 0);
-                               for(j=0; rp<end; j++, np++, rp++) {
-                                       if (*np) {
-                                               for(; rb<rp; rb++)
-                                                       *rb = j / cnt_cast;
+                               bo1 = bo2 = (bit*)Tloc(o, 0);
+                               no = bo1 + BATcount(b);
+                               cnt_cast = (dbl) BATcount(b);
+                               for (; bo2<no; bo2++) {
+                                       if (*bo2) {
+                                               j += (bo2 - bo1);
+                                               nres = j / cnt_cast;
+                                               for (; bo1 < bo2; bo1++, rb++)
+                                                       *rb = nres;
                                        }
                                }
-                               for(; rb<rp; rb++)
+                               for (; bo1 < bo2; bo1++, rb++)
                                        *rb = 1;
-                               BBPunfix(p->batCacheid);
+                               BBPunfix(o->batCacheid);
+                       } else { /* single value, ie no ordering */
+                               rp = rb + BATcount(b);
+                               for (; rb<rp; rb++)
+                                       *rb = 1;
                        }
-               } else {
-                       for(; rp<end; rp++)
-                               *rp = 1;
                }
-               BATsetcount(r, cnt);
+               BATsetcount(r, BATcount(b));
                BBPunfix(b->batCacheid);
                BBPkeepref(*res = r->batCacheid);
        } else {
diff --git a/sql/test/analytics/Tests/analytics01.stable.out 
b/sql/test/analytics/Tests/analytics01.stable.out
--- a/sql/test/analytics/Tests/analytics01.stable.out
+++ b/sql/test/analytics/Tests/analytics01.stable.out
@@ -28,8 +28,8 @@ stdout of test 'analytics01` in director
 [ 10   ]
 #start transaction;
 #select percent_rank() over (partition by aa) from analytics;
-% sys.L3 # table_name
-% L3 # name
+% sys.L2 # table_name
+% L2 # name
 % double # type
 % 24 # length
 [ 0    ]
@@ -43,8 +43,8 @@ stdout of test 'analytics01` in director
 [ 0    ]
 [ 0    ]
 #select percent_rank() over (partition by aa order by aa asc) from analytics;
-% sys.L3 # table_name
-% L3 # name
+% sys.L2 # table_name
+% L2 # name
 % double # type
 % 24 # length
 [ 0    ]
@@ -58,8 +58,8 @@ stdout of test 'analytics01` in director
 [ 0    ]
 [ 0    ]
 #select percent_rank() over (partition by aa order by aa desc) from analytics;
-% sys.L3 # table_name
-% L3 # name
+% sys.L2 # table_name
+% L2 # name
 % double # type
 % 24 # length
 [ 0    ]
@@ -73,8 +73,8 @@ stdout of test 'analytics01` in director
 [ 0    ]
 [ 0    ]
 #select percent_rank() over (order by aa) from analytics;
-% sys.L3 # table_name
-% L3 # name
+% sys.L2 # table_name
+% L2 # name
 % double # type
 % 24 # length
 [ 0    ]
@@ -88,8 +88,8 @@ stdout of test 'analytics01` in director
 [ 0.8888888889 ]
 [ 1    ]
 #select percent_rank() over (order by aa desc) from analytics;
-% sys.L3 # table_name
-% L3 # name
+% sys.L2 # table_name
+% L2 # name
 % double # type
 % 24 # length
 [ 0    ]
@@ -103,8 +103,8 @@ stdout of test 'analytics01` in director
 [ 0.8888888889 ]
 [ 0.8888888889 ]
 #select percent_rank() over (partition by bb) from analytics;
-% sys.L3 # table_name
-% L3 # name
+% sys.L2 # table_name
+% L2 # name
 % double # type
 % 24 # length
 [ 0    ]
@@ -118,8 +118,8 @@ stdout of test 'analytics01` in director
 [ 0    ]
 [ 0    ]
 #select percent_rank() over (partition by bb order by bb asc) from analytics;
-% sys.L3 # table_name
-% L3 # name
+% sys.L2 # table_name
+% L2 # name
 % double # type
 % 24 # length
 [ 0    ]
@@ -133,8 +133,8 @@ stdout of test 'analytics01` in director
 [ 0    ]
 [ 0    ]
 #select percent_rank() over (partition by bb order by bb desc) from analytics;
-% sys.L3 # table_name
-% L3 # name
+% sys.L2 # table_name
+% L2 # name
 % double # type
 % 24 # length
 [ 0    ]
@@ -148,8 +148,8 @@ stdout of test 'analytics01` in director
 [ 0    ]
 [ 0    ]
 #select percent_rank() over (order by bb) from analytics;
-% sys.L3 # table_name
-% L3 # name
+% sys.L2 # table_name
+% L2 # name
 % double # type
 % 24 # length
 [ 0    ]
@@ -163,8 +163,8 @@ stdout of test 'analytics01` in director
 [ 0.6666666667 ]
 [ 1    ]
 #select percent_rank() over (order by bb desc) from analytics;
-% sys.L3 # table_name
-% L3 # name
+% sys.L2 # table_name
+% L2 # name
 % double # type
 % 24 # length
 [ 0    ]
@@ -178,53 +178,8 @@ stdout of test 'analytics01` in director
 [ 0.7777777778 ]
 [ 0.7777777778 ]
 #select cume_dist() over (partition by aa) from analytics;
-% sys.L3 # table_name
-% L3 # name
-% double # type
-% 24 # length
-[ 0.2  ]
-[ 0.2  ]
-[ 0.3  ]
-[ 0.5  ]
-[ 0.5  ]
-[ 0.6  ]
-[ 0.7  ]
-[ 0.8  ]
-[ 0.9  ]
-[ 1    ]
-#select cume_dist() over (partition by aa order by aa asc) from analytics;
-% sys.L3 # table_name
-% L3 # name
-% double # type
-% 24 # length
-[ 0.2  ]
-[ 0.2  ]
-[ 0.3  ]
-[ 0.5  ]
-[ 0.5  ]
-[ 0.6  ]
-[ 0.7  ]
-[ 0.8  ]
-[ 0.9  ]
-[ 1    ]
-#select cume_dist() over (partition by aa order by aa desc) from analytics;
-% sys.L3 # table_name
-% L3 # name
-% double # type
-% 24 # length
-[ 0.2  ]
-[ 0.2  ]
-[ 0.3  ]
-[ 0.4  ]
-[ 0.5  ]
-[ 0.6  ]
-[ 0.7  ]
-[ 0.9  ]
-[ 0.9  ]
-[ 1    ]
-#select cume_dist() over (order by aa) from analytics;
-% sys.L3 # table_name
-% L3 # name
+% sys.L2 # table_name
+% L2 # name
 % double # type
 % 24 # length
 [ 1    ]
@@ -237,9 +192,24 @@ stdout of test 'analytics01` in director
 [ 1    ]
 [ 1    ]
 [ 1    ]
-#select cume_dist() over (order by aa desc) from analytics;
-% sys.L3 # table_name
-% L3 # name
+#select cume_dist() over (partition by aa order by aa asc) from analytics;
+% sys.L2 # table_name
+% L2 # name
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to