Changeset: dcced2474e90 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=dcced2474e90
Modified Files:
        gdk/gdk_relop.mx
        
sql/test/BugTracker-2009/Tests/use_order_column_first.SF-2686008.stable.out
Branch: Feb2013
Log Message:

BATfetchjoin(): derive sortedness of result tail on-the-fly,
whenever possible and whenever we cannot predict it easily;
otherwise, resort to BATderiveHeadProps() (scan, only).


diffs (truncated from 541 to 300 lines):

diff --git a/gdk/gdk_relop.mx b/gdk/gdk_relop.mx
--- a/gdk/gdk_relop.mx
+++ b/gdk/gdk_relop.mx
@@ -548,16 +548,21 @@ BAThashjoin(BAT *l, BAT *r, BUN estimate
 #define HVATOMput(bn, dst) Hputvalue(bn, dst, BUNhvar(li,l_cur), 1)
 #define TLATOMput(bn, dst) ATOMput(bn->ttype, bn->T->vheap, dst, 
BUNtloc(ri,r_cur))
 #define TVATOMput(bn, dst) Tputvalue(bn, dst, BUNtvar(ri,r_cur), 1)
+#define LATOM_cmp(bn, p,n) atom_CMP(p, n, bn->ttype)
+#define VATOM_cmp(bn, p,n) atom_CMP(p, n, bn->ttype)
 
 @= SIMPLEput
 #define H@1put(bn,dst) *(@1*) (dst) = *(@1*) (BUNhloc(li,l_cur))
 #define T@1put(bn,dst) *(@1*) (dst) = *(@1*) (BUNtloc(ri,r_cur))
+#define @1_cmp(bn,p,n)  simple_CMP(p, n, @1)
 @
 @c
 @:SIMPLEput(bte)@
 @:SIMPLEput(sht)@
 @:SIMPLEput(int)@
+@:SIMPLEput(flt)@
 @:SIMPLEput(lng)@
+@:SIMPLEput(dbl)@
 
 @= bunfastins_nocheck_
        H@1put(bn, BUNhloc(bni, dst));
@@ -575,6 +580,7 @@ densefetchjoin_@1_@2(BAT *bn, BAT *l, BA
        BATiter bni = bat_iterator(bn);
        BATiter li = bat_iterator(l);
        BATiter ri = bat_iterator(r);
+       void *prev = NULL, *next = NULL;
 
        if (bn == NULL)
                return NULL;
@@ -583,10 +589,20 @@ densefetchjoin_@1_@2(BAT *bn, BAT *l, BA
        offset = (ssize_t) base - (ssize_t) r->hseqbase; /* cast first, 
subtract second */
        r_cur = (BUN) (offset + *(oid *) BUNtail(li, BUNfirst(l)));
 
-       ALGODEBUG fprintf(stderr, "#BATfetchjoin: densefetchjoin(@1,@2);\n");
+       ALGODEBUG fprintf(stderr, "#BATfetchjoin: densefetchjoin(@1,@2,@3);\n");
 
        BATloop(l, l_cur, l_end) {
                @:bunfastins_nocheck_(@1,@2)@
+               if (bn->tsorted || bn->trevsorted) {
+                       next = BUNt@3(bni,dst);
+                       if (bn->tsorted && prev && @2_cmp(bn,prev,next) > 0) {
+                               bn->tsorted = 0;
+                       }
+                       if (bn->trevsorted && prev && @2_cmp(bn,prev,next) < 0) 
{
+                               bn->trevsorted = 0;
+                       }
+                       prev = next;
+               }
                r_cur++;
                dst++;
        }
@@ -609,6 +625,7 @@ orderedfetchjoin_@1_@2(BAT *bn, BAT *l, 
        BATiter bni = bat_iterator(bn);
        BATiter li = bat_iterator(l);
        BATiter ri = bat_iterator(r);
+       void *prev = NULL, *next = NULL;
 
        if (bn == NULL)
                return NULL;
@@ -616,12 +633,22 @@ orderedfetchjoin_@1_@2(BAT *bn, BAT *l, 
        base = BUNfirst(r);
        offset = (ssize_t) base - (ssize_t) r->hseqbase; /* cast first, 
subtract second */
 
-       ALGODEBUG fprintf(stderr, "#BATfetchjoin: orderedfetchjoin(@1,@2);\n");
+       ALGODEBUG fprintf(stderr, "#BATfetchjoin: 
orderedfetchjoin(@1,@2,@3);\n");
 
        BATloop(l, l_cur, l_end) {
                yy = (BUN) (offset + *(oid *) BUNtail(li, l_cur));
                r_cur = yy;
                @:bunfastins_nocheck_(@1,@2)@
+               if (bn->tsorted || bn->trevsorted) {
+                       next = BUNt@3(bni,dst);
+                       if (bn->tsorted && prev && @2_cmp(bn,prev,next) > 0) {
+                               bn->tsorted = 0;
+                       }
+                       if (bn->trevsorted && prev && @2_cmp(bn,prev,next) < 0) 
{
+                               bn->trevsorted = 0;
+                       }
+                       prev = next;
+               }
                dst++;
        }
        BATsetcount(bn, dst);
@@ -643,7 +670,7 @@ defaultfetchjoin_@1_@2(BAT *bn, BAT *l, 
        BATiter bni = bat_iterator(bn);
        BATiter li = bat_iterator(l);
        BATiter ri = bat_iterator(r);
-
+       void *prev = NULL, *next = NULL;
 
        if (bn == NULL)
                return NULL;
@@ -652,7 +679,7 @@ defaultfetchjoin_@1_@2(BAT *bn, BAT *l, 
        offset = (ssize_t) base - (ssize_t) r->hseqbase; /* cast first, 
subtract second */
        end = base + BATcount(r);
 
-       ALGODEBUG fprintf(stderr, "#BATfetchjoin: defaultfetchjoin(@1,@2);\n");
+       ALGODEBUG fprintf(stderr, "#BATfetchjoin: 
defaultfetchjoin(@1,@2,@3);\n");
 
        BATloop(l, l_cur, l_end) {
                yy = (BUN) (offset + *(oid *) BUNtail(li, l_cur));
@@ -661,6 +688,16 @@ defaultfetchjoin_@1_@2(BAT *bn, BAT *l, 
                }
                r_cur = yy;
                @:bunfastins_nocheck_(@1,@2)@
+               if (bn->tsorted || bn->trevsorted) {
+                       next = BUNt@3(bni,dst);
+                       if (bn->tsorted && prev && @2_cmp(bn,prev,next) > 0) {
+                               bn->tsorted = 0;
+                       }
+                       if (bn->trevsorted && prev && @2_cmp(bn,prev,next) < 0) 
{
+                               bn->trevsorted = 0;
+                       }
+                       prev = next;
+               }
                dst++;
        }
        BATsetcount(bn, dst);
@@ -674,12 +711,14 @@ defaultfetchjoin_@1_@2(BAT *bn, BAT *l, 
 @
 @c
 @= fetchjoin2
-       @:fetchjoin(@1,bte)@
-       @:fetchjoin(@1,sht)@
-       @:fetchjoin(@1,int)@
-       @:fetchjoin(@1,lng)@
-       @:fetchjoin(@1,VATOM)@
-       @:fetchjoin(@1,LATOM)@
+       @:fetchjoin(@1,bte,loc)@
+       @:fetchjoin(@1,sht,loc)@
+       @:fetchjoin(@1,int,loc)@
+       @:fetchjoin(@1,flt,loc)@
+       @:fetchjoin(@1,lng,loc)@
+       @:fetchjoin(@1,dbl,loc)@
+       @:fetchjoin(@1,VATOM,var)@
+       @:fetchjoin(@1,LATOM,loc)@
 @
 @c
 @:fetchjoin2(bte)@
@@ -695,20 +734,14 @@ defaultfetchjoin_@1_@2(BAT *bn, BAT *l, 
                bn = @1fetchjoin_@2_bte(bn, l, r);
        } else if (ATOMstorage(rtt) == TYPE_sht) {
                bn = @1fetchjoin_@2_sht(bn, l, r);
-       } else if (ATOMstorage(rtt) == TYPE_int ||
-                  ATOMstorage(rtt) == TYPE_flt
-#if SIZEOF_OID == SIZEOF_INT
-                  || ATOMstorage(rtt) == TYPE_oid
-#endif
-               ) {
+       } else if (ATOMstorage(rtt) == TYPE_int) {
                bn = @1fetchjoin_@2_int(bn, l, r);
-       } else if (ATOMstorage(rtt) == TYPE_lng ||
-                  ATOMstorage(rtt) == TYPE_dbl
-#if SIZEOF_OID == SIZEOF_LNG
-                  || ATOMstorage(lht) == TYPE_oid
-#endif
-               ) {
+       } else if (ATOMstorage(rtt) == TYPE_flt) {
+               bn = @1fetchjoin_@2_flt(bn, l, r);
+       } else if (ATOMstorage(rtt) == TYPE_lng) {
                bn = @1fetchjoin_@2_lng(bn, l, r);
+       } else if (ATOMstorage(rtt) == TYPE_dbl) {
+               bn = @1fetchjoin_@2_dbl(bn, l, r);
        } else if (r->tvarsized) {
                bn = @1fetchjoin_@2_VATOM(bn, l, r);
        } else {
@@ -722,18 +755,10 @@ defaultfetchjoin_@1_@2(BAT *bn, BAT *l, 
        } else if (ATOMstorage(lht) == TYPE_sht) {
                @:fetchjoin_switch_rtt(@1,sht)@
        } else if (ATOMstorage(lht) == TYPE_int ||
-                  ATOMstorage(lht) == TYPE_flt
-#if SIZEOF_OID == SIZEOF_INT
-                  || ATOMstorage(lht) == TYPE_oid
-#endif
-               ) {
+                  ATOMstorage(lht) == TYPE_flt) {
                @:fetchjoin_switch_rtt(@1,int)@
        } else if (ATOMstorage(lht) == TYPE_lng ||
-                  ATOMstorage(lht) == TYPE_dbl
-#if SIZEOF_OID == SIZEOF_LNG
-                  || ATOMstorage(lht) == TYPE_oid
-#endif
-               ) {
+                  ATOMstorage(lht) == TYPE_dbl) {
                @:fetchjoin_switch_rtt(@1,lng)@
        } else if (l->hvarsized) {
                @:fetchjoin_switch_rtt(@1,VATOM)@
@@ -743,50 +768,84 @@ defaultfetchjoin_@1_@2(BAT *bn, BAT *l, 
 @
 @c
 @= densevoidfetchjoin
-       ALGODEBUG fprintf(stderr, "#BATfetchjoin: densevoidfetchjoin(@1);\n");
+       ALGODEBUG fprintf(stderr, "#BATfetchjoin: 
densevoidfetchjoin(@1,@2);\n");
        r_cur = (BUN) (offset + * (oid *) BUNtloc(li, BUNfirst(l)));
        BATloop(l, l_cur, l_end) {
-               @1put(bn, Tloc(bn,dst));
+               T@1put(bn, Tloc(bn,dst));
+               if (bn->tsorted || bn->trevsorted) {
+                       next = BUNt@2(bni,dst);
+                       if (bn->tsorted && prev && @1_cmp(bn,prev,next) > 0) {
+                               bn->tsorted = 0;
+                       }
+                       if (bn->trevsorted && prev && @1_cmp(bn,prev,next) < 0) 
{
+                               bn->trevsorted = 0;
+                       }
+                       prev = next;
+               }
                r_cur++;
                dst++;
        }
 @
 @c
 @= orderedvoidfetchjoin
-       ALGODEBUG fprintf(stderr, "#BATfetchjoin: orderedvoidfetchjoin(@1);\n");
+       ALGODEBUG fprintf(stderr, "#BATfetchjoin: 
orderedvoidfetchjoin(@1,@2);\n");
        BATloop(l, l_cur, l_end) {
                BUN _yy = (BUN) (offset + * (oid *) BUNtloc(li, l_cur));
 
                r_cur = _yy;
-               @1put(bn, Tloc(bn,dst));
+               T@1put(bn, Tloc(bn,dst));
+               if (bn->tsorted || bn->trevsorted) {
+                       next = BUNt@2(bni,dst);
+                       if (bn->tsorted && prev && @1_cmp(bn,prev,next) > 0) {
+                               bn->tsorted = 0;
+                       }
+                       if (bn->trevsorted && prev && @1_cmp(bn,prev,next) < 0) 
{
+                               bn->trevsorted = 0;
+                       }
+                       prev = next;
+               }
                dst++;
        }
 @
 @c
 @= defaultvoidfetchjoin
-       ALGODEBUG fprintf(stderr, "#BATfetchjoin: defaultvoidfetchjoin(@1);\n");
+       ALGODEBUG fprintf(stderr, "#BATfetchjoin: 
defaultvoidfetchjoin(@1,@2);\n");
        BATloop(l, l_cur, l_end) {
                BUN _yy = (BUN) (offset + * (oid *) BUNtloc(li, l_cur));
 
                if (_yy < base || _yy >= end) {
+                       bntsorted = bn->tsorted;
+                       bntrevsorted = bn->trevsorted;
                        BBPreclaim(bn);
                        bn = NULL;
                        nondense = 1;
+                       prev = next = NULL;
                        break;
                }
                r_cur = _yy;
-               @1put(bn, Tloc(bn,dst));
+               T@1put(bn, Tloc(bn,dst));
+               if (bn->tsorted || bn->trevsorted) {
+                       next = BUNt@2(bni,dst);
+                       if (bn->tsorted && prev && @1_cmp(bn,prev,next) > 0) {
+                               bn->tsorted = 0;
+                       }
+                       if (bn->trevsorted && prev && @1_cmp(bn,prev,next) < 0) 
{
+                               bn->trevsorted = 0;
+                       }
+                       prev = next;
+               }
                dst++;
        }
        if (nondense) {
-               BATiter bni;
                /* not (yet?) completely type-optimized ! */
-               ALGODEBUG fprintf(stderr, "#BATfetchjoin: 
defaultvoidfetchjoin(@1): discovered non-density, resuming with non-void 
head\n");
+               ALGODEBUG fprintf(stderr, "#BATfetchjoin: 
defaultvoidfetchjoin(@1,@2): discovered non-density, resuming with non-void 
head\n");
                bn = BATnew(BAThtype(l), ATOMtype(tpe), BATcount(l));
                if (bn == NULL)
                        return bn;
                dst = BUNfirst(bn);
                bni = bat_iterator(bn);
+               bn->tsorted = bntsorted;
+               bn->trevsorted = bntrevsorted;
                BATloop(l, l_cur, l_end) {
                        BUN _yy = (BUN) (offset + * (oid *) BUNtail(li, l_cur));
 
@@ -795,27 +854,55 @@ defaultfetchjoin_@1_@2(BAT *bn, BAT *l, 
                        }
                        r_cur = _yy;
                        Hputvalue(bn, BUNhloc(bni, dst), BUNhead(li, l_cur), 1);
-                       @1put(bn, BUNtloc(bni, dst));
+                       T@1put(bn, BUNtloc(bni, dst));
+                       if (bn->tsorted || bn->trevsorted) {
+                               next = BUNt@2(bni,dst);
+                               if (bn->tsorted && prev && @1_cmp(bn,prev,next) 
> 0) {
+                                       bn->tsorted = 0;
+                               }
+                               if (bn->trevsorted && prev && 
@1_cmp(bn,prev,next) < 0) {
+                                       bn->trevsorted = 0;
+                               }
+                               prev = next;
+                       }
_______________________________________________
checkin-list mailing list
[email protected]
http://mail.monetdb.org/mailman/listinfo/checkin-list

Reply via email to