LCOV - code coverage report
Current view: top level - sql/backends/monet5 - rel_bin.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 4315 4657 92.7 %
Date: 2024-11-12 21:42:17 Functions: 126 129 97.7 %

          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             : 
      15             : #include "rel_bin.h"
      16             : #include "rel_rel.h"
      17             : #include "rel_basetable.h"
      18             : #include "rel_exp.h"
      19             : #include "rel_dump.h"
      20             : #include "rel_psm.h"
      21             : #include "rel_prop.h"
      22             : #include "rel_select.h"
      23             : #include "rel_updates.h"
      24             : #include "rel_predicates.h"
      25             : #include "rel_file_loader.h"
      26             : #include "sql_env.h"
      27             : #include "sql_optimizer.h"
      28             : #include "sql_gencode.h"
      29             : #include "mal_builder.h"
      30             : #include "opt_prelude.h"
      31             : 
      32             : static stmt * rel_bin(backend *be, sql_rel *rel);
      33             : static stmt * subrel_bin(backend *be, sql_rel *rel, list *refs);
      34             : 
      35             : static stmt *check_types(backend *be, sql_subtype *fromtype, stmt *s, check_type tpe);
      36             : 
      37             : static void
      38       46685 : clean_mal_statements(backend *be, int oldstop, int oldvtop)
      39             : {
      40       46685 :         MSresetInstructions(be->mb, oldstop);
      41       46685 :         freeVariables(be->client, be->mb, NULL, oldvtop);
      42       46685 :         be->mvc->session->status = 0; /* clean possible generated error */
      43       46685 :         be->mvc->errstr[0] = '\0';
      44       46685 : }
      45             : 
      46             : static int
      47      149888 : add_to_rowcount_accumulator(backend *be, int nr)
      48             : {
      49      149888 :         if (be->silent)
      50             :                 return 0;
      51             : 
      52      149761 :         if (be->rowcount == 0) {
      53      149488 :                 be->rowcount = nr;
      54      149488 :                 return 0;
      55             :         }
      56             : 
      57         273 :         InstrPtr q = newStmt(be->mb, calcRef, plusRef);
      58         273 :         if (q == NULL) {
      59           0 :                 if (be->mvc->sa->eb.enabled)
      60           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);
      61             :                 return -1;
      62             :         }
      63         273 :         q = pushArgument(be->mb, q, be->rowcount);
      64         273 :         q = pushArgument(be->mb, q, nr);
      65         273 :         pushInstruction(be->mb, q);
      66             : 
      67         273 :         be->rowcount = getDestVar(q);
      68         273 :         return 0;
      69             : }
      70             : 
      71             : static stmt *
      72       12765 : stmt_selectnil(backend *be, stmt *col)
      73             : {
      74       12765 :         sql_subtype *t = tail_type(col);
      75       12765 :         return stmt_uselect(be, col, stmt_atom(be, atom_general(be->mvc->sa, t, NULL, 0)), cmp_equal, NULL, 0, 1);
      76             : }
      77             : 
      78             : static stmt *
      79        5798 : sql_unop_(backend *be, const char *fname, stmt *rs)
      80             : {
      81        5798 :         mvc *sql = be->mvc;
      82        5798 :         sql_subtype *rt = NULL;
      83        5798 :         sql_subfunc *f = NULL;
      84             : 
      85        5798 :         rt = tail_type(rs);
      86        5798 :         f = sql_bind_func(sql, "sys", fname, rt, NULL, F_FUNC, true, true);
      87             :         /* try to find the function without a type, and convert
      88             :          * the value to the type needed by this function!
      89             :          */
      90        5798 :         if (!f && (f = sql_find_func(sql, "sys", fname, 1, F_FUNC, true, NULL)) != NULL) {
      91           0 :                 sql_arg *a = f->func->ops->h->data;
      92             : 
      93           0 :                 sql->session->status = 0;
      94           0 :                 sql->errstr[0] = '\0';
      95           0 :                 rs = check_types(be, &a->type, rs, type_equal);
      96           0 :                 if (!rs)
      97             :                         f = NULL;
      98             :         }
      99             :         if (f) {
     100             :                 /*
     101             :                 if (f->func->res.scale == INOUT) {
     102             :                         f->res.digits = rt->digits;
     103             :                         f->res.scale = rt->scale;
     104             :                 }
     105             :                 */
     106        5798 :                 return stmt_unop(be, rs, NULL, f);
     107           0 :         } else if (rs) {
     108           0 :                 char *type = tail_type(rs)->type->base.name;
     109             : 
     110           0 :                 return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "SELECT: no such unary operator '%s(%s)'", fname, type);
     111             :         }
     112             :         return NULL;
     113             : }
     114             : 
     115             : static stmt *
     116       16387 : refs_find_rel(list *refs, sql_rel *rel)
     117             : {
     118       16387 :         node *n;
     119             : 
     120       20787 :         for (n=refs->h; n; n = n->next->next) {
     121       11627 :                 sql_rel *ref = n->data;
     122       11627 :                 stmt *s = n->next->data;
     123             : 
     124       11627 :                 if (rel == ref)
     125             :                         return s;
     126             :         }
     127             :         return NULL;
     128             : }
     129             : 
     130             : static void
     131        5133 : refs_update_stmt(list *refs, sql_rel *rel, stmt *s)
     132             : {
     133        5133 :         node *n;
     134             : 
     135        6351 :         for (n=refs->h; n; n = n->next->next) {
     136        6351 :                 sql_rel *ref = n->data;
     137             : 
     138        6351 :                 if (rel == ref) {
     139        5133 :                         n->next->data = s;
     140        5133 :                         break;
     141             :                 }
     142             :         }
     143             : }
     144             : 
     145             : 
     146             : static void
     147           0 : print_stmtlist(allocator *sa, stmt *l)
     148             : {
     149           0 :         node *n;
     150           0 :         if (l) {
     151           0 :                 for (n = l->op4.lval->h; n; n = n->next) {
     152           0 :                         const char *rnme = table_name(sa, n->data);
     153           0 :                         const char *nme = column_name(sa, n->data);
     154             : 
     155           0 :                         TRC_INFO(SQL_EXECUTION, "%s.%s\n", rnme ? rnme : "(null!)", nme ? nme : "(null!)");
     156             :                 }
     157             :         }
     158           0 : }
     159             : 
     160             : static stmt *
     161        4230 : list_find_column(backend *be, list *l, const char *rname, const char *name)
     162             : {
     163        4230 :         stmt *res = NULL;
     164        4230 :         node *n;
     165             : 
     166        4230 :         if (!l)
     167             :                 return NULL;
     168        4230 :         if (!l->ht && list_length(l) > HASH_MIN_SIZE) {
     169         799 :                 l->ht = hash_new(l->sa, MAX(list_length(l), l->expected_cnt), (fkeyvalue)&stmt_key);
     170         799 :                 if (l->ht != NULL) {
     171       16143 :                         for (n = l->h; n; n = n->next) {
     172       15344 :                                 const char *nme = column_name(be->mvc->sa, n->data);
     173       15344 :                                 if (nme) {
     174       15344 :                                         int key = hash_key(nme);
     175             : 
     176       15344 :                                         if (hash_add(l->ht, key, n->data) == NULL) {
     177           0 :                                                 hash_destroy(l->ht);
     178           0 :                                                 l->ht = NULL;
     179           0 :                                                 break;
     180             :                                         }
     181             :                                 }
     182             :                         }
     183             :                 }
     184             :         }
     185        4230 :         if (l->ht) {
     186         876 :                 int key = hash_key(name);
     187         876 :                 sql_hash_e *e = l->ht->buckets[key&(l->ht->size-1)];
     188             : 
     189         876 :                 if (rname) {
     190         907 :                         for (; e; e = e->chain) {
     191         849 :                                 stmt *s = e->value;
     192         849 :                                 const char *rnme = table_name(be->mvc->sa, s);
     193         849 :                                 const char *nme = column_name(be->mvc->sa, s);
     194             : 
     195         849 :                                 if (rnme && strcmp(rnme, rname) == 0 &&
     196         840 :                                             strcmp(nme, name) == 0) {
     197             :                                         res = s;
     198             :                                         break;
     199             :                                 }
     200             :                         }
     201             :                 } else {
     202           0 :                         for (; e; e = e->chain) {
     203           0 :                                 stmt *s = e->value;
     204           0 :                                 const char *rnme = table_name(be->mvc->sa, s);
     205           0 :                                 const char *nme = column_name(be->mvc->sa, s);
     206             : 
     207           0 :                                 if (!rnme && nme && strcmp(nme, name) == 0) {
     208             :                                         res = s;
     209             :                                         break;
     210             :                                 }
     211             :                         }
     212             :                 }
     213         876 :                 if (!res)
     214             :                         return NULL;
     215             :                 return res;
     216             :         }
     217        3354 :         if (rname) {
     218        7474 :                 for (n = l->h; n; n = n->next) {
     219        7436 :                         const char *rnme = table_name(be->mvc->sa, n->data);
     220        7436 :                         const char *nme = column_name(be->mvc->sa, n->data);
     221             : 
     222        7436 :                         if (rnme && strcmp(rnme, rname) == 0 &&
     223        7359 :                                     strcmp(nme, name) == 0) {
     224        3316 :                                 res = n->data;
     225        3316 :                                 break;
     226             :                         }
     227             :                 }
     228             :         } else {
     229           0 :                 for (n = l->h; n; n = n->next) {
     230           0 :                         const char *rnme = table_name(be->mvc->sa, n->data);
     231           0 :                         const char *nme = column_name(be->mvc->sa, n->data);
     232             : 
     233           0 :                         if (!rnme && nme && strcmp(nme, name) == 0) {
     234           0 :                                 res = n->data;
     235           0 :                                 break;
     236             :                         }
     237             :                 }
     238             :         }
     239        3354 :         if (!res)
     240             :                 return NULL;
     241             :         return res;
     242             : }
     243             : 
     244             : static stmt *
     245        4230 : bin_find_column(backend *be, stmt *sub, const char *rname, const char *name)
     246             : {
     247        4230 :         return list_find_column(be, sub->op4.lval, rname, name);
     248             : }
     249             : 
     250             : static stmt *
     251     1172924 : list_find_column_nid(backend *be, list *l, int label)
     252             : {
     253     1172924 :         (void)be;
     254     1172924 :         if (!l)
     255             :                 return NULL;
     256    12175931 :         for (node *n = l->h; n; n = n->next) {
     257    12124623 :                 stmt *s = n->data;
     258             : 
     259    12124623 :                 if (s->label == label)
     260             :                         return s;
     261             :         }
     262             :         return NULL;
     263             : }
     264             : 
     265             : static stmt *
     266     1143486 : bin_find_column_nid(backend *be, stmt *sub, int label)
     267             : {
     268     1143486 :         list *l = sub->op4.lval;
     269     3416203 :         return list_find_column_nid(be, l, label);
     270             : }
     271             : 
     272             : static list *
     273             : bin_find_columns(backend *be, stmt *sub, const char *name)
     274             : {
     275             :         node *n;
     276             :         list *l = sa_list(be->mvc->sa);
     277             : 
     278             :         for (n = sub->op4.lval->h; n; n = n->next) {
     279             :                 const char *nme = column_name(be->mvc->sa, n->data);
     280             : 
     281             :                 if (strcmp(nme, name) == 0)
     282             :                         append(l, n->data);
     283             :         }
     284             :         if (list_length(l))
     285             :                 return l;
     286             :         return NULL;
     287             : }
     288             : 
     289             : static stmt *
     290     2323696 : column(backend *be, stmt *val)
     291             : {
     292     2323696 :         if (val->nrcols == 0)
     293       20887 :                 return const_column(be, val);
     294             :         return val;
     295             : }
     296             : 
     297             : static stmt *
     298       85503 : create_const_column(backend *be, stmt *val)
     299             : {
     300       85503 :         if (val->nrcols == 0)
     301          16 :                 val = const_column(be, val);
     302       85503 :         return stmt_append(be, stmt_temp(be, tail_type(val)), val);
     303             : }
     304             : 
     305             : static int
     306      779304 : statment_score(stmt *c)
     307             : {
     308      779304 :         sql_subtype *t = tail_type(c);
     309      779304 :         int score = 0;
     310             : 
     311      779304 :         if (c->nrcols != 0) /* no need to create an extra intermediate */
     312      778992 :                 score += 200;
     313             : 
     314      779304 :         if (!t)
     315             :                 return score;
     316      779304 :         switch (ATOMstorage(t->type->localtype)) { /* give preference to smaller types */
     317       51008 :                 case TYPE_bte:
     318       51008 :                         score += 150 - 8;
     319       51008 :                         break;
     320       57842 :                 case TYPE_sht:
     321       57842 :                         score += 150 - 16;
     322       57842 :                         break;
     323      251574 :                 case TYPE_int:
     324      251574 :                         score += 150 - 32;
     325      251574 :                         break;
     326       44975 :                 case TYPE_void:
     327             :                 case TYPE_lng:
     328       44975 :                         score += 150 - 64;
     329       44975 :                         break;
     330        1423 :                 case TYPE_uuid:
     331             : #ifdef HAVE_HGE
     332             :                 case TYPE_hge:
     333             : #endif
     334        1423 :                         score += 150 - 128;
     335        1423 :                         break;
     336        3332 :                 case TYPE_flt:
     337        3332 :                         score += 75 - 24;
     338        3332 :                         break;
     339        5952 :                 case TYPE_dbl:
     340        5952 :                         score += 75 - 53;
     341        5952 :                         break;
     342             :                 default:
     343             :                         break;
     344             :         }
     345             :         return score;
     346             : }
     347             : 
     348             : static stmt *
     349       91930 : bin_find_smallest_column(backend *be, stmt *sub)
     350             : {
     351       91930 :         stmt *res = sub->op4.lval->h->data;
     352       91930 :         int best_score = statment_score(sub->op4.lval->h->data);
     353             : 
     354       91930 :         if (sub->op4.lval->h->next)
     355      763153 :                 for (node *n = sub->op4.lval->h->next ; n ; n = n->next) {
     356      687374 :                         stmt *c = n->data;
     357      687374 :                         int next_score = statment_score(c);
     358             : 
     359      687374 :                         if (next_score > best_score) {
     360       53474 :                                 res = c;
     361       53474 :                                 best_score = next_score;
     362             :                         }
     363             :                 }
     364       91930 :         if (res->nrcols == 0)
     365         260 :                 return const_column(be, res);
     366             :         return res;
     367             : }
     368             : 
     369             : static stmt *
     370      234716 : row2cols(backend *be, stmt *sub)
     371             : {
     372      234716 :         if (sub->nrcols == 0 && sub->key) {
     373        5909 :                 node *n;
     374        5909 :                 list *l = sa_list(be->mvc->sa);
     375        5909 :                 if (l == NULL)
     376             :                         return NULL;
     377             : 
     378       23837 :                 for (n = sub->op4.lval->h; n; n = n->next) {
     379       17928 :                         stmt *sc = n->data;
     380       17928 :                         assert(sc->type == st_alias);
     381       17928 :                         const char *cname = column_name(be->mvc->sa, sc);
     382       17928 :                         const char *tname = table_name(be->mvc->sa, sc);
     383       17928 :                         int label = sc->label;
     384             : 
     385       17928 :                         sc = column(be, sc);
     386       17928 :                         list_append(l, stmt_alias(be, sc, label, tname, cname));
     387             :                 }
     388        5909 :                 sub = stmt_list(be, l);
     389             :         }
     390             :         return sub;
     391             : }
     392             : 
     393             : static stmt*
     394          44 : distinct_value_list(backend *be, list *vals, stmt **last_null_value, int depth, int push)
     395             : {
     396          44 :         list *l = sa_list(be->mvc->sa);
     397          44 :         stmt *s;
     398             : 
     399             :         /* create bat append values */
     400        1843 :         for (node *n = vals->h; n; n = n->next) {
     401        1799 :                 sql_exp *e = n->data;
     402        1799 :                 stmt *i = exp_bin(be, e, NULL, NULL, NULL, NULL, NULL, NULL, depth, 0, push);
     403             : 
     404        1799 :                 if (exp_is_null(e))
     405           0 :                         *last_null_value = i;
     406             : 
     407        1799 :                 if (!i)
     408             :                         return NULL;
     409             : 
     410        1799 :                 list_append(l, i);
     411             :         }
     412          44 :         s = stmt_append_bulk(be, stmt_temp(be, exp_subtype(vals->h->data)), l);
     413             :         /* Probably faster to filter out the values directly in the underlying list of atoms.
     414             :            But for now use groupby to filter out duplicate values. */
     415          44 :         stmt* groupby = stmt_group(be, s, NULL, NULL, NULL, 1);
     416          44 :         stmt* ext = stmt_result(be, groupby, 1);
     417             : 
     418          44 :         return stmt_project(be, ext, s);
     419             : }
     420             : 
     421             : static stmt *
     422        3028 : stmt_selectnonil(backend *be, stmt *col, stmt *s)
     423             : {
     424        3028 :         sql_subtype *t = tail_type(col);
     425        3028 :         return stmt_uselect(be, col, stmt_atom(be, atom_general(be->mvc->sa, t, NULL, 0)), cmp_equal, s, 1, 1);
     426             : }
     427             : 
     428             : static int
     429       14676 : is_tid_chain(stmt *cand)
     430             : {
     431       18130 :         while(cand && cand->type != st_tid && cand->cand) {
     432             :                 cand = cand->cand;
     433             :         }
     434       14676 :         if (cand && cand->type == st_tid)
     435       12602 :                 return 1;
     436             :         return 0;
     437             : }
     438             : 
     439             : static stmt *
     440     1127310 : subrel_project(backend *be, stmt *s, list *refs, sql_rel *rel)
     441             : {
     442     1127310 :         if (!s || s->type != st_list || !s->cand)
     443             :                 return s;
     444             : 
     445      157660 :         list *l = sa_list(be->mvc->sa);
     446      157660 :         stmt *cand = s->cand;
     447      157660 :         if (!l)
     448             :                 return NULL;
     449      720804 :         for (node *n = s->op4.lval->h; n; n = n->next) {
     450      563144 :                 stmt *c = n->data;
     451             : 
     452      563144 :                 assert(c->type == st_alias || (c->type == st_join && c->flag == cmp_project) || c->type == st_bat || c->type == st_idxbat || c->type == st_single);
     453      563144 :                 if (c->type != st_alias) {
     454           0 :                         c = stmt_project(be, cand, c);
     455      563144 :                 } else if (c->op1->type == st_mirror && is_tid_chain(cand)) { /* alias with mirror (ie full row ids) */
     456             :                         //c = stmt_alias(be, cand, 0, c->tname, c->cname);
     457       12602 :                         c = stmt_as(be, cand, c);
     458             :                 } else { /* st_alias */
     459      550542 :                         stmt *s = c->op1;
     460      550542 :                         if (s->nrcols == 0)
     461           0 :                                 s = stmt_const(be, cand, s);
     462             :                         else
     463      550542 :                                 s = stmt_project(be, cand, s);
     464             :                         //c = stmt_alias(be, s, c->flag, c->tname, c->cname);
     465      550541 :                         c = stmt_as(be, s, c);
     466             :                 }
     467      563145 :                 append(l, c);
     468             :         }
     469      157660 :         s = stmt_list(be, l);
     470      157660 :         if (rel && rel_is_ref(rel))
     471        5079 :                 refs_update_stmt(refs, rel, s);
     472             :         return s;
     473             : }
     474             : 
     475             : static stmt *
     476          72 : handle_in_tuple_exps(backend *be, sql_exp *ce, list *nl, stmt *left, stmt *right, stmt *grp, stmt *ext, stmt *cnt, stmt *sel, bool in, int depth, int reduce, int push)
     477             : {
     478          72 :         mvc *sql = be->mvc;
     479          72 :         stmt *s = NULL;
     480             : 
     481          72 :         list *lvals = ce->f, *lstmts = sa_list(sql->sa);
     482         327 :         for(node *n = lvals->h; n; n = n->next) {
     483         255 :                 sql_exp *ce = n->data;
     484         255 :                 stmt *c = exp_bin(be, ce, left, right, grp, ext, cnt, NULL, depth+1, 0, push);
     485             : 
     486         255 :                 if (c && reduce && c->nrcols == 0)
     487           0 :                         c = stmt_const(be, bin_find_smallest_column(be, left), c);
     488           0 :                 if(!c)
     489             :                         return NULL;
     490         255 :                 lstmts = append(lstmts, c);
     491             :         }
     492             : 
     493          72 :         sql_subtype *bt = sql_bind_localtype("bit");
     494          72 :         sql_subfunc *and = sql_bind_func(sql, "sys", "and", bt, bt, F_FUNC, true, true);
     495          72 :         sql_subfunc *or = sql_bind_func(sql, "sys", "or", bt, bt, F_FUNC, true, true);
     496         572 :         for (node *n = nl->h; n; n = n->next) {
     497         500 :                 sql_exp *e = n->data;
     498         500 :                 list *vals = e->f;
     499         500 :                 stmt *cursel = NULL;
     500             : 
     501        2061 :                 for (node *m = vals->h, *o = lstmts->h; m && o; m = m->next, o = o->next) {
     502        1561 :                         stmt *c = o->data;
     503        3122 :                         sql_subfunc *cmp = (in)
     504        1561 :                                 ?sql_bind_func(sql, "sys", "=", tail_type(c), tail_type(c), F_FUNC, true, true)
     505        1561 :                                 :sql_bind_func(sql, "sys", "<>", tail_type(c), tail_type(c), F_FUNC, true, true);
     506        1561 :                         sql_exp *e = m->data;
     507             : 
     508        1561 :                         stmt *i = exp_bin(be, e, left, right, grp, ext, cnt, NULL, depth+1, 0, push);
     509        1561 :                         if(!i)
     510             :                                 return NULL;
     511             : 
     512        1561 :                         i = stmt_binop(be, c, i, NULL, cmp);
     513        1561 :                         if (cursel)
     514        1061 :                                 cursel = stmt_binop(be, cursel, i, NULL, in?and:or);
     515             :                         else
     516             :                                 cursel = i;
     517             :                 }
     518         500 :                 if (s)
     519         428 :                         s = stmt_binop(be, s, cursel, NULL, in?or:and);
     520             :                 else
     521             :                         s = cursel;
     522             :         }
     523          72 :         if (!depth && reduce)
     524         144 :                 s = stmt_uselect(be,
     525          72 :                         s->nrcols == 0?stmt_const(be, bin_find_smallest_column(be, left), s): s,
     526             :                         stmt_bool(be, 1), cmp_equal, sel, 0, 0);
     527             :         return s;
     528             : }
     529             : 
     530             : static stmt *
     531       17238 : handle_in_exps(backend *be, sql_exp *ce, list *nl, stmt *left, stmt *right, stmt *grp, stmt *ext, stmt *cnt, stmt *sel, bool in, int depth, int reduce, int push)
     532             : {
     533       17238 :         if (ce && is_values(ce))
     534          72 :                 return handle_in_tuple_exps(be, ce, nl, left, right, grp, ext, cnt, sel, in, depth, reduce, push);
     535       17166 :         mvc *sql = be->mvc;
     536       17166 :         node *n;
     537       17166 :         stmt *s = NULL, *c = exp_bin(be, ce, left, right, grp, ext, cnt, NULL, depth+1, 0, push);
     538             : 
     539       17166 :         if(!c)
     540             :                 return NULL;
     541             : 
     542       17166 :         if (reduce && c->nrcols == 0)
     543          44 :                 c = stmt_const(be, bin_find_smallest_column(be, left), c);
     544             : 
     545       17166 :         if (c->nrcols == 0 || depth || !reduce) {
     546           2 :                 sql_subtype *bt = sql_bind_localtype("bit");
     547           4 :                 sql_subfunc *cmp = (in)
     548           2 :                         ?sql_bind_func(sql, "sys", "=", tail_type(c), tail_type(c), F_FUNC, true, true)
     549           2 :                         :sql_bind_func(sql, "sys", "<>", tail_type(c), tail_type(c), F_FUNC, true, true);
     550           2 :                 sql_subfunc *a = (in)?sql_bind_func(sql, "sys", "or", bt, bt, F_FUNC, true, true)
     551           2 :                                      :sql_bind_func(sql, "sys", "and", bt, bt, F_FUNC, true, true);
     552             : 
     553           6 :                 for (n = nl->h; n; n = n->next) {
     554           4 :                         sql_exp *e = n->data;
     555           4 :                         stmt *i = exp_bin(be, e, left, right, grp, ext, cnt, NULL, depth+1, 0, push);
     556           4 :                         if(!i)
     557             :                                 return NULL;
     558             : 
     559           4 :                         i = stmt_binop(be, c, i, NULL, cmp);
     560           4 :                         if (s)
     561           2 :                                 s = stmt_binop(be, s, i, NULL, a);
     562             :                         else
     563             :                                 s = i;
     564             :                 }
     565           2 :                 if (sel && !(depth || !reduce))
     566           0 :                         s = stmt_uselect(be,
     567             :                                 stmt_const(be, bin_find_smallest_column(be, left), s),
     568             :                                 stmt_bool(be, 1), cmp_equal, sel, 0, 0);
     569       17164 :         } else if (list_length(nl) < 16) {
     570       17120 :                 comp_type cmp = (in)?cmp_equal:cmp_notequal;
     571             : 
     572       17120 :                 if (!in)
     573        7114 :                         s = sel;
     574       92125 :                 for (n = nl->h; n; n = n->next) {
     575       75005 :                         sql_exp *e = n->data;
     576       75005 :                         stmt *i = exp_bin(be, e, left, right, grp, ext, cnt, NULL, depth+1, 0, push);
     577       75005 :                         if(!i)
     578             :                                 return NULL;
     579             : 
     580       75005 :                         if (in) {
     581       50895 :                                 i = stmt_uselect(be, c, i, cmp, sel, 0, 0);
     582       50895 :                                 if (s)
     583       40889 :                                         s = stmt_tunion(be, s, i);
     584             :                                 else
     585             :                                         s = i;
     586             :                         } else {
     587       24110 :                                 s = stmt_uselect(be, c, i, cmp, s, 0, 0);
     588             :                         }
     589             :                 }
     590             :         } else {
     591             :                 /* TODO: handle_in_exps should contain all necessary logic for in-expressions to be SQL compliant.
     592             :                    For non-SQL-standard compliant behavior, e.g. PostgreSQL backwards compatibility, we should
     593             :                    make sure that this behavior is replicated by the sql optimizer and not handle_in_exps. */
     594             : 
     595          44 :                 stmt* last_null_value = NULL;  /* CORNER CASE ALERT: See description below. */
     596             : 
     597             :                 /* The actual in-value-list should not contain duplicates to ensure that final join results are unique. */
     598          44 :                 s = distinct_value_list(be, nl, &last_null_value, depth+1, push);
     599          44 :                 if (!s)
     600           0 :                         return NULL;
     601             : 
     602          44 :                 if (last_null_value) {
     603             :                         /* The actual in-value-list should not contain null values. */
     604           0 :                         s = stmt_project(be, stmt_selectnonil(be, s, NULL), s);
     605             :                 }
     606             : 
     607          44 :                 if (in) {
     608          42 :                         s = stmt_semijoin(be, c, s, sel, NULL, 0, false);
     609             :                 } else {
     610           2 :                         if (last_null_value) {
     611             :                                 /* CORNER CASE ALERT:
     612             :                                    In case of a not-in-expression with the associated in-value-list containing a null value,
     613             :                                    the entire in-predicate is forced to always return false, i.e. an empty candidate list.
     614             :                                    This is similar to postgres behavior.
     615             :                                    TODO: However I do not think this behavior is in accordance with SQL standard 2003.
     616             : 
     617             :                                    Ugly trick to return empty candidate list, because for all x it holds that: (x == null) == false.
     618             :                                    list* singleton_bat = sa_list(sql->sa);
     619             :                                    list_append(singleton_bat, null_value); */
     620           0 :                                 s = stmt_uselect(be, c, last_null_value, cmp_equal, NULL, 0, 0);
     621             :                         } else {
     622             :                                 /* BACK TO HAPPY FLOW:
     623             :                                    Make sure that null values are never returned. */
     624           2 :                                 stmt* non_nulls;
     625           2 :                                 non_nulls = stmt_selectnonil(be, c, sel);
     626           2 :                                 s = stmt_tdiff(be, stmt_project(be, non_nulls, c), s, NULL);
     627           2 :                                 s = stmt_project(be, s, non_nulls);
     628             :                         }
     629             :                 }
     630             :         }
     631             :         return s;
     632             : }
     633             : 
     634             : static stmt *
     635      138277 : value_list(backend *be, list *vals, stmt *left, stmt *sel)
     636             : {
     637      138277 :         sql_subtype *type = exp_subtype(vals->h->data);
     638      138267 :         list *l;
     639             : 
     640      138267 :         if (!type)
     641           0 :                 return sql_error(be->mvc, 02, SQLSTATE(42000) "Could not infer the type of a value list column");
     642             :         /* create bat append values */
     643      138267 :         l = sa_list(be->mvc->sa);
     644      886232 :         for (node *n = vals->h; n; n = n->next) {
     645      748582 :                 sql_exp *e = n->data;
     646      748582 :                 stmt *i = exp_bin(be, e, left, NULL, NULL, NULL, NULL, sel, 0, 0, 0);
     647             : 
     648      748610 :                 if (!i)
     649             :                         return NULL;
     650             : 
     651      748610 :                 if (list_length(vals) == 1)
     652         574 :                         return i;
     653      748029 :                 list_append(l, i);
     654             :         }
     655      137650 :         return stmt_append_bulk(be, stmt_temp(be, type), l);
     656             : }
     657             : 
     658             : static stmt *
     659         282 : exp_list(backend *be, list *exps, stmt *l, stmt *r, stmt *grp, stmt *ext, stmt *cnt, stmt *sel)
     660             : {
     661         282 :         mvc *sql = be->mvc;
     662         282 :         node *n;
     663         282 :         list *nl = sa_list(sql->sa);
     664             : 
     665         282 :         if (nl == NULL)
     666             :                 return NULL;
     667         673 :         for (n = exps->h; n; n = n->next) {
     668         391 :                 sql_exp *e = n->data;
     669         391 :                 stmt *i = exp_bin(be, e, l, r, grp, ext, cnt, sel, 0, 0, 0);
     670         391 :                 if(!i)
     671             :                         return NULL;
     672             : 
     673         391 :                 if (n->next && i->type == st_table) /* relational statement */
     674           3 :                         l = i->op1;
     675             :                 else
     676         388 :                         append(nl, i);
     677             :         }
     678         282 :         return stmt_list(be, nl);
     679             : }
     680             : 
     681             : static stmt *
     682        1644 : exp_count_no_nil_arg(sql_exp *e, stmt *ext, sql_exp *ae, stmt *as)
     683             : {
     684             :         /* small optimization, ie use candidates directly on count(*) */
     685        1644 :         if (!need_distinct(e) && !ext && as && (!need_no_nil(e) || !ae || !has_nil(ae))) {
     686             :                 /* skip alias statements */
     687        3154 :                 while (as->type == st_alias)
     688        1646 :                         as = as->op1;
     689             :                 /* use candidate */
     690        1508 :                 if (as && as->type == st_join && as->flag == cmp_project) {
     691        1378 :                         if (as->op1 && (as->op1->type != st_result || as->op1->op1->type != st_group)) /* exclude a subquery with select distinct under the count */
     692        1644 :                                 as = as->op1;
     693             :                 }
     694             :         }
     695        1644 :         return as;
     696             : }
     697             : 
     698             : static stmt *
     699       13147 : exp_bin_or(backend *be, sql_exp *e, stmt *left, stmt *right, stmt *grp, stmt *ext, stmt *cnt, stmt *sel, int depth, bool reduce, int push)
     700             : {
     701       13147 :         sql_subtype *bt = sql_bind_localtype("bit");
     702       13147 :         list *l = e->l;
     703       13147 :         node *n;
     704       13147 :         stmt *sel1 = NULL, *sel2 = NULL, *s = NULL;
     705       13147 :         int anti = is_anti(e);
     706             : 
     707       13147 :         sel1 = sel;
     708       13147 :         sel2 = sel;
     709       27440 :         for (n = l->h; n; n = n->next) {
     710       14293 :                 sql_exp *c = n->data;
     711       14293 :                 stmt *sin = (sel1 && sel1->nrcols)?sel1:NULL;
     712             : 
     713             :                 /* propagate the anti flag */
     714       14293 :                 if (anti)
     715          55 :                         set_anti(c);
     716       14297 :                 s = exp_bin(be, c, left, right, grp, ext, cnt, reduce?sin:NULL, depth, reduce, push);
     717       14293 :                 if (!s)
     718             :                         return s;
     719             : 
     720       14293 :                 if (!reduce && sin) {
     721           0 :                         sql_subfunc *f = sql_bind_func(be->mvc, "sys", anti?"or":"and", bt, bt, F_FUNC, true, true);
     722           0 :                         assert(f);
     723           0 :                         s = stmt_binop(be, sin, s, NULL, f);
     724       14293 :                 } else if (!sin && sel1 && sel1->nrcols == 0 && s->nrcols == 0) {
     725           0 :                         sql_subfunc *f = sql_bind_func(be->mvc, "sys", anti?"or":"and", bt, bt, F_FUNC, true, true);
     726           0 :                         assert(f);
     727           0 :                         s = stmt_binop(be, sel1, s, sin, f);
     728       14293 :                 } else if (sel1 && (sel1->nrcols == 0 || s->nrcols == 0)) {
     729           1 :                         stmt *predicate = bin_find_smallest_column(be, left);
     730             : 
     731           1 :                         predicate = stmt_const(be, predicate, stmt_bool(be, 1));
     732           1 :                         if (s->nrcols == 0)
     733           0 :                                 s = stmt_uselect(be, predicate, s, cmp_equal, sel1, anti, is_semantics(c));
     734             :                         else
     735           1 :                                 s = stmt_uselect(be, predicate, sel1, cmp_equal, s, anti, is_semantics(c));
     736             :                 }
     737       14293 :                 sel1 = s;
     738             :         }
     739       13147 :         l = e->r;
     740       28675 :         for (n = l->h; n; n = n->next) {
     741       15528 :                 sql_exp *c = n->data;
     742       15528 :                 stmt *sin = (sel2 && sel2->nrcols)?sel2:NULL;
     743             : 
     744             :                 /* propagate the anti flag */
     745       15528 :                 if (anti)
     746          55 :                         set_anti(c);
     747       15533 :                 s = exp_bin(be, c, left, right, grp, ext, cnt, reduce?sin:NULL, depth, reduce, push);
     748       15528 :                 if (!s)
     749             :                         return s;
     750             : 
     751       15528 :                 if (!reduce && sin) {
     752           2 :                         sql_subfunc *f = sql_bind_func(be->mvc, "sys", anti?"or":"and", bt, bt, F_FUNC, true, true);
     753           1 :                         assert(f);
     754           1 :                         s = stmt_binop(be, sin, s, NULL, f);
     755       15527 :                 } else if (!sin && sel2 && sel2->nrcols == 0 && s->nrcols == 0) {
     756           0 :                         sql_subfunc *f = sql_bind_func(be->mvc, "sys", anti?"or":"and", bt, bt, F_FUNC, true, true);
     757           0 :                         assert(f);
     758           0 :                         s = stmt_binop(be, sel2, s, sin, f);
     759       15527 :                 } else if (sel2 && (sel2->nrcols == 0 || s->nrcols == 0)) {
     760           6 :                         stmt *predicate = bin_find_smallest_column(be, left);
     761             : 
     762           6 :                         predicate = stmt_const(be, predicate, stmt_bool(be, 1));
     763           6 :                         if (s->nrcols == 0)
     764           5 :                                 s = stmt_uselect(be, predicate, s, cmp_equal, sel2, anti, 0);
     765             :                         else
     766           1 :                                 s = stmt_uselect(be, predicate, sel2, cmp_equal, s, anti, 0);
     767             :                 }
     768       15528 :                 sel2 = s;
     769             :         }
     770       13147 :         if (sel1->nrcols == 0 && sel2->nrcols == 0) {
     771           0 :                 sql_subfunc *f = sql_bind_func(be->mvc, "sys", anti?"and":"or", bt, bt, F_FUNC, true, true);
     772           0 :                 assert(f);
     773           0 :                 return stmt_binop(be, sel1, sel2, NULL, f);
     774             :         }
     775       13147 :         if (sel1->nrcols == 0) {
     776           2 :                 stmt *predicate = bin_find_smallest_column(be, left);
     777             : 
     778           2 :                 if (!reduce) {
     779           0 :                         predicate = stmt_const(be, predicate, sel1);
     780             :                 } else {
     781           2 :                         predicate = stmt_const(be, predicate, stmt_bool(be, 1));
     782           2 :                         sel1 = stmt_uselect(be, predicate, sel1, cmp_equal, NULL, 0/*anti*/, 0);
     783             :                 }
     784             :         }
     785       13147 :         if (sel2->nrcols == 0) {
     786          81 :                 stmt *predicate = bin_find_smallest_column(be, left);
     787             : 
     788          81 :                 if (!reduce) {
     789           0 :                         predicate = stmt_const(be, predicate, sel2);
     790             :                 } else {
     791          81 :                         predicate = stmt_const(be, predicate, stmt_bool(be, 1));
     792          81 :                         sel2 = stmt_uselect(be, predicate, sel2, cmp_equal, NULL, 0/*anti*/, 0);
     793             :                 }
     794             :         }
     795       13147 :         if (!reduce) {
     796           8 :                         sql_subfunc *f = sql_bind_func(be->mvc, "sys", anti?"and":"or", bt, bt, F_FUNC, true, true);
     797           4 :                         assert(f);
     798           4 :                         return stmt_binop(be, sel1, sel2, NULL, f);
     799             :         }
     800       13143 :         if (anti)
     801          55 :                 return stmt_project(be, stmt_tinter(be, sel1, sel2, false), sel1);
     802       13088 :         return stmt_tunion(be, sel1, sel2);
     803             : }
     804             : 
     805             : static stmt *
     806       17995 : exp2bin_case(backend *be, sql_exp *fe, stmt *left, stmt *right, stmt *isel, int depth)
     807             : {
     808       17995 :         stmt *res = NULL, *ires = NULL, *rsel = NULL, *osel = NULL, *ncond = NULL, *ocond = NULL, *cond = NULL;
     809       17995 :         int next_cond = 1, single_value = (fe->card <= CARD_ATOM && (!left || !left->nrcols));
     810       17995 :         char name[16], *nme = NULL;
     811       17995 :         sql_subtype *bt = sql_bind_localtype("bit");
     812       17995 :         sql_subfunc *not = sql_bind_func(be->mvc, "sys", "not", bt, NULL, F_FUNC, true, true);
     813       17995 :         sql_subfunc *or = sql_bind_func(be->mvc, "sys", "or", bt, bt, F_FUNC, true, true);
     814       17995 :         sql_subfunc *and = sql_bind_func(be->mvc, "sys", "and", bt, bt, F_FUNC, true, true);
     815             : 
     816       17995 :         if (single_value) {
     817             :                 /* var_x = nil; */
     818          90 :                 nme = number2name(name, sizeof(name), ++be->mvc->label);
     819          90 :                 (void)stmt_var(be, NULL, nme, exp_subtype(fe), 1, 2);
     820             :         }
     821             : 
     822       17995 :         list *exps = fe->l;
     823             : 
     824             :         /*
     825             :          * left - isel: calls down need id's from the range of left
     826             :          * res  - rsel: updates to res need id's in the range from res
     827             :          */
     828       77148 :         for (node *en = exps->h; en; en = en->next) {
     829       59153 :                 sql_exp *e = en->data;
     830             : 
     831       59153 :                 next_cond = next_cond && en->next; /* last else is only a value */
     832             : 
     833       59153 :                 stmt *nsel = rsel;
     834       59153 :                 if (!single_value) {
     835       58829 :                         if (/*!next_cond &&*/ rsel && isel) {
     836             :                                 /* back into left range */
     837        8430 :                                 nsel = stmt_project(be, rsel, isel);
     838       50399 :                         } else if (isel && !rsel)
     839       59153 :                                 nsel = isel;
     840             :                 }
     841       59153 :                 stmt *es = exp_bin(be, e, left, right, NULL, NULL, NULL, nsel, depth+1, 0, 1);
     842             : 
     843       59153 :                 if (!es)
     844             :                         return NULL;
     845       59153 :                 if (!single_value) {
     846             :                         /* create result */
     847       58829 :                         if (!res) {
     848       17905 :                                 stmt *l = isel;
     849       17905 :                                 if (!l)
     850       13704 :                                         l = bin_find_smallest_column(be, left);
     851       17905 :                                 res = stmt_const(be, l, stmt_atom(be, atom_general(be->mvc->sa, exp_subtype(fe), NULL, 0)));
     852       17905 :                                 ires = l;
     853       17905 :                                 if (res)
     854       17905 :                                         res->cand = isel;
     855       40924 :                         } else if (res && !next_cond) { /* use result to update column */
     856       38367 :                                 stmt *val = es;
     857       38367 :                                 stmt *pos = rsel;
     858             : 
     859       38367 :                                 if (val->nrcols == 0)
     860       18975 :                                         val = stmt_const(be, pos, val);
     861       19392 :                                 else if (!val->cand && nsel)
     862        4060 :                                         val = stmt_project(be, nsel, val);
     863       38367 :                                 res = stmt_replace(be, res, pos, val);
     864             : 
     865       38367 :                                 assert(cond);
     866             : 
     867       38367 :                                 if (en->next) {
     868             :                                         /* osel - rsel */
     869       20462 :                                         if (!osel)
     870       17905 :                                                 osel = stmt_mirror(be, ires);
     871       20462 :                                         stmt *d = stmt_tdiff(be, osel, rsel, NULL);
     872       20462 :                                         osel = rsel = stmt_project(be, d, osel);
     873             :                                 }
     874             :                         }
     875       58829 :                         if (next_cond) {
     876       20462 :                                 ncond = cond = es;
     877       20462 :                                 if (!ncond->nrcols) {
     878         132 :                                         if (osel) {
     879          75 :                                                 ncond = stmt_const(be, nsel, ncond);
     880          75 :                                                 ncond->cand = nsel;
     881          57 :                                         } else if (isel) {
     882           3 :                                                 ncond = stmt_const(be, isel, ncond);
     883           3 :                                                 ncond->cand = isel;
     884             :                                         } else
     885          54 :                                                 ncond = stmt_const(be, bin_find_smallest_column(be, left), ncond);
     886             :                                 }
     887       20462 :                                 if (isel && !ncond->cand) {
     888          10 :                                         ncond = stmt_project(be, nsel, ncond);
     889          10 :                                         ncond->cand = nsel;
     890             :                                 }
     891       27212 :                                 stmt *s = stmt_uselect(be, ncond, stmt_bool(be, 1), cmp_equal, !ncond->cand?rsel:NULL, 0/*anti*/, 0);
     892       20462 :                                 if (rsel && ncond->cand)
     893        2549 :                                         rsel = stmt_project(be, s, rsel);
     894             :                                 else
     895             :                                         rsel = s;
     896             :                         }
     897             :                 } else {
     898         324 :                         if (!res) {
     899             :                                 /* if_barrier ... */
     900         117 :                                 assert(next_cond);
     901         117 :                                 if (next_cond) {
     902         117 :                                         if (cond) {
     903          27 :                                                 ncond = stmt_binop(be, cond, es, nsel, and);
     904             :                                         } else {
     905             :                                                 ncond = es;
     906             :                                         }
     907             :                                         cond = es;
     908             :                                 }
     909             :                         } else {
     910             :                                 /* var_x = s */
     911         207 :                                 (void)stmt_assign(be, NULL, nme, es, 2);
     912             :                                 /* endif_barrier */
     913         207 :                                 (void)stmt_control_end(be, res);
     914         207 :                                 res = NULL;
     915             : 
     916         207 :                                 if (en->next) {
     917         117 :                                         cond = stmt_unop(be, cond, nsel, not);
     918             : 
     919         117 :                                         sql_subfunc *isnull = sql_bind_func(be->mvc, "sys", "isnull", bt, NULL, F_FUNC, true, true);
     920         117 :                                         cond = stmt_binop(be, cond, stmt_unop(be, cond, nsel, isnull), nsel, or);
     921         117 :                                         if (ocond)
     922          27 :                                                 cond = stmt_binop(be, ocond, cond, nsel, and);
     923         117 :                                         ocond = cond;
     924         117 :                                         if (!en->next->next)
     925          90 :                                                 ncond = cond;
     926             :                                 }
     927             :                         }
     928         324 :                         if (ncond && (next_cond || (en->next && !en->next->next))) {
     929             :                                 /* if_barrier ... */
     930         207 :                                 res = stmt_cond(be, ncond, NULL, 0, 0);
     931             :                         }
     932             :                 }
     933       59153 :                 next_cond = !next_cond;
     934             :         }
     935       17995 :         if (single_value)
     936          90 :                 return stmt_var(be, NULL, nme, exp_subtype(fe), 0, 2);
     937             :         return res;
     938             : }
     939             : 
     940             : static stmt *
     941           3 : exp2bin_named_placeholders(backend *be, sql_exp *fe)
     942             : {
     943           3 :         int argc = 0;
     944           3 :         char arg[IDLENGTH];
     945           3 :         list *args = fe->l;
     946             : 
     947           3 :         if (list_empty(args))
     948             :                 return NULL;
     949           8 :         for (node *n = args->h; n; n = n->next, argc++) {
     950           5 :                 sql_exp *a = n->data;
     951           5 :                 sql_subtype *t = exp_subtype(a);
     952           5 :                 stmt *s = exp_bin(be, a, NULL, NULL, NULL, NULL, NULL, NULL, 1, 0, 1);
     953           5 :                 InstrPtr q = newAssignment(be->mb);
     954             : 
     955           5 :                 if (!q || !t || !s) {
     956           0 :             sql_error(be->mvc, 10, SQLSTATE(42000) MAL_MALLOC_FAIL);
     957           0 :                         return NULL;
     958             :                 }
     959           5 :         int type = t->type->localtype, varid = 0;
     960             : 
     961           5 :         snprintf(arg, IDLENGTH, "A%d", argc);
     962           5 :         if ((varid = newVariable(be->mb, arg, strlen(arg), type)) < 0) {
     963           0 :             sql_error(be->mvc, 10, SQLSTATE(42000) "Internal error while compiling statement: variable id too long");
     964           0 :                         return NULL;
     965             :         }
     966           5 :                 if (q)
     967           5 :                         getDestVar(q) = varid;
     968           5 :         q = pushArgument(be->mb, q, s->nr);
     969           5 :                 pushInstruction(be->mb, q);
     970             :         }
     971             :         return NULL;
     972             : }
     973             : 
     974             : static stmt *
     975        2284 : exp2bin_casewhen(backend *be, sql_exp *fe, stmt *left, stmt *right, stmt *isel, int depth)
     976             : {
     977        2284 :         stmt *res = NULL, *ires = NULL, *rsel = NULL, *osel = NULL, *ncond = NULL, *ocond = NULL, *cond = NULL;
     978        2284 :         int next_cond = 1, single_value = (fe->card <= CARD_ATOM && (!left || !left->nrcols));
     979        2284 :         char name[16], *nme = NULL;
     980        2284 :         sql_subtype *bt = sql_bind_localtype("bit");
     981        2284 :         sql_subfunc *not = sql_bind_func(be->mvc, "sys", "not", bt, NULL, F_FUNC, true, true);
     982        2284 :         sql_subfunc *or = sql_bind_func(be->mvc, "sys", "or", bt, bt, F_FUNC, true, true);
     983        2284 :         sql_subfunc *and = sql_bind_func(be->mvc, "sys", "and", bt, bt, F_FUNC, true, true);
     984        2284 :         sql_subfunc *cmp;
     985             : 
     986        2284 :         if (single_value) {
     987             :                 /* var_x = nil; */
     988          86 :                 nme = number2name(name, sizeof(name), ++be->mvc->label);
     989          86 :                 (void)stmt_var(be, NULL, nme, exp_subtype(fe), 1, 2);
     990             :         }
     991             : 
     992        2284 :         list *exps = fe->l;
     993        2284 :         node *en = exps->h;
     994        2284 :         sql_exp *e = en->data;
     995             : 
     996        2284 :         stmt *nsel = !single_value?isel:NULL;
     997        2284 :         stmt *case_when = exp_bin(be, e, left, right, NULL, NULL, NULL, nsel, depth+1, 0, 1);
     998        2284 :         if (!case_when)
     999             :                 return NULL;
    1000        2284 :         cmp = sql_bind_func(be->mvc, "sys", "=", exp_subtype(e), exp_subtype(e), F_FUNC, true, true);
    1001        2284 :         if (!cmp)
    1002             :                 return NULL;
    1003        2284 :         if (!single_value && !case_when->nrcols) {
    1004          34 :                 stmt *l = isel;
    1005          34 :                 if (!l && left)
    1006          29 :                         l = bin_find_smallest_column(be, left);
    1007           5 :                 else if (!l)
    1008             :                         return NULL;
    1009          34 :                 case_when = stmt_const(be, l, case_when);
    1010          34 :                 if (case_when)
    1011          34 :                         case_when->cand = isel;
    1012             :         }
    1013        2284 :         if (!single_value && isel && !case_when->cand) {
    1014         308 :                 case_when = stmt_project(be, isel, case_when);
    1015         308 :                 case_when->cand = isel;
    1016             :         }
    1017             : 
    1018             :         /*
    1019             :          * left - isel: calls down need id's from the range of left
    1020             :          * res  - rsel: updates to res need id's in the range from res
    1021             :          */
    1022       32032 :         for (en = en->next; en; en = en->next) {
    1023       29748 :                 sql_exp *e = en->data;
    1024             : 
    1025       29748 :                 next_cond = next_cond && en->next; /* last else is only a value */
    1026             : 
    1027       29748 :                 stmt *nsel = rsel;
    1028       29748 :                 if (!single_value) {
    1029       28920 :                         if (/*!next_cond &&*/ rsel && isel) {
    1030             :                                 /* back into left range */
    1031        3954 :                                 nsel = stmt_project(be, rsel, isel);
    1032       24966 :                         } else if (isel && !rsel)
    1033       29748 :                                 nsel = isel;
    1034             :                 }
    1035       29748 :                 stmt *es = exp_bin(be, e, left, right, NULL, NULL, NULL, nsel, depth+1, 0, 1);
    1036             : 
    1037       29748 :                 if (!es)
    1038             :                         return NULL;
    1039       29748 :                 if (next_cond) {
    1040       13732 :                         stmt *l = case_when;
    1041       13732 :                         if (!single_value) {
    1042       13361 :                                 if (rsel && isel) {
    1043        1662 :                                         assert(l->cand == isel);
    1044        1662 :                                         l = stmt_project(be, rsel, l);
    1045        1662 :                                         l->cand = nsel;
    1046             :                                 }
    1047             : 
    1048       13361 :                                 if (es->cand && !l->cand) {
    1049           9 :                                         assert(es->cand == rsel);
    1050           9 :                                         l = stmt_project(be, es->cand, l);
    1051           9 :                                         l->cand = es->cand;
    1052       13352 :                                 } else if (nsel && !es->cand) {
    1053       11467 :                                         es = stmt_project(be, nsel, es);
    1054       11467 :                                         es->cand = nsel;
    1055       11467 :                                         if (!l->cand) {
    1056        9492 :                                                 l = stmt_project(be, nsel, l);
    1057        9492 :                                                 l->cand = nsel;
    1058             :                                         }
    1059             :                                 }
    1060       13361 :                                 assert(l->cand == es->cand);
    1061             :                         }
    1062       13732 :                         es = stmt_binop(be, l, es, NULL, cmp);
    1063             :                 }
    1064       29748 :                 if (!single_value) {
    1065             :                         /* create result */
    1066       28920 :                         if (!res) {
    1067        2198 :                                 stmt *l = isel;
    1068        2198 :                                 if (!l)
    1069        1883 :                                         l = bin_find_smallest_column(be, left);
    1070        2198 :                                 res = stmt_const(be, l, stmt_atom(be, atom_general(be->mvc->sa, exp_subtype(fe), NULL, 0)));
    1071        2198 :                                 ires = l;
    1072        2198 :                                 if (res)
    1073        2198 :                                         res->cand = isel;
    1074       26722 :                         } else if (res && !next_cond) { /* use result to update column */
    1075       15559 :                                 stmt *val = es;
    1076       15559 :                                 stmt *pos = rsel;
    1077             : 
    1078       15559 :                                 if (val->nrcols == 0)
    1079       13887 :                                         val = stmt_const(be, pos, val);
    1080        1672 :                                 else if (!val->cand && nsel)
    1081         209 :                                         val = stmt_project(be, nsel, val);
    1082       15559 :                                 res = stmt_replace(be, res, pos, val);
    1083             : 
    1084       15559 :                                 assert(cond);
    1085             : 
    1086       15559 :                                 if (en->next) {
    1087             :                                         /* osel - rsel */
    1088       13361 :                                         if (!osel)
    1089        2198 :                                                 osel = stmt_mirror(be, ires);
    1090       13361 :                                         stmt *d = stmt_tdiff(be, osel, rsel, NULL);
    1091       13361 :                                         osel = rsel = stmt_project(be, d, osel);
    1092             :                                 }
    1093             :                         }
    1094       28920 :                         if (next_cond) {
    1095       13361 :                                 ncond = cond = es;
    1096       13361 :                                 if (!ncond->nrcols) {
    1097           0 :                                         if (osel) {
    1098           0 :                                                 ncond = stmt_const(be, nsel, ncond);
    1099           0 :                                                 ncond->cand = nsel;
    1100           0 :                                         } else if (isel) {
    1101           0 :                                                 ncond = stmt_const(be, isel, ncond);
    1102           0 :                                                 ncond->cand = isel;
    1103             :                                         } else
    1104           0 :                                                 ncond = stmt_const(be, bin_find_smallest_column(be, left), ncond);
    1105             :                                 }
    1106       13361 :                                 if (isel && !ncond->cand)
    1107           0 :                                         ncond = stmt_project(be, nsel, ncond);
    1108       24839 :                                 stmt *s = stmt_uselect(be, ncond, stmt_bool(be, 1), cmp_equal, !ncond->cand?rsel:NULL, 0/*anti*/, 0);
    1109       13361 :                                 if (rsel && ncond->cand)
    1110       11163 :                                         rsel = stmt_project(be, s, rsel);
    1111             :                                 else
    1112             :                                         rsel = s;
    1113             :                         }
    1114             :                 } else {
    1115         828 :                         if (!res) {
    1116             :                                 /* if_barrier ... */
    1117         371 :                                 assert(next_cond);
    1118         371 :                                 if (next_cond) {
    1119         371 :                                         if (cond) {
    1120         285 :                                                 ncond = stmt_binop(be, cond, es, nsel, and);
    1121             :                                         } else {
    1122             :                                                 ncond = es;
    1123             :                                         }
    1124             :                                         cond = es;
    1125             :                                 }
    1126             :                         } else {
    1127             :                                 /* var_x = s */
    1128         457 :                                 (void)stmt_assign(be, NULL, nme, es, 2);
    1129             :                                 /* endif_barrier */
    1130         457 :                                 (void)stmt_control_end(be, res);
    1131         457 :                                 res = NULL;
    1132             : 
    1133         457 :                                 if (en->next) {
    1134         371 :                                         cond = stmt_unop(be, cond, nsel, not);
    1135             : 
    1136         371 :                                         sql_subfunc *isnull = sql_bind_func(be->mvc, "sys", "isnull", bt, NULL, F_FUNC, true, true);
    1137         371 :                                         cond = stmt_binop(be, cond, stmt_unop(be, cond, nsel, isnull), nsel, or);
    1138         371 :                                         if (ocond)
    1139         285 :                                                 cond = stmt_binop(be, ocond, cond, nsel, and);
    1140         371 :                                         ocond = cond;
    1141         371 :                                         if (!en->next->next)
    1142          86 :                                                 ncond = cond;
    1143             :                                 }
    1144             :                         }
    1145         828 :                         if (ncond && (next_cond || (en->next && !en->next->next))) {
    1146             :                                 /* if_barrier ... */
    1147         457 :                                 res = stmt_cond(be, ncond, NULL, 0, 0);
    1148             :                         }
    1149             :                 }
    1150       29748 :                 next_cond = !next_cond;
    1151             :         }
    1152        2284 :         if (single_value)
    1153          86 :                 return stmt_var(be, NULL, nme, exp_subtype(fe), 0, 2);
    1154             :         return res;
    1155             : }
    1156             : 
    1157             : static stmt*
    1158         527 : exp2bin_coalesce(backend *be, sql_exp *fe, stmt *left, stmt *right, stmt *isel, int depth)
    1159             : {
    1160         527 :         stmt *res = NULL, *rsel = NULL, *osel = NULL, *ncond = NULL, *ocond = NULL;
    1161         527 :         int single_value = (fe->card <= CARD_ATOM && (!left || !left->nrcols));
    1162         527 :         char name[16], *nme = NULL;
    1163         527 :         sql_subtype *bt = sql_bind_localtype("bit");
    1164         527 :         sql_subfunc *and = sql_bind_func(be->mvc, "sys", "and", bt, bt, F_FUNC, true, true);
    1165         527 :         sql_subfunc *not = sql_bind_func(be->mvc, "sys", "not", bt, NULL, F_FUNC, true, true);
    1166             : 
    1167         527 :         if (single_value) {
    1168             :                 /* var_x = nil; */
    1169          57 :                 nme = number2name(name, sizeof(name), ++be->mvc->label);
    1170          57 :                 (void)stmt_var(be, NULL, nme, exp_subtype(fe), 1, 2);
    1171             :         }
    1172             : 
    1173         527 :         list *exps = fe->l;
    1174        2028 :         for (node *en = exps->h; en; en = en->next) {
    1175        1501 :                 sql_exp *e = en->data;
    1176             : 
    1177        1501 :                 stmt *nsel = rsel;
    1178        1501 :                 if (!single_value) {
    1179        1346 :                         if (/*!next_cond &&*/ rsel && isel) {
    1180             :                                 /* back into left range */
    1181          48 :                                 nsel = stmt_project(be, rsel, isel);
    1182        1298 :                         } else if (isel && !rsel)
    1183        1501 :                                 nsel = isel;
    1184             :                 }
    1185        1501 :                 stmt *es = exp_bin(be, e, left, right, NULL, NULL, NULL, nsel, depth+1, 0, 1);
    1186             : 
    1187        1501 :                 if (!es)
    1188             :                         return NULL;
    1189             :                 /* create result */
    1190        1501 :                 if (!single_value) {
    1191        1346 :                         if (!res) {
    1192         470 :                                 stmt *l = isel;
    1193         470 :                                 if (!l)
    1194         436 :                                         l = bin_find_smallest_column(be, left);
    1195         470 :                                 res = stmt_const(be, l, stmt_atom(be, atom_general(be->mvc->sa, exp_subtype(fe), NULL, 0)));
    1196         470 :                                 if (res)
    1197         470 :                                         res->cand = isel;
    1198             :                         }
    1199         470 :                         if (res) {
    1200        1346 :                                 stmt *val = es;
    1201        1346 :                                 stmt *pos = rsel;
    1202             : 
    1203        1346 :                                 if (en->next) {
    1204         876 :                                         sql_subfunc *a = sql_bind_func(be->mvc, "sys", "isnotnull", tail_type(es), NULL, F_FUNC, true, true);
    1205         876 :                                         ncond = stmt_unop(be, es, NULL, a);
    1206         876 :                                         if (ncond->nrcols == 0) {
    1207         120 :                                                 stmt *l = bin_find_smallest_column(be, left);
    1208         120 :                                                 if (nsel && l)
    1209          61 :                                                         l = stmt_project(be, nsel, l);
    1210         120 :                                                 ncond = stmt_const(be, l, ncond);
    1211         120 :                                                 if (nsel)
    1212          61 :                                                         ncond->cand = nsel;
    1213         756 :                                         } else if (!ncond->cand && nsel)
    1214         348 :                                                 ncond = stmt_project(be, nsel, ncond);
    1215         876 :                                         stmt *s = stmt_uselect(be, ncond, stmt_bool(be, 1), cmp_equal, NULL, 0/*anti*/, 0);
    1216         876 :                                         if (!val->cand && nsel)
    1217         409 :                                                 val = stmt_project(be, nsel, val);
    1218         876 :                                         val = stmt_project(be, s, val);
    1219         876 :                                         if (osel)
    1220         406 :                                                 rsel = stmt_project(be, s, osel);
    1221             :                                         else
    1222             :                                                 rsel = s;
    1223         876 :                                         pos = rsel;
    1224         876 :                                         val->cand = pos;
    1225             :                                 }
    1226        1346 :                                 if (val->nrcols == 0)
    1227         163 :                                         val = stmt_const(be, pos, val);
    1228        1183 :                                 else if (!val->cand && nsel)
    1229         171 :                                         val = stmt_project(be, nsel, val);
    1230             : 
    1231        1346 :                                 res = stmt_replace(be, res, pos, val);
    1232             :                         }
    1233        1346 :                         if (en->next) { /* handled then part */
    1234         876 :                                 stmt *s = stmt_uselect(be, ncond, stmt_bool(be, 1), cmp_equal, NULL, 1/*anti*/, 0);
    1235         876 :                                 if (osel)
    1236         406 :                                         rsel = stmt_project(be, s, osel);
    1237             :                                 else
    1238             :                                         rsel = s;
    1239             :                                 osel = rsel;
    1240             :                         }
    1241             :                 } else {
    1242         155 :                         stmt *cond = ocond;
    1243         155 :                         if (en->next) {
    1244          98 :                                 sql_subfunc *a = sql_bind_func(be->mvc, "sys", "isnotnull", tail_type(es), NULL, F_FUNC, true, true);
    1245          98 :                                 ncond = stmt_unop(be, es, nsel, a);
    1246             : 
    1247          98 :                                 if (ocond)
    1248          41 :                                         cond = stmt_binop(be, ocond, ncond, nsel, and);
    1249             :                                 else
    1250             :                                         cond = ncond;
    1251             :                         }
    1252             : 
    1253             :                         /* if_barrier ... */
    1254         155 :                         stmt *b = stmt_cond(be, cond, NULL, 0, 0);
    1255             :                         /* var_x = s */
    1256         155 :                         (void)stmt_assign(be, NULL, nme, es, 2);
    1257             :                         /* endif_barrier */
    1258         155 :                         (void)stmt_control_end(be, b);
    1259             : 
    1260         155 :                         cond = stmt_unop(be, ncond, nsel, not);
    1261         155 :                         if (ocond)
    1262          98 :                                 ocond = stmt_binop(be, cond, ocond, nsel, and);
    1263             :                         else
    1264             :                                 ocond = cond;
    1265             :                 }
    1266             :         }
    1267         527 :         if (single_value)
    1268          57 :                 return stmt_var(be, NULL, nme, exp_subtype(fe), 0, 2);
    1269             :         return res;
    1270             : }
    1271             : 
    1272             : // This is the per-column portion of exp2bin_copyfrombinary
    1273             : static stmt *
    1274         216 : emit_loadcolumn(backend *be, stmt *onclient_stmt, stmt *bswap_stmt,  int *count_var, node *file_node, node *type_node)
    1275             : {
    1276         216 :         MalBlkPtr mb = be->mb;
    1277             : 
    1278         216 :         sql_exp *file_exp = file_node->data;
    1279         216 :         stmt *file_stmt = exp_bin(be, file_exp, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    1280         216 :         sql_subtype *subtype = type_node->data;
    1281         216 :         int data_type = subtype->type->localtype;
    1282         216 :         int bat_type = newBatType(data_type);
    1283             : 
    1284             :         // The sql.importColumn operator takes a 'method' string to determine how to
    1285             :         // load the data. This leaves the door open to have multiple loaders for the
    1286             :         // same backend type, for example nul- and newline terminated strings.
    1287             :         // For the time being we just use the name of the storage type as the method
    1288             :         // name.
    1289         216 :         const char *method = ATOMname(data_type);
    1290             : 
    1291         216 :         int width;
    1292         216 :         switch (subtype->type->eclass) {
    1293          37 :                 case EC_DEC:
    1294             :                 case EC_STRING:
    1295          37 :                         width = subtype->digits;
    1296          37 :                         break;
    1297             :                 default:
    1298             :                         width = 0;
    1299             :                         break;
    1300             :         }
    1301             : 
    1302         216 :         int new_count_var = newTmpVariable(mb, TYPE_oid);
    1303             : 
    1304         216 :         InstrPtr p = newStmt(mb, sqlRef, importColumnRef);
    1305         216 :         if (p != NULL) {
    1306         216 :                 setArgType(mb, p, 0, bat_type);
    1307         216 :                 p = pushReturn(mb, p, new_count_var);
    1308             :                 //
    1309         216 :                 p = pushStr(mb, p, method);
    1310         216 :                 p = pushInt(mb, p, width);
    1311         216 :                 p = pushArgument(mb, p, bswap_stmt->nr);
    1312         216 :                 p = pushArgument(mb, p, file_stmt->nr);
    1313         216 :                 p = pushArgument(mb, p, onclient_stmt->nr);
    1314         216 :                 if (*count_var < 0)
    1315          94 :                         p = pushOid(mb, p, 0);
    1316             :                 else
    1317         122 :                         p = pushArgument(mb, p, *count_var);
    1318         216 :                 pushInstruction(mb, p);
    1319             :         }
    1320         216 :         if (p == NULL || mb->errors) {
    1321           0 :                 if (be->mvc->sa->eb.enabled)
    1322           0 :                         eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : mb->errors ? mb->errors : *GDKerrbuf ? GDKerrbuf : "out of memory", 1000);
    1323           0 :                 return sql_error(be->mvc, 10, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    1324             :         }
    1325             : 
    1326         216 :         *count_var = new_count_var;
    1327             : 
    1328         216 :         stmt *s = stmt_blackbox_result(be, p, 0, subtype);
    1329         216 :         return s;
    1330             : }
    1331             : 
    1332             : // Try to predict which column will be quickest to load first
    1333             : static int
    1334         216 : node_type_score(node *n)
    1335             : {
    1336         216 :         sql_subtype *st = n->data;
    1337         216 :         int tpe = st->type->localtype;
    1338         216 :         int stpe = ATOMstorage(tpe);
    1339         216 :         int score = stpe + (stpe == TYPE_bit);
    1340         216 :         return score;
    1341             : }
    1342             : 
    1343             : static stmt*
    1344          94 : exp2bin_copyfrombinary(backend *be, sql_exp *fe, stmt *left, stmt *right, stmt *isel)
    1345             : {
    1346          94 :         mvc *sql = be->mvc;
    1347          94 :         assert(left == NULL); (void)left;
    1348          94 :         assert(right == NULL); (void)right;
    1349          94 :         assert(isel == NULL); (void)isel;
    1350          94 :         sql_subfunc *f = fe->f;
    1351             : 
    1352          94 :         list *arg_list = fe->l;
    1353          94 :         list *type_list = f->res;
    1354          94 :         assert(4 + list_length(type_list) == list_length(arg_list));
    1355             : 
    1356          94 :         sql_exp * onclient_exp = arg_list->h->next->next->data;
    1357          94 :         stmt *onclient_stmt = exp_bin(be, onclient_exp, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    1358          94 :         sql_exp *bswap_exp = arg_list->h->next->next->next->data;
    1359          94 :         stmt *bswap_stmt = exp_bin(be, bswap_exp, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    1360             : 
    1361             :         // If it's ON SERVER we can optimize by running the imports in parallel
    1362          94 :         bool onserver = false;
    1363          94 :         if (onclient_exp->type == e_atom) {
    1364          94 :                 atom *onclient_atom = onclient_exp->l;
    1365          94 :                 int onclient = onclient_atom->data.val.ival;
    1366          94 :                 onserver = (onclient == 0);
    1367             :         }
    1368             : 
    1369          94 :         node *const first_file = arg_list->h->next->next->next->next;
    1370          94 :         node *const first_type = type_list->h;
    1371          94 :         node *file, *type;
    1372             : 
    1373             :         // The first column we load determines the number of rows.
    1374             :         // We pass it on to the other columns.
    1375             :         // The first column to load should therefore be an 'easy' one.
    1376             :         // We identify the columns by the address of their type node.
    1377          94 :         node *prototype_file = first_file;
    1378          94 :         node *prototype_type = first_type;
    1379          94 :         int score = node_type_score(prototype_type);
    1380         216 :         for (file = first_file->next, type = first_type->next; file && type; file = file->next, type = type->next) {
    1381         122 :                 int sc = node_type_score(type);
    1382         122 :                 if (sc < score) {
    1383           6 :                         prototype_file = file;
    1384           6 :                         prototype_type = type;
    1385           6 :                         score = sc;
    1386             :                 }
    1387             :         }
    1388             : 
    1389             :         // Emit the columns
    1390          94 :         int count_var = -1;
    1391          94 :         list *columns = sa_list(sql->sa);
    1392          94 :         if (columns == NULL)
    1393             :                 return NULL;
    1394          94 :         stmt *prototype_stmt = emit_loadcolumn(be, onclient_stmt, bswap_stmt, &count_var, prototype_file, prototype_type);
    1395          94 :         if (!prototype_stmt)
    1396             :                 return NULL;
    1397          94 :         int orig_count_var = count_var;
    1398         310 :         for (file = first_file, type = first_type; file && type; file = file->next, type = type->next) {
    1399         216 :                 stmt *s;
    1400         216 :                 if (type == prototype_type) {
    1401             :                         s = prototype_stmt;
    1402             :                 } else {
    1403         122 :                         s = emit_loadcolumn(be, onclient_stmt, bswap_stmt, &count_var, file, type);
    1404         122 :                         if (!s)
    1405             :                                 return NULL;
    1406             :                 }
    1407         216 :                 list_append(columns, s);
    1408         216 :                 if (onserver) {
    1409             :                         // Not threading the count variable from one importColumn to the next
    1410             :                         // makes it possible to run them in parallel in a dataflow region.
    1411         106 :                         count_var = orig_count_var;
    1412             :                 }
    1413             :         }
    1414             : 
    1415          94 :         return stmt_list(be, columns);
    1416             : }
    1417             : 
    1418             : static bool
    1419         241 : is_const_func(sql_subfunc *f, list *attr)
    1420             : {
    1421         241 :         if (list_length(attr) != 2)
    1422             :                 return false;
    1423         134 :         if (strcmp(f->func->base.name, "quantile") == 0 ||
    1424         107 :             strcmp(f->func->base.name, "quantile_avg") == 0)
    1425          30 :                 return true;
    1426             :         return false;
    1427             : }
    1428             : 
    1429             : static stmt*
    1430          12 : exp2bin_file_loader(backend *be, sql_exp *fe, stmt *left, stmt *right, stmt *sel)
    1431             : {
    1432          12 :         assert(left == NULL); (void)left;
    1433          12 :         assert(right == NULL); (void)right;
    1434          12 :         assert(sel == NULL); (void)sel;
    1435          12 :         sql_subfunc *f = fe->f;
    1436             : 
    1437          12 :         list *arg_list = fe->l;
    1438             :         /*
    1439             :         list *type_list = f->res;
    1440             :         assert(1 + list_length(type_list) == list_length(arg_list));
    1441             :         */
    1442             : 
    1443          12 :         sql_exp *eexp = arg_list->h->next->data;
    1444          12 :         assert(is_atom(eexp->type));
    1445          12 :         atom *ea = eexp->l;
    1446          12 :         assert(ea->data.vtype == TYPE_str);
    1447          12 :         char *ext = ea->data.val.sval;
    1448             : 
    1449          12 :         file_loader_t *fl = fl_find(ext);
    1450          12 :         if (!fl)
    1451           0 :                 fl = fl_find("csv");
    1452           0 :         if (!fl)
    1453             :                 return NULL;
    1454          12 :         sql_exp *fexp = arg_list->h->data;
    1455          12 :         assert(is_atom(fexp->type));
    1456          12 :         atom *fa = fexp->l;
    1457          12 :         assert(fa->data.vtype == TYPE_str);
    1458          12 :         char *filename = fa->data.val.sval;
    1459          12 :         sql_exp *topn = NULL;
    1460          12 :         if (list_length(arg_list) == 3)
    1461           0 :                 topn = list_fetch(arg_list, 2);
    1462          12 :         return (stmt*)fl->load(be, f, filename, topn);
    1463             : }
    1464             : 
    1465             : stmt *
    1466     4850567 : exp_bin(backend *be, sql_exp *e, stmt *left, stmt *right, stmt *grp, stmt *ext, stmt *cnt, stmt *sel, int depth, int reduce, int push)
    1467             : {
    1468     4850567 :         mvc *sql = be->mvc;
    1469     4850567 :         stmt *s = NULL;
    1470             : 
    1471     4850567 :         if (mvc_highwater(sql))
    1472           0 :                 return sql_error(be->mvc, 10, SQLSTATE(42000) "Query too complex: running out of stack space");
    1473             : 
    1474     4850643 :         if (!e) {
    1475           0 :                 assert(0);
    1476             :                 return NULL;
    1477             :         }
    1478             : 
    1479     4850643 :         switch(e->type) {
    1480        7528 :         case e_psm:
    1481        7528 :                 if (e->flag & PSM_SET) {
    1482         597 :                         stmt *r = exp_bin(be, e->l, left, right, grp, ext, cnt, sel, 0, 0, push);
    1483         597 :                         if(!r)
    1484             :                                 return NULL;
    1485         597 :                         if (e->card <= CARD_ATOM && r->nrcols > 0) /* single value, get result from bat */
    1486           6 :                                 r = stmt_fetch(be, r);
    1487         597 :                         return stmt_assign(be, exp_relname(e), exp_name(e), r, GET_PSM_LEVEL(e->flag));
    1488        6931 :                 } else if (e->flag & PSM_VAR) {
    1489         176 :                         if (e->f)
    1490          20 :                                 return stmt_vars(be, exp_name(e), e->f, 1, GET_PSM_LEVEL(e->flag));
    1491             :                         else
    1492         156 :                                 return stmt_var(be, exp_relname(e), exp_name(e), &e->tpe, 1, GET_PSM_LEVEL(e->flag));
    1493        6755 :                 } else if (e->flag & PSM_RETURN) {
    1494         659 :                         sql_exp *l = e->l;
    1495         659 :                         stmt *r = exp_bin(be, l, left, right, grp, ext, cnt, sel, 0, 0, push);
    1496             : 
    1497         658 :                         if (!r)
    1498             :                                 return NULL;
    1499             :                         /* handle table returning functions */
    1500         658 :                         if (l->type == e_psm && l->flag & PSM_REL) {
    1501         241 :                                 stmt *lst = r->op1;
    1502         281 :                                 if (r->type == st_table && lst->nrcols == 0 && lst->key && e->card > CARD_ATOM) {
    1503          40 :                                         node *n;
    1504          40 :                                         list *l = sa_list(sql->sa);
    1505          40 :                                         if (l == NULL)
    1506             :                                                 return NULL;
    1507             : 
    1508          86 :                                         for (n=lst->op4.lval->h; n; n = n->next)
    1509          46 :                                                 list_append(l, const_column(be, (stmt*)n->data));
    1510          40 :                                         r = stmt_list(be, l);
    1511         201 :                                 } else if (r->type == st_table && e->card == CARD_ATOM) { /* fetch value */
    1512          35 :                                         r = lst->op4.lval->h->data;
    1513          35 :                                         if (!r->aggr) /* if the cardinality is atom, no fetch call needed */
    1514           4 :                                                 r = stmt_fetch(be, r);
    1515             :                                 }
    1516         241 :                                 if (r->type == st_list)
    1517          40 :                                         r = stmt_table(be, r, 1);
    1518             :                         }
    1519         658 :                         return stmt_return(be, r, GET_PSM_LEVEL(e->flag));
    1520        6096 :                 } else if (e->flag & PSM_WHILE) {
    1521             :                         /* while is a if - block true with leave statement
    1522             :                          * needed because the condition needs to be inside this outer block */
    1523          17 :                         stmt *ifstmt = stmt_cond(be, stmt_bool(be, 1), NULL, 0, 0);
    1524          17 :                         stmt *cond = exp_bin(be, e->l, left, right, grp, ext, cnt, sel, 0, 0, push);
    1525          17 :                         stmt *wstmt;
    1526             : 
    1527          17 :                         if(!cond)
    1528             :                                 return NULL;
    1529          17 :                         wstmt = stmt_cond(be, cond, ifstmt, 1, 0);
    1530             : 
    1531          17 :                         if (!exp_list(be, e->r, left, right, grp, ext, cnt, sel))
    1532             :                                 return NULL;
    1533          17 :                         (void)stmt_control_end(be, wstmt);
    1534          17 :                         return stmt_control_end(be, ifstmt);
    1535        6079 :                 } else if (e->flag & PSM_IF) {
    1536         240 :                         stmt *cond = exp_bin(be, e->l, left, right, grp, ext, cnt, sel, 0, 0, push);
    1537         240 :                         stmt *ifstmt, *res;
    1538             : 
    1539         240 :                         if(!cond)
    1540             :                                 return NULL;
    1541         240 :                         ifstmt = stmt_cond(be, cond, NULL, 0, 0);
    1542         240 :                         if (!exp_list(be, e->r, left, right, grp, ext, cnt, sel))
    1543             :                                 return NULL;
    1544         240 :                         res = stmt_control_end(be, ifstmt);
    1545         240 :                         if (e->f) {
    1546          25 :                                 stmt *elsestmt = stmt_cond(be, cond, NULL, 0, 1);
    1547             : 
    1548          25 :                                 if (!exp_list(be, e->f, left, right, grp, ext, cnt, sel))
    1549             :                                         return NULL;
    1550          25 :                                 res = stmt_control_end(be, elsestmt);
    1551             :                         }
    1552         240 :                         return res;
    1553        5839 :                 } else if (e->flag & PSM_REL) {
    1554        5682 :                         sql_rel *rel = e->l;
    1555        5682 :                         stmt *r = rel_bin(be, rel);
    1556             : 
    1557        5682 :                         if (!r)
    1558             :                                 return NULL;
    1559         861 :                         if (is_modify(rel->op) || is_ddl(rel->op))
    1560             :                                 return r;
    1561         648 :                         return stmt_table(be, r, 1);
    1562         157 :                 } else if (e->flag & PSM_EXCEPTION) {
    1563         157 :                         stmt *cond = exp_bin(be, e->l, left, right, grp, ext, cnt, sel, 0, 0, push);
    1564         157 :                         if (!cond)
    1565             :                                 return NULL;
    1566         157 :                         if (cond->nrcols)
    1567           0 :                                 cond = stmt_fetch(be, cond);
    1568         157 :                         return stmt_exception(be, cond, (const char *) e->r, 0);
    1569             :                 }
    1570             :                 break;
    1571     3310395 :         case e_atom: {
    1572     3310395 :                 if (e->l) {                  /* literals */
    1573     3167936 :                         s = stmt_atom(be, e->l);
    1574      142459 :                 } else if (e->r) {           /* parameters and declared variables */
    1575        2130 :                         sql_var_name *vname = (sql_var_name*) e->r;
    1576        2130 :                         assert(vname->name);
    1577        2558 :                         s = stmt_var(be, vname->sname ? sa_strdup(sql->sa, vname->sname) : NULL, sa_strdup(sql->sa, vname->name), e->tpe.type?&e->tpe:NULL, 0, e->flag);
    1578      140329 :                 } else if (e->f) {           /* values */
    1579      138277 :                         s = value_list(be, e->f, left, sel);
    1580             :                 } else {                        /* arguments */
    1581        2052 :                         sql_subtype *t = e->tpe.type?&e->tpe:NULL;
    1582        2052 :                         if (!t && 0) {
    1583             :                                 sql_arg *a = sql_bind_paramnr(be->mvc, e->flag);
    1584             :                                 t = a->type.type?&a->type:NULL;
    1585             :                         }
    1586        2052 :                         s = stmt_varnr(be, e->flag, t);
    1587             :                 }
    1588             :         }       break;
    1589       49273 :         case e_convert: {
    1590             :                 /* if input is type any NULL or column of nulls, change type */
    1591       49273 :                 list *tps = e->r;
    1592       49273 :                 sql_subtype *from = tps->h->data;
    1593       49273 :                 sql_subtype *to = tps->h->next->data;
    1594       49273 :                 stmt *l;
    1595             : 
    1596       49273 :                 if (from->type->localtype == 0) {
    1597         750 :                         l = exp_bin(be, e->l, left, right, grp, ext, cnt, sel, depth+1, 0, push);
    1598         750 :                         if (l)
    1599         749 :                                 l = stmt_atom(be, atom_general(sql->sa, to, NULL, 0));
    1600             :                 } else {
    1601       48523 :                         l = exp_bin(be, e->l, left, right, grp, ext, cnt, sel, depth+1, 0, push);
    1602             :                 }
    1603       49272 :                 if (!l)
    1604         222 :                         return NULL;
    1605       49051 :                 if (from->type->eclass == EC_SEC && to->type->eclass == EC_SEC) {
    1606             :                         // trivial conversion because EC_SEC is always in milliseconds
    1607             :                         s = l;
    1608       49407 :                 } else if (depth && sel && l->nrcols == 0 && left && left->nrcols && exp_unsafe(e, false, true)) {
    1609         419 :                         stmt *rows = bin_find_smallest_column(be, left);
    1610         419 :                         l = stmt_const(be, rows, l);
    1611         419 :                         s = stmt_convert(be, l, sel, from, to);
    1612       48831 :                 } else if (depth && l->nrcols == 0 && left && left->nrcols && from->type->localtype > to->type->localtype &&
    1613         518 :                                 exp_unsafe(e, false, true)) {
    1614         256 :                         stmt *rows = bin_find_smallest_column(be, left);
    1615         256 :                         l = stmt_const(be, rows, l);
    1616         256 :                         s = stmt_convert(be, l, sel, from, to);
    1617             :                 } else {
    1618       87795 :                         s = stmt_convert(be, l, (!push&&l->nrcols==0)?NULL:sel, from, to);
    1619             :                 }
    1620             :         }       break;
    1621      163904 :         case e_func: {
    1622      163904 :                 node *en;
    1623      163904 :                 list *l = sa_list(sql->sa), *exps = e->l;
    1624      163904 :                 sql_subfunc *f = e->f;
    1625      163904 :                 const char *fname = f->func->base.name;
    1626      163904 :                 stmt *rows = NULL;
    1627      163904 :                 const char *mod, *fimp;
    1628             : 
    1629      163904 :                 if (l == NULL)
    1630             :                         return NULL;
    1631             : 
    1632             :                 /* attempt to instantiate MAL functions now, so we can know if we can push candidate lists */
    1633      163904 :                 if (f->func->lang == FUNC_LANG_MAL && backend_create_mal_func(be->mvc, f) < 0)
    1634             :                         return NULL;
    1635      163904 :                 mod = sql_func_mod(f->func);
    1636      163904 :                 fimp = backend_function_imp(be, f->func);
    1637             : 
    1638      163904 :                 if (f->func->side_effect && left && left->nrcols > 0 && f->func->type != F_LOADER && exps_card(exps) < CARD_MULTI) {
    1639         101 :                         rows = bin_find_smallest_column(be, left);
    1640             :                 }
    1641      163904 :                 assert(!e->r);
    1642      163904 :                 if (strcmp(mod, "") == 0 && strcmp(fimp, "") == 0) {
    1643       21475 :                         if (strcmp(fname, "star") == 0) {
    1644         465 :                                 if (!left)
    1645           1 :                                         return const_column(be, stmt_bool(be, 1));
    1646         464 :                                 return left->op4.lval->h->data;
    1647             :                         }
    1648       21010 :                         if (strcmp(fname, "case") == 0)
    1649       17995 :                                 return exp2bin_case(be, e, left, right, sel, depth);
    1650        3015 :                         if (strcmp(fname, "casewhen") == 0)
    1651        2284 :                                 return exp2bin_casewhen(be, e, left, right, sel, depth);
    1652         731 :                         if (strcmp(fname, "coalesce") == 0)
    1653         527 :                                 return exp2bin_coalesce(be, e, left, right, sel, depth);
    1654         204 :                         if (strcmp(fname, "copyfrombinary") == 0)
    1655          94 :                                 return exp2bin_copyfrombinary(be, e, left, right, sel);
    1656         110 :                         if (strcmp(fname, "file_loader") == 0)
    1657          12 :                                 return exp2bin_file_loader(be, e, left, right, sel);
    1658          98 :                         if (strcmp(fname, "-1") == 0) /* map arguments to A0 .. An */
    1659           3 :                                 return exp2bin_named_placeholders(be, e);
    1660             :                 }
    1661      142524 :                 if (!list_empty(exps)) {
    1662      140706 :                         unsigned nrcols = 0;
    1663      140706 :                         int push_cands = can_push_cands(sel, mod, fimp);
    1664             : 
    1665      412696 :                         for (en = exps->h; en; en = en->next) {
    1666      282530 :                                 sql_exp *e = en->data;
    1667      514521 :                                 stmt *es = exp_bin(be, e, left, right, grp, ext, cnt, (push_cands)?sel:NULL, depth+1, 0, push);
    1668             : 
    1669      282530 :                                 if (!es)
    1670             :                                         return NULL;
    1671             :                                 /*if (rows && en == exps->h && f->func->type != F_LOADER)
    1672             :                                         es = stmt_const(be, rows, es);*/
    1673      271990 :                                 else if (f->func->type == F_ANALYTIC && es->nrcols == 0) {
    1674        7994 :                                         if (en == exps->h && left && left->nrcols)
    1675         463 :                                                 es = stmt_const(be, bin_find_smallest_column(be, left), es); /* ensure the first argument is a column */
    1676        7994 :                                         if (!f->func->s && !strcmp(f->func->base.name, "window_bound")
    1677        3287 :                                                 && exps->h->next && list_length(f->func->ops) == 6 && en == exps->h->next && left->nrcols)
    1678          12 :                                                 es = stmt_const(be, bin_find_smallest_column(be, left), es);
    1679             :                                 }
    1680      271990 :                                 if (es->nrcols > nrcols)
    1681             :                                         nrcols = es->nrcols;
    1682      271990 :                                 list_append(l, es);
    1683             :                         }
    1684             :                 }
    1685      131984 :                 if (!(s = stmt_Nop(be, stmt_list(be, l), sel, f, rows)))
    1686             :                         return NULL;
    1687             :         }       break;
    1688       11181 :         case e_aggr: {
    1689       11181 :                 list *attr = e->l;
    1690       11181 :                 stmt *as = NULL;
    1691       11181 :                 sql_subfunc *a = e->f;
    1692             : 
    1693       11181 :                 assert(sel == NULL);
    1694       17052 :                 if (attr && attr->h) {
    1695        5872 :                         node *en;
    1696        5872 :                         list *l = sa_list(sql->sa);
    1697             : 
    1698       12143 :                         for (en = attr->h; en; en = en->next) {
    1699        6272 :                                 sql_exp *at = en->data;
    1700             : 
    1701        6272 :                                 as = exp_bin(be, at, left, right, NULL, NULL, NULL, sel, depth+1, 0, push);
    1702             : 
    1703        6272 :                                 if (as && as->nrcols <= 0 && left && (!is_const_func(a, attr) || grp))
    1704         223 :                                         as = stmt_const(be, bin_find_smallest_column(be, left), as);
    1705        6272 :                                 if (en == attr->h && !en->next && exp_aggr_is_count(e))
    1706         140 :                                         as = exp_count_no_nil_arg(e, ext, at, as);
    1707             :                                 /* insert single value into a column */
    1708        6272 :                                 if (as && as->nrcols <= 0 && !left)
    1709         160 :                                         as = const_column(be, as);
    1710             : 
    1711         178 :                                 if (!as)
    1712           1 :                                         return NULL;
    1713        6271 :                                 append(l, as);
    1714             :                         }
    1715        5871 :                         if (need_distinct(e) && (grp || list_length(l) > 1)){
    1716          26 :                                 list *nl = sa_list(sql->sa);
    1717          26 :                                 stmt *ngrp = grp;
    1718          26 :                                 stmt *next = ext;
    1719          26 :                                 stmt *ncnt = cnt;
    1720          26 :                                 if (nl == NULL)
    1721             :                                         return NULL;
    1722          54 :                                 for (en = l->h; en; en = en->next) {
    1723          28 :                                         stmt *as = en->data;
    1724          28 :                                         stmt *g = stmt_group(be, as, ngrp, next, ncnt, 1);
    1725          28 :                                         ngrp = stmt_result(be, g, 0);
    1726          28 :                                         next = stmt_result(be, g, 1);
    1727          28 :                                         ncnt = stmt_result(be, g, 2);
    1728             :                                 }
    1729          54 :                                 for (en = l->h; en; en = en->next) {
    1730          28 :                                         stmt *as = en->data;
    1731          28 :                                         append(nl, stmt_project(be, next, as));
    1732             :                                 }
    1733          26 :                                 if (grp)
    1734          25 :                                         grp = stmt_project(be, next, grp);
    1735             :                                 l = nl;
    1736        5845 :                         } else if (need_distinct(e)) {
    1737          76 :                                 stmt *a = l->h->data;
    1738          76 :                                 stmt *u = stmt_unique(be, a);
    1739          76 :                                 if (u == NULL)
    1740             :                                         return NULL;
    1741          76 :                                 l = sa_list(sql->sa);
    1742          76 :                                 if (l == NULL)
    1743             :                                         return NULL;
    1744          76 :                                 append(l, stmt_project(be, u, a));
    1745             :                         }
    1746        5871 :                         as = stmt_list(be, l);
    1747             :                 } else {
    1748             :                         /* count(*) may need the default group (relation) and
    1749             :                            and/or an attribute to count */
    1750        5309 :                         if (grp) {
    1751             :                                 as = grp;
    1752        1505 :                         } else if (left && !list_empty(left->op4.lval)) {
    1753        1504 :                                 as = bin_find_smallest_column(be, left);
    1754        1504 :                                 as = exp_count_no_nil_arg(e, ext, NULL, as);
    1755             :                         } else {
    1756             :                                 /* create dummy single value in a column */
    1757           1 :                                 as = stmt_atom_lng(be, 0);
    1758           1 :                                 as = const_column(be, as);
    1759             :                         }
    1760             :                 }
    1761       11180 :                 s = stmt_aggr(be, as, grp, ext, a, 1, need_no_nil(e) /* ignore nil*/, !zero_if_empty(e));
    1762       11180 :                 if (find_prop(e->p, PROP_COUNT)) /* propagate count == 0 ipv NULL in outer joins */
    1763           0 :                         s->flag |= OUTER_ZERO;
    1764             :         }       break;
    1765     1135655 :         case e_column: {
    1766     1135655 :                 if (right) /* check relation names */
    1767             :                         //s = bin_find_column(be, right, e->l, e->r);
    1768       53417 :                         s = bin_find_column_nid(be, right, e->nid);
    1769     1135655 :                 if (!s && left)
    1770             :                         //s = bin_find_column(be, left, e->l, e->r);
    1771     1088662 :                         s = bin_find_column_nid(be, left, e->nid);
    1772     1135655 :                 if (s && grp)
    1773         289 :                         s = stmt_project(be, ext, s);
    1774     1135655 :                 if (!s && right) {
    1775           0 :                         TRC_CRITICAL(SQL_EXECUTION, "Could not find %s.%s\n", (char*)e->l, (char*)e->r);
    1776           0 :                         print_stmtlist(sql->sa, left);
    1777           0 :                         print_stmtlist(sql->sa, right);
    1778           0 :                         if (!s) {
    1779           0 :                                 TRC_ERROR(SQL_EXECUTION, "Query: '%s'\n", be->client->query);
    1780             :                         }
    1781           0 :                         assert(s);
    1782             :                         return NULL;
    1783             :                 }
    1784             :         }       break;
    1785      172707 :         case e_cmp: {
    1786      172707 :                 stmt *l = NULL, *r = NULL, *r2 = NULL;
    1787      172707 :                 int swapped = 0, is_select = 0, oldvtop, oldstop;
    1788      172707 :                 sql_exp *re = e->r, *re2 = e->f;
    1789             : 
    1790             :                 /* general predicate, select and join */
    1791      172707 :                 if (e->flag == cmp_filter) {
    1792        3740 :                         list *args;
    1793        3740 :                         list *ops;
    1794        3740 :                         node *n;
    1795        3740 :                         int first = 1;
    1796             : 
    1797        3740 :                         ops = sa_list(sql->sa);
    1798        3740 :                         if (ops == NULL)
    1799             :                                 return NULL;
    1800        3740 :                         args = e->l;
    1801        7478 :                         for (n = args->h; n; n = n->next) {
    1802        3740 :                                 oldvtop = be->mb->vtop;
    1803        3740 :                                 oldstop = be->mb->stop;
    1804        3740 :                                 s = NULL;
    1805        3740 :                                 if (!swapped)
    1806        3740 :                                         s = exp_bin(be, n->data, left, NULL, grp, ext, cnt, NULL, depth+1, 0, push);
    1807        3740 :                                 if (!s && right && (first || swapped)) {
    1808           9 :                                         clean_mal_statements(be, oldstop, oldvtop);
    1809           9 :                                         s = exp_bin(be, n->data, right, NULL, grp, ext, cnt, NULL, depth+1, 0, push);
    1810           9 :                                         swapped = 1;
    1811             :                                 }
    1812        3740 :                                 if (!s)
    1813             :                                         return s;
    1814        3738 :                                 if (s->nrcols == 0 && first && left)
    1815         619 :                                         s = stmt_const(be, bin_find_smallest_column(be, swapped?right:left), s);
    1816        3738 :                                 list_append(ops, s);
    1817        3738 :                                 first = 0;
    1818             :                         }
    1819        3738 :                         l = stmt_list(be, ops);
    1820        3738 :                         ops = sa_list(sql->sa);
    1821        3738 :                         if (ops == NULL)
    1822             :                                 return NULL;
    1823        3738 :                         args = e->r;
    1824       14725 :                         for (n = args->h; n; n = n->next) {
    1825       11086 :                                 s = exp_bin(be, n->data, (swapped || !right)?left:right, NULL, grp, ext, cnt, NULL, depth+1, 0, push);
    1826       10987 :                                 if (!s)
    1827             :                                         return s;
    1828       10987 :                                 list_append(ops, s);
    1829             :                         }
    1830        3738 :                         r = stmt_list(be, ops);
    1831             : 
    1832        3738 :                         if (!reduce) {
    1833         433 :                                 sql_subfunc *f = e->f;
    1834         433 :                                 list *ops = sa_list(sql->sa);
    1835         866 :                                 for (node *n = l->op4.lval->h ; n ; n = n->next)
    1836         433 :                                         append(ops, n->data);
    1837        1601 :                                 for (node *n = r->op4.lval->h ; n ; n = n->next)
    1838        1168 :                                         append(ops, n->data);
    1839         433 :                                 if (!(s = stmt_Nop(be, stmt_list(be, ops), sel, f, NULL)))
    1840             :                                         return NULL;
    1841             :                                 return s;
    1842             :                         }
    1843             : 
    1844        3305 :                         if (left && right && (exps_card(e->r) != CARD_ATOM || !exps_are_atoms(e->r))) {
    1845          49 :                                 sql_subfunc *f = e->f;
    1846          49 :                                 bool first = true;
    1847          98 :                                 for (node *n = l->op4.lval->h ; n ; n = n->next) {
    1848          49 :                                         stmt *s = n->data;
    1849          49 :                                         if (s->nrcols == 0) {
    1850           0 :                                                 if (first)
    1851           0 :                                                         n->data = stmt_const(be, bin_find_smallest_column(be, left), n->data);
    1852             :                                                 else
    1853           0 :                                                         n->data = column(be, s);
    1854             :                                         }
    1855          49 :                                         first = false;
    1856             :                                 }
    1857          49 :                                 first = true;
    1858         162 :                                 for (node *n = r->op4.lval->h ; n ; n = n->next) {
    1859         113 :                                         stmt *s = n->data;
    1860         113 :                                         if (s->nrcols == 0) {
    1861          63 :                                                 if (first)
    1862           3 :                                                         n->data = stmt_const(be, bin_find_smallest_column(be, right), s);
    1863             :                                                 else /* last arg maybe const */
    1864          60 :                                                         n->data = column(be, s);
    1865             :                                         }
    1866         113 :                                         first = false;
    1867             :                                 }
    1868          49 :                                 return stmt_genjoin(be, l, r, f, is_anti(e), swapped);
    1869             :                         }
    1870        3256 :                         assert(!swapped);
    1871        3256 :                         s = stmt_genselect(be, l, r, e->f, sel, is_anti(e));
    1872        3256 :                         return s;
    1873             :                 }
    1874      168967 :                 if (e->flag == cmp_in || e->flag == cmp_notin)
    1875       17238 :                         return handle_in_exps(be, e->l, e->r, left, right, grp, ext, cnt, sel, (e->flag == cmp_in), depth, reduce, push);
    1876      151729 :                 if (e->flag == cmp_or && (!right || right->nrcols == 1))
    1877       13147 :                         return exp_bin_or(be, e, left, right, grp, ext, cnt, sel, depth, reduce, push);
    1878      138582 :                 if (e->flag == cmp_or && right) {  /* join */
    1879           0 :                         assert(0);
    1880             :                 }
    1881             : 
    1882             :                 /* mark use of join indices */
    1883      138582 :                 if (right && find_prop(e->p, PROP_JOINIDX) != NULL)
    1884         620 :                         be->join_idx++;
    1885             : 
    1886      138582 :                 oldvtop = be->mb->vtop;
    1887      138582 :                 oldstop = be->mb->stop;
    1888      138582 :                 if (!l) {
    1889      276240 :                         l = exp_bin(be, e->l, left, (!reduce)?right:NULL, grp, ext, cnt, sel, depth+1, 0, push);
    1890      138582 :                         swapped = 0;
    1891             :                 }
    1892      138582 :                 if (!l && right) {
    1893       18250 :                         clean_mal_statements(be, oldstop, oldvtop);
    1894       18250 :                         l = exp_bin(be, e->l, right, NULL, grp, ext, cnt, sel, depth+1, 0, push);
    1895       18250 :                         swapped = 1;
    1896             :                 }
    1897             : 
    1898      138582 :                 oldvtop = be->mb->vtop;
    1899      138582 :                 oldstop = be->mb->stop;
    1900      138582 :                 if (swapped || !right || !reduce)
    1901       93478 :                         r = exp_bin(be, re, left, (!reduce)?right:NULL, grp, ext, cnt, sel, depth+1, 0, push);
    1902             :                 else
    1903       46024 :                         r = exp_bin(be, re, right, NULL, grp, ext, cnt, sel, depth+1, 0, push);
    1904      138582 :                 if (!r && !swapped) {
    1905           2 :                         clean_mal_statements(be, oldstop, oldvtop);
    1906           2 :                         r = exp_bin(be, re, left, NULL, grp, ext, cnt, sel, depth+1, 0, push);
    1907           2 :                         is_select = 1;
    1908             :                 }
    1909      138582 :                 if (!r && swapped) {
    1910           0 :                         clean_mal_statements(be, oldstop, oldvtop);
    1911           0 :                         r = exp_bin(be, re, right, NULL, grp, ext, cnt, sel, depth+1, 0, push);
    1912           0 :                         is_select = 1;
    1913             :                 }
    1914      138582 :                 if (re2 && (swapped || !right || !reduce))
    1915        5288 :                         r2 = exp_bin(be, re2, left, (!reduce)?right:NULL, grp, ext, cnt, sel, depth+1, 0, push);
    1916          52 :                 else if (re2)
    1917          52 :                         r2 = exp_bin(be, re2, right, NULL, grp, ext, cnt, sel, depth+1, 0, push);
    1918             : 
    1919      138582 :                 if (!l || !r || (re2 && !r2))
    1920             :                         return NULL;
    1921             : 
    1922      138576 :                 (void)is_select;
    1923      138576 :                 if (reduce && left && right) {
    1924       64274 :                         if (l->nrcols == 0)
    1925           0 :                                 l = stmt_const(be, bin_find_smallest_column(be, swapped?right:left), l);
    1926       64274 :                         if (r->nrcols == 0)
    1927           4 :                                 r = stmt_const(be, bin_find_smallest_column(be, swapped?left:right), r);
    1928       64274 :                         if (r2 && r2->nrcols == 0)
    1929           8 :                                 r2 = stmt_const(be, bin_find_smallest_column(be, swapped?left:right), r2);
    1930           4 :                         if (r2) {
    1931          54 :                                 s = stmt_join2(be, l, r, r2, (comp_type)e->flag, is_anti(e), is_symmetric(e), swapped);
    1932       64220 :                         } else if (swapped) {
    1933       18248 :                                 s = stmt_join(be, r, l, is_anti(e), swap_compare((comp_type)e->flag), 0, is_semantics(e), false);
    1934             :                         } else {
    1935       45972 :                                 s = stmt_join(be, l, r, is_anti(e), (comp_type)e->flag, 0, is_semantics(e), false);
    1936             :                         }
    1937             :                 } else {
    1938       74302 :                         if (r2) { /* handle all cases in stmt_uselect, reducing, non reducing, scalar etc */
    1939        5053 :                                 if (l->nrcols == 0 && ((sel && sel->nrcols > 0) || r->nrcols > 0 || r2->nrcols > 0 || reduce))
    1940          56 :                                         l = left ? stmt_const(be, bin_find_smallest_column(be, left), l) : column(be, l);
    1941        5053 :                                 s = stmt_uselect2(be, l, r, r2, (comp_type)e->flag, sel, is_anti(e), is_symmetric(e), reduce);
    1942             :                         } else {
    1943             :                                 /* value compare or select */
    1944       69249 :                                 if (!reduce || (l->nrcols == 0 && r->nrcols == 0)) {
    1945         903 :                                         sql_subfunc *f = sql_bind_func(sql, "sys", compare_func((comp_type)e->flag, is_anti(e)),
    1946             :                                                                                                    tail_type(l), tail_type(l), F_FUNC, true, true);
    1947         903 :                                         assert(f);
    1948         903 :                                         if (is_semantics(e)) {
    1949         136 :                                                 if (exp_is_null(e->l) && exp_is_null(e->r) && (e->flag == cmp_equal || e->flag == cmp_notequal)) {
    1950           4 :                                                         s = stmt_bool(be, e->flag == cmp_equal ? !is_anti(e): is_anti(e));
    1951             :                                                 } else {
    1952         132 :                                                         list *args = sa_list(sql->sa);
    1953         132 :                                                         if (args == NULL)
    1954             :                                                                 return NULL;
    1955             :                                                         /* add nil semantics bit */
    1956         132 :                                                         list_append(args, l);
    1957         132 :                                                         list_append(args, r);
    1958         132 :                                                         list_append(args, stmt_bool(be, 1));
    1959         132 :                                                         s = stmt_Nop(be, stmt_list(be, args), sel, f, NULL);
    1960             :                                                 }
    1961             :                                         } else {
    1962         767 :                                                 s = stmt_binop(be, l, r, sel, f);
    1963             :                                         }
    1964         903 :                                         if (l->cand)
    1965           0 :                                                 s->cand = l->cand;
    1966         903 :                                         if (r->cand)
    1967           0 :                                                 s->cand = r->cand;
    1968             :                                 } else {
    1969             :                                         /* this can still be a join (as relational algebra and single value subquery results still means joins */
    1970       68346 :                                         s = stmt_uselect(be, l, r, (comp_type)e->flag, sel, is_anti(e), is_semantics(e));
    1971             :                                 }
    1972             :                         }
    1973             :                 }
    1974             :          }      break;
    1975             :         default:
    1976             :                 ;
    1977             :         }
    1978             :         return s;
    1979             : }
    1980             : 
    1981             : static stmt *
    1982      507011 : stmt_col(backend *be, sql_column *c, stmt *del, int part)
    1983             : {
    1984      507011 :         stmt *sc = stmt_bat(be, c, RDONLY, part);
    1985             : 
    1986      507019 :         if (isTable(c->t) && c->t->access != TABLE_READONLY &&
    1987      493810 :            (!isNew(c) || !isNew(c->t) /* alter */) &&
    1988      479331 :            (c->t->persistence == SQL_PERSIST || c->t->s) /*&& !c->t->commit_action*/) {
    1989      479331 :                 stmt *u = stmt_bat(be, c, RD_UPD_ID, part);
    1990      479332 :                 assert(u);
    1991      479332 :                 sc = stmt_project_delta(be, sc, u);
    1992      479506 :                 if (c->storage_type && c->storage_type[0] == 'D') {
    1993         176 :                         stmt *v = stmt_bat(be, c, RD_EXT, part);
    1994         176 :                         sc = stmt_dict(be, sc, v);
    1995      479154 :                 } else if (c->storage_type && c->storage_type[0] == 'F') {
    1996           8 :                         sc = stmt_for(be, sc, stmt_atom(be, atom_general(be->mvc->sa, &c->type, c->storage_type+4/*skip FOR-*/, be->mvc->timezone)));
    1997             :                 }
    1998      479330 :                 if (del)
    1999       25846 :                         sc = stmt_project(be, del, sc);
    2000       27688 :         } else if (del) { /* always handle the deletes */
    2001        6913 :                 sc = stmt_project(be, del, sc);
    2002             :         }
    2003      507017 :         return sc;
    2004             : }
    2005             : 
    2006             : static stmt *
    2007        3596 : stmt_idx(backend *be, sql_idx *i, stmt *del, int part)
    2008             : {
    2009        3596 :         stmt *sc = stmt_idxbat(be, i, RDONLY, part);
    2010             : 
    2011        3596 :         if (isTable(i->t) && i->t->access != TABLE_READONLY &&
    2012        3596 :            (!isNew(i) || !isNew(i->t) /* alter */) &&
    2013        2871 :            (i->t->persistence == SQL_PERSIST || i->t->s) /*&& !i->t->commit_action*/) {
    2014        2871 :                 stmt *u = stmt_idxbat(be, i, RD_UPD_ID, part);
    2015        2871 :                 sc = stmt_project_delta(be, sc, u);
    2016        2871 :                 if (del)
    2017         320 :                         sc = stmt_project(be, del, sc);
    2018         725 :         } else if (del) { /* always handle the deletes */
    2019         613 :                 sc = stmt_project(be, del, sc);
    2020             :         }
    2021        3596 :         return sc;
    2022             : }
    2023             : 
    2024             : static int
    2025           0 : stmt_set_type_param(mvc *sql, sql_subtype *type, stmt *param)
    2026             : {
    2027           0 :         if (!type || !param || param->type != st_var)
    2028             :                 return -1;
    2029             : 
    2030           0 :         if (set_type_param(sql, type, param->flag) == 0) {
    2031           0 :                 param->op4.typeval = *type;
    2032           0 :                 return 0;
    2033             :         }
    2034             :         return -1;
    2035             : }
    2036             : 
    2037             : /* check_types tries to match the t type with the type of s if they don't
    2038             :  * match s is converted. Returns NULL on failure.
    2039             :  */
    2040             : static stmt *
    2041        5091 : check_types(backend *be, sql_subtype *t, stmt *s, check_type tpe)
    2042             : {
    2043        5091 :         mvc *sql = be->mvc;
    2044        5091 :         int c, err = 0;
    2045        5091 :         sql_subtype *fromtype = tail_type(s);
    2046             : 
    2047        5091 :         if ((!fromtype || !fromtype->type) && stmt_set_type_param(sql, t, s) == 0)
    2048             :                 return s;
    2049           0 :         if (!fromtype)
    2050           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "statement has no type information");
    2051             : 
    2052        5091 :         if (fromtype && subtype_cmp(t, fromtype) != 0) {
    2053           1 :                 if (EC_INTERVAL(fromtype->type->eclass) && (t->type->eclass == EC_NUM || t->type->eclass == EC_POS) && t->digits < fromtype->digits) {
    2054             :                         err = 1; /* conversion from interval to num depends on the number of digits */
    2055             :                 } else {
    2056           1 :                         c = sql_type_convert(fromtype->type->eclass, t->type->eclass);
    2057           1 :                         if (!c || (c == 2 && tpe == type_set) || (c == 3 && tpe != type_cast)) {
    2058             :                                 err = 1;
    2059             :                         } else {
    2060           1 :                                 s = stmt_convert(be, s, NULL, fromtype, t);
    2061             :                         }
    2062             :                 }
    2063             :         }
    2064           1 :         if (err) {
    2065           0 :                 stmt *res = sql_error(sql, 10, SQLSTATE(42000) "types %s(%u,%u) (%s) and %s(%u,%u) (%s) are not equal",
    2066             :                         fromtype->type->base.name,
    2067             :                         fromtype->digits,
    2068             :                         fromtype->scale,
    2069           0 :                         fromtype->type->impl,
    2070             :                         t->type->base.name,
    2071             :                         t->digits,
    2072             :                         t->scale,
    2073           0 :                         t->type->impl
    2074             :                 );
    2075           0 :                 return res;
    2076             :         }
    2077             :         return s;
    2078             : }
    2079             : 
    2080             : static stmt *
    2081        6008 : sql_Nop_(backend *be, const char *fname, stmt *a1, stmt *a2, stmt *a3, stmt *a4)
    2082             : {
    2083        6008 :         mvc *sql = be->mvc;
    2084        6008 :         list *sl = sa_list(sql->sa);
    2085        6008 :         list *tl = sa_list(sql->sa);
    2086        6008 :         sql_subfunc *f = NULL;
    2087             : 
    2088        6008 :         if (sl == NULL || tl == NULL)
    2089             :                 return NULL;
    2090        6008 :         list_append(sl, a1);
    2091        6008 :         list_append(tl, tail_type(a1));
    2092        6008 :         list_append(sl, a2);
    2093        6008 :         list_append(tl, tail_type(a2));
    2094        6008 :         list_append(sl, a3);
    2095        6008 :         list_append(tl, tail_type(a3));
    2096        6008 :         if (a4) {
    2097           0 :                 list_append(sl, a4);
    2098           0 :                 list_append(tl, tail_type(a4));
    2099             :         }
    2100             : 
    2101        6008 :         if ((f = sql_bind_func_(sql, "sys", fname, tl, F_FUNC, true, true)))
    2102        6008 :                 return stmt_Nop(be, stmt_list(be, sl), NULL, f, NULL);
    2103           0 :         return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "SELECT: no such operator '%s'", fname);
    2104             : }
    2105             : 
    2106             : static stmt *
    2107          10 : parse_value(backend *be, sql_schema *s, char *query, sql_subtype *tpe, char emode)
    2108             : {
    2109          10 :         sql_exp *e = NULL;
    2110             : 
    2111          10 :         if (!(e = rel_parse_val(be->mvc, s, query, tpe, emode, NULL)))
    2112             :                 return NULL;
    2113          10 :         return exp_bin(be, e, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    2114             : }
    2115             : 
    2116             : static stmt *
    2117        1209 : rel2bin_sql_table(backend *be, sql_table *t, list *aliases)
    2118             : {
    2119        1209 :         mvc *sql = be->mvc;
    2120        1209 :         list *l = sa_list(sql->sa);
    2121        1209 :         node *n;
    2122        1209 :         stmt *dels = stmt_tid(be, t, 0);
    2123             : 
    2124        1209 :         if (l == NULL || dels == NULL)
    2125             :                 return NULL;
    2126        1209 :         if (aliases) {
    2127       20474 :                 for (n = aliases->h; n; n = n->next) {
    2128       19265 :                         sql_exp *e = n->data;
    2129       19265 :                         if (e->type != e_column)
    2130         105 :                                 continue;
    2131       19160 :                         assert(e->type == e_column);
    2132       19160 :                         char *name = e->r;
    2133       19160 :                         if (name[0] == '%') {
    2134        1203 :                                 if (strcmp(name, TID)==0) {
    2135             :                                         /* tid function  sql.tid(t) */
    2136        1203 :                                         const char *rnme = t->base.name;
    2137             : 
    2138        1203 :                                         stmt *sc = dels?dels:stmt_tid(be, t, 0);
    2139        1203 :                                         sc = stmt_alias(be, sc, e->alias.label, rnme, TID);
    2140        1203 :                                         list_append(l, sc);
    2141             :                                 } else {
    2142           0 :                                         node *m = ol_find_name(t->idxs, name+1);
    2143           0 :                                         if (!m)
    2144           0 :                                                 assert(0);
    2145           0 :                                         sql_idx *i = m->data;
    2146           0 :                                         stmt *sc = stmt_idx(be, i, dels, dels->partition);
    2147           0 :                                         const char *rnme = t->base.name;
    2148             : 
    2149             :                                         /* index names are prefixed, to make them independent */
    2150           0 :                                         sc = stmt_alias(be, sc, e->alias.label, rnme, sa_strconcat(sql->sa, "%", i->base.name));
    2151           0 :                                         list_append(l, sc);
    2152             :                                 }
    2153             :                         } else {
    2154       17957 :                                 node *m = ol_find_name(t->columns, name);
    2155       17957 :                                 if (!m)
    2156           0 :                                         assert(0);
    2157       17957 :                                 sql_column *c = m->data;
    2158       17957 :                                 stmt *sc = stmt_col(be, c, dels, dels->partition);
    2159       17957 :                                 sc = stmt_alias(be, sc, e->alias.label, exp_relname(e), exp_name(e));
    2160       17957 :                                 list_append(l, sc);
    2161             :                         }
    2162             :                 }
    2163             :         } else {
    2164           0 :                 assert(0);
    2165             :                 sql_exp *e = NULL;
    2166             :                 for (n = ol_first_node(t->columns); n; n = n->next) {
    2167             :                         sql_column *c = n->data;
    2168             :                         stmt *sc = stmt_col(be, c, dels, dels->partition);
    2169             : 
    2170             :                         list_append(l, sc);
    2171             :                 }
    2172             :                 /* TID column */
    2173             :                 if (ol_first_node(t->columns)) {
    2174             :                         /* tid function  sql.tid(t) */
    2175             :                         const char *rnme = t->base.name;
    2176             : 
    2177             :                         stmt *sc = dels?dels:stmt_tid(be, t, 0);
    2178             :                         sc = stmt_alias(be, sc, e->alias.label, rnme, TID);
    2179             :                         list_append(l, sc);
    2180             :                 }
    2181             :                 if (t->idxs) {
    2182             :                         for (n = ol_first_node(t->idxs); n; n = n->next) {
    2183             :                                 sql_idx *i = n->data;
    2184             :                                 stmt *sc = stmt_idx(be, i, dels, dels->partition);
    2185             :                                 const char *rnme = t->base.name;
    2186             : 
    2187             :                                 /* index names are prefixed, to make them independent */
    2188             :                                 sc = stmt_alias(be, sc, e->alias.label, rnme, sa_strconcat(sql->sa, "%", i->base.name));
    2189             :                                 list_append(l, sc);
    2190             :                         }
    2191             :                 }
    2192             :         }
    2193        1209 :         return stmt_list(be, l);
    2194             : }
    2195             : 
    2196             : static stmt *
    2197      146874 : rel2bin_basetable(backend *be, sql_rel *rel)
    2198             : {
    2199      146874 :         mvc *sql = be->mvc;
    2200      146874 :         sql_table *t = rel->l;
    2201      146874 :         sql_column *fcol = NULL;
    2202      146874 :         sql_idx *fi = NULL;
    2203      146874 :         list *l = sa_list(sql->sa);
    2204      146874 :         stmt *dels = stmt_tid(be, t, rel->flag == REL_PARTITION), *col = NULL;
    2205      146874 :         node *en;
    2206             : 
    2207      146874 :         if (l == NULL || dels == NULL)
    2208             :                 return NULL;
    2209             :         /* add aliases */
    2210      146874 :         assert(rel->exps);
    2211      293761 :         for (en = rel->exps->h; en && !col; en = en->next) {
    2212      146887 :                 sql_exp *exp = en->data;
    2213      146887 :                 const char *oname = exp->r;
    2214             : 
    2215      146887 :                 if (is_func(exp->type) || (oname[0] == '%' && strcmp(oname, TID) == 0))
    2216         923 :                         continue;
    2217      145964 :                 if (oname[0] == '%') {
    2218          61 :                         sql_idx *i = find_sql_idx(t, oname+1);
    2219             : 
    2220             :                         /* do not include empty indices in the plan */
    2221          61 :                         if ((hash_index(i->type) && list_length(i->columns) <= 1) || !idx_has_column(i->type))
    2222           0 :                                 continue;
    2223          61 :                         fi = i;
    2224          61 :                         col = stmt_idx(be, i, NULL/*dels*/, dels->partition);
    2225             :                 } else {
    2226      145903 :                         sql_column *c = find_sql_column(t, oname);
    2227             : 
    2228      145903 :                         fcol = c;
    2229      145903 :                         col = stmt_col(be, c, NULL/*dels*/, dels->partition);
    2230             :                 }
    2231             :         }
    2232      638604 :         for (en = rel->exps->h; en; en = en->next) {
    2233      491731 :                 sql_exp *exp = en->data;
    2234      491731 :                 const char *rname = exp_relname(exp)?exp_relname(exp):exp->l;
    2235      491731 :                 const char *oname = exp->r;
    2236      491731 :                 stmt *s = NULL;
    2237             : 
    2238      491731 :                 assert(!is_func(exp->type));
    2239      491731 :                 if (oname[0] == '%' && strcmp(oname, TID) == 0) {
    2240             :                         /* tid function  sql.tid(t) */
    2241             :                         //const char *rnme = t->base.name;
    2242             : 
    2243       14814 :                         if (col)
    2244       13904 :                                 s = stmt_mirror(be, col);
    2245             :                         else {
    2246         910 :                                 s = dels?dels:stmt_tid(be, t, 0);
    2247             :                                 dels = NULL;
    2248             :                         }
    2249             :                         //s = stmt_alias(be, s, exp->alias.label, rnme, TID);
    2250      476917 :                 } else if (oname[0] == '%') {
    2251        2663 :                         sql_idx *i = find_sql_idx(t, oname+1);
    2252             : 
    2253             :                         /* do not include empty indices in the plan */
    2254        2663 :                         if ((hash_index(i->type) && list_length(i->columns) <= 1) || !idx_has_column(i->type))
    2255           0 :                                 continue;
    2256        2663 :                         s = (i == fi) ? col : stmt_idx(be, i, NULL/*dels*/, dels->partition);
    2257             :                         //s = stmt_alias(be, s, exp->alias.label, rname, exp_name(exp));
    2258             :                 } else {
    2259      474254 :                         sql_column *c = find_sql_column(t, oname);
    2260             : 
    2261      474254 :                         s = (c == fcol) ? col : stmt_col(be, c, NULL/*dels*/, dels->partition);
    2262             :                         //s = stmt_alias(be, s, exp->alias.label, rname, exp_name(exp));
    2263             :                 }
    2264      491736 :                 s = stmt_alias(be, s, exp->alias.label, rname, exp_name(exp));
    2265             :                 //s->tname = rname;
    2266             :                 //s->cname = exp_name(exp);
    2267             :                 //s->flag = exp->alias.label;
    2268      491734 :                 list_append(l, s);
    2269             :         }
    2270      146873 :         stmt *res = stmt_list(be, l);
    2271      146873 :         if (res && dels)
    2272      145963 :                 res->cand = dels;
    2273             :         return res;
    2274             : }
    2275             : 
    2276             : static int
    2277          21 : alias_cmp(stmt *s, const char *nme)
    2278             : {
    2279          21 :         return strcmp(s->cname, nme);
    2280             : }
    2281             : 
    2282             : static list* exps2bin_args(backend *be, list *exps, list *args);
    2283             : 
    2284             : static list *
    2285        1947 : exp2bin_args(backend *be, sql_exp *e, list *args)
    2286             : {
    2287        2018 :         mvc *sql = be->mvc;
    2288             : 
    2289        2018 :         if (mvc_highwater(sql))
    2290           0 :                 return sql_error(sql, 10, SQLSTATE(42000) "Query too complex: running out of stack space");
    2291             : 
    2292        2018 :         if (!e || !args)
    2293             :                 return args;
    2294        2018 :         switch(e->type){
    2295             :         case e_column:
    2296             :         case e_psm:
    2297             :                 return args;
    2298          79 :         case e_cmp:
    2299          79 :                 if (e->flag == cmp_or || e->flag == cmp_filter) {
    2300           7 :                         args = exps2bin_args(be, e->l, args);
    2301           7 :                         args = exps2bin_args(be, e->r, args);
    2302          72 :                 } else if (e->flag == cmp_in || e->flag == cmp_notin) {
    2303           2 :                         args = exp2bin_args(be, e->l, args);
    2304           2 :                         args = exps2bin_args(be, e->r, args);
    2305             :                 } else {
    2306          70 :                         args = exp2bin_args(be, e->l, args);
    2307          70 :                         args = exp2bin_args(be, e->r, args);
    2308          70 :                         if (e->f)
    2309             :                                 args = exp2bin_args(be, e->f, args);
    2310             :                 }
    2311             :                 return args;
    2312          54 :         case e_convert:
    2313          54 :                 if (e->l)
    2314             :                         return exp2bin_args(be, e->l, args);
    2315             :                 break;
    2316         150 :         case e_aggr:
    2317             :         case e_func:
    2318         150 :                 if (e->l)
    2319         117 :                         return exps2bin_args(be, e->l, args);
    2320             :                 break;
    2321         337 :         case e_atom:
    2322         337 :                 if (e->l) {
    2323             :                         return args;
    2324          22 :                 } else if (e->f) {
    2325           0 :                         return exps2bin_args(be, e->f, args);
    2326          22 :                 } else if (e->r) {
    2327          22 :                         sql_var_name *vname = (sql_var_name*) e->r;
    2328          22 :                         const char *nme = sql_escape_ident(sql->sa, vname->name);
    2329          22 :                         char *buf = NULL;
    2330             : 
    2331          22 :                         if (vname->sname) { /* Global variable */
    2332           3 :                                 const char *sname = sql_escape_ident(sql->sa, vname->sname);
    2333           6 :                                 if (!nme || !sname || !(buf = SA_NEW_ARRAY(be->mvc->sa, char, strlen(sname) + strlen(nme) + 6)))
    2334           0 :                                         return NULL;
    2335           3 :                                 stpcpy(stpcpy(stpcpy(stpcpy(stpcpy(buf, "0\""), sname), "\"\""), nme), "\""); /* escape variable name */
    2336             :                         } else { /* Parameter or local variable */
    2337          19 :                                 char levelstr[16];
    2338          19 :                                 snprintf(levelstr, sizeof(levelstr), "%u", e->flag);
    2339          38 :                                 if (!nme || !(buf = SA_NEW_ARRAY(be->mvc->sa, char, strlen(levelstr) + strlen(nme) + 3)))
    2340           0 :                                         return NULL;
    2341          19 :                                 stpcpy(stpcpy(stpcpy(stpcpy(buf, levelstr), "\""), nme), "\""); /* escape variable name */
    2342             :                         }
    2343          22 :                         if (!list_find(args, buf, (fcmp)&alias_cmp)) {
    2344          22 :                                 stmt *s = stmt_var(be, vname->sname, vname->name, &e->tpe, 0, e->flag);
    2345             : 
    2346          22 :                                 if (!e->alias.label)
    2347          18 :                                         exp_label(be->mvc->sa, e, ++be->mvc->label);
    2348          22 :                                 s = stmt_alias(be, s, e->alias.label, NULL, sa_strdup(sql->sa, buf));
    2349          22 :                                 list_append(args, s);
    2350             :                         }
    2351             :                 }
    2352             :         }
    2353             :         return args;
    2354             : }
    2355             : 
    2356             : static list *
    2357         417 : exps2bin_args(backend *be, list *exps, list *args)
    2358             : {
    2359         417 :         node *n;
    2360             : 
    2361         417 :         if (!exps)
    2362             :                 return args;
    2363        2222 :         for (n = exps->h; n; n = n->next)
    2364        1805 :                 args = exp2bin_args(be, n->data, args);
    2365             :         return args;
    2366             : }
    2367             : 
    2368             : static list *
    2369         197 : rel2bin_args(backend *be, sql_rel *rel, list *args)
    2370             : {
    2371         479 :         if (mvc_highwater(be->mvc))
    2372           0 :                 return sql_error(be->mvc, 10, SQLSTATE(42000) "Query too complex: running out of stack space");
    2373             : 
    2374         479 :         if (!rel || !args)
    2375             :                 return args;
    2376         479 :         switch(rel->op) {
    2377             :         case op_basetable:
    2378             :         case op_table:
    2379             :                 break;
    2380          11 :         case op_join:
    2381             :         case op_left:
    2382             :         case op_right:
    2383             :         case op_full:
    2384             : 
    2385             :         case op_semi:
    2386             :         case op_anti:
    2387             : 
    2388             :         case op_union:
    2389             :         case op_inter:
    2390             :         case op_except:
    2391             :         case op_merge:
    2392          11 :                 args = rel2bin_args(be, rel->l, args);
    2393          11 :                 args = rel2bin_args(be, rel->r, args);
    2394          11 :                 break;
    2395           0 :         case op_munion:
    2396           0 :                 if (rel->l) {
    2397           0 :                         for (node* n = ((list*)rel->l)->h; n; n = n->next) {
    2398           0 :                                 args = rel2bin_args(be, n->data, args);
    2399             :                         }
    2400             :                 }
    2401             :                 break;
    2402          49 :         case op_groupby:
    2403          49 :                 if (rel->r)
    2404          13 :                         args = exps2bin_args(be, rel->r, args);
    2405             :                 /* fall through */
    2406             :         case op_project:
    2407             :         case op_select:
    2408             :         case op_topn:
    2409             :         case op_sample:
    2410         271 :                 if (rel->exps)
    2411         271 :                         args = exps2bin_args(be, rel->exps, args);
    2412         271 :                 args = rel2bin_args(be, rel->l, args);
    2413         271 :                 break;
    2414           0 :         case op_ddl:
    2415           0 :                 args = rel2bin_args(be, rel->l, args);
    2416           0 :                 if (rel->r)
    2417             :                         args = rel2bin_args(be, rel->r, args);
    2418             :                 break;
    2419           0 :         case op_insert:
    2420             :         case op_update:
    2421             :         case op_delete:
    2422             :         case op_truncate:
    2423           0 :                 args = rel2bin_args(be, rel->r, args);
    2424           0 :                 break;
    2425             :         }
    2426             :         return args;
    2427             : }
    2428             : 
    2429             : typedef struct trigger_input {
    2430             :         sql_table *t;
    2431             :         stmt *tids;
    2432             :         stmt **updates;
    2433             :         int type; /* insert 1, update 2, delete 3, truncate 4 */
    2434             :         const char *on;
    2435             :         const char *nn;
    2436             : } trigger_input;
    2437             : 
    2438             : static stmt *
    2439        3014 : rel2bin_table(backend *be, sql_rel *rel, list *refs)
    2440             : {
    2441        3014 :         mvc *sql = be->mvc;
    2442        3014 :         list *l;
    2443        3014 :         stmt *sub = NULL, *osub = NULL;
    2444        3014 :         node *en, *n;
    2445        3014 :         sql_exp *op = rel->r;
    2446             : 
    2447        3014 :         if (rel->flag == TRIGGER_WRAPPER) {
    2448          11 :                 trigger_input *ti = rel->l;
    2449          11 :                 l = sa_list(sql->sa);
    2450          11 :                 if (l == NULL)
    2451             :                         return NULL;
    2452             : 
    2453          20 :                 assert(list_length(rel->exps) == ((ti->type == 2)?2:1) * ol_length(ti->t->columns));
    2454          30 :                 for (n = ol_first_node(ti->t->columns), en = rel->exps->h; n && en; n = n->next, en = en->next) {
    2455          19 :                         sql_column *c = n->data;
    2456          19 :                         sql_exp *e = en->data;
    2457             : 
    2458          19 :                         if (ti->type == 2) { /* updates */
    2459           4 :                                 stmt *s = stmt_col(be, c, ti->tids, ti->tids->partition);
    2460           4 :                                 append(l, stmt_alias(be, s, e->alias.label, ti->on, c->base.name));
    2461           4 :                                 en = en->next;
    2462           4 :                                 e = en->data;
    2463             :                         }
    2464          19 :                         if (ti->updates && ti->updates[c->colnr]) {
    2465          17 :                                 append(l, stmt_alias(be, ti->updates[c->colnr], e->alias.label, ti->nn, c->base.name));
    2466             :                         } else {
    2467           2 :                                 stmt *s = stmt_col(be, c, ti->tids, ti->tids->partition);
    2468           2 :                                 append(l, stmt_alias(be, s, e->alias.label, ti->nn, c->base.name));
    2469           2 :                                 assert(ti->type != 1);
    2470             :                         }
    2471             :                 }
    2472          11 :                 sub = stmt_list(be, l);
    2473          11 :                 return sub;
    2474        3003 :         } else if (op) {
    2475        2817 :                 int i;
    2476        2817 :                 sql_subfunc *f = op->f;
    2477        2817 :                 stmt *psub = NULL;
    2478        2817 :                 list *ops = NULL;
    2479        2817 :                 stmt *ids = NULL;
    2480             : 
    2481        2817 :                 if (rel->l) { /* first construct the sub relation */
    2482          72 :                         sql_rel *l = rel->l;
    2483          72 :                         if (l->op == op_ddl) {
    2484           0 :                                 sql_table *t = rel_ddl_table_get(l);
    2485             : 
    2486           0 :                                 if (t)
    2487           0 :                                         sub = rel2bin_sql_table(be, t, NULL);
    2488             :                         } else {
    2489          72 :                                 sub = subrel_bin(be, rel->l, refs);
    2490             :                         }
    2491          72 :                         sub = subrel_project(be, sub, refs, rel->l);
    2492          72 :                         if (!sub)
    2493             :                                 return NULL;
    2494             :                 }
    2495             : 
    2496        2817 :                 assert(f);
    2497        2817 :                 if (f->func->res && list_length(f->func->res) + 1 == list_length(rel->exps) && !f->func->varres) {
    2498             :                         /* add inputs in correct order ie loop through args of f and pass column */
    2499          23 :                         list *exps = op->l;
    2500          23 :                         ops = sa_list(be->mvc->sa);
    2501          23 :                         if (exps) {
    2502          83 :                                 for (node *en = exps->h; en; en = en->next) {
    2503          60 :                                         sql_exp *e = en->data;
    2504             : 
    2505             :                                         /* find column */
    2506          60 :                                         stmt *s = exp_bin(be, e, sub, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    2507          60 :                                         if (!s)
    2508             :                                                 return NULL;
    2509          60 :                                         if (en->next)
    2510          37 :                                                 append(ops, s);
    2511             :                                         else /* last added exp is the ids (todo use name base lookup !!) */
    2512             :                                                 ids = s;
    2513             :                                 }
    2514             :                         }
    2515             :                 } else {
    2516        2794 :                         psub = exp_bin(be, op, sub, NULL, NULL, NULL, NULL, NULL, 0, 0, 0); /* table function */
    2517        2794 :                         if (!psub)
    2518             :                                 return NULL;
    2519             :                 }
    2520        2817 :                 l = sa_list(sql->sa);
    2521        2817 :                 if (l == NULL)
    2522             :                         return NULL;
    2523        2817 :                 if (f->func->res) {
    2524        2792 :                         if (f->func->varres) {
    2525       11772 :                                 for (i=0, en = rel->exps->h, n = f->res->h; en; en = en->next, n = n->next, i++) {
    2526       10556 :                                         sql_exp *exp = en->data;
    2527       10556 :                                         sql_subtype *st = n->data;
    2528       10556 :                                         const char *rnme = exp_relname(exp)?exp_relname(exp):exp->l;
    2529       10556 :                                         stmt *s = stmt_rs_column(be, psub, i, st);
    2530             : 
    2531       10556 :                                         s = stmt_alias(be, s, exp->alias.label, rnme, exp_name(exp));
    2532       10556 :                                         list_append(l, s);
    2533             :                                 }
    2534             :                         } else {
    2535        1576 :                                 node *m = rel->exps->h;
    2536        1576 :                                 int i = 0;
    2537             : 
    2538             :                                 /* correlated table returning function */
    2539        1576 :                                 if (list_length(f->func->res) + 1 == list_length(rel->exps)) {
    2540             :                                         /* use a simple nested loop solution for this case, ie
    2541             :                                          * output a table of (input) row-ids, the output of the table producing function
    2542             :                                          */
    2543             :                                         /* make sure the input for sql.unionfunc are bats */
    2544          23 :                                         if (ids)
    2545          23 :                                                 ids = column(be, ids);
    2546          23 :                                         if (ops)
    2547          60 :                                                 for (node *en = ops->h; en; en = en->next)
    2548          37 :                                                         en->data = column(be, (stmt *) en->data);
    2549             : 
    2550          23 :                                         int narg = 3 + list_length(rel->exps);
    2551          23 :                                         if (ops)
    2552          23 :                                                 narg += list_length(ops);
    2553          23 :                                         InstrPtr q = newStmtArgs(be->mb, sqlRef, "unionfunc", narg);
    2554          23 :                                         if (q == NULL) {
    2555           0 :                                                 if (be->mvc->sa->eb.enabled)
    2556           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);
    2557           0 :                                                 return sql_error(sql, 10, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    2558             :                                         }
    2559             :                                         /* Generate output rowid column and output of function f */
    2560          69 :                                         for (i=0; m; m = m->next, i++) {
    2561          46 :                                                 sql_exp *e = m->data;
    2562          46 :                                                 int type = exp_subtype(e)->type->localtype;
    2563             : 
    2564          46 :                                                 type = newBatType(type);
    2565          46 :                                                 if (i)
    2566          23 :                                                         q = pushReturn(be->mb, q, newTmpVariable(be->mb, type));
    2567             :                                                 else
    2568          23 :                                                         getArg(q, 0) = newTmpVariable(be->mb, type);
    2569             :                                         }
    2570          23 :                                         if (backend_create_subfunc(be, f, ops) < 0) {
    2571           0 :                                                 freeInstruction(q);
    2572           0 :                                                 return NULL;
    2573             :                                         }
    2574          23 :                                         str mod = sql_func_mod(f->func);
    2575          23 :                                         str fcn = backend_function_imp(be, f->func);
    2576          23 :                                         q = pushStr(be->mb, q, mod);
    2577          23 :                                         q = pushStr(be->mb, q, fcn);
    2578          23 :                                         psub = stmt_direct_func(be, q);
    2579          23 :                                         if (psub == NULL) {
    2580           0 :                                                 freeInstruction(q);
    2581           0 :                                                 return NULL;
    2582             :                                         }
    2583             : 
    2584          23 :                                         if (ids) /* push input rowids column */
    2585          23 :                                                 q = pushArgument(be->mb, q, ids->nr);
    2586             : 
    2587             :                                         /* add inputs in correct order ie loop through args of f and pass column */
    2588          23 :                                         if (ops) {
    2589          60 :                                                 for (node *en = ops->h; en; en = en->next) {
    2590          37 :                                                         stmt *op = en->data;
    2591             : 
    2592          37 :                                                         q = pushArgument(be->mb, q, op->nr);
    2593             :                                                 }
    2594             :                                         }
    2595          23 :                                         pushInstruction(be->mb, q);
    2596             : 
    2597             :                                         /* name output of dependent columns, output of function is handled the same as without correlation */
    2598          23 :                                         int len = list_length(rel->exps)-list_length(f->func->res);
    2599          23 :                                         assert(len== 1);
    2600          46 :                                         for (i=0, m=rel->exps->h; m && i<len; m = m->next, i++) {
    2601          23 :                                                 sql_exp *exp = m->data;
    2602          23 :                                                 stmt *s = stmt_rs_column(be, psub, i, exp_subtype(exp));
    2603             : 
    2604          23 :                                                 s = stmt_alias(be, s, exp->alias.label, exp->l, exp->r);
    2605          23 :                                                 list_append(l, s);
    2606             :                                         }
    2607             :                                 }
    2608       15164 :                                 for (n = f->func->res->h; n && m; n = n->next, m = m->next, i++) {
    2609       13588 :                                         sql_arg *a = n->data;
    2610       13588 :                                         sql_exp *exp = m->data;
    2611       13588 :                                         stmt *s = stmt_rs_column(be, psub, i, &a->type);
    2612       13588 :                                         const char *rnme = exp_relname(exp)?exp_relname(exp):exp_find_rel_name(op);
    2613             : 
    2614       13588 :                                         s = stmt_alias(be, s, exp->alias.label, rnme, a->name);
    2615       13588 :                                         list_append(l, s);
    2616             :                                 }
    2617             :                         }
    2618             :                 }
    2619        2817 :                 assert(rel->flag != TABLE_PROD_FUNC || !sub || !(sub->nrcols));
    2620        2817 :                 sub = stmt_list(be, l);
    2621        2817 :                 return sub;
    2622         186 :         } else if (rel->l) { /* handle sub query via function */
    2623         186 :                 int i;
    2624         186 :                 char name[16], *nme;
    2625             : 
    2626         186 :                 nme = number2name(name, sizeof(name), ++be->remote);
    2627             : 
    2628         186 :                 l = rel2bin_args(be, rel->l, sa_list(sql->sa));
    2629         186 :                 if (!l)
    2630           0 :                         return NULL;
    2631         186 :                 sub = stmt_list(be, l);
    2632         186 :                 if (!(sub = stmt_func(be, sub, sa_strdup(sql->sa, nme), rel->l, 0)))
    2633             :                         return NULL;
    2634         186 :                 rel->l = sub->op4.rel; /* rel->l may get rewritten */
    2635         186 :                 l = sa_list(sql->sa);
    2636        1593 :                 for (i = 0, n = rel->exps->h; n; n = n->next, i++) {
    2637        1407 :                         sql_exp *c = n->data;
    2638        1407 :                         stmt *s = stmt_rs_column(be, sub, i, exp_subtype(c));
    2639        1407 :                         const char *nme = exp_name(c);
    2640        1407 :                         const char *rnme = exp_relname(c);
    2641             : 
    2642        1407 :                         s = stmt_alias(be, s, c->alias.label, rnme, nme);
    2643        1407 :                         list_append(l, s);
    2644             :                 }
    2645         186 :                 sub = stmt_list(be, l);
    2646             :         }
    2647         186 :         if (!sub) {
    2648           0 :                 assert(sql->session->status == -10); /* Stack overflow errors shouldn't terminate the server */
    2649             :                 return NULL;
    2650             :         }
    2651         186 :         l = sa_list(sql->sa);
    2652         186 :         if (l == NULL)
    2653             :                 return NULL;
    2654        1593 :         for (en = rel->exps->h; en; en = en->next) {
    2655        1407 :                 sql_exp *exp = en->data;
    2656        1407 :                 const char *rnme = exp_relname(exp)?exp_relname(exp):exp->l;
    2657             :                 //stmt *s = bin_find_column(be, sub, exp->l, exp->r);
    2658        1407 :                 stmt *s = bin_find_column_nid(be, sub, exp->nid);
    2659             : 
    2660        1407 :                 if (!s) {
    2661           0 :                         assert(sql->session->status == -10); /* Stack overflow errors shouldn't terminate the server */
    2662             :                         return NULL;
    2663             :                 }
    2664        1407 :                 if (sub && sub->nrcols >= 1 && s->nrcols == 0)
    2665           0 :                         s = stmt_const(be, bin_find_smallest_column(be, sub), s);
    2666        1407 :                 s = stmt_alias(be, s, exp->alias.label, rnme, exp_name(exp));
    2667        1407 :                 list_append(l, s);
    2668             :         }
    2669         186 :         if (osub && osub->nrcols)
    2670             :                 list_merge(l, osub->op4.lval, NULL);
    2671         186 :         sub = stmt_list(be, l);
    2672         186 :         return sub;
    2673             : }
    2674             : 
    2675             : static stmt *
    2676          90 : rel2bin_hash_lookup(backend *be, sql_rel *rel, stmt *left, stmt *right, sql_idx *i, node *en)
    2677             : {
    2678          90 :         mvc *sql = be->mvc;
    2679          90 :         node *n;
    2680          90 :         sql_subtype *it = sql_bind_localtype("int");
    2681          90 :         sql_subtype *lng = sql_bind_localtype("lng");
    2682          90 :         stmt *h = NULL;
    2683          90 :         stmt *bits = stmt_atom_int(be, 1 + ((sizeof(lng)*8)-1)/(list_length(i->columns)+1));
    2684          90 :         sql_exp *e = en->data;
    2685          90 :         sql_exp *l = e->l;
    2686          90 :         stmt *idx = bin_find_column(be, left, l->l, sa_strconcat(sql->sa, "%", i->base.name));
    2687          90 :         int swap_exp = 0, swap_rel = 0, semantics = 0;
    2688             : 
    2689          90 :         if (!idx) {
    2690          49 :                 swap_exp = 1;
    2691          49 :                 l = e->r;
    2692          49 :                 idx = bin_find_column(be, left, l->l, sa_strconcat(sql->sa, "%", i->base.name));
    2693             :         }
    2694          90 :         if (!idx && right) {
    2695          47 :                 swap_exp = 0;
    2696          47 :                 swap_rel = 1;
    2697          47 :                 l = e->l;
    2698          47 :                 idx = bin_find_column(be, right, l->l, sa_strconcat(sql->sa, "%", i->base.name));
    2699             :         }
    2700          90 :         if (!idx && right) {
    2701           0 :                 swap_exp = 1;
    2702           0 :                 swap_rel = 1;
    2703           0 :                 l = e->r;
    2704           0 :                 idx = bin_find_column(be, right, l->l, sa_strconcat(sql->sa, "%", i->base.name));
    2705             :         }
    2706          90 :         if (!idx)
    2707             :                 return NULL;
    2708             :         /* should be in key order! */
    2709         314 :         for (en = rel->exps->h, n = i->columns->h; en && n; en = en->next, n = n->next) {
    2710         226 :                 sql_exp *e = en->data;
    2711         226 :                 stmt *s = NULL;
    2712             : 
    2713         226 :                 if (e->type == e_cmp && e->flag == cmp_equal) {
    2714         226 :                         sql_exp *ee = (swap_exp)?e->l:e->r;
    2715         226 :                         if (swap_rel)
    2716         109 :                                 s = exp_bin(be, ee, left, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    2717             :                         else
    2718         117 :                                 s = exp_bin(be, ee, right, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    2719             :                 }
    2720             : 
    2721         226 :                 if (!s)
    2722           2 :                         return NULL;
    2723         224 :                 if (h) {
    2724         136 :                         sql_subfunc *xor = sql_bind_func_result(sql, "sys", "rotate_xor_hash", F_FUNC, true, lng, 3, lng, it, tail_type(s));
    2725             : 
    2726         136 :                         h = stmt_Nop(be, stmt_list(be, list_append(list_append(
    2727             :                                 list_append(sa_list(sql->sa), h), bits), s)), NULL, xor, NULL);
    2728         136 :                         semantics = 1;
    2729             :                 } else {
    2730          88 :                         sql_subfunc *hf = sql_bind_func_result(sql, "sys", "hash", F_FUNC, true, lng, 1, tail_type(s));
    2731             : 
    2732          88 :                         h = stmt_unop(be, s, NULL, hf);
    2733             :                 }
    2734             :         }
    2735          88 :         if (n != NULL) /* need to use all cols of the index */
    2736             :                 return NULL;
    2737          87 :         if (h && h->nrcols) {
    2738          78 :                 if (!swap_rel) {
    2739          31 :                         return stmt_join(be, idx, h, 0, cmp_equal, 0, semantics, false);
    2740             :                 } else {
    2741          47 :                         return stmt_join(be, h, idx, 0, cmp_equal, 0, semantics, false);
    2742             :                 }
    2743             :         } else {
    2744           9 :                 return stmt_uselect(be, idx, h, cmp_equal, NULL, 0, semantics);
    2745             :         }
    2746             : }
    2747             : 
    2748             : static stmt *
    2749        5720 : join_hash_key(backend *be, list *l)
    2750             : {
    2751        5720 :         mvc *sql = be->mvc;
    2752        5720 :         node *m;
    2753        5720 :         sql_subtype *it, *lng;
    2754        5720 :         stmt *h = NULL;
    2755        5720 :         stmt *bits = stmt_atom_int(be, 1 + ((sizeof(lng)*8)-1)/(list_length(l)+1));
    2756             : 
    2757        5720 :         it = sql_bind_localtype("int");
    2758        5720 :         lng = sql_bind_localtype("lng");
    2759       18044 :         for (m = l->h; m; m = m->next) {
    2760       12324 :                 stmt *s = m->data;
    2761             : 
    2762       12324 :                 if (h) {
    2763        6604 :                         sql_subfunc *xor = sql_bind_func_result(sql, "sys", "rotate_xor_hash", F_FUNC, true, lng, 3, lng, it, tail_type(s));
    2764             : 
    2765        6604 :                         h = stmt_Nop(be, stmt_list(be, list_append(list_append(list_append(sa_list(sql->sa), h), bits), s)), NULL, xor, NULL);
    2766             :                 } else {
    2767        5720 :                         sql_subfunc *hf = sql_bind_func_result(sql, "sys", "hash", F_FUNC, true, lng, 1, tail_type(s));
    2768        5720 :                         h = stmt_unop(be, s, NULL, hf);
    2769             :                 }
    2770             :         }
    2771        5720 :         return h;
    2772             : }
    2773             : 
    2774             : static stmt *
    2775        5494 : releqjoin(backend *be, list *l1, list *l2, list *exps, int used_hash, int need_left, int is_semantics)
    2776             : {
    2777        5494 :         node *n1 = l1->h, *n2 = l2->h, *n3 = NULL;
    2778        5494 :         stmt *l, *r, *res;
    2779        5494 :         sql_exp *e;
    2780             : 
    2781        5494 :         if (exps)
    2782        2799 :                 n3 = exps->h;
    2783        5494 :         if (list_length(l1) <= 1) {
    2784        2309 :                 l = l1->h->data;
    2785        2309 :                 r = l2->h->data;
    2786        2309 :                 if (!is_semantics && exps) {
    2787           0 :                         e = n3->data;
    2788           0 :                         is_semantics = is_semantics(e);
    2789             :                 }
    2790        2309 :                 r =  stmt_join(be, l, r, 0, cmp_equal, need_left, is_semantics, false);
    2791        2309 :                 return r;
    2792             :         }
    2793        3185 :         if (used_hash) {
    2794         325 :                 l = n1->data;
    2795         325 :                 r = n2->data;
    2796         325 :                 n1 = n1->next;
    2797         325 :                 n2 = n2->next;
    2798         325 :                 n3 = n3?n3->next:NULL;
    2799         325 :                 res = stmt_join(be, l, r, 0, cmp_equal, need_left, 1, false);
    2800             :         } else { /* need hash */
    2801        2860 :                 l = join_hash_key(be, l1);
    2802        2860 :                 r = join_hash_key(be, l2);
    2803        2860 :                 res = stmt_join(be, l, r, 0, cmp_equal, need_left, 1, false);
    2804             :         }
    2805        3185 :         l = stmt_result(be, res, 0);
    2806        3185 :         r = stmt_result(be, res, 1);
    2807       19224 :         for (; n1 && n2; n1 = n1->next, n2 = n2->next, n3 = n3?n3->next:NULL) {
    2808        6906 :                 int semantics = is_semantics;
    2809        6906 :                 stmt *ld = n1->data;
    2810        6906 :                 stmt *rd = n2->data;
    2811        6906 :                 stmt *le = stmt_project(be, l, ld);
    2812        6906 :                 stmt *re = stmt_project(be, r, rd);
    2813        6906 :                 stmt *cmp;
    2814             :                 /* intentional both tail_type's of le (as re sometimes is a find for bulk loading */
    2815             : 
    2816        6906 :                 if (!semantics && exps) {
    2817        5948 :                         e = n3->data;
    2818        5948 :                         semantics = is_semantics(e);
    2819             :                 }
    2820        6906 :                 cmp = stmt_uselect(be, le, re, cmp_equal, NULL, 0, semantics);
    2821        6906 :                 l = stmt_project(be, cmp, l);
    2822        6906 :                 r = stmt_project(be, cmp, r);
    2823             :         }
    2824        3185 :         res = stmt_join(be, l, r, 0, cmp_joined, 0, 0, false);
    2825        3185 :         return res;
    2826             : }
    2827             : 
    2828             : static bool
    2829       79469 : can_join_exp(sql_rel *rel, sql_exp *e, bool anti)
    2830             : {
    2831       79469 :         bool can_join = 0;
    2832             : 
    2833       79469 :         if (e->type == e_cmp) {
    2834       79346 :                 int flag = e->flag;
    2835             :                 /* check if its a select or join expression, ie use only expressions of one relation left and of the other right (than join) */
    2836       79346 :                 if (flag < cmp_filter) { /* theta and range joins */
    2837             :                         /* join or select ? */
    2838       79255 :                         sql_exp *l = e->l, *r = e->r, *f = e->f;
    2839             : 
    2840       79255 :                         if (f) {
    2841         100 :                                 int ll = rel_has_exp(rel->l, l, true) == 0;
    2842         100 :                                 int rl = rel_has_exp(rel->r, l, true) == 0;
    2843         100 :                                 int lr = rel_has_exp(rel->l, r, true) == 0;
    2844         100 :                                 int rr = rel_has_exp(rel->r, r, true) == 0;
    2845         100 :                                 int lf = rel_has_exp(rel->l, f, true) == 0;
    2846         100 :                                 int rf = rel_has_exp(rel->r, f, true) == 0;
    2847         100 :                                 int nrcr1 = 0, nrcr2 = 0, nrcl1 = 0, nrcl2 = 0;
    2848             : 
    2849         100 :                                 if ((ll && !rl &&
    2850          82 :                                    ((rr && !lr) || (nrcr1 = r->card == CARD_ATOM && exp_is_atom(r))) &&
    2851          70 :                                    ((rf && !lf) || (nrcr2 = f->card == CARD_ATOM && exp_is_atom(f))) && (nrcr1+nrcr2) <= 1) ||
    2852          31 :                                     (rl && !ll &&
    2853          16 :                                    ((lr && !rr) || (nrcl1 = r->card == CARD_ATOM && exp_is_atom(r))) &&
    2854          15 :                                    ((lf && !rf) || (nrcl2 = f->card == CARD_ATOM && exp_is_atom(f))) && (nrcl1+nrcl2) <= 1)) {
    2855             :                                         can_join = 1;
    2856             :                                 }
    2857             :                         } else {
    2858       79155 :                                 int ll = 0, lr = 0, rl = 0, rr = 0, cst = 0;
    2859       79155 :                                 if (l->card != CARD_ATOM || !exp_is_atom(l)) {
    2860       78956 :                                         ll = rel_has_exp(rel->l, l, true) == 0;
    2861       78956 :                                         rl = rel_has_exp(rel->r, l, true) == 0;
    2862         199 :                                 } else if (anti) {
    2863          15 :                                         ll = 1;
    2864          15 :                                         cst = 1;
    2865             :                                 }
    2866       79155 :                                 if (r->card != CARD_ATOM || !exp_is_atom(r)) {
    2867       79001 :                                         lr = rel_has_exp(rel->l, r, true) == 0;
    2868       79001 :                                         rr = rel_has_exp(rel->r, r, true) == 0;
    2869         154 :                                 } else if (anti) {
    2870           3 :                                         rr = cst?0:1;
    2871             :                                 }
    2872       79155 :                                 if ((ll && !lr && !rl && rr) || (!ll && lr && rl && !rr))
    2873       78826 :                                         can_join = 1;
    2874             :                         }
    2875          91 :                 } else if (flag == cmp_filter) {
    2876          61 :                         list *l = e->l, *r = e->r;
    2877          61 :                         int ll = 0, lr = 0, rl = 0, rr = 0;
    2878             : 
    2879         122 :                         for (node *n = l->h ; n ; n = n->next) {
    2880          61 :                                 sql_exp *ee = n->data;
    2881             : 
    2882          61 :                                 if (ee->card != CARD_ATOM || !exp_is_atom(ee)) {
    2883          58 :                                         ll |= rel_has_exp(rel->l, ee, true) == 0;
    2884          58 :                                         rl |= rel_has_exp(rel->r, ee, true) == 0;
    2885             :                                 }
    2886             :                         }
    2887         207 :                         for (node *n = r->h ; n ; n = n->next) {
    2888         146 :                                 sql_exp *ee = n->data;
    2889             : 
    2890         146 :                                 if (ee->card != CARD_ATOM || !exp_is_atom(ee)) {
    2891          63 :                                         lr |= rel_has_exp(rel->l, ee, true) == 0;
    2892          63 :                                         rr |= rel_has_exp(rel->r, ee, true) == 0;
    2893             :                                 }
    2894             :                         }
    2895          61 :                         if ((ll && !lr && !rl && rr) || (!ll && lr && rl && !rr))
    2896       78826 :                                 can_join = 1;
    2897             :                 }
    2898             :         }
    2899       79469 :         return can_join;
    2900             : }
    2901             : 
    2902             : static void
    2903       66266 : split_join_exps(sql_rel *rel, list *joinable, list *not_joinable, bool anti)
    2904             : {
    2905       66266 :         if (!list_empty(rel->exps)) {
    2906      137104 :                 for (node *n = rel->exps->h; n; n = n->next) {
    2907       70844 :                         sql_exp *e = n->data;
    2908             : 
    2909             :                         /* we can handle thetajoins, rangejoins and filter joins (like) */
    2910             :                         /* ToDo how about atom expressions? */
    2911       70844 :                         if (can_join_exp(rel, e, anti)) {
    2912       70433 :                                 append(joinable, e);
    2913             :                         } else {
    2914         411 :                                 append(not_joinable, e);
    2915             :                         }
    2916             :                 }
    2917             :         }
    2918       66266 : }
    2919             : 
    2920             : 
    2921             : #define is_equi_exp_(e) ((e)->flag == cmp_equal)
    2922             : 
    2923             : static list *
    2924        3091 : get_simple_equi_joins_first(mvc *sql, sql_rel *rel, list *exps, bool *equality_only)
    2925             : {
    2926        3091 :         list *new_exps = sa_list(sql->sa);
    2927        3091 :         *equality_only = true;
    2928             : 
    2929        3091 :         if (!exps)
    2930             :                 return new_exps;
    2931             : 
    2932        6083 :         for (node *n = exps->h; n; n = n->next) {
    2933        3095 :                 sql_exp *e = n->data;
    2934             : 
    2935        3095 :                 if (can_join_exp(rel, e, false) && is_equi_exp_(e) && !is_any(e))
    2936         218 :                         list_append(new_exps, e);
    2937             :                 else
    2938        2877 :                         *equality_only = false;
    2939             :         }
    2940        6083 :         for (node *n = exps->h; n; n = n->next) {
    2941        3095 :                 sql_exp *e = n->data;
    2942             : 
    2943        3095 :                 if (!is_equi_exp_(e) || !can_join_exp(rel, e, false) || is_any(e))
    2944        2877 :                         list_append(new_exps, e);
    2945             :         }
    2946             :         return new_exps;
    2947             : }
    2948             : 
    2949             : static stmt *
    2950        3091 : rel2bin_groupjoin(backend *be, sql_rel *rel, list *refs)
    2951             : {
    2952        3091 :         mvc *sql = be->mvc;
    2953        3091 :         list *l;
    2954        3091 :         node *n , *en;
    2955        3091 :         stmt *left = NULL, *right = NULL, *join = NULL, *jl = NULL, *jr = NULL, *m = NULL, *ls = NULL, *res;
    2956        3091 :         bool need_project = false, exist = true, mark = false;
    2957             : 
    2958        3091 :         if (rel->op == op_left) { /* left outer group join */
    2959        3003 :                 if (list_length(rel->attr) == 1) {
    2960        3003 :                         sql_exp *e = rel->attr->h->data;
    2961        3003 :                         if (exp_is_atom(e))
    2962        3003 :                                 mark = true;
    2963        3003 :                         if (exp_is_atom(e) && exp_is_false(e))
    2964        3091 :                                 exist = false;
    2965             :                 }
    2966             :         }
    2967             : 
    2968        3091 :         if (rel->l) /* first construct the left sub relation */
    2969        3091 :                 left = subrel_bin(be, rel->l, refs);
    2970        3091 :         if (rel->r) /* first construct the right sub relation */
    2971        3091 :                 right = subrel_bin(be, rel->r, refs);
    2972        3091 :         left = subrel_project(be, left, refs, rel->l);
    2973        3091 :         right = subrel_project(be, right, refs, rel->r);
    2974        3091 :         if (!left || !right)
    2975             :                 return NULL;
    2976        3091 :         left = row2cols(be, left);
    2977        3091 :         right = row2cols(be, right);
    2978             : 
    2979        3091 :         bool equality_only = true;
    2980        3091 :         list *jexps = get_simple_equi_joins_first(sql, rel, rel->exps, &equality_only);
    2981             : 
    2982        3091 :         en = jexps?jexps->h:NULL;
    2983        3527 :         if (list_empty(jexps) || !(is_equi_exp_((sql_exp*)en->data) && can_join_exp(rel, en->data, false))) {
    2984         436 :                 stmt *l = bin_find_smallest_column(be, left);
    2985         436 :                 stmt *r = bin_find_smallest_column(be, right);
    2986         436 :                 if (list_empty(jexps)) {
    2987         114 :                         stmt *limit = stmt_limit(be, r, NULL, NULL, stmt_atom_lng(be, 0), stmt_atom_lng(be, 1), 0, 0, 0, 0, 0);
    2988         114 :                         r = stmt_project(be, limit, r);
    2989             :                 }
    2990         436 :                 join = stmt_join_cand(be, column(be, l), column(be, r), left->cand, NULL/*right->cand*/, 0, cmp_all, 0, 0, false, rel->op == op_left?false:true);
    2991         436 :                 need_project = true;
    2992         436 :                 jl = stmt_result(be, join, 0);
    2993         436 :                 jr = stmt_result(be, join, 1);
    2994             :         } else {
    2995        2655 :                 sql_exp *e = en->data;
    2996        2655 :                 en = en->next;
    2997        2655 :                 stmt *l = exp_bin(be, e->l, left, NULL, NULL, NULL, NULL, NULL, 0, 1, 0), *r = NULL;
    2998        2655 :                 bool swap = false;
    2999             : 
    3000        2655 :                 if (!l) {
    3001          16 :                         swap = true;
    3002          16 :                         l = exp_bin(be, e->l, right, NULL, NULL, NULL, NULL, NULL, 0, 1, 0);
    3003             :                 }
    3004          16 :                 if (!l)
    3005             :                         return NULL;
    3006        2655 :                 if ((r = exp_bin(be, e->r, left, right, NULL, NULL, NULL, NULL, 0, 1, 0)) == NULL)
    3007             :                         return NULL;
    3008             : 
    3009        2655 :                 if (l && l->nrcols == 0)
    3010           0 :                         l = stmt_const(be, bin_find_smallest_column(be, left), l);
    3011        2655 :                 if (r && r->nrcols == 0)
    3012           0 :                         r = stmt_const(be, bin_find_smallest_column(be, right), r);
    3013        2655 :                 if (swap) {
    3014          16 :                         stmt *t = l;
    3015          16 :                         l = r;
    3016          16 :                         r = t;
    3017             :                 }
    3018        2655 :                 if ((!is_semantics(e) && is_anti(e)) || !mark)
    3019          88 :                         ls = l;
    3020        2655 :                 if (en || !mark) {
    3021             :                         /* split out (left)join vs (left)mark-join */
    3022             :                         /* call 3 result version */
    3023         168 :                         if (mark && is_any(e)) {
    3024           5 :                                 join = stmt_markjoin(be, l, r, 0);
    3025             :                         } else
    3026         163 :                                 join = stmt_join_cand(be, column(be, l), column(be, r), left->cand, NULL/*right->cand*/, is_anti(e), (comp_type) cmp_equal/*e->flag*/, 0, is_any(e)|is_semantics(e), false, rel->op == op_left?false:true);
    3027         168 :                         jl = stmt_result(be, join, 0);
    3028         168 :                         jr = stmt_result(be, join, 1);
    3029         168 :                         if (mark && is_any(e))
    3030           5 :                                 m = stmt_result(be, join, 2);
    3031             :                 } else {
    3032        2487 :                         join = stmt_markjoin(be, l, r, 1);
    3033        2487 :                         jl = stmt_result(be, join, 0);
    3034        2487 :                         m = stmt_result(be, join, 1);
    3035             :                 }
    3036             :         }
    3037             : 
    3038        3091 :         if (en) {
    3039         402 :                 stmt *sub, *sel = NULL, *osel = NULL;
    3040         402 :                 list *nl;
    3041             : 
    3042         402 :                 need_project = false;
    3043             : 
    3044             :                 /* construct relation */
    3045         402 :                 nl = sa_list(sql->sa);
    3046             : 
    3047             :                 /* first project using equi-joins */
    3048        2086 :                 for (n = left->op4.lval->h; n; n = n->next) {
    3049        1684 :                         stmt *c = n->data;
    3050        1684 :                         assert(c->type == st_alias);
    3051        1684 :                         const char *rnme = table_name(sql->sa, c);
    3052        1684 :                         const char *nme = column_name(sql->sa, c);
    3053        1684 :                         stmt *s = stmt_project(be, jl, column(be, c));
    3054             : 
    3055        1684 :                         s = stmt_alias(be, s, c->label, rnme, nme);
    3056        1684 :                         list_append(nl, s);
    3057             :                 }
    3058        1026 :                 for (n = right->op4.lval->h; n; n = n->next) {
    3059         624 :                         stmt *c = n->data;
    3060         624 :                         assert(c->type == st_alias);
    3061         624 :                         const char *rnme = table_name(sql->sa, c);
    3062         624 :                         const char *nme = column_name(sql->sa, c);
    3063         624 :                         stmt *s = stmt_project(be, jr, column(be, c));
    3064             : 
    3065         624 :                         s = stmt_alias(be, s, c->label, rnme, nme);
    3066         624 :                         list_append(nl, s);
    3067             :                 }
    3068         402 :                 left = sub = stmt_list(be, nl);
    3069             : 
    3070         402 :                 if (!m) {
    3071         397 :                         if (ls) {
    3072           0 :                                 stmt *nls = stmt_project(be, jl, ls);
    3073           0 :                                         m = sql_Nop_(be, "ifthenelse", sql_unop_(be, "isnull", nls), stmt_bool(be, bit_nil),
    3074             :                                                 sql_Nop_(be, "ifthenelse", sql_unop_(be, "isnull", jr), stmt_bool(be, 0), stmt_bool(be, 1), NULL),
    3075             :                                                 NULL);
    3076             :                         } else {
    3077             :                                 /* 0 == empty (no matches possible), nil - no match (but has nil), 1 match */
    3078         397 :                                 m = sql_Nop_(be, "ifthenelse", sql_unop_(be, "isnull", jr), stmt_bool(be, 0), stmt_bool(be, 1), NULL);
    3079             :                         }
    3080             :                 }
    3081             : 
    3082             :                 /* continue with non equi-joins */
    3083         842 :                 for ( ; en; en = en->next) {
    3084         440 :                         sql_exp *e = en->data;
    3085         440 :                         stmt *p = exp_bin(be, e, sub, NULL, NULL, NULL, NULL, NULL, 1, 0, 0);
    3086             : 
    3087         440 :                         if (!p) {
    3088           0 :                                 assert(sql->session->status == -10); /* Stack overflow errors shouldn't terminate the server */
    3089             :                                 return NULL;
    3090             :                         }
    3091         440 :                         if (p->nrcols == 0)
    3092           1 :                                 p = stmt_const(be, bin_find_smallest_column(be, sub), p);
    3093         440 :                         if (sel)
    3094          38 :                                 p = stmt_project(be, sel, column(be, p));
    3095          38 :                         stmt *li = jl;
    3096          38 :                         if (sel)
    3097          38 :                                 li = stmt_project(be, sel, li);
    3098         440 :                         osel = sel;
    3099         440 :                         if (en->next) {
    3100          38 :                                 join = stmt_outerselect(be, li, m, p, is_any(e));
    3101             :                         } else {
    3102         402 :                                 join = stmt_markselect(be, li, m, p, is_any(e));
    3103             :                         }
    3104         440 :                         sel = stmt_result(be, join, 0);
    3105         440 :                         m = stmt_result(be, join, 1);
    3106             :                         /* go back to offset in the table */
    3107         440 :                         if (sel && osel)
    3108          38 :                                 sel = stmt_project(be, sel, osel);
    3109         440 :                         if (!en->next)
    3110         402 :                                 jl = sel;
    3111             :                 }
    3112             :         }
    3113             :         /* construct relation */
    3114        3091 :         l = sa_list(sql->sa);
    3115       13831 :         for (n = left->op4.lval->h; n; n = n->next) {
    3116       10740 :                 stmt *c = n->data;
    3117       10740 :                 assert(c->type == st_alias);
    3118       10740 :                 const char *rnme = table_name(sql->sa, c);
    3119       10740 :                 const char *nme = column_name(sql->sa, c);
    3120       10740 :                 stmt *s = stmt_project(be, jl, column(be, c));
    3121             : 
    3122       10740 :                 s = stmt_alias(be, s, c->label, rnme, nme);
    3123       10740 :                 list_append(l, s);
    3124             :         }
    3125        3091 :         if (!mark && jr) {
    3126         182 :                 for (n = right->op4.lval->h; n; n = n->next) {
    3127          94 :                         stmt *c = n->data;
    3128          94 :                         assert(c->type == st_alias);
    3129          94 :                         const char *rnme = table_name(sql->sa, c);
    3130          94 :                         const char *nme = column_name(sql->sa, c);
    3131          94 :                         stmt *s = stmt_project(be, jr, column(be, c));
    3132             : 
    3133          94 :                         s = stmt_alias(be, s, c->label, rnme, nme);
    3134          94 :                         list_append(l, s);
    3135             :                 }
    3136          88 :                 left = stmt_list(be, l);
    3137          88 :                 l = sa_list(sql->sa);
    3138             :         }
    3139        3091 :         if (rel->attr) {
    3140        3091 :                 sql_exp *e = rel->attr->h->data;
    3141        3091 :                 const char *rnme = exp_relname(e);
    3142        3091 :                 const char *nme = exp_name(e);
    3143             : 
    3144        3091 :                 if (mark) {
    3145        3003 :                         if (need_project) {
    3146         114 :                                 m = sql_Nop_(be, "ifthenelse", sql_unop_(be, "isnull", jr), stmt_bool(be, !exist), stmt_bool(be, exist), NULL);
    3147             :                         } else {
    3148        2889 :                                 assert(m);
    3149        2889 :                                 sql_exp *e = rel->attr->h->data;
    3150        2889 :                                 if (exp_is_atom(e) && need_no_nil(e))
    3151         298 :                                         m = sql_Nop_(be, "ifthenelse", sql_unop_(be, "isnull", m), stmt_bool(be, false), m, NULL);
    3152        2889 :                                 if (!exist) {
    3153          54 :                                         sql_subtype *bt = sql_bind_localtype("bit");
    3154          54 :                                         sql_subfunc *not = sql_bind_func(be->mvc, "sys", "not", bt, NULL, F_FUNC, true, true);
    3155          54 :                                         m = stmt_unop(be, m, NULL, not);
    3156             :                                 }
    3157             :                         }
    3158        3003 :                         stmt *s = stmt_alias(be, m, e->alias.label, rnme, nme);
    3159        3003 :                         append(l, s);
    3160             :                 } else {
    3161             :                         /* group / aggrs */
    3162          88 :                         stmt *nls = stmt_project(be, jl, ls);
    3163          88 :                         stmt *groupby = stmt_group(be, nls, NULL, NULL, NULL, true);
    3164          88 :                         stmt *grp = stmt_result(be, groupby, 0);
    3165          88 :                         stmt *ext = stmt_result(be, groupby, 1);
    3166          88 :                         stmt *cnt = stmt_result(be, groupby, 2);
    3167         176 :                         for(node *n = rel->attr->h; n; n = n->next) {
    3168          88 :                                 sql_exp *e = n->data;
    3169          88 :                                 const char *rnme = exp_relname(e);
    3170          88 :                                 const char *nme = exp_name(e);
    3171          88 :                                 stmt *s = exp_bin(be, e, left, NULL, grp, ext, cnt, NULL, 0, 0, 0);
    3172          88 :                                 s = stmt_alias(be, s, e->alias.label, rnme, nme);
    3173          88 :                                 append(l, s);
    3174             :                         }
    3175             :                 }
    3176             :         }
    3177        3091 :         res = stmt_list(be, l);
    3178        3091 :         return res;
    3179             : }
    3180             : 
    3181             : static list *
    3182       64446 : get_equi_joins_first(mvc *sql, list *exps, int *equality_only)
    3183             : {
    3184       64446 :         list *new_exps = sa_list(sql->sa);
    3185             : 
    3186      133201 :         for (node *n = exps->h; n; n = n->next) {
    3187       68755 :                 sql_exp *e = n->data;
    3188             : 
    3189       68755 :                 assert(e->type == e_cmp && e->flag != cmp_in && e->flag != cmp_notin && e->flag != cmp_or);
    3190       68755 :                 if (is_equi_exp_(e))
    3191       64879 :                         list_append(new_exps, e);
    3192             :                 else
    3193        3876 :                         *equality_only = 0;
    3194             :         }
    3195      133201 :         for (node *n = exps->h; n; n = n->next) {
    3196       68755 :                 sql_exp *e = n->data;
    3197             : 
    3198       68755 :                 if (!is_equi_exp_(e))
    3199        3876 :                         list_append(new_exps, e);
    3200             :         }
    3201       64446 :         return new_exps;
    3202             : }
    3203             : 
    3204             : static stmt *
    3205       75939 : rel2bin_join(backend *be, sql_rel *rel, list *refs)
    3206             : {
    3207       75939 :         mvc *sql = be->mvc;
    3208       75939 :         list *l, *sexps = NULL, *l2 = NULL;
    3209       75939 :         node *en = NULL, *n;
    3210       75939 :         stmt *left = NULL, *right = NULL, *join = NULL, *jl, *jr, *ld = NULL, *rd = NULL, *res;
    3211       75939 :         int need_left = (rel->flag & LEFT_JOIN);
    3212             : 
    3213       75939 :         if (rel->attr && list_length(rel->attr) > 0)
    3214        3091 :                 return rel2bin_groupjoin(be, rel, refs);
    3215             : 
    3216       72848 :         if (rel->l) /* first construct the left sub relation */
    3217       72848 :                 left = subrel_bin(be, rel->l, refs);
    3218       72848 :         if (rel->r) /* first construct the right sub relation */
    3219       72848 :                 right = subrel_bin(be, rel->r, refs);
    3220       72848 :         left = subrel_project(be, left, refs, rel->l);
    3221       72848 :         right = subrel_project(be, right, refs, rel->r);
    3222       72848 :         if (!left || !right)
    3223             :                 return NULL;
    3224       72848 :         left = row2cols(be, left);
    3225       72848 :         right = row2cols(be, right);
    3226             :         /*
    3227             :          * split in 2 steps,
    3228             :          *      first cheap join(s) (equality or idx)
    3229             :          *      second selects/filters
    3230             :          */
    3231       72848 :         if (!list_empty(rel->exps)) {
    3232       61689 :                 list *jexps = sa_list(sql->sa);
    3233       61689 :                 sexps = sa_list(sql->sa);
    3234             : 
    3235       61689 :                 split_join_exps(rel, jexps, sexps, false);
    3236       61689 :                 if (list_empty(jexps)) { /* cross product and continue after project */
    3237         268 :                         stmt *l = bin_find_smallest_column(be, left);
    3238         268 :                         stmt *r = bin_find_smallest_column(be, right);
    3239         268 :                         join = stmt_join(be, l, r, 0, cmp_all, 0, 0, false);
    3240             :                 }
    3241             : 
    3242         268 :                 if (join) {
    3243         268 :                         en = jexps->h;
    3244             :                 } else {
    3245       61421 :                         list *lje = sa_list(sql->sa), *rje = sa_list(sql->sa), *exps = sa_list(sql->sa);
    3246       61421 :                         int used_hash = 0, idx = 0, equality_only = 1;
    3247             : 
    3248       61421 :                         (void) equality_only;
    3249       61421 :                         jexps = get_equi_joins_first(sql, jexps, &equality_only);
    3250             :                         /* generate a relational join (releqjoin) which does a multi attribute (equi) join */
    3251      123017 :                         for (en = jexps->h; en ; en = en->next) {
    3252       63948 :                                 int join_idx = be->join_idx;
    3253       63948 :                                 sql_exp *e = en->data;
    3254       63948 :                                 stmt *s = NULL;
    3255       63948 :                                 prop *p;
    3256             : 
    3257             :                                 /* stop search for equi joins on first non equi */
    3258       63948 :                                 if (list_length(lje) && (idx || e->type != e_cmp || e->flag != cmp_equal))
    3259             :                                         break;
    3260             : 
    3261             :                                 /* handle possible index lookups, expressions are in index order! */
    3262       63854 :                                 if (!join && (p=find_prop(e->p, PROP_HASHCOL)) != NULL) {
    3263          78 :                                         sql_idx *i = p->value.pval;
    3264          78 :                                         int oldvtop = be->mb->vtop, oldstop = be->mb->stop;
    3265             : 
    3266          78 :                                         join = s = rel2bin_hash_lookup(be, rel, left, right, i, en);
    3267          78 :                                         if (s) {
    3268          78 :                                                 list_append(lje, s->op1);
    3269          78 :                                                 list_append(rje, s->op2);
    3270          78 :                                                 list_append(exps, NULL);
    3271          78 :                                                 used_hash = 1;
    3272             :                                         } else {
    3273             :                                                 /* hash lookup cannot be used, clean leftover mal statements */
    3274           0 :                                                 clean_mal_statements(be, oldstop, oldvtop);
    3275             :                                         }
    3276             :                                 }
    3277             : 
    3278       63854 :                                 s = exp_bin(be, e, left, right, NULL, NULL, NULL, NULL, 0, 1, 0);
    3279       63854 :                                 if (!s) {
    3280           0 :                                         assert(sql->session->status == -10); /* Stack overflow errors shouldn't terminate the server */
    3281           0 :                                         return NULL;
    3282             :                                 }
    3283       63854 :                                 if (join_idx != be->join_idx)
    3284         620 :                                         idx = 1;
    3285       63854 :                                 assert(s->type == st_join || s->type == st_join2 || s->type == st_joinN);
    3286       63854 :                                 if (!join)
    3287       61343 :                                         join = s;
    3288       63854 :                                 if (e->flag != cmp_equal) { /* only collect equi joins */
    3289        2258 :                                         en = en->next;
    3290        2258 :                                         break;
    3291             :                                 }
    3292       61596 :                                 list_append(lje, s->op1);
    3293       61596 :                                 list_append(rje, s->op2);
    3294       61596 :                                 list_append(exps, e);
    3295             :                         }
    3296       61421 :                         if (list_length(lje) > 1) {
    3297        2186 :                                 join = releqjoin(be, lje, rje, exps, used_hash, need_left, 0);
    3298       59235 :                         } else if (!join || need_left) {
    3299         632 :                                 sql_exp *e = exps->h->data;
    3300         632 :                                 join = stmt_join(be, lje->h->data, rje->h->data, 0, cmp_equal, need_left, is_semantics(e), false);
    3301             :                         }
    3302             :                 }
    3303             :         } else {
    3304       11159 :                 stmt *l = bin_find_smallest_column(be, left);
    3305       11159 :                 stmt *r = bin_find_smallest_column(be, right);
    3306       11159 :                 join = stmt_join(be, l, r, 0, cmp_all, 0, 0, is_single(rel));
    3307             :         }
    3308       72848 :         jl = stmt_result(be, join, 0);
    3309       72848 :         jr = stmt_result(be, join, 1);
    3310       72848 :         if (en || (sexps && list_length(sexps))) {
    3311        1440 :                 stmt *sub, *sel = NULL;
    3312        1440 :                 list *nl;
    3313             : 
    3314             :                 /* construct relation */
    3315        1440 :                 nl = sa_list(sql->sa);
    3316             : 
    3317             :                 /* first project using equi-joins */
    3318        6868 :                 for (n = left->op4.lval->h; n; n = n->next) {
    3319        5428 :                         stmt *c = n->data;
    3320        5428 :                         assert(c->type == st_alias);
    3321        5428 :                         const char *rnme = table_name(sql->sa, c);
    3322        5428 :                         const char *nme = column_name(sql->sa, c);
    3323        5428 :                         stmt *s = stmt_project(be, jl, column(be, c));
    3324             : 
    3325        5428 :                         s = stmt_alias(be, s, c->label, rnme, nme);
    3326        5428 :                         list_append(nl, s);
    3327             :                 }
    3328        4650 :                 for (n = right->op4.lval->h; n; n = n->next) {
    3329        3210 :                         stmt *c = n->data;
    3330        3210 :                         assert(c->type == st_alias);
    3331        3210 :                         const char *rnme = table_name(sql->sa, c);
    3332        3210 :                         const char *nme = column_name(sql->sa, c);
    3333        3210 :                         stmt *s = stmt_project(be, jr, column(be, c));
    3334             : 
    3335        3210 :                         s = stmt_alias(be, s, c->label, rnme, nme);
    3336        3210 :                         list_append(nl, s);
    3337             :                 }
    3338        1440 :                 sub = stmt_list(be, nl);
    3339             : 
    3340             :                 /* continue with non equi-joins */
    3341        4020 :                 while(sexps) {
    3342        2580 :                         if (!en) {
    3343        1440 :                                 en = sexps->h;
    3344        1440 :                                 sexps = NULL;
    3345             :                         }
    3346        4042 :                         for ( ; en; en = en->next) {
    3347        1462 :                                 stmt *s = exp_bin(be, en->data, sub, NULL, NULL, NULL, NULL, sel, 0, 1, 0);
    3348             : 
    3349        1462 :                                 if (!s) {
    3350           0 :                                         assert(sql->session->status == -10); /* Stack overflow errors shouldn't terminate the server */
    3351             :                                         return NULL;
    3352             :                                 }
    3353        1462 :                                 if (s->nrcols == 0) {
    3354         124 :                                         stmt *l = bin_find_smallest_column(be, sub);
    3355         124 :                                         s = stmt_uselect(be, stmt_const(be, l, stmt_bool(be, 1)), s, cmp_equal, sel, 0, 0);
    3356             :                                 }
    3357        1462 :                                 sel = s;
    3358             :                         }
    3359             :                 }
    3360             :                 /* recreate join output */
    3361        1440 :                 jl = stmt_project(be, sel, jl);
    3362        1440 :                 jr = stmt_project(be, sel, jr);
    3363             :         }
    3364             : 
    3365             :         /* construct relation */
    3366       72848 :         l = sa_list(sql->sa);
    3367             : 
    3368       72848 :         if (rel->op == op_left || rel->op == op_full || is_single(rel)) {
    3369             :                 /* we need to add the missing oid's */
    3370       11381 :                 stmt *l = ld = stmt_mirror(be, bin_find_smallest_column(be, left));
    3371       11381 :                 if (rel->op == op_left || rel->op == op_full)
    3372        8225 :                         ld = stmt_tdiff(be, ld, jl, NULL);
    3373       11381 :                 if (is_single(rel) && !list_empty(rel->exps)) {
    3374         280 :                         join = stmt_semijoin(be, l, jl, NULL, NULL, 0, true);
    3375         280 :                         jl = stmt_result(be, join, 0);
    3376         280 :                         jr = stmt_project(be, stmt_result(be, join, 1), jr);
    3377             :                 }
    3378             :         }
    3379       72848 :         if (rel->op == op_right || rel->op == op_full) {
    3380             :                 /* we need to add the missing oid's */
    3381         166 :                 rd = stmt_mirror(be, bin_find_smallest_column(be, right));
    3382         166 :                 rd = stmt_tdiff(be, rd, jr, NULL);
    3383             :         }
    3384             : 
    3385       72848 :         if (rel->op == op_left) { /* used for merge statements, this will be cleaned out on the pushcands branch :) */
    3386        8154 :                 l2 = sa_list(sql->sa);
    3387        8154 :                 list_append(l2, left);
    3388        8154 :                 list_append(l2, right);
    3389        8154 :                 list_append(l2, jl);
    3390        8154 :                 list_append(l2, jr);
    3391        8154 :                 list_append(l2, ld);
    3392             :         }
    3393             : 
    3394     1929421 :         for (n = left->op4.lval->h; n; n = n->next) {
    3395     1856573 :                 stmt *c = n->data;
    3396     1856573 :                 const char *rnme = table_name(sql->sa, c);
    3397     1856573 :                 const char *nme = column_name(sql->sa, c);
    3398     1856573 :                 stmt *s = stmt_project(be, jl, column(be, c));
    3399             : 
    3400             :                 /* as append isn't save, we append to a new copy */
    3401     1856573 :                 if (rel->op == op_left || rel->op == op_full || rel->op == op_right)
    3402       60927 :                         s = create_const_column(be, s);
    3403     1856573 :                 if (rel->op == op_left || rel->op == op_full)
    3404       60725 :                         s = stmt_append(be, s, stmt_project(be, ld, c));
    3405     1856573 :                 if (rel->op == op_right || rel->op == op_full)
    3406         324 :                         s = stmt_append(be, s, stmt_const(be, rd, (c->flag&OUTER_ZERO)?stmt_atom_lng(be, 0):stmt_atom(be, atom_general(sql->sa, tail_type(c), NULL, 0))));
    3407             : 
    3408     1856573 :                 s = stmt_alias(be, s, c->label, rnme, nme);
    3409     1856573 :                 list_append(l, s);
    3410             :         }
    3411      279059 :         for (n = right->op4.lval->h; n; n = n->next) {
    3412      206211 :                 stmt *c = n->data;
    3413      206211 :                 const char *rnme = table_name(sql->sa, c);
    3414      206211 :                 const char *nme = column_name(sql->sa, c);
    3415      206211 :                 stmt *s = stmt_project(be, jr, column(be, c));
    3416             : 
    3417             :                 /* as append isn't save, we append to a new copy */
    3418      206211 :                 if (rel->op == op_left || rel->op == op_full || rel->op == op_right)
    3419       18554 :                         s = create_const_column(be, s);
    3420      206211 :                 if (rel->op == op_left || rel->op == op_full)
    3421       18295 :                         s = stmt_append(be, s, stmt_const(be, ld, (c->flag&OUTER_ZERO)?stmt_atom_lng(be, 0):stmt_atom(be, atom_general(sql->sa, tail_type(c), NULL, 0))));
    3422      206211 :                 if (rel->op == op_right || rel->op == op_full)
    3423         371 :                         s = stmt_append(be, s, stmt_project(be, rd, c));
    3424             : 
    3425      206211 :                 s = stmt_alias(be, s, c->label, rnme, nme);
    3426      206211 :                 list_append(l, s);
    3427             :         }
    3428       72848 :         if (rel->attr) {
    3429           0 :                 sql_exp *e = rel->attr->h->data;
    3430           0 :                 const char *rnme = exp_relname(e);
    3431           0 :                 const char *nme = exp_name(e);
    3432           0 :                 stmt *last = l->t->data;
    3433           0 :                 sql_subtype *tp = tail_type(last);
    3434             : 
    3435           0 :                 sql_subfunc *isnil = sql_bind_func(sql, "sys", "isnull", tp, NULL, F_FUNC, true, true);
    3436             : 
    3437           0 :                 stmt *s = stmt_unop(be, last, NULL, isnil);
    3438             : 
    3439           0 :                 sql_subtype *bt = sql_bind_localtype("bit");
    3440           0 :                 sql_subfunc *not = sql_bind_func(be->mvc, "sys", "not", bt, NULL, F_FUNC, true, true);
    3441             : 
    3442           0 :                 s = stmt_unop(be, s, NULL, not);
    3443           0 :                 s = stmt_alias(be, s, e->alias.label, rnme, nme);
    3444           0 :                 list_append(l, s);
    3445             :         }
    3446             : 
    3447       72848 :         res = stmt_list(be, l);
    3448       72848 :         res->extra = l2; /* used for merge statements, this will be cleaned out on the pushcands branch :) */
    3449       72848 :         return res;
    3450             : }
    3451             : 
    3452             : static stmt *
    3453        1498 : rel2bin_antijoin(backend *be, sql_rel *rel, list *refs)
    3454             : {
    3455        1498 :         mvc *sql = be->mvc;
    3456        1498 :         list *l, *jexps = NULL, *sexps = NULL;
    3457        1498 :         node *en = NULL, *n;
    3458        1498 :         stmt *left = NULL, *right = NULL, *join = NULL, *sel = NULL, *sub = NULL;
    3459             : 
    3460        1498 :         if (rel->l) /* first construct the left sub relation */
    3461        1498 :                 left = subrel_bin(be, rel->l, refs);
    3462        1498 :         if (rel->r) /* first construct the right sub relation */
    3463        1498 :                 right = subrel_bin(be, rel->r, refs);
    3464        1498 :         left = subrel_project(be, left, refs, rel->l);
    3465        1498 :         right = subrel_project(be, right, refs, rel->r);
    3466        1498 :         if (!left || !right)
    3467             :                 return NULL;
    3468        1498 :         left = row2cols(be, left);
    3469        1498 :         right = row2cols(be, right);
    3470             : 
    3471        1498 :         stmt *li = NULL;
    3472        1498 :         bool swap = false;
    3473             : 
    3474        1498 :         jexps = sa_list(sql->sa);
    3475        1498 :         sexps = sa_list(sql->sa);
    3476             : 
    3477        1498 :         split_join_exps(rel, jexps, sexps, true);
    3478        1498 :         if (list_empty(jexps)) {
    3479          16 :                 stmt *l = bin_find_smallest_column(be, left);
    3480          16 :                 stmt *r = bin_find_smallest_column(be, right);
    3481          16 :                 join = stmt_join(be, l, r, 0, cmp_all, 0, 0, false);
    3482             : 
    3483          16 :                 jexps = sexps;
    3484          16 :                 sexps = NULL;
    3485          16 :                 en = jexps->h;
    3486             :         } else {
    3487        1482 :                 if (list_length(sexps))
    3488           4 :                         list_merge(jexps, sexps, NULL);
    3489        1482 :                 en = jexps->h;
    3490        1482 :                 sql_exp *e = en->data;
    3491        1482 :                 assert(e->type == e_cmp);
    3492        1482 :                 stmt *ls = exp_bin(be, e->l, left, NULL, NULL, NULL, NULL, NULL, 1, 0, 0), *rs;
    3493        1482 :                 bool constval = false;
    3494        1482 :                 if (!ls) {
    3495          12 :                         swap = true;
    3496          12 :                         ls = exp_bin(be, e->l, right, NULL, NULL, NULL, NULL, NULL, 1, 0, 0);
    3497             :                 }
    3498          12 :                 if (!ls)
    3499             :                         return NULL;
    3500             : 
    3501        1482 :                 if (!(rs = exp_bin(be, e->r, left, right, NULL, NULL, NULL, NULL, 1, 0, 0)))
    3502             :                         return NULL;
    3503             : 
    3504        1482 :                 if (swap) {
    3505          12 :                         stmt *t = ls;
    3506          12 :                         ls = rs;
    3507          12 :                         rs = t;
    3508             :                 }
    3509        1482 :                 if (ls->nrcols == 0) {
    3510          10 :                         constval = true;
    3511          10 :                         ls = stmt_const(be, bin_find_smallest_column(be, left), ls);
    3512             :                 }
    3513        1482 :                 if (rs->nrcols == 0)
    3514           3 :                         rs = stmt_const(be, bin_find_smallest_column(be, right), rs);
    3515             : 
    3516        1482 :                 if (!li)
    3517        1482 :                         li = ls;
    3518             : 
    3519        1482 :                 if (!en->next && (constval || stmt_has_null(ls) /*|| stmt_has_null(rs) (change into check for fk)*/)) {
    3520        1262 :                         assert(e->flag == cmp_equal);
    3521        1262 :                         join = stmt_tdiff2(be, ls, rs, NULL);
    3522        1262 :                         jexps = NULL;
    3523             :                 } else {
    3524         220 :                         join = stmt_join_cand(be, ls, rs, NULL, NULL, is_anti(e), (comp_type) e->flag, 0, is_semantics(e), false, true);
    3525             :                 }
    3526        1482 :                 en = en->next;
    3527             :         }
    3528        1498 :         if (en || jexps) {
    3529         236 :                 stmt *jl = stmt_result(be, join, 0);
    3530         236 :                 stmt *jr = stmt_result(be, join, 1);
    3531         236 :                 stmt *nulls = NULL;
    3532             : 
    3533         236 :                 if (li && stmt_has_null(li)) {
    3534         123 :                         nulls = stmt_selectnil(be, li);
    3535             :                 }
    3536             :                 /* construct relation */
    3537         236 :                 list *nl = sa_list(sql->sa);
    3538             :                 /* first project after equi-joins */
    3539        2259 :                 for (n = left->op4.lval->h; n; n = n->next) {
    3540        2023 :                         stmt *c = n->data;
    3541        2023 :                         assert(c->type == st_alias);
    3542        2023 :                         const char *rnme = table_name(sql->sa, c);
    3543        2023 :                         const char *nme = column_name(sql->sa, c);
    3544        2023 :                         stmt *s = stmt_project(be, jl, column(be, c));
    3545             : 
    3546        2023 :                         s = stmt_alias(be, s, c->label, rnme, nme);
    3547        2023 :                         list_append(nl, s);
    3548             :                 }
    3549         679 :                 for (n = right->op4.lval->h; n; n = n->next) {
    3550         443 :                         stmt *c = n->data;
    3551         443 :                         assert(c->type == st_alias);
    3552         443 :                         const char *rnme = table_name(sql->sa, c);
    3553         443 :                         const char *nme = column_name(sql->sa, c);
    3554         443 :                         stmt *s = stmt_project(be, jr, column(be, c));
    3555             : 
    3556         443 :                         s = stmt_alias(be, s, c->label, rnme, nme);
    3557         443 :                         list_append(nl, s);
    3558             :                 }
    3559         236 :                 sub = stmt_list(be, nl);
    3560             : 
    3561             :                 /* continue with non equi-joins */
    3562         682 :                 for (; en; en = en->next) {
    3563         210 :                         stmt *s = exp_bin(be, en->data, sub, NULL, NULL, NULL, NULL, NULL /* sel */, 0, 0/* just the project call not the select*/, 0);
    3564             : 
    3565             :                         /* ifthenelse if (not(predicate)) then false else true (needed for antijoin) */
    3566         210 :                         sql_subtype *bt = sql_bind_localtype("bit");
    3567         210 :                         sql_subfunc *not = sql_bind_func(be->mvc, "sys", "not", bt, NULL, F_FUNC, true, true);
    3568         210 :                         s = stmt_unop(be, s, NULL, not);
    3569         210 :                         s = sql_Nop_(be, "ifthenelse", s, stmt_bool(be, 0), stmt_bool(be, 1), NULL);
    3570             : 
    3571         210 :                         if (s->nrcols == 0) {
    3572           0 :                                 stmt *l = bin_find_smallest_column(be, sub);
    3573           0 :                                 s = stmt_uselect(be, stmt_const(be, l, s), stmt_bool(be, 1), cmp_equal, sel, 0, 0);
    3574             :                         } else {
    3575         210 :                                 s = stmt_uselect(be, s, stmt_bool(be, 1), cmp_equal, sel, 0, 0);
    3576             :                         }
    3577             : 
    3578         210 :                         if (!s) {
    3579           0 :                                 assert(sql->session->status == -10); /* Stack overflow errors shouldn't terminate the server */
    3580             :                                 return NULL;
    3581             :                         }
    3582             : 
    3583         210 :                         sel = s;
    3584             :                 }
    3585         236 :                 stmt *c = stmt_mirror(be, bin_find_smallest_column(be, left));
    3586         236 :                 if (nulls) {
    3587         123 :                         stmt *nonilcand = stmt_tdiff(be, c, nulls, NULL);
    3588         123 :                         c = stmt_project(be, nonilcand, c);
    3589             :                 }
    3590         236 :                 if (join && sel) {
    3591             :                         /* recreate join output */
    3592         153 :                         jl = stmt_project(be, sel, jl);
    3593         153 :                         join = stmt_tdiff(be, c, jl, NULL);
    3594             :                 } else {
    3595          83 :                         join = stmt_tdiff2(be, c, jl, NULL);
    3596             :                 }
    3597         236 :                 if (nulls)
    3598         123 :                         join = stmt_project(be, join, c);
    3599             : 
    3600             :         } else if (jexps && list_empty(jexps)) {
    3601             :                 stmt *jl = stmt_result(be, join, 0);
    3602             :                 stmt *c = stmt_mirror(be, bin_find_smallest_column(be, left));
    3603             :                 join = stmt_tdiff2(be, c, jl, NULL);
    3604             :         }
    3605             : 
    3606             :         /* construct relation */
    3607        1498 :         l = sa_list(sql->sa);
    3608             : 
    3609             :         /* project all the left columns */
    3610       12836 :         for (n = left->op4.lval->h; n; n = n->next) {
    3611       11338 :                 stmt *c = n->data;
    3612       11338 :                 assert(c->type == st_alias);
    3613       11338 :                 const char *rnme = table_name(sql->sa, c);
    3614       11338 :                 const char *nme = column_name(sql->sa, c);
    3615       11338 :                 stmt *s = stmt_project(be, join, column(be, c));
    3616             : 
    3617       11338 :                 s = stmt_alias(be, s, c->label, rnme, nme);
    3618       11338 :                 list_append(l, s);
    3619             :         }
    3620        1498 :         return stmt_list(be, l);
    3621             : }
    3622             : 
    3623             : static stmt *
    3624        3112 : rel2bin_semijoin(backend *be, sql_rel *rel, list *refs)
    3625             : {
    3626        3112 :         mvc *sql = be->mvc;
    3627        3112 :         list *l, *sexps = NULL;
    3628        3112 :         node *en = NULL, *n;
    3629        3112 :         stmt *left = NULL, *right = NULL, *join = NULL, *jl, *jr, *c, *lcand = NULL;
    3630        3112 :         int semijoin_only = 0, l_is_base = 0;
    3631             : 
    3632        3112 :         assert(rel->op != op_anti);
    3633             : 
    3634        3112 :         if (rel->l) { /* first construct the left sub relation */
    3635        3112 :                 sql_rel *l = rel->l;
    3636        3112 :                 l_is_base = is_basetable(l->op);
    3637        3112 :                 left = subrel_bin(be, l, refs);
    3638             :         }
    3639        3112 :         if (rel->r) /* first construct the right sub relation */
    3640        3112 :                 right = subrel_bin(be, rel->r, refs);
    3641        3112 :         if (!left || !right)
    3642             :                 return NULL;
    3643        3112 :         left = row2cols(be, left);
    3644        3112 :         right = row2cols(be, right);
    3645             :         /*
    3646             :          * split in 2 steps,
    3647             :          *      first cheap join(s) (equality or idx)
    3648             :          *      second selects/filters
    3649             :          */
    3650        3112 :         if (!list_empty(rel->exps)) {
    3651        3079 :                 list *jexps = sa_list(sql->sa);
    3652        3079 :                 sexps = sa_list(sql->sa);
    3653             : 
    3654        3079 :                 split_join_exps(rel, jexps, sexps, false);
    3655        3079 :                 if (list_empty(jexps)) { /* cross product and continue after project */
    3656          54 :                         right = subrel_project(be, right, refs, rel->r);
    3657          54 :                         stmt *l = bin_find_smallest_column(be, left);
    3658          54 :                         stmt *r = bin_find_smallest_column(be, right);
    3659          54 :                         join = stmt_join(be, l, r, 0, cmp_all, 0, 0, false);
    3660          54 :                         lcand = left->cand;
    3661             :                 }
    3662             : 
    3663        3079 :                 if (join) {
    3664          54 :                         en = jexps->h;
    3665             :                 } else {
    3666        3025 :                         list *lje = sa_list(sql->sa), *rje = sa_list(sql->sa), *exps = sa_list(sql->sa);
    3667        3025 :                         int idx = 0, equality_only = 1;
    3668             : 
    3669        3025 :                         jexps = get_equi_joins_first(sql, jexps, &equality_only);
    3670        3025 :                         if (!equality_only || list_length(jexps) > 1 || exp_has_func((sql_exp*)jexps->h->data))
    3671        1083 :                                 left = subrel_project(be, left, refs, rel->l);
    3672        3025 :                         right = subrel_project(be, right, refs, rel->r);
    3673             : 
    3674        6763 :                         for (en = jexps->h; en; en = en->next) {
    3675        3751 :                                 int join_idx = be->join_idx;
    3676        3751 :                                 sql_exp *e = en->data;
    3677        3751 :                                 stmt *s = NULL;
    3678             : 
    3679             :                                 /* only handle simple joins here */
    3680        3751 :                                 if ((exp_has_func(e) && e->flag != cmp_filter) || e->flag == cmp_or || (e->f && is_anti(e))) {
    3681           3 :                                         if (!join && !list_length(lje)) {
    3682           3 :                                                 stmt *l = bin_find_smallest_column(be, left);
    3683           3 :                                                 stmt *r = bin_find_smallest_column(be, right);
    3684           3 :                                                 join = stmt_join(be, l, r, 0, cmp_all, 0, 0, false);
    3685             :                                         }
    3686             :                                         break;
    3687             :                                 }
    3688        3748 :                                 if (list_length(lje) && (idx || e->type != e_cmp || (e->flag != cmp_equal && e->flag != cmp_filter) ||
    3689         716 :                                 (join && e->flag == cmp_filter)))
    3690             :                                         break;
    3691             : 
    3692        3738 :                                 if (equality_only) {
    3693        3269 :                                         int oldvtop = be->mb->vtop, oldstop = be->mb->stop, swap = 0;
    3694        3269 :                                         stmt *r, *l = exp_bin(be, e->l, left, NULL, NULL, NULL, NULL, NULL, 1, 0, 0);
    3695             : 
    3696        3269 :                                         if (l && left && l->nrcols==0 && left->nrcols >0)
    3697           1 :                                                 l = stmt_const(be, bin_find_smallest_column(be, left), l);
    3698         411 :                                         if (!l) {
    3699         410 :                                                 swap = 1;
    3700         410 :                                                 clean_mal_statements(be, oldstop, oldvtop);
    3701         410 :                                                 l = exp_bin(be, e->l, right, NULL, NULL, NULL, NULL, NULL, 1, 0, 0);
    3702             :                                         }
    3703        3269 :                                         r = exp_bin(be, e->r, left, right, NULL, NULL, NULL, NULL, 1, 0, 0);
    3704             : 
    3705        3269 :                                         if (swap) {
    3706         410 :                                                 stmt *t = l;
    3707         410 :                                                 l = r;
    3708         410 :                                                 r = t;
    3709             :                                         }
    3710             : 
    3711        3269 :                                         if (!l || !r)
    3712           0 :                                                 return NULL;
    3713        3269 :                                         if (be->no_mitosis && list_length(jexps) == 1 && list_empty(sexps) && rel->op == op_semi && !is_anti(e) && is_equi_exp_(e)) {
    3714           0 :                                                 join = stmt_semijoin(be, column(be, l), column(be, r), left->cand, NULL/*right->cand*/, is_semantics(e), false);
    3715           0 :                                                 semijoin_only = 1;
    3716           0 :                                                 en = NULL;
    3717           0 :                                                 break;
    3718             :                                         } else
    3719        3269 :                                                 s = stmt_join_cand(be, column(be, l), column(be, r), left->cand, NULL/*right->cand*/, is_anti(e), (comp_type) e->flag, 0, is_semantics(e), false, true);
    3720        3269 :                                         lcand = left->cand;
    3721             :                                 } else {
    3722         469 :                                         s = exp_bin(be, e, left, right, NULL, NULL, NULL, NULL, 0, 1, 0);
    3723             :                                 }
    3724        3738 :                                 if (!s) {
    3725           0 :                                         assert(sql->session->status == -10); /* Stack overflow errors shouldn't terminate the server */
    3726             :                                         return NULL;
    3727             :                                 }
    3728        3738 :                                 if (join_idx != be->join_idx)
    3729           0 :                                         idx = 1;
    3730             :                                 /* stop on first non equality join */
    3731        3738 :                                 if (!join) {
    3732        3022 :                                         if (s->type != st_join && s->type != st_join2 && s->type != st_joinN) {
    3733           0 :                                                 if (!en->next && (s->type == st_uselect || s->type == st_uselect2))
    3734             :                                                         join = s;
    3735             :                                                 else
    3736             :                                                         break;
    3737             :                                         }
    3738             :                                         join = s;
    3739         716 :                                 } else if (s->type != st_join && s->type != st_join2 && s->type != st_joinN) {
    3740             :                                         /* handle select expressions */
    3741             :                                         break;
    3742             :                                 }
    3743        3738 :                                 if (s->type == st_join || s->type == st_join2 || s->type == st_joinN) {
    3744        3738 :                                         list_append(lje, s->op1);
    3745        3738 :                                         list_append(rje, s->op2);
    3746        3738 :                                         list_append(exps, e);
    3747             :                                 }
    3748             :                         }
    3749        3025 :                         if (list_length(lje) > 1) {
    3750         613 :                                 join = releqjoin(be, lje, rje, exps, 0 /* use hash */, 0, rel->op == op_anti?1:0);
    3751        2412 :                         } else if (!join && list_length(lje) == list_length(rje) && list_length(lje)) {
    3752           0 :                                 sql_exp *e = exps->h->data;
    3753           0 :                                 join = stmt_join(be, lje->h->data, rje->h->data, 0, cmp_equal, 0, is_semantics(e), false);
    3754           0 :                         } else if (!join) {
    3755           0 :                                 stmt *l = bin_find_smallest_column(be, left);
    3756           0 :                                 stmt *r = bin_find_smallest_column(be, right);
    3757           0 :                                 join = stmt_join(be, l, r, 0, cmp_all, 0, 0, false);
    3758             :                         }
    3759             :                 }
    3760             :         } else {
    3761          33 :                 right = subrel_project(be, right, refs, rel->r);
    3762          33 :                 stmt *l = bin_find_smallest_column(be, left);
    3763          33 :                 stmt *r = bin_find_smallest_column(be, right);
    3764          33 :                 join = stmt_join(be, l, r, 0, cmp_all, 0, 0, false);
    3765          33 :                 lcand = left->cand;
    3766             :         }
    3767        3112 :         jl = stmt_result(be, join, 0);
    3768        3112 :         if (en || (sexps && list_length(sexps))) {
    3769          88 :                 stmt *sub, *sel = NULL;
    3770          88 :                 list *nl;
    3771             : 
    3772          88 :                 jr = stmt_result(be, join, 1);
    3773             :                 /* construct relation */
    3774          88 :                 nl = sa_list(sql->sa);
    3775             : 
    3776             :                 /* first project after equi-joins */
    3777         432 :                 for (n = left->op4.lval->h; n; n = n->next) {
    3778         344 :                         stmt *c = n->data;
    3779         344 :                         assert(c->type == st_alias);
    3780         344 :                         const char *rnme = table_name(sql->sa, c);
    3781         344 :                         const char *nme = column_name(sql->sa, c);
    3782         344 :                         stmt *s = stmt_project(be, jl, column(be, c));
    3783             : 
    3784         344 :                         s = stmt_alias(be, s, c->label, rnme, nme);
    3785         344 :                         list_append(nl, s);
    3786             :                 }
    3787         247 :                 for (n = right->op4.lval->h; n; n = n->next) {
    3788         159 :                         stmt *c = n->data;
    3789         159 :                         assert(c->type == st_alias);
    3790         159 :                         const char *rnme = table_name(sql->sa, c);
    3791         159 :                         const char *nme = column_name(sql->sa, c);
    3792         159 :                         stmt *s = stmt_project(be, jr, column(be, c));
    3793             : 
    3794         159 :                         s = stmt_alias(be, s, c->label, rnme, nme);
    3795         159 :                         list_append(nl, s);
    3796             :                 }
    3797          88 :                 sub = stmt_list(be, nl);
    3798             : 
    3799             :                 /* continue with non equi-joins */
    3800         189 :                 while(sexps) {
    3801         101 :                         if (!en) {
    3802          88 :                                 en = sexps->h;
    3803          88 :                                 sexps = NULL;
    3804             :                         }
    3805         199 :                         for ( ; en; en = en->next) {
    3806          98 :                                 stmt *s = exp_bin(be, en->data, sub, NULL, NULL, NULL, NULL, sel, 0, 1, 0);
    3807             : 
    3808          98 :                                 if (!s) {
    3809           0 :                                         assert(sql->session->status == -10); /* Stack overflow errors shouldn't terminate the server */
    3810             :                                         return NULL;
    3811             :                                 }
    3812          98 :                                 if (s->nrcols == 0) {
    3813           2 :                                         stmt *l = bin_find_smallest_column(be, sub);
    3814           2 :                                         s = stmt_uselect(be, stmt_const(be, l, stmt_bool(be, 1)), s, cmp_equal, sel, 0, 0);
    3815             :                                 }
    3816          98 :                                 sel = s;
    3817             :                         }
    3818             :                 }
    3819             :                 /* recreate join output */
    3820          88 :                 jl = stmt_project(be, sel, jl);
    3821             :         }
    3822             : 
    3823             :         /* construct relation */
    3824        3112 :         l = sa_list(sql->sa);
    3825             : 
    3826             :         /* We did a full join, that's too much.
    3827             :            Reduce this using difference and intersect */
    3828        3112 :         if (!semijoin_only) {
    3829        3112 :                 c = stmt_mirror(be, bin_find_smallest_column(be, left));
    3830        3112 :                 if (rel->op == op_anti) {
    3831           0 :                         assert(0);
    3832             :                         join = stmt_tdiff(be, c, jl, lcand);
    3833             :                 } else {
    3834        3112 :                         if (lcand)
    3835        1107 :                                 join = stmt_semijoin(be, c, jl, lcand, NULL/*right->cand*/, 0, false);
    3836             :                         else
    3837        2005 :                                 join = stmt_tinter(be, c, jl, false);
    3838             :                 }
    3839             :         }
    3840             : 
    3841             :         /* project all the left columns */
    3842       24051 :         for (n = left->op4.lval->h; n; n = n->next) {
    3843       20939 :                 stmt *c = n->data, *s;
    3844       20939 :                 const char *rnme = table_name(sql->sa, c);
    3845       20939 :                 const char *nme = column_name(sql->sa, c);
    3846             : 
    3847       20939 :                 if (semijoin_only && l_is_base && nme[0] == '%' && strcmp(nme, TID) == 0)
    3848             :                         s = join;
    3849             :                 else
    3850       20939 :                         s = stmt_project(be, join, column(be, c));
    3851             : 
    3852       20939 :                 s = stmt_alias(be, s, c->label, rnme, nme);
    3853       20939 :                 list_append(l, s);
    3854             :         }
    3855        3112 :         return stmt_list(be, l);
    3856             : }
    3857             : 
    3858             : static stmt *
    3859        1685 : rel2bin_distinct(backend *be, stmt *s, stmt **distinct)
    3860             : {
    3861        1685 :         mvc *sql = be->mvc;
    3862        1685 :         node *n;
    3863        1685 :         stmt *g = NULL, *grp = NULL, *ext = NULL, *cnt = NULL;
    3864        1685 :         list *rl = sa_list(sql->sa), *tids;
    3865             : 
    3866             :         /* single values are unique */
    3867        1685 :         if (s->key && s->nrcols == 0)
    3868             :                 return s;
    3869             : 
    3870             :         /* Use 'all' tid columns */
    3871        1676 :         if (/* DISABLES CODE */ (0) && (tids = bin_find_columns(be, s, TID)) != NULL) {
    3872             :                 for (n = tids->h; n; n = n->next) {
    3873             :                         stmt *t = n->data;
    3874             : 
    3875             :                         g = stmt_group(be, column(be, t), grp, ext, cnt, !n->next);
    3876             :                         grp = stmt_result(be, g, 0);
    3877             :                         ext = stmt_result(be, g, 1);
    3878             :                         cnt = stmt_result(be, g, 2);
    3879             :                 }
    3880             :         } else {
    3881        3690 :                 for (n = s->op4.lval->h; n; n = n->next) {
    3882        2014 :                         stmt *t = n->data;
    3883             : 
    3884        2014 :                         g = stmt_group(be, column(be, t), grp, ext, cnt, !n->next);
    3885        2014 :                         grp = stmt_result(be, g, 0);
    3886        2014 :                         ext = stmt_result(be, g, 1);
    3887        2014 :                         cnt = stmt_result(be, g, 2);
    3888             :                 }
    3889             :         }
    3890        1676 :         if (!ext)
    3891             :                 return NULL;
    3892             : 
    3893        3690 :         for (n = s->op4.lval->h; n; n = n->next) {
    3894        2014 :                 stmt *t = n->data;
    3895             : 
    3896        2014 :                 stmt *s = stmt_project(be, ext, t);
    3897        2014 :                 t = stmt_alias(be, s, t->label, table_name(sql->sa, t), column_name(sql->sa, t));
    3898        2014 :                 list_append(rl, t);
    3899             :         }
    3900             : 
    3901        1676 :         if (distinct)
    3902           5 :                 *distinct = ext;
    3903        1676 :         s = stmt_list(be, rl);
    3904        1676 :         return s;
    3905             : }
    3906             : 
    3907             : static stmt *
    3908          11 : rel2bin_single(backend *be, stmt *s)
    3909             : {
    3910          11 :         if (s->key && s->nrcols == 0)
    3911             :                 return s;
    3912             : 
    3913          11 :         mvc *sql = be->mvc;
    3914          11 :         list *rl = sa_list(sql->sa);
    3915             : 
    3916          25 :         for (node *n = s->op4.lval->h; n; n = n->next) {
    3917          14 :                 stmt *t = n->data;
    3918          14 :                 assert(t->type == st_alias);
    3919          14 :                 const char *rnme = table_name(sql->sa, t);
    3920          14 :                 const char *nme = column_name(sql->sa, t);
    3921          14 :                 int label = t->label;
    3922          14 :                 sql_subfunc *zero_or_one = sql_bind_func(sql, "sys", "zero_or_one", tail_type(t), NULL, F_AGGR, true, true);
    3923             : 
    3924          14 :                 t = stmt_aggr(be, t, NULL, NULL, zero_or_one, 1, 0, 1);
    3925          14 :                 t = stmt_alias(be, t, label, rnme, nme);
    3926          14 :                 list_append(rl, t);
    3927             :         }
    3928          11 :         s = stmt_list(be, rl);
    3929          11 :         return s;
    3930             : }
    3931             : 
    3932             : static stmt *
    3933        6905 : rel_rename(backend *be, sql_rel *rel, stmt *sub)
    3934             : {
    3935        6905 :         mvc *sql = be->mvc;
    3936             : 
    3937        6905 :         (void) sql;
    3938        6905 :         if (rel->exps) {
    3939        6905 :                 node *en, *n;
    3940        6905 :                 list *l = sa_list(be->mvc->sa);
    3941             : 
    3942       31522 :                 for (en = rel->exps->h, n = sub->op4.lval->h; en && n; en = en->next, n = n->next) {
    3943       24617 :                         sql_exp *exp = en->data;
    3944       24617 :                         stmt *s = n->data;
    3945             : 
    3946       24617 :                         if (!s) {
    3947           0 :                                 assert(sql->session->status == -10); /* Stack overflow errors shouldn't terminate the server */
    3948             :                                 return NULL;
    3949             :                         }
    3950       24617 :                         s = stmt_rename(be, exp, s);
    3951       24617 :                         list_append(l, s);
    3952             :                 }
    3953        6905 :                 sub = stmt_list(be, l);
    3954             :         }
    3955             :         return sub;
    3956             : }
    3957             : 
    3958             : static stmt *
    3959        4207 : rel2bin_munion(backend *be, sql_rel *rel, list *refs)
    3960             : {
    3961        4207 :         mvc *sql = be->mvc;
    3962        4207 :         list *l, *rstmts;
    3963        4207 :         node *n, *m;
    3964        4207 :         stmt *rel_stmt = NULL, *sub;
    3965        4207 :         int i, len = 0, nr_unions = list_length((list*)rel->l);
    3966             : 
    3967             :         /* convert to stmt and store the munion operands in rstmts list */
    3968        4207 :         rstmts = sa_list(sql->sa);
    3969       15511 :         for (n = ((list*)rel->l)->h; n; n = n->next) {
    3970       11304 :                 rel_stmt = subrel_bin(be, n->data, refs);
    3971       11304 :                 rel_stmt = subrel_project(be, rel_stmt, refs, n->data);
    3972       11304 :                 if (!rel_stmt)
    3973             :                         return NULL;
    3974       11304 :                 list_append(rstmts, rel_stmt);
    3975       11304 :                 if (!len || len > list_length(rel_stmt->op4.lval))
    3976        4207 :                         len = list_length(rel_stmt->op4.lval);
    3977             :         }
    3978             : 
    3979             :         /* construct relation */
    3980        4207 :         l = sa_list(sql->sa);
    3981             : 
    3982             :         /* for every op4 lval node */
    3983             :         //len = list_length(((stmt*)rstmts->h->data)->op4.lval);
    3984       24335 :         for (i = 0; i < len; i++) {
    3985             :                 /* extract t and c name from the first stmt */
    3986       15921 :                 stmt *s = list_fetch(((stmt*)rstmts->h->data)->op4.lval, i);
    3987       15921 :                 if (s == NULL)
    3988             :                         return NULL;
    3989       15921 :                 const char *rnme = table_name(sql->sa, s);
    3990       15921 :                 const char *nme = column_name(sql->sa, s);
    3991       15921 :                 int label = s->label;
    3992             :                 /* create a const column also from the first stmt */
    3993       15921 :                 s = stmt_pack(be, column(be, s), nr_unions);
    3994             :                 /* for every other rstmt */
    3995       45562 :                 for (m = rstmts->h->next; m; m = m->next) {
    3996       29641 :                         stmt *t = list_fetch(((stmt*)m->data)->op4.lval, i);
    3997       29641 :                         if (t == NULL)
    3998             :                                 return NULL;
    3999       29641 :                         s = stmt_pack_add(be, s, column(be, t));
    4000       29641 :                         if (s == NULL)
    4001             :                                 return NULL;
    4002             :                 }
    4003       15921 :                 s = stmt_alias(be, s, label, rnme, nme);
    4004       15921 :                 if (s == NULL)
    4005             :                         return NULL;
    4006       15921 :                 list_append(l, s);
    4007             :         }
    4008        4207 :         sub = stmt_list(be, l);
    4009             : 
    4010        4207 :         sub = rel_rename(be, rel, sub);
    4011        4207 :         if (need_distinct(rel))
    4012        1671 :                 sub = rel2bin_distinct(be, sub, NULL);
    4013        4207 :         if (is_single(rel))
    4014          11 :                 sub = rel2bin_single(be, sub);
    4015             :         return sub;
    4016             : }
    4017             : 
    4018             : static stmt *
    4019         301 : rel2bin_union(backend *be, sql_rel *rel, list *refs)
    4020             : {
    4021         301 :         mvc *sql = be->mvc;
    4022         301 :         list *l;
    4023         301 :         node *n, *m;
    4024         301 :         stmt *left = NULL, *right = NULL, *sub;
    4025             : 
    4026         301 :         if (rel->l) /* first construct the left sub relation */
    4027         301 :                 left = subrel_bin(be, rel->l, refs);
    4028         301 :         if (rel->r) /* first construct the right sub relation */
    4029         301 :                 right = subrel_bin(be, rel->r, refs);
    4030         301 :         left = subrel_project(be, left, refs, rel->l);
    4031         301 :         right = subrel_project(be, right, refs, rel->r);
    4032         301 :         if (!left || !right)
    4033             :                 return NULL;
    4034             : 
    4035             :         /* construct relation */
    4036         301 :         l = sa_list(sql->sa);
    4037        6323 :         for (n = left->op4.lval->h, m = right->op4.lval->h; n && m;
    4038        6022 :                  n = n->next, m = m->next) {
    4039        6022 :                 stmt *c1 = n->data;
    4040        6022 :                 assert(c1->type == st_alias);
    4041        6022 :                 stmt *c2 = m->data;
    4042        6022 :                 const char *rnme = table_name(sql->sa, c1);
    4043        6022 :                 const char *nme = column_name(sql->sa, c1);
    4044        6022 :                 stmt *s;
    4045             : 
    4046        6022 :                 s = stmt_append(be, create_const_column(be, c1), c2);
    4047        6022 :                 if (s == NULL)
    4048             :                         return NULL;
    4049        6022 :                 s = stmt_alias(be, s, c1->label, rnme, nme);
    4050        6022 :                 if (s == NULL)
    4051             :                         return NULL;
    4052        6022 :                 list_append(l, s);
    4053             :         }
    4054         301 :         sub = stmt_list(be, l);
    4055             : 
    4056         301 :         sub = rel_rename(be, rel, sub);
    4057         301 :         if (need_distinct(rel))
    4058           0 :                 sub = rel2bin_distinct(be, sub, NULL);
    4059         301 :         if (is_single(rel))
    4060           0 :                 sub = rel2bin_single(be, sub);
    4061             :         return sub;
    4062             : }
    4063             : 
    4064             : static stmt *
    4065        2022 : rel2bin_except(backend *be, sql_rel *rel, list *refs)
    4066             : {
    4067        2022 :         mvc *sql = be->mvc;
    4068        2022 :         sql_subtype *lng = sql_bind_localtype("lng");
    4069        2022 :         list *stmts;
    4070        2022 :         node *n, *m;
    4071        2022 :         stmt *left = NULL, *right = NULL, *sub;
    4072        2022 :         sql_subfunc *min;
    4073             : 
    4074        2022 :         stmt *lg = NULL, *rg = NULL;
    4075        2022 :         stmt *lgrp = NULL, *rgrp = NULL;
    4076        2022 :         stmt *lext = NULL, *rext = NULL, *next = NULL;
    4077        2022 :         stmt *lcnt = NULL, *rcnt = NULL, *ncnt = NULL, *zero = NULL;
    4078        2022 :         stmt *s, *lm, *rm;
    4079        2022 :         list *lje = sa_list(sql->sa);
    4080        2022 :         list *rje = sa_list(sql->sa);
    4081             : 
    4082        2022 :         if (rel->l) /* first construct the left sub relation */
    4083        2022 :                 left = subrel_bin(be, rel->l, refs);
    4084        2022 :         if (rel->r) /* first construct the right sub relation */
    4085        2022 :                 right = subrel_bin(be, rel->r, refs);
    4086        2022 :         if (!left || !right)
    4087             :                 return NULL;
    4088        2022 :         left = subrel_project(be, left, refs, rel->l);
    4089        2022 :         right = subrel_project(be, right, refs, rel->r);
    4090        2022 :         left = row2cols(be, left);
    4091        2022 :         right = row2cols(be, right);
    4092             : 
    4093             :         /*
    4094             :          * The multi column except is handled using group by's and
    4095             :          * group size counts on both sides of the intersect. We then
    4096             :          * return for each group of L with min(L.count,R.count),
    4097             :          * number of rows.
    4098             :          */
    4099        4233 :         for (n = left->op4.lval->h; n; n = n->next) {
    4100        2211 :                 lg = stmt_group(be, column(be, n->data), lgrp, lext, lcnt, !n->next);
    4101        2211 :                 lgrp = stmt_result(be, lg, 0);
    4102        2211 :                 lext = stmt_result(be, lg, 1);
    4103        2211 :                 lcnt = stmt_result(be, lg, 2);
    4104             :         }
    4105        4233 :         for (n = right->op4.lval->h; n; n = n->next) {
    4106        2211 :                 rg = stmt_group(be, column(be, n->data), rgrp, rext, rcnt, !n->next);
    4107        2211 :                 rgrp = stmt_result(be, rg, 0);
    4108        2211 :                 rext = stmt_result(be, rg, 1);
    4109        2211 :                 rcnt = stmt_result(be, rg, 2);
    4110             :         }
    4111             : 
    4112        2022 :         if (!lg || !rg)
    4113             :                 return NULL;
    4114             : 
    4115        2022 :         if (need_distinct(rel)) {
    4116        1940 :                 lcnt = stmt_const(be, lcnt, stmt_atom_lng(be, 1));
    4117        1940 :                 rcnt = stmt_const(be, rcnt, stmt_atom_lng(be, 1));
    4118             :         }
    4119             : 
    4120             :         /* now find the matching groups */
    4121        4233 :         for (n = left->op4.lval->h, m = right->op4.lval->h; n && m; n = n->next, m = m->next) {
    4122        2211 :                 stmt *l = column(be, n->data);
    4123        2211 :                 stmt *r = column(be, m->data);
    4124             : 
    4125        2211 :                 l = stmt_project(be, lext, l);
    4126        2211 :                 r = stmt_project(be, rext, r);
    4127        2211 :                 list_append(lje, l);
    4128        2211 :                 list_append(rje, r);
    4129             :         }
    4130        2022 :         s = releqjoin(be, lje, rje, NULL, 0 /* use hash */, 0, 1 /*is_semantics*/);
    4131        2022 :         lm = stmt_result(be, s, 0);
    4132        2022 :         rm = stmt_result(be, s, 1);
    4133             : 
    4134        2022 :         s = stmt_mirror(be, lext);
    4135        2022 :         s = stmt_tdiff(be, s, lm, NULL);
    4136             : 
    4137             :         /* first we find those missing in R */
    4138        2022 :         next = stmt_project(be, s, lext);
    4139        2022 :         ncnt = stmt_project(be, s, lcnt);
    4140        2022 :         zero = stmt_const(be, s, stmt_atom_lng(be, 0));
    4141             : 
    4142             :         /* ext, lcount, rcount */
    4143        2022 :         lext = stmt_project(be, lm, lext);
    4144        2022 :         lcnt = stmt_project(be, lm, lcnt);
    4145        2022 :         rcnt = stmt_project(be, rm, rcnt);
    4146             : 
    4147             :         /* append those missing in L */
    4148        2022 :         lext = stmt_append(be, lext, next);
    4149        2022 :         lcnt = stmt_append(be, lcnt, ncnt);
    4150        2022 :         rcnt = stmt_append(be, rcnt, zero);
    4151             : 
    4152        2022 :         min = sql_bind_func_result(sql, "sys", "sql_sub", F_FUNC, true, lng, 2, lng, lng);
    4153        2022 :         s = stmt_binop(be, lcnt, rcnt, NULL, min); /* use count */
    4154             : 
    4155             :         /* now we have gid,cnt, blowup to full groupsizes */
    4156        2022 :         s = stmt_gen_group(be, lext, s);
    4157             : 
    4158             :         /* project columns of left hand expression */
    4159        2022 :         stmts = sa_list(sql->sa);
    4160        4233 :         for (n = left->op4.lval->h; n; n = n->next) {
    4161        2211 :                 stmt *c1 = column(be, n->data);
    4162        2211 :                 assert(c1->type == st_alias);
    4163        2211 :                 const char *rnme = NULL;
    4164        2211 :                 const char *nme = column_name(sql->sa, c1);
    4165        2211 :                 int label = c1->label;
    4166             : 
    4167             :                 /* retain name via the stmt_alias */
    4168        2211 :                 c1 = stmt_project(be, s, c1);
    4169             : 
    4170        2211 :                 rnme = table_name(sql->sa, c1);
    4171        2211 :                 c1 = stmt_alias(be, c1, label, rnme, nme);
    4172        2211 :                 list_append(stmts, c1);
    4173             :         }
    4174        2022 :         sub = stmt_list(be, stmts);
    4175        2022 :         return rel_rename(be, rel, sub);
    4176             : }
    4177             : 
    4178             : static stmt *
    4179         375 : rel2bin_inter(backend *be, sql_rel *rel, list *refs)
    4180             : {
    4181         375 :         mvc *sql = be->mvc;
    4182         375 :         sql_subtype *lng = sql_bind_localtype("lng");
    4183         375 :         list *stmts;
    4184         375 :         node *n, *m;
    4185         375 :         stmt *left = NULL, *right = NULL, *sub;
    4186         375 :         sql_subfunc *min;
    4187             : 
    4188         375 :         stmt *lg = NULL, *rg = NULL;
    4189         375 :         stmt *lgrp = NULL, *rgrp = NULL;
    4190         375 :         stmt *lext = NULL, *rext = NULL;
    4191         375 :         stmt *lcnt = NULL, *rcnt = NULL;
    4192         375 :         stmt *s, *lm, *rm;
    4193         375 :         list *lje = sa_list(sql->sa);
    4194         375 :         list *rje = sa_list(sql->sa);
    4195             : 
    4196         375 :         if (rel->l) /* first construct the left sub relation */
    4197         375 :                 left = subrel_bin(be, rel->l, refs);
    4198         375 :         if (rel->r) /* first construct the right sub relation */
    4199         375 :                 right = subrel_bin(be, rel->r, refs);
    4200         375 :         left = subrel_project(be, left, refs, rel->l);
    4201         375 :         right = subrel_project(be, right, refs, rel->r);
    4202         375 :         if (!left || !right)
    4203             :                 return NULL;
    4204         375 :         left = row2cols(be, left);
    4205             : 
    4206             :         /*
    4207             :          * The multi column intersect is handled using group by's and
    4208             :          * group size counts on both sides of the intersect. We then
    4209             :          * return for each group of L with min(L.count,R.count),
    4210             :          * number of rows.
    4211             :          */
    4212         839 :         for (n = left->op4.lval->h; n; n = n->next) {
    4213         464 :                 lg = stmt_group(be, column(be, n->data), lgrp, lext, lcnt, !n->next);
    4214         464 :                 lgrp = stmt_result(be, lg, 0);
    4215         464 :                 lext = stmt_result(be, lg, 1);
    4216         464 :                 lcnt = stmt_result(be, lg, 2);
    4217             :         }
    4218         839 :         for (n = right->op4.lval->h; n; n = n->next) {
    4219         464 :                 rg = stmt_group(be, column(be, n->data), rgrp, rext, rcnt, !n->next);
    4220         464 :                 rgrp = stmt_result(be, rg, 0);
    4221         464 :                 rext = stmt_result(be, rg, 1);
    4222         464 :                 rcnt = stmt_result(be, rg, 2);
    4223             :         }
    4224             : 
    4225         375 :         if (!lg || !rg)
    4226             :                 return NULL;
    4227             : 
    4228         375 :         if (need_distinct(rel)) {
    4229         364 :                 lcnt = stmt_const(be, lcnt, stmt_atom_lng(be, 1));
    4230         364 :                 rcnt = stmt_const(be, rcnt, stmt_atom_lng(be, 1));
    4231             :         }
    4232             : 
    4233             :         /* now find the matching groups */
    4234         839 :         for (n = left->op4.lval->h, m = right->op4.lval->h; n && m; n = n->next, m = m->next) {
    4235         464 :                 stmt *l = column(be, n->data);
    4236         464 :                 stmt *r = column(be, m->data);
    4237             : 
    4238         464 :                 l = stmt_project(be, lext, l);
    4239         464 :                 r = stmt_project(be, rext, r);
    4240         464 :                 list_append(lje, l);
    4241         464 :                 list_append(rje, r);
    4242             :         }
    4243         375 :         s = releqjoin(be, lje, rje, NULL, 0 /* use hash */, 0, 1 /* is_semantics */);
    4244         375 :         lm = stmt_result(be, s, 0);
    4245         375 :         rm = stmt_result(be, s, 1);
    4246             : 
    4247             :         /* ext, lcount, rcount */
    4248         375 :         lext = stmt_project(be, lm, lext);
    4249         375 :         lcnt = stmt_project(be, lm, lcnt);
    4250         375 :         rcnt = stmt_project(be, rm, rcnt);
    4251             : 
    4252         375 :         min = sql_bind_func(sql, "sys", "sql_min", lng, lng, F_FUNC, true, true);
    4253         375 :         s = stmt_binop(be, lcnt, rcnt, NULL, min);
    4254             : 
    4255             :         /* now we have gid,cnt, blowup to full groupsizes */
    4256         375 :         s = stmt_gen_group(be, lext, s);
    4257             : 
    4258             :         /* project columns of left hand expression */
    4259         375 :         stmts = sa_list(sql->sa);
    4260         839 :         for (n = left->op4.lval->h; n; n = n->next) {
    4261         464 :                 stmt *c1 = column(be, n->data);
    4262         464 :                 assert(c1->type == st_alias);
    4263         464 :                 const char *rnme = NULL;
    4264         464 :                 const char *nme = column_name(sql->sa, c1);
    4265         464 :                 int label = c1->label;
    4266             : 
    4267             :                 /* retain name via the stmt_alias */
    4268         464 :                 c1 = stmt_project(be, s, c1);
    4269             : 
    4270         464 :                 rnme = table_name(sql->sa, c1);
    4271         464 :                 c1 = stmt_alias(be, c1, label, rnme, nme);
    4272         464 :                 list_append(stmts, c1);
    4273             :         }
    4274         375 :         sub = stmt_list(be, stmts);
    4275         375 :         return rel_rename(be, rel, sub);
    4276             : }
    4277             : 
    4278             : static int
    4279       44870 : find_matching_exp(list *exps, sql_exp *e)
    4280             : {
    4281       44870 :         int i = 0;
    4282      157999 :         for (node *n = exps->h; n; n = n->next, i++) {
    4283      134989 :                 if (exp_match(n->data, e))
    4284       21860 :                         return i;
    4285             :         }
    4286             :         return -1;
    4287             : }
    4288             : 
    4289             : static stmt *
    4290       10277 : sql_reorder(backend *be, stmt *order, list *exps, stmt *s, list *oexps, list *ostmts)
    4291             : {
    4292       10277 :         list *l = sa_list(be->mvc->sa);
    4293             : 
    4294       55147 :         for (node *n = s->op4.lval->h, *m = exps->h; n && m; n = n->next, m = m->next) {
    4295       44870 :                 int pos = 0;
    4296       44870 :                 stmt *sc = n->data;
    4297       44870 :                 sql_exp *pe = m->data;
    4298       44870 :                 const char *cname = column_name(be->mvc->sa, sc);
    4299       44870 :                 const char *tname = table_name(be->mvc->sa, sc);
    4300             : 
    4301       44870 :                 if (oexps && (pos = find_matching_exp(oexps, pe)) >= 0 && list_fetch(ostmts, pos)) {
    4302       21860 :                         sc = list_fetch(ostmts, pos);
    4303             :                 } else {
    4304       23010 :                         sc = stmt_project(be, order, sc);
    4305             :                 }
    4306       44870 :                 sc = stmt_alias(be, sc, pe->alias.label, tname, cname);
    4307       44870 :                 list_append(l, sc);
    4308             :         }
    4309       10277 :         return stmt_list(be, l);
    4310             : }
    4311             : 
    4312             : static sql_exp*
    4313       17116 : topn_limit(sql_rel *rel)
    4314             : {
    4315       17116 :         if (rel->exps) {
    4316       17116 :                 sql_exp *limit = rel->exps->h->data;
    4317       17116 :                 if (exp_is_null(limit)) /* If the limit is NULL, ignore the value */
    4318             :                         return NULL;
    4319             :                 return limit;
    4320             :         }
    4321             :         return NULL;
    4322             : }
    4323             : 
    4324             : static sql_exp*
    4325       17116 : topn_offset(sql_rel *rel)
    4326             : {
    4327       17116 :         if (rel->exps && list_length(rel->exps) > 1) {
    4328         159 :                 sql_exp *offset = rel->exps->h->next->data;
    4329             : 
    4330         159 :                 return offset;
    4331             :         }
    4332             :         return NULL;
    4333             : }
    4334             : 
    4335             : static stmt *
    4336      393735 : rel2bin_project(backend *be, sql_rel *rel, list *refs, sql_rel *topn)
    4337             : {
    4338      393735 :         mvc *sql = be->mvc;
    4339      393735 :         list *pl;
    4340      393735 :         node *en, *n;
    4341      393735 :         stmt *sub = NULL, *psub = NULL;
    4342      393735 :         stmt *l = NULL;
    4343             : 
    4344      393735 :         if (topn) {
    4345         319 :                 sql_exp *le = topn_limit(topn);
    4346         319 :                 sql_exp *oe = topn_offset(topn);
    4347             : 
    4348         319 :                 if (!le) { /* Don't push only offset */
    4349             :                         topn = NULL;
    4350             :                 } else {
    4351         293 :                         l = exp_bin(be, le, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    4352         293 :                         if(!l)
    4353             :                                 return NULL;
    4354         293 :                         if (oe) {
    4355          44 :                                 sql_subtype *lng = sql_bind_localtype("lng");
    4356          44 :                                 sql_subfunc *add = sql_bind_func_result(sql, "sys", "sql_add", F_FUNC, true, lng, 2, lng, lng);
    4357          44 :                                 stmt *o = exp_bin(be, oe, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    4358          44 :                                 if(!o)
    4359             :                                         return NULL;
    4360          44 :                                 l = stmt_binop(be, l, o, NULL, add);
    4361             :                         }
    4362             :                 }
    4363             :         }
    4364             : 
    4365      393735 :         if (!rel->exps)
    4366           0 :                 return stmt_none(be);
    4367             : 
    4368      393735 :         if (rel->l) { /* first construct the sub relation */
    4369      220457 :                 sql_rel *l = rel->l;
    4370      220457 :                 if (l->op == op_ddl) {
    4371        1209 :                         sql_table *t = rel_ddl_table_get(l);
    4372             : 
    4373        1209 :                         if (t)
    4374        1209 :                                 sub = rel2bin_sql_table(be, t, rel->exps);
    4375             :                 } else {
    4376      219248 :                         sub = subrel_bin(be, rel->l, refs);
    4377             :                 }
    4378      220450 :                 sub = subrel_project(be, sub, refs, rel->l);
    4379      220455 :                 if (!sub)
    4380             :                         return NULL;
    4381             :         }
    4382             : 
    4383      393733 :         pl = sa_list(sql->sa);
    4384      393724 :         if (pl == NULL)
    4385             :                 return NULL;
    4386      393724 :         if (sub)
    4387      220446 :                 pl->expected_cnt = list_length(sub->op4.lval);
    4388      393721 :         psub = stmt_list(be, pl);
    4389      393717 :         if (psub == NULL)
    4390             :                 return NULL;
    4391     1960882 :         for (en = rel->exps->h; en; en = en->next) {
    4392     1567176 :                 sql_exp *exp = en->data;
    4393     1567176 :                 int oldvtop = be->mb->vtop, oldstop = be->mb->stop;
    4394     1567176 :                 stmt *s = exp_bin(be, exp, sub, NULL /*psub*/, NULL, NULL, NULL, NULL, 0, 0, 0);
    4395             : 
    4396     1567199 :                 if (!s) { /* try with own projection as well, but first clean leftover statements */
    4397       11274 :                         clean_mal_statements(be, oldstop, oldvtop);
    4398       11274 :                         s = exp_bin(be, exp, sub, psub, NULL, NULL, NULL, NULL, 0, 0, 0);
    4399             :                 }
    4400       11274 :                 if (!s) /* error */
    4401             :                         return NULL;
    4402             :                 /* single value with limit */
    4403     1567199 :                 if (topn && rel->r && sub && sub->nrcols == 0 && s->nrcols == 0)
    4404          14 :                         s = const_column(be, s);
    4405     1567185 :                 else if (sub && sub->nrcols >= 1 && s->nrcols == 0)
    4406       30813 :                         s = stmt_const(be, bin_find_smallest_column(be, sub), s);
    4407             : 
    4408     1567199 :                 if (!exp_name(exp))
    4409       80072 :                         exp_label(sql->sa, exp, ++sql->label);
    4410     1567203 :                 if (exp_name(exp)) {
    4411     1567196 :                         s = stmt_rename(be, exp, s);
    4412             :                         //column_name(sql->sa, s); /* save column name */
    4413     1567185 :                         s->label = exp->alias.label;
    4414             :                 }
    4415     1567185 :                 list_append(pl, s);
    4416             :         }
    4417      393706 :         stmt_set_nrcols(psub);
    4418             : 
    4419             :         /* In case of a topn
    4420             :                 if both order by and distinct: then get first order by col
    4421             :                 do topn on it. Project all again! Then rest
    4422             :         */
    4423      393716 :         if (topn && rel->r) {
    4424         274 :                 list *oexps = rel->r, *npl = sa_list(sql->sa);
    4425             :                 /* distinct, topn returns at least N (unique groups) */
    4426         274 :                 int distinct = need_distinct(rel);
    4427         274 :                 stmt *limit = NULL, *lpiv = NULL, *lgid = NULL;
    4428             : 
    4429         858 :                 for (n=oexps->h; n; n = n->next) {
    4430         584 :                         sql_exp *orderbycole = n->data;
    4431         584 :                         int last = (n->next == NULL);
    4432             : 
    4433         584 :                         stmt *orderbycolstmt = exp_bin(be, orderbycole, sub, psub, NULL, NULL, NULL, NULL, 0, 0, 0);
    4434             : 
    4435         584 :                         if (!orderbycolstmt)
    4436             :                                 return NULL;
    4437             : 
    4438             :                         /* handle constants */
    4439         584 :                         if (orderbycolstmt->nrcols == 0 && !last) /* no need to sort on constant */
    4440           0 :                                 continue;
    4441         584 :                         orderbycolstmt = column(be, orderbycolstmt);
    4442         584 :                         if (!limit) {   /* topn based on a single column */
    4443         274 :                                 limit = stmt_limit(be, orderbycolstmt, NULL, NULL, stmt_atom_lng(be, 0), l, distinct, is_ascending(orderbycole), nulls_last(orderbycole), last, 1);
    4444             :                         } else {        /* topn based on 2 columns */
    4445         310 :                                 limit = stmt_limit(be, orderbycolstmt, lpiv, lgid, stmt_atom_lng(be, 0), l, distinct, is_ascending(orderbycole), nulls_last(orderbycole), last, 1);
    4446             :                         }
    4447         584 :                         if (!limit)
    4448             :                                 return NULL;
    4449         584 :                         lpiv = limit;
    4450         584 :                         if (!last) {
    4451         310 :                                 lpiv = stmt_result(be, limit, 0);
    4452         310 :                                 lgid = stmt_result(be, limit, 1);
    4453         310 :                                 if (lpiv == NULL || lgid == NULL)
    4454             :                                         return NULL;
    4455             :                         }
    4456             :                 }
    4457             : 
    4458         274 :                 limit = lpiv;
    4459         274 :                 stmt *s;
    4460        1471 :                 for (n=pl->h ; n; n = n->next) {
    4461        1197 :                         stmt *os = n->data;
    4462        1197 :                         list_append(npl, s=stmt_project(be, limit, column(be, os)));
    4463        1197 :                         s->label = os->label;
    4464             :                 }
    4465         274 :                 psub = stmt_list(be, npl);
    4466             : 
    4467             :                 /* also rebuild sub as multiple orderby expressions may use the sub table (ie aren't part of the result columns) */
    4468         274 :                 pl = sub->op4.lval;
    4469         274 :                 npl = sa_list(sql->sa);
    4470        1613 :                 for (n=pl->h ; n; n = n->next) {
    4471        1339 :                         stmt *os = n->data;
    4472        1339 :                         list_append(npl, s = stmt_project(be, limit, column(be, os)));
    4473        1339 :                         s->label = os->label;
    4474             :                 }
    4475         274 :                 sub = stmt_list(be, npl);
    4476             :         }
    4477      393716 :         if (need_distinct(rel)) {
    4478          14 :                 stmt *distinct = NULL;
    4479          14 :                 psub = rel2bin_distinct(be, psub, &distinct);
    4480             :                 /* also rebuild sub as multiple orderby expressions may use the sub table (ie aren't part of the result columns) */
    4481          14 :                 if (sub && distinct) {
    4482           5 :                         list *npl = sa_list(sql->sa);
    4483             : 
    4484           5 :                         pl = sub->op4.lval;
    4485          21 :                         for (n=pl->h ; n; n = n->next)
    4486          16 :                                 list_append(npl, stmt_project(be, distinct, column(be, n->data)));
    4487           5 :                         sub = stmt_list(be, npl);
    4488             :                 }
    4489             :         }
    4490      393716 :         if (/*(!topn || need_distinct(rel)) &&*/ rel->r) {
    4491       10350 :                 list *oexps = rel->r;
    4492       10350 :                 stmt *orderby_ids = NULL, *orderby_grp = NULL;
    4493             : 
    4494       10350 :                 list *ostmts = sa_list(be->mvc->sa);
    4495       34112 :                 for (en = oexps->h; en; en = en->next) {
    4496       23762 :                         stmt *orderby = NULL;
    4497       23762 :                         sql_exp *orderbycole = en->data;
    4498       23762 :                         stmt *orderbycolstmt = exp_bin(be, orderbycole, sub, psub, NULL, NULL, NULL, NULL, 0, 0, 0);
    4499             : 
    4500       23762 :                         if (!orderbycolstmt) {
    4501           0 :                                 assert(sql->session->status == -10); /* Stack overflow errors shouldn't terminate the server */
    4502             :                                 return NULL;
    4503             :                         }
    4504             :                         /* single values don't need sorting */
    4505       23762 :                         if (orderbycolstmt->nrcols == 0) {
    4506          67 :                                 append(ostmts, NULL);
    4507          67 :                                 continue;
    4508             :                         }
    4509       23695 :                         if (orderby_ids)
    4510       13418 :                                 orderby = stmt_reorder(be, orderbycolstmt, is_ascending(orderbycole), nulls_last(orderbycole), orderby_ids, orderby_grp);
    4511             :                         else
    4512       10277 :                                 orderby = stmt_order(be, orderbycolstmt, is_ascending(orderbycole), nulls_last(orderbycole));
    4513       23695 :                         stmt *orderby_vals = stmt_result(be, orderby, 0);
    4514       23695 :                         append(ostmts, orderby_vals);
    4515       23695 :                         orderby_ids = stmt_result(be, orderby, 1);
    4516       23695 :                         orderby_grp = stmt_result(be, orderby, 2);
    4517             :                 }
    4518       10350 :                 if (orderby_ids)
    4519       10277 :                         psub = sql_reorder(be, orderby_ids, rel->exps, psub, oexps, ostmts);
    4520             :         }
    4521             :         return psub;
    4522             : }
    4523             : 
    4524             : static stmt *
    4525           0 : rel2bin_predicate(backend *be)
    4526             : {
    4527           0 :         return const_column(be, stmt_bool(be, 1));
    4528             : }
    4529             : 
    4530             : static stmt *
    4531       69199 : rel2bin_select(backend *be, sql_rel *rel, list *refs)
    4532             : {
    4533       69199 :         mvc *sql = be->mvc;
    4534       69199 :         node *en;
    4535       69199 :         stmt *sub = NULL, *sel = NULL;
    4536       69199 :         stmt *predicate = NULL;
    4537             : 
    4538       69199 :         if (rel->l) { /* first construct the sub relation */
    4539       69199 :                 sub = subrel_bin(be, rel->l, refs);
    4540       69199 :                 if (!sub)
    4541             :                         return NULL;
    4542       69199 :                 sel = sub->cand;
    4543       69199 :                 sub = row2cols(be, sub);
    4544             :         }
    4545       69199 :         if (!sub && !predicate)
    4546           0 :                 predicate = rel2bin_predicate(be);
    4547       69199 :         if (list_empty(rel->exps)) {
    4548        1480 :                 if (sub)
    4549             :                         return sub;
    4550           0 :                 if (predicate)
    4551             :                         return predicate;
    4552           0 :                 assert(0);
    4553             :         }
    4554       67719 :         en = rel->exps->h;
    4555       67719 :         if (!sub && predicate) {
    4556           0 :                 list *l = sa_list(sql->sa);
    4557           0 :                 assert(predicate);
    4558           0 :                 append(l, predicate);
    4559           0 :                 sub = stmt_list(be, l);
    4560             :         }
    4561             :         /* handle possible index lookups */
    4562             :         /* expressions are in index order ! */
    4563       67719 :         if (sub && en) {
    4564       67719 :                 sql_exp *e = en->data;
    4565       67719 :                 prop *p;
    4566             : 
    4567       67719 :                 if ((p=find_prop(e->p, PROP_HASHCOL)) != NULL && !is_anti(e)) {
    4568          12 :                         sql_idx *i = p->value.pval;
    4569          12 :                         int oldvtop = be->mb->vtop, oldstop = be->mb->stop;
    4570             : 
    4571          12 :                         if (!(sel = rel2bin_hash_lookup(be, rel, sub, NULL, i, en))) {
    4572             :                                 /* hash lookup cannot be used, clean leftover mal statements */
    4573           3 :                                 clean_mal_statements(be, oldstop, oldvtop);
    4574             :                         }
    4575             :                 }
    4576             :         }
    4577      145740 :         for (en = rel->exps->h; en; en = en->next) {
    4578       78021 :                 sql_exp *e = en->data;
    4579       78021 :                 stmt *s = exp_bin(be, e, sub, NULL, NULL, NULL, NULL, sel, 0, 1, 0);
    4580             : 
    4581       78021 :                 if (!s) {
    4582           0 :                         assert(sql->session->status == -10); /* Stack overflow errors shouldn't terminate the server */
    4583             :                         return NULL;
    4584             :                 }
    4585       78021 :                 if (s->nrcols == 0){
    4586        2372 :                         if (!predicate && sub && !list_empty(sub->op4.lval))
    4587        2364 :                                 predicate = stmt_const(be, bin_find_smallest_column(be, sub), stmt_bool(be, 1));
    4588           8 :                         else if (!predicate)
    4589           0 :                                 predicate = const_column(be, stmt_bool(be, 1));
    4590        2372 :                         if (e->type != e_cmp) {
    4591        2251 :                                 sql_subtype *bt = sql_bind_localtype("bit");
    4592             : 
    4593        2251 :                                 s = stmt_convert(be, s, NULL, exp_subtype(e), bt);
    4594             :                         }
    4595        2372 :                         sel = stmt_uselect(be, predicate, s, cmp_equal, sel, 0, 0);
    4596       75649 :                 } else if (e->type != e_cmp) {
    4597           0 :                         sel = stmt_uselect(be, s, stmt_bool(be, 1), cmp_equal, sel, 0, 0);
    4598             :                 } else {
    4599             :                         sel = s;
    4600             :                 }
    4601             :         }
    4602             : 
    4603       67719 :         if (sub && sel) {
    4604       67719 :                 sub = stmt_list(be, sub->op4.lval); /* protect against references */
    4605       67719 :                 sub->cand = sel;
    4606             :         }
    4607             :         return sub;
    4608             : }
    4609             : 
    4610             : static stmt *
    4611       13685 : rel2bin_groupby(backend *be, sql_rel *rel, list *refs)
    4612             : {
    4613       13685 :         mvc *sql = be->mvc;
    4614       13685 :         list *l, *aggrs, *gbexps = sa_list(sql->sa);
    4615       13685 :         node *n, *en;
    4616       13685 :         stmt *sub = NULL, *cursub;
    4617       13685 :         stmt *groupby = NULL, *grp = NULL, *ext = NULL, *cnt = NULL;
    4618             : 
    4619       13685 :         if (rel->l) { /* first construct the sub relation */
    4620       13685 :                 sub = subrel_bin(be, rel->l, refs);
    4621       13685 :                 sub = subrel_project(be, sub, refs, rel->l);
    4622       13684 :                 if (!sub)
    4623             :                         return NULL;
    4624             :         }
    4625             : 
    4626       13684 :         if (sub && sub->type == st_list && sub->op4.lval->h && !((stmt*)sub->op4.lval->h->data)->nrcols) {
    4627         360 :                 list *newl = sa_list(sql->sa);
    4628         360 :                 node *n;
    4629             : 
    4630         812 :                 for (n=sub->op4.lval->h; n; n = n->next) {
    4631         451 :                         stmt *s = n->data;
    4632         451 :                         assert(s->type == st_alias);
    4633         451 :                         const char *cname = column_name(sql->sa, s);
    4634         451 :                         const char *tname = table_name(sql->sa, s);
    4635         451 :                         int label = s->label;
    4636             : 
    4637         451 :                         s = column(be, s);
    4638         452 :                         s = stmt_alias(be, s, label, tname, cname);
    4639         452 :                         append(newl, s);
    4640             :                 }
    4641         361 :                 sub = stmt_list(be, newl);
    4642             :         }
    4643             : 
    4644             :         /* groupby columns */
    4645             : 
    4646             :         /* Keep groupby columns, so that they can be looked up in the aggr list */
    4647       13685 :         if (rel->r) {
    4648       10095 :                 list *exps = rel->r;
    4649             : 
    4650       25240 :                 for (en = exps->h; en; en = en->next) {
    4651       15145 :                         sql_exp *e = en->data;
    4652       15145 :                         stmt *gbcol = exp_bin(be, e, sub, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    4653             : 
    4654       15145 :                         if (!gbcol) {
    4655           0 :                                 assert(sql->session->status == -10); /* Stack overflow errors shouldn't terminate the server */
    4656             :                                 return NULL;
    4657             :                         }
    4658       15145 :                         if (!gbcol->nrcols)
    4659          12 :                                 gbcol = stmt_const(be, bin_find_smallest_column(be, sub), gbcol);
    4660       15145 :                         groupby = stmt_group(be, gbcol, grp, ext, cnt, !en->next);
    4661       15145 :                         grp = stmt_result(be, groupby, 0);
    4662       15145 :                         ext = stmt_result(be, groupby, 1);
    4663       15145 :                         cnt = stmt_result(be, groupby, 2);
    4664       15145 :                         gbcol = stmt_alias(be, gbcol, e->alias.label, exp_find_rel_name(e), exp_name(e));
    4665       15145 :                         list_append(gbexps, gbcol);
    4666             :                 }
    4667             :         }
    4668             :         /* now aggregate */
    4669       13685 :         l = sa_list(sql->sa);
    4670       13685 :         if (l == NULL)
    4671             :                 return NULL;
    4672       13685 :         aggrs = rel->exps;
    4673       13685 :         cursub = stmt_list(be, l);
    4674       13685 :         if (cursub == NULL)
    4675             :                 return NULL;
    4676       13685 :         if (aggrs && !aggrs->h && ext)
    4677           0 :                 list_append(l, ext);
    4678       39384 :         for (n = aggrs->h; n; n = n->next) {
    4679       25699 :                 sql_exp *aggrexp = n->data;
    4680       25699 :                 stmt *aggrstmt = NULL;
    4681       25699 :                 int oldvtop, oldstop;
    4682             : 
    4683             :                 /* first look in the current aggr list (l) and group by column list */
    4684       25699 :                 if (l && !aggrstmt && aggrexp->type == e_column)
    4685             :                         //aggrstmt = list_find_column(be, l, aggrexp->l, aggrexp->r);
    4686       14732 :                         aggrstmt = list_find_column_nid(be, l, aggrexp->nid);
    4687       25699 :                 if (gbexps && !aggrstmt && aggrexp->type == e_column) {
    4688             :                         //aggrstmt = list_find_column(be, gbexps, aggrexp->l, aggrexp->r);
    4689       14706 :                         aggrstmt = list_find_column_nid(be, gbexps, aggrexp->nid);
    4690       14706 :                         if (aggrstmt && groupby) {
    4691       14505 :                                 aggrstmt = stmt_project(be, ext, aggrstmt);
    4692       14505 :                                 if (list_length(gbexps) == 1)
    4693        6132 :                                         aggrstmt->key = 1;
    4694             :                         }
    4695             :                 }
    4696             : 
    4697       25699 :                 oldvtop = be->mb->vtop;
    4698       25699 :                 oldstop = be->mb->stop;
    4699       25699 :                 if (!aggrstmt)
    4700       11168 :                         aggrstmt = exp_bin(be, aggrexp, sub, NULL, grp, ext, cnt, NULL, 0, 0, 0);
    4701             :                 /* maybe the aggr uses intermediate results of this group by,
    4702             :                    therefore we pass the group by columns too
    4703             :                  */
    4704       11168 :                 if (!aggrstmt) {
    4705           1 :                         clean_mal_statements(be, oldstop, oldvtop);
    4706           1 :                         aggrstmt = exp_bin(be, aggrexp, sub, cursub, grp, ext, cnt, NULL, 0, 0, 0);
    4707             :                 }
    4708           1 :                 if (!aggrstmt) {
    4709           0 :                         assert(sql->session->status == -10); /* Stack overflow errors shouldn't terminate the server */
    4710             :                         return NULL;
    4711             :                 }
    4712             : 
    4713       25699 :                 if (!aggrstmt->nrcols && ext && ext->nrcols)
    4714           0 :                         aggrstmt = stmt_const(be, ext, aggrstmt);
    4715             : 
    4716       25699 :                 aggrstmt = stmt_rename(be, aggrexp, aggrstmt);
    4717       25699 :                 list_append(l, aggrstmt);
    4718             :         }
    4719       13685 :         stmt_set_nrcols(cursub);
    4720       13685 :         return cursub;
    4721             : }
    4722             : 
    4723             : static stmt *
    4724       16797 : rel2bin_topn(backend *be, sql_rel *rel, list *refs)
    4725             : {
    4726       16797 :         mvc *sql = be->mvc;
    4727       16797 :         sql_exp *oe = NULL, *le = NULL;
    4728       16797 :         stmt *sub = NULL, *l = NULL, *o = NULL;
    4729       16797 :         node *n;
    4730             : 
    4731       16797 :         if (rel->l) { /* first construct the sub relation */
    4732       16797 :                 sql_rel *rl = rel->l;
    4733             : 
    4734       16797 :                 if (rl->op == op_project) {
    4735         324 :                         if (rel_is_ref(rl)) {
    4736           5 :                                 sub = refs_find_rel(refs, rl);
    4737           5 :                                 if (!sub)
    4738           0 :                                         sub = rel2bin_project(be, rl, refs, rel);
    4739             :                         } else
    4740         319 :                                 sub = rel2bin_project(be, rl, refs, rel);
    4741             :                 } else {
    4742       16473 :                         sub = subrel_bin(be, rl, refs);
    4743             :                 }
    4744       16797 :                 sub = subrel_project(be, sub, refs, rl);
    4745             :         }
    4746       16797 :         if (!sub)
    4747           0 :                 return NULL;
    4748             : 
    4749       16797 :         le = topn_limit(rel);
    4750       16797 :         oe = topn_offset(rel);
    4751             : 
    4752       16797 :         n = sub->op4.lval->h;
    4753       16797 :         if (n) {
    4754       16797 :                 stmt *limit = NULL, *sc = n->data;
    4755             :                 //const char *cname = column_name(sql->sa, sc);
    4756             :                 //const char *tname = table_name(sql->sa, sc);
    4757       16797 :                 list *newl = sa_list(sql->sa);
    4758       16797 :                 int oldvtop = be->mb->vtop, oldstop = be->mb->stop;
    4759             : 
    4760       16797 :                 if (le)
    4761       16769 :                         l = exp_bin(be, le, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    4762       16769 :                 if (!l) {
    4763          28 :                         clean_mal_statements(be, oldstop, oldvtop);
    4764          28 :                         l = stmt_atom_lng_nil(be);
    4765             :                 }
    4766             : 
    4767       16797 :                 oldvtop = be->mb->vtop;
    4768       16797 :                 oldstop = be->mb->stop;
    4769       16797 :                 if (oe)
    4770          89 :                         o = exp_bin(be, oe, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    4771          89 :                 if (!o) {
    4772       16708 :                         clean_mal_statements(be, oldstop, oldvtop);
    4773       16708 :                         o = stmt_atom_lng(be, 0);
    4774             :                 }
    4775       16796 :                 if (!l || !o)
    4776             :                         return NULL;
    4777             : 
    4778       16796 :                 sc = column(be, sc);
    4779       16796 :                 limit = stmt_limit(be, sc /*stmt_alias(be, sc, 0, tname, cname)*/, NULL, NULL, o, l, 0,0,0,0,0);
    4780             : 
    4781      117694 :                 for ( ; n; n = n->next) {
    4782       84101 :                         stmt *sc = n->data;
    4783       84101 :                         assert(sc->type == st_alias);
    4784       84101 :                         const char *cname = column_name(sql->sa, sc);
    4785       84102 :                         const char *tname = table_name(sql->sa, sc);
    4786       84102 :                         int label = sc->label;
    4787             : 
    4788       84102 :                         sc = column(be, sc);
    4789       84102 :                         sc = stmt_project(be, limit, sc);
    4790       84101 :                         list_append(newl, stmt_alias(be, sc, label, tname, cname));
    4791             :                 }
    4792       16797 :                 sub = stmt_list(be, newl);
    4793             :         }
    4794             :         return sub;
    4795             : }
    4796             : 
    4797             : static stmt *
    4798          21 : rel2bin_sample(backend *be, sql_rel *rel, list *refs)
    4799             : {
    4800          21 :         mvc *sql = be->mvc;
    4801          21 :         list *newl;
    4802          21 :         stmt *sub = NULL, *sample_size = NULL, *sample = NULL, *seed = NULL;
    4803          21 :         node *n;
    4804             : 
    4805          21 :         if (rel->l) /* first construct the sub relation */
    4806          21 :                 sub = subrel_bin(be, rel->l, refs);
    4807          21 :         sub = subrel_project(be, sub, refs, rel->l);
    4808          21 :         if (!sub)
    4809             :                 return NULL;
    4810             : 
    4811          21 :         n = sub->op4.lval->h;
    4812          21 :         newl = sa_list(sql->sa);
    4813             : 
    4814          21 :         if (n) {
    4815          21 :                 stmt *sc = n->data;
    4816             :                 //const char *cname = column_name(sql->sa, sc);
    4817             :                 //const char *tname = table_name(sql->sa, sc);
    4818             : 
    4819          21 :                  if (!(sample_size = exp_bin(be, rel->exps->h->data, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0)))
    4820             :                         return NULL;
    4821             : 
    4822          21 :                 if (rel->exps->cnt == 2) {
    4823          12 :                         seed = exp_bin(be, rel->exps->h->next->data, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    4824          12 :                         if (!seed)
    4825             :                                 return NULL;
    4826             :                 }
    4827             : 
    4828          21 :                 sc = column(be, sc);
    4829          21 :                 sample = stmt_sample(be, sc /*stmt_alias(be, sc, 0, tname, cname)*/,sample_size, seed);
    4830             : 
    4831          65 :                 for ( ; n; n = n->next) {
    4832          23 :                         stmt *sc = n->data;
    4833          23 :                         assert(sc->type == st_alias);
    4834          23 :                         const char *cname = column_name(sql->sa, sc);
    4835          23 :                         const char *tname = table_name(sql->sa, sc);
    4836          23 :                         int label = sc->label;
    4837             : 
    4838          23 :                         sc = column(be, sc);
    4839          23 :                         sc = stmt_project(be, sample, sc);
    4840          23 :                         list_append(newl, stmt_alias(be, sc, label, tname, cname));
    4841             :                 }
    4842             :         }
    4843          21 :         sub = stmt_list(be, newl);
    4844          21 :         return sub;
    4845             : }
    4846             : 
    4847             : static stmt *
    4848        5133 : sql_parse(backend *be, sql_schema *s, const char *query, char mode)
    4849             : {
    4850        5133 :         sql_rel *rel = rel_parse(be->mvc, s, query, mode);
    4851        5133 :         stmt *sq = NULL;
    4852             : 
    4853        5133 :         if (rel && (rel = sql_processrelation(be->mvc, rel, 0, 1, 1, 1)))
    4854        5130 :                 sq = rel_bin(be, rel);
    4855        5133 :         return sq;
    4856             : }
    4857             : 
    4858             : static stmt *
    4859       12808 : insert_check_ukey(backend *be, list *inserts, sql_key *k, stmt *idx_inserts)
    4860             : {
    4861       12808 :         mvc *sql = be->mvc;
    4862             : /* pkey's cannot have NULLs, ukeys however can
    4863             :    current implementation switches on 'NOT NULL' on primary key columns */
    4864             : 
    4865       12808 :         char *msg = NULL;
    4866       12808 :         stmt *res;
    4867             : 
    4868       12808 :         sql_subtype *lng = sql_bind_localtype("lng");
    4869       12808 :         sql_subfunc *cnt = sql_bind_func(sql, "sys", "count", sql_bind_localtype("void"), NULL, F_AGGR, true, true);
    4870       12808 :         sql_subtype *bt = sql_bind_localtype("bit");
    4871       12808 :         stmt *dels = stmt_tid(be, k->t, 0);
    4872       12808 :         sql_subfunc *ne = sql_bind_func_result(sql, "sys", "<>", F_FUNC, true, bt, 2, lng, lng);
    4873             : 
    4874       12808 :         if (list_length(k->columns) > 1) {
    4875         820 :                 node *m;
    4876         820 :                 stmt *s = list_fetch(inserts, 0), *ins = s;
    4877         820 :                 sql_subfunc *sum;
    4878         820 :                 stmt *ssum = NULL;
    4879         820 :                 stmt *col = NULL;
    4880             : 
    4881         820 :                 s = ins;
    4882             :                 /* 1st stage: find out if original contains same values */
    4883         820 :                 if (/*s->key &&*/ s->nrcols == 0) {
    4884         586 :                         s = NULL;
    4885         586 :                         if (k->idx && hash_index(k->idx->type))
    4886         586 :                                 s = stmt_uselect(be, stmt_idx(be, k->idx, dels, dels->partition), idx_inserts, cmp_equal, s, 0, 1 /* is_semantics*/);
    4887        2149 :                         for (m = k->columns->h; m; m = m->next) {
    4888        1563 :                                 sql_kc *c = m->data;
    4889        1563 :                                 stmt *cs = list_fetch(inserts, c->c->colnr);
    4890             : 
    4891             :                                 /* foreach column add predicate */
    4892        1563 :                                 stmt_add_column_predicate(be, c->c);
    4893             : 
    4894        1563 :                                 col = stmt_col(be, c->c, dels, dels->partition);
    4895        1563 :                                 if (k->type == unndkey)
    4896           8 :                                         s = stmt_uselect(be, col, cs, cmp_equal, s, 0, 1);
    4897        1664 :                                 else if ((k->type == ukey) && stmt_has_null(col)) {
    4898         109 :                                         stmt *nn = stmt_selectnonil(be, col, s);
    4899         109 :                                         s = stmt_uselect(be, col, cs, cmp_equal, nn, 0, 0);
    4900             :                                 } else {
    4901        1446 :                                         s = stmt_uselect(be, col, cs, cmp_equal, s, 0, 0);
    4902             :                                 }
    4903             :                         }
    4904             :                 } else {
    4905         234 :                         list *lje = sa_list(sql->sa);
    4906         234 :                         list *rje = sa_list(sql->sa);
    4907         234 :                         if (k->idx && hash_index(k->idx->type)) {
    4908         234 :                                 list_append(lje, stmt_idx(be, k->idx, dels, dels->partition));
    4909         234 :                                 list_append(rje, idx_inserts);
    4910             :                         }
    4911         749 :                         for (m = k->columns->h; m; m = m->next) {
    4912         515 :                                 sql_kc *c = m->data;
    4913         515 :                                 stmt *cs = list_fetch(inserts, c->c->colnr);
    4914             : 
    4915             :                                 /* foreach column add predicate */
    4916         515 :                                 stmt_add_column_predicate(be, c->c);
    4917             : 
    4918         515 :                                 col = stmt_col(be, c->c, dels, dels->partition);
    4919         515 :                                 list_append(lje, col);
    4920         515 :                                 list_append(rje, cs);
    4921             :                         }
    4922         234 :                         s = releqjoin(be, lje, rje, NULL, 1 /* hash used */, 0, k->type == unndkey? 1: 0);
    4923         234 :                         s = stmt_result(be, s, 0);
    4924             :                 }
    4925         820 :                 s = stmt_binop(be, stmt_aggr(be, s, NULL, NULL, cnt, 1, 0, 1), stmt_atom_lng(be, 0), NULL, ne);
    4926             : 
    4927             :                 /* 2nd stage: find out if inserted are unique */
    4928         820 :                 if ((!idx_inserts && ins->nrcols) || (idx_inserts && idx_inserts->nrcols)) {      /* insert columns not atoms */
    4929         234 :                         sql_subfunc *or = sql_bind_func_result(sql, "sys", "or", F_FUNC, true, bt, 2, bt, bt);
    4930         234 :                         stmt *orderby_ids = NULL, *orderby_grp = NULL;
    4931         234 :                         stmt *sel = NULL;
    4932             : 
    4933             :                         /* remove any nils as in stmt_order NULL = NULL, instead of NULL != NULL */
    4934         234 :                         if (k->type == ukey) {
    4935         153 :                                 for (m = k->columns->h; m; m = m->next) {
    4936         105 :                                         sql_kc *c = m->data;
    4937         105 :                                         stmt *cs = list_fetch(inserts, c->c->colnr);
    4938         105 :                                         if (stmt_has_null(cs))
    4939         102 :                                                 sel = stmt_selectnonil(be, cs, sel);
    4940             :                                 }
    4941             :                         }
    4942             :                         /* implementation uses sort key check */
    4943         749 :                         for (m = k->columns->h; m; m = m->next) {
    4944         515 :                                 sql_kc *c = m->data;
    4945         515 :                                 stmt *orderby;
    4946         515 :                                 stmt *cs = list_fetch(inserts, c->c->colnr);
    4947             : 
    4948         515 :                                 if (sel)
    4949         102 :                                         cs = stmt_project(be, sel, cs);
    4950         515 :                                 if (orderby_grp)
    4951         281 :                                         orderby = stmt_reorder(be, cs, 1, 0, orderby_ids, orderby_grp);
    4952             :                                 else
    4953         234 :                                         orderby = stmt_order(be, cs, 1, 0);
    4954         515 :                                 orderby_ids = stmt_result(be, orderby, 1);
    4955         515 :                                 orderby_grp = stmt_result(be, orderby, 2);
    4956             :                         }
    4957             : 
    4958         234 :                         if (!orderby_grp || !orderby_ids)
    4959             :                                 return NULL;
    4960             : 
    4961         234 :                         sum = sql_bind_func(sql, "sys", "not_unique", tail_type(orderby_grp), NULL, F_AGGR, true, true);
    4962         234 :                         ssum = stmt_aggr(be, orderby_grp, NULL, NULL, sum, 1, 0, 1);
    4963             :                         /* combine results */
    4964         234 :                         s = stmt_binop(be, s, ssum, NULL, or);
    4965             :                 }
    4966             : 
    4967         820 :                 if (k->type == pkey) {
    4968         330 :                         msg = sa_message(sql->sa, SQLSTATE(40002) "INSERT INTO: PRIMARY KEY constraint '%s.%s' violated", k->t->base.name, k->base.name);
    4969             :                 } else {
    4970         490 :                         msg = sa_message(sql->sa, SQLSTATE(40002) "INSERT INTO: UNIQUE constraint '%s.%s' violated", k->t->base.name, k->base.name);
    4971             :                 }
    4972         820 :                 res = stmt_exception(be, s, msg, 00001);
    4973             :         } else {                /* single column key */
    4974       11988 :                 sql_kc *c = k->columns->h->data;
    4975       11988 :                 stmt *s = list_fetch(inserts, c->c->colnr), *h = s;
    4976             : 
    4977             :                 /* add predicate for this column */
    4978       11988 :                 stmt_add_column_predicate(be, c->c);
    4979             : 
    4980       11988 :                 s = stmt_col(be, c->c, dels, dels->partition);
    4981       11988 :                 if ((k->type == ukey) && stmt_has_null(s)) {
    4982         202 :                         stmt *nn = stmt_selectnonil(be, s, NULL);
    4983         202 :                         s = stmt_project(be, nn, s);
    4984             :                 }
    4985       11988 :                 if (h->nrcols) {
    4986        4784 :                         s = stmt_join(be, s, h, 0, cmp_equal, 0, k->type == unndkey? 1: 0, false);
    4987             :                         /* s should be empty */
    4988        4784 :                         s = stmt_result(be, s, 0);
    4989        4784 :                         s = stmt_aggr(be, s, NULL, NULL, cnt, 1, 0, 1);
    4990             :                 } else {
    4991        7204 :                         s = stmt_uselect(be, s, h, cmp_equal, NULL, 0, k->type == unndkey? 1: 0);
    4992             :                         /* s should be empty */
    4993        7204 :                         s = stmt_aggr(be, s, NULL, NULL, cnt, 1, 0, 1);
    4994             :                 }
    4995             :                 /* s should be empty */
    4996       11988 :                 s = stmt_binop(be, s, stmt_atom_lng(be, 0), NULL, ne);
    4997             : 
    4998             :                 /* 2e stage: find out if inserts are unique */
    4999       11988 :                 if (h->nrcols) {     /* insert multiple atoms */
    5000        4784 :                         sql_subfunc *sum;
    5001        4784 :                         stmt *count_sum = NULL;
    5002        4784 :                         sql_subfunc *or = sql_bind_func_result(sql, "sys", "or", F_FUNC, true, bt, 2, bt, bt);
    5003        4784 :                         stmt *ssum, *ss;
    5004             : 
    5005        4784 :                         stmt *g = list_fetch(inserts, c->c->colnr), *ins = g;
    5006             : 
    5007             :                         /* inserted values may be null */
    5008        4784 :                         if ((k->type == ukey) && stmt_has_null(ins)) {
    5009        1755 :                                 stmt *nn = stmt_selectnonil(be, ins, NULL);
    5010        1755 :                                 ins = stmt_project(be, nn, ins);
    5011             :                         }
    5012             : 
    5013        4784 :                         g = stmt_group(be, ins, NULL, NULL, NULL, 1);
    5014        4784 :                         ss = stmt_result(be, g, 2); /* use count */
    5015             :                         /* (count(ss) <> sum(ss)) */
    5016        4784 :                         sum = sql_bind_func(sql, "sys", "sum", lng, NULL, F_AGGR, true, true);
    5017        4784 :                         ssum = stmt_aggr(be, ss, NULL, NULL, sum, 1, 0, 1);
    5018        4784 :                         ssum = sql_Nop_(be, "ifthenelse", sql_unop_(be, "isnull", ssum), stmt_atom_lng(be, 0), ssum, NULL);
    5019        4784 :                         count_sum = stmt_binop(be, check_types(be, tail_type(ssum), stmt_aggr(be, ss, NULL, NULL, cnt, 1, 0, 1), type_equal), ssum, NULL, ne);
    5020             : 
    5021             :                         /* combine results */
    5022        4784 :                         s = stmt_binop(be, s, count_sum, NULL, or);
    5023             :                 }
    5024       11988 :                 if (k->type == pkey) {
    5025       10072 :                         msg = sa_message(sql->sa, SQLSTATE(40002) "INSERT INTO: PRIMARY KEY constraint '%s.%s' violated", k->t->base.name, k->base.name);
    5026             :                 } else {
    5027        1916 :                         msg = sa_message(sql->sa, SQLSTATE(40002) "INSERT INTO: UNIQUE constraint '%s.%s' violated", k->t->base.name, k->base.name);
    5028             :                 }
    5029       11988 :                 res = stmt_exception(be, s, msg, 00001);
    5030             :         }
    5031             :         return res;
    5032             : }
    5033             : 
    5034             : static stmt *
    5035         777 : insert_check_fkey(backend *be, list *inserts, sql_key *k, stmt *idx_inserts, stmt *pin)
    5036             : {
    5037         777 :         mvc *sql = be->mvc;
    5038         777 :         char *msg = NULL;
    5039         777 :         stmt *cs = list_fetch(inserts, 0), *s = cs;
    5040         777 :         sql_subtype *lng = sql_bind_localtype("lng");
    5041         777 :         sql_subfunc *cnt = sql_bind_func(sql, "sys", "count", sql_bind_localtype("void"), NULL, F_AGGR, true, true);
    5042         777 :         sql_subtype *bt = sql_bind_localtype("bit");
    5043         777 :         sql_subfunc *ne = sql_bind_func_result(sql, "sys", "<>", F_FUNC, true, bt, 2, lng, lng);
    5044             : 
    5045         777 :     stmt *nonil_rows = NULL;
    5046        1589 :         for (node *m = k->columns->h; m; m = m->next) {
    5047         812 :                 sql_kc *c = m->data;
    5048             : 
    5049             :                 /* foreach column add predicate */
    5050         812 :                 stmt_add_column_predicate(be, c->c);
    5051             : 
    5052             :                 // foreach column aggregate the nonil (literally 'null') values.
    5053             :                 // mind that null values are valid fkeys with undefined value so
    5054             :                 // we won't have an entry for them in the idx_inserts col
    5055         812 :                 s = list_fetch(inserts, c->c->colnr);
    5056         812 :                 nonil_rows = stmt_selectnonil(be, s, nonil_rows);
    5057             :         }
    5058             : 
    5059         777 :         if (!s && pin && list_length(pin->op4.lval))
    5060           0 :                 s = pin->op4.lval->h->data;
    5061             : 
    5062             :     // we want to make sure that the data column(s) has the same number
    5063             :     // of (nonil) rows as the index column. if that is **not** the case
    5064             :     // then we are obviously dealing with an invalid foreign key
    5065         777 :         if (s->key && s->nrcols == 0) {
    5066           0 :                 s = stmt_binop(be,
    5067             :                         stmt_aggr(be, idx_inserts, NULL, NULL, cnt, 1, 1, 1),
    5068             :                         stmt_aggr(be, const_column(be, nonil_rows), NULL, NULL, cnt, 1, 1, 1),
    5069             :                         NULL, ne);
    5070             :         } else {
    5071             :                 /* relThetaJoin.notNull.count <> inserts[notNull(col1) && ... && notNull(colN)].count */
    5072         777 :                 s = stmt_binop(be,
    5073             :                         stmt_aggr(be, idx_inserts, NULL, NULL, cnt, 1, 1, 1),
    5074             :                         stmt_aggr(be, column(be, nonil_rows), NULL, NULL, cnt, 1, 1, 1),
    5075             :                         NULL, ne);
    5076             :         }
    5077             : 
    5078             :         /* s should be empty */
    5079         777 :         msg = sa_message(sql->sa, SQLSTATE(40002) "INSERT INTO: FOREIGN KEY constraint '%s.%s' violated", k->t->base.name, k->base.name);
    5080         777 :         return stmt_exception(be, s, msg, 00001);
    5081             : }
    5082             : 
    5083             : static stmt *
    5084       13585 : sql_insert_key(backend *be, list *inserts, sql_key *k, stmt *idx_inserts, stmt *pin)
    5085             : {
    5086             :         /* int insert = 1;
    5087             :          * while insert and has u/pkey and not deferred then
    5088             :          *      if u/pkey values exist then
    5089             :          *              insert = 0
    5090             :          * while insert and has fkey and not deferred then
    5091             :          *      find id of corresponding u/pkey
    5092             :          *      if (!found)
    5093             :          *              insert = 0
    5094             :          * if insert
    5095             :          *      insert values
    5096             :          *      insert fkey/pkey index
    5097             :          */
    5098       13585 :         if (k->type == pkey || k->type == ukey || k->type == unndkey) {
    5099       12808 :                 return insert_check_ukey(be, inserts, k, idx_inserts);
    5100             :         } else {                /* foreign keys */
    5101         777 :                 return insert_check_fkey(be, inserts, k, idx_inserts, pin);
    5102             :         }
    5103             : }
    5104             : 
    5105             : static int
    5106        4852 : sql_stack_add_inserted(mvc *sql, const char *name, sql_table *t, stmt **updates)
    5107             : {
    5108             :         /* Put single relation of updates and old values on to the stack */
    5109        4852 :         sql_rel *r = NULL;
    5110        4852 :         node *n;
    5111        4852 :         list *exps = sa_list(sql->sa);
    5112        4852 :         trigger_input *ti = SA_NEW(sql->sa, trigger_input);
    5113             : 
    5114        4852 :         ti->t = t;
    5115        4852 :         ti->tids = NULL;
    5116        4852 :         ti->updates = updates;
    5117        4852 :         ti->type = 1;
    5118        4852 :         ti->nn = name;
    5119             : 
    5120       12945 :         for (n = ol_first_node(t->columns); n; n = n->next) {
    5121        8093 :                 sql_column *c = n->data;
    5122        8093 :                 sql_exp *ne = exp_column(sql->sa, name, c->base.name, &c->type, CARD_MULTI, c->null, is_column_unique(c), 0);
    5123        8093 :                 ne->alias.label = -(sql->nid++);
    5124             : 
    5125        8093 :                 append(exps, ne);
    5126             :         }
    5127        4852 :         r = rel_table_func(sql->sa, NULL, NULL, exps, TRIGGER_WRAPPER);
    5128        4852 :         r->l = ti;
    5129             : 
    5130        4852 :         return stack_push_rel_view(sql, name, r) ? 1 : 0;
    5131             : }
    5132             : 
    5133             : static int
    5134      216049 : sql_insert_triggers(backend *be, sql_table *t, stmt **updates, int time)
    5135             : {
    5136      216049 :         mvc *sql = be->mvc;
    5137      216049 :         node *n;
    5138      216049 :         int res = 1;
    5139             : 
    5140      216049 :         if (!ol_length(t->triggers))
    5141             :                 return res;
    5142             : 
    5143        8336 :         for (n = ol_first_node(t->triggers); n; n = n->next) {
    5144        6650 :                 sql_trigger *trigger = n->data;
    5145             : 
    5146        6650 :                 if (!stack_push_frame(sql, "%OLD-NEW"))
    5147             :                         return 0;
    5148        6650 :                 if (trigger->event == 0 && trigger->time == time) {
    5149        4852 :                         const char *n = trigger->new_name;
    5150             : 
    5151             :                         /* add name for the 'inserted' to the stack */
    5152        4852 :                         if (!n) n = "new";
    5153             : 
    5154        4852 :                         if(!sql_stack_add_inserted(sql, n, t, updates)) {
    5155           0 :                                 stack_pop_frame(sql);
    5156           0 :                                 return 0;
    5157             :                         }
    5158        4852 :                         if (!sql_parse(be, trigger->t->s, trigger->statement, m_instantiate)) {
    5159        4824 :                                 stack_pop_frame(sql);
    5160        4824 :                                 return 0;
    5161             :                         }
    5162             :                 }
    5163        1826 :                 stack_pop_frame(sql);
    5164             :         }
    5165             :         return res;
    5166             : }
    5167             : 
    5168             : static void
    5169          83 : sql_insert_check(backend *be, sql_key *key, list *inserts)
    5170             : {
    5171          83 :         mvc *sql = be->mvc;
    5172          83 :         int pos = 0;
    5173          83 :         sql_rel *rel = rel_basetable(sql, key->t, key->t->base.name);
    5174          83 :         sql_exp *exp = exp_read(sql, rel, NULL, NULL, sa_strdup(sql->sa, key->check), &pos, 0);
    5175          83 :         rel->exps = rel_base_projection(sql, rel, 0);
    5176             : 
    5177             :         /* create new sub stmt with needed inserts */
    5178          83 :         list *ins = sa_list(sql->sa);
    5179         173 :         for(node *n = key->columns->h; n; n = n->next) {
    5180          90 :                 sql_kc *kc = n->data;
    5181          90 :                 stmt *in = list_fetch(inserts, kc->c->colnr);
    5182             : 
    5183          90 :                 sql_exp *e = rel_base_bind_column2(sql, rel, kc->c->t->base.name, kc->c->base.name);
    5184          90 :                 in = stmt_alias(be, in, e->alias.label, kc->c->t->base.name, kc->c->base.name);
    5185          90 :                 append(ins, in);
    5186             :         }
    5187          83 :         stmt *sub = stmt_list(be, ins);
    5188          83 :         stmt *s = exp_bin(be, exp, sub, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    5189          83 :         sql_subfunc *cnt = sql_bind_func(sql, "sys", "count", sql_bind_localtype("void"), NULL, F_AGGR, true, true);
    5190          83 :         s = stmt_uselect(be, column(be, s), stmt_bool(be, 0), cmp_equal, NULL, 0, 1);
    5191          83 :         s = stmt_aggr(be, s, NULL, NULL, cnt, 1, 0, 1);
    5192          83 :         char *msg = sa_message(sql->sa, SQLSTATE(40002) "INSERT INTO: violated constraint '%s.%s' CHECK(%s)", key->t->s->base.name, key->base.name, exp->comment);
    5193          83 :         (void)stmt_exception(be, s, msg, 00001);
    5194          83 : }
    5195             : 
    5196             : static sql_table *
    5197      109633 : sql_insert_check_null(backend *be, sql_table *t, list *inserts)
    5198             : {
    5199      109633 :         mvc *sql = be->mvc;
    5200      109633 :         node *m, *n;
    5201      109633 :         sql_subfunc *cnt = NULL;
    5202             : 
    5203      779861 :         for (n = ol_first_node(t->columns), m = inserts->h; n && m;
    5204      670228 :                 n = n->next, m = m->next) {
    5205      670228 :                 stmt *i = m->data;
    5206      670228 :                 sql_column *c = n->data;
    5207             : 
    5208      670228 :                 if (!c->null) {
    5209       24535 :                         stmt *s = i;
    5210       24535 :                         char *msg = NULL;
    5211             : 
    5212       24535 :                         if (!(s->key && s->nrcols == 0)) {
    5213       11503 :                                 s = stmt_selectnil(be, column(be, i));
    5214       11503 :                                 if (!cnt)
    5215        3641 :                                         cnt = sql_bind_func(sql, "sys", "count", sql_bind_localtype("void"), NULL, F_AGGR, true, true);
    5216       11503 :                                 s = stmt_aggr(be, s, NULL, NULL, cnt, 1, 0, 1);
    5217             :                         } else {
    5218       13032 :                                 sql_subfunc *isnil = sql_bind_func(sql, "sys", "isnull", &c->type, NULL, F_FUNC, true, true);
    5219             : 
    5220       13032 :                                 s = stmt_unop(be, i, NULL, isnil);
    5221             :                         }
    5222       24535 :                         msg = sa_message(sql->sa, SQLSTATE(40002) "INSERT INTO: NOT NULL constraint violated for column %s.%s", c->t->base.name, c->base.name);
    5223       24535 :                         (void)stmt_exception(be, s, msg, 00001);
    5224             :                 }
    5225             :         }
    5226      109633 :         return t; /* return something to say it succeeded */
    5227             : }
    5228             : 
    5229             : static stmt **
    5230      113793 : table_update_stmts(mvc *sql, sql_table *t, int *Len)
    5231             : {
    5232      113793 :         *Len = ol_length(t->columns);
    5233      113793 :         return SA_ZNEW_ARRAY(sql->sa, stmt *, *Len);
    5234             : }
    5235             : 
    5236             : static stmt *
    5237      109633 : rel2bin_insert(backend *be, sql_rel *rel, list *refs)
    5238             : {
    5239      109633 :         mvc *sql = be->mvc;
    5240      109633 :         list *l;
    5241      109633 :         stmt *inserts = NULL, *insert = NULL, *ddl = NULL, *pin = NULL, **updates, *ret = NULL, *cnt = NULL, *pos = NULL, *returning = NULL;
    5242      109633 :         int idx_ins = 0, len = 0;
    5243      109633 :         node *n, *m, *idx_m = NULL;
    5244      109633 :         sql_rel *tr = rel->l, *prel = rel->r;
    5245      109633 :         sql_table *t = NULL;
    5246             : 
    5247      109633 :         if ((rel->flag&UPD_COMP)) {  /* special case ! */
    5248           0 :                 idx_ins = 1;
    5249           0 :                 prel = rel->l;
    5250           0 :                 rel = rel->r;
    5251           0 :                 tr = rel->l;
    5252             :         }
    5253             : 
    5254      109633 :         if (tr->op == op_basetable) {
    5255      109541 :                 t = tr->l;
    5256             :         } else {
    5257          92 :                 ddl = subrel_bin(be, tr, refs);
    5258          92 :                 ddl = subrel_project(be, ddl, refs, NULL);
    5259          92 :                 if (!ddl)
    5260             :                         return NULL;
    5261          92 :                 t = rel_ddl_table_get(tr);
    5262             :         }
    5263             : 
    5264      109633 :         if (rel->r) /* first construct the inserts relation */
    5265      109633 :                 inserts = subrel_bin(be, rel->r, refs);
    5266      109633 :         inserts = subrel_project(be, inserts, refs, rel->r);
    5267             : 
    5268      109633 :         if (!inserts)
    5269             :                 return NULL;
    5270             : 
    5271      109633 :         if (idx_ins)
    5272           0 :                 pin = refs_find_rel(refs, prel);
    5273             : 
    5274      123301 :         for (n = ol_first_node(t->keys); n; n = n->next) {
    5275       13668 :                 sql_key * key = n->data;
    5276       13668 :                 if (key->type == ckey)
    5277          83 :                         sql_insert_check(be, key, inserts->op4.lval);
    5278             :         }
    5279             : 
    5280      109633 :         if (!sql_insert_check_null(be, t, inserts->op4.lval))
    5281             :                 return NULL;
    5282             : 
    5283      109633 :         updates = table_update_stmts(sql, t, &len);
    5284      779846 :         for (n = ol_first_node(t->columns), m = inserts->op4.lval->h; n && m; n = n->next, m = m->next) {
    5285      670213 :                 sql_column *c = n->data;
    5286             : 
    5287      670213 :                 updates[c->colnr] = m->data;
    5288             :         }
    5289             : 
    5290             : /* before */
    5291      109633 :         if (!sql_insert_triggers(be, t, updates, 0))
    5292        3216 :                 return sql_error(sql, 10, SQLSTATE(27000) "INSERT INTO: triggers failed for table '%s'", t->base.name);
    5293             : 
    5294      106417 :         insert = inserts->op4.lval->h->data;
    5295      106417 :         if (insert->nrcols == 0) {
    5296       74844 :                 cnt = stmt_atom_lng(be, 1);
    5297             :         } else {
    5298       31573 :                 cnt = stmt_aggr(be, insert, NULL, NULL, sql_bind_func(sql, "sys", "count", sql_bind_localtype("void"), NULL, F_AGGR, true, true), 1, 0, 1);
    5299             :         }
    5300      106417 :         insert = NULL;
    5301             : 
    5302      106417 :         l = sa_list(sql->sa);
    5303      106417 :         if (t->idxs) {
    5304      106417 :                 idx_m = m;
    5305      121593 :                 for (n = ol_first_node(t->idxs); n && m; n = n->next, m = m->next) {
    5306       15176 :                         stmt *is = m->data;
    5307       15176 :                         sql_idx *i = n->data;
    5308             : 
    5309       15176 :                         if (non_updatable_index(i->type)) /* Some indexes don't hold delta structures */
    5310         107 :                                 continue;
    5311       15069 :                         if (hash_index(i->type) && list_length(i->columns) <= 1)
    5312       15069 :                                 is = NULL;
    5313       15069 :                         if (i->key) {
    5314       13585 :                                 stmt *ckeys = sql_insert_key(be, inserts->op4.lval, i->key, is, pin);
    5315             : 
    5316       13585 :                                 list_append(l, ckeys);
    5317             :                         }
    5318       15069 :                         if (!insert)
    5319       15176 :                                 insert = is;
    5320             :                 }
    5321      106417 :                 assert(!n && !m);
    5322             :         }
    5323             : 
    5324      106417 :         if (t->s) /* only not declared tables, need this */
    5325      106399 :                 pos = stmt_claim(be, t, cnt);
    5326             : 
    5327      106416 :         if (t->idxs) {
    5328      121593 :                 for (n = ol_first_node(t->idxs), m = idx_m; n && m; n = n->next, m = m->next) {
    5329       15176 :                         stmt *is = m->data;
    5330       15176 :                         sql_idx *i = n->data;
    5331             : 
    5332       15176 :                         if (non_updatable_index(i->type)) /* Some indexes don't hold delta structures */
    5333         107 :                                 continue;
    5334       15069 :                         if (hash_index(i->type) && list_length(i->columns) <= 1)
    5335             :                                 is = NULL;
    5336        2220 :                         if (is)
    5337        2220 :                                 is = stmt_append_idx(be, i, pos, is);
    5338             :                 }
    5339      106417 :                 assert(!n && !m);
    5340             :         }
    5341             : 
    5342      106417 :         int mvc_var = be->mvc_var;
    5343      770193 :         for (n = ol_first_node(t->columns), m = inserts->op4.lval->h; n && m; n = n->next, m = m->next) {
    5344             : 
    5345      663777 :                 stmt *ins = m->data;
    5346      663777 :                 sql_column *c = n->data;
    5347             : 
    5348      663777 :                 insert = stmt_append_col(be, c, pos, ins, &mvc_var, rel->flag);
    5349      663773 :                 append(l,insert);
    5350             :         }
    5351      106416 :         be->mvc_var = mvc_var;
    5352      106416 :         if (!insert)
    5353             :                 return NULL;
    5354             : 
    5355      106416 :         if (rel->returning) {
    5356           5 :                 list* il = sa_list(sql->sa);
    5357           5 :                 sql_rel* inner = rel->l;
    5358           5 :                 assert(inner->op == op_basetable);
    5359          15 :                 for (n = inner->exps->h, m = inserts->op4.lval->h; n && m; n = n->next, m = m->next) {
    5360          10 :                         sql_exp* ce     = n->data;
    5361          10 :                         stmt*   ins     = m->data;
    5362          10 :                         stmt*   s       = stmt_rename(be, ce, ins);// label each insert statement with the corresponding col exp label
    5363          10 :                         append(il, s);
    5364             :                 }
    5365           5 :                 returning = stmt_list(be, il);
    5366           5 :                 sql->type = Q_TABLE;
    5367             :         }
    5368             : 
    5369      106416 :         if (!sql_insert_triggers(be, t, updates, 1))
    5370        1608 :                 return sql_error(sql, 10, SQLSTATE(27000) "INSERT INTO: triggers failed for table '%s'", t->base.name);
    5371             :         /* update predicate list */
    5372      104808 :         if (rel->r && !rel_predicates(be, rel->r))
    5373             :                 return NULL;
    5374             : 
    5375      104808 :         if (ddl) {
    5376          92 :                 ret = ddl;
    5377          92 :                 list_prepend(l, ddl);
    5378          92 :                 return stmt_list(be, l);
    5379             :         } else {
    5380      104716 :                 ret = cnt;
    5381      104716 :                 if (add_to_rowcount_accumulator(be, ret->nr) < 0)
    5382           0 :                         return sql_error(sql, 10, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    5383      104716 :                 if (t->s && isGlobal(t) && !isGlobalTemp(t))
    5384      104636 :                         stmt_add_dependency_change(be, t, ret);
    5385      104717 :                 return returning?returning:ret;
    5386             :         }
    5387             : }
    5388             : 
    5389             : static int
    5390        1371 : is_idx_updated(sql_idx * i, stmt **updates)
    5391             : {
    5392        1371 :         int update = 0;
    5393        1371 :         node *m;
    5394             : 
    5395        2708 :         for (m = i->columns->h; m; m = m->next) {
    5396        1543 :                 sql_kc *ic = m->data;
    5397             : 
    5398        1543 :                 if (updates[ic->c->colnr]) {
    5399             :                         update = 1;
    5400             :                         break;
    5401             :                 }
    5402             :         }
    5403        1371 :         return update;
    5404             : }
    5405             : 
    5406             : static int
    5407          14 : is_check_updated(sql_key * k, stmt **updates)
    5408             : {
    5409          14 :         int update = 0;
    5410          14 :         node *m;
    5411             : 
    5412          16 :         for (m = k->columns->h; m; m = m->next) {
    5413          14 :                 sql_kc *kc = m->data;
    5414             : 
    5415          14 :                 if (updates[kc->c->colnr]) {
    5416             :                         update = 1;
    5417             :                         break;
    5418             :                 }
    5419             :         }
    5420          14 :         return update;
    5421             : }
    5422             : 
    5423             : static int
    5424        4104 : first_updated_col(stmt **updates, int cnt)
    5425             : {
    5426        4104 :         int i;
    5427             : 
    5428       37863 :         for (i = 0; i < cnt; i++) {
    5429       36765 :                 if (updates[i])
    5430             :                         return i;
    5431             :         }
    5432             :         return -1;
    5433             : }
    5434             : 
    5435             : static stmt *
    5436         205 : update_check_ukey(backend *be, stmt **updates, sql_key *k, stmt *u_tids, stmt *idx_updates, int updcol)
    5437             : {
    5438         205 :         mvc *sql = be->mvc;
    5439         205 :         char *msg = NULL;
    5440         205 :         stmt *res = NULL;
    5441             : 
    5442         205 :         sql_subtype *lng = sql_bind_localtype("lng");
    5443         205 :         sql_subfunc *cnt = sql_bind_func(sql, "sys", "count", sql_bind_localtype("void"), NULL, F_AGGR, true, true);
    5444         205 :         sql_subtype *bt = sql_bind_localtype("bit");
    5445         205 :         sql_subfunc *ne;
    5446             : 
    5447         205 :         ne = sql_bind_func_result(sql, "sys", "<>", F_FUNC, true, bt, 2, lng, lng);
    5448         205 :         if (list_length(k->columns) > 1) {
    5449          48 :                 stmt *dels = stmt_tid(be, k->t, 0);
    5450          48 :                 node *m;
    5451          48 :                 stmt *s = NULL;
    5452             : 
    5453             :                 /* 1st stage: find out if original (without the updated)
    5454             :                         do not contain the same values as the updated values.
    5455             :                         This is done using a relation join and a count (which
    5456             :                         should be zero)
    5457             :                 */
    5458          48 :                 if (!isNew(k)) {
    5459          11 :                         stmt *nu_tids = stmt_tdiff(be, dels, u_tids, NULL); /* not updated ids */
    5460          11 :                         nu_tids = stmt_project(be, nu_tids, dels);
    5461          11 :                         list *lje = sa_list(sql->sa);
    5462          11 :                         list *rje = sa_list(sql->sa);
    5463             : 
    5464          11 :                         if (k->idx && hash_index(k->idx->type)) {
    5465          11 :                                 list_append(lje, stmt_idx(be, k->idx, nu_tids, nu_tids->partition));
    5466          11 :                                 list_append(rje, idx_updates);
    5467             :                         }
    5468          33 :                         for (m = k->columns->h; m; m = m->next) {
    5469          22 :                                 sql_kc *c = m->data;
    5470          22 :                                 stmt *upd;
    5471             : 
    5472          22 :                                 assert(updates);
    5473          22 :                                 if (updates[c->c->colnr]) {
    5474             :                                         upd = updates[c->c->colnr];
    5475             :                                 } else {
    5476           8 :                                         upd = stmt_col(be, c->c, u_tids, u_tids->partition);
    5477             :                                 }
    5478          22 :                                 list_append(lje, stmt_col(be, c->c, nu_tids, nu_tids->partition));
    5479          22 :                                 list_append(rje, upd);
    5480             :                         }
    5481          11 :                         s = releqjoin(be, lje, rje, NULL, 1 /* hash used */, 0, 0);
    5482          11 :                         s = stmt_result(be, s, 0);
    5483          11 :                         s = stmt_binop(be, stmt_aggr(be, s, NULL, NULL, cnt, 1, 0, 1), stmt_atom_lng(be, 0), NULL, ne);
    5484             :                 }
    5485             : 
    5486             :                 /* 2e stage: find out if the updated are unique */
    5487          48 :                 if (!updates || updates[updcol]->nrcols) {   /* update columns not atoms */
    5488          48 :                         sql_subfunc *sum;
    5489          48 :                         stmt *count_sum = NULL, *ssum;
    5490          48 :                         stmt *g = NULL, *grp = NULL, *ext = NULL, *Cnt = NULL;
    5491          48 :                         stmt *cand = NULL;
    5492          48 :                         stmt *ss;
    5493          48 :                         sql_subfunc *or = sql_bind_func_result(sql, "sys", "or", F_FUNC, true, bt, 2, bt, bt);
    5494             : 
    5495             :                         /* also take the hopefully unique hash keys, to reduce
    5496             :                            (re)group costs */
    5497          48 :                         if (k->idx && hash_index(k->idx->type)) {
    5498          48 :                                 g = stmt_group(be, idx_updates, grp, ext, Cnt, 0);
    5499          48 :                                 grp = stmt_result(be, g, 0);
    5500          48 :                                 ext = stmt_result(be, g, 1);
    5501          48 :                                 Cnt = stmt_result(be, g, 2);
    5502             : 
    5503             :                                 /* continue only with groups with a cnt > 1 */
    5504          48 :                                 cand = stmt_uselect(be, Cnt, stmt_atom_lng(be, 1), cmp_gt, NULL, 0, 0);
    5505             :                                 /* project cand on ext and Cnt */
    5506          48 :                                 Cnt = stmt_project(be, cand, Cnt);
    5507          48 :                                 ext = stmt_project(be, cand, ext);
    5508             : 
    5509             :                                 /* join groups with extend to retrieve all oid's of the original
    5510             :                                  * bat that belong to a group with Cnt >1 */
    5511          48 :                                 g = stmt_join(be, grp, ext, 0, cmp_equal, 0, 0, false);
    5512          48 :                                 cand = stmt_result(be, g, 0);
    5513          48 :                                 grp = stmt_project(be, cand, grp);
    5514             :                         }
    5515             : 
    5516         154 :                         for (m = k->columns->h; m; m = m->next) {
    5517         106 :                                 sql_kc *c = m->data;
    5518         106 :                                 stmt *upd;
    5519             : 
    5520         106 :                                 if (updates && updates[c->c->colnr]) {
    5521             :                                         upd = updates[c->c->colnr];
    5522             :                                 } else {
    5523          92 :                                         upd = stmt_col(be, c->c, dels, dels->partition);
    5524             :                                 }
    5525             : 
    5526             :                                 /* apply cand list first */
    5527         106 :                                 if (cand)
    5528         106 :                                         upd = stmt_project(be, cand, upd);
    5529             : 
    5530             :                                 /* remove nulls */
    5531         106 :                                 if ((k->type == ukey) && stmt_has_null(upd)) {
    5532          31 :                                         stmt *nn = stmt_selectnonil(be, upd, NULL);
    5533          31 :                                         upd = stmt_project(be, nn, upd);
    5534          31 :                                         if (grp)
    5535          31 :                                                 grp = stmt_project(be, nn, grp);
    5536          31 :                                         if (cand)
    5537          31 :                                                 cand = stmt_project(be, nn, cand);
    5538             :                                 }
    5539             : 
    5540             :                                 /* apply group by on groups with Cnt > 1 */
    5541         106 :                                 g = stmt_group(be, upd, grp, ext, Cnt, !m->next);
    5542         106 :                                 grp = stmt_result(be, g, 0);
    5543         106 :                                 ext = stmt_result(be, g, 1);
    5544         106 :                                 Cnt = stmt_result(be, g, 2);
    5545             :                         }
    5546          48 :                         ss = Cnt; /* use count */
    5547             :                         /* (count(ss) <> sum(ss)) */
    5548          48 :                         sum = sql_bind_func(sql, "sys", "sum", lng, NULL, F_AGGR, true, true);
    5549          48 :                         ssum = stmt_aggr(be, ss, NULL, NULL, sum, 1, 0, 1);
    5550          48 :                         ssum = sql_Nop_(be, "ifthenelse", sql_unop_(be, "isnull", ssum), stmt_atom_lng(be, 0), ssum, NULL);
    5551          48 :                         count_sum = stmt_binop(be, stmt_aggr(be, ss, NULL, NULL, cnt, 1, 0, 1), check_types(be, lng, ssum, type_equal), NULL, ne);
    5552             : 
    5553             :                         /* combine results */
    5554          48 :                         if (s)
    5555          11 :                                 s = stmt_binop(be, s, count_sum, NULL, or);
    5556             :                         else
    5557             :                                 s = count_sum;
    5558             :                 }
    5559             : 
    5560          48 :                 if (k->type == pkey) {
    5561          31 :                         msg = sa_message(sql->sa, SQLSTATE(40002) "UPDATE: PRIMARY KEY constraint '%s.%s' violated", k->t->base.name, k->base.name);
    5562             :                 } else {
    5563          17 :                         msg = sa_message(sql->sa, SQLSTATE(40002) "UPDATE: UNIQUE constraint '%s.%s' violated", k->t->base.name, k->base.name);
    5564             :                 }
    5565          48 :                 res = stmt_exception(be, s, msg, 00001);
    5566             :         } else {                /* single column key */
    5567         157 :                 stmt *dels = stmt_tid(be, k->t, 0);
    5568         157 :                 sql_kc *c = k->columns->h->data;
    5569         157 :                 stmt *s = NULL, *h = NULL, *o;
    5570             : 
    5571             :                 /* s should be empty */
    5572         157 :                 if (!isNew(k)) {
    5573          69 :                         stmt *nu_tids = stmt_tdiff(be, dels, u_tids, NULL); /* not updated ids */
    5574          69 :                         nu_tids = stmt_project(be, nu_tids, dels);
    5575          69 :                         assert (updates);
    5576             : 
    5577          69 :                         h = updates[c->c->colnr];
    5578          69 :                         o = stmt_col(be, c->c, nu_tids, nu_tids->partition);
    5579          69 :                         s = stmt_join(be, o, h, 0, cmp_equal, 0, 0, false);
    5580          69 :                         s = stmt_result(be, s, 0);
    5581          69 :                         s = stmt_binop(be, stmt_aggr(be, s, NULL, NULL, cnt, 1, 0, 1), stmt_atom_lng(be, 0), NULL, ne);
    5582             :                 }
    5583             : 
    5584             :                 /* 2e stage: find out if updated are unique */
    5585          69 :                 if (!h || h->nrcols) {       /* update columns not atoms */
    5586         157 :                         sql_subfunc *sum;
    5587         157 :                         stmt *count_sum = NULL;
    5588         157 :                         sql_subfunc *or = sql_bind_func_result(sql, "sys", "or", F_FUNC, true, bt, 2, bt, bt);
    5589         157 :                         stmt *ssum, *ss;
    5590         157 :                         stmt *upd;
    5591         157 :                         stmt *g;
    5592             : 
    5593         157 :                         if (updates) {
    5594          70 :                                 upd = updates[c->c->colnr];
    5595             :                         } else {
    5596          87 :                                 upd = stmt_col(be, c->c, dels, dels->partition);
    5597             :                         }
    5598             : 
    5599             :                         /* remove nulls */
    5600         157 :                         if ((k->type == ukey) && stmt_has_null(upd)) {
    5601          15 :                                 stmt *nn = stmt_selectnonil(be, upd, NULL);
    5602          15 :                                 upd = stmt_project(be, nn, upd);
    5603             :                         }
    5604             : 
    5605         157 :                         g = stmt_group(be, upd, NULL, NULL, NULL, 1);
    5606         157 :                         ss = stmt_result(be, g, 2); /* use count */
    5607             : 
    5608             :                         /* (count(ss) <> sum(ss)) */
    5609         157 :                         sum = sql_bind_func(sql, "sys", "sum", lng, NULL, F_AGGR, true, true);
    5610         157 :                         ssum = stmt_aggr(be, ss, NULL, NULL, sum, 1, 0, 1);
    5611         157 :                         ssum = sql_Nop_(be, "ifthenelse", sql_unop_(be, "isnull", ssum), stmt_atom_lng(be, 0), ssum, NULL);
    5612         157 :                         count_sum = stmt_binop(be, check_types(be, tail_type(ssum), stmt_aggr(be, ss, NULL, NULL, cnt, 1, 0, 1), type_equal), ssum, NULL, ne);
    5613             : 
    5614             :                         /* combine results */
    5615         157 :                         if (s)
    5616          69 :                                 s = stmt_binop(be, s, count_sum, NULL, or);
    5617             :                         else
    5618             :                                 s = count_sum;
    5619             :                 }
    5620             : 
    5621         157 :                 if (k->type == pkey) {
    5622         140 :                         msg = sa_message(sql->sa, SQLSTATE(40002) "UPDATE: PRIMARY KEY constraint '%s.%s' violated", k->t->base.name, k->base.name);
    5623             :                 } else {
    5624          17 :                         msg = sa_message(sql->sa, SQLSTATE(40002) "UPDATE: UNIQUE constraint '%s.%s' violated", k->t->base.name, k->base.name);
    5625             :                 }
    5626         157 :                 res = stmt_exception(be, s, msg, 00001);
    5627             :         }
    5628         205 :         return res;
    5629             : }
    5630             : 
    5631             : /*
    5632             :          A referential constraint is satisfied if one of the following con-
    5633             :          ditions is true, depending on the <match option> specified in the
    5634             :          <referential constraint definition>:
    5635             : 
    5636             :          -  If no <match type> was specified then, for each row R1 of the
    5637             :             referencing table, either at least one of the values of the
    5638             :             referencing columns in R1 shall be a null value, or the value of
    5639             :             each referencing column in R1 shall be equal to the value of the
    5640             :             corresponding referenced column in some row of the referenced
    5641             :             table.
    5642             : 
    5643             :          -  If MATCH FULL was specified then, for each row R1 of the refer-
    5644             :             encing table, either the value of every referencing column in R1
    5645             :             shall be a null value, or the value of every referencing column
    5646             :             in R1 shall not be null and there shall be some row R2 of the
    5647             :             referenced table such that the value of each referencing col-
    5648             :             umn in R1 is equal to the value of the corresponding referenced
    5649             :             column in R2.
    5650             : 
    5651             :          -  If MATCH PARTIAL was specified then, for each row R1 of the
    5652             :             referencing table, there shall be some row R2 of the refer-
    5653             :             enced table such that the value of each referencing column in
    5654             :             R1 is either null or is equal to the value of the corresponding
    5655             :             referenced column in R2.
    5656             : */
    5657             : 
    5658             : static stmt *
    5659         698 : update_check_fkey(backend *be, stmt **updates, sql_key *k, stmt *tids, stmt *idx_updates, int updcol, stmt *pup)
    5660             : {
    5661         698 :         mvc *sql = be->mvc;
    5662         698 :         char *msg = NULL;
    5663         698 :         stmt *s, *cur, *null = NULL, *cntnulls;
    5664         698 :         sql_subtype *lng = sql_bind_localtype("lng"), *bt = sql_bind_localtype("bit");
    5665         698 :         sql_subfunc *cnt = sql_bind_func(sql, "sys", "count", sql_bind_localtype("void"), NULL, F_AGGR, true, true);
    5666         698 :         sql_subfunc *ne = sql_bind_func_result(sql, "sys", "<>", F_FUNC, true, bt, 2, lng, lng);
    5667         698 :         sql_subfunc *or = sql_bind_func_result(sql, "sys", "or", F_FUNC, true, bt, 2, bt, bt);
    5668         698 :         node *m;
    5669             : 
    5670         698 :         if (!idx_updates)
    5671             :                 return NULL;
    5672             :         /* releqjoin.count <> updates[updcol].count */
    5673         698 :         if (pup && list_length(pup->op4.lval)) {
    5674         655 :                 cur = pup->op4.lval->h->data;
    5675          43 :         } else if (updates) {
    5676          43 :                 cur = updates[updcol];
    5677             :         } else {
    5678           0 :                 sql_kc *c = k->columns->h->data;
    5679           0 :                 stmt *dels = stmt_tid(be, k->t, 0);
    5680           0 :                 assert(0);
    5681             :                 cur = stmt_col(be, c->c, dels, dels->partition);
    5682             :         }
    5683         698 :         s = stmt_binop(be, stmt_aggr(be, idx_updates, NULL, NULL, cnt, 1, 0, 1), stmt_aggr(be, cur, NULL, NULL, cnt, 1, 0, 1), NULL, ne);
    5684             : 
    5685        1419 :         for (m = k->columns->h; m; m = m->next) {
    5686         721 :                 sql_kc *c = m->data;
    5687             : 
    5688             :                 /* FOR MATCH FULL/SIMPLE/PARTIAL see above */
    5689             :                 /* Currently only the default MATCH SIMPLE is supported */
    5690         721 :                 if (c->c->null) {
    5691         309 :                         stmt *upd, *nn;
    5692             : 
    5693         309 :                         if (updates && updates[c->c->colnr]) {
    5694             :                                 upd = updates[c->c->colnr];
    5695             :                         } else { /* created idx/key using alter */
    5696         277 :                                 upd = stmt_col(be, c->c, tids, tids->partition);
    5697             :                         }
    5698         309 :                         nn = stmt_selectnil(be, upd);
    5699         309 :                         if (null)
    5700          11 :                                 null = stmt_tunion(be, null, nn);
    5701             :                         else
    5702             :                                 null = nn;
    5703             :                 }
    5704             :         }
    5705         698 :         if (null) {
    5706         298 :                 cntnulls = stmt_aggr(be, null, NULL, NULL, cnt, 1, 0, 1);
    5707             :         } else {
    5708         400 :                 cntnulls = stmt_atom_lng(be, 0);
    5709             :         }
    5710         698 :         s = stmt_binop(be, s,
    5711             :                 stmt_binop(be, stmt_aggr(be, stmt_selectnil(be, idx_updates), NULL, NULL, cnt, 1, 0, 1), cntnulls, NULL, ne), NULL, or);
    5712             : 
    5713             :         /* s should be empty */
    5714         698 :         msg = sa_message(sql->sa, SQLSTATE(40002) "UPDATE: FOREIGN KEY constraint '%s.%s' violated", k->t->base.name, k->base.name);
    5715         698 :         return stmt_exception(be, s, msg, 00001);
    5716             : }
    5717             : 
    5718             : static stmt *
    5719          10 : join_updated_pkey(backend *be, sql_key * k, stmt *tids, stmt **updates)
    5720             : {
    5721          10 :         mvc *sql = be->mvc;
    5722          10 :         sql_trans *tr = sql->session->tr;
    5723          10 :         char *msg = NULL;
    5724          10 :         int nulls = 0;
    5725          10 :         node *m, *o;
    5726          10 :         sql_key *rk = (sql_key*)os_find_id(tr->cat->objects, tr, ((sql_fkey*)k)->rkey);
    5727          10 :         stmt *s = NULL, *dels = stmt_tid(be, rk->t, 0), *fdels, *cnteqjoin;
    5728          10 :         stmt *null = NULL, *rows;
    5729          10 :         sql_subtype *lng = sql_bind_localtype("lng");
    5730          10 :         sql_subtype *bt = sql_bind_localtype("bit");
    5731          10 :         sql_subfunc *cnt = sql_bind_func(sql, "sys", "count", sql_bind_localtype("void"), NULL, F_AGGR, true, true);
    5732          10 :         sql_subfunc *ne = sql_bind_func_result(sql, "sys", "<>", F_FUNC, true, bt, 2, lng, lng);
    5733          10 :         list *lje = sa_list(sql->sa);
    5734          10 :         list *rje = sa_list(sql->sa);
    5735             : 
    5736          10 :         fdels = stmt_tid(be, k->idx->t, 0);
    5737          10 :         rows = stmt_idx(be, k->idx, fdels, fdels->partition);
    5738             : 
    5739          10 :         rows = stmt_join(be, rows, tids, 0, cmp_equal, 0, 0, false); /* join over the join index */
    5740          10 :         rows = stmt_result(be, rows, 0);
    5741             : 
    5742          22 :         for (m = k->idx->columns->h, o = rk->columns->h; m && o; m = m->next, o = o->next) {
    5743          12 :                 sql_kc *fc = m->data;
    5744          12 :                 sql_kc *c = o->data;
    5745          12 :                 stmt *upd, *col;
    5746             : 
    5747          12 :                 if (updates[c->c->colnr]) {
    5748             :                         upd = updates[c->c->colnr];
    5749             :                 } else {
    5750           2 :                         upd = stmt_project(be, tids, stmt_col(be, c->c, dels, dels->partition));
    5751             :                 }
    5752          12 :                 if (c->c->null) { /* new nulls (MATCH SIMPLE) */
    5753           3 :                         stmt *nn = stmt_selectnil(be, upd);
    5754           3 :                         if (null)
    5755           1 :                                 null = stmt_tunion(be, null, nn);
    5756             :                         else
    5757             :                                 null = nn;
    5758             :                         nulls = 1;
    5759             :                 }
    5760          12 :                 col = stmt_col(be, fc->c, rows, rows->partition);
    5761          12 :                 if (!upd || (upd = check_types(be, &fc->c->type, upd, type_equal)) == NULL)
    5762           0 :                         return NULL;
    5763          12 :                 list_append(lje, upd);
    5764          12 :                 list_append(rje, col);
    5765             :         }
    5766          10 :         s = releqjoin(be, lje, rje, NULL, 1 /* hash used */, 0, 0);
    5767          10 :         s = stmt_result(be, s, 0);
    5768             : 
    5769             :         /* add missing nulls */
    5770          10 :         cnteqjoin = stmt_aggr(be, s, NULL, NULL, cnt, 1, 0, 1);
    5771          10 :         if (nulls) {
    5772           2 :                 sql_subfunc *add = sql_bind_func_result(sql, "sys", "sql_add", F_FUNC, true, lng, 2, lng, lng);
    5773           2 :                 cnteqjoin = stmt_binop(be, cnteqjoin, stmt_aggr(be, null, NULL, NULL, cnt, 1, 0, 1), NULL, add);
    5774             :         }
    5775             : 
    5776             :         /* releqjoin.count <> updates[updcol].count */
    5777          10 :         s = stmt_binop(be, cnteqjoin, stmt_aggr(be, rows, NULL, NULL, cnt, 1, 0, 1), NULL, ne);
    5778             : 
    5779             :         /* s should be empty */
    5780          10 :         msg = sa_message(sql->sa, SQLSTATE(40002) "UPDATE: FOREIGN KEY constraint '%s.%s' violated", k->t->base.name, k->base.name);
    5781          10 :         return stmt_exception(be, s, msg, 00001);
    5782             : }
    5783             : 
    5784             : static list * sql_update(backend *be, sql_table *t, stmt *rows, stmt **updates);
    5785             : 
    5786             : static stmt*
    5787          11 : sql_delete_set_Fkeys(backend *be, sql_key *k, stmt *ftids /* to be updated rows of fkey table */, int action)
    5788             : {
    5789          11 :         mvc *sql = be->mvc;
    5790          11 :         sql_trans *tr = sql->session->tr;
    5791          11 :         list *l = NULL;
    5792          11 :         int len = 0;
    5793          11 :         node *m, *o;
    5794          11 :         sql_key *rk = (sql_key*)os_find_id(tr->cat->objects, tr, ((sql_fkey*)k)->rkey);
    5795          11 :         stmt **new_updates;
    5796          11 :         sql_table *t = mvc_bind_table(sql, k->t->s, k->t->base.name);
    5797             : 
    5798          11 :         new_updates = table_update_stmts(sql, t, &len);
    5799          24 :         for (m = k->idx->columns->h, o = rk->columns->h; m && o; m = m->next, o = o->next) {
    5800          13 :                 sql_kc *fc = m->data;
    5801          13 :                 stmt *upd = NULL;
    5802             : 
    5803          13 :                 if (action == ACT_SET_DEFAULT) {
    5804           4 :                         if (fc->c->def) {
    5805           4 :                                 stmt *sq = parse_value(be, fc->c->t->s, fc->c->def, &fc->c->type, sql->emode);
    5806           4 :                                 if (!sq)
    5807             :                                         return NULL;
    5808             :                                 upd = sq;
    5809             :                         } else {
    5810           0 :                                 upd = stmt_atom(be, atom_general(sql->sa, &fc->c->type, NULL, 0));
    5811             :                         }
    5812             :                 } else {
    5813           9 :                         upd = stmt_atom(be, atom_general(sql->sa, &fc->c->type, NULL, 0));
    5814             :                 }
    5815             : 
    5816          13 :                 if (!upd || (upd = check_types(be, &fc->c->type, upd, type_equal)) == NULL)
    5817           0 :                         return NULL;
    5818             : 
    5819          13 :                 if (upd->nrcols <= 0)
    5820          13 :                         upd = stmt_const(be, ftids, upd);
    5821             : 
    5822          13 :                 new_updates[fc->c->colnr] = upd;
    5823             :         }
    5824          11 :         if ((l = sql_update(be, t, ftids, new_updates)) == NULL)
    5825             :                 return NULL;
    5826          11 :         return stmt_list(be, l);
    5827             : }
    5828             : 
    5829             : static stmt*
    5830          32 : sql_update_cascade_Fkeys(backend *be, sql_key *k, stmt *utids, stmt **updates, int action)
    5831             : {
    5832          32 :         mvc *sql = be->mvc;
    5833          32 :         sql_trans *tr = sql->session->tr;
    5834          32 :         list *l = NULL;
    5835          32 :         int len = 0;
    5836          32 :         node *m, *o;
    5837          32 :         sql_key *rk = (sql_key*)os_find_id(tr->cat->objects, tr, ((sql_fkey*)k)->rkey);
    5838          32 :         stmt **new_updates;
    5839          32 :         stmt *rows;
    5840          32 :         sql_table *t = mvc_bind_table(sql, k->t->s, k->t->base.name);
    5841          32 :         stmt *ftids, *upd_ids;
    5842             : 
    5843          32 :         ftids = stmt_tid(be, k->idx->t, 0);
    5844          32 :         rows = stmt_idx(be, k->idx, ftids, ftids->partition);
    5845             : 
    5846          32 :         rows = stmt_join(be, rows, utids, 0, cmp_equal, 0, 0, false); /* join over the join index */
    5847          32 :         upd_ids = stmt_result(be, rows, 1);
    5848          32 :         rows = stmt_result(be, rows, 0);
    5849          32 :         rows = stmt_project(be, rows, ftids);
    5850             : 
    5851          32 :         new_updates = table_update_stmts(sql, t, &len);
    5852          64 :         for (m = k->idx->columns->h, o = rk->columns->h; m && o; m = m->next, o = o->next) {
    5853          32 :                 sql_kc *fc = m->data;
    5854          32 :                 sql_kc *c = o->data;
    5855          32 :                 stmt *upd = NULL;
    5856             : 
    5857          32 :                 if (!updates[c->c->colnr]) {
    5858           0 :                         continue;
    5859          32 :                 } else if (action == ACT_CASCADE) {
    5860             :                         upd = updates[c->c->colnr];
    5861          16 :                 } else if (action == ACT_SET_DEFAULT) {
    5862           6 :                         if (fc->c->def) {
    5863           6 :                                 stmt *sq = parse_value(be, fc->c->t->s, fc->c->def, &fc->c->type, sql->emode);
    5864           6 :                                 if (!sq)
    5865             :                                         return NULL;
    5866             :                                 upd = sq;
    5867             :                         } else {
    5868           0 :                                 upd = stmt_atom(be, atom_general(sql->sa, &fc->c->type, NULL, 0));
    5869             :                         }
    5870          10 :                 } else if (action == ACT_SET_NULL) {
    5871          10 :                         upd = stmt_atom(be, atom_general(sql->sa, &fc->c->type, NULL, 0));
    5872             :                 }
    5873             : 
    5874          32 :                 if (!upd || (upd = check_types(be, &fc->c->type, upd, type_equal)) == NULL)
    5875           0 :                         return NULL;
    5876             : 
    5877          32 :                 if (upd->nrcols <= 0)
    5878          16 :                         upd = stmt_const(be, upd_ids, upd);
    5879             :                 else
    5880          16 :                         upd = stmt_project(be, upd_ids, upd);
    5881             : 
    5882          32 :                 new_updates[fc->c->colnr] = upd;
    5883             :         }
    5884             : 
    5885          32 :         if ((l = sql_update(be, t, rows, new_updates)) == NULL)
    5886             :                 return NULL;
    5887          32 :         return stmt_list(be, l);
    5888             : }
    5889             : 
    5890             : static int
    5891          81 : cascade_ukey(backend *be, stmt **updates, sql_key *k, stmt *tids)
    5892             : {
    5893             :         /* now iterate over all keys */
    5894          81 :         sql_trans *tr = be->mvc->session->tr;
    5895          81 :         list *keys = sql_trans_get_dependents(tr, k->base.id, FKEY_DEPENDENCY, NULL);
    5896          81 :         if (keys) {
    5897         131 :                 for (node *n = keys->h; n; n = n->next->next) {
    5898          50 :                         sqlid fkey_id = *(sqlid*)n->data;
    5899          50 :                         sql_base *b = os_find_id(tr->cat->objects, tr, fkey_id);
    5900          50 :                         sql_key *fk = (sql_key*)b;
    5901          50 :                         sql_fkey *rk = (sql_fkey*)b;
    5902             : 
    5903          50 :                         if (fk->type != fkey || rk->rkey != k->base.id)
    5904           0 :                                 continue;
    5905             : 
    5906             :                         /* All rows of the foreign key table which are
    5907             :                            affected by the primary key update should all
    5908             :                            match one of the updated primary keys again.
    5909             :                          */
    5910          50 :                         switch (((sql_fkey*)fk)->on_update) {
    5911             :                         case ACT_NO_ACTION:
    5912             :                                 break;
    5913          32 :                         case ACT_SET_NULL:
    5914             :                         case ACT_SET_DEFAULT:
    5915             :                         case ACT_CASCADE:
    5916          32 :                                 if (!sql_update_cascade_Fkeys(be, fk, tids, updates, ((sql_fkey*)fk)->on_update)) {
    5917           0 :                                         list_destroy(keys);
    5918           0 :                                         return -1;
    5919             :                                 }
    5920             :                                 break;
    5921          10 :                         default:        /*RESTRICT*/
    5922          10 :                                 if (!join_updated_pkey(be, fk, tids, updates)) {
    5923           0 :                                         list_destroy(keys);
    5924           0 :                                         return -1;
    5925             :                                 }
    5926             :                         }
    5927             :                 }
    5928          81 :                 list_destroy(keys);
    5929             :         }
    5930             :         return 0;
    5931             : }
    5932             : 
    5933             : static void
    5934         903 : sql_update_check_key(backend *be, stmt **updates, sql_key *k, stmt *tids, stmt *idx_updates, int updcol, list *l, stmt *pup)
    5935             : {
    5936         903 :         stmt *ckeys;
    5937             : 
    5938         903 :         if (k->type == pkey || k->type == ukey) {
    5939         205 :                 ckeys = update_check_ukey(be, updates, k, tids, idx_updates, updcol);
    5940             :         } else { /* foreign keys */
    5941         698 :                 ckeys = update_check_fkey(be, updates, k, tids, idx_updates, updcol, pup);
    5942             :         }
    5943         903 :         list_append(l, ckeys);
    5944         903 : }
    5945             : 
    5946             : static stmt *
    5947          20 : hash_update(backend *be, sql_idx * i, stmt *rows, stmt **updates, int updcol)
    5948             : {
    5949          20 :         mvc *sql = be->mvc;
    5950             :         /* calculate new value */
    5951          20 :         node *m;
    5952          20 :         sql_subtype *it, *lng;
    5953          20 :         int bits = 1 + ((sizeof(lng)*8)-1)/(list_length(i->columns)+1);
    5954          20 :         stmt *h = NULL, *tids;
    5955             : 
    5956          20 :         if (list_length(i->columns) <= 1)
    5957             :                 return NULL;
    5958             : 
    5959           0 :         tids = stmt_tid(be, i->t, 0);
    5960           0 :         it = sql_bind_localtype("int");
    5961           0 :         lng = sql_bind_localtype("lng");
    5962           0 :         for (m = i->columns->h; m; m = m->next) {
    5963           0 :                 sql_kc *c = m->data;
    5964           0 :                 stmt *upd;
    5965             : 
    5966           0 :                 if (updates && updates[c->c->colnr]) {
    5967             :                         upd = updates[c->c->colnr];
    5968           0 :                 } else if (updates && updcol >= 0) {
    5969           0 :                         assert(0);
    5970             :                         upd = stmt_col(be, c->c, rows, rows->partition);
    5971             :                 } else { /* created idx/key using alter */
    5972           0 :                         upd = stmt_col(be, c->c, tids, tids->partition);
    5973             :                 }
    5974             : 
    5975           0 :                 if (h && i->type == hash_idx)  {
    5976           0 :                         sql_subfunc *xor = sql_bind_func_result(sql, "sys", "rotate_xor_hash", F_FUNC, true, lng, 3, lng, it, &c->c->type);
    5977             : 
    5978           0 :                         h = stmt_Nop(be, stmt_list(be, list_append(list_append(
    5979             :                                 list_append(sa_list(sql->sa), h),
    5980           0 :                                 stmt_atom_int(be, bits)),  upd)), NULL,
    5981             :                                 xor, NULL);
    5982           0 :                 } else if (h)  {
    5983           0 :                         stmt *h2;
    5984           0 :                         sql_subfunc *lsh = sql_bind_func_result(sql, "sys", "left_shift", F_FUNC, true, lng, 2, lng, it);
    5985           0 :                         sql_subfunc *lor = sql_bind_func_result(sql, "sys", "bit_or", F_FUNC, true, lng, 2, lng, lng);
    5986           0 :                         sql_subfunc *hf = sql_bind_func_result(sql, "sys", "hash", F_FUNC, true, lng, 1, &c->c->type);
    5987             : 
    5988           0 :                         h = stmt_binop(be, h, stmt_atom_int(be, bits), NULL, lsh);
    5989           0 :                         h2 = stmt_unop(be, upd, NULL, hf);
    5990           0 :                         h = stmt_binop(be, h, h2, NULL, lor);
    5991             :                 } else {
    5992           0 :                         sql_subfunc *hf = sql_bind_func_result(sql, "sys", "hash", F_FUNC, true, lng, 1, &c->c->type);
    5993           0 :                         h = stmt_unop(be, upd, NULL, hf);
    5994           0 :                         if (i->type == oph_idx)
    5995             :                                 break;
    5996             :                 }
    5997             :         }
    5998             :         return h;
    5999             : }
    6000             : 
    6001             : static stmt *
    6002          43 : join_idx_update(backend *be, sql_idx * i, stmt *ftids, stmt **updates, int updcol)
    6003             : {
    6004          43 :         mvc *sql = be->mvc;
    6005          43 :         sql_trans *tr = sql->session->tr;
    6006          43 :         node *m, *o;
    6007          43 :         sql_key *rk = (sql_key*)os_find_id(tr->cat->objects, tr, ((sql_fkey*)i->key)->rkey);
    6008          43 :         stmt *s = NULL, *ptids = stmt_tid(be, rk->t, 0), *l, *r;
    6009          43 :         list *lje = sa_list(sql->sa);
    6010          43 :         list *rje = sa_list(sql->sa);
    6011             : 
    6012          88 :         for (m = i->columns->h, o = rk->columns->h; m && o; m = m->next, o = o->next) {
    6013          45 :                 sql_kc *c = m->data;
    6014          45 :                 sql_kc *rc = o->data;
    6015          45 :                 stmt *upd;
    6016             : 
    6017          45 :                 if (updates && updates[c->c->colnr]) {
    6018             :                         upd = updates[c->c->colnr];
    6019           0 :                 } else if (updates && updcol >= 0) {
    6020           0 :                         assert(0);
    6021             :                         upd = stmt_col(be, c->c, ftids, ftids->partition);
    6022             :                 } else { /* created idx/key using alter */
    6023           0 :                         upd = stmt_col(be, c->c, ftids, ftids->partition);
    6024             :                 }
    6025             : 
    6026          45 :                 if (!upd || (upd = check_types(be, &rc->c->type, upd, type_equal)) == NULL)
    6027           0 :                         return NULL;
    6028          45 :                 list_append(lje, upd);
    6029          45 :                 list_append(rje, stmt_col(be, rc->c, ptids, ptids->partition));
    6030             :         }
    6031          43 :         s = releqjoin(be, lje, rje, NULL, 0 /* use hash */, 0, 0);
    6032          43 :         l = stmt_result(be, s, 0);
    6033          43 :         r = stmt_result(be, s, 1);
    6034          43 :         r = stmt_project(be, r, ptids);
    6035          43 :         return stmt_left_project(be, ftids, l, r);
    6036             : }
    6037             : 
    6038             : static int
    6039        4104 : cascade_updates(backend *be, sql_table *t, stmt *rows, stmt **updates)
    6040             : {
    6041        4104 :         mvc *sql = be->mvc;
    6042        4104 :         node *n;
    6043             : 
    6044        4104 :         if (!ol_length(t->idxs))
    6045             :                 return 0;
    6046             : 
    6047        2541 :         for (n = ol_first_node(t->idxs); n; n = n->next) {
    6048        1308 :                 sql_idx *i = n->data;
    6049             : 
    6050             :                 /* check if update is needed,
    6051             :                  * ie at least on of the idx columns is updated
    6052             :                  */
    6053        1308 :                 if (is_idx_updated(i, updates) == 0)
    6054        1165 :                         continue;
    6055             : 
    6056         143 :                 if (i->key) {
    6057         140 :                         if (!(sql->cascade_action && list_find_id(sql->cascade_action, i->key->base.id))) {
    6058         140 :                                 sql_key *k = i->key;
    6059         140 :                                 sqlid *local_id = SA_NEW(sql->sa, sqlid);
    6060         140 :                                 if (!sql->cascade_action)
    6061          65 :                                         sql->cascade_action = sa_list(sql->sa);
    6062         140 :                                 *local_id = i->key->base.id;
    6063         140 :                                 list_append(sql->cascade_action, local_id);
    6064         140 :                                 if (k->type == pkey || k->type == ukey) {
    6065          81 :                                         if (cascade_ukey(be, updates, k, rows))
    6066             :                                                 return -1;
    6067             :                                 }
    6068             :                         }
    6069             :                 }
    6070             :         }
    6071             :         return 0;
    6072             : }
    6073             : 
    6074             : static list *
    6075          43 : update_idxs_and_check_keys(backend *be, sql_table *t, stmt *rows, stmt **updates, list *l, stmt *pup)
    6076             : {
    6077          43 :         mvc *sql = be->mvc;
    6078          43 :         node *n;
    6079          43 :         int updcol;
    6080          43 :         list *idx_updates = sa_list(sql->sa);
    6081             : 
    6082          43 :         if (!ol_length(t->idxs))
    6083             :                 return idx_updates;
    6084             : 
    6085          43 :         updcol = first_updated_col(updates, ol_length(t->columns));
    6086         106 :         for (n = ol_first_node(t->idxs); n; n = n->next) {
    6087          63 :                 sql_idx *i = n->data;
    6088          63 :                 stmt *is = NULL;
    6089             : 
    6090             :                 /* check if update is needed,
    6091             :                  * ie at least on of the idx columns is updated
    6092             :                  */
    6093          63 :                 if (is_idx_updated(i, updates) == 0)
    6094           0 :                         continue;
    6095             : 
    6096          63 :                 if (hash_index(i->type)) {
    6097          20 :                         is = hash_update(be, i, rows, updates, updcol);
    6098          43 :                 } else if (i->type == join_idx) {
    6099          43 :                         if (updcol < 0)
    6100             :                                 return NULL;
    6101          43 :                         if (!(is = join_idx_update(be, i, rows, updates, updcol)))
    6102             :                                 return NULL;
    6103             :                 }
    6104          63 :                 if (i->key)
    6105          63 :                         sql_update_check_key(be, updates, i->key, rows, is, updcol, l, pup);
    6106          63 :                 if (is)
    6107          43 :                         list_append(idx_updates, stmt_update_idx(be, i, rows, is));
    6108             :         }
    6109             :         return idx_updates;
    6110             : }
    6111             : 
    6112             : static int
    6113         261 : sql_stack_add_updated(mvc *sql, const char *on, const char *nn, sql_table *t, stmt *tids, stmt **updates)
    6114             : {
    6115             :         /* Put single relation of updates and old values on to the stack */
    6116         261 :         sql_rel *r = NULL;
    6117         261 :         node *n;
    6118         261 :         list *exps = sa_list(sql->sa);
    6119         261 :         trigger_input *ti = SA_NEW(sql->sa, trigger_input);
    6120             : 
    6121         261 :         ti->t = t;
    6122         261 :         ti->tids = tids;
    6123         261 :         ti->updates = updates;
    6124         261 :         ti->type = 2;
    6125         261 :         ti->on = on;
    6126         261 :         ti->nn = nn;
    6127        1459 :         for (n = ol_first_node(t->columns); n; n = n->next) {
    6128        1198 :                 sql_column *c = n->data;
    6129             : 
    6130        1198 :                 if (updates[c->colnr]) {
    6131         261 :                         sql_exp *oe = exp_column(sql->sa, on, c->base.name, &c->type, CARD_MULTI, c->null, is_column_unique(c), 0);
    6132         261 :                         sql_exp *ne = exp_column(sql->sa, nn, c->base.name, &c->type, CARD_MULTI, c->null, is_column_unique(c), 0);
    6133         261 :                         oe->alias.label = -(sql->nid++);
    6134         261 :                         ne->alias.label = -(sql->nid++);
    6135             : 
    6136         261 :                         append(exps, oe);
    6137         261 :                         append(exps, ne);
    6138             :                 } else {
    6139         937 :                         sql_exp *oe = exp_column(sql->sa, on, c->base.name, &c->type, CARD_MULTI, c->null, is_column_unique(c), 0);
    6140         937 :                         sql_exp *ne = exp_column(sql->sa, nn, c->base.name, &c->type, CARD_MULTI, c->null, is_column_unique(c), 0);
    6141         937 :                         oe->alias.label = -(sql->nid++);
    6142         937 :                         ne->alias.label = -(sql->nid++);
    6143             : 
    6144         937 :                         append(exps, oe);
    6145         937 :                         append(exps, ne);
    6146             :                 }
    6147             :         }
    6148         261 :         r = rel_table_func(sql->sa, NULL, NULL, exps, TRIGGER_WRAPPER);
    6149         261 :         r->l = ti;
    6150             : 
    6151             :         /* put single table into the stack with 2 names, needed for the psm code */
    6152         261 :         if (!stack_push_rel_view(sql, on, r) || !stack_push_rel_view(sql, nn, rel_dup(r)))
    6153           0 :                 return 0;
    6154             :         return 1;
    6155             : }
    6156             : 
    6157             : static int
    6158        8208 : sql_update_triggers(backend *be, sql_table *t, stmt *tids, stmt **updates, int time)
    6159             : {
    6160        8208 :         mvc *sql = be->mvc;
    6161        8208 :         node *n;
    6162        8208 :         int res = 1;
    6163             : 
    6164        8208 :         if (!ol_length(t->triggers))
    6165             :                 return res;
    6166             : 
    6167        1016 :         for (n = ol_first_node(t->triggers); n; n = n->next) {
    6168         542 :                 sql_trigger *trigger = n->data;
    6169             : 
    6170         542 :                 if (!stack_push_frame(sql, "%OLD-NEW"))
    6171             :                         return 0;
    6172         542 :                 if (trigger->event == 2 && trigger->time == time) {
    6173             :                         /* add name for the 'inserted' to the stack */
    6174         261 :                         const char *n = trigger->new_name;
    6175         261 :                         const char *o = trigger->old_name;
    6176             : 
    6177         261 :                         if (!n) n = "new";
    6178         261 :                         if (!o) o = "old";
    6179             : 
    6180         261 :                         if(!sql_stack_add_updated(sql, o, n, t, tids, updates)) {
    6181           0 :                                 stack_pop_frame(sql);
    6182           0 :                                 return 0;
    6183             :                         }
    6184             : 
    6185         261 :                         if (!sql_parse(be, trigger->t->s, trigger->statement, m_instantiate)) {
    6186           0 :                                 stack_pop_frame(sql);
    6187           0 :                                 return 0;
    6188             :                         }
    6189             :                 }
    6190         542 :                 stack_pop_frame(sql);
    6191             :         }
    6192             :         return res;
    6193             : }
    6194             : 
    6195             : static void
    6196          25 : sql_update_check(backend *be, stmt **updates, sql_key *key, stmt *u_tids)
    6197             : {
    6198          25 :         mvc *sql = be->mvc;
    6199          25 :         int pos = 0;
    6200          25 :         sql_rel *rel = rel_basetable(sql, key->t, key->t->base.name);
    6201          25 :         sql_exp *exp = exp_read(sql, rel, NULL, NULL, sa_strdup(sql->sa, key->check), &pos, 0);
    6202          25 :         rel->exps = rel_base_projection(sql, rel, 0);
    6203             : 
    6204             :         /* create sub stmt with needed updates (or projected col from to be updated table) */
    6205          25 :         list *ups = sa_list(sql->sa);
    6206          55 :         for(node *n = key->columns->h; n; n = n->next) {
    6207          30 :                 sql_kc *kc = n->data;
    6208          30 :                 stmt *upd = NULL;
    6209             : 
    6210          30 :                 if (updates && updates[kc->c->colnr]) {
    6211             :                         upd = updates[kc->c->colnr];
    6212             :                 } else {
    6213          17 :                         upd = stmt_col(be, kc->c, u_tids, u_tids->partition);
    6214             :                 }
    6215          30 :                 sql_exp *e = rel_base_bind_column2(sql, rel, kc->c->t->base.name, kc->c->base.name);
    6216          30 :                 upd = stmt_alias(be, upd, e->alias.label, kc->c->t->base.name, kc->c->base.name);
    6217          30 :                 append(ups, upd);
    6218             :         }
    6219             : 
    6220          25 :         stmt *sub = stmt_list(be, ups);
    6221          25 :         stmt *s = exp_bin(be, exp, sub, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    6222             : 
    6223          25 :         sql_subfunc *cnt = sql_bind_func(sql, "sys", "count", sql_bind_localtype("void"), NULL, F_AGGR, true, true);
    6224          25 :         s = stmt_uselect(be, column(be, s), stmt_bool(be, 0), cmp_equal, NULL, 0, 1);
    6225          25 :         s = stmt_aggr(be, s, NULL, NULL, cnt, 1, 0, 1);
    6226          25 :         char *msg = sa_message(sql->sa, SQLSTATE(40002) "UPDATE: violated constraint '%s.%s' CHECK(%s)", key->t->s->base.name, key->base.name, exp->comment);
    6227          25 :         (void)stmt_exception(be, s, msg, 00001);
    6228          25 : }
    6229             : 
    6230             : static void
    6231        4104 : sql_update_check_null(backend *be, sql_table *t, stmt **updates)
    6232             : {
    6233        4104 :         mvc *sql = be->mvc;
    6234        4104 :         node *n;
    6235        4104 :         sql_subfunc *cnt = sql_bind_func(sql, "sys", "count", sql_bind_localtype("void"), NULL, F_AGGR, true, true);
    6236             : 
    6237       48096 :         for (n = ol_first_node(t->columns); n; n = n->next) {
    6238       43992 :                 sql_column *c = n->data;
    6239             : 
    6240       43992 :                 if (updates[c->colnr] && !c->null) {
    6241         129 :                         stmt *s = updates[c->colnr];
    6242         129 :                         char *msg = NULL;
    6243             : 
    6244         129 :                         if (!(s->key && s->nrcols == 0)) {
    6245         129 :                                 s = stmt_selectnil(be, updates[c->colnr]);
    6246         129 :                                 s = stmt_aggr(be, s, NULL, NULL, cnt, 1, 0, 1);
    6247             :                         } else {
    6248           0 :                                 sql_subfunc *isnil = sql_bind_func(sql, "sys", "isnull", &c->type, NULL, F_FUNC, true, true);
    6249             : 
    6250           0 :                                 s = stmt_unop(be, updates[c->colnr], NULL, isnil);
    6251             :                         }
    6252         129 :                         msg = sa_message(sql->sa, SQLSTATE(40002) "UPDATE: NOT NULL constraint violated for column '%s.%s'", c->t->base.name, c->base.name);
    6253         129 :                         (void)stmt_exception(be, s, msg, 00001);
    6254             :                 }
    6255             :         }
    6256        4104 : }
    6257             : 
    6258             : /* updates: an array of table width, per column holds the values for the to be updated rows  */
    6259             : static list *
    6260          43 : sql_update(backend *be, sql_table *t, stmt *rows, stmt **updates)
    6261             : {
    6262          43 :         mvc *sql = be->mvc;
    6263          43 :         list *idx_updates = NULL;
    6264          43 :         int i, nr_cols = ol_length(t->columns);
    6265          43 :         list *l = sa_list(sql->sa);
    6266          43 :         node *n;
    6267          43 :         stmt *cnt = NULL;
    6268             : 
    6269          43 :         sql_update_check_null(be, t, updates);
    6270             : 
    6271             :         /* check keys + get idx */
    6272          43 :         idx_updates = update_idxs_and_check_keys(be, t, rows, updates, l, NULL);
    6273          43 :         if (!idx_updates) {
    6274           0 :                 assert(0);
    6275             :                 return sql_error(sql, 10, SQLSTATE(42000) "UPDATE: failed to update indexes for table '%s'", t->base.name);
    6276             :         }
    6277             : 
    6278             : /* before */
    6279          43 :         if (!sql_update_triggers(be, t, rows, updates, 0))
    6280           0 :                 return sql_error(sql, 10, SQLSTATE(27000) "UPDATE: triggers failed for table '%s'", t->base.name);
    6281             : 
    6282             : /* apply updates */
    6283         131 :         for (i = 0, n = ol_first_node(t->columns); i < nr_cols && n; i++, n = n->next) {
    6284          88 :                 sql_column *c = n->data;
    6285             : 
    6286          88 :                 if (updates[i])
    6287          45 :                         append(l, stmt_update_col(be, c, rows, updates[i]));
    6288             :         }
    6289          43 :         if (cascade_updates(be, t, rows, updates))
    6290           0 :                 return sql_error(sql, 10, SQLSTATE(42000) "UPDATE: cascade failed for table '%s'", t->base.name);
    6291             : 
    6292             : /* after */
    6293          43 :         if (!sql_update_triggers(be, t, rows, updates, 1))
    6294           0 :                 return sql_error(sql, 10, SQLSTATE(27000) "UPDATE: triggers failed for table '%s'", t->base.name);
    6295             : 
    6296          43 :         if (!be->silent || (t->s && isGlobal(t) && !isGlobalTemp(t)))
    6297          43 :                 cnt = stmt_aggr(be, rows, NULL, NULL, sql_bind_func(sql, "sys", "count", sql_bind_localtype("void"), NULL, F_AGGR, true, true), 1, 0, 1);
    6298          43 :         if (add_to_rowcount_accumulator(be, cnt->nr) < 0)
    6299           0 :                 return sql_error(sql, 10, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    6300          43 :         if (t->s && isGlobal(t) && !isGlobalTemp(t))
    6301          43 :                 stmt_add_dependency_change(be, t, cnt);
    6302             : /* cascade ?? */
    6303             :         return l;
    6304             : }
    6305             : 
    6306             : /* updates with empty list is alter with create idx or keys */
    6307             : static stmt *
    6308        4202 : rel2bin_update(backend *be, sql_rel *rel, list *refs)
    6309             : {
    6310        4202 :         mvc *sql = be->mvc;
    6311        4202 :         stmt *update = NULL, **updates = NULL, *tids, *ddl = NULL, *pup = NULL, *cnt;
    6312        4202 :         list *l = sa_list(sql->sa);
    6313        4202 :         int nr_cols, updcol, idx_ups = 0;
    6314        4202 :         node *m;
    6315        4202 :         sql_rel *tr = rel->l, *prel = rel->r;
    6316        4202 :         sql_table *t = NULL;
    6317             : 
    6318        4202 :         if ((rel->flag&UPD_COMP)) {  /* special case ! */
    6319        1164 :                 idx_ups = 1;
    6320        1164 :                 prel = rel->l;
    6321        1164 :                 rel = rel->r;
    6322        1164 :                 tr = rel->l;
    6323             :         }
    6324        4202 :         if (tr->op == op_basetable) {
    6325        2858 :                 t = tr->l;
    6326             :         } else {
    6327        1344 :                 ddl = subrel_bin(be, tr, refs);
    6328        1344 :                 ddl = subrel_project(be, ddl, refs, NULL);
    6329        1344 :                 if (!ddl)
    6330             :                         return NULL;
    6331        1344 :                 t = rel_ddl_table_get(tr);
    6332             : 
    6333             :                 /* no columns to update (probably an new pkey or ckey!) */
    6334        1344 :                 if (!rel->exps) {
    6335         141 :                         stmt *tids = stmt_tid(be, t, 0);
    6336         154 :                         for (m = ol_first_node(t->keys); m; m = m->next) {
    6337          13 :                                 sql_key * key = m->data;
    6338          13 :                                 if (key->type == ckey && key->base.new)
    6339          13 :                                         sql_update_check(be, NULL, key, tids);
    6340             :                         }
    6341             :                         return ddl;
    6342             :                 }
    6343             :         }
    6344             : 
    6345        4061 :         if (rel->r) /* first construct the update relation */
    6346        4061 :                 update = subrel_bin(be, rel->r, refs);
    6347        4061 :         update = subrel_project(be, update, refs, rel->r);
    6348             : 
    6349        4061 :         if (!update)
    6350             :                 return NULL;
    6351             : 
    6352        4061 :         if (idx_ups)
    6353        1164 :                 pup = refs_find_rel(refs, prel);
    6354             : 
    6355        4061 :         updates = table_update_stmts(sql, t, &nr_cols);
    6356        4061 :         tids = update->op4.lval->h->data;
    6357             : 
    6358             :         /* lookup the updates */
    6359       11111 :         for (m = rel->exps->h; m; m = m->next) {
    6360        7050 :                 sql_exp *ce = m->data;
    6361        7050 :                 sql_column *c = find_sql_column(t, exp_name(ce));
    6362             : 
    6363        7050 :                 if (c)
    6364        3014 :                         updates[c->colnr] = bin_find_column(be, update, ce->l, ce->r);
    6365             :         }
    6366             : 
    6367        4966 :         for (m = ol_first_node(t->keys); m; m = m->next) {
    6368         905 :                 sql_key * key = m->data;
    6369         905 :                 if (key->type == ckey && is_check_updated(key, updates))
    6370          12 :                         sql_update_check(be, updates, key, tids);
    6371             :         }
    6372        4061 :         sql_update_check_null(be, t, updates);
    6373             : 
    6374             :         /* check keys + get idx */
    6375        4061 :         updcol = first_updated_col(updates, ol_length(t->columns));
    6376       11111 :         for (m = rel->exps->h; m; m = m->next) {
    6377        7050 :                 sql_exp *ce = m->data;
    6378        7050 :                 sql_idx *i = find_sql_idx(t, exp_name(ce)+1);
    6379        7050 :                 stmt *update_idx, *is = NULL;
    6380             : 
    6381        7050 :                 if (i) {
    6382        1178 :                         if (non_updatable_index(i->type)) /* Some indexes don't hold delta structures */
    6383         148 :                                 continue;
    6384             : 
    6385        1030 :                         update_idx = bin_find_column(be, update, ce->l, ce->r);
    6386        1030 :                         if (update_idx)
    6387             :                                 is = update_idx;
    6388        1030 :                         if (hash_index(i->type) && list_length(i->columns) <= 1) {
    6389        1030 :                                 is = NULL;
    6390        1030 :                                 update_idx = NULL;
    6391             :                         }
    6392        1030 :                         if (i->key)
    6393        1603 :                                 sql_update_check_key(be, (updcol>=0)?updates:NULL, i->key, tids, update_idx, updcol, l, pup);
    6394        1030 :                         if (is)
    6395         780 :                                 list_append(l, stmt_update_idx(be,  i, tids, is));
    6396             :                 }
    6397             :         }
    6398             : 
    6399             : /* before */
    6400        4061 :         if (!sql_update_triggers(be, t, tids, updates, 0)) {
    6401           0 :                 if (sql->cascade_action)
    6402           0 :                         sql->cascade_action = NULL;
    6403           0 :                 return sql_error(sql, 10, SQLSTATE(27000) "UPDATE: triggers failed for table '%s'", t->base.name);
    6404             :         }
    6405             : 
    6406             : /* apply the update */
    6407       11111 :         for (m = rel->exps->h; m; m = m->next) {
    6408        7050 :                 sql_exp *ce = m->data;
    6409        7050 :                 sql_column *c = find_sql_column(t, exp_name(ce));
    6410             : 
    6411        7050 :                 if (c)
    6412        3014 :                         append(l, stmt_update_col(be,  c, tids, updates[c->colnr]));
    6413             :         }
    6414             : 
    6415        4061 :         stmt* returning = NULL;
    6416        4061 :         if (rel->returning) {
    6417           5 :                 sql_rel* b = rel->l;
    6418           5 :                 int refcnt = b->ref.refcnt; // HACK: forces recalculation of base columns since they are assumed to be updated
    6419           5 :                 b->ref.refcnt = 1;
    6420           5 :                 returning = subrel_bin(be, b, refs);
    6421           5 :                 b->ref.refcnt = refcnt;
    6422           5 :                 returning->cand = tids;
    6423           5 :                 returning = subrel_project(be, returning, refs, b);
    6424           5 :                 sql->type = Q_TABLE;
    6425             :         }
    6426             : 
    6427        4061 :         if (cascade_updates(be, t, tids, updates)) {
    6428           0 :                 if (sql->cascade_action)
    6429           0 :                         sql->cascade_action = NULL;
    6430           0 :                 return sql_error(sql, 10, SQLSTATE(42000) "UPDATE: cascade failed for table '%s'", t->base.name);
    6431             :         }
    6432             : 
    6433             : /* after */
    6434        4061 :         if (!sql_update_triggers(be, t, tids, updates, 1)) {
    6435           0 :                 if (sql->cascade_action)
    6436           0 :                         sql->cascade_action = NULL;
    6437           0 :                 return sql_error(sql, 10, SQLSTATE(27000) "UPDATE: triggers failed for table '%s'", t->base.name);
    6438             :         }
    6439             : 
    6440        4061 :         if (ddl) {
    6441        1203 :                 list_prepend(l, ddl);
    6442        1203 :                 cnt = stmt_list(be, l);
    6443             :         } else {
    6444        2858 :                 cnt = stmt_aggr(be, tids, NULL, NULL, sql_bind_func(sql, "sys", "count", sql_bind_localtype("void"), NULL, F_AGGR, true, true), 1, 0, 1);
    6445        2858 :                 if (add_to_rowcount_accumulator(be, cnt->nr) < 0)
    6446           0 :                         return sql_error(sql, 10, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    6447        2858 :                 if (t->s && isGlobal(t) && !isGlobalTemp(t))
    6448        2854 :                         stmt_add_dependency_change(be, t, cnt);
    6449             :         }
    6450             : 
    6451        4061 :         if (sql->cascade_action)
    6452          65 :                 sql->cascade_action = NULL;
    6453        4061 :         if (rel->r && !rel_predicates(be, rel->r))
    6454             :                 return NULL;
    6455        4061 :         return returning?returning:cnt;
    6456             : }
    6457             : 
    6458             : static int
    6459          20 : sql_stack_add_deleted(mvc *sql, const char *name, sql_table *t, stmt *tids, stmt **deleted_cols, int type)
    6460             : {
    6461             :         /* Put single relation of updates and old values on to the stack */
    6462          20 :         sql_rel *r = NULL;
    6463          20 :         node *n;
    6464          20 :         list *exps = sa_list(sql->sa);
    6465          20 :         trigger_input *ti = SA_NEW(sql->sa, trigger_input);
    6466             : 
    6467          20 :         ti->t = t;
    6468          20 :         ti->tids = tids;
    6469          20 :         ti->updates = deleted_cols;
    6470          20 :         ti->type = type;
    6471          20 :         ti->nn = name;
    6472          56 :         for (n = ol_first_node(t->columns); n; n = n->next) {
    6473          36 :                 sql_column *c = n->data;
    6474          36 :                 sql_exp *ne = exp_column(sql->sa, name, c->base.name, &c->type, CARD_MULTI, c->null, is_column_unique(c), 0);
    6475          36 :                 ne->alias.label = -(sql->nid++);
    6476             : 
    6477          36 :                 append(exps, ne);
    6478             :         }
    6479          20 :         r = rel_table_func(sql->sa, NULL, NULL, exps, TRIGGER_WRAPPER);
    6480          20 :         r->l = ti;
    6481             : 
    6482          20 :         return stack_push_rel_view(sql, name, r) ? 1 : 0;
    6483             : }
    6484             : 
    6485             : static int
    6486       84396 : sql_delete_triggers(backend *be, sql_table *t, stmt *tids, stmt **deleted_cols, int time, int firing_type, int internal_type)
    6487             : {
    6488       84396 :         mvc *sql = be->mvc;
    6489       84396 :         node *n;
    6490       84396 :         int res = 1;
    6491             : 
    6492       84396 :         if (!ol_length(t->triggers))
    6493             :                 return res;
    6494             : 
    6495         188 :         for (n = ol_first_node(t->triggers); n; n = n->next) {
    6496         142 :                 sql_trigger *trigger = n->data;
    6497             : 
    6498         142 :                 if (!stack_push_frame(sql, "%OLD-NEW"))
    6499             :                         return 0;
    6500         142 :                 if (trigger->event == firing_type && trigger->time == time) {
    6501             :                         /* add name for the 'deleted' to the stack */
    6502          20 :                         const char *o = trigger->old_name;
    6503             : 
    6504          20 :                         if (!o) o = "old";
    6505             : 
    6506          20 :                         if(!sql_stack_add_deleted(sql, o, t, tids, deleted_cols, internal_type)) {
    6507           0 :                                 stack_pop_frame(sql);
    6508           0 :                                 return 0;
    6509             :                         }
    6510             : 
    6511          20 :                         if (!sql_parse(be, trigger->t->s, trigger->statement, m_instantiate)) {
    6512           0 :                                 stack_pop_frame(sql);
    6513           0 :                                 return 0;
    6514             :                         }
    6515             :                 }
    6516         142 :                 stack_pop_frame(sql);
    6517             :         }
    6518             :         return res;
    6519             : }
    6520             : 
    6521             : static stmt * sql_delete(backend *be, sql_table *t, stmt *rows);
    6522             : 
    6523             : static stmt *
    6524           9 : sql_delete_cascade_Fkeys(backend *be, sql_key *fk, stmt *ftids)
    6525             : {
    6526           9 :         sql_table *t = mvc_bind_table(be->mvc, fk->t->s, fk->t->base.name);
    6527           9 :         return sql_delete(be, t, ftids);
    6528             : }
    6529             : 
    6530             : static void
    6531         130 : sql_delete_ukey(backend *be, stmt *utids /* deleted tids from ukey table */, sql_key *k, list *l, char* which, int cascade)
    6532             : {
    6533         130 :         mvc *sql = be->mvc;
    6534         130 :         sql_subtype *lng = sql_bind_localtype("lng");
    6535         130 :         sql_subtype *bt = sql_bind_localtype("bit");
    6536         130 :         sql_trans *tr = be->mvc->session->tr;
    6537         130 :         list *keys = sql_trans_get_dependents(tr, k->base.id, FKEY_DEPENDENCY, NULL);
    6538             : 
    6539         130 :         if (keys) {
    6540         190 :                 for (node *n = keys->h; n; n = n->next->next) {
    6541          60 :                         sqlid fkey_id = *(sqlid*)n->data;
    6542          60 :                         sql_base *b = os_find_id(tr->cat->objects, tr, fkey_id);
    6543          60 :                         sql_key *fk = (sql_key*)b;
    6544          60 :                         sql_fkey *rk = (sql_fkey*)b;
    6545             : 
    6546          60 :                         if (fk->type != fkey || rk->rkey != k->base.id)
    6547           0 :                                 continue;
    6548          60 :                         char *msg = NULL;
    6549          60 :                         sql_subfunc *cnt = sql_bind_func(sql, "sys", "count", sql_bind_localtype("void"), NULL, F_AGGR, true, true);
    6550          60 :                         sql_subfunc *ne = sql_bind_func_result(sql, "sys", "<>", F_FUNC, true, bt, 2, lng, lng);
    6551          60 :                         stmt *s, *tids;
    6552             : 
    6553          60 :                         tids = stmt_tid(be, fk->idx->t, 0);
    6554          60 :                         s = stmt_idx(be, fk->idx, tids, tids->partition);
    6555          60 :                         s = stmt_join(be, s, utids, 0, cmp_equal, 0, 0, false); /* join over the join index */
    6556          60 :                         s = stmt_result(be, s, 0);
    6557          60 :                         tids = stmt_project(be, s, tids);
    6558          60 :                         if(cascade) { /* for truncate statements with the cascade option */
    6559           1 :                                 s = sql_delete_cascade_Fkeys(be, fk, tids);
    6560           1 :                                 list_prepend(l, s);
    6561             :                         } else {
    6562          59 :                                 switch (((sql_fkey*)fk)->on_delete) {
    6563             :                                         case ACT_NO_ACTION:
    6564             :                                                 break;
    6565          11 :                                         case ACT_SET_NULL:
    6566             :                                         case ACT_SET_DEFAULT:
    6567          11 :                                                 s = sql_delete_set_Fkeys(be, fk, tids, ((sql_fkey*)fk)->on_delete);
    6568          11 :                                                 list_prepend(l, s);
    6569          11 :                                                 break;
    6570           8 :                                         case ACT_CASCADE:
    6571           8 :                                                 s = sql_delete_cascade_Fkeys(be, fk, tids);
    6572           8 :                                                 list_prepend(l, s);
    6573           8 :                                                 break;
    6574          38 :                                         default:        /*RESTRICT*/
    6575             :                                                 /* The overlap between deleted primaries and foreign should be empty */
    6576          38 :                                                 s = stmt_binop(be, stmt_aggr(be, tids, NULL, NULL, cnt, 1, 0, 1), stmt_atom_lng(be, 0), NULL, ne);
    6577          38 :                                                 msg = sa_message(sql->sa, SQLSTATE(40002) "%s: FOREIGN KEY constraint '%s.%s' violated", which, fk->t->base.name, fk->base.name);
    6578          38 :                                                 s = stmt_exception(be, s, msg, 00001);
    6579          38 :                                                 list_prepend(l, s);
    6580             :                                 }
    6581             :                         }
    6582             :                 }
    6583         130 :                 list_destroy(keys);
    6584             :         }
    6585         130 : }
    6586             : 
    6587             : static int
    6588       42200 : sql_delete_keys(backend *be, sql_table *t, stmt *rows, list *l, char* which, int cascade)
    6589             : {
    6590       42200 :         mvc *sql = be->mvc;
    6591       42200 :         int res = 1;
    6592       42200 :         node *n;
    6593             : 
    6594       42200 :         if (!ol_length(t->keys))
    6595             :                 return res;
    6596             : 
    6597         304 :         for (n = ol_first_node(t->keys); n; n = n->next) {
    6598         173 :                 sql_key *k = n->data;
    6599             : 
    6600         173 :                 if (k->type == pkey || k->type == ukey) {
    6601         131 :                         if (!(sql->cascade_action && list_find_id(sql->cascade_action, k->base.id))) {
    6602         130 :                                 sqlid *local_id = SA_NEW(sql->sa, sqlid);
    6603         130 :                                 if (!sql->cascade_action)
    6604         120 :                                         sql->cascade_action = sa_list(sql->sa);
    6605             : 
    6606         130 :                                 *local_id = k->base.id;
    6607         130 :                                 list_append(sql->cascade_action, local_id);
    6608         130 :                                 sql_delete_ukey(be, rows, k, l, which, cascade);
    6609             :                         }
    6610             :                 }
    6611             :         }
    6612             :         return res;
    6613             : }
    6614             : 
    6615             : static stmt *
    6616         495 : sql_delete(backend *be, sql_table *t, stmt *rows)
    6617             : {
    6618         495 :         mvc *sql = be->mvc;
    6619         495 :         stmt *v = NULL, *s = NULL;
    6620         495 :         list *l = sa_list(sql->sa);
    6621         495 :         stmt **deleted_cols = NULL;
    6622             : 
    6623         495 :         if (rows) {
    6624             :                 v = rows;
    6625             :         } else { /* delete all */
    6626         128 :                 v = stmt_tid(be, t, 0);
    6627             :         }
    6628             : 
    6629             :         /*  project all columns */
    6630         495 :         if (ol_length(t->triggers) || partition_find_part(sql->session->tr, t, NULL)) {
    6631          38 :                 int nr = 0;
    6632          38 :                 deleted_cols = table_update_stmts(sql, t, &nr);
    6633          38 :                 int i = 0;
    6634         105 :                 for (node *n = ol_first_node(t->columns); n; n = n->next, i++) {
    6635          67 :                         sql_column *c = n->data;
    6636          67 :                         stmt *s = stmt_col(be, c, v, v->partition);
    6637             : 
    6638          67 :                         deleted_cols[i] = s;
    6639          67 :                         list_append(l, s);
    6640             :                 }
    6641             :         }
    6642             : 
    6643             : /* before */
    6644         495 :         if (!sql_delete_triggers(be, t, v, deleted_cols, 0, 1, 3))
    6645           0 :                 return sql_error(sql, 10, SQLSTATE(27000) "DELETE: triggers failed for table '%s'", t->base.name);
    6646             : 
    6647         495 :         if (!sql_delete_keys(be, t, v, l, "DELETE", 0))
    6648           0 :                 return sql_error(sql, 10, SQLSTATE(42000) "DELETE: failed to delete indexes for table '%s'", t->base.name);
    6649             : 
    6650         495 :         if (rows) {
    6651         367 :                 s = stmt_delete(be, t, rows);
    6652         367 :                 if (!be->silent || (t->s && isGlobal(t) && !isGlobalTemp(t)))
    6653         366 :                         s = stmt_aggr(be, rows, NULL, NULL, sql_bind_func(sql, "sys", "count", sql_bind_localtype("void"), NULL, F_AGGR, true, true), 1, 0, 1);
    6654             :         } else { /* delete all */
    6655         128 :                 s = stmt_table_clear(be, t, 0); /* first column */
    6656             :         }
    6657             : 
    6658             : /* after */
    6659         495 :         if (!sql_delete_triggers(be, t, v, deleted_cols, 1, 1, 3))
    6660           0 :                 return sql_error(sql, 10, SQLSTATE(27000) "DELETE: triggers failed for table '%s'", t->base.name);
    6661             : 
    6662         495 :         if (add_to_rowcount_accumulator(be, s->nr) < 0)
    6663           0 :                 return sql_error(sql, 10, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    6664         495 :         if (t->s && isGlobal(t) && !isGlobalTemp(t))
    6665         493 :                 stmt_add_dependency_change(be, t, s);
    6666             :         return s;
    6667             : }
    6668             : 
    6669             : static stmt *
    6670         486 : rel2bin_delete(backend *be, sql_rel *rel, list *refs)
    6671             : {
    6672         486 :         mvc *sql = be->mvc;
    6673         486 :         stmt *stdelete = NULL, *tids = NULL, *returning = NULL;
    6674         486 :         sql_rel *tr = rel->l;
    6675         486 :         sql_table *t = NULL;
    6676             : 
    6677         486 :         if (tr->op == op_basetable)
    6678         486 :                 t = tr->l;
    6679             :         else
    6680           0 :                 assert(0/*ddl statement*/);
    6681             : 
    6682         486 :         if (rel->r) { /* first construct the deletes relation */
    6683         358 :                 stmt *rows = subrel_bin(be, rel->r, refs);
    6684         358 :                 rows = subrel_project(be, rows, refs, rel->r);
    6685         358 :                 if (!rows)
    6686             :                         return NULL;
    6687         358 :                 assert(rows->type == st_list);
    6688         358 :                 tids = rows->op4.lval->h->data; /* TODO this should be the candidate list instead */
    6689             :         }
    6690             : 
    6691         486 :         if (rel->returning) {
    6692           4 :                 returning = subrel_bin(be, rel->l, refs);
    6693           4 :                 returning->cand = tids;
    6694           4 :                 returning = subrel_project(be, returning, refs, rel->l);
    6695           4 :                 sql->type = Q_TABLE;
    6696             :         }
    6697             : 
    6698         486 :         stdelete = sql_delete(be, t, tids);
    6699         486 :         if (sql->cascade_action)
    6700         107 :                 sql->cascade_action = NULL;
    6701         486 :         if (!stdelete)
    6702             :                 return NULL;
    6703             : 
    6704         486 :         if (rel->r && !rel_predicates(be, rel->r))
    6705             :                 return NULL;
    6706         486 :         return returning?returning:stdelete;
    6707             : }
    6708             : 
    6709             : struct tablelist {
    6710             :         sql_table *table;
    6711             :         struct tablelist* next;
    6712             : };
    6713             : 
    6714             : static sql_table * /* inspect the other tables recursively for foreign key dependencies */
    6715       41707 : check_for_foreign_key_references(mvc *sql, struct tablelist* tlist, struct tablelist* next_append, sql_table *t, int cascade)
    6716             : {
    6717       41707 :         struct tablelist* new_node;
    6718       41707 :         sql_trans *tr = sql->session->tr;
    6719       41707 :         sqlstore *store = sql->session->tr->store;
    6720             : 
    6721       41707 :         if (mvc_highwater(sql))
    6722           0 :                 return sql_error(sql, 10, SQLSTATE(42000) "Query too complex: running out of stack space");
    6723             : 
    6724       41707 :         if (t->keys) { /* Check for foreign key references */
    6725       41725 :                 for (node *n = ol_first_node(t->keys); n; n = n->next) {
    6726          21 :                         sql_key *k = n->data;
    6727             : 
    6728          21 :                         if (k->type == ukey || k->type == pkey) {
    6729          17 :                                 list *keys = sql_trans_get_dependents(tr, k->base.id, FKEY_DEPENDENCY, NULL);
    6730             : 
    6731          16 :                                 if (keys) {
    6732          21 :                                         for (node *nn = keys->h; nn; nn = nn->next->next) {
    6733           6 :                                                 sqlid fkey_id = *(sqlid*)nn->data;
    6734           6 :                                                 sql_base *b = os_find_id(tr->cat->objects, tr, fkey_id);
    6735           6 :                                                 sql_key *fk = (sql_key*)b;
    6736           6 :                                                 sql_fkey *rk = (sql_fkey*)b;
    6737             : 
    6738           6 :                                                 if (fk->type != fkey || rk->rkey != k->base.id)
    6739           1 :                                                         continue;
    6740           5 :                                                 k = fk;
    6741             :                                                 /* make sure it is not a self referencing key */
    6742           5 :                                                 if (k->t != t && !cascade && isTable(t)) {
    6743           4 :                                                         node *nnn = ol_first_node(t->columns);
    6744           4 :                                                         sql_column *c = nnn->data;
    6745           4 :                                                         size_t n_rows = store->storage_api.count_col(sql->session->tr, c, 10);
    6746           4 :                                                         if (n_rows > 0) {
    6747           2 :                                                                 list_destroy(keys);
    6748           2 :                                                                 return sql_error(sql, 02, SQLSTATE(23000) "TRUNCATE: FOREIGN KEY %s.%s depends on %s", k->t->base.name, k->base.name, t->base.name);
    6749             :                                                         }
    6750           1 :                                                 } else if (k->t != t) {
    6751             :                                                         int found = 0;
    6752           2 :                                                         for (struct tablelist *node_check = tlist; node_check; node_check = node_check->next) {
    6753           1 :                                                                 if (node_check->table == k->t)
    6754           0 :                                                                         found = 1;
    6755             :                                                         }
    6756           1 :                                                         if (!found) {
    6757           2 :                                                                 if ((new_node = SA_NEW(sql->ta, struct tablelist)) == NULL) {
    6758           0 :                                                                         list_destroy(keys);
    6759           0 :                                                                         return sql_error(sql, 10, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    6760             :                                                                 }
    6761           1 :                                                                 new_node->table = k->t;
    6762           1 :                                                                 new_node->next = NULL;
    6763           1 :                                                                 next_append->next = new_node;
    6764           1 :                                                                 if (!check_for_foreign_key_references(sql, tlist, new_node, k->t, cascade)) {
    6765           0 :                                                                         list_destroy(keys);
    6766           0 :                                                                         return NULL;
    6767             :                                                                 }
    6768             :                                                         }
    6769             :                                                 }
    6770             :                                         }
    6771          15 :                                         list_destroy(keys);
    6772             :                                 }
    6773             :                         }
    6774             :                 }
    6775             :         }
    6776             :         return t;
    6777             : }
    6778             : 
    6779             : static stmt *
    6780       41706 : sql_truncate(backend *be, sql_table *t, int restart_sequences, int cascade)
    6781             : {
    6782       41706 :         mvc *sql = be->mvc;
    6783       41706 :         list *l = sa_list(sql->sa);
    6784       41706 :         stmt *ret = NULL, *other = NULL;
    6785       41706 :         struct tablelist *new_list = SA_NEW(sql->ta, struct tablelist);
    6786       41706 :         stmt **deleted_cols = NULL;
    6787             : 
    6788       41706 :         if (!new_list)
    6789           0 :                 return sql_error(sql, 10, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    6790       41706 :         new_list->table = t;
    6791       41706 :         new_list->next = NULL;
    6792       41706 :         if (!check_for_foreign_key_references(sql, new_list, new_list, t, cascade))
    6793           2 :                 goto finalize;
    6794             : 
    6795       83407 :         for (struct tablelist *list_node = new_list; list_node; list_node = list_node->next) {
    6796       41704 :                 sql_table *next = list_node->table;
    6797       41704 :                 stmt *v = stmt_tid(be, next, 0);
    6798             : 
    6799             :                 /* project all columns */
    6800       41705 :                 if (ol_length(t->triggers) || partition_find_part(sql->session->tr, t, NULL)) {
    6801          18 :                         int nr = 0;
    6802          18 :                         deleted_cols = table_update_stmts(sql, t, &nr);
    6803          18 :                         int i = 0;
    6804          50 :                         for (node *n = ol_first_node(t->columns); n; n = n->next, i++) {
    6805          32 :                                 sql_column *c = n->data;
    6806          32 :                                 stmt *s = stmt_col(be, c, v, v->partition);
    6807             : 
    6808          32 :                                 deleted_cols[i] = s;
    6809          32 :                                 list_append(l, s);
    6810             :                         }
    6811             :                 }
    6812             : 
    6813             :                 /* before */
    6814       41705 :                 if (!sql_delete_triggers(be, next, v, deleted_cols, 0, 3, 4)) {
    6815           0 :                         (void) sql_error(sql, 10, SQLSTATE(27000) "TRUNCATE: triggers failed for table '%s'", next->base.name);
    6816           0 :                         ret = NULL;
    6817           0 :                         goto finalize;
    6818             :                 }
    6819             : 
    6820       41705 :                 if (!sql_delete_keys(be, next, v, l, "TRUNCATE", cascade)) {
    6821           0 :                         (void) sql_error(sql, 10, SQLSTATE(42000) "TRUNCATE: failed to delete indexes for table '%s'", next->base.name);
    6822           0 :                         ret = NULL;
    6823           0 :                         goto finalize;
    6824             :                 }
    6825             : 
    6826       41705 :                 other = stmt_table_clear(be, next, restart_sequences);
    6827       41704 :                 list_append(l, other);
    6828       41703 :                 if (next && t && next->base.id == t->base.id)
    6829       41703 :                         ret = other;
    6830             : 
    6831             :                 /* after */
    6832       41703 :                 if (!sql_delete_triggers(be, next, v, deleted_cols, 1, 3, 4)) {
    6833           0 :                         (void) sql_error(sql, 10, SQLSTATE(27000) "TRUNCATE: triggers failed for table '%s'", next->base.name);
    6834           0 :                         ret = NULL;
    6835           0 :                         goto finalize;
    6836             :                 }
    6837             : 
    6838       41703 :                 if (add_to_rowcount_accumulator(be, other->nr) < 0) {
    6839           0 :                         (void) sql_error(sql, 10, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    6840           0 :                         ret = NULL;
    6841           0 :                         goto finalize;
    6842             :                 }
    6843       41702 :                 if (next->s && isGlobal(next) && !isGlobalTemp(next))
    6844       41696 :                         stmt_add_dependency_change(be, next, other);
    6845             :         }
    6846             : 
    6847       41703 : finalize:
    6848       41705 :         sa_reset(sql->ta);
    6849       41705 :         return ret;
    6850             : }
    6851             : 
    6852             : #define E_ATOM_INT(e) ((atom*)((sql_exp*)e)->l)->data.val.ival
    6853             : #define E_ATOM_STRING(e) ((atom*)((sql_exp*)e)->l)->data.val.sval
    6854             : 
    6855             : static stmt *
    6856       41706 : rel2bin_truncate(backend *be, sql_rel *rel)
    6857             : {
    6858       41706 :         mvc *sql = be->mvc;
    6859       41706 :         stmt *truncate = NULL;
    6860       41706 :         sql_rel *tr = rel->l;
    6861       41706 :         sql_table *t = NULL;
    6862       41706 :         node *n = NULL;
    6863       41706 :         int restart_sequences, cascade;
    6864             : 
    6865       41706 :         if (tr->op == op_basetable)
    6866       41706 :                 t = tr->l;
    6867             :         else
    6868           0 :                 assert(0/*ddl statement*/);
    6869             : 
    6870       41706 :         n = rel->exps->h;
    6871       41706 :         restart_sequences = E_ATOM_INT(n->data);
    6872       41706 :         cascade = E_ATOM_INT(n->next->data);
    6873       41706 :         truncate = sql_truncate(be, t, restart_sequences, cascade);
    6874       41706 :         if (sql->cascade_action)
    6875          13 :                 sql->cascade_action = NULL;
    6876       41706 :         return truncate;
    6877             : }
    6878             : 
    6879         234 : static ValPtr take_atom_arg(node **n, int expected_type) {
    6880         234 :         sql_exp *e = (*n)->data;
    6881         234 :         atom *a = e->l;
    6882         234 :         assert(a->tpe.type->localtype == expected_type); (void) expected_type;
    6883         234 :         assert(!a->isnull);
    6884         234 :         *n = (*n)->next;
    6885         234 :         return &a->data;
    6886             : }
    6887             : 
    6888             : static stmt *
    6889          74 : rel2bin_output(backend *be, sql_rel *rel, list *refs)
    6890             : {
    6891          74 :         mvc *sql = be->mvc;
    6892          74 :         stmt *sub = NULL, *fns = NULL, *res = NULL;
    6893          74 :         list *slist = sa_list(sql->sa);
    6894             : 
    6895          74 :         if (rel->l)  /* first construct the sub relation */
    6896          74 :                 sub = subrel_bin(be, rel->l, refs);
    6897          74 :         sub = subrel_project(be, sub, refs, rel->l);
    6898          74 :         if (!sub)
    6899             :                 return NULL;
    6900             : 
    6901          74 :         if (!rel->exps)
    6902             :                 return sub;
    6903             : 
    6904          74 :         list *arglist = rel->exps;
    6905          74 :         node *argnode = arglist->h;
    6906          74 :         atom *a = ((sql_exp*)argnode->data)->l;
    6907          74 :         int tpe = a->tpe.type->localtype;
    6908             : 
    6909             :         // With regular COPY INTO <file>, the first argument is a string.
    6910             :         // With COPY INTO BINARY, it is an int.
    6911          74 :         if (tpe == TYPE_str) {
    6912          32 :                 atom *tatom = ((sql_exp*) argnode->data)->l;
    6913          32 :                 const char *tsep  = sa_strdup(sql->sa, tatom->isnull ? "" : tatom->data.val.sval);
    6914          32 :                 atom *ratom = ((sql_exp*) argnode->next->data)->l;
    6915          32 :                 const char *rsep  = sa_strdup(sql->sa, ratom->isnull ? "" : ratom->data.val.sval);
    6916          32 :                 atom *satom = ((sql_exp*) argnode->next->next->data)->l;
    6917          32 :                 const char *ssep  = sa_strdup(sql->sa, satom->isnull ? "" : satom->data.val.sval);
    6918          32 :                 atom *natom = ((sql_exp*) argnode->next->next->next->data)->l;
    6919          32 :                 const char *ns = sa_strdup(sql->sa, natom->isnull ? "" : natom->data.val.sval);
    6920             : 
    6921          32 :                 const char *fn = NULL;
    6922          32 :                 int onclient = 0;
    6923          32 :                 if (argnode->next->next->next->next) {
    6924          23 :                         fn = E_ATOM_STRING(argnode->next->next->next->next->data);
    6925          23 :                         fns = stmt_atom_string(be, sa_strdup(sql->sa, fn));
    6926          23 :                         onclient = E_ATOM_INT(argnode->next->next->next->next->next->data);
    6927             :                 }
    6928          32 :                 stmt *export = stmt_export(be, sub, tsep, rsep, ssep, ns, onclient, fns);
    6929          32 :                 list_append(slist, export);
    6930          42 :         } else if (tpe == TYPE_int) {
    6931          42 :                 endianness endian = take_atom_arg(&argnode, TYPE_int)->val.ival;
    6932          42 :                 bool do_byteswap = (endian != endian_native && endian != OUR_ENDIANNESS);
    6933          42 :                 int on_client = take_atom_arg(&argnode, TYPE_int)->val.ival;
    6934          42 :                 assert(sub->type == st_list);
    6935          42 :                 list *collist = sub->op4.lval;
    6936         192 :                 for (node *colnode = collist->h; colnode; colnode = colnode->next) {
    6937         150 :                         stmt *colstmt = colnode->data;
    6938         150 :                         assert(argnode != NULL);
    6939         150 :                         const char *filename = take_atom_arg(&argnode, TYPE_str)->val.sval;
    6940         150 :                         stmt *export = stmt_export_bin(be, colstmt, do_byteswap, filename, on_client);
    6941         150 :                         list_append(slist, export);
    6942             :                 }
    6943          42 :                 assert(argnode == NULL);
    6944             : 
    6945             :         } else {
    6946           0 :                 assert(0 && "unimplemented export statement type");
    6947             :                 return sub;
    6948             :         }
    6949             : 
    6950          74 :         if (sub->type == st_list && ((stmt*)sub->op4.lval->h->data)->nrcols != 0) {
    6951          73 :                 res = stmt_aggr(be, sub->op4.lval->h->data, NULL, NULL, sql_bind_func(sql, "sys", "count", sql_bind_localtype("void"), NULL, F_AGGR, true, true), 1, 0, 1);
    6952             :         } else {
    6953           1 :                 res = stmt_atom_lng(be, 1);
    6954             :         }
    6955          74 :         if (add_to_rowcount_accumulator(be, res->nr) < 0)
    6956           0 :                 return sql_error(sql, 10, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    6957             :         return res;
    6958             : }
    6959             : 
    6960             : static list *
    6961          54 : merge_stmt_join_projections(backend *be, stmt *left, stmt *right, stmt *jl, stmt *jr, stmt *diff)
    6962             : {
    6963          54 :         mvc *sql = be->mvc;
    6964          54 :         list *l = sa_list(sql->sa);
    6965             : 
    6966          54 :         if (left)
    6967         117 :                 for (node *n = left->op4.lval->h; n; n = n->next) {
    6968          89 :                         stmt *c = n->data;
    6969          89 :                         assert(c->type == st_alias);
    6970          89 :                         const char *rnme = table_name(sql->sa, c);
    6971          89 :                         const char *nme = column_name(sql->sa, c);
    6972          89 :                         stmt *s = stmt_project(be, jl ? jl : diff, column(be, c));
    6973             : 
    6974          89 :                         s = stmt_alias(be, s, c->label, rnme, nme);
    6975          89 :                         list_append(l, s);
    6976             :                 }
    6977          54 :         if (right)
    6978         154 :                 for (node *n = right->op4.lval->h; n; n = n->next) {
    6979         110 :                         stmt *c = n->data;
    6980         110 :                         assert(c->type == st_alias);
    6981         110 :                         const char *rnme = table_name(sql->sa, c);
    6982         110 :                         const char *nme = column_name(sql->sa, c);
    6983         170 :                         stmt *s = stmt_project(be, jr ? jr : diff, column(be, c));
    6984             : 
    6985         110 :                         s = stmt_alias(be, s, c->label, rnme, nme);
    6986         110 :                         list_append(l, s);
    6987             :                 }
    6988          54 :         return l;
    6989             : }
    6990             : 
    6991             : static void
    6992          28 : validate_merge_delete_update(backend *be, bool delete, stmt *bt_stmt, sql_rel *bt, stmt *jl, stmt *ld)
    6993             : {
    6994          28 :         mvc *sql = be->mvc;
    6995          28 :         str msg;
    6996          28 :         sql_table *t = bt->l;
    6997          28 :         char *alias = (char *) rel_name(bt);
    6998          28 :         stmt *cnt1 = stmt_aggr(be, jl, NULL, NULL, sql_bind_func(sql, "sys", "count", sql_bind_localtype("void"), NULL, F_AGGR, true, true), 1, 0, 1);
    6999          28 :         stmt *cnt2 = stmt_aggr(be, ld, NULL, NULL, sql_bind_func(sql, "sys", "count", sql_bind_localtype("void"), NULL, F_AGGR, true, true), 1, 0, 1);
    7000          28 :         sql_subfunc *add = sql_bind_func(sql, "sys", "sql_add", tail_type(cnt1), tail_type(cnt2), F_FUNC, true, true);
    7001          28 :         stmt *s1 = stmt_binop(be, cnt1, cnt2, NULL, add);
    7002          28 :         stmt *cnt3 = stmt_aggr(be, bin_find_smallest_column(be, bt_stmt), NULL, NULL, sql_bind_func(sql, "sys", "count", sql_bind_localtype("void"), NULL, F_AGGR, true, true), 1, 0, 1);
    7003          28 :         sql_subfunc *bf = sql_bind_func(sql, "sys", ">", tail_type(s1), tail_type(cnt3), F_FUNC, true, true);
    7004          28 :         stmt *s2 = stmt_binop(be, s1, cnt3, NULL, bf);
    7005             : 
    7006          28 :         if (alias && strcmp(alias, t->base.name) == 0) /* detect if alias is present */
    7007             :                 alias = NULL;
    7008          38 :         msg = sa_message(sql->sa, SQLSTATE(40002) "MERGE %s: Multiple rows in the input relation match the same row in the target %s '%s%s%s'",
    7009             :                                          delete ? "DELETE" : "UPDATE",
    7010             :                                          alias ? "relation" : "table",
    7011          22 :                                          alias ? alias : t->s ? t->s->base.name : "", alias ? "" : ".", alias ? "" : t->base.name);
    7012          28 :         (void)stmt_exception(be, s2, msg, 00001);
    7013          28 : }
    7014             : 
    7015             : static stmt *
    7016          54 : rel2bin_merge_apply_update(backend *be, sql_rel *join, sql_rel *upd, list *refs, stmt *bt_stmt, stmt *target_stmt, stmt *jl, stmt *jr, stmt *ld, stmt **rd)
    7017             : {
    7018          54 :         if (is_insert(upd->op)) {
    7019          26 :                 if (!*rd) {
    7020          26 :                         *rd = stmt_tdiff(be, stmt_mirror(be, bin_find_smallest_column(be, target_stmt)), jr, NULL);
    7021             :                 }
    7022          26 :                 stmt *s = stmt_list(be, merge_stmt_join_projections(be, NULL, target_stmt, NULL, NULL, *rd));
    7023          26 :                 refs_update_stmt(refs, join, s); /* project the differences on the target side for inserts */
    7024             : 
    7025          26 :                 return rel2bin_insert(be, upd, refs);
    7026             :         } else {
    7027          48 :                 stmt *s = stmt_list(be, merge_stmt_join_projections(be, bt_stmt, is_update(upd->op) ? target_stmt : NULL, jl, is_update(upd->op) ? jr : NULL, NULL));
    7028          28 :                 refs_update_stmt(refs, join, s); /* project the matched values on both sides for updates and deletes */
    7029             : 
    7030          28 :                 assert(is_update(upd->op) || is_delete(upd->op));
    7031             :                 /* the left joined values + left difference must be smaller than the table count */
    7032          28 :                 validate_merge_delete_update(be, is_update(upd->op), bt_stmt, join->l, jl, ld);
    7033             : 
    7034          28 :                 return is_update(upd->op) ? rel2bin_update(be, upd, refs) : rel2bin_delete(be, upd, refs);
    7035             :         }
    7036             : }
    7037             : 
    7038             : static stmt *
    7039          44 : rel2bin_merge(backend *be, sql_rel *rel, list *refs)
    7040             : {
    7041          44 :         mvc *sql = be->mvc;
    7042          44 :         sql_rel *join;
    7043             : 
    7044          44 :         if (is_project(((sql_rel*)rel->l)->op)) {
    7045           0 :                 join = ((sql_rel*)rel->l)->l;
    7046             :         } else {
    7047             :                 join = rel->l;
    7048             :         }
    7049             : 
    7050          44 :         sql_rel *r = rel->r;
    7051          44 :         stmt *join_st, *bt_stmt, *target_stmt, *jl, *jr, *ld, *rd = NULL, *ns;
    7052          44 :         list *slist = sa_list(sql->sa);
    7053             : 
    7054          44 :         assert(rel_is_ref(join) && is_left(join->op));
    7055          44 :         join_st = subrel_bin(be, join, refs);
    7056          44 :         if (!join_st)
    7057             :                 return NULL;
    7058             : 
    7059             :         /* grab generated left join outputs and generate updates accordingly to matched and not matched values */
    7060          44 :         assert(join_st->type == st_list && list_length(join_st->extra) == 5);
    7061          44 :         bt_stmt = join_st->extra->h->data;
    7062          44 :         target_stmt = join_st->extra->h->next->data;
    7063          44 :         jl = join_st->extra->h->next->next->data;
    7064          44 :         jr = join_st->extra->h->next->next->next->data;
    7065          44 :         ld = join_st->extra->h->next->next->next->next->data;
    7066             : 
    7067          44 :         if (is_ddl(r->op)) {
    7068          44 :                 assert(r->flag == ddl_list);
    7069          44 :                 if (r->l) {
    7070          44 :                         if ((ns = rel2bin_merge_apply_update(be, join, r->l, refs, bt_stmt, target_stmt, jl, jr, ld, &rd)) == NULL)
    7071             :                                 return NULL;
    7072          44 :                         list_append(slist, ns);
    7073             :                 }
    7074          44 :                 if (r->r) {
    7075          10 :                         if ((ns = rel2bin_merge_apply_update(be, join, r->r, refs, bt_stmt, target_stmt, jl, jr, ld, &rd)) == NULL)
    7076             :                                 return NULL;
    7077          10 :                         list_append(slist, ns);
    7078             :                 }
    7079             :         } else {
    7080           0 :                 if (!(ns = rel2bin_merge_apply_update(be, join, r, refs, bt_stmt, target_stmt, jl, jr, ld, &rd)))
    7081             :                         return NULL;
    7082           0 :                 list_append(slist, ns);
    7083             :         }
    7084          44 :         return stmt_list(be, slist);
    7085             : }
    7086             : 
    7087             : static stmt *
    7088         518 : rel2bin_list(backend *be, sql_rel *rel, list *refs)
    7089             : {
    7090         518 :         mvc *sql = be->mvc;
    7091         518 :         stmt *l = NULL, *r = NULL;
    7092         518 :         list *slist = sa_list(sql->sa);
    7093             : 
    7094         518 :         if (rel->l)  /* first construct the sub relation */
    7095         518 :                 l = subrel_bin(be, rel->l, refs);
    7096         518 :         if (rel->r)  /* first construct the sub relation */
    7097         518 :                 r = subrel_bin(be, rel->r, refs);
    7098         518 :         l = subrel_project(be, l, refs, rel->l);
    7099         518 :         r = subrel_project(be, r, refs, rel->r);
    7100         518 :         if (!l || !r)
    7101             :                 return NULL;
    7102         518 :         list_append(slist, l);
    7103         518 :         list_append(slist, r);
    7104         518 :         return stmt_list(be, slist);
    7105             : }
    7106             : 
    7107             : static stmt *
    7108       14821 : rel2bin_psm(backend *be, sql_rel *rel)
    7109             : {
    7110       14821 :         mvc *sql = be->mvc;
    7111       14821 :         node *n;
    7112       14821 :         list *l = sa_list(sql->sa);
    7113       14821 :         stmt *sub = NULL;
    7114             : 
    7115       25353 :         for (n = rel->exps->h; n; n = n->next) {
    7116       15358 :                 sql_exp *e = n->data;
    7117       15358 :                 stmt *s = exp_bin(be, e, sub, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    7118       15356 :                 if (!s)
    7119             :                         return NULL;
    7120             : 
    7121       10532 :                 if (s && s->type == st_table) /* relational statement */
    7122          37 :                         sub = s->op1;
    7123             :                 else
    7124       10495 :                         append(l, s);
    7125             :         }
    7126        9995 :         return stmt_list(be, l);
    7127             : }
    7128             : 
    7129             : static stmt *
    7130         285 : rel2bin_partition_limits(backend *be, sql_rel *rel, list *refs)
    7131             : {
    7132         285 :         stmt *l = NULL, *r = NULL;
    7133         285 :         node *n = NULL;
    7134         285 :         list *slist = sa_list(be->mvc->sa);
    7135             : 
    7136         285 :         if (rel->l)  /* first construct the sub relation */
    7137           0 :                 l = subrel_bin(be, rel->l, refs);
    7138         285 :         if (rel->r)  /* first construct the sub relation */
    7139           0 :                 r = subrel_bin(be, rel->r, refs);
    7140         285 :         l = subrel_project(be, l, refs, rel->l);
    7141         285 :         r = subrel_project(be, r, refs, rel->r);
    7142         285 :         if ((rel->l && !l) || (rel->r && !r))
    7143             :                 return NULL;
    7144             : 
    7145         285 :         assert(rel->exps);
    7146         285 :         assert(rel->flag == ddl_alter_table_add_range_partition || rel->flag == ddl_alter_table_add_list_partition);
    7147             : 
    7148         285 :         if (rel->exps) {
    7149        2909 :                 for (n = rel->exps->h; n; n = n->next) {
    7150        2624 :                         sql_exp *e = n->data;
    7151        2624 :                         stmt *s = exp_bin(be, e, l, r, NULL, NULL, NULL, NULL, 0, 0, 0);
    7152        2624 :                         if (!s)
    7153             :                                 return NULL;
    7154        2624 :                         append(slist, s);
    7155             :                 }
    7156             :         }
    7157         285 :         return stmt_catalog(be, rel->flag, stmt_list(be, slist));
    7158             : }
    7159             : 
    7160             : static stmt *
    7161         157 : rel2bin_exception(backend *be, sql_rel *rel, list *refs)
    7162             : {
    7163         157 :         stmt *l = NULL, *r = NULL;
    7164         157 :         list *slist = sa_list(be->mvc->sa);
    7165             : 
    7166         157 :         if (rel->l)  /* first construct the sub relation */
    7167           0 :                 l = subrel_bin(be, rel->l, refs);
    7168         157 :         if (rel->r)  /* first construct the sub relation */
    7169         157 :                 r = subrel_bin(be, rel->r, refs);
    7170         157 :         l = subrel_project(be, l, refs, rel->l);
    7171         157 :         r = subrel_project(be, r, refs, rel->r);
    7172         157 :         if ((rel->l && !l) || (rel->r && !r))
    7173             :                 return NULL;
    7174             : 
    7175         157 :         assert(rel->exps);
    7176         314 :         for (node *n = rel->exps->h; n; n = n->next) {
    7177         157 :                 sql_exp *e = n->data;
    7178         157 :                 stmt *s = exp_bin(be, e, l, r, NULL, NULL, NULL, NULL, 0, 0, 0);
    7179         157 :                 if (!s)
    7180             :                         return NULL;
    7181         157 :                 list_append(slist, s);
    7182             :         }
    7183         157 :         return stmt_list(be, slist);
    7184             : }
    7185             : 
    7186             : static stmt *
    7187         360 : rel2bin_seq(backend *be, sql_rel *rel, list *refs)
    7188             : {
    7189         360 :         mvc *sql = be->mvc;
    7190         360 :         node *en = rel->exps->h;
    7191         360 :         stmt *restart, *sname, *seq, *seqname, *sl = NULL;
    7192         360 :         list *l = sa_list(sql->sa);
    7193             : 
    7194         360 :         if (rel->l) { /* first construct the sub relation */
    7195           0 :                 sl = subrel_bin(be, rel->l, refs);
    7196           0 :                 sl = subrel_project(be, sl, refs, rel->l);
    7197           0 :                 if (!sl)
    7198             :                         return NULL;
    7199             :         }
    7200             : 
    7201         360 :         restart = exp_bin(be, en->data, sl, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    7202         360 :         sname = exp_bin(be, en->next->data, sl, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    7203         360 :         seqname = exp_bin(be, en->next->next->data, sl, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    7204         360 :         seq = exp_bin(be, en->next->next->next->data, sl, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    7205         360 :         if (!restart || !sname || !seqname || !seq)
    7206             :                 return NULL;
    7207             : 
    7208         360 :         (void)refs;
    7209         360 :         append(l, sname);
    7210         360 :         append(l, seqname);
    7211         360 :         append(l, seq);
    7212         360 :         append(l, restart);
    7213         360 :         return stmt_catalog(be, rel->flag, stmt_list(be, l));
    7214             : }
    7215             : 
    7216             : static stmt *
    7217        3162 : rel2bin_trans(backend *be, sql_rel *rel, list *refs)
    7218             : {
    7219        3162 :         node *en = rel->exps->h;
    7220        3162 :         stmt *chain = exp_bin(be, en->data, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    7221        3162 :         stmt *name = NULL;
    7222             : 
    7223        3162 :         if (!chain)
    7224             :                 return NULL;
    7225             : 
    7226        3162 :         (void)refs;
    7227        3162 :         if (en->next) {
    7228          79 :                 name = exp_bin(be, en->next->data, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    7229          79 :                 if (!name)
    7230             :                         return NULL;
    7231             :         }
    7232        3162 :         return stmt_trans(be, rel->flag, chain, name);
    7233             : }
    7234             : 
    7235             : static stmt *
    7236        1273 : rel2bin_catalog_schema(backend *be, sql_rel *rel, list *refs)
    7237             : {
    7238        1273 :         mvc *sql = be->mvc;
    7239        1273 :         node *en = rel->exps->h;
    7240        1273 :         stmt *action = exp_bin(be, en->data, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    7241        1273 :         stmt *sname = NULL, *name = NULL, *ifexists = NULL;
    7242        1273 :         list *l = sa_list(sql->sa);
    7243             : 
    7244        1273 :         if (!action)
    7245             :                 return NULL;
    7246             : 
    7247        1273 :         (void)refs;
    7248        1273 :         en = en->next;
    7249        1273 :         sname = exp_bin(be, en->data, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    7250        1273 :         if (!sname)
    7251             :                 return NULL;
    7252        1273 :         append(l, sname);
    7253        1273 :         en = en->next;
    7254        1273 :         if (rel->flag == ddl_create_schema) {
    7255        1082 :                 if (en) {
    7256          38 :                         name = exp_bin(be, en->data, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    7257          38 :                         if (!name)
    7258             :                                 return NULL;
    7259             :                 } else {
    7260        1044 :                         name = stmt_atom_string_nil(be);
    7261             :                 }
    7262        1082 :                 append(l, name);
    7263             :         } else {
    7264         191 :                 assert(rel->flag == ddl_drop_schema);
    7265         191 :                 ifexists = exp_bin(be, en->data, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    7266         191 :                 if (!ifexists)
    7267             :                         return NULL;
    7268         191 :                 append(l, ifexists);
    7269             :         }
    7270        1273 :         append(l, action);
    7271        1273 :         return stmt_catalog(be, rel->flag, stmt_list(be, l));
    7272             : }
    7273             : 
    7274             : static stmt *
    7275       38061 : rel2bin_catalog_table(backend *be, sql_rel *rel, list *refs)
    7276             : {
    7277       38061 :         mvc *sql = be->mvc;
    7278       38061 :         node *en = rel->exps->h;
    7279       38061 :         stmt *action = exp_bin(be, en->data, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    7280       38061 :         stmt *table = NULL, *sname, *tname = NULL, *kname = NULL, *ifexists = NULL, *replace = NULL;
    7281       38061 :         list *l = sa_list(sql->sa);
    7282             : 
    7283       38061 :         if (!action)
    7284             :                 return NULL;
    7285             : 
    7286       38061 :         (void)refs;
    7287       38061 :         en = en->next;
    7288       38061 :         sname = exp_bin(be, en->data, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    7289       38061 :         if (!sname)
    7290             :                 return NULL;
    7291       38061 :         en = en->next;
    7292       38061 :         if (en) {
    7293       38061 :                 tname = exp_bin(be, en->data, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    7294       38061 :                 if (!tname)
    7295             :                         return NULL;
    7296       38061 :                 en = en->next;
    7297             :         }
    7298       38061 :         append(l, sname);
    7299       38061 :         assert(tname);
    7300       38061 :         append(l, tname);
    7301       38061 :         if (rel->flag == ddl_drop_constraint) { /* needs extra string parameter for constraint name */
    7302         150 :                 if (en) {
    7303         150 :                         kname = exp_bin(be, en->data, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    7304         150 :                         if (!kname)
    7305             :                                 return NULL;
    7306         150 :                         en = en->next;
    7307             :                 }
    7308         150 :                 append(l, kname);
    7309             :         }
    7310       38061 :         if (rel->flag != ddl_drop_table && rel->flag != ddl_drop_view && rel->flag != ddl_drop_constraint) {
    7311       33874 :                 if (en) {
    7312       33874 :                         table = exp_bin(be, en->data, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    7313       33874 :                         if (!table)
    7314             :                                 return NULL;
    7315       33874 :                         en = en->next;
    7316             :                 }
    7317       33874 :                 append(l, table);
    7318             :         } else {
    7319        4187 :                 if (en) {
    7320        4187 :                         ifexists = exp_bin(be, en->data, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    7321        4187 :                         if (!ifexists)
    7322             :                                 return NULL;
    7323        4187 :                         en = en->next;
    7324             :                 } else {
    7325           0 :                         ifexists = stmt_atom_int(be, 0);
    7326             :                 }
    7327        4187 :                 append(l, ifexists);
    7328             :         }
    7329       38061 :         append(l, action);
    7330       38061 :         if (rel->flag == ddl_create_view) {
    7331       22390 :                 if (en) {
    7332       22390 :                         replace = exp_bin(be, en->data, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    7333       22390 :                         if (!replace)
    7334             :                                 return NULL;
    7335             :                 } else {
    7336           0 :                         replace = stmt_atom_int(be, 0);
    7337             :                 }
    7338       22390 :                 append(l, replace);
    7339       15671 :         } else if (rel->flag == ddl_create_table && en) {
    7340         101 :                 stmt *name = exp_bin(be, en->data, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    7341         101 :                 if (!name)
    7342             :                         return NULL;
    7343         101 :                 en = en->next;
    7344         101 :                 append(l, name);
    7345         101 :                 if (!en)
    7346             :                         return NULL;
    7347         101 :                 stmt *passwd = exp_bin(be, en->data, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    7348         101 :                 if (!passwd)
    7349             :                         return NULL;
    7350         101 :                 append(l, passwd);
    7351             :         }
    7352       38061 :         return stmt_catalog(be, rel->flag, stmt_list(be, l));
    7353             : }
    7354             : 
    7355             : static stmt *
    7356      245094 : rel2bin_catalog2(backend *be, sql_rel *rel, list *refs)
    7357             : {
    7358      245094 :         mvc *sql = be->mvc;
    7359      245094 :         node *en;
    7360      245094 :         list *l = sa_list(sql->sa);
    7361             : 
    7362      245094 :         (void)refs;
    7363     1481259 :         for (en = rel->exps->h; en; en = en->next) {
    7364     1236165 :                 stmt *es = NULL;
    7365             : 
    7366     1236165 :                 if (en->data) {
    7367     1217721 :                         es = exp_bin(be, en->data, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0);
    7368     1217721 :                         if (!es)
    7369             :                                 return NULL;
    7370             :                 } else {
    7371       18444 :                         es = stmt_atom_string_nil(be);
    7372             :                 }
    7373     1236165 :                 append(l,es);
    7374             :         }
    7375      245094 :         return stmt_catalog(be, rel->flag, stmt_list(be, l));
    7376             : }
    7377             : 
    7378             : static stmt *
    7379      303805 : rel2bin_ddl(backend *be, sql_rel *rel, list *refs)
    7380             : {
    7381      303805 :         mvc *sql = be->mvc;
    7382      303805 :         stmt *s = NULL;
    7383             : 
    7384      303805 :         switch (rel->flag) {
    7385          74 :                 case ddl_output:
    7386          74 :                         s = rel2bin_output(be, rel, refs);
    7387          74 :                         sql->type = Q_TABLE;
    7388          74 :                         break;
    7389         518 :                 case ddl_list:
    7390         518 :                         s = rel2bin_list(be, rel, refs);
    7391         518 :                         break;
    7392       14821 :                 case ddl_psm:
    7393       14821 :                         s = rel2bin_psm(be, rel);
    7394       14821 :                         break;
    7395         157 :                 case ddl_exception:
    7396         157 :                         s = rel2bin_exception(be, rel, refs);
    7397         157 :                         break;
    7398         360 :                 case ddl_create_seq:
    7399             :                 case ddl_alter_seq:
    7400         360 :                         s = rel2bin_seq(be, rel, refs);
    7401         360 :                         sql->type = Q_SCHEMA;
    7402         360 :                         break;
    7403         285 :                 case ddl_alter_table_add_range_partition:
    7404             :                 case ddl_alter_table_add_list_partition:
    7405         285 :                         s = rel2bin_partition_limits(be, rel, refs);
    7406         285 :                         sql->type = Q_SCHEMA;
    7407         285 :                         break;
    7408        3162 :                 case ddl_release:
    7409             :                 case ddl_commit:
    7410             :                 case ddl_rollback:
    7411             :                 case ddl_trans:
    7412        3162 :                         s = rel2bin_trans(be, rel, refs);
    7413        3162 :                         sql->type = Q_TRANS;
    7414        3162 :                         break;
    7415        1273 :                 case ddl_create_schema:
    7416             :                 case ddl_drop_schema:
    7417        1273 :                         s = rel2bin_catalog_schema(be, rel, refs);
    7418        1273 :                         sql->type = Q_SCHEMA;
    7419        1273 :                         break;
    7420       38061 :                 case ddl_create_table:
    7421             :                 case ddl_drop_table:
    7422             :                 case ddl_create_view:
    7423             :                 case ddl_drop_view:
    7424             :                 case ddl_drop_constraint:
    7425             :                 case ddl_alter_table:
    7426       38061 :                         s = rel2bin_catalog_table(be, rel, refs);
    7427       38061 :                         sql->type = Q_SCHEMA;
    7428       38061 :                         break;
    7429      245094 :                 case ddl_drop_seq:
    7430             :                 case ddl_create_type:
    7431             :                 case ddl_drop_type:
    7432             :                 case ddl_drop_index:
    7433             :                 case ddl_create_function:
    7434             :                 case ddl_drop_function:
    7435             :                 case ddl_create_trigger:
    7436             :                 case ddl_drop_trigger:
    7437             :                 case ddl_grant_roles:
    7438             :                 case ddl_revoke_roles:
    7439             :                 case ddl_grant:
    7440             :                 case ddl_revoke:
    7441             :                 case ddl_grant_func:
    7442             :                 case ddl_revoke_func:
    7443             :                 case ddl_create_user:
    7444             :                 case ddl_drop_user:
    7445             :                 case ddl_alter_user:
    7446             :                 case ddl_rename_user:
    7447             :                 case ddl_create_role:
    7448             :                 case ddl_drop_role:
    7449             :                 case ddl_alter_table_add_table:
    7450             :                 case ddl_alter_table_del_table:
    7451             :                 case ddl_alter_table_set_access:
    7452             :                 case ddl_comment_on:
    7453             :                 case ddl_rename_schema:
    7454             :                 case ddl_rename_table:
    7455             :                 case ddl_rename_column:
    7456      245094 :                         s = rel2bin_catalog2(be, rel, refs);
    7457      245094 :                         sql->type = Q_SCHEMA;
    7458      245094 :                         break;
    7459             :                 default:
    7460           0 :                         assert(0);
    7461             :         }
    7462      303803 :         return s;
    7463             : }
    7464             : 
    7465             : static stmt *
    7466     1196336 : subrel_bin(backend *be, sql_rel *rel, list *refs)
    7467             : {
    7468     1196336 :         mvc *sql = be->mvc;
    7469     1196336 :         stmt *s = NULL;
    7470             : 
    7471     1196336 :         if (mvc_highwater(sql))
    7472           2 :                 return sql_error(be->mvc, 10, SQLSTATE(42000) "Query too complex: running out of stack space");
    7473             : 
    7474     1196334 :         if (!rel)
    7475             :                 return s;
    7476     1196334 :         if (rel_is_ref(rel)) {
    7477       15218 :                 s = refs_find_rel(refs, rel);
    7478             :                 /* needs a proper fix!! */
    7479       15218 :                 if (s)
    7480             :                         return s;
    7481             :         }
    7482     1190276 :         switch (rel->op) {
    7483      146874 :         case op_basetable:
    7484      146874 :                 s = rel2bin_basetable(be, rel);
    7485      146873 :                 sql->type = Q_TABLE;
    7486      146873 :                 break;
    7487        3014 :         case op_table:
    7488        3014 :                 s = rel2bin_table(be, rel, refs);
    7489        3014 :                 sql->type = Q_TABLE;
    7490        3014 :                 break;
    7491       75939 :         case op_join:
    7492             :         case op_left:
    7493             :         case op_right:
    7494             :         case op_full:
    7495       75939 :                 s = rel2bin_join(be, rel, refs);
    7496       75939 :                 sql->type = Q_TABLE;
    7497       75939 :                 break;
    7498        3112 :         case op_semi:
    7499        3112 :                 s = rel2bin_semijoin(be, rel, refs);
    7500        3112 :                 sql->type = Q_TABLE;
    7501        3112 :                 break;
    7502        1498 :         case op_anti:
    7503        1498 :                 s = rel2bin_antijoin(be, rel, refs);
    7504        1498 :                 sql->type = Q_TABLE;
    7505        1498 :                 break;
    7506         301 :         case op_union:
    7507         301 :                 s = rel2bin_union(be, rel, refs);
    7508         301 :                 sql->type = Q_TABLE;
    7509         301 :                 break;
    7510        4207 :         case op_munion:
    7511        4207 :                 s = rel2bin_munion(be, rel, refs);
    7512        4207 :                 sql->type = Q_TABLE;
    7513        4207 :                 break;
    7514        2022 :         case op_except:
    7515        2022 :                 s = rel2bin_except(be, rel, refs);
    7516        2022 :                 sql->type = Q_TABLE;
    7517        2022 :                 break;
    7518         375 :         case op_inter:
    7519         375 :                 s = rel2bin_inter(be, rel, refs);
    7520         375 :                 sql->type = Q_TABLE;
    7521         375 :                 break;
    7522      393410 :         case op_project:
    7523      393410 :                 s = rel2bin_project(be, rel, refs, NULL);
    7524      393397 :                 sql->type = Q_TABLE;
    7525      393397 :                 break;
    7526       69199 :         case op_select:
    7527       69199 :                 s = rel2bin_select(be, rel, refs);
    7528       69199 :                 sql->type = Q_TABLE;
    7529       69199 :                 break;
    7530       13685 :         case op_groupby:
    7531       13685 :                 s = rel2bin_groupby(be, rel, refs);
    7532       13685 :                 sql->type = Q_TABLE;
    7533       13685 :                 break;
    7534       16797 :         case op_topn:
    7535       16797 :                 s = rel2bin_topn(be, rel, refs);
    7536       16797 :                 sql->type = Q_TABLE;
    7537       16797 :                 break;
    7538          21 :         case op_sample:
    7539          21 :                 s = rel2bin_sample(be, rel, refs);
    7540          21 :                 sql->type = Q_TABLE;
    7541          21 :                 break;
    7542      109607 :         case op_insert:
    7543      109607 :                 s = rel2bin_insert(be, rel, refs);
    7544      109606 :                 if (!(rel->returning) && sql->type == Q_TABLE)
    7545      104763 :                         sql->type = Q_UPDATE;
    7546             :                 break;
    7547        4184 :         case op_update:
    7548        4184 :                 s = rel2bin_update(be, rel, refs);
    7549        4184 :                 if (!(rel->returning) && sql->type == Q_TABLE)
    7550        4026 :                         sql->type = Q_UPDATE;
    7551             :                 break;
    7552         476 :         case op_delete:
    7553         476 :                 s = rel2bin_delete(be, rel, refs);
    7554         476 :                 if (!(rel->returning) && sql->type == Q_TABLE)
    7555         341 :                         sql->type = Q_UPDATE;
    7556             :                 break;
    7557       41706 :         case op_truncate:
    7558       41706 :                 s = rel2bin_truncate(be, rel);
    7559       41706 :                 if (sql->type == Q_TABLE)
    7560           5 :                         sql->type = Q_UPDATE;
    7561             :                 break;
    7562          44 :         case op_merge:
    7563          44 :                 s = rel2bin_merge(be, rel, refs);
    7564          44 :                 if (sql->type == Q_TABLE)
    7565          44 :                         sql->type = Q_UPDATE;
    7566             :                 break;
    7567      303805 :         case op_ddl:
    7568      303805 :                 s = rel2bin_ddl(be, rel, refs);
    7569      303805 :                 break;
    7570             :         }
    7571     1190259 :         if (s && rel_is_ref(rel)) {
    7572        9160 :                 list_append(refs, rel);
    7573        9160 :                 list_append(refs, s);
    7574             :         }
    7575             :         return s;
    7576             : }
    7577             : 
    7578             : stmt *
    7579       10812 : rel_bin(backend *be, sql_rel *rel)
    7580             : {
    7581       10812 :         mvc *sql = be->mvc;
    7582       10812 :         list *refs = sa_list(sql->sa);
    7583       10811 :         mapi_query_t sqltype = sql->type;
    7584       10811 :         stmt *s = subrel_bin(be, rel, refs);
    7585             : 
    7586       10812 :         s = subrel_project(be, s, refs, rel);
    7587       10812 :         if (sqltype == Q_SCHEMA)
    7588         317 :                 sql->type = sqltype;  /* reset */
    7589             : 
    7590       10812 :         if (be->mb->errors) {
    7591           0 :                 if (be->mvc->sa->eb.enabled)
    7592           0 :                         eb_error(&be->mvc->sa->eb, be->mvc->errstr[0] ? be->mvc->errstr : be->mb->errors, 1000);
    7593             :                 return NULL;
    7594             :         }
    7595             :         return s;
    7596             : }
    7597             : 
    7598             : stmt *
    7599      572223 : output_rel_bin(backend *be, sql_rel *rel, int top)
    7600             : {
    7601      572223 :         mvc *sql = be->mvc;
    7602      572223 :         list *refs = sa_list(sql->sa);
    7603      572223 :         mapi_query_t sqltype = sql->type;
    7604      572223 :         stmt *s = NULL;
    7605             : 
    7606      572223 :         be->join_idx = 0;
    7607      572223 :         be->rowcount = 0;
    7608      572223 :         be->silent = !top;
    7609             : 
    7610      572223 :         s = subrel_bin(be, rel, refs);
    7611      572215 :         s = subrel_project(be, s, refs, rel);
    7612             : 
    7613      572214 :         if (!s)
    7614             :                 return NULL;
    7615      572206 :         if (sqltype == Q_SCHEMA)
    7616      285180 :                 sql->type = sqltype; /* reset */
    7617             : 
    7618      572206 :         if (!be->silent) { /* don't generate outputs when we are silent */
    7619      565826 :                 if (!is_ddl(rel->op) && sql->type == Q_TABLE && stmt_output(be, s) < 0) {
    7620             :                         return NULL;
    7621      565832 :                 } else if (be->rowcount > 0 && sqltype == Q_UPDATE && stmt_affected_rows(be, be->rowcount) < 0) {
    7622             :                         /* only call stmt_affected_rows outside functions and ddl */
    7623             :                         return NULL;
    7624             :                 }
    7625             :         }
    7626             :         return s;
    7627             : }

Generated by: LCOV version 1.14