LCOV - code coverage report
Current view: top level - sql/backends/monet5 - sql_statement.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 2886 3480 82.9 %
Date: 2024-11-14 20:04:02 Functions: 108 112 96.4 %

          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 "sql_mem.h"
      15             : #include "sql_stack.h"
      16             : #include "sql_statement.h"
      17             : #include "sql_gencode.h"
      18             : #include "rel_rel.h"
      19             : #include "rel_exp.h"
      20             : #include "rel_prop.h"
      21             : 
      22             : #include "mal_namespace.h"
      23             : #include "mal_builder.h"
      24             : #include "opt_prelude.h"
      25             : 
      26             : /*
      27             :  * Some utility routines to generate code
      28             :  * The equality operator in MAL is '==' instead of '='.
      29             :  */
      30             : static const char *
      31       17900 : convertMultiplexMod(const char *mod, const char *op)
      32             : {
      33       17900 :         if (strcmp(op, "=") == 0)
      34           0 :                 return "calc";
      35             :         return mod;
      36             : }
      37             : 
      38             : static const char *
      39      255261 : convertMultiplexFcn(const char *op)
      40             : {
      41      255261 :         if (strcmp(op, "=") == 0)
      42       32739 :                 return "==";
      43             :         return op;
      44             : }
      45             : 
      46             : static InstrPtr
      47       17825 : multiplex2(MalBlkPtr mb, const char *mod, const char *name, int o1, int o2, int rtype)
      48             : {
      49       17825 :         InstrPtr q = NULL;
      50             : 
      51       17825 :         q = newStmt(mb, malRef, multiplexRef);
      52       17825 :         if (q == NULL)
      53             :                 return NULL;
      54       17825 :         setVarType(mb, getArg(q, 0), newBatType(rtype));
      55       17825 :         q = pushStr(mb, q, convertMultiplexMod(mod, name));
      56       17825 :         q = pushStr(mb, q, convertMultiplexFcn(name));
      57       17825 :         q = pushArgument(mb, q, o1);
      58       17825 :         q = pushArgument(mb, q, o2);
      59       17825 :         pushInstruction(mb, q);
      60       17825 :         return q;
      61             : }
      62             : 
      63             : static InstrPtr
      64       50836 : dump_1(MalBlkPtr mb, const char *mod, const char *name, stmt *o1)
      65             : {
      66       50836 :         InstrPtr q = NULL;
      67             : 
      68       50836 :         if (o1 == NULL || o1->nr < 0)
      69             :                 return NULL;
      70       50836 :         q = newStmt(mb, mod, name);
      71       50836 :         q = pushArgument(mb, q, o1->nr);
      72       50836 :         pushInstruction(mb, q);
      73       50836 :         return q;
      74             : }
      75             : 
      76             : static InstrPtr
      77      180183 : dump_2(MalBlkPtr mb, const char *mod, const char *name, stmt *o1, stmt *o2)
      78             : {
      79      180183 :         InstrPtr q = NULL;
      80             : 
      81      180183 :         if (o1 == NULL || o2 == NULL || o1->nr < 0 || o2->nr < 0)
      82             :                 return NULL;
      83      180183 :         q = newStmt(mb, mod, name);
      84      180183 :         q = pushArgument(mb, q, o1->nr);
      85      180183 :         q = pushArgument(mb, q, o2->nr);
      86      180183 :         pushInstruction(mb, q);
      87      180183 :         return q;
      88             : }
      89             : 
      90             : static InstrPtr
      91         268 : pushPtr(MalBlkPtr mb, InstrPtr q, ptr val)
      92             : {
      93         268 :         int _t;
      94         268 :         ValRecord cst;
      95             : 
      96         268 :         if (q == NULL || mb->errors)
      97             :                 return q;
      98         268 :         cst.vtype= TYPE_ptr;
      99         268 :         cst.val.pval = val;
     100         268 :         cst.len = 0;
     101         268 :         _t = defConstant(mb, TYPE_ptr, &cst);
     102         268 :         if( _t >= 0)
     103         268 :                 return pushArgument(mb, q, _t);
     104             :         return q;
     105             : }
     106             : 
     107             : static InstrPtr
     108     2058876 : pushSchema(MalBlkPtr mb, InstrPtr q, sql_table *t)
     109             : {
     110     2058876 :         if (t->s)
     111     2058876 :                 return pushArgument(mb, q, getStrConstant(mb,t->s->base.name));
     112             :         else
     113           0 :                 return pushNil(mb, q, TYPE_str);
     114             : }
     115             : 
     116             : int
     117           0 : stmt_key(stmt *s)
     118             : {
     119           0 :         const char *nme = column_name(NULL, s);
     120             : 
     121           0 :         return hash_key(nme);
     122             : }
     123             : 
     124             : /* #TODO make proper traversal operations */
     125             : stmt *
     126         209 : stmt_atom_string(backend *be, const char *S)
     127             : {
     128         209 :         const char *s = sa_strdup(be->mvc->sa, S);
     129         209 :         sql_subtype t;
     130             : 
     131         209 :         if (s == NULL)
     132             :                 return NULL;
     133         209 :         sql_find_subtype(&t, "varchar", _strlen(s), 0);
     134         209 :         return stmt_atom(be, atom_string(be->mvc->sa, &t, s));
     135             : }
     136             : 
     137             : stmt *
     138       19456 : stmt_atom_string_nil(backend *be)
     139             : {
     140       19456 :         sql_subtype t;
     141             : 
     142       19456 :         sql_find_subtype(&t, "varchar", 0, 0);
     143       19456 :         return stmt_atom(be, atom_string(be->mvc->sa, &t, NULL));
     144             : }
     145             : 
     146             : stmt *
     147        5798 : stmt_atom_int(backend *be, int i)
     148             : {
     149        5798 :         sql_subtype t;
     150             : 
     151        5798 :         sql_find_subtype(&t, "int", 32, 0);
     152        5798 :         return stmt_atom(be, atom_int(be->mvc->sa, &t, i));
     153             : }
     154             : 
     155             : stmt *
     156      106663 : stmt_atom_lng(backend *be, lng i)
     157             : {
     158      106663 :         sql_subtype t;
     159             : 
     160      106663 :         sql_find_subtype(&t, "bigint", 64, 0);
     161      106662 :         return stmt_atom(be, atom_int(be->mvc->sa, &t, i));
     162             : }
     163             : 
     164             : stmt *
     165          28 : stmt_atom_lng_nil(backend *be)
     166             : {
     167          28 :         sql_subtype t;
     168             : 
     169          28 :         sql_find_subtype(&t, "bigint", 64, 0);
     170          28 :         return stmt_atom(be, atom_general(be->mvc->sa, &t, NULL, 0));
     171             : }
     172             : 
     173             : stmt *
     174       40349 : stmt_bool(backend *be, int b)
     175             : {
     176       40349 :         sql_subtype t;
     177             : 
     178       40349 :         sql_find_subtype(&t, "boolean", 0, 0);
     179             : 
     180       40349 :         if (b == bit_nil) {
     181           0 :                 return stmt_atom(be, atom_bool(be->mvc->sa, &t, bit_nil));
     182       40349 :         } else if (b) {
     183       39220 :                 return stmt_atom(be, atom_bool(be->mvc->sa, &t, TRUE));
     184             :         } else {
     185        1129 :                 return stmt_atom(be, atom_bool(be->mvc->sa, &t, FALSE));
     186             :         }
     187             : }
     188             : 
     189             : static stmt *
     190    17413986 : stmt_create(allocator *sa, st_type type)
     191             : {
     192    17413986 :         stmt *s = SA_NEW(sa, stmt);
     193             : 
     194    17413801 :         if (!s)
     195             :                 return NULL;
     196    17413801 :         *s = (stmt) {
     197             :                 .type = type,
     198             :         };
     199    17413801 :         return s;
     200             : }
     201             : 
     202             : stmt *
     203       27771 : stmt_group(backend *be, stmt *s, stmt *grp, stmt *ext, stmt *cnt, int done)
     204             : {
     205       27771 :         MalBlkPtr mb = be->mb;
     206       27771 :         InstrPtr q = NULL;
     207             : 
     208       27771 :         if (s == NULL || s->nr < 0)
     209           0 :                 goto bailout;
     210       27771 :         if (grp && (grp->nr < 0 || ext->nr < 0 || cnt->nr < 0))
     211           0 :                 goto bailout;
     212             : 
     213       27771 :         q = newStmt(mb, groupRef, done ? grp ? subgroupdoneRef : groupdoneRef : grp ? subgroupRef : groupRef);
     214       27771 :         if (q == NULL)
     215           0 :                 goto bailout;
     216             : 
     217             :         /* output variables extent and hist */
     218       27771 :         q = pushReturn(mb, q, newTmpVariable(mb, TYPE_any));
     219       27771 :         q = pushReturn(mb, q, newTmpVariable(mb, TYPE_any));
     220       27771 :         q = pushArgument(mb, q, s->nr);
     221       27771 :         if (grp)
     222        6188 :                 q = pushArgument(mb, q, grp->nr);
     223             : 
     224       27771 :         bool enabled = be->mvc->sa->eb.enabled;
     225       27771 :         be->mvc->sa->eb.enabled = false;
     226       27771 :         stmt *ns = stmt_create(be->mvc->sa, st_group);
     227       27771 :         be->mvc->sa->eb.enabled = enabled;
     228       27771 :         if (ns == NULL) {
     229           0 :                 freeInstruction(q);
     230           0 :                 goto bailout;
     231             :         }
     232             : 
     233       27771 :         ns->op1 = s;
     234             : 
     235       27771 :         if (grp) {
     236        6188 :                 ns->op2 = grp;
     237        6188 :                 ns->op3 = ext;
     238        6188 :                 ns->op4.stval = cnt;
     239             :         }
     240       27771 :         ns->nrcols = s->nrcols;
     241       27771 :         ns->key = 0;
     242       27771 :         ns->q = q;
     243       27771 :         ns->nr = getDestVar(q);
     244       27771 :         pushInstruction(mb, q);
     245       27771 :         return ns;
     246             : 
     247           0 :   bailout:
     248           0 :         if (be->mvc->sa->eb.enabled)
     249           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
     250             :         return NULL;
     251             : }
     252             : 
     253             : stmt *
     254          76 : stmt_unique(backend *be, stmt *s)
     255             : {
     256          76 :         MalBlkPtr mb = be->mb;
     257          76 :         InstrPtr q = NULL;
     258             : 
     259          76 :         if (s == NULL || s->nr < 0)
     260           0 :                 goto bailout;
     261             : 
     262          76 :         q = newStmt(mb, algebraRef, uniqueRef);
     263          76 :         if (q == NULL)
     264           0 :                 goto bailout;
     265             : 
     266          76 :         q = pushArgument(mb, q, s->nr);
     267          76 :         q = pushNilBat(mb, q); /* candidate list */
     268             : 
     269          76 :         bool enabled = be->mvc->sa->eb.enabled;
     270          76 :         be->mvc->sa->eb.enabled = false;
     271          76 :         stmt *ns = stmt_create(be->mvc->sa, st_unique);
     272          76 :         be->mvc->sa->eb.enabled = enabled;
     273          76 :         if (ns == NULL) {
     274           0 :                 freeInstruction(q);
     275           0 :                 goto bailout;
     276             :         }
     277             : 
     278          76 :         ns->op1 = s;
     279          76 :         ns->nrcols = s->nrcols;
     280          76 :         ns->key = 1;
     281          76 :         ns->q = q;
     282          76 :         ns->nr = getDestVar(q);
     283          76 :         pushInstruction(mb, q);
     284          76 :         return ns;
     285             : 
     286           0 :   bailout:
     287           0 :         if (be->mvc->sa->eb.enabled)
     288           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
     289             :         return NULL;
     290             : }
     291             : 
     292             : stmt *
     293           0 : stmt_none(backend *be)
     294             : {
     295           0 :         return stmt_create(be->mvc->sa, st_none);
     296             : }
     297             : 
     298             : static int
     299          55 : create_bat(MalBlkPtr mb, int tt)
     300             : {
     301          55 :         InstrPtr q = newStmt(mb, batRef, newRef);
     302             : 
     303          55 :         if (q == NULL)
     304             :                 return -1;
     305          55 :         setVarType(mb, getArg(q, 0), newBatType(tt));
     306          55 :         q = pushType(mb, q, tt);
     307          55 :         pushInstruction(mb, q);
     308          55 :         return getDestVar(q);
     309             : }
     310             : 
     311             : static int *
     312          20 : dump_table(allocator *sa, MalBlkPtr mb, sql_table *t)
     313             : {
     314          20 :         int i = 0;
     315          20 :         node *n;
     316          20 :         int *l = SA_NEW_ARRAY(sa, int, ol_length(t->columns) + 1);
     317             : 
     318          20 :         if (!l)
     319             :                 return NULL;
     320             : 
     321             :         /* tid column */
     322          20 :         if ((l[i++] = create_bat(mb, TYPE_oid)) < 0)
     323             :                 return NULL;
     324             : 
     325          55 :         for (n = ol_first_node(t->columns); n; n = n->next) {
     326          35 :                 sql_column *c = n->data;
     327             : 
     328          35 :                 if ((l[i++] = create_bat(mb, c->type.type->localtype)) < 0)
     329             :                         return NULL;
     330             :         }
     331             :         return l;
     332             : }
     333             : 
     334             : stmt *
     335        2776 : stmt_var(backend *be, const char *sname, const char *varname, sql_subtype *t, int declare, int level)
     336             : {
     337        2776 :         MalBlkPtr mb = be->mb;
     338        2776 :         InstrPtr q = NULL;
     339        2776 :         char *buf;
     340             : 
     341        2776 :         if (level == 0) { /* global */
     342         433 :                 int tt = t->type->localtype;
     343             : 
     344         433 :                 assert(sname);
     345         433 :                 q = newStmt(mb, sqlRef, getVariableRef);
     346         433 :                 if (q == NULL)
     347           0 :                         goto bailout;
     348         433 :                 q = pushArgument(mb, q, be->mvc_var);
     349         433 :                 q = pushStr(mb, q, sname); /* all global variables have a schema */
     350         433 :                 q = pushStr(mb, q, varname);
     351         433 :                 setVarType(mb, getArg(q, 0), tt);
     352        2343 :         } else if (!declare) {
     353        1954 :                 char levelstr[16];
     354             : 
     355        1954 :                 assert(!sname);
     356        1954 :                 snprintf(levelstr, sizeof(levelstr), "%d", level);
     357        1954 :                 buf = SA_NEW_ARRAY(be->mvc->sa, char, strlen(levelstr) + strlen(varname) + 3);
     358        1954 :                 if (!buf)
     359           1 :                         goto bailout;
     360        1954 :                 stpcpy(stpcpy(stpcpy(stpcpy(buf, "A"), levelstr), "%"), varname); /* mangle variable name */
     361        1954 :                 q = newAssignment(mb);
     362        1954 :                 if (q == NULL)
     363           1 :                         goto bailout;
     364        1953 :                 q = pushArgumentId(mb, q, buf);
     365             :         } else {
     366         389 :                 int tt = t->type->localtype;
     367         389 :                 char levelstr[16];
     368             : 
     369         389 :                 assert(!sname);
     370         389 :                 snprintf(levelstr, sizeof(levelstr), "%d", level);
     371         389 :                 buf = SA_NEW_ARRAY(be->mvc->sa, char, strlen(levelstr) + strlen(varname) + 3);
     372         389 :                 if (!buf)
     373           0 :                         goto bailout;
     374         389 :                 stpcpy(stpcpy(stpcpy(stpcpy(buf, "A"), levelstr), "%"), varname); /* mangle variable name */
     375             : 
     376         389 :                 q = newInstruction(mb, NULL, NULL);
     377         389 :                 if (q == NULL) {
     378           0 :                         goto bailout;
     379             :                 }
     380         389 :                 q->argc = q->retc = 0;
     381         389 :                 q = pushArgumentId(mb, q, buf);
     382         389 :                 q = pushNil(mb, q, tt);
     383         389 :                 q->retc++;
     384             :         }
     385        2775 :         bool enabled = be->mvc->sa->eb.enabled;
     386        2775 :         be->mvc->sa->eb.enabled = false;
     387        2775 :         stmt *s = stmt_create(be->mvc->sa, st_var);
     388        2775 :         be->mvc->sa->eb.enabled = enabled;
     389        2775 :         if (s == NULL) {
     390           0 :                 freeInstruction(q);
     391           0 :                 goto bailout;
     392             :         }
     393             : 
     394        2775 :         if (t)
     395        2775 :                 s->op4.typeval = *t;
     396             :         else
     397           0 :                 s->op4.typeval.type = NULL;
     398        2775 :         s->flag = declare + (level << 1);
     399        2775 :         s->key = 1;
     400        2775 :         s->q = q;
     401        2775 :         s->nr = getDestVar(q);
     402        2775 :         pushInstruction(mb, q);
     403        2775 :         return s;
     404             : 
     405           1 :   bailout:
     406           1 :         if (be->mvc->sa->eb.enabled)
     407           1 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
     408             :         return NULL;
     409             : }
     410             : 
     411             : stmt *
     412          20 : stmt_vars(backend *be, const char *varname, sql_table *t, int declare, int level)
     413             : {
     414          20 :         MalBlkPtr mb = be->mb;
     415          20 :         int *l;
     416             : 
     417          20 :         (void)varname;
     418             :         /* declared table */
     419          20 :         if ((l = dump_table(be->mvc->sa, mb, t)) != NULL) {
     420          20 :                 stmt *s = stmt_create(be->mvc->sa, st_var);
     421             : 
     422          20 :                 if (s == NULL) {
     423             :                         return NULL;
     424             :                 }
     425             : 
     426          20 :                 ATOMIC_PTR_SET(&t->data, l);
     427             :                 /*
     428             :                 s->op2 = (stmt*)l;
     429             :                 s->op3 = (stmt*)t;
     430             :                 */
     431          20 :                 s->flag = declare + (level << 1);
     432          20 :                 s->key = 1;
     433          20 :                 s->nr = l[0];
     434          20 :                 return s;
     435             :         }
     436           0 :         if (be->mvc->sa->eb.enabled)
     437           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
     438             :         return NULL;
     439             : }
     440             : 
     441             : stmt *
     442        2052 : stmt_varnr(backend *be, int nr, sql_subtype *t)
     443             : {
     444        2052 :         MalBlkPtr mb = be->mb;
     445        2052 :         InstrPtr q = newAssignment(mb);
     446        2052 :         char buf[IDLENGTH];
     447             : 
     448        2052 :         if (q == NULL)
     449           0 :                 goto bailout;
     450             : 
     451        2052 :         (void) snprintf(buf, sizeof(buf), "A%d", nr);
     452        2052 :         q = pushArgumentId(mb, q, buf);
     453             : 
     454        2052 :         bool enabled = be->mvc->sa->eb.enabled;
     455        2052 :         be->mvc->sa->eb.enabled = false;
     456        2052 :         stmt *s = stmt_create(be->mvc->sa, st_var);
     457        2052 :         be->mvc->sa->eb.enabled = enabled;
     458        2052 :         if (s == NULL) {
     459           0 :                 freeInstruction(q);
     460           0 :                 goto bailout;
     461             :         }
     462             : 
     463        2052 :         s->op1 = NULL;
     464        2052 :         if (t)
     465        2052 :                 s->op4.typeval = *t;
     466             :         else
     467           0 :                 s->op4.typeval.type = NULL;
     468        2052 :         s->flag = nr;
     469        2052 :         s->key = 1;
     470        2052 :         s->q = q;
     471        2052 :         s->nr = getDestVar(q);
     472        2052 :         pushInstruction(mb, q);
     473        2052 :         return s;
     474             : 
     475           0 :   bailout:
     476           0 :         if (be->mvc->sa->eb.enabled)
     477           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
     478             :         return NULL;
     479             : }
     480             : 
     481             : stmt *
     482         688 : stmt_table(backend *be, stmt *cols, int temp)
     483             : {
     484         688 :         MalBlkPtr mb = be->mb;
     485             : 
     486         688 :         if (cols == NULL || cols->nr < 0)
     487           0 :                 goto bailout;
     488             : 
     489         688 :         stmt *s = stmt_create(be->mvc->sa, st_table);
     490             : 
     491         688 :         if (s == NULL)
     492           0 :                 goto bailout;
     493             : 
     494         688 :         if (cols->type != st_list) {
     495           0 :             InstrPtr q = newAssignment(mb);
     496           0 :                 if (q == NULL)
     497           0 :                         goto bailout;
     498           0 :                 pushInstruction(mb, q);
     499           0 :                 q = newStmt(mb, sqlRef, printRef);
     500           0 :                 if (q == NULL)
     501           0 :                         goto bailout;
     502           0 :                 q = pushStr(mb, q, "not a valid output list\n");
     503           0 :                 pushInstruction(mb, q);
     504             :         }
     505         688 :         s->op1 = cols;
     506         688 :         s->flag = temp;
     507         688 :         s->nr = cols->nr;
     508         688 :         s->nrcols = cols->nrcols;
     509         688 :         return s;
     510             : 
     511           0 :   bailout:
     512           0 :         if (be->mvc->sa->eb.enabled)
     513           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
     514             :         return NULL;
     515             : }
     516             : 
     517             : stmt *
     518      223199 : stmt_temp(backend *be, sql_subtype *t)
     519             : {
     520      223199 :         int tt = t->type->localtype;
     521      223199 :         MalBlkPtr mb = be->mb;
     522      223199 :         InstrPtr q = newStmt(mb, batRef, newRef);
     523             : 
     524      223281 :         if (q == NULL)
     525           0 :                 goto bailout;
     526      223281 :         setVarType(mb, getArg(q, 0), newBatType(tt));
     527      223281 :         q = pushType(mb, q, tt);
     528      223246 :         bool enabled = be->mvc->sa->eb.enabled;
     529      223246 :         be->mvc->sa->eb.enabled = false;
     530      223246 :         stmt *s = stmt_create(be->mvc->sa, st_temp);
     531      223241 :         be->mvc->sa->eb.enabled = enabled;
     532             : 
     533      223241 :         if (s == NULL) {
     534           0 :                 freeInstruction(q);
     535           0 :                 goto bailout;
     536             :         }
     537      223241 :         s->op4.typeval = *t;
     538      223241 :         s->nrcols = 1;
     539      223241 :         s->q = q;
     540      223241 :         s->nr = getDestVar(q);
     541      223241 :         pushInstruction(mb, q);
     542      223241 :         return s;
     543             : 
     544           0 :   bailout:
     545           0 :         if (be->mvc->sa->eb.enabled)
     546           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
     547             :         return NULL;
     548             : }
     549             : 
     550             : stmt *
     551         216 : stmt_blackbox_result(backend *be, InstrPtr q, int retnr, sql_subtype *t)
     552             : {
     553         216 :         if (q == NULL)
     554             :                 return NULL;
     555         216 :         stmt *s = stmt_create(be->mvc->sa, st_result);
     556         216 :         if (s == NULL)
     557             :                 return NULL;
     558         216 :         s->op4.typeval = *t;
     559         216 :         s->nrcols = 1;
     560         216 :         s->q = q;
     561         216 :         s->nr = getArg(q, retnr);
     562         216 :         s->flag = retnr;
     563         216 :         return s;
     564             : }
     565             : 
     566             : 
     567             : stmt *
     568      202999 : stmt_tid(backend *be, sql_table *t, int partition)
     569             : {
     570      202999 :         int tt = TYPE_oid;
     571      202999 :         MalBlkPtr mb = be->mb;
     572      202999 :         InstrPtr q;
     573             : 
     574      202999 :         if (!t->s && ATOMIC_PTR_GET(&t->data)) { /* declared table */
     575          16 :                 stmt *s = stmt_create(be->mvc->sa, st_tid);
     576          16 :                 int *l = ATOMIC_PTR_GET(&t->data);
     577             : 
     578          16 :                 if (s == NULL) {
     579           0 :                         goto bailout;
     580             :                 }
     581          16 :                 assert(partition == 0);
     582          16 :                 s->partition = partition;
     583          16 :                 s->op4.tval = t;
     584          16 :                 s->nrcols = 1;
     585          16 :                 s->nr = l[0];
     586          16 :                 return s;
     587             :         }
     588      202983 :         q = newStmt(mb, sqlRef, tidRef);
     589      202983 :         if (q == NULL)
     590           0 :                 goto bailout;
     591      202983 :         setVarType(mb, getArg(q, 0), newBatType(tt));
     592      202983 :         q = pushArgument(mb, q, be->mvc_var);
     593      202983 :         q = pushSchema(mb, q, t);
     594      202983 :         q = pushStr(mb, q, t->base.name);
     595      202983 :         if (t && isTable(t) && partition) {
     596       75580 :                 sql_trans *tr = be->mvc->session->tr;
     597       75580 :                 sqlstore *store = tr->store;
     598       75580 :                 BUN rows = (BUN) store->storage_api.count_col(tr, ol_first_node(t->columns)->data, RDONLY);
     599       75580 :                 setRowCnt(mb,getArg(q,0),rows);
     600             :         }
     601             : 
     602      202983 :         bool enabled = be->mvc->sa->eb.enabled;
     603      202983 :         be->mvc->sa->eb.enabled = false;
     604      202983 :         stmt *s = stmt_create(be->mvc->sa, st_tid);
     605      202982 :         be->mvc->sa->eb.enabled = enabled;
     606      202982 :         if (s == NULL) {
     607           0 :                 freeInstruction(q);
     608           0 :                 goto bailout;
     609             :         }
     610             : 
     611      202982 :         s->partition = partition;
     612      202982 :         s->op4.tval = t;
     613      202982 :         s->nrcols = 1;
     614      202982 :         s->nr = getDestVar(q);
     615      202982 :         s->q = q;
     616      202982 :         pushInstruction(mb, q);
     617      202982 :         return s;
     618             : 
     619           0 :   bailout:
     620           0 :         if (be->mvc->sa->eb.enabled)
     621           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
     622             :         return NULL;
     623             : }
     624             : 
     625             : static sql_column *
     626      984925 : find_real_column(backend *be, sql_column *c)
     627             : {
     628      984925 :         if (c && c->t && c->t->s && c->t->persistence == SQL_DECLARED_TABLE) {
     629       36774 :                 sql_table *nt = find_sql_table_id(be->mvc->session->tr, c->t->s, c->t->base.id);
     630       36774 :                 if (nt) {
     631       36774 :                         node *n = ol_find_id(nt->columns, c->base.id);
     632       36774 :                         if (n)
     633       36774 :                                 return n->data;
     634             :                 }
     635             :         }
     636             :         return c;
     637             : }
     638             : 
     639             : stmt *
     640      984925 : stmt_bat(backend *be, sql_column *c, int access, int partition)
     641             : {
     642      984925 :         int tt = c->type.type->localtype;
     643      984925 :         MalBlkPtr mb = be->mb;
     644      984925 :         InstrPtr q;
     645             : 
     646      984925 :         c = find_real_column(be, c);
     647             : 
     648      984924 :         if (access == RD_EXT)
     649         176 :                 partition = 0;
     650             : 
     651             :         /* for read access tid.project(col) */
     652      984924 :         if (!c->t->s && ATOMIC_PTR_GET(&c->t->data)) { /* declared table */
     653          23 :                 stmt *s = stmt_create(be->mvc->sa, st_bat);
     654          23 :                 int *l = ATOMIC_PTR_GET(&c->t->data);
     655             : 
     656          23 :                 if (s == NULL) {
     657           0 :                         goto bailout;
     658             :                 }
     659          23 :                 assert(partition == 0);
     660          23 :                 s->partition = partition;
     661          23 :                 s->op4.cval = c;
     662          23 :                 s->nrcols = 1;
     663          23 :                 s->flag = access;
     664          23 :                 s->nr = l[c->colnr+1];
     665          23 :                 s->tname = c->t?c->t->base.name:NULL;
     666          23 :                 s->cname = c->base.name;
     667          23 :                 return s;
     668             :         }
     669      984901 :         q = newStmtArgs(mb, sqlRef, bindRef, 9);
     670      984900 :         if (q == NULL)
     671           0 :                 goto bailout;
     672      984900 :         if (c->storage_type && access != RD_EXT) {
     673         368 :                 sql_trans *tr = be->mvc->session->tr;
     674         368 :                 sqlstore *store = tr->store;
     675         368 :                 BAT *b = store->storage_api.bind_col(tr, c, QUICK);
     676         368 :                 if (!b) {
     677           0 :                         freeInstruction(q);
     678           0 :                         goto bailout;
     679             :                 }
     680         368 :                 tt = b->ttype;
     681             :         }
     682      984724 :         if (access == RD_UPD_ID) {
     683      478543 :                 q = pushReturn(mb, q, newTmpVariable(mb, newBatType(tt)));
     684             :         } else {
     685      506357 :                 setVarType(mb, getArg(q, 0), newBatType(tt));
     686             :         }
     687      984900 :         q = pushArgument(mb, q, be->mvc_var);
     688      984898 :         q = pushSchema(mb, q, c->t);
     689      984901 :         q = pushArgument(mb, q, getStrConstant(mb,c->t->base.name));
     690      984880 :         q = pushArgument(mb, q, getStrConstant(mb,c->base.name));
     691      984895 :         q = pushArgument(mb, q, getIntConstant(mb,access));
     692             : 
     693      984882 :         if (access == RD_UPD_ID) {
     694      478542 :                 setVarType(mb, getArg(q, 1), newBatType(tt));
     695             :         }
     696      984882 :         if (partition) {
     697      544107 :                 sql_trans *tr = be->mvc->session->tr;
     698      544107 :                 sqlstore *store = tr->store;
     699             : 
     700      544107 :                 if (c && isTable(c->t)) {
     701      544107 :                         BUN rows = (BUN) store->storage_api.count_col(tr, c, RDONLY);
     702      544136 :                         setRowCnt(mb,getArg(q,0),rows);
     703             :                 }
     704             :         }
     705             : 
     706      984911 :         bool enabled = be->mvc->sa->eb.enabled;
     707      984911 :         be->mvc->sa->eb.enabled = false;
     708      984911 :         stmt *s = stmt_create(be->mvc->sa, st_bat);
     709      984906 :         be->mvc->sa->eb.enabled = enabled;
     710      984906 :         if (s == NULL) {
     711           0 :                 freeInstruction(q);
     712           0 :                 goto bailout;
     713             :         }
     714             : 
     715      984906 :         s->partition = partition;
     716      984906 :         s->op4.cval = c;
     717      984906 :         s->nrcols = 1;
     718      984906 :         s->flag = access;
     719      984906 :         s->nr = getDestVar(q);
     720      984906 :         s->q = q;
     721      984906 :         s->tname = c->t->base.name;
     722      984906 :         s->cname = c->base.name;
     723      984906 :         pushInstruction(mb, q);
     724      984906 :         return s;
     725             : 
     726           0 :   bailout:
     727           0 :         if (be->mvc->sa->eb.enabled)
     728           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
     729             :         return NULL;
     730             : }
     731             : 
     732             : stmt *
     733        6467 : stmt_idxbat(backend *be, sql_idx *i, int access, int partition)
     734             : {
     735        6467 :         int tt = hash_index(i->type)?TYPE_lng:TYPE_oid;
     736        6467 :         MalBlkPtr mb = be->mb;
     737        6467 :         InstrPtr q = newStmtArgs(mb, sqlRef, bindidxRef, 9);
     738             : 
     739        6467 :         if (q == NULL)
     740           0 :                 goto bailout;
     741             : 
     742        6467 :         if (access == RD_UPD_ID) {
     743        2871 :                 q = pushReturn(mb, q, newTmpVariable(mb, newBatType(tt)));
     744             :         } else {
     745        3596 :                 setVarType(mb, getArg(q, 0), newBatType(tt));
     746             :         }
     747             : 
     748        6467 :         q = pushArgument(mb, q, be->mvc_var);
     749        6467 :         q = pushSchema(mb, q, i->t);
     750        6467 :         q = pushArgument(mb, q, getStrConstant(mb, i->t->base.name));
     751        6467 :         q = pushArgument(mb, q, getStrConstant(mb, i->base.name));
     752        6467 :         q = pushArgument(mb, q, getIntConstant(mb, access));
     753             : 
     754        6467 :         if (access == RD_UPD_ID) {
     755        2871 :                 setVarType(mb, getArg(q, 1), newBatType(tt));
     756             :         }
     757        6467 :         if (partition) {
     758        3550 :                 sql_trans *tr = be->mvc->session->tr;
     759        3550 :                 sqlstore *store = tr->store;
     760             : 
     761        3550 :                 if (i && isTable(i->t)) {
     762        3550 :                         BUN rows = (BUN) store->storage_api.count_idx(tr, i, QUICK);
     763        3550 :                         setRowCnt(mb,getArg(q,0),rows);
     764             :                 }
     765             :         }
     766             : 
     767        6467 :         bool enabled = be->mvc->sa->eb.enabled;
     768        6467 :         be->mvc->sa->eb.enabled = false;
     769        6467 :         stmt *s = stmt_create(be->mvc->sa, st_idxbat);
     770        6467 :         be->mvc->sa->eb.enabled = enabled;
     771        6467 :         if (s == NULL) {
     772           0 :                 freeInstruction(q);
     773           0 :                 goto bailout;
     774             :         }
     775             : 
     776        6467 :         s->partition = partition;
     777        6467 :         s->op4.idxval = i;
     778        6467 :         s->nrcols = 1;
     779        6467 :         s->flag = access;
     780        6467 :         s->nr = getDestVar(q);
     781        6467 :         s->q = q;
     782        6467 :         s->tname = i->t->base.name;
     783        6467 :         s->cname = i->base.name;
     784        6467 :         pushInstruction(mb, q);
     785        6467 :         return s;
     786             : 
     787           0 :   bailout:
     788           0 :         if (be->mvc->sa->eb.enabled)
     789           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
     790             :         return NULL;
     791             : }
     792             : 
     793             : stmt *
     794      566787 : stmt_append_col(backend *be, sql_column *c, stmt *offset, stmt *b, int *mvc_var_update, int fake)
     795             : {
     796      566787 :         MalBlkPtr mb = be->mb;
     797      566787 :         InstrPtr q = NULL;
     798             : 
     799      566787 :         if (b == NULL || b->nr < 0)
     800           0 :                 goto bailout;
     801             : 
     802      566787 :         if (!c->t->s && ATOMIC_PTR_GET(&c->t->data)) { /* declared table */
     803          23 :                 int *l = ATOMIC_PTR_GET(&c->t->data);
     804             : 
     805          23 :                 if (c->colnr == 0) { /* append to tid column */
     806          18 :                         q = newStmt(mb, sqlRef, growRef);
     807          18 :                         if (q == NULL)
     808           0 :                                 goto bailout;
     809          18 :                         q = pushArgument(mb, q, l[0]);
     810          18 :                         q = pushArgument(mb, q, b->nr);
     811          18 :                         pushInstruction(mb, q);
     812             :                 }
     813          23 :                 q = newStmt(mb, batRef, appendRef);
     814          23 :                 if (q == NULL)
     815           0 :                         goto bailout;
     816          23 :                 q = pushArgument(mb, q, l[c->colnr+1]);
     817          23 :                 q = pushArgument(mb, q, b->nr);
     818          23 :                 q = pushBit(mb, q, TRUE);
     819          23 :                 getArg(q,0) = l[c->colnr+1];
     820      566764 :         } else if (!fake) {     /* fake append */
     821      566764 :                 if (offset == NULL || offset->nr < 0)
     822           0 :                         goto bailout;
     823      566764 :                 q = newStmt(mb, sqlRef, appendRef);
     824      566791 :                 if (q == NULL)
     825           0 :                         goto bailout;
     826      566791 :                 q = pushArgument(mb, q, be->mvc_var);
     827      566796 :                 int tmpvar = newTmpVariable(mb, TYPE_int);
     828      566794 :                 getArg(q, 0) = tmpvar;
     829      566794 :                 if (mvc_var_update != NULL)
     830      566794 :                         *mvc_var_update = tmpvar;
     831      566794 :                 q = pushSchema(mb, q, c->t);
     832      566790 :                 q = pushStr(mb, q, c->t->base.name);
     833      566786 :                 q = pushStr(mb, q, c->base.name);
     834      566761 :                 q = pushArgument(mb, q, offset->nr);
     835             :                 /* also the offsets */
     836      566761 :                 assert(offset->q->retc == 2);
     837      566761 :                 q = pushArgument(mb, q, getArg(offset->q, 1));
     838      566764 :                 q = pushArgument(mb, q, b->nr);
     839      566758 :                 if (mvc_var_update != NULL)
     840      566758 :                         *mvc_var_update = getDestVar(q);
     841             :         } else {
     842             :                 return b;
     843             :         }
     844      566781 :         bool enabled = be->mvc->sa->eb.enabled;
     845      566781 :         be->mvc->sa->eb.enabled = false;
     846      566781 :         stmt *s = stmt_create(be->mvc->sa, st_append_col);
     847      566782 :         be->mvc->sa->eb.enabled = enabled;
     848             : 
     849      566782 :         if (s == NULL) {
     850           0 :                 freeInstruction(q);
     851           0 :                 goto bailout;
     852             :         }
     853      566782 :         s->op1 = b;
     854      566782 :         s->op2 = offset;
     855      566782 :         s->op4.cval = c;
     856      566782 :         s->q = q;
     857      566782 :         s->nr = getDestVar(q);
     858      566782 :         pushInstruction(mb, q);
     859      566782 :         return s;
     860             : 
     861           0 :   bailout:
     862           0 :         if (be->mvc->sa->eb.enabled)
     863           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
     864             :         return NULL;
     865             : }
     866             : 
     867             : stmt *
     868        2220 : stmt_append_idx(backend *be, sql_idx *i, stmt *offset, stmt *b)
     869             : {
     870        2220 :         MalBlkPtr mb = be->mb;
     871        2220 :         InstrPtr q = NULL;
     872             : 
     873        2220 :         if (offset == NULL || b == NULL || offset->nr < 0 || b->nr < 0)
     874           0 :                 goto bailout;
     875             : 
     876        2220 :         q = newStmt(mb, sqlRef, appendRef);
     877        2220 :         if (q == NULL)
     878           0 :                 goto bailout;
     879        2220 :         q = pushArgument(mb, q, be->mvc_var);
     880        2220 :         getArg(q, 0) = be->mvc_var = newTmpVariable(mb, TYPE_int);
     881        2220 :         q = pushSchema(mb, q, i->t);
     882        2220 :         q = pushStr(mb, q, i->t->base.name);
     883        2220 :         q = pushStr(mb, q, sa_strconcat(be->mvc->sa, "%", i->base.name));
     884        2220 :         q = pushArgument(mb, q, offset->nr);
     885             :         /* also the offsets */
     886        2220 :         assert(offset->q->retc == 2);
     887        2220 :         q = pushArgument(mb, q, getArg(offset->q, 1));
     888        2220 :         q = pushArgument(mb, q, b->nr);
     889        2220 :         be->mvc_var = getDestVar(q);
     890             : 
     891        2220 :         bool enabled = be->mvc->sa->eb.enabled;
     892        2220 :         be->mvc->sa->eb.enabled = false;
     893        2220 :         stmt *s = stmt_create(be->mvc->sa, st_append_idx);
     894        2220 :         be->mvc->sa->eb.enabled = enabled;
     895        2220 :         if (s == NULL) {
     896           0 :                 freeInstruction(q);
     897           0 :                 goto bailout;
     898             :         }
     899             : 
     900        2220 :         s->op1 = b;
     901        2220 :         s->op2 = offset;
     902        2220 :         s->op4.idxval = i;
     903        2220 :         s->q = q;
     904        2220 :         s->nr = getDestVar(q);
     905        2220 :         pushInstruction(mb, q);
     906        2220 :         return s;
     907             : 
     908           0 :   bailout:
     909           0 :         if (be->mvc->sa->eb.enabled)
     910           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
     911             :         return NULL;
     912             : }
     913             : 
     914             : stmt *
     915        2997 : stmt_update_col(backend *be, sql_column *c, stmt *tids, stmt *upd)
     916             : {
     917        2997 :         MalBlkPtr mb = be->mb;
     918        2997 :         InstrPtr q = NULL;
     919             : 
     920        2997 :         if (tids == NULL || upd == NULL || tids->nr < 0 || upd->nr < 0)
     921           0 :                 goto bailout;
     922             : 
     923        2998 :         if (!c->t->s && ATOMIC_PTR_GET(&c->t->data)) { /* declared table */
     924           1 :                 int *l = ATOMIC_PTR_GET(&c->t->data);
     925             : 
     926           1 :                 q = newStmt(mb, batRef, replaceRef);
     927           1 :                 if (q == NULL)
     928           0 :                         goto bailout;
     929           1 :                 q = pushArgument(mb, q, l[c->colnr+1]);
     930           1 :                 q = pushArgument(mb, q, tids->nr);
     931           1 :                 q = pushArgument(mb, q, upd->nr);
     932             :         } else {
     933        2996 :                 q = newStmt(mb, sqlRef, updateRef);
     934        2996 :                 if (q == NULL)
     935           0 :                         goto bailout;
     936        2996 :                 q = pushArgument(mb, q, be->mvc_var);
     937        2996 :                 getArg(q, 0) = be->mvc_var = newTmpVariable(mb, TYPE_int);
     938        2996 :                 q = pushSchema(mb, q, c->t);
     939        2996 :                 q = pushStr(mb, q, c->t->base.name);
     940        2996 :                 q = pushStr(mb, q, c->base.name);
     941        2996 :                 q = pushArgument(mb, q, tids->nr);
     942        2996 :                 q = pushArgument(mb, q, upd->nr);
     943        2996 :                 be->mvc_var = getDestVar(q);
     944             :         }
     945        2997 :         bool enabled = be->mvc->sa->eb.enabled;
     946        2997 :         be->mvc->sa->eb.enabled = false;
     947        2997 :         stmt *s = stmt_create(be->mvc->sa, st_update_col);
     948        2997 :         be->mvc->sa->eb.enabled = enabled;
     949             : 
     950        2997 :         if (s == NULL) {
     951           0 :                 freeInstruction(q);
     952           0 :                 goto bailout;
     953             :         }
     954        2997 :         s->op1 = tids;
     955        2997 :         s->op2 = upd;
     956        2997 :         s->op4.cval = c;
     957        2997 :         s->q = q;
     958        2997 :         s->nr = getDestVar(q);
     959        2997 :         pushInstruction(mb, q);
     960        2997 :         return s;
     961             : 
     962           0 :   bailout:
     963           0 :         if (be->mvc->sa->eb.enabled)
     964           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
     965             :         return NULL;
     966             : }
     967             : 
     968             : 
     969             : stmt *
     970         823 : stmt_update_idx(backend *be, sql_idx *i, stmt *tids, stmt *upd)
     971             : {
     972         823 :         MalBlkPtr mb = be->mb;
     973         823 :         InstrPtr q = NULL;
     974             : 
     975         823 :         if (tids == NULL || upd == NULL || tids->nr < 0 || upd->nr < 0)
     976           0 :                 goto bailout;
     977             : 
     978         823 :         q = newStmt(mb, sqlRef, updateRef);
     979         823 :         if (q == NULL)
     980           0 :                 goto bailout;
     981         823 :         q = pushArgument(mb, q, be->mvc_var);
     982         823 :         getArg(q, 0) = be->mvc_var = newTmpVariable(mb, TYPE_int);
     983         823 :         q = pushSchema(mb, q, i->t);
     984         823 :         q = pushStr(mb, q, i->t->base.name);
     985         823 :         q = pushStr(mb, q, sa_strconcat(be->mvc->sa, "%", i->base.name));
     986         823 :         q = pushArgument(mb, q, tids->nr);
     987         823 :         q = pushArgument(mb, q, upd->nr);
     988         823 :         be->mvc_var = getDestVar(q);
     989         823 :         bool enabled = be->mvc->sa->eb.enabled;
     990         823 :         be->mvc->sa->eb.enabled = false;
     991         823 :         stmt *s = stmt_create(be->mvc->sa, st_update_idx);
     992         823 :         be->mvc->sa->eb.enabled = enabled;
     993         823 :         if (s == NULL) {
     994           0 :                 freeInstruction(q);
     995           0 :                 goto bailout;
     996             :         }
     997             : 
     998         823 :         s->op1 = tids;
     999         823 :         s->op2 = upd;
    1000         823 :         s->op4.idxval = i;
    1001         823 :         s->q = q;
    1002         823 :         s->nr = getDestVar(q);
    1003         823 :         pushInstruction(mb, q);
    1004         823 :         return s;
    1005             : 
    1006           0 :   bailout:
    1007           0 :         if (be->mvc->sa->eb.enabled)
    1008           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    1009             :         return NULL;
    1010             : }
    1011             : 
    1012             : stmt *
    1013         349 : stmt_delete(backend *be, sql_table *t, stmt *tids)
    1014             : {
    1015         349 :         MalBlkPtr mb = be->mb;
    1016         349 :         InstrPtr q = NULL;
    1017             : 
    1018         349 :         if (tids == NULL || tids->nr < 0)
    1019           0 :                 goto bailout;
    1020             : 
    1021         350 :         if (!t->s && ATOMIC_PTR_GET(&t->data)) { /* declared table */
    1022           1 :                 int *l = ATOMIC_PTR_GET(&t->data);
    1023             : 
    1024           1 :                 q = newStmt(mb, batRef, deleteRef);
    1025           1 :                 if (q == NULL)
    1026           0 :                         goto bailout;
    1027           1 :                 q = pushArgument(mb, q, l[0]);
    1028           1 :                 q = pushArgument(mb, q, tids->nr);
    1029             :         } else {
    1030         348 :                 q = newStmt(mb, sqlRef, deleteRef);
    1031         348 :                 if (q == NULL)
    1032           0 :                         goto bailout;
    1033         348 :                 q = pushArgument(mb, q, be->mvc_var);
    1034         348 :                 getArg(q, 0) = be->mvc_var = newTmpVariable(mb, TYPE_int);
    1035         348 :                 q = pushSchema(mb, q, t);
    1036         348 :                 q = pushStr(mb, q, t->base.name);
    1037         348 :                 q = pushArgument(mb, q, tids->nr);
    1038         348 :                 be->mvc_var = getDestVar(q);
    1039             :         }
    1040         349 :         bool enabled = be->mvc->sa->eb.enabled;
    1041         349 :         be->mvc->sa->eb.enabled = false;
    1042         349 :         stmt *s = stmt_create(be->mvc->sa, st_delete);
    1043         349 :         be->mvc->sa->eb.enabled = enabled;
    1044         349 :         if (s == NULL) {
    1045           0 :                 freeInstruction(q);
    1046           0 :                 goto bailout;
    1047             :         }
    1048             : 
    1049         349 :         s->op1 = tids;
    1050         349 :         s->op4.tval = t;
    1051         349 :         s->q = q;
    1052         349 :         s->nr = getDestVar(q);
    1053         349 :         pushInstruction(mb, q);
    1054         349 :         return s;
    1055             : 
    1056           0 :   bailout:
    1057           0 :         if (be->mvc->sa->eb.enabled)
    1058           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    1059             :         return NULL;
    1060             : }
    1061             : 
    1062             : stmt *
    1063      123611 : stmt_const(backend *be, stmt *s, stmt *val)
    1064             : {
    1065      123611 :         InstrPtr q = NULL;
    1066      123611 :         MalBlkPtr mb = be->mb;
    1067             : 
    1068      123611 :         if (s == NULL)
    1069           0 :                 goto bailout;
    1070      123611 :         if (val)
    1071      123611 :                 q = dump_2(mb, algebraRef, projectRef, s, val);
    1072             :         else
    1073           0 :                 q = dump_1(mb, algebraRef, projectRef, s);
    1074      123611 :         if (q) {
    1075      123611 :                 stmt *ns = stmt_create(be->mvc->sa, st_const);
    1076      123611 :                 if (ns == NULL) {
    1077           0 :                         goto bailout;
    1078             :                 }
    1079             : 
    1080      123611 :                 ns->op1 = s;
    1081      123611 :                 ns->op2 = val;
    1082      123611 :                 ns->nrcols = s->nrcols;
    1083      123611 :                 ns->key = s->key;
    1084      123611 :                 ns->aggr = s->aggr;
    1085      123611 :                 ns->q = q;
    1086      123611 :                 ns->nr = getDestVar(q);
    1087      123611 :                 ns->tname = val->tname;
    1088      123611 :                 ns->cname = val->cname;
    1089      123611 :                 return ns;
    1090             :         }
    1091           0 :   bailout:
    1092           0 :         if (be->mvc->sa->eb.enabled)
    1093           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    1094             :         return NULL;
    1095             : }
    1096             : 
    1097             : stmt *
    1098        2397 : stmt_gen_group(backend *be, stmt *gids, stmt *cnts)
    1099             : {
    1100        2397 :         MalBlkPtr mb = be->mb;
    1101             : 
    1102        2397 :         if (gids == NULL || cnts == NULL)
    1103           0 :                 goto bailout;
    1104             : 
    1105        2397 :         InstrPtr q = dump_2(mb, algebraRef, groupbyRef, gids, cnts);
    1106             : 
    1107        2397 :         if (q) {
    1108        2397 :                 stmt *ns = stmt_create(be->mvc->sa, st_gen_group);
    1109        2397 :                 if (ns == NULL) {
    1110           0 :                         goto bailout;
    1111             :                 }
    1112             : 
    1113        2397 :                 ns->op1 = gids;
    1114        2397 :                 ns->op2 = cnts;
    1115             : 
    1116        2397 :                 ns->nrcols = gids->nrcols;
    1117        2397 :                 ns->key = 0;
    1118        2397 :                 ns->aggr = 0;
    1119        2397 :                 ns->q = q;
    1120        2397 :                 ns->nr = getDestVar(q);
    1121        2397 :                 return ns;
    1122             :         }
    1123           0 :   bailout:
    1124           0 :         if (be->mvc->sa->eb.enabled)
    1125           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    1126             :         return NULL;
    1127             : }
    1128             : 
    1129             : stmt *
    1130       50836 : stmt_mirror(backend *be, stmt *s)
    1131             : {
    1132       50836 :         MalBlkPtr mb = be->mb;
    1133             : 
    1134       50836 :         if (s == NULL)
    1135           0 :                 goto bailout;
    1136             : 
    1137       50836 :         InstrPtr q = dump_1(mb, batRef, mirrorRef, s);
    1138             : 
    1139       50836 :         if (q) {
    1140       50836 :                 stmt *ns = stmt_create(be->mvc->sa, st_mirror);
    1141       50836 :                 if (ns == NULL) {
    1142           0 :                         goto bailout;
    1143             :                 }
    1144             : 
    1145       50836 :                 ns->op1 = s;
    1146       50836 :                 ns->nrcols = 2;
    1147       50836 :                 ns->key = s->key;
    1148       50836 :                 ns->aggr = s->aggr;
    1149       50836 :                 ns->q = q;
    1150       50836 :                 ns->nr = getDestVar(q);
    1151       50836 :                 return ns;
    1152             :         }
    1153           0 :   bailout:
    1154           0 :         if (be->mvc->sa->eb.enabled)
    1155           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    1156             :         return NULL;
    1157             : }
    1158             : 
    1159             : #define MARKJOIN 100
    1160             : stmt *
    1161      319493 : stmt_result(backend *be, stmt *s, int nr)
    1162             : {
    1163      319493 :         stmt *ns;
    1164             : 
    1165      319493 :         if (s == NULL)
    1166             :                 return NULL;
    1167             : 
    1168      319493 :         if (s->type == st_join && s->flag == cmp_joined) {
    1169        5513 :                 if (nr)
    1170        2334 :                         return s->op2;
    1171        3179 :                 return s->op1;
    1172             :         }
    1173             : 
    1174      313980 :         if (s->op1->nr < 0)
    1175             :                 return NULL;
    1176             : 
    1177      313980 :         ns = stmt_create(be->mvc->sa, st_result);
    1178      313979 :         if(!ns) {
    1179             :                 return NULL;
    1180             :         }
    1181      313979 :         if (s->op1->type == st_join && s->op1->flag == cmp_joined) {
    1182           0 :                 assert(0);
    1183      313979 :         } else if (nr) {
    1184      179542 :                 int v = getArg(s->q, nr);
    1185             : 
    1186      179542 :                 assert(s->q->retc > nr);
    1187      179542 :                 ns->nr = v;
    1188             :         } else {
    1189      134437 :                 ns->nr = s->nr;
    1190             :         }
    1191      313979 :         ns->op1 = s;
    1192      313979 :         if (!nr && (s->type == st_order || s->type == st_reorder))
    1193       23695 :                 ns->op4.typeval = *tail_type(s->op1);
    1194      179542 :         else if (nr && ((s->type == st_join && s->flag == MARKJOIN) || (s->type == st_uselect2 && s->flag == MARKJOIN)))
    1195        2935 :                 ns->op4.typeval = *sql_bind_localtype("bit");
    1196             :         else
    1197      287349 :                 ns->op4.typeval = *sql_bind_localtype("oid");
    1198      313981 :         ns->flag = nr;
    1199      313981 :         ns->nrcols = s->nrcols;
    1200      313981 :         ns->key = s->key;
    1201      313981 :         ns->aggr = s->aggr;
    1202      313981 :         return ns;
    1203             : }
    1204             : 
    1205             : 
    1206             : /* limit maybe atom nil */
    1207             : stmt *
    1208       17467 : stmt_limit(backend *be, stmt *col, stmt *piv, stmt *gid, stmt *offset, stmt *limit, int distinct, int dir, int nullslast, int last, int order)
    1209             : {
    1210       17467 :         MalBlkPtr mb = be->mb;
    1211       17467 :         InstrPtr q = NULL;
    1212       17467 :         int l, p, g, c;
    1213             : 
    1214       17467 :         if (col == NULL || offset == NULL || limit == NULL || col->nr < 0 || offset->nr < 0 || limit->nr < 0)
    1215           0 :                 goto bailout;
    1216       17467 :         if (piv && (piv->nr < 0 || gid->nr < 0))
    1217           0 :                 goto bailout;
    1218             : 
    1219       17467 :         c = (col) ? col->nr : 0;
    1220       17467 :         p = (piv) ? piv->nr : 0;
    1221       17467 :         g = (gid) ? gid->nr : 0;
    1222             : 
    1223             :         /* first insert single value into a bat */
    1224       17467 :         if (col->nrcols == 0) {
    1225           0 :                 int k, tt = tail_type(col)->type->localtype;
    1226             : 
    1227           0 :                 q = newStmt(mb, batRef, newRef);
    1228           0 :                 if (q == NULL)
    1229           0 :                         goto bailout;
    1230           0 :                 setVarType(mb, getArg(q, 0), newBatType(tt));
    1231           0 :                 q = pushType(mb, q, tt);
    1232           0 :                 k = getDestVar(q);
    1233           0 :                 pushInstruction(mb, q);
    1234             : 
    1235           0 :                 q = newStmt(mb, batRef, appendRef);
    1236           0 :                 if (q == NULL)
    1237           0 :                         goto bailout;
    1238           0 :                 q = pushArgument(mb, q, k);
    1239           0 :                 q = pushArgument(mb, q, c);
    1240           0 :                 pushInstruction(mb, q);
    1241           0 :                 c = k;
    1242             :         }
    1243       17467 :         if (order) {
    1244         584 :                 int topn = 0;
    1245             : 
    1246         584 :                 q = newStmt(mb, calcRef, plusRef);
    1247         584 :                 if (q == NULL)
    1248           0 :                         goto bailout;
    1249         584 :                 q = pushArgument(mb, q, offset->nr);
    1250         584 :                 q = pushArgument(mb, q, limit->nr);
    1251         584 :                 topn = getDestVar(q);
    1252         584 :                 pushInstruction(mb, q);
    1253             : 
    1254         584 :                 q = newStmtArgs(mb, algebraRef, firstnRef, 9);
    1255         584 :                 if (q == NULL)
    1256           0 :                         goto bailout;
    1257         584 :                 if (!last) /* we need the groups for the next firstn */
    1258         310 :                         q = pushReturn(mb, q, newTmpVariable(mb, TYPE_any));
    1259         584 :                 q = pushArgument(mb, q, c);
    1260         584 :                 if (p)
    1261         310 :                         q = pushArgument(mb, q, p);
    1262             :                 else
    1263         274 :                         q = pushNilBat(mb, q);
    1264         584 :                 if (g)
    1265         310 :                         q = pushArgument(mb, q, g);
    1266             :                 else
    1267         274 :                         q = pushNilBat(mb, q);
    1268         584 :                 q = pushArgument(mb, q, topn);
    1269         584 :                 q = pushBit(mb, q, dir);
    1270         584 :                 q = pushBit(mb, q, nullslast);
    1271         584 :                 q = pushBit(mb, q, distinct != 0);
    1272             : 
    1273         584 :                 l = getArg(q, 0);
    1274         584 :                 l = getDestVar(q);
    1275         584 :                 pushInstruction(mb, q);
    1276             :         } else {
    1277       16883 :                 int len;
    1278             : 
    1279       16883 :                 q = newStmt(mb, calcRef, plusRef);
    1280       16883 :                 if (q == NULL)
    1281           0 :                         goto bailout;
    1282       16883 :                 q = pushArgument(mb, q, offset->nr);
    1283       16883 :                 q = pushArgument(mb, q, limit->nr);
    1284       16883 :                 len = getDestVar(q);
    1285       16883 :                 pushInstruction(mb, q);
    1286             : 
    1287             :                 /* since both arguments of algebra.subslice are
    1288             :                    inclusive correct the LIMIT value by
    1289             :                    subtracting 1 */
    1290       16883 :                 q = newStmt(mb, calcRef, minusRef);
    1291       16883 :                 if (q == NULL)
    1292           0 :                         goto bailout;
    1293       16883 :                 q = pushArgument(mb, q, len);
    1294       16883 :                 q = pushInt(mb, q, 1);
    1295       16883 :                 len = getDestVar(q);
    1296       16883 :                 pushInstruction(mb, q);
    1297             : 
    1298       16883 :                 q = newStmt(mb, algebraRef, subsliceRef);
    1299       16883 :                 if (q == NULL)
    1300           0 :                         goto bailout;
    1301       16883 :                 q = pushArgument(mb, q, c);
    1302       16883 :                 q = pushArgument(mb, q, offset->nr);
    1303       16883 :                 q = pushArgument(mb, q, len);
    1304       16883 :                 l = getDestVar(q);
    1305       16883 :                 pushInstruction(mb, q);
    1306             :         }
    1307             :         /* retrieve the single values again */
    1308       17467 :         if (col->nrcols == 0) {
    1309           0 :                 q = newStmt(mb, algebraRef, findRef);
    1310           0 :                 if (q == NULL)
    1311           0 :                         goto bailout;
    1312           0 :                 q = pushArgument(mb, q, l);
    1313           0 :                 q = pushOid(mb, q, 0);
    1314           0 :                 l = getDestVar(q);
    1315           0 :                 pushInstruction(mb, q);
    1316             :         }
    1317             : 
    1318       34624 :         stmt *ns = stmt_create(be->mvc->sa, piv?st_limit2:st_limit);
    1319       17467 :         if (ns == NULL) {
    1320           0 :                 goto bailout;
    1321             :         }
    1322             : 
    1323       17467 :         ns->op1 = col;
    1324       17467 :         ns->op2 = offset;
    1325       17467 :         ns->op3 = limit;
    1326       17467 :         ns->nrcols = col->nrcols;
    1327       17467 :         ns->key = col->key;
    1328       17467 :         ns->aggr = col->aggr;
    1329       17467 :         ns->q = q;
    1330       17467 :         ns->nr = l;
    1331       17467 :         return ns;
    1332             : 
    1333           0 :   bailout:
    1334           0 :         if (be->mvc->sa->eb.enabled)
    1335           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    1336             :         return NULL;
    1337             : }
    1338             : 
    1339             : stmt *
    1340          21 : stmt_sample(backend *be, stmt *s, stmt *sample, stmt *seed)
    1341             : {
    1342          21 :         MalBlkPtr mb = be->mb;
    1343          21 :         InstrPtr q = NULL;
    1344             : 
    1345          21 :         if (s == NULL || sample == NULL || s->nr < 0 || sample->nr < 0)
    1346           0 :                 goto bailout;
    1347          21 :         q = newStmt(mb, sampleRef, subuniformRef);
    1348          21 :         if (q == NULL)
    1349           0 :                 goto bailout;
    1350          21 :         q = pushArgument(mb, q, s->nr);
    1351          21 :         q = pushArgument(mb, q, sample->nr);
    1352             : 
    1353          21 :         if (seed) {
    1354          12 :                 if (seed->nr < 0)
    1355           0 :                         goto bailout;
    1356             : 
    1357          12 :                 q = pushArgument(mb, q, seed->nr);
    1358             :         }
    1359             : 
    1360          21 :         bool enabled = be->mvc->sa->eb.enabled;
    1361          21 :         be->mvc->sa->eb.enabled = false;
    1362          21 :         stmt *ns = stmt_create(be->mvc->sa, st_sample);
    1363          21 :         be->mvc->sa->eb.enabled = enabled;
    1364          21 :         if (ns == NULL) {
    1365           0 :                 freeInstruction(q);
    1366           0 :                 goto bailout;
    1367             :         }
    1368             : 
    1369          21 :         ns->op1 = s;
    1370          21 :         ns->op2 = sample;
    1371             : 
    1372          21 :         if (seed) {
    1373          12 :                 ns->op3 = seed;
    1374             :         }
    1375             : 
    1376          21 :         ns->nrcols = s->nrcols;
    1377          21 :         ns->key = s->key;
    1378          21 :         ns->aggr = s->aggr;
    1379          21 :         ns->flag = 0;
    1380          21 :         ns->q = q;
    1381          21 :         ns->nr = getDestVar(q);
    1382          21 :         pushInstruction(mb, q);
    1383          21 :         return ns;
    1384             : 
    1385           0 :   bailout:
    1386           0 :         if (be->mvc->sa->eb.enabled)
    1387           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    1388             :         return NULL;
    1389             : }
    1390             : 
    1391             : 
    1392             : stmt *
    1393       10511 : stmt_order(backend *be, stmt *s, int direction, int nullslast)
    1394             : {
    1395       10511 :         MalBlkPtr mb = be->mb;
    1396       10511 :         InstrPtr q = NULL;
    1397             : 
    1398       10511 :         if (s == NULL || s->nr < 0)
    1399           0 :                 goto bailout;
    1400       10511 :         q = newStmt(mb, algebraRef, sortRef);
    1401       10511 :         if (q == NULL)
    1402           0 :                 goto bailout;
    1403             :         /* both ordered result and oid's order en subgroups */
    1404       10511 :         q = pushReturn(mb, q, newTmpVariable(mb, TYPE_any));
    1405       10511 :         q = pushReturn(mb, q, newTmpVariable(mb, TYPE_any));
    1406       10511 :         q = pushArgument(mb, q, s->nr);
    1407       10511 :         q = pushBit(mb, q, !direction);
    1408       10511 :         q = pushBit(mb, q, nullslast);
    1409       10511 :         q = pushBit(mb, q, FALSE);
    1410             : 
    1411       10511 :         bool enabled = be->mvc->sa->eb.enabled;
    1412       10511 :         be->mvc->sa->eb.enabled = false;
    1413       10511 :         stmt *ns = stmt_create(be->mvc->sa, st_order);
    1414       10511 :         be->mvc->sa->eb.enabled = enabled;
    1415       10511 :         if (ns == NULL) {
    1416           0 :                 freeInstruction(q);
    1417           0 :                 goto bailout;
    1418             :         }
    1419             : 
    1420       10511 :         ns->op1 = s;
    1421       10511 :         ns->flag = direction;
    1422       10511 :         ns->nrcols = s->nrcols;
    1423       10511 :         ns->key = s->key;
    1424       10511 :         ns->aggr = s->aggr;
    1425       10511 :         ns->q = q;
    1426       10511 :         ns->nr = getDestVar(q);
    1427       10511 :         pushInstruction(mb, q);
    1428       10511 :         return ns;
    1429             : 
    1430           0 :   bailout:
    1431           0 :         if (be->mvc->sa->eb.enabled)
    1432           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    1433             :         return NULL;
    1434             : }
    1435             : 
    1436             : stmt *
    1437       13699 : stmt_reorder(backend *be, stmt *s, int direction, int nullslast, stmt *orderby_ids, stmt *orderby_grp)
    1438             : {
    1439       13699 :         MalBlkPtr mb = be->mb;
    1440       13699 :         InstrPtr q = NULL;
    1441             : 
    1442       13699 :         if (s == NULL || orderby_ids == NULL || orderby_grp == NULL || s->nr < 0 || orderby_ids->nr < 0 || orderby_grp->nr < 0)
    1443           0 :                 goto bailout;
    1444       13699 :         q = newStmtArgs(mb, algebraRef, sortRef, 9);
    1445       13699 :         if (q == NULL)
    1446           0 :                 goto bailout;
    1447             :         /* both ordered result and oid's order en subgroups */
    1448       13699 :         q = pushReturn(mb, q, newTmpVariable(mb, TYPE_any));
    1449       13699 :         q = pushReturn(mb, q, newTmpVariable(mb, TYPE_any));
    1450       13699 :         q = pushArgument(mb, q, s->nr);
    1451       13699 :         q = pushArgument(mb, q, orderby_ids->nr);
    1452       13699 :         q = pushArgument(mb, q, orderby_grp->nr);
    1453       13699 :         q = pushBit(mb, q, !direction);
    1454       13699 :         q = pushBit(mb, q, nullslast);
    1455       13699 :         q = pushBit(mb, q, FALSE);
    1456             : 
    1457       13699 :         bool enabled = be->mvc->sa->eb.enabled;
    1458       13699 :         be->mvc->sa->eb.enabled = false;
    1459       13699 :         stmt *ns = stmt_create(be->mvc->sa, st_reorder);
    1460       13699 :         be->mvc->sa->eb.enabled = enabled;
    1461       13699 :         if (ns == NULL) {
    1462           0 :                 freeInstruction(q);
    1463           0 :                 goto bailout;
    1464             :         }
    1465             : 
    1466       13699 :         ns->op1 = s;
    1467       13699 :         ns->op2 = orderby_ids;
    1468       13699 :         ns->op3 = orderby_grp;
    1469       13699 :         ns->flag = direction;
    1470       13699 :         ns->nrcols = s->nrcols;
    1471       13699 :         ns->key = s->key;
    1472       13699 :         ns->aggr = s->aggr;
    1473       13699 :         ns->nr = getDestVar(q);
    1474       13699 :         ns->q = q;
    1475       13699 :         pushInstruction(mb, q);
    1476       13699 :         return ns;
    1477             : 
    1478           0 :   bailout:
    1479           0 :         if (be->mvc->sa->eb.enabled)
    1480           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    1481             :         return NULL;
    1482             : }
    1483             : 
    1484             : stmt *
    1485     3292553 : stmt_atom(backend *be, atom *a)
    1486             : {
    1487     3292553 :         MalBlkPtr mb = be->mb;
    1488             : 
    1489     3292553 :         if (a == NULL)
    1490           0 :                 goto bailout;
    1491             : 
    1492     3292553 :         InstrPtr q = EC_TEMP_FRAC(atom_type(a)->type->eclass) ? newStmt(mb, calcRef, atom_type(a)->type->impl) : newAssignment(mb);
    1493             : 
    1494     3292683 :         if (q == NULL)
    1495           0 :                 goto bailout;
    1496     3292683 :         if (atom_null(a)) {
    1497      151572 :                 q = pushNil(mb, q, atom_type(a)->type->localtype);
    1498             :         } else {
    1499     3141111 :                 int k;
    1500     3141111 :                 if ((k = constantAtom(be, mb, a)) == -1) {
    1501           0 :                         freeInstruction(q);
    1502           0 :                         goto bailout;
    1503             :                 }
    1504     3140804 :                 q = pushArgument(mb, q, k);
    1505             :         }
    1506             :         /* digits of the result timestamp/daytime */
    1507     3292373 :         if (EC_TEMP_FRAC(atom_type(a)->type->eclass))
    1508       47456 :                 q = pushInt(mb, q, atom_type(a)->digits);
    1509     3292333 :         bool enabled = be->mvc->sa->eb.enabled;
    1510     3292333 :         be->mvc->sa->eb.enabled = false;
    1511     3292333 :         stmt *s = stmt_create(be->mvc->sa, st_atom);
    1512     3292300 :         be->mvc->sa->eb.enabled = enabled;
    1513     3292300 :         if (s == NULL) {
    1514           0 :                 freeInstruction(q);
    1515           0 :                 goto bailout;
    1516             :         }
    1517             : 
    1518     3292300 :         s->op4.aval = a;
    1519     3292300 :         s->key = 1;          /* values are also unique */
    1520     3292300 :         s->q = q;
    1521     3292300 :         s->nr = getDestVar(q);
    1522     3292300 :         pushInstruction(mb, q);
    1523     3292300 :         return s;
    1524             : 
    1525           0 :   bailout:
    1526           0 :         if (be->mvc->sa->eb.enabled)
    1527           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    1528             :         return NULL;
    1529             : }
    1530             : 
    1531             : stmt *
    1532        3239 : stmt_genselect(backend *be, stmt *lops, stmt *rops, sql_subfunc *f, stmt *sub, int anti)
    1533             : {
    1534        3239 :         MalBlkPtr mb = be->mb;
    1535        3239 :         InstrPtr q = NULL;
    1536        3239 :         const char *mod, *op;
    1537        3239 :         node *n;
    1538        3239 :         int k;
    1539             : 
    1540        3239 :         if (lops == NULL || rops == NULL)
    1541           0 :                 goto bailout;
    1542             : 
    1543        3239 :         if (backend_create_subfunc(be, f, NULL) < 0)
    1544           0 :                 goto bailout;
    1545        3239 :         op = backend_function_imp(be, f->func);
    1546        3239 :         mod = sql_func_mod(f->func);
    1547             : 
    1548        3239 :         if (rops->nrcols >= 1) {
    1549          54 :                 bit need_not = FALSE;
    1550             : 
    1551          54 :                 int narg = 3;
    1552         108 :                 for (n = lops->op4.lval->h; n; n = n->next)
    1553          54 :                         narg++;
    1554         171 :                 for (n = rops->op4.lval->h; n; n = n->next)
    1555         117 :                         narg++;
    1556          54 :                 q = newStmtArgs(mb, malRef, multiplexRef, narg);
    1557          54 :                 if (q == NULL)
    1558           0 :                         goto bailout;
    1559          54 :                 setVarType(mb, getArg(q, 0), newBatType(TYPE_bit));
    1560          54 :                 q = pushStr(mb, q, convertMultiplexMod(mod, op));
    1561          54 :                 q = pushStr(mb, q, convertMultiplexFcn(op));
    1562         108 :                 for (n = lops->op4.lval->h; n; n = n->next) {
    1563          54 :                         stmt *op = n->data;
    1564             : 
    1565          54 :                         q = pushArgument(mb, q, op->nr);
    1566             :                 }
    1567         171 :                 for (n = rops->op4.lval->h; n; n = n->next) {
    1568         117 :                         stmt *op = n->data;
    1569             : 
    1570         117 :                         q = pushArgument(mb, q, op->nr);
    1571             :                 }
    1572          54 :                 k = getDestVar(q);
    1573          54 :                 pushInstruction(mb, q);
    1574             : 
    1575          54 :                 q = newStmtArgs(mb, algebraRef, selectRef, 9);
    1576          54 :                 if (q == NULL)
    1577           0 :                         goto bailout;
    1578          54 :                 q = pushArgument(mb, q, k);
    1579          54 :                 if (sub)
    1580          37 :                         q = pushArgument(mb, q, sub->nr);
    1581          54 :                 q = pushBit(mb, q, !need_not);
    1582          54 :                 q = pushBit(mb, q, !need_not);
    1583          54 :                 q = pushBit(mb, q, TRUE);
    1584          54 :                 q = pushBit(mb, q, TRUE);
    1585          54 :                 q = pushBit(mb, q, anti);
    1586             :         } else {
    1587        3185 :                 node *n;
    1588             : 
    1589        3185 :                 op = sa_strconcat(be->mvc->sa, op, selectRef);
    1590        3185 :                 q = newStmtArgs(mb, mod, convertMultiplexFcn(op), 9);
    1591        3185 :                 if (q == NULL)
    1592           0 :                         goto bailout;
    1593             :                 // push pointer to the SQL structure into the MAL call
    1594             :                 // allows getting argument names for example
    1595        3185 :                 if (LANG_EXT(f->func->lang))
    1596           0 :                         q = pushPtr(mb, q, f->func); // nothing to see here, please move along
    1597             :                 // f->query contains the R code to be run
    1598        3185 :                 if (f->func->lang == FUNC_LANG_R || f->func->lang >= FUNC_LANG_PY)
    1599           0 :                         q = pushStr(mb, q, f->func->query);
    1600             : 
    1601        6370 :                 for (n = lops->op4.lval->h; n; n = n->next) {
    1602        3185 :                         stmt *op = n->data;
    1603             : 
    1604        3185 :                         q = pushArgument(mb, q, op->nr);
    1605             :                 }
    1606             :                 /* candidate lists */
    1607        3185 :                 if (sub)
    1608        3116 :                         q = pushArgument(mb, q, sub->nr);
    1609             :                 else
    1610          69 :                         q = pushNilBat(mb, q);
    1611             : 
    1612       12723 :                 for (n = rops->op4.lval->h; n; n = n->next) {
    1613        9538 :                         stmt *op = n->data;
    1614             : 
    1615        9538 :                         q = pushArgument(mb, q, op->nr);
    1616             :                 }
    1617             : 
    1618        3185 :                 q = pushBit(mb, q, anti);
    1619             :         }
    1620             : 
    1621        3239 :         bool enabled = be->mvc->sa->eb.enabled;
    1622        3239 :         be->mvc->sa->eb.enabled = false;
    1623        3239 :         stmt *s = stmt_create(be->mvc->sa, st_uselect);
    1624        3239 :         be->mvc->sa->eb.enabled = enabled;
    1625        3239 :         if (s == NULL) {
    1626           0 :                 freeInstruction(q);
    1627           0 :                 goto bailout;
    1628             :         }
    1629             : 
    1630        3239 :         s->op1 = lops;
    1631        3239 :         s->op2 = rops;
    1632        3239 :         s->op3 = sub;
    1633        3239 :         s->key = lops->nrcols == 0 && rops->nrcols == 0;
    1634        3239 :         s->flag = cmp_filter;
    1635        3239 :         s->nrcols = lops->nrcols;
    1636        3239 :         s->nr = getDestVar(q);
    1637        3239 :         s->q = q;
    1638        3239 :         s->cand = sub;
    1639        3239 :         pushInstruction(mb, q);
    1640        3239 :         return s;
    1641             : 
    1642           0 :   bailout:
    1643           0 :         if (be->mvc->sa->eb.enabled)
    1644           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    1645             :         return NULL;
    1646             : }
    1647             : 
    1648             : stmt *
    1649      214539 : stmt_uselect(backend *be, stmt *op1, stmt *op2, comp_type cmptype, stmt *sub, int anti, int is_semantics)
    1650             : {
    1651      214539 :         MalBlkPtr mb = be->mb;
    1652      214539 :         InstrPtr q = NULL;
    1653      214539 :         int l, r;
    1654      214539 :         stmt *sel = sub;
    1655             : 
    1656      214539 :         if (op1 == NULL || op2 == NULL || op1->nr < 0 || op2->nr < 0 || (sub && sub->nr < 0))
    1657           0 :                 goto bailout;
    1658      214539 :         l = op1->nr;
    1659      214539 :         r = op2->nr;
    1660             : 
    1661      214539 :         if (op2->nrcols >= 1 && op1->nrcols == 0) { /* swap */
    1662          79 :                 stmt *v = op1;
    1663          79 :                 op1 = op2;
    1664          79 :                 op2 = v;
    1665          79 :                 int n = l;
    1666          79 :                 l = r;
    1667          79 :                 r = n;
    1668          79 :                 cmptype = swap_compare(cmptype);
    1669             :         }
    1670      214539 :         if (op2->nrcols >= 1) {
    1671       17825 :                 bit need_not = FALSE;
    1672       17825 :                 const char *mod = calcRef;
    1673       17825 :                 const char *op = "=";
    1674       17825 :                 int k;
    1675             : 
    1676       17825 :                 switch (cmptype) {
    1677             :                 case cmp_equal:
    1678             :                         op = "=";
    1679             :                         break;
    1680             :                 case cmp_notequal:
    1681       17825 :                         op = "!=";
    1682             :                         break;
    1683         917 :                 case cmp_lt:
    1684         917 :                         op = "<";
    1685         917 :                         break;
    1686         775 :                 case cmp_lte:
    1687         775 :                         op = "<=";
    1688         775 :                         break;
    1689        4831 :                 case cmp_gt:
    1690        4831 :                         op = ">";
    1691        4831 :                         break;
    1692         800 :                 case cmp_gte:
    1693         800 :                         op = ">=";
    1694         800 :                         break;
    1695           0 :                 default:
    1696           0 :                         TRC_ERROR(SQL_EXECUTION, "Unknown operator\n");
    1697             :                 }
    1698             : 
    1699       28060 :                 if ((q = multiplex2(mb, mod, convertMultiplexFcn(op), l, r, TYPE_bit)) == NULL)
    1700           0 :                         goto bailout;
    1701       17825 :                 if (sub && (op1->cand || op2->cand)) {
    1702        1694 :                         if (op1->cand && !op2->cand) {
    1703          30 :                                 if (op1->nrcols > 0)
    1704          30 :                                         q = pushNilBat(mb, q);
    1705          30 :                                 q = pushArgument(mb, q, sub->nr);
    1706        1664 :                         } else if (!op1->cand && op2->cand) {
    1707        1629 :                                 q = pushArgument(mb, q, sub->nr);
    1708        1629 :                                 if (op2->nrcols > 0)
    1709        1629 :                                         q = pushNilBat(mb, q);
    1710             :                         }
    1711             :                         sub = NULL;
    1712             :                 }
    1713       17825 :                 if (is_semantics)
    1714        2972 :                         q = pushBit(mb, q, TRUE);
    1715       17825 :                 k = getDestVar(q);
    1716             : 
    1717       17825 :                 q = newStmtArgs(mb, algebraRef, selectRef, 9);
    1718       17825 :                 if (q == NULL)
    1719           0 :                         goto bailout;
    1720       17825 :                 q = pushArgument(mb, q, k);
    1721       17825 :                 if (sub)
    1722        4364 :                         q = pushArgument(mb, q, sub->nr);
    1723       17825 :                 q = pushBit(mb, q, !need_not);
    1724       17825 :                 q = pushBit(mb, q, !need_not);
    1725       17825 :                 q = pushBit(mb, q, TRUE);
    1726       17825 :                 q = pushBit(mb, q, TRUE);
    1727       17825 :                 q = pushBit(mb, q, anti);
    1728       17825 :                 k = getDestVar(q);
    1729             :         } else {
    1730      196714 :                 assert (cmptype != cmp_filter);
    1731      196714 :                 if (is_semantics) {
    1732       19575 :                         assert(cmptype == cmp_equal || cmptype == cmp_notequal);
    1733       19575 :                         if (cmptype == cmp_notequal)
    1734          73 :                                 anti = !anti;
    1735       19575 :                         q = newStmtArgs(mb, algebraRef, selectRef, 9);
    1736       19575 :                         if (q == NULL)
    1737           0 :                                 goto bailout;
    1738       19575 :                         q = pushArgument(mb, q, l);
    1739       19575 :                         if (sub && !op1->cand) {
    1740        1447 :                                 q = pushArgument(mb, q, sub->nr);
    1741             :                         } else {
    1742          14 :                                 assert(!sub || op1->cand == sub);
    1743             :                                 sub = NULL;
    1744             :                         }
    1745       19575 :                         q = pushArgument(mb, q, r);
    1746       19575 :                         q = pushArgument(mb, q, r);
    1747       19575 :                         q = pushBit(mb, q, TRUE);
    1748       19575 :                         q = pushBit(mb, q, TRUE);
    1749       19575 :                         q = pushBit(mb, q, anti);
    1750             :                 } else {
    1751      177139 :                         q = newStmt(mb, algebraRef, thetaselectRef);
    1752      177139 :                         if (q == NULL)
    1753           0 :                                 goto bailout;
    1754      177139 :                         q = pushArgument(mb, q, l);
    1755      177139 :                         if (sub && !op1->cand) {
    1756      126594 :                                 q = pushArgument(mb, q, sub->nr);
    1757             :                         } else {
    1758         610 :                                 assert(!sub || op1->cand == sub);
    1759       50545 :                                 q = pushNilBat(mb, q);
    1760       50545 :                                 sub = NULL;
    1761             :                         }
    1762      177139 :                         q = pushArgument(mb, q, r);
    1763      177139 :                         switch (cmptype) {
    1764      141445 :                         case cmp_equal:
    1765      141445 :                                 q = pushStr(mb, q, anti?"!=":"==");
    1766      141445 :                                 break;
    1767       32544 :                         case cmp_notequal:
    1768       32544 :                                 q = pushStr(mb, q, anti?"==":"!=");
    1769       32544 :                                 break;
    1770         365 :                         case cmp_lt:
    1771         365 :                                 q = pushStr(mb, q, anti?">=":"<");
    1772         365 :                                 break;
    1773          73 :                         case cmp_lte:
    1774          73 :                                 q = pushStr(mb, q, anti?">":"<=");
    1775          73 :                                 break;
    1776        2433 :                         case cmp_gt:
    1777        2433 :                                 q = pushStr(mb, q, anti?"<=":">");
    1778        2433 :                                 break;
    1779         279 :                         case cmp_gte:
    1780         279 :                                 q = pushStr(mb, q, anti?"<":">=");
    1781         279 :                                 break;
    1782           0 :                         default:
    1783           0 :                                 TRC_ERROR(SQL_EXECUTION, "Impossible select compare\n");
    1784           0 :                                 if (q)
    1785           0 :                                         freeInstruction(q);
    1786           0 :                                 q = NULL;
    1787           0 :                                 goto bailout;
    1788             :                         }
    1789             :                 }
    1790             :         }
    1791             : 
    1792      214539 :         bool enabled = be->mvc->sa->eb.enabled;
    1793      214539 :         be->mvc->sa->eb.enabled = false;
    1794      214539 :         stmt *s = stmt_create(be->mvc->sa, st_uselect);
    1795      214539 :         be->mvc->sa->eb.enabled = enabled;
    1796      214539 :         if (s == NULL) {
    1797           0 :                 freeInstruction(q);
    1798           0 :                 goto bailout;
    1799             :         }
    1800             : 
    1801      214539 :         s->op1 = op1;
    1802      214539 :         s->op2 = op2;
    1803      214539 :         s->op3 = sub;
    1804      214539 :         s->flag = cmptype;
    1805      214539 :         s->key = op1->nrcols == 0 && op2->nrcols == 0;
    1806      214539 :         s->nrcols = op1->nrcols;
    1807      214539 :         s->nr = getDestVar(q);
    1808      214539 :         s->q = q;
    1809      214539 :         s->cand = sub;
    1810      214539 :         pushInstruction(mb, q);
    1811      214539 :         if (!sub && sel) /* project back the old ids */
    1812        2318 :                 return stmt_project(be, s, sel);
    1813             :         return s;
    1814             : 
    1815           0 :   bailout:
    1816           0 :         if (be->mvc->sa->eb.enabled)
    1817           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    1818             :         return NULL;
    1819             : }
    1820             : 
    1821             : /*
    1822             : static int
    1823             : range_join_convertable(stmt *s, stmt **base, stmt **L, stmt **H)
    1824             : {
    1825             :         int ls = 0, hs = 0;
    1826             :         stmt *l = NULL, *h = NULL;
    1827             :         stmt *bl = s->op2, *bh = s->op3;
    1828             :         int tt = tail_type(s->op2)->type->localtype;
    1829             : 
    1830             : #ifdef HAVE_HGE
    1831             :         if (tt > TYPE_hge)
    1832             : #else
    1833             :         if (tt > TYPE_lng)
    1834             : #endif
    1835             :                 return 0;
    1836             :         if (s->op2->type == st_Nop && list_length(s->op2->op1->op4.lval) == 2) {
    1837             :                 bl = s->op2->op1->op4.lval->h->data;
    1838             :                 l = s->op2->op1->op4.lval->t->data;
    1839             :         }
    1840             :         if (s->op3->type == st_Nop && list_length(s->op3->op1->op4.lval) == 2) {
    1841             :                 bh = s->op3->op1->op4.lval->h->data;
    1842             :                 h = s->op3->op1->op4.lval->t->data;
    1843             :         }
    1844             : 
    1845             :         if (((ls = (l && strcmp(s->op2->op4.funcval->func->base.name, "sql_sub") == 0 && l->nrcols == 0)) || (hs = (h && strcmp(s->op3->op4.funcval->func->base.name, "sql_add") == 0 && h->nrcols == 0))) && (ls || hs) && bl == bh) {
    1846             :                 *base = bl;
    1847             :                 *L = l;
    1848             :                 *H = h;
    1849             :                 return 1;
    1850             :         }
    1851             :         return 0;
    1852             : }
    1853             : 
    1854             : static int
    1855             : argumentZero(MalBlkPtr mb, int tpe)
    1856             : {
    1857             :         ValRecord cst;
    1858             :         str msg;
    1859             : 
    1860             :         cst.vtype = TYPE_int;
    1861             :         cst.val.ival = 0;
    1862             :         msg = convertConstant(tpe, &cst);
    1863             :         if( msg)
    1864             :                 freeException(msg); // will not be called
    1865             :         return defConstant(mb, tpe, &cst);
    1866             : }
    1867             : */
    1868             : 
    1869             : static InstrPtr
    1870        5103 : select2_join2(backend *be, stmt *op1, stmt *op2, stmt *op3, int cmp, stmt **Sub, int anti, int symmetric, int swapped, int type, int reduce)
    1871             : {
    1872        5103 :         MalBlkPtr mb = be->mb;
    1873        5103 :         InstrPtr p, q;
    1874        5103 :         int l;
    1875        5103 :         const char *cmd = (type == st_uselect2) ? selectRef : rangejoinRef;
    1876        5103 :         stmt *sub = (Sub)?*Sub:NULL;
    1877             : 
    1878        5103 :         if (op1 == NULL || op2 == NULL || op3 == NULL || op1->nr < 0 || (sub && sub->nr < 0))
    1879           0 :                 goto bailout;
    1880        5103 :         l = op1->nr;
    1881        8475 :         if ((symmetric || op2->nrcols > 0 || op3->nrcols > 0 || !reduce) && (type == st_uselect2)) {
    1882        3597 :                 int k;
    1883        3597 :                 int nrcols = (op1->nrcols || op2->nrcols || op3->nrcols);
    1884             : 
    1885        3597 :                 if (op2->nr < 0 || op3->nr < 0)
    1886           0 :                         goto bailout;
    1887             : 
    1888        3597 :                 if (nrcols)
    1889        3556 :                         p = newStmtArgs(mb, batcalcRef, betweenRef, 12);
    1890             :                 else
    1891          41 :                         p = newStmtArgs(mb, calcRef, betweenRef, 9);
    1892        3597 :                 if (p == NULL)
    1893           0 :                         goto bailout;
    1894        3597 :                 p = pushArgument(mb, p, l);
    1895        3597 :                 p = pushArgument(mb, p, op2->nr);
    1896        3597 :                 p = pushArgument(mb, p, op3->nr);
    1897             : 
    1898             :                 /* cands */
    1899        3597 :                 if ((sub && !reduce) || op1->cand || op2->cand || op3->cand) { /* some already handled the previous selection */
    1900        1568 :                         if (op1->cand && op1->nrcols)
    1901        1287 :                                 p = pushNilBat(mb, p);
    1902         281 :                         else if (op1->nrcols)
    1903         281 :                                 p = pushArgument(mb, p, sub->nr);
    1904        1568 :                         if (op2->nrcols) {
    1905        1466 :                                 if (op2->cand)
    1906        1220 :                                         p = pushNilBat(mb, p);
    1907         246 :                                 else if (op2->nrcols)
    1908         246 :                                         p = pushArgument(mb, p, sub->nr);
    1909             :                         }
    1910        1568 :                         if (op3->nrcols) {
    1911        1461 :                                 if (op3->cand)
    1912        1336 :                                         p = pushNilBat(mb, p);
    1913         125 :                                 else if (op3->nrcols)
    1914         125 :                                         p = pushArgument(mb, p, sub->nr);
    1915             :                         }
    1916             :                         sub = NULL;
    1917             :                 }
    1918             : 
    1919        3597 :                 p = pushBit(mb, p, (symmetric)?TRUE:FALSE); /* symmetric */
    1920        3597 :                 p = pushBit(mb, p, (cmp & 1) != 0);     /* lo inclusive */
    1921        3597 :                 p = pushBit(mb, p, (cmp & 2) != 0);     /* hi inclusive */
    1922        3597 :                 p = pushBit(mb, p, FALSE);                  /* nils_false */
    1923        3597 :                 p = pushBit(mb, p, (anti)?TRUE:FALSE);      /* anti */
    1924        3597 :                 pushInstruction(mb, p);
    1925        3597 :                 if (!reduce)
    1926             :                         return p;
    1927        3372 :                 k = getDestVar(p);
    1928             : 
    1929        3372 :                 q = newStmtArgs(mb, algebraRef, selectRef, 9);
    1930        3372 :                 if (q == NULL)
    1931           0 :                         goto bailout;
    1932        3372 :                 q = pushArgument(mb, q, k);
    1933        3372 :                 if (sub)
    1934         896 :                         q = pushArgument(mb, q, sub->nr);
    1935        3372 :                 q = pushBit(mb, q, TRUE);
    1936        3372 :                 q = pushBit(mb, q, TRUE);
    1937        3372 :                 q = pushBit(mb, q, TRUE);
    1938        3372 :                 q = pushBit(mb, q, TRUE);
    1939        3372 :                 q = pushBit(mb, q, FALSE);
    1940        3372 :                 pushInstruction(mb, q);
    1941             :         } else {
    1942             :                 /* if st_join2 try to convert to bandjoin */
    1943             :                 /* ie check if we subtract/add a constant, to the
    1944             :                 same column */
    1945             :                 /* move this optimization into the relational phase! */
    1946             :         /*
    1947             :                 stmt *base, *low = NULL, *high = NULL;
    1948             :                 if (type == st_join2 && range_join_convertable(s, &base, &low, &high)) {
    1949             :                         int tt = tail_type(base)->type->localtype;
    1950             : 
    1951             :                         if ((rs = _dumpstmt(sql, mb, base)) < 0)
    1952             :                                 return -1;
    1953             :                         if (low) {
    1954             :                                 if ((r1 = _dumpstmt(sql, mb, low)) < 0)
    1955             :                                         return -1;
    1956             :                         } else
    1957             :                                 r1 = argumentZero(mb, tt);
    1958             :                         if (high) {
    1959             :                                 if ((r2 = _dumpstmt(sql, mb, high)) < 0)
    1960             :                                         return -1;
    1961             :                         } else
    1962             :                                 r2 = argumentZero(mb, tt);
    1963             :                         cmd = bandjoinRef;
    1964             :                 }
    1965             :         */
    1966             : 
    1967        1506 :                 int r1 = op2->nr;
    1968        1506 :                 int r2 = op3->nr;
    1969        1506 :                 int rs = 0;
    1970        1506 :                 q = newStmtArgs(mb, algebraRef, cmd, 12);
    1971        1506 :                 if (q == NULL)
    1972           0 :                         goto bailout;
    1973        1506 :                 if (type == st_join2)
    1974          54 :                         q = pushReturn(mb, q, newTmpVariable(mb, TYPE_any));
    1975        1506 :                 q = pushArgument(mb, q, l);
    1976        1506 :                 if (sub) {
    1977        1187 :                         int cand = op1->cand || op2->cand || op3->cand;
    1978        1180 :                         if (cand) {
    1979           7 :                                 assert(!op1->nrcols || op1->cand);
    1980           7 :                                 assert(!op2->nrcols || op2->cand);
    1981           7 :                                 assert(!op3->nrcols || op3->cand);
    1982             :                                 sub = NULL;
    1983             :                         }
    1984             :                 }
    1985        1180 :                 if (sub) /* only for uselect2 */
    1986        1180 :                         q = pushArgument(mb, q, sub->nr);
    1987        1506 :                 if (rs) {
    1988             :                         q = pushArgument(mb, q, rs);
    1989             :                 } else {
    1990        1506 :                         q = pushArgument(mb, q, r1);
    1991        1506 :                         q = pushArgument(mb, q, r2);
    1992             :                 }
    1993        1506 :                 if (type == st_join2) {
    1994          54 :                         q = pushNilBat(mb, q);
    1995          54 :                         q = pushNilBat(mb, q);
    1996             :                 }
    1997             : 
    1998        1506 :                 switch (cmp & 3) {
    1999          59 :                 case 0:
    2000          59 :                         q = pushBit(mb, q, FALSE);
    2001          59 :                         q = pushBit(mb, q, FALSE);
    2002          59 :                         break;
    2003         228 :                 case 1:
    2004         228 :                         q = pushBit(mb, q, TRUE);
    2005         228 :                         q = pushBit(mb, q, FALSE);
    2006         228 :                         break;
    2007          11 :                 case 2:
    2008          11 :                         q = pushBit(mb, q, FALSE);
    2009          11 :                         q = pushBit(mb, q, TRUE);
    2010          11 :                         break;
    2011        1208 :                 case 3:
    2012        1208 :                         q = pushBit(mb, q, TRUE);
    2013        1208 :                         q = pushBit(mb, q, TRUE);
    2014        1208 :                         break;
    2015             :                 }
    2016        1506 :                 q = pushBit(mb, q, anti);
    2017        1506 :                 if (type == st_uselect2) {
    2018        1452 :                         q = pushBit(mb, q, TRUE); /* all nil's are != */
    2019             :                 } else {
    2020          54 :                         q = pushBit(mb, q, (symmetric)?TRUE:FALSE);
    2021             :                 }
    2022        1506 :                 if (type == st_join2)
    2023          54 :                         q = pushNil(mb, q, TYPE_lng); /* estimate */
    2024        1506 :                 pushInstruction(mb, q);
    2025        1506 :                 if (swapped) {
    2026           2 :                         InstrPtr r = newInstruction(mb,  NULL, NULL);
    2027           2 :                         if (r == NULL)
    2028           0 :                                 goto bailout;
    2029           2 :                         getArg(r, 0) = newTmpVariable(mb, TYPE_any);
    2030           2 :                         r = pushReturn(mb, r, newTmpVariable(mb, TYPE_any));
    2031           2 :                         r = pushArgument(mb, r, getArg(q,1));
    2032           2 :                         r = pushArgument(mb, r, getArg(q,0));
    2033           2 :                         pushInstruction(mb, r);
    2034           2 :                         q = r;
    2035             :                 }
    2036             :         }
    2037        4878 :         if (Sub)
    2038        4824 :                 *Sub = sub;
    2039             :         return q;
    2040             : 
    2041           0 :   bailout:
    2042           0 :         if (be->mvc->sa->eb.enabled)
    2043           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    2044             :         return NULL;
    2045             : }
    2046             : 
    2047             : stmt *
    2048          38 : stmt_outerselect(backend *be, stmt *g, stmt *m, stmt *p, bool any)
    2049             : {
    2050          38 :         MalBlkPtr mb = be->mb;
    2051          38 :         InstrPtr q;
    2052             : 
    2053          38 :         q = newStmtArgs(mb, algebraRef, outerselectRef, 6);
    2054          38 :         q = pushReturn(mb, q, newTmpVariable(mb, TYPE_any));
    2055          38 :         q = pushArgument(mb, q, g->nr); /* group ids */
    2056          38 :         q = pushArgument(mb, q, m->nr); /* mark flag */
    2057          38 :         q = pushArgument(mb, q, p->nr); /* predicate */
    2058          38 :         q = pushBit(mb, q, (any)?TRUE:FALSE);
    2059          38 :         pushInstruction(mb, q);
    2060             : 
    2061          38 :         if (!q)
    2062             :                 return NULL;
    2063          38 :         stmt *s = stmt_create(be->mvc->sa, st_uselect2);
    2064          38 :         if (s == NULL) {
    2065           0 :                 freeInstruction(q);
    2066           0 :                 return NULL;
    2067             :         }
    2068             : 
    2069          38 :         s->op1 = g;
    2070          38 :         s->op2 = m;
    2071          38 :         s->flag = MARKJOIN;
    2072          38 :         s->key = 0;
    2073          38 :         s->nrcols = g->nrcols;
    2074          38 :         s->nr = getDestVar(q);
    2075          38 :         s->q = q;
    2076          38 :         return s;
    2077             : }
    2078             : 
    2079             : stmt *
    2080         402 : stmt_markselect(backend *be, stmt *g, stmt *m, stmt *p, bool any)
    2081             : {
    2082         402 :         MalBlkPtr mb = be->mb;
    2083         402 :         InstrPtr q;
    2084             : 
    2085         402 :         q = newStmtArgs(mb, algebraRef, markselectRef, 6);
    2086         402 :         q = pushReturn(mb, q, newTmpVariable(mb, TYPE_any));
    2087         402 :         q = pushArgument(mb, q, g->nr); /* left ids */
    2088         402 :         q = pushArgument(mb, q, m->nr); /* mark info mask */
    2089         402 :         q = pushArgument(mb, q, p->nr);      /* predicate */
    2090         402 :         q = pushBit(mb, q, (any)?TRUE:FALSE);
    2091         402 :         pushInstruction(mb, q);
    2092             : 
    2093         402 :         if (!q)
    2094             :                 return NULL;
    2095         402 :         stmt *s = stmt_create(be->mvc->sa, st_uselect2);
    2096         402 :         if (s == NULL) {
    2097           0 :                 freeInstruction(q);
    2098           0 :                 return NULL;
    2099             :         }
    2100             : 
    2101         402 :         s->op1 = g;
    2102         402 :         s->op2 = m;
    2103         402 :         s->flag = MARKJOIN;
    2104         402 :         s->key = 0;
    2105         402 :         s->nrcols = g->nrcols;
    2106         402 :         s->nr = getDestVar(q);
    2107         402 :         s->q = q;
    2108         402 :         return s;
    2109             : }
    2110             : 
    2111             : stmt *
    2112        2490 : stmt_markjoin(backend *be, stmt *l, stmt *r, bool final)
    2113             : {
    2114        2490 :         MalBlkPtr mb = be->mb;
    2115        2490 :         InstrPtr q;
    2116             : 
    2117        2490 :         q = newStmtArgs(mb, algebraRef, markjoinRef, 8);
    2118        2490 :         q = pushReturn(mb, q, newTmpVariable(mb, TYPE_any));
    2119        2490 :         if (!final)
    2120           5 :                 q = pushReturn(mb, q, newTmpVariable(mb, TYPE_any));
    2121        2490 :         q = pushArgument(mb, q, l->nr); /* left ids */
    2122        2490 :         q = pushArgument(mb, q, r->nr); /* mark info mask */
    2123        2490 :         q = pushNilBat(mb, q);
    2124        2490 :         q = pushNilBat(mb, q);
    2125        2490 :         q = pushNil(mb, q, TYPE_lng);
    2126        2490 :         pushInstruction(mb, q);
    2127             : 
    2128        2490 :         if (!q)
    2129             :                 return NULL;
    2130        2490 :         stmt *s = stmt_create(be->mvc->sa, st_join);
    2131        2490 :         if (s == NULL) {
    2132           0 :                 freeInstruction(q);
    2133           0 :                 return NULL;
    2134             :         }
    2135             : 
    2136        2490 :         s->op1 = l;
    2137        2490 :         s->op2 = r;
    2138        2490 :         s->flag = MARKJOIN;
    2139        2490 :         s->key = 0;
    2140        2490 :         s->nrcols = l->nrcols;
    2141        2490 :         s->nr = getDestVar(q);
    2142        2490 :         s->q = q;
    2143        2490 :         return s;
    2144             : }
    2145             : 
    2146             : stmt *
    2147        5049 : stmt_uselect2(backend *be, stmt *op1, stmt *op2, stmt *op3, int cmp, stmt *sub, int anti, int symmetric, int reduce)
    2148             : {
    2149        5049 :         stmt *sel = sub;
    2150        5049 :         InstrPtr q = select2_join2(be, op1, op2, op3, cmp, &sub, anti, symmetric, 0, st_uselect2, reduce);
    2151             : 
    2152        5049 :         if (q == NULL)
    2153             :                 return NULL;
    2154             : 
    2155        5049 :         stmt *s = stmt_create(be->mvc->sa, st_uselect2);
    2156        5049 :         if (s == NULL) {
    2157             :                 return NULL;
    2158             :         }
    2159             : 
    2160        5049 :         s->op1 = op1;
    2161        5049 :         s->op2 = op2;
    2162        5049 :         s->op3 = op3;
    2163        5049 :         s->op4.stval = sub;
    2164        5049 :         s->flag = cmp;
    2165        5049 :         s->nrcols = op1->nrcols;
    2166        5049 :         s->key = op1->nrcols == 0 && op2->nrcols == 0 && op3->nrcols == 0;
    2167        5049 :         s->nr = getDestVar(q);
    2168        5049 :         s->q = q;
    2169        5049 :         s->cand = sub;
    2170        5049 :         s->reduce = reduce;
    2171        5049 :         if (!sub && sel) /* project back the old ids */
    2172        1458 :                 return stmt_project(be, s, sel);
    2173             :         return s;
    2174             : }
    2175             : 
    2176             : stmt *
    2177       54175 : stmt_tunion(backend *be, stmt *op1, stmt *op2)
    2178             : {
    2179       54175 :         InstrPtr q = NULL;
    2180       54175 :         MalBlkPtr mb = be->mb;
    2181             : 
    2182       54175 :         q = dump_2(mb, batRef, mergecandRef, op1, op2);
    2183       54175 :         if (q) {
    2184       54175 :                 stmt *s = stmt_create(be->mvc->sa, st_tunion);
    2185       54175 :                 if (s == NULL) {
    2186             :                         return NULL;
    2187             :                 }
    2188             : 
    2189       54175 :                 s->op1 = op1;
    2190       54175 :                 s->op2 = op2;
    2191       54175 :                 s->nrcols = op1->nrcols;
    2192       54175 :                 s->key = op1->key;
    2193       54175 :                 s->aggr = op1->aggr;
    2194       54175 :                 s->nr = getDestVar(q);
    2195       54175 :                 s->q = q;
    2196       54175 :                 return s;
    2197             :         }
    2198             : 
    2199           0 :         if (be->mvc->sa->eb.enabled)
    2200           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    2201             :         return NULL;
    2202             : }
    2203             : 
    2204             : stmt *
    2205       44615 : stmt_tdiff(backend *be, stmt *op1, stmt *op2, stmt *lcand)
    2206             : {
    2207       44615 :         InstrPtr q = NULL;
    2208       44615 :         MalBlkPtr mb = be->mb;
    2209             : 
    2210       44615 :         if (op1 == NULL || op2 == NULL || op1->nr < 0 || op2->nr < 0)
    2211           0 :                 goto bailout;
    2212       44615 :         q = newStmt(mb, algebraRef, differenceRef);
    2213       44615 :         if (q == NULL)
    2214           0 :                 goto bailout;
    2215       44615 :         q = pushArgument(mb, q, op1->nr); /* left */
    2216       44615 :         q = pushArgument(mb, q, op2->nr); /* right */
    2217       44615 :         if (lcand)
    2218           0 :                 q = pushArgument(mb, q, lcand->nr); /* left */
    2219             :         else
    2220       44615 :                 q = pushNilBat(mb, q); /* left candidate */
    2221       44615 :         q = pushNilBat(mb, q); /* right candidate */
    2222       44615 :         q = pushBit(mb, q, FALSE);    /* nil matches */
    2223       44615 :         q = pushBit(mb, q, FALSE);    /* do not clear nils */
    2224       44615 :         q = pushNil(mb, q, TYPE_lng); /* estimate */
    2225             : 
    2226       44615 :         bool enabled = be->mvc->sa->eb.enabled;
    2227       44615 :         be->mvc->sa->eb.enabled = false;
    2228       44615 :         stmt *s = stmt_create(be->mvc->sa, st_tdiff);
    2229       44615 :         be->mvc->sa->eb.enabled = enabled;
    2230       44615 :         if (s == NULL) {
    2231           0 :                 freeInstruction(q);
    2232           0 :                 goto bailout;
    2233             :         }
    2234             : 
    2235       44615 :         s->op1 = op1;
    2236       44615 :         s->op2 = op2;
    2237       44615 :         s->nrcols = op1->nrcols;
    2238       44615 :         s->key = op1->key;
    2239       44615 :         s->aggr = op1->aggr;
    2240       44615 :         s->nr = getDestVar(q);
    2241       44615 :         s->q = q;
    2242       44615 :         pushInstruction(mb, q);
    2243       44615 :         return s;
    2244             : 
    2245           0 :   bailout:
    2246           0 :         if (be->mvc->sa->eb.enabled)
    2247           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    2248             :         return NULL;
    2249             : }
    2250             : 
    2251             : stmt *
    2252        1341 : stmt_tdiff2(backend *be, stmt *op1, stmt *op2, stmt *lcand)
    2253             : {
    2254        1341 :         InstrPtr q = NULL;
    2255        1341 :         MalBlkPtr mb = be->mb;
    2256             : 
    2257        1341 :         if (op1 == NULL || op2 == NULL || op1->nr < 0 || op2->nr < 0)
    2258           0 :                 goto bailout;
    2259        1341 :         q = newStmt(mb, algebraRef, differenceRef);
    2260        1341 :         if (q == NULL)
    2261           0 :                 goto bailout;
    2262        1341 :         q = pushArgument(mb, q, op1->nr); /* left */
    2263        1341 :         q = pushArgument(mb, q, op2->nr); /* right */
    2264        1341 :         if (lcand)
    2265           0 :                 q = pushArgument(mb, q, lcand->nr); /* left */
    2266             :         else
    2267        1341 :                 q = pushNilBat(mb, q); /* left candidate */
    2268        1341 :         q = pushNilBat(mb, q); /* right candidate */
    2269        1341 :         q = pushBit(mb, q, FALSE);     /* nil matches */
    2270        1341 :         q = pushBit(mb, q, TRUE);     /* not in */
    2271        1341 :         q = pushNil(mb, q, TYPE_lng); /* estimate */
    2272             : 
    2273        1341 :         bool enabled = be->mvc->sa->eb.enabled;
    2274        1341 :         be->mvc->sa->eb.enabled = false;
    2275        1341 :         stmt *s = stmt_create(be->mvc->sa, st_tdiff);
    2276        1341 :         be->mvc->sa->eb.enabled = enabled;
    2277        1341 :         if (s == NULL) {
    2278           0 :                 freeInstruction(q);
    2279           0 :                 goto bailout;
    2280             :         }
    2281             : 
    2282        1341 :         s->op1 = op1;
    2283        1341 :         s->op2 = op2;
    2284        1341 :         s->nrcols = op1->nrcols;
    2285        1341 :         s->key = op1->key;
    2286        1341 :         s->aggr = op1->aggr;
    2287        1341 :         s->nr = getDestVar(q);
    2288        1341 :         s->q = q;
    2289        1341 :         pushInstruction(mb, q);
    2290        1341 :         return s;
    2291             : 
    2292           0 :   bailout:
    2293           0 :         if (be->mvc->sa->eb.enabled)
    2294           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    2295             :         return NULL;
    2296             : }
    2297             : 
    2298             : stmt *
    2299        2054 : stmt_tinter(backend *be, stmt *op1, stmt *op2, bool single)
    2300             : {
    2301        2054 :         InstrPtr q = NULL;
    2302        2054 :         MalBlkPtr mb = be->mb;
    2303             : 
    2304        2054 :         if (op1 == NULL || op2 == NULL || op1->nr < 0 || op2->nr < 0)
    2305           0 :                 goto bailout;
    2306        2054 :         q = newStmt(mb, algebraRef, intersectRef);
    2307        2054 :         if (q == NULL)
    2308           0 :                 goto bailout;
    2309        2054 :         q = pushArgument(mb, q, op1->nr); /* left */
    2310        2054 :         q = pushArgument(mb, q, op2->nr); /* right */
    2311        2054 :         q = pushNilBat(mb, q); /* left candidate */
    2312        2054 :         q = pushNilBat(mb, q); /* right candidate */
    2313        2054 :         q = pushBit(mb, q, FALSE);    /* nil matches */
    2314        2054 :         q = pushBit(mb, q, single?TRUE:FALSE);    /* max_one */
    2315        2054 :         q = pushNil(mb, q, TYPE_lng); /* estimate */
    2316             : 
    2317        2054 :         bool enabled = be->mvc->sa->eb.enabled;
    2318        2054 :         be->mvc->sa->eb.enabled = false;
    2319        2054 :         stmt *s = stmt_create(be->mvc->sa, st_tinter);
    2320        2054 :         be->mvc->sa->eb.enabled = enabled;
    2321        2054 :         if (s == NULL) {
    2322           0 :                 freeInstruction(q);
    2323           0 :                 goto bailout;
    2324             :         }
    2325             : 
    2326        2054 :         s->op1 = op1;
    2327        2054 :         s->op2 = op2;
    2328        2054 :         s->nrcols = op1->nrcols;
    2329        2054 :         s->key = op1->key;
    2330        2054 :         s->aggr = op1->aggr;
    2331        2054 :         s->nr = getDestVar(q);
    2332        2054 :         s->q = q;
    2333        2054 :         pushInstruction(mb, q);
    2334        2054 :         return s;
    2335             : 
    2336           0 :   bailout:
    2337           0 :         if (be->mvc->sa->eb.enabled)
    2338           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    2339             :         return NULL;
    2340             : }
    2341             : 
    2342             : stmt *
    2343       94163 : stmt_join_cand(backend *be, stmt *op1, stmt *op2, stmt *lcand, stmt *rcand, int anti, comp_type cmptype, int need_left, int is_semantics, bool single, bool inner)
    2344             : {
    2345       94163 :         MalBlkPtr mb = be->mb;
    2346       94163 :         InstrPtr q = NULL;
    2347       94163 :         const char *sjt = inner?joinRef:outerjoinRef;
    2348             : 
    2349       94163 :         (void)anti;
    2350       94163 :         (void)inner;
    2351             : 
    2352       94163 :         if (need_left) {
    2353         653 :                 cmptype = cmp_equal;
    2354         653 :                 sjt = leftjoinRef;
    2355             :         }
    2356       94163 :         if (op1 == NULL || op2 == NULL || op1->nr < 0 || op2->nr < 0)
    2357           0 :                 goto bailout;
    2358             : 
    2359       94163 :         assert (!single || cmptype == cmp_all);
    2360             : 
    2361       94163 :         switch (cmptype) {
    2362       76404 :         case cmp_equal:
    2363       76404 :                 q = newStmtArgs(mb, algebraRef, sjt, 9);
    2364       76404 :                 if (q == NULL)
    2365           0 :                         goto bailout;
    2366       76404 :                 q = pushReturn(mb, q, newTmpVariable(mb, TYPE_any));
    2367       76404 :                 q = pushArgument(mb, q, op1->nr);
    2368       76404 :                 q = pushArgument(mb, q, op2->nr);
    2369       76404 :                 if (!lcand)
    2370       75341 :                         q = pushNilBat(mb, q);
    2371             :                 else
    2372        1063 :                         q = pushArgument(mb, q, lcand->nr);
    2373       76404 :                 if (!rcand)
    2374       76404 :                         q = pushNilBat(mb, q);
    2375             :                 else
    2376           0 :                         q = pushArgument(mb, q, rcand->nr);
    2377       76404 :                 q = pushBit(mb, q, is_semantics?TRUE:FALSE);
    2378       76404 :                 if (!inner)
    2379          75 :                         q = pushBit(mb, q, FALSE); /* not match_one */
    2380       76404 :                 q = pushNil(mb, q, TYPE_lng);
    2381       76404 :                 pushInstruction(mb, q);
    2382       76404 :                 break;
    2383          34 :         case cmp_notequal:
    2384          34 :                 if (inner)
    2385          34 :                         sjt = thetajoinRef;
    2386          34 :                 q = newStmtArgs(mb, algebraRef, sjt, 9);
    2387          34 :                 if (q == NULL)
    2388           0 :                         goto bailout;
    2389          34 :                 q = pushReturn(mb, q, newTmpVariable(mb, TYPE_any));
    2390          34 :                 q = pushArgument(mb, q, op1->nr);
    2391          34 :                 q = pushArgument(mb, q, op2->nr);
    2392          34 :                 if (!lcand)
    2393          34 :                         q = pushNilBat(mb, q);
    2394             :                 else
    2395           0 :                         q = pushArgument(mb, q, lcand->nr);
    2396          34 :                 if (!rcand)
    2397          34 :                         q = pushNilBat(mb, q);
    2398             :                 else
    2399           0 :                         q = pushArgument(mb, q, rcand->nr);
    2400          34 :                 if (inner)
    2401          34 :                         q = pushInt(mb, q, JOIN_NE);
    2402          34 :                 q = pushBit(mb, q, is_semantics?TRUE:FALSE);
    2403          34 :                 if (!inner)
    2404           0 :                         q = pushBit(mb, q, FALSE); /* not match_one */
    2405          34 :                 q = pushNil(mb, q, TYPE_lng);
    2406          34 :                 pushInstruction(mb, q);
    2407          34 :                 break;
    2408        2583 :         case cmp_lt:
    2409             :         case cmp_lte:
    2410             :         case cmp_gt:
    2411             :         case cmp_gte:
    2412        2583 :                 q = newStmtArgs(mb, algebraRef, thetajoinRef, 9);
    2413        2583 :                 if (q == NULL)
    2414           0 :                         goto bailout;
    2415        2583 :                 q = pushReturn(mb, q, newTmpVariable(mb, TYPE_any));
    2416        2583 :                 q = pushArgument(mb, q, op1->nr);
    2417        2583 :                 q = pushArgument(mb, q, op2->nr);
    2418        2583 :                 if (!lcand)
    2419        2583 :                         q = pushNilBat(mb, q);
    2420             :                 else
    2421           0 :                         q = pushArgument(mb, q, lcand->nr);
    2422        2583 :                 if (!rcand)
    2423        2583 :                         q = pushNilBat(mb, q);
    2424             :                 else
    2425           0 :                         q = pushArgument(mb, q, rcand->nr);
    2426        2583 :                 if (cmptype == cmp_lt)
    2427        2067 :                         q = pushInt(mb, q, JOIN_LT);
    2428         516 :                 else if (cmptype == cmp_lte)
    2429           8 :                         q = pushInt(mb, q, JOIN_LE);
    2430         508 :                 else if (cmptype == cmp_gt)
    2431         503 :                         q = pushInt(mb, q, JOIN_GT);
    2432           5 :                 else if (cmptype == cmp_gte)
    2433           5 :                         q = pushInt(mb, q, JOIN_GE);
    2434        2583 :                 q = pushBit(mb, q, is_semantics?TRUE:FALSE);
    2435        2583 :                 q = pushNil(mb, q, TYPE_lng);
    2436        2583 :                 pushInstruction(mb, q);
    2437        2583 :                 break;
    2438       11963 :         case cmp_all:   /* aka cross table */
    2439       11963 :                 q = newStmt(mb, algebraRef, inner?crossRef:outercrossRef);
    2440       11963 :                 if (q == NULL)
    2441           0 :                         goto bailout;
    2442       11963 :                 q = pushReturn(mb, q, newTmpVariable(mb, TYPE_any));
    2443       11963 :                 q = pushArgument(mb, q, op1->nr);
    2444       11963 :                 q = pushArgument(mb, q, op2->nr);
    2445       11963 :                 if (!inner) {
    2446         436 :                         q = pushNilBat(mb, q);
    2447         436 :                         q = pushNilBat(mb, q);
    2448             :                 }
    2449       11963 :                 q = pushBit(mb, q, single?TRUE:FALSE); /* max_one */
    2450       11963 :                 assert(!lcand && !rcand);
    2451       11963 :                 pushInstruction(mb, q);
    2452       11963 :                 break;
    2453        3179 :         case cmp_joined:
    2454        3179 :                 q = op1->q;
    2455        3179 :                 if (q == NULL)
    2456           0 :                         goto bailout;
    2457             :                 break;
    2458           0 :         default:
    2459           0 :                 TRC_ERROR(SQL_EXECUTION, "Impossible action\n");
    2460             :         }
    2461             : 
    2462       94163 :         stmt *s = stmt_create(be->mvc->sa, st_join);
    2463       94163 :         if (s == NULL) {
    2464           0 :                 goto bailout;
    2465             :         }
    2466             : 
    2467       94163 :         s->op1 = op1;
    2468       94163 :         s->op2 = op2;
    2469       94163 :         s->flag = cmptype;
    2470       94163 :         s->key = 0;
    2471       94163 :         s->nrcols = 2;
    2472       94163 :         s->nr = getDestVar(q);
    2473       94163 :         s->q = q;
    2474       94163 :         return s;
    2475             : 
    2476           0 :   bailout:
    2477           0 :         if (be->mvc->sa->eb.enabled)
    2478           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    2479             :         return NULL;
    2480             : }
    2481             : 
    2482             : stmt *
    2483       90093 : stmt_join(backend *be, stmt *l, stmt *r, int anti, comp_type cmptype, int need_left, int is_semantics, bool single)
    2484             : {
    2485       90093 :         return stmt_join_cand(be, l, r, NULL, NULL, anti, cmptype, need_left, is_semantics, single, true);
    2486             : }
    2487             : 
    2488             : stmt *
    2489        1433 : stmt_semijoin(backend *be, stmt *op1, stmt *op2, stmt *lcand, stmt *rcand, int is_semantics, bool single)
    2490             : {
    2491        1433 :         MalBlkPtr mb = be->mb;
    2492        1433 :         InstrPtr q = NULL;
    2493             : 
    2494        1433 :         if (op1 == NULL || op2 == NULL || op1->nr < 0 || op2->nr < 0)
    2495           0 :                 goto bailout;
    2496             : 
    2497        1433 :         if (single) {
    2498         280 :                 q = newStmtArgs(mb, algebraRef, semijoinRef, 9);
    2499         280 :                 q = pushReturn(mb, q, newTmpVariable(mb, TYPE_any));
    2500             :         } else
    2501        1153 :                 q = newStmt(mb, algebraRef, intersectRef);
    2502        1433 :         if (q == NULL)
    2503           0 :                 goto bailout;
    2504        1433 :         q = pushArgument(mb, q, op1->nr);
    2505        1433 :         q = pushArgument(mb, q, op2->nr);
    2506        1433 :         if (lcand)
    2507        1150 :                 q = pushArgument(mb, q, lcand->nr);
    2508             :         else
    2509         283 :                 q = pushNilBat(mb, q);
    2510        1433 :         if (rcand)
    2511           0 :                 q = pushArgument(mb, q, rcand->nr);
    2512             :         else
    2513        1433 :                 q = pushNilBat(mb, q);
    2514        1433 :         q = pushBit(mb, q, is_semantics?TRUE:FALSE);
    2515        1433 :         q = pushBit(mb, q, single?TRUE:FALSE); /* max_one */
    2516        1433 :         q = pushNil(mb, q, TYPE_lng);
    2517             : 
    2518        1433 :         bool enabled = be->mvc->sa->eb.enabled;
    2519        1433 :         be->mvc->sa->eb.enabled = false;
    2520        1433 :         stmt *s = stmt_create(be->mvc->sa, st_semijoin);
    2521        1433 :         be->mvc->sa->eb.enabled = enabled;
    2522        1433 :         if (s == NULL) {
    2523           0 :                 freeInstruction(q);
    2524           0 :                 goto bailout;
    2525             :         }
    2526             : 
    2527        1433 :         s->op1 = op1;
    2528        1433 :         s->op2 = op2;
    2529        1433 :         s->flag = cmp_equal;
    2530        1433 :         s->key = 0;
    2531        1433 :         s->nrcols = 1;
    2532        1433 :         if (single)
    2533         280 :                 s->nrcols = 2;
    2534        1433 :         s->nr = getDestVar(q);
    2535        1433 :         s->q = q;
    2536        1433 :         pushInstruction(mb, q);
    2537        1433 :         return s;
    2538             : 
    2539           0 :   bailout:
    2540           0 :         if (be->mvc->sa->eb.enabled)
    2541           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    2542             :         return NULL;
    2543             : }
    2544             : 
    2545             : static InstrPtr
    2546     3510487 : stmt_project_join(backend *be, stmt *op1, stmt *op2, bool delta)
    2547             : {
    2548     3510487 :         MalBlkPtr mb = be->mb;
    2549     3510487 :         InstrPtr q = NULL;
    2550             : 
    2551     3510487 :         if (op1 == NULL || op2 == NULL || op1->nr < 0 || op2->nr < 0)
    2552             :                 return NULL;
    2553             :         /* delta bat */
    2554     3510487 :         if (delta) {
    2555      481418 :                 int uval = getArg(op2->q, 1);
    2556             : 
    2557      481418 :                 q = newStmt(mb, sqlRef, deltaRef);
    2558      481415 :                 q = pushArgument(mb, q, op1->nr);
    2559      481416 :                 q = pushArgument(mb, q, op2->nr);
    2560      481414 :                 q = pushArgument(mb, q, uval);
    2561             :         } else {
    2562             :                 /* projections, ie left is void headed */
    2563     3029069 :                 q = newStmt(mb, algebraRef, projectionRef);
    2564     3029077 :                 q = pushArgument(mb, q, op1->nr);
    2565     3029075 :                 q = pushArgument(mb, q, op2->nr);
    2566             :         }
    2567     3510487 :         pushInstruction(mb, q);
    2568     3510487 :         return q;
    2569             : }
    2570             : 
    2571             : stmt *
    2572     3038376 : stmt_project(backend *be, stmt *op1, stmt *op2)
    2573             : {
    2574     3038376 :         if (op1 == NULL || op2 == NULL)
    2575             :                 return NULL;
    2576     3038376 :         if (!op2->nrcols)
    2577        9305 :                 return stmt_const(be, op1, op2);
    2578     3029071 :         InstrPtr q = stmt_project_join(be, op1, op2, false);
    2579     3029076 :         if (q) {
    2580     3029076 :                 stmt *s = stmt_create(be->mvc->sa, st_join);
    2581     3029065 :                 if (s == NULL) {
    2582             :                         return NULL;
    2583             :                 }
    2584             : 
    2585     3029065 :                 s->op1 = op1;
    2586     3029065 :                 s->op2 = op2;
    2587     3029065 :                 s->flag = cmp_project;
    2588     3029065 :                 s->key = 0;
    2589     3029065 :                 s->nrcols = MAX(op1->nrcols,op2->nrcols);
    2590     3029065 :                 s->nr = getDestVar(q);
    2591     3029065 :                 s->q = q;
    2592     3029065 :                 s->tname = op2->tname;
    2593     3029065 :                 s->cname = op2->cname;
    2594     3029065 :                 s->label = op2->label;
    2595     3029065 :                 return s;
    2596             :         }
    2597           0 :         if (be->mvc->sa->eb.enabled)
    2598           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : be->mb->errors ? be->mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    2599             :         return NULL;
    2600             : }
    2601             : 
    2602             : stmt *
    2603      481418 : stmt_project_delta(backend *be, stmt *col, stmt *upd)
    2604             : {
    2605      481418 :         InstrPtr q = stmt_project_join(be, col, upd, true);
    2606      481414 :         if (q) {
    2607      481414 :                 stmt *s = stmt_create(be->mvc->sa, st_join);
    2608      481411 :                 if (s == NULL) {
    2609             :                         return NULL;
    2610             :                 }
    2611             : 
    2612      481411 :                 s->op1 = col;
    2613      481411 :                 s->op2 = upd;
    2614      481411 :                 s->flag = cmp_project;
    2615      481411 :                 s->key = 0;
    2616      481411 :                 s->nrcols = 2;
    2617      481411 :                 s->nr = getDestVar(q);
    2618      481411 :                 s->q = q;
    2619      481411 :                 s->tname = col->tname;
    2620      481411 :                 s->cname = col->cname;
    2621      481411 :                 return s;
    2622             :         }
    2623             : 
    2624           0 :         if (be->mvc->sa->eb.enabled)
    2625           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : be->mb->errors ? be->mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    2626             :         return NULL;
    2627             : }
    2628             : 
    2629             : stmt *
    2630          43 : stmt_left_project(backend *be, stmt *op1, stmt *op2, stmt *op3)
    2631             : {
    2632          43 :         MalBlkPtr mb = be->mb;
    2633          43 :         InstrPtr q = NULL;
    2634          43 :         if (op1 == NULL || op2 == NULL || op3 == NULL || op1->nr < 0 || op2->nr < 0 || op3->nr < 0)
    2635           0 :                 goto bailout;
    2636             : 
    2637          43 :         q = newStmt(mb, sqlRef, projectRef);
    2638          43 :         if (q == NULL)
    2639           0 :                 goto bailout;
    2640          43 :         q = pushArgument(mb, q, op1->nr);
    2641          43 :         q = pushArgument(mb, q, op2->nr);
    2642          43 :         q = pushArgument(mb, q, op3->nr);
    2643             : 
    2644          43 :         bool enabled = be->mvc->sa->eb.enabled;
    2645          43 :         be->mvc->sa->eb.enabled = false;
    2646          43 :         stmt *s = stmt_create(be->mvc->sa, st_join);
    2647          43 :         be->mvc->sa->eb.enabled = enabled;
    2648          43 :         if (s == NULL) {
    2649           0 :                 freeInstruction(q);
    2650           0 :                 goto bailout;
    2651             :         }
    2652             : 
    2653          43 :         s->op1 = op1;
    2654          43 :         s->op2 = op2;
    2655          43 :         s->op3 = op3;
    2656          43 :         s->flag = cmp_left_project;
    2657          43 :         s->key = 0;
    2658          43 :         s->nrcols = 2;
    2659          43 :         s->nr = getDestVar(q);
    2660          43 :         s->q = q;
    2661          43 :         pushInstruction(mb, q);
    2662          43 :         return s;
    2663             : 
    2664           0 :   bailout:
    2665           0 :         if (be->mvc->sa->eb.enabled)
    2666           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    2667             :         return NULL;
    2668             : }
    2669             : 
    2670             : stmt *
    2671         176 : stmt_dict(backend *be, stmt *op1, stmt *op2)
    2672             : {
    2673         176 :         MalBlkPtr mb = be->mb;
    2674         176 :         InstrPtr q = NULL;
    2675             : 
    2676         176 :         if (op1 == NULL || op2 == NULL || op1->nr < 0 || op2->nr < 0)
    2677             :                 return NULL;
    2678             : 
    2679         176 :         q = newStmt(mb, dictRef, decompressRef);
    2680         176 :         if (q == NULL)
    2681           0 :                 goto bailout;
    2682         176 :         q = pushArgument(mb, q, op1->nr);
    2683         176 :         q = pushArgument(mb, q, op2->nr);
    2684             : 
    2685         176 :         bool enabled = be->mvc->sa->eb.enabled;
    2686         176 :         be->mvc->sa->eb.enabled = false;
    2687         176 :         stmt *s = stmt_create(be->mvc->sa, st_join);
    2688         176 :         be->mvc->sa->eb.enabled = enabled;
    2689         176 :         if (s == NULL) {
    2690           0 :                 freeInstruction(q);
    2691           0 :                 return NULL;
    2692             :         }
    2693             : 
    2694         176 :         s->op1 = op1;
    2695         176 :         s->op2 = op2;
    2696         176 :         s->flag = cmp_project;
    2697         176 :         s->key = 0;
    2698         176 :         s->nrcols = MAX(op1->nrcols,op2->nrcols);
    2699         176 :         s->nr = getDestVar(q);
    2700         176 :         s->q = q;
    2701         176 :         s->tname = op1->tname;
    2702         176 :         s->cname = op1->cname;
    2703         176 :         pushInstruction(mb, q);
    2704         176 :         return s;
    2705             : 
    2706           0 :   bailout:
    2707           0 :         if (be->mvc->sa->eb.enabled)
    2708           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    2709             :         return NULL;
    2710             : }
    2711             : 
    2712             : stmt *
    2713           8 : stmt_for(backend *be, stmt *op1, stmt *min_val)
    2714             : {
    2715           8 :         MalBlkPtr mb = be->mb;
    2716           8 :         InstrPtr q = NULL;
    2717             : 
    2718           8 :         if (op1 == NULL || min_val == NULL || op1->nr < 0)
    2719             :                 return NULL;
    2720             : 
    2721           8 :         q = newStmt(mb, forRef, decompressRef);
    2722           8 :         if (q == NULL)
    2723           0 :                 goto bailout;
    2724           8 :         q = pushArgument(mb, q, op1->nr);
    2725           8 :         q = pushArgument(mb, q, min_val->nr);
    2726             : 
    2727           8 :         bool enabled = be->mvc->sa->eb.enabled;
    2728           8 :         be->mvc->sa->eb.enabled = false;
    2729           8 :         stmt *s = stmt_create(be->mvc->sa, st_join);
    2730           8 :         be->mvc->sa->eb.enabled = enabled;
    2731           8 :         if (s == NULL) {
    2732           0 :                 freeInstruction(q);
    2733           0 :                 return NULL;
    2734             :         }
    2735             : 
    2736           8 :         s->op1 = op1;
    2737           8 :         s->op2 = min_val;
    2738           8 :         s->flag = cmp_project;
    2739           8 :         s->key = 0;
    2740           8 :         s->nrcols = op1->nrcols;
    2741           8 :         s->nr = getDestVar(q);
    2742           8 :         s->q = q;
    2743           8 :         s->tname = op1->tname;
    2744           8 :         s->cname = op1->cname;
    2745           8 :         pushInstruction(mb, q);
    2746           8 :         return s;
    2747             : 
    2748           0 :   bailout:
    2749           0 :         if (be->mvc->sa->eb.enabled)
    2750           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    2751             :         return NULL;
    2752             : }
    2753             : 
    2754             : stmt *
    2755          54 : stmt_join2(backend *be, stmt *l, stmt *ra, stmt *rb, int cmp, int anti, int symmetric, int swapped)
    2756             : {
    2757          54 :         InstrPtr q = select2_join2(be, l, ra, rb, cmp, NULL, anti, symmetric, swapped, st_join2, 1/*reduce semantics*/);
    2758          54 :         if (q == NULL)
    2759             :                 return NULL;
    2760             : 
    2761          54 :         stmt *s = stmt_create(be->mvc->sa, st_join2);
    2762          54 :         if (s == NULL) {
    2763             :                 return NULL;
    2764             :         }
    2765             : 
    2766          54 :         s->op1 = l;
    2767          54 :         s->op2 = ra;
    2768          54 :         s->op3 = rb;
    2769          54 :         s->flag = cmp;
    2770          54 :         s->nrcols = 2;
    2771          54 :         s->nr = getDestVar(q);
    2772          54 :         s->q = q;
    2773          54 :         s->reduce = 1;
    2774          54 :         return s;
    2775             : }
    2776             : 
    2777             : stmt *
    2778          49 : stmt_genjoin(backend *be, stmt *l, stmt *r, sql_subfunc *op, int anti, int swapped)
    2779             : {
    2780          49 :         MalBlkPtr mb = be->mb;
    2781          49 :         InstrPtr q = NULL;
    2782          49 :         const char *mod, *fimp;
    2783          49 :         node *n;
    2784             : 
    2785          49 :         if (l == NULL || r == NULL)
    2786           0 :                 goto bailout;
    2787          49 :         if (backend_create_subfunc(be, op, NULL) < 0)
    2788           0 :                 goto bailout;
    2789          49 :         mod = sql_func_mod(op->func);
    2790          49 :         fimp = backend_function_imp(be, op->func);
    2791          49 :         fimp = sa_strconcat(be->mvc->sa, fimp, "join");
    2792             : 
    2793             :         /* filter qualifying tuples, return oids of h and tail */
    2794          49 :         q = newStmtArgs(mb, mod, fimp, list_length(l->op4.lval) + list_length(r->op4.lval) + 7);
    2795          49 :         if (q == NULL)
    2796           0 :                 goto bailout;
    2797          49 :         q = pushReturn(mb, q, newTmpVariable(mb, TYPE_any));
    2798          98 :         for (n = l->op4.lval->h; n; n = n->next) {
    2799          49 :                 stmt *op = n->data;
    2800             : 
    2801          49 :                 q = pushArgument(mb, q, op->nr);
    2802             :         }
    2803             : 
    2804         162 :         for (n = r->op4.lval->h; n; n = n->next) {
    2805         113 :                 stmt *op = n->data;
    2806             : 
    2807         113 :                 q = pushArgument(mb, q, op->nr);
    2808             :         }
    2809          49 :         q = pushNilBat(mb, q); /* candidate lists */
    2810          49 :         q = pushNilBat(mb, q); /* candidate lists */
    2811          49 :         q = pushBit(mb, q, TRUE);     /* nil_matches */
    2812          49 :         q = pushNil(mb, q, TYPE_lng); /* estimate */
    2813          49 :         q = pushBit(mb, q, anti?TRUE:FALSE); /* 'not' matching */
    2814          49 :         pushInstruction(mb, q);
    2815             : 
    2816          49 :         if (swapped) {
    2817           7 :                 InstrPtr r = newInstruction(mb,  NULL, NULL);
    2818           7 :                 if (r == NULL)
    2819           0 :                         goto bailout;
    2820           7 :                 getArg(r, 0) = newTmpVariable(mb, TYPE_any);
    2821           7 :                 r = pushReturn(mb, r, newTmpVariable(mb, TYPE_any));
    2822           7 :                 r = pushArgument(mb, r, getArg(q,1));
    2823           7 :                 r = pushArgument(mb, r, getArg(q,0));
    2824           7 :                 pushInstruction(mb, r);
    2825           7 :                 q = r;
    2826             :         }
    2827             : 
    2828          49 :         stmt *s = stmt_create(be->mvc->sa, st_joinN);
    2829          49 :         if (s == NULL) {
    2830           0 :                 goto bailout;
    2831             :         }
    2832             : 
    2833          49 :         s->op1 = l;
    2834          49 :         s->op2 = r;
    2835          49 :         s->op4.funcval = op;
    2836          49 :         s->nrcols = 2;
    2837          49 :         if (swapped)
    2838           7 :                 s->flag |= SWAPPED;
    2839          49 :         s->nr = getDestVar(q);
    2840          49 :         s->q = q;
    2841          49 :         return s;
    2842             : 
    2843           0 :   bailout:
    2844           0 :         if (be->mvc->sa->eb.enabled)
    2845           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    2846             :         return NULL;
    2847             : }
    2848             : 
    2849             : stmt *
    2850       25575 : stmt_rs_column(backend *be, stmt *rs, int i, sql_subtype *tpe)
    2851             : {
    2852       25575 :         InstrPtr q = NULL;
    2853             : 
    2854       25575 :         if (rs == NULL || rs->nr < 0)
    2855             :                 return NULL;
    2856       25575 :         q = rs->q;
    2857       25575 :         if (q) {
    2858       25359 :                 stmt *s = stmt_create(be->mvc->sa, st_rs_column);
    2859       25359 :                 if (s == NULL) {
    2860             :                         return NULL;
    2861             :                 }
    2862             : 
    2863       25359 :                 s->op1 = rs;
    2864       25359 :                 s->op4.typeval = *tpe;
    2865       25359 :                 s->flag = i;
    2866       25359 :                 s->nrcols = 1;
    2867       25359 :                 s->key = 0;
    2868       25359 :                 s->q = q;
    2869       25359 :                 s->nr = getArg(q, s->flag);
    2870       25359 :                 return s;
    2871         216 :         } else if (rs->type == st_list) {
    2872         216 :                 list *cols = rs->op4.lval;
    2873         216 :                 if (i < list_length(cols))
    2874         216 :                         return list_fetch(cols, i);
    2875             :         }
    2876             :         return NULL;
    2877             : }
    2878             : 
    2879             : /*
    2880             :  * The dump_header produces a sequence of instructions for
    2881             :  * the front-end to prepare presentation of a result table.
    2882             :  *
    2883             :  * A secondary scheme is added to assemble all information
    2884             :  * in columns first. Then it can be returned to the environment.
    2885             :  */
    2886             : #define NEWRESULTSET
    2887             : 
    2888             : #define meta(P, Id, Tpe, Args)                                          \
    2889             :         do {                                                                                    \
    2890             :                 P = newStmtArgs(mb, batRef, packRef, Args);     \
    2891             :                 if (P) {                                                                        \
    2892             :                         Id = getArg(P,0);                                               \
    2893             :                         setVarType(mb, Id, newBatType(Tpe));    \
    2894             :                         setVarFixed(mb, Id);                                    \
    2895             :                         list = pushArgument(mb, list, Id);              \
    2896             :                         pushInstruction(mb, P);                                 \
    2897             :                 }                                                                                       \
    2898             :         } while (0)
    2899             : 
    2900             : static int
    2901          32 : dump_export_header(mvc *sql, MalBlkPtr mb, list *l, int file, const char * format, const char * sep,const char * rsep,const char * ssep,const char * ns, int onclient)
    2902             : {
    2903          32 :         node *n;
    2904          32 :         int ret = -1;
    2905          32 :         int args;
    2906             : 
    2907             :         // gather the meta information
    2908          32 :         int tblId, nmeId, tpeId, lenId, scaleId;
    2909          32 :         InstrPtr list;
    2910          32 :         InstrPtr tblPtr, nmePtr, tpePtr, lenPtr, scalePtr;
    2911             : 
    2912          32 :         args = list_length(l) + 1;
    2913             : 
    2914          32 :         list = newInstructionArgs(mb, sqlRef, export_tableRef, args + 13);
    2915          32 :         if (list == NULL)
    2916             :                 return -1;
    2917          32 :         getArg(list,0) = newTmpVariable(mb,TYPE_int);
    2918          32 :         if( file >= 0){
    2919          32 :                 list = pushArgument(mb, list, file);
    2920          32 :                 list = pushStr(mb, list, format);
    2921          32 :                 list = pushStr(mb, list, sep);
    2922          32 :                 list = pushStr(mb, list, rsep);
    2923          32 :                 list = pushStr(mb, list, ssep);
    2924          32 :                 list = pushStr(mb, list, ns);
    2925          32 :                 list = pushInt(mb, list, onclient);
    2926             :         }
    2927          32 :         meta(tblPtr, tblId, TYPE_str, args);
    2928          32 :         meta(nmePtr, nmeId, TYPE_str, args);
    2929          32 :         meta(tpePtr, tpeId, TYPE_str, args);
    2930          32 :         meta(lenPtr, lenId, TYPE_int, args);
    2931          32 :         meta(scalePtr, scaleId, TYPE_int, args);
    2932          32 :         if(tblPtr == NULL || nmePtr == NULL || tpePtr == NULL || lenPtr == NULL || scalePtr == NULL)
    2933             :                 return -1;
    2934             : 
    2935         172 :         for (n = l->h; n; n = n->next) {
    2936         140 :                 stmt *c = n->data;
    2937         140 :                 sql_subtype *t = tail_type(c);
    2938         140 :                 const char *tname = table_name(sql->sa, c);
    2939         140 :                 const char *sname = schema_name(sql->sa, c);
    2940         140 :                 const char *_empty = "";
    2941         140 :                 const char *tn = (tname) ? tname : _empty;
    2942         140 :                 const char *sn = (sname) ? sname : _empty;
    2943         140 :                 const char *cn = column_name(sql->sa, c);
    2944         140 :                 const char *ntn = sql_escape_ident(sql->ta, tn);
    2945         140 :                 const char *nsn = sql_escape_ident(sql->ta, sn);
    2946         140 :                 size_t fqtnl;
    2947         140 :                 char *fqtn = NULL;
    2948             : 
    2949         140 :                 if (ntn && nsn && (fqtnl = strlen(ntn) + 1 + strlen(nsn) + 1) ){
    2950         140 :                         fqtn = SA_NEW_ARRAY(sql->ta, char, fqtnl);
    2951         140 :                         if (fqtn == NULL)
    2952             :                                 return -1;
    2953         140 :                         snprintf(fqtn, fqtnl, "%s.%s", nsn, ntn);
    2954         140 :                         tblPtr = pushStr(mb, tblPtr, fqtn);
    2955         140 :                         nmePtr = pushStr(mb, nmePtr, cn);
    2956         140 :                         tpePtr = pushStr(mb, tpePtr, (t->type->localtype == TYPE_void ? "char" : t->type->base.name));
    2957         140 :                         lenPtr = pushInt(mb, lenPtr, t->digits);
    2958         140 :                         scalePtr = pushInt(mb, scalePtr, t->scale);
    2959         140 :                         list = pushArgument(mb, list, c->nr);
    2960             :                 } else
    2961             :                         return -1;
    2962             :         }
    2963          32 :         sa_reset(sql->ta);
    2964          32 :         ret = getArg(list,0);
    2965          32 :         pushInstruction(mb,list);
    2966          32 :         return ret;
    2967             : }
    2968             : 
    2969             : 
    2970             : stmt *
    2971          32 : stmt_export(backend *be, stmt *t, const char *sep, const char *rsep, const char *ssep, const char *null_string, int onclient, stmt *file)
    2972             : {
    2973          32 :         MalBlkPtr mb = be->mb;
    2974          32 :         InstrPtr q = NULL;
    2975          32 :         int fnr;
    2976          32 :         list *l;
    2977             : 
    2978          32 :         if (t == NULL || t->nr < 0)
    2979           0 :                 goto bailout;
    2980          32 :         l = t->op4.lval;
    2981          32 :         if (file) {
    2982          23 :                 if (file->nr < 0)
    2983           0 :                         goto bailout;
    2984             :                 fnr = file->nr;
    2985             :         } else {
    2986           9 :                 q = newAssignment(mb);
    2987           9 :                 if (q == NULL)
    2988           0 :                         goto bailout;
    2989           9 :                 q = pushStr(mb,q,"stdout");
    2990           9 :                 fnr = getArg(q,0);
    2991           9 :                 pushInstruction(mb, q);
    2992             :         }
    2993          32 :         if (t->type == st_list) {
    2994          32 :                 if (dump_export_header(be->mvc, mb, l, fnr, "csv", sep, rsep, ssep, null_string, onclient) < 0)
    2995           0 :                         goto bailout;
    2996             :         } else {
    2997           0 :                 q = newStmt(mb, sqlRef, raiseRef);
    2998           0 :                 if (q == NULL)
    2999           0 :                         goto bailout;
    3000           0 :                 q = pushStr(mb, q, "not a valid output list\n");
    3001           0 :                 pushInstruction(mb, q);
    3002             :         }
    3003          32 :         stmt *s = stmt_create(be->mvc->sa, st_export);
    3004          32 :         if(!s) {
    3005           0 :                 goto bailout;
    3006             :         }
    3007          32 :         s->op1 = t;
    3008          32 :         s->op2 = file;
    3009          32 :         s->q = q;
    3010          32 :         s->nr = 1;
    3011          32 :         return s;
    3012             : 
    3013           0 :   bailout:
    3014           0 :         if (be->mvc->sa->eb.enabled)
    3015           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    3016             :         return NULL;
    3017             : }
    3018             : 
    3019             : stmt *
    3020         150 : stmt_export_bin(backend *be, stmt *colstmt, bool byteswap, const char *filename, int on_client)
    3021             : {
    3022         150 :         MalBlkPtr mb = be->mb;
    3023         150 :         InstrPtr q;
    3024             : 
    3025         150 :         if (colstmt == NULL)
    3026           0 :                 goto bailout;
    3027         150 :         q = newStmt(mb, sqlRef, export_bin_columnRef);
    3028         150 :         if (q == NULL)
    3029           0 :                 goto bailout;
    3030         150 :         pushArgument(mb, q, colstmt->nr);
    3031         150 :         pushBit(mb, q, byteswap);
    3032         150 :         pushStr(mb, q, filename);
    3033         150 :         pushInt(mb, q, on_client);
    3034         150 :         pushInstruction(mb, q);
    3035             : 
    3036         150 :         stmt *s = stmt_create(be->mvc->sa, st_export);
    3037         150 :         if (!s)
    3038           0 :                 goto bailout;
    3039             : 
    3040         150 :         s->q = q;
    3041         150 :         return s;
    3042             : 
    3043           0 :   bailout:
    3044           0 :         if (be->mvc->sa->eb.enabled)
    3045           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    3046             :         return NULL;
    3047             : }
    3048             : 
    3049             : stmt *
    3050        3162 : stmt_trans(backend *be, int type, stmt *chain, stmt *name)
    3051             : {
    3052        3162 :         MalBlkPtr mb = be->mb;
    3053        3162 :         InstrPtr q = NULL;
    3054             : 
    3055        3162 :         if (chain == NULL || chain->nr < 0)
    3056           0 :                 goto bailout;
    3057             : 
    3058        3162 :         switch(type){
    3059          13 :         case ddl_release:
    3060          13 :                 q = newStmt(mb, sqlRef, transaction_releaseRef);
    3061          13 :                 break;
    3062         558 :         case ddl_commit:
    3063         558 :                 q = newStmt(mb, sqlRef, transaction_commitRef);
    3064         558 :                 break;
    3065        1084 :         case ddl_rollback:
    3066        1084 :                 q = newStmt(mb, sqlRef, transaction_rollbackRef);
    3067        1084 :                 break;
    3068        1507 :         case ddl_trans:
    3069        1507 :                 q = newStmt(mb, sqlRef, transaction_beginRef);
    3070        1507 :                 break;
    3071           0 :         default:
    3072           0 :                 TRC_ERROR(SQL_EXECUTION, "Unknown transaction type\n");
    3073           0 :                 goto bailout;
    3074             :         }
    3075        3162 :         if (q == NULL)
    3076           0 :                 goto bailout;
    3077        3162 :         q = pushArgument(mb, q, chain->nr);
    3078        3162 :         if (name)
    3079          79 :                 q = pushArgument(mb, q, name->nr);
    3080             :         else
    3081        3083 :                 q = pushNil(mb, q, TYPE_str);
    3082             : 
    3083        3162 :         bool enabled = be->mvc->sa->eb.enabled;
    3084        3162 :         be->mvc->sa->eb.enabled = false;
    3085        3162 :         stmt *s = stmt_create(be->mvc->sa, st_trans);
    3086        3162 :         be->mvc->sa->eb.enabled = enabled;
    3087        3162 :         if(!s) {
    3088           0 :                 freeInstruction(q);
    3089           0 :                 goto bailout;
    3090             :         }
    3091        3162 :         s->op1 = chain;
    3092        3162 :         s->op2 = name;
    3093        3162 :         s->flag = type;
    3094        3162 :         s->q = q;
    3095        3162 :         s->nr = getDestVar(q);
    3096        3162 :         pushInstruction(mb, q);
    3097        3162 :         return s;
    3098             : 
    3099           0 :   bailout:
    3100           0 :         if (be->mvc->sa->eb.enabled)
    3101           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    3102             :         return NULL;
    3103             : }
    3104             : 
    3105             : stmt *
    3106      283753 : stmt_catalog(backend *be, int type, stmt *args)
    3107             : {
    3108      283753 :         MalBlkPtr mb = be->mb;
    3109      283753 :         InstrPtr q = NULL;
    3110      283753 :         node *n;
    3111             : 
    3112      283753 :         if (args == NULL || args->nr < 0)
    3113           0 :                 goto bailout;
    3114             : 
    3115             :         /* cast them into properly named operations */
    3116      283753 :         const char *ref;
    3117      283753 :         switch(type){
    3118         313 :         case ddl_create_seq:                    ref = create_seqRef;            break;
    3119          47 :         case ddl_alter_seq:                             ref = alter_seqRef;                     break;
    3120          32 :         case ddl_drop_seq:                              ref = drop_seqRef;                      break;
    3121        1082 :         case ddl_create_schema:                 ref = create_schemaRef;         break;
    3122         191 :         case ddl_drop_schema:                   ref = drop_schemaRef;           break;
    3123       10110 :         case ddl_create_table:                  ref = create_tableRef;          break;
    3124       22358 :         case ddl_create_view:                   ref = create_viewRef;           break;
    3125        3679 :         case ddl_drop_table:                    ref = drop_tableRef;            break;
    3126         314 :         case ddl_drop_view:                             ref = drop_viewRef;                     break;
    3127         150 :         case ddl_drop_constraint:               ref = drop_constraintRef;       break;
    3128        1369 :         case ddl_alter_table:                   ref = alter_tableRef;           break;
    3129         906 :         case ddl_create_type:                   ref = create_typeRef;           break;
    3130           4 :         case ddl_drop_type:                             ref = drop_typeRef;                     break;
    3131          40 :         case ddl_grant_roles:                   ref = grant_rolesRef;           break;
    3132          10 :         case ddl_revoke_roles:                  ref = revoke_rolesRef;          break;
    3133       18455 :         case ddl_grant:                                 ref = grantRef;                         break;
    3134          15 :         case ddl_revoke:                                ref = revokeRef;                        break;
    3135       99018 :         case ddl_grant_func:                    ref = grant_functionRef;        break;
    3136           1 :         case ddl_revoke_func:                   ref = revoke_functionRef;       break;
    3137         358 :         case ddl_create_user:                   ref = create_userRef;           break;
    3138         107 :         case ddl_drop_user:                             ref = drop_userRef;                     break;
    3139          83 :         case ddl_alter_user:                    ref = alter_userRef;            break;
    3140           5 :         case ddl_rename_user:                   ref = rename_userRef;           break;
    3141          26 :         case ddl_create_role:                   ref = create_roleRef;           break;
    3142          19 :         case ddl_drop_role:                             ref = drop_roleRef;                     break;
    3143         160 :         case ddl_drop_index:                    ref = drop_indexRef;            break;
    3144         669 :         case ddl_drop_function:                 ref = drop_functionRef;         break;
    3145      120296 :         case ddl_create_function:               ref = create_functionRef;       break;
    3146         345 :         case ddl_create_trigger:                ref = create_triggerRef;        break;
    3147          84 :         case ddl_drop_trigger:                  ref = drop_triggerRef;          break;
    3148         312 :         case ddl_alter_table_add_table: ref = alter_add_tableRef;       break;
    3149         180 :         case ddl_alter_table_del_table: ref = alter_del_tableRef;       break;
    3150        2310 :         case ddl_alter_table_set_access:ref = alter_set_tableRef;       break;
    3151         225 :         case ddl_alter_table_add_range_partition: ref = alter_add_range_partitionRef; break;
    3152          60 :         case ddl_alter_table_add_list_partition: ref = alter_add_value_partitionRef; break;
    3153         354 :         case ddl_comment_on:                    ref = comment_onRef;            break;
    3154           8 :         case ddl_rename_schema:                 ref = rename_schemaRef;         break;
    3155          45 :         case ddl_rename_table:                  ref = rename_tableRef;          break;
    3156          13 :         case ddl_rename_column:                 ref = rename_columnRef;         break;
    3157           0 :         default:
    3158           0 :                 TRC_ERROR(SQL_EXECUTION, "Unknown catalog operation\n");
    3159           0 :                 goto bailout;
    3160             :         }
    3161      283753 :         q = newStmtArgs(mb, sqlcatalogRef, ref, list_length(args->op4.lval) + 1);
    3162      283753 :         if (q == NULL)
    3163           0 :                 goto bailout;
    3164             :         // pass all arguments as before
    3165     1696896 :         for (n = args->op4.lval->h; n; n = n->next) {
    3166     1413143 :                 stmt *c = n->data;
    3167             : 
    3168     1413143 :                 q = pushArgument(mb, q, c->nr);
    3169             :         }
    3170             : 
    3171      283753 :         bool enabled = be->mvc->sa->eb.enabled;
    3172      283753 :         be->mvc->sa->eb.enabled = false;
    3173      283753 :         stmt *s = stmt_create(be->mvc->sa, st_catalog);
    3174      283753 :         be->mvc->sa->eb.enabled = enabled;
    3175      283753 :         if(!s) {
    3176           0 :                 freeInstruction(q);
    3177           0 :                 goto bailout;
    3178             :         }
    3179      283753 :         s->op1 = args;
    3180      283753 :         s->flag = type;
    3181      283753 :         s->q = q;
    3182      283753 :         s->nr = getDestVar(q);
    3183      283753 :         pushInstruction(mb, q);
    3184      283753 :         return s;
    3185             : 
    3186           0 :   bailout:
    3187           0 :         if (be->mvc->sa->eb.enabled)
    3188           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    3189             :         return NULL;
    3190             : }
    3191             : 
    3192             : void
    3193     1827644 : stmt_set_nrcols(stmt *s)
    3194             : {
    3195     1827644 :         unsigned nrcols = 0;
    3196     1827644 :         int key = 1;
    3197     1827644 :         node *n;
    3198     1827644 :         list *l = s->op4.lval;
    3199             : 
    3200     1827644 :         assert(s->type == st_list);
    3201     8894499 :         for (n = l->h; n; n = n->next) {
    3202     7066855 :                 stmt *f = n->data;
    3203             : 
    3204     7066855 :                 if (!f)
    3205           0 :                         continue;
    3206     7066855 :                 if (f->nrcols > nrcols)
    3207             :                         nrcols = f->nrcols;
    3208     7066855 :                 key &= f->key;
    3209     7066855 :                 s->nr = f->nr;
    3210             :         }
    3211     1827644 :         s->nrcols = nrcols;
    3212     1827644 :         s->key = key;
    3213     1827644 : }
    3214             : 
    3215             : stmt *
    3216     1431217 : stmt_list(backend *be, list *l)
    3217             : {
    3218     1431217 :         if (l == NULL)
    3219             :                 return NULL;
    3220     1431217 :         stmt *s = stmt_create(be->mvc->sa, st_list);
    3221     1431207 :         if(!s) {
    3222             :                 return NULL;
    3223             :         }
    3224     1431207 :         s->op4.lval = l;
    3225     1431207 :         stmt_set_nrcols(s);
    3226     1431207 :         return s;
    3227             : }
    3228             : 
    3229             : static InstrPtr
    3230       64479 : dump_header(mvc *sql, MalBlkPtr mb, list *l)
    3231             : {
    3232       64479 :         node *n;
    3233             :         // gather the meta information
    3234       64479 :         int tblId, nmeId, tpeId, lenId, scaleId;
    3235       64479 :         int args;
    3236       64479 :         InstrPtr list;
    3237       64479 :         InstrPtr tblPtr, nmePtr, tpePtr, lenPtr, scalePtr;
    3238             : 
    3239       64479 :         args = list_length(l) + 1;
    3240             : 
    3241       64479 :         list = newInstructionArgs(mb,sqlRef, resultSetRef, args + 5);
    3242       64479 :         if(!list) {
    3243             :                 return NULL;
    3244             :         }
    3245       64479 :         getArg(list,0) = newTmpVariable(mb,TYPE_int);
    3246       64478 :         meta(tblPtr, tblId, TYPE_str, args);
    3247       64479 :         meta(nmePtr, nmeId, TYPE_str, args);
    3248       64479 :         meta(tpePtr, tpeId, TYPE_str, args);
    3249       64479 :         meta(lenPtr, lenId, TYPE_int, args);
    3250       64479 :         meta(scalePtr, scaleId, TYPE_int, args);
    3251       64479 :         if(tblPtr == NULL || nmePtr == NULL || tpePtr == NULL || lenPtr == NULL || scalePtr == NULL)
    3252             :                 return NULL;
    3253             : 
    3254      339372 :         for (n = l->h; n; n = n->next) {
    3255      274893 :                 stmt *c = n->data;
    3256      274893 :                 sql_subtype *t = tail_type(c);
    3257      274895 :                 const char *tname = table_name(sql->sa, c);
    3258      274895 :                 const char *sname = schema_name(sql->sa, c);
    3259      274894 :                 const char *_empty = "";
    3260      274894 :                 const char *tn = (tname) ? tname : _empty;
    3261      274894 :                 const char *sn = (sname) ? sname : _empty;
    3262      274894 :                 const char *cn = column_name(sql->sa, c);
    3263      274895 :                 const char *ntn = sql_escape_ident(sql->ta, tn);
    3264      274894 :                 const char *nsn = sql_escape_ident(sql->ta, sn);
    3265      274896 :                 size_t fqtnl;
    3266             : 
    3267      274896 :                 if (ntn && nsn && (fqtnl = strlen(ntn) + 1 + strlen(nsn) + 1) ){
    3268      274896 :                         char *fqtn = SA_NEW_ARRAY(sql->ta, char, fqtnl);
    3269      274895 :                         if (fqtn == NULL)
    3270             :                                 return NULL;
    3271      274895 :                         snprintf(fqtn, fqtnl, "%s.%s", nsn, ntn);
    3272      274895 :                         tblPtr = pushStr(mb, tblPtr, fqtn);
    3273      274897 :                         nmePtr = pushStr(mb, nmePtr, cn);
    3274      274894 :                         tpePtr = pushStr(mb, tpePtr, (t->type->localtype == TYPE_void ? "char" : t->type->base.name));
    3275      274899 :                         lenPtr = pushInt(mb, lenPtr, t->digits);
    3276      274891 :                         scalePtr = pushInt(mb, scalePtr, t->scale);
    3277      274893 :                         list = pushArgument(mb,list,c->nr);
    3278             :                 } else
    3279             :                         return NULL;
    3280             :         }
    3281       64479 :         sa_reset(sql->ta);
    3282       64479 :         pushInstruction(mb,list);
    3283       64479 :         return list;
    3284             : }
    3285             : 
    3286             : int
    3287      124948 : stmt_output(backend *be, stmt *lst)
    3288             : {
    3289      124948 :         MalBlkPtr mb = be->mb;
    3290      124948 :         InstrPtr q = NULL;
    3291      124948 :         list *l = lst->op4.lval;
    3292      124948 :         int cnt = list_length(l), ok = 0;
    3293      124947 :         node *n = l->h;
    3294      124947 :         stmt *first = n->data;
    3295             : 
    3296             :         /* single value result, has a fast exit */
    3297      124947 :         if (cnt == 1 && first->nrcols <= 0 ){
    3298       60469 :                 stmt *c = n->data;
    3299       60469 :                 sql_subtype *t = tail_type(c);
    3300       60470 :                 const char *tname = table_name(be->mvc->sa, c);
    3301       60471 :                 const char *sname = schema_name(be->mvc->sa, c);
    3302       60471 :                 const char *_empty = "";
    3303       60471 :                 const char *tn = (tname) ? tname : _empty;
    3304       60471 :                 const char *sn = (sname) ? sname : _empty;
    3305       60471 :                 const char *cn = column_name(be->mvc->sa, c);
    3306       60471 :                 const char *ntn = sql_escape_ident(be->mvc->ta, tn);
    3307       60452 :                 const char *nsn = sql_escape_ident(be->mvc->ta, sn);
    3308             : 
    3309       60466 :                 if (ntn && nsn) {
    3310       60466 :                         size_t fqtnl = strlen(ntn) + 1 + strlen(nsn) + 1;
    3311       60466 :                         char *fqtn = SA_NEW_ARRAY(be->mvc->ta, char, fqtnl);
    3312       60466 :                         if (fqtn == NULL)
    3313             :                                 return -1;
    3314       60466 :                         ok = 1;
    3315       60466 :                         snprintf(fqtn, fqtnl, "%s.%s", nsn, ntn);
    3316             : 
    3317       60466 :                         q = newStmt(mb, sqlRef, resultSetRef);
    3318       60473 :                         if (q == NULL)
    3319             :                                 return -1;
    3320       60473 :                         getArg(q,0) = newTmpVariable(mb,TYPE_int);
    3321       60473 :                         q = pushStr(mb, q, fqtn);
    3322       60470 :                         q = pushStr(mb, q, cn);
    3323       60472 :                         q = pushStr(mb, q, t->type->localtype == TYPE_void ? "char" : t->type->base.name);
    3324       60471 :                         q = pushInt(mb, q, t->digits);
    3325       60472 :                         q = pushInt(mb, q, t->scale);
    3326       60472 :                         q = pushInt(mb, q, t->type->eclass);
    3327       60470 :                         q = pushArgument(mb, q, c->nr);
    3328       60470 :                         pushInstruction(mb, q);
    3329             :                 }
    3330       60470 :                 sa_reset(be->mvc->ta);
    3331       60470 :                 if (!ok)
    3332             :                         return -1;
    3333             :         } else {
    3334       64478 :                 if ((q = dump_header(be->mvc, mb, l)) == NULL)
    3335             :                         return -1;
    3336             :         }
    3337             :         return 0;
    3338             : }
    3339             : 
    3340             : int
    3341      138727 : stmt_affected_rows(backend *be, int lastnr)
    3342             : {
    3343      138727 :         MalBlkPtr mb = be->mb;
    3344      138727 :         InstrPtr q = NULL;
    3345             : 
    3346      138727 :         q = newStmt(mb, sqlRef, affectedRowsRef);
    3347      138727 :         if (q == NULL)
    3348             :                 return -1;
    3349      138727 :         q = pushArgument(mb, q, be->mvc_var);
    3350      138726 :         getArg(q, 0) = be->mvc_var = newTmpVariable(mb, TYPE_int);
    3351      138725 :         q = pushArgument(mb, q, lastnr);
    3352      138725 :         pushInstruction(mb, q);
    3353      138724 :         be->mvc_var = getDestVar(q);
    3354      138724 :         return 0;
    3355             : }
    3356             : 
    3357             : stmt *
    3358      177367 : stmt_append(backend *be, stmt *c, stmt *a)
    3359             : {
    3360      177367 :         MalBlkPtr mb = be->mb;
    3361      177367 :         InstrPtr q = NULL;
    3362             : 
    3363      177367 :         if (c == NULL || a == NULL || c->nr < 0 || a->nr < 0)
    3364           0 :                 goto bailout;
    3365      177367 :         q = newStmt(mb, batRef, appendRef);
    3366      177367 :         if (q == NULL)
    3367           0 :                 goto bailout;
    3368      177367 :         q = pushArgument(mb, q, c->nr);
    3369      177367 :         q = pushArgument(mb, q, a->nr);
    3370      177367 :         q = pushBit(mb, q, TRUE);
    3371      177367 :         bool enabled = be->mvc->sa->eb.enabled;
    3372      177367 :         be->mvc->sa->eb.enabled = false;
    3373      177367 :         stmt *s = stmt_create(be->mvc->sa, st_append);
    3374      177367 :         be->mvc->sa->eb.enabled = enabled;
    3375      177367 :         if(!s) {
    3376           0 :                 freeInstruction(q);
    3377           0 :                 goto bailout;
    3378             :         }
    3379      177367 :         s->op1 = c;
    3380      177367 :         s->op2 = a;
    3381      177367 :         s->nrcols = c->nrcols;
    3382      177367 :         s->key = c->key;
    3383      177367 :         s->nr = getDestVar(q);
    3384      177367 :         s->q = q;
    3385      177367 :         pushInstruction(mb, q);
    3386      177367 :         return s;
    3387             : 
    3388           0 :   bailout:
    3389           0 :         if (be->mvc->sa->eb.enabled)
    3390           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    3391             :         return NULL;
    3392             : }
    3393             : 
    3394             : stmt *
    3395      137726 : stmt_append_bulk(backend *be, stmt *c, list *l)
    3396             : {
    3397      137726 :         MalBlkPtr mb = be->mb;
    3398      137726 :         InstrPtr q = NULL;
    3399      137726 :         bool needs_columns = false;
    3400             : 
    3401      137726 :         if (c->nr < 0)
    3402           0 :                 goto bailout;
    3403             : 
    3404             :         /* currently appendBulk accepts its inputs all either scalar or vectors
    3405             :            if there is one vector and any scala, then the scalars mut be upgraded to vectors */
    3406      888245 :         for (node *n = l->h; n; n = n->next) {
    3407      750519 :                 stmt *t = n->data;
    3408      750519 :                 needs_columns |= t->nrcols > 0;
    3409             :         }
    3410      137726 :         if (needs_columns) {
    3411           0 :                 for (node *n = l->h; n; n = n->next) {
    3412           0 :                         stmt *t = n->data;
    3413           0 :                         if (t->nrcols == 0)
    3414           0 :                                 n->data = const_column(be, t);
    3415             :                 }
    3416             :         }
    3417             : 
    3418      137726 :         q = newStmtArgs(mb, batRef, appendBulkRef, list_length(l) + 3);
    3419      137736 :         if (q == NULL)
    3420           0 :                 goto bailout;
    3421      137736 :         q = pushArgument(mb, q, c->nr);
    3422      137738 :         q = pushBit(mb, q, TRUE);
    3423      888195 :         for (node *n = l->h ; n ; n = n->next) {
    3424      750474 :                 stmt *a = n->data;
    3425      750474 :                 q = pushArgument(mb, q, a->nr);
    3426             :         }
    3427      137721 :         bool enabled = be->mvc->sa->eb.enabled;
    3428      137721 :         be->mvc->sa->eb.enabled = false;
    3429      137721 :         stmt *s = stmt_create(be->mvc->sa, st_append_bulk);
    3430      137719 :         be->mvc->sa->eb.enabled = enabled;
    3431      137719 :         if(!s) {
    3432           0 :                 freeInstruction(q);
    3433           0 :                 goto bailout;
    3434             :         }
    3435      137719 :         s->op1 = c;
    3436      137719 :         s->op4.lval = l;
    3437      137719 :         s->nrcols = c->nrcols;
    3438      137719 :         s->key = c->key;
    3439      137719 :         s->nr = getDestVar(q);
    3440      137719 :         s->q = q;
    3441      137719 :         pushInstruction(mb, q);
    3442      137719 :         return s;
    3443             : 
    3444           0 :   bailout:
    3445           0 :         if (be->mvc->sa->eb.enabled)
    3446           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    3447             :         return NULL;
    3448             : }
    3449             : 
    3450             : stmt *
    3451       15921 : stmt_pack(backend *be, stmt *c, int n)
    3452             : {
    3453       15921 :         MalBlkPtr mb = be->mb;
    3454       15921 :         InstrPtr q = NULL;
    3455             : 
    3456       15921 :         if (c == NULL || c->nr < 0)
    3457           0 :                 goto bailout;
    3458       15921 :         q = newStmtArgs(mb, matRef, packIncrementRef, 3);
    3459       15921 :         if (q == NULL)
    3460           0 :                 goto bailout;
    3461       15921 :         q = pushArgument(mb, q, c->nr);
    3462       15921 :         q = pushInt(mb, q, n);
    3463       15921 :         bool enabled = be->mvc->sa->eb.enabled;
    3464       15921 :         be->mvc->sa->eb.enabled = false;
    3465       15921 :         stmt *s = stmt_create(be->mvc->sa, st_append);
    3466       15921 :         be->mvc->sa->eb.enabled = enabled;
    3467       15921 :         if(!s) {
    3468           0 :                 freeInstruction(q);
    3469           0 :                 goto bailout;
    3470             :         }
    3471       15921 :         s->op1 = c;
    3472       15921 :         s->nrcols = c->nrcols;
    3473       15921 :         s->key = c->key;
    3474       15921 :         s->nr = getDestVar(q);
    3475       15921 :         s->q = q;
    3476       15921 :         pushInstruction(mb, q);
    3477       15921 :         return s;
    3478             : 
    3479           0 :   bailout:
    3480           0 :         if (be->mvc->sa->eb.enabled)
    3481           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    3482             :         return NULL;
    3483             : 
    3484             : }
    3485             : 
    3486             : stmt *
    3487       29641 : stmt_pack_add(backend *be, stmt *c, stmt *a)
    3488             : {
    3489       29641 :         MalBlkPtr mb = be->mb;
    3490       29641 :         InstrPtr q = NULL;
    3491             : 
    3492       29641 :         if (c == NULL || a == NULL || c->nr < 0 || a->nr < 0)
    3493           0 :                 goto bailout;
    3494       29641 :         q = newStmtArgs(mb, matRef, packIncrementRef, 3);
    3495       29641 :         if (q == NULL)
    3496           0 :                 goto bailout;
    3497       29641 :         q = pushArgument(mb, q, c->nr);
    3498       29641 :         q = pushArgument(mb, q, a->nr);
    3499       29641 :         bool enabled = be->mvc->sa->eb.enabled;
    3500       29641 :         be->mvc->sa->eb.enabled = false;
    3501       29641 :         stmt *s = stmt_create(be->mvc->sa, st_append);
    3502       29641 :         be->mvc->sa->eb.enabled = enabled;
    3503       29641 :         if(!s) {
    3504           0 :                 freeInstruction(q);
    3505           0 :                 goto bailout;
    3506             :         }
    3507       29641 :         s->op1 = c;
    3508       29641 :         s->op2 = a;
    3509       29641 :         s->nrcols = c->nrcols;
    3510       29641 :         s->key = c->key;
    3511       29641 :         s->nr = getDestVar(q);
    3512       29641 :         s->q = q;
    3513       29641 :         pushInstruction(mb, q);
    3514       29641 :         return s;
    3515             : 
    3516           0 :   bailout:
    3517           0 :         if (be->mvc->sa->eb.enabled)
    3518           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    3519             :         return NULL;
    3520             : }
    3521             : 
    3522             : stmt *
    3523       95719 : stmt_claim(backend *be, sql_table *t, stmt *cnt)
    3524             : {
    3525       95719 :         MalBlkPtr mb = be->mb;
    3526       95719 :         InstrPtr q = NULL;
    3527             : 
    3528       95719 :         if (!t || cnt->nr < 0)
    3529           0 :                 goto bailout;
    3530       95719 :         assert(t->s);                                /* declared table */
    3531       95719 :         q = newStmtArgs(mb, sqlRef, claimRef, 6);
    3532       95720 :         if (q == NULL)
    3533           0 :                 goto bailout;
    3534             :         /* returns offset or offsets */
    3535       95720 :         q = pushReturn(mb, q, newTmpVariable(mb, newBatType(TYPE_oid)));
    3536       95720 :         q = pushArgument(mb, q, be->mvc_var);
    3537       95720 :         q = pushSchema(mb, q, t);
    3538       95719 :         q = pushStr(mb, q, t->base.name);
    3539       95720 :         q = pushArgument(mb, q, cnt->nr);
    3540       95720 :         bool enabled = be->mvc->sa->eb.enabled;
    3541       95720 :         be->mvc->sa->eb.enabled = false;
    3542       95720 :         stmt *s = stmt_create(be->mvc->sa, st_claim);
    3543       95720 :         be->mvc->sa->eb.enabled = enabled;
    3544       95720 :         if(!s) {
    3545           0 :                 freeInstruction(q);
    3546           0 :                 goto bailout;
    3547             :         }
    3548       95720 :         s->op1 = cnt;
    3549       95720 :         s->op4.tval = t;
    3550       95720 :         s->nr = getDestVar(q);
    3551       95720 :         s->q = q;
    3552       95720 :         pushInstruction(mb, q);
    3553       95720 :         return s;
    3554             : 
    3555           0 :   bailout:
    3556           0 :         if (be->mvc->sa->eb.enabled)
    3557           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    3558             :         return NULL;
    3559             : }
    3560             : 
    3561             : void
    3562      138959 : stmt_add_dependency_change(backend *be, sql_table *t, stmt *cnt)
    3563             : {
    3564      138959 :         MalBlkPtr mb = be->mb;
    3565      138959 :         InstrPtr q = NULL;
    3566             : 
    3567      138959 :         if (!t || cnt->nr < 0)
    3568           0 :                 goto bailout;
    3569      138959 :         q = newStmtArgs(mb, sqlRef, dependRef, 4);
    3570      138960 :         if (q == NULL)
    3571           0 :                 goto bailout;
    3572      138960 :         q = pushSchema(mb, q, t);
    3573      138959 :         q = pushStr(mb, q, t->base.name);
    3574      138958 :         q = pushArgument(mb, q, cnt->nr);
    3575      138958 :         pushInstruction(mb, q);
    3576      138958 :         return;
    3577             : 
    3578           0 :   bailout:
    3579           0 :         if (be->mvc->sa->eb.enabled)
    3580           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : be->mb->errors ? be->mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    3581             : }
    3582             : 
    3583             : void
    3584       14878 : stmt_add_column_predicate(backend *be, sql_column *c)
    3585             : {
    3586       14878 :         MalBlkPtr mb = be->mb;
    3587       14878 :         InstrPtr q = NULL;
    3588             : 
    3589       14878 :         if (!c)
    3590           0 :                 goto bailout;
    3591       14878 :         q = newStmtArgs(mb, sqlRef, predicateRef, 4);
    3592       14878 :         if (q == NULL)
    3593           0 :                 goto bailout;
    3594       14878 :         q = pushSchema(mb, q, c->t);
    3595       14878 :         q = pushStr(mb, q, c->t->base.name);
    3596       14878 :         q = pushStr(mb, q, c->base.name);
    3597       14878 :         pushInstruction(mb, q);
    3598       14878 :         return;
    3599             : 
    3600           0 :   bailout:
    3601           0 :         if (be->mvc->sa->eb.enabled)
    3602           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : be->mb->errors ? be->mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    3603             : }
    3604             : 
    3605             : stmt *
    3606       55240 : stmt_replace(backend *be, stmt *r, stmt *id, stmt *val)
    3607             : {
    3608       55240 :         MalBlkPtr mb = be->mb;
    3609       55240 :         InstrPtr q = NULL;
    3610             : 
    3611       55240 :         if (r->nr < 0)
    3612           0 :                 goto bailout;
    3613             : 
    3614       55240 :         q = newStmt(mb, batRef, replaceRef);
    3615       55240 :         if (q == NULL)
    3616           0 :                 goto bailout;
    3617       55240 :         q = pushArgument(mb, q, r->nr);
    3618       55240 :         q = pushArgument(mb, q, id->nr);
    3619       55240 :         q = pushArgument(mb, q, val->nr);
    3620       55240 :         q = pushBit(mb, q, TRUE); /* forced */
    3621       55240 :         bool enabled = be->mvc->sa->eb.enabled;
    3622       55240 :         be->mvc->sa->eb.enabled = false;
    3623       55240 :         stmt *s = stmt_create(be->mvc->sa, st_replace);
    3624       55240 :         be->mvc->sa->eb.enabled = enabled;
    3625       55240 :         if(!s) {
    3626           0 :                 freeInstruction(q);
    3627           0 :                 goto bailout;
    3628             :         }
    3629       55240 :         s->op1 = r;
    3630       55240 :         s->op2 = id;
    3631       55240 :         s->op3 = val;
    3632       55240 :         s->nrcols = r->nrcols;
    3633       55240 :         s->key = r->key;
    3634       55240 :         s->nr = getDestVar(q);
    3635       55240 :         s->q = q;
    3636       55240 :         s->cand = r->cand;
    3637       55240 :         pushInstruction(mb, q);
    3638       55240 :         return s;
    3639             : 
    3640           0 :   bailout:
    3641           0 :         if (be->mvc->sa->eb.enabled)
    3642           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    3643             :         return NULL;
    3644             : }
    3645             : 
    3646             : stmt *
    3647       41827 : stmt_table_clear(backend *be, sql_table *t, int restart_sequences)
    3648             : {
    3649       41827 :         MalBlkPtr mb = be->mb;
    3650       41827 :         InstrPtr q = NULL;
    3651             : 
    3652       41827 :         if (!t->s && ATOMIC_PTR_GET(&t->data)) { /* declared table */
    3653           2 :                 int *l = ATOMIC_PTR_GET(&t->data), cnt = ol_length(t->columns)+1;
    3654             : 
    3655           6 :                 for (int i = 0; i < cnt; i++) {
    3656           4 :                         q = newStmt(mb, batRef, deleteRef);
    3657           4 :                         if (q == NULL)
    3658           0 :                                 goto bailout;
    3659           4 :                         q = pushArgument(mb, q, l[i]);
    3660           4 :                         pushInstruction(mb, q);
    3661             :                 }
    3662             :                 /* declared tables don't have sequences */
    3663             :         } else {
    3664       41825 :                 q = newStmt(mb, sqlRef, clear_tableRef);
    3665       41825 :                 if (q == NULL)
    3666           0 :                         goto bailout;
    3667       41825 :                 q = pushSchema(mb, q, t);
    3668       41824 :                 q = pushStr(mb, q, t->base.name);
    3669       41824 :                 q = pushInt(mb, q, restart_sequences);
    3670       41825 :                 pushInstruction(mb, q);
    3671             :         }
    3672       41827 :         stmt *s = stmt_create(be->mvc->sa, st_table_clear);
    3673             : 
    3674       41826 :         if(!s) {
    3675           0 :                 goto bailout;
    3676             :         }
    3677       41826 :         s->op4.tval = t;
    3678       41826 :         s->nrcols = 0;
    3679       41826 :         s->nr = getDestVar(q);
    3680       41826 :         s->q = q;
    3681       41826 :         return s;
    3682             : 
    3683           0 :   bailout:
    3684           0 :         if (be->mvc->sa->eb.enabled)
    3685           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    3686             :         return NULL;
    3687             : }
    3688             : 
    3689             : stmt *
    3690       39493 : stmt_exception(backend *be, stmt *cond, const char *errstr, int errcode)
    3691             : {
    3692       39493 :         MalBlkPtr mb = be->mb;
    3693       39493 :         InstrPtr q = NULL;
    3694             : 
    3695       39493 :         if (cond->nr < 0)
    3696             :                 return NULL;
    3697             : 
    3698             :         /* if(bit(l)) { error(r);}  ==raising an exception */
    3699       39493 :         q = newStmt(mb, sqlRef, assertRef);
    3700       39493 :         if (q == NULL)
    3701           0 :                 goto bailout;
    3702       39493 :         q = pushArgument(mb, q, cond->nr);
    3703       39493 :         q = pushStr(mb, q, errstr);
    3704       39493 :         bool enabled = be->mvc->sa->eb.enabled;
    3705       39493 :         be->mvc->sa->eb.enabled = false;
    3706       39493 :         stmt *s = stmt_create(be->mvc->sa, st_exception);
    3707       39493 :         be->mvc->sa->eb.enabled = enabled;
    3708       39493 :         if(!s) {
    3709           0 :                 freeInstruction(q);
    3710           0 :                 return NULL;
    3711             :         }
    3712       39493 :         assert(cond);
    3713       39493 :         s->op1 = cond;
    3714       39493 :         (void)errcode;
    3715       39493 :         s->nrcols = 0;
    3716       39493 :         s->q = q;
    3717       39493 :         s->nr = getDestVar(q);
    3718       39493 :         pushInstruction(mb, q);
    3719       39493 :         return s;
    3720             : 
    3721           0 :   bailout:
    3722           0 :         if (be->mvc->sa->eb.enabled)
    3723           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    3724             :         return NULL;
    3725             : }
    3726             : 
    3727             : /* The type setting is not propagated to statements such as st_bat and st_append,
    3728             :         because they are not considered projections */
    3729             : static void
    3730       14941 : tail_set_type(mvc *m, stmt *st, sql_subtype *t)
    3731             : {
    3732       40007 :         for (;;) {
    3733       40007 :                 switch (st->type) {
    3734        3688 :                 case st_const:
    3735        3688 :                         st = st->op2;
    3736        3688 :                         continue;
    3737       18003 :                 case st_alias:
    3738             :                 case st_gen_group:
    3739             :                 case st_order:
    3740       18003 :                         st = st->op1;
    3741       18003 :                         continue;
    3742           0 :                 case st_list:
    3743           0 :                         st = st->op4.lval->h->data;
    3744           0 :                         continue;
    3745        3375 :                 case st_join:
    3746             :                 case st_join2:
    3747             :                 case st_joinN:
    3748        3375 :                         if (st->flag == cmp_project) {
    3749        3375 :                                 st = st->op2;
    3750        3375 :                                 continue;
    3751             :                         }
    3752             :                         return;
    3753        2783 :                 case st_aggr:
    3754             :                 case st_Nop: {
    3755        2783 :                         list *res = st->op4.funcval->res;
    3756             : 
    3757        2783 :                         if (res && list_length(res) == 1)
    3758        2783 :                                 res->h->data = t;
    3759             :                         return;
    3760             :                 }
    3761        6863 :                 case st_atom:
    3762        6863 :                         st->op4.aval = atom_set_type(m->sa, st->op4.aval, t);
    3763        6863 :                         return;
    3764          60 :                 case st_convert:
    3765             :                 case st_temp:
    3766             :                 case st_single:
    3767          60 :                         st->op4.typeval = *t;
    3768          60 :                         return;
    3769          86 :                 case st_var:
    3770          86 :                         if (st->op4.typeval.type)
    3771          86 :                                 st->op4.typeval = *t;
    3772             :                         return;
    3773             :                 default:
    3774             :                         return;
    3775             :                 }
    3776             :         }
    3777             : }
    3778             : 
    3779             : #define trivial_string_conversion(x) ((x) == EC_BIT || (x) == EC_CHAR || (x) == EC_STRING || (x) == EC_NUM || (x) == EC_POS || (x) == EC_FLT \
    3780             :                                                                           || (x) == EC_DATE || (x) == EC_BLOB || (x) == EC_MONTH)
    3781             : 
    3782             : static stmt *
    3783         351 : temporal_convert(backend *be, stmt *v, stmt *sel, sql_subtype *f, sql_subtype *t, bool before)
    3784             : {
    3785         351 :         MalBlkPtr mb = be->mb;
    3786         351 :         InstrPtr q = NULL;
    3787         351 :         const char *convert = t->type->impl, *mod = mtimeRef;
    3788         351 :         bool add_tz = false, pushed = (v->cand && v->cand == sel), cand = 0;
    3789             : 
    3790         351 :         if (before) {
    3791         290 :                 if (f->type->eclass == EC_TIMESTAMP_TZ && (t->type->eclass == EC_TIMESTAMP || t->type->eclass == EC_TIME)) {
    3792             :                         /* call timestamp+local_timezone */
    3793             :                         convert = "timestamp_add_msec_interval";
    3794             :                         add_tz = true;
    3795         211 :                 } else if (f->type->eclass == EC_TIMESTAMP_TZ && t->type->eclass == EC_DATE) {
    3796             :                         /* call convert timestamp with tz to date */
    3797          13 :                         convert = "datetz";
    3798          13 :                         mod = calcRef;
    3799          13 :                         add_tz = true;
    3800         198 :                 } else if (f->type->eclass == EC_TIMESTAMP && t->type->eclass == EC_TIMESTAMP_TZ) {
    3801             :                         /* call timestamp+local_timezone */
    3802             :                         convert = "timestamp_sub_msec_interval";
    3803             :                         add_tz = true;
    3804         159 :                 } else if (f->type->eclass == EC_TIME_TZ && (t->type->eclass == EC_TIME || t->type->eclass == EC_TIMESTAMP)) {
    3805             :                         /* call times+local_timezone */
    3806             :                         convert = "time_add_msec_interval";
    3807             :                         add_tz = true;
    3808         146 :                 } else if (f->type->eclass == EC_TIME && t->type->eclass == EC_TIME_TZ) {
    3809             :                         /* call times+local_timezone */
    3810             :                         convert = "time_sub_msec_interval";
    3811             :                         add_tz = true;
    3812         131 :                 } else if (EC_VARCHAR(f->type->eclass) && EC_TEMP_TZ(t->type->eclass)) {
    3813         124 :                         if (t->type->eclass == EC_TIME_TZ)
    3814             :                                 convert = "daytimetz";
    3815             :                         else
    3816         100 :                                 convert = "timestamptz";
    3817         124 :                         mod = calcRef;
    3818         124 :                         add_tz = true;
    3819         124 :                         cand = 1;
    3820             :                 } else {
    3821             :                         return v;
    3822             :                 }
    3823             :         } else {
    3824          61 :                 if (f->type->eclass == EC_DATE && t->type->eclass == EC_TIMESTAMP_TZ) {
    3825             :                         convert = "timestamp_sub_msec_interval";
    3826             :                         add_tz = true;
    3827          56 :                 } else if (f->type->eclass == EC_DATE && t->type->eclass == EC_TIME_TZ) {
    3828             :                         convert = "time_sub_msec_interval";
    3829             :                         add_tz = true;
    3830             :                 } else {
    3831             :                         return v;
    3832             :                 }
    3833             :         }
    3834             : 
    3835         288 :         if (v->nrcols == 0 && (!sel || sel->nrcols == 0)) {       /* simple calc */
    3836         171 :                 q = newStmtArgs(mb, mod, convert, 13);
    3837         171 :                 if (q == NULL)
    3838           0 :                         goto bailout;
    3839             :         } else {
    3840         117 :                 if (sel && !pushed && v->nrcols == 0) {
    3841           1 :                         pushed = 1;
    3842           1 :                         v = stmt_project(be, sel, v);
    3843           1 :                         v->cand = sel;
    3844             :                 }
    3845         117 :                 q = newStmtArgs(mb, mod==calcRef?batcalcRef:batmtimeRef, convert, 13);
    3846         117 :                 if (q == NULL)
    3847           0 :                         goto bailout;
    3848             :         }
    3849         288 :         q = pushArgument(mb, q, v->nr);
    3850             : 
    3851         288 :         if (cand) {
    3852         124 :                 if (sel && !pushed && !v->cand) {
    3853          14 :                         q = pushArgument(mb, q, sel->nr);
    3854          14 :                         pushed = 1;
    3855         110 :                 } else if (v->nrcols > 0) {
    3856          18 :                         q = pushNilBat(mb, q);
    3857             :                 }
    3858             :         }
    3859             : 
    3860         288 :         if (EC_VARCHAR(f->type->eclass))
    3861         124 :                 q = pushInt(mb, q, t->digits);
    3862             : 
    3863         288 :         if (add_tz)
    3864         288 :                         q = pushLng(mb, q, be->mvc->timezone);
    3865             : 
    3866         288 :         if (!cand) {
    3867         164 :                 if (sel && !pushed && !v->cand) {
    3868           2 :                         q = pushArgument(mb, q, sel->nr);
    3869           2 :                         pushed = 1;
    3870         162 :                 } else if (v->nrcols > 0) {
    3871          83 :                         q = pushNilBat(mb, q);
    3872             :                 }
    3873             :         }
    3874             : 
    3875         288 :         bool enabled = be->mvc->sa->eb.enabled;
    3876         288 :         be->mvc->sa->eb.enabled = false;
    3877         288 :         stmt *s = stmt_create(be->mvc->sa, st_convert);
    3878         288 :         be->mvc->sa->eb.enabled = enabled;
    3879         288 :         if(!s) {
    3880           0 :                 freeInstruction(q);
    3881           0 :                 goto bailout;
    3882             :         }
    3883         288 :         s->op1 = v;
    3884         288 :         s->nrcols = 0;       /* function without arguments returns single value */
    3885         288 :         s->key = v->key;
    3886         288 :         s->nrcols = v->nrcols;
    3887         288 :         s->aggr = v->aggr;
    3888         288 :         s->op4.typeval = *t;
    3889         288 :         s->nr = getDestVar(q);
    3890         288 :         s->q = q;
    3891         288 :         s->cand = pushed ? sel : NULL;
    3892         288 :         pushInstruction(mb, q);
    3893         288 :         return s;
    3894             : 
    3895           0 :   bailout:
    3896           0 :         if (be->mvc->sa->eb.enabled)
    3897           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    3898             :         return NULL;
    3899             : }
    3900             : 
    3901             : stmt *
    3902       51492 : stmt_convert(backend *be, stmt *v, stmt *sel, sql_subtype *f, sql_subtype *t)
    3903             : {
    3904       51492 :         MalBlkPtr mb = be->mb;
    3905       51492 :         InstrPtr q = NULL;
    3906       51492 :         const char *convert = t->type->impl, *mod = calcRef;
    3907       51492 :         int pushed = (v->cand && v->cand == sel), no_candidates = 0;
    3908       51492 :         bool add_tz = false;
    3909             :         /* convert types and make sure they are rounded up correctly */
    3910             : 
    3911       51492 :         if (v->nr < 0)
    3912           0 :                 goto bailout;
    3913             : 
    3914       51492 :         if (f->type->eclass != EC_EXTERNAL && t->type->eclass != EC_EXTERNAL &&
    3915             :                 /* general cases */
    3916       51134 :                 ((t->type->localtype == f->type->localtype && t->type->eclass == f->type->eclass &&
    3917       51134 :                 !EC_INTERVAL(f->type->eclass) && f->type->eclass != EC_DEC && (t->digits == 0 || f->digits == t->digits) && type_has_tz(t) == type_has_tz(f)) ||
    3918             :                 /* trivial decimal cases */
    3919        2043 :                 (f->type->eclass == EC_DEC && t->type->eclass == EC_DEC && f->scale == t->scale && f->type->localtype == t->type->localtype) ||
    3920             :                 /* trivial string cases */
    3921       44267 :                 (EC_VARCHAR(f->type->eclass) && EC_VARCHAR(t->type->eclass) && (t->digits == 0 || (f->digits > 0 && t->digits >= f->digits))))) {
    3922             :                 /* set output type. Despite the MAL code already being generated, the output type may still be checked */
    3923       14941 :                 tail_set_type(be->mvc, v, t);
    3924       14941 :                 return v;
    3925             :         }
    3926             : 
    3927             :         /* external types have sqlname convert functions,
    3928             :            these can generate errors (fromstr cannot) */
    3929       36551 :         if (t->type->eclass == EC_EXTERNAL)
    3930         331 :                 convert = t->type->base.name;
    3931       36220 :         else if (t->type->eclass == EC_MONTH)
    3932             :                 convert = "month_interval";
    3933       36197 :         else if (t->type->eclass == EC_SEC)
    3934         100 :                 convert = "second_interval";
    3935             : 
    3936       36551 :         no_candidates = t->type->eclass == EC_EXTERNAL && strcmp(convert, "uuid") != 0; /* uuids conversions support candidate lists */
    3937             : 
    3938       36551 :         if ((type_has_tz(f) && !type_has_tz(t) && !EC_VARCHAR(t->type->eclass)) || (!type_has_tz(f) && type_has_tz(t))) {
    3939         290 :                 v = temporal_convert(be, v, sel, f, t, true);
    3940         290 :                 sel = NULL;
    3941         290 :                 pushed = 0;
    3942         290 :                 if (EC_VARCHAR(f->type->eclass))
    3943             :                         return v;
    3944             :         }
    3945             : 
    3946             :         /* Lookup the sql convert function, there is no need
    3947             :          * for single value vs bat, this is handled by the
    3948             :          * mal function resolution */
    3949       36427 :         if (v->nrcols == 0 && (!sel || sel->nrcols == 0)) {       /* simple calc */
    3950        8244 :                 q = newStmtArgs(mb, mod, convert, 13);
    3951        8244 :                 if (q == NULL)
    3952           0 :                         goto bailout;
    3953       28204 :         } else if ((v->nrcols > 0 || (sel && sel->nrcols > 0)) && no_candidates) {
    3954          21 :                 int type = t->type->localtype;
    3955             : 
    3956             :                 /* with our current implementation, all internal SQL types have candidate list support on their conversions */
    3957          21 :                 if (sel && !pushed) {
    3958           0 :                         pushed = 1;
    3959           0 :                         v = stmt_project(be, sel, v);
    3960           0 :                         v->cand = sel;
    3961             :                 }
    3962          21 :                 q = newStmtArgs(mb, malRef, multiplexRef, 15);
    3963          21 :                 if (q == NULL)
    3964           0 :                         goto bailout;
    3965          21 :                 setVarType(mb, getArg(q, 0), newBatType(type));
    3966          21 :                 q = pushStr(mb, q, convertMultiplexMod(mod, convert));
    3967          21 :                 q = pushStr(mb, q, convertMultiplexFcn(convert));
    3968             :         } else {
    3969       28162 :                 if (v->nrcols == 0 && sel && !pushed) {
    3970         905 :                         pushed = 1;
    3971         905 :                         v = stmt_project(be, sel, v);
    3972         905 :                         v->cand = sel;
    3973             :                 }
    3974       28162 :                 q = newStmtArgs(mb, mod==calcRef?batcalcRef:batmtimeRef, convert, 13);
    3975       28162 :                 if (q == NULL)
    3976           0 :                         goto bailout;
    3977             :         }
    3978             : 
    3979             :         /* convert to string is complex, we need full type info and mvc for the timezone */
    3980       36601 :         if (EC_VARCHAR(t->type->eclass) && !(trivial_string_conversion(f->type->eclass) && t->digits == 0)) {
    3981        1850 :                 q = pushInt(mb, q, f->type->eclass);
    3982        1850 :                 q = pushInt(mb, q, f->digits);
    3983        1850 :                 q = pushInt(mb, q, f->scale);
    3984        1850 :                 q = pushInt(mb, q, type_has_tz(f));
    3985       34577 :         } else if (f->type->eclass == EC_DEC) {
    3986             :                 /* scale of the current decimal */
    3987        1664 :                 q = pushInt(mb, q, f->scale);
    3988       32913 :         } else if (f->type->eclass == EC_SEC && (EC_COMPUTE(t->type->eclass) || t->type->eclass == EC_DEC)) {
    3989             :                 /* scale of the current decimal */
    3990           0 :                 q = pushInt(mb, q, 3);
    3991             :         }
    3992       36427 :         q = pushArgument(mb, q, v->nr);
    3993       36427 :         if (add_tz)
    3994             :                         q = pushLng(mb, q, be->mvc->timezone);
    3995       36427 :         if (sel && !pushed && !v->cand) {
    3996        3489 :                 q = pushArgument(mb, q, sel->nr);
    3997        3489 :                 pushed = 1;
    3998       32938 :         } else if (v->nrcols > 0 && !no_candidates) {
    3999       24673 :                 q = pushNilBat(mb, q);
    4000             :         }
    4001       36427 :         if (!add_tz && (t->type->eclass == EC_DEC || EC_TEMP_FRAC(t->type->eclass) || EC_INTERVAL(t->type->eclass))) {
    4002             :                 /* digits, scale of the result decimal */
    4003        3168 :                 q = pushInt(mb, q, t->digits);
    4004        3168 :                 if (!EC_TEMP_FRAC(t->type->eclass))
    4005        2829 :                         q = pushInt(mb, q, t->scale);
    4006             :         }
    4007             :         /* convert to string, give error on to large strings */
    4008       36601 :         if (EC_VARCHAR(t->type->eclass) && !(trivial_string_conversion(f->type->eclass) && t->digits == 0))
    4009        1850 :                 q = pushInt(mb, q, t->digits);
    4010             :         /* convert a string to a time(stamp) with time zone */
    4011       36427 :         if (EC_VARCHAR(f->type->eclass) && EC_TEMP_TZ(t->type->eclass))
    4012             :                 //q = pushInt(mb, q, type_has_tz(t));
    4013           0 :                 q = pushLng(mb, q, be->mvc->timezone);
    4014       36427 :         if (t->type->eclass == EC_GEOM) {
    4015             :                 /* push the type and coordinates of the column */
    4016         687 :                 q = pushInt(mb, q, t->digits);
    4017             :                 /* push the SRID of the whole columns */
    4018         687 :                 q = pushInt(mb, q, t->scale);
    4019             :                 /* push the type and coordinates of the inserted value */
    4020             :                 //q = pushInt(mb, q, f->digits);
    4021             :                 /* push the SRID of the inserted value */
    4022             :                 //q = pushInt(mb, q, f->scale);
    4023             :                 /* we decided to create the EWKB type also used by PostGIS and has the SRID provided by the user inside already */
    4024             :                 /* push the SRID provided for this value */
    4025             :                 /* GEOS library is able to store in the returned wkb the type an
    4026             :                  * number if coordinates but not the SRID so SRID should be provided
    4027             :                  * from this level */
    4028             : /*              if(be->argc > 1)
    4029             :                         f->scale = ((ValRecord)((atom*)(be->mvc)->args[1])->data).val.ival;
    4030             : 
    4031             :                         q = pushInt(mb, q, f->digits);
    4032             :                         q = pushInt(mb, q, f->scale);
    4033             : */                      //q = pushInt(mb, q, ((ValRecord)((atom*)(be->mvc)->args[1])->data).val.ival);
    4034             :         }
    4035             : 
    4036       36427 :         bool enabled = be->mvc->sa->eb.enabled;
    4037       36427 :         be->mvc->sa->eb.enabled = false;
    4038       36427 :         stmt *s = stmt_create(be->mvc->sa, st_convert);
    4039       36427 :         be->mvc->sa->eb.enabled = enabled;
    4040       36427 :         if(!s) {
    4041           0 :                 freeInstruction(q);
    4042           0 :                 goto bailout;
    4043             :         }
    4044       36427 :         s->op1 = v;
    4045       36427 :         s->nrcols = 0;       /* function without arguments returns single value */
    4046       36427 :         s->key = v->key;
    4047       36427 :         s->nrcols = v->nrcols;
    4048       36427 :         s->aggr = v->aggr;
    4049       36427 :         s->op4.typeval = *t;
    4050       36427 :         s->nr = getDestVar(q);
    4051       36427 :         s->q = q;
    4052       36427 :         s->cand = pushed ? sel : NULL;
    4053       36427 :         pushInstruction(mb, q);
    4054       36427 :         if ((!type_has_tz(f) && type_has_tz(t)))
    4055          61 :                 return temporal_convert(be, s, NULL, f, t, false);
    4056             :         return s;
    4057             : 
    4058           0 :   bailout:
    4059           0 :         if (be->mvc->sa->eb.enabled)
    4060           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    4061             :         return NULL;
    4062             : }
    4063             : 
    4064             : stmt *
    4065       27045 : stmt_unop(backend *be, stmt *op1, stmt *sel, sql_subfunc *op)
    4066             : {
    4067       27045 :         list *ops = sa_list(be->mvc->sa);
    4068       27045 :         list_append(ops, op1);
    4069       27045 :         stmt *r = stmt_Nop(be, stmt_list(be, ops), sel, op, NULL);
    4070       27045 :         if (r && !r->cand)
    4071       27045 :                 r->cand = op1->cand;
    4072       27045 :         return r;
    4073             : }
    4074             : 
    4075             : stmt *
    4076       44191 : stmt_binop(backend *be, stmt *op1, stmt *op2, stmt *sel, sql_subfunc *op)
    4077             : {
    4078       44191 :         list *ops = sa_list(be->mvc->sa);
    4079       44191 :         list_append(ops, op1);
    4080       44191 :         list_append(ops, op2);
    4081       44191 :         stmt *r = stmt_Nop(be, stmt_list(be, ops), sel, op, NULL);
    4082       44191 :         if (r && !r->cand)
    4083       44191 :                 r->cand = op1->cand?op1->cand:op2->cand;
    4084       44191 :         return r;
    4085             : }
    4086             : 
    4087             : #define LANG_INT_OR_MAL(l)  ((l)==FUNC_LANG_INT || (l)==FUNC_LANG_MAL)
    4088             : 
    4089             : stmt *
    4090      216449 : stmt_Nop(backend *be, stmt *ops, stmt *sel, sql_subfunc *f, stmt* rows)
    4091             : {
    4092      216449 :         MalBlkPtr mb = be->mb;
    4093      216449 :         InstrPtr q = NULL;
    4094      216449 :         const char *mod = sql_func_mod(f->func), *fimp = backend_function_imp(be, f->func);
    4095      216447 :         sql_subtype *tpe = NULL;
    4096      216447 :         int push_cands = 0, default_nargs;
    4097      216447 :         stmt *o = NULL, *card = NULL;
    4098             : 
    4099      216447 :         if (ops == NULL)
    4100           0 :                 goto bailout;
    4101             : 
    4102      216447 :         if (rows) {
    4103         101 :                 if (sel) /* if there's a candidate list, use it instead of 'rows' */
    4104           0 :                         rows = sel;
    4105             :                 o = rows;
    4106      216346 :         } else if (list_length(ops->op4.lval)) {
    4107      214545 :                 o = ops->op4.lval->h->data;
    4108      640706 :                 for (node *n = ops->op4.lval->h; n; n = n->next) {
    4109      426161 :                         stmt *c = n->data;
    4110             : 
    4111      426161 :                         if (c && o->nrcols < c->nrcols)
    4112      426161 :                                 o = c;
    4113             :                 }
    4114             :         }
    4115             : 
    4116             :         /* handle nullif */
    4117      216447 :         if (list_length(ops->op4.lval) == 2 &&
    4118      134992 :                 strcmp(mod, "") == 0 && strcmp(fimp, "") == 0) {
    4119          95 :                 stmt *e1 = ops->op4.lval->h->data;
    4120          95 :                 stmt *e2 = ops->op4.lval->h->next->data;
    4121          95 :                 int nrcols = 0;
    4122             : 
    4123          95 :                 nrcols = e1->nrcols>e2->nrcols ? e1->nrcols:e2->nrcols;
    4124             :                 /* nullif(e1,e2) -> ifthenelse(e1==e2),NULL,e1) */
    4125          95 :                 if (strcmp(f->func->base.name, "nullif") == 0) {
    4126          95 :                         const char *mod = (!nrcols)?calcRef:batcalcRef;
    4127          95 :                         sql_subtype *t = tail_type(e1);
    4128          95 :                         int tt = t->type->localtype;
    4129          95 :                         q = newStmt(mb, mod, "==");
    4130          95 :                         if (q == NULL)
    4131           0 :                                 goto bailout;
    4132          95 :                         q = pushArgument(mb, q, e1->nr);
    4133          95 :                         q = pushArgument(mb, q, e2->nr);
    4134          95 :                         int nr = getDestVar(q);
    4135          95 :                         pushInstruction(mb, q);
    4136             : 
    4137          95 :                         q = newStmt(mb, mod, ifthenelseRef);
    4138          95 :                         if (q == NULL)
    4139           0 :                                 goto bailout;
    4140          95 :                         q = pushArgument(mb, q, nr);
    4141          95 :                         q = pushNil(mb, q, tt);
    4142          95 :                         q = pushArgument(mb, q, e1->nr);
    4143          95 :                         pushInstruction(mb, q);
    4144             :                 }
    4145          95 :                 push_cands = f->func->type == F_FUNC && can_push_cands(sel, mod, fimp);
    4146             :         }
    4147          95 :         if (q == NULL) {
    4148      216354 :                 if (backend_create_subfunc(be, f, ops->op4.lval) < 0)
    4149           2 :                         goto bailout;
    4150      216351 :                 mod = sql_func_mod(f->func);
    4151      216351 :                 fimp = convertMultiplexFcn(backend_function_imp(be, f->func));
    4152      216351 :                 push_cands = f->func->type == F_FUNC && can_push_cands(sel, mod, fimp);
    4153      225354 :                 default_nargs = (f->res && list_length(f->res) ? list_length(f->res) : 1) + list_length(ops->op4.lval) + (o && o->nrcols > 0 ? 6 : 4);
    4154      216351 :                 if (rows) {
    4155         101 :                         card = stmt_aggr(be, rows, NULL, NULL, sql_bind_func(be->mvc, "sys", "count", sql_bind_localtype("void"), NULL, F_AGGR, true, true), 1, 0, 1);
    4156         101 :                         default_nargs++;
    4157             :                 }
    4158             : 
    4159      216351 :                 if (o && o->nrcols > 0 && f->func->type != F_LOADER && f->func->type != F_PROC) {
    4160      136754 :                         sql_subtype *res = f->res->h->data;
    4161             : 
    4162      136754 :                         q = newStmtArgs(mb, f->func->type == F_UNION ? batmalRef : malRef, multiplexRef, default_nargs);
    4163      136754 :                         if (q == NULL)
    4164           0 :                                 goto bailout;
    4165      136754 :                         if (rows)
    4166         101 :                                 q = pushArgument(mb, q, card->nr);
    4167      136754 :                         q = pushStr(mb, q, mod);
    4168      136754 :                         q = pushStr(mb, q, fimp);
    4169      136754 :                         setVarType(mb, getArg(q, 0), newBatType(res->type->localtype));
    4170             :                 } else {
    4171       79597 :                         q = newStmtArgs(mb, mod, fimp, default_nargs);
    4172       79598 :                         if (q == NULL)
    4173           0 :                                 goto bailout;
    4174             : 
    4175       79598 :                         if (rows)
    4176           0 :                                 q = pushArgument(mb, q, card->nr);
    4177       79598 :                         if (f->res && list_length(f->res)) {
    4178       70595 :                                 sql_subtype *res = f->res->h->data;
    4179             : 
    4180       70595 :                                 setVarType(mb, getArg(q, 0), res->type->localtype);
    4181             :                         }
    4182             :                 }
    4183      216352 :                 if (LANG_EXT(f->func->lang)) {
    4184             :                         /* TODO LOADER functions still use information in sql_subfunc struct
    4185             :                            that won't be visible to other sessions if another function uses them.
    4186             :                            It has to be cleaned up */
    4187         221 :                         if (f->func->type == F_LOADER)
    4188          25 :                                 q = pushPtr(mb, q, f);
    4189             :                         else
    4190         196 :                                 q = pushPtr(mb, q, f->func);
    4191             :                 }
    4192      216352 :                 if (f->func->lang == FUNC_LANG_C) {
    4193          35 :                         q = pushBit(mb, q, 0);
    4194      216317 :                 } else if (f->func->lang == FUNC_LANG_CPP) {
    4195           1 :                         q = pushBit(mb, q, 1);
    4196             :                 }
    4197      216352 :                 if (f->func->lang == FUNC_LANG_R || f->func->lang >= FUNC_LANG_PY ||
    4198             :                         f->func->lang == FUNC_LANG_C || f->func->lang == FUNC_LANG_CPP) {
    4199         221 :                         q = pushStr(mb, q, f->func->query);
    4200             :                 }
    4201             :                 /* first dynamic output of copy* functions */
    4202      216352 :                 if (f->func->type == F_UNION || (f->func->type == F_LOADER && f->res != NULL))
    4203        2677 :                         q = table_func_create_result(mb, q, f->func, f->res);
    4204      216352 :                 if (list_length(ops->op4.lval))
    4205      214536 :                         tpe = tail_type(ops->op4.lval->h->data);
    4206             : 
    4207      642495 :                 for (node *n = ops->op4.lval->h; n; n = n->next) {
    4208      426143 :                         stmt *op = n->data;
    4209      426143 :                         q = pushArgument(mb, q, op->nr);
    4210             :                 }
    4211             :                 /* push candidate lists if that's the case */
    4212      216352 :                 if (push_cands) {
    4213       76716 :                         for (node *n = ops->op4.lval->h; n; n = n->next) {
    4214       50601 :                                 stmt *op = n->data;
    4215             : 
    4216       50601 :                                 if (op->nrcols > 0) {
    4217       28427 :                                         if (op->cand && op->cand == sel) {
    4218        3838 :                                                 q = pushNilBat(mb, q);
    4219             :                                         } else {
    4220       24589 :                                                 q = pushArgument(mb, q, sel->nr);
    4221             :                                         }
    4222             :                                 }
    4223             :                         }
    4224             :                 }
    4225             :                 /* special case for round function on decimals */
    4226      216352 :                 if (LANG_INT_OR_MAL(f->func->lang) && strcmp(fimp, "round") == 0 && tpe && tpe->type->eclass == EC_DEC && ops->op4.lval->h && ops->op4.lval->h->data) {
    4227          48 :                         q = pushInt(mb, q, tpe->digits);
    4228          48 :                         q = pushInt(mb, q, tpe->scale);
    4229             :                 }
    4230      216352 :                 pushInstruction(mb, q);
    4231             :         }
    4232             : 
    4233      216447 :         stmt *s = stmt_create(be->mvc->sa, st_Nop);
    4234      216447 :         if(!s) {
    4235           0 :                 goto bailout;
    4236             :         }
    4237      216447 :         s->op1 = ops;
    4238      216447 :         if (o) {
    4239      214647 :                 s->nrcols = o->nrcols;
    4240      214647 :                 s->key = o->key;
    4241      214647 :                 s->aggr = o->aggr;
    4242             :         } else {
    4243        1800 :                 s->nrcols = 0;
    4244        1800 :                 s->key = 1;
    4245             :         }
    4246      216447 :         s->op4.funcval = f;
    4247      216447 :         s->nr = getDestVar(q);
    4248      216447 :         s->q = q;
    4249      216447 :         if (sel && push_cands && s->nrcols)
    4250       24600 :                 s->cand = sel;
    4251             :         return s;
    4252             : 
    4253           2 :   bailout:
    4254           2 :         if (be->mvc->sa->eb.enabled)
    4255           2 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    4256             :         return NULL;
    4257             : }
    4258             : 
    4259             : stmt *
    4260          23 : stmt_direct_func(backend *be, InstrPtr q)
    4261             : {
    4262          23 :         if (q) {
    4263          23 :                 stmt *s = stmt_create(be->mvc->sa, st_func);
    4264          23 :                 if(!s) {
    4265             :                         return NULL;
    4266             :                 }
    4267          23 :                 s->flag = op_union;
    4268          23 :                 s->nrcols = 3;
    4269          23 :                 s->nr = getDestVar(q);
    4270          23 :                 s->q = q;
    4271          23 :                 return s;
    4272             :         }
    4273             :         return NULL;
    4274             : }
    4275             : 
    4276             : stmt *
    4277         186 : stmt_func(backend *be, stmt *ops, const char *name, sql_rel *rel, int f_union)
    4278             : {
    4279         186 :         MalBlkPtr mb = be->mb;
    4280         186 :         InstrPtr q = NULL;
    4281         186 :         prop *p = NULL;
    4282             : 
    4283             :         /* dump args */
    4284         186 :         if (ops && ops->nr < 0)
    4285           0 :                 goto bailout;
    4286             : 
    4287         186 :         if ((p = find_prop(rel->p, PROP_REMOTE)))
    4288         186 :                 rel->p = prop_remove(rel->p, p);
    4289             :         /* sql_processrelation may split projections, so make sure the topmost relation only contains references */
    4290         186 :         rel = rel_project(be->mvc->sa, rel, rel_projections(be->mvc, rel, NULL, 1, 1));
    4291         186 :         if (!(rel = sql_processrelation(be->mvc, rel, 0, 0, 1, 1)))
    4292           0 :                 goto bailout;
    4293         186 :         if (p) {
    4294         186 :                 p->p = rel->p;
    4295         186 :                 rel->p = p;
    4296             :         }
    4297             : 
    4298         186 :         if (monet5_create_relational_function(be->mvc, sql_private_module_name, name, rel, ops, NULL, 1) < 0)
    4299           0 :                 goto bailout;
    4300             : 
    4301         186 :         int nargs;
    4302         186 :         sql_rel *r = relational_func_create_result_part1(be->mvc, rel, &nargs);
    4303         186 :         if (ops)
    4304         186 :                 nargs += list_length(ops->op4.lval);
    4305         186 :         if (f_union)
    4306           0 :                 q = newStmt(mb, batmalRef, multiplexRef);
    4307             :         else
    4308         186 :                 q = newStmt(mb, sql_private_module_name, name);
    4309         186 :         if (q == NULL)
    4310           0 :                 goto bailout;
    4311         186 :         q = relational_func_create_result_part2(mb, q, r);
    4312         186 :         if (f_union) {
    4313           0 :                 q = pushStr(mb, q, sql_private_module_name);
    4314           0 :                 q = pushStr(mb, q, name);
    4315             :         }
    4316         186 :         if (ops) {
    4317         208 :                 for (node *n = ops->op4.lval->h; n; n = n->next) {
    4318          22 :                         stmt *op = n->data;
    4319             : 
    4320          22 :                         q = pushArgument(mb, q, op->nr);
    4321             :                 }
    4322             :         }
    4323             : 
    4324         186 :         allocator *sa = be->mvc->sa;
    4325         186 :         bool enabled = be->mvc->sa->eb.enabled;
    4326         186 :         be->mvc->sa->eb.enabled = false;
    4327         186 :         stmt *o = NULL, *s = stmt_create(sa, st_func);
    4328         186 :         be->mvc->sa->eb.enabled = enabled;
    4329         186 :         if(!s) {
    4330           0 :                 freeInstruction(q);
    4331           0 :                 goto bailout;
    4332             :         }
    4333         186 :         s->op1 = ops;
    4334         186 :         s->op2 = stmt_atom_string(be, name);
    4335         186 :         s->op4.rel = rel;
    4336         186 :         s->flag = f_union;
    4337         186 :         if (ops && list_length(ops->op4.lval)) {
    4338          10 :                 node *n;
    4339          32 :                 for (n = ops->op4.lval->h, o = n->data; n; n = n->next) {
    4340          22 :                         stmt *c = n->data;
    4341             : 
    4342          22 :                         if (o->nrcols < c->nrcols)
    4343           0 :                                 o = c;
    4344             :                 }
    4345             :         }
    4346             : 
    4347          10 :         if (o) {
    4348          10 :                 s->nrcols = o->nrcols;
    4349          10 :                 s->key = o->key;
    4350          10 :                 s->aggr = o->aggr;
    4351             :         } else {
    4352         176 :                 s->nrcols = 0;
    4353         176 :                 s->key = 1;
    4354             :         }
    4355         186 :         s->nr = getDestVar(q);
    4356         186 :         s->q = q;
    4357         186 :         pushInstruction(mb, q);
    4358         186 :         return s;
    4359             : 
    4360           0 :   bailout:
    4361           0 :         if (be->mvc->sa->eb.enabled)
    4362           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    4363             :         return NULL;
    4364             : }
    4365             : 
    4366             : stmt *
    4367       85038 : stmt_aggr(backend *be, stmt *op1, stmt *grp, stmt *ext, sql_subfunc *op, int reduce, int no_nil, int nil_if_empty)
    4368             : {
    4369       85038 :         MalBlkPtr mb = be->mb;
    4370       85038 :         InstrPtr q = NULL;
    4371       85038 :         const char *mod, *aggrfunc;
    4372       85038 :         sql_subtype *res = op->res->h->data;
    4373       85038 :         int restype = res->type->localtype;
    4374       85038 :         bool complex_aggr = false;
    4375       85038 :         int *stmt_nr = NULL;
    4376       85038 :         int avg = 0;
    4377             : 
    4378       85038 :         if (op1->nr < 0)
    4379           0 :                 goto bailout;
    4380       85038 :         if (backend_create_subfunc(be, op, NULL) < 0)
    4381           0 :                 goto bailout;
    4382       85038 :         mod = sql_func_mod(op->func);
    4383       85038 :         aggrfunc = backend_function_imp(be, op->func);
    4384             : 
    4385       85038 :         if (LANG_INT_OR_MAL(op->func->lang)) {
    4386       84990 :                 if (strcmp(aggrfunc, "avg") == 0)
    4387             :                         avg = 1;
    4388       83620 :                 if (avg || strcmp(aggrfunc, "sum") == 0 || strcmp(aggrfunc, "prod") == 0
    4389       76357 :                         || strcmp(aggrfunc, "str_group_concat") == 0)
    4390             :                         complex_aggr = true;
    4391       84990 :                 if (restype == TYPE_dbl)
    4392        1561 :                         avg = 0;
    4393             :         }
    4394             : 
    4395      170076 :         int argc = 1
    4396       85038 :                 + 2 * avg
    4397       85038 :                 + (LANG_EXT(op->func->lang) != 0)
    4398       85038 :                 + 2 * (op->func->lang == FUNC_LANG_C || op->func->lang == FUNC_LANG_CPP)
    4399       85038 :                 + (op->func->lang == FUNC_LANG_PY || op->func->lang == FUNC_LANG_R)
    4400       85038 :                 + (op1->type != st_list ? 1 : list_length(op1->op4.lval))
    4401       85038 :                 + (grp ? 4 : avg + 1);
    4402             : 
    4403       85038 :         if (grp) {
    4404        6829 :                 char *aggrF = SA_NEW_ARRAY(be->mvc->sa, char, strlen(aggrfunc) + 4);
    4405        6829 :                 if (!aggrF)
    4406           0 :                         goto bailout;
    4407        6829 :                 stpcpy(stpcpy(aggrF, "sub"), aggrfunc);
    4408        6829 :                 aggrfunc = aggrF;
    4409        6829 :                 if ((grp && grp->nr < 0) || (ext && ext->nr < 0))
    4410           0 :                         goto bailout;
    4411             : 
    4412        6829 :                 q = newStmtArgs(mb, mod, aggrfunc, argc);
    4413        6829 :                 if (q == NULL)
    4414           0 :                         goto bailout;
    4415        6829 :                 setVarType(mb, getArg(q, 0), newBatType(restype));
    4416        6829 :                 if (avg) { /* for avg also return rest and count */
    4417          96 :                         q = pushReturn(mb, q, newTmpVariable(mb, newBatType(TYPE_lng)));
    4418          96 :                         q = pushReturn(mb, q, newTmpVariable(mb, newBatType(TYPE_lng)));
    4419             :                 }
    4420             :         } else {
    4421       78209 :                 q = newStmtArgs(mb, mod, aggrfunc, argc);
    4422       78209 :                 if (q == NULL)
    4423           0 :                         goto bailout;
    4424       78209 :                 if (complex_aggr) {
    4425        6714 :                         setVarType(mb, getArg(q, 0), restype);
    4426        6714 :                         if (avg) { /* for avg also return rest and count */
    4427          57 :                                 q = pushReturn(mb, q, newTmpVariable(mb, TYPE_lng));
    4428          57 :                                 q = pushReturn(mb, q, newTmpVariable(mb, TYPE_lng));
    4429             :                         }
    4430             :                 }
    4431             :         }
    4432             : 
    4433       85038 :         if (LANG_EXT(op->func->lang))
    4434          47 :                 q = pushPtr(mb, q, op->func);
    4435       85038 :         if (op->func->lang == FUNC_LANG_R ||
    4436       85003 :                 op->func->lang >= FUNC_LANG_PY ||
    4437             :                 op->func->lang == FUNC_LANG_C ||
    4438             :                 op->func->lang == FUNC_LANG_CPP) {
    4439          47 :                 if (!grp) {
    4440          20 :                         setVarType(mb, getArg(q, 0), restype);
    4441             :                 }
    4442          47 :                 if (op->func->lang == FUNC_LANG_C) {
    4443          12 :                         q = pushBit(mb, q, 0);
    4444          35 :                 } else if (op->func->lang == FUNC_LANG_CPP) {
    4445           0 :                         q = pushBit(mb, q, 1);
    4446             :                 }
    4447          47 :                 q = pushStr(mb, q, op->func->query);
    4448             :         }
    4449             : 
    4450       85038 :         if (op1->type != st_list) {
    4451       79175 :                 q = pushArgument(mb, q, op1->nr);
    4452             :         } else {
    4453        5863 :                 int i;
    4454        5863 :                 node *n;
    4455             : 
    4456       12126 :                 for (i=0, n = op1->op4.lval->h; n; n = n->next, i++) {
    4457        6263 :                         stmt *op = n->data;
    4458             : 
    4459        6263 :                         if (stmt_nr)
    4460             :                                 q = pushArgument(mb, q, stmt_nr[i]);
    4461             :                         else
    4462        6263 :                                 q = pushArgument(mb, q, op->nr);
    4463             :                 }
    4464             :         }
    4465       85038 :         if (grp) {
    4466        6829 :                 q = pushArgument(mb, q, grp->nr);
    4467        6829 :                 q = pushArgument(mb, q, ext->nr);
    4468        6829 :                 if (LANG_INT_OR_MAL(op->func->lang)) {
    4469        6802 :                         if (avg) /* push nil candidates */
    4470          96 :                                 q = pushNilBat(mb, q);
    4471        6802 :                         q = pushBit(mb, q, no_nil);
    4472             :                 }
    4473       78209 :         } else if (LANG_INT_OR_MAL(op->func->lang) && no_nil && strncmp(aggrfunc, "count", 5) == 0) {
    4474        1643 :                 q = pushBit(mb, q, no_nil);
    4475       76566 :         } else if (LANG_INT_OR_MAL(op->func->lang) && !nil_if_empty && strncmp(aggrfunc, "sum", 3) == 0) {
    4476           0 :                 q = pushBit(mb, q, FALSE);
    4477       76566 :         } else if (LANG_INT_OR_MAL(op->func->lang) && avg) { /* push candidates */
    4478          57 :                 q = pushNilBat(mb, q);
    4479          57 :                 q = pushBit(mb, q, no_nil);
    4480             :         }
    4481             : 
    4482       85038 :         bool enabled = be->mvc->sa->eb.enabled;
    4483       85038 :         be->mvc->sa->eb.enabled = false;
    4484       85038 :         stmt *s = stmt_create(be->mvc->sa, st_aggr);
    4485       85037 :         be->mvc->sa->eb.enabled = enabled;
    4486       85037 :         if(!s) {
    4487           0 :                 freeInstruction(q);
    4488           0 :                 goto bailout;
    4489             :         }
    4490       85037 :         s->op1 = op1;
    4491       85037 :         if (grp) {
    4492        6829 :                 s->op2 = grp;
    4493        6829 :                 s->op3 = ext;
    4494        6829 :                 s->nrcols = 1;
    4495             :         } else {
    4496       78208 :                 if (!reduce)
    4497           0 :                         s->nrcols = 1;
    4498             :         }
    4499       85037 :         s->key = reduce;
    4500       85037 :         s->aggr = reduce;
    4501       85037 :         s->flag = no_nil;
    4502       85037 :         s->op4.funcval = op;
    4503       85037 :         s->nr = getDestVar(q);
    4504       85037 :         s->q = q;
    4505       85037 :         pushInstruction(mb, q);
    4506       85037 :         return s;
    4507             : 
    4508           0 :   bailout:
    4509           0 :         if (be->mvc->sa->eb.enabled)
    4510           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    4511             :         return NULL;
    4512             : }
    4513             : 
    4514             : static stmt *
    4515     4930797 : stmt_alias_(backend *be, stmt *op1, int label, const char *tname, const char *alias)
    4516             : {
    4517     4930797 :         assert(label);
    4518     4930797 :         stmt *s = stmt_create(be->mvc->sa, st_alias);
    4519     4930586 :         if(!s) {
    4520             :                 return NULL;
    4521             :         }
    4522     4930586 :         s->label = label;
    4523     4930586 :         s->op1 = op1;
    4524     4930586 :         s->nrcols = op1->nrcols;
    4525     4930586 :         s->key = op1->key;
    4526     4930586 :         s->aggr = op1->aggr;
    4527             : 
    4528     4930586 :         s->tname = tname;
    4529     4930586 :         s->cname = alias;
    4530     4930586 :         s->nr = op1->nr;
    4531     4930586 :         s->q = op1->q;
    4532     4930586 :         return s;
    4533             : }
    4534             : 
    4535             : stmt *
    4536     4368756 : stmt_alias(backend *be, stmt *op1, int label, const char *tname, const char *alias)
    4537             : {
    4538             :         /*
    4539             :         if (((!op1->tname && !tname) ||
    4540             :             (op1->tname && tname && strcmp(op1->tname, tname)==0)) &&
    4541             :             op1->cname && strcmp(op1->cname, alias)==0)
    4542             :                 return op1;
    4543             :                 */
    4544     4368756 :         return stmt_alias_(be, op1, label, tname, alias);
    4545             : }
    4546             : 
    4547             : stmt *
    4548      562049 : stmt_as(backend *be, stmt *s, stmt *org)
    4549             : {
    4550      562049 :         assert(org->type == st_alias);
    4551      562049 :         return stmt_alias_(be, s, org->label, org->tname, org->cname);
    4552             : }
    4553             : 
    4554             : sql_subtype *
    4555     1543874 : tail_type(stmt *st)
    4556             : {
    4557     8573523 :         for (;;) {
    4558     8573523 :                 switch (st->type) {
    4559       36425 :                 case st_const:
    4560       36425 :                         st = st->op2;
    4561       36425 :                         continue;
    4562        3374 :                 case st_uselect:
    4563             :                 case st_semijoin:
    4564             :                 case st_limit:
    4565             :                 case st_limit2:
    4566             :                 case st_sample:
    4567             :                 case st_tunion:
    4568             :                 case st_tdiff:
    4569             :                 case st_tinter:
    4570        3374 :                         return sql_bind_localtype("oid");
    4571         587 :                 case st_uselect2:
    4572         587 :                         if (!st->reduce)
    4573          61 :                                 return sql_bind_localtype("bit");
    4574         526 :                         return sql_bind_localtype("oid");
    4575     3874375 :                 case st_append:
    4576             :                 case st_append_bulk:
    4577             :                 case st_replace:
    4578             :                 case st_alias:
    4579             :                 case st_gen_group:
    4580             :                 case st_order:
    4581     3874375 :                         st = st->op1;
    4582     3874375 :                         continue;
    4583           0 :                 case st_list:
    4584           0 :                         st = st->op4.lval->h->data;
    4585           0 :                         continue;
    4586      902749 :                 case st_bat:
    4587      902749 :                         return &st->op4.cval->type;
    4588        2016 :                 case st_idxbat:
    4589        2016 :                         if (hash_index(st->op4.idxval->type)) {
    4590         269 :                                 return sql_bind_localtype("lng");
    4591        1747 :                         } else if (oid_index(st->op4.idxval->type)) {
    4592        1747 :                                 return sql_bind_localtype("oid");
    4593             :                         }
    4594             :                         /* fall through */
    4595             :                 case st_join:
    4596             :                 case st_join2:
    4597             :                 case st_joinN:
    4598     3118892 :                         if (st->flag == cmp_project) {
    4599     3118849 :                                 st = st->op2;
    4600     3118849 :                                 continue;
    4601             :                         }
    4602             :                         /* fall through */
    4603             :                 case st_reorder:
    4604             :                 case st_group:
    4605             :                 case st_tid:
    4606             :                 case st_mirror:
    4607       15186 :                         return sql_bind_localtype("oid");
    4608       31253 :                 case st_result:
    4609       31253 :                         return &st->op4.typeval;
    4610           0 :                 case st_table_clear:
    4611           0 :                         return sql_bind_localtype("lng");
    4612      154584 :                 case st_aggr:
    4613             :                 case st_Nop: {
    4614      154584 :                         list *res = st->op4.funcval->res;
    4615             : 
    4616      154584 :                         if (res && list_length(res) == 1)
    4617      154584 :                                 return res->h->data;
    4618             : 
    4619             :                         return NULL;
    4620             :                 }
    4621      150816 :                 case st_atom:
    4622      150816 :                         return atom_type(st->op4.aval);
    4623      281004 :                 case st_convert:
    4624             :                 case st_temp:
    4625             :                 case st_single:
    4626             :                 case st_rs_column:
    4627      281004 :                         return &st->op4.typeval;
    4628        2297 :                 case st_var:
    4629        2297 :                         if (st->op4.typeval.type)
    4630        2297 :                                 return &st->op4.typeval;
    4631             :                         /* fall through */
    4632             :                 case st_exception:
    4633             :                         return NULL;
    4634           8 :                 case st_table:
    4635           8 :                         return sql_bind_localtype("bat");
    4636             :                 default:
    4637           0 :                         assert(0);
    4638             :                         return NULL;
    4639             :                 }
    4640             :         }
    4641             : }
    4642             : 
    4643             : int
    4644        6634 : stmt_has_null(stmt *s)
    4645             : {
    4646       20858 :         switch (s->type) {
    4647             :         case st_aggr:
    4648             :         case st_semijoin:
    4649             :         case st_uselect:
    4650             :         case st_uselect2:
    4651             :         case st_atom:
    4652             :                 return 0;
    4653        8183 :         case st_alias:
    4654        8183 :                 return stmt_has_null(s->op1);
    4655        6041 :         case st_join:
    4656        6041 :                 return stmt_has_null(s->op2);
    4657        4212 :         case st_bat:
    4658        4212 :                 return s->op4.cval->null;
    4659             : 
    4660        2422 :         default:
    4661        2422 :                 return 1;
    4662             :         }
    4663             : }
    4664             : 
    4665             : static const char *
    4666           0 : func_name(allocator *sa, const char *n1, const char *n2)
    4667             : {
    4668           0 :         size_t l1 = _strlen(n1), l2;
    4669             : 
    4670           0 :         if (!sa)
    4671             :                 return n1;
    4672           0 :         if (!n2)
    4673           0 :                 return sa_strdup(sa, n1);
    4674           0 :         l2 = _strlen(n2);
    4675             : 
    4676           0 :         if (l2 > 16) {               /* only support short names */
    4677           0 :                 char *ns = SA_NEW_ARRAY(sa, char, l2 + 1);
    4678           0 :                 if(!ns)
    4679             :                         return NULL;
    4680           0 :                 snprintf(ns, l2 + 1, "%s", n2);
    4681           0 :                 return ns;
    4682             :         } else {
    4683           0 :                 char *ns = SA_NEW_ARRAY(sa, char, l1 + l2 + 2), *s = ns;
    4684           0 :                 if(!ns)
    4685             :                         return NULL;
    4686           0 :                 snprintf(ns, l1 + l2 + 2, "%s_%s", n1, n2);
    4687           0 :                 return s;
    4688             :         }
    4689             : }
    4690             : 
    4691             : static const char *_column_name(allocator *sa, stmt *st);
    4692             : 
    4693             : const char *
    4694     2652437 : column_name(allocator *sa, stmt *st)
    4695             : {
    4696     2652437 :         if (!st->cname)
    4697           0 :                 st->cname = _column_name(sa, st);
    4698     2652437 :         return st->cname;
    4699             : }
    4700             : 
    4701             : static const char *
    4702           0 : _column_name(allocator *sa, stmt *st)
    4703             : {
    4704           0 :         switch (st->type) {
    4705           0 :         case st_order:
    4706             :         case st_reorder:
    4707           0 :                 return column_name(sa, st->op1);
    4708           0 :         case st_const:
    4709             :         case st_join:
    4710             :         case st_join2:
    4711             :         case st_joinN:
    4712           0 :                 return column_name(sa, st->op2);
    4713             : 
    4714           0 :         case st_mirror:
    4715             :         case st_group:
    4716             :         case st_result:
    4717             :         case st_append:
    4718             :         case st_append_bulk:
    4719             :         case st_replace:
    4720             :         case st_gen_group:
    4721             :         case st_semijoin:
    4722             :         case st_uselect:
    4723             :         case st_uselect2:
    4724             :         case st_limit:
    4725             :         case st_limit2:
    4726             :         case st_sample:
    4727             :         case st_tunion:
    4728             :         case st_tdiff:
    4729             :         case st_tinter:
    4730             :         case st_convert:
    4731           0 :                 return column_name(sa, st->op1);
    4732           0 :         case st_Nop:
    4733             :         case st_aggr:
    4734             :         {
    4735           0 :                 const char *cn = column_name(sa, st->op1);
    4736           0 :                 return func_name(sa, st->op4.funcval->func->base.name, cn);
    4737             :         }
    4738           0 :         case st_alias:
    4739           0 :                 if (st->op3)
    4740           0 :                         return column_name(sa, st->op3);
    4741             :                 break;
    4742           0 :         case st_bat:
    4743           0 :                 return st->op4.cval->base.name;
    4744           0 :         case st_atom:
    4745           0 :                 if (st->op4.aval->data.vtype == TYPE_str)
    4746           0 :                         return atom2string(sa, st->op4.aval);
    4747             :                 /* fall through */
    4748             :         case st_var:
    4749             :         case st_temp:
    4750             :         case st_single:
    4751           0 :                 if (sa)
    4752           0 :                         return sa_strdup(sa, "single_value");
    4753             :                 return "single_value";
    4754             : 
    4755           0 :         case st_list:
    4756           0 :                 if (list_length(st->op4.lval))
    4757           0 :                         return column_name(sa, st->op4.lval->h->data);
    4758             :                 /* fall through */
    4759             :         case st_rs_column:
    4760             :                 return NULL;
    4761             :         default:
    4762             :                 return NULL;
    4763             :         }
    4764             :         return NULL;
    4765             : }
    4766             : 
    4767             : const char *
    4768     2637091 : table_name(allocator *sa, stmt *st)
    4769             : {
    4770     2637091 :         (void)sa;
    4771     2637091 :         return st->tname;
    4772             : }
    4773             : 
    4774             : const char *
    4775      335507 : schema_name(allocator *sa, stmt *st)
    4776             : {
    4777     3097872 :         switch (st->type) {
    4778     1192002 :         case st_const:
    4779             :         case st_semijoin:
    4780             :         case st_join:
    4781             :         case st_join2:
    4782             :         case st_joinN:
    4783     1192002 :                 return schema_name(sa, st->op2);
    4784      120352 :         case st_mirror:
    4785             :         case st_group:
    4786             :         case st_result:
    4787             :         case st_append:
    4788             :         case st_append_bulk:
    4789             :         case st_replace:
    4790             :         case st_gen_group:
    4791             :         case st_uselect:
    4792             :         case st_uselect2:
    4793             :         case st_limit:
    4794             :         case st_limit2:
    4795             :         case st_sample:
    4796             :         case st_tunion:
    4797             :         case st_tdiff:
    4798             :         case st_tinter:
    4799             :         case st_convert:
    4800             :         case st_Nop:
    4801             :         case st_aggr:
    4802             :                 /* there are no schema aliases, ie look into the base column */
    4803      120352 :                 if (st->op1)
    4804             :                         return schema_name(sa, st->op1);
    4805             :                 return NULL;
    4806     1418776 :         case st_alias:
    4807     1418776 :                 return schema_name(sa, st->op1);
    4808      200674 :         case st_bat:
    4809      200674 :                 return st->op4.cval->t->s->base.name;
    4810             :         case st_atom:
    4811             :                 return NULL;
    4812             :         case st_var:
    4813             :         case st_temp:
    4814             :         case st_single:
    4815             :                 return NULL;
    4816       31355 :         case st_list:
    4817       31355 :                 if (list_length(st->op4.lval))
    4818       31235 :                         return schema_name(sa, st->op4.lval->h->data);
    4819             :                 return NULL;
    4820             :         default:
    4821             :                 return NULL;
    4822             :         }
    4823             : }
    4824             : 
    4825             : stmt *
    4826        1118 : stmt_cond(backend *be, stmt *cond, stmt *outer, int loop /* 0 if, 1 while */, int anti )
    4827             : {
    4828        1118 :         MalBlkPtr mb = be->mb;
    4829        1118 :         InstrPtr q = NULL;
    4830             : 
    4831        1118 :         if (cond->nr < 0)
    4832           0 :                 goto bailout;
    4833        1118 :         if (anti) {
    4834          25 :                 sql_subtype *bt = sql_bind_localtype("bit");
    4835          25 :                 sql_subfunc *not = sql_bind_func(be->mvc, "sys", "not", bt, NULL, F_FUNC, true, true);
    4836          25 :                 sql_subfunc *or = sql_bind_func(be->mvc, "sys", "or", bt, bt, F_FUNC, true, true);
    4837          25 :                 sql_subfunc *isnull = sql_bind_func(be->mvc, "sys", "isnull", bt, NULL, F_FUNC, true, true);
    4838          25 :                 cond = stmt_binop(be,
    4839             :                         stmt_unop(be, cond, NULL, not),
    4840             :                         stmt_unop(be, cond, NULL, isnull), NULL, or);
    4841             :         }
    4842        1118 :         if (!loop) {    /* if */
    4843        1101 :                 q = newAssignment(mb);
    4844        1101 :                 if (q == NULL)
    4845           0 :                         goto bailout;
    4846        1101 :                 q->barrier = BARRIERsymbol;
    4847        1101 :                 q = pushArgument(mb, q, cond->nr);
    4848             :         } else {        /* while */
    4849          17 :                 int c;
    4850             : 
    4851          17 :                 if (outer->nr < 0)
    4852           0 :                         goto bailout;
    4853             :                 /* leave barrier */
    4854          17 :                 q = newStmt(mb, calcRef, notRef);
    4855          17 :                 if (q == NULL)
    4856           0 :                         goto bailout;
    4857          17 :                 q = pushArgument(mb, q, cond->nr);
    4858          17 :                 c = getArg(q, 0);
    4859          17 :                 pushInstruction(mb, q);
    4860             : 
    4861          17 :                 q = newAssignment(mb);
    4862          17 :                 if (q == NULL)
    4863           0 :                         goto bailout;
    4864          17 :                 getArg(q, 0) = outer->nr;
    4865          17 :                 q->barrier = LEAVEsymbol;
    4866          17 :                 q = pushArgument(mb, q, c);
    4867             :         }
    4868             : 
    4869        1118 :         bool enabled = be->mvc->sa->eb.enabled;
    4870        1118 :         be->mvc->sa->eb.enabled = false;
    4871        1118 :         stmt *s = stmt_create(be->mvc->sa, st_cond);
    4872        1118 :         be->mvc->sa->eb.enabled = enabled;
    4873        1118 :         if(!s) {
    4874           0 :                 freeInstruction(q);
    4875           0 :                 goto bailout;
    4876             :         }
    4877        1118 :         s->flag = be->mvc_var; /* keep the mvc_var of the outer context */
    4878        1118 :         s->loop = loop;
    4879        1118 :         s->op1 = cond;
    4880        1118 :         s->nr = getArg(q, 0);
    4881        1118 :         pushInstruction(mb, q);
    4882        1118 :         return s;
    4883             : 
    4884           0 :   bailout:
    4885           0 :         if (be->mvc->sa->eb.enabled)
    4886           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    4887             :         return NULL;
    4888             : }
    4889             : 
    4890             : stmt *
    4891        1118 : stmt_control_end(backend *be, stmt *cond)
    4892             : {
    4893        1118 :         MalBlkPtr mb = be->mb;
    4894        1118 :         InstrPtr q = NULL;
    4895             : 
    4896        1118 :         if (cond->nr < 0)
    4897           0 :                 goto bailout;
    4898             : 
    4899        1118 :         if (cond->loop) {    /* while */
    4900             :                 /* redo barrier */
    4901          17 :                 q = newAssignment(mb);
    4902          17 :                 if (q == NULL)
    4903           0 :                         goto bailout;
    4904          17 :                 getArg(q, 0) = cond->nr;
    4905          17 :                 q->argc = q->retc = 1;
    4906          17 :                 q->barrier = REDOsymbol;
    4907          17 :                 q = pushBit(mb, q, TRUE);
    4908             :         } else {
    4909        1101 :                 q = newAssignment(mb);
    4910        1101 :                 if (q == NULL)
    4911           0 :                         goto bailout;
    4912        1101 :                 getArg(q, 0) = cond->nr;
    4913        1101 :                 q->argc = q->retc = 1;
    4914        1101 :                 q->barrier = EXITsymbol;
    4915             :         }
    4916        1118 :         be->mvc_var = cond->flag; /* restore old mvc_var from before the barrier */
    4917        1118 :         bool enabled = be->mvc->sa->eb.enabled;
    4918        1118 :         be->mvc->sa->eb.enabled = false;
    4919        1118 :         stmt *s = stmt_create(be->mvc->sa, st_control_end);
    4920        1118 :         be->mvc->sa->eb.enabled = enabled;
    4921        1118 :         if(!s) {
    4922           0 :                 freeInstruction(q);
    4923           0 :                 goto bailout;
    4924             :         }
    4925        1118 :         s->op1 = cond;
    4926        1118 :         s->nr = getArg(q, 0);
    4927        1118 :         pushInstruction(mb, q);
    4928        1118 :         return s;
    4929             : 
    4930           0 :   bailout:
    4931           0 :         if (be->mvc->sa->eb.enabled)
    4932           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    4933             :         return NULL;
    4934             : }
    4935             : 
    4936             : 
    4937             : static InstrPtr
    4938         206 : dump_cols(MalBlkPtr mb, list *l, InstrPtr q)
    4939             : {
    4940         206 :         int i;
    4941         206 :         node *n;
    4942             : 
    4943         206 :         if (q == NULL)
    4944             :                 return NULL;
    4945         206 :         q->retc = q->argc = 0;
    4946        1651 :         for (i = 0, n = l->h; n; n = n->next, i++) {
    4947        1445 :                 stmt *c = n->data;
    4948             : 
    4949        1445 :                 q = pushArgument(mb, q, c->nr);
    4950             :         }
    4951         206 :         if (q == NULL)
    4952             :                 return NULL;
    4953         206 :         q->retc = q->argc;
    4954             :         /* Let's make it a proper assignment */
    4955        1651 :         for (i = 0, n = l->h; n; n = n->next, i++) {
    4956        1445 :                 stmt *c = n->data;
    4957             : 
    4958        1445 :                 q = pushArgument(mb, q, c->nr);
    4959             :         }
    4960             :         return q;
    4961             : }
    4962             : 
    4963             : stmt *
    4964         658 : stmt_return(backend *be, stmt *val, int nr_declared_tables)
    4965             : {
    4966         658 :         MalBlkPtr mb = be->mb;
    4967         658 :         InstrPtr q = NULL;
    4968             : 
    4969         658 :         if (val->nr < 0)
    4970           0 :                 goto bailout;
    4971         658 :         int args = val->type == st_table ? 2 * list_length(val->op1->op4.lval) : 0;
    4972         206 :         if (args < MAXARG)
    4973             :                 args = MAXARG;
    4974         658 :         q = newInstructionArgs(mb, NULL, NULL, args);
    4975         658 :         if (q == NULL)
    4976           0 :                 goto bailout;
    4977         658 :         q->barrier= RETURNsymbol;
    4978         658 :         if (val->type == st_table) {
    4979         206 :                 list *l = val->op1->op4.lval;
    4980             : 
    4981         206 :                 q = dump_cols(mb, l, q);
    4982             :         } else {
    4983         452 :                 getArg(q, 0) = getArg(getInstrPtr(mb, 0), 0);
    4984         452 :                 q = pushArgument(mb, q, val->nr);
    4985             :         }
    4986             : 
    4987         658 :         bool enabled = be->mvc->sa->eb.enabled;
    4988         658 :         be->mvc->sa->eb.enabled = false;
    4989         658 :         stmt *s = stmt_create(be->mvc->sa, st_return);
    4990         658 :         be->mvc->sa->eb.enabled = enabled;
    4991         658 :         if(!s) {
    4992           0 :                 freeInstruction(q);
    4993           0 :                 goto bailout;
    4994             :         }
    4995         658 :         s->op1 = val;
    4996         658 :         s->flag = nr_declared_tables;
    4997         658 :         s->nr = getDestVar(q);
    4998         658 :         s->q = q;
    4999         658 :         pushInstruction(mb, q);
    5000         658 :         return s;
    5001             : 
    5002           0 :   bailout:
    5003           0 :         if (be->mvc->sa->eb.enabled)
    5004           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    5005             :         return NULL;
    5006             : }
    5007             : 
    5008             : stmt *
    5009        1416 : stmt_assign(backend *be, const char *sname, const char *varname, stmt *val, int level)
    5010             : {
    5011        1416 :         MalBlkPtr mb = be->mb;
    5012        1416 :         InstrPtr q = NULL;
    5013             : 
    5014        1416 :         if (val && val->nr < 0)
    5015           0 :                 goto bailout;
    5016        1416 :         if (level != 0) {
    5017        1037 :                 char *buf,  levelstr[16];
    5018             : 
    5019        1037 :                 if (!val) {
    5020             :                         /* drop declared table */
    5021           0 :                         assert(0);
    5022             :                 }
    5023             : 
    5024        1037 :                 assert(!sname);
    5025        1037 :                 snprintf(levelstr, sizeof(levelstr), "%d", level);
    5026        1037 :                 buf = SA_NEW_ARRAY(be->mvc->sa, char, strlen(levelstr) + strlen(varname) + 3);
    5027        1037 :                 if (!buf)
    5028           0 :                         goto bailout;
    5029        1037 :                 stpcpy(stpcpy(stpcpy(stpcpy(buf, "A"), levelstr), "%"), varname); /* mangle variable name */
    5030        1037 :                 q = newInstruction(mb, NULL, NULL);
    5031        1037 :                 if (q == NULL) {
    5032           0 :                         goto bailout;
    5033             :                 }
    5034        1037 :                 q->argc = q->retc = 0;
    5035        1037 :                 q = pushArgumentId(mb, q, buf);
    5036        1037 :                 pushInstruction(mb, q);
    5037        1037 :                 q->retc++;
    5038             :         } else {
    5039         379 :                 assert(sname); /* all global variables have a schema */
    5040         379 :                 q = newStmt(mb, sqlRef, setVariableRef);
    5041         379 :                 if (q == NULL)
    5042           0 :                         goto bailout;
    5043         379 :                 q = pushArgument(mb, q, be->mvc_var);
    5044         379 :                 q = pushStr(mb, q, sname);
    5045         379 :                 q = pushStr(mb, q, varname);
    5046         379 :                 getArg(q, 0) = be->mvc_var = newTmpVariable(mb, TYPE_int);
    5047         379 :                 pushInstruction(mb, q);
    5048         379 :                 be->mvc_var = getDestVar(q);
    5049             :         }
    5050        1416 :         q = pushArgument(mb, q, val->nr);
    5051             : 
    5052        1416 :         bool enabled = be->mvc->sa->eb.enabled;
    5053        1416 :         be->mvc->sa->eb.enabled = false;
    5054        1416 :         stmt *s = stmt_create(be->mvc->sa, st_assign);
    5055        1416 :         be->mvc->sa->eb.enabled = enabled;
    5056        1416 :         if(!s) {
    5057           0 :                 goto bailout;
    5058             :         }
    5059        1416 :         s->op2 = val;
    5060        1416 :         s->flag = (level << 1);
    5061        1416 :         s->q = q;
    5062        1416 :         s->nr = 1;
    5063        1416 :         return s;
    5064             : 
    5065           0 :   bailout:
    5066           0 :         if (be->mvc->sa->eb.enabled)
    5067           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    5068             :         return NULL;
    5069             : }
    5070             : 
    5071             : stmt *
    5072       21373 : const_column(backend *be, stmt *val)
    5073             : {
    5074       21373 :         sql_subtype *ct = tail_type(val);
    5075       21373 :         MalBlkPtr mb = be->mb;
    5076       21373 :         InstrPtr q = NULL;
    5077       21373 :         int tt = ct->type->localtype;
    5078             : 
    5079       21373 :         if (val->nr < 0)
    5080           0 :                 goto bailout;
    5081       21373 :         q = newStmt(mb, batRef, singleRef);
    5082       21373 :         if (q == NULL)
    5083           0 :                 goto bailout;
    5084       21373 :         setVarType(mb, getArg(q, 0), newBatType(tt));
    5085       21373 :         q = pushArgument(mb, q, val->nr);
    5086             : 
    5087       21373 :         bool enabled = be->mvc->sa->eb.enabled;
    5088       21373 :         be->mvc->sa->eb.enabled = false;
    5089       21373 :         stmt *s = stmt_create(be->mvc->sa, st_single);
    5090       21373 :         be->mvc->sa->eb.enabled = enabled;
    5091       21373 :         if(!s) {
    5092           0 :                 freeInstruction(q);
    5093           0 :                 goto bailout;
    5094             :         }
    5095       21373 :         s->op1 = val;
    5096       21373 :         s->op4.typeval = *ct;
    5097       21373 :         s->nrcols = 1;
    5098             : 
    5099       21373 :         s->tname = val->tname;
    5100       21373 :         s->cname = val->cname;
    5101       21373 :         s->nr = getDestVar(q);
    5102       21373 :         s->q = q;
    5103       21373 :         pushInstruction(mb, q);
    5104       21373 :         return s;
    5105             : 
    5106           0 :   bailout:
    5107           0 :         if (be->mvc->sa->eb.enabled)
    5108           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    5109             :         return NULL;
    5110             : }
    5111             : 
    5112             : stmt *
    5113          10 : stmt_fetch(backend *be, stmt *val)
    5114             : {
    5115          10 :         sql_subtype *ct;
    5116          10 :         MalBlkPtr mb = be->mb;
    5117          10 :         InstrPtr q = NULL;
    5118          10 :         int tt;
    5119             : 
    5120          10 :         if (val->nr < 0)
    5121           0 :                 goto bailout;
    5122             :         /* pick from first column on a table case */
    5123          10 :         if (val->type == st_table) {
    5124           0 :                 if (list_length(val->op1->op4.lval) > 1)
    5125           0 :                         goto bailout;
    5126           0 :                 val = val->op1->op4.lval->h->data;
    5127             :         }
    5128          10 :         ct = tail_type(val);
    5129          10 :         tt = ct->type->localtype;
    5130             : 
    5131          10 :         q = newStmt(mb, algebraRef, fetchRef);
    5132          10 :         if (q == NULL)
    5133           0 :                 goto bailout;
    5134          10 :         setVarType(mb, getArg(q, 0), tt);
    5135          10 :         q = pushArgument(mb, q, val->nr);
    5136          10 :         q = pushOid(mb, q, 0);
    5137             : 
    5138          10 :         bool enabled = be->mvc->sa->eb.enabled;
    5139          10 :         be->mvc->sa->eb.enabled = false;
    5140          10 :         stmt *s = stmt_create(be->mvc->sa, st_single);
    5141          10 :         be->mvc->sa->eb.enabled = enabled;
    5142          10 :         if(!s) {
    5143           0 :                 freeInstruction(q);
    5144           0 :                 goto bailout;
    5145             :         }
    5146          10 :         s->op1 = val;
    5147          10 :         s->op4.typeval = *ct;
    5148          10 :         s->nrcols = 0;
    5149             : 
    5150          10 :         s->tname = val->tname;
    5151          10 :         s->cname = val->cname;
    5152          10 :         s->nr = getDestVar(q);
    5153          10 :         s->q = q;
    5154          10 :         pushInstruction(mb, q);
    5155          10 :         return s;
    5156             : 
    5157           0 :   bailout:
    5158           0 :         if (be->mvc->sa->eb.enabled)
    5159           0 :                 eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    5160             :         return NULL;
    5161             : }
    5162             : 
    5163             : stmt *
    5164     1519972 : stmt_rename(backend *be, sql_exp *exp, stmt *s )
    5165             : {
    5166     1519972 :         int label = exp_get_label(exp);
    5167     1519968 :         const char *name = exp_name(exp);
    5168     1519961 :         const char *rname = exp_relname(exp);
    5169     1519945 :         stmt *o = s;
    5170             : 
    5171     1519945 :         if (!name && exp_is_atom(exp))
    5172           0 :                 name = sa_strdup(be->mvc->sa, "single_value");
    5173           0 :         assert(name);
    5174     1519945 :         s = stmt_alias(be, s, label, rname, name);
    5175     1519907 :         if (o->flag & OUTER_ZERO)
    5176         521 :                 s->flag |= OUTER_ZERO;
    5177     1519907 :         return s;
    5178             : }

Generated by: LCOV version 1.14