Changeset: ce0f899592a2 for MonetDB
URL: https://dev.monetdb.org/hg/MonetDB/rev/ce0f899592a2
Modified Files:
        clients/Tests/MAL-signatures-hge.test
        clients/Tests/MAL-signatures.test
        monetdb5/modules/mal/pp_algebra.c
        monetdb5/optimizer/opt_mergetable.c
        monetdb5/optimizer/opt_support.c
        sql/backends/monet5/rel_bin.c
        sql/backends/monet5/sql.c
        sql/backends/monet5/sql_pp_statement.c
Branch: pp_hashjoin
Log Message:

handle row_number in merge table optimizer
in pipeline optimized the distinct, if possible use result of uniques directly


diffs (truncated from 337 to 300 lines):

diff --git a/clients/Tests/MAL-signatures-hge.test 
b/clients/Tests/MAL-signatures-hge.test
--- a/clients/Tests/MAL-signatures-hge.test
+++ b/clients/Tests/MAL-signatures-hge.test
@@ -34550,6 +34550,11 @@ SQLrank
 return the ranked groups
 batsql
 row_number
+pattern batsql.row_number(X_0:bat[:any], X_1:oid) (X_2:bat[:int], X_3:oid)
+PBATSQLrow_number
+Returns the unique row number.
+batsql
+row_number
 pattern batsql.row_number(X_0:bat[:any_1], X_1:bat?[:bit], 
X_2:bat?[:bit]):bat[:int]
 SQLrow_number
 return the row_numer-ed groups
diff --git a/clients/Tests/MAL-signatures.test 
b/clients/Tests/MAL-signatures.test
--- a/clients/Tests/MAL-signatures.test
+++ b/clients/Tests/MAL-signatures.test
@@ -25705,6 +25705,11 @@ SQLrank
 return the ranked groups
 batsql
 row_number
+pattern batsql.row_number(X_0:bat[:any], X_1:oid) (X_2:bat[:int], X_3:oid)
+PBATSQLrow_number
+Returns the unique row number.
+batsql
+row_number
 pattern batsql.row_number(X_0:bat[:any_1], X_1:bat?[:bit], 
X_2:bat?[:bit]):bat[:int]
 SQLrow_number
 return the row_numer-ed groups
diff --git a/monetdb5/modules/mal/pp_algebra.c 
b/monetdb5/modules/mal/pp_algebra.c
--- a/monetdb5/modules/mal/pp_algebra.c
+++ b/monetdb5/modules/mal/pp_algebra.c
@@ -919,7 +919,7 @@ LOCKEDAGGRnull(Client ctx, bat *result, 
        return err;
 }
 
-#define unique_(Type, BaseType, INIT_ALLOCATOR, INIT_ITER, NEW_VAL, HASH_VAL, 
VAL_NOT_EQUAL, VAL_ASSIGN, ITER_NEXT, NEXTK) \
+#define unique_(Type, BaseType, INIT_ALLOCATOR, INIT_ITER, IS_NIL, NEW_VAL, 
HASH_VAL, VAL_NOT_EQUAL, VAL_ASSIGN, ITER_DONE, NEXTK) \
        if (tt == TYPE_##Type) {                                                
                                        \
                int slots = 0;                                                  
                                                \
                gid slot = 0;                                                   
                                                \
@@ -928,6 +928,8 @@ LOCKEDAGGRnull(Client ctx, bat *result, 
                INIT_ALLOCATOR;                                                 
                                                \
                TIMEOUT_LOOP_IDX_DECL(i, cnt, qry_ctx) {                        
                        \
                        bool new = 0, fnd = 0;                                  
                                        \
+                       if(IS_NIL)                                              
                                                        \
+                               continue;                                       
                                                        \
                        for(; !fnd; ) {                                         
                                                \
                                NEW_VAL;                                        
                                                        \
                                gid hv = HASH_VAL&h->mask, k = hv;              
                                \
@@ -963,7 +965,7 @@ LOCKEDAGGRnull(Client ctx, bat *result, 
                        if (new)                                                
                                                        \
                                gp[r++] = b->hseqbase + i;                      
                                        \
                }                                                               
                                                                \
-               ITER_NEXT;                                                      
                                                        \
+               ITER_DONE;                                                      
                                                        \
        }
 
 #define unique(Type)                                                   \
@@ -971,6 +973,7 @@ LOCKEDAGGRnull(Client ctx, bat *result, 
                        Type,                                                   
        \
                        ,                                                       
                \
                        Type *bp = Tloc(b, 0),                          \
+                       is_##Type##_nil(bp[i]),                         \
                        ,                                                       
                \
                        (gid)_hash_##Type(bp[i]),                       \
                        vals[g] != bp[i],                                       
\
@@ -984,6 +987,7 @@ LOCKEDAGGRnull(Client ctx, bat *result, 
                        BaseType,                                               
                                                        \
                        ,                                                       
                                                                \
                        Type *bp = Tloc(b, 0),                                  
                                        \
+                       is_##Type##_nil(bp[i]),                                 
                                        \
                        ,                                                       
                                                                \
                        (gid)_hash_##Type(*(((BaseType*)bp)+i)),                
                        \
                        (!(is_##Type##_nil(bp[i]) && is_##Type##_nil(vals[g])) 
&& vals[g] != bp[i]), \
@@ -997,6 +1001,7 @@ LOCKEDAGGRnull(Client ctx, bat *result, 
                        BaseType,                                               
                                                        \
                        ,                                                       
                                                                \
                        Type *bp = Tloc(b, 0),                                  
                                        \
+                       is_##Type##_nil(bp[i]),                                 
                                        \
                        ,                                                       
                                                                \
                        (gid)_hash_##Type(*(((BaseType*)bp)+i)),                
                        \
                        (!(is_##Type##_nil(bp[i]) && is_##Type##_nil(vals[g])) 
&& h->cmp(vals+g, bp+i) != 0), \
@@ -1010,6 +1015,7 @@ LOCKEDAGGRnull(Client ctx, bat *result, 
                        Type,                                                   
                \
                        allocator *ma = h->allocators[p->wid],  \
                        BATiter bi = bat_iterator(b),                   \
+                       VarHeapVal(bi.base,i,bi.width) == 0,    \
                        CType bpi = BUNtvar(&bi, i),                    \
                        (gid)h->hsh(bpi),                                       
        \
                        (h->cmp(vals[g], bpi) != 0),                    \
@@ -1023,6 +1029,7 @@ LOCKEDAGGRnull(Client ctx, bat *result, 
                        Type,                                                   
        \
                        ,                                                       
                \
                        BATiter bi = bat_iterator(b),           \
+                       VarHeapVal(bi.base,i,bi.width) == 0,\
                        CType bpi = BUNtvar(&bi, i),            \
                        (gid)h->hsh(bpi),                                       
\
                        (h->cmp(vals[g], bpi) != 0),            \
@@ -1034,6 +1041,7 @@ LOCKEDAGGRnull(Client ctx, bat *result, 
 static str
 LALGunique(Client ctx, bat *rid, bat *uid, bat *bid, bat *sid)
 {
+       /* TODO add skip nil (or not) */
        (void)ctx;
        Pipeline *p = pipeline_get_thread_private_pipeline();
        assert(!is_bat_nil(*uid));
@@ -1168,7 +1176,7 @@ error:
        return err;
 }
 
-#define gunique_(Type, BaseType, INIT_ALLOCATOR, INIT_ITER, NEW_VAL, HASH_VAL, 
VAL_NOT_EQUAL, VAL_ASSIGN, ITER_NEXT, NEXTK) \
+#define gunique_(Type, BaseType, INIT_ALLOCATOR, INIT_ITER, IS_NIL, NEW_VAL, 
HASH_VAL, VAL_NOT_EQUAL, VAL_ASSIGN, ITER_DONE, NEXTK) \
        if (tt == TYPE_##Type) {                                                
                                        \
                int slots = 0;                                                  
                                                \
                gid slot = 0;                                                   
                                                \
@@ -1177,6 +1185,8 @@ error:
                INIT_ALLOCATOR;                                                 
                                                \
                TIMEOUT_LOOP_IDX_DECL(i, cnt, qry_ctx) {                        
                        \
                        bool new = 0, fnd = 0;                                  
                                        \
+                       if(IS_NIL)                                              
                                                        \
+                               continue;                                       
                                                        \
                        for(; !fnd;) {                                          
                                                \
                                NEW_VAL;                                        
                                                        \
                                gid hv = (gid)combine(gi[i], HASH_VAL, 
prime)&h->mask, k = hv; \
@@ -1215,7 +1225,7 @@ error:
                        if (new)                                                
                                                        \
                                gp[r++] = b->hseqbase + i;                      
                                        \
                }                                                               
                                                                \
-               ITER_NEXT;                                                      
                                                        \
+               ITER_DONE;                                                      
                                                        \
        }
 
 #define gunique(Type)                                                  \
@@ -1223,6 +1233,7 @@ error:
                         Type,                                                  
        \
                         ,                                                      
                \
                         Type *bp = Tloc(b, 0),                         \
+                       is_##Type##_nil(bp[i]),                         \
                         ,                                                      
                \
                         (gid)_hash_##Type(bp[i]),                      \
                         vals[g] != bp[i],                                      
\
@@ -1236,6 +1247,7 @@ error:
                         BaseType,                                              
                                                        \
                         ,                                                      
                                                                \
                         Type *bp = Tloc(b, 0),                                 
                                        \
+                       is_##Type##_nil(bp[i]),                                 
                                        \
                         ,                                                      
                                                                \
                         (gid)_hash_##Type(*(((BaseType*)bp)+i)),               
                        \
                         (!(is_##Type##_nil(bp[i]) && is_##Type##_nil(vals[g])) 
&& vals[g] != bp[i]), \
@@ -1249,6 +1261,7 @@ error:
                         BaseType,                                              
                                                        \
                         ,                                                      
                                                                \
                         Type *bp = Tloc(b, 0),                                 
                                        \
+                        is_##Type##_nil(bp[i]),                                
                                        \
                         ,                                                      
                                                                \
                         (gid)_hash_##Type(*(((BaseType*)bp)+i)),               
                        \
                         (!(is_##Type##_nil(bp[i]) && is_##Type##_nil(vals[g])) 
&& h->cmp(vals+g, bp+i) != 0), \
@@ -1262,6 +1275,7 @@ error:
                         Type,                                                  
                \
                         allocator *ma = h->allocators[p->wid], \
                         BATiter bi = bat_iterator(b),                  \
+                        VarHeapVal(bi.base,i,bi.width) == 0,   \
                         CType bpi = BUNtvar(&bi, i),                   \
                         (gid)h->hsh(bpi),                                      
        \
                         (h->cmp(vals[g], bpi) != 0),                   \
@@ -1270,17 +1284,18 @@ error:
                         nextk                                                  
                \
                )
 
-#define gaunique(Type,CType)                                   \
-       gunique_(Type,                                                          
\
-                        Type,                                                  
        \
-                        ,                                                      
                \
-                        BATiter bi = bat_iterator(b),          \
-                        CType bpi = BUNtvar(&bi, i),           \
-                        (gid)h->hsh(bpi),                                      
\
-                        (h->cmp(vals[g], bpi) != 0),           \
-                        vals[g] = (Type)bpi,                           \
-                        bat_iterator_end(&bi),                         \
-                        nextk                                                  
        \
+#define gaunique(Type,CType)                                           \
+       gunique_(Type,                                                          
        \
+                        Type,                                                  
                \
+                        ,                                                      
                        \
+                        BATiter bi = bat_iterator(b),                  \
+                        VarHeapVal(bi.base,i,bi.width) == 0,   \
+                        CType bpi = BUNtvar(&bi, i),                   \
+                        (gid)h->hsh(bpi),                                      
        \
+                        (h->cmp(vals[g], bpi) != 0),                   \
+                        vals[g] = (Type)bpi,                                   
\
+                        bat_iterator_end(&bi),                                 
\
+                        nextk                                                  
                \
                )
 
 static str
@@ -2748,6 +2763,7 @@ LALGcount(Client ctx, bat *rid, bat *gid
                gcount(date);
                gfcount(inet4);
                gcount(lng);
+               gcount(oid);
                gcount(daytime);
                gcount(timestamp);
 #ifdef HAVE_HGE
diff --git a/monetdb5/optimizer/opt_mergetable.c 
b/monetdb5/optimizer/opt_mergetable.c
--- a/monetdb5/optimizer/opt_mergetable.c
+++ b/monetdb5/optimizer/opt_mergetable.c
@@ -449,7 +449,9 @@ mat_apply1(allocator *ma, MalBlkPtr mb, 
        int tpe, k, is_select = isSelect(p),
                is_mirror = (getFunctionId(p) == mirrorRef);
        int is_identity = (getFunctionId(p) == identityRef
-                                          && getModuleId(p) == batcalcRef);
+                                          && getModuleId(p) == batcalcRef) ||
+                          (getFunctionId(p) == row_numberRef
+                                          && getModuleId(p) == batsqlRef);
        int ident_var = 0, is_assign = (getFunctionId(p) == NULL), n = 0;
        InstrPtr r = NULL, q;
        mat_t *mat = ml->v;
@@ -478,7 +480,10 @@ mat_apply1(allocator *ma, MalBlkPtr mb, 
                getArg(q, 0) = newTmpVariable(mb, TYPE_oid);
                q->retc = 1;
                q->argc = 1;
-               q = pushOid(mb, q, 0);
+               oid init = 0;
+               if (getFunctionId(p) == row_numberRef && getModuleId(p) == 
batsqlRef)
+                       init = 1;
+               q = pushOid(mb, q, init);
                ident_var = getArg(q, 0);
                pushInstruction(mb, q);
                if (mb->errors) {
@@ -546,6 +551,7 @@ mat_apply(allocator *ma, MalBlkPtr mb, I
 
        if (nrmats == 1
                && ((getModuleId(p) == batcalcRef && getFunctionId(p) == 
identityRef)
+                       || (getModuleId(p) == batsqlRef && getFunctionId(p) == 
row_numberRef)
                        || (getModuleId(p) == batRef && getFunctionId(p) == 
mirrorRef)))
                return mat_apply1(ma, mb, p, ml, is_a_mat(getArg(p, 1), ml), 1);
        assert(nrmats <= 8);
diff --git a/monetdb5/optimizer/opt_support.c b/monetdb5/optimizer/opt_support.c
--- a/monetdb5/optimizer/opt_support.c
+++ b/monetdb5/optimizer/opt_support.c
@@ -385,7 +385,7 @@ isOrderDepenent(InstrPtr p)
        if (getModuleId(p) != batsqlRef)
                return 0;
        if (getFunctionId(p) == diffRef || getFunctionId(p) == window_boundRef
-               || getFunctionId(p) == row_numberRef || getFunctionId(p) == 
rankRef
+               /*|| getFunctionId(p) == row_numberRef*/ || getFunctionId(p) == 
rankRef
                || getFunctionId(p) == dense_rankRef
                || getFunctionId(p) == percent_rankRef
                || getFunctionId(p) == cume_distRef || getFunctionId(p) == 
ntileRef
@@ -419,6 +419,7 @@ isMap2Op(InstrPtr p)
        return getModuleId(p)
                        && ((getModuleId(p) == malRef && getFunctionId(p) == 
multiplexRef)
                                || (getModuleId(p) == malRef && 
getFunctionId(p) == manifoldRef)
+                               || (getModuleId(p) == batsqlRef && 
getFunctionId(p) == row_numberRef)
                                || (getModuleId(p) == batcalcRef)
                                || (getModuleId(p) != batcalcRef && 
getModuleId(p) != batRef
                                        && strncmp(getModuleId(p), "bat", 3) == 
0)
diff --git a/sql/backends/monet5/rel_bin.c b/sql/backends/monet5/rel_bin.c
--- a/sql/backends/monet5/rel_bin.c
+++ b/sql/backends/monet5/rel_bin.c
@@ -1865,7 +1865,7 @@ exp_bin(backend *be, sql_exp *e, stmt *l
 
                                if (as && as->nrcols <= 0 && left && 
(!is_const_func(a, attr) || grp))
                                        as = stmt_const(be, 
bin_find_smallest_column(be, left), as);
-                               if (en == attr->h && !en->next && 
exp_aggr_is_count(e) && 0)
+                               if (en == attr->h && !en->next && 
exp_aggr_is_count(e) && !need_distinct(e) && 0)
                                        as = exp_count_no_nil_arg(e, ext, at, 
as);
                                /* insert single value into a column */
                                if (as && as->nrcols <= 0 && !left)
@@ -1918,7 +1918,10 @@ exp_bin(backend *be, sql_exp *e, stmt *l
                                        return NULL;
                                if (be->pipeline && grp)
                                        grp = stmt_project(be, u, grp);
-                               append(l, stmt_project(be, u, a));
+                               if (be->pipeline && exp_aggr_is_count(e))
+                                       append(l, u);
+                               else
+                                       append(l, stmt_project(be, u, a));
                        }
                        if (r) { /* check new ordered aggregation */
                                list *obe = r->h->data;
diff --git a/sql/backends/monet5/sql.c b/sql/backends/monet5/sql.c
--- a/sql/backends/monet5/sql.c
+++ b/sql/backends/monet5/sql.c
@@ -3363,6 +3363,31 @@ PBATSQLidentity(Client cntxt, MalBlkPtr 
        return MAL_SUCCEED;
 }
 
+static str
+PBATSQLrow_number(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
+{
+       bat *res = getArgReference_bat(stk, pci, 0);
+       oid *ns = getArgReference_oid(stk, pci, 1);
+       bat bid = *getArgReference_bat(stk, pci, 2);
+       oid s = *getArgReference_oid(stk, pci, 3);
+       BAT *b, *bn = NULL;
+
+       (void) cntxt;
_______________________________________________
checkin-list mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to