LCOV - code coverage report
Current view: top level - gdk - gdk_join.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 1988 2999 66.3 %
Date: 2024-11-13 22:44:48 Functions: 27 30 90.0 %

          Line data    Source code
       1             : /*
       2             :  * SPDX-License-Identifier: MPL-2.0
       3             :  *
       4             :  * This Source Code Form is subject to the terms of the Mozilla Public
       5             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       6             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       7             :  *
       8             :  * Copyright 2024 MonetDB Foundation;
       9             :  * Copyright August 2008 - 2023 MonetDB B.V.;
      10             :  * Copyright 1997 - July 2008 CWI.
      11             :  */
      12             : 
      13             : #include "monetdb_config.h"
      14             : #include "gdk.h"
      15             : #include "gdk_private.h"
      16             : #include "gdk_calc_private.h"
      17             : 
      18             : /*
      19             :  * All join variants produce some sort of join on two input BATs,
      20             :  * optionally subject to up to two candidate lists.  Only values in
      21             :  * the input BATs that are mentioned in the associated candidate list
      22             :  * (if provided) are eligible.  They all return two output BATs in the
      23             :  * first two arguments.  The join operations differ in the way in
      24             :  * which tuples from the two inputs are matched.
      25             :  *
      26             :  * The outputs consist of two aligned BATs (i.e. same length and same
      27             :  * hseqbase (0@0)) that contain the OIDs of the input BATs that match.
      28             :  * The candidate lists, if given, contain the OIDs of the associated
      29             :  * input BAT which must be considered for matching.  The input BATs
      30             :  * must have the same type.
      31             :  *
      32             :  * All functions also have a parameter nil_matches which indicates
      33             :  * whether NIL must be considered an ordinary value that can match, or
      34             :  * whether NIL must be considered to never match.
      35             :  *
      36             :  * The join functions that are provided here are:
      37             :  * BATjoin
      38             :  *      normal equi-join
      39             :  * BATleftjoin
      40             :  *      normal equi-join, but the left output is sorted
      41             :  * BATouterjoin
      42             :  *      equi-join, but the left output is sorted, and if there is no
      43             :  *      match for a value in the left input, there is still an output
      44             :  *      with NIL in the right output
      45             :  * BATsemijoin
      46             :  *      equi-join, but the left output is sorted, and if there are
      47             :  *      multiple matches, only one is returned (i.e., the left output
      48             :  *      is also key, making it a candidate list)
      49             :  * BATmarkjoin
      50             :  *      equi-join, but the left output is sorted, if there is no
      51             :  *      match for a value in the left input, there is still an output
      52             :  *      with NIL in the right output, and there is a third output column
      53             :  *      containing a flag that indicates the "certainty" of the match: 1
      54             :  *      there is a match, 0, there is no match and there are no NIL
      55             :  *      values, NIL, there is no match but there are NIL values
      56             :  * BATthetajoin
      57             :  *      theta-join: an extra operator must be provided encoded as an
      58             :  *      integer (macros JOIN_EQ, JOIN_NE, JOIN_LT, JOIN_LE, JOIN_GT,
      59             :  *      JOIN_GE); values match if the left input has the given
      60             :  *      relationship with the right input; order of the outputs is not
      61             :  *      guaranteed
      62             :  * BATbandjoin
      63             :  *      band-join: two extra input values (c1, c2) must be provided as
      64             :  *      well as Booleans (li, hi) that indicate whether the value
      65             :  *      ranges are inclusive or not; values in the left and right
      66             :  *      inputs match if right - c1 <[=] left <[=] right + c2; if c1 or
      67             :  *      c2 is NIL, there are no matches
      68             :  * BATrangejoin
      69             :  *      range-join: the right input consists of two aligned BATs,
      70             :  *      values match if the left value is between two corresponding
      71             :  *      right values; two extra Boolean parameters, li and hi,
      72             :  *      indicate whether equal values match
      73             :  *
      74             :  * In addition to these functions, there are two more functions that
      75             :  * are closely related:
      76             :  * BATintersect
      77             :  *      intersection: return a candidate list with OIDs of tuples in
      78             :  *      the left input whose value occurs in the right input
      79             :  * BATdiff
      80             :  *      difference: return a candidate list with OIDs of tuples in the
      81             :  *      left input whose value does not occur in the right input
      82             :  */
      83             : 
      84             : /* Perform a bunch of sanity checks on the inputs to a join. */
      85             : static gdk_return
      86      316196 : joinparamcheck(BAT *l, BAT *r1, BAT *r2, BAT *sl, BAT *sr, const char *func)
      87             : {
      88      786190 :         if (ATOMtype(l->ttype) != ATOMtype(r1->ttype) ||
      89         250 :             (r2 && ATOMtype(l->ttype) != ATOMtype(r2->ttype))) {
      90           0 :                 GDKerror("%s: inputs not compatible.\n", func);
      91           0 :                 return GDK_FAIL;
      92             :         }
      93         125 :         if (r2 &&
      94         125 :             (BATcount(r1) != BATcount(r2) || r1->hseqbase != r2->hseqbase)) {
      95           0 :                 GDKerror("%s: right inputs not aligned.\n", func);
      96           0 :                 return GDK_FAIL;
      97             :         }
      98      316196 :         if ((sl && !BATiscand(sl)) || (sr && !BATiscand(sr))) {
      99           0 :                 GDKerror("%s: argument not a candidate list.\n", func);
     100           0 :                 return GDK_FAIL;
     101             :         }
     102             :         return GDK_SUCCEED;
     103             : }
     104             : 
     105             : #define INCRSIZELOG     (8 + (SIZEOF_OID / 2))
     106             : #define INCRSIZE        (1 << INCRSIZELOG)
     107             : 
     108             : /* Create the result bats for a join, returns the absolute maximum
     109             :  * number of outputs that could possibly be generated. */
     110             : static BUN
     111       37229 : joininitresults(BAT **r1p, BAT **r2p, BAT **r3p, BUN lcnt, BUN rcnt,
     112             :                 bool lkey, bool rkey, bool semi, bool nil_on_miss,
     113             :                 bool only_misses, bool min_one, BUN estimate)
     114             : {
     115       37229 :         BAT *r1 = NULL, *r2 = NULL, *r3 = NULL;
     116       37229 :         BUN maxsize, size;
     117             : 
     118             :         /* if nil_on_miss is set, we really need a right output */
     119       37229 :         assert(!nil_on_miss || r2p != NULL || r3p != NULL);
     120             : 
     121       37229 :         lkey |= lcnt <= 1;
     122       37229 :         rkey |= rcnt <= 1;
     123             : 
     124       37229 :         *r1p = NULL;
     125       37229 :         if (r2p)
     126       17663 :                 *r2p = NULL;
     127       37229 :         if (r3p)
     128          92 :                 *r3p = NULL;
     129       37229 :         if (lcnt == 0) {
     130             :                 /* there is nothing to match */
     131             :                 maxsize = 0;
     132       34870 :         } else if (!only_misses && !nil_on_miss && rcnt == 0) {
     133             :                 /* if right is empty, we have no hits, so if we don't
     134             :                  * want misses, the result is empty */
     135             :                 maxsize = 0;
     136       34867 :         } else if (rkey | semi | only_misses) {
     137             :                 /* each entry left matches at most one on right, in
     138             :                  * case nil_on_miss is also set, each entry matches
     139             :                  * exactly one (see below) */
     140             :                 maxsize = lcnt;
     141       20488 :         } else if (lkey) {
     142             :                 /* each entry on right is matched at most once */
     143        3852 :                 if (nil_on_miss) {
     144             :                         /* one entry left could match all right, and
     145             :                          * all other entries left match nil */
     146          15 :                         maxsize = lcnt + rcnt - 1;
     147             :                 } else {
     148             :                         maxsize = rcnt;
     149             :                 }
     150       16636 :         } else if (rcnt == 0) {
     151             :                 /* nil_on_miss must be true due to previous checks, so
     152             :                  * all values on left miss */
     153             :                 maxsize = lcnt;
     154       16636 :         } else if (BUN_MAX / lcnt >= rcnt) {
     155             :                 /* in the worst case we have a full cross product */
     156       16637 :                 maxsize = lcnt * rcnt;
     157             :         } else {
     158             :                 /* a BAT cannot grow larger than BUN_MAX */
     159             :                 maxsize = BUN_MAX;
     160             :         }
     161       37229 :         size = estimate == BUN_NONE ? lcnt < rcnt ? lcnt : rcnt : estimate;
     162       37229 :         if (size < INCRSIZE)
     163             :                 size = INCRSIZE;
     164       37229 :         if (size > maxsize)
     165             :                 size = maxsize;
     166       37229 :         if ((rkey | semi | only_misses) & nil_on_miss) {
     167             :                 /* see comment above: each entry left matches exactly
     168             :                  * once */
     169         102 :                 size = maxsize;
     170             :         }
     171       37229 :         if (min_one && size < lcnt)
     172           0 :                 size = lcnt;
     173             : 
     174       37229 :         if (maxsize == 0) {
     175        2366 :                 r1 = BATdense(0, 0, 0);
     176        2367 :                 if (r1 == NULL) {
     177             :                         return BUN_NONE;
     178             :                 }
     179        2367 :                 if (r2p) {
     180         290 :                         r2 = BATdense(0, 0, 0);
     181         290 :                         if (r2 == NULL) {
     182           0 :                                 BBPreclaim(r1);
     183           0 :                                 return BUN_NONE;
     184             :                         }
     185         290 :                         *r2p = r2;
     186             :                 }
     187        2367 :                 if (r3p) {
     188           0 :                         r3 = COLnew(0, TYPE_bit, 0, TRANSIENT);
     189           0 :                         if (r3 == NULL) {
     190           0 :                                 BBPreclaim(r1);
     191           0 :                                 BBPreclaim(r2);
     192           0 :                                 if (r2p)
     193           0 :                                         *r2p = NULL;
     194           0 :                                 return BUN_NONE;
     195             :                         }
     196           0 :                         *r3p = r3;
     197             :                 }
     198        2367 :                 *r1p = r1;
     199        2367 :                 return 0;
     200             :         }
     201             : 
     202       34863 :         r1 = COLnew(0, TYPE_oid, size, TRANSIENT);
     203       34860 :         if (r1 == NULL) {
     204             :                 return BUN_NONE;
     205             :         }
     206       34860 :         r1->tnil = false;
     207       34860 :         r1->tnonil = true;
     208       34860 :         r1->tkey = true;
     209       34860 :         r1->tsorted = true;
     210       34860 :         r1->trevsorted = true;
     211       34860 :         r1->tseqbase = 0;
     212       34860 :         r1->theap->dirty = true;
     213       34860 :         *r1p = r1;
     214       34860 :         if (r2p) {
     215       17375 :                 r2 = COLnew(0, TYPE_oid, size, TRANSIENT);
     216       17377 :                 if (r2 == NULL) {
     217           0 :                         BBPreclaim(r1);
     218           0 :                         return BUN_NONE;
     219             :                 }
     220       17377 :                 r2->tnil = false;
     221       17377 :                 r2->tnonil = true;
     222       17377 :                 r2->tkey = true;
     223       17377 :                 r2->tsorted = true;
     224       17377 :                 r2->trevsorted = true;
     225       17377 :                 r2->tseqbase = 0;
     226       17377 :                 r2->theap->dirty = true;
     227       17377 :                 *r2p = r2;
     228             :         }
     229       34862 :         if (r3p) {
     230          96 :                 BAT *r3 = COLnew(0, TYPE_bit, size, TRANSIENT);
     231          96 :                 if (r3 == NULL) {
     232           0 :                         BBPreclaim(r1);
     233           0 :                         BBPreclaim(r2);
     234           0 :                         return BUN_NONE;
     235             :                 }
     236          96 :                 r3->tnil = false;
     237          96 :                 r3->tnonil = true;
     238          96 :                 r3->tkey = false;
     239          96 :                 r3->tsorted = false;
     240          96 :                 r3->trevsorted = false;
     241          96 :                 r3->tseqbase = oid_nil;
     242          96 :                 r3->theap->dirty = true;
     243          96 :                 *r3p = r3;
     244             :         }
     245             :         return maxsize;
     246             : }
     247             : 
     248             : #define VALUE(s, x)     (s##vars ?                                      \
     249             :                          s##vars + VarHeapVal(s##vals, (x), s##i.width) : \
     250             :                          s##vals ? (const char *) s##vals + ((x) * s##i.width) : \
     251             :                          (s##val = BUNtoid(s, (x)), (const char *) &s##val))
     252             : #define FVALUE(s, x)    ((const char *) s##vals + ((x) * s##i.width))
     253             : 
     254             : #define APPEND(b, o)            (((oid *) b->theap->base)[b->batCount++] = (o))
     255             : 
     256             : static inline gdk_return
     257   188254730 : maybeextend(BAT *restrict r1, BAT *restrict r2, BAT *restrict r3,
     258             :             BUN cnt, BUN lcur, BUN lcnt, BUN maxsize)
     259             : {
     260   188254730 :         if (BATcount(r1) + cnt > BATcapacity(r1)) {
     261             :                 /* make some extra space by extrapolating how much more
     262             :                  * we need (fraction of l we've seen so far is used to
     263             :                  * estimate a new size but with a shallow slope so that
     264             :                  * a skewed join doesn't overwhelm, whilst making sure
     265             :                  * there is somewhat significant progress) */
     266        1757 :                 BUN newcap = (BUN) (lcnt / (lcnt / 4.0 + lcur * .75) * (BATcount(r1) + cnt));
     267        1757 :                 newcap = (newcap + INCRSIZE - 1) & ~(((BUN) 1 << INCRSIZELOG) - 1);
     268        1757 :                 if (newcap < cnt + BATcount(r1))
     269           0 :                         newcap = cnt + BATcount(r1) + INCRSIZE;
     270             :                 /* if close to maxsize, then just use maxsize */
     271        1757 :                 if (newcap + INCRSIZE > maxsize)
     272         147 :                         newcap = maxsize;
     273             :                 /* make sure heap.free is set properly before
     274             :                  * extending */
     275        1757 :                 BATsetcount(r1, BATcount(r1));
     276        1758 :                 if (BATextend(r1, newcap) != GDK_SUCCEED)
     277             :                         return GDK_FAIL;
     278        1759 :                 if (r2) {
     279        1180 :                         BATsetcount(r2, BATcount(r2));
     280        1180 :                         if (BATextend(r2, newcap) != GDK_SUCCEED)
     281             :                                 return GDK_FAIL;
     282        1180 :                         assert(BATcapacity(r1) == BATcapacity(r2));
     283             :                 }
     284        1759 :                 if (r3) {
     285           0 :                         BATsetcount(r3, BATcount(r3));
     286           0 :                         if (BATextend(r3, newcap) != GDK_SUCCEED)
     287             :                                 return GDK_FAIL;
     288           0 :                         assert(BATcapacity(r1) == BATcapacity(r3));
     289             :                 }
     290             :         }
     291             :         return GDK_SUCCEED;
     292             : }
     293             : 
     294             : /* Return BATs through r1p, r2p, and r3p for the case that there is no
     295             :  * match between l and r, taking all flags into consideration.
     296             :  *
     297             :  * This means, if nil_on_miss is set or only_misses is set, *r1p is a
     298             :  * copy of the left candidate list or a dense list of all "head"
     299             :  * values of l, and *r2p (if r2p is not NULL) is all nil.  If neither
     300             :  * of those flags is set, the result is two empty BATs. */
     301             : static gdk_return
     302      223601 : nomatch(BAT **r1p, BAT **r2p, BAT **r3p, BAT *l, BAT *r,
     303             :         struct canditer *restrict lci, bit defmark,
     304             :         bool nil_on_miss, bool only_misses, const char *func, lng t0)
     305             : {
     306      223601 :         BAT *r1, *r2 = NULL, *r3 = NULL;
     307             : 
     308      223601 :         MT_thread_setalgorithm(__func__);
     309      223560 :         if (lci->ncand == 0 || !(nil_on_miss | only_misses)) {
     310             :                 /* return empty BATs */
     311      211293 :                 if ((r1 = BATdense(0, 0, 0)) == NULL)
     312             :                         return GDK_FAIL;
     313      211323 :                 if (r2p) {
     314      136221 :                         if ((r2 = BATdense(0, 0, 0)) == NULL) {
     315           0 :                                 BBPreclaim(r1);
     316           0 :                                 return GDK_FAIL;
     317             :                         }
     318      136182 :                         *r2p = r2;
     319             :                 }
     320      211284 :                 if (r3p) {
     321        9309 :                         if ((r3 = COLnew(0, TYPE_bit, 0, TRANSIENT)) == NULL) {
     322           0 :                                 BBPreclaim(r1);
     323           0 :                                 BBPreclaim(r2);
     324           0 :                                 return GDK_FAIL;
     325             :                         }
     326        9307 :                         *r3p = r3;
     327             :                 }
     328             :         } else {
     329       12267 :                 r1 = canditer_slice(lci, 0, lci->ncand);
     330       12267 :                 if (r2p) {
     331          22 :                         if ((r2 = BATconstant(0, TYPE_void, &oid_nil, lci->ncand, TRANSIENT)) == NULL) {
     332           0 :                                 BBPreclaim(r1);
     333           0 :                                 return GDK_FAIL;
     334             :                         }
     335          22 :                         *r2p = r2;
     336             :                 }
     337       12267 :                 if (r3p) {
     338          56 :                         if ((r3 = BATconstant(0, TYPE_bit, &defmark, lci->ncand, TRANSIENT)) == NULL) {
     339           0 :                                 BBPreclaim(r1);
     340           0 :                                 BBPreclaim(r2);
     341           0 :                                 return GDK_FAIL;
     342             :                         }
     343          56 :                         *r3p = r3;
     344             :                 }
     345             :         }
     346      223549 :         *r1p = r1;
     347      223549 :         TRC_DEBUG(ALGO, "l=" ALGOBATFMT ",r=" ALGOBATFMT ",sl=" ALGOOPTBATFMT
     348             :                   ",nil_on_miss=%s,only_misses=%s"
     349             :                   " - > " ALGOBATFMT "," ALGOOPTBATFMT "," ALGOOPTBATFMT
     350             :                   " (%s -- " LLFMT "usec)\n",
     351             :                   ALGOBATPAR(l), ALGOBATPAR(r), ALGOOPTBATPAR(lci->s),
     352             :                   nil_on_miss ? "true" : "false",
     353             :                   only_misses ? "true" : "false",
     354             :                   ALGOBATPAR(r1), ALGOOPTBATPAR(r2), ALGOOPTBATPAR(r3),
     355             :                   func, GDKusec() - t0);
     356             :         return GDK_SUCCEED;
     357             : }
     358             : 
     359             : /* Implementation of join where there is a single value (possibly
     360             :  * repeated multiple times) on the left.  This means we can use a
     361             :  * point select to find matches in the right column. */
     362             : static gdk_return
     363       39138 : selectjoin(BAT **r1p, BAT **r2p, BAT **r3p, BAT *l, BAT *r,
     364             :            struct canditer *lci, struct canditer *rci,
     365             :            bool nil_matches, bool nil_on_miss, bool semi, bool max_one, bool min_one,
     366             :            lng t0, bool swapped, const char *reason)
     367             : {
     368       39138 :         BATiter li = bat_iterator(l);
     369       39138 :         const void *v;
     370       39138 :         BAT *bn = NULL;
     371       39138 :         BAT *r1 = NULL;
     372       39138 :         BAT *r2 = NULL;
     373       39138 :         BUN bncount;
     374             : 
     375       39138 :         assert(lci->ncand > 0);
     376       39138 :         assert(lci->ncand == 1 || (li.sorted && li.revsorted));
     377             : 
     378       39138 :         size_t counter = 0;
     379       39138 :         QryCtx *qry_ctx = MT_thread_get_qry_ctx();
     380             : 
     381       39138 :         MT_thread_setalgorithm(__func__);
     382       39138 :         oid o = canditer_next(lci);
     383       39138 :         v = BUNtail(li, o - l->hseqbase);
     384             : 
     385       76632 :         if (!nil_matches &&
     386       37494 :             (*ATOMcompare(li.type))(v, ATOMnilptr(li.type)) == 0) {
     387             :                 /* NIL doesn't match anything */
     388         140 :                 bat_iterator_end(&li);
     389         140 :                 gdk_return rc = nomatch(r1p, r2p, r3p, l, r, lci, bit_nil, nil_on_miss,
     390             :                                         false, reason, t0);
     391         140 :                 return rc;
     392             :         }
     393             : 
     394       38998 :         bn = BATselect(r, rci->s, v, NULL, true, true, false, false);
     395       38997 :         bat_iterator_end(&li);
     396       38997 :         if (bn == NULL) {
     397             :                 return GDK_FAIL;
     398             :         }
     399       38997 :         bncount = BATcount(bn);
     400       38997 :         if (bncount == 0) {
     401        6057 :                 BBPreclaim(bn);
     402        6057 :                 if (min_one) {
     403           0 :                         GDKerror("not enough matches");
     404           0 :                         return GDK_FAIL;
     405             :                 }
     406        6057 :                 if (!nil_on_miss) {
     407        5907 :                         assert(r3p == NULL);
     408        5907 :                         return nomatch(r1p, r2p, r3p, l, r, lci, 0, nil_on_miss,
     409             :                                        false, reason, t0);
     410             :                 }
     411             :                 /* special case: return nil on RHS */
     412             :                 bncount = 1;
     413             :                 bn = NULL;
     414             :         }
     415       32940 :         if (bncount > 1) {
     416        1151 :                 if (semi)
     417         361 :                         bncount = 1;
     418        1151 :                 if (max_one) {
     419          16 :                         GDKerror("more than one match");
     420          16 :                         goto bailout;
     421             :                 }
     422             :         }
     423       33074 :         r1 = COLnew(0, TYPE_oid, lci->ncand * bncount, TRANSIENT);
     424       33074 :         if (r1 == NULL)
     425           0 :                 goto bailout;
     426       33074 :         r1->tsorted = true;
     427       33074 :         r1->trevsorted = lci->ncand == 1;
     428       33074 :         r1->tseqbase = bncount == 1 && lci->tpe == cand_dense ? o : oid_nil;
     429       33074 :         r1->tkey = bncount == 1;
     430       33074 :         r1->tnil = false;
     431       33074 :         r1->tnonil = true;
     432       33074 :         if (bn == NULL) {
     433             :                 /* left outer join, no match, we're returning nil in r2 */
     434         150 :                 oid *o1p = (oid *) Tloc(r1, 0);
     435         150 :                 BUN p, q = bncount;
     436             : 
     437         150 :                 if (r2p) {
     438           2 :                         r2 = BATconstant(0, TYPE_void, &oid_nil, lci->ncand * bncount, TRANSIENT);
     439           2 :                         if (r2 == NULL)
     440           0 :                                 goto bailout;
     441           2 :                         *r2p = r2;
     442             :                 }
     443         307 :                 do {
     444         307 :                         GDK_CHECK_TIMEOUT(qry_ctx, counter,
     445             :                                           GOTO_LABEL_TIMEOUT_HANDLER(bailout, qry_ctx));
     446         614 :                         for (p = 0; p < q; p++) {
     447         307 :                                 *o1p++ = o;
     448             :                         }
     449         307 :                         o = canditer_next(lci);
     450         307 :                 } while (!is_oid_nil(o));
     451             :         } else {
     452       32924 :                 oid *o1p = (oid *) Tloc(r1, 0);
     453       32924 :                 oid *o2p;
     454       32924 :                 BUN p, q = bncount;
     455             : 
     456       32924 :                 if (r2p) {
     457       29717 :                         r2 = COLnew(0, TYPE_oid, lci->ncand * bncount, TRANSIENT);
     458       29717 :                         if (r2 == NULL)
     459           0 :                                 goto bailout;
     460       29717 :                         r2->tsorted = lci->ncand == 1 || bncount == 1;
     461       29717 :                         r2->trevsorted = bncount == 1;
     462       29717 :                         r2->tseqbase = lci->ncand == 1 && BATtdense(bn) ? bn->tseqbase : oid_nil;
     463       29717 :                         r2->tkey = lci->ncand == 1;
     464       29717 :                         r2->tnil = false;
     465       29717 :                         r2->tnonil = true;
     466       29717 :                         *r2p = r2;
     467       29717 :                         o2p = (oid *) Tloc(r2, 0);
     468             :                 } else {
     469             :                         o2p = NULL;
     470             :                 }
     471             : 
     472       32924 :                 if (BATtdense(bn)) {
     473             :                         oid bno = bn->tseqbase;
     474             : 
     475     1281942 :                         do {
     476     1281942 :                                 GDK_CHECK_TIMEOUT(qry_ctx, counter,
     477             :                                                   GOTO_LABEL_TIMEOUT_HANDLER(bailout, qry_ctx));
     478     2616755 :                                 for (p = 0; p < q; p++) {
     479     1334813 :                                         *o1p++ = o;
     480             :                                 }
     481     1281942 :                                 if (o2p) {
     482      352223 :                                         for (p = 0; p < q; p++) {
     483      202496 :                                                 *o2p++ = bno + p;
     484             :                                         }
     485             :                                 }
     486     1281942 :                                 o = canditer_next(lci);
     487     1281940 :                         } while (!is_oid_nil(o));
     488             :                 } else {
     489         175 :                         const oid *bnp = (const oid *) Tloc(bn, 0);
     490             : 
     491      140893 :                         do {
     492      140893 :                                 GDK_CHECK_TIMEOUT(qry_ctx, counter,
     493             :                                                   GOTO_LABEL_TIMEOUT_HANDLER(bailout, qry_ctx));
     494     5509801 :                                 for (p = 0; p < q; p++) {
     495     5368908 :                                         *o1p++ = o;
     496             :                                 }
     497      140893 :                                 if (o2p) {
     498     5235252 :                                         for (p = 0; p < q; p++) {
     499     5087704 :                                                 *o2p++ = bnp[p];
     500             :                                         }
     501             :                                 }
     502      140893 :                                 o = canditer_next(lci);
     503      140893 :                         } while (!is_oid_nil(o));
     504             :                 }
     505       32922 :                 if (r2)
     506       29715 :                         BATsetcount(r2, lci->ncand * bncount);
     507             :         }
     508       33072 :         BATsetcount(r1, lci->ncand * bncount);
     509       33072 :         *r1p = r1;
     510       33072 :         BAT *r3 = NULL;
     511       33072 :         if (r3p) {
     512         189 :                 bit mark;
     513         189 :                 if (bn) {
     514             :                         /* there is a match */
     515          40 :                         mark = 1;
     516         149 :                 } else if (r->tnonil) {
     517             :                         /* no match, no NIL in r */
     518         144 :                         mark = 0;
     519             :                 } else {
     520             :                         /* no match, search for NIL in r */
     521           5 :                         BAT *n = BATselect(r, rci->s, ATOMnilptr(r->ttype), NULL, true, true, false, false);
     522           5 :                         if (n == NULL)
     523           0 :                                 goto bailout;
     524           5 :                         mark = BATcount(n) == 0 ? 0 : bit_nil;
     525           5 :                         BBPreclaim(n);
     526             :                 }
     527         189 :                 r3 = BATconstant(0, TYPE_bit, &mark, lci->ncand, TRANSIENT);
     528         189 :                 if (r3 == NULL)
     529           0 :                         goto bailout;
     530         189 :                 *r3p = r3;
     531             :         }
     532       33072 :         BBPreclaim(bn);
     533       33074 :         TRC_DEBUG(ALGO, "l=" ALGOBATFMT ","
     534             :                   "r=" ALGOBATFMT ",sl=" ALGOOPTBATFMT ","
     535             :                   "sr=" ALGOOPTBATFMT ",nil_matches=%s;%s %s "
     536             :                   "-> " ALGOBATFMT "," ALGOOPTBATFMT "," ALGOOPTBATFMT
     537             :                   " (" LLFMT "usec)\n",
     538             :                   ALGOBATPAR(l), ALGOBATPAR(r),
     539             :                   ALGOOPTBATPAR(lci->s), ALGOOPTBATPAR(rci->s),
     540             :                   nil_matches ? "true" : "false",
     541             :                   swapped ? " swapped" : "", reason,
     542             :                   ALGOBATPAR(r1), ALGOOPTBATPAR(r2), ALGOOPTBATPAR(r3),
     543             :                   GDKusec() - t0);
     544             : 
     545             :         return GDK_SUCCEED;
     546             : 
     547          16 :   bailout:
     548          16 :         BBPreclaim(bn);
     549          16 :         BBPreclaim(r1);
     550          16 :         BBPreclaim(r2);
     551          16 :         if (r2p)
     552          15 :                 *r2p = NULL;
     553             :         return GDK_FAIL;
     554             : }
     555             : 
     556             : #if SIZEOF_OID == SIZEOF_INT
     557             : #define binsearch_oid(indir, offset, vals, lo, hi, v, ordering, last) binsearch_int(indir, offset, (const int *) vals, lo, hi, (int) (v), ordering, last)
     558             : #endif
     559             : #if SIZEOF_OID == SIZEOF_LNG
     560             : #define binsearch_oid(indir, offset, vals, lo, hi, v, ordering, last) binsearch_lng(indir, offset, (const lng *) vals, lo, hi, (lng) (v), ordering, last)
     561             : #endif
     562             : 
     563             : /* Implementation of join where the right-hand side is dense, and if
     564             :  * there is a right candidate list, it too is dense.  This means there
     565             :  * are no NIL values in r.  In case nil_on_miss is not set, we use a
     566             :  * range select (BATselect) to find the matching values in the left
     567             :  * column and then calculate the corresponding matches from the right.
     568             :  * If nil_on_miss is set, we need to do some more work. The latter is
     569             :  * also the only case in which r3p van be set. */
     570             : static gdk_return
     571       17650 : mergejoin_void(BAT **r1p, BAT **r2p, BAT **r3p, BAT *l, BAT *r,
     572             :                struct canditer *restrict lci, struct canditer *restrict rci,
     573             :                bool nil_on_miss, bool only_misses, lng t0, bool swapped,
     574             :                const char *reason)
     575             : {
     576       17650 :         oid lo, hi;
     577       17650 :         BUN i;
     578       17650 :         oid o, *o1p = NULL, *o2p = NULL;
     579       17650 :         bit *m3p = NULL;
     580       17650 :         BAT *r1 = NULL, *r2 = NULL, *r3 = NULL;
     581       17650 :         bool ltsorted = false, ltrevsorted = false, ltkey = false;
     582             : 
     583             :         /* r is dense, and if there is a candidate list, it too is
     584             :          * dense.  This means we don't have to do any searches, we
     585             :          * only need to compare ranges to know whether a value from l
     586             :          * has a match in r */
     587       25500 :         assert(ATOMtype(l->ttype) == ATOMtype(r->ttype));
     588       17650 :         assert(r->tsorted || r->trevsorted);
     589       17650 :         assert(BATcount(l) > 0);
     590       17650 :         assert(rci->tpe == cand_dense);
     591       17650 :         assert(BATcount(r) > 0);
     592             : 
     593       17650 :         QryCtx *qry_ctx = MT_thread_get_qry_ctx();
     594             : 
     595       17648 :         MT_thread_setalgorithm(__func__);
     596             :         /* figure out range [lo..hi) of values in r that we need to match */
     597       17647 :         lo = r->tseqbase;
     598       17647 :         hi = lo + BATcount(r);
     599             :         /* restrict [lo..hi) range further using candidate list */
     600       17647 :         if (rci->seq > r->hseqbase)
     601           0 :                 lo += rci->seq - r->hseqbase;
     602       17647 :         if (rci->seq + rci->ncand < r->hseqbase + BATcount(r))
     603           0 :                 hi -= r->hseqbase + BATcount(r) - rci->seq - rci->ncand;
     604             : 
     605             :         /* at this point, the matchable values in r are [lo..hi) */
     606       17647 :         if (!nil_on_miss) {
     607       17647 :                 assert(r3p == NULL);
     608       17647 :                 r1 = BATselect(l, lci->s, &lo, &hi, true, false, only_misses, false);
     609       17650 :                 if (r1 == NULL)
     610             :                         return GDK_FAIL;
     611       17650 :                 if (only_misses && !l->tnonil) {
     612             :                         /* also look for NILs */
     613           0 :                         r2 = BATselect(l, lci->s, &oid_nil, NULL, true, false, false, false);
     614           0 :                         if (r2 == NULL) {
     615           0 :                                 BBPreclaim(r1);
     616           0 :                                 return GDK_FAIL;
     617             :                         }
     618           0 :                         if (BATcount(r2) > 0) {
     619           0 :                                 BAT *mg = BATmergecand(r1, r2);
     620           0 :                                 BBPunfix(r1->batCacheid);
     621           0 :                                 BBPunfix(r2->batCacheid);
     622           0 :                                 r1 = mg;
     623           0 :                                 if (r1 == NULL)
     624             :                                         return GDK_FAIL;
     625             :                         } else {
     626           0 :                                 BBPunfix(r2->batCacheid);
     627             :                         }
     628             :                         r2 = NULL;
     629             :                 }
     630       17650 :                 *r1p = r1;
     631       17650 :                 if (r2p == NULL)
     632       16944 :                         goto doreturn2;
     633         706 :                 if (BATcount(r1) == 0) {
     634          14 :                         r2 = BATdense(0, 0, 0);
     635          14 :                         if (r2 == NULL) {
     636           0 :                                 BBPreclaim(r1);
     637           0 :                                 return GDK_FAIL;
     638             :                         }
     639         692 :                 } else if (BATtdense(r1) && BATtdense(l)) {
     640          79 :                         r2 = BATdense(0, l->tseqbase + r1->tseqbase - l->hseqbase + r->hseqbase - r->tseqbase, BATcount(r1));
     641          79 :                         if (r2 == NULL) {
     642           0 :                                 BBPreclaim(r1);
     643           0 :                                 return GDK_FAIL;
     644             :                         }
     645             :                 } else {
     646         613 :                         r2 = COLnew(0, TYPE_oid, BATcount(r1), TRANSIENT);
     647         613 :                         if (r2 == NULL) {
     648           0 :                                 BBPreclaim(r1);
     649           0 :                                 return GDK_FAIL;
     650             :                         }
     651         613 :                         BATiter li = bat_iterator(l);
     652         613 :                         const oid *lp = (const oid *) li.base;
     653         613 :                         const oid *o1p = (const oid *) Tloc(r1, 0);
     654         613 :                         oid *o2p = (oid *) Tloc(r2, 0);
     655         613 :                         hi = BATcount(r1);
     656         613 :                         if (complex_cand(l)) {
     657             :                                 /* this is actually generic code */
     658           0 :                                 for (o = 0; o < hi; o++)
     659           0 :                                         o2p[o] = BUNtoid(l, BUNtoid(r1, o) - l->hseqbase) - r->tseqbase + r->hseqbase;
     660         613 :                         } else if (BATtdense(r1)) {
     661         283 :                                 lo = r1->tseqbase - l->hseqbase;
     662         283 :                                 if (r->tseqbase == r->hseqbase) {
     663         276 :                                         memcpy(o2p, lp + lo, hi * SIZEOF_OID);
     664             :                                 } else {
     665           7 :                                         hi += lo;
     666     5085011 :                                         for (o = 0; lo < hi; o++, lo++) {
     667     5085004 :                                                 o2p[o] = lp[lo] - r->tseqbase + r->hseqbase;
     668             :                                         }
     669             :                                 }
     670         330 :                         } else if (BATtdense(l)) {
     671           0 :                                 for (o = 0; o < hi; o++) {
     672           0 :                                         o2p[o] = o1p[o] - l->hseqbase + li.tseq - r->tseqbase + r->hseqbase;
     673             :                                 }
     674             :                         } else {
     675    41579000 :                                 for (o = 0; o < hi; o++) {
     676    41578670 :                                         o2p[o] = lp[o1p[o] - l->hseqbase] - r->tseqbase + r->hseqbase;
     677             :                                 }
     678             :                         }
     679         613 :                         r2->tkey = li.key;
     680         613 :                         r2->tsorted = li.sorted;
     681         613 :                         r2->trevsorted = li.revsorted;
     682         613 :                         bat_iterator_end(&li);
     683         613 :                         r2->tnil = false;
     684         613 :                         r2->tnonil = true;
     685         613 :                         BATsetcount(r2, BATcount(r1));
     686             :                 }
     687         705 :                 *r2p = r2;
     688         705 :                 goto doreturn2;
     689             :         }
     690             :         /* nil_on_miss is set, this means we must have a second or third
     691             :          * output */
     692           0 :         assert(r2p || r3p);
     693           0 :         if (BATtdense(l)) {
     694             :                 /* if l is dense, we can further restrict the [lo..hi)
     695             :                  * range to values in l that match with values in r */
     696           0 :                 o = lo;
     697           0 :                 i = lci->seq - l->hseqbase;
     698           0 :                 if (l->tseqbase + i > lo)
     699           0 :                         lo = l->tseqbase + i;
     700           0 :                 i = canditer_last(lci) + 1 - l->hseqbase;
     701           0 :                 if (l->tseqbase + i < hi)
     702           0 :                         hi = l->tseqbase + i;
     703           0 :                 if (lci->tpe == cand_dense) {
     704             :                         /* l is dense, and so is the left candidate
     705             :                          * list (if it exists); this means we don't
     706             :                          * have to actually look at any values in l:
     707             :                          * we can just do some arithmetic; it also
     708             :                          * means that r1 will be dense, and if
     709             :                          * nil_on_miss is not set, or if all values in
     710             :                          * l match, r2 will too */
     711           0 :                         if (hi <= lo) {
     712           0 :                                 return nomatch(r1p, r2p, r3p, l, r, lci, 0,
     713             :                                                nil_on_miss, only_misses,
     714             :                                                __func__, t0);
     715             :                         }
     716             : 
     717             :                         /* at this point, the matched values in l and
     718             :                          * r (taking candidate lists into account) are
     719             :                          * [lo..hi) which we can translate back to the
     720             :                          * respective OID values that we can store in
     721             :                          * r1 and r2; note that r1 will be dense since
     722             :                          * all values in l will match something (even
     723             :                          * if nil since nil_on_miss is set) */
     724           0 :                         *r1p = r1 = BATdense(0, lci->seq, lci->ncand);
     725           0 :                         if (r1 == NULL)
     726             :                                 return GDK_FAIL;
     727           0 :                         if (r2p) {
     728           0 :                                 if (hi - lo < lci->ncand) {
     729             :                                         /* we need to fill in nils in r2 for
     730             :                                          * missing values */
     731           0 :                                         *r2p = r2 = COLnew(0, TYPE_oid, lci->ncand, TRANSIENT);
     732           0 :                                         if (r2 == NULL) {
     733           0 :                                                 BBPreclaim(*r1p);
     734           0 :                                                 return GDK_FAIL;
     735             :                                         }
     736           0 :                                         o2p = (oid *) Tloc(r2, 0);
     737           0 :                                         i = l->tseqbase + lci->seq - l->hseqbase;
     738           0 :                                         lo -= i;
     739           0 :                                         hi -= i;
     740           0 :                                         i += r->hseqbase - r->tseqbase;
     741           0 :                                         for (o = 0; o < lo; o++)
     742           0 :                                                 *o2p++ = oid_nil;
     743           0 :                                         for (o = lo; o < hi; o++)
     744           0 :                                                 *o2p++ = o + i;
     745           0 :                                         for (o = hi; o < lci->ncand; o++)
     746           0 :                                                 *o2p++ = oid_nil;
     747           0 :                                         r2->tnonil = false;
     748           0 :                                         r2->tnil = true;
     749             :                                         /* sorted of no nils at end */
     750           0 :                                         r2->tsorted = hi == lci->ncand;
     751             :                                         /* reverse sorted if single non-nil at start */
     752           0 :                                         r2->trevsorted = lo == 0 && hi == 1;
     753           0 :                                         r2->tseqbase = oid_nil;
     754             :                                         /* (hi - lo) different OIDs in r2,
     755             :                                          * plus one for nil */
     756           0 :                                         r2->tkey = hi - lo + 1 == lci->ncand;
     757           0 :                                         BATsetcount(r2, lci->ncand);
     758             :                                 } else {
     759             :                                         /* no missing values */
     760           0 :                                         *r2p = r2 = BATdense(0, r->hseqbase + lo - r->tseqbase, lci->ncand);
     761           0 :                                         if (r2 == NULL) {
     762           0 :                                                 BBPreclaim(*r1p);
     763           0 :                                                 return GDK_FAIL;
     764             :                                         }
     765             :                                 }
     766             :                         }
     767           0 :                         if (r3p) {
     768           0 :                                 if (hi - lo < lci->ncand) {
     769           0 :                                         *r3p = r3 = COLnew(0, TYPE_bit, lci->ncand, TRANSIENT);
     770           0 :                                         if (r3 == NULL) {
     771           0 :                                                 BBPreclaim(*r1p);
     772           0 :                                                 BBPreclaim(r2);
     773           0 :                                                 return GDK_FAIL;
     774             :                                         }
     775           0 :                                         m3p = (bit *) Tloc(r3, 0);
     776           0 :                                         for (o = 0; o < lo; o++)
     777           0 :                                                 *m3p++ = 0;
     778           0 :                                         for (o = lo; o < hi; o++)
     779           0 :                                                 *m3p++ = 1;
     780           0 :                                         for (o = hi; o < lci->ncand; o++)
     781           0 :                                                 *m3p++ = 0;
     782           0 :                                         r3->tnonil = true;
     783           0 :                                         r3->tnil = false;
     784           0 :                                         r3->tsorted = hi == lci->ncand;
     785           0 :                                         r3->trevsorted = lo == 0;
     786           0 :                                         r3->tkey = false;
     787           0 :                                         BATsetcount(r3, lci->ncand);
     788             :                                 }
     789             :                         }
     790           0 :                         goto doreturn;
     791             :                 }
     792             :                 /* l is dense, but the candidate list exists and is
     793             :                  * not dense; we can, by manipulating the range
     794             :                  * [lo..hi), just look at the candidate list values */
     795             : 
     796             :                 /* translate lo and hi to l's OID values that now need
     797             :                  * to match */
     798           0 :                 lo = lo - l->tseqbase + l->hseqbase;
     799           0 :                 hi = hi - l->tseqbase + l->hseqbase;
     800             : 
     801           0 :                 *r1p = r1 = COLnew(0, TYPE_oid, lci->ncand, TRANSIENT);
     802           0 :                 if (r2p)
     803           0 :                         *r2p = r2 = COLnew(0, TYPE_oid, lci->ncand, TRANSIENT);
     804           0 :                 if (r3p)
     805           0 :                         *r3p = r3 = COLnew(0, TYPE_bit, lci->ncand, TRANSIENT);
     806           0 :                 if (r1 == NULL || (r2p != NULL && r2 == NULL) || (r3p != NULL && r3 == NULL)) {
     807           0 :                         BBPreclaim(r1);
     808           0 :                         BBPreclaim(r2);
     809           0 :                         BBPreclaim(r3);
     810           0 :                         return GDK_FAIL;
     811             :                 }
     812           0 :                 o1p = (oid *) Tloc(r1, 0);
     813           0 :                 if (r2) {
     814           0 :                         o2p = (oid *) Tloc(r2, 0);
     815           0 :                         r2->tnil = false;
     816           0 :                         r2->tnonil = true;
     817           0 :                         r2->tkey = true;
     818           0 :                         r2->tsorted = true;
     819             :                 }
     820           0 :                 if (r3) {
     821           0 :                         m3p = (bit *) Tloc(r3, 0);
     822           0 :                         r3->tnil = false;
     823           0 :                         r3->tnonil = true;
     824           0 :                         r3->tkey = false;
     825           0 :                         r3->tsorted = false;
     826             :                 }
     827           0 :                 o = canditer_next(lci);
     828           0 :                 for (i = 0; i < lci->ncand && o < lo; i++) {
     829           0 :                         *o1p++ = o;
     830           0 :                         if (r2)
     831           0 :                                 *o2p++ = oid_nil;
     832           0 :                         if (r3)
     833           0 :                                 *m3p++ = 0;
     834           0 :                         o = canditer_next(lci);
     835             :                 }
     836           0 :                 if (i > 0 && r2) {
     837           0 :                         r2->tnil = true;
     838           0 :                         r2->tnonil = false;
     839           0 :                         r2->tkey = i == 1;
     840             :                 }
     841           0 :                 for (; i < lci->ncand && o < hi; i++) {
     842           0 :                         *o1p++ = o;
     843           0 :                         if (r2)
     844           0 :                                 *o2p++ = o - l->hseqbase + l->tseqbase - r->tseqbase + r->hseqbase;
     845           0 :                         if (r3)
     846           0 :                                 *m3p++ = 1;
     847           0 :                         o = canditer_next(lci);
     848             :                 }
     849           0 :                 if (i < lci->ncand) {
     850           0 :                         if (r2) {
     851           0 :                                 r2->tkey = !r2->tnil && lci->ncand - i == 1;
     852           0 :                                 r2->tnil = true;
     853           0 :                                 r2->tnonil = false;
     854           0 :                                 r2->tsorted = false;
     855             :                         }
     856           0 :                         for (; i < lci->ncand; i++) {
     857           0 :                                 *o1p++ = o;
     858           0 :                                 if (r2)
     859           0 :                                         *o2p++ = oid_nil;
     860           0 :                                 if (r1)
     861           0 :                                         *m3p++ = 0;
     862           0 :                                 o = canditer_next(lci);
     863             :                         }
     864             :                 }
     865           0 :                 BATsetcount(r1, lci->ncand);
     866           0 :                 r1->tseqbase = BATcount(r1) == 1 ? *(oid*)Tloc(r1, 0) : oid_nil;
     867           0 :                 r1->tsorted = true;
     868           0 :                 r1->trevsorted = BATcount(r1) <= 1;
     869           0 :                 r1->tnil = false;
     870           0 :                 r1->tnonil = true;
     871           0 :                 r1->tkey = true;
     872           0 :                 if (r2) {
     873           0 :                         BATsetcount(r2, BATcount(r1));
     874           0 :                         r2->tseqbase = r2->tnil || BATcount(r2) > 1 ? oid_nil : BATcount(r2) == 1 ? *(oid*)Tloc(r2, 0) : 0;
     875           0 :                         r2->trevsorted = BATcount(r2) <= 1;
     876             :                 }
     877           0 :                 if (r3) {
     878           0 :                         BATsetcount(r3, BATcount(r1));
     879             :                 }
     880           0 :                 goto doreturn;
     881             :         }
     882             :         /* l is not dense, so we need to look at the values and check
     883             :          * whether they are in the range [lo..hi) */
     884             : 
     885             :         /* do indirection through the candidate list to look at the
     886             :          * value */
     887             : 
     888           0 :         *r1p = r1 = COLnew(0, TYPE_oid, lci->ncand, TRANSIENT);
     889           0 :         if (r2p)
     890           0 :                 *r2p = r2 = COLnew(0, TYPE_oid, lci->ncand, TRANSIENT);
     891           0 :         if (r3p)
     892           0 :                 *r3p = r3 = COLnew(0, TYPE_bit, lci->ncand, TRANSIENT);
     893           0 :         if (r1 == NULL || (r2p != NULL && r2 == NULL) || (r3p != NULL && r3 == NULL)) {
     894           0 :                 BBPreclaim(r1);
     895           0 :                 BBPreclaim(r2);
     896           0 :                 BBPreclaim(r3);
     897           0 :                 return GDK_FAIL;
     898             :         }
     899           0 :         o1p = (oid *) Tloc(r1, 0);
     900           0 :         if (r2) {
     901           0 :                 o2p = (oid *) Tloc(r2, 0);
     902           0 :                 r2->tnil = false;
     903           0 :                 r2->tnonil = true;
     904             :         }
     905           0 :         if (r3) {
     906           0 :                 m3p = (bit *) Tloc(r3, 0);
     907           0 :                 r3->tnil = false;
     908           0 :                 r3->tnonil = true;
     909             :         }
     910           0 :         if (complex_cand(l)) {
     911           0 :                 ltsorted = l->tsorted;
     912           0 :                 ltrevsorted = l->trevsorted;
     913           0 :                 ltkey = l->tkey;
     914           0 :                 TIMEOUT_LOOP(lci->ncand, qry_ctx) {
     915           0 :                         oid c = canditer_next(lci);
     916             : 
     917           0 :                         o = BUNtoid(l, c - l->hseqbase);
     918           0 :                         *o1p++ = c;
     919           0 :                         if (r2) {
     920           0 :                                 if (o >= lo && o < hi) {
     921           0 :                                         *o2p++ = o - r->tseqbase + r->hseqbase;
     922             :                                 } else {
     923           0 :                                         *o2p++ = oid_nil;
     924           0 :                                         r2->tnil = true;
     925           0 :                                         r2->tnonil = false;
     926             :                                 }
     927             :                         }
     928           0 :                         if (r3) {
     929           0 :                                 if (is_oid_nil(o)) {
     930           0 :                                         *m3p++ = bit_nil;
     931           0 :                                         r3->tnil = true;
     932           0 :                                         r3->tnonil = false;
     933             :                                 } else {
     934           0 :                                         *m3p++ = (o >= lo && o < hi);
     935             :                                 }
     936             :                         }
     937             :                 }
     938           0 :                 TIMEOUT_CHECK(qry_ctx,
     939             :                               GOTO_LABEL_TIMEOUT_HANDLER(bailout, qry_ctx));
     940             :         } else {
     941           0 :                 BATiter li = bat_iterator(l);
     942           0 :                 const oid *lvals = (const oid *) li.base;
     943           0 :                 ltsorted = li.sorted;
     944           0 :                 ltrevsorted = li.revsorted;
     945           0 :                 ltkey = li.key;
     946           0 :                 TIMEOUT_LOOP(lci->ncand, qry_ctx) {
     947           0 :                         oid c = canditer_next(lci);
     948             : 
     949           0 :                         o = lvals[c - l->hseqbase];
     950           0 :                         *o1p++ = c;
     951           0 :                         if (r2) {
     952           0 :                                 if (o >= lo && o < hi) {
     953           0 :                                         *o2p++ = o - r->tseqbase + r->hseqbase;
     954             :                                 } else {
     955           0 :                                         *o2p++ = oid_nil;
     956           0 :                                         r2->tnil = true;
     957           0 :                                         r2->tnonil = false;
     958             :                                 }
     959             :                         }
     960           0 :                         if (r3) {
     961           0 :                                 if (is_oid_nil(o)) {
     962           0 :                                         *m3p++ = bit_nil;
     963           0 :                                         r3->tnil = true;
     964           0 :                                         r3->tnonil = false;
     965             :                                 } else {
     966           0 :                                         *m3p++ = (o >= lo && o < hi);
     967             :                                 }
     968             :                         }
     969             :                 }
     970           0 :                 bat_iterator_end(&li);
     971           0 :                 TIMEOUT_CHECK(qry_ctx,
     972             :                               GOTO_LABEL_TIMEOUT_HANDLER(bailout, qry_ctx));
     973             :         }
     974           0 :         r1->tsorted = true;
     975           0 :         r1->trevsorted = BATcount(r1) <= 1;
     976           0 :         r1->tkey = true;
     977           0 :         r1->tseqbase = oid_nil;
     978           0 :         r1->tnil = false;
     979           0 :         r1->tnonil = true;
     980           0 :         BATsetcount(r1, lci->ncand);
     981           0 :         if (r2) {
     982           0 :                 BATsetcount(r2, lci->ncand);
     983           0 :                 r2->tsorted = ltsorted || BATcount(r2) <= 1;
     984           0 :                 r2->trevsorted = ltrevsorted || BATcount(r2) <= 1;
     985           0 :                 r2->tkey = ltkey || BATcount(r2) <= 1;
     986           0 :                 r2->tseqbase = oid_nil;
     987             :         }
     988           0 :         if (r3) {
     989           0 :                 BATsetcount(r3, lci->ncand);
     990             :         }
     991             : 
     992           0 :   doreturn:
     993           0 :         if (r1->tkey)
     994           0 :                 virtualize(r1);
     995           0 :         if (r2 && r2->tkey && r2->tsorted)
     996           0 :                 virtualize(r2);
     997           0 :   doreturn2:
     998       17649 :         TRC_DEBUG(ALGO, "l=" ALGOBATFMT ","
     999             :                   "r=" ALGOBATFMT ",sl=" ALGOOPTBATFMT ","
    1000             :                   "sr=" ALGOOPTBATFMT ","
    1001             :                   "nil_on_miss=%s,only_misses=%s;%s %s "
    1002             :                   "-> " ALGOBATFMT "," ALGOOPTBATFMT "," ALGOOPTBATFMT
    1003             :                   " (" LLFMT "usec)\n",
    1004             :                   ALGOBATPAR(l), ALGOBATPAR(r),
    1005             :                   ALGOOPTBATPAR(lci->s), ALGOOPTBATPAR(rci->s),
    1006             :                   nil_on_miss ? "true" : "false",
    1007             :                   only_misses ? "true" : "false",
    1008             :                   swapped ? " swapped" : "", reason,
    1009             :                   ALGOBATPAR(r1), ALGOOPTBATPAR(r2), ALGOOPTBATPAR(r3),
    1010             :                   GDKusec() - t0);
    1011             : 
    1012             :         return GDK_SUCCEED;
    1013             : 
    1014           0 :   bailout:
    1015           0 :         BBPreclaim(r1);
    1016           0 :         BBPreclaim(r2);
    1017             :         return GDK_FAIL;
    1018             : }
    1019             : 
    1020             : /* Implementation of mergejoin (see below) for the special case that
    1021             :  * the values are of type int, and some more conditions are met. */
    1022             : static gdk_return
    1023        4914 : mergejoin_int(BAT **r1p, BAT **r2p, BAT *l, BAT *r,
    1024             :               bool nil_matches, BUN estimate, lng t0, bool swapped,
    1025             :               const char *reason)
    1026             : {
    1027        4914 :         BAT *r1, *r2;
    1028        4914 :         BUN lstart, lend, lcnt;
    1029        4914 :         BUN rstart, rend;
    1030        4914 :         BUN lscan, rscan;       /* opportunistic scan window */
    1031        4914 :         BUN maxsize;
    1032        4914 :         const int *lvals, *rvals;
    1033        4914 :         int v;
    1034        4914 :         BUN nl, nr;
    1035        4914 :         oid lv;
    1036        4914 :         BUN i;
    1037        4914 :         BATiter li = bat_iterator(l);
    1038        4914 :         BATiter ri = bat_iterator(r);
    1039             : 
    1040       14742 :         assert(ATOMtype(li.type) == ATOMtype(ri.type));
    1041        4914 :         assert(ri.sorted || ri.revsorted);
    1042             : 
    1043        4914 :         MT_thread_setalgorithm(__func__);
    1044        4914 :         lstart = rstart = 0;
    1045        4914 :         lend = BATcount(l);
    1046        4914 :         lcnt = lend - lstart;
    1047        4914 :         rend = BATcount(r);
    1048        4914 :         lvals = (const int *) li.base;
    1049        4914 :         rvals = (const int *) ri.base;
    1050        4914 :         size_t counter = 0;
    1051        4914 :         QryCtx *qry_ctx = MT_thread_get_qry_ctx();
    1052             : 
    1053             :         /* basic properties will be adjusted if necessary later on,
    1054             :          * they were initially set by joininitresults() */
    1055             : 
    1056        4914 :         if (lend == 0 || rend == 0) {
    1057             :                 /* there are no matches */
    1058           0 :                 bat_iterator_end(&li);
    1059           0 :                 bat_iterator_end(&ri);
    1060           0 :                 return nomatch(r1p, r2p, NULL, l, r,
    1061           0 :                                &(struct canditer) {.tpe = cand_dense, .ncand = lcnt,},
    1062             :                                0, false, false, __func__, t0);
    1063             :         }
    1064             : 
    1065        4912 :         if ((maxsize = joininitresults(r1p, r2p, NULL, BATcount(l), BATcount(r),
    1066        4914 :                                        li.key, ri.key, false, false,
    1067             :                                        false, false, estimate)) == BUN_NONE) {
    1068           0 :                 bat_iterator_end(&li);
    1069           0 :                 bat_iterator_end(&ri);
    1070           0 :                 return GDK_FAIL;
    1071             :         }
    1072        4912 :         r1 = *r1p;
    1073        4912 :         r2 = r2p ? *r2p : NULL;
    1074             : 
    1075             :         /* determine opportunistic scan window for l and r */
    1076       31042 :         for (nl = lend - lstart, lscan = 4; nl > 0; lscan++)
    1077       26130 :                 nl >>= 1;
    1078       36829 :         for (nr = rend - rstart, rscan = 4; nr > 0; rscan++)
    1079       31917 :                 nr >>= 1;
    1080             : 
    1081        4912 :         if (!nil_matches) {
    1082             :                 /* skip over nils at the start of the columns */
    1083        3124 :                 if (lscan < lend - lstart && is_int_nil(lvals[lstart + lscan])) {
    1084           0 :                         lstart = binsearch_int(NULL, 0, lvals, lstart + lscan,
    1085             :                                                lend - 1, int_nil, 1, 1);
    1086             :                 } else {
    1087        3126 :                         while (is_int_nil(lvals[lstart]))
    1088           2 :                                 lstart++;
    1089             :                 }
    1090        3124 :                 if (rscan < rend - rstart && is_int_nil(rvals[rstart + rscan])) {
    1091           0 :                         rstart = binsearch_int(NULL, 0, rvals, rstart + rscan,
    1092             :                                                rend - 1, int_nil, 1, 1);
    1093             :                 } else {
    1094        3124 :                         while (is_int_nil(rvals[rstart]))
    1095           0 :                                 rstart++;
    1096             :                 }
    1097             :         }
    1098             :         /* from here on we don't have to worry about nil values */
    1099             : 
    1100      370122 :         while (lstart < lend && rstart < rend) {
    1101      366240 :                 GDK_CHECK_TIMEOUT(qry_ctx, counter,
    1102             :                                 GOTO_LABEL_TIMEOUT_HANDLER(bailout, qry_ctx));
    1103             : 
    1104      366240 :                 v = rvals[rstart];
    1105             : 
    1106      366240 :                 if (lscan < lend - lstart && lvals[lstart + lscan] < v) {
    1107        1024 :                         lstart = binsearch_int(NULL, 0, lvals, lstart + lscan,
    1108             :                                                lend - 1, v, 1, 0);
    1109             :                 } else {
    1110             :                         /* scan l for v */
    1111      369989 :                         while (lstart < lend && lvals[lstart] < v)
    1112        4773 :                                 lstart++;
    1113             :                 }
    1114      366372 :                 if (lstart >= lend) {
    1115             :                         /* nothing found */
    1116             :                         break;
    1117             :                 }
    1118             : 
    1119             :                 /* Here we determine the next value in l that we are
    1120             :                  * going to try to match in r.  We will also count the
    1121             :                  * number of occurrences in l of that value.
    1122             :                  * Afterwards, v points to the value and nl is the
    1123             :                  * number of times it occurs.  Also, lstart will
    1124             :                  * point to the next value to be considered (ready for
    1125             :                  * the next iteration).
    1126             :                  * If there are many equal values in l (more than
    1127             :                  * lscan), we will use binary search to find the end
    1128             :                  * of the sequence.  Obviously, we can do this only if
    1129             :                  * l is actually sorted (lscan > 0). */
    1130      366033 :                 nl = 1;         /* we'll match (at least) one in l */
    1131      366033 :                 nr = 0;         /* maybe we won't match anything in r */
    1132      366033 :                 v = lvals[lstart];
    1133      366033 :                 if (li.key) {
    1134             :                         /* if l is key, there is a single value */
    1135       54188 :                         lstart++;
    1136      311845 :                 } else if (lscan < lend - lstart &&
    1137      306530 :                            v == lvals[lstart + lscan]) {
    1138             :                         /* lots of equal values: use binary search to
    1139             :                          * find end */
    1140       25199 :                         nl = binsearch_int(NULL, 0, lvals, lstart + lscan,
    1141             :                                            lend - 1, v, 1, 1);
    1142       25190 :                         nl -= lstart;
    1143       25190 :                         lstart += nl;
    1144             :                 } else {
    1145             :                         /* just scan */
    1146     1393155 :                         while (++lstart < lend && v == lvals[lstart])
    1147     1106509 :                                 nl++;
    1148             :                 }
    1149             :                 /* lstart points one beyond the value we're
    1150             :                  * going to match: ready for the next iteration. */
    1151             : 
    1152             :                 /* First we find the first value in r that is at
    1153             :                  * least as large as v, then we find the first
    1154             :                  * value in r that is larger than v.  The difference
    1155             :                  * is the number of values equal to v and is stored in
    1156             :                  * nr.
    1157             :                  * We will use binary search on r to find both ends of
    1158             :                  * the sequence of values that are equal to v in case
    1159             :                  * the position is "too far" (more than rscan
    1160             :                  * away). */
    1161             : 
    1162             :                 /* first find the location of the first value in r
    1163             :                  * that is >= v, then find the location of the first
    1164             :                  * value in r that is > v; the difference is the
    1165             :                  * number of values equal to v */
    1166             : 
    1167             :                 /* look ahead a little (rscan) in r to see whether
    1168             :                  * we're better off doing a binary search */
    1169      366024 :                 if (rscan < rend - rstart && rvals[rstart + rscan] < v) {
    1170             :                         /* value too far away in r: use binary
    1171             :                          * search */
    1172       18200 :                         rstart = binsearch_int(NULL, 0, rvals, rstart + rscan,
    1173             :                                                rend - 1, v, 1, 0);
    1174             :                 } else {
    1175             :                         /* scan r for v */
    1176      368603 :                         while (rstart < rend && rvals[rstart] < v)
    1177       20779 :                                 rstart++;
    1178             :                 }
    1179      366565 :                 if (rstart == rend) {
    1180             :                         /* nothing found */
    1181             :                         break;
    1182             :                 }
    1183             : 
    1184             :                 /* now find the end of the sequence of equal values v */
    1185             : 
    1186             :                 /* if r is key, there is zero or one match, otherwise
    1187             :                  * look ahead a little (rscan) in r to see whether
    1188             :                  * we're better off doing a binary search */
    1189      365872 :                 if (ri.key) {
    1190      165628 :                         if (rstart < rend && v == rvals[rstart]) {
    1191      165778 :                                 nr = 1;
    1192      165778 :                                 rstart++;
    1193             :                         }
    1194      200244 :                 } else if (rscan < rend - rstart &&
    1195      199434 :                            v == rvals[rstart + rscan]) {
    1196             :                         /* range too large: use binary search */
    1197       70552 :                         nr = binsearch_int(NULL, 0, rvals, rstart + rscan,
    1198             :                                            rend - 1, v, 1, 1);
    1199       70575 :                         nr -= rstart;
    1200       70575 :                         rstart += nr;
    1201             :                 } else {
    1202             :                         /* scan r for end of range */
    1203     1054630 :                         while (rstart < rend && v == rvals[rstart]) {
    1204      924938 :                                 nr++;
    1205      924938 :                                 rstart++;
    1206             :                         }
    1207             :                 }
    1208             :                 /* rstart points to first value > v or end of
    1209             :                  * r, and nr is the number of values in r that
    1210             :                  * are equal to v */
    1211      366045 :                 if (nr == 0) {
    1212             :                         /* no entries in r found */
    1213         330 :                         continue;
    1214             :                 }
    1215             :                 /* make space: nl values in l match nr values in r, so
    1216             :                  * we need to add nl * nr values in the results */
    1217      365565 :                 if (maybeextend(r1, r2, NULL, nl * nr, lstart, lend, maxsize) != GDK_SUCCEED)
    1218           0 :                         goto bailout;
    1219             : 
    1220             :                 /* maintain properties */
    1221      364880 :                 if (nl > 1) {
    1222             :                         /* value occurs multiple times in l, so entry
    1223             :                          * in r will be repeated multiple times: hence
    1224             :                          * r2 is not key and not dense */
    1225      245726 :                         if (r2) {
    1226      212753 :                                 r2->tkey = false;
    1227      212753 :                                 r2->tseqbase = oid_nil;
    1228             :                         }
    1229             :                         /* multiple different values will be inserted
    1230             :                          * in r1 (always in order), so not reverse
    1231             :                          * ordered anymore */
    1232      245726 :                         r1->trevsorted = false;
    1233             :                 }
    1234      364880 :                 if (nr > 1) {
    1235             :                         /* value occurs multiple times in r, so entry
    1236             :                          * in l will be repeated multiple times: hence
    1237             :                          * r1 is not key and not dense */
    1238      160475 :                         r1->tkey = false;
    1239      160475 :                         r1->tseqbase = oid_nil;
    1240             :                         /* multiple different values will be inserted
    1241             :                          * in r2 (in order), so not reverse ordered
    1242             :                          * anymore */
    1243      160475 :                         if (r2) {
    1244      109455 :                                 r2->trevsorted = false;
    1245      109455 :                                 if (nl > 1) {
    1246             :                                         /* multiple values in l match
    1247             :                                          * multiple values in r, so an
    1248             :                                          * ordered sequence will be
    1249             :                                          * inserted multiple times in
    1250             :                                          * r2, so r2 is not ordered
    1251             :                                          * anymore */
    1252       83793 :                                         r2->tsorted = false;
    1253             :                                 }
    1254             :                         }
    1255             :                 }
    1256      364880 :                 if (BATcount(r1) > 0) {
    1257             :                         /* a new, higher value will be inserted into
    1258             :                          * r1, so r1 is not reverse ordered anymore */
    1259      360760 :                         r1->trevsorted = false;
    1260             :                         /* a new higher value will be added to r2 */
    1261      360760 :                         if (r2) {
    1262      295657 :                                 r2->trevsorted = false;
    1263             :                         }
    1264      360760 :                         if (BATtdense(r1) &&
    1265      194383 :                             ((oid *) r1->theap->base)[r1->batCount - 1] + 1 != l->hseqbase + lstart - nl) {
    1266          60 :                                 r1->tseqbase = oid_nil;
    1267             :                         }
    1268             :                 }
    1269             : 
    1270      364880 :                 if (r2 &&
    1271      299130 :                     BATcount(r2) > 0 &&
    1272      294690 :                     BATtdense(r2) &&
    1273       73353 :                     ((oid *) r2->theap->base)[r2->batCount - 1] + 1 != r->hseqbase + rstart - nr) {
    1274         512 :                         r2->tseqbase = oid_nil;
    1275             :                 }
    1276             : 
    1277             :                 /* insert values */
    1278      364880 :                 lv = l->hseqbase + lstart - nl;
    1279    15957758 :                 for (i = 0; i < nl; i++) {
    1280             :                         BUN j;
    1281             : 
    1282   115313170 :                         for (j = 0; j < nr; j++) {
    1283    99720292 :                                 APPEND(r1, lv);
    1284             :                         }
    1285    15592878 :                         if (r2) {
    1286    15413565 :                                 oid rv = r->hseqbase + rstart - nr;
    1287             : 
    1288   106181795 :                                 for (j = 0; j < nr; j++) {
    1289    90768230 :                                         APPEND(r2, rv);
    1290    90768230 :                                         rv++;
    1291             :                                 }
    1292             :                         }
    1293    15592878 :                         lv++;
    1294             :                 }
    1295             :         }
    1296             :         /* also set other bits of heap to correct value to indicate size */
    1297        4914 :         BATsetcount(r1, BATcount(r1));
    1298        4914 :         if (r2) {
    1299        3850 :                 BATsetcount(r2, BATcount(r2));
    1300        3849 :                 assert(BATcount(r1) == BATcount(r2));
    1301             :         }
    1302        4913 :         if (BATcount(r1) > 0) {
    1303        4184 :                 if (BATtdense(r1))
    1304        3354 :                         r1->tseqbase = ((oid *) r1->theap->base)[0];
    1305        4184 :                 if (r2 && BATtdense(r2))
    1306        2246 :                         r2->tseqbase = ((oid *) r2->theap->base)[0];
    1307             :         } else {
    1308         729 :                 r1->tseqbase = 0;
    1309         729 :                 if (r2) {
    1310         337 :                         r2->tseqbase = 0;
    1311             :                 }
    1312             :         }
    1313        4913 :         bat_iterator_end(&li);
    1314        4913 :         bat_iterator_end(&ri);
    1315        4914 :         TRC_DEBUG(ALGO, "l=" ALGOBATFMT "," "r=" ALGOBATFMT ","
    1316             :                   "nil_matches=%s;%s %s "
    1317             :                   "-> " ALGOBATFMT "," ALGOOPTBATFMT " (" LLFMT "usec)\n",
    1318             :                   ALGOBATPAR(l), ALGOBATPAR(r),
    1319             :                   nil_matches ? "true" : "false",
    1320             :                   swapped ? " swapped" : "", reason,
    1321             :                   ALGOBATPAR(r1), ALGOOPTBATPAR(r2),
    1322             :                   GDKusec() - t0);
    1323             : 
    1324             :         return GDK_SUCCEED;
    1325             : 
    1326           0 :   bailout:
    1327           0 :         bat_iterator_end(&li);
    1328           0 :         bat_iterator_end(&ri);
    1329           0 :         BBPreclaim(r1);
    1330           0 :         BBPreclaim(r2);
    1331             :         return GDK_FAIL;
    1332             : }
    1333             : 
    1334             : /* Implementation of mergejoin (see below) for the special case that
    1335             :  * the values are of type lng, and some more conditions are met. */
    1336             : static gdk_return
    1337         216 : mergejoin_lng(BAT **r1p, BAT **r2p, BAT *l, BAT *r,
    1338             :               bool nil_matches, BUN estimate, lng t0, bool swapped,
    1339             :               const char *reason)
    1340             : {
    1341         216 :         BAT *r1, *r2;
    1342         216 :         BUN lstart, lend, lcnt;
    1343         216 :         BUN rstart, rend;
    1344         216 :         BUN lscan, rscan;       /* opportunistic scan window */
    1345         216 :         BUN maxsize;
    1346         216 :         const lng *lvals, *rvals;
    1347         216 :         lng v;
    1348         216 :         BUN nl, nr;
    1349         216 :         oid lv;
    1350         216 :         BUN i;
    1351         216 :         BATiter li = bat_iterator(l);
    1352         216 :         BATiter ri = bat_iterator(r);
    1353             : 
    1354         648 :         assert(ATOMtype(li.type) == ATOMtype(ri.type));
    1355         216 :         assert(ri.sorted || ri.revsorted);
    1356             : 
    1357         216 :         MT_thread_setalgorithm(__func__);
    1358         216 :         lstart = rstart = 0;
    1359         216 :         lend = BATcount(l);
    1360         216 :         lcnt = lend - lstart;
    1361         216 :         rend = BATcount(r);
    1362         216 :         lvals = (const lng *) li.base;
    1363         216 :         rvals = (const lng *) ri.base;
    1364         216 :         size_t counter = 0;
    1365         216 :         QryCtx *qry_ctx = MT_thread_get_qry_ctx();
    1366             : 
    1367             :         /* basic properties will be adjusted if necessary later on,
    1368             :          * they were initially set by joininitresults() */
    1369             : 
    1370         216 :         if (lend == 0 || rend == 0) {
    1371             :                 /* there are no matches */
    1372           0 :                 bat_iterator_end(&li);
    1373           0 :                 bat_iterator_end(&ri);
    1374           0 :                 return nomatch(r1p, r2p, NULL, l, r,
    1375           0 :                                &(struct canditer) {.tpe = cand_dense, .ncand = lcnt,},
    1376             :                                0, false, false, __func__, t0);
    1377             :         }
    1378             : 
    1379         216 :         if ((maxsize = joininitresults(r1p, r2p, NULL, BATcount(l), BATcount(r),
    1380         216 :                                        li.key, ri.key, false, false,
    1381             :                                        false, false, estimate)) == BUN_NONE) {
    1382           0 :                 bat_iterator_end(&li);
    1383           0 :                 bat_iterator_end(&ri);
    1384           0 :                 return GDK_FAIL;
    1385             :         }
    1386         216 :         r1 = *r1p;
    1387         216 :         r2 = r2p ? *r2p : NULL;
    1388             : 
    1389             :         /* determine opportunistic scan window for l and r */
    1390        1408 :         for (nl = lend - lstart, lscan = 4; nl > 0; lscan++)
    1391        1192 :                 nl >>= 1;
    1392        1384 :         for (nr = rend - rstart, rscan = 4; nr > 0; rscan++)
    1393        1168 :                 nr >>= 1;
    1394             : 
    1395         216 :         if (!nil_matches) {
    1396             :                 /* skip over nils at the start of the columns */
    1397         100 :                 if (lscan < lend - lstart && is_lng_nil(lvals[lstart + lscan])) {
    1398           0 :                         lstart = binsearch_lng(NULL, 0, lvals, lstart + lscan,
    1399             :                                                lend - 1, lng_nil, 1, 1);
    1400             :                 } else {
    1401         100 :                         while (is_lng_nil(lvals[lstart]))
    1402           0 :                                 lstart++;
    1403             :                 }
    1404         100 :                 if (rscan < rend - rstart && is_lng_nil(rvals[rstart + rscan])) {
    1405           0 :                         rstart = binsearch_lng(NULL, 0, rvals, rstart + rscan,
    1406             :                                                rend - 1, lng_nil, 1, 1);
    1407             :                 } else {
    1408         100 :                         while (is_lng_nil(rvals[rstart]))
    1409           0 :                                 rstart++;
    1410             :                 }
    1411             :         }
    1412             :         /* from here on we don't have to worry about nil values */
    1413             : 
    1414      416137 :         while (lstart < lend && rstart < rend) {
    1415      416006 :                 GDK_CHECK_TIMEOUT(qry_ctx, counter,
    1416             :                                 GOTO_LABEL_TIMEOUT_HANDLER(bailout, qry_ctx));
    1417      416005 :                 v = rvals[rstart];
    1418             : 
    1419      416005 :                 if (lscan < lend - lstart && lvals[lstart + lscan] < v) {
    1420         883 :                         lstart = binsearch_lng(NULL, 0, lvals, lstart + lscan,
    1421             :                                                lend - 1, v, 1, 0);
    1422             :                 } else {
    1423             :                         /* scan l for v */
    1424      497022 :                         while (lstart < lend && lvals[lstart] < v)
    1425       81900 :                                 lstart++;
    1426             :                 }
    1427      415728 :                 if (lstart >= lend) {
    1428             :                         /* nothing found */
    1429             :                         break;
    1430             :                 }
    1431             : 
    1432             :                 /* Here we determine the next value in l that we are
    1433             :                  * going to try to match in r.  We will also count the
    1434             :                  * number of occurrences in l of that value.
    1435             :                  * Afterwards, v points to the value and nl is the
    1436             :                  * number of times it occurs.  Also, lstart will
    1437             :                  * point to the next value to be considered (ready for
    1438             :                  * the next iteration).
    1439             :                  * If there are many equal values in l (more than
    1440             :                  * lscan), we will use binary search to find the end
    1441             :                  * of the sequence.  Obviously, we can do this only if
    1442             :                  * l is actually sorted (lscan > 0). */
    1443      415663 :                 nl = 1;         /* we'll match (at least) one in l */
    1444      415663 :                 nr = 0;         /* maybe we won't match anything in r */
    1445      415663 :                 v = lvals[lstart];
    1446      415663 :                 if (li.key) {
    1447             :                         /* if l is key, there is a single value */
    1448      370907 :                         lstart++;
    1449       44756 :                 } else if (lscan < lend - lstart &&
    1450       44679 :                            v == lvals[lstart + lscan]) {
    1451             :                         /* lots of equal values: use binary search to
    1452             :                          * find end */
    1453         395 :                         nl = binsearch_lng(NULL, 0, lvals, lstart + lscan,
    1454             :                                            lend - 1, v, 1, 1);
    1455         395 :                         nl -= lstart;
    1456         395 :                         lstart += nl;
    1457             :                 } else {
    1458             :                         /* just scan */
    1459       69975 :                         while (++lstart < lend && v == lvals[lstart])
    1460       25614 :                                 nl++;
    1461             :                 }
    1462             :                 /* lstart points one beyond the value we're
    1463             :                  * going to match: ready for the next iteration. */
    1464             : 
    1465             :                 /* First we find the first value in r that is at
    1466             :                  * least as large as v, then we find the first
    1467             :                  * value in r that is larger than v.  The difference
    1468             :                  * is the number of values equal to v and is stored in
    1469             :                  * nr.
    1470             :                  * We will use binary search on r to find both ends of
    1471             :                  * the sequence of values that are equal to v in case
    1472             :                  * the position is "too far" (more than rscan
    1473             :                  * away). */
    1474             : 
    1475             :                 /* first find the location of the first value in r
    1476             :                  * that is >= v, then find the location of the first
    1477             :                  * value in r that is > v; the difference is the
    1478             :                  * number of values equal to v */
    1479             : 
    1480             :                 /* look ahead a little (rscan) in r to see whether
    1481             :                  * we're better off doing a binary search */
    1482      415663 :                 if (rscan < rend - rstart && rvals[rstart + rscan] < v) {
    1483             :                         /* value too far away in r: use binary
    1484             :                          * search */
    1485        2229 :                         rstart = binsearch_lng(NULL, 0, rvals, rstart + rscan,
    1486             :                                                rend - 1, v, 1, 0);
    1487             :                 } else {
    1488             :                         /* scan r for v */
    1489     1495013 :                         while (rstart < rend && rvals[rstart] < v)
    1490     1081579 :                                 rstart++;
    1491             :                 }
    1492      415753 :                 if (rstart == rend) {
    1493             :                         /* nothing found */
    1494             :                         break;
    1495             :                 }
    1496             : 
    1497             :                 /* now find the end of the sequence of equal values v */
    1498             : 
    1499             :                 /* if r is key, there is zero or one match, otherwise
    1500             :                  * look ahead a little (rscan) in r to see whether
    1501             :                  * we're better off doing a binary search */
    1502      415734 :                 if (ri.key) {
    1503      377024 :                         if (rstart < rend && v == rvals[rstart]) {
    1504       82076 :                                 nr = 1;
    1505       82076 :                                 rstart++;
    1506             :                         }
    1507       38710 :                 } else if (rscan < rend - rstart &&
    1508       38668 :                            v == rvals[rstart + rscan]) {
    1509             :                         /* range too large: use binary search */
    1510           0 :                         nr = binsearch_lng(NULL, 0, rvals, rstart + rscan,
    1511             :                                            rend - 1, v, 1, 1);
    1512           0 :                         nr -= rstart;
    1513           0 :                         rstart += nr;
    1514             :                 } else {
    1515             :                         /* scan r for end of range */
    1516       95505 :                         while (rstart < rend && v == rvals[rstart]) {
    1517       56795 :                                 nr++;
    1518       56795 :                                 rstart++;
    1519             :                         }
    1520             :                 }
    1521             :                 /* rstart points to first value > v or end of
    1522             :                  * r, and nr is the number of values in r that
    1523             :                  * are equal to v */
    1524      120786 :                 if (nr == 0) {
    1525             :                         /* no entries in r found */
    1526      294974 :                         continue;
    1527             :                 }
    1528             :                 /* make space: nl values in l match nr values in r, so
    1529             :                  * we need to add nl * nr values in the results */
    1530      120760 :                 if (maybeextend(r1, r2, NULL, nl * nr, lstart, lend, maxsize) != GDK_SUCCEED)
    1531           0 :                         goto bailout;
    1532             : 
    1533             :                 /* maintain properties */
    1534      120947 :                 if (nl > 1) {
    1535             :                         /* value occurs multiple times in l, so entry
    1536             :                          * in r will be repeated multiple times: hence
    1537             :                          * r2 is not key and not dense */
    1538        6720 :                         if (r2) {
    1539        1844 :                                 r2->tkey = false;
    1540        1844 :                                 r2->tseqbase = oid_nil;
    1541             :                         }
    1542             :                         /* multiple different values will be inserted
    1543             :                          * in r1 (always in order), so not reverse
    1544             :                          * ordered anymore */
    1545        6720 :                         r1->trevsorted = false;
    1546             :                 }
    1547      120947 :                 if (nr > 1) {
    1548             :                         /* value occurs multiple times in r, so entry
    1549             :                          * in l will be repeated multiple times: hence
    1550             :                          * r1 is not key and not dense */
    1551        5319 :                         r1->tkey = false;
    1552        5319 :                         r1->tseqbase = oid_nil;
    1553             :                         /* multiple different values will be inserted
    1554             :                          * in r2 (in order), so not reverse ordered
    1555             :                          * anymore */
    1556        5319 :                         if (r2) {
    1557        5319 :                                 r2->trevsorted = false;
    1558        5319 :                                 if (nl > 1) {
    1559             :                                         /* multiple values in l match
    1560             :                                          * multiple values in r, so an
    1561             :                                          * ordered sequence will be
    1562             :                                          * inserted multiple times in
    1563             :                                          * r2, so r2 is not ordered
    1564             :                                          * anymore */
    1565          51 :                                         r2->tsorted = false;
    1566             :                                 }
    1567             :                         }
    1568             :                 }
    1569      120947 :                 if (BATcount(r1) > 0) {
    1570             :                         /* a new, higher value will be inserted into
    1571             :                          * r1, so r1 is not reverse ordered anymore */
    1572      120563 :                         r1->trevsorted = false;
    1573             :                         /* a new higher value will be added to r2 */
    1574      120563 :                         if (r2) {
    1575      114091 :                                 r2->trevsorted = false;
    1576             :                         }
    1577      120563 :                         if (BATtdense(r1) &&
    1578       52804 :                             ((oid *) r1->theap->base)[r1->batCount - 1] + 1 != l->hseqbase + lstart - nl) {
    1579          53 :                                 r1->tseqbase = oid_nil;
    1580             :                         }
    1581             :                 }
    1582             : 
    1583      120947 :                 if (r2 &&
    1584      114465 :                     BATcount(r2) > 0 &&
    1585      114084 :                     BATtdense(r2) &&
    1586       51667 :                     ((oid *) r2->theap->base)[r2->batCount - 1] + 1 != r->hseqbase + rstart - nr) {
    1587          26 :                         r2->tseqbase = oid_nil;
    1588             :                 }
    1589             : 
    1590             :                 /* insert values */
    1591      120947 :                 lv = l->hseqbase + lstart - nl;
    1592      271689 :                 for (i = 0; i < nl; i++) {
    1593             :                         BUN j;
    1594             : 
    1595      320119 :                         for (j = 0; j < nr; j++) {
    1596      169377 :                                 APPEND(r1, lv);
    1597             :                         }
    1598      150742 :                         if (r2) {
    1599      133384 :                                 oid rv = r->hseqbase + rstart - nr;
    1600             : 
    1601      285231 :                                 for (j = 0; j < nr; j++) {
    1602      151847 :                                         APPEND(r2, rv);
    1603      151847 :                                         rv++;
    1604             :                                 }
    1605             :                         }
    1606      150742 :                         lv++;
    1607             :                 }
    1608             :         }
    1609             :         /* also set other bits of heap to correct value to indicate size */
    1610         215 :         BATsetcount(r1, BATcount(r1));
    1611         215 :         if (r2) {
    1612         196 :                 BATsetcount(r2, BATcount(r2));
    1613         196 :                 assert(BATcount(r1) == BATcount(r2));
    1614             :         }
    1615         215 :         if (BATcount(r1) > 0) {
    1616         191 :                 if (BATtdense(r1))
    1617         122 :                         r1->tseqbase = ((oid *) r1->theap->base)[0];
    1618         191 :                 if (r2 && BATtdense(r2))
    1619         121 :                         r2->tseqbase = ((oid *) r2->theap->base)[0];
    1620             :         } else {
    1621          24 :                 r1->tseqbase = 0;
    1622          24 :                 if (r2) {
    1623          14 :                         r2->tseqbase = 0;
    1624             :                 }
    1625             :         }
    1626         215 :         bat_iterator_end(&li);
    1627         215 :         bat_iterator_end(&ri);
    1628         215 :         TRC_DEBUG(ALGO, "l=" ALGOBATFMT "," "r=" ALGOBATFMT ","
    1629             :                   "nil_matches=%s;%s %s "
    1630             :                   "-> " ALGOBATFMT "," ALGOOPTBATFMT " (" LLFMT "usec)\n",
    1631             :                   ALGOBATPAR(l), ALGOBATPAR(r),
    1632             :                   nil_matches ? "true" : "false",
    1633             :                   swapped ? " swapped" : "", reason,
    1634             :                   ALGOBATPAR(r1), ALGOOPTBATPAR(r2),
    1635             :                   GDKusec() - t0);
    1636             : 
    1637             :         return GDK_SUCCEED;
    1638             : 
    1639           1 :   bailout:
    1640           1 :         bat_iterator_end(&li);
    1641           1 :         bat_iterator_end(&ri);
    1642           1 :         BBPreclaim(r1);
    1643           1 :         BBPreclaim(r2);
    1644             :         return GDK_FAIL;
    1645             : }
    1646             : 
    1647             : /* Implementation of mergejoin (see below) for the special case that
    1648             :  * the values are of type oid, and the right-hand side is a candidate
    1649             :  * list with exception, and some more conditions are met. */
    1650             : static gdk_return
    1651           0 : mergejoin_cand(BAT **r1p, BAT **r2p, BAT *l, BAT *r,
    1652             :                bool nil_matches, BUN estimate, lng t0, bool swapped,
    1653             :                const char *reason)
    1654             : {
    1655             : /* the comments in this function have not been checked after making a
    1656             :  * copy of mergejoin below and adapting it to a mask right-hand side */
    1657           0 :         BAT *r1, *r2;
    1658           0 :         BUN lstart, lend, lcnt;
    1659           0 :         struct canditer lci, rci;
    1660           0 :         BUN lscan;              /* opportunistic scan window */
    1661           0 :         BUN maxsize;
    1662           0 :         const oid *lvals;
    1663           0 :         oid v;
    1664           0 :         BUN nl, nr;
    1665           0 :         oid lv;
    1666           0 :         BUN i;
    1667           0 :         BATiter li = bat_iterator(l);
    1668           0 :         BATiter ri = bat_iterator(r);
    1669             : 
    1670           0 :         assert(ATOMtype(li.type) == ATOMtype(ri.type));
    1671             : 
    1672           0 :         MT_thread_setalgorithm(__func__);
    1673           0 :         lstart = 0;
    1674           0 :         lend = BATcount(l);
    1675           0 :         lcnt = lend - lstart;
    1676           0 :         if (li.type == TYPE_void) {
    1677           0 :                 assert(!is_oid_nil(l->tseqbase));
    1678           0 :                 canditer_init(&lci, NULL, l);
    1679           0 :                 lcnt = lci.ncand;
    1680           0 :                 lvals = NULL;
    1681             :         } else {
    1682           0 :                 lci = (struct canditer) {.tpe = cand_dense}; /* not used */
    1683           0 :                 lvals = (const oid *) li.base;
    1684           0 :                 assert(lvals != NULL);
    1685             :         }
    1686             : 
    1687           0 :         assert(complex_cand(r));
    1688           0 :         canditer_init(&rci, NULL, r);
    1689           0 :         size_t counter = 0;
    1690           0 :         QryCtx *qry_ctx = MT_thread_get_qry_ctx();
    1691             : 
    1692             :         /* basic properties will be adjusted if necessary later on,
    1693             :          * they were initially set by joininitresults() */
    1694             : 
    1695           0 :         if (lend == 0 || rci.ncand == 0) {
    1696             :                 /* there are no matches */
    1697           0 :                 bat_iterator_end(&li);
    1698           0 :                 bat_iterator_end(&ri);
    1699           0 :                 return nomatch(r1p, r2p, NULL, l, r,
    1700           0 :                                &(struct canditer) {.tpe = cand_dense, .ncand = lcnt,},
    1701             :                                0, false, false, __func__, t0);
    1702             :         }
    1703             : 
    1704           0 :         if ((maxsize = joininitresults(r1p, r2p, NULL, BATcount(l), BATcount(r),
    1705           0 :                                        li.key, ri.key, false, false,
    1706             :                                        false, false, estimate)) == BUN_NONE) {
    1707           0 :                 bat_iterator_end(&li);
    1708           0 :                 bat_iterator_end(&ri);
    1709           0 :                 return GDK_FAIL;
    1710             :         }
    1711           0 :         r1 = *r1p;
    1712           0 :         r2 = r2p ? *r2p : NULL;
    1713             : 
    1714             :         /* determine opportunistic scan window for l and r */
    1715           0 :         for (nl = lend - lstart, lscan = 4; nl > 0; lscan++)
    1716           0 :                 nl >>= 1;
    1717             : 
    1718           0 :         if (!nil_matches) {
    1719             :                 /* skip over nils at the start of the columns */
    1720           0 :                 if (lscan < lend - lstart && lvals && is_oid_nil(lvals[lstart + lscan])) {
    1721           0 :                         lstart = binsearch_oid(NULL, 0, lvals, lstart + lscan,
    1722             :                                                lend - 1, oid_nil, 1, 1);
    1723           0 :                 } else if (lvals) {
    1724           0 :                         while (is_oid_nil(lvals[lstart]))
    1725           0 :                                 lstart++;
    1726             :                 } /* else l is candidate list: no nils */
    1727             :         }
    1728             :         /* from here on we don't have to worry about nil values */
    1729             : 
    1730           0 :         while (lstart < lend && rci.next < rci.ncand) {
    1731           0 :                 GDK_CHECK_TIMEOUT(qry_ctx, counter,
    1732             :                                 GOTO_LABEL_TIMEOUT_HANDLER(bailout, qry_ctx));
    1733           0 :                 v = canditer_peek(&rci);
    1734             : 
    1735           0 :                 if (lvals) {
    1736           0 :                         if (lscan < lend - lstart &&
    1737           0 :                             lvals[lstart + lscan] < v) {
    1738           0 :                                 lstart = binsearch_oid(NULL, 0, lvals,
    1739             :                                                        lstart + lscan,
    1740             :                                                        lend - 1, v, 1, 0);
    1741             :                         } else {
    1742             :                                 /* scan l for v */
    1743           0 :                                 while (lstart < lend && lvals[lstart] < v)
    1744           0 :                                         lstart++;
    1745             :                         }
    1746             :                 } else {
    1747           0 :                         lstart = canditer_search(&lci, v, true);
    1748           0 :                         canditer_setidx(&lci, lstart);
    1749             :                 }
    1750           0 :                 if (lstart >= lend) {
    1751             :                         /* nothing found */
    1752             :                         break;
    1753             :                 }
    1754             : 
    1755             :                 /* Here we determine the next value in l that we are
    1756             :                  * going to try to match in r.  We will also count the
    1757             :                  * number of occurrences in l of that value.
    1758             :                  * Afterwards, v points to the value and nl is the
    1759             :                  * number of times it occurs.  Also, lstart will
    1760             :                  * point to the next value to be considered (ready for
    1761             :                  * the next iteration).
    1762             :                  * If there are many equal values in l (more than
    1763             :                  * lscan), we will use binary search to find the end
    1764             :                  * of the sequence.  Obviously, we can do this only if
    1765             :                  * l is actually sorted (lscan > 0). */
    1766           0 :                 nl = 1;         /* we'll match (at least) one in l */
    1767           0 :                 nr = 0;         /* maybe we won't match anything in r */
    1768           0 :                 v = lvals ? lvals[lstart] : canditer_next(&lci);
    1769           0 :                 if (li.key || lvals == NULL) {
    1770             :                         /* if l is key, there is a single value */
    1771           0 :                         lstart++;
    1772           0 :                 } else if (lscan < lend - lstart &&
    1773           0 :                            v == lvals[lstart + lscan]) {
    1774             :                         /* lots of equal values: use binary search to
    1775             :                          * find end */
    1776           0 :                         nl = binsearch_oid(NULL, 0, lvals, lstart + lscan,
    1777             :                                            lend - 1, v, 1, 1);
    1778           0 :                         nl -= lstart;
    1779           0 :                         lstart += nl;
    1780             :                 } else {
    1781             :                         /* just scan */
    1782           0 :                         while (++lstart < lend && v == lvals[lstart])
    1783           0 :                                 nl++;
    1784             :                 }
    1785             :                 /* lstart points one beyond the value we're
    1786             :                  * going to match: ready for the next iteration. */
    1787             : 
    1788             :                 /* First we find the first value in r that is at
    1789             :                  * least as large as v, then we find the first
    1790             :                  * value in r that is larger than v.  The difference
    1791             :                  * is the number of values equal to v and is stored in
    1792             :                  * nr.
    1793             :                  * We will use binary search on r to find both ends of
    1794             :                  * the sequence of values that are equal to v in case
    1795             :                  * the position is "too far" (more than rscan
    1796             :                  * away). */
    1797             : 
    1798             :                 /* first find the location of the first value in r
    1799             :                  * that is >= v, then find the location of the first
    1800             :                  * value in r that is > v; the difference is the
    1801             :                  * number of values equal to v */
    1802           0 :                 nr = canditer_search(&rci, v, true);
    1803           0 :                 canditer_setidx(&rci, nr);
    1804           0 :                 if (nr == rci.ncand) {
    1805             :                         /* nothing found */
    1806             :                         break;
    1807             :                 }
    1808             : 
    1809             :                 /* now find the end of the sequence of equal values v */
    1810             : 
    1811             :                 /* if r is key, there is zero or one match, otherwise
    1812             :                  * look ahead a little (rscan) in r to see whether
    1813             :                  * we're better off doing a binary search */
    1814           0 :                 if (canditer_peek(&rci) == v) {
    1815           0 :                         nr = 1;
    1816           0 :                         canditer_next(&rci);
    1817             :                 } else {
    1818             :                         /* rci points to first value > v or end of
    1819             :                          * r, and nr is the number of values in r that
    1820             :                          * are equal to v */
    1821             :                         /* no entries in r found */
    1822           0 :                         continue;
    1823             :                 }
    1824             :                 /* make space: nl values in l match nr values in r, so
    1825             :                  * we need to add nl * nr values in the results */
    1826           0 :                 if (maybeextend(r1, r2, NULL, nl * nr, lstart, lend, maxsize) != GDK_SUCCEED)
    1827           0 :                         goto bailout;
    1828             : 
    1829             :                 /* maintain properties */
    1830           0 :                 if (nl > 1) {
    1831             :                         /* value occurs multiple times in l, so entry
    1832             :                          * in r will be repeated multiple times: hence
    1833             :                          * r2 is not key and not dense */
    1834           0 :                         if (r2) {
    1835           0 :                                 r2->tkey = false;
    1836           0 :                                 r2->tseqbase = oid_nil;
    1837             :                         }
    1838             :                         /* multiple different values will be inserted
    1839             :                          * in r1 (always in order), so not reverse
    1840             :                          * ordered anymore */
    1841           0 :                         r1->trevsorted = false;
    1842             :                 }
    1843           0 :                 if (BATcount(r1) > 0) {
    1844             :                         /* a new, higher value will be inserted into
    1845             :                          * r1, so r1 is not reverse ordered anymore */
    1846           0 :                         r1->trevsorted = false;
    1847             :                         /* a new higher value will be added to r2 */
    1848           0 :                         if (r2) {
    1849           0 :                                 r2->trevsorted = false;
    1850             :                         }
    1851           0 :                         if (BATtdense(r1) &&
    1852           0 :                             ((oid *) r1->theap->base)[r1->batCount - 1] + 1 != l->hseqbase + lstart - nl) {
    1853           0 :                                 r1->tseqbase = oid_nil;
    1854             :                         }
    1855             :                 }
    1856             : 
    1857           0 :                 if (r2 &&
    1858           0 :                     BATcount(r2) > 0 &&
    1859           0 :                     BATtdense(r2) &&
    1860           0 :                     ((oid *) r2->theap->base)[r2->batCount - 1] + 1 != r->hseqbase + rci.next - nr) {
    1861           0 :                         r2->tseqbase = oid_nil;
    1862             :                 }
    1863             : 
    1864             :                 /* insert values */
    1865           0 :                 lv = l->hseqbase + lstart - nl;
    1866           0 :                 for (i = 0; i < nl; i++) {
    1867             :                         BUN j;
    1868             : 
    1869           0 :                         for (j = 0; j < nr; j++) {
    1870           0 :                                 APPEND(r1, lv);
    1871             :                         }
    1872           0 :                         if (r2) {
    1873           0 :                                 oid rv = r->hseqbase + rci.next - nr;
    1874             : 
    1875           0 :                                 for (j = 0; j < nr; j++) {
    1876           0 :                                         APPEND(r2, rv);
    1877           0 :                                         rv++;
    1878             :                                 }
    1879             :                         }
    1880           0 :                         lv++;
    1881             :                 }
    1882             :         }
    1883             :         /* also set other bits of heap to correct value to indicate size */
    1884           0 :         BATsetcount(r1, BATcount(r1));
    1885           0 :         if (r2) {
    1886           0 :                 BATsetcount(r2, BATcount(r2));
    1887           0 :                 assert(BATcount(r1) == BATcount(r2));
    1888             :         }
    1889           0 :         if (BATcount(r1) > 0) {
    1890           0 :                 if (BATtdense(r1))
    1891           0 :                         r1->tseqbase = ((oid *) r1->theap->base)[0];
    1892           0 :                 if (r2 && BATtdense(r2))
    1893           0 :                         r2->tseqbase = ((oid *) r2->theap->base)[0];
    1894             :         } else {
    1895           0 :                 r1->tseqbase = 0;
    1896           0 :                 if (r2) {
    1897           0 :                         r2->tseqbase = 0;
    1898             :                 }
    1899             :         }
    1900           0 :         bat_iterator_end(&li);
    1901           0 :         bat_iterator_end(&ri);
    1902           0 :         TRC_DEBUG(ALGO, "l=" ALGOBATFMT "," "r=" ALGOBATFMT ","
    1903             :                   "nil_matches=%s;%s %s "
    1904             :                   "-> " ALGOBATFMT "," ALGOOPTBATFMT " (" LLFMT "usec)\n",
    1905             :                   ALGOBATPAR(l), ALGOBATPAR(r),
    1906             :                   nil_matches ? "true" : "false",
    1907             :                   swapped ? " swapped" : "", reason,
    1908             :                   ALGOBATPAR(r1), ALGOOPTBATPAR(r2),
    1909             :                   GDKusec() - t0);
    1910             : 
    1911             :         return GDK_SUCCEED;
    1912             : 
    1913           0 :   bailout:
    1914           0 :         bat_iterator_end(&li);
    1915           0 :         bat_iterator_end(&ri);
    1916           0 :         BBPreclaim(r1);
    1917           0 :         BBPreclaim(r2);
    1918             :         return GDK_FAIL;
    1919             : }
    1920             : 
    1921             : /* Perform a "merge" join on l and r (if both are sorted) with
    1922             :  * optional candidate lists, or join using binary search on r if l is
    1923             :  * not sorted.
    1924             :  *
    1925             :  * If nil_matches is set, nil values are treated as ordinary values
    1926             :  * that can match; otherwise nil values never match.
    1927             :  *
    1928             :  * If nil_on_miss is set, a nil value is returned in r2 if there is no
    1929             :  * match in r for a particular value in l (left outer join).
    1930             :  *
    1931             :  * If semi is set, only a single set of values in r1/r2 is returned if
    1932             :  * there is a match of l in r, no matter how many matches there are in
    1933             :  * r; otherwise all matches are returned.
    1934             :  *
    1935             :  * If max_one is set, only a single match is allowed.  This is like
    1936             :  * semi, but enforces the single match.
    1937             :  *
    1938             :  * t0 and swapped are only for debugging (ALGOMASK set in GDKdebug).
    1939             :  */
    1940             : static gdk_return
    1941       10560 : mergejoin(BAT **r1p, BAT **r2p, BAT **r3p, BAT *l, BAT *r,
    1942             :           struct canditer *restrict lci, struct canditer *restrict rci,
    1943             :           bool nil_matches, bool nil_on_miss, bool semi, bool only_misses,
    1944             :           bool not_in, bool max_one, bool min_one, BUN estimate,
    1945             :           lng t0, bool swapped,
    1946             :           const char *reason)
    1947             : {
    1948             :         /* [lr]scan determine how far we look ahead in l/r in order to
    1949             :          * decide whether we want to do a binary search or a scan */
    1950       10560 :         BUN lscan, rscan;
    1951       10560 :         const void *lvals, *rvals; /* the values of l/r (NULL if dense) */
    1952       10560 :         const char *lvars, *rvars; /* the indirect values (NULL if fixed size) */
    1953       10560 :         const void *nil = ATOMnilptr(l->ttype);
    1954       10560 :         int (*cmp)(const void *, const void *) = ATOMcompare(l->ttype);
    1955       10560 :         const void *v;          /* points to value under consideration */
    1956       10560 :         const void *prev = NULL;
    1957       10560 :         BUN nl, nr;
    1958       10560 :         bool insert_nil;
    1959             :         /* equal_order is set if we can scan both BATs in the same
    1960             :          * order, so when both are sorted or both are reverse sorted
    1961             :          * -- important to know in order to skip over values; if l is
    1962             :          * not sorted, this must be set to true and we will always do a
    1963             :          * binary search on all of r */
    1964       10560 :         bool equal_order;
    1965             :         /* [lr]ordering is either 1 or -1 depending on the order of
    1966             :          * l/r: it determines the comparison function used */
    1967       10560 :         int lordering, rordering;
    1968       10560 :         oid lv;
    1969       10560 :         BUN i, j;               /* counters */
    1970       10560 :         oid lval = oid_nil, rval = oid_nil; /* temporary space to point v to */
    1971       10560 :         struct canditer llci, rrci;
    1972       10560 :         struct canditer *mlci, xlci;
    1973       10560 :         struct canditer *mrci, xrci;
    1974             : 
    1975       10560 :         if (lci->tpe == cand_dense && lci->ncand == BATcount(l) &&
    1976       10541 :             rci->tpe == cand_dense && rci->ncand == BATcount(r) &&
    1977        9960 :             !nil_on_miss && !semi && !max_one && !min_one && !only_misses &&
    1978        6208 :             !not_in &&
    1979        5233 :             l->tsorted && r->tsorted) {
    1980             :                 /* special cases with far fewer options */
    1981        5220 :                 if (complex_cand(r))
    1982           0 :                         return mergejoin_cand(r1p, r2p, l, r, nil_matches,
    1983             :                                               estimate, t0, swapped, __func__);
    1984       10397 :                 switch (ATOMbasetype(l->ttype)) {
    1985        4914 :                 case TYPE_int:
    1986        4914 :                         return mergejoin_int(r1p, r2p, l, r, nil_matches,
    1987             :                                              estimate, t0, swapped, __func__);
    1988         216 :                 case TYPE_lng:
    1989         216 :                         return mergejoin_lng(r1p, r2p, l, r, nil_matches,
    1990             :                                              estimate, t0, swapped, __func__);
    1991             :                 }
    1992             :         }
    1993             : 
    1994       16234 :         assert(ATOMtype(l->ttype) == ATOMtype(r->ttype));
    1995        5430 :         assert(r->tsorted || r->trevsorted);
    1996             : 
    1997        5430 :         size_t counter = 0;
    1998        5430 :         QryCtx *qry_ctx = MT_thread_get_qry_ctx();
    1999             : 
    2000        5430 :         BATiter li = bat_iterator(l);
    2001        5430 :         BATiter ri = bat_iterator(r);
    2002        5430 :         MT_thread_setalgorithm(__func__);
    2003        5430 :         if (BATtvoid(l)) {
    2004             :                 /* l->ttype == TYPE_void && is_oid_nil(l->tseqbase) is
    2005             :                  * handled by selectjoin */
    2006          55 :                 assert(!is_oid_nil(l->tseqbase));
    2007          55 :                 canditer_init(&llci, NULL, l);
    2008          55 :                 lvals = NULL;
    2009             :         } else {
    2010        5375 :                 lvals = li.base;                              /* non NULL */
    2011        5375 :                 llci = (struct canditer) {.tpe = cand_dense}; /* not used */
    2012             :         }
    2013        5430 :         rrci = (struct canditer) {.tpe = cand_dense};
    2014        5430 :         if (BATtvoid(r)) {
    2015           1 :                 if (!is_oid_nil(r->tseqbase))
    2016           1 :                         canditer_init(&rrci, NULL, r);
    2017             :                 rvals = NULL;
    2018             :         } else {
    2019        5429 :                 rvals = ri.base;
    2020             :         }
    2021        5430 :         if (li.vh && li.type) {
    2022          99 :                 assert(ri.vh && ri.type);
    2023          99 :                 lvars = li.vh->base;
    2024          99 :                 rvars = ri.vh->base;
    2025             :         } else {
    2026        5331 :                 assert(ri.vh == NULL || ri.type == TYPE_void);
    2027             :                 lvars = rvars = NULL;
    2028             :         }
    2029             :         /* if the var pointer is not NULL, then so is the val pointer */
    2030        5430 :         assert(lvars == NULL || lvals != NULL);
    2031        5430 :         assert(rvars == NULL || rvals != NULL);
    2032             : 
    2033        5430 :         const bool rhasnil = !ri.nonil &&
    2034         471 :                 ((BATtvoid(r) && r->tseqbase == oid_nil) ||
    2035         471 :                  (rvals && cmp(nil, VALUE(r, (ri.sorted ? rci->seq : canditer_last(rci)) - r->hseqbase)) == 0));
    2036          19 :         const bit defmark = rhasnil ? bit_nil : 0;
    2037             : 
    2038        5430 :         if (not_in && (rhasnil || (BATtvoid(l) && l->tseqbase == oid_nil))) {
    2039           0 :                 bat_iterator_end(&li);
    2040           0 :                 bat_iterator_end(&ri);
    2041           0 :                 return nomatch(r1p, r2p, r3p, l, r, lci, defmark, false, false,
    2042             :                                __func__, t0);
    2043             :         }
    2044             : 
    2045        5430 :         if ((!nil_matches &&
    2046        4989 :              ((li.type == TYPE_void && is_oid_nil(l->tseqbase)) ||
    2047        4989 :               (ri.type == TYPE_void && is_oid_nil(r->tseqbase)))) ||
    2048        5430 :             (li.type == TYPE_void && is_oid_nil(l->tseqbase) &&
    2049           0 :              (ri.nonil ||
    2050           0 :               (ri.type == TYPE_void && !is_oid_nil(r->tseqbase)))) ||
    2051        5430 :             (ri.type == TYPE_void && is_oid_nil(r->tseqbase) &&
    2052           0 :              (li.nonil ||
    2053           0 :               (li.type == TYPE_void && !is_oid_nil(l->tseqbase))))) {
    2054             :                 /* there are no matches */
    2055           0 :                 bat_iterator_end(&li);
    2056           0 :                 bat_iterator_end(&ri);
    2057           0 :                 return nomatch(r1p, r2p, r3p, l, r, lci, defmark,
    2058             :                                nil_on_miss, only_misses, __func__, t0);
    2059             :         }
    2060             : 
    2061       10860 :         BUN maxsize = joininitresults(r1p, r2p, r3p, lci->ncand, rci->ncand,
    2062        5430 :                                       li.key, ri.key, semi | max_one,
    2063             :                                       nil_on_miss, only_misses, min_one,
    2064             :                                       estimate);
    2065        5430 :         if (maxsize == BUN_NONE) {
    2066           0 :                 bat_iterator_end(&li);
    2067           0 :                 bat_iterator_end(&ri);
    2068           0 :                 return GDK_FAIL;
    2069             :         }
    2070        5430 :         BAT *r1 = *r1p;
    2071        5430 :         BAT *r2 = r2p ? *r2p : NULL;
    2072        5430 :         BAT *r3 = r3p ? *r3p : NULL;
    2073             : 
    2074        5430 :         if (lci->tpe == cand_mask) {
    2075           0 :                 mlci = lci;
    2076           0 :                 canditer_init(&xlci, l, NULL);
    2077           0 :                 lci = &xlci;
    2078             :         } else {
    2079        5430 :                 mlci = NULL;
    2080        5430 :                 xlci = (struct canditer) {.tpe = cand_dense}; /* not used */
    2081             :         }
    2082        5430 :         if (rci->tpe == cand_mask) {
    2083           0 :                 mrci = rci;
    2084           0 :                 canditer_init(&xrci, r, NULL);
    2085           0 :                 rci = &xrci;
    2086             :         } else {
    2087        5430 :                 mrci = NULL;
    2088        5430 :                 xrci = (struct canditer) {.tpe = cand_dense}; /* not used */
    2089             :         }
    2090             : 
    2091        5430 :         if (li.sorted || li.revsorted) {
    2092        4379 :                 equal_order = (li.sorted && ri.sorted) ||
    2093         134 :                         (li.revsorted && ri.revsorted &&
    2094          66 :                          !BATtvoid(l) && !BATtvoid(r));
    2095        4379 :                 lordering = li.sorted && (ri.sorted || !equal_order) ? 1 : -1;
    2096        4335 :                 rordering = equal_order ? lordering : -lordering;
    2097        4379 :                 if (!li.nonil && !nil_matches && !nil_on_miss && lvals != NULL) {
    2098             :                         /* find first non-nil */
    2099         100 :                         nl = binsearch(NULL, 0, li.type, lvals, lvars, li.width, 0, BATcount(l), nil, li.sorted ? 1 : -1, li.sorted ? 1 : 0);
    2100          98 :                         nl = canditer_search(lci, nl + l->hseqbase, true);
    2101          98 :                         if (li.sorted) {
    2102          95 :                                 canditer_setidx(lci, nl);
    2103           3 :                         } else if (li.revsorted) {
    2104           3 :                                 lci->ncand = nl;
    2105             :                         }
    2106             :                 }
    2107             :                 /* determine opportunistic scan window for l */
    2108        8758 :                 lscan = 4 + ilog2(lci->ncand);
    2109             :         } else {
    2110             :                 /* if l not sorted, we will always use binary search
    2111             :                  * on r */
    2112        1051 :                 assert(!BATtvoid(l)); /* void is always sorted */
    2113        1051 :                 lscan = 0;
    2114        1051 :                 equal_order = true;
    2115        1051 :                 lordering = 1;
    2116        1051 :                 rordering = ri.sorted ? 1 : -1;
    2117             :         }
    2118             :         /* determine opportunistic scan window for r; if l is not
    2119             :          * sorted this is only used to find range of equal values */
    2120        5430 :         rscan = 4 + ilog2(rci->ncand);
    2121             : 
    2122        5430 :         if (!equal_order) {
    2123             :                 /* we go through r backwards */
    2124          68 :                 canditer_setidx(rci, rci->ncand);
    2125             :         }
    2126             :         /* At this point the various variables that help us through
    2127             :          * the algorithm have been set.  The table explains them.  The
    2128             :          * first two columns are the inputs, the next three columns
    2129             :          * are the variables, the final two columns indicate how the
    2130             :          * variables can be used.
    2131             :          *
    2132             :          * l/r    sl/sr | vals  cand  off | result   value being matched
    2133             :          * -------------+-----------------+----------------------------------
    2134             :          * dense  NULL  | NULL  NULL  set | i        off==nil?nil:i+off
    2135             :          * dense  dense | NULL  NULL  set | i        off==nil?nil:i+off
    2136             :          * dense  set   | NULL  set   set | cand[i]  off==nil?nil:cand[i]+off
    2137             :          * set    NULL  | set   NULL  0   | i        vals[i]
    2138             :          * set    dense | set   NULL  0   | i        vals[i]
    2139             :          * set    set   | set   set   0   | cand[i]  vals[cand[i]]
    2140             :          *
    2141             :          * If {l,r}off is lng_nil, all values in the corresponding bat
    2142             :          * are oid_nil because the bat has type VOID and the tseqbase
    2143             :          * is nil.
    2144             :          */
    2145             : 
    2146             : 
    2147             :         /* Before we start adding values to r1 and r2, the properties
    2148             :          * are as follows:
    2149             :          * tseqbase - 0
    2150             :          * tkey - true
    2151             :          * tsorted - true
    2152             :          * trevsorted - true
    2153             :          * tnil - false
    2154             :          * tnonil - true
    2155             :          * We will modify these as we go along.
    2156             :          */
    2157      328754 :         while (lci->next < lci->ncand) {
    2158      325552 :                 GDK_CHECK_TIMEOUT(qry_ctx, counter,
    2159             :                                 GOTO_LABEL_TIMEOUT_HANDLER(bailout, qry_ctx));
    2160      325552 :                 bit mark = defmark;
    2161      325552 :                 if (lscan == 0) {
    2162             :                         /* always search r completely */
    2163      146231 :                         assert(equal_order);
    2164      146231 :                         canditer_reset(rci);
    2165             :                 } else {
    2166             :                         /* If l is sorted (lscan > 0), we look at the
    2167             :                          * next value in r to see whether we can jump
    2168             :                          * over a large section of l using binary
    2169             :                          * search.  We do this by looking ahead in l
    2170             :                          * (lscan far, to be precise) and seeing if
    2171             :                          * the value there is still too "small"
    2172             :                          * (definition depends on sort order of l).
    2173             :                          * If it is, we use binary search on l,
    2174             :                          * otherwise we scan l for the next position
    2175             :                          * with a value greater than or equal to the
    2176             :                          * value in r.
    2177             :                          * The next value to match in r is the first
    2178             :                          * if equal_order is set, the last
    2179             :                          * otherwise.
    2180             :                          * When skipping over values in l, we count
    2181             :                          * how many we skip in nlx.  We need this in
    2182             :                          * case only_misses or nil_on_miss is set, and
    2183             :                          * to properly set the dense property in the
    2184             :                          * first output BAT. */
    2185      179321 :                         BUN nlx = 0; /* number of non-matching values in l */
    2186             : 
    2187      179321 :                         if (equal_order) {
    2188      178628 :                                 if (rci->next == rci->ncand)
    2189             :                                         v = NULL; /* no more values */
    2190      176458 :                                 else if (mrci) {
    2191           0 :                                         oid rv = canditer_mask_next(mrci, canditer_peek(rci), true);
    2192           0 :                                         v = rv == oid_nil ? NULL : VALUE(r, rv - r->hseqbase);
    2193             :                                 } else
    2194      176458 :                                         v = VALUE(r, canditer_peek(rci) - r->hseqbase);
    2195             :                         } else {
    2196         693 :                                 if (rci->next == 0)
    2197             :                                         v = NULL; /* no more values */
    2198         687 :                                 else if (mrci) {
    2199           0 :                                         oid rv = canditer_mask_next(mrci, canditer_peekprev(rci), false);
    2200           0 :                                         v = rv == oid_nil ? NULL : VALUE(r, rv - r->hseqbase);
    2201             :                                 } else
    2202         687 :                                         v = VALUE(r, canditer_peekprev(rci) - r->hseqbase);
    2203             :                         }
    2204             :                         /* here, v points to next value in r, or if
    2205             :                          * we're at the end of r, v is NULL */
    2206          17 :                         if (v == NULL) {
    2207        2176 :                                 nlx = lci->ncand - lci->next;
    2208             :                         } else {
    2209      177145 :                                 if (lscan < lci->ncand - lci->next) {
    2210      156187 :                                         lv = canditer_idx(lci, lci->next + lscan);
    2211      156187 :                                         lv -= l->hseqbase;
    2212      156187 :                                         if (lvals) {
    2213      151945 :                                                 if (lordering * cmp(VALUE(l, lv), v) < 0) {
    2214        1496 :                                                         nlx = binsearch(NULL, 0, li.type, lvals, lvars, li.width, lv, BATcount(l), v, lordering, 0);
    2215        1496 :                                                         nlx = canditer_search(lci, nlx + l->hseqbase, true);
    2216        1496 :                                                         nlx -= lci->next;
    2217             :                                                 }
    2218             :                                         } else {
    2219        4242 :                                                 assert(lordering == 1);
    2220        4242 :                                                 if (canditer_idx(&llci, lv) < *(const oid *)v) {
    2221           8 :                                                         nlx = canditer_search(&llci, *(const oid *)v, true);
    2222           8 :                                                         nlx = canditer_search(lci, nlx + l->hseqbase, true);
    2223           8 :                                                         nlx -= lci->next;
    2224             :                                                 }
    2225             :                                         }
    2226      156285 :                                         if (mlci) {
    2227           0 :                                                 lv = canditer_mask_next(mlci, lci->seq + lci->next + nlx, true);
    2228           0 :                                                 if (lv == oid_nil)
    2229           0 :                                                         nlx = lci->ncand - lci->next;
    2230             :                                                 else
    2231           0 :                                                         nlx = lv - lci->seq - lci->next;
    2232             :                                         }
    2233      156285 :                                         if (lci->next + nlx == lci->ncand)
    2234          11 :                                                 v = NULL;
    2235             :                                 }
    2236             :                         }
    2237      158461 :                         if (nlx > 0) {
    2238        3680 :                                 if (only_misses) {
    2239        2062 :                                         if (maybeextend(r1, r2, r3, nlx, lci->next, lci->ncand, maxsize) != GDK_SUCCEED)
    2240           0 :                                                 goto bailout;
    2241       45092 :                                         while (nlx > 0) {
    2242       43030 :                                                 lv = canditer_next(lci);
    2243       43030 :                                                 if (mlci == NULL || canditer_contains(mlci, lv))
    2244       43030 :                                                         APPEND(r1, lv);
    2245       43030 :                                                 nlx--;
    2246             :                                         }
    2247        2062 :                                         if (r1->trevsorted && BATcount(r1) > 1)
    2248         535 :                                                 r1->trevsorted = false;
    2249        1618 :                                 } else if (nil_on_miss) {
    2250          21 :                                         if (r2 && r2->tnonil) {
    2251           2 :                                                 r2->tnil = true;
    2252           2 :                                                 r2->tnonil = false;
    2253           2 :                                                 r2->tseqbase = oid_nil;
    2254           2 :                                                 r2->tsorted = false;
    2255           2 :                                                 r2->trevsorted = false;
    2256           2 :                                                 r2->tkey = false;
    2257             :                                         }
    2258          21 :                                         if (maybeextend(r1, r2, r3, nlx, lci->next, lci->ncand, maxsize) != GDK_SUCCEED)
    2259           0 :                                                 goto bailout;
    2260          21 :                                         if (r3)
    2261          20 :                                                 r3->tnil = false;
    2262        2081 :                                         while (nlx > 0) {
    2263        2060 :                                                 lv = canditer_next(lci);
    2264        2060 :                                                 if (mlci == NULL || canditer_contains(mlci, lv)) {
    2265        2060 :                                                         APPEND(r1, lv);
    2266        2060 :                                                         if (r2)
    2267           2 :                                                                 APPEND(r2, oid_nil);
    2268        2060 :                                                         if (r3) {
    2269        2059 :                                                                 if (rhasnil || cmp(VALUE(l, lv - l->hseqbase), nil) == 0) {
    2270           0 :                                                                         ((bit *) r3->theap->base)[r3->batCount++] = bit_nil;
    2271           0 :                                                                         r3->tnil = true;
    2272             :                                                                 } else {
    2273        2059 :                                                                         ((bit *) r3->theap->base)[r3->batCount++] = 0;
    2274             :                                                                 }
    2275             :                                                         }
    2276             :                                                 }
    2277        2060 :                                                 nlx--;
    2278             :                                         }
    2279          21 :                                         if (r1->trevsorted && BATcount(r1) > 1)
    2280           8 :                                                 r1->trevsorted = false;
    2281             :                                 } else {
    2282        1597 :                                         canditer_setidx(lci, lci->next + nlx);
    2283             :                                 }
    2284             :                         }
    2285      179419 :                         if (v == NULL) {
    2286             :                                 /* we have exhausted the inputs */
    2287             :                                 break;
    2288             :                         }
    2289             :                 }
    2290             : 
    2291             :                 /* Here we determine the next value in l that we are
    2292             :                  * going to try to match in r.  We will also count the
    2293             :                  * number of occurrences in l of that value.
    2294             :                  * Afterwards, v points to the value and nl is the
    2295             :                  * number of times it occurs.  Also, lci will point to
    2296             :                  * the next value to be considered (ready for the next
    2297             :                  * iteration).
    2298             :                  * If there are many equal values in l (more than
    2299             :                  * lscan), we will use binary search to find the end
    2300             :                  * of the sequence.  Obviously, we can do this only if
    2301             :                  * l is actually sorted (lscan > 0). */
    2302      319661 :                 nl = 1;         /* we'll match (at least) one in l */
    2303      319661 :                 nr = 0;         /* maybe we won't match anything in r */
    2304      319661 :                 lv = canditer_peek(lci);
    2305      319661 :                 if (mlci) {
    2306           0 :                         lv = canditer_mask_next(mlci, lv, true);
    2307           0 :                         if (lv == oid_nil)
    2308             :                                 break;
    2309           0 :                         canditer_setidx(lci, canditer_search(lci, lv, true));
    2310             :                 }
    2311      320326 :                 v = VALUE(l, lv - l->hseqbase);
    2312      320326 :                 if (li.key) {
    2313             :                         /* if l is key, there is a single value */
    2314      191226 :                 } else if (lscan > 0 &&
    2315      108810 :                            lscan < lci->ncand - lci->next &&
    2316       53853 :                            cmp(v, VALUE(l, canditer_idx(lci, lci->next + lscan) - l->hseqbase)) == 0) {
    2317             :                         /* lots of equal values: use binary search to
    2318             :                          * find end */
    2319         667 :                         assert(lvals != NULL);
    2320        1334 :                         nl = binsearch(NULL, 0,
    2321         667 :                                        li.type, lvals, lvars,
    2322         667 :                                        li.width, lci->next + lscan,
    2323             :                                        BATcount(l),
    2324             :                                        v, lordering, 1);
    2325         667 :                         nl = canditer_search(lci, nl + l->hseqbase, true);
    2326         667 :                         nl -= lci->next;
    2327             :                 } else {
    2328      190551 :                         struct canditer ci = *lci; /* work on copy */
    2329      190551 :                         nl = 0; /* it will be incremented again */
    2330      345324 :                         do {
    2331      345324 :                                 canditer_next(&ci);
    2332      342141 :                                 nl++;
    2333      680656 :                         } while (ci.next < ci.ncand &&
    2334      341229 :                                  cmp(v, VALUE(l, canditer_peek(&ci) - l->hseqbase)) == 0);
    2335             :                 }
    2336             :                 /* lci->next + nl is the position for the next iteration */
    2337             : 
    2338      314421 :                 if ((!nil_matches || not_in) && !li.nonil && cmp(v, nil) == 0) {
    2339         664 :                         if (not_in) {
    2340             :                                 /* just skip the whole thing: nils
    2341             :                                  * don't cause any output */
    2342           1 :                                 canditer_setidx(lci, lci->next + nl);
    2343           1 :                                 continue;
    2344             :                         }
    2345             :                         /* v is nil and nils don't match anything, set
    2346             :                          * to NULL to indicate nil */
    2347         663 :                         v = NULL;
    2348         663 :                         mark = bit_nil;
    2349         663 :                         if (r3)
    2350          54 :                                 r3->tnil = true;
    2351             :                 }
    2352             : 
    2353             :                 /* First we find the "first" value in r that is "at
    2354             :                  * least as large" as v, then we find the "first"
    2355             :                  * value in r that is "larger" than v.  The difference
    2356             :                  * is the number of values equal to v and is stored in
    2357             :                  * nr.  The definitions of "larger" and "first" depend
    2358             :                  * on the orderings of l and r.  If equal_order is
    2359             :                  * set, we go through r from low to high (this
    2360             :                  * includes the case that l is not sorted); otherwise
    2361             :                  * we go through r from high to low.
    2362             :                  * In either case, we will use binary search on r to
    2363             :                  * find both ends of the sequence of values that are
    2364             :                  * equal to v in case the position is "too far" (more
    2365             :                  * than rscan away). */
    2366          54 :                 if (v == NULL) {
    2367             :                         nr = 0; /* nils don't match anything */
    2368      312180 :                 } else if (ri.type == TYPE_void && is_oid_nil(r->tseqbase)) {
    2369           0 :                         if (is_oid_nil(*(const oid *) v)) {
    2370             :                                 /* all values in r match */
    2371           0 :                                 nr = rci->ncand;
    2372             :                         } else {
    2373             :                                 /* no value in r matches */
    2374             :                                 nr = 0;
    2375             :                         }
    2376             :                         /* in either case, we're done after this */
    2377           0 :                         canditer_setidx(rci, equal_order ? rci->ncand : 0);
    2378      312180 :                 } else if (equal_order) {
    2379             :                         /* first find the location of the first value
    2380             :                          * in r that is >= v, then find the location
    2381             :                          * of the first value in r that is > v; the
    2382             :                          * difference is the number of values equal
    2383             :                          * v; we change rci */
    2384             : 
    2385             :                         /* look ahead a little (rscan) in r to
    2386             :                          * see whether we're better off doing
    2387             :                          * a binary search */
    2388      311493 :                         if (rvals) {
    2389      311476 :                                 if (rscan < rci->ncand - rci->next &&
    2390      271677 :                                     rordering * cmp(v, VALUE(r, canditer_idx(rci, rci->next + rscan) - r->hseqbase)) > 0) {
    2391             :                                         /* value too far away in r:
    2392             :                                          * use binary search */
    2393       84310 :                                         lv = binsearch(NULL, 0, ri.type, rvals, rvars, ri.width, rci->next + rscan, BATcount(r), v, rordering, 0);
    2394       97707 :                                         lv = canditer_search(rci, lv + r->hseqbase, true);
    2395       97707 :                                         canditer_setidx(rci, lv);
    2396             :                                 } else {
    2397             :                                         /* scan r for v */
    2398      238131 :                                         while (rci->next < rci->ncand) {
    2399      237498 :                                                 if (rordering * cmp(v, VALUE(r, canditer_peek(rci) - r->hseqbase)) <= 0)
    2400             :                                                         break;
    2401       11981 :                                                 canditer_next(rci);
    2402             :                                         }
    2403             :                                 }
    2404      624228 :                                 if (rci->next < rci->ncand &&
    2405      303785 :                                     cmp(v, VALUE(r, canditer_peek(rci) - r->hseqbase)) == 0) {
    2406             :                                         /* if we found an equal value,
    2407             :                                          * look for the last equal
    2408             :                                          * value */
    2409      216667 :                                         if (ri.key) {
    2410             :                                                 /* r is key, there can
    2411             :                                                  * only be a single
    2412             :                                                  * equal value */
    2413      109226 :                                                 nr = 1;
    2414      109226 :                                                 canditer_next(rci);
    2415      213337 :                                         } else if (rscan < rci->ncand - rci->next &&
    2416      105897 :                                                    cmp(v, VALUE(r, canditer_idx(rci, rci->next + rscan) - r->hseqbase)) == 0) {
    2417             :                                                 /* many equal values:
    2418             :                                                  * use binary search
    2419             :                                                  * to find the end */
    2420       43084 :                                                 nr = binsearch(NULL, 0, ri.type, rvals, rvars, ri.width, rci->next + rscan, BATcount(r), v, rordering, 1);
    2421       43084 :                                                 nr = canditer_search(rci, nr + r->hseqbase, true);
    2422       43084 :                                                 nr -= rci->next;
    2423       43084 :                                                 canditer_setidx(rci, rci->next + nr);
    2424             :                                         } else {
    2425             :                                                 /* scan r for end of
    2426             :                                                  * range */
    2427      175603 :                                                 do {
    2428      175603 :                                                         nr++;
    2429      175603 :                                                         canditer_next(rci);
    2430      350838 :                                                 } while (rci->next < rci->ncand &&
    2431      175253 :                                                          cmp(v, VALUE(r, canditer_peek(rci) - r->hseqbase)) == 0);
    2432             :                                         }
    2433             :                                 }
    2434             :                         } else {
    2435          17 :                                 assert(rordering == 1);
    2436          17 :                                 rval = canditer_search(&rrci, *(const oid*)v, true) + r->hseqbase;
    2437          17 :                                 lv = canditer_search(rci, rval, true);
    2438          17 :                                 canditer_setidx(rci, lv);
    2439          17 :                                 nr = (canditer_idx(&rrci, canditer_peek(rci) - r->hseqbase) == *(oid*)v);
    2440          17 :                                 if (nr == 1)
    2441          17 :                                         canditer_next(rci);
    2442             :                         }
    2443             :                         /* rci points to first value > v or end of r,
    2444             :                          * and nr is the number of values in r that
    2445             :                          * are equal to v */
    2446             :                 } else {
    2447             :                         /* first find the location of the first value
    2448             :                          * in r that is > v, then find the location
    2449             :                          * of the first value in r that is >= v; the
    2450             :                          * difference is the number of values equal
    2451             :                          * v; we change rci */
    2452             : 
    2453             :                         /* look back from the end a little
    2454             :                          * (rscan) in r to see whether we're
    2455             :                          * better off doing a binary search */
    2456         687 :                         if (rvals) {
    2457         687 :                                 if (rci->next > rscan &&
    2458         491 :                                     rordering * cmp(v, VALUE(r, canditer_idx(rci, rci->next - rscan) - r->hseqbase)) < 0) {
    2459             :                                         /* value too far away
    2460             :                                          * in r: use binary
    2461             :                                          * search */
    2462          13 :                                         lv = binsearch(NULL, 0, ri.type, rvals, rvars, ri.width, 0, rci->next - rscan, v, rordering, 1);
    2463          13 :                                         lv = canditer_search(rci, lv + r->hseqbase, true);
    2464          13 :                                         canditer_setidx(rci, lv);
    2465             :                                 } else {
    2466             :                                         /* scan r for v */
    2467         928 :                                         while (rci->next > 0 &&
    2468         921 :                                                rordering * cmp(v, VALUE(r, canditer_peekprev(rci) - r->hseqbase)) < 0)
    2469         254 :                                                 canditer_prev(rci);
    2470             :                                 }
    2471        1367 :                                 if (rci->next > 0 &&
    2472         680 :                                     cmp(v, VALUE(r, canditer_peekprev(rci) - r->hseqbase)) == 0) {
    2473             :                                         /* if we found an equal value,
    2474             :                                          * look for the last equal
    2475             :                                          * value */
    2476         558 :                                         if (ri.key) {
    2477             :                                                 /* r is key, there can only be a single equal value */
    2478          71 :                                                 nr = 1;
    2479          71 :                                                 canditer_prev(rci);
    2480         934 :                                         } else if (rci->next > rscan &&
    2481         447 :                                                    cmp(v, VALUE(r, canditer_idx(rci, rci->next - rscan) - r->hseqbase)) == 0) {
    2482             :                                                 /* use binary search to find the start */
    2483           0 :                                                 nr = binsearch(NULL, 0, ri.type, rvals, rvars, ri.width, 0, rci->next - rscan, v, rordering, 0);
    2484           0 :                                                 nr = canditer_search(rci, nr + r->hseqbase, true);
    2485           0 :                                                 nr = rci->next - nr;
    2486           0 :                                                 canditer_setidx(rci, rci->next - nr);
    2487             :                                         } else {
    2488             :                                                 /* scan r for start of range */
    2489         525 :                                                 do {
    2490         525 :                                                         canditer_prev(rci);
    2491         525 :                                                         nr++;
    2492        1045 :                                                 } while (rci->next > 0 &&
    2493         520 :                                                          cmp(v, VALUE(r, canditer_peekprev(rci) - r->hseqbase)) == 0);
    2494             :                                         }
    2495             :                                 }
    2496             :                         } else {
    2497           0 :                                 lv = canditer_search(&rrci, *(const oid *)v, true);
    2498           0 :                                 lv = canditer_search(rci, lv + r->hseqbase, true);
    2499           0 :                                 nr = (canditer_idx(rci, lv) == *(const oid*)v);
    2500           0 :                                 canditer_setidx(rci, lv);
    2501             :                         }
    2502             :                         /* rci points to first value > v
    2503             :                          * or end of r, and nr is the number of values
    2504             :                          * in r that are equal to v */
    2505             :                 }
    2506             : 
    2507      219409 :                 if (nr == 0) {
    2508             :                         /* no entries in r found */
    2509      106743 :                         if (!(nil_on_miss | only_misses)) {
    2510       86689 :                                 if (min_one) {
    2511           0 :                                         GDKerror("not enough matches");
    2512           0 :                                         goto bailout;
    2513             :                                 }
    2514       90910 :                                 if (lscan > 0 &&
    2515        4221 :                                     (equal_order ? rci->next == rci->ncand : rci->next == 0)) {
    2516             :                                         /* nothing more left to match
    2517             :                                          * in r */
    2518             :                                         break;
    2519             :                                 }
    2520       86669 :                                 canditer_setidx(lci, lci->next + nl);
    2521       86292 :                                 continue;
    2522             :                         }
    2523             :                         /* insert a nil to indicate a non-match */
    2524       20054 :                         insert_nil = true;
    2525       20054 :                         nr = 1;
    2526       20054 :                         if (r2) {
    2527           4 :                                 r2->tnil = true;
    2528           4 :                                 r2->tnonil = false;
    2529           4 :                                 r2->tsorted = false;
    2530           4 :                                 r2->trevsorted = false;
    2531           4 :                                 r2->tseqbase = oid_nil;
    2532           4 :                                 r2->tkey = false;
    2533             :                         }
    2534      217129 :                 } else if (nr > 1 && max_one) {
    2535          21 :                         GDKerror("more than one match");
    2536          21 :                         goto bailout;
    2537      217108 :                 } else if (only_misses) {
    2538             :                         /* we had a match, so we're not interested */
    2539       93398 :                         canditer_setidx(lci, lci->next + nl);
    2540       93413 :                         continue;
    2541             :                 } else {
    2542      123710 :                         insert_nil = false;
    2543      123710 :                         if (semi) {
    2544             :                                 /* for semi-join, only insert single
    2545             :                                  * value */
    2546       19503 :                                 nr = 1;
    2547             :                         }
    2548             :                 }
    2549             :                 /* make space: nl values in l match nr values in r, so
    2550             :                  * we need to add nl * nr values in the results */
    2551      143764 :                 if (maybeextend(r1, r2, r3, nl * nr, lci->next, lci->ncand, maxsize) != GDK_SUCCEED)
    2552           0 :                         goto bailout;
    2553             : 
    2554             :                 /* maintain properties */
    2555      143780 :                 if (nl > 1) {
    2556       51435 :                         if (r2) {
    2557             :                                 /* value occurs multiple times in l,
    2558             :                                  * so entry in r will be repeated
    2559             :                                  * multiple times: hence r2 is not key
    2560             :                                  * and not dense */
    2561       11361 :                                 r2->tkey = false;
    2562       11361 :                                 r2->tseqbase = oid_nil;
    2563             :                         }
    2564             :                         /* multiple different values will be inserted
    2565             :                          * in r1 (always in order), so not reverse
    2566             :                          * ordered anymore */
    2567       51435 :                         r1->trevsorted = false;
    2568             :                 }
    2569      143780 :                 if (nr > 1) {
    2570             :                         /* value occurs multiple times in r, so entry
    2571             :                          * in l will be repeated multiple times: hence
    2572             :                          * r1 is not key and not dense */
    2573       42943 :                         r1->tkey = false;
    2574       42943 :                         if (r2) {
    2575             :                                 /* multiple different values will be
    2576             :                                  * inserted in r2 (in order), so not
    2577             :                                  * reverse ordered anymore */
    2578       42528 :                                 r2->trevsorted = false;
    2579       42528 :                                 if (nl > 1) {
    2580             :                                         /* multiple values in l match
    2581             :                                          * multiple values in r, so an
    2582             :                                          * ordered sequence will be
    2583             :                                          * inserted multiple times in
    2584             :                                          * r2, so r2 is not ordered
    2585             :                                          * anymore */
    2586        3875 :                                         r2->tsorted = false;
    2587             :                                 }
    2588             :                         }
    2589             :                 }
    2590      143780 :                 if (lscan == 0) {
    2591             :                         /* deduce relative positions of r matches for
    2592             :                          * this and previous value in v */
    2593       59124 :                         if (prev && r2) {
    2594             :                                 /* keyness or r2 can only be assured
    2595             :                                  * as long as matched values are
    2596             :                                  * ordered */
    2597       58015 :                                 int ord = rordering * cmp(prev, v ? v : nil);
    2598       58280 :                                 if (ord < 0) {
    2599             :                                         /* previous value in l was
    2600             :                                          * less than current */
    2601       23603 :                                         r2->trevsorted = false;
    2602       23603 :                                         r2->tkey &= r2->tsorted;
    2603       34677 :                                 } else if (ord > 0) {
    2604             :                                         /* previous value was
    2605             :                                          * greater */
    2606       22008 :                                         r2->tsorted = false;
    2607       22008 :                                         r2->tkey &= r2->trevsorted;
    2608             :                                 } else {
    2609             :                                         /* value can be equal if
    2610             :                                          * intervening values in l
    2611             :                                          * didn't match anything; if
    2612             :                                          * multiple values match in r,
    2613             :                                          * r2 won't be sorted */
    2614       12669 :                                         r2->tkey = false;
    2615       12669 :                                         if (nr > 1) {
    2616       12640 :                                                 r2->tsorted = false;
    2617       12640 :                                                 r2->trevsorted = false;
    2618             :                                         }
    2619             :                                 }
    2620             :                         }
    2621       59389 :                         prev = v ? v : nil;
    2622             :                 }
    2623      144045 :                 if (BATcount(r1) > 0) {
    2624             :                         /* a new, higher value will be inserted into
    2625             :                          * r1, so r1 is not reverse ordered anymore */
    2626      139780 :                         r1->trevsorted = false;
    2627      139780 :                         if (r2) {
    2628             :                                 /* depending on whether l and r are
    2629             :                                  * ordered the same or not, a new
    2630             :                                  * higher or lower value will be added
    2631             :                                  * to r2 */
    2632       60721 :                                 if (equal_order)
    2633       60672 :                                         r2->trevsorted = false;
    2634             :                                 else {
    2635          49 :                                         r2->tsorted = false;
    2636          49 :                                         r2->tseqbase = oid_nil;
    2637             :                                 }
    2638             :                         }
    2639             :                 }
    2640             : 
    2641             :                 /* insert values: first the left output */
    2642             :                 BUN nladded = 0;
    2643      395583 :                 for (i = 0; i < nl; i++) {
    2644      251970 :                         lv = canditer_next(lci);
    2645      251538 :                         if (mlci == NULL || canditer_contains(mlci, lv)) {
    2646      251148 :                                 nladded++;
    2647    39416486 :                                 for (j = 0; j < nr; j++)
    2648    39165338 :                                         APPEND(r1, lv);
    2649             :                         }
    2650             :                 }
    2651      143613 :                 nl = nladded;
    2652             :                 /* then the right output, various different ways of
    2653             :                  * doing it */
    2654      143613 :                 if (r2) {
    2655       61337 :                         if (insert_nil) {
    2656          11 :                                 for (i = 0; i < nl; i++) {
    2657          14 :                                         for (j = 0; j < nr; j++) {
    2658           7 :                                                 APPEND(r2, oid_nil);
    2659             :                                         }
    2660             :                                 }
    2661       61333 :                         } else if (equal_order) {
    2662       61254 :                                 struct canditer ci = *rci; /* work on copy */
    2663       61254 :                                 if (r2->batCount > 0 &&
    2664       60647 :                                     BATtdense(r2) &&
    2665        3806 :                                     ((oid *) r2->theap->base)[r2->batCount - 1] + 1 != canditer_idx(&ci, ci.next - nr))
    2666          59 :                                         r2->tseqbase = oid_nil;
    2667      147231 :                                 for (i = 0; i < nl; i++) {
    2668       85972 :                                         canditer_setidx(&ci, ci.next - nr);
    2669    39170951 :                                         for (j = 0; j < nr; j++) {
    2670    38999002 :                                                 APPEND(r2, canditer_next(&ci));
    2671             :                                         }
    2672             :                                 }
    2673             :                         } else {
    2674          79 :                                 if (r2->batCount > 0 &&
    2675          49 :                                     BATtdense(r2) &&
    2676           0 :                                     ((oid *) r2->theap->base)[r2->batCount - 1] + 1 != canditer_peek(rci))
    2677           0 :                                         r2->tseqbase = oid_nil;
    2678         261 :                                 for (i = 0; i < nl; i++) {
    2679         182 :                                         struct canditer ci = *rci; /* work on copy */
    2680         364 :                                         for (j = 0; j < nr; j++) {
    2681         182 :                                                 APPEND(r2, canditer_next(&ci));
    2682             :                                         }
    2683             :                                 }
    2684             :                         }
    2685             :                 }
    2686             :                 /* finally the mark output */
    2687      143618 :                 if (r3) {
    2688        2705 :                         if (insert_nil) {
    2689         334 :                                 r3->tnil |= rhasnil;
    2690         836 :                                 for (i = 0; i < nl; i++) {
    2691        1004 :                                         for (j = 0; j < nr; j++) {
    2692         502 :                                                 ((bit *) r3->theap->base)[r3->batCount++] = mark;
    2693             :                                         }
    2694             :                                 }
    2695             :                         } else {
    2696        8154 :                                 for (i = 0; i < nl; i++) {
    2697       11567 :                                         for (j = 0; j < nr; j++) {
    2698        5784 :                                                 ((bit *) r3->theap->base)[r3->batCount++] = 1;
    2699             :                                         }
    2700             :                                 }
    2701             :                         }
    2702             :                 }
    2703             :         }
    2704             :         /* also set other bits of heap to correct value to indicate size */
    2705        5409 :         BATsetcount(r1, BATcount(r1));
    2706        5409 :         r1->tseqbase = oid_nil;
    2707        5409 :         if (r1->tkey)
    2708        5386 :                 r1 = virtualize(r1);
    2709        5409 :         if (r2) {
    2710        1081 :                 BATsetcount(r2, BATcount(r2));
    2711        1081 :                 assert(BATcount(r1) == BATcount(r2));
    2712        1081 :                 r2->tseqbase = oid_nil;
    2713        1081 :                 if (BATcount(r2) <= 1) {
    2714         399 :                         r2->tkey = true;
    2715         399 :                         r2 = virtualize(r2);
    2716             :                 }
    2717             :         }
    2718        5409 :         if (r3) {
    2719          61 :                 BATsetcount(r3, BATcount(r3));
    2720          61 :                 assert(BATcount(r1) == BATcount(r3));
    2721          61 :                 r3->tseqbase = oid_nil;
    2722          61 :                 r3->tnonil = !r3->tnil;
    2723          61 :                 if (BATcount(r3) <= 1) {
    2724           0 :                         r3->tkey = true;
    2725           0 :                         r3->tsorted = true;
    2726           0 :                         r3->trevsorted = true;
    2727             :                 }
    2728             :         }
    2729        5409 :         bat_iterator_end(&li);
    2730        5409 :         bat_iterator_end(&ri);
    2731        5409 :         TRC_DEBUG(ALGO, "l=" ALGOBATFMT ","
    2732             :                   "r=" ALGOBATFMT ",sl=" ALGOOPTBATFMT ","
    2733             :                   "sr=" ALGOOPTBATFMT ","
    2734             :                   "nil_on_miss=%s,semi=%s,only_misses=%s,not_in=%s;%s %s "
    2735             :                   "-> " ALGOBATFMT "," ALGOOPTBATFMT " (" LLFMT "usec)\n",
    2736             :                   ALGOBATPAR(l), ALGOBATPAR(r),
    2737             :                   ALGOOPTBATPAR(lci->s), ALGOOPTBATPAR(rci->s),
    2738             :                   nil_on_miss ? "true" : "false",
    2739             :                   semi ? "true" : "false",
    2740             :                   only_misses ? "true" : "false",
    2741             :                   not_in ? "true" : "false",
    2742             :                   swapped ? " swapped" : "", reason,
    2743             :                   ALGOBATPAR(r1), ALGOOPTBATPAR(r2),
    2744             :                   GDKusec() - t0);
    2745             : 
    2746             :         return GDK_SUCCEED;
    2747             : 
    2748          21 :   bailout:
    2749          21 :         bat_iterator_end(&li);
    2750          21 :         bat_iterator_end(&ri);
    2751          21 :         BBPreclaim(r1);
    2752          21 :         BBPreclaim(r2);
    2753          21 :         BBPreclaim(r3);
    2754             :         return GDK_FAIL;
    2755             : }
    2756             : 
    2757             : #define HASHLOOPBODY()                                                  \
    2758             :         do {                                                            \
    2759             :                 if (nr >= 1 && max_one) {                            \
    2760             :                         GDKerror("more than one match");              \
    2761             :                         goto bailout;                                   \
    2762             :                 }                                                       \
    2763             :                 if (maybeextend(r1, r2, r3, 1, lci->next, lci->ncand, maxsize) != GDK_SUCCEED) \
    2764             :                         goto bailout;                                   \
    2765             :                 APPEND(r1, lo);                                         \
    2766             :                 if (r2)                                                 \
    2767             :                         APPEND(r2, ro);                                 \
    2768             :                 if (r3)                                                 \
    2769             :                         ((bit *) r3->theap->base)[r3->batCount++] = 1; \
    2770             :                 nr++;                                                   \
    2771             :         } while (false)
    2772             : 
    2773             : #define EQ_int(a, b)    ((a) == (b))
    2774             : #define EQ_lng(a, b)    ((a) == (b))
    2775             : #ifdef HAVE_HGE
    2776             : #define EQ_uuid(a, b)   ((a).h == (b).h)
    2777             : #else
    2778             : #define EQ_uuid(a, b)   (memcmp((a).u, (b).u, UUID_SIZE) == 0)
    2779             : #endif
    2780             : 
    2781             : #define HASHJOIN(TYPE)                                                  \
    2782             :         do {                                                            \
    2783             :                 TYPE *rvals = ri.base;                                  \
    2784             :                 TYPE *lvals = li.base;                                  \
    2785             :                 TYPE v;                                                 \
    2786             :                 while (lci->next < lci->ncand) {                       \
    2787             :                         GDK_CHECK_TIMEOUT(qry_ctx, counter, GOTO_LABEL_TIMEOUT_HANDLER(bailout, qry_ctx)); \
    2788             :                         lo = canditer_next(lci);                        \
    2789             :                         v = lvals[lo - l->hseqbase];                 \
    2790             :                         nr = 0;                                         \
    2791             :                         bit mark = defmark;                             \
    2792             :                         if ((!nil_matches || not_in) && is_##TYPE##_nil(v)) { \
    2793             :                                 /* no match */                          \
    2794             :                                 if (not_in) {                           \
    2795             :                                         lskipped = BATcount(r1) > 0; \
    2796             :                                         continue;                       \
    2797             :                                 }                                       \
    2798             :                                 mark = bit_nil;                         \
    2799             :                         } else if (hash_cand) {                         \
    2800             :                                 /* private hash: no locks */            \
    2801             :                                 for (rb = HASHget(hsh, hash_##TYPE(hsh, &v)); \
    2802             :                                      rb != BUN_NONE;                    \
    2803             :                                      rb = HASHgetlink(hsh, rb)) {       \
    2804             :                                         ro = canditer_idx(rci, rb);     \
    2805             :                                         if (!EQ_##TYPE(v, rvals[ro - r->hseqbase])) \
    2806             :                                                 continue;               \
    2807             :                                         if (only_misses) {              \
    2808             :                                                 nr++;                   \
    2809             :                                                 break;                  \
    2810             :                                         }                               \
    2811             :                                         HASHLOOPBODY();                 \
    2812             :                                         if (semi && !max_one)           \
    2813             :                                                 break;                  \
    2814             :                                 }                                       \
    2815             :                         } else if (rci->tpe != cand_dense) {         \
    2816             :                                 for (rb = HASHget(hsh, hash_##TYPE(hsh, &v)); \
    2817             :                                      rb != BUN_NONE;                    \
    2818             :                                      rb = HASHgetlink(hsh, rb)) {       \
    2819             :                                         if (rb >= rl && rb < rh &&        \
    2820             :                                             EQ_##TYPE(v, rvals[rb]) &&  \
    2821             :                                             canditer_contains(rci, ro = (oid) (rb - roff + rseq))) { \
    2822             :                                                 if (only_misses) {      \
    2823             :                                                         nr++;           \
    2824             :                                                         break;          \
    2825             :                                                 }                       \
    2826             :                                                 HASHLOOPBODY();         \
    2827             :                                                 if (semi && !max_one)   \
    2828             :                                                         break;          \
    2829             :                                         }                               \
    2830             :                                 }                                       \
    2831             :                         } else {                                        \
    2832             :                                 for (rb = HASHget(hsh, hash_##TYPE(hsh, &v)); \
    2833             :                                      rb != BUN_NONE;                    \
    2834             :                                      rb = HASHgetlink(hsh, rb)) {       \
    2835             :                                         if (rb >= rl && rb < rh &&        \
    2836             :                                             EQ_##TYPE(v, rvals[rb])) {  \
    2837             :                                                 if (only_misses) {      \
    2838             :                                                         nr++;           \
    2839             :                                                         break;          \
    2840             :                                                 }                       \
    2841             :                                                 ro = (oid) (rb - roff + rseq); \
    2842             :                                                 HASHLOOPBODY();         \
    2843             :                                                 if (semi && !max_one)   \
    2844             :                                                         break;          \
    2845             :                                         }                               \
    2846             :                                 }                                       \
    2847             :                         }                                               \
    2848             :                         if (nr == 0) {                                  \
    2849             :                                 if (only_misses) {                      \
    2850             :                                         nr = 1;                         \
    2851             :                                         if (maybeextend(r1, r2, r3, 1, lci->next, lci->ncand, maxsize) != GDK_SUCCEED) \
    2852             :                                                 goto bailout;           \
    2853             :                                         APPEND(r1, lo);                 \
    2854             :                                         if (lskipped)                   \
    2855             :                                                 r1->tseqbase = oid_nil;      \
    2856             :                                 } else if (nil_on_miss) {               \
    2857             :                                         nr = 1;                         \
    2858             :                                         if (maybeextend(r1, r2, r3, 1, lci->next, lci->ncand, maxsize) != GDK_SUCCEED) \
    2859             :                                                 goto bailout;           \
    2860             :                                         APPEND(r1, lo);                 \
    2861             :                                         if (r2) {                       \
    2862             :                                                 r2->tnil = true;     \
    2863             :                                                 r2->tnonil = false;  \
    2864             :                                                 r2->tkey = false;    \
    2865             :                                                 APPEND(r2, oid_nil);    \
    2866             :                                         }                               \
    2867             :                                         if (r3) {                       \
    2868             :                                                 r3->tnil |= mark == bit_nil; \
    2869             :                                                 ((bit *) r3->theap->base)[r3->batCount++] = mark; \
    2870             :                                         }                               \
    2871             :                                 } else if (min_one) {                   \
    2872             :                                         GDKerror("not enough matches");       \
    2873             :                                         goto bailout;                   \
    2874             :                                 } else {                                \
    2875             :                                         lskipped = BATcount(r1) > 0; \
    2876             :                                 }                                       \
    2877             :                         } else if (only_misses) {                       \
    2878             :                                 lskipped = BATcount(r1) > 0;         \
    2879             :                         } else {                                        \
    2880             :                                 if (lskipped) {                         \
    2881             :                                         /* note, we only get here in an \
    2882             :                                          * iteration *after* lskipped was \
    2883             :                                          * first set to true, i.e. we did \
    2884             :                                          * indeed skip values in l */   \
    2885             :                                         r1->tseqbase = oid_nil;              \
    2886             :                                 }                                       \
    2887             :                                 if (nr > 1) {                                \
    2888             :                                         r1->tkey = false;            \
    2889             :                                         r1->tseqbase = oid_nil;              \
    2890             :                                 }                                       \
    2891             :                         }                                               \
    2892             :                         if (nr > 0 && BATcount(r1) > nr)          \
    2893             :                                 r1->trevsorted = false;                      \
    2894             :                 }                                                       \
    2895             :         } while (0)
    2896             : 
    2897             : /* Implementation of join using a hash lookup of values in the right
    2898             :  * column. */
    2899             : static gdk_return
    2900        9729 : hashjoin(BAT **r1p, BAT **r2p, BAT **r3p, BAT *l, BAT *r,
    2901             :          struct canditer *restrict lci, struct canditer *restrict rci,
    2902             :          bool nil_matches, bool nil_on_miss, bool semi, bool only_misses,
    2903             :          bool not_in, bool max_one, bool min_one,
    2904             :          BUN estimate, lng t0, bool swapped,
    2905             :          bool hash, bool phash, bool hash_cand,
    2906             :          const char *reason)
    2907             : {
    2908        9729 :         oid lo, ro;
    2909        9729 :         BATiter li, ri;
    2910        9729 :         BUN rb, roff = 0;
    2911             :         /* rl, rh: lower and higher bounds for BUN values in hash table */
    2912        9729 :         BUN rl, rh;
    2913        9729 :         oid rseq;
    2914        9729 :         BUN nr;
    2915        9729 :         const char *lvals;
    2916        9729 :         const char *lvars;
    2917        9729 :         const void *nil = ATOMnilptr(l->ttype);
    2918        9729 :         int (*cmp)(const void *, const void *) = ATOMcompare(l->ttype);
    2919        9729 :         oid lval = oid_nil;     /* hold value if l is dense */
    2920        9729 :         const char *v = (const char *) &lval;
    2921        9729 :         bool lskipped = false;  /* whether we skipped values in l */
    2922        9729 :         Hash *restrict hsh = NULL;
    2923        9729 :         bool locked = false;
    2924        9729 :         BUN maxsize;
    2925        9729 :         BAT *r1 = NULL;
    2926        9729 :         BAT *r2 = NULL;
    2927        9729 :         BAT *r3 = NULL;
    2928        9729 :         BAT *b = NULL;
    2929             : 
    2930       29179 :         assert(ATOMtype(l->ttype) == ATOMtype(r->ttype));
    2931             : 
    2932        9729 :         size_t counter = 0;
    2933        9729 :         QryCtx *qry_ctx = MT_thread_get_qry_ctx();
    2934             : 
    2935        9728 :         li = bat_iterator(l);
    2936        9727 :         ri = bat_iterator(r);
    2937             : 
    2938        9728 :         int t = ATOMbasetype(ri.type);
    2939        9728 :         if (BATtvoid(r) || BATtvoid(l))
    2940           7 :                 t = TYPE_void;
    2941             : 
    2942        9728 :         lvals = (const char *) li.base;
    2943        9728 :         if (li.vh && li.type) {
    2944         549 :                 assert(ri.vh && ri.type);
    2945         549 :                 lvars = li.vh->base;
    2946             :         } else {
    2947        9179 :                 assert(ri.vh == NULL);
    2948             :                 lvars = NULL;
    2949             :         }
    2950             :         /* offset to convert BUN to OID for value in right column */
    2951        9728 :         rseq = r->hseqbase;
    2952             : 
    2953        9728 :         rl = rci->seq - r->hseqbase;
    2954        9728 :         rh = canditer_last(rci) + 1 - r->hseqbase;
    2955        9728 :         if (hash_cand) {
    2956             :                 /* we need to create a hash on r specific for the
    2957             :                  * candidate list */
    2958         148 :                 char ext[32];
    2959         148 :                 assert(rci->s);
    2960         170 :                 MT_thread_setalgorithm(swapped ? "hashjoin using candidate hash (swapped)" : "hashjoin using candidate hash");
    2961         148 :                 TRC_DEBUG(ALGO, ALGOBATFMT ": creating "
    2962             :                           "hash for candidate list " ALGOBATFMT "%s%s\n",
    2963             :                           ALGOBATPAR(r), ALGOBATPAR(rci->s),
    2964             :                           r->thash ? " ignoring existing hash" : "",
    2965             :                           swapped ? " (swapped)" : "");
    2966         148 :                 if (snprintf(ext, sizeof(ext), "thshjn%x",
    2967         148 :                              (unsigned) MT_getpid()) >= (int) sizeof(ext))
    2968           0 :                         goto bailout;
    2969         148 :                 if ((hsh = BAThash_impl(r, rci, ext)) == NULL) {
    2970           0 :                         goto bailout;
    2971             :                 }
    2972        9580 :         } else if (phash) {
    2973             :                 /* there is a hash on the parent which we should use */
    2974         918 :                 MT_thread_setalgorithm(swapped ? "hashjoin using parent hash (swapped)" : "hashjoin using parent hash");
    2975         764 :                 b = BATdescriptor(VIEWtparent(r));
    2976         764 :                 if (b == NULL)
    2977           0 :                         goto bailout;
    2978         764 :                 TRC_DEBUG(ALGO, "%s(%s): using "
    2979             :                           "parent(" ALGOBATFMT ") for hash%s\n",
    2980             :                           __func__,
    2981             :                           BATgetId(r), ALGOBATPAR(b),
    2982             :                           swapped ? " (swapped)" : "");
    2983         764 :                 roff = r->tbaseoff - b->tbaseoff;
    2984         764 :                 rl += roff;
    2985         764 :                 rh += roff;
    2986         764 :                 r = b;
    2987         764 :                 bat_iterator_end(&ri);
    2988         764 :                 ri = bat_iterator(r);
    2989         764 :                 MT_rwlock_rdlock(&r->thashlock);
    2990         764 :                 hsh = r->thash;
    2991         764 :                 locked = true;
    2992        8816 :         } else if (hash) {
    2993             :                 /* there is a hash on r which we should use */
    2994        6494 :                 MT_thread_setalgorithm(swapped ? "hashjoin using existing hash (swapped)" : "hashjoin using existing hash");
    2995        4061 :                 MT_rwlock_rdlock(&r->thashlock);
    2996        4061 :                 hsh = r->thash;
    2997        4061 :                 locked = true;
    2998        4061 :                 TRC_DEBUG(ALGO, ALGOBATFMT ": using "
    2999             :                           "existing hash%s\n",
    3000             :                           ALGOBATPAR(r),
    3001             :                           swapped ? " (swapped)" : "");
    3002        4755 :         } else if (BATtdensebi(&ri)) {
    3003             :                 /* no hash, just dense lookup */
    3004           0 :                 MT_thread_setalgorithm(swapped ? "hashjoin on dense (swapped)" : "hashjoin on dense");
    3005             :         } else {
    3006             :                 /* we need to create a hash on r */
    3007        6166 :                 MT_thread_setalgorithm(swapped ? "hashjoin using new hash (swapped)" : "hashjoin using new hash");
    3008        4756 :                 TRC_DEBUG(ALGO, ALGOBATFMT ": creating hash%s\n",
    3009             :                           ALGOBATPAR(r),
    3010             :                           swapped ? " (swapped)" : "");
    3011        4756 :                 if (BAThash(r) != GDK_SUCCEED)
    3012           0 :                         goto bailout;
    3013        4756 :                 MT_rwlock_rdlock(&r->thashlock);
    3014        4756 :                 hsh = r->thash;
    3015        4756 :                 locked = true;
    3016             :         }
    3017        9729 :         if (locked && hsh == NULL) {
    3018           0 :                 GDKerror("Hash disappeared for "ALGOBATFMT"\n", ALGOBATPAR(r));
    3019           0 :                 goto bailout;
    3020             :         }
    3021        9729 :         assert(hsh != NULL || BATtdensebi(&ri));
    3022             :         if (hsh) {
    3023        9729 :                 TRC_DEBUG(ALGO, "hash for " ALGOBATFMT ": nbucket " BUNFMT ", nunique " BUNFMT ", nheads " BUNFMT "\n", ALGOBATPAR(r), hsh->nbucket, hsh->nunique, hsh->nheads);
    3024             :         }
    3025             : 
    3026        9729 :         bit defmark = 0;
    3027        9729 :         if ((not_in || r3p) && !ri.nonil) {
    3028             :                 /* check whether there is a nil on the right, since if
    3029             :                  * so, we should return an empty result if not_in is
    3030             :                  * set, or use a NIL mark for non-matches if r3p is
    3031             :                  * set */
    3032          31 :                 if (hash_cand) {
    3033           0 :                         for (rb = HASHget(hsh, HASHprobe(hsh, nil));
    3034           0 :                              rb != BUN_NONE;
    3035           0 :                              rb = HASHgetlink(hsh, rb)) {
    3036           0 :                                 ro = canditer_idx(rci, rb);
    3037           0 :                                 if ((*cmp)(nil, BUNtail(ri, ro - r->hseqbase)) == 0) {
    3038           0 :                                         assert(!locked);
    3039           0 :                                         if (r3p) {
    3040           0 :                                                 defmark = bit_nil;
    3041           0 :                                                 break;
    3042             :                                         }
    3043           0 :                                         HEAPfree(&hsh->heaplink, true);
    3044           0 :                                         HEAPfree(&hsh->heapbckt, true);
    3045           0 :                                         GDKfree(hsh);
    3046           0 :                                         bat_iterator_end(&li);
    3047           0 :                                         bat_iterator_end(&ri);
    3048           0 :                                         BBPreclaim(b);
    3049           0 :                                         return nomatch(r1p, r2p, r3p, l, r, lci,
    3050             :                                                        bit_nil, false, false,
    3051             :                                                        __func__, t0);
    3052             :                                 }
    3053             :                         }
    3054          31 :                 } else if (!BATtdensebi(&ri)) {
    3055          31 :                         for (rb = HASHget(hsh, HASHprobe(hsh, nil));
    3056          33 :                              rb != BUN_NONE;
    3057           2 :                              rb = HASHgetlink(hsh, rb)) {
    3058          18 :                                 if (rb >= rl && rb < rh &&
    3059          18 :                                     (cmp == NULL ||
    3060          18 :                                      (*cmp)(nil, BUNtail(ri, rb)) == 0)) {
    3061          16 :                                         if (r3p) {
    3062          15 :                                                 defmark = bit_nil;
    3063          15 :                                                 break;
    3064             :                                         }
    3065           1 :                                         if (locked)
    3066           1 :                                                 MT_rwlock_rdunlock(&r->thashlock);
    3067           1 :                                         bat_iterator_end(&li);
    3068           1 :                                         bat_iterator_end(&ri);
    3069           1 :                                         BBPreclaim(b);
    3070           1 :                                         return nomatch(r1p, r2p, r3p, l, r, lci,
    3071             :                                                        bit_nil, false, false,
    3072             :                                                        __func__, t0);
    3073             :                                 }
    3074             :                         }
    3075             :                 }
    3076             :         }
    3077             : 
    3078       19456 :         maxsize = joininitresults(r1p, r2p, r3p, lci->ncand, rci->ncand,
    3079        9728 :                                   li.key, ri.key, semi | max_one,
    3080             :                                   nil_on_miss, only_misses, min_one,
    3081             :                                   estimate);
    3082        9728 :         if (maxsize == BUN_NONE) {
    3083           0 :                 goto bailout;
    3084             :         }
    3085             : 
    3086        9728 :         r1 = *r1p;
    3087        9728 :         r2 = r2p ? *r2p : NULL;
    3088        9728 :         r3 = r3p ? *r3p : NULL;
    3089             : 
    3090             :         /* basic properties will be adjusted if necessary later on,
    3091             :          * they were initially set by joininitresults() */
    3092             : 
    3093        9728 :         if (r2) {
    3094        8127 :                 r2->tkey = li.key;
    3095             :                 /* r2 is not likely to be sorted (although it is
    3096             :                  * certainly possible) */
    3097        8127 :                 r2->tsorted = false;
    3098        8127 :                 r2->trevsorted = false;
    3099        8127 :                 r2->tseqbase = oid_nil;
    3100             :         }
    3101             : 
    3102        9728 :         if (lci->tpe != cand_dense)
    3103         246 :                 r1->tseqbase = oid_nil;
    3104             : 
    3105             : 
    3106        9728 :         switch (t) {
    3107        7903 :         case TYPE_int:
    3108   345242564 :                 HASHJOIN(int);
    3109             :                 break;
    3110        1017 :         case TYPE_lng:
    3111   123981223 :                 HASHJOIN(lng);
    3112             :                 break;
    3113           0 :         case TYPE_uuid:
    3114           0 :                 HASHJOIN(uuid);
    3115           0 :                 break;
    3116             :         default:
    3117     2957231 :                 while (lci->next < lci->ncand) {
    3118     2956429 :                         GDK_CHECK_TIMEOUT(qry_ctx, counter,
    3119             :                                         GOTO_LABEL_TIMEOUT_HANDLER(bailout, qry_ctx));
    3120     2956429 :                         lo = canditer_next(lci);
    3121     2961395 :                         if (BATtdensebi(&li))
    3122         323 :                                 lval = lo - l->hseqbase + l->tseqbase;
    3123     2961072 :                         else if (li.type != TYPE_void)
    3124     2957568 :                                 v = VALUE(l, lo - l->hseqbase);
    3125     2961395 :                         nr = 0;
    3126     2961395 :                         bit mark = defmark;
    3127     2961395 :                         if ((!nil_matches || not_in) && cmp(v, nil) == 0) {
    3128             :                                 /* no match */
    3129        2975 :                                 if (not_in) {
    3130          10 :                                         lskipped = BATcount(r1) > 0;
    3131          10 :                                         continue;
    3132             :                                 }
    3133        2965 :                                 mark = bit_nil;
    3134     2958799 :                         } else if (hash_cand) {
    3135           0 :                                 for (rb = HASHget(hsh, HASHprobe(hsh, v));
    3136           0 :                                      rb != BUN_NONE;
    3137           0 :                                      rb = HASHgetlink(hsh, rb)) {
    3138           0 :                                         ro = canditer_idx(rci, rb);
    3139           0 :                                         if ((*cmp)(v, BUNtail(ri, ro - r->hseqbase)) != 0)
    3140           0 :                                                 continue;
    3141           0 :                                         if (only_misses) {
    3142           0 :                                                 nr++;
    3143           0 :                                                 break;
    3144             :                                         }
    3145           0 :                                         HASHLOOPBODY();
    3146           0 :                                         if (semi && !max_one)
    3147             :                                                 break;
    3148             :                                 }
    3149     2958799 :                         } else if (hsh == NULL) {
    3150           0 :                                 assert(BATtdensebi(&ri));
    3151           0 :                                 ro = *(const oid *) v;
    3152           0 :                                 if (ro >= r->tseqbase &&
    3153           0 :                                     ro < r->tseqbase + r->batCount) {
    3154           0 :                                         ro -= r->tseqbase;
    3155           0 :                                         ro += rseq;
    3156           0 :                                         if (canditer_contains(rci, ro)) {
    3157           0 :                                                 if (only_misses) {
    3158        9722 :                                                         nr++;
    3159             :                                                         break;
    3160             :                                                 }
    3161           0 :                                                 HASHLOOPBODY();
    3162           0 :                                                 if (semi && !max_one)
    3163             :                                                         break;
    3164             :                                         }
    3165             :                                 }
    3166     2958799 :                         } else if (rci->tpe != cand_dense) {
    3167           0 :                                 for (rb = HASHget(hsh, HASHprobe(hsh, v));
    3168           0 :                                      rb != BUN_NONE;
    3169           0 :                                      rb = HASHgetlink(hsh, rb)) {
    3170           0 :                                         if (rb >= rl && rb < rh &&
    3171           0 :                                             (*(cmp))(v, BUNtail(ri, rb)) == 0 &&
    3172           0 :                                             canditer_contains(rci, ro = (oid) (rb - roff + rseq))) {
    3173           0 :                                                 if (only_misses) {
    3174           0 :                                                         nr++;
    3175           0 :                                                         break;
    3176             :                                                 }
    3177           0 :                                                 HASHLOOPBODY();
    3178           0 :                                                 if (semi && !max_one)
    3179             :                                                         break;
    3180             :                                         }
    3181             :                                 }
    3182             :                         } else {
    3183     2958799 :                                 for (rb = HASHget(hsh, HASHprobe(hsh, v));
    3184     5840369 :                                      rb != BUN_NONE;
    3185     2895361 :                                      rb = HASHgetlink(hsh, rb)) {
    3186     5819915 :                                         if (rb >= rl && rb < rh &&
    3187     2902310 :                                             (*(cmp))(v, BUNtail(ri, rb)) == 0) {
    3188     2592481 :                                                 if (only_misses) {
    3189       15528 :                                                         nr++;
    3190       15528 :                                                         break;
    3191             :                                                 }
    3192     2576953 :                                                 ro = (oid) (rb - roff + rseq);
    3193     2576953 :                                                 HASHLOOPBODY();
    3194     2570870 :                                                 if (semi && !max_one)
    3195             :                                                         break;
    3196             :                                         }
    3197             :                                 }
    3198             :                         }
    3199     2956397 :                         if (nr == 0) {
    3200      389282 :                                 if (only_misses) {
    3201         260 :                                         nr = 1;
    3202         260 :                                         if (maybeextend(r1, r2, r3, 1, lci->next, lci->ncand, maxsize) != GDK_SUCCEED)
    3203           0 :                                                 goto bailout;
    3204         260 :                                         APPEND(r1, lo);
    3205         260 :                                         if (lskipped)
    3206         231 :                                                 r1->tseqbase = oid_nil;
    3207      389022 :                                 } else if (nil_on_miss) {
    3208       11770 :                                         nr = 1;
    3209       11770 :                                         if (maybeextend(r1, r2, r3, 1, lci->next, lci->ncand, maxsize) != GDK_SUCCEED)
    3210           0 :                                                 goto bailout;
    3211       11786 :                                         APPEND(r1, lo);
    3212       11786 :                                         if (r2) {
    3213           0 :                                                 r2->tnil = true;
    3214           0 :                                                 r2->tnonil = false;
    3215           0 :                                                 r2->tkey = false;
    3216           0 :                                                 APPEND(r2, oid_nil);
    3217             :                                         }
    3218       11786 :                                         if (r3) {
    3219       11786 :                                                 r3->tnil |= mark == bit_nil;
    3220       11786 :                                                 ((bit *) r3->theap->base)[r3->batCount++] = mark;
    3221             :                                         }
    3222      377252 :                                 } else if (min_one) {
    3223           0 :                                         GDKerror("not enough matches");
    3224           0 :                                         goto bailout;
    3225             :                                 } else {
    3226      377252 :                                         lskipped = BATcount(r1) > 0;
    3227             :                                 }
    3228     2567115 :                         } else if (only_misses) {
    3229       15529 :                                 lskipped = BATcount(r1) > 0;
    3230             :                         } else {
    3231     2551586 :                                 if (lskipped) {
    3232             :                                         /* note, we only get here in an
    3233             :                                          * iteration *after* lskipped was
    3234             :                                          * first set to true, i.e. we did
    3235             :                                          * indeed skip values in l */
    3236     2470633 :                                         r1->tseqbase = oid_nil;
    3237             :                                 }
    3238     2551586 :                                 if (nr > 1) {
    3239        2418 :                                         r1->tkey = false;
    3240        2418 :                                         r1->tseqbase = oid_nil;
    3241             :                                 }
    3242             :                         }
    3243     2956413 :                         if (nr > 0 && BATcount(r1) > nr)
    3244     2562218 :                                 r1->trevsorted = false;
    3245             :                 }
    3246             :                 break;
    3247             :         }
    3248        9722 :         if (locked) {
    3249        9574 :                 locked = false;
    3250        9574 :                 MT_rwlock_rdunlock(&r->thashlock);
    3251             :         }
    3252        9721 :         bat_iterator_end(&li);
    3253        9722 :         bat_iterator_end(&ri);
    3254             : 
    3255        9722 :         if (hash_cand) {
    3256         148 :                 HEAPfree(&hsh->heaplink, true);
    3257         148 :                 HEAPfree(&hsh->heapbckt, true);
    3258         148 :                 GDKfree(hsh);
    3259             :         }
    3260             :         /* also set other bits of heap to correct value to indicate size */
    3261        9722 :         BATsetcount(r1, BATcount(r1));
    3262        9722 :         r1->tunique_est = MIN(l->tunique_est, r->tunique_est);
    3263        9722 :         if (BATcount(r1) <= 1) {
    3264        3539 :                 r1->tsorted = true;
    3265        3539 :                 r1->trevsorted = true;
    3266        3539 :                 r1->tkey = true;
    3267        3539 :                 r1->tseqbase = 0;
    3268             :         }
    3269        9722 :         if (r2) {
    3270        8121 :                 BATsetcount(r2, BATcount(r2));
    3271        8121 :                 assert(BATcount(r1) == BATcount(r2));
    3272        8121 :                 if (BATcount(r2) <= 1) {
    3273        3328 :                         r2->tsorted = true;
    3274        3328 :                         r2->trevsorted = true;
    3275        3328 :                         r2->tkey = true;
    3276        3328 :                         r2->tseqbase = 0;
    3277             :                 }
    3278       11967 :                 r2->tunique_est = MIN(l->tunique_est, r->tunique_est);
    3279             :         }
    3280        9722 :         if (r3) {
    3281          35 :                 r3->tnonil = !r3->tnil;
    3282          35 :                 BATsetcount(r3, BATcount(r3));
    3283          35 :                 assert(BATcount(r1) == BATcount(r3));
    3284          62 :                 r3->tunique_est = MIN(l->tunique_est, r->tunique_est);
    3285             :         }
    3286        9722 :         if (BATcount(r1) > 0) {
    3287        7376 :                 if (BATtdense(r1))
    3288        3981 :                         r1->tseqbase = ((oid *) r1->theap->base)[0];
    3289        7376 :                 if (r2 && BATtdense(r2))
    3290        1163 :                         r2->tseqbase = ((oid *) r2->theap->base)[0];
    3291             :         } else {
    3292        2346 :                 r1->tseqbase = 0;
    3293        2346 :                 if (r2) {
    3294        2165 :                         r2->tseqbase = 0;
    3295             :                 }
    3296             :         }
    3297        9722 :         TRC_DEBUG(ALGO, "l=" ALGOBATFMT "," "r=" ALGOBATFMT
    3298             :                   ",sl=" ALGOOPTBATFMT "," "sr=" ALGOOPTBATFMT ","
    3299             :                   "nil_matches=%s,nil_on_miss=%s,semi=%s,only_misses=%s,"
    3300             :                   "not_in=%s,max_one=%s,min_one=%s;%s %s -> " ALGOBATFMT "," ALGOOPTBATFMT
    3301             :                   " (" LLFMT "usec)\n",
    3302             :                   ALGOBATPAR(l), ALGOBATPAR(r),
    3303             :                   ALGOOPTBATPAR(lci->s), ALGOOPTBATPAR(rci->s),
    3304             :                   nil_matches ? "true" : "false",
    3305             :                   nil_on_miss ? "true" : "false",
    3306             :                   semi ? "true" : "false",
    3307             :                   only_misses ? "true" : "false",
    3308             :                   not_in ? "true" : "false",
    3309             :                   max_one ? "true" : "false",
    3310             :                   min_one ? "true" : "false",
    3311             :                   swapped ? " swapped" : "", reason,
    3312             :                   ALGOBATPAR(r1), ALGOOPTBATPAR(r2),
    3313             :                   GDKusec() - t0);
    3314             : 
    3315        9722 :         BBPreclaim(b);
    3316             :         return GDK_SUCCEED;
    3317             : 
    3318           6 :   bailout:
    3319           6 :         bat_iterator_end(&li);
    3320           6 :         bat_iterator_end(&ri);
    3321           6 :         if (locked)
    3322           6 :                 MT_rwlock_rdunlock(&r->thashlock);
    3323           6 :         if (hash_cand && hsh) {
    3324           0 :                 HEAPfree(&hsh->heaplink, true);
    3325           0 :                 HEAPfree(&hsh->heapbckt, true);
    3326           0 :                 GDKfree(hsh);
    3327             :         }
    3328           6 :         BBPreclaim(r1);
    3329           6 :         BBPreclaim(r2);
    3330           6 :         BBPreclaim(b);
    3331             :         return GDK_FAIL;
    3332             : }
    3333             : 
    3334             : /* Count the number of unique values for the first half and the complete
    3335             :  * set (the sample s of b) and return the two values in *cnt1 and
    3336             :  * *cnt2. In case of error, both values are 0. */
    3337             : static gdk_return
    3338      992700 : count_unique(BAT *b, BAT *s, BUN *cnt1, BUN *cnt2)
    3339             : {
    3340      992700 :         struct canditer ci;
    3341      992700 :         BUN half;
    3342      992700 :         BUN cnt = 0;
    3343      992700 :         const void *v;
    3344      992700 :         const char *bvals;
    3345      992700 :         const char *bvars;
    3346      992700 :         oid bval;
    3347      992700 :         oid i, o;
    3348      992700 :         const char *nme;
    3349      992700 :         BUN hb;
    3350      992700 :         BATiter bi;
    3351      992700 :         int (*cmp)(const void *, const void *);
    3352      992700 :         const char *algomsg = "";
    3353      992700 :         lng t0 = 0;
    3354             : 
    3355      992700 :         TRC_DEBUG_IF(ALGO) t0 = GDKusec();
    3356      992700 :         canditer_init(&ci, b, s);
    3357      992699 :         half = ci.ncand / 2;
    3358             : 
    3359      992699 :         MT_lock_set(&b->theaplock);
    3360      992695 :         if (b->tkey || ci.ncand <= 1 || BATtdense(b)) {
    3361             :                 /* trivial: already unique */
    3362        1194 :                 MT_lock_unset(&b->theaplock);
    3363        1194 :                 *cnt1 = half;
    3364        1194 :                 *cnt2 = ci.ncand;
    3365        1194 :                 return GDK_SUCCEED;
    3366             :         }
    3367      991501 :         MT_lock_unset(&b->theaplock);
    3368             : 
    3369      991506 :         (void) BATordered(b);
    3370      991504 :         (void) BATordered_rev(b);
    3371      991506 :         bi = bat_iterator(b);
    3372      991505 :         if ((bi.sorted && bi.revsorted) ||
    3373      953480 :             (bi.type == TYPE_void && is_oid_nil(bi.tseq))) {
    3374             :                 /* trivial: all values are the same */
    3375       38025 :                 *cnt1 = *cnt2 = 1;
    3376       38025 :                 bat_iterator_end(&bi);
    3377       38025 :                 return GDK_SUCCEED;
    3378             :         }
    3379             : 
    3380      953480 :         assert(bi.type != TYPE_void);
    3381             : 
    3382      953480 :         bvals = bi.base;
    3383      953480 :         if (bi.vh && bi.type)
    3384       68173 :                 bvars = bi.vh->base;
    3385             :         else
    3386             :                 bvars = NULL;
    3387      953480 :         cmp = ATOMcompare(bi.type);
    3388             : 
    3389      953480 :         *cnt1 = *cnt2 = 0;
    3390             : 
    3391      953480 :         if (bi.sorted || bi.revsorted) {
    3392             :                 const void *prev = NULL;
    3393     9030345 :                 algomsg = "sorted";
    3394     9030345 :                 for (i = 0; i < ci.ncand; i++) {
    3395     8965245 :                         if (i == half)
    3396       65100 :                                 *cnt1 = cnt;
    3397     8965245 :                         o = canditer_next(&ci);
    3398     8965238 :                         v = VALUE(b, o - b->hseqbase);
    3399     8965238 :                         if (prev == NULL || (*cmp)(v, prev) != 0) {
    3400     3803838 :                                 cnt++;
    3401             :                         }
    3402     8965243 :                         prev = v;
    3403             :                 }
    3404       65100 :                 *cnt2 = cnt;
    3405      888378 :         } else if (ATOMbasetype(bi.type) == TYPE_bte) {
    3406       45697 :                 unsigned char val;
    3407       45697 :                 uint32_t seen[256 / 32];
    3408             : 
    3409       45697 :                 algomsg = "byte-sized atoms";
    3410       45697 :                 assert(bvars == NULL);
    3411       45697 :                 memset(seen, 0, sizeof(seen));
    3412     6168624 :                 for (i = 0; i < ci.ncand; i++) {
    3413     6122928 :                         if (i == ci.ncand/ 2) {
    3414             :                                 cnt = 0;
    3415      410908 :                                 for (int j = 0; j < 256 / 32; j++)
    3416      365208 :                                         cnt += candmask_pop(seen[j]);
    3417       45700 :                                 *cnt1 = cnt;
    3418             :                         }
    3419     6122928 :                         o = canditer_next(&ci);
    3420     6122927 :                         val = ((const unsigned char *) bvals)[o - b->hseqbase];
    3421     6122927 :                         if (!(seen[val >> 5] & (1U << (val & 0x1F)))) {
    3422      116159 :                                 seen[val >> 5] |= 1U << (val & 0x1F);
    3423             :                         }
    3424             :                 }
    3425             :                 cnt = 0;
    3426      411128 :                 for (int j = 0; j < 256 / 32; j++)
    3427      365432 :                         cnt += candmask_pop(seen[j]);
    3428       45696 :                 *cnt2 = cnt;
    3429      842681 :         } else if (ATOMbasetype(bi.type) == TYPE_sht) {
    3430       43585 :                 unsigned short val;
    3431       43585 :                 uint32_t *seen = NULL;
    3432             : 
    3433       43585 :                 algomsg = "short-sized atoms";
    3434       43585 :                 assert(bvars == NULL);
    3435       43585 :                 seen = GDKzalloc((65536 / 32) * sizeof(seen[0]));
    3436       43585 :                 if (seen == NULL) {
    3437           0 :                         bat_iterator_end(&bi);
    3438           0 :                         return GDK_FAIL;
    3439             :                 }
    3440     6750126 :                 for (i = 0; i < ci.ncand; i++) {
    3441     6706541 :                         if (i == half) {
    3442             :                                 cnt = 0;
    3443    89193025 :                                 for (int j = 0; j < 65536 / 32; j++)
    3444    89149440 :                                         cnt += candmask_pop(seen[j]);
    3445       43585 :                                 *cnt1 = cnt;
    3446             :                         }
    3447     6706541 :                         o = canditer_next(&ci);
    3448     6706541 :                         val = ((const unsigned short *) bvals)[o - b->hseqbase];
    3449     6706541 :                         if (!(seen[val >> 5] & (1U << (val & 0x1F)))) {
    3450      132979 :                                 seen[val >> 5] |= 1U << (val & 0x1F);
    3451             :                         }
    3452             :                 }
    3453             :                 cnt = 0;
    3454    89188929 :                 for (int j = 0; j < 65536 / 32; j++)
    3455    89145344 :                         cnt += candmask_pop(seen[j]);
    3456       43585 :                 *cnt2 = cnt;
    3457       43585 :                 GDKfree(seen);
    3458       43585 :                 seen = NULL;
    3459             :         } else {
    3460      799096 :                 BUN prb;
    3461      799096 :                 BUN mask;
    3462      799096 :                 Hash hs = {
    3463             :                         .heapbckt.parentid = b->batCacheid,
    3464      799096 :                         .heaplink.parentid = b->batCacheid,
    3465             :                 };
    3466             : 
    3467      799096 :                 GDKclrerr();    /* not interested in BAThash errors */
    3468      799096 :                 algomsg = "new partial hash";
    3469      799096 :                 nme = BBP_physical(b->batCacheid);
    3470      799096 :                 mask = HASHmask(ci.ncand);
    3471      587393 :                 if (mask < ((BUN) 1 << 16))
    3472      799096 :                         mask = (BUN) 1 << 16;
    3473      799096 :                 if ((hs.heaplink.farmid = BBPselectfarm(TRANSIENT, bi.type, hashheap)) < 0 ||
    3474      799096 :                     (hs.heapbckt.farmid = BBPselectfarm(TRANSIENT, bi.type, hashheap)) < 0 ||
    3475      799096 :                     snprintf(hs.heaplink.filename, sizeof(hs.heaplink.filename), "%s.thshjnl%x", nme, (unsigned) MT_getpid()) >= (int) sizeof(hs.heaplink.filename) ||
    3476     1598191 :                     snprintf(hs.heapbckt.filename, sizeof(hs.heapbckt.filename), "%s.thshjnb%x", nme, (unsigned) MT_getpid()) >= (int) sizeof(hs.heapbckt.filename) ||
    3477      799096 :                     HASHnew(&hs, bi.type, ci.ncand, mask, BUN_NONE, false) != GDK_SUCCEED) {
    3478           0 :                         GDKerror("cannot allocate hash table\n");
    3479           0 :                         HEAPfree(&hs.heaplink, true);
    3480           0 :                         HEAPfree(&hs.heapbckt, true);
    3481           0 :                         bat_iterator_end(&bi);
    3482           0 :                         return GDK_FAIL;
    3483             :                 }
    3484   401489427 :                 for (i = 0; i < ci.ncand; i++) {
    3485   400690336 :                         if (i == half)
    3486      799093 :                                 *cnt1 = cnt;
    3487   400690336 :                         o = canditer_next(&ci);
    3488   400690483 :                         v = VALUE(b, o - b->hseqbase);
    3489   400690483 :                         prb = HASHprobe(&hs, v);
    3490   400692582 :                         for (hb = HASHget(&hs, prb);
    3491   400701554 :                              hb != BUN_NONE;
    3492        8972 :                              hb = HASHgetlink(&hs, hb)) {
    3493   237856437 :                                 BUN p = canditer_idx(&ci, hb) - b->hseqbase;
    3494   237856437 :                                 if (cmp(v, BUNtail(bi, p)) == 0)
    3495             :                                         break;
    3496             :                         }
    3497   400690773 :                         if (hb == BUN_NONE) {
    3498   162843913 :                                 cnt++;
    3499             :                                 /* enter into hash table */
    3500   162843913 :                                 HASHputlink(&hs, i, HASHget(&hs, prb));
    3501   162844089 :                                 HASHput(&hs, prb, i);
    3502             :                         }
    3503             :                 }
    3504      799091 :                 *cnt2 = cnt;
    3505      799091 :                 HEAPfree(&hs.heaplink, true);
    3506      799094 :                 HEAPfree(&hs.heapbckt, true);
    3507             :         }
    3508      953477 :         bat_iterator_end(&bi);
    3509             : 
    3510      953478 :         TRC_DEBUG(ALGO, "b=" ALGOBATFMT ",s=" ALGOOPTBATFMT
    3511             :                   " -> " BUNFMT " " BUNFMT " (%s -- " LLFMT "usec)\n",
    3512             :                   ALGOBATPAR(b), ALGOOPTBATPAR(s),
    3513             :                   *cnt1, *cnt2, algomsg, GDKusec() - t0);
    3514             : 
    3515             :         return GDK_SUCCEED;
    3516             : }
    3517             : 
    3518             : static double
    3519     1916668 : guess_uniques(BAT *b, struct canditer *ci)
    3520             : {
    3521     1916668 :         BUN cnt1, cnt2;
    3522     1916668 :         BAT *s1;
    3523             : 
    3524     1916668 :         MT_lock_set(&b->theaplock);
    3525     1916659 :         bool key = b->tkey;
    3526     1916659 :         double unique_est = b->tunique_est;
    3527     1916659 :         BUN batcount = BATcount(b);
    3528     1916659 :         MT_lock_unset(&b->theaplock);
    3529     1916667 :         if (key)
    3530      923788 :                 return (double) ci->ncand;
    3531             : 
    3532      992879 :         if (ci->s == NULL ||
    3533           0 :             (ci->tpe == cand_dense && ci->ncand == batcount)) {
    3534      992879 :                 if (unique_est != 0) {
    3535         183 :                         TRC_DEBUG(ALGO, "b=" ALGOBATFMT " use cached value\n",
    3536             :                                   ALGOBATPAR(b));
    3537         183 :                         return unique_est;
    3538             :                 }
    3539      992696 :                 s1 = BATsample(b, 1000);
    3540             :         } else {
    3541           0 :                 BAT *s2 = BATsample(ci->s, 1000);
    3542           0 :                 if (s2 == NULL)
    3543             :                         return -1;
    3544           0 :                 s1 = BATproject(s2, ci->s);
    3545           0 :                 BBPreclaim(s2);
    3546             :         }
    3547      992700 :         if (s1 == NULL)
    3548             :                 return -1;
    3549      992700 :         BUN n2 = BATcount(s1);
    3550      992700 :         BUN n1 = n2 / 2;
    3551      992700 :         if (count_unique(b, s1, &cnt1, &cnt2) != GDK_SUCCEED) {
    3552           0 :                 BBPreclaim(s1);
    3553           0 :                 return -1;
    3554             :         }
    3555      992696 :         BBPreclaim(s1);
    3556             : 
    3557      992700 :         double A = (double) (cnt2 - cnt1) / (n2 - n1);
    3558      992700 :         double B = cnt1 - n1 * A;
    3559             : 
    3560      992700 :         B += A * ci->ncand;
    3561      992700 :         MT_lock_set(&b->theaplock);
    3562      992698 :         if (ci->s == NULL ||
    3563           0 :             (ci->tpe == cand_dense && ci->ncand == BATcount(b) && ci->ncand == batcount)) {
    3564      992698 :                 if (b->tunique_est == 0)
    3565      990859 :                         b->tunique_est = B;
    3566             :         }
    3567      992698 :         MT_lock_unset(&b->theaplock);
    3568      992695 :         return B;
    3569             : }
    3570             : 
    3571             : BUN
    3572     1173639 : BATguess_uniques(BAT *b, struct canditer *ci)
    3573             : {
    3574     1173639 :         struct canditer lci;
    3575     1173639 :         if (ci == NULL) {
    3576     1173639 :                 canditer_init(&lci, b, NULL);
    3577     1173639 :                 ci = &lci;
    3578             :         }
    3579     1173626 :         return (BUN) guess_uniques(b, ci);
    3580             : }
    3581             : 
    3582             : /* estimate the cost of doing a hashjoin with a hash on r; return value
    3583             :  * is the estimated cost, the last three arguments receive some extra
    3584             :  * information */
    3585             : double
    3586     1334593 : joincost(BAT *r, BUN lcount, struct canditer *rci,
    3587             :          bool *hash, bool *phash, bool *cand)
    3588             : {
    3589     1334593 :         bool rhash;
    3590     1334593 :         bool prhash = false;
    3591     1334593 :         bool rcand = false;
    3592     1334593 :         double rcost = 1;
    3593     1334593 :         bat parent;
    3594     1334593 :         BAT *b;
    3595     1334593 :         BUN nheads;
    3596     1334593 :         BUN cnt;
    3597             : 
    3598     1334593 :         (void) BATcheckhash(r);
    3599     1334587 :         MT_rwlock_rdlock(&r->thashlock);
    3600     1334591 :         rhash = r->thash != NULL;
    3601     1334591 :         nheads = r->thash ? r->thash->nheads : 0;
    3602     1334591 :         cnt = BATcount(r);
    3603     1334591 :         MT_rwlock_rdunlock(&r->thashlock);
    3604             : 
    3605     1334594 :         if ((rci->tpe == cand_materialized || rci->tpe == cand_except) &&
    3606      300164 :             rci->nvals > 0) {
    3607             :                 /* if we need to do binary search on candidate list,
    3608             :                  * take that into account; note checking the other
    3609             :                  * candidate types is essentially free */
    3610      300164 :                 rcost += log2((double) rci->nvals);
    3611             :         }
    3612     1334594 :         rcost *= lcount;
    3613     1334594 :         if (BATtdense(r)) {
    3614             :                 /* no need for a hash, and lookup is free */
    3615             :                 rhash = false;  /* don't use it, even if it's there */
    3616             :         } else {
    3617     1334593 :                 if (rhash) {
    3618             :                         /* average chain length */
    3619        5222 :                         rcost *= (double) cnt / nheads;
    3620     1329371 :                 } else if ((parent = VIEWtparent(r)) != 0 &&
    3621     1236600 :                            (b = BATdescriptor(parent)) != NULL) {
    3622     1236598 :                         if (BATcheckhash(b)) {
    3623       47249 :                                 MT_rwlock_rdlock(&b->thashlock);
    3624       47249 :                                 rhash = prhash = b->thash != NULL;
    3625       47249 :                                 if (rhash) {
    3626             :                                         /* average chain length */
    3627       47249 :                                         rcost *= (double) BATcount(b) / b->thash->nheads;
    3628             :                                 }
    3629       47249 :                                 MT_rwlock_rdunlock(&b->thashlock);
    3630             :                         }
    3631     1236587 :                         BBPunfix(b->batCacheid);
    3632             :                 }
    3633     1334558 :                 if (!rhash) {
    3634     1282096 :                         MT_lock_set(&r->theaplock);
    3635     1282076 :                         double unique_est = r->tunique_est;
    3636     1282076 :                         MT_lock_unset(&r->theaplock);
    3637     1282083 :                         if (unique_est == 0) {
    3638      742978 :                                 unique_est = guess_uniques(r, &(struct canditer){.tpe=cand_dense, .ncand=BATcount(r)});
    3639      742979 :                                 if (unique_est < 0)
    3640           0 :                                         return -1;
    3641             :                         }
    3642             :                         /* we have an estimate of the number of unique
    3643             :                          * values, assume some collisions */
    3644     1282084 :                         rcost *= 1.1 * ((double) cnt / unique_est);
    3645             :                         /* only count the cost of creating the hash for
    3646             :                          * non-persistent bats */
    3647     1282084 :                         MT_lock_set(&r->theaplock);
    3648     1282076 :                         if (r->batRole != PERSISTENT /* || r->theap->dirty */ || GDKinmemory(r->theap->farmid))
    3649     1255349 :                                 rcost += cnt * 2.0;
    3650     1282076 :                         MT_lock_unset(&r->theaplock);
    3651             :                 }
    3652             :         }
    3653     1334542 :         if (cand) {
    3654       20997 :                 if (rci->ncand != BATcount(r) && rci->tpe != cand_mask) {
    3655             :                         /* instead of using the hash on r (cost in
    3656             :                          * rcost), we can build a new hash on r taking
    3657             :                          * the candidate list into account; don't do
    3658             :                          * this for masked candidate since the searching
    3659             :                          * of the candidate list (canditer_idx) will
    3660             :                          * kill us */
    3661        1843 :                         double rccost;
    3662        1843 :                         if (rhash && !prhash) {
    3663         598 :                                 rccost = (double) cnt / nheads;
    3664             :                         } else {
    3665        1245 :                                 MT_lock_set(&r->theaplock);
    3666        1245 :                                 double unique_est = r->tunique_est;
    3667        1245 :                                 MT_lock_unset(&r->theaplock);
    3668        1245 :                                 if (unique_est == 0) {
    3669          59 :                                         unique_est = guess_uniques(r, rci);
    3670          59 :                                         if (unique_est < 0)
    3671             :                                                 return -1;
    3672             :                                 }
    3673             :                                 /* we have an estimate of the number of unique
    3674             :                                  * values, assume some chains */
    3675        1245 :                                 rccost = 1.1 * ((double) cnt / unique_est);
    3676             :                         }
    3677        1843 :                         rccost *= lcount;
    3678        1843 :                         rccost += rci->ncand * 2.0; /* cost of building the hash */
    3679        1843 :                         if (rccost < rcost) {
    3680       20997 :                                 rcost = rccost;
    3681       20997 :                                 rcand = true;
    3682             :                         }
    3683             :                 }
    3684       20997 :                 *cand = rcand;
    3685             :         }
    3686     1334542 :         *hash = rhash;
    3687     1334542 :         *phash = prhash;
    3688     1334542 :         return rcost;
    3689             : }
    3690             : 
    3691             : #define MASK_EQ         1
    3692             : #define MASK_LT         2
    3693             : #define MASK_GT         4
    3694             : #define MASK_LE         (MASK_EQ | MASK_LT)
    3695             : #define MASK_GE         (MASK_EQ | MASK_GT)
    3696             : #define MASK_NE         (MASK_LT | MASK_GT)
    3697             : 
    3698             : static gdk_return
    3699       16837 : thetajoin(BAT **r1p, BAT **r2p, BAT *l, BAT *r, BAT *sl, BAT *sr, int opcode,
    3700             :           BUN estimate, bool nil_matches, const char *reason, lng t0)
    3701             : {
    3702       16837 :         struct canditer lci, rci;
    3703       16837 :         const char *lvals, *rvals;
    3704       16837 :         const char *lvars, *rvars;
    3705       16837 :         const void *nil = ATOMnilptr(l->ttype);
    3706       16837 :         int (*cmp)(const void *, const void *) = ATOMcompare(l->ttype);
    3707       16837 :         const void *vl, *vr;
    3708       16837 :         oid lastr = 0;          /* last value inserted into r2 */
    3709       16837 :         BUN nr;
    3710       16837 :         oid lo, ro;
    3711       16837 :         int c;
    3712       16837 :         bool lskipped = false;  /* whether we skipped values in l */
    3713       16837 :         lng loff = 0, roff = 0;
    3714       16837 :         oid lval = oid_nil, rval = oid_nil;
    3715             : 
    3716       16837 :         QryCtx *qry_ctx = MT_thread_get_qry_ctx();
    3717             : 
    3718       50500 :         assert(ATOMtype(l->ttype) == ATOMtype(r->ttype));
    3719       16836 :         assert((opcode & (MASK_EQ | MASK_LT | MASK_GT)) != 0);
    3720             : 
    3721       16836 :         BATiter li = bat_iterator(l);
    3722       16836 :         BATiter ri = bat_iterator(r);
    3723             : 
    3724       16837 :         canditer_init(&lci, l, sl);
    3725       16830 :         canditer_init(&rci, r, sr);
    3726             : 
    3727       16834 :         lvals = BATtvoid(l) ? NULL : (const char *) li.base;
    3728       16834 :         rvals = BATtvoid(r) ? NULL : (const char *) ri.base;
    3729       16834 :         if (li.vh && li.type) {
    3730           8 :                 assert(ri.vh && ri.type);
    3731           8 :                 lvars = li.vh->base;
    3732           8 :                 rvars = ri.vh->base;
    3733             :         } else {
    3734       16826 :                 assert(ri.vh == NULL);
    3735             :                 lvars = rvars = NULL;
    3736             :         }
    3737             : 
    3738       16834 :         if (BATtvoid(l)) {
    3739           0 :                 if (!BATtdensebi(&li)) {
    3740           0 :                         if (!nil_matches) {
    3741             :                                 /* trivial: nils don't match anything */
    3742           0 :                                 bat_iterator_end(&li);
    3743           0 :                                 bat_iterator_end(&ri);
    3744           0 :                                 return nomatch(r1p, r2p, NULL, l, r, &lci,
    3745             :                                                0, false, false, __func__, t0);
    3746             :                         }
    3747             :                 } else {
    3748           0 :                         loff = (lng) l->tseqbase - (lng) l->hseqbase;
    3749             :                 }
    3750             :         }
    3751       16834 :         if (BATtvoid(r)) {
    3752           1 :                 if (!BATtdensebi(&ri)) {
    3753           0 :                         if (!nil_matches) {
    3754             :                                 /* trivial: nils don't match anything */
    3755           0 :                                 bat_iterator_end(&li);
    3756           0 :                                 bat_iterator_end(&ri);
    3757           0 :                                 return nomatch(r1p, r2p, NULL, l, r, &lci,
    3758             :                                                0, false, false, __func__, t0);
    3759             :                         }
    3760             :                 } else {
    3761           1 :                         roff = (lng) r->tseqbase - (lng) r->hseqbase;
    3762             :                 }
    3763             :         }
    3764             : 
    3765       16834 :         BUN maxsize = joininitresults(r1p, r2p, NULL, lci.ncand, rci.ncand, false, false,
    3766             :                                       false, false, false, false, estimate);
    3767       16835 :         if (maxsize == BUN_NONE) {
    3768           0 :                 bat_iterator_end(&li);
    3769           0 :                 bat_iterator_end(&ri);
    3770           0 :                 return GDK_FAIL;
    3771             :         }
    3772       16835 :         BAT *r1 = *r1p;
    3773       16835 :         BAT *r2 = r2p ? *r2p : NULL;
    3774             : 
    3775       16835 :         r1->tkey = true;
    3776       16835 :         r1->tsorted = true;
    3777       16835 :         r1->trevsorted = true;
    3778       16835 :         if (r2) {
    3779        4299 :                 r2->tkey = true;
    3780        4299 :                 r2->tsorted = true;
    3781        4299 :                 r2->trevsorted = true;
    3782             :         }
    3783             : 
    3784             :         /* nested loop implementation for theta join */
    3785             :         vl = &lval;
    3786             :         vr = &rval;
    3787      188096 :         for (BUN lidx = 0; lidx < lci.ncand; lidx++) {
    3788      171288 :                 lo = canditer_next(&lci);
    3789      170508 :                 if (lvals)
    3790      170508 :                         vl = VALUE(l, lo - l->hseqbase);
    3791           0 :                 else if (BATtdensebi(&li))
    3792           0 :                         lval = (oid) ((lng) lo + loff);
    3793      170508 :                 nr = 0;
    3794      170508 :                 if (nil_matches || cmp(vl, nil) != 0) {
    3795      166593 :                         canditer_reset(&rci);
    3796     3422467 :                         TIMEOUT_LOOP(rci.ncand, qry_ctx) {
    3797     3091318 :                                 ro = canditer_next(&rci);
    3798     3056864 :                                 if (rvals)
    3799     3056860 :                                         vr = VALUE(r, ro - r->hseqbase);
    3800           4 :                                 else if (BATtdensebi(&ri))
    3801           4 :                                         rval = (oid) ((lng) ro + roff);
    3802     3056864 :                                 if (!nil_matches && cmp(vr, nil) == 0)
    3803       60192 :                                         continue;
    3804     2995184 :                                 c = cmp(vl, vr);
    3805     3015125 :                                 if (!((opcode & MASK_LT && c < 0) ||
    3806     2806917 :                                       (opcode & MASK_GT && c > 0) ||
    3807     1527261 :                                       (opcode & MASK_EQ && c == 0)))
    3808     1527237 :                                         continue;
    3809     1487888 :                                 if (maybeextend(r1, r2, NULL, 1, lci.next, lci.ncand, maxsize) != GDK_SUCCEED)
    3810           0 :                                         goto bailout;
    3811     1502783 :                                 if (BATcount(r1) > 0) {
    3812     1492834 :                                         if (r2 && lastr + 1 != ro)
    3813       41170 :                                                 r2->tseqbase = oid_nil;
    3814     1492834 :                                         if (nr == 0) {
    3815      100044 :                                                 r1->trevsorted = false;
    3816      100044 :                                                 if (r2 == NULL) {
    3817             :                                                         /* nothing */
    3818       31001 :                                                 } else if (lastr > ro) {
    3819       29177 :                                                         r2->tsorted = false;
    3820       29177 :                                                         r2->tkey = false;
    3821        1824 :                                                 } else if (lastr < ro) {
    3822           0 :                                                         r2->trevsorted = false;
    3823             :                                                 } else {
    3824        1824 :                                                         r2->tkey = false;
    3825             :                                                 }
    3826             :                                         }
    3827             :                                 }
    3828     1502783 :                                 APPEND(r1, lo);
    3829     1502783 :                                 if (r2) {
    3830     1207483 :                                         APPEND(r2, ro);
    3831             :                                 }
    3832     1502783 :                                 lastr = ro;
    3833     1502783 :                                 nr++;
    3834             :                         }
    3835      165740 :                         TIMEOUT_CHECK(qry_ctx,
    3836             :                                       GOTO_LABEL_TIMEOUT_HANDLER(bailout, qry_ctx));
    3837             :                 }
    3838      171261 :                 if (nr > 1) {
    3839       83337 :                         r1->tkey = false;
    3840       83337 :                         r1->tseqbase = oid_nil;
    3841       83337 :                         if (r2) {
    3842       32731 :                                 r2->trevsorted = false;
    3843             :                         }
    3844       87924 :                 } else if (nr == 0) {
    3845       60106 :                         lskipped = BATcount(r1) > 0;
    3846       27818 :                 } else if (lskipped) {
    3847       20929 :                         r1->tseqbase = oid_nil;
    3848             :                 }
    3849             :         }
    3850             :         /* also set other bits of heap to correct value to indicate size */
    3851       16808 :         BATsetcount(r1, BATcount(r1));
    3852       16820 :         if (r2) {
    3853        4295 :                 BATsetcount(r2, BATcount(r2));
    3854        4296 :                 assert(BATcount(r1) == BATcount(r2));
    3855             :         }
    3856       16821 :         if (BATcount(r1) > 0) {
    3857       11248 :                 if (BATtdense(r1))
    3858         171 :                         r1->tseqbase = ((oid *) r1->theap->base)[0];
    3859       11248 :                 if (r2 && BATtdense(r2))
    3860         386 :                         r2->tseqbase = ((oid *) r2->theap->base)[0];
    3861             :         } else {
    3862        5573 :                 r1->tseqbase = 0;
    3863        5573 :                 if (r2) {
    3864         650 :                         r2->tseqbase = 0;
    3865             :                 }
    3866             :         }
    3867       16821 :         bat_iterator_end(&li);
    3868       16832 :         bat_iterator_end(&ri);
    3869       16836 :         TRC_DEBUG(ALGO, "l=" ALGOBATFMT "," "r=" ALGOBATFMT
    3870             :                   ",sl=" ALGOOPTBATFMT "," "sr=" ALGOOPTBATFMT ","
    3871             :                   "opcode=%s%s%s; %s -> " ALGOBATFMT "," ALGOOPTBATFMT
    3872             :                   " (" LLFMT "usec)\n",
    3873             :                   ALGOBATPAR(l), ALGOBATPAR(r),
    3874             :                   ALGOOPTBATPAR(sl), ALGOOPTBATPAR(sr),
    3875             :                   opcode & MASK_LT ? "<" : "",
    3876             :                   opcode & MASK_GT ? ">" : "",
    3877             :                   opcode & MASK_EQ ? "=" : "",
    3878             :                   reason,
    3879             :                   ALGOBATPAR(r1), ALGOOPTBATPAR(r2),
    3880             :                   GDKusec() - t0);
    3881             :         return GDK_SUCCEED;
    3882             : 
    3883           0 :   bailout:
    3884           0 :         bat_iterator_end(&li);
    3885           0 :         bat_iterator_end(&ri);
    3886           0 :         BBPreclaim(r1);
    3887           0 :         BBPreclaim(r2);
    3888             :         return GDK_FAIL;
    3889             : }
    3890             : 
    3891             : /* small ordered right, dense left, oid's only, do fetches */
    3892             : static gdk_return
    3893           0 : fetchjoin(BAT **r1p, BAT **r2p, BAT *l, BAT *r, BAT *sl, BAT *sr,
    3894             :           struct canditer *restrict lci, struct canditer *restrict rci,
    3895             :           const char *reason, lng t0)
    3896             : {
    3897           0 :         oid lo = lci->seq - l->hseqbase + l->tseqbase, hi = lo + lci->ncand;
    3898           0 :         BUN b, e, p;
    3899           0 :         BAT *r1, *r2 = NULL;
    3900             : 
    3901           0 :         MT_thread_setalgorithm(__func__);
    3902           0 :         if (r->tsorted) {
    3903           0 :                 b = SORTfndfirst(r, &lo);
    3904           0 :                 e = SORTfndfirst(r, &hi);
    3905             :         } else {
    3906           0 :                 assert(r->trevsorted);
    3907           0 :                 b = SORTfndlast(r, &hi);
    3908           0 :                 e = SORTfndlast(r, &lo);
    3909             :         }
    3910           0 :         if (b < rci->seq - r->hseqbase)
    3911             :                 b = rci->seq - r->hseqbase;
    3912           0 :         if (e > rci->seq + rci->ncand - r->hseqbase)
    3913             :                 e = rci->seq + rci->ncand - r->hseqbase;
    3914           0 :         if (e == b) {
    3915           0 :                 return nomatch(r1p, r2p, NULL, l, r, lci,
    3916             :                                0, false, false, __func__, t0);
    3917             :         }
    3918           0 :         r1 = COLnew(0, TYPE_oid, e - b, TRANSIENT);
    3919           0 :         if (r1 == NULL)
    3920             :                 return GDK_FAIL;
    3921           0 :         if (r2p) {
    3922           0 :                 if ((r2 = BATdense(0, r->hseqbase + b, e - b)) == NULL) {
    3923           0 :                         BBPreclaim(r1);
    3924           0 :                         return GDK_FAIL;
    3925             :                 }
    3926           0 :                 *r2p = r2;
    3927             :         }
    3928           0 :         *r1p = r1;
    3929           0 :         oid *op = (oid *) Tloc(r1, 0);
    3930           0 :         BATiter ri = bat_iterator(r);
    3931           0 :         const oid *rp = (const oid *) ri.base;
    3932           0 :         for (p = b; p < e; p++) {
    3933           0 :                 *op++ = rp[p] + l->hseqbase - l->tseqbase;
    3934             :         }
    3935           0 :         BATsetcount(r1, e - b);
    3936           0 :         r1->tkey = ri.key;
    3937           0 :         r1->tsorted = ri.sorted || e - b <= 1;
    3938           0 :         r1->trevsorted = ri.revsorted || e - b <= 1;
    3939           0 :         r1->tseqbase = e == b ? 0 : e - b == 1 ? *(const oid *)Tloc(r1, 0) : oid_nil;
    3940           0 :         bat_iterator_end(&ri);
    3941           0 :         TRC_DEBUG(ALGO, "%s(l=" ALGOBATFMT ","
    3942             :                   "r=" ALGOBATFMT ",sl=" ALGOOPTBATFMT ","
    3943             :                   "sr=" ALGOOPTBATFMT ") %s "
    3944             :                   "-> (" ALGOBATFMT "," ALGOOPTBATFMT ") " LLFMT "us\n",
    3945             :                   __func__,
    3946             :                   ALGOBATPAR(l), ALGOBATPAR(r),
    3947             :                   ALGOOPTBATPAR(sl), ALGOOPTBATPAR(sr),
    3948             :                   reason,
    3949             :                   ALGOBATPAR(r1), ALGOOPTBATPAR(r2),
    3950             :                   GDKusec() - t0);
    3951             : 
    3952             :         return GDK_SUCCEED;
    3953             : }
    3954             : 
    3955             : static BAT *
    3956        4624 : bitmaskjoin(BAT *l, BAT *r,
    3957             :             struct canditer *restrict lci, struct canditer *restrict rci,
    3958             :             bool only_misses,
    3959             :             const char *reason, lng t0)
    3960             : {
    3961        4624 :         BAT *r1;
    3962        4624 :         size_t nmsk = (lci->ncand + 31) / 32;
    3963        4624 :         uint32_t *mask = GDKzalloc(nmsk * sizeof(uint32_t));
    3964        4624 :         BUN cnt = 0;
    3965             : 
    3966        4624 :         MT_thread_setalgorithm(__func__);
    3967        4624 :         if (mask == NULL)
    3968             :                 return NULL;
    3969             : 
    3970    22033548 :         for (BUN n = 0; n < rci->ncand; n++) {
    3971    22028924 :                 oid o = canditer_next(rci) - r->hseqbase;
    3972    22028924 :                 o = BUNtoid(r, o);
    3973    22028924 :                 if (is_oid_nil(o))
    3974           0 :                         continue;
    3975    22028924 :                 o += l->hseqbase;
    3976    22028924 :                 if (o < lci->seq + l->tseqbase)
    3977           2 :                         continue;
    3978    22028922 :                 o -= lci->seq + l->tseqbase;
    3979    22028922 :                 if (o >= lci->ncand)
    3980           0 :                         continue;
    3981    22028922 :                 if ((mask[o >> 5] & (1U << (o & 0x1F))) == 0) {
    3982    16134920 :                         cnt++;
    3983    16134920 :                         mask[o >> 5] |= 1U << (o & 0x1F);
    3984             :                 }
    3985             :         }
    3986        4624 :         if (only_misses)
    3987        3389 :                 cnt = lci->ncand - cnt;
    3988        4624 :         if (cnt == 0 || cnt == lci->ncand) {
    3989        1089 :                 GDKfree(mask);
    3990        1089 :                 if (cnt == 0)
    3991         234 :                         return BATdense(0, 0, 0);
    3992         855 :                 return BATdense(0, lci->seq, lci->ncand);
    3993             :         }
    3994        3535 :         r1 = COLnew(0, TYPE_oid, cnt, TRANSIENT);
    3995        3535 :         if (r1 != NULL) {
    3996        3535 :                 oid *r1p = Tloc(r1, 0);
    3997             : 
    3998        3535 :                 r1->tkey = true;
    3999        3535 :                 r1->tnil = false;
    4000        3535 :                 r1->tnonil = true;
    4001        3535 :                 r1->tsorted = true;
    4002        3535 :                 r1->trevsorted = cnt <= 1;
    4003        3535 :                 if (only_misses) {
    4004             :                         /* set the bits for unused values at the
    4005             :                          * end so that we don't need special
    4006             :                          * code in the loop */
    4007        3155 :                         if (lci->ncand & 0x1F)
    4008        3118 :                                 mask[nmsk - 1] |= ~0U << (lci->ncand & 0x1F);
    4009     1986343 :                         for (size_t i = 0; i < nmsk; i++)
    4010     1983188 :                                 if (mask[i] != ~0U)
    4011    62665878 :                                         for (uint32_t j = 0; j < 32; j++)
    4012    60766912 :                                                 if ((mask[i] & (1U << j)) == 0)
    4013    54038634 :                                                         *r1p++ = i * 32 + j + lci->seq;
    4014             :                 } else {
    4015      314622 :                         for (size_t i = 0; i < nmsk; i++)
    4016      314242 :                                 if (mask[i] != 0U)
    4017     7989333 :                                         for (uint32_t j = 0; j < 32; j++)
    4018     7747232 :                                                 if ((mask[i] & (1U << j)) != 0)
    4019     6999264 :                                                         *r1p++ = i * 32 + j + lci->seq;
    4020             :                 }
    4021        3535 :                 BATsetcount(r1, cnt);
    4022        3535 :                 assert((BUN) (r1p - (oid*) Tloc(r1, 0)) == BATcount(r1));
    4023             : 
    4024        3535 :                 TRC_DEBUG(ALGO, "l=" ALGOBATFMT ","
    4025             :                           "r=" ALGOBATFMT ",sl=" ALGOOPTBATFMT ","
    4026             :                           "sr=" ALGOOPTBATFMT ",only_misses=%s; %s "
    4027             :                           "-> " ALGOBATFMT " (" LLFMT "usec)\n",
    4028             :                           ALGOBATPAR(l), ALGOBATPAR(r),
    4029             :                           ALGOOPTBATPAR(lci->s), ALGOOPTBATPAR(rci->s),
    4030             :                           only_misses ? "true" : "false",
    4031             :                           reason,
    4032             :                           ALGOBATPAR(r1),
    4033             :                           GDKusec() - t0);
    4034             :         }
    4035        3535 :         GDKfree(mask);
    4036        3535 :         return r1;
    4037             : }
    4038             : 
    4039             : /* Make the implementation choices for various left joins.
    4040             :  * If r3p is set, this is a "mark join" and *r3p will be a third return value containing a bat with type msk with a bit set for each
    4041             :  * nil_matches: nil is an ordinary value that can match;
    4042             :  * nil_on_miss: outer join: fill in a nil value in case of no match;
    4043             :  * semi: semi join: return one of potentially more than one matches;
    4044             :  * only_misses: difference: list rows without match on the right;
    4045             :  * not_in: for implementing NOT IN: if nil on right then there are no matches;
    4046             :  * max_one: error if there is more than one match;
    4047             :  * min_one: error if there are no matches. */
    4048             : static gdk_return
    4049      103208 : leftjoin(BAT **r1p, BAT **r2p, BAT **r3p, BAT *l, BAT *r, BAT *sl, BAT *sr,
    4050             :          bool nil_matches, bool nil_on_miss, bool semi, bool only_misses,
    4051             :          bool not_in, bool max_one, bool min_one, BUN estimate,
    4052             :          const char *func, lng t0)
    4053             : {
    4054      103208 :         struct canditer lci, rci;
    4055      103208 :         bool rhash, prhash, rcand;
    4056      103208 :         bat parent;
    4057      103208 :         double rcost = 0;
    4058      103208 :         gdk_return rc;
    4059      103208 :         BAT *lp = NULL;
    4060      103208 :         BAT *rp = NULL;
    4061             : 
    4062      103208 :         MT_thread_setalgorithm(__func__);
    4063             :         /* only_misses implies left output only */
    4064      103193 :         assert(!only_misses || r2p == NULL);
    4065             :         /* if nil_on_miss is set, we really need a right output */
    4066      103193 :         assert(!nil_on_miss || r2p != NULL || r3p != NULL);
    4067             :         /* if not_in is set, then so is only_misses */
    4068      103193 :         assert(!not_in || only_misses);
    4069             :         /* if r3p is set, then so is nil_on_miss */
    4070      103193 :         assert(r3p == NULL || nil_on_miss);
    4071      103193 :         *r1p = NULL;
    4072      103193 :         if (r2p)
    4073         999 :                 *r2p = NULL;
    4074      103193 :         if (r3p)
    4075        9643 :                 *r3p = NULL;
    4076             : 
    4077      103193 :         canditer_init(&lci, l, sl);
    4078      103192 :         canditer_init(&rci, r, sr);
    4079             : 
    4080      103195 :         if ((parent = VIEWtparent(l)) != 0) {
    4081        2777 :                 lp = BATdescriptor(parent);
    4082        2777 :                 if (lp == NULL)
    4083             :                         return GDK_FAIL;
    4084        2777 :                 if (l->hseqbase == lp->hseqbase &&
    4085        3830 :                     BATcount(l) == BATcount(lp) &&
    4086        2824 :                     ATOMtype(l->ttype) == ATOMtype(lp->ttype)) {
    4087             :                         l = lp;
    4088             :                 } else {
    4089        1365 :                         BBPunfix(lp->batCacheid);
    4090        1365 :                         lp = NULL;
    4091             :                 }
    4092             :         }
    4093      103195 :         if ((parent = VIEWtparent(r)) != 0) {
    4094        3260 :                 rp = BATdescriptor(parent);
    4095        3260 :                 if (rp == NULL) {
    4096           0 :                         BBPreclaim(lp);
    4097           0 :                         return GDK_FAIL;
    4098             :                 }
    4099        3260 :                 if (r->hseqbase == rp->hseqbase &&
    4100        4906 :                     BATcount(r) == BATcount(rp) &&
    4101        3292 :                     ATOMtype(r->ttype) == ATOMtype(rp->ttype)) {
    4102             :                         r = rp;
    4103             :                 } else {
    4104        1616 :                         BBPunfix(rp->batCacheid);
    4105        1616 :                         rp = NULL;
    4106             :                 }
    4107             :         }
    4108             : 
    4109      103195 :         if (l->ttype == TYPE_msk || mask_cand(l)) {
    4110           5 :                 l = BATunmask(l);
    4111           5 :                 BBPreclaim(lp);
    4112           5 :                 if (l == NULL) {
    4113           0 :                         BBPreclaim(rp);
    4114           0 :                         return GDK_FAIL;
    4115             :                 }
    4116             :                 lp = l;
    4117             :         }
    4118      103195 :         if (r->ttype == TYPE_msk || mask_cand(r)) {
    4119           2 :                 r = BATunmask(r);
    4120           2 :                 BBPreclaim(rp);
    4121           2 :                 if (r == NULL) {
    4122           0 :                         BBPreclaim(lp);
    4123           0 :                         return GDK_FAIL;
    4124             :                 }
    4125             :                 rp = r;
    4126             :         }
    4127             : 
    4128      103195 :         if (joinparamcheck(l, r, NULL, sl, sr, func) != GDK_SUCCEED) {
    4129           0 :                 rc = GDK_FAIL;
    4130           0 :                 goto doreturn;
    4131             :         }
    4132             : 
    4133      103207 :         if (lci.ncand == 0 || rci.ncand == 0) {
    4134       75748 :                 TRC_DEBUG(ALGO, "%s(l=" ALGOBATFMT ","
    4135             :                           "r=" ALGOBATFMT ",sl=" ALGOOPTBATFMT ","
    4136             :                           "sr=" ALGOOPTBATFMT ",nil_matches=%d,"
    4137             :                           "nil_on_miss=%d,semi=%d,only_misses=%d,"
    4138             :                           "not_in=%d,max_one=%d,min_one=%d)\n",
    4139             :                           func,
    4140             :                           ALGOBATPAR(l), ALGOBATPAR(r),
    4141             :                           ALGOOPTBATPAR(sl), ALGOOPTBATPAR(sr),
    4142             :                           nil_matches, nil_on_miss, semi, only_misses,
    4143             :                           not_in, max_one, min_one);
    4144       75748 :                 rc = nomatch(r1p, r2p, r3p, l, r, &lci,
    4145             :                              0, nil_on_miss, only_misses, func, t0);
    4146       75744 :                 goto doreturn;
    4147             :         }
    4148             : 
    4149       27459 :         if (!only_misses && !not_in &&
    4150        3670 :             (lci.ncand == 1 || (BATordered(l) && BATordered_rev(l)) ||
    4151        3612 :              (l->ttype == TYPE_void && is_oid_nil(l->tseqbase)))) {
    4152             :                 /* single value to join, use select */
    4153        1153 :                 rc = selectjoin(r1p, r2p, r3p, l, r, &lci, &rci,
    4154             :                                 nil_matches, nil_on_miss, semi, max_one, min_one,
    4155             :                                 t0, false, func);
    4156        1153 :                 goto doreturn;
    4157       26306 :         } else if (BATtdense(r) && rci.tpe == cand_dense) {
    4158             :                 /* use special implementation for dense right-hand side */
    4159       16661 :                 rc = mergejoin_void(r1p, r2p, r3p, l, r, &lci, &rci,
    4160             :                                     nil_on_miss, only_misses, t0, false,
    4161             :                                     func);
    4162       16661 :                 goto doreturn;
    4163        9645 :         } else if (BATtdense(l)
    4164        4686 :                    && lci.tpe == cand_dense
    4165        4675 :                    && rci.tpe == cand_dense
    4166             :                    && !semi
    4167        4675 :                    && !max_one
    4168             :                    && !min_one
    4169        3400 :                    && !nil_matches
    4170             :                    && !only_misses
    4171        3400 :                    && !not_in
    4172             :                    /* && (rci.ncand * 1024) < lci.ncand */
    4173           0 :                    && (BATordered(r) || BATordered_rev(r))) {
    4174           0 :                 assert(ATOMtype(l->ttype) == TYPE_oid); /* tdense */
    4175           0 :                 rc = fetchjoin(r1p, r2p, l, r, sl, sr, &lci, &rci, func, t0);
    4176           0 :                 goto doreturn;
    4177        9645 :         } else if (BATtdense(l)
    4178        4686 :                    && lci.tpe == cand_dense
    4179        4675 :                    && r2p == NULL
    4180        4636 :                    && (semi || only_misses)
    4181             :                    && !nil_on_miss
    4182        4636 :                    && !not_in
    4183             :                    && !max_one
    4184        4625 :                    && !min_one) {
    4185        4624 :                 *r1p = bitmaskjoin(l, r, &lci, &rci, only_misses, func, t0);
    4186        4624 :                 rc = *r1p == NULL ? GDK_FAIL : GDK_SUCCEED;
    4187        4624 :                 goto doreturn;
    4188             :         } else {
    4189             :                 /* looking at r->tvheap, so we need a lock */
    4190        5021 :                 MT_lock_set(&r->theaplock);
    4191        5021 :                 BUN hsz = r->tvheap ? r->tvheap->size : 0;
    4192        5021 :                 MT_lock_unset(&r->theaplock);
    4193        5021 :                 if ((BATordered(r) || BATordered_rev(r))
    4194        3943 :                     && (BATordered(l)
    4195         277 :                         || BATordered_rev(l)
    4196         270 :                         || BATtdense(r)
    4197         270 :                         || lci.ncand < 1024
    4198         216 :                         || BATcount(r) * (r->twidth + hsz + 2 * sizeof(BUN)) > GDK_mem_maxsize / (GDKnr_threads ? GDKnr_threads : 1))) {
    4199        3835 :                         rc = mergejoin(r1p, r2p, r3p, l, r, &lci, &rci,
    4200             :                                        nil_matches, nil_on_miss, semi, only_misses,
    4201             :                                        not_in, max_one, min_one, estimate, t0, false, func);
    4202        3835 :                         goto doreturn;
    4203             :                 }
    4204             :         }
    4205        1186 :         rcost = joincost(r, lci.ncand, &rci, &rhash, &prhash, &rcand);
    4206        1186 :         if (rcost < 0) {
    4207           0 :                 rc = GDK_FAIL;
    4208           0 :                 goto doreturn;
    4209             :         }
    4210             : 
    4211        1186 :         if (!nil_on_miss && !only_misses && !not_in && !max_one && !min_one) {
    4212             :                 /* maybe do a hash join on the swapped operands; if we
    4213             :                  * do, we need to sort the output, so we take that into
    4214             :                  * account as well */
    4215         948 :                 bool lhash, plhash, lcand, rkey = r->tkey;
    4216         948 :                 double lcost;
    4217             : 
    4218         948 :                 lcost = joincost(l, rci.ncand, &lci, &lhash, &plhash, &lcand);
    4219         948 :                 if (lcost < 0) {
    4220           0 :                         rc = GDK_FAIL;
    4221         825 :                         goto doreturn;
    4222             :                 }
    4223         948 :                 if (semi && !rkey)
    4224         840 :                         lcost += rci.ncand; /* cost of BATunique(r) */
    4225             :                 /* add cost of sorting; obviously we don't know the
    4226             :                  * size, so we guess that the size of the output is
    4227             :                  * the same as the right input */
    4228         948 :                 lcost += rci.ncand * log((double) rci.ncand); /* sort */
    4229         948 :                 if (lcost < rcost) {
    4230         825 :                         BAT *tmp = sr;
    4231         825 :                         BAT *r1, *r2;
    4232         825 :                         if (semi && !rkey) {
    4233         818 :                                 sr = BATunique(r, sr);
    4234         818 :                                 if (sr == NULL) {
    4235           0 :                                         rc = GDK_FAIL;
    4236           0 :                                         goto doreturn;
    4237             :                                 }
    4238         818 :                                 canditer_init(&rci, r, sr);
    4239             :                         }
    4240         825 :                         rc = hashjoin(&r2, &r1, NULL, r, l, &rci, &lci, nil_matches,
    4241             :                                       false, false, false, false, false, false, estimate,
    4242             :                                       t0, true, lhash, plhash, lcand, func);
    4243         825 :                         if (semi && !rkey)
    4244         818 :                                 BBPunfix(sr->batCacheid);
    4245         825 :                         if (rc != GDK_SUCCEED)
    4246           0 :                                 goto doreturn;
    4247         825 :                         if (r2p == NULL) {
    4248         820 :                                 BBPunfix(r2->batCacheid);
    4249         820 :                                 r2 = NULL;
    4250             :                         }
    4251         825 :                         if (semi)
    4252         820 :                                 r1->tkey = true;
    4253         825 :                         if (!VIEWtparent(r1) &&
    4254         825 :                             r1->ttype == TYPE_oid &&
    4255         825 :                             BBP_refs(r1->batCacheid) == 1 &&
    4256         825 :                             (r2 == NULL ||
    4257           5 :                              (!VIEWtparent(r2) &&
    4258           5 :                               BBP_refs(r2->batCacheid) == 1 &&
    4259           5 :                               r2->ttype == TYPE_oid))) {
    4260             :                                 /* in-place sort if we can */
    4261         825 :                                 if (r2) {
    4262           5 :                                         GDKqsort(r1->theap->base, r2->theap->base,
    4263           5 :                                                  NULL, r1->batCount, r1->twidth,
    4264           5 :                                                  r2->twidth, TYPE_oid, false,
    4265             :                                                  false);
    4266           5 :                                         r2->tsorted = false;
    4267           5 :                                         r2->trevsorted = false;
    4268           5 :                                         r2->tseqbase = oid_nil;
    4269           5 :                                         *r2p = r2;
    4270             :                                 } else {
    4271         820 :                                         GDKqsort(r1->theap->base, NULL, NULL,
    4272         820 :                                                  r1->batCount, r1->twidth, 0,
    4273             :                                                  TYPE_oid, false, false);
    4274             :                                 }
    4275         825 :                                 r1->tsorted = true;
    4276         825 :                                 r1->trevsorted = false;
    4277         825 :                                 *r1p = r1;
    4278             :                         } else {
    4279           0 :                                 BAT *ob;
    4280           0 :                                 rc = BATsort(&tmp, r2p ? &ob : NULL, NULL,
    4281             :                                              r1, NULL, NULL, false, false, false);
    4282           0 :                                 BBPunfix(r1->batCacheid);
    4283           0 :                                 if (rc != GDK_SUCCEED) {
    4284           0 :                                         BBPreclaim(r2);
    4285           0 :                                         goto doreturn;
    4286             :                                 }
    4287           0 :                                 *r1p = r1 = tmp;
    4288           0 :                                 if (r2p) {
    4289           0 :                                         tmp = BATproject(ob, r2);
    4290           0 :                                         BBPunfix(r2->batCacheid);
    4291           0 :                                         BBPunfix(ob->batCacheid);
    4292           0 :                                         if (tmp == NULL) {
    4293           0 :                                                 BBPunfix(r1->batCacheid);
    4294           0 :                                                 rc = GDK_FAIL;
    4295           0 :                                                 goto doreturn;
    4296             :                                         }
    4297           0 :                                         *r2p = tmp;
    4298             :                                 }
    4299             :                         }
    4300         825 :                         rc = GDK_SUCCEED;
    4301         825 :                         goto doreturn;
    4302             :                 }
    4303             :         }
    4304         361 :         rc = hashjoin(r1p, r2p, r3p, l, r, &lci, &rci,
    4305             :                       nil_matches, nil_on_miss, semi, only_misses,
    4306             :                       not_in, max_one, min_one, estimate, t0, false, rhash, prhash,
    4307             :                       rcand, func);
    4308      103203 :   doreturn:
    4309      103203 :         BBPreclaim(lp);
    4310      103204 :         BBPreclaim(rp);
    4311      103203 :         if (rc == GDK_SUCCEED && (semi | only_misses))
    4312      102387 :                 *r1p = virtualize(*r1p);
    4313             :         return rc;
    4314             : }
    4315             : 
    4316             : /* Perform an equi-join over l and r.  Returns two new, aligned, bats
    4317             :  * with the oids of matching tuples.  The result is in the same order
    4318             :  * as l (i.e. r1 is sorted). */
    4319             : gdk_return
    4320         645 : BATleftjoin(BAT **r1p, BAT **r2p, BAT *l, BAT *r, BAT *sl, BAT *sr, bool nil_matches, BUN estimate)
    4321             : {
    4322         645 :         return leftjoin(r1p, r2p, NULL, l, r, sl, sr, nil_matches,
    4323             :                         false, false, false, false, false, false,
    4324             :                         estimate, __func__,
    4325         645 :                         GDK_TRACER_TEST(M_DEBUG, ALGO) ? GDKusec() : 0);
    4326             : }
    4327             : 
    4328             : /* Performs a left outer join over l and r.  Returns two new, aligned,
    4329             :  * bats with the oids of matching tuples, or the oid in the first
    4330             :  * output bat and nil in the second output bat if the value in l does
    4331             :  * not occur in r.  The result is in the same order as l (i.e. r1 is
    4332             :  * sorted). */
    4333             : gdk_return
    4334         123 : BATouterjoin(BAT **r1p, BAT **r2p, BAT *l, BAT *r, BAT *sl, BAT *sr, bool nil_matches, bool match_one, BUN estimate)
    4335             : {
    4336         123 :         return leftjoin(r1p, r2p, NULL, l, r, sl, sr, nil_matches,
    4337             :                         true, false, false, false, match_one, match_one,
    4338             :                         estimate, __func__,
    4339         123 :                         GDK_TRACER_TEST(M_DEBUG, ALGO) ? GDKusec() : 0);
    4340             : }
    4341             : 
    4342             : /* Perform a semi-join over l and r.  Returns one or two new bats
    4343             :  * with the oids of matching tuples.  The result is in the same order
    4344             :  * as l (i.e. r1 is sorted).  If a single bat is returned, it is a
    4345             :  * candidate list. */
    4346             : gdk_return
    4347        1031 : BATsemijoin(BAT **r1p, BAT **r2p, BAT *l, BAT *r, BAT *sl, BAT *sr,
    4348             :             bool nil_matches, bool max_one, BUN estimate)
    4349             : {
    4350        1031 :         return leftjoin(r1p, r2p, NULL, l, r, sl, sr, nil_matches,
    4351             :                         false, true, false, false, max_one, false,
    4352             :                         estimate, __func__,
    4353        1031 :                         GDK_TRACER_TEST(M_DEBUG, ALGO) ? GDKusec() : 0);
    4354             : }
    4355             : 
    4356             : /* Perform a mark-join over l and r.  Returns one or two new bats with
    4357             :  * the oids of matching tuples.  In addition, returns a bat with "marks"
    4358             :  * that indicate the type of match.  This is an outer join, so returns
    4359             :  * at least one value for each row on the left.  If the second output
    4360             :  * pointer (r2p) is NULL, this is also a semi-join, so returns exactly
    4361             :  * one row for each row on the left.  If there is a match, the mark
    4362             :  * column will be TRUE, of there is no match, the second output is NIL,
    4363             :  * and the mark output is FALSE if there are no NILs in the right input,
    4364             :  * and the left input is also not NIL, otherwise the mark output is
    4365             :  * NIL. */
    4366             : gdk_return
    4367        9650 : BATmarkjoin(BAT **r1p, BAT **r2p, BAT **r3p, BAT *l, BAT *r, BAT *sl, BAT *sr,
    4368             :             BUN estimate)
    4369             : {
    4370        9650 :         return leftjoin(r1p, r2p, r3p, l, r, sl, sr, false, true, r2p == NULL,
    4371             :                         false, false, false, false, estimate, __func__,
    4372        9650 :                         GDK_TRACER_TEST(M_DEBUG, ALGO) ? GDKusec() : 0);
    4373             : }
    4374             : 
    4375             : /* Return a candidate list with the list of rows in l whose value also
    4376             :  * occurs in r.  This is just the left output of a semi-join. */
    4377             : BAT *
    4378        5700 : BATintersect(BAT *l, BAT *r, BAT *sl, BAT *sr, bool nil_matches, bool max_one,
    4379             :              BUN estimate)
    4380             : {
    4381        5700 :         BAT *bn;
    4382             : 
    4383        5700 :         if (leftjoin(&bn, NULL, NULL, l, r, sl, sr, nil_matches,
    4384             :                      false, true, false, false, max_one, false,
    4385             :                      estimate, __func__,
    4386        5700 :                      GDK_TRACER_TEST(M_DEBUG, ALGO) ? GDKusec() : 0) == GDK_SUCCEED)
    4387        5698 :                 return bn;
    4388             :         return NULL;
    4389             : }
    4390             : 
    4391             : /* Return the difference of l and r.  The result is a BAT with the
    4392             :  * oids of those values in l that do not occur in r.  This is what you
    4393             :  * might call an anti-semi-join.  The result is a candidate list. */
    4394             : BAT *
    4395       86059 : BATdiff(BAT *l, BAT *r, BAT *sl, BAT *sr, bool nil_matches, bool not_in,
    4396             :         BUN estimate)
    4397             : {
    4398       86059 :         BAT *bn;
    4399             : 
    4400       86059 :         if (leftjoin(&bn, NULL, NULL, l, r, sl, sr, nil_matches,
    4401             :                      false, false, true, not_in, false, false,
    4402             :                      estimate, __func__,
    4403       86059 :                      GDK_TRACER_TEST(M_DEBUG, ALGO) ? GDKusec() : 0) == GDK_SUCCEED)
    4404       86055 :                 return bn;
    4405             :         return NULL;
    4406             : }
    4407             : 
    4408             : gdk_return
    4409       16837 : BATthetajoin(BAT **r1p, BAT **r2p, BAT *l, BAT *r, BAT *sl, BAT *sr, int op, bool nil_matches, BUN estimate)
    4410             : {
    4411       16837 :         int opcode = 0;
    4412       16837 :         lng t0 = 0;
    4413             : 
    4414             :         /* encode operator as a bit mask into opcode */
    4415       16837 :         switch (op) {
    4416           0 :         case JOIN_EQ:
    4417           0 :                 return BATjoin(r1p, r2p, l, r, sl, sr, nil_matches, estimate);
    4418             :         case JOIN_NE:
    4419             :                 opcode = MASK_NE;
    4420             :                 break;
    4421        4225 :         case JOIN_LT:
    4422        4225 :                 opcode = MASK_LT;
    4423        4225 :                 break;
    4424           7 :         case JOIN_LE:
    4425           7 :                 opcode = MASK_LE;
    4426           7 :                 break;
    4427       12525 :         case JOIN_GT:
    4428       12525 :                 opcode = MASK_GT;
    4429       12525 :                 break;
    4430          18 :         case JOIN_GE:
    4431          18 :                 opcode = MASK_GE;
    4432          18 :                 break;
    4433           0 :         default:
    4434           0 :                 GDKerror("unknown operator %d.\n", op);
    4435           0 :                 return GDK_FAIL;
    4436             :         }
    4437             : 
    4438       16837 :         TRC_DEBUG_IF(ALGO) t0 = GDKusec();
    4439       16837 :         *r1p = NULL;
    4440       16837 :         if (r2p) {
    4441        4300 :                 *r2p = NULL;
    4442             :         }
    4443       16837 :         if (joinparamcheck(l, r, NULL, sl, sr, __func__) != GDK_SUCCEED)
    4444             :                 return GDK_FAIL;
    4445             : 
    4446       16837 :         return thetajoin(r1p, r2p, l, r, sl, sr, opcode, estimate, nil_matches,
    4447             :                          __func__, t0);
    4448             : }
    4449             : 
    4450             : gdk_return
    4451      196027 : BATjoin(BAT **r1p, BAT **r2p, BAT *l, BAT *r, BAT *sl, BAT *sr, bool nil_matches, BUN estimate)
    4452             : {
    4453      196027 :         struct canditer lci, rci;
    4454      196027 :         bool lhash = false, rhash = false, lcand = false;
    4455      196027 :         bool plhash = false, prhash = false, rcand = false;
    4456      196027 :         bool swap;
    4457      196027 :         bat parent;
    4458      196027 :         double rcost = 0;
    4459      196027 :         double lcost = 0;
    4460      196027 :         gdk_return rc;
    4461      196027 :         lng t0 = 0;
    4462      196027 :         BAT *r2 = NULL;
    4463      196027 :         BAT *lp = NULL;
    4464      196027 :         BAT *rp = NULL;
    4465             : 
    4466      196027 :         TRC_DEBUG_IF(ALGO) t0 = GDKusec();
    4467             : 
    4468      196027 :         canditer_init(&lci, l, sl);
    4469      196022 :         canditer_init(&rci, r, sr);
    4470             : 
    4471      195962 :         if ((parent = VIEWtparent(l)) != 0) {
    4472       47786 :                 lp = BATdescriptor(parent);
    4473       47788 :                 if (lp == NULL)
    4474             :                         return GDK_FAIL;
    4475       47788 :                 if (l->hseqbase == lp->hseqbase &&
    4476       50808 :                     BATcount(l) == BATcount(lp) &&
    4477       16966 :                     ATOMtype(l->ttype) == ATOMtype(lp->ttype)) {
    4478             :                         l = lp;
    4479             :                 } else {
    4480       39305 :                         BBPunfix(lp->batCacheid);
    4481       39305 :                         lp = NULL;
    4482             :                 }
    4483             :         }
    4484      195962 :         if ((parent = VIEWtparent(r)) != 0) {
    4485      156702 :                 rp = BATdescriptor(parent);
    4486      156753 :                 if (rp == NULL) {
    4487           0 :                         BBPreclaim(lp);
    4488           0 :                         return GDK_FAIL;
    4489             :                 }
    4490      156753 :                 if (r->hseqbase == rp->hseqbase &&
    4491      265641 :                     BATcount(r) == BATcount(rp) &&
    4492      237921 :                     ATOMtype(r->ttype) == ATOMtype(rp->ttype)) {
    4493             :                         r = rp;
    4494             :                 } else {
    4495       37796 :                         BBPunfix(rp->batCacheid);
    4496       37796 :                         rp = NULL;
    4497             :                 }
    4498             :         }
    4499             : 
    4500      196008 :         if (l->ttype == TYPE_msk || mask_cand(l)) {
    4501           0 :                 l = BATunmask(l);
    4502           0 :                 BBPreclaim(lp);
    4503           0 :                 if (l == NULL) {
    4504           0 :                         BBPreclaim(rp);
    4505           0 :                         return GDK_FAIL;
    4506             :                 }
    4507             :                 lp = l;
    4508             :         }
    4509      196008 :         if (r->ttype == TYPE_msk || mask_cand(r)) {
    4510          24 :                 r = BATunmask(r);
    4511          24 :                 BBPreclaim(rp);
    4512          24 :                 if (r == NULL) {
    4513           0 :                         BBPreclaim(lp);
    4514           0 :                         return GDK_FAIL;
    4515             :                 }
    4516             :                 rp = r;
    4517             :         }
    4518             : 
    4519      196008 :         *r1p = NULL;
    4520      196008 :         if (r2p)
    4521      175211 :                 *r2p = NULL;
    4522             : 
    4523      196008 :         if (joinparamcheck(l, r, NULL, sl, sr, __func__) != GDK_SUCCEED) {
    4524           0 :                 rc = GDK_FAIL;
    4525           0 :                 goto doreturn;
    4526             :         }
    4527             : 
    4528      196030 :         if (lci.ncand == 0 || rci.ncand == 0) {
    4529      141788 :                 TRC_DEBUG(ALGO, "BATjoin(l=" ALGOBATFMT ","
    4530             :                           "r=" ALGOBATFMT ",sl=" ALGOOPTBATFMT ","
    4531             :                           "sr=" ALGOOPTBATFMT ",nil_matches=%d)\n",
    4532             :                           ALGOBATPAR(l), ALGOBATPAR(r),
    4533             :                           ALGOOPTBATPAR(sl), ALGOOPTBATPAR(sr),
    4534             :                           nil_matches);
    4535      141788 :                 rc = nomatch(r1p, r2p, NULL, l, r, &lci,
    4536             :                              0, false, false, __func__, t0);
    4537      141744 :                 goto doreturn;
    4538             :         }
    4539             : 
    4540       54242 :         swap = false;
    4541             : 
    4542       54242 :         if (lci.ncand == 1 || (BATordered(l) && BATordered_rev(l)) || (l->ttype == TYPE_void && is_oid_nil(l->tseqbase))) {
    4543             :                 /* single value to join, use select */
    4544       32757 :                 rc = selectjoin(r1p, r2p, NULL, l, r, &lci, &rci,
    4545             :                                 nil_matches, false, false, false, false,
    4546             :                                 t0, false, __func__);
    4547       32757 :                 goto doreturn;
    4548       21484 :         } else if (rci.ncand == 1 || (BATordered(r) && BATordered_rev(r)) || (r->ttype == TYPE_void && is_oid_nil(r->tseqbase))) {
    4549             :                 /* single value to join, use select */
    4550        7862 :                 rc = selectjoin(r2p ? r2p : &r2, r1p, NULL, r, l, &rci, &lci,
    4551             :                                 nil_matches, false, false, false, false,
    4552             :                                 t0, true, __func__);
    4553        5228 :                 if (rc == GDK_SUCCEED && r2p == NULL)
    4554        2593 :                         BBPunfix(r2->batCacheid);
    4555        5228 :                 goto doreturn;
    4556       16256 :         } else if (BATtdense(r) && rci.tpe == cand_dense) {
    4557             :                 /* use special implementation for dense right-hand side */
    4558         950 :                 rc = mergejoin_void(r1p, r2p, NULL, l, r, &lci, &rci,
    4559             :                                     false, false, t0, false, __func__);
    4560         950 :                 goto doreturn;
    4561       15306 :         } else if (BATtdense(l) && lci.tpe == cand_dense) {
    4562             :                 /* use special implementation for dense right-hand side */
    4563          58 :                 rc = mergejoin_void(r2p ? r2p : &r2, r1p, NULL, r, l, &rci, &lci,
    4564             :                                     false, false, t0, true, __func__);
    4565          39 :                 if (rc == GDK_SUCCEED && r2p == NULL)
    4566          20 :                         BBPunfix(r2->batCacheid);
    4567          39 :                 goto doreturn;
    4568       22815 :         } else if ((BATordered(l) || BATordered_rev(l)) &&
    4569        9293 :                    (BATordered(r) || BATordered_rev(r))) {
    4570             :                 /* both sorted */
    4571        5835 :                 rc = mergejoin(r1p, r2p, NULL, l, r, &lci, &rci,
    4572             :                                nil_matches, false, false, false, false, false, false,
    4573             :                                estimate, t0, false, __func__);
    4574        5835 :                 goto doreturn;
    4575             :         }
    4576             : 
    4577        9432 :         lcost = joincost(l, rci.ncand, &lci, &lhash, &plhash, &lcand);
    4578        9431 :         rcost = joincost(r, lci.ncand, &rci, &rhash, &prhash, &rcand);
    4579        9431 :         if (lcost < 0 || rcost < 0) {
    4580           0 :                 rc = GDK_FAIL;
    4581           0 :                 goto doreturn;
    4582             :         }
    4583             : 
    4584             :         /* if the cost of doing searches on l is lower than the cost
    4585             :          * of doing searches on r, we swap */
    4586        9431 :         swap = (lcost < rcost);
    4587             : 
    4588       18863 :         if ((r->ttype == TYPE_void && r->tvheap != NULL) ||
    4589       18981 :             ((BATordered(r) || BATordered_rev(r)) &&
    4590        3236 :              (lci.ncand * (log2((double) rci.ncand) + 1) < (swap ? lcost : rcost)))) {
    4591             :                 /* r is sorted and it is cheaper to do multiple binary
    4592             :                  * searches than it is to use a hash */
    4593         411 :                 rc = mergejoin(r1p, r2p, NULL, l, r, &lci, &rci,
    4594             :                                nil_matches, false, false, false, false, false, false,
    4595             :                                estimate, t0, false, __func__);
    4596       18041 :         } else if ((l->ttype == TYPE_void && l->tvheap != NULL) ||
    4597       18198 :             ((BATordered(l) || BATordered_rev(l)) &&
    4598        1713 :              (rci.ncand * (log2((double) lci.ncand) + 1) < (swap ? lcost : rcost)))) {
    4599             :                 /* l is sorted and it is cheaper to do multiple binary
    4600             :                  * searches than it is to use a hash */
    4601         954 :                 rc = mergejoin(r2p ? r2p : &r2, r1p, NULL, r, l, &rci, &lci,
    4602             :                                nil_matches, false, false, false, false, false, false,
    4603             :                                estimate, t0, true, __func__);
    4604         478 :                 if (rc == GDK_SUCCEED && r2p == NULL)
    4605           2 :                         BBPunfix(r2->batCacheid);
    4606        8543 :         } else if (swap) {
    4607        9479 :                 rc = hashjoin(r2p ? r2p : &r2, r1p, NULL, r, l, &rci, &lci,
    4608             :                               nil_matches, false, false, false, false, false, false,
    4609             :                               estimate, t0, true, lhash, plhash, lcand,
    4610             :                               __func__);
    4611        4884 :                 if (rc == GDK_SUCCEED && r2p == NULL)
    4612         289 :                         BBPunfix(r2->batCacheid);
    4613             :         } else {
    4614        3659 :                 rc = hashjoin(r1p, r2p, NULL, l, r, &lci, &rci,
    4615             :                               nil_matches, false, false, false, false, false, false,
    4616             :                               estimate, t0, false, rhash, prhash, rcand,
    4617             :                               __func__);
    4618             :         }
    4619      195985 :   doreturn:
    4620      195985 :         BBPreclaim(lp);
    4621      195984 :         BBPreclaim(rp);
    4622             :         return rc;
    4623             : }
    4624             : 
    4625             : gdk_return
    4626           0 : BATbandjoin(BAT **r1p, BAT **r2p, BAT *l, BAT *r, BAT *sl, BAT *sr,
    4627             :             const void *c1, const void *c2, bool linc, bool hinc, BUN estimate)
    4628             : {
    4629           0 :         lng t0 = 0;
    4630           0 :         struct canditer lci, rci;
    4631           0 :         const char *lvals, *rvals;
    4632           0 :         int t;
    4633           0 :         const void *nil = ATOMnilptr(l->ttype);
    4634           0 :         int (*cmp)(const void *, const void *) = ATOMcompare(l->ttype);
    4635           0 :         const char *vl, *vr;
    4636           0 :         oid lastr = 0;          /* last value inserted into r2 */
    4637           0 :         BUN nr;
    4638           0 :         oid lo, ro;
    4639           0 :         bool lskipped = false;  /* whether we skipped values in l */
    4640             : 
    4641           0 :         TRC_DEBUG_IF(ALGO) t0 = GDKusec();
    4642             : 
    4643           0 :         size_t counter = 0;
    4644           0 :         QryCtx *qry_ctx = MT_thread_get_qry_ctx();
    4645             : 
    4646             : 
    4647           0 :         MT_thread_setalgorithm(__func__);
    4648           0 :         *r1p = NULL;
    4649           0 :         if (r2p) {
    4650           0 :                 *r2p = NULL;
    4651             :         }
    4652           0 :         if (joinparamcheck(l, r, NULL, sl, sr, __func__) != GDK_SUCCEED)
    4653             :                 return GDK_FAIL;
    4654             : 
    4655           0 :         assert(ATOMtype(l->ttype) == ATOMtype(r->ttype));
    4656             : 
    4657           0 :         t = ATOMtype(l->ttype);
    4658           0 :         t = ATOMbasetype(t);
    4659             : 
    4660           0 :         canditer_init(&lci, l, sl);
    4661           0 :         canditer_init(&rci, r, sr);
    4662             : 
    4663           0 :         if (lci.ncand == 0 || rci.ncand == 0)
    4664           0 :                 return nomatch(r1p, r2p, NULL, l, r, &lci,
    4665             :                                0, false, false, __func__, t0);
    4666             : 
    4667           0 :         switch (t) {
    4668           0 :         case TYPE_bte:
    4669           0 :                 if (is_bte_nil(*(const bte *)c1) ||
    4670           0 :                     is_bte_nil(*(const bte *)c2) ||
    4671           0 :                     -*(const bte *)c1 > *(const bte *)c2 ||
    4672           0 :                     ((!hinc || !linc) && -*(const bte *)c1 == *(const bte *)c2))
    4673           0 :                         return nomatch(r1p, r2p, NULL, l, r, &lci,
    4674             :                                        0, false, false, __func__, t0);
    4675             :                 break;
    4676           0 :         case TYPE_sht:
    4677           0 :                 if (is_sht_nil(*(const sht *)c1) ||
    4678           0 :                     is_sht_nil(*(const sht *)c2) ||
    4679           0 :                     -*(const sht *)c1 > *(const sht *)c2 ||
    4680           0 :                     ((!hinc || !linc) && -*(const sht *)c1 == *(const sht *)c2))
    4681           0 :                         return nomatch(r1p, r2p, NULL, l, r, &lci,
    4682             :                                        0, false, false, __func__, t0);
    4683             :                 break;
    4684           0 :         case TYPE_int:
    4685           0 :                 if (is_int_nil(*(const int *)c1) ||
    4686           0 :                     is_int_nil(*(const int *)c2) ||
    4687           0 :                     -*(const int *)c1 > *(const int *)c2 ||
    4688           0 :                     ((!hinc || !linc) && -*(const int *)c1 == *(const int *)c2))
    4689           0 :                         return nomatch(r1p, r2p, NULL, l, r, &lci,
    4690             :                                        0, false, false, __func__, t0);
    4691             :                 break;
    4692           0 :         case TYPE_lng:
    4693           0 :                 if (is_lng_nil(*(const lng *)c1) ||
    4694           0 :                     is_lng_nil(*(const lng *)c2) ||
    4695           0 :                     -*(const lng *)c1 > *(const lng *)c2 ||
    4696           0 :                     ((!hinc || !linc) && -*(const lng *)c1 == *(const lng *)c2))
    4697           0 :                         return nomatch(r1p, r2p, NULL, l, r, &lci,
    4698             :                                        0, false, false, __func__, t0);
    4699             :                 break;
    4700             : #ifdef HAVE_HGE
    4701           0 :         case TYPE_hge:
    4702           0 :                 if (is_hge_nil(*(const hge *)c1) ||
    4703           0 :                     is_hge_nil(*(const hge *)c2) ||
    4704           0 :                     -*(const hge *)c1 > *(const hge *)c2 ||
    4705           0 :                     ((!hinc || !linc) && -*(const hge *)c1 == *(const hge *)c2))
    4706           0 :                         return nomatch(r1p, r2p, NULL, l, r, &lci,
    4707             :                                        0, false, false, __func__, t0);
    4708             :                 break;
    4709             : #endif
    4710           0 :         case TYPE_flt:
    4711           0 :                 if (is_flt_nil(*(const flt *)c1) ||
    4712           0 :                     is_flt_nil(*(const flt *)c2) ||
    4713           0 :                     -*(const flt *)c1 > *(const flt *)c2 ||
    4714           0 :                     ((!hinc || !linc) && -*(const flt *)c1 == *(const flt *)c2))
    4715           0 :                         return nomatch(r1p, r2p, NULL, l, r, &lci,
    4716             :                                        0, false, false, __func__, t0);
    4717             :                 break;
    4718           0 :         case TYPE_dbl:
    4719           0 :                 if (is_dbl_nil(*(const dbl *)c1) ||
    4720           0 :                     is_dbl_nil(*(const dbl *)c2) ||
    4721           0 :                     -*(const dbl *)c1 > *(const dbl *)c2 ||
    4722           0 :                     ((!hinc || !linc) && -*(const dbl *)c1 == *(const dbl *)c2))
    4723           0 :                         return nomatch(r1p, r2p, NULL, l, r, &lci,
    4724             :                                        0, false, false, __func__, t0);
    4725             :                 break;
    4726           0 :         default:
    4727           0 :                 GDKerror("unsupported type\n");
    4728           0 :                 return GDK_FAIL;
    4729             :         }
    4730             : 
    4731           0 :         BUN maxsize = joininitresults(r1p, r2p, NULL, lci.ncand, rci.ncand, false, false,
    4732             :                                       false, false, false, false, estimate);
    4733           0 :         if (maxsize == BUN_NONE)
    4734             :                 return GDK_FAIL;
    4735           0 :         BAT *r1 = *r1p;
    4736           0 :         BAT *r2 = r2p ? *r2p : NULL;
    4737           0 :         BATiter li = bat_iterator(l);
    4738           0 :         BATiter ri = bat_iterator(r);
    4739             : 
    4740           0 :         lvals = (const char *) li.base;
    4741           0 :         rvals = (const char *) ri.base;
    4742           0 :         assert(ri.vh == NULL);
    4743             : 
    4744           0 :         assert(lvals != NULL);
    4745           0 :         assert(rvals != NULL);
    4746             : 
    4747           0 :         r1->tkey = true;
    4748           0 :         r1->tsorted = true;
    4749           0 :         r1->trevsorted = true;
    4750           0 :         if (r2) {
    4751           0 :                 r2->tkey = true;
    4752           0 :                 r2->tsorted = true;
    4753           0 :                 r2->trevsorted = true;
    4754             :         }
    4755             : 
    4756             :         /* nested loop implementation for band join */
    4757           0 :         for (BUN lidx = 0; lidx < lci.ncand; lidx++) {
    4758           0 :                 GDK_CHECK_TIMEOUT(qry_ctx, counter,
    4759             :                                 GOTO_LABEL_TIMEOUT_HANDLER(bailout, qry_ctx));
    4760           0 :                 lo = canditer_next(&lci);
    4761           0 :                 vl = FVALUE(l, lo - l->hseqbase);
    4762           0 :                 if (cmp(vl, nil) == 0)
    4763           0 :                         continue;
    4764           0 :                 nr = 0;
    4765           0 :                 canditer_reset(&rci);
    4766           0 :                 for (BUN ridx = 0; ridx < rci.ncand; ridx++) {
    4767           0 :                         ro = canditer_next(&rci);
    4768           0 :                         vr = FVALUE(r, ro - r->hseqbase);
    4769           0 :                         switch (ATOMtype(li.type)) {
    4770           0 :                         case TYPE_bte: {
    4771           0 :                                 if (is_bte_nil(*(const bte *) vr))
    4772           0 :                                         continue;
    4773           0 :                                 sht v1 = (sht) *(const bte *) vr, v2;
    4774           0 :                                 v2 = v1;
    4775           0 :                                 v1 -= *(const bte *)c1;
    4776           0 :                                 if (*(const bte *)vl <= v1 &&
    4777           0 :                                     (!linc || *(const bte *)vl != v1))
    4778           0 :                                         continue;
    4779           0 :                                 v2 += *(const bte *)c2;
    4780           0 :                                 if (*(const bte *)vl >= v2 &&
    4781           0 :                                     (!hinc || *(const bte *)vl != v2))
    4782           0 :                                         continue;
    4783             :                                 break;
    4784             :                         }
    4785           0 :                         case TYPE_sht: {
    4786           0 :                                 if (is_sht_nil(*(const sht *) vr))
    4787           0 :                                         continue;
    4788           0 :                                 int v1 = (int) *(const sht *) vr, v2;
    4789           0 :                                 v2 = v1;
    4790           0 :                                 v1 -= *(const sht *)c1;
    4791           0 :                                 if (*(const sht *)vl <= v1 &&
    4792           0 :                                     (!linc || *(const sht *)vl != v1))
    4793           0 :                                         continue;
    4794           0 :                                 v2 += *(const sht *)c2;
    4795           0 :                                 if (*(const sht *)vl >= v2 &&
    4796           0 :                                     (!hinc || *(const sht *)vl != v2))
    4797           0 :                                         continue;
    4798             :                                 break;
    4799             :                         }
    4800           0 :                         case TYPE_int: {
    4801           0 :                                 if (is_int_nil(*(const int *) vr))
    4802           0 :                                         continue;
    4803           0 :                                 lng v1 = (lng) *(const int *) vr, v2;
    4804           0 :                                 v2 = v1;
    4805           0 :                                 v1 -= *(const int *)c1;
    4806           0 :                                 if (*(const int *)vl <= v1 &&
    4807           0 :                                     (!linc || *(const int *)vl != v1))
    4808           0 :                                         continue;
    4809           0 :                                 v2 += *(const int *)c2;
    4810           0 :                                 if (*(const int *)vl >= v2 &&
    4811           0 :                                     (!hinc || *(const int *)vl != v2))
    4812           0 :                                         continue;
    4813             :                                 break;
    4814             :                         }
    4815             : #ifdef HAVE_HGE
    4816           0 :                         case TYPE_lng: {
    4817           0 :                                 if (is_lng_nil(*(const lng *) vr))
    4818           0 :                                         continue;
    4819           0 :                                 hge v1 = (hge) *(const lng *) vr, v2;
    4820           0 :                                 v2 = v1;
    4821           0 :                                 v1 -= *(const lng *)c1;
    4822           0 :                                 if (*(const lng *)vl <= v1 &&
    4823           0 :                                     (!linc || *(const lng *)vl != v1))
    4824           0 :                                         continue;
    4825           0 :                                 v2 += *(const lng *)c2;
    4826           0 :                                 if (*(const lng *)vl >= v2 &&
    4827           0 :                                     (!hinc || *(const lng *)vl != v2))
    4828           0 :                                         continue;
    4829             :                                 break;
    4830             :                         }
    4831             : #else
    4832             : #ifdef HAVE___INT128
    4833             :                         case TYPE_lng: {
    4834             :                                 if (is_lng_nil(*(const lng *) vr))
    4835             :                                         continue;
    4836             :                                 __int128 v1 = (__int128) *(const lng *) vr, v2;
    4837             :                                 v2 = v1;
    4838             :                                 v1 -= *(const lng *)c1;
    4839             :                                 if (*(const lng *)vl <= v1 &&
    4840             :                                     (!linc || *(const lng *)vl != v1))
    4841             :                                         continue;
    4842             :                                 v2 += *(const lng *)c2;
    4843             :                                 if (*(const lng *)vl >= v2 &&
    4844             :                                     (!hinc || *(const lng *)vl != v2))
    4845             :                                         continue;
    4846             :                                 break;
    4847             :                         }
    4848             : #else
    4849             : #ifdef HAVE___INT128_T
    4850             :                         case TYPE_lng: {
    4851             :                                 if (is_lng_nil(*(const lng *) vr))
    4852             :                                         continue;
    4853             :                                 __int128_t v1 = (__int128_t) *(const lng *) vr, v2;
    4854             :                                 v2 = v1;
    4855             :                                 v1 -= *(const lng *)c1;
    4856             :                                 if (*(const lng *)vl <= v1 &&
    4857             :                                     (!linc || *(const lng *)vl != v1))
    4858             :                                         continue;
    4859             :                                 v2 += *(const lng *)c2;
    4860             :                                 if (*(const lng *)vl >= v2 &&
    4861             :                                     (!hinc || *(const lng *)vl != v2))
    4862             :                                         continue;
    4863             :                                 break;
    4864             :                         }
    4865             : #else
    4866             :                         case TYPE_lng: {
    4867             :                                 if (is_lng_nil(*(const lng *) vr))
    4868             :                                         continue;
    4869             :                                 lng v1, v2;
    4870             :                                 SUBI_WITH_CHECK(*(const lng *)vr,
    4871             :                                                *(const lng *)c1,
    4872             :                                                lng, v1,
    4873             :                                                GDK_lng_max,
    4874             :                                                do{if(*(const lng*)c1<0)goto nolmatch;else goto lmatch1;}while(false));
    4875             :                                 if (*(const lng *)vl <= v1 &&
    4876             :                                     (!linc || *(const lng *)vl != v1))
    4877             :                                         continue;
    4878             :                                   lmatch1:
    4879             :                                 ADDI_WITH_CHECK(*(const lng *)vr,
    4880             :                                                *(const lng *)c2,
    4881             :                                                lng, v2,
    4882             :                                                GDK_lng_max,
    4883             :                                                do{if(*(const lng*)c2>0)goto nolmatch;else goto lmatch2;}while(false));
    4884             :                                 if (*(const lng *)vl >= v2 &&
    4885             :                                     (!hinc || *(const lng *)vl != v2))
    4886             :                                         continue;
    4887             :                                   lmatch2:
    4888             :                                 break;
    4889             :                                   nolmatch:
    4890             :                                 continue;
    4891             :                         }
    4892             : #endif
    4893             : #endif
    4894             : #endif
    4895             : #ifdef HAVE_HGE
    4896           0 :                         case TYPE_hge: {
    4897           0 :                                 if (is_hge_nil(*(const hge *) vr))
    4898           0 :                                         continue;
    4899           0 :                                 hge v1, v2;
    4900           0 :                                 SUBI_WITH_CHECK(*(const hge *)vr,
    4901             :                                                *(const hge *)c1,
    4902             :                                                hge, v1,
    4903             :                                                GDK_hge_max,
    4904             :                                                do{if(*(const hge*)c1<0)goto nohmatch;else goto hmatch1;}while(false));
    4905           0 :                                 if (*(const hge *)vl <= v1 &&
    4906           0 :                                     (!linc || *(const hge *)vl != v1))
    4907           0 :                                         continue;
    4908           0 :                                   hmatch1:
    4909           0 :                                 ADDI_WITH_CHECK(*(const hge *)vr,
    4910             :                                                *(const hge *)c2,
    4911             :                                                hge, v2,
    4912             :                                                GDK_hge_max,
    4913             :                                                do{if(*(const hge*)c2>0)goto nohmatch;else goto hmatch2;}while(false));
    4914           0 :                                 if (*(const hge *)vl >= v2 &&
    4915           0 :                                     (!hinc || *(const hge *)vl != v2))
    4916           0 :                                         continue;
    4917           0 :                                   hmatch2:
    4918             :                                 break;
    4919           0 :                                   nohmatch:
    4920           0 :                                 continue;
    4921             :                         }
    4922             : #endif
    4923           0 :                         case TYPE_flt: {
    4924           0 :                                 if (is_flt_nil(*(const flt *) vr))
    4925           0 :                                         continue;
    4926           0 :                                 dbl v1 = (dbl) *(const flt *) vr, v2;
    4927           0 :                                 v2 = v1;
    4928           0 :                                 v1 -= *(const flt *)c1;
    4929           0 :                                 if (*(const flt *)vl <= v1 &&
    4930           0 :                                     (!linc || *(const flt *)vl != v1))
    4931           0 :                                         continue;
    4932           0 :                                 v2 += *(const flt *)c2;
    4933           0 :                                 if (*(const flt *)vl >= v2 &&
    4934           0 :                                     (!hinc || *(const flt *)vl != v2))
    4935           0 :                                         continue;
    4936             :                                 break;
    4937             :                         }
    4938           0 :                         case TYPE_dbl: {
    4939           0 :                                 if (is_dbl_nil(*(const dbl *) vr))
    4940           0 :                                         continue;
    4941           0 :                                 dbl v1, v2;
    4942           0 :                                 SUBF_WITH_CHECK(*(const dbl *)vr,
    4943             :                                                *(const dbl *)c1,
    4944             :                                                dbl, v1,
    4945             :                                                GDK_dbl_max,
    4946             :                                                do{if(*(const dbl*)c1<0)goto nodmatch;else goto dmatch1;}while(false));
    4947           0 :                                 if (*(const dbl *)vl <= v1 &&
    4948           0 :                                     (!linc || *(const dbl *)vl != v1))
    4949           0 :                                         continue;
    4950           0 :                                   dmatch1:
    4951           0 :                                 ADDF_WITH_CHECK(*(const dbl *)vr,
    4952             :                                                *(const dbl *)c2,
    4953             :                                                dbl, v2,
    4954             :                                                GDK_dbl_max,
    4955             :                                                do{if(*(const dbl*)c2>0)goto nodmatch;else goto dmatch2;}while(false));
    4956           0 :                                 if (*(const dbl *)vl >= v2 &&
    4957           0 :                                     (!hinc || *(const dbl *)vl != v2))
    4958           0 :                                         continue;
    4959           0 :                                   dmatch2:
    4960             :                                 break;
    4961           0 :                                   nodmatch:
    4962           0 :                                 continue;
    4963             :                         }
    4964             :                         }
    4965           0 :                         if (maybeextend(r1, r2, NULL, 1, lci.next, lci.ncand, maxsize) != GDK_SUCCEED)
    4966           0 :                                 goto bailout;
    4967           0 :                         if (BATcount(r1) > 0) {
    4968           0 :                                 if (r2 && lastr + 1 != ro)
    4969           0 :                                         r2->tseqbase = oid_nil;
    4970           0 :                                 if (nr == 0) {
    4971           0 :                                         r1->trevsorted = false;
    4972           0 :                                         if (r2 == NULL) {
    4973             :                                                 /* nothing */
    4974           0 :                                         } else if (lastr > ro) {
    4975           0 :                                                 r2->tsorted = false;
    4976           0 :                                                 r2->tkey = false;
    4977           0 :                                         } else if (lastr < ro) {
    4978           0 :                                                 r2->trevsorted = false;
    4979             :                                         } else {
    4980           0 :                                                 r2->tkey = false;
    4981             :                                         }
    4982             :                                 }
    4983             :                         }
    4984           0 :                         APPEND(r1, lo);
    4985           0 :                         if (r2) {
    4986           0 :                                 APPEND(r2, ro);
    4987             :                         }
    4988           0 :                         lastr = ro;
    4989           0 :                         nr++;
    4990             :                 }
    4991           0 :                 if (nr > 1) {
    4992           0 :                         r1->tkey = false;
    4993           0 :                         r1->tseqbase = oid_nil;
    4994           0 :                         if (r2) {
    4995           0 :                                 r2->trevsorted = false;
    4996             :                         }
    4997           0 :                 } else if (nr == 0) {
    4998           0 :                         lskipped = BATcount(r1) > 0;
    4999           0 :                 } else if (lskipped) {
    5000           0 :                         r1->tseqbase = oid_nil;
    5001             :                 }
    5002             :         }
    5003             :         /* also set other bits of heap to correct value to indicate size */
    5004           0 :         BATsetcount(r1, BATcount(r1));
    5005           0 :         if (r2) {
    5006           0 :                 BATsetcount(r2, BATcount(r2));
    5007           0 :                 assert(BATcount(r1) == BATcount(r2));
    5008             :         }
    5009           0 :         if (BATcount(r1) > 0) {
    5010           0 :                 if (BATtdense(r1))
    5011           0 :                         r1->tseqbase = ((oid *) r1->theap->base)[0];
    5012           0 :                 if (r2 && BATtdense(r2))
    5013           0 :                         r2->tseqbase = ((oid *) r2->theap->base)[0];
    5014             :         } else {
    5015           0 :                 r1->tseqbase = 0;
    5016           0 :                 if (r2) {
    5017           0 :                         r2->tseqbase = 0;
    5018             :                 }
    5019             :         }
    5020           0 :         bat_iterator_end(&li);
    5021           0 :         bat_iterator_end(&ri);
    5022           0 :         TRC_DEBUG(ALGO, "l=" ALGOBATFMT "," "r=" ALGOBATFMT
    5023             :                   ",sl=" ALGOOPTBATFMT "," "sr=" ALGOOPTBATFMT ","
    5024             :                   " -> " ALGOBATFMT "," ALGOOPTBATFMT
    5025             :                   " (" LLFMT "usec)\n",
    5026             :                   ALGOBATPAR(l), ALGOBATPAR(r),
    5027             :                   ALGOOPTBATPAR(sl), ALGOOPTBATPAR(sr),
    5028             :                   ALGOBATPAR(r1), ALGOOPTBATPAR(r2),
    5029             :                   GDKusec() - t0);
    5030             :         return GDK_SUCCEED;
    5031             : 
    5032           0 :   bailout:
    5033           0 :         bat_iterator_end(&li);
    5034           0 :         bat_iterator_end(&ri);
    5035           0 :         BBPreclaim(r1);
    5036           0 :         BBPreclaim(r2);
    5037             :         return GDK_FAIL;
    5038             : }
    5039             : 
    5040             : #define LTany(a,b)      ((*cmp)(a, b) < 0)
    5041             : #define EQany(a,b)      ((*cmp)(a, b) == 0)
    5042             : #define is_any_nil(v)   ((v) == NULL || (*cmp)((v), nil) == 0)
    5043             : 
    5044             : #define less3(a,b,i,t)  (is_##t##_nil(a) || is_##t##_nil(b) ? bit_nil : LT##t(a, b) || (i && EQ##t(a, b)))
    5045             : #define grtr3(a,b,i,t)  (is_##t##_nil(a) || is_##t##_nil(b) ? bit_nil : LT##t(b, a) || (i && EQ##t(a, b)))
    5046             : #define or3(a,b)        ((a) == 1 || (b) == 1 ? 1 : is_bit_nil(a) || is_bit_nil(b) ? bit_nil : 0)
    5047             : #define and3(a,b)       ((a) == 0 || (b) == 0 ? 0 : is_bit_nil(a) || is_bit_nil(b) ? bit_nil : 1)
    5048             : #define not3(a)         (is_bit_nil(a) ? bit_nil : !(a))
    5049             : 
    5050             : #define between3(v, lo, linc, hi, hinc, TYPE)                           \
    5051             :         and3(grtr3(v, lo, linc, TYPE), less3(v, hi, hinc, TYPE))
    5052             : 
    5053             : #define BETWEEN(v, lo, linc, hi, hinc, TYPE)                            \
    5054             :         (is_##TYPE##_nil(v)                                             \
    5055             :          ? bit_nil                                                      \
    5056             :          : (bit) (anti                                                  \
    5057             :                   ? (symmetric                                          \
    5058             :                      ? not3(or3(between3(v, lo, linc, hi, hinc, TYPE),  \
    5059             :                                 between3(v, hi, hinc, lo, linc, TYPE))) \
    5060             :                      : not3(between3(v, lo, linc, hi, hinc, TYPE)))     \
    5061             :                   : (symmetric                                          \
    5062             :                      ? or3(between3(v, lo, linc, hi, hinc, TYPE),       \
    5063             :                            between3(v, hi, hinc, lo, linc, TYPE))       \
    5064             :                      : between3(v, lo, linc, hi, hinc, TYPE))))
    5065             : 
    5066             : static gdk_return
    5067         110 : rangejoin(BAT *r1, BAT *r2, BAT *l, BAT *rl, BAT *rh,
    5068             :           struct canditer *lci, struct canditer *rci,
    5069             :           bool linc, bool hinc, bool anti, bool symmetric, BUN maxsize)
    5070             : {
    5071         110 :         if (!anti && !symmetric) {
    5072             :                 /* we'll need these */
    5073          99 :                 (void) BATordered(l);
    5074          99 :                 (void) BATordered_rev(l);
    5075             :         }
    5076         110 :         BATiter li = bat_iterator(l);
    5077         110 :         BATiter rli = bat_iterator(rl);
    5078         110 :         BATiter rhi = bat_iterator(rh);
    5079         110 :         const char *rlvals, *rhvals;
    5080         110 :         const char *lvars, *rlvars, *rhvars;
    5081         110 :         const void *nil = ATOMnilptr(li.type);
    5082         110 :         int (*cmp)(const void *, const void *) = ATOMcompare(li.type);
    5083         110 :         int t;
    5084         110 :         BUN cnt, ncnt, lncand = lci->ncand, rncand = rci->ncand;
    5085         110 :         oid *restrict dst1, *restrict dst2;
    5086         110 :         const void *vrl, *vrh;
    5087         110 :         oid ro;
    5088         110 :         oid rlval = oid_nil, rhval = oid_nil;
    5089         110 :         int sorted = 0;         /* which output column is sorted */
    5090         110 :         Heap *oidxh = NULL;
    5091             : 
    5092         110 :         QryCtx *qry_ctx = MT_thread_get_qry_ctx();
    5093             : 
    5094         330 :         assert(ATOMtype(li.type) == ATOMtype(rli.type));
    5095         220 :         assert(ATOMtype(li.type) == ATOMtype(rhi.type));
    5096         110 :         assert(BATcount(rl) == BATcount(rh));
    5097         110 :         assert(rl->hseqbase == rh->hseqbase);
    5098         110 :         assert(r1->ttype == TYPE_oid);
    5099         110 :         assert(r2 == NULL || r2->ttype == TYPE_oid);
    5100          93 :         assert(r2 == NULL || BATcount(r1) == BATcount(r2));
    5101         110 :         assert(li.type != TYPE_void || !is_oid_nil(l->tseqbase));
    5102         110 :         assert(rli.type != TYPE_void || !is_oid_nil(rl->tseqbase));
    5103         110 :         assert(rhi.type != TYPE_void || !is_oid_nil(rh->tseqbase));
    5104             : 
    5105         110 :         TRC_DEBUG(ALGO, "l=" ALGOBATFMT ","
    5106             :                   "rl=" ALGOBATFMT ",rh=" ALGOBATFMT ","
    5107             :                   "sl=" ALGOOPTBATFMT ",sr=" ALGOOPTBATFMT ","
    5108             :                   "anti=%s,symmetric=%s\n",
    5109             :                   ALGOBATPAR(l),
    5110             :                   ALGOBATPAR(rl),
    5111             :                   ALGOBATPAR(rh),
    5112             :                   ALGOOPTBATPAR(lci->s),
    5113             :                   ALGOOPTBATPAR(rci->s),
    5114             :                   anti ? "true" : "false",
    5115             :                   symmetric ? "true" : "false");
    5116             : 
    5117         110 :         rlvals = rli.type == TYPE_void ? NULL : (const char *) rli.base;
    5118         110 :         rhvals = rhi.type == TYPE_void ? NULL : (const char *) rhi.base;
    5119         110 :         dst1 = (oid *) Tloc(r1, 0);
    5120         110 :         dst2 = r2 ? (oid *) Tloc(r2, 0) : NULL;
    5121             : 
    5122         110 :         t = ATOMtype(li.type);
    5123         110 :         t = ATOMbasetype(t);
    5124             : 
    5125         110 :         if (li.vh && li.type) {
    5126          15 :                 assert(rli.vh && rli.type);
    5127          15 :                 assert(rhi.vh && rhi.type);
    5128          15 :                 lvars = li.vh->base;
    5129          15 :                 rlvars = rli.vh->base;
    5130          15 :                 rhvars = rhi.vh->base;
    5131             :         } else {
    5132          95 :                 assert(rli.vh == NULL);
    5133          95 :                 assert(rhi.vh == NULL);
    5134             :                 lvars = rlvars = rhvars = NULL;
    5135             :         }
    5136             : 
    5137         110 :         if (!anti && !symmetric && !li.sorted && !li.revsorted) {
    5138          10 :                 (void) BATcheckorderidx(l);
    5139          10 :                 MT_lock_set(&l->batIdxLock);
    5140          10 :                 if ((oidxh = l->torderidx) != NULL)
    5141           0 :                         HEAPincref(oidxh);
    5142          10 :                 MT_lock_unset(&l->batIdxLock);
    5143             : #if 0 /* needs checking */
    5144             :                 if (oidxh == NULL && VIEWtparent(l)) {
    5145             : /* if enabled, need to fix/unfix parent bat */
    5146             :                         BAT *pb = BBP_desc(VIEWtparent(l));
    5147             :                         (void) BATcheckorderidx(pb);
    5148             :                         MT_lock_set(&pb->batIdxLock);
    5149             :                         if ((oidxh = pb->torderidx) != NULL) {
    5150             :                                 HEAPincref(oidxh);
    5151             :                                 l = pb;
    5152             :                         }
    5153             :                         MT_lock_unset(&pb->batIdxLock);
    5154             :                 }
    5155             : #endif
    5156             :         }
    5157             : 
    5158         110 :         vrl = &rlval;
    5159         110 :         vrh = &rhval;
    5160         110 :         if (!anti && !symmetric && (li.sorted || li.revsorted || oidxh)) {
    5161             :                 /* left column is sorted, use binary search */
    5162          89 :                 sorted = 2;
    5163         412 :                 TIMEOUT_LOOP(rncand, qry_ctx) {
    5164         234 :                         BUN low, high;
    5165             : 
    5166         234 :                         ro = canditer_next(rci);
    5167         235 :                         if (rlvals) {
    5168         235 :                                 vrl = VALUE(rl, ro - rl->hseqbase);
    5169             :                         } else {
    5170             :                                 /* TYPE_void */
    5171           0 :                                 rlval = ro - rl->hseqbase + rl->tseqbase;
    5172             :                         }
    5173         235 :                         if (rhvals) {
    5174         235 :                                 vrh = VALUE(rh, ro - rh->hseqbase);
    5175             :                         } else {
    5176             :                                 /* TYPE_void */
    5177           0 :                                 rhval = ro - rh->hseqbase + rh->tseqbase;
    5178             :                         }
    5179         235 :                         if (cmp(vrl, nil) == 0 || cmp(vrh, nil) == 0)
    5180           9 :                                 continue;
    5181         226 :                         if (li.sorted) {
    5182         207 :                                 if (linc)
    5183         144 :                                         low = SORTfndfirst(l, vrl);
    5184             :                                 else
    5185          63 :                                         low = SORTfndlast(l, vrl);
    5186         207 :                                 if (hinc)
    5187         148 :                                         high = SORTfndlast(l, vrh);
    5188             :                                 else
    5189          59 :                                         high = SORTfndfirst(l, vrh);
    5190          19 :                         } else  if (li.revsorted) {
    5191          19 :                                 if (hinc)
    5192          19 :                                         low = SORTfndfirst(l, vrh);
    5193             :                                 else
    5194           0 :                                         low = SORTfndlast(l, vrh);
    5195          19 :                                 if (linc)
    5196           3 :                                         high = SORTfndlast(l, vrl);
    5197             :                                 else
    5198          16 :                                         high = SORTfndfirst(l, vrl);
    5199             :                         } else {
    5200           0 :                                 assert(oidxh);
    5201           0 :                                 if (linc)
    5202           0 :                                         low = ORDERfndfirst(l, oidxh, vrl);
    5203             :                                 else
    5204           0 :                                         low = ORDERfndlast(l, oidxh, vrl);
    5205           0 :                                 if (hinc)
    5206           0 :                                         high = ORDERfndlast(l, oidxh, vrh);
    5207             :                                 else
    5208           0 :                                         high = ORDERfndfirst(l, oidxh, vrh);
    5209             :                         }
    5210         225 :                         if (high <= low)
    5211         122 :                                 continue;
    5212         103 :                         if (li.sorted || li.revsorted) {
    5213         103 :                                 low = canditer_search(lci, low + l->hseqbase, true);
    5214         103 :                                 high = canditer_search(lci, high + l->hseqbase, true);
    5215         103 :                                 assert(high >= low);
    5216             : 
    5217         103 :                                 if (BATcapacity(r1) < BATcount(r1) + high - low) {
    5218           0 :                                         cnt = BATcount(r1) + high - low + 1024;
    5219           0 :                                         if (cnt > maxsize)
    5220             :                                                 cnt = maxsize;
    5221           0 :                                         BATsetcount(r1, BATcount(r1));
    5222           0 :                                         if (BATextend(r1, cnt) != GDK_SUCCEED)
    5223           0 :                                                 goto bailout;
    5224           0 :                                         dst1 = (oid *) Tloc(r1, 0);
    5225           0 :                                         if (r2) {
    5226           0 :                                                 BATsetcount(r2, BATcount(r2));
    5227           0 :                                                 if (BATextend(r2, cnt) != GDK_SUCCEED)
    5228           0 :                                                         goto bailout;
    5229           0 :                                                 assert(BATcapacity(r1) == BATcapacity(r2));
    5230           0 :                                                 dst2 = (oid *) Tloc(r2, 0);
    5231             :                                         }
    5232             :                                 }
    5233         103 :                                 canditer_setidx(lci, low);
    5234         580 :                                 while (low < high) {
    5235         477 :                                         dst1[r1->batCount++] = canditer_next(lci);
    5236         477 :                                         if (r2) {
    5237         465 :                                                 dst2[r2->batCount++] = ro;
    5238             :                                         }
    5239         477 :                                         low++;
    5240             :                                 }
    5241             :                         } else {
    5242           0 :                                 const oid *ord;
    5243             : 
    5244           0 :                                 assert(oidxh);
    5245           0 :                                 ord = (const oid *) oidxh->base + ORDERIDXOFF;
    5246             : 
    5247           0 :                                 if (BATcapacity(r1) < BATcount(r1) + high - low) {
    5248           0 :                                         cnt = BATcount(r1) + high - low + 1024;
    5249           0 :                                         if (cnt > maxsize)
    5250             :                                                 cnt = maxsize;
    5251           0 :                                         BATsetcount(r1, BATcount(r1));
    5252           0 :                                         if (BATextend(r1, cnt) != GDK_SUCCEED)
    5253           0 :                                                 goto bailout;
    5254           0 :                                         dst1 = (oid *) Tloc(r1, 0);
    5255           0 :                                         if (r2) {
    5256           0 :                                                 BATsetcount(r2, BATcount(r2));
    5257           0 :                                                 if (BATextend(r2, cnt) != GDK_SUCCEED)
    5258           0 :                                                         goto bailout;
    5259           0 :                                                 assert(BATcapacity(r1) == BATcapacity(r2));
    5260           0 :                                                 dst2 = (oid *) Tloc(r2, 0);
    5261             :                                         }
    5262             :                                 }
    5263             : 
    5264           0 :                                 while (low < high) {
    5265           0 :                                         if (canditer_contains(lci, ord[low])) {
    5266           0 :                                                 dst1[r1->batCount++] = ord[low];
    5267           0 :                                                 if (r2) {
    5268           0 :                                                         dst2[r2->batCount++] = ro;
    5269             :                                                 }
    5270             :                                         }
    5271           0 :                                         low++;
    5272             :                                 }
    5273             :                         }
    5274             :                 }
    5275          89 :                 if (oidxh)
    5276           0 :                         HEAPdecref(oidxh, false);
    5277          89 :                 TIMEOUT_CHECK(qry_ctx, GOTO_LABEL_TIMEOUT_HANDLER(bailout, qry_ctx));
    5278          89 :                 cnt = BATcount(r1);
    5279          89 :                 assert(r2 == NULL || BATcount(r1) == BATcount(r2));
    5280             :         } else {
    5281             :                 /* nested loop implementation */
    5282          21 :                 const void *vl;
    5283          21 :                 const char *lvals;
    5284          21 :                 oid lval;
    5285             : 
    5286          21 :                 sorted = 1;
    5287          21 :                 lvals = li.type == TYPE_void ? NULL : (const char *) li.base;
    5288          21 :                 vl = &lval;
    5289         287 :                 TIMEOUT_LOOP(lncand, qry_ctx) {
    5290         245 :                         oid lo;
    5291             : 
    5292         245 :                         lo = canditer_next(lci);
    5293         245 :                         if (lvals) {
    5294         245 :                                 vl = VALUE(l, lo - l->hseqbase);
    5295         245 :                                 if (cmp(vl, nil) == 0)
    5296           8 :                                         continue;
    5297             :                         } else {
    5298           0 :                                 lval = lo - l->hseqbase + l->tseqbase;
    5299             :                         }
    5300         237 :                         canditer_reset(rci);
    5301       36960 :                         for (BUN j = 0; j < rncand; j++) {
    5302       36723 :                                 ro = canditer_next(rci);
    5303       32784 :                                 if (rlvals) {
    5304       32784 :                                         vrl = VALUE(rl, ro - rl->hseqbase);
    5305             :                                 } else {
    5306             :                                         /* TYPE_void */
    5307           0 :                                         rlval = ro - rl->hseqbase + rl->tseqbase;
    5308             :                                 }
    5309       32784 :                                 if (rhvals) {
    5310       32784 :                                         vrh = VALUE(rh, ro - rh->hseqbase);
    5311             :                                 } else {
    5312             :                                         /* TYPE_void */
    5313           0 :                                         rhval = ro - rh->hseqbase + rh->tseqbase;
    5314             :                                 }
    5315       32787 :                                 if (BETWEEN(vl, vrl, linc, vrh, hinc, any) != 1)
    5316       23878 :                                         continue;
    5317       12845 :                                 if (BATcount(r1) == BATcapacity(r1)) {
    5318           2 :                                         BUN newcap = BATgrows(r1);
    5319           2 :                                         if (newcap > maxsize)
    5320             :                                                 newcap = maxsize;
    5321           2 :                                         BATsetcount(r1, BATcount(r1));
    5322           2 :                                         if (BATextend(r1, newcap) != GDK_SUCCEED)
    5323           0 :                                                 goto bailout;
    5324           2 :                                         dst1 = (oid *) Tloc(r1, 0);
    5325           2 :                                         if (r2) {
    5326           2 :                                                 BATsetcount(r2, BATcount(r2));
    5327           2 :                                                 if (BATextend(r2, newcap) != GDK_SUCCEED)
    5328           0 :                                                         goto bailout;
    5329           2 :                                                 assert(BATcapacity(r1) == BATcapacity(r2));
    5330           2 :                                                 dst2 = (oid *) Tloc(r2, 0);
    5331             :                                         }
    5332             :                                 }
    5333       12845 :                                 dst1[r1->batCount++] = lo;
    5334       12845 :                                 if (r2) {
    5335       12836 :                                         dst2[r2->batCount++] = ro;
    5336             :                                 }
    5337             :                         }
    5338             :                 }
    5339          21 :                 TIMEOUT_CHECK(qry_ctx, GOTO_LABEL_TIMEOUT_HANDLER(bailout, qry_ctx));
    5340          21 :                 cnt = BATcount(r1);
    5341          21 :                 assert(r2 == NULL || BATcount(r1) == BATcount(r2));
    5342             :         }
    5343             : 
    5344             :         /* also set other bits of heap to correct value to indicate size */
    5345         110 :         BATsetcount(r1, cnt);
    5346             : 
    5347             :         /* set properties using an extra scan (usually not complete) */
    5348         110 :         dst1 = (oid *) Tloc(r1, 0);
    5349         110 :         r1->tkey = true;
    5350         110 :         r1->tsorted = true;
    5351         110 :         r1->trevsorted = true;
    5352         110 :         r1->tseqbase = 0;
    5353         110 :         r1->tnil = false;
    5354         110 :         r1->tnonil = true;
    5355         414 :         for (ncnt = 1; ncnt < cnt; ncnt++) {
    5356         310 :                 if (dst1[ncnt - 1] == dst1[ncnt]) {
    5357         249 :                         r1->tseqbase = oid_nil;
    5358         249 :                         r1->tkey = false;
    5359          61 :                 } else if (dst1[ncnt - 1] < dst1[ncnt]) {
    5360          58 :                         r1->trevsorted = false;
    5361          58 :                         if (dst1[ncnt - 1] + 1 != dst1[ncnt])
    5362           3 :                                 r1->tseqbase = oid_nil;
    5363             :                 } else {
    5364           3 :                         assert(sorted != 1);
    5365           3 :                         r1->tsorted = false;
    5366           3 :                         r1->tseqbase = oid_nil;
    5367           3 :                         r1->tkey = false;
    5368             :                 }
    5369         362 :                 if (!(r1->trevsorted | BATtdense(r1) | r1->tkey | ((sorted != 1) & r1->tsorted)))
    5370             :                         break;
    5371             :         }
    5372         110 :         if (BATtdense(r1))
    5373          90 :                 r1->tseqbase = cnt > 0 ? dst1[0] : 0;
    5374         110 :         if (r2) {
    5375          93 :                 BATsetcount(r2, cnt);
    5376          93 :                 dst2 = (oid *) Tloc(r2, 0);
    5377          93 :                 r2->tkey = true;
    5378          93 :                 r2->tsorted = true;
    5379          93 :                 r2->trevsorted = true;
    5380          93 :                 r2->tseqbase = 0;
    5381          93 :                 r2->tnil = false;
    5382          93 :                 r2->tnonil = true;
    5383         398 :                 for (ncnt = 1; ncnt < cnt; ncnt++) {
    5384         313 :                         if (dst2[ncnt - 1] == dst2[ncnt]) {
    5385          43 :                                 r2->tseqbase = oid_nil;
    5386          43 :                                 r2->tkey = false;
    5387         270 :                         } else if (dst2[ncnt - 1] < dst2[ncnt]) {
    5388         263 :                                 r2->trevsorted = false;
    5389         263 :                                 if (dst2[ncnt - 1] + 1 != dst2[ncnt])
    5390          80 :                                         r2->tseqbase = oid_nil;
    5391             :                         } else {
    5392           7 :                                 assert(sorted != 2);
    5393           7 :                                 r2->tsorted = false;
    5394           7 :                                 r2->tseqbase = oid_nil;
    5395           7 :                                 r2->tkey = false;
    5396             :                         }
    5397         408 :                         if (!(r2->trevsorted | BATtdense(r2) | r2->tkey | ((sorted != 2) & r2->tsorted)))
    5398             :                                 break;
    5399             :                 }
    5400          93 :                 if (BATtdense(r2))
    5401          64 :                         r2->tseqbase = cnt > 0 ? dst2[0] : 0;
    5402             :         }
    5403         110 :         TRC_DEBUG(ALGO, "l=%s,rl=%s,rh=%s -> "
    5404             :                   "(" ALGOBATFMT "," ALGOOPTBATFMT ")\n",
    5405             :                   BATgetId(l), BATgetId(rl), BATgetId(rh),
    5406             :                   ALGOBATPAR(r1), ALGOOPTBATPAR(r2));
    5407         110 :         bat_iterator_end(&li);
    5408         110 :         bat_iterator_end(&rli);
    5409         110 :         bat_iterator_end(&rhi);
    5410         110 :         return GDK_SUCCEED;
    5411             : 
    5412           0 :   bailout:
    5413           0 :         bat_iterator_end(&li);
    5414           0 :         bat_iterator_end(&rli);
    5415           0 :         bat_iterator_end(&rhi);
    5416           0 :         BBPreclaim(r1);
    5417           0 :         BBPreclaim(r2);
    5418             :         return GDK_FAIL;
    5419             : }
    5420             : 
    5421             : gdk_return
    5422         125 : BATrangejoin(BAT **r1p, BAT **r2p, BAT *l, BAT *rl, BAT *rh,
    5423             :              BAT *sl, BAT *sr, bool linc, bool hinc, bool anti, bool symmetric,
    5424             :              BUN estimate)
    5425             : {
    5426         125 :         struct canditer lci, rci;
    5427         125 :         BAT *r1 = NULL, *r2 = NULL;
    5428         125 :         BUN maxsize;
    5429         125 :         lng t0 = 0;
    5430             : 
    5431         125 :         TRC_DEBUG_IF(ALGO) t0 = GDKusec();
    5432         125 :         *r1p = NULL;
    5433         125 :         if (r2p) {
    5434         102 :                 *r2p = NULL;
    5435             :         }
    5436         125 :         if (joinparamcheck(l, rl, rh, sl, sr, __func__) != GDK_SUCCEED)
    5437             :                 return GDK_FAIL;
    5438         125 :         canditer_init(&lci, l, sl);
    5439         125 :         canditer_init(&rci, rl, sr);
    5440         125 :         if (lci.ncand == 0 ||
    5441         118 :             rci.ncand == 0 ||
    5442         110 :             (l->ttype == TYPE_void && is_oid_nil(l->tseqbase)) ||
    5443         110 :             ((rl->ttype == TYPE_void && is_oid_nil(rl->tseqbase)) &&
    5444           0 :              (rh->ttype == TYPE_void && is_oid_nil(rh->tseqbase)))) {
    5445             :                 /* trivial: empty input */
    5446          15 :                 return nomatch(r1p, r2p, NULL, l, rl, &lci, 0, false, false,
    5447             :                                __func__, t0);
    5448             :         }
    5449         110 :         if (rl->ttype == TYPE_void && is_oid_nil(rl->tseqbase)) {
    5450           0 :                 if (!anti)
    5451           0 :                         return nomatch(r1p, r2p, NULL, l, rl, &lci, 0, false, false,
    5452             :                                        __func__, t0);
    5453           0 :                 return thetajoin(r1p, r2p, l, rh, sl, sr, MASK_GT, estimate, false,
    5454             :                                  __func__, t0);
    5455             :         }
    5456         110 :         if (rh->ttype == TYPE_void && is_oid_nil(rh->tseqbase)) {
    5457           0 :                 if (!anti)
    5458           0 :                         return nomatch(r1p, r2p, NULL, l, rl, &lci, 0, false, false,
    5459             :                                        __func__, t0);
    5460           0 :                 return thetajoin(r1p, r2p, l, rl, sl, sr, MASK_LT, estimate, false,
    5461             :                                  __func__, t0);
    5462             :         }
    5463             : 
    5464         127 :         if ((maxsize = joininitresults(&r1, r2p ? &r2 : NULL, NULL, sl ? BATcount(sl) : BATcount(l), sr ? BATcount(sr) : BATcount(rl), false, false, false, false, false, false, estimate)) == BUN_NONE)
    5465             :                 return GDK_FAIL;
    5466         110 :         *r1p = r1;
    5467         110 :         if (r2p) {
    5468          93 :                 *r2p = r2;
    5469             :         }
    5470         110 :         if (maxsize == 0)
    5471             :                 return GDK_SUCCEED;
    5472             : 
    5473         110 :         return rangejoin(r1, r2, l, rl, rh, &lci, &rci, linc, hinc, anti, symmetric, maxsize);
    5474             : }

Generated by: LCOV version 1.14