LCOV - code coverage report
Current view: top level - sql/server - rel_select.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 3738 4072 91.8 %
Date: 2024-12-19 20:05:57 Functions: 116 117 99.1 %

          Line data    Source code
       1             : /*
       2             :  * SPDX-License-Identifier: MPL-2.0
       3             :  *
       4             :  * This Source Code Form is subject to the terms of the Mozilla Public
       5             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       6             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       7             :  *
       8             :  * Copyright 2024 MonetDB Foundation;
       9             :  * Copyright August 2008 - 2023 MonetDB B.V.;
      10             :  * Copyright 1997 - July 2008 CWI.
      11             :  */
      12             : 
      13             : #include "monetdb_config.h"
      14             : #include "rel_select.h"
      15             : #include "sql_tokens.h"
      16             : #include "sql_privileges.h"
      17             : #include "sql_env.h"
      18             : #include "sql_decimal.h"
      19             : #include "sql_qc.h"
      20             : #include "rel_rel.h"
      21             : #include "rel_basetable.h"
      22             : #include "rel_exp.h"
      23             : #include "rel_xml.h"
      24             : #include "rel_dump.h"
      25             : #include "rel_prop.h"
      26             : #include "rel_psm.h"
      27             : #include "rel_schema.h"
      28             : #include "rel_unnest.h"
      29             : #include "rel_sequence.h"
      30             : #include "rel_file_loader.h"
      31             : 
      32             : #define VALUE_FUNC(f) (f->func->type == F_FUNC || f->func->type == F_FILT)
      33             : #define check_card(card,f) ((card == card_none && !f->res) || (CARD_VALUE(card) && f->res && VALUE_FUNC(f)) || card == card_loader || (card == card_relation && f->func->type == F_UNION))
      34             : 
      35             : /* return all expressions, with table name == tname */
      36             : static list *
      37       57141 : rel_table_projections( mvc *sql, sql_rel *rel, char *tname, int level )
      38             : {
      39       57654 :         list *exps;
      40             : 
      41       57654 :         if (mvc_highwater(sql))
      42           0 :                 return sql_error(sql, 10, SQLSTATE(42000) "Query too complex: running out of stack space");
      43             : 
      44       57654 :         if (!rel)
      45             :                 return NULL;
      46             : 
      47       57654 :         if (!tname)
      48       41914 :                 return _rel_projections(sql, rel, NULL, 1, 0, 1);
      49             : 
      50       15740 :         switch(rel->op) {
      51         521 :         case op_join:
      52             :         case op_left:
      53             :         case op_right:
      54             :         case op_full:
      55         521 :                 exps = rel_table_projections( sql, rel->l, tname, level+1);
      56         521 :                 if (exps)
      57             :                         return exps;
      58          13 :                 return rel_table_projections( sql, rel->r, tname, level+1);
      59         500 :         case op_semi:
      60             :         case op_anti:
      61             :         case op_select:
      62         500 :                 return rel_table_projections( sql, rel->l, tname, level+1);
      63             : 
      64          54 :         case op_topn:
      65             :         case op_sample:
      66             :         case op_groupby:
      67             :         case op_union:
      68             :         case op_except:
      69             :         case op_inter:
      70             :         case op_project:
      71          54 :                 if (!is_processed(rel) && level == 0)
      72           0 :                         return rel_table_projections( sql, rel->l, tname, level+1);
      73             :                 /* fall through */
      74             :         case op_munion:
      75          60 :                 if (!is_processed(rel) && level == 0) {
      76           0 :                         node *n = ((list*)rel->l)->h;
      77           0 :                         if (n)
      78           0 :                                 return rel_table_projections(sql, n->data, tname, level+1);
      79             :                 }
      80             :         /* fall through */
      81             :         case op_table:
      82             :         case op_basetable:
      83       14719 :                 if (is_basetable(rel->op) && !rel->exps)
      84       14159 :                         return rel_base_project_all(sql, rel, tname);
      85         560 :                 if (rel->exps) {
      86         560 :                         int rename = 0;
      87         560 :                         node *en;
      88             : 
      89             :                         /* first check alias */
      90         560 :                         if (!is_base(rel->op) && !level) {
      91          54 :                                 list *exps = sa_list(sql->sa);
      92             : 
      93         409 :                                 for (en = rel->exps->h; en && !rename; en = en->next) {
      94         355 :                                         sql_exp *e = en->data;;
      95             : 
      96         355 :                                         if ((is_basecol(e) && exp_relname(e) && strcmp(exp_relname(e), tname) == 0) ||
      97         138 :                                             (is_basecol(e) && !exp_relname(e) && e->l && strcmp(e->l, tname) == 0)) {
      98         217 :                                                 if (exp_name(e) && exps_bind_column2(exps, tname, exp_name(e), NULL))
      99             :                                                         rename = 1;
     100             :                                                 else
     101         217 :                                                         append(exps, e);
     102             :                                         }
     103             :                                 }
     104             :                         }
     105             : 
     106         560 :                         exps = new_exp_list(sql->sa);
     107        4930 :                         for (en = rel->exps->h; en; en = en->next) {
     108        4370 :                                 sql_exp *e = en->data;
     109        4370 :                                 if (is_basecol(e) && exp_relname(e) && strcmp(exp_relname(e), tname) == 0) {
     110        4230 :                                         if (rename)
     111           0 :                                                 append(exps, exp_alias_ref(sql, e));
     112             :                                         else {
     113        4230 :                                                 sql_exp *ne = exp_ref(sql, e);
     114        4230 :                                                 exp_setname(sql, ne, tname, exp_name(e));
     115        4230 :                                                 append(exps, ne);
     116             :                                         }
     117             :                                 }
     118        4370 :                                 if (is_basecol(e) && !exp_relname(e) && e->l && strcmp(e->l, tname) == 0) {
     119           0 :                                         if (rename)
     120           0 :                                                 append(exps, exp_alias_ref(sql, e));
     121             :                                         else {
     122           0 :                                                 sql_exp *ne = exp_ref(sql, e);
     123           0 :                                                 exp_setname(sql, ne, tname, exp_name(e));
     124           0 :                                                 append(exps, ne);
     125             :                                         }
     126             :                                 }
     127             : 
     128             :                         }
     129         560 :                         if (exps && list_length(exps))
     130             :                                 return exps;
     131             :                 }
     132             :                 /* fall through */
     133             :         default:
     134             :                 return NULL;
     135             :         }
     136             : }
     137             : 
     138             : static sql_exp *
     139           0 : rel_lastexp(mvc *sql, sql_rel *rel )
     140             : {
     141           0 :         sql_exp *e;
     142             : 
     143           0 :         if (!is_processed(rel) || is_topn(rel->op) || is_sample(rel->op))
     144           0 :                 rel = rel_parent(rel);
     145           0 :         assert(list_length(rel->exps));
     146           0 :         if (rel->op == op_project) {
     147           0 :                 list_hash_clear(rel->exps);
     148           0 :                 return exp_ref(sql, rel->exps->t->data);
     149             :         }
     150           0 :         assert(is_project(rel->op));
     151           0 :         e = rel->exps->t->data;
     152           0 :         return exp_ref(sql, e);
     153             : }
     154             : 
     155             : static sql_rel *
     156       36634 : rel_orderby(mvc *sql, sql_rel *l)
     157             : {
     158       36634 :         sql_rel *rel = rel_create(sql->sa);
     159       36634 :         if (!rel)
     160             :                 return NULL;
     161             : 
     162       36634 :         assert(l->op == op_project && !l->r);
     163       36634 :         rel->l = l;
     164       36634 :         rel->r = NULL;
     165       36634 :         rel->op = op_project;
     166       36634 :         rel->exps = rel_projections(sql, l, NULL, 1, 0);
     167       36634 :         rel->card = l->card;
     168       36634 :         rel->nrcols = l->nrcols;
     169       36634 :         return rel;
     170             : }
     171             : 
     172             : /* forward refs */
     173             : static sql_rel * rel_setquery(sql_query *query, symbol *sq);
     174             : static sql_rel * rel_joinquery(sql_query *query, symbol *sq, list *refs);
     175             : 
     176             : static sql_rel *
     177      202552 : rel_table_optname(mvc *sql, sql_rel *sq, symbol *optname, list *refs)
     178             : {
     179      202552 :         sql_rel *osq = sq;
     180      202552 :         node *ne;
     181             : 
     182      202552 :         if (optname && optname->token == SQL_NAME) {
     183       17993 :                 dlist *columnrefs = NULL;
     184       17993 :                 char *tname = optname->data.lval->h->data.sval;
     185       17993 :                 list *l = sa_list(sql->sa);
     186             : 
     187       17993 :                 columnrefs = optname->data.lval->h->next->data.lval;
     188       17993 :                 if (is_topn(sq->op) || is_sample(sq->op) || ((is_simple_project(sq->op) || is_groupby(sq->op)) && sq->r) || is_base(sq->op)) {
     189         110 :                         sq = rel_project(sql->sa, sq, rel_projections(sql, sq, NULL, 1, 0));
     190         110 :                         osq = sq;
     191             :                 }
     192       17993 :                 if (columnrefs && dlist_length(columnrefs) != list_length(sq->exps))
     193           6 :                         return sql_error(sql, 02, SQLSTATE(42000) "SELECT: The number of aliases don't match the number of columns (%d != %d)", dlist_length(columnrefs), sq->nrcols);
     194        5032 :                 if (columnrefs && sq->exps) {
     195        5032 :                         dnode *d = columnrefs->h;
     196             : 
     197        5032 :                         ne = sq->exps->h;
     198        5032 :                         list_hash_clear(sq->exps);
     199       20772 :                         for (; d && ne; d = d->next, ne = ne->next) {
     200       10709 :                                 sql_exp *e = ne->data;
     201             : 
     202       10709 :                                 if (exps_bind_column2(l, tname, d->data.sval, NULL))
     203           1 :                                         return sql_error(sql, ERR_AMBIGUOUS, SQLSTATE(42000) "SELECT: Duplicate column name '%s.%s'", tname, d->data.sval);
     204       10708 :                                 exp_setname(sql, e, tname, d->data.sval );
     205       10708 :                                 if (!is_intern(e))
     206       10708 :                                         set_basecol(e);
     207       10708 :                                 append(l, e);
     208             :                         }
     209             :                 }
     210       17986 :                 if (!columnrefs && sq->exps) {
     211       12955 :                         ne = sq->exps->h;
     212       12955 :                         list_hash_clear(sq->exps);
     213      126652 :                         for (; ne; ne = ne->next) {
     214      100742 :                                 sql_exp *e = ne->data;
     215      100742 :                                 char *name = NULL;
     216             : 
     217      100742 :                                 if (!is_intern(e)) {
     218      100742 :                                         if (!exp_name(e))
     219         108 :                                                 name = make_label(sql->sa, ++sql->label);
     220      100742 :                                         noninternexp_setname(sql, e, tname, name);
     221      100742 :                                         set_basecol(e);
     222             :                                 }
     223             :                         }
     224             :                 }
     225       17986 :                 if (refs) { /* if this relation is under a FROM clause, check for duplicate names */
     226       17936 :                         if (list_find(refs, tname, (fcmp) &strcmp))
     227           6 :                                 return sql_error(sql, 02, SQLSTATE(42000) "SELECT: relation name \"%s\" specified more than once", tname);
     228       17930 :                         assert(tname);
     229       17930 :                         list_append(refs, tname);
     230             :                 }
     231             :         } else {
     232      184559 :                 if (!is_project(sq->op) || is_topn(sq->op) || is_sample(sq->op) || ((is_simple_project(sq->op) || is_groupby(sq->op)) && sq->r)) {
     233       58602 :                         sq = rel_project(sql->sa, sq, rel_projections(sql, sq, NULL, 1, 1));
     234       58602 :                         osq = sq;
     235             :                 }
     236     2320922 :                 for (ne = osq->exps->h; ne; ne = ne->next) {
     237     2136363 :                         sql_exp *e = ne->data;
     238             : 
     239     2136363 :                         if (!is_intern(e))
     240     2009777 :                                 set_basecol(e);
     241             :                 }
     242             :         }
     243             :         return osq;
     244             : }
     245             : 
     246             : static sql_rel *
     247       97575 : rel_subquery_optname(sql_query *query, symbol *ast, list *refs)
     248             : {
     249       97575 :         mvc *sql = query->sql;
     250       97575 :         SelectNode *sn = (SelectNode *) ast;
     251       97575 :         exp_kind ek = {type_value, card_relation, TRUE};
     252       97575 :         sql_rel *sq = rel_subquery(query, ast, ek);
     253             : 
     254       97575 :         assert(ast->token == SQL_SELECT);
     255       97575 :         if (!sq)
     256             :                 return NULL;
     257             : 
     258       97558 :         return rel_table_optname(sql, sq, sn->name, refs);
     259             : }
     260             : 
     261             : sql_rel *
     262        6933 : rel_with_query(sql_query *query, symbol *q )
     263             : {
     264        6933 :         mvc *sql = query->sql;
     265        6933 :         dnode *d = q->data.lval->h;
     266        6933 :         symbol *next = d->next->data.sym;
     267        6933 :         sql_rel *rel;
     268             : 
     269        6933 :         if (!stack_push_frame(sql, NULL))
     270           0 :                 return sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     271             :         /* first handle all with's (ie inlined views) */
     272       19667 :         for (d = d->data.lval->h; d; d = d->next) {
     273       12740 :                 symbol *sym = d->data.sym;
     274       12740 :                 dnode *dn = sym->data.lval->h;
     275       12740 :                 char *rname = qname_schema_object(dn->data.lval);
     276       12740 :                 sql_rel *nrel;
     277             : 
     278       12740 :                 if (frame_find_rel_view(sql, rname)) {
     279           1 :                         stack_pop_frame(sql);
     280           1 :                         return sql_error(sql, 01, SQLSTATE(42000) "View '%s' already declared", rname);
     281             :                 }
     282       12739 :                 nrel = rel_semantic(query, sym);
     283       12739 :                 if (!nrel) {
     284           5 :                         stack_pop_frame(sql);
     285           5 :                         return NULL;
     286             :                 }
     287       12734 :                 if (!stack_push_rel_view(sql, rname, nrel)) {
     288           0 :                         stack_pop_frame(sql);
     289           0 :                         return sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
     290             :                 }
     291       12734 :                 if (!is_project(nrel->op)) {
     292           0 :                         if (is_topn(nrel->op) || is_sample(nrel->op)) {
     293           0 :                                 nrel = rel_project(sql->sa, nrel, rel_projections(sql, nrel, NULL, 1, 1));
     294             :                         } else {
     295           0 :                                 stack_pop_frame(sql);
     296           0 :                                 return NULL;
     297             :                         }
     298             :                 }
     299       12734 :                 assert(is_project(nrel->op));
     300       12734 :                 if (is_project(nrel->op) && nrel->exps) {
     301       12734 :                         node *ne = nrel->exps->h;
     302             : 
     303       56477 :                         for (; ne; ne = ne->next) {
     304       43743 :                                 sql_exp *e = ne->data;
     305       43743 :                                 char *name = NULL;
     306             : 
     307       43743 :                                 if (!is_intern(e)) {
     308       43743 :                                         if (!exp_name(e))
     309          13 :                                                 name = make_label(sql->sa, ++sql->label);
     310       43743 :                                         noninternexp_setname(sql, e, rname, name);
     311       43743 :                                         set_basecol(e);
     312             :                                 }
     313             :                         }
     314       12734 :                         list_hash_clear(nrel->exps);
     315             :                 }
     316             :         }
     317        6927 :         rel = rel_semantic(query, next);
     318        6927 :         stack_pop_frame(sql);
     319        6927 :         return rel;
     320             : }
     321             : 
     322             : static sql_rel *
     323       59661 : query_exp_optname(sql_query *query, symbol *q, list *refs)
     324             : {
     325       59661 :         mvc *sql = query->sql;
     326       59661 :         switch (q->token) {
     327         943 :         case SQL_WITH:
     328             :         {
     329         943 :                 sql_rel *tq = rel_with_query(query, q);
     330             : 
     331         943 :                 if (!tq)
     332             :                         return NULL;
     333         943 :                 if (q->data.lval->t->type == type_symbol)
     334         943 :                         return rel_table_optname(sql, tq, q->data.lval->t->data.sym, refs);
     335             :                 return tq;
     336             :         }
     337       58718 :         case SQL_JOIN:
     338             :         {
     339       58718 :                 sql_rel *tq = rel_joinquery(query, q, refs);
     340             : 
     341       58718 :                 if (!tq)
     342             :                         return NULL;
     343       58677 :                 return rel_table_optname(sql, tq, q->data.lval->t->data.sym, NULL);
     344             :         }
     345           0 :         default:
     346           0 :                 (void) sql_error(sql, 02, SQLSTATE(42000) "case %d %s", (int) q->token, token2string(q->token));
     347             :         }
     348           0 :         return NULL;
     349             : }
     350             : 
     351             : static sql_subfunc *
     352      113274 : bind_func_(mvc *sql, char *sname, char *fname, list *ops, sql_ftype type, bool private, bool *found, bool exact)
     353             : {
     354      113274 :         sql_subfunc *sf = NULL;
     355             : 
     356      113499 :         if (sql->forward && strcmp(fname, sql->forward->base.name) == 0 &&
     357         225 :             list_cmp(sql->forward->ops, ops, (fcmp)&arg_subtype_cmp) == 0 &&
     358           0 :             execute_priv(sql, sql->forward) && type == sql->forward->type)
     359           0 :                 return sql_dup_subfunc(sql->sa, sql->forward, NULL, NULL);
     360      113274 :         sf = sql_bind_func_(sql, sname, fname, ops, type, private, exact);
     361      113274 :         if (found)
     362      112744 :                 *found |= sf != NULL;
     363      113274 :         if (sf && execute_priv(sql, sf->func))
     364             :                 return sf;
     365             :         return NULL;
     366             : }
     367             : 
     368             : static sql_subfunc *
     369      617434 : bind_func(mvc *sql, char *sname, char *fname, sql_subtype *t1, sql_subtype *t2, int nr, sql_ftype type, bool private, bool *found, bool exact)
     370             : {
     371      617434 :         list *tl = sa_list(sql->sa);
     372      617434 :         assert(nr >= 1 && nr <= 2);
     373      617434 :         append(tl, t1);
     374      617434 :         if (nr == 2)
     375      522736 :                 append(tl, t2);
     376      617434 :         sql_subfunc *sf = NULL;
     377             : 
     378      617434 :         if (sql->forward) {
     379      131598 :                 if (execute_priv(sql, sql->forward) &&
     380      131598 :                     strcmp(fname, sql->forward->base.name) == 0 &&
     381         230 :                    ((!t1 && list_length(sql->forward->ops) == 0) ||
     382         232 :                     (!t2 && list_length(sql->forward->ops) == 1 && arg_subtype_cmp(sql->forward->ops->h->data, t1) == 0) ||
     383         228 :                     (list_length(sql->forward->ops) == 2 &&
     384           0 :                         arg_subtype_cmp(sql->forward->ops->h->data, t1) == 0 &&
     385           2 :                         arg_subtype_cmp(sql->forward->ops->h->next->data, t2) == 0)) && type == sql->forward->type) {
     386           2 :                         return sql_dup_subfunc(sql->sa, sql->forward, NULL, NULL);
     387             :                 }
     388             :         }
     389      617432 :         sf = sql_bind_func_(sql, sname, fname, tl, type, private, exact);
     390      617432 :         if (found)
     391      617432 :                 *found |= sf != NULL;
     392      617432 :         if (sf && execute_priv(sql, sf->func))
     393             :                 return sf;
     394             :         return NULL;
     395             : }
     396             : 
     397             : static sql_subfunc *
     398      636843 : find_func(mvc *sql, char *sname, char *fname, int len, sql_ftype type, bool private, sql_subfunc *prev, bool *found)
     399             : {
     400      636843 :         sql_subfunc *sf = NULL;
     401             : 
     402      636843 :         if (sql->forward && strcmp(fname, sql->forward->base.name) == 0 && list_length(sql->forward->ops) == len && execute_priv(sql, sql->forward) && type == sql->forward->type)
     403           0 :                 return sql_dup_subfunc(sql->sa, sql->forward, NULL, NULL);
     404      636843 :         sf = sql_find_func(sql, sname, fname, len, type, private, prev);
     405      636843 :         if (found)
     406           0 :                 *found |= sf != NULL;
     407      636843 :         if (sf && execute_priv(sql, sf->func))
     408             :                 return sf;
     409             :         return NULL;
     410             : }
     411             : 
     412             : static sql_exp *
     413      694833 : exp_fix_scale(mvc *sql, sql_subtype *ct, sql_exp *e)
     414             : {
     415      694833 :         sql_subtype *et = exp_subtype(e);
     416             : 
     417      694833 :         if (ct->type->scale == SCALE_FIX && et->type->scale == SCALE_FIX) {
     418       82996 :                 int scale_diff = ((int) ct->scale - (int) et->scale);
     419             : 
     420       82996 :                 if (scale_diff) {
     421         132 :                         if (scale_diff < 0)
     422             :                                 return e;
     423         102 :                         sql_subtype st;
     424         102 :                         int scale = ct->scale;
     425         102 :                         int digits = ((et->type->eclass == EC_NUM)?bits2digits(et->digits):et->digits)-et->scale+scale;
     426         102 :                         (void)sql_find_subtype(&st, ct->type->base.name, digits, scale);
     427         102 :                         return exp_convert(sql, e, et, &st);
     428             :                 }
     429             :         }
     430             :         return e;
     431             : }
     432             : 
     433             : static lng
     434        9649 : rel_get_count(sql_rel *rel)
     435             : {
     436       31093 :         if (!rel)
     437             :                 return 0;
     438       31089 :         prop *p = NULL;
     439       31089 :         if (rel->p && (p = find_prop(rel->p, PROP_COUNT)) != NULL)
     440        3041 :                 return p->value.lval;
     441       28048 :         else if(is_munion(rel->op)) {
     442         168 :                 lng cnt = 0;
     443         168 :                 list *l = rel->l;
     444         504 :                 for (node *n = l->h; n; n = n->next) {
     445         336 :                         lng lcnt = rel_get_count(n->data);
     446         336 :                         if (lcnt == BUN_MAX)
     447             :                                 return BUN_MAX;
     448         336 :                         cnt += lcnt;
     449             :                 }
     450         168 :                 return cnt;
     451       27880 :         } else if(rel->l) {
     452       25209 :                 if (is_select(rel->op) || is_project(rel->op))
     453             :                         return rel_get_count(rel->l);
     454             :         }
     455             :         return 0;
     456             : }
     457             : 
     458             : #define is_sum_aggr(f) (f->type == F_AGGR && strcmp(f->base.name, "sum") == 0)
     459             : 
     460             : list *
     461      738119 : check_arguments_and_find_largest_any_type(mvc *sql, sql_rel *rel, list *exps, sql_subfunc *sf, int maybe_zero_or_one, bool internal)
     462             : {
     463      738119 :         list *nexps = new_exp_list(sql->sa);
     464      738119 :         sql_subtype *atp = NULL, super, *res = !list_empty(sf->res) ? sf->res->h->data: NULL;
     465      738119 :         unsigned int digits = 0, scale = 0;
     466             : 
     467             :         /* find largest any type argument */
     468     2179093 :         for (node *n = exps->h, *m = sf->func->ops->h; n && m; n = n->next, m = m->next) {
     469     1440974 :                 sql_arg *a = m->data;
     470     1440974 :                 sql_exp *e = n->data;
     471     1440974 :                 sql_subtype *t = exp_subtype(e);
     472             : 
     473     1440974 :                 if (a->type.type->eclass == EC_ANY) {
     474      265526 :                         if (t && atp) {
     475      114501 :                                 supertype(&super, t, atp);
     476      114501 :                                 atp = &super;
     477      151025 :                         } else if (t) {
     478             :                                 atp = t;
     479             :                         }
     480             :                 }
     481     1440971 :                 if (t && sf->func->fix_scale == SCALE_FIX && t->type->eclass == EC_DEC) {
     482        1264 :                         if (digits < t->digits)
     483             :                                 digits = t->digits;
     484        1264 :                         if (scale < t->scale)
     485             :                                 scale = t->scale;
     486             :                 }
     487             :         }
     488      738119 :         if (!atp && !list_empty(exps))
     489      587095 :                 atp = exp_subtype(exps->h->data);
     490             : 
     491      738119 :         if ((atp && atp->type->localtype == TYPE_void) || !atp) /* NULL */
     492        9418 :                 atp = sql_bind_localtype("str");
     493             : 
     494      738119 :         node *n, *m;
     495     2179093 :         for (n = exps->h, m = sf->func->ops->h; n && m; n = n->next, m = m->next) {
     496     1440974 :                 sql_arg *a = m->data;
     497     1440974 :                 sql_exp *e = n->data;
     498     1440974 :                 sql_subtype *ntp = &a->type, *t = exp_subtype(e);
     499             : 
     500     1440974 :                 if (!t) {
     501          38 :                         if (a->type.type->eclass == EC_ANY && atp)
     502           3 :                                 ntp = sql_create_subtype(sql->sa, atp->type, atp->digits, atp->scale);
     503          61 :                         rel_set_type_param(sql, ntp, rel, e, sf->func->fix_scale != INOUT && !UDF_LANG(sf->func->lang));
     504     1440936 :                 } else if (a->type.type->eclass == EC_ANY && atp) {
     505      265523 :                         ntp = sql_create_subtype(sql->sa, atp->type, atp->digits, atp->scale);
     506     1175413 :                 } else if (t && ntp->digits == 0 && EC_VARCHAR(a->type.type->eclass)) {
     507      510941 :                         ntp = sql_create_subtype(sql->sa, a->type.type, type_digits_to_char_digits(t), 0);
     508      664472 :                 } else if (t && ntp->digits > 0 && a->type.type->eclass == EC_NUM && t->type->eclass == EC_NUM) {
     509      359483 :                         ntp = sql_create_subtype(sql->sa, a->type.type, t->digits, 0);
     510      304989 :                 } else if (t && ntp->scale == 0 && ntp->type->eclass == EC_DEC && EC_VARCHAR(t->type->eclass)) {
     511           0 :                         sql_subtype *res = SA_NEW(sql->sa, sql_subtype);
     512           0 :                         int digits = t->digits?t->digits+3:ntp->digits;
     513           0 :                         (void)sql_find_subtype(res, a->type.type->base.name, digits, 3);
     514           0 :                         ntp = res;
     515      304989 :                 } else if (t && ntp->scale == 0 && ntp->type->eclass == EC_DEC) {
     516        4869 :                         ntp = sql_create_subtype(sql->sa, a->type.type, t->type->eclass == EC_NUM?bits2digits(t->digits):t->digits, t->scale);
     517      300120 :                 } else if (t->type == ntp->type) {
     518      298021 :                         ntp = t;
     519             :                 }
     520     1440974 :                 if (!(e = exp_check_type(sql, ntp, rel, e, type_equal)))
     521             :                         return NULL;
     522     1440974 :                 if (sf->func->fix_scale == SCALE_FIX) {
     523      523405 :                         ntp = sql_create_subtype(sql->sa, a->type.type->localtype?a->type.type:t?t->type:atp->type, digits, scale);
     524      523405 :                         e = exp_fix_scale(sql, ntp, e);
     525      917569 :                 } else if (sf->func->fix_scale == SCALE_EQ) {
     526      171428 :                         e = exp_fix_scale(sql, &a->type, e);
     527             :                 }
     528     1440974 :                 if (maybe_zero_or_one && e->card > CARD_ATOM) {
     529          22 :                         sql_subfunc *zero_or_one = sql_bind_func(sql, "sys", "zero_or_one", exp_subtype(e), NULL, F_AGGR, true, false);
     530          22 :                         e = exp_aggr1(sql->sa, e, zero_or_one, 0, 0, CARD_ATOM, has_nil(e));
     531             :                 }
     532     1440974 :                 append(nexps, e);
     533             :         }
     534             :         /* handle any extra arguments for rel_dump/analytic funcs */
     535      738338 :         for ( ; n; n = n->next)
     536         219 :                 append(nexps, n->data);
     537      738119 :         if (sf->func->fix_scale == SCALE_FIX || IS_ANALYTIC(sf->func)) {
     538      270532 :                 exps_scale_fix(sf, nexps, atp);
     539      467587 :         } else if (sf->func->fix_scale == MAX_BITS) {
     540      111708 :                 exps_max_bits(sf, nexps);
     541      355879 :         } else if (sf->func->fix_scale == SCALE_MUL) {
     542       25185 :                 exps_sum_scales(sf, nexps);
     543      330694 :         } else if (!internal && sf->func->fix_scale == SCALE_DIV) {
     544        2604 :                 if (!exps_scale_algebra(sql, sf, rel, nexps))
     545             :                         return NULL;
     546      328090 :         } else if (sf->func->fix_scale == DIGITS_ADD) {
     547      181313 :                 exps_digits_add(sf, nexps);
     548      146777 :         } else if (sf->func->fix_scale == INOUT) {
     549       19015 :                 exps_inout(sf, nexps);
     550      127762 :         } else if (is_sum_aggr(sf->func))
     551        9313 :                 exps_largest_int(sf, nexps, rel_get_count(rel));
     552             : 
     553             :         /* dirty hack */
     554      738117 :         if (sf->func->type != F_PROC && sf->func->type != F_UNION && sf->func->type != F_LOADER && res) {
     555      733316 :                 if (res->type->eclass == EC_ANY && atp)
     556          73 :                         sf->res->h->data = sql_create_subtype(sql->sa, atp->type, atp->digits, atp->scale);
     557             :         }
     558             :         return nexps;
     559             : }
     560             : 
     561             : static char *
     562          32 : nary_function_arg_types_2str(mvc *sql, list* types, int N)
     563             : {
     564          32 :         char *arg_list = NULL;
     565          32 :         int i = 0;
     566             : 
     567         116 :         for (node *n = types->h; n && i < N; n = n->next) {
     568          84 :                 sql_subtype *t = (sql_subtype *) n->data;
     569          84 :                 char *tpe = t ? sql_subtype_string(sql->ta, t) : "?";
     570             : 
     571          84 :                 if (arg_list) {
     572          52 :                         arg_list = sa_message(sql->ta, "%s, %s", arg_list, tpe);
     573             :                 } else {
     574             :                         arg_list = tpe;
     575             :                 }
     576          84 :                 i++;
     577             :         }
     578          32 :         return arg_list;
     579             : }
     580             : 
     581             : static char *
     582          35 : file_loader_add_table_column_types(mvc *sql, sql_subfunc *f, list *exps, list *res_exps, char *tname)
     583             : {
     584          35 :         sql_exp *file = exps->h->data;
     585          35 :         if (!exp_is_atom(file))
     586             :                 return "Filename missing";
     587             : 
     588          35 :         atom *a = file->l;
     589          35 :         if (a->data.vtype != TYPE_str || !a->data.val.sval)
     590             :                 return "Filename missing";
     591             : 
     592          35 :         char *filename = a->data.val.sval;
     593          35 :         if (strcmp(filename, "") == 0)
     594             :                 return "Filename missing";
     595             : 
     596          33 :         char *ext = strrchr(filename, '.'), *ep = ext;
     597             : 
     598          33 :         if (ext) {
     599          30 :                 ext = ext + 1;
     600          30 :                 ext = mkLower(sa_strdup(sql->sa, ext));
     601             :         }
     602          30 :         if (!ext)
     603           3 :                 return "Filename extension missing";
     604             : 
     605          30 :         file_loader_t *fl = fl_find(ext);
     606          30 :         if (!fl) {
     607             :                 /* maybe compressed */
     608          10 :                 char *p = ep - 1;
     609          66 :                 while (p > filename && *p != '.')
     610          56 :                         p--;
     611          10 :                 if (p != filename) {
     612           8 :                         ext = p + 1;
     613           8 :                         ext = sa_strdup(sql->sa, ext);
     614           8 :                         char *d = strchr(ext, '.');
     615           8 :                         assert(d);
     616           8 :                         *d = 0;
     617           8 :                         fl = fl_find(ext);
     618             :                 }
     619          10 :                 if (!fl) /* fallback */
     620           2 :                         fl = fl_find("csv");
     621           2 :                 if (!fl) /* not expected */
     622           0 :                         return sa_message(sql->ta, "Filename extension '%s' missing", ext?ext:"");
     623             :         }
     624          30 :         str err = fl->add_types(sql, f, filename, res_exps, tname);
     625          30 :         if (err)
     626             :                 return err;
     627          16 :         sql_subtype *st = sql_bind_localtype("str");
     628          16 :         sql_exp *ext_exp = exp_atom(sql->sa, atom_string(sql->sa, st, ext));
     629          16 :         if (!ext_exp)
     630             :                 return MAL_MALLOC_FAIL;
     631          16 :         append(exps, ext_exp);
     632          16 :         return NULL;
     633             : }
     634             : 
     635             : static sql_rel *
     636          35 : rel_file_loader(mvc *sql, list *exps, list *tl, char *tname)
     637             : {
     638          35 :         sql_subfunc *f = NULL;
     639          35 :         bool found = false;
     640             : 
     641          35 :         if ((f = bind_func_(sql, NULL, "file_loader", tl, F_UNION, true, &found, false))) {
     642          35 :                 list *nexps = exps;
     643          35 :                 if (list_empty(tl) || f->func->vararg || (nexps = check_arguments_and_find_largest_any_type(sql, NULL, exps, f, 1, false))) {
     644          35 :                         list *res_exps = sa_list(sql->sa);
     645          35 :                         if (list_length(exps) == 1 && f && f->func->varres && strlen(f->func->mod) == 0 && strlen(f->func->imp) == 0) {
     646          35 :                                 char *err = file_loader_add_table_column_types(sql, f, nexps, res_exps, tname);
     647          35 :                                 if (err)
     648          19 :                                         return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "SELECT: file_loader function failed '%s'", err);
     649             :                         }
     650          16 :                         sql_exp *e = exp_op(sql->sa, nexps, f);
     651          16 :                         sql_rel *rel = rel_table_func(sql->sa, NULL, e, res_exps, TABLE_PROD_FUNC);
     652          16 :                         if (rel)
     653          16 :                                 rel = rel_project(sql->sa, rel, exps_alias(sql, res_exps));
     654          16 :                         return rel;
     655             :                 }
     656             :         }
     657             :         return NULL;
     658             : }
     659             : 
     660             : sql_exp *
     661       10743 : find_table_function(mvc *sql, char *sname, char *fname, list *exps, list *tl, sql_ftype type)
     662             : {
     663       10743 :         bool found = false;
     664       10743 :         sql_subfunc *f = NULL;
     665             : 
     666       10743 :         assert(type == F_UNION || type == F_LOADER);
     667       10743 :         if ((f = bind_func_(sql, sname, fname, tl, type, false, &found, false))) {
     668       10723 :                 list *nexps = exps;
     669       10723 :                 if (list_empty(tl) || f->func->vararg || (nexps = check_arguments_and_find_largest_any_type(sql, NULL, exps, f, 1, false)))
     670       10723 :                         return exp_op(sql->sa, nexps, f);
     671           0 :                 found = false;
     672             :         }
     673          20 :         char *arg_list = list_length(tl) ? nary_function_arg_types_2str(sql, tl, list_length(tl)) : NULL;
     674          55 :         return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "SELECT: %s %s function %s%s%s'%s'(%s)",
     675          20 :                                          found ? "insufficient privileges for" : "no such", type == F_UNION ? "table returning" : "loader", sname ? "'":"", sname ? sname : "",
     676             :                                          sname ? "'.":"", fname, arg_list ? arg_list : "");
     677             : }
     678             : 
     679             : static sql_rel *
     680       10759 : rel_named_table_function(sql_query *query, sql_rel *rel, symbol *ast, int lateral, list *refs)
     681             : {
     682       10759 :         mvc *sql = query->sql;
     683       10759 :         list *exps = NULL, *tl;
     684       10759 :         node *m;
     685       10759 :         exp_kind ek = {type_value, card_relation, TRUE};
     686       10759 :         sql_rel *sq = NULL, *outer = NULL;
     687       10759 :         sql_exp *e = NULL;
     688       10759 :         sql_subfunc *sf = NULL;
     689       10759 :         symbol *sym = ast->data.lval->h->data.sym, *subquery = NULL;
     690       10759 :         dnode *l = sym->data.lval->h, *n;
     691       10759 :         char *tname = NULL;
     692       10759 :         char *fname = qname_schema_object(l->data.lval);
     693       10759 :         char *sname = qname_schema(l->data.lval);
     694             : 
     695       10759 :         tl = sa_list(sql->sa);
     696       10759 :         exps = sa_list(sql->sa);
     697       10759 :         if (l->next)
     698       10759 :                 l = l->next; /* skip distinct */
     699       10759 :         if (l->next) { /* table call with subquery */
     700        1034 :                 int is_value = 1;
     701        1034 :                 if (l->next->type == type_symbol || l->next->type == type_list) {
     702        1034 :                         exp_kind iek = {type_value, card_set, TRUE};
     703        1034 :                         int count = 0;
     704             : 
     705        1034 :                         if (l->next->type == type_symbol)
     706             :                                 n = l->next;
     707             :                         else
     708         673 :                                 n = l->next->data.lval->h;
     709             : 
     710        3667 :                         for (dnode *m = n; m; m = m->next) {
     711        2633 :                                 if (m->type == type_symbol && m->data.sym->token == SQL_SELECT)
     712        2633 :                                         subquery = m->data.sym;
     713        2633 :                                 count++;
     714             :                         }
     715        1034 :                         if (subquery && count > 1)
     716          17 :                                 return sql_error(sql, 02, SQLSTATE(42000) "SELECT: The input for the table returning function %s%s%s'%s' must be either a single sub query, or a list of values",
     717             :                                                                  sname ? "'":"", sname ? sname : "", sname ? "'.":"", fname);
     718             : 
     719        1029 :                         if (subquery) {
     720          40 :                                 if (!(sq = rel_subquery(query, subquery, ek)))
     721             :                                         return NULL;
     722             :                                 is_value = 0;
     723             :                         } else {
     724        3561 :                                 for ( ; n; n = n->next) {
     725        2578 :                                         sql_exp *e = rel_value_exp(query, &outer, n->data.sym, sql_sel | sql_from, iek);
     726             : 
     727        2578 :                                         if (!e)
     728             :                                                 return NULL;
     729        2572 :                                         append(exps, e);
     730        2572 :                                         is_value &= exp_is_atom(e);
     731             :                                 }
     732         983 :                                 if (!is_value || (lateral && outer))
     733          14 :                                         sq = rel_project(sql->sa, NULL, exps);
     734          14 :                                 if (lateral && outer) {
     735           0 :                                         sq = rel_crossproduct(sql->sa, sq, outer, op_join);
     736           0 :                                         set_dependent(sq);
     737           0 :                                         set_processed(sq);
     738             :                                 }
     739             :                         }
     740             :                 }
     741        1022 :                 if (!is_value && (!sq || (!lateral && outer)))
     742           0 :                         return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "SELECT: no such table returning function %s%s%s'%s'", sname ? "'":"", sname ? sname : "", sname ? "'.":"", fname);
     743        1022 :                 if (!is_value) {
     744          53 :                         if (list_length(exps))
     745          14 :                                 exps = sa_list(sql->sa);
     746         171 :                         for (node *en = sq->exps->h; en; en = en->next) {
     747         118 :                                 sql_exp *e = en->data;
     748             : 
     749         118 :                                 if (!e->alias.label)
     750          28 :                                         exp_label(sql->sa, e, ++sql->label);
     751         118 :                                 sql_exp *ne = exp_ref(sql, e);
     752             :                                 /* allow for table functions with table input */
     753         118 :                                 ne->card = CARD_ATOM;
     754         118 :                                 exp_setname(sql, ne, tname, exp_name(e));
     755         118 :                                 append(exps, ne);
     756         118 :                                 append(tl, exp_subtype(e));
     757             :                         }
     758             :                 } else {
     759        3516 :                         for (node *en = exps->h; en; en = en->next)
     760        2547 :                                 append(tl, exp_subtype(en->data));
     761             :                 }
     762             :         }
     763             : 
     764       10747 :         rel = NULL;
     765       10747 :         if (ast->data.lval->t->type == type_symbol && ast->data.lval->t->data.sym)
     766        1193 :                 tname = ast->data.lval->t->data.sym->data.lval->h->data.sval;
     767             :         else
     768        9554 :                 tname = make_label(sql->sa, ++sql->label);
     769             : 
     770       10747 :         if (!sname && strcmp(fname, "file_loader") == 0) {
     771          35 :                 rel = rel_file_loader(sql, exps, tl, tname);
     772          35 :                 if (!rel)
     773             :                         return NULL;
     774       20437 :         } else if (!(e = find_table_function(sql, sname, fname, list_empty(exps) ? NULL : exps, tl, F_UNION)))
     775             :                 return NULL;
     776             : 
     777       10693 :         if (!rel) {
     778       10693 :                 rel = sq;
     779             : 
     780             :                 /* column or table function */
     781       10693 :                 sf = e->f;
     782       10693 :                 if (e->type != e_func || sf->func->type != F_UNION)
     783           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "SELECT: '%s' does not return a table", exp_func_name(e));
     784             : 
     785       10693 :                 if (sq) {
     786         171 :                         for (node *n = sq->exps->h, *m = sf->func->ops->h ; n && m ; n = n->next, m = m->next) {
     787         118 :                                 sql_exp *e = (sql_exp*) n->data;
     788         118 :                                 sql_arg *a = (sql_arg*) m->data;
     789         118 :                                 if (!exp_subtype(e) && rel_set_type_param(sql, &(a->type), sq, e, 0) < 0)
     790             :                                         return NULL;
     791             :                         }
     792             :                 }
     793             : 
     794             :                 /* for each column add table.column name */
     795       10693 :                 exps = new_exp_list(sql->sa);
     796      105730 :                 for (m = sf->func->res->h; m; m = m->next) {
     797       95037 :                         sql_arg *a = m->data;
     798       95037 :                         sql_exp *e = exp_column(sql->sa, tname, a->name, &a->type, CARD_MULTI, 1, 0, 0);
     799       95037 :                         e->alias.label = -(sql->nid++);
     800             : 
     801       95037 :                         set_basecol(e);
     802       95037 :                         append(exps, e);
     803             :                 }
     804       21333 :                 rel = rel_table_func(sql->sa, rel, e, exps, (sq)?TABLE_FROM_RELATION:TABLE_PROD_FUNC);
     805             :         }
     806       10709 :         if (ast->data.lval->t->type == type_symbol && ast->data.lval->t->data.sym && ast->data.lval->t->data.sym->data.lval->h->next->data.lval) {
     807          27 :                 rel = rel_table_optname(sql, rel, ast->data.lval->t->data.sym, refs);
     808       10682 :         } else if (refs) { /* if this relation is under a FROM clause, check for duplicate names */
     809       10682 :                 if (list_find(refs, tname, (fcmp) &strcmp))
     810           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "SELECT: relation name \"%s\" specified more than once", tname);
     811       10682 :                 list_append(refs, tname);
     812             :         }
     813             :         return rel;
     814             : }
     815             : 
     816             : static sql_exp *
     817        1827 : rel_op_(mvc *sql, char *sname, char *fname, exp_kind ek)
     818             : {
     819        1827 :         bool found = false;
     820        1827 :         sql_subfunc *f = NULL;
     821        1827 :         sql_ftype type = (ek.card == card_loader)?F_LOADER:((ek.card == card_none)?F_PROC:
     822             :                    ((ek.card == card_relation)?F_UNION:F_FUNC));
     823             : 
     824        1827 :         if ((f = bind_func_(sql, sname, fname, NULL, type, false, &found, true))) {
     825        1811 :                 if (check_card(ek.card, f))
     826        1811 :                         return exp_op(sql->sa, NULL, f);
     827           0 :                 found = false;
     828             :         }
     829          32 :         return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "SELECT: %s operator %s%s%s'%s'()",
     830          16 :                                          found ? "insufficient privileges for" : "no such", sname ? "'":"", sname ? sname : "", sname ? "'.":"", fname);
     831             : }
     832             : 
     833             : static sql_exp*
     834        4108 : exp_tuples_set_supertype(mvc *sql, list *tuple_values, sql_exp *tuples)
     835             : {
     836        4108 :         assert(is_values(tuples));
     837        4108 :         list *vals = exp_get_values(tuples);
     838        4108 :         if (!vals || !vals->h)
     839             :                 return NULL;
     840             : 
     841        4108 :         int tuple_width = list_length(tuple_values), i;
     842        4108 :         sql_subtype *types = SA_NEW_ARRAY(sql->sa, sql_subtype, tuple_width);
     843        4108 :         bool *has_type = SA_NEW_ARRAY(sql->sa, bool, tuple_width);
     844        4108 :         node *n;
     845             : 
     846        4108 :         memset(has_type, 0, sizeof(bool)*tuple_width);
     847       13373 :         for(n = tuple_values->h, i = 0; n; n = n->next, i++) {
     848        9265 :                 sql_exp *e = n->data;
     849        9265 :                 if (exp_subtype(e)) {
     850        9263 :                         types[i] = *exp_subtype(e);
     851        9263 :                         has_type[i] = 1;
     852             :                 }
     853             :         }
     854             : 
     855        8218 :         for (node *m = vals->h; m; m = m->next) {
     856        4110 :                 sql_exp *tuple = m->data;
     857        4110 :                 if (is_values(tuple)) {
     858           3 :                         list *exps = tuple->f;
     859           9 :                         for(n = exps->h, i = 0; n; n = n->next, i++) {
     860           6 :                                 sql_subtype *tpe;
     861           6 :                                 sql_exp *e = n->data;
     862             : 
     863           6 :                                 if (has_type[i] && e->type == e_atom && !e->l && !e->r && !e->f && !e->tpe.type) {
     864           0 :                                         if (set_type_param(sql, types+i, e->flag) == 0)
     865           0 :                                                 e->tpe = types[i];
     866             :                                         else
     867             :                                                 return NULL;
     868             :                                 }
     869           6 :                                 tpe = exp_subtype(e);
     870           6 :                                 if (!tpe)
     871             :                                         return NULL;
     872           6 :                                 if (has_type[i] && tpe) {
     873           6 :                                         supertype(types+i, types+i, tpe);
     874             :                                 } else {
     875           0 :                                         has_type[i] = 1;
     876           0 :                                         types[i] = *tpe;
     877             :                                 }
     878             :                         }
     879             :                 } else {
     880        4107 :                         sql_rel *tuple_relation = exp_rel_get_rel(sql->sa, tuple);
     881             : 
     882       13370 :                         for(n = tuple_relation->exps->h, i = 0; n; n = n->next, i++) {
     883        9263 :                                 sql_subtype *tpe;
     884        9263 :                                 sql_exp *e = n->data;
     885             : 
     886        9263 :                                 if (has_type[i] && e->type == e_atom && !e->l && !e->r && !e->f && !e->tpe.type) {
     887           5 :                                         if (set_type_param(sql, types+i, e->flag) == 0)
     888           5 :                                                 e->tpe = types[i];
     889             :                                         else
     890             :                                                 return NULL;
     891             :                                 }
     892        9263 :                                 tpe = exp_subtype(e);
     893        9263 :                                 if (!tpe)
     894             :                                         return NULL;
     895        9263 :                                 if (has_type[i] && tpe) {
     896        9261 :                                         cmp_supertype(types+i, types+i, tpe);
     897             :                                 } else {
     898           2 :                                         has_type[i] = 1;
     899           2 :                                         types[i] = *tpe;
     900             :                                 }
     901             :                         }
     902             :                 }
     903             :         }
     904             : 
     905        8218 :         for (node *m = vals->h; m; m = m->next) {
     906        4110 :                 sql_exp *tuple = m->data;
     907        4110 :                 if (is_values(tuple)) {
     908           3 :                         list *exps = tuple->f;
     909           3 :                         list *nexps = sa_list(sql->sa);
     910           9 :                         for(n = exps->h, i = 0; n; n = n->next, i++) {
     911           6 :                                 sql_exp *e = n->data;
     912             : 
     913           6 :                                 e = exp_check_type(sql, types+i, NULL, e, type_equal);
     914           6 :                                 if (!e)
     915             :                                         return NULL;
     916           6 :                                 exp_label(sql->sa, e, ++sql->label);
     917           6 :                                 append(nexps, e);
     918             :                         }
     919           3 :                         tuple->f = nexps;
     920             :                 } else {
     921        4107 :                         sql_rel *tuple_relation = exp_rel_get_rel(sql->sa, tuple);
     922             : 
     923        4107 :                         list *nexps = sa_list(sql->sa);
     924       13370 :                         for(n = tuple_relation->exps->h, i = 0; n; n = n->next, i++) {
     925        9263 :                                 sql_exp *e = n->data;
     926             : 
     927        9263 :                                 e = exp_check_type(sql, types+i, NULL, e, type_equal);
     928        9263 :                                 if (!e)
     929             :                                         return NULL;
     930        9263 :                                 append(nexps, e);
     931             :                         }
     932        4107 :                         tuple_relation->exps = nexps;
     933             :                 }
     934             :         }
     935             :         return tuples;
     936             : }
     937             : 
     938             : static int
     939        9265 : rel_binop_check_types(mvc *sql, sql_rel *rel, sql_exp *ls, sql_exp *rs, int upcast)
     940             : {
     941        9265 :         sql_subtype *t1 = exp_subtype(ls), *t2 = exp_subtype(rs);
     942             : 
     943        9265 :         if (!t1 || !t2) {
     944           2 :                 if (t2 && !t1 && rel_set_type_param(sql, t2, rel, ls, upcast) < 0)
     945             :                         return -1;
     946           2 :                 if (t1 && !t2 && rel_set_type_param(sql, t1, rel, rs, upcast) < 0)
     947             :                         return -1;
     948             :         }
     949        9265 :         if (!exp_subtype(ls) && !exp_subtype(rs)) {
     950           0 :                 (void) sql_error(sql, 01, SQLSTATE(42000) "Cannot have a parameter (?) on both sides of an expression");
     951           0 :                 return -1;
     952             :         }
     953             :         return 0;
     954             : }
     955             : 
     956             : static list *
     957        4108 : tuples_check_types(mvc *sql, list *tuple_values, sql_exp *tuples)
     958             : {
     959        4108 :         list *tuples_list = exp_get_values(tuples);
     960        4108 :         sql_exp *first_tuple = tuples_list->h->data;
     961        4108 :         list *nvalues = sa_list(sql->sa);
     962        4108 :         if (is_values(first_tuple)) {
     963           1 :                 list *vals = first_tuple->f;
     964             : 
     965           3 :                 for (node *n = tuple_values->h, *m = vals->h; n && m; n = n->next, m = m->next) {
     966           2 :                         sql_exp *le = n->data, *re = m->data;
     967             : 
     968           2 :                         if (rel_binop_check_types(sql, NULL, le, re, 0) < 0)
     969             :                                 return NULL;
     970           2 :                         if ((le = exp_check_type(sql, exp_subtype(re), NULL, le, type_equal)) == NULL)
     971             :                                 return NULL;
     972           2 :                         append(nvalues, le);
     973             :                 }
     974             :                 return nvalues;
     975             :         } else {
     976        4107 :                 sql_rel *tuple_relation = exp_rel_get_rel(sql->sa, first_tuple);
     977             : 
     978        4107 :                 assert(list_length(tuple_values) == list_length(tuple_relation->exps));
     979       13370 :                 for (node *n = tuple_values->h, *m = tuple_relation->exps->h; n && m; n = n->next, m = m->next) {
     980        9263 :                         sql_exp *le = n->data, *re = m->data;
     981             : 
     982        9263 :                         if (rel_binop_check_types(sql, NULL, le, re, 0) < 0)
     983             :                                 return NULL;
     984        9263 :                         if ((le = exp_check_type(sql, exp_subtype(re), NULL, le, type_equal)) == NULL)
     985             :                                 return NULL;
     986        9263 :                         append(nvalues, le);
     987             :                 }
     988             :                 return nvalues;
     989             :         }
     990             :         return tuple_values;
     991             : }
     992             : 
     993             : static sql_rel *
     994        3756 : rel_values(sql_query *query, symbol *tableref, list *refs)
     995             : {
     996        3756 :         mvc *sql = query->sql;
     997        3756 :         sql_rel *r = NULL;
     998        3756 :         dlist *rowlist = tableref->data.lval->h->data.lval;
     999        3756 :         symbol *optname = tableref->data.lval->t->type == type_symbol ? tableref->data.lval->t->data.sym : NULL;
    1000        3756 :         node *m;
    1001        3756 :         list *exps = sa_list(sql->sa);
    1002        3756 :         exp_kind ek = {type_value, card_value, TRUE};
    1003             : 
    1004       15034 :         for (dnode *o = rowlist->h; o; o = o->next) {
    1005       11288 :                 dlist *values = o->data.lval;
    1006             : 
    1007       11288 :                 if (!list_empty(exps) && list_length(exps) != dlist_length(values)) {
    1008           3 :                         return sql_error(sql, 02, SQLSTATE(42000) "VALUES: number of columns doesn't match between rows");
    1009             :                 } else {
    1010       11285 :                         dnode *n;
    1011             : 
    1012       11285 :                         if (list_empty(exps)) {
    1013       10720 :                                 for (n = values->h; n; n = n->next) {
    1014        6964 :                                         sql_exp *vals = exp_values(sql->sa, sa_list(sql->sa));
    1015             : 
    1016        6964 :                                         exp_label(sql->sa, vals, ++sql->label);
    1017        6964 :                                         list_append(exps, vals);
    1018             :                                 }
    1019             :                         }
    1020       34528 :                         for (n = values->h, m = exps->h; n && m; n = n->next, m = m->next) {
    1021       23250 :                                 sql_exp *vals = m->data;
    1022       23250 :                                 list *vals_list = vals->f;
    1023       23250 :                                 sql_exp *e = rel_value_exp(query, NULL, n->data.sym, sql_sel | sql_values, ek);
    1024       23250 :                                 if (!e)
    1025             :                                         return NULL;
    1026       23243 :                                 list_append(vals_list, e);
    1027             :                         }
    1028             :                 }
    1029             :         }
    1030             : 
    1031             :         /* loop to check types and cardinality */
    1032        3746 :         unsigned int card = exps->h && list_length(((sql_exp*)exps->h->data)->f) > 1 ? CARD_MULTI : CARD_ATOM;
    1033       10693 :         for (m = exps->h; m; m = m->next) {
    1034        6950 :                 sql_exp *e = m->data;
    1035             : 
    1036        6950 :                 if (!(e = exp_values_set_supertype(sql, e, NULL)))
    1037             :                         return NULL;
    1038        6947 :                 e->card = card;
    1039        6947 :                 m->data = e;
    1040             :         }
    1041             : 
    1042        3743 :         r = rel_project(sql->sa, NULL, exps);
    1043        3743 :         r->nrcols = list_length(exps);
    1044        3743 :         r->card = card;
    1045        3743 :         return rel_table_optname(sql, r, optname, refs);
    1046             : }
    1047             : 
    1048             : static int
    1049      497595 : check_is_lateral(symbol *tableref)
    1050             : {
    1051      497595 :         if (tableref->token == SQL_NAME || tableref->token == SQL_TABLE ||
    1052             :                 tableref->token == SQL_VALUES) {
    1053      446190 :                 if (dlist_length(tableref->data.lval) == 3)
    1054      445357 :                         return tableref->data.lval->h->next->data.i_val;
    1055             :                 return 0;
    1056             :         } else if (tableref->token == SQL_WITH) {
    1057         941 :                 if (dlist_length(tableref->data.lval) == 4)
    1058           5 :                         return tableref->data.lval->h->next->next->data.i_val;
    1059             :                 return 0;
    1060             :         } else if (tableref->token == SQL_SELECT) {
    1061        3612 :                 SelectNode *sn = (SelectNode *) tableref;
    1062        3612 :                 return sn->lateral;
    1063             :         } else if (tableref->token == SQL_EXCEPT || tableref->token == SQL_INTERSECT ||
    1064             :                            tableref->token == SQL_UNION) {
    1065       10678 :                 if (dlist_length(tableref->data.lval) == 6)
    1066        9016 :                         return tableref->data.lval->h->next->next->next->next->data.i_val;
    1067             :                 return 0;
    1068             :         } else {
    1069             :                 return 0;
    1070             :         }
    1071             : }
    1072             : 
    1073             : static sql_rel *
    1074          25 : rel_reduce_on_column_privileges(mvc *sql, sql_rel *rel, sql_table *t)
    1075             : {
    1076          25 :         list *exps = sa_list(sql->sa);
    1077             : 
    1078          99 :         for (node *n = rel->exps->h, *m = ol_first_node(t->columns); n && m; n = n->next, m = m->next) {
    1079          74 :                 sql_exp *e = n->data;
    1080          74 :                 sql_column *c = m->data;
    1081             : 
    1082          74 :                 if (column_privs(sql, c, PRIV_SELECT))
    1083          12 :                         append(exps, e);
    1084             :         }
    1085          25 :         if (!list_empty(exps)) {
    1086           9 :                 rel->exps = exps;
    1087           9 :                 return rel;
    1088             :         }
    1089             :         return NULL;
    1090             : }
    1091             : 
    1092             : sql_rel *
    1093      678562 : table_ref(sql_query *query, symbol *tableref, int lateral, list *refs)
    1094             : {
    1095      678562 :         mvc *sql = query->sql;
    1096      678562 :         char *tname = NULL;
    1097      678562 :         sql_table *t = NULL;
    1098      678562 :         sql_rel *res = NULL;
    1099             : 
    1100      678562 :         if (tableref->token == SQL_NAME) {
    1101      467608 :                 dlist *name = tableref->data.lval->h->data.lval;
    1102      467608 :                 sql_rel *temp_table = NULL;
    1103      467608 :                 char *sname = qname_schema(name);
    1104      467608 :                 int allowed = 1;
    1105             : 
    1106      467608 :                 tname = qname_schema_object(name);
    1107             : 
    1108      467608 :                 if (dlist_length(name) > 2)
    1109           4 :                         return sql_error(sql, 02, SQLSTATE(3F000) "SELECT: only a schema and table name expected");
    1110             : 
    1111      467604 :                 if (!sname)
    1112      113465 :                         temp_table = stack_find_rel_view(sql, tname);
    1113      113465 :                 if (!temp_table)
    1114      450772 :                         t = find_table_or_view_on_scope(sql, NULL, sname, tname, "SELECT", false);
    1115      467604 :                 if (!t && !temp_table)
    1116             :                         return NULL;
    1117      467505 :                 if (!temp_table && !table_privs(sql, t, PRIV_SELECT))
    1118      467505 :                         allowed = 0;
    1119             : 
    1120      467505 :                 if (tableref->data.lval->t->type == type_symbol && tableref->data.lval->t->data.sym) /* AS */
    1121      273403 :                         tname = tableref->data.lval->t->data.sym->data.lval->h->data.sval;
    1122      467505 :                 if (temp_table && !t) {
    1123       16832 :                         node *n;
    1124       16832 :                         int needed = !is_simple_project(temp_table->op);
    1125             : 
    1126       16832 :                         if (is_basetable(temp_table->op) && !temp_table->exps) {
    1127           0 :                                 if (strcmp(rel_base_name(temp_table), tname) != 0)
    1128           0 :                                         rel_base_rename(temp_table, tname);
    1129             :                         } else {
    1130       60214 :                                 for (n = temp_table->exps->h; n && !needed; n = n->next) {
    1131       43382 :                                         sql_exp *e = n->data;
    1132             : 
    1133       43382 :                                         if (!exp_relname(e) || strcmp(exp_relname(e), tname) != 0)
    1134             :                                                 needed = 1;
    1135             :                                 }
    1136             : 
    1137       16832 :                                 if (needed) {
    1138        6817 :                                         list *exps = rel_projections(sql, temp_table, NULL, 1, 1);
    1139             : 
    1140        6817 :                                         temp_table = rel_project(sql->sa, temp_table, exps);
    1141       24458 :                                         for (n = exps->h; n; n = n->next) {
    1142       17641 :                                                 sql_exp *e = n->data;
    1143             : 
    1144       17641 :                                                 noninternexp_setname(sql, e, tname, NULL);
    1145       17641 :                                                 set_basecol(e);
    1146             :                                         }
    1147        6817 :                                         list_hash_clear(exps);
    1148             :                                 }
    1149             :                         }
    1150       16832 :                         if (temp_table && tableref->data.lval->t->type == type_symbol && tableref->data.lval->t->data.sym && tableref->data.lval->t->data.sym->data.lval->h->next->data.lval) /* AS with column aliases */
    1151           1 :                                 temp_table = rel_table_optname(sql, temp_table, tableref->data.lval->t->data.sym, refs);
    1152       16832 :                         if (allowed)
    1153             :                                 return temp_table;
    1154           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "SELECT: access denied for %s to table '%s'", get_string_global_var(sql, "current_user"), tname);
    1155      450673 :                 } else if (isView(t)) {
    1156             :                         /* instantiate base view */
    1157       69528 :                         node *n,*m;
    1158       69528 :                         sql_rel *rel;
    1159             : 
    1160       69528 :                         if (sql->emode == m_deps) {
    1161       24382 :                                 rel = rel_basetable(sql, t, tname);
    1162       24382 :                                 if (!allowed)
    1163           0 :                                         rel_base_disallow(rel);
    1164             :                         } else {
    1165             :                                 /* when recreating a view, the view itself can't be found */
    1166       45146 :                                 if (sql->objid && sql->objid == t->base.id)
    1167           1 :                                         return sql_error(sql, 02, SQLSTATE(42000) "SELECT: attempting to recursively bind view '%s'.'%s'", t->s->base.name, tname);
    1168       45145 :                                 rel = rel_parse(sql, t->s, t->query, m_instantiate);
    1169       45145 :                                 if (rel && sql->emode == m_deps)
    1170           0 :                                         rel = rel_unnest(sql, rel);
    1171             :                         }
    1172             : 
    1173       69525 :                         if (!rel)
    1174           2 :                                 return NULL;
    1175             :                         /* Rename columns of the rel_parse relation */
    1176       69525 :                         if (sql->emode != m_deps) {
    1177       45143 :                                 assert(is_project(rel->op));
    1178       45143 :                                 set_processed(rel);
    1179       45143 :                                 if (is_mset(rel->op) || is_simple_project(rel->op) || (is_groupby(rel->op) && !list_empty(rel->r))) {
    1180             :                                         /* it's unsafe to set the projection names because of possible dependent sorting/grouping columns */
    1181       45143 :                                         rel = rel_project(sql->sa, rel, rel_projections(sql, rel, NULL, 0, 0));
    1182       45143 :                                         set_processed(rel);
    1183             :                                 }
    1184      407916 :                                 for (n = ol_first_node(t->columns), m = rel->exps->h; n && m; n = n->next, m = m->next) {
    1185      362773 :                                         sql_column *c = n->data;
    1186      362773 :                                         sql_exp *e = m->data;
    1187             : 
    1188      362773 :                                         exp_setname(sql, e, tname, c->base.name);
    1189      362773 :                                         set_basecol(e);
    1190             :                                 }
    1191       45143 :                                 list_hash_clear(rel->exps);
    1192             :                         }
    1193       69525 :                         if (rel && !allowed && t->query && (rel = rel_reduce_on_column_privileges(sql, rel, t)) == NULL)
    1194          16 :                                 return sql_error(sql, 02, SQLSTATE(42000) "SELECT: access denied for %s to view '%s.%s'", get_string_global_var(sql, "current_user"), t->s->base.name, tname);
    1195       69509 :                         return rel;
    1196             :                 }
    1197      381145 :                 if ((isMergeTable(t) || isReplicaTable(t)) && list_length(t->members)==0)
    1198          14 :                         return sql_error(sql, 02, SQLSTATE(42000) "%s '%s'.'%s' should have at least one table associated",
    1199          14 :                                                         TABLE_TYPE_DESCRIPTION(t->type, t->properties), t->s->base.name, tname);
    1200      381131 :                 res = rel_basetable(sql, t, tname);
    1201      381131 :                 if (!allowed) {
    1202          53 :                         rel_base_disallow(res);
    1203          53 :                         if (rel_base_has_column_privileges(sql, res) == 0)
    1204          62 :                                 return sql_error(sql, 02, SQLSTATE(42000) "SELECT: access denied for %s to %s '%s.%s'", get_string_global_var(sql, "current_user"), isView(t) ? "view" : "table", t->s->base.name, tname);
    1205             :                 }
    1206      381100 :                 if (tableref->data.lval->t->type == type_symbol && tableref->data.lval->t->data.sym && tableref->data.lval->t->data.sym->data.lval->h->next->data.lval) { /* AS with column aliases */
    1207           8 :                         res = rel_table_optname(sql, res, tableref->data.lval->t->data.sym, refs);
    1208      381092 :                 } else if (refs) { /* if this relation is under a FROM clause, check for duplicate names */
    1209      381087 :                         if (list_find(refs, tname, (fcmp) &strcmp))
    1210           7 :                                 return sql_error(sql, 02, SQLSTATE(42000) "SELECT: relation name \"%s\" specified more than once", tname);
    1211      381080 :                         assert(tname);
    1212      381080 :                         list_append(refs, tname);
    1213             :                 }
    1214      381093 :                 return res;
    1215             :         } else if (tableref->token == SQL_VALUES) {
    1216        1361 :                 return rel_values(query, tableref, refs);
    1217             :         } else if (tableref->token == SQL_TABLE) {
    1218       10759 :                 return rel_named_table_function(query, NULL, tableref, lateral, refs);
    1219             :         } else if (tableref->token == SQL_SELECT) {
    1220       97575 :                 return rel_subquery_optname(query, tableref, refs);
    1221             :         } else if (tableref->token == SQL_UNION || tableref->token == SQL_EXCEPT || tableref->token == SQL_INTERSECT) {
    1222             :                 /* subqueries will be called, ie no need to test for duplicate references */
    1223       41598 :                 sql_rel *tq = rel_setquery(query, tableref);
    1224             : 
    1225       41598 :                 if (!tq)
    1226             :                         return NULL;
    1227             :                 /* look for lateral joins */
    1228       41595 :                 symbol *optname = tableref->data.lval->t->type == type_symbol ? tableref->data.lval->t->data.sym : NULL;
    1229       41595 :                 return rel_table_optname(sql, tq, optname, refs);
    1230             :         } else {
    1231       59661 :                 return query_exp_optname(query, tableref, refs);
    1232             :         }
    1233             : }
    1234             : 
    1235             : static sql_exp *
    1236      120838 : rel_exp_variable_on_scope(mvc *sql, const char *sname, const char *vname)
    1237             : {
    1238      120838 :         sql_subtype *tpe = NULL;
    1239      120838 :         sql_var *var = NULL;
    1240      120838 :         sql_arg *a = NULL;
    1241      120838 :         int level = 1;
    1242             : 
    1243      120838 :         if (find_variable_on_scope(sql, sname, vname, &var, &a, &tpe, &level, "SELECT")) {
    1244      119930 :                 if (var) /* if variable is known from the stack or a global var */
    1245       37360 :                         return exp_param_or_declared(sql->sa, var->sname ? sa_strdup(sql->sa, var->sname) : NULL, sa_strdup(sql->sa, var->name), &(var->var.tpe), level);
    1246       87550 :                 if (a) /* if variable is a parameter */
    1247       87550 :                         return exp_param_or_declared(sql->sa, NULL, sa_strdup(sql->sa, vname), &(a->type), level);
    1248             :         }
    1249             :         return NULL;
    1250             : }
    1251             : 
    1252             : static sql_exp *
    1253        9247 : exps_get_exp(list *exps, int nth)
    1254             : {
    1255        9247 :         node *n = NULL;
    1256        9247 :         int i = 0;
    1257             : 
    1258        9247 :         if (exps)
    1259       27170 :                 for (n=exps->h, i=1; n && i<nth; n=n->next, i++)
    1260             :                         ;
    1261        9247 :         if (n && i == nth)
    1262        9244 :                 return n->data;
    1263             :         return NULL;
    1264             : }
    1265             : 
    1266             : static sql_rel *
    1267       27680 : rel_find_groupby(sql_rel *groupby)
    1268             : {
    1269       27680 :         if (groupby && !is_processed(groupby) && !is_base(groupby->op)) {
    1270       37279 :                 while(!is_processed(groupby) && !is_base(groupby->op)) {
    1271       29481 :                         if (is_groupby(groupby->op) || !groupby->l)
    1272             :                                 break;
    1273             :                         if (groupby->l)
    1274             :                                 groupby = groupby->l;
    1275             :                 }
    1276       27556 :                 if (groupby && is_groupby(groupby->op))
    1277       19758 :                         return groupby;
    1278             :         }
    1279             :         return NULL;
    1280             : }
    1281             : 
    1282             : static int
    1283         306 : is_groupby_col(sql_rel *gb, sql_exp *e)
    1284             : {
    1285         306 :         gb = rel_find_groupby(gb);
    1286             : 
    1287         306 :         if (gb) {
    1288         174 :                 if (exp_relname(e)) {
    1289         163 :                         if (exp_name(e) && exps_bind_column2(gb->r, exp_relname(e), exp_name(e), NULL))
    1290             :                                 return 1;
    1291             :                 } else {
    1292          11 :                         if (exp_name(e) && exps_bind_column(gb->r, exp_name(e), NULL, NULL, 1))
    1293             :                                 return 1;
    1294             :                 }
    1295             :         }
    1296             :         return 0;
    1297             : }
    1298             : 
    1299             : static void
    1300        4726 : set_dependent_( sql_rel *r)
    1301             : {
    1302        4726 :         if (is_select(r->op))
    1303        3586 :                 r = r->l;
    1304        4726 :         if (r && is_join(r->op))
    1305        1194 :                 set_dependent(r);
    1306        4726 : }
    1307             : 
    1308             : static
    1309          15 : sql_rel* find_union(visitor *v, sql_rel *rel) {
    1310          15 :         if (rel->op == op_union || rel->op == op_munion)
    1311           1 :                 v->data = rel;
    1312          15 :         return rel;
    1313             : }
    1314             : 
    1315             : static inline
    1316          54 : bool group_by_pk_project_uk_cond(mvc* sql, sql_rel* inner, sql_exp* exp,const char* sname, const char* tname) {
    1317          54 :         sql_table* t = find_table_or_view_on_scope(sql, NULL, sname, tname, "SELECT", false);
    1318          54 :         bool allow = false;
    1319          54 :         if (t) {
    1320          17 :                 sql_idx* pki = NULL;
    1321          17 :                 list *ukil = sa_list(sql->sa);
    1322             : 
    1323          23 :                 for (node * n = ol_first_node(t->idxs); n; n = n->next) {
    1324           6 :                         sql_idx *i = n->data;
    1325           6 :                         if (!i->key)
    1326           1 :                                 continue;
    1327           5 :                         switch (i->key->type) {
    1328           3 :                         case pkey:
    1329           3 :                                 pki = i;
    1330           3 :                                 continue;
    1331           2 :                         case ukey:
    1332             :                         case unndkey:
    1333           2 :                                 list_append(ukil, i);
    1334           2 :                                 continue;
    1335           0 :                         default:
    1336           0 :                                 continue;
    1337             :                         }
    1338             :                 }
    1339          17 :                 if (pki && pki->columns->cnt == 1 && inner->r && ((list*) inner->r)->cnt == 1) {
    1340             :                         /* for now only check simple case where primary key and group by expression is a single column*/
    1341           3 :                         sql_exp* gbe = ((list*) inner->r)->h->data;
    1342           3 :                         sql_column* pkc = ((sql_kc *)pki->columns->h->data)->c;
    1343           3 :                         if (gbe->type == e_column && strcmp(gbe->alias.name, pkc->base.name) == 0) {
    1344           2 :                                 node *n;
    1345           2 :                                 for (n = ukil->h; n; n = n->next){
    1346           2 :                                         sql_idx* uki = n->data;
    1347           2 :                                         if (uki->columns->cnt == 1) {
    1348             :                                                 /* for now only check simple case where unique key is a single column*/
    1349           2 :                                                 sql_column* ukc = ((sql_kc *)uki->columns->h->data)->c;
    1350           2 :                                                 if (strcmp(exp->alias.name, ukc->base.name) == 0) {
    1351             :                                                         allow = true;
    1352             :                                                         break;
    1353             :                                                 }
    1354             :                                         }
    1355             :                                 }
    1356             :                         }
    1357             :                 }
    1358             : 
    1359           2 :                 if (allow) {
    1360             :                         /* sufficiency condition: abort if relation contains union subrelation
    1361             :                         * because it may break functional dependency between pk and uk */
    1362           2 :                         visitor v = {.sql=sql};
    1363           2 :                         rel_visitor_topdown(&v, inner, &find_union);
    1364           2 :                         if (v.data)
    1365           1 :                                 allow = false;
    1366             :                 }
    1367             :         }
    1368             : 
    1369          54 :         return allow;
    1370             : 
    1371             : }
    1372             : 
    1373             : static sql_exp *
    1374     2339912 : rel_column_ref(sql_query *query, sql_rel **rel, symbol *column_r, int f)
    1375             : {
    1376     2339912 :         mvc *sql = query->sql;
    1377     2339912 :         sql_exp *exp = NULL;
    1378     2339912 :         dlist *l = NULL;
    1379     2339912 :         sql_rel *inner = rel?*rel:NULL, *outer = NULL;
    1380     2339912 :         int used_lower_after_processed = 0;
    1381             : 
    1382     2339912 :         assert((column_r->token == SQL_COLUMN || column_r->token == SQL_IDENT) && column_r->type == type_list);
    1383     2339912 :         l = column_r->data.lval;
    1384             : 
    1385     2339912 :         if (dlist_length(l) == 1) {
    1386     1105609 :                 const char *name = l->h->data.sval;
    1387             : 
    1388     1105609 :                 if (!exp && inner)
    1389     1011260 :                         if (!(exp = rel_bind_column(sql, inner, name, f, 0)) && sql->session->status == -ERR_AMBIGUOUS)
    1390             :                                 return NULL;
    1391     1105595 :                 if (!exp && inner && ((is_sql_aggr(f) && (is_groupby(inner->op) || is_select(inner->op))) ||
    1392       23872 :                                                      (is_groupby(inner->op) && inner->flag))) {
    1393             :                         /* if inner is selection, ie having clause, get the left relation to reach group by */
    1394             :                         sql_rel *gp = inner;
    1395         258 :                         while (gp && is_select(gp->op))
    1396          27 :                                 gp = gp->l;
    1397         231 :                         if (gp && !is_basetable(gp->op) && gp->l && !(exp = rel_bind_column(sql, gp->l, name, f, 0)) && sql->session->status == -ERR_AMBIGUOUS)
    1398             :                                 return NULL;
    1399             :                 }
    1400     1105595 :                 if (!exp && query && query_has_outer(query)) {
    1401         448 :                         int i;
    1402             : 
    1403         501 :                         for (i=query_has_outer(query)-1; i>= 0 && !exp && (outer = query_fetch_outer(query,i)); i--) {
    1404         472 :                                 if (!(exp = rel_bind_column(sql, outer, name, f, 0)) && sql->session->status == -ERR_AMBIGUOUS)
    1405             :                                         return NULL;
    1406          84 :                                 if (!exp && is_groupby(outer->op)) {
    1407          31 :                                         if (!(exp = rel_bind_column(sql, outer->l, name, f, 0)) && sql->session->status == -ERR_AMBIGUOUS)
    1408             :                                                 return NULL;
    1409             :                                         else
    1410          31 :                                                 used_lower_after_processed = is_processed(outer);
    1411             :                                 }
    1412         472 :                                 if (exp && is_simple_project(outer->op) && !rel_find_exp(outer, exp))
    1413           0 :                                         exp = rel_project_add_exp(sql, outer, exp);
    1414         419 :                                 if (exp)
    1415             :                                         break;
    1416             :                         }
    1417         448 :                         if (exp && exp->card != CARD_AGGR && is_groupby(outer->op) && !is_sql_aggr(f) && rel_find_exp(outer->l, exp))
    1418           5 :                                 return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column '%s' in query results without an aggregate function", name);
    1419         443 :                         if (exp && outer && outer->card <= CARD_AGGR && exp->card > CARD_AGGR && !is_sql_aggr(f))
    1420           0 :                                 return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column '%s' in query results without an aggregate function", name);
    1421         443 :                         if (exp && outer && !is_sql_aggr(f) && !is_sql_aggr(query_fetch_outer_state(query, i))) {
    1422         275 :                                 if (used_lower_after_processed || query_outer_used_exp( query, i, exp, f)) {
    1423           0 :                                         sql_exp *lu = used_lower_after_processed?exp:query_outer_last_used(query, i);
    1424           0 :                                         if (exp_name(lu) && exp_relname(lu) && !has_label(lu))
    1425           0 :                                                 return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: subquery uses ungrouped column \"%s.%s\" from outer query", exp_relname(lu), exp_name(lu));
    1426           0 :                                         return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: subquery uses ungrouped column from outer query");
    1427             :                                 }
    1428             :                         }
    1429         443 :                         if (exp) {
    1430         414 :                                 int of = query_fetch_outer_state(query, i);
    1431         414 :                                 if (is_groupby(outer->op) && !is_sql_aggr(f)) {
    1432          37 :                                         exp = rel_groupby_add_aggr(sql, outer, exp);
    1433          37 :                                         exp->card = CARD_ATOM;
    1434         377 :                                 } else if (is_groupby(outer->op) && is_sql_aggr(f) && exps_any_match(outer->exps, exp))
    1435          57 :                                         exp = exp_ref(sql, exp);
    1436             :                                 else
    1437         320 :                                         exp->card = CARD_ATOM;
    1438         414 :                                 set_freevar(exp, i);
    1439             :                                 /*
    1440             :                                 if (exp->alias.label == exp->nid)
    1441             :                                         exp->alias.label = -(sql->nid++);
    1442             :                                         */
    1443         414 :                                 if (!is_sql_where(of) && !is_sql_aggr(of) && !is_sql_aggr(f) && !outer->grouped)
    1444         125 :                                         set_outer(outer);
    1445             :                         }
    1446         443 :                         if (exp && outer && (is_select(outer->op) || is_join(outer->op)))
    1447          31 :                                 set_dependent_(outer);
    1448             :                 }
    1449             : 
    1450             :                 /* some views are just in the stack, like before and after updates views */
    1451     1105590 :                 if (rel && sql->use_views) {
    1452          13 :                         sql_rel *v = NULL;
    1453          13 :                         int dup = stack_find_rel_view_projection_columns(sql, name, &v); /* trigger views are basetables relations, so those may conflict */
    1454             : 
    1455          13 :                         if (dup < 0 || (v && exp && *rel && is_base(v->op) && v != *rel)) /* comparing pointers, ugh */
    1456           2 :                                 return sql_error(sql, ERR_AMBIGUOUS, SQLSTATE(42000) "SELECT: identifier '%s' ambiguous", name);
    1457          11 :                         if (v && !exp) {
    1458           4 :                                 if (*rel)
    1459           1 :                                         *rel = rel_crossproduct(sql->sa, *rel, rel_dup(v), op_join);
    1460             :                                 else
    1461           3 :                                         *rel = rel_dup(v);
    1462           4 :                                 exp = rel_bind_column(sql, *rel, name, f, 0);
    1463             :                         }
    1464             :                 }
    1465     1105588 :                 if (!exp) /* If no column was found, try a variable or parameter */
    1466      117831 :                         exp = rel_exp_variable_on_scope(sql, NULL, name);
    1467             : 
    1468      117831 :                 if (!exp) {
    1469         908 :                         if (inner && !is_sql_aggr(f) && is_groupby(inner->op) && inner->l && (exp = rel_bind_column(sql, inner->l, name, f, 0)))
    1470          46 :                                 return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column '%s' in query results without an aggregate function", name);
    1471             :                 }
    1472             : 
    1473     1104680 :                 if (!exp)
    1474         862 :                         return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "SELECT: identifier '%s' unknown", name);
    1475     1104680 :                 if (exp && inner && inner->card <= CARD_AGGR && exp->card > CARD_AGGR && (is_sql_sel(f) || is_sql_having(f)) && (!is_sql_aggr(f) && !(inner->flag)))
    1476           0 :                         return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column '%s' in query results without an aggregate function", name);
    1477     1104680 :                 if (exp && inner && is_groupby(inner->op) && !is_sql_aggr(f) && !is_freevar(exp) && !inner->flag)
    1478       25019 :                         exp = rel_groupby_add_aggr(sql, inner, exp);
    1479     1234303 :         } else if (dlist_length(l) == 2 || dlist_length(l) == 3) {
    1480     1234303 :                 const char *sname = NULL;
    1481     1234303 :                 const char *tname = l->h->data.sval;
    1482     1234303 :                 const char *cname = l->h->next->data.sval;
    1483     1234303 :                 if (dlist_length(l) == 3) {
    1484           1 :                         sname = l->h->data.sval;
    1485           1 :                         tname = l->h->next->data.sval;
    1486           1 :                         cname = l->h->next->next->data.sval;
    1487             :                 }
    1488             : 
    1489     1234303 :                 if (!exp && rel && inner)
    1490     1234176 :                         if (!(exp = rel_bind_column3(sql, inner, sname, tname, cname, f)) && sql->session->status == -ERR_AMBIGUOUS)
    1491             :                                 return NULL;
    1492     1234302 :                 if (!exp && inner && is_sql_aggr(f) && (is_groupby(inner->op) || is_select(inner->op))) {
    1493             :                         /* if inner is selection, ie having clause, get the left relation to reach group by */
    1494             :                         sql_rel *gp = inner;
    1495          56 :                         while (gp && is_select(gp->op))
    1496           4 :                                 gp = gp->l;
    1497          52 :                         if (gp && !is_basetable(gp->op) && gp->l && !(exp = rel_bind_column3(sql, gp->l, sname, tname, cname, f)) && sql->session->status == -ERR_AMBIGUOUS)
    1498             :                                 return NULL;
    1499             :                 }
    1500     1234302 :                 if (!exp && query && query_has_outer(query)) {
    1501       11813 :                         int i;
    1502             : 
    1503       12023 :                         for (i=query_has_outer(query)-1; i>= 0 && !exp && (outer = query_fetch_outer(query,i)); i--) {
    1504       12022 :                                 if (!(exp = rel_bind_column3(sql, outer, sname, tname, cname, f | sql_outer)) && sql->session->status == -ERR_AMBIGUOUS)
    1505             :                                         return NULL;
    1506         293 :                                 if (!exp && is_groupby(outer->op)) {
    1507         102 :                                         if (!(exp = rel_bind_column3(sql, outer->l, sname, tname, cname, f)) && sql->session->status == -ERR_AMBIGUOUS)
    1508             :                                                 return NULL;
    1509             :                                         else
    1510         102 :                                                 used_lower_after_processed = is_processed(outer);
    1511             :                                 }
    1512       12022 :                                 if (exp && is_simple_project(outer->op) && !rel_find_exp(outer, exp))
    1513           0 :                                         exp = rel_project_add_exp(sql, outer, exp);
    1514       11812 :                                 if (exp)
    1515             :                                         break;
    1516             :                         }
    1517       11813 :                         if (exp && exp->card != CARD_AGGR && is_groupby(outer->op) && !is_sql_aggr(f) && rel_find_exp(outer->l, exp))
    1518           6 :                                 return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column '%s.%s' in query results without an aggregate function", tname, cname);
    1519       11807 :                         if (exp && outer && outer->card <= CARD_AGGR && exp->card > CARD_AGGR && !is_sql_aggr(f))
    1520           0 :                                 return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column '%s.%s' in query results without an aggregate function", tname, cname);
    1521       11807 :                         if (exp && outer && !is_sql_aggr(f)) {
    1522       11532 :                                 if (used_lower_after_processed || query_outer_used_exp( query, i, exp, f)) {
    1523           0 :                                         sql_exp *lu = used_lower_after_processed?exp:query_outer_last_used(query, i);
    1524           0 :                                         if (exp_name(lu) && exp_relname(lu) && !has_label(lu))
    1525           0 :                                                 return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: subquery uses ungrouped column \"%s.%s\" from outer query", exp_relname(lu), exp_name(lu));
    1526           0 :                                         return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: subquery uses ungrouped column from outer query");
    1527             :                                 }
    1528             :                         }
    1529       11807 :                         if (exp) {
    1530       11806 :                                 int of = query_fetch_outer_state(query, i);
    1531       11806 :                                 if (is_groupby(outer->op) && !is_sql_aggr(f)) {
    1532          64 :                                         exp = rel_groupby_add_aggr(sql, outer, exp);
    1533          64 :                                         exp->card = CARD_ATOM;
    1534       11742 :                                 } else if (is_groupby(outer->op) && is_sql_aggr(f) && exps_any_match(outer->exps, exp))
    1535          52 :                                         exp = exp_ref(sql, exp);
    1536             :                                 else
    1537       11690 :                                         exp->card = CARD_ATOM;
    1538       11806 :                                 set_freevar(exp, i);
    1539             :                                 /*
    1540             :                                 if (exp->alias.label == exp->nid)
    1541             :                                         exp->alias.label = -(sql->nid++);
    1542             :                                         */
    1543       11806 :                                 if (!is_sql_where(of) && !is_sql_aggr(of) && !is_sql_aggr(f) && !outer->grouped)
    1544       10113 :                                         set_outer(outer);
    1545             :                         }
    1546       11807 :                         if (exp && outer && (is_select(outer->op) || is_join(outer->op)))
    1547        4695 :                                 set_dependent_(outer);
    1548             :                 }
    1549             : 
    1550             :                 /* some views are just in the stack, like before and after updates views */
    1551     1234296 :                 if (rel && sql->use_views) {
    1552          40 :                         sql_rel *v = stack_find_rel_view(sql, tname);
    1553             : 
    1554          40 :                         if (v && exp && *rel && is_base(v->op) && v != *rel) /* trigger views are basetables relations, so those may conflict */
    1555           0 :                                 return sql_error(sql, ERR_AMBIGUOUS, SQLSTATE(42000) "SELECT: identifier '%s.%s' ambiguous", tname, cname);
    1556          40 :                         if (v && !exp) {
    1557          20 :                                 if (*rel)
    1558           0 :                                         *rel = rel_crossproduct(sql->sa, *rel, rel_dup(v), op_join);
    1559             :                                 else
    1560          20 :                                         *rel = rel_dup(v);
    1561          20 :                                 if (!(exp = rel_bind_column3(sql, *rel, sname, tname, cname, f)) && sql->session->status == -ERR_AMBIGUOUS)
    1562             :                                         return NULL;
    1563             :                         }
    1564             :                 }
    1565     1234276 :                 if (!exp) { /* If no column was found, try a global variable */
    1566       66270 :                         sql_var *var = NULL;
    1567       66270 :                         sql_subtype *tpe = NULL;
    1568       66270 :                         int level = 0;
    1569       66270 :                         sql_arg *a = NULL;
    1570             : 
    1571       66270 :                         if (find_variable_on_scope(sql, tname, cname, &var, &a, &tpe, &level, "SELECT")) { /* search schema with table name, ugh */
    1572          10 :                                 assert(level == 0);
    1573          10 :                                 exp = exp_param_or_declared(sql->sa, sa_strdup(sql->sa, var->sname), sa_strdup(sql->sa, var->name), &(var->var.tpe), 0);
    1574             :                         }
    1575             :                 }
    1576       66270 :                 if (!exp) {
    1577       66260 :                         if (inner && !is_sql_aggr(f) && is_groupby(inner->op) && inner->l && (exp = rel_bind_column3(sql, inner->l, sname, tname, cname, f))) {
    1578          54 :                                 if (group_by_pk_project_uk_cond(sql, inner, exp, sname, tname)) {
    1579             :                                         /* SQL23 feature: very special case where primary key is used in GROUP BY expression and
    1580             :                                          * unique key is in the project list or ORDER BY clause */
    1581           1 :                                         sql->session->status = 0;
    1582           1 :                                         sql->errstr[0] = 0;
    1583           1 :                                         exp->card = CARD_AGGR;
    1584           1 :                                         list_append(inner->exps, exp);
    1585             :                                 }
    1586             :                                 else
    1587          53 :                                         return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column '%s.%s' in query results without an aggregate function", tname, cname);
    1588             :                         }
    1589             :                 }
    1590             : 
    1591           1 :                 if (!exp)
    1592       66206 :                         return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42S22) "SELECT: no such column '%s.%s'", tname, cname);
    1593     1168037 :                 if (exp && inner && inner->card <= CARD_AGGR && exp->card > CARD_AGGR && (is_sql_sel(f) || is_sql_having(f)) && !is_sql_aggr(f))
    1594           0 :                         return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column '%s.%s' in query results without an aggregate function", tname, cname);
    1595     1168037 :                 if (exp && inner && is_groupby(inner->op) && !is_sql_aggr(f) && !is_freevar(exp))
    1596        5656 :                         exp = rel_groupby_add_aggr(sql, inner, exp);
    1597           0 :         } else if (dlist_length(l) > 3) {
    1598           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "cross-database references are not implemented");
    1599             :         }
    1600     2272717 :         if (exp && !exp_is_atom(exp) && rel && !outer) {
    1601     2143570 :                 if (query->last_exp && query->last_rel == *rel && !is_sql_aggr(query->last_state) && is_sql_aggr(f)) {
    1602          52 :                         if (!is_groupby(query->last_rel->op) || list_empty(query->last_rel->r) || !exps_find_exp(query->last_rel->r, query->last_exp)) {
    1603          31 :                                 if (exp_relname(query->last_exp) && exp_name(query->last_exp) && !has_label(query->last_exp))
    1604          31 :                                         return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column '%s.%s' in query results without an aggregate function", exp_relname(query->last_exp), exp_name(query->last_exp));
    1605           0 :                                 return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column in query results without an aggregate function");
    1606             :                         }
    1607             :                 }
    1608     2143539 :                 query->prev = query->last_exp;
    1609     2143539 :                 query->last_exp = exp;
    1610     2143539 :                 query->last_state = f;
    1611     2143539 :                 query->last_rel = *rel;
    1612             :         }
    1613             :         return exp;
    1614             : }
    1615             : 
    1616             : int
    1617      977569 : rel_convert_types(mvc *sql, sql_rel *ll, sql_rel *rr, sql_exp **L, sql_exp **R, int scale_fixing, check_type tpe)
    1618             : {
    1619      977569 :         sql_exp *ls = *L;
    1620      977569 :         sql_exp *rs = *R;
    1621      977569 :         sql_subtype *lt = exp_subtype(ls);
    1622      977569 :         sql_subtype *rt = exp_subtype(rs);
    1623             : 
    1624      977569 :         if (!rt && !lt) {
    1625           2 :                 sql_error(sql, 01, SQLSTATE(42000) "Cannot have a parameter (?) on both sides of an expression");
    1626           2 :                 return -1;
    1627             :         }
    1628      977567 :         if (rt && (!lt || !lt->type))
    1629           7 :                  return rel_set_type_param(sql, rt, ll, ls, 0);
    1630      977560 :         if (lt && (!rt || !rt->type))
    1631         648 :                  return rel_set_type_param(sql, lt, rr, rs, 0);
    1632             : 
    1633      976912 :         if (rt && lt) {
    1634      976912 :                 sql_subtype *i = lt;
    1635      976912 :                 sql_subtype *r = rt;
    1636             : 
    1637      976912 :                 if (subtype_cmp(lt, rt) != 0 || (tpe == type_equal_no_any && (lt->type->localtype==0 || rt->type->localtype==0))) {
    1638      268855 :                         sql_subtype super;
    1639             : 
    1640      268855 :                         cmp_supertype(&super, r, i);
    1641      268855 :                         if (scale_fixing) {
    1642             :                                 /* convert ls to super type */
    1643      268855 :                                 ls = exp_check_type(sql, &super, ll, ls, tpe);
    1644             :                                 /* convert rs to super type */
    1645      268855 :                                 rs = exp_check_type(sql, &super, rr, rs, tpe);
    1646             :                         } else {
    1647             :                                 /* convert ls to super type */
    1648           0 :                                 super.scale = lt->scale;
    1649           0 :                                 ls = exp_check_type(sql, &super, ll, ls, tpe);
    1650             :                                 /* convert rs to super type */
    1651           0 :                                 super.scale = rt->scale;
    1652           0 :                                 rs = exp_check_type(sql, &super, rr, rs, tpe);
    1653             :                         }
    1654             :                 }
    1655      976912 :                 *L = ls;
    1656      976912 :                 *R = rs;
    1657      976912 :                 if (!ls || !rs)
    1658             :                         return -1;
    1659             :                 return 0;
    1660             :         }
    1661             :         return -1;
    1662             : }
    1663             : 
    1664             : static sql_rel *
    1665      108848 : push_select_exp(mvc *sql, sql_rel *rel, sql_exp *e, sql_exp *ls, int f)
    1666             : {
    1667      108848 :         if (is_outerjoin(rel->op)) {
    1668        1178 :                 if ((is_left(rel->op) || is_full(rel->op)) && rel_find_exp(rel->l, ls)) {
    1669          38 :                         rel_join_add_exp(sql->sa, rel, e);
    1670          38 :                         return rel;
    1671        1140 :                 } else if ((is_right(rel->op) || is_full(rel->op)) && rel_find_exp(rel->r, ls)) {
    1672           5 :                         rel_join_add_exp(sql->sa, rel, e);
    1673           5 :                         return rel;
    1674             :                 }
    1675        1135 :                 if (is_left(rel->op) && rel_find_exp(rel->r, ls)) {
    1676        1109 :                         rel->r = rel_push_select(sql, rel->r, ls, e, f);
    1677        1109 :                         return rel;
    1678          26 :                 } else if (is_right(rel->op) && rel_find_exp(rel->l, ls)) {
    1679          11 :                         rel->l = rel_push_select(sql, rel->l, ls, e, f);
    1680          11 :                         return rel;
    1681             :                 }
    1682             :         }
    1683             :         /* push select into the given relation */
    1684      107685 :         return rel_push_select(sql, rel, ls, e, f);
    1685             : }
    1686             : 
    1687             : static sql_rel *
    1688      205007 : push_join_exp(mvc *sql, sql_rel *rel, sql_exp *e, sql_exp *L, sql_exp *R, sql_exp *R2, int f)
    1689             : {
    1690      205007 :         sql_rel *r;
    1691      205007 :         if (/*is_semi(rel->op) ||*/ (is_outerjoin(rel->op) && !is_processed((rel)))) {
    1692       22022 :                 rel_join_add_exp(sql->sa, rel, e);
    1693       22022 :                 return rel;
    1694             :         }
    1695             :         /* push join into the given relation */
    1696      182985 :         if ((r = rel_push_join(sql, rel, L, R, R2, e, f)) != NULL)
    1697             :                 return r;
    1698           0 :         rel_join_add_exp(sql->sa, rel, e);
    1699           0 :         return rel;
    1700             : }
    1701             : 
    1702             : static sql_rel *
    1703        3390 : rel_select_push_filter_exp_down(mvc *sql, sql_rel *rel, sql_exp *e, list *l, list *r, int ff)
    1704             : {
    1705        3390 :         sql_exp *ll;
    1706        3390 :         if (exps_card(r) <= CARD_ATOM && (exps_are_atoms(r) || exps_have_freevar(sql, r) || exps_have_freevar(sql, l))) {
    1707        3293 :                 if (exps_card(l) == exps_card(r) || rel->processed)  /* bin compare op */
    1708          98 :                         return rel_select(sql->sa, rel, e);
    1709        3195 :                 if ((ll = exps_find_one_multi_exp(l)))
    1710        3195 :                         return push_select_exp(sql, rel, e, ll, ff);
    1711             :         }
    1712          97 :         if (is_outerjoin(rel->op))
    1713           8 :                 return rel_select(sql->sa, rel, e);
    1714          89 :         return rel_select_add_exp(sql->sa, rel, e);
    1715             : }
    1716             : 
    1717             : static sql_rel *
    1718        3388 : rel_filter(mvc *sql, sql_rel *rel, list *l, list *r, char *sname, char *filter_op, int anti, int ff)
    1719             : {
    1720        3388 :         node *n;
    1721        3388 :         sql_exp *e = NULL;
    1722        3388 :         sql_subfunc *f = NULL;
    1723        3388 :         list *tl = sa_list(sql->sa);
    1724        3388 :         bool found = false;
    1725             : 
    1726        6776 :         for (n = l->h; n; n = n->next){
    1727        3388 :                 sql_exp *e = n->data;
    1728             : 
    1729        3388 :                 list_append(tl, exp_subtype(e));
    1730             :         }
    1731       13475 :         for (n = r->h; n; n = n->next){
    1732       10087 :                 sql_exp *e = n->data;
    1733             : 
    1734       10087 :                 list_append(tl, exp_subtype(e));
    1735             :         }
    1736             :         /* find filter function */
    1737        3388 :         f = bind_func_(sql, sname, filter_op, tl, F_FILT, false, &found, false);
    1738        3388 :         if (f) {
    1739        3386 :                 node *n,*m = f->func->ops->h;
    1740        3386 :                 list *nexps = sa_list(sql->sa);
    1741             : 
    1742        6772 :                 for(n=l->h; m && n; m = m->next, n = n->next) {
    1743        3386 :                         sql_arg *a = m->data;
    1744        3386 :                         sql_exp *e = n->data;
    1745             : 
    1746        3386 :                         e = exp_check_type(sql, &a->type, rel, e, type_equal);
    1747        3386 :                         if (!e)
    1748             :                                 return NULL;
    1749        3386 :                         list_append(nexps, e);
    1750             :                 }
    1751        3386 :                 l = nexps;
    1752        3386 :                 nexps = sa_list(sql->sa);
    1753       13465 :                 for(n=r->h; m && n; m = m->next, n = n->next) {
    1754       10079 :                         sql_arg *a = m->data;
    1755       10079 :                         sql_exp *e = n->data;
    1756             : 
    1757       10079 :                         e = exp_check_type(sql, &a->type, rel, e, type_equal);
    1758       10079 :                         if (!e)
    1759             :                                 return NULL;
    1760       10079 :                         list_append(nexps, e);
    1761             :                 }
    1762             :                 r = nexps;
    1763             :         }
    1764        3388 :         if (!f)
    1765           4 :                 return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "SELECT: %s FILTER function %s%s%s'%s'",
    1766           2 :                                                  found ? "insufficient privileges for" : "no such", sname ? "'":"", sname ? sname : "", sname ? "'.":"", filter_op);
    1767        3386 :         e = exp_filter(sql->sa, l, r, f, anti);
    1768             : 
    1769        3386 :         if (exps_one_is_rel(l) || exps_one_is_rel(r)) /* uncorrelated subquery case */
    1770           9 :                 return rel_select(sql->sa, rel, e);
    1771             :         /* atom or row => select */
    1772        6754 :         for (node *n=l->h; n; n = n->next) {
    1773        3377 :                 sql_exp *ls = n->data;
    1774             : 
    1775        3377 :                 if (ls->card > rel->card) {
    1776           0 :                         if (exp_name(ls) && !has_label(ls))
    1777           0 :                                 return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column '%s' in query results without an aggregate function", exp_name(ls));
    1778           0 :                         return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column in query results without an aggregate function");
    1779             :                 }
    1780             :         }
    1781       13429 :         for (node *n=r->h; n; n = n->next) {
    1782       10052 :                 sql_exp *rs = n->data;
    1783             : 
    1784       10052 :                 if (rs->card > rel->card) {
    1785           0 :                         if (exp_name(rs) && !has_label(rs))
    1786           0 :                                 return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column '%s' in query results without an aggregate function", exp_name(rs));
    1787           0 :                         return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column in query results without an aggregate function");
    1788             :                 }
    1789             :         }
    1790        3377 :         return rel_select_push_filter_exp_down(sql, rel, e, l, r, ff);
    1791             : }
    1792             : 
    1793             : static sql_rel *
    1794        3328 : rel_filter_exp_(mvc *sql, sql_rel *rel, sql_exp *ls, sql_exp *r1, sql_exp *r2, sql_exp *r3, char *filter_op, int anti, int f)
    1795             : {
    1796        3328 :         list *l = sa_list(sql->sa);
    1797        3328 :         list *r = sa_list(sql->sa);
    1798             : 
    1799        3328 :         list_append(l, ls);
    1800        3328 :         list_append(r, r1);
    1801        3328 :         if (r2)
    1802        3328 :                 list_append(r, r2);
    1803        3328 :         if (r3)
    1804        3328 :                 list_append(r, r3);
    1805        3328 :         return rel_filter(sql, rel, l, r, "sys", filter_op, anti, f);
    1806             : }
    1807             : 
    1808             : static sql_rel *
    1809      379055 : rel_select_push_compare_exp_down(mvc *sql, sql_rel *rel, sql_exp *e, sql_exp *ls, sql_exp *rs, sql_exp *rs2, int f)
    1810             : {
    1811      379055 :         if (!is_join(rel->op) && !is_select(rel->op))
    1812       86010 :                 return rel_select(sql->sa, rel, e);
    1813      381619 :         if ((rs->card <= CARD_ATOM || (rs2 && ls->card <= CARD_ATOM)) &&
    1814      177148 :                 (exp_is_atom(rs) || (rs2 && exp_is_atom(ls)) || exp_has_freevar(sql, rs) || exp_has_freevar(sql, ls)) &&
    1815         485 :                 (!rs2 || (rs2->card <= CARD_ATOM && (exp_is_atom(rs2) || exp_has_freevar(sql, rs2))))) {
    1816       88038 :                 if (ls->card == rs->card || (rs2 && (ls->card == rs2->card || rs->card == rs2->card)) || rel->processed) /* bin compare op */
    1817         994 :                         return rel_select(sql->sa, rel, e);
    1818             : 
    1819       87044 :                 return push_select_exp(sql, rel, e, ls, f);
    1820             :         } else { /* join */
    1821      205007 :                 return push_join_exp(sql, rel, e, ls, rs, rs2, f);
    1822             :         }
    1823             :         return rel;
    1824             : }
    1825             : 
    1826             : static sql_rel *
    1827      352397 : rel_compare_exp_(sql_query *query, sql_rel *rel, sql_exp *ls, sql_exp *rs, sql_exp *rs2, int type, int anti, int quantifier, int f, int symmetric, int is_semantics)
    1828             : {
    1829      352397 :         mvc *sql = query->sql;
    1830      352397 :         sql_exp *e = NULL;
    1831             : 
    1832      352397 :         if (quantifier || exp_is_rel(ls) || exp_is_rel(rs) || (rs2 && exp_is_rel(rs2))) {
    1833        3314 :                 if (rs2) {
    1834           7 :                         e = exp_compare2(sql->sa, ls, rs, rs2, type, symmetric);
    1835           7 :                         if (anti)
    1836           0 :                                 set_anti(e);
    1837             :                 } else {
    1838        3307 :                         assert(!symmetric);
    1839        3307 :                         if (rel_convert_types(sql, rel, rel, &ls, &rs, 1, type_equal_no_any) < 0)
    1840             :                                 return NULL;
    1841        3307 :                         e = exp_compare_func(sql, ls, rs, compare_func((comp_type)type, anti), quantifier);
    1842             :                 }
    1843        3314 :                 return rel_select(sql->sa, rel, e);
    1844      349083 :         } else if (!rs2) {
    1845      346136 :                 assert(!symmetric);
    1846      346136 :                 if (ls->card < rs->card) {
    1847       12636 :                         sql_exp *swap = ls;
    1848       12636 :                         ls = rs;
    1849       12636 :                         rs = swap;
    1850       12636 :                         type = (int)swap_compare((comp_type)type);
    1851             :                 }
    1852      346136 :                 if (rel_convert_types(sql, rel, rel, &ls, &rs, 1, type_equal_no_any) < 0)
    1853             :                         return NULL;
    1854      346133 :                 e = exp_compare(sql->sa, ls, rs, type);
    1855      346133 :                 if (is_semantics) set_semantics(e);
    1856             :         } else {
    1857        2947 :                 assert(rs2);
    1858        2947 :                 if (rel_convert_types(sql, rel, rel, &ls, &rs, 1, type_equal_no_any) < 0)
    1859             :                         return NULL;
    1860        2947 :                 if (!(rs2 = exp_check_type(sql, exp_subtype(ls), rel, rs2, type_equal)))
    1861             :                         return NULL;
    1862        2947 :                 e = exp_compare2(sql->sa, ls, rs, rs2, type, symmetric);
    1863             :         }
    1864      349080 :         if (anti)
    1865         847 :                 set_anti(e);
    1866             : 
    1867      349080 :         if (!rel)
    1868           0 :                 return rel_select(sql->sa, rel_project_exp(sql, exp_atom_bool(sql->sa, 1)), e);
    1869             : 
    1870             :         /* atom or row => select */
    1871      349080 :         if (ls->card > rel->card || rs->card > rel->card || (rs2 && rs2->card > rel->card)) {
    1872           0 :                 sql_exp *e = ls->card > rel->card ? ls : rs->card > rel->card ? rs : rs2;
    1873           0 :                 if (exp_name(e) && !has_label(e))
    1874           0 :                         return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column '%s' in query results without an aggregate function", exp_name(e));
    1875           0 :                 return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column in query results without an aggregate function");
    1876             :         }
    1877      349080 :         return rel_select_push_compare_exp_down(sql, rel, e, ls, rs, rs2, f);
    1878             : }
    1879             : 
    1880             : static sql_rel *
    1881      349443 : rel_compare_exp(sql_query *query, sql_rel *rel, sql_exp *ls, sql_exp *rs, char *compare_op, int reduce, int quantifier, int need_not, int f, int is_semantics)
    1882             : {
    1883      349443 :         mvc *sql = query->sql;
    1884      349443 :         comp_type type = cmp_equal;
    1885             : 
    1886      349443 :         if (!ls || !rs)
    1887             :                 return NULL;
    1888             : 
    1889      349443 :         if (!quantifier && ((!rel && !query_has_outer(query)) || !reduce)) {
    1890             :                 /* TODO to handle filters here */
    1891           0 :                 sql_exp *e;
    1892             : 
    1893           0 :                 if (rel_convert_types(sql, rel, rel, &ls, &rs, 1, type_equal_no_any) < 0)
    1894             :                         return NULL;
    1895           0 :                 e = rel_binop_(sql, rel, ls, rs, "sys", compare_op, card_value, true);
    1896             : 
    1897           0 :                 if (!e)
    1898             :                         return NULL;
    1899           0 :                 if (!reduce) {
    1900           0 :                         if (rel->op == op_project) {
    1901           0 :                                 append(rel->exps, e);
    1902             :                         } else {
    1903           0 :                                 list *exps = new_exp_list(sql->sa);
    1904             : 
    1905           0 :                                 append(exps, e);
    1906           0 :                                 return rel_project(sql->sa, rel, exps);
    1907             :                         }
    1908             :                 } else {
    1909           0 :                         return rel_select(sql->sa, rel, e);
    1910             :                 }
    1911             :         }
    1912      349443 :         type = compare_str2type(compare_op);
    1913      349443 :         assert(type != cmp_filter);
    1914      349443 :         return rel_compare_exp_(query, rel, ls, rs, NULL, type, need_not, quantifier, f, 0, is_semantics);
    1915             : }
    1916             : 
    1917             : static sql_rel *
    1918      349423 : rel_compare(sql_query *query, sql_rel *rel, symbol *sc, symbol *lo, symbol *ro, char *compare_op, int f, exp_kind k, int quantifier, int is_semantics)
    1919             : {
    1920      349423 :         mvc *sql = query->sql;
    1921      349423 :         sql_exp *rs = NULL, *ls;
    1922      349423 :         comp_type cmp_type = compare_str2type(compare_op);
    1923      349423 :         exp_kind ek = {type_value, card_column, FALSE};
    1924      349423 :         int need_not = 0;
    1925             : 
    1926      349423 :         if ((quantifier == 1 && cmp_type == cmp_equal) ||
    1927      349388 :             (quantifier == 2 && cmp_type == cmp_notequal)) {
    1928          63 :                 dnode *n = sc->data.lval->h;
    1929          63 :                 dlist *dl = dlist_create(sql->sa);
    1930             :                 /* map into IN/NOT IN */
    1931          63 :                 sc->token = cmp_type==cmp_equal?SQL_IN:SQL_NOT_IN;
    1932          63 :                 n->next->type = type_list;
    1933          63 :                 n->next->data.lval = dl;
    1934          63 :                 n->next->next->next = NULL; /* remove quantifier */
    1935          63 :                 dl->h = n->next->next;
    1936          63 :                 n->next->next = NULL; /* (remove comparison) moved righthand side */
    1937          63 :                 return rel_logical_exp(query, rel, sc, f);
    1938             :         }
    1939             :         /* <> ANY -> NOT (= ALL) */
    1940      349360 :         if (quantifier == 1 && cmp_type == cmp_notequal) {
    1941           5 :                 need_not = 1;
    1942           5 :                 quantifier = 2;
    1943           5 :                 cmp_type = cmp_equal;
    1944           5 :                 compare_op = "=";
    1945             :         }
    1946             : 
    1947      349360 :         if ((lo->token == SQL_SELECT || lo->token == SQL_UNION || lo->token == SQL_EXCEPT || lo->token == SQL_INTERSECT || lo->token == SQL_VALUES) &&
    1948          43 :                 (ro->token != SQL_SELECT && ro->token != SQL_UNION && ro->token != SQL_EXCEPT && ro->token != SQL_INTERSECT && ro->token != SQL_VALUES)) {
    1949          30 :                 symbol *tmp = lo; /* swap subquery to the right hand side */
    1950             : 
    1951          30 :                 lo = ro;
    1952          30 :                 ro = tmp;
    1953             : 
    1954          30 :                 if (compare_op[0] == '>')
    1955          16 :                         compare_op[0] = '<';
    1956          14 :                 else if (compare_op[0] == '<' && compare_op[1] != '>')
    1957           1 :                         compare_op[0] = '>';
    1958          30 :                 cmp_type = swap_compare(cmp_type);
    1959             :         }
    1960             : 
    1961      349360 :         ls = rel_value_exp(query, &rel, lo, f|sql_farg, ek);
    1962      349360 :         if (!ls)
    1963             :                 return NULL;
    1964      349311 :         if (ls && rel && exp_has_freevar(sql, ls) && is_sql_sel(f))
    1965           0 :                 ls = rel_project_add_exp(sql, rel, ls);
    1966      349311 :         if (quantifier)
    1967          58 :                 ek.card = card_set;
    1968             : 
    1969      349311 :         rs = rel_value_exp(query, &rel, ro, f|sql_farg, ek);
    1970      349311 :         if (!rs)
    1971             :                 return NULL;
    1972      349283 :         if (ls->card > rs->card && rs->card == CARD_AGGR && is_sql_having(f)) {
    1973           0 :                 if (exp_name(ls) && exp_relname(ls) && !has_label(ls))
    1974           0 :                         return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column '%s.%s' in query results without an aggregate function", exp_relname(ls), exp_name(ls));
    1975           0 :                 return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column in query results without an aggregate function");
    1976             :         }
    1977      349283 :         if (rs->card > ls->card && ls->card == CARD_AGGR && is_sql_having(f)) {
    1978           0 :                 if (exp_name(rs) && exp_relname(rs) && !has_label(rs))
    1979           0 :                         return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column '%s.%s' in query results without an aggregate function", exp_relname(rs), exp_name(rs));
    1980           0 :                 return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column in query results without an aggregate function");
    1981             :         }
    1982      349283 :         return rel_compare_exp(query, rel, ls, rs, compare_op, k.reduce, quantifier, need_not, f, is_semantics);
    1983             : }
    1984             : 
    1985             : static sql_exp*
    1986       78859 : _rel_nop(mvc *sql, char *sname, char *fname, list *tl, sql_rel *rel, list *exps, exp_kind ek)
    1987             : {
    1988       78859 :         bool found = false;
    1989       78859 :         int table_func = (ek.card == card_relation);
    1990       78859 :         sql_ftype type = (ek.card == card_loader)?F_LOADER:((ek.card == card_none)?F_PROC:
    1991             :                    ((ek.card == card_relation)?F_UNION:F_FUNC));
    1992             : 
    1993       78859 :         sql_subfunc *f = bind_func_(sql, sname, fname, tl, type, false, &found, false);
    1994       78859 :         if (f && !(exps = check_arguments_and_find_largest_any_type(sql, rel, exps, f, table_func, false)))
    1995             :                 f = NULL;
    1996             : 
    1997       78843 :         if (f)
    1998       78843 :                 return exp_op(sql->sa, exps, f);
    1999          16 :         char *arg_list = nary_function_arg_types_2str(sql, tl, list_length(tl));
    2000          32 :         return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "SELECT: %s operator %s%s%s'%s'(%s)",
    2001          16 :                                          found ? "insufficient privileges for" : "no such", sname ? "'":"", sname ? sname : "", sname ? "'.":"", fname, arg_list ? arg_list : "");
    2002             : }
    2003             : 
    2004             : static sql_exp *
    2005        2057 : exp_exist(sql_query *query, sql_rel *rel, sql_exp *le, int exists)
    2006             : {
    2007        2057 :         mvc *sql = query->sql;
    2008        2057 :         sql_subfunc *exists_func = NULL;
    2009        2057 :         sql_subtype *t;
    2010        2057 :         sql_exp *res;
    2011             : 
    2012        2057 :         if (!exp_name(le))
    2013         753 :                 exp_label(sql->sa, le, ++sql->label);
    2014        2057 :         if (exp_is_rel(le)) { /* for the subquery case, propagate to the inner query */
    2015        2057 :                 sql_rel *r = exp_rel_get_rel(sql->sa, le);
    2016        2057 :                 if (is_project(r->op) && !list_empty(r->exps)) {
    2017        5898 :                         for (node *n = r->exps->h; n; n = n->next)
    2018        3841 :                                 if (!exp_subtype(n->data) && rel_set_type_param(sql, sql_bind_localtype("bit"), r, n->data, 0) < 0) /* workaround */
    2019             :                                         return NULL;
    2020        2057 :                         le->tpe = *exp_subtype(r->exps->h->data); /* just take the first expression type */
    2021             :                 }
    2022           0 :         } else if (!exp_subtype(le) && rel_set_type_param(sql, sql_bind_localtype("bit"), rel, le, 0) < 0) /* workaround */
    2023             :                 return NULL;
    2024        2057 :         t = exp_subtype(le);
    2025             : 
    2026        2118 :         if (!(exists_func = sql_bind_func(sql, "sys", exists ? "sql_exists" : "sql_not_exists", t, NULL, F_FUNC, true, true)))
    2027           0 :                 return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "exist operator on type %s missing", t ? t->type->base.name : "unknown");
    2028        2057 :         res = exp_unop(sql->sa, le, exists_func);
    2029        2057 :         set_has_no_nil(res);
    2030        2057 :         return res;
    2031             : }
    2032             : 
    2033             : static sql_exp *
    2034        1073 : rel_exists_value_exp(sql_query *query, sql_rel **rel, symbol *sc, int f)
    2035             : {
    2036        1073 :         exp_kind ek = {type_value, card_exists, FALSE};
    2037        1073 :         sql_exp *le, *e;
    2038             : 
    2039        1073 :         le = rel_value_exp(query, rel, sc->data.sym, f|sql_farg, ek);
    2040        1073 :         if (!le)
    2041             :                 return NULL;
    2042        1072 :         if (!(e = exp_exist(query, rel ? *rel : NULL, le, sc->token == SQL_EXISTS)))
    2043             :                 return NULL;
    2044             :         /* only freevar should have CARD_AGGR */
    2045        1072 :         e->card = CARD_ATOM;
    2046        1072 :         return e;
    2047             : }
    2048             : 
    2049             : static sql_rel *
    2050         991 : rel_exists_exp(sql_query *query, sql_rel *rel, symbol *sc, int f)
    2051             : {
    2052         991 :         exp_kind ek = {type_value, card_exists, TRUE};
    2053         991 :         mvc *sql = query->sql;
    2054         991 :         sql_rel *sq = NULL;
    2055             : 
    2056         991 :         if (is_psm_call(f) || is_sql_merge(f))
    2057           3 :                 return sql_error(sql, 02, SQLSTATE(42000) "%s: subqueries not supported inside %s", is_psm_call(f) ? "CALL" : "MERGE", is_psm_call(f) ? "CALL statements" : "MERGE conditions");
    2058         990 :         if (rel) {
    2059         990 :                 query_processed(query);
    2060         990 :                 query_push_outer(query, rel, f);
    2061             :         }
    2062         990 :         sq = rel_subquery(query, sc->data.sym, ek);
    2063         990 :         if (rel)
    2064         990 :                 rel = query_pop_outer(query);
    2065         990 :         assert(!is_sql_sel(f));
    2066         990 :         if (sq) {
    2067         985 :                 sql_exp *e = exp_rel(sql, sq);
    2068         985 :                 if (!(e = exp_exist(query, rel, e, sc->token == SQL_EXISTS)))
    2069             :                         return NULL;
    2070             :                 /* only freevar should have CARD_AGGR */
    2071         985 :                 e->card = CARD_ATOM;
    2072         985 :                 rel = rel_select_add_exp(sql->sa, rel, e);
    2073         985 :                 return rel;
    2074             :         }
    2075             :         return NULL;
    2076             : }
    2077             : 
    2078             : static int
    2079        7029 : is_project_true(sql_rel *r)
    2080             : {
    2081        7029 :         if (r && !r->l && list_length(r->exps) == 1) {
    2082          82 :                 sql_exp *e = r->exps->h->data;
    2083          82 :                 if (exp_is_atom(e) && exp_is_true(e))
    2084             :                         return 1;
    2085             :         }
    2086             :         return 0;
    2087             : }
    2088             : 
    2089             : static sql_exp *
    2090       62330 : rel_in_value_exp(sql_query *query, sql_rel **rel, symbol *sc, int f)
    2091             : {
    2092       62330 :         mvc *sql = query->sql;
    2093       62330 :         exp_kind ek = {type_value, card_column, TRUE};
    2094       62330 :         dlist *dl = sc->data.lval;
    2095       62330 :         symbol *lo = NULL;
    2096       62330 :         dnode *n = dl->h->next, *dn = NULL;
    2097       62330 :         sql_exp *le = NULL, *re, *e = NULL;
    2098       62330 :         list *ll = sa_list(sql->sa);
    2099       62330 :         int is_tuple = 0, add_select = 0;
    2100             : 
    2101             :         /* complex case */
    2102       62330 :         if (dl->h->type == type_list) { /* (a,b..) in (.. ) */
    2103        4117 :                 dn = dl->h->data.lval->h;
    2104        4117 :                 lo = dn->data.sym;
    2105        4117 :                 dn = dn->next;
    2106             :         } else {
    2107       58213 :                 lo = dl->h->data.sym;
    2108             :         }
    2109      197314 :         for( ; lo; lo = dn?dn->data.sym:NULL, dn = dn?dn->next:NULL ) {
    2110       67496 :                 le = rel_value_exp(query, rel, lo, f|sql_farg, ek);
    2111       67496 :                 if (!le)
    2112             :                         return NULL;
    2113       67492 :                 append(ll, le);
    2114             :         }
    2115       62326 :         if (list_length(ll) == 1) {
    2116       58209 :                 le = ll->h->data;
    2117       58209 :                 ek.card = card_set;
    2118             :         } else {
    2119        4117 :                 le = exp_values(sql->sa, ll);
    2120        4117 :                 exp_label(sql->sa, le, ++sql->label);
    2121        4117 :                 ek.type = list_length(ll);
    2122        4117 :                 is_tuple = list_length(ll);
    2123             :         }
    2124             :         /* list of values or subqueries */
    2125             :         /* 3 right hand side cases,
    2126             :          *      1 - value/tuple list of atoms
    2127             :          *      2 - list of scalar sub queries (needs check on 1 row/column per query)
    2128             :          *      3 - single tabular ie. multi column, multi row query
    2129             :          */
    2130       62326 :         if (n->type == type_list) {
    2131       62326 :                 sql_exp *values;
    2132       62326 :                 list *vals = sa_list(sql->sa);
    2133             : 
    2134       62326 :                 n = dl->h->next;
    2135       62326 :                 n = n->data.lval->h;
    2136             : 
    2137      309117 :                 for (; n; n = n->next) {
    2138      246823 :                         if (n->type == type_list) {
    2139           3 :                                 dnode *m = n->data.lval->h;
    2140           3 :                                 list *rl = sa_list(sql->sa);
    2141          12 :                                 for (; m; m = m->next) {
    2142           6 :                                         assert(m->type == type_symbol);
    2143           6 :                                         sql_exp *re = rel_value_exp(query, rel, m->data.sym, f|sql_farg, ek);
    2144           6 :                                         if (!re)
    2145             :                                                 return NULL;
    2146           6 :                                         append(rl, re);
    2147             :                                 }
    2148           3 :                                 re = exp_values(sql->sa, rl);
    2149             :                         } else {
    2150      246820 :                                 assert(n->type == type_symbol);
    2151      246820 :                                 re = rel_value_exp(query, rel, n->data.sym, f|sql_farg, ek);
    2152             :                         }
    2153      246823 :                         if (!re)
    2154             :                                 return NULL;
    2155      246800 :                         if (is_tuple && (!exp_is_rel(re) && !is_values(re)))
    2156           0 :                                 return sql_error(sql, 02, SQLSTATE(42000) "Cannot match a tuple to a single value");
    2157        8226 :                         if (is_tuple && exp_is_rel(re)) {
    2158        4116 :                                 sql_rel *r = exp_rel_get_rel(sql->sa, re);
    2159             : 
    2160        4116 :                                 if (!r)
    2161           0 :                                         return sql_error(sql, 02, SQLSTATE(42000) "Subquery missing");
    2162        4116 :                                 if (r->nrcols != ek.type)
    2163          12 :                                         return sql_error(sql, 02, SQLSTATE(42000) "Subquery has too %s columns", (r->nrcols < ek.type) ? "few" : "many");
    2164        4107 :                                 re = exp_rel_label(sql, re);
    2165      242684 :                         } else if (exp_is_rel(re)) {
    2166        7030 :                                 sql_rel *r = exp_rel_get_rel(sql->sa, re);
    2167        7030 :                                 add_select = 1;
    2168        7030 :                                 if (rel && *rel && is_join((*rel)->op))
    2169        2946 :                                         set_dependent((*rel));
    2170        7030 :                                 if (is_project(r->op) && is_project_true(r->l) && list_length(r->exps) == 1)
    2171          80 :                                         re = r->exps->h->data;
    2172      235654 :                         } else if (is_values(re) && is_tuple != list_length(exp_get_values(re))) {
    2173           0 :                                 return sql_error(sql, 02, SQLSTATE(42000) "Tuple sizes do not match");
    2174             :                         }
    2175      246791 :                         append(vals, re);
    2176             :                 }
    2177             : 
    2178       62294 :                 if (list_empty(vals))
    2179           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "The list of values for IN operator cannot be empty");
    2180             : 
    2181       62294 :                 values = exp_values(sql->sa, vals);
    2182       62294 :                 exp_label(sql->sa, values, ++sql->label);
    2183       62294 :                 if (is_tuple) {
    2184        4108 :                         if (!(values = exp_tuples_set_supertype(sql, exp_get_values(le), values)))
    2185             :                                 return NULL;
    2186        4108 :                         if (!(le->f = tuples_check_types(sql, exp_get_values(le), values)))
    2187             :                                 return NULL;
    2188             :                 } else { /* if it's not a tuple, enforce coercion on the type for every element on the list */
    2189       58186 :                         sql_subtype super, *le_tpe = exp_subtype(le), *values_tpe = NULL;
    2190             : 
    2191      300866 :                         for (node *m = vals->h; m; m = m->next) { /* first get values supertype */
    2192      242680 :                                 sql_exp *e = m->data;
    2193      242680 :                                 sql_subtype *tpe = exp_subtype(e);
    2194             : 
    2195      242680 :                                 if (values_tpe && tpe) {
    2196      184491 :                                         cmp_supertype(&super, values_tpe, tpe);
    2197      184491 :                                         *values_tpe = super;
    2198       58189 :                                 } else if (!values_tpe && tpe) {
    2199       58182 :                                         super = *tpe;
    2200       58182 :                                         values_tpe = &super;
    2201             :                                 }
    2202             :                         }
    2203       58186 :                         if (!le_tpe)
    2204           2 :                                 le_tpe = values_tpe;
    2205       58186 :                         if (!values_tpe)
    2206           4 :                                 values_tpe = le_tpe;
    2207       58186 :                         if (!le_tpe || !values_tpe)
    2208           1 :                                 return sql_error(sql, 01, SQLSTATE(42000) "For the IN operator, both sides must have a type defined");
    2209       58185 :                         cmp_supertype(&super, values_tpe, le_tpe); /* compute supertype */
    2210             : 
    2211             :                         /* on selection/join cases we can generate cmp expressions instead of anyequal for trivial cases */
    2212       58185 :                         if ((is_sql_where(f) || is_sql_having(f)) && !is_sql_farg(f) && !exp_has_rel(le) && exps_are_atoms(vals)) {
    2213       24023 :                                 if (list_length(vals) == 1) { /* use cmp_equal instead of cmp_in for 1 expression */
    2214         115 :                                         sql_exp *first = vals->h->data;
    2215         115 :                                         if (rel_convert_types(sql, rel ? *rel : NULL, rel ? *rel : NULL, &le, &first, 1, type_equal_no_any) < 0)
    2216           0 :                                                 return NULL;
    2217         140 :                                         e = exp_compare(sql->sa, le, first, (sc->token == SQL_IN) ? cmp_equal : cmp_notequal);
    2218             :                                 } else { /* use cmp_in instead of anyequal for n simple expressions */
    2219      120838 :                                         for (node *n = vals->h ; n ; n = n->next)
    2220       96930 :                                                 if ((n->data = exp_check_type(sql, &super, rel ? *rel : NULL, n->data, type_equal)) == NULL)
    2221             :                                                         return NULL;
    2222       23908 :                                         if ((le = exp_check_type(sql, &super, rel ? *rel : NULL, le, type_equal)) == NULL)
    2223             :                                                 return NULL;
    2224       32524 :                                         e = exp_in(sql->sa, le, vals, (sc->token == SQL_IN) ? cmp_in : cmp_notin);
    2225             :                                 }
    2226             :                         }
    2227       24023 :                         if (!e) { /* after computing supertype, check types for each IN value */
    2228      179796 :                                 for (node *n = vals->h ; n ; n = n->next)
    2229      145634 :                                         if ((n->data = exp_check_type(sql, &super, rel ? *rel : NULL, n->data, type_equal)) == NULL)
    2230             :                                                 return NULL;
    2231       34162 :                                 values->tpe = *exp_subtype(vals->h->data);
    2232       34162 :                                 if (!(le = exp_check_type(sql, &super, rel ? *rel : NULL, le, type_equal)))
    2233             :                                         return NULL;
    2234             :                         }
    2235             :                 }
    2236       58185 :                 if (!e) {
    2237       38270 :                         if (add_select && rel && *rel && !is_project((*rel)->op) && !is_select((*rel)->op) && !is_base((*rel)->op))
    2238        2945 :                                 *rel = rel_select(sql->sa, *rel, NULL);
    2239       38270 :                         if ((rel && *rel) || exp_has_rel(le) || exp_has_rel(values))
    2240       32708 :                                 e = exp_in_func(sql, le, values, (sc->token == SQL_IN), is_tuple);
    2241             :                         else
    2242        5562 :                                 e = exp_in_aggr(sql, le, values, (sc->token == SQL_IN), is_tuple);
    2243             :                 }
    2244             :         }
    2245             :         return e;
    2246             : }
    2247             : 
    2248             : static sql_rel *
    2249       32615 : rel_in_exp(sql_query *query, sql_rel *rel, symbol *sc, int f)
    2250             : {
    2251       32615 :         mvc *sql = query->sql;
    2252       32615 :         sql_exp *e = rel_in_value_exp(query, &rel, sc, f);
    2253             : 
    2254       32615 :         assert(!is_sql_sel(f));
    2255       32615 :         if (!e || !rel)
    2256             :                 return NULL;
    2257             : 
    2258       32591 :         if (e->type == e_cmp) { /* it's a exp_in or cmp_equal of simple expressions, push down early on if possible */
    2259       24021 :                 sql_exp *ls = e->l;
    2260       24021 :                 bool rlist = (e->flag == cmp_in || e->flag == cmp_notin);
    2261       24021 :                 unsigned int rcard = rlist ? exps_card(e->r) : exp_card(e->r);
    2262       24021 :                 int r_is_atoms = rlist ? exps_are_atoms(e->r) : exp_is_atom(e->r);
    2263       24021 :                 int r_has_freevar = rlist ? exps_have_freevar(sql, e->r) : exp_has_freevar(sql, e->r);
    2264             : 
    2265       24021 :                 if (rcard <= CARD_ATOM && (r_is_atoms || r_has_freevar || exp_has_freevar(sql, ls))) {
    2266       24021 :                         if ((exp_card(ls) == rcard) || rel->processed) /* bin compare op */
    2267        5412 :                                 return rel_select(sql->sa, rel, e);
    2268             : 
    2269       18609 :                         return push_select_exp(sql, rel, e, ls, f);
    2270             :                 } else { /* join */
    2271           0 :                         sql_exp *rs = rlist ? exps_find_one_multi_exp(e->r) : e->r;
    2272           0 :                         if (rs)
    2273           0 :                                 return push_join_exp(sql, rel, e, ls, rs, NULL, f);
    2274             :                 }
    2275             :         }
    2276        8570 :         if (is_outerjoin(rel->op))
    2277          13 :                 return rel_select(sql->sa, rel, e);
    2278        8557 :         return rel_select_add_exp(sql->sa, rel, e);
    2279             : }
    2280             : 
    2281             : static bool
    2282       52633 : not_symbol_can_be_propagated(mvc *sql, symbol *sc)
    2283             : {
    2284       52633 :         switch (sc->token) {
    2285             :         case SQL_IN:
    2286             :         case SQL_NOT_IN:
    2287             :         case SQL_EXISTS:
    2288             :         case SQL_NOT_EXISTS:
    2289             :         case SQL_LIKE:
    2290             :         case SQL_NOT_LIKE:
    2291             :         case SQL_BETWEEN:
    2292             :         case SQL_NOT_BETWEEN:
    2293             :         case SQL_IS_NULL:
    2294             :         case SQL_IS_NOT_NULL:
    2295             :         case SQL_NOT:
    2296             :         case SQL_COMPARE:
    2297             :                 return true;
    2298        6822 :         case SQL_AND:
    2299             :         case SQL_OR: {
    2300        6822 :                 symbol *lo = sc->data.lval->h->data.sym;
    2301        6822 :                 symbol *ro = sc->data.lval->h->next->data.sym;
    2302        6826 :                 return not_symbol_can_be_propagated(sql, lo) && not_symbol_can_be_propagated(sql, ro);
    2303             :         }
    2304       36563 :         default:
    2305       36563 :                 return false;
    2306             :         }
    2307             : }
    2308             : 
    2309             : /* Warning, this function assumes the entire bison tree can be negated, so call it after 'not_symbol_can_be_propagated' */
    2310             : static symbol *
    2311       16065 : negate_symbol_tree(mvc *sql, symbol *sc)
    2312             : {
    2313       16065 :         switch (sc->token) {
    2314        1454 :         case SQL_IN:
    2315        1454 :                 sc->token = SQL_NOT_IN;
    2316        1454 :                 break;
    2317          22 :         case SQL_NOT_IN:
    2318          22 :                 sc->token = SQL_IN;
    2319          22 :                 break;
    2320           0 :         case SQL_EXISTS:
    2321           0 :                 sc->token = SQL_NOT_EXISTS;
    2322           0 :                 break;
    2323           0 :         case SQL_NOT_EXISTS:
    2324           0 :                 sc->token = SQL_EXISTS;
    2325           0 :                 break;
    2326           7 :         case SQL_LIKE:
    2327           7 :                 sc->token = SQL_NOT_LIKE;
    2328           7 :                 break;
    2329           0 :         case SQL_NOT_LIKE:
    2330           0 :                 sc->token = SQL_LIKE;
    2331           0 :                 break;
    2332          17 :         case SQL_BETWEEN:
    2333          17 :                 sc->token = SQL_NOT_BETWEEN;
    2334          17 :                 break;
    2335          11 :         case SQL_NOT_BETWEEN:
    2336          11 :                 sc->token = SQL_BETWEEN;
    2337          11 :                 break;
    2338          32 :         case SQL_IS_NULL:
    2339          32 :                 sc->token = SQL_IS_NOT_NULL;
    2340          32 :                 break;
    2341          15 :         case SQL_IS_NOT_NULL:
    2342          15 :                 sc->token = SQL_IS_NULL;
    2343          15 :                 break;
    2344          27 :         case SQL_NOT: { /* nested NOTs eliminate each other */
    2345          27 :                 if (sc->data.sym->token == SQL_ATOM) {
    2346           5 :                         AtomNode *an = (AtomNode*) sc->data.sym;
    2347           5 :                         sc = newAtomNode(sql->sa, an->a);
    2348          22 :                 } else if (sc->data.sym->token == SQL_SELECT) {
    2349           2 :                         SelectNode *sn = (SelectNode*) sc->data.sym;
    2350           2 :                         sc = newSelectNode(sql->sa, sn->distinct, sn->selection, sn->into, sn->from, sn->where, sn->groupby, sn->having,
    2351             :                                                            sn->orderby, sn->name, sn->limit, sn->offset, sn->sample, sn->seed, sn->window);
    2352             :                 } else {
    2353          20 :                         memmove(sc, sc->data.sym, sizeof(symbol));
    2354             :                 }
    2355             :         } break;
    2356        7662 :         case SQL_COMPARE: {
    2357        7662 :                 dnode *cmp_n = sc->data.lval->h;
    2358        7662 :                 comp_type neg_cmp_type = negate_compare(compare_str2type(cmp_n->next->data.sval)); /* negate the comparator */
    2359        7662 :                 if (cmp_n->next->next->next) {
    2360          22 :                         switch(cmp_n->next->next->next->data.i_val)
    2361             :                         {
    2362          12 :                         case 0: /* negating ANY/ALL */
    2363          12 :                                 cmp_n->next->next->next->data.i_val = 1;
    2364          12 :                                 break;
    2365           7 :                         case 1: /* negating ANY/ALL */
    2366           7 :                                 cmp_n->next->next->next->data.i_val = 0;
    2367           7 :                                 break;
    2368           2 :                         case 2: /* negating IS [NOT] DINSTINCT FROM */
    2369           2 :                                 cmp_n->next->next->next->data.i_val = 3;
    2370           2 :                                 break;
    2371           1 :                         case 3: /* negating IS [NOT] DINSTINCT FROM */
    2372           1 :                                 cmp_n->next->next->next->data.i_val = 2;
    2373           1 :                                 break;
    2374             :                         }
    2375             :                 }
    2376        7662 :                 cmp_n->next->data.sval = sa_strdup(sql->sa, compare_func(neg_cmp_type, 0));
    2377        7662 :         } break;
    2378        6818 :         case SQL_AND:
    2379             :         case SQL_OR: {
    2380        6818 :                 sc->data.lval->h->data.sym = negate_symbol_tree(sql, sc->data.lval->h->data.sym);
    2381        6818 :                 sc->data.lval->h->next->data.sym= negate_symbol_tree(sql, sc->data.lval->h->next->data.sym);
    2382        6818 :                 sc->token = sc->token == SQL_AND ? SQL_OR : SQL_AND;
    2383        6818 :         } break;
    2384             :         default:
    2385             :                 break;
    2386             :         }
    2387       16065 :         return sc;
    2388             : }
    2389             : 
    2390             : static int
    2391        3111 : exp_between_check_types(sql_subtype *res, sql_subtype *t1, sql_subtype *t2, sql_subtype *t3)
    2392             : {
    2393        3111 :         bool type_found = false;
    2394        3111 :         sql_subtype super;
    2395             : 
    2396        3111 :         if (t1 && t2) {
    2397        3105 :                 cmp_supertype(&super, t2, t1);
    2398        3105 :                 type_found = true;
    2399           6 :         } else if (t1) {
    2400           3 :                 super = *t1;
    2401           3 :                 type_found = true;
    2402           3 :         } else if (t2) {
    2403           1 :                 super = *t2;
    2404           1 :                 type_found = true;
    2405             :         }
    2406        3111 :         if (t3) {
    2407        3108 :                 if (type_found)
    2408        3107 :                         cmp_supertype(&super, t3, &super);
    2409             :                 else
    2410           1 :                         super = *t3;
    2411             :                 type_found = true;
    2412             :         }
    2413           3 :         if (!type_found)
    2414             :                 return -1;
    2415        3110 :         *res = super;
    2416        3110 :         return 0;
    2417             : }
    2418             : 
    2419             : static bool
    2420      116718 : exp_is_null_no_value_opt(sql_exp *e)
    2421             : {
    2422      116718 :         if (!e)
    2423             :                 return false;
    2424      120623 :         while (is_convert(e->type))
    2425        3905 :                 e = e->l;
    2426      233409 :         return e->type == e_atom && e->l && atom_null(e->l);
    2427             : }
    2428             : 
    2429             : sql_exp *
    2430     2536889 : rel_logical_value_exp(sql_query *query, sql_rel **rel, symbol *sc, int f, exp_kind ek)
    2431             : {
    2432     2536988 :         mvc *sql = query->sql;
    2433             : 
    2434     2536988 :         if (!sc)
    2435             :                 return NULL;
    2436             : 
    2437     2536988 :         if (mvc_highwater(sql))
    2438           0 :                 return sql_error(sql, 10, SQLSTATE(42000) "Query too complex: running out of stack space");
    2439             : 
    2440     2536994 :         switch (sc->token) {
    2441       51537 :         case SQL_OR:
    2442             :         case SQL_AND:
    2443             :         {
    2444       51537 :                 symbol *lo = sc->data.lval->h->data.sym;
    2445       51537 :                 symbol *ro = sc->data.lval->h->next->data.sym;
    2446       51537 :                 sql_exp *ls, *rs;
    2447             : 
    2448       51537 :                 if (!(ls = rel_value_exp(query, rel, lo, f|sql_farg, ek)))
    2449             :                         return NULL;
    2450       51536 :                 if (!(rs = rel_value_exp(query, rel, ro, f|sql_farg, ek)))
    2451             :                         return NULL;
    2452       90954 :                 return rel_binop_(sql, rel ? *rel : NULL, ls, rs, "sys", sc->token == SQL_OR ? "or": "and", card_value, false);
    2453             :         }
    2454           5 :         case SQL_FILTER:
    2455             :                 /* [ x,..] filter [ y,..] */
    2456             :                 /* todo add anti, [ x,..] not filter [ y,...] */
    2457             :                 /* no correlation */
    2458             :         {
    2459           5 :                 dnode *ln = sc->data.lval->h->data.lval->h;
    2460           5 :                 dnode *rn = sc->data.lval->h->next->next->data.lval->h;
    2461           5 :                 dlist *filter_op = sc->data.lval->h->next->data.lval;
    2462           5 :                 char *fname = qname_schema_object(filter_op);
    2463           5 :                 char *sname = qname_schema(filter_op);
    2464           5 :                 list *exps, *tl;
    2465           5 :                 sql_subtype *obj_type = NULL;
    2466             : 
    2467           5 :                 exps = sa_list(sql->sa);
    2468           5 :                 tl = sa_list(sql->sa);
    2469          15 :                 for (; ln; ln = ln->next) {
    2470           5 :                         symbol *sym = ln->data.sym;
    2471             : 
    2472           5 :                         sql_exp *e = rel_value_exp(query, rel, sym, f|sql_farg, ek);
    2473           5 :                         if (!e)
    2474             :                                 return NULL;
    2475           5 :                         if (!obj_type)
    2476           5 :                                 obj_type = exp_subtype(e);
    2477           5 :                         list_append(exps, e);
    2478           5 :                         append(tl, exp_subtype(e));
    2479             :                 }
    2480          13 :                 for (; rn; rn = rn->next) {
    2481          10 :                         symbol *sym = rn->data.sym;
    2482             : 
    2483          10 :                         sql_exp *e = rel_value_exp(query, rel, sym, f|sql_farg, ek);
    2484          10 :                         if (!e)
    2485             :                                 return NULL;
    2486           8 :                         list_append(exps, e);
    2487           8 :                         append(tl, exp_subtype(e));
    2488             :                 }
    2489             :                 /* find the predicate filter function */
    2490           3 :                 return _rel_nop(sql, sname, fname, tl, rel ? *rel : NULL, exps, ek);
    2491             :         }
    2492      116434 :         case SQL_COMPARE:
    2493             :         {
    2494      116434 :                 dnode *n = sc->data.lval->h;
    2495      116434 :                 symbol *lo = n->data.sym;
    2496      116434 :                 symbol *ro = n->next->next->data.sym;
    2497      116434 :                 char *compare_op = n->next->data.sval;
    2498      116434 :                 int quantifier = 0, need_not = 0;
    2499      116434 :                 sql_exp *rs = NULL, *ls;
    2500      116434 :                 comp_type cmp_type = compare_str2type(compare_op);
    2501      116434 :                 bool is_not_distinct_from = false;
    2502      116434 :                 bool is_distinct_from = false;
    2503             : 
    2504             :                 /*
    2505             :                  * = ANY -> IN, <> ALL -> NOT( = ANY) -> NOT IN
    2506             :                  * = ALL -> all(groupby(all, nil)), <> ANY -> NOT ( = ALL )
    2507             :                  */
    2508      116434 :                 if (n->next->next->next)
    2509         256 :                         quantifier = n->next->next->next->data.i_val + 1;
    2510         256 :                 assert(quantifier == 0 || quantifier == 1 || quantifier == 2 || quantifier == 3 || quantifier == 4);
    2511             : 
    2512             :                 /* [NOT] DISTINCT FROM */
    2513      116434 :                 if (quantifier == 3) {
    2514             :                                 is_not_distinct_from = true;
    2515             :                                 quantifier = 0;
    2516             :                 }
    2517      116421 :                 else if (quantifier == 4) {
    2518          14 :                                 is_distinct_from = true;
    2519          14 :                                 quantifier = 0;
    2520             :                 }
    2521             : 
    2522      116434 :                 if ((quantifier == 1 && cmp_type == cmp_equal) ||
    2523      116368 :                     (quantifier == 2 && cmp_type == cmp_notequal)) {
    2524          93 :                         dlist *dl = dlist_create(sql->sa);
    2525             :                         /* map into IN/NOT IN */
    2526          93 :                         sc->token = cmp_type==cmp_equal?SQL_IN:SQL_NOT_IN;
    2527          93 :                         n->next->type = type_list;
    2528          93 :                         n->next->data.lval = dl;
    2529          93 :                         n->next->next->next = NULL; /* remove quantifier */
    2530          93 :                         dl->h = n->next->next;
    2531          93 :                         n->next->next = NULL; /* (remove comparison) moved righthand side */
    2532          93 :                         return rel_logical_value_exp(query, rel, sc, f, ek);
    2533             :                 }
    2534             :                 /* <> ANY -> NOT (= ALL) */
    2535      116341 :                 if (quantifier == 1 && cmp_type == cmp_notequal) {
    2536          15 :                         need_not = 1;
    2537          15 :                         quantifier = 2;
    2538          15 :                         cmp_type = cmp_equal;
    2539          15 :                         compare_op = "=";
    2540             :                 }
    2541             : 
    2542      116341 :                 ls = rel_value_exp(query, rel, lo, f|sql_farg, ek);
    2543      116341 :                 if (!ls)
    2544             :                         return NULL;
    2545      116329 :                 if (quantifier)
    2546         136 :                         ek.card = card_set;
    2547             : 
    2548      116329 :                 rs = rel_value_exp(query, rel, ro, f|sql_farg, ek);
    2549      116329 :                 if (!rs)
    2550             :                         return NULL;
    2551             : 
    2552      116319 :                 if (is_distinct_from || is_not_distinct_from) {
    2553          27 :                         if (rel_convert_types(sql, rel ? *rel : NULL, rel ? *rel : NULL, &ls, &rs, 1, type_equal_no_any) < 0)
    2554             :                                 return NULL;
    2555          41 :                         sql_exp* e = exp_compare(sql->sa, ls, rs, is_not_distinct_from?cmp_equal:cmp_notequal);
    2556          27 :                         set_semantics(e);
    2557          27 :                         return e;
    2558             :                 }
    2559             : 
    2560      116292 :                 if (rs->type == e_atom)
    2561      104996 :                         quantifier = 0;
    2562             : 
    2563      116292 :                 if (!exp_is_rel(ls) && !exp_is_rel(rs) && ls->card < rs->card) {
    2564          14 :                         sql_exp *swap = ls; /* has to swap parameters like in the rel_logical_exp case */
    2565          14 :                         ls = rs;
    2566          14 :                         rs = swap;
    2567          14 :                         cmp_type = swap_compare(cmp_type);
    2568             :                 }
    2569             : 
    2570      116292 :                 if (rel_convert_types(sql, rel ? *rel : NULL, rel ? *rel : NULL, &ls, &rs, 1, type_equal_no_any) < 0)
    2571             :                         return NULL;
    2572      116289 :                 if (exp_is_null_no_value_opt(ls) && exp_is_null_no_value_opt(rs))
    2573           7 :                         return exp_atom(sql->sa, atom_general(sql->sa, sql_bind_localtype("bit"), NULL, 0));
    2574             : 
    2575      116282 :                 return exp_compare_func(sql, ls, rs, compare_func(cmp_type, need_not), quantifier);
    2576             :         }
    2577             :         /* Set Member ship */
    2578       29715 :         case SQL_IN:
    2579             :         case SQL_NOT_IN:
    2580       29715 :                 return rel_in_value_exp(query, rel, sc, f);
    2581        1073 :         case SQL_EXISTS:
    2582             :         case SQL_NOT_EXISTS:
    2583        1073 :                 return rel_exists_value_exp(query, rel, sc, f);
    2584         347 :         case SQL_LIKE:
    2585             :         case SQL_NOT_LIKE:
    2586             :         {
    2587         347 :                 symbol *lo = sc->data.lval->h->data.sym;
    2588         347 :                 symbol *ro = sc->data.lval->h->next->data.sym;
    2589         347 :                 int insensitive = sc->data.lval->h->next->next->data.i_val;
    2590         347 :                 int anti = (sc->token == SQL_NOT_LIKE) != (sc->data.lval->h->next->next->next->data.i_val != 0);
    2591         347 :                 sql_subtype *st = sql_bind_localtype("str");
    2592         347 :                 sql_exp *le = rel_value_exp(query, rel, lo, f|sql_farg, ek), *re, *ee = NULL, *ie = exp_atom_bool(sql->sa, insensitive);
    2593             : 
    2594         347 :                 if (!le)
    2595             :                         return NULL;
    2596             : 
    2597         347 :                 if (!exp_subtype(le))
    2598           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "SELECT: parameter not allowed on "
    2599             :                                         "left hand side of LIKE operator");
    2600             : 
    2601         347 :                 lo = ro->data.lval->h->data.sym;
    2602             :                 /* like uses a single string pattern */
    2603         347 :                 ek.card = card_value;
    2604         347 :                 re = rel_value_exp(query, rel, lo, f|sql_farg, ek);
    2605         347 :                 if (!re)
    2606             :                         return NULL;
    2607         343 :                 if ((re = exp_check_type(sql, st, rel ? *rel : NULL, re, type_equal)) == NULL)
    2608           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "LIKE: wrong type, should be string");
    2609         343 :                 if ((le = exp_check_type(sql, st, rel ? *rel : NULL, le, type_equal)) == NULL)
    2610           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "LIKE: wrong type, should be string");
    2611             :                 /* Do we need to escape ? */
    2612         343 :                 if (dlist_length(ro->data.lval) == 2) {
    2613          63 :                         char *escape = ro->data.lval->h->next->data.sval;
    2614          63 :                         ee = exp_atom(sql->sa, atom_string(sql->sa, st, sa_strdup(sql->sa, escape)));
    2615             :                 } else {
    2616         280 :                         ee = exp_atom(sql->sa, atom_string(sql->sa, st, sa_strdup(sql->sa, "")));
    2617             :                 }
    2618         573 :                 return rel_nop_(sql, rel ? *rel : NULL, le, re, ee, ie, "sys", anti ? "not_like" : "like", card_value);
    2619             :         }
    2620         164 :         case SQL_BETWEEN:
    2621             :         case SQL_NOT_BETWEEN:
    2622             :         {
    2623         164 :                 symbol *lo = sc->data.lval->h->data.sym;
    2624         164 :                 int symmetric = sc->data.lval->h->next->data.i_val;
    2625         164 :                 symbol *ro1 = sc->data.lval->h->next->next->data.sym;
    2626         164 :                 symbol *ro2 = sc->data.lval->h->next->next->next->data.sym;
    2627         164 :                 sql_exp *le, *re1, *re2;
    2628         164 :                 sql_subtype super;
    2629             : 
    2630         164 :                 assert(sc->data.lval->h->next->type == type_int);
    2631             : 
    2632         164 :                 if (!(le = rel_value_exp(query, rel, lo, f|sql_farg, ek)))
    2633             :                         return NULL;
    2634         163 :                 if (!(re1 = rel_value_exp(query, rel, ro1, f|sql_farg, ek)))
    2635             :                         return NULL;
    2636         160 :                 if (!(re2 = rel_value_exp(query, rel, ro2, f|sql_farg, ek)))
    2637             :                         return NULL;
    2638             : 
    2639         155 :                 if (exp_between_check_types(&super, exp_subtype(le), exp_subtype(re1), exp_subtype(re2)) < 0)
    2640           0 :                         return sql_error(sql, 01, SQLSTATE(42000) "Cannot have a parameter (?) on both sides of an expression");
    2641             : 
    2642         155 :                 if ((le = exp_check_type(sql, &super, rel ? *rel:NULL, le, type_equal)) == NULL ||
    2643         155 :                     (re1 = exp_check_type(sql, &super, rel ? *rel:NULL, re1, type_equal)) == NULL ||
    2644         155 :                     (re2 = exp_check_type(sql, &super, rel ? *rel:NULL, re2, type_equal)) == NULL)
    2645           0 :                         return NULL;
    2646             : 
    2647         155 :                 le = exp_compare2(sql->sa, le, re1, re2, 3, symmetric);
    2648         155 :                 if (sc->token == SQL_NOT_BETWEEN)
    2649          56 :                         set_anti(le);
    2650             :                 return le;
    2651             :         }
    2652       16277 :         case SQL_IS_NULL:
    2653             :         case SQL_IS_NOT_NULL:
    2654             :         /* is (NOT) NULL */
    2655             :         {
    2656       16277 :                 sql_exp *le = rel_value_exp(query, rel, sc->data.sym, f|sql_farg, ek);
    2657             : 
    2658       16277 :                 if (!le)
    2659             :                         return NULL;
    2660       20527 :                 le = rel_unop_(sql, rel ? *rel : NULL, le, "sys", sc->token == SQL_IS_NULL ? "isnull" : "isnotnull", card_value);
    2661       16274 :                 if (!le)
    2662             :                         return NULL;
    2663       16274 :                 set_has_no_nil(le);
    2664       16274 :                 return le;
    2665             :         }
    2666       18914 :         case SQL_NOT: {
    2667       18914 :                 if (not_symbol_can_be_propagated(sql, sc->data.sym)) {
    2668          99 :                         sc->data.sym = negate_symbol_tree(sql, sc->data.sym);
    2669          99 :                         return rel_logical_value_exp(query, rel, sc->data.sym, f, ek);
    2670             :                 }
    2671       18815 :                 sql_exp *le = rel_value_exp(query, rel, sc->data.sym, f|sql_farg, ek);
    2672             : 
    2673       18815 :                 if (!le)
    2674             :                         return NULL;
    2675       18814 :                 return rel_unop_(sql, rel ? *rel : NULL, le, "sys", "not", card_value);
    2676             :         }
    2677             :         case SQL_ATOM: {
    2678     2281608 :                 AtomNode *an = (AtomNode *) sc;
    2679     2281608 :                 assert(an && an->a);
    2680     2281608 :                 return exp_atom(sql->sa, an->a);
    2681             :         }
    2682       20400 :         case SQL_IDENT:
    2683             :         case SQL_COLUMN:
    2684       20400 :                 return rel_column_ref(query, rel, sc, f);
    2685         201 :         case SQL_UNION:
    2686             :         case SQL_EXCEPT:
    2687             :         case SQL_INTERSECT: {
    2688         201 :                 sql_rel *sq;
    2689             : 
    2690         201 :                 if (is_psm_call(f) || is_sql_merge(f))
    2691           4 :                         return sql_error(sql, 02, SQLSTATE(42000) "%s: subqueries not supported inside %s", is_psm_call(f) ? "CALL" : "MERGE", is_psm_call(f) ? "CALL statements" : "MERGE conditions");
    2692         199 :                 if (rel && *rel)
    2693         141 :                         query_push_outer(query, *rel, f);
    2694         199 :                 sq = rel_setquery(query, sc);
    2695         199 :                 if (rel && *rel) {
    2696         141 :                         *rel = query_pop_outer(query);
    2697         141 :                         if (is_sql_join(f) && is_groupby((*rel)->op)) {
    2698           0 :                                 return sql_error(sql, 05, SQLSTATE(42000) "SELECT: aggregate functions not allowed in JOIN conditions");
    2699         141 :                         } else if (is_sql_where(f) && is_groupby((*rel)->op)) {
    2700           0 :                                 return sql_error(sql, 02, SQLSTATE(42000) "SELECT: aggregate functions not allowed in WHERE clause");
    2701         141 :                         } else if ((is_sql_update_set(f) || is_sql_psm(f)) && is_groupby((*rel)->op)) {
    2702           0 :                                 return sql_error(sql, 02, SQLSTATE(42000) "SELECT: aggregate functions not allowed in SET, WHILE, IF, ELSE, CASE, WHEN, RETURN, ANALYZE clauses");
    2703             :                         }
    2704             :                 }
    2705         199 :                 if (!sq)
    2706             :                         return NULL;
    2707         197 :                 if (ek.type == type_value && ek.card <= card_set && is_project(sq->op) && list_length(sq->exps) > 1)
    2708           1 :                         return sql_error(sql, 02, SQLSTATE(42000) "SELECT: subquery must return only one column");
    2709         196 :                 if (ek.type == type_relation && is_project(sq->op) && list_length(sq->exps) != ek.type)
    2710           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "SELECT: subquery has too %s columns", list_length(sq->exps) < ek.type ? "few" : "many");
    2711         302 :                 if (ek.type == type_value && ek.card < card_set && sq->card >= CARD_AGGR && (is_sql_sel(f) | is_sql_having(f) | is_sql_farg(f) |
    2712         106 :                         ( is_sql_where(f) && rel && (!*rel || is_basetable((*rel)->op) || is_simple_project((*rel)->op) || is_joinop((*rel)->op)))))
    2713         106 :                         sq = rel_zero_or_one(sql, sq, ek);
    2714         196 :                 return exp_rel(sql, sq);
    2715             :         }
    2716           1 :         case SQL_DEFAULT:
    2717           1 :                 return sql_error(sql, 02, SQLSTATE(42000) "DEFAULT keyword not allowed outside insert and update statements");
    2718         318 :         default: {
    2719         318 :                 sql_exp *le = rel_value_exp(query, rel, sc, f|sql_farg, ek);
    2720         318 :                 sql_subtype bt;
    2721             : 
    2722         318 :                 if (!le)
    2723             :                         return NULL;
    2724         316 :                 sql_find_subtype(&bt, "boolean", 0, 0);
    2725         316 :                 if ((le = exp_check_type(sql, &bt, rel ? *rel : NULL, le, type_equal)) == NULL)
    2726             :                         return NULL;
    2727         316 :                 return rel_binop_(sql, rel ? *rel : NULL, le, exp_atom_bool(sql->sa, 1), "sys", "=", 0, true);
    2728             :         }
    2729             :         }
    2730             :         /* never reached, as all switch cases have a `return` */
    2731             : }
    2732             : 
    2733             : sql_rel *
    2734      632654 : rel_logical_exp(sql_query *query, sql_rel *rel, symbol *sc, int f)
    2735             : {
    2736      632654 :         mvc *sql = query->sql;
    2737      632654 :         exp_kind ek = {type_value, card_column, TRUE};
    2738             : 
    2739      632654 :         if (!sc)
    2740             :                 return NULL;
    2741             : 
    2742      632654 :         if (mvc_highwater(sql))
    2743           0 :                 return sql_error(sql, 10, SQLSTATE(42000) "Query too complex: running out of stack space");
    2744             : 
    2745      632654 :         switch (sc->token) {
    2746       18969 :         case SQL_OR:
    2747             :         {
    2748       18969 :                 list *exps = NULL, *lexps = NULL, *rexps = NULL;
    2749       18969 :                 symbol *lo = sc->data.lval->h->data.sym;
    2750       18969 :                 symbol *ro = sc->data.lval->h->next->data.sym;
    2751       18969 :                 sql_rel *lr, *rr;
    2752             : 
    2753       18969 :                 if (!rel)
    2754             :                         return NULL;
    2755             : 
    2756       18969 :                 lr = rel;
    2757       18969 :                 rr = rel_dup(lr);
    2758             : 
    2759       18969 :                 if (is_outerjoin(rel->op) && !is_processed(rel)) {
    2760          44 :                         exps = rel->exps;
    2761             : 
    2762          44 :                         lr = rel_select_copy(sql->sa, lr, sa_list(sql->sa));
    2763          44 :                         lr = rel_logical_exp(query, lr, lo, f | sql_or);
    2764          44 :                         if (!lr)
    2765             :                                 return NULL;
    2766          43 :                         query_processed(query);
    2767          43 :                         rr = rel_select_copy(sql->sa, rr, sa_list(sql->sa));
    2768          43 :                         rr = rel_logical_exp(query, rr, ro, f | sql_or);
    2769          43 :                         if (!rr)
    2770             :                                 return NULL;
    2771          42 :                         if (lr->l == rr->l) {
    2772          42 :                                 lexps = lr->exps;
    2773          42 :                                 lr = lr->l;
    2774          42 :                                 rexps = rr->exps;
    2775          42 :                                 rr = rr->l;
    2776             :                         }
    2777          42 :                         rel = NULL;
    2778             :                 } else {
    2779       18925 :                         lr = rel_logical_exp(query, lr, lo, f | sql_or);
    2780       18925 :                         if (!lr)
    2781             :                                 return NULL;
    2782       18923 :                         rr = rel_logical_exp(query, rr, ro, f | sql_or);
    2783             :                 }
    2784             : 
    2785       18965 :                 if (!lr || !rr)
    2786             :                         return NULL;
    2787       18965 :                 return rel_or(sql, rel, lr, rr, exps, lexps, rexps);
    2788             :         }
    2789      191952 :         case SQL_AND:
    2790             :         {
    2791      191952 :                 symbol *lo = sc->data.lval->h->data.sym;
    2792      191952 :                 symbol *ro = sc->data.lval->h->next->data.sym;
    2793      191952 :                 rel = rel_logical_exp(query, rel, lo, f);
    2794      191952 :                 if (!rel)
    2795             :                         return NULL;
    2796      191945 :                 return rel_logical_exp(query, rel, ro, f);
    2797             :         }
    2798          60 :         case SQL_FILTER:
    2799             :                 /* [ x,..] filter [ y,..] */
    2800             :                 /* todo add anti, [ x,..] NOT filter [ y,...] */
    2801             :                 /* no correlation */
    2802             :         {
    2803          60 :                 dnode *ln = sc->data.lval->h->data.lval->h;
    2804          60 :                 dnode *rn = sc->data.lval->h->next->next->data.lval->h;
    2805          60 :                 dlist *filter_op = sc->data.lval->h->next->data.lval;
    2806          60 :                 char *fname = qname_schema_object(filter_op);
    2807          60 :                 char *sname = qname_schema(filter_op);
    2808          60 :                 list *l, *r;
    2809             : 
    2810          60 :                 l = sa_list(sql->sa);
    2811          60 :                 r = sa_list(sql->sa);
    2812         180 :                 for (; ln; ln = ln->next) {
    2813          60 :                         symbol *sym = ln->data.sym;
    2814             : 
    2815          60 :                         sql_exp *e = rel_value_exp(query, &rel, sym, f|sql_farg, ek);
    2816          60 :                         if (!e)
    2817             :                                 return NULL;
    2818          60 :                         list_append(l, e);
    2819             :                 }
    2820         163 :                 for (; rn; rn = rn->next) {
    2821         103 :                         symbol *sym = rn->data.sym;
    2822             : 
    2823         103 :                         sql_exp *e = rel_value_exp(query, &rel, sym, f|sql_farg, ek);
    2824         103 :                         if (!e)
    2825             :                                 return NULL;
    2826         103 :                         list_append(r, e);
    2827             :                 }
    2828          60 :                 return rel_filter(sql, rel, l, r, sname, fname, 0, f);
    2829             :         }
    2830      349423 :         case SQL_COMPARE:
    2831             :         {
    2832      349423 :                 dnode *n = sc->data.lval->h;
    2833      349423 :                 symbol *lo = n->data.sym;
    2834      349423 :                 symbol *ro = n->next->next->data.sym;
    2835      349423 :                 char *compare_op = n->next->data.sval;
    2836      349423 :                 int quantifier = 0;
    2837      349423 :                 int is_semantics = 0;
    2838      349423 :                 bool is_distinct_from = false;
    2839             : 
    2840      349423 :                 if (n->next->next->next)
    2841         131 :                         quantifier = n->next->next->next->data.i_val + 1;
    2842         131 :                 assert(quantifier == 0 || quantifier == 1 || quantifier == 2 || quantifier == 3 || quantifier == 4);
    2843             : 
    2844      349423 :                 if (quantifier >= 3) {
    2845          10 :                         if (quantifier == 4) {
    2846           6 :                                 is_distinct_from = true;
    2847           6 :                                 compare_op = "<>";
    2848             :                         }
    2849             :                         quantifier = 0;
    2850             :                         is_semantics = 1;
    2851             :                 }
    2852      349423 :                 if (is_distinct_from) {
    2853           6 :                         sql_exp* ls = rel_value_exp(query, &rel, lo, f|sql_farg, ek);
    2854           6 :                         if (!ls)
    2855             :                                 return NULL;
    2856           6 :                         sql_exp* rs = rel_value_exp(query, &rel, ro, f|sql_farg, ek);
    2857           6 :                         if (!rs)
    2858             :                                 return NULL;
    2859             : 
    2860           6 :                         bool ls_is_non_null_atom = exp_is_atom(ls) && exp_is_not_null(ls);
    2861           6 :                         bool rs_is_non_null_atom = exp_is_atom(rs) && exp_is_not_null(rs);
    2862             : 
    2863           6 :                         if (ls_is_non_null_atom || rs_is_non_null_atom) {
    2864           4 :                                 sql_rel *r = rel_dup(rel);
    2865           4 :                                 sql_rel* l = rel_compare(query, rel, sc, lo, ro, compare_op, f | sql_or, ek, quantifier, 0);
    2866           4 :                                 if (!l)
    2867             :                                         return NULL;
    2868           3 :                                 sql_subtype *t;
    2869           5 :                                 if (!(t = exp_subtype(rs_is_non_null_atom?ls:rs)))
    2870           0 :                                         return sql_error(sql, 01, SQLSTATE(42000) "Cannot have a parameter for IS NULL operator");
    2871           3 :                                 sql_exp* e = exp_compare(sql->sa, rs_is_non_null_atom?ls:rs, exp_atom(sql->sa, atom_general(sql->sa, t, NULL, 0)), cmp_equal);
    2872           3 :                                 set_has_no_nil(e);
    2873           3 :                                 set_semantics(e);
    2874             : 
    2875           3 :                                 r = rel_select_push_compare_exp_down(sql, r, e, e->l, e->r, NULL, f | sql_or);
    2876           3 :                                 if (!r)
    2877             :                                         return NULL;
    2878           3 :                                 return rel_or(sql, rel, l, r, NULL, NULL, NULL);
    2879             :                         }
    2880             :                 }
    2881             : 
    2882             :                 /* [NOT] DISTINCT FROM */
    2883      349419 :                 return rel_compare(query, rel, sc, lo, ro, compare_op, f, ek, quantifier, is_semantics);
    2884             :         }
    2885             :         /* Set Member ship */
    2886       32615 :         case SQL_IN:
    2887             :         case SQL_NOT_IN:
    2888       32615 :                 return rel_in_exp(query, rel, sc, f);
    2889         991 :         case SQL_EXISTS:
    2890             :         case SQL_NOT_EXISTS:
    2891         991 :                 return rel_exists_exp(query, rel , sc, f);
    2892        3331 :         case SQL_LIKE:
    2893             :         case SQL_NOT_LIKE:
    2894             :         {
    2895        3331 :                 symbol *lo = sc->data.lval->h->data.sym;
    2896        3331 :                 symbol *ro = sc->data.lval->h->next->data.sym;
    2897        3331 :                 int insensitive = sc->data.lval->h->next->next->data.i_val;
    2898        3331 :                 int anti = (sc->token == SQL_NOT_LIKE) != (sc->data.lval->h->next->next->next->data.i_val != 0);
    2899        3331 :                 sql_subtype *st = sql_bind_localtype("str");
    2900        3331 :                 sql_exp *le = rel_value_exp(query, &rel, lo, f|sql_farg, ek), *re, *ee = NULL, *ie = exp_atom_bool(sql->sa, insensitive);
    2901             : 
    2902        3331 :                 if (!le)
    2903             :                         return NULL;
    2904             : 
    2905        3329 :                 if (!exp_subtype(le))
    2906           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "SELECT: parameter not allowed on "
    2907             :                                         "left hand side of LIKE operator");
    2908             : 
    2909             :                 /* Do we need to escape ? */
    2910        3329 :                 if (dlist_length(ro->data.lval) == 2) {
    2911          82 :                         char *escape = ro->data.lval->h->next->data.sval;
    2912          82 :                         ee = exp_atom(sql->sa, atom_string(sql->sa, st, sa_strdup(sql->sa, escape)));
    2913             :                 } else {
    2914        3247 :                         ee = exp_atom(sql->sa, atom_string(sql->sa, st, sa_strdup(sql->sa, "")));
    2915             :                 }
    2916        3329 :                 ro = ro->data.lval->h->data.sym;
    2917        3329 :                 re = rel_value_exp(query, &rel, ro, f|sql_farg, ek);
    2918        3329 :                 if (!re)
    2919             :                         return NULL;
    2920        3328 :                 if ((re = exp_check_type(sql, st, rel, re, type_equal)) == NULL)
    2921           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "LIKE: wrong type, should be string");
    2922        3328 :                 if ((le = exp_check_type(sql, st, rel, le, type_equal)) == NULL)
    2923           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "LIKE: wrong type, should be string");
    2924        3328 :                 return rel_filter_exp_(sql, rel, le, re, ee, ie, "like", anti, f);
    2925             :         }
    2926        2960 :         case SQL_BETWEEN:
    2927             :         case SQL_NOT_BETWEEN:
    2928             :         {
    2929        2960 :                 symbol *lo = sc->data.lval->h->data.sym;
    2930        2960 :                 int symmetric = sc->data.lval->h->next->data.i_val;
    2931        2960 :                 symbol *ro1 = sc->data.lval->h->next->next->data.sym;
    2932        2960 :                 symbol *ro2 = sc->data.lval->h->next->next->next->data.sym;
    2933        2960 :                 sql_exp *le, *re1, *re2;
    2934        2960 :                 sql_subtype super;
    2935             : 
    2936        2960 :                 assert(sc->data.lval->h->next->type == type_int);
    2937             : 
    2938        2960 :                 if (!(le = rel_value_exp(query, &rel, lo, f|sql_farg, ek)))
    2939             :                         return NULL;
    2940        2958 :                 if (!(re1 = rel_value_exp(query, &rel, ro1, f|sql_farg, ek)))
    2941             :                         return NULL;
    2942        2956 :                 if (!(re2 = rel_value_exp(query, &rel, ro2, f|sql_farg, ek)))
    2943             :                         return NULL;
    2944             : 
    2945        2956 :                 if (exp_between_check_types(&super, exp_subtype(le), exp_subtype(re1), exp_subtype(re2)) < 0)
    2946           1 :                         return sql_error(sql, 01, SQLSTATE(42000) "Cannot have a parameter (?) on both sides of an expression");
    2947             : 
    2948        2955 :                 if ((le = exp_check_type(sql, &super, rel, le, type_equal)) == NULL ||
    2949        2955 :                     (re1 = exp_check_type(sql, &super, rel, re1, type_equal)) == NULL ||
    2950        2955 :                     (re2 = exp_check_type(sql, &super, rel, re2, type_equal)) == NULL)
    2951           1 :                         return NULL;
    2952             : 
    2953        2954 :                 return rel_compare_exp_(query, rel, le, re1, re2, 3, sc->token == SQL_NOT_BETWEEN ? 1 : 0, 0, f, symmetric, 0);
    2954             :         }
    2955       10601 :         case SQL_IS_NULL:
    2956             :         case SQL_IS_NOT_NULL:
    2957             :         /* is (NOT) NULL */
    2958             :         {
    2959       10601 :                 sql_exp *le = rel_value_exp(query, &rel, sc->data.sym, f|sql_farg, ek);
    2960       10601 :                 sql_subtype *t;
    2961             : 
    2962       10601 :                 if (!le)
    2963             :                         return NULL;
    2964       10597 :                 if (!(t = exp_subtype(le)))
    2965           1 :                         return sql_error(sql, 01, SQLSTATE(42000) "Cannot have a parameter (?) for IS%s NULL operator", sc->token == SQL_IS_NOT_NULL ? " NOT" : "");
    2966       10596 :                 le = exp_compare(sql->sa, le, exp_atom(sql->sa, atom_general(sql->sa, t, NULL, 0)), cmp_equal);
    2967       10596 :                 if (sc->token == SQL_IS_NOT_NULL)
    2968        2582 :                         set_anti(le);
    2969       10596 :                 set_has_no_nil(le);
    2970       10596 :                 set_semantics(le);
    2971       10596 :                 return rel_select_push_compare_exp_down(sql, rel, le, le->l, le->r, NULL, f);
    2972             :         }
    2973       20078 :         case SQL_NOT: {
    2974       20078 :                 if (not_symbol_can_be_propagated(sql, sc->data.sym)) {
    2975        2330 :                         sc->data.sym = negate_symbol_tree(sql, sc->data.sym);
    2976        2330 :                         return rel_logical_exp(query, rel, sc->data.sym, f);
    2977             :                 }
    2978       17748 :                 sql_exp *le = rel_value_exp(query, &rel, sc->data.sym, f|sql_farg, ek);
    2979       17748 :                 sql_subtype bt;
    2980             : 
    2981       17748 :                 sql_find_subtype(&bt, "boolean", 0, 0);
    2982       17748 :                 if (!le || !(le = exp_check_type(sql, &bt, rel, le, type_equal)))
    2983           1 :                         return NULL;
    2984       17747 :                 le = exp_compare(sql->sa, le, exp_atom_bool(sql->sa, 0), cmp_equal);
    2985       17747 :                 return rel_select_push_compare_exp_down(sql, rel, le, le->l, le->r, NULL, f);
    2986             :         }
    2987         627 :         case SQL_ATOM: {
    2988             :                 /* TRUE or FALSE */
    2989         627 :                 sql_rel *or = rel;
    2990         627 :                 AtomNode *an = (AtomNode *) sc;
    2991         627 :                 sql_exp *e = exp_atom(sql->sa, an->a);
    2992             : 
    2993         627 :                 if (e) {
    2994         627 :                         sql_subtype bt;
    2995             : 
    2996         627 :                         sql_find_subtype(&bt, "boolean", 0, 0);
    2997         627 :                         e = exp_check_type(sql, &bt, rel, e, type_equal);
    2998             :                 }
    2999         627 :                 if (!e || or != rel)
    3000           2 :                         return NULL;
    3001         625 :                 e = exp_compare(sql->sa, e, exp_atom_bool(sql->sa, 1), cmp_equal);
    3002         625 :                 return rel_select_push_compare_exp_down(sql, rel, e, e->l, e->r, NULL, f);
    3003             :         }
    3004         708 :         case SQL_IDENT:
    3005             :         case SQL_COLUMN: {
    3006         708 :                 sql_rel *or = rel;
    3007         708 :                 sql_exp *e = rel_column_ref(query, &rel, sc, f);
    3008             : 
    3009         708 :                 if (e) {
    3010         705 :                         sql_subtype bt;
    3011             : 
    3012         705 :                         sql_find_subtype(&bt, "boolean", 0, 0);
    3013         705 :                         e = exp_check_type(sql, &bt, rel, e, type_equal);
    3014             :                 }
    3015         705 :                 if (!e || or != rel)
    3016           3 :                         return NULL;
    3017         705 :                 e = exp_compare(sql->sa, e, exp_atom_bool(sql->sa, 1), cmp_equal);
    3018         705 :                 return rel_select_push_compare_exp_down(sql, rel, e, e->l, e->r, NULL, f);
    3019             :         }
    3020          12 :         case SQL_UNION:
    3021             :         case SQL_EXCEPT:
    3022             :         case SQL_INTERSECT: {
    3023          12 :                 sql_rel *sq;
    3024             : 
    3025          12 :                 if (is_psm_call(f) || is_sql_merge(f))
    3026           3 :                         return sql_error(sql, 02, SQLSTATE(42000) "%s: set operations not supported inside %s", is_psm_call(f) ? "CALL" : "MERGE", is_psm_call(f) ? "CALL statements" : "MERGE conditions");
    3027          11 :                 if (rel)
    3028          11 :                         query_push_outer(query, rel, f);
    3029          11 :                 sq = rel_setquery(query, sc);
    3030          11 :                 if (rel) {
    3031          11 :                         rel = query_pop_outer(query);
    3032          11 :                         if (is_sql_join(f) && is_groupby(rel->op)) {
    3033           0 :                                 return sql_error(sql, 05, SQLSTATE(42000) "SELECT: aggregate functions not allowed in JOIN conditions");
    3034          11 :                         } else if (is_sql_where(f) && is_groupby(rel->op)) {
    3035           0 :                                 return sql_error(sql, 02, SQLSTATE(42000) "SELECT: aggregate functions not allowed in WHERE clause");
    3036          11 :                         } else if ((is_sql_update_set(f) || is_sql_psm(f)) && is_groupby(rel->op)) {
    3037           0 :                                 return sql_error(sql, 02, SQLSTATE(42000) "SELECT: aggregate functions not allowed in SET, WHILE, IF, ELSE, CASE, WHEN, RETURN, ANALYZE clauses");
    3038             :                         }
    3039             :                 }
    3040          11 :                 if (!sq)
    3041             :                         return NULL;
    3042          11 :                 assert(ek.type == type_value); /* I don't expect IN tuple matching calls to land here */
    3043          11 :                 if (is_sql_where(f) && is_groupby(rel->op))
    3044           0 :                         assert(0);
    3045          11 :                 if (ek.card <= card_set && is_project(sq->op) && list_length(sq->exps) > 1)
    3046           1 :                         return sql_error(sql, 02, SQLSTATE(42000) "SELECT: subquery must return only one column");
    3047          10 :                 if (!rel)
    3048             :                         return sq;
    3049          10 :                 sq = rel_zero_or_one(sql, sq, ek);
    3050          10 :                 if (is_sql_where(f) || is_sql_having(f)) {
    3051          10 :                         sql_exp *le = exp_rel(sql, sq);
    3052          10 :                         sql_subtype bt;
    3053             : 
    3054          10 :                         sql_find_subtype(&bt, "boolean", 0, 0);
    3055          10 :                         le = exp_check_type(sql, &bt, rel, le, type_equal);
    3056          10 :                         if (!le)
    3057             :                                 return NULL;
    3058          10 :                         le = exp_compare(sql->sa, le, exp_atom_bool(sql->sa, 1), cmp_equal);
    3059          10 :                         return rel_select_push_compare_exp_down(sql, rel, le, le->l, le->r, NULL, f);
    3060             :                 } else {
    3061           0 :                         sq = rel_crossproduct(sql->sa, rel, sq, (f==sql_sel || is_single(sq))?op_left:op_join);
    3062           0 :                         set_processed(sq);
    3063             :                 }
    3064           0 :                 return sq;
    3065             :         }
    3066           0 :         case SQL_DEFAULT:
    3067           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "DEFAULT keyword not allowed outside insert and update statements");
    3068         327 :         default: {
    3069         327 :                 sql_exp *le = rel_value_exp(query, &rel, sc, f|sql_farg, ek);
    3070         327 :                 sql_subtype bt;
    3071             : 
    3072         327 :                 if (!le)
    3073             :                         return NULL;
    3074         302 :                 if (le && (!is_compare(le->type) || le->flag > cmp_filter)) {
    3075         288 :                         sql_find_subtype(&bt, "boolean", 0, 0);
    3076         288 :                         if (!(le = exp_check_type(sql, &bt, rel, le, type_equal)))
    3077             :                                 return NULL;
    3078         288 :                         le = exp_compare(sql->sa, le, exp_atom_bool(sql->sa, 1), cmp_equal);
    3079             :                 }
    3080         302 :                 if (le->flag == cmp_filter)
    3081          13 :                         return rel_select_push_filter_exp_down(sql, rel, le, le->l, le->r, f);
    3082             :                 else
    3083         289 :                         return rel_select_push_compare_exp_down(sql, rel, le, le->l, le->r, le->f, f);
    3084             :         }
    3085             :         }
    3086             :         /* never reached, as all switch cases have a `return` */
    3087             : }
    3088             : 
    3089             : static sql_exp * _rel_aggr(sql_query *query, sql_rel **rel, int distinct, char *sname, char *aname, dnode *arguments, int f);
    3090             : static sql_exp *rel_aggr(sql_query *query, sql_rel **rel, symbol *se, int f);
    3091             : 
    3092             : static sql_exp *
    3093        1827 : rel_op(sql_query *query, sql_rel **rel, symbol *se, int f, exp_kind ek )
    3094             : {
    3095        1827 :         mvc *sql = query->sql;
    3096        1827 :         dnode *l = se->data.lval->h;
    3097        1827 :         char *fname = qname_schema_object(l->data.lval);
    3098        1827 :         char *sname = qname_schema(l->data.lval);
    3099             : 
    3100        1827 :         if (find_func(sql, sname, fname, 0, F_AGGR, false, NULL, NULL))
    3101           0 :                 return _rel_aggr(query, rel, 0, sname, fname, NULL, f);
    3102        1827 :         sql->session->status = 0; /* if the function was not found clean the error */
    3103        1827 :         sql->errstr[0] = '\0';
    3104        1827 :         return rel_op_(sql, sname, fname, ek);
    3105             : }
    3106             : 
    3107             : sql_exp *
    3108       94698 : rel_unop_(mvc *sql, sql_rel *rel, sql_exp *e, char *sname, char *fname, int card)
    3109             : {
    3110       94698 :         bool found = false;
    3111       94698 :         sql_subtype *t = exp_subtype(e);
    3112       94698 :         sql_ftype type = (card == card_loader)?F_LOADER:((card == card_none)?F_PROC:
    3113             :                    ((card == card_relation)?F_UNION:F_FUNC));
    3114             : 
    3115       94698 :         sql_subfunc *f = bind_func(sql, sname, fname, t, NULL, 1, type, false, &found, false);
    3116       94698 :         if (f && !f->func->vararg) {
    3117       94626 :                 sql_arg *a = f->func->ops->h->data;
    3118       94626 :                 t = &a->type;
    3119             :         }
    3120       94698 :         if (f && t && type_has_tz(t) && f->func->fix_scale == SCALE_FIX) {
    3121             :                 /* set timezone (using msec (.3)) */
    3122           0 :                 sql_subtype *intsec = sql_bind_subtype(sql->sa, "sec_interval", 10 /*hour to second */, 3);
    3123           0 :                 atom *a = atom_int(sql->sa, intsec, sql->timezone);
    3124           0 :                 sql_exp *tz = exp_atom(sql->sa, a);
    3125             : 
    3126           0 :                 e = rel_binop_(sql, rel, e, tz, "sys", "sql_add", card, true);
    3127           0 :                 if (!e)
    3128             :                         return NULL;
    3129             :         }
    3130       94676 :         if (f) {
    3131       94627 :                 if (check_card(card, f)) {
    3132       94627 :                         list *args = list_append(sa_list(sql->sa), e);
    3133       94627 :                         if (!f->func->vararg)
    3134      189252 :                                 args = check_arguments_and_find_largest_any_type(sql, rel, args, f, card == card_relation && e->card > CARD_ATOM, false);
    3135       94627 :                         if (args)
    3136       94627 :                                 return exp_op(sql->sa, args, f);
    3137             :                 }
    3138           0 :                 found = false; /* reset found */
    3139           0 :                 f = NULL;
    3140             :         }
    3141             :         /* reset error */
    3142          71 :         sql->session->status = 0;
    3143          71 :         sql->errstr[0] = '\0';
    3144         212 :         return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "SELECT: %s unary operator %s%s%s'%s'(%s)",
    3145         141 :                                          found ? "insufficient privileges for" : "no such", sname ? "'":"", sname ? sname : "", sname ? "'.":"", fname, t ? t->type->base.name : "?");
    3146             : }
    3147             : 
    3148             : static sql_exp *
    3149       77657 : rel_unop(sql_query *query, sql_rel **rel, symbol *se, int f, exp_kind ek)
    3150             : {
    3151       77657 :         mvc *sql = query->sql;
    3152       77657 :         dnode *l = se->data.lval->h;
    3153       77657 :         char *fname = qname_schema_object(l->data.lval);
    3154       77657 :         char *sname = qname_schema(l->data.lval);
    3155       77657 :         exp_kind iek = {type_value, card_column, FALSE};
    3156       77657 :         sql_exp *e = NULL;
    3157             : 
    3158       77657 :         if (find_func(sql, sname, fname, 1, F_AGGR, false, NULL, NULL))
    3159       18824 :                 return rel_aggr(query, rel, se, f);
    3160             : 
    3161       58833 :         sql->session->status = 0; /* if the function was not found clean the error */
    3162       58833 :         sql->errstr[0] = '\0';
    3163       58833 :         if (!(e = rel_value_exp(query, rel, l->next->next->data.sym, f|sql_farg, iek)))
    3164             :                 return NULL;
    3165       58819 :         return rel_unop_(sql, rel ? *rel : NULL, e, sname, fname, ek.card);
    3166             : }
    3167             : 
    3168             : sql_exp *
    3169      522736 : rel_binop_(mvc *sql, sql_rel *rel, sql_exp *l, sql_exp *r, char *sname, char *fname, int card, bool exact)
    3170             : {
    3171      522736 :         sql_subtype *t1 = exp_subtype(l), *t2 = exp_subtype(r);
    3172      522736 :         sql_ftype type = (card == card_loader)?F_LOADER:((card == card_none)?F_PROC:((card == card_relation)?F_UNION:F_FUNC));
    3173      522736 :         bool found = false;
    3174             : 
    3175      522736 :         if (card == card_loader)
    3176           0 :                 card = card_none;
    3177             : 
    3178      522736 :         if (is_commutative(sname, fname) && l->card < r->card) { /* move constants to the right if possible */
    3179             :                 sql_subtype *tmp = t1;
    3180      522736 :                 t1 = t2;
    3181      522736 :                 t2 = tmp;
    3182             :                 sql_exp *res = l;
    3183      522736 :                 l = r;
    3184      522736 :                 r = res;
    3185             :         }
    3186             :         /* swap complex types (intervals) to left side of +, * */
    3187      522736 :         if (t1 && t2 && is_commutative(sname, fname)) {
    3188      121355 :                 if ((EC_INTERVAL(t1->type->eclass) && EC_TEMP(t2->type->eclass)) ||
    3189      121353 :            ((!EC_TEMP(t1->type->eclass) && !EC_INTERVAL(t1->type->eclass)) && EC_INTERVAL(t2->type->eclass))) {
    3190             :                         sql_subtype *tmp = t1;
    3191      522736 :                         t1 = t2;
    3192      522736 :                         t2 = tmp;
    3193             :                         sql_exp *res = l;
    3194      522736 :                         l = r;
    3195      522736 :                         r = res;
    3196             :                 }
    3197             :         }
    3198             : 
    3199      522736 :         sql_subfunc *f = bind_func(sql, sname, fname, t1, t2, 2, type, false, &found, exact);
    3200      522736 :         if (f && check_card(card,f)) {
    3201      522574 :                 t1 = exp_subtype(l);
    3202      522574 :                 t2 = exp_subtype(r);
    3203      522574 :                 list *args = list_append(list_append(sa_list(sql->sa), l), r);
    3204      522574 :                 if (!f->func->vararg)
    3205      522573 :                         args = check_arguments_and_find_largest_any_type(sql, rel, args, f, 0, false);
    3206      522574 :                 if (args)
    3207      522572 :                                 return exp_op(sql->sa, args, f);
    3208             :                 return NULL;
    3209             :         }
    3210             : 
    3211         162 :         if (!exp_subtype(l) || !exp_subtype(r))
    3212           0 :                 return sql_error(sql, 01, SQLSTATE(42000) "Cannot have a parameter (?) on both sides of an expression");
    3213         162 :         return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "SELECT: %s binary operator %s%s%s'%s'(%s,%s)",
    3214         162 :                                          found ? "insufficient privileges for" : "no such", sname ? "'":"", sname ? sname : "", sname ? "'.":"", fname,
    3215         162 :                                          exp_subtype(l)->type->base.name, exp_subtype(r)->type->base.name);
    3216             : }
    3217             : 
    3218             : static sql_exp *
    3219      373954 : rel_binop(sql_query *query, sql_rel **rel, symbol *se, int f, exp_kind ek)
    3220             : {
    3221      373954 :         mvc *sql = query->sql;
    3222      373954 :         dnode *dl = se->data.lval->h;
    3223      373954 :         sql_exp *l, *r;
    3224      373954 :         char *fname = qname_schema_object(dl->data.lval);
    3225      373954 :         char *sname = qname_schema(dl->data.lval);
    3226      373954 :         exp_kind iek = {type_value, card_column, FALSE};
    3227             : 
    3228      373954 :         if (find_func(sql, sname, fname, 2, F_AGGR, false, NULL, NULL))
    3229        4190 :                 return rel_aggr(query, rel, se, f);
    3230             : 
    3231      369764 :         sql->session->status = 0; /* if the function was not found clean the error */
    3232      369764 :         sql->errstr[0] = '\0';
    3233      369764 :         if (!(l = rel_value_exp(query, rel, dl->next->next->data.sym, f|sql_farg, iek)))
    3234             :                 return NULL;
    3235      369735 :         if (!(r = rel_value_exp(query, rel, dl->next->next->next->data.sym, f|sql_farg, iek)))
    3236             :                 return NULL;
    3237      369696 :         return rel_binop_(sql, rel ? *rel : NULL, l, r, sname, fname, ek.card, false);
    3238             : }
    3239             : 
    3240             : sql_exp *
    3241         530 : rel_nop_(mvc *sql, sql_rel *rel, sql_exp *a1, sql_exp *a2, sql_exp *a3, sql_exp *a4, char *sname, char *fname, int card)
    3242             : {
    3243         530 :         list *tl = sa_list(sql->sa);
    3244         530 :         sql_subfunc *f = NULL;
    3245         530 :         sql_ftype type = (card == card_none)?F_PROC:((card == card_relation)?F_UNION:F_FUNC);
    3246             : 
    3247             :         /* rel_nop_ should only be called for functions available to everyone, ie defined at sql_types! */
    3248         530 :         (void) rel;
    3249         530 :         append(tl, exp_subtype(a1));
    3250         530 :         append(tl, exp_subtype(a2));
    3251         530 :         append(tl, exp_subtype(a3));
    3252         530 :         if (a4)
    3253         371 :                 append(tl, exp_subtype(a4));
    3254             : 
    3255         530 :         if (!(f = bind_func_(sql, sname, fname, tl, type, false, NULL, true)))
    3256             :                 return NULL;
    3257         530 :         if (!a4)
    3258         159 :                 return exp_op3(sql->sa, a1,a2,a3,f);
    3259         371 :         return exp_op4(sql->sa, a1,a2,a3,a4,f);
    3260             : }
    3261             : 
    3262             : static sql_func *
    3263           3 : inplace_func(mvc *sql)
    3264             : {
    3265           3 :         sql_func *f = SA_NEW(sql->sa, sql_func);
    3266             : 
    3267           3 :     *f = (sql_func) {
    3268             :         .mod = "",
    3269             :         .imp = "",
    3270             :         .type = F_PROC,
    3271             :         .lang = FUNC_LANG_INT,
    3272             :         .query = NULL,
    3273           3 :         .ops = sql->params,
    3274             :         .res = NULL,
    3275             :     };
    3276           3 :     base_init(sql->sa, &f->base, 0, true, NULL);
    3277           3 :     f->base.new = 1;
    3278           3 :     f->base.id = -1;
    3279           3 :     f->base.name = "-1";
    3280           3 :     f->instantiated = TRUE;
    3281           3 :         return f;
    3282             : }
    3283             : 
    3284             : static list *
    3285           3 : reorder_args(mvc *sql, list *exps, list *names, list *params)
    3286             : {
    3287           3 :         list *nexps = sa_list(sql->sa);
    3288           8 :         for(node *n = params->h; n; n = n->next) {
    3289           5 :                 sql_arg *a = n->data;
    3290           5 :                 int found =0;
    3291           7 :                 for(node *m = names->h, *o = exps->h; m && o; m = m->next, o = o->next) {
    3292           7 :                         if (strcmp(m->data, a->name) == 0) {
    3293           5 :                                 append(nexps, o->data);
    3294           5 :                                 found = 1;
    3295           5 :                                 break;
    3296             :                         }
    3297             :                 }
    3298           5 :                 if (!found)
    3299             :                         return NULL;
    3300             :         }
    3301             :         return nexps;
    3302             : }
    3303             : 
    3304             : static sql_exp *
    3305       84771 : rel_nop(sql_query *query, sql_rel **rel, symbol *se, int fs, exp_kind ek)
    3306             : {
    3307       84771 :         mvc *sql = query->sql;
    3308       84771 :         int nr_args = 0, err = 0;
    3309       84771 :         dnode *l = se->data.lval->h;
    3310       84771 :         dnode *ops = l->next->next->data.lval?l->next->next->data.lval->h:NULL;
    3311       84771 :         list *exps = sa_list(sql->sa), *tl = sa_list(sql->sa);
    3312       84771 :         exp_kind iek = {type_value, card_column, FALSE};
    3313       84771 :         char buf[ERRSIZE];
    3314       84771 :         int split = (l->type == type_int && l->data.i_val == -1);
    3315       84771 :         list *names = NULL;
    3316             : 
    3317           3 :         if (split)
    3318           3 :                 names = sa_list(sql->sa);
    3319      348682 :         for (; ops; ops = ops->next, nr_args++) {
    3320      263911 :                 if (!err) { /* we need the nr_args count at the end, but if an error is found, stop calling rel_value_exp */
    3321      263889 :                         sql_exp *e = rel_value_exp(query, rel, ops->data.sym, fs|sql_farg, iek);
    3322      263889 :                         if (!e) {
    3323           6 :                                 err = sql->session->status;
    3324           6 :                                 strcpy(buf, sql->errstr);
    3325           6 :                                 continue;
    3326             :                         }
    3327      263883 :                         if (split) {
    3328           5 :                                 ops = ops->next;
    3329           5 :                                 append(names, ops->data.sval);
    3330             :                         }
    3331      263883 :                         append(exps, e);
    3332      263883 :                         append(tl, exp_subtype(e));
    3333             :                 }
    3334             :         }
    3335       84771 :         if (l->type == type_int) {
    3336             :                 /* exec nr (ops)*/
    3337        5909 :                 int nr = l->data.i_val;
    3338        5909 :                 cq *q = NULL;
    3339             : 
    3340        5909 :                 if (err)
    3341             :                         return NULL;
    3342        5908 :                 if (nr == -1 || (q = qc_find(sql->qc, nr))) {
    3343        5906 :                         list *nexps = new_exp_list(sql->sa);
    3344        5906 :                         sql_func *f = q?q->f:inplace_func(sql);
    3345        5906 :                         list *ops = q?f->ops:sql->params;
    3346             : 
    3347        5906 :                         tl = sa_list(sql->sa);
    3348        5906 :                         if (list_length(ops) != list_length(exps))
    3349           3 :                                 return sql_error(sql, 02, SQLSTATE(42000) "EXEC called with wrong number of arguments: expected %d, got %d", list_length(ops), list_length(exps));
    3350        5903 :                         if (split) {
    3351           3 :                                 exps = reorder_args(sql, exps, names, ops);
    3352           3 :                                 if (!exps)
    3353           0 :                                         return sql_error(sql, 02, SQLSTATE(42000) "EXEC called with wrong arguments");
    3354             :                         }
    3355        5903 :                         if (exps->h && ops) {
    3356       21927 :                                 for (node *n = exps->h, *m = ops->h; n && m; n = n->next, m = m->next) {
    3357       16115 :                                         sql_arg *a = m->data;
    3358       16115 :                                         sql_exp *e = n->data;
    3359       16115 :                                         sql_subtype *ntp = &a->type;
    3360             : 
    3361       16115 :                                         if (ntp && ntp->type)
    3362       16115 :                                                 e = exp_check_type(sql, ntp, NULL, e, type_equal);
    3363             :                                         else
    3364           0 :                                                 a->type = *exp_subtype(e);
    3365       16115 :                                         if (!e) {
    3366           1 :                                                 err = sql->session->status;
    3367           1 :                                                 strcpy(buf, sql->errstr);
    3368           1 :                                                 break;
    3369             :                                         }
    3370       16114 :                                         append(nexps, e);
    3371       16114 :                                         append(tl, exp_subtype(e));
    3372             :                                 }
    3373             :                         }
    3374             : 
    3375        5903 :                         if (err)
    3376             :                                 return NULL;
    3377        5902 :                         if (q)
    3378        5899 :                                 sql->type = q->type;
    3379        5992 :                         return exp_op(sql->sa, list_empty(nexps) ? NULL : nexps, sql_dup_subfunc(sql->sa, f, tl, NULL));
    3380             :                 } else {
    3381           2 :                         return sql_error(sql, 02, SQLSTATE(42000) "EXEC: PREPARED Statement missing '%d'", nr);
    3382             :                 }
    3383             :         }
    3384       78862 :         char *fname = qname_schema_object(l->data.lval);
    3385       78862 :         char *sname = qname_schema(l->data.lval);
    3386             : 
    3387       78862 :         if (!sname && strcmp(fname, "field") == 0) { /* map into join */
    3388           2 :                 if (err)
    3389           2 :                         return NULL;
    3390           1 :                 if (list_length(exps) < 2)
    3391           1 :                         return sql_error(sql, 02, SQLSTATE(42000) "Field function called with not enough arguments");
    3392           0 :                 sql_exp *le = exps->h->data;
    3393           0 :                 set_freevar(le, 1);
    3394           0 :                 list_remove_data(exps, NULL, le);
    3395           0 :                 sql_exp *re = exp_values(sql->sa, exps);
    3396           0 :                 exp_label(sql->sa, re, ++sql->label);
    3397           0 :                 sql_rel *r = rel_project(sql->sa, NULL, append(sa_list(sql->sa), re));
    3398           0 :                 sql_exp *id = NULL;
    3399           0 :                 rel_add_identity(sql, r, &id);
    3400           0 :                 re = exp_ref(sql, re);
    3401           0 :                 id = exp_ref(sql, id);
    3402           0 :                 if (r) {
    3403           0 :                         r->nrcols = list_length(exps);
    3404           0 :                         sql_exp *e = exp_compare(sql->sa, le, re, cmp_equal);
    3405           0 :                         r = rel_select(sql->sa, r, e);
    3406           0 :                         r = rel_project(sql->sa, r, append(sa_list(sql->sa), exp_convert(sql, id, exp_subtype(id), sql_bind_localtype("int"))));
    3407           0 :                         re = exp_rel(sql, r);
    3408           0 :                         return re;
    3409             :                 }
    3410             :         }
    3411             :         /* first try aggregate */
    3412       78860 :         if (find_func(sql, sname, fname, nr_args, F_AGGR, false, NULL, NULL)) { /* We have to pass the arguments properly, so skip call to rel_aggr */
    3413             :                 /* reset error */
    3414           2 :                 sql->session->status = 0;
    3415           2 :                 sql->errstr[0] = '\0';
    3416           2 :                 return _rel_aggr(query, rel, l->next->data.i_val, sname, fname, l->next->next->data.lval->h, fs);
    3417             :         }
    3418       78858 :         if (err) {
    3419           2 :                 sql->session->status = err;
    3420           2 :                 strcpy(sql->errstr, buf);
    3421           2 :                 return NULL;
    3422             :         }
    3423       78856 :         sql->session->status = 0; /* if the function was not found clean the error */
    3424       78856 :         sql->errstr[0] = '\0';
    3425       78856 :         return _rel_nop(sql, sname, fname, tl, rel ? *rel : NULL, exps, ek);
    3426             : }
    3427             : 
    3428             : typedef struct aggr_input {
    3429             :         sql_query *query;
    3430             :         int groupby;
    3431             :         char *err;
    3432             : } aggr_input;
    3433             : 
    3434             : static sql_exp *
    3435         372 : exp_valid(visitor *v, sql_rel *rel, sql_exp *e, int depth)
    3436             : {
    3437         372 :         aggr_input *ai = v->data;
    3438         372 :         (void)rel; (void)depth;
    3439             : 
    3440         372 :         int vf = is_freevar(e);
    3441         372 :         if (!v->changes && vf && vf < ai->groupby) { /* check need with outer query */
    3442          14 :                 sql_rel *sq = query_fetch_outer(ai->query, vf-1);
    3443             : 
    3444             :                 /* problem freevar have cardinality CARD_ATOM */
    3445          14 :                 if (sq->card <= CARD_AGGR && exp_card(e) != CARD_AGGR && is_alias(e->type)) {
    3446           4 :                         if (!exps_bind_column(sq->exps, e->l, e->r, NULL, 0)) {
    3447           1 :                                 v->changes = 1;
    3448           1 :                                 ai->err = SQLSTATE(42000) "SELECT: subquery uses ungrouped column from outer query";
    3449             :                         }
    3450             :                 }
    3451         358 :         } else if (!v->changes && vf && vf == ai->groupby) { /* check if input is already aggregated */
    3452         279 :                 sql_rel *sq = query_fetch_outer(ai->query, vf-1);
    3453         279 :                 sql_exp *a = NULL;
    3454             : 
    3455         279 :                 if (sq->card <= CARD_AGGR && is_alias(e->type)) {
    3456         187 :                         if ((a = exps_bind_column(sq->exps, e->l, e->r, NULL, 0)) && is_aggr(a->type)) { /* aggregate */
    3457           9 :                                 v->changes = 1;
    3458           9 :                                 ai->err = SQLSTATE(42000) "SELECT: aggregate function calls cannot be nested";
    3459             :                         }
    3460             :                 }
    3461             :         }
    3462         372 :         return e;
    3463             : }
    3464             : 
    3465             : static char *
    3466         272 : exps_valid(sql_query *query, list *exps, int groupby)
    3467             : {
    3468         272 :         aggr_input ai = { .query = query, .groupby = groupby };
    3469         272 :         visitor v = { .sql = query->sql, .data = &ai };
    3470             : 
    3471         272 :         exps_exp_visitor_topdown(&v, NULL, exps, 0, &exp_valid, true);
    3472         272 :         if (v.changes)
    3473          10 :                 return ai.err;
    3474             :         return NULL;
    3475             : }
    3476             : 
    3477             : static sql_exp *
    3478       41742 : _rel_aggr(sql_query *query, sql_rel **rel, int distinct, char *sname, char *aname, dnode *args, int f)
    3479             : {
    3480       41742 :         mvc *sql = query->sql;
    3481       41742 :         exp_kind ek = {type_value, card_column, FALSE};
    3482       41742 :         sql_subfunc *a = NULL;
    3483       41742 :         int no_nil = 0, group = 0, all_aggr = query_has_outer(query), i;
    3484       41742 :         unsigned int all_freevar = 0;
    3485       41742 :         sql_rel *groupby = rel ? *rel : NULL, *sel = NULL, *gr, *og = NULL, *res = groupby;
    3486       41742 :         sql_rel *subquery = NULL;
    3487       41742 :         list *exps = NULL, *ungrouped_cols = NULL;
    3488       41742 :         bool is_grouping = !strcmp(aname, "grouping"), has_args = false, found = false, used_rel = false;
    3489             : 
    3490       41742 :         if (!all_aggr) {
    3491       27513 :                 if (!groupby) {
    3492           4 :                         char *uaname = SA_NEW_ARRAY(sql->ta, char, strlen(aname) + 1);
    3493           4 :                         return sql_error(sql, 02, SQLSTATE(42000) "%s: missing group by", toUpperCopy(uaname, aname));
    3494       27509 :                 } else if (is_sql_groupby(f)) {
    3495           8 :                         char *uaname = SA_NEW_ARRAY(sql->ta, char, strlen(aname) + 1);
    3496           8 :                         return sql_error(sql, 02, SQLSTATE(42000) "%s: aggregate function '%s' not allowed in GROUP BY clause", toUpperCopy(uaname, aname), aname);
    3497       27501 :                 } else if (is_sql_values(f)) {
    3498           1 :                         char *uaname = SA_NEW_ARRAY(sql->ta, char, strlen(aname) + 1);
    3499           1 :                         return sql_error(sql, 02, SQLSTATE(42000) "%s: aggregate functions not allowed on an unique value", toUpperCopy(uaname, aname));
    3500       27500 :                 } else if (is_sql_join(f)) { /* the is_sql_join test must come before is_sql_where, because the join conditions are handled with sql_where */
    3501           3 :                         char *uaname = SA_NEW_ARRAY(sql->ta, char, strlen(aname) + 1);
    3502           3 :                         return sql_error(sql, 02, SQLSTATE(42000) "%s: aggregate functions not allowed in JOIN conditions", toUpperCopy(uaname, aname));
    3503       27497 :                 } else if (is_sql_where(f)) {
    3504           4 :                         char *uaname = SA_NEW_ARRAY(sql->ta, char, strlen(aname) + 1);
    3505           4 :                         return sql_error(sql, 02, SQLSTATE(42000) "%s: aggregate functions not allowed in WHERE clause", toUpperCopy(uaname, aname));
    3506       27493 :                 } else if (is_sql_update_set(f) || is_sql_psm(f)) {
    3507           1 :                         char *uaname = SA_NEW_ARRAY(sql->ta, char, strlen(aname) + 1);
    3508           1 :                         return sql_error(sql, 02, SQLSTATE(42000) "%s: aggregate functions not allowed in SET, WHILE, IF, ELSE, CASE, WHEN, RETURN, ANALYZE clauses (use subquery)", toUpperCopy(uaname, aname));
    3509       27492 :                 } else if (is_sql_aggr(f)) {
    3510           4 :                         char *uaname = SA_NEW_ARRAY(sql->ta, char, strlen(aname) + 1);
    3511           4 :                         return sql_error(sql, 02, SQLSTATE(42000) "%s: aggregate functions cannot be nested", toUpperCopy(uaname, aname));
    3512       27488 :                 } else if (is_psm_call(f)) {
    3513           0 :                         char *uaname = SA_NEW_ARRAY(sql->ta, char, strlen(aname) + 1);
    3514           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "%s: aggregate functions not allowed inside CALL", toUpperCopy(uaname, aname));
    3515       27488 :                 } else if (is_sql_from(f)) {
    3516           0 :                         char *uaname = SA_NEW_ARRAY(sql->ta, char, strlen(aname) + 1);
    3517           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "%s: aggregate functions not allowed in functions in FROM", toUpperCopy(uaname, aname));
    3518             :                 }
    3519             :         }
    3520             : 
    3521       41717 :         exps = sa_list(sql->sa);
    3522       41717 :         if (args && args->data.sym) {
    3523       23106 :                 bool arguments_correlated = true, all_const = true;
    3524             : 
    3525       23106 :                 all_freevar = all_aggr?1:0;
    3526       50424 :                 for (i = 0; args && args->data.sym; args = args->next, i++) {
    3527       27375 :                         int base = (!groupby || !is_project(groupby->op) || is_base(groupby->op) || is_processed(groupby));
    3528       19508 :                         sql_rel *gl = base?groupby:groupby->l, *ogl = gl; /* handle case of subqueries without correlation */
    3529       27375 :                         sql_exp *e = rel_value_exp(query, &gl, args->data.sym, (f | sql_aggr)& ~sql_farg, ek);
    3530       27375 :                         bool found_one_freevar = false;
    3531             : 
    3532       27375 :                         if (!e)
    3533          57 :                                 return NULL;
    3534       27324 :                         used_rel |= (rel_has_exp(gl, e, true) == 0);
    3535       27324 :                         has_args = true;
    3536       27324 :                         if (gl && gl != ogl) {
    3537           5 :                                 if (gl->grouped) {
    3538           5 :                                         char *uaname = SA_NEW_ARRAY(sql->ta, char, strlen(aname) + 1);
    3539           5 :                                         return sql_error(sql, 02, SQLSTATE(42000) "%s: aggregate functions cannot be nested", toUpperCopy(uaname, aname));
    3540             :                                 }
    3541           0 :                                 if (!base)
    3542           0 :                                         groupby->l = subquery = gl;
    3543             :                                 else
    3544             :                                         groupby = subquery = gl;
    3545             :                         }
    3546       27319 :                         sql_subtype *t = exp_subtype(e);
    3547       27319 :                         if (!t) { /* we also do not expect parameters here */
    3548           1 :                                 char *uaname = SA_NEW_ARRAY(sql->ta, char, strlen(aname) + 1);
    3549           1 :                                 return sql_error(sql, 02, SQLSTATE(42000) "%s: parameters not allowed as arguments to aggregate functions", toUpperCopy(uaname, aname));
    3550             :                         }
    3551       27318 :                         if (!t->type->localtype) {
    3552          47 :                                 if (e->type == e_atom && !e->f) {
    3553          44 :                                         t = sql_bind_localtype("bte");
    3554          44 :                                         e->tpe = *t;
    3555          44 :                                         if (e->l)
    3556          44 :                                                 e->l = atom_set_type(sql->sa, e->l, t);
    3557             :                                 }
    3558             :                         }
    3559             : 
    3560       27318 :                         all_aggr &= (exp_card(e) <= CARD_AGGR && !exp_is_atom(e) && is_aggr(e->type) && !is_func(e->type) && (!groupby || !is_groupby(groupby->op) || !groupby->r || !exps_find_exp(groupby->r, e)));
    3561       27318 :                         exp_only_freevar(query, e, &arguments_correlated, &found_one_freevar, &ungrouped_cols);
    3562       27318 :                         all_freevar &= (arguments_correlated && found_one_freevar) || (is_atom(e->type)?all_freevar:0); /* no uncorrelated variables must be found, plus at least one correlated variable to push this aggregate to an outer query */
    3563       27318 :                         all_const &= is_atom(e->type);
    3564       27318 :                         list_append(exps, e);
    3565             :                 }
    3566       23049 :                 if (all_const)
    3567         203 :                         all_freevar = 0;
    3568             :         }
    3569       41660 :         if (!all_freevar) {
    3570       41368 :                 if (is_sql_groupby(f)) {
    3571           1 :                         char *uaname = SA_NEW_ARRAY(sql->ta, char, strlen(aname) + 1);
    3572           1 :                         return sql_error(sql, 02, SQLSTATE(42000) "%s: aggregate function '%s' not allowed in GROUP BY clause", toUpperCopy(uaname, aname), aname);
    3573       41367 :                 } else if (is_sql_from(f)) {
    3574           2 :                         char *uaname = SA_NEW_ARRAY(sql->ta, char, strlen(aname) + 1);
    3575           2 :                         return sql_error(sql, 02, SQLSTATE(42000) "%s: aggregate functions not allowed in functions in FROM", toUpperCopy(uaname, aname));
    3576       41365 :                 } else if (is_sql_aggr(f) && groupby->grouped) {
    3577           2 :                         char *uaname = SA_NEW_ARRAY(sql->ta, char, strlen(aname) + 1);
    3578           2 :                         return sql_error(sql, 02, SQLSTATE(42000) "%s: aggregate functions cannot be nested", toUpperCopy(uaname, aname));
    3579       41363 :                 } else if (is_sql_values(f)) {
    3580           1 :                         char *uaname = SA_NEW_ARRAY(sql->ta, char, strlen(aname) + 1);
    3581           1 :                         return sql_error(sql, 02, SQLSTATE(42000) "%s: aggregate functions not allowed on an unique value", toUpperCopy(uaname, aname));
    3582       41362 :                 } else if (is_sql_join(f)) { /* the is_sql_join test must come before is_sql_where, because the join conditions are handled with sql_where */
    3583           1 :                         char *uaname = SA_NEW_ARRAY(sql->ta, char, strlen(aname) + 1);
    3584           1 :                         return sql_error(sql, 02, SQLSTATE(42000) "%s: aggregate functions not allowed in JOIN conditions", toUpperCopy(uaname, aname));
    3585       41361 :                 } else if (is_sql_where(f)) {
    3586           5 :                         char *uaname = SA_NEW_ARRAY(sql->ta, char, strlen(aname) + 1);
    3587           5 :                         return sql_error(sql, 02, SQLSTATE(42000) "%s: aggregate functions not allowed in WHERE clause", toUpperCopy(uaname, aname));
    3588       41356 :                 } else if (!all_aggr && !list_empty(ungrouped_cols)) {
    3589         123 :                         for (node *n = ungrouped_cols->h ; n ; n = n->next) {
    3590          66 :                                 sql_rel *outer;
    3591          66 :                                 sql_exp *e = (sql_exp*) n->data;
    3592             : 
    3593          66 :                                 if ((outer = query_fetch_outer(query, is_freevar(e)-1))) {
    3594          66 :                                         int of = query_fetch_outer_state(query, is_freevar(e)-1);
    3595          66 :                                         if (outer->grouped) {
    3596          24 :                                                 bool err = false, was_processed = false;
    3597             : 
    3598          24 :                                                 if (is_processed(outer)) {
    3599           5 :                                                         was_processed = true;
    3600           5 :                                                         reset_processed(outer);
    3601             :                                                 }
    3602          24 :                                                 if (!is_groupby_col(outer, e))
    3603           3 :                                                         err = true;
    3604          24 :                                                 if (was_processed)
    3605           5 :                                                         set_processed(outer);
    3606          24 :                                                 if (err) {
    3607           3 :                                                         if (exp_name(e) && exp_relname(e) && !has_label(e))
    3608           3 :                                                                 return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: subquery uses ungrouped column \"%s.%s\" from outer query", exp_relname(e), exp_name(e));
    3609           0 :                                                         return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: subquery uses ungrouped column from outer query");
    3610             :                                                 }
    3611          42 :                                         } else if (!used_rel && is_sql_where(of)) {
    3612           2 :                                                 char *uaname = SA_NEW_ARRAY(sql->ta, char, strlen(aname) + 1);
    3613           2 :                                                 return sql_error(sql, 02, SQLSTATE(42000) "%s: aggregate functions not allowed in WHERE clause", toUpperCopy(uaname, aname));
    3614          40 :                                         } else if (!is_sql_aggr(of)) {
    3615          36 :                                                 set_outer(outer);
    3616             :                                         }
    3617             :                                 }
    3618             :                         }
    3619             :                 }
    3620             :         }
    3621             : 
    3622       41643 :         if (all_freevar) { /* case 2, ie use outer */
    3623         292 :                 int card;
    3624         292 :                 sql_exp *exp = NULL;
    3625             :                 /* find proper groupby relation */
    3626         591 :                 for (node *n = exps->h; n; n = n->next) {
    3627         299 :                         sql_exp *e = n->data;
    3628             : 
    3629         299 :                         int vf = exp_freevar_offset(sql, e);
    3630         299 :                         if (vf > (int)all_freevar)
    3631          23 :                                 all_freevar = vf;
    3632         299 :                         exp = e;
    3633             :                 }
    3634         292 :                 if (query_has_outer(query) >= all_freevar) {
    3635         292 :                         int sql_state = query_fetch_outer_state(query,all_freevar-1);
    3636         292 :                         res = groupby = query_fetch_outer(query, all_freevar-1);
    3637         292 :                         card = query_outer_used_card(query, all_freevar-1);
    3638             :                         /* given groupby validate all input expressions */
    3639         292 :                         char *err;
    3640         292 :                         if (groupby && !is_groupby(groupby->op)) {
    3641         111 :                                 sql_exp *p = query_outer_last_used(query, all_freevar-1);
    3642         111 :                                 if (p && !is_aggr(p->type) && !is_groupby_col(groupby, p)) {
    3643          20 :                                         if (p->type == e_column)
    3644          20 :                                                 return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column '%s.%s' in query results without an aggregate function", (char*)p->l, (char*)p->r);
    3645           0 :                                         if (exp_name(p) && exp_relname(p) && !has_label(p))
    3646           0 :                                                 return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column '%s.%s' in query results without an aggregate function", exp_relname(p), exp_name(p));
    3647           0 :                                         return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column in query results without an aggregate function");
    3648             :                                 }
    3649             :                         }
    3650         272 :                         if ((err = exps_valid(query, exps, all_freevar)) != NULL) {
    3651          10 :                                 strcpy(sql->errstr, err);
    3652          10 :                                 sql->session->status = -ERR_GROUPBY;
    3653          10 :                                 return NULL;
    3654             :                         }
    3655         262 :                         if (exp && !is_groupby_col(res, exp)) {
    3656         191 :                                 if (is_sql_groupby(sql_state))
    3657           2 :                                         return sql_error(sql, 05, SQLSTATE(42000) "SELECT: aggregate function '%s' not allowed in GROUP BY clause", aname);
    3658         189 :                                 if (is_sql_aggr(sql_state) && groupby->grouped) {
    3659           3 :                                         char *uaname = SA_NEW_ARRAY(sql->ta, char, strlen(aname) + 1);
    3660           3 :                                         return sql_error(sql, 02, SQLSTATE(42000) "%s: aggregate functions cannot be nested", toUpperCopy(uaname, aname));
    3661             :                                 }
    3662         186 :                                 if (is_sql_values(sql_state))
    3663           0 :                                         return sql_error(sql, 05, SQLSTATE(42000) "SELECT: aggregate functions not allowed on an unique value");
    3664         186 :                                 if (is_sql_update_set(sql_state) || is_sql_psm(f))
    3665           2 :                                         return sql_error(sql, 05, SQLSTATE(42000) "SELECT: aggregate functions not allowed in SET, WHILE, IF, ELSE, CASE, WHEN, RETURN, ANALYZE clauses");
    3666         184 :                                 if (is_sql_join(sql_state))
    3667           4 :                                         return sql_error(sql, 05, SQLSTATE(42000) "SELECT: aggregate functions not allowed in JOIN conditions");
    3668         180 :                                 if (is_sql_where(sql_state))
    3669           3 :                                         return sql_error(sql, 05, SQLSTATE(42000) "SELECT: aggregate functions not allowed in WHERE clause");
    3670         177 :                                 if (is_psm_call(sql_state))
    3671           0 :                                         return sql_error(sql, 05, SQLSTATE(42000) "CALL: aggregate functions not allowed inside CALL");
    3672         177 :                                 if (is_sql_from(sql_state))
    3673           0 :                                         return sql_error(sql, 05, SQLSTATE(42000) "SELECT: aggregate functions not allowed in functions in FROM");
    3674         177 :                                 if (card > CARD_AGGR) { /* used an expression before on the non grouped relation */
    3675           0 :                                         sql_exp *lu = query_outer_last_used(query, all_freevar-1);
    3676           0 :                                         if (lu->type == e_column)
    3677           0 :                                                 return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: subquery uses ungrouped column \"%s.%s\" from outer query", (char*)lu->l, (char*)lu->r);
    3678           0 :                                         if (exp_name(lu) && exp_relname(lu) && !has_label(lu))
    3679           0 :                                                 return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: subquery uses ungrouped column \"%s.%s\" from outer query", exp_relname(lu), exp_name(lu));
    3680           0 :                                         return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: subquery uses ungrouped column from outer query");
    3681             :                                 }
    3682         177 :                                 if (is_outer(groupby))
    3683           2 :                                         return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: subquery uses ungrouped column from outer query");
    3684             :                         }
    3685             :                 } else {
    3686             :                         all_freevar = 0;
    3687             :                 }
    3688       41351 :         } else if (!subquery && groupby && is_outer(groupby) && !is_groupby(groupby->op))
    3689           1 :                 return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: subquery uses ungrouped column from outer query");
    3690             : 
    3691             :         /* find having select */
    3692       41596 :         if (!subquery && groupby && !is_processed(groupby) && is_sql_having(f)) {
    3693         104 :                 og = groupby;
    3694         104 :                 while(!is_processed(groupby) && !is_base(groupby->op)) {
    3695          59 :                         if (is_select(groupby->op) || !groupby->l)
    3696             :                                 break;
    3697             :                         if (groupby->l)
    3698             :                                 groupby = groupby->l;
    3699             :                 }
    3700          63 :                 if (groupby && is_select(groupby->op) && !is_processed(groupby)) {
    3701          18 :                         group = 1;
    3702          18 :                         sel = groupby;
    3703             :                         /* At the end we switch back to the old projection relation og.
    3704             :                          * During the partitioning and ordering we add the expressions to the intermediate relations. */
    3705             :                 }
    3706          18 :                 if (!sel)
    3707             :                         groupby = og;
    3708          18 :                 if (sel && sel->l)
    3709          18 :                         groupby = sel->l;
    3710             :         }
    3711             : 
    3712             :         /* find groupby */
    3713       41596 :         if (!subquery && groupby && !is_processed(groupby) && !is_base(groupby->op)) {
    3714       27374 :                 og = groupby;
    3715       27374 :                 groupby = rel_find_groupby(groupby);
    3716       27374 :                 if (groupby)
    3717             :                         group = 1;
    3718             :                 else
    3719             :                         groupby = og;
    3720             :         }
    3721             : 
    3722        7790 :         if (!groupby && exps_card(exps) > CARD_ATOM) {
    3723           0 :                 char *uaname = SA_NEW_ARRAY(sql->ta, char, strlen(aname) + 1);
    3724           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "%s: missing group by", toUpperCopy(uaname, aname));
    3725             :         }
    3726             : 
    3727       41596 :         if (!subquery && groupby && groupby->op != op_groupby) {             /* implicit groupby */
    3728       20282 :                 if (!all_freevar && query->last_exp && !is_sql_aggr(query->last_state)) {
    3729           9 :                         if (exp_relname(query->last_exp) && exp_name(query->last_exp) && !has_label(query->last_exp))
    3730           9 :                                 return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column '%s.%s' in query results without an aggregate function", exp_relname(query->last_exp), exp_name(query->last_exp));
    3731           0 :                         return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column in query results without an aggregate function");
    3732             :                 }
    3733       20273 :                 res = groupby = rel_groupby(sql, groupby, NULL);
    3734             :         }
    3735       41587 :         if (subquery) {
    3736           0 :                 assert(!all_freevar);
    3737           0 :                 res = groupby;
    3738           0 :                 if (is_sql_sel(f) && is_left(subquery->op) && !is_groupby(groupby->op)) {
    3739           0 :                         res = groupby = rel_groupby(sql, groupby, NULL);
    3740           0 :                         exps_set_freevar(sql, exps, groupby); /* mark free variables */
    3741           0 :                 } else if (!is_groupby(groupby->op)) {
    3742           0 :                         res = groupby = rel_groupby(sql, groupby, NULL);
    3743             :                 }
    3744           0 :                 assert(!is_base(groupby->op));
    3745             :         }
    3746       41587 :         if ((!exps || exps_card(exps) > CARD_ATOM) && (!res || !groupby))
    3747             :                 return NULL;
    3748             : 
    3749       41587 :         if (all_freevar) {
    3750         246 :                 query_update_outer(query, res, all_freevar-1);
    3751       41341 :         } else if (rel) {
    3752       41341 :                 *rel = res;
    3753       41341 :                 if (query->last_rel != res) {
    3754       37410 :                         query->last_rel = res;
    3755       37410 :                         query->last_state |= sql_aggr;
    3756             :                 }
    3757             :         }
    3758             : 
    3759       41587 :         if (!has_args) {        /* count(*) case */
    3760       18607 :                 sql_exp *e;
    3761             : 
    3762       18607 :                 if (strcmp(aname, "count") != 0) {
    3763           2 :                         char *uaname = SA_NEW_ARRAY(sql->ta, char, strlen(aname) + 1);
    3764           2 :                         return sql_error(sql, 02, SQLSTATE(42000) "%s: unable to perform '%s(*)'", toUpperCopy(uaname, aname), aname);
    3765             :                 }
    3766       18605 :                 a = sql_bind_func(sql, "sys", aname, sql_bind_localtype("void"), NULL, F_AGGR, true, true);
    3767       18605 :                 e = exp_aggr(sql->sa, NULL, a, distinct, 0, groupby?groupby->card:CARD_ATOM, 0);
    3768             : 
    3769       18605 :                 if (!groupby)
    3770             :                         return e;
    3771       18605 :                 if (all_freevar)
    3772           0 :                         query_outer_used_exp(query, all_freevar-1, e, sql_aggr);
    3773       18605 :                 e = rel_groupby_add_aggr(sql, groupby, e);
    3774       18605 :                 if (!group && !all_freevar)
    3775             :                         return e;
    3776        3203 :                 if (all_freevar) {
    3777           0 :                         assert(!is_simple_project(res->op));
    3778           0 :                         e->card = CARD_ATOM;
    3779           0 :                         set_freevar(e, all_freevar-1);
    3780           0 :                         return e;
    3781             :                 }
    3782             :                 return e;
    3783             :         }
    3784             : 
    3785             :         /* use cnt as nils shouldn't be counted */
    3786       22980 :         no_nil = 1;
    3787             : 
    3788       22980 :         gr = groupby;
    3789       22980 :         if (gr && gr->op == op_project && gr->l)
    3790             :                 gr = gr->l;
    3791             : 
    3792       22980 :         if (is_grouping) {
    3793         109 :                 sql_subtype *tpe;
    3794         109 :                 list *l = (list*) groupby->r;
    3795             : 
    3796         109 :                 if (list_length(l) <= 7)
    3797         107 :                         tpe = sql_bind_localtype("bte");
    3798           2 :                 else if (list_length(l) <= 15)
    3799           2 :                         tpe = sql_bind_localtype("sht");
    3800           0 :                 else if (list_length(l) <= 31)
    3801           0 :                         tpe = sql_bind_localtype("int");
    3802           0 :                 else if (list_length(l) <= 63)
    3803           0 :                         tpe = sql_bind_localtype("lng");
    3804             : #ifdef HAVE_HGE
    3805           0 :                 else if (list_length(l) <= 127)
    3806           0 :                         tpe = sql_bind_localtype("hge");
    3807             : #endif
    3808             :                 else
    3809           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "SELECT: GROUPING the number of grouping columns is larger"
    3810             :                                                                 " than the maximum number of representable bits from this server (%d > %d)", list_length(l),
    3811             : #ifdef HAVE_HGE
    3812             :                                                         127
    3813             : #else
    3814             :                                                          63
    3815             : #endif
    3816             :                                                         );
    3817         109 :                 a = sql_bind_func_result(sql, sname, aname, F_AGGR, true, tpe, 1, exp_subtype(exps->h->data));
    3818             :         } else
    3819       22871 :                 a = sql_bind_func_(sql, sname, aname, exp_types(sql->sa, exps), F_AGGR, false, false);
    3820             : 
    3821       22980 :         if (a) {
    3822       22975 :                 found = true;
    3823       22975 :                 if (!execute_priv(sql, a->func))
    3824           0 :                         a = NULL;
    3825       22975 :                 if (!is_grouping && a && !(exps = check_arguments_and_find_largest_any_type(sql, rel ? *rel : NULL, exps, a, 0, false)))
    3826           0 :                         a = NULL;
    3827             :         }
    3828             : 
    3829       22980 :         if (a) {
    3830       22975 :                 bool hasnil = have_nil(exps) || (strcmp(aname, "count") != 0 && (!groupby || list_empty(groupby->r))); /* for global case, the aggregate may return NULL */
    3831       22975 :                 sql_exp *e = exp_aggr(sql->sa, exps, a, distinct, no_nil, groupby?groupby->card:CARD_ATOM, hasnil);
    3832             : 
    3833       22975 :                 if (!groupby)
    3834             :                         return e;
    3835       22975 :                 if (all_freevar)
    3836         246 :                         query_outer_aggregated(query, all_freevar-1, e);
    3837       22975 :                 e = rel_groupby_add_aggr(sql, groupby, e);
    3838       22975 :                 if (!group && !all_freevar)
    3839             :                         return e;
    3840       16494 :                 if (all_freevar) {
    3841         246 :                         rel_bind_vars(sql, groupby->l, exps);
    3842         246 :                         assert(!is_simple_project(res->op));
    3843         246 :                         e->card = CARD_ATOM;
    3844         246 :                         set_freevar(e, all_freevar-1);
    3845         246 :                         return e;
    3846             :                 }
    3847             :                 return e;
    3848             :         }
    3849           5 :         const char *type = "unknown";
    3850           5 :         char *uaname = SA_NEW_ARRAY(sql->ta, char, strlen(aname) + 1);
    3851             : 
    3852           5 :         if (!list_empty(exps)) {
    3853           5 :                 sql_exp *e = exps->h->data;
    3854           5 :                 type = exp_subtype(e)->type->base.name;
    3855             :         }
    3856          15 :         return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "%s: %s aggregate %s%s%s'%s'(%s)", toUpperCopy(uaname, aname), found ? "insufficient privileges for" : "no such",
    3857             :                                          sname ? "'":"", sname ? sname : "", sname ? "'.":"", aname, type);
    3858             : }
    3859             : 
    3860             : static sql_exp *
    3861       41740 : rel_aggr(sql_query *query, sql_rel **rel, symbol *se, int f)
    3862             : {
    3863       41740 :         dlist *l = se->data.lval;
    3864       41740 :         dnode *d = l->h->next->next;
    3865       41740 :         int distinct = l->h->next->data.i_val;
    3866       41740 :         char *aname = qname_schema_object(l->h->data.lval);
    3867       41740 :         char *sname = qname_schema(l->h->data.lval);
    3868             : 
    3869       41740 :         return _rel_aggr(query, rel, distinct, sname, aname, d, f);
    3870             : }
    3871             : 
    3872             : static sql_exp *
    3873      100733 : rel_case(sql_query *query, sql_rel **rel, symbol *opt_cond, dlist *when_search_list, symbol *opt_else, int f)
    3874             : {
    3875      100733 :         mvc *sql = query->sql;
    3876      100733 :         sql_subtype *tpe = NULL;
    3877      100733 :         list *conds = new_exp_list(sql->sa), *results = new_exp_list(sql->sa);
    3878      100733 :         sql_subtype *restype = NULL, *condtype = NULL, ctype, rtype, bt;
    3879      100733 :         sql_exp *res = NULL, *opt_cond_exp = NULL;
    3880      100733 :         exp_kind ek = {type_value, card_column, FALSE};
    3881             : 
    3882      100733 :         if (opt_cond) {
    3883        9436 :                 if (!(opt_cond_exp = rel_value_exp(query, rel, opt_cond, f, ek)))
    3884             :                         return NULL;
    3885        9435 :                 condtype = exp_subtype(opt_cond_exp);
    3886             :         }
    3887             : 
    3888      248521 :         for (dnode *dn = when_search_list->h; dn; dn = dn->next) {
    3889      147798 :                 sql_exp *cond = NULL, *result = NULL;
    3890      147798 :                 dlist *when = dn->data.sym->data.lval;
    3891             : 
    3892      147798 :                 if (opt_cond)
    3893       45602 :                         cond = rel_value_exp(query, rel, when->h->data.sym, f, ek);
    3894             :                 else
    3895      102196 :                         cond = rel_logical_value_exp(query, rel, when->h->data.sym, f, ek);
    3896      147798 :                 if (!cond)
    3897             :                         return NULL;
    3898      147792 :                 append(conds, cond);
    3899      147792 :                 tpe = exp_subtype(cond);
    3900      147792 :                 if (tpe && condtype) {
    3901       56495 :                         result_datatype(&ctype, condtype, tpe);
    3902       56495 :                         condtype = &ctype;
    3903       91297 :                 } else if (tpe) {
    3904       91295 :                         condtype = tpe;
    3905             :                 }
    3906             : 
    3907      147792 :                 if (!(result = rel_value_exp(query, rel, when->h->next->data.sym, f, ek)))
    3908             :                         return NULL;
    3909      147789 :                 append(results, result);
    3910      147789 :                 tpe = exp_subtype(result);
    3911      147789 :                 if (tpe && restype) {
    3912       47063 :                         result_datatype(&rtype, restype, tpe);
    3913       47063 :                         restype = &rtype;
    3914      100726 :                 } else if (tpe) {
    3915      100720 :                         restype = tpe;
    3916             :                 }
    3917             :         }
    3918      100723 :         if (opt_else) {
    3919       98153 :                 if (!(res = rel_value_exp(query, rel, opt_else, f, ek)))
    3920             :                         return NULL;
    3921             : 
    3922       98153 :                 tpe = exp_subtype(res);
    3923       98153 :                 if (tpe && restype) {
    3924       98148 :                         result_datatype(&rtype, restype, tpe);
    3925       98148 :                         restype = &rtype;
    3926           5 :                 } else if (tpe) {
    3927             :                         restype = tpe;
    3928             :                 }
    3929             : 
    3930       98151 :                 if (!restype)
    3931           2 :                         return sql_error(sql, 02, SQLSTATE(42000) "Result type missing");
    3932       98151 :                 if (restype->type->localtype == TYPE_void) /* NULL */
    3933           0 :                         restype = sql_bind_localtype("str");
    3934             : 
    3935       98151 :                 if (!(res = exp_check_type(sql, restype, rel ? *rel : NULL, res, type_equal)))
    3936             :                         return NULL;
    3937             :         } else {
    3938        2570 :                 if (!restype)
    3939           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "Result type missing");
    3940        2570 :                 if (restype->type->localtype == TYPE_void) /* NULL */
    3941           7 :                         restype = sql_bind_localtype("str");
    3942        2570 :                 res = exp_null(sql->sa, restype);
    3943             :         }
    3944             : 
    3945      100721 :         if (!condtype)
    3946           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "Condition type missing");
    3947      100721 :         if (condtype->type->localtype == TYPE_void) /* NULL */
    3948           0 :                 condtype = sql_bind_localtype("str");
    3949      100721 :         if (opt_cond_exp && !(opt_cond_exp = exp_check_type(sql, condtype, rel ? *rel : NULL, opt_cond_exp, type_equal)))
    3950             :                 return NULL;
    3951      100721 :         sql_find_subtype(&bt, "boolean", 0, 0);
    3952      100721 :         list *args = sa_list(sql->sa);
    3953      100721 :         if (opt_cond_exp)
    3954        9429 :                 append(args, opt_cond_exp);
    3955      248503 :         for (node *n = conds->h, *m = results->h; n && m; n = n->next, m = m->next) {
    3956      147783 :                 sql_exp *cond = n->data;
    3957      147783 :                 sql_exp *result = m->data;
    3958             : 
    3959      147783 :                 if (!(result = exp_check_type(sql, restype, rel ? *rel : NULL, result, type_equal)))
    3960             :                         return NULL;
    3961             : 
    3962      147783 :                 if (!(cond = exp_check_type(sql, condtype, rel ? *rel : NULL, cond, type_equal)))
    3963             :                         return NULL;
    3964      147782 :                 if (!opt_cond_exp && !(cond = exp_check_type(sql, &bt, rel ? *rel : NULL, cond, type_equal)))
    3965             :                         return NULL;
    3966      147782 :                 append(args, cond);
    3967      147782 :                 append(args, result);
    3968             :         }
    3969      100720 :         assert(res);
    3970      100720 :         list_append(args, res);
    3971      100720 :         list *types = sa_list(sql->sa);
    3972      100720 :         types = append(append(types, restype), restype);
    3973      192012 :         sql_subfunc *case_func = find_func(sql, NULL, opt_cond_exp?"casewhen":"case", list_length(types), F_FUNC, true, NULL, NULL);
    3974      100720 :         res = exp_op(sql->sa, args, case_func);
    3975      100720 :         ((sql_subfunc*)res->f)->res->h->data = sql_create_subtype(sql->sa, restype->type, restype->digits, restype->scale);
    3976      100720 :         return res;
    3977             : }
    3978             : 
    3979             : static sql_exp *
    3980        3838 : rel_complex_case(sql_query *query, sql_rel **rel, dlist *case_args, int f, str func)
    3981             : {
    3982        3838 :         exp_kind ek = {type_value, card_column, FALSE};
    3983        3838 :         list *args = sa_list(query->sql->sa);
    3984        3838 :         sql_subtype *restype = NULL, rtype;
    3985        3838 :         sql_exp *res;
    3986             : 
    3987             :         /* generate nested func calls */
    3988       11970 :         for(dnode *dn = case_args->h; dn; dn = dn->next) {
    3989        8145 :                 sql_exp *a = rel_value_exp(query, rel, dn->data.sym, f, ek);
    3990        8145 :                 if (!a)
    3991             :                         return NULL;
    3992        8132 :                 append(args, a);
    3993             :                 /* all arguments should have the same type */
    3994        8132 :                 sql_subtype *tpe = exp_subtype(a);
    3995        8132 :                 if (tpe && restype) {
    3996        4296 :                         result_datatype(&rtype, restype, tpe);
    3997        4296 :                         restype = &rtype;
    3998        3836 :                 } else if (tpe) {
    3999        3836 :                         restype = tpe;
    4000             :                 }
    4001             :         }
    4002        3825 :         if (!restype)
    4003           0 :                 return sql_error(query->sql, 02, SQLSTATE(42000) "Result type missing");
    4004        3825 :         if (restype->type->localtype == TYPE_void) /* NULL */
    4005           0 :                 restype = sql_bind_localtype("str");
    4006        3825 :         list *nargs = sa_list(query->sql->sa);
    4007       11942 :         for (node *m = args->h; m; m = m->next) {
    4008        8117 :                 sql_exp *result = m->data;
    4009             : 
    4010        8117 :                 if (!(result = exp_check_type(query->sql, restype, rel ? *rel : NULL, result, type_equal)))
    4011             :                         return NULL;
    4012        8117 :                 append(nargs, result);
    4013             :         }
    4014        3825 :         list *types = append(append(sa_list(query->sql->sa), restype), restype);
    4015        3825 :         sql_subfunc *fnc = find_func(query->sql, NULL, func, list_length(types), F_FUNC, true, NULL, NULL);
    4016        3825 :         res = exp_op(query->sql->sa, nargs, fnc);
    4017        3825 :         ((sql_subfunc*)res->f)->res->h->data = sql_create_subtype(query->sql->sa, restype->type, restype->digits, restype->scale);
    4018        3825 :         return res;
    4019             : }
    4020             : 
    4021             : static sql_exp *
    4022      104571 : rel_case_exp(sql_query *query, sql_rel **rel, symbol *se, int f)
    4023             : {
    4024      104571 :         dlist *l = se->data.lval;
    4025             : 
    4026      104571 :         if (se->token == SQL_COALESCE) {
    4027        3687 :                 return rel_complex_case(query, rel, l, f | sql_farg, "coalesce");
    4028      100884 :         } else if (se->token == SQL_NULLIF) {
    4029         151 :                 return rel_complex_case(query, rel, l, f | sql_farg, "nullif");
    4030      100733 :         } else if (l->h->type == type_list) {
    4031       91297 :                 dlist *when_search_list = l->h->data.lval;
    4032       91297 :                 symbol *opt_else = l->h->next->data.sym;
    4033             : 
    4034       91297 :                 return rel_case(query, rel, NULL, when_search_list, opt_else, f | sql_farg);
    4035             :         } else {
    4036        9436 :                 symbol *scalar_exp = l->h->data.sym;
    4037        9436 :                 dlist *when_value_list = l->h->next->data.lval;
    4038        9436 :                 symbol *opt_else = l->h->next->next->data.sym;
    4039             : 
    4040        9436 :                 return rel_case(query, rel, scalar_exp, when_value_list, opt_else, f | sql_farg);
    4041             :         }
    4042             : }
    4043             : 
    4044             : #define E_ATOM_STRING(e) ((atom*)(e)->l)->data.val.sval
    4045             : 
    4046             : static sql_exp *
    4047      169087 : rel_cast(sql_query *query, sql_rel **rel, symbol *se, int f)
    4048             : {
    4049      169087 :         mvc *sql = query->sql;
    4050      169087 :         dlist *dl = se->data.lval;
    4051      169087 :         symbol *s = dl->h->data.sym;
    4052      169087 :         sql_subtype *tpe = &dl->h->next->data.typeval;
    4053      169087 :         exp_kind ek = {type_value, card_column, FALSE};
    4054      169087 :         sql_exp *e = rel_value_exp(query, rel, s, f|sql_farg, ek);
    4055             : 
    4056      169087 :         if (!e)
    4057             :                 return NULL;
    4058             : 
    4059             :         /* strings may need to be truncated */
    4060      169072 :         if (EC_VARCHAR(tpe->type->eclass) && tpe->digits > 0) {
    4061       82568 :                 sql_subtype *et = exp_subtype(e);
    4062             :                 /* truncate only if the number of digits are smaller or from clob */
    4063       82568 :                 if (et && EC_VARCHAR(et->type->eclass) && (tpe->digits < et->digits || et->digits == 0)) {
    4064        2870 :                         sql_subfunc *c = sql_bind_func(sql, "sys", "truncate", et, sql_bind_localtype("int"), F_FUNC, true, true);
    4065        2870 :                         if (c)
    4066        2870 :                                 e = exp_binop(sql->sa, e, exp_atom_int(sql->sa, tpe->digits), c);
    4067             :                 }
    4068             :         }
    4069             : 
    4070      169072 :         if (tpe->type->eclass == EC_DEC) {
    4071         447 :                 sql_subtype *et = exp_subtype(e);
    4072         447 :                 if (e->type == e_atom && !tpe->digits) {
    4073           9 :                         if (et->type->eclass == EC_NUM || et->type->eclass == EC_DEC) {
    4074           6 :                                 tpe->digits = atom_num_digits(e->l);
    4075           6 :                                 tpe = sql_bind_subtype(sql->sa, "decimal", tpe->digits, et->scale);
    4076           3 :                         } else if (EC_VARCHAR(et->type->eclass)) {
    4077           2 :                                 char *s = E_ATOM_STRING(e);
    4078           2 :                                 unsigned int min_precision = 0, min_scale = 0;
    4079           2 :                                 bool dot_seen = false;
    4080          16 :                                 for (size_t i = 0; i < strlen(s); i++) {
    4081          14 :                                         if (isdigit(s[i])) {
    4082          12 :                                                 min_precision++;
    4083          12 :                                                 if (dot_seen)
    4084          10 :                                                         min_scale++;
    4085           2 :                                         } else if (s[i] == '.') {
    4086           2 :                                                 dot_seen = true;
    4087             :                                         }
    4088             :                                 }
    4089           2 :                                 tpe = sql_bind_subtype(sql->sa, "decimal", min_precision, min_scale);
    4090             :                         } else { /* fallback */
    4091           1 :                                 tpe = sql_bind_subtype(sql->sa, "decimal", 18, 3);
    4092             :                         }
    4093         438 :                 } else if (!tpe->digits && !tpe->scale) {
    4094         105 :                         if (et->type->eclass == EC_NUM)
    4095          36 :                                 tpe = sql_bind_subtype(sql->sa, "decimal", et->digits, 0);
    4096             :                         else /* fallback */
    4097          69 :                                 tpe = sql_bind_subtype(sql->sa, "decimal", 18, 3);
    4098             :                 }
    4099             :         }
    4100             : 
    4101      169072 :         if (e)
    4102      169072 :                 e = exp_check_type(sql, tpe, rel ? *rel : NULL, e, type_cast);
    4103             : 
    4104      169072 :         if (e && e->type == e_convert)
    4105       48063 :                 exp_label(sql->sa, e, ++sql->label);
    4106             : 
    4107             :         return e;
    4108             : }
    4109             : 
    4110             : static sql_exp *
    4111        1590 : rel_next_value_for( mvc *sql, symbol *se )
    4112             : {
    4113        1590 :         char *sname = qname_schema(se->data.lval);
    4114        1590 :         char *seqname = qname_schema_object(se->data.lval);
    4115        1590 :         sql_sequence *seq = NULL;
    4116        1590 :         sql_subtype t;
    4117        1590 :         sql_subfunc *f;
    4118             : 
    4119        1590 :         if (!sname)
    4120         225 :                 sname = "sys";
    4121        1590 :         if (!stack_find_rel_view(sql, seqname)) {
    4122        1389 :                 if (!(seq = find_sequence_on_scope(sql, sname, seqname, "NEXT VALUE FOR")))
    4123             :                         return NULL;
    4124        1387 :                 if (!mvc_schema_privs(sql, seq->s))
    4125           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "NEXT VALUE FOR: access denied for %s to schema '%s'", get_string_global_var(sql, "current_user"), seq->s->base.name);
    4126             :         }
    4127        1588 :         sql_find_subtype(&t, "varchar", 0, 0);
    4128        1588 :         f = sql_bind_func(sql, "sys", "next_value_for", &t, &t, F_FUNC, true, true);
    4129        1588 :         assert(f);
    4130             :         /* sequence found in the stack, ie just created. use given schema? */
    4131        1588 :         return exp_binop(sql->sa, exp_atom_str(sql->sa, seq && seq->s ? seq->s->base.name : sname, &t), exp_atom_str(sql->sa, seqname, &t), f);
    4132             : }
    4133             : 
    4134             : /* some users like to use aliases already in the groupby */
    4135             : static sql_exp *
    4136         165 : rel_selection_ref(sql_query *query, sql_rel **rel, char *name, dlist *selection)
    4137             : {
    4138         165 :         allocator *sa = query->sql->sa;
    4139         165 :         dlist *nl;
    4140         165 :         exp_kind ek = {type_value, card_column, FALSE};
    4141         165 :         sql_exp *res = NULL;
    4142         165 :         symbol *nsym;
    4143             : 
    4144         165 :         if (!selection)
    4145             :                 return NULL;
    4146             : 
    4147         679 :         for (dnode *n = selection->h; n; n = n->next) {
    4148             :                 /* we only look for columns */
    4149         517 :                 tokens to = n->data.sym->token;
    4150         517 :                 if (to == SQL_COLUMN || to == SQL_IDENT) {
    4151         515 :                         dlist *l = n->data.sym->data.lval;
    4152             :                         /* AS name */
    4153         515 :                         if (l->h->next->data.sval && strcmp(l->h->next->data.sval, name) == 0) {
    4154         146 :                                 sql_exp *ve = rel_value_exp(query, rel, l->h->data.sym, sql_sel|sql_groupby, ek);
    4155         146 :                                 if (ve) {
    4156         145 :                                         if (res)
    4157           1 :                                                 return sql_error(query->sql, ERR_AMBIGUOUS, SQLSTATE(42000) "SELECT: identifier '%s' ambiguous", name);
    4158         144 :                                         res = ve;
    4159             : 
    4160         144 :                                         nl = dlist_create(sa);
    4161         144 :                                         exp_setname(query->sql, ve, NULL, name);
    4162             :                                         /* now we should rewrite the selection such that it uses the new group by column */
    4163         144 :                                         dlist_append_string(sa, nl, sa_strdup(sa, name));
    4164         144 :                                         nsym = symbol_create_list(sa, to, nl);
    4165         144 :                                         nl = dlist_create(sa);
    4166         144 :                                         dlist_append_symbol(sa, nl, nsym);
    4167             :                                         /* no alias */
    4168         144 :                                         dlist_append_symbol(sa, nl, NULL);
    4169         144 :                                         n->data.sym = symbol_create_list(sa, to, nl);
    4170             :                                 }
    4171             :                         }
    4172             :                 }
    4173             :         }
    4174             :         return res;
    4175             : }
    4176             : 
    4177             : static char*
    4178         197 : symbol_get_identifier(symbol *sym)
    4179             : {
    4180         197 :         dlist *syml;
    4181             : 
    4182         197 :         if (sym->token != SQL_COLUMN && sym->token != SQL_IDENT)
    4183             :                 return NULL;
    4184         182 :         syml = sym->data.lval;
    4185         182 :         if (dlist_length(syml) > 1)
    4186             :                 return NULL;
    4187             : 
    4188         165 :         return syml->h->data.sval;
    4189             : }
    4190             : 
    4191             : static sql_exp*
    4192       23643 : rel_group_column(sql_query *query, sql_rel **rel, symbol *grp, dlist *selection, list *exps, int f)
    4193             : {
    4194       23643 :         sql_query *lquery = query_create(query->sql);
    4195       23643 :         mvc *sql = query->sql;
    4196       23643 :         exp_kind ek = {type_value, card_value, TRUE};
    4197       23643 :         sql_exp *e = rel_value_exp2(lquery, rel, grp, f, ek);
    4198             : 
    4199       23643 :         if (e && exp_is_atom(e)) {
    4200          34 :                 sql_subtype *tpe = exp_subtype(e);
    4201          34 :                 if (!is_atom(e->type) ||!tpe || tpe->type->eclass != EC_NUM) {
    4202           2 :                         if (!tpe)
    4203           2 :                                 return sql_error(sql, 02, SQLSTATE(42000) "Cannot have a parameter (?) for group by column");
    4204           3 :                         return sql_error(sql, 02, SQLSTATE(42000) "SELECT: non-integer constant in GROUP BY");
    4205             :                 }
    4206             :         }
    4207             : 
    4208         192 :         if (!e) {
    4209         192 :                 char buf[ERRSIZE], *name;
    4210         192 :                 int status = sql->session->status;
    4211         192 :                 strcpy(buf, sql->errstr);
    4212             :                 /* reset error */
    4213         192 :                 sql->session->status = 0;
    4214         192 :                 sql->errstr[0] = '\0';
    4215             : 
    4216         192 :                 if ((name = symbol_get_identifier(grp))) {
    4217         163 :                         e = rel_selection_ref(query, rel, name, selection);
    4218         163 :                         if (!e) { /* attempt to find in the existing list of group by expressions */
    4219          29 :                                 for (node *n = exps->h ; n && !e ; n = n->next) {
    4220           9 :                                         sql_exp *ge = (sql_exp *) n->data;
    4221           9 :                                         const char *gen = exp_name(ge);
    4222             : 
    4223           9 :                                         if (gen && strcmp(name, gen) == 0)
    4224           5 :                                                 e = exp_ref(sql, ge);
    4225             :                                 }
    4226             :                         }
    4227             :                 }
    4228          49 :                 if (!e && query_has_outer(query)) {
    4229             :                         /* reset error */
    4230          32 :                         sql->session->status = 0;
    4231          32 :                         sql->errstr[0] = '\0';
    4232          32 :                         e = rel_value_exp2(query, rel, grp, f, ek);
    4233             :                 }
    4234          44 :                 if (!e) {
    4235          13 :                         if (sql->errstr[0] == 0) {
    4236          10 :                                 sql->session->status = status;
    4237          10 :                                 strcpy(sql->errstr, buf);
    4238             :                         }
    4239          13 :                         return NULL;
    4240             :                 }
    4241             :         }
    4242       23625 :         if (!exp_subtype(e))
    4243           0 :                 return sql_error(sql, 01, SQLSTATE(42000) "Cannot have a parameter (?) for group by column");
    4244             :         return e;
    4245             : }
    4246             : 
    4247             : static list*
    4248          44 : list_power_set(allocator *sa, list* input) /* cube */
    4249             : {
    4250          44 :         list *res = sa_list(sa);
    4251             :         /* N stores total number of subsets */
    4252          44 :         int N = (int) pow(2, input->cnt);
    4253             : 
    4254             :         /* generate each subset one by one */
    4255         266 :         for (int i = 0; i < N; i++) {
    4256         222 :                 list *ll = sa_list(sa);
    4257         222 :                 int j = 0; /* check every bit of i */
    4258         844 :                 for (node *n = input->h ; n ; n = n->next) {
    4259             :                         /* if j'th bit of i is set, then append */
    4260         622 :                         if (i & (1 << j))
    4261         311 :                                 list_prepend(ll, n->data);
    4262         622 :                         j++;
    4263             :                 }
    4264         222 :                 list_prepend(res, ll);
    4265             :         }
    4266          44 :         return res;
    4267             : }
    4268             : 
    4269             : static list*
    4270          61 : list_rollup(allocator *sa, list* input)
    4271             : {
    4272          61 :         list *res = sa_list(sa);
    4273             : 
    4274         188 :         for (int counter = input->cnt; counter > 0; counter--) {
    4275         127 :                 list *ll = sa_list(sa);
    4276         127 :                 int j = 0;
    4277         376 :                 for (node *n = input->h; n && j < counter; j++, n = n->next)
    4278         249 :                         list_append(ll, n->data);
    4279         127 :                 list_append(res, ll);
    4280             :         }
    4281          61 :         list_append(res, sa_list(sa)); /* global aggregate case */
    4282          61 :         return res;
    4283             : }
    4284             : 
    4285             : static int
    4286         320 : list_equal(list* list1, list* list2)
    4287             : {
    4288         375 :         for (node *n = list1->h; n ; n = n->next) {
    4289         328 :                 sql_exp *e = (sql_exp*) n->data;
    4290         328 :                 if (!exps_find_exp(list2, e))
    4291             :                         return 1;
    4292             :         }
    4293          78 :         for (node *n = list2->h; n ; n = n->next) {
    4294          47 :                 sql_exp *e = (sql_exp*) n->data;
    4295          47 :                 if (!exps_find_exp(list1, e))
    4296             :                         return 1;
    4297             :         }
    4298             :         return 0;
    4299             : }
    4300             : 
    4301             : static list*
    4302          26 : lists_cartesian_product_and_distinct(allocator *sa, list *l1, list *l2)
    4303             : {
    4304          26 :         list *res = sa_list(sa);
    4305             : 
    4306             :         /* for each list of l2, merge into each list of l1 while removing duplicates */
    4307         116 :         for (node *n = l1->h ; n ; n = n->next) {
    4308          90 :                 list *sub_list = (list*) n->data;
    4309             : 
    4310         279 :                 for (node *m = l2->h ; m ; m = m->next) {
    4311         189 :                         list *other = (list*) m->data;
    4312         189 :                         list_append(res, list_distinct(list_merge(list_dup(sub_list, (fdup) NULL), other, (fdup) NULL), (fcmp) list_equal, (fdup) NULL));
    4313             :                 }
    4314             :         }
    4315          26 :         return res;
    4316             : }
    4317             : 
    4318             : static list*
    4319        9802 : rel_groupings(sql_query *query, sql_rel **rel, symbol *groupby, dlist *selection, int f, bool grouping_sets, list **sets)
    4320             : {
    4321        9802 :         mvc *sql = query->sql;
    4322        9802 :         list *exps = new_exp_list(sql->sa);
    4323             : 
    4324        9802 :         if (mvc_highwater(sql))
    4325           0 :                 return sql_error(sql, 10, SQLSTATE(42000) "Query too complex: running out of stack space");
    4326             : 
    4327       33353 :         for (dnode *o = groupby->data.lval->h; o; o = o->next) {
    4328       23570 :                 symbol *grouping = o->data.sym;
    4329       23570 :                 list *next_set = NULL;
    4330             : 
    4331       23570 :                 if (grouping->token == SQL_GROUPING_SETS) { /* call recursively, and merge the generated sets */
    4332          39 :                         list *other = rel_groupings(query, rel, grouping, selection, f, true, &next_set);
    4333          39 :                         if (!other)
    4334          19 :                                 return NULL;
    4335          39 :                         exps = list_distinct(list_merge(exps, other, (fdup) NULL), (fcmp) exp_equal, (fdup) NULL);
    4336             :                 } else {
    4337       23531 :                         dlist *dl = grouping->data.lval;
    4338       23531 :                         if (dl) {
    4339       23505 :                                 list *set_cols = new_exp_list(sql->sa); /* columns and combination of columns to be used for the next set */
    4340             : 
    4341       47100 :                                 for (dnode *oo = dl->h; oo; oo = oo->next) {
    4342       23613 :                                         symbol *grp = oo->data.sym;
    4343       23613 :                                         list *next_tuple = new_exp_list(sql->sa); /* next tuple of columns */
    4344             : 
    4345       23613 :                                         if (grp->token == SQL_COLUMN_GROUP) { /* set of columns */
    4346          71 :                                                 assert(is_sql_group_totals(f));
    4347         172 :                                                 for (dnode *ooo = grp->data.lval->h; ooo; ooo = ooo->next) {
    4348         101 :                                                         symbol *elm = ooo->data.sym;
    4349         101 :                                                         sql_exp *e = rel_group_column(query, rel, elm, selection, exps, f);
    4350         101 :                                                         if (!e)
    4351             :                                                                 return NULL;
    4352         101 :                                                         assert(e->type == e_column);
    4353         101 :                                                         list_append(next_tuple, e);
    4354         101 :                                                         list_append(exps, e);
    4355             :                                                 }
    4356             :                                         } else { /* single column or expression */
    4357       23542 :                                                 sql_exp *e = rel_group_column(query, rel, grp, selection, exps, f);
    4358       23542 :                                                 if (!e)
    4359             :                                                         return NULL;
    4360       23524 :                                                 if (e->type != e_column && !exp_is_atom(e)) { /* store group by expressions in the stack */
    4361         140 :                                                         if (is_sql_group_totals(f))
    4362           0 :                                                                 return sql_error(sql, 02, SQLSTATE(42000) "GROUP BY: grouping expressions not possible with ROLLUP, CUBE and GROUPING SETS");
    4363         140 :                                                         if (!exp_has_rel(e) && !frame_push_groupby_expression(sql, grp, e))
    4364             :                                                                 return NULL;
    4365             :                                                 }
    4366       23524 :                                                 list_append(next_tuple, e);
    4367       23524 :                                                 list_append(exps, e);
    4368             :                                         }
    4369       23595 :                                         list_append(set_cols, next_tuple);
    4370             :                                 }
    4371       23487 :                                 if (is_sql_group_totals(f)) {
    4372         178 :                                         if (grouping->token == SQL_ROLLUP)
    4373          61 :                                                 next_set = list_rollup(sql->sa, set_cols);
    4374         117 :                                         else if (grouping->token == SQL_CUBE)
    4375          44 :                                                 next_set = list_power_set(sql->sa, set_cols);
    4376             :                                         else /* the list of sets is not used in the "GROUP BY a, b, ..." case */
    4377          73 :                                                 next_set = list_append(new_exp_list(sql->sa), set_cols);
    4378             :                                 }
    4379          26 :                         } else if (is_sql_group_totals(f) && grouping_sets) /* The GROUP BY () case is the global aggregate which is always added by ROLLUP and CUBE */
    4380          17 :                                 next_set = list_append(new_exp_list(sql->sa), new_exp_list(sql->sa));
    4381             :                 }
    4382       23552 :                 if (is_sql_group_totals(f)) { /* if there are no sets, set the found one, otherwise calculate cartesian product and merge the distinct ones */
    4383         235 :                         if (!next_set)
    4384           1 :                                 return sql_error(sql, 02, SQLSTATE(42000) "GROUP BY: GROUPING SETS is empty");
    4385         234 :                         if (!*sets)
    4386         157 :                                 *sets = next_set;
    4387             :                         else
    4388          77 :                                 *sets = grouping_sets ? list_merge(*sets, next_set, (fdup) NULL) : lists_cartesian_product_and_distinct(sql->sa, *sets, next_set);
    4389             :                 }
    4390             :         }
    4391             :         return exps;
    4392             : }
    4393             : 
    4394             : static list*
    4395        8208 : rel_partition_groupings(sql_query *query, sql_rel **rel, symbol *partitionby, dlist *selection, int f)
    4396             : {
    4397        8208 :         mvc *sql = query->sql;
    4398        8208 :         dnode *o = partitionby->data.lval->h;
    4399        8208 :         list *exps = new_exp_list(sql->sa);
    4400             : 
    4401       24651 :         for (; o; o = o->next) {
    4402        8243 :                 symbol *grp = o->data.sym;
    4403        8243 :                 exp_kind ek = {type_value, card_value, TRUE};
    4404        8243 :                 sql_exp *e = rel_value_exp2(query, rel, grp, f, ek);
    4405             : 
    4406        8243 :                 if (!e) {
    4407           5 :                         int status = sql->session->status;
    4408           5 :                         char buf[ERRSIZE], *name;
    4409             : 
    4410             :                         /* reset error */
    4411           5 :                         sql->session->status = 0;
    4412           5 :                         strcpy(buf, sql->errstr);
    4413           5 :                         sql->errstr[0] = '\0';
    4414             : 
    4415           5 :                         if ((name = symbol_get_identifier(grp))) {
    4416           2 :                                 e = rel_selection_ref(query, rel, name, selection);
    4417           2 :                                 if (!e) { /* attempt to find in the existing list of partition by expressions */
    4418           3 :                                         for (node *n = exps->h ; n ; n = n->next) {
    4419           1 :                                                 sql_exp *ge = (sql_exp *) n->data;
    4420           1 :                                                 const char *gen = exp_name(ge);
    4421             : 
    4422           1 :                                                 if (gen && strcmp(name, gen) == 0) {
    4423           0 :                                                         e = exp_ref(sql, ge);
    4424           0 :                                                         break;
    4425             :                                                 }
    4426             :                                         }
    4427             :                                 }
    4428             :                         }
    4429           5 :                         if (!e) {
    4430           5 :                                 if (sql->errstr[0] == 0) {
    4431           5 :                                         sql->session->status = status;
    4432           5 :                                         strcpy(sql->errstr, buf);
    4433             :                                 }
    4434           5 :                                 return NULL;
    4435             :                         }
    4436             :                 }
    4437             : 
    4438        8238 :                 if (exp_has_rel(e))
    4439           3 :                         return sql_error(sql, 02, SQLSTATE(42000) "PARTITION BY: subqueries not allowed in PARTITION BY clause");
    4440             : 
    4441        8235 :                 if (e->type != e_column) { /* store group by expressions in the stack */
    4442          36 :                         if (!frame_push_groupby_expression(sql, grp, e))
    4443             :                                 return NULL;
    4444             :                 }
    4445             : 
    4446        8235 :                 if (e->card > CARD_AGGR)
    4447        8115 :                         e->card = CARD_AGGR;
    4448        8235 :                 append(exps, e);
    4449             :         }
    4450             :         return exps;
    4451             : }
    4452             : 
    4453             : /* find selection expressions matching the order by column expression */
    4454             : /* complex columns only */
    4455             : static sql_exp *
    4456       66963 : rel_order_by_column_exp(sql_query *query, sql_rel **R, symbol *column_r, int needs_distinct, int f)
    4457             : {
    4458       66963 :         mvc *sql = query->sql;
    4459       66963 :         sql_rel *r = *R, *p = NULL;
    4460       66963 :         sql_exp *e = NULL, *found = NULL;
    4461       66963 :         exp_kind ek = {type_value, card_column, FALSE};
    4462             : 
    4463       66963 :         if (!r)
    4464             :                 return e;
    4465             : 
    4466       66963 :         if (is_simple_project(r->op) && r->l && is_processed(r)) {
    4467       66901 :                 p = r;
    4468       66901 :                 r = r->l;
    4469             :         }
    4470             : 
    4471       66963 :         e = rel_value_exp(query, &r, column_r, f, ek);
    4472             : 
    4473       66963 :         if (r && !p)
    4474          62 :                 *R = r;
    4475       66901 :         else if (r)
    4476       66901 :                 p->l = r;
    4477       66963 :         if (e && p) {
    4478       66874 :                 if (is_project(p->op) && (found = exps_any_match(p->exps, e))) { /* if one of the projections matches, return a reference to it */
    4479       59401 :                         e = exp_ref(sql, found);
    4480             :                 } else {
    4481        7473 :                         if (needs_distinct)
    4482           2 :                                 return sql_error(sql, 02, SQLSTATE(42000) "SELECT: with DISTINCT ORDER BY expressions must appear in select list");
    4483        7471 :                         e = rel_project_add_exp(sql, p, e);
    4484        7471 :                         if (r) {
    4485      121118 :                                 for (node *n = p->exps->h ; n ; n = n->next) {
    4486      113649 :                                         sql_exp *ee = n->data;
    4487             : 
    4488      113649 :                                         if (ee->card > r->card) {
    4489           2 :                                                 if (exp_name(ee) && !has_label(ee))
    4490           2 :                                                         return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column '%s' in query results without an aggregate function", exp_name(ee));
    4491           0 :                                                 return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column in query results without an aggregate function");
    4492             :                                         }
    4493             :                                 }
    4494             :                         }
    4495             :                 }
    4496       66870 :                 return e;
    4497             :         }
    4498          89 :         if (e && r && is_project(r->op)) {
    4499          28 :                 sql_exp *found = exps_find_exp(r->exps, e);
    4500             : 
    4501          28 :                 if (!found) {
    4502           8 :                         if (needs_distinct)
    4503           0 :                                 return sql_error(sql, 02, SQLSTATE(42000) "SELECT: with DISTINCT ORDER BY expressions must appear in select list");
    4504           8 :                         if (!is_simple_project(r->op) && !is_groupby(r->op))
    4505           1 :                                 *R = r = rel_project(sql->sa, r, rel_projections(sql, r, NULL, 1, 0));
    4506           8 :                         append(r->exps, e);
    4507             :                 } else {
    4508             :                         e = found;
    4509             :                 }
    4510          28 :                 e = exp_ref(sql, e);
    4511             :         }
    4512             :         return e;
    4513             : }
    4514             : 
    4515             : static dlist *
    4516      114752 : simple_selection(symbol *sq)
    4517             : {
    4518      114752 :         if (sq->token == SQL_SELECT) {
    4519          13 :                 SelectNode *sn;
    4520          13 :                 sn = (SelectNode *) sq;
    4521             : 
    4522          13 :                 if (!sn->from && !sn->where && !sn->distinct && !sn->window && dlist_length(sn->selection) == 1)
    4523           7 :                         return sn->selection;
    4524             :         }
    4525             :         return NULL;
    4526             : }
    4527             : 
    4528             : static list *
    4529       45517 : rel_order_by(sql_query *query, sql_rel **R, symbol *orderby, int needs_distinct, int f)
    4530             : {
    4531       45517 :         mvc *sql = query->sql;
    4532       45517 :         sql_rel *rel = *R, *or = rel; /* the order by relation */
    4533       45517 :         list *exps = new_exp_list(sql->sa);
    4534             : 
    4535       45517 :         if (!orderby->data.lval) { /* by all */
    4536           2 :                 if (is_sql_orderby(f)) {
    4537           2 :                         assert(is_project(rel->op));
    4538           6 :                         for(node *n = rel->exps->h; n; n = n->next) {
    4539           4 :                                 sql_exp *e = n->data;
    4540           4 :                                 append(exps, exp_ref(sql, e));
    4541             :                         }
    4542             :                         return exps;
    4543             :                 }
    4544             :                 return NULL;
    4545             :         }
    4546             : 
    4547       45515 :         dnode *o = orderby->data.lval->h;
    4548       45515 :         dlist *selection = NULL;
    4549             : 
    4550       45515 :         if (is_sql_orderby(f)) {
    4551       36632 :                 assert(is_project(rel->op));
    4552       36632 :                 rel = rel->l;
    4553             :         }
    4554             : 
    4555      160220 :         for (; o; o = o->next) {
    4556      114752 :                 symbol *order = o->data.sym;
    4557             : 
    4558      114752 :                 if (order->token == SQL_COLUMN || order->token == SQL_IDENT) {
    4559      114752 :                         symbol *col = order->data.lval->h->data.sym;
    4560      114752 :                         int direction = order->data.lval->h->next->data.i_val;
    4561      114752 :                         sql_exp *e = NULL;
    4562             : 
    4563      114752 :                         assert(order->data.lval->h->next->type == type_int);
    4564      114752 :                         if ((selection = simple_selection(col)) != NULL) {
    4565           7 :                                 dnode *o = selection->h;
    4566           7 :                                 order = o->data.sym;
    4567           7 :                                 if (order->data.lval->h->type == type_symbol)
    4568           6 :                                         col = order->data.lval->h->data.sym;
    4569             :                                 /* remove optional name from selection */
    4570           7 :                                 order->data.lval->h->next = NULL;
    4571             :                         }
    4572             : 
    4573      114752 :                         if (col->token == SQL_COLUMN || col->token == SQL_IDENT || col->token == SQL_ATOM) {
    4574      114530 :                                 exp_kind ek = {type_value, card_column, FALSE};
    4575             : 
    4576      114530 :                                 e = rel_value_exp2(query, &rel, col, f, ek);
    4577             : 
    4578      114530 :                                 if (e && e->card <= CARD_ATOM) {
    4579        9364 :                                         sql_subtype *tpe = exp_subtype(e);
    4580             :                                         /* integer atom on the stack */
    4581        9364 :                                         if (!is_sql_window(f) && e->type == e_atom &&
    4582        9256 :                                             tpe->type->eclass == EC_NUM) {
    4583        9247 :                                                 atom *a = e->l;
    4584        9247 :                                                 int nr = (int)atom_get_int(a);
    4585             : 
    4586        9247 :                                                 e = exps_get_exp(rel->exps, nr);
    4587        9247 :                                                 if (!e)
    4588           3 :                                                         return sql_error(sql, 02, SQLSTATE(42000) "SELECT: the order by column number (%d) is not in the number of projections range (%d)", nr, list_length(rel->exps));
    4589        9244 :                                                 e = exp_ref(sql, e);
    4590             :                                         }
    4591       38420 :                                 } else if (e && exp_card(e) > rel->card) {
    4592           0 :                                         if (exp_name(e) && !has_label(e))
    4593           0 :                                                 return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column '%s' in query results without an aggregate function", exp_name(e));
    4594           0 :                                         return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column in query results without an aggregate function");
    4595             :                                 }
    4596      114527 :                                 if (e && !exp_name(e))
    4597          23 :                                         exp_label(sql->sa, e, ++sql->label);
    4598       47781 :                                 if (e && rel && is_project(rel->op)) {
    4599       39365 :                                         sql_exp *found = exps_find_exp(rel->exps, e);
    4600             : 
    4601       39365 :                                         if (!found) {
    4602          33 :                                                 if (needs_distinct)
    4603           0 :                                                         return sql_error(sql, 02, SQLSTATE(42000) "SELECT: with DISTINCT ORDER BY expressions must appear in select list");
    4604          33 :                                                 if (!is_freevar(e))
    4605          16 :                                                         append(rel->exps, e);
    4606             :                                         } else {
    4607             :                                                 e = found;
    4608             :                                         }
    4609       39365 :                                         if (!is_freevar(e))
    4610       39348 :                                                 e = exp_ref(sql, e);
    4611             :                                 }
    4612             :                         }
    4613             : 
    4614      114749 :                         if (rel && !e && sql->session->status != -ERR_AMBIGUOUS) {
    4615             :                                 /* reset error */
    4616       66963 :                                 sql->session->status = 0;
    4617       66963 :                                 sql->errstr[0] = '\0';
    4618             : 
    4619       66963 :                                 e = rel_order_by_column_exp(query, &rel, col, needs_distinct, sql_sel | sql_orderby | (f & sql_group_totals) | (f & sql_window));
    4620             :                         }
    4621       66968 :                         if (!e)
    4622             :                                 return NULL;
    4623      114706 :                         if (!exp_subtype(e))
    4624           1 :                                 return sql_error(sql, 01, SQLSTATE(42000) "Cannot have a parameter (?) for order by column");
    4625      114705 :                         set_direction(e, direction);
    4626      114705 :                         list_append(exps, e);
    4627             :                 } else {
    4628           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "SELECT: order not of type SQL_COLUMN");
    4629             :                 }
    4630             :         }
    4631       45468 :         if (is_sql_orderby(f) && or != rel)
    4632       36592 :                 or->l = rel;
    4633       45468 :         if (is_sql_window(f))
    4634        8876 :                 *R = rel;
    4635             :         return exps;
    4636             : }
    4637             : 
    4638             : static int
    4639         814 : generate_window_bound(tokens sql_token, bool first_half)
    4640             : {
    4641         814 :         switch (sql_token) {
    4642         408 :                 case SQL_PRECEDING:
    4643         408 :                         return first_half ? BOUND_FIRST_HALF_PRECEDING : BOUND_SECOND_HALF_PRECEDING;
    4644         284 :                 case SQL_FOLLOWING:
    4645         284 :                         return first_half ? BOUND_FIRST_HALF_FOLLOWING : BOUND_SECOND_HALF_FOLLOWING;
    4646         122 :                 case SQL_CURRENT_ROW:
    4647         122 :                         return first_half ? CURRENT_ROW_PRECEDING : CURRENT_ROW_FOLLOWING;
    4648             :                 default:
    4649           0 :                         assert(0);
    4650             :         }
    4651             :         return 0;
    4652             : }
    4653             : 
    4654             : /* window functions */
    4655             : static sql_exp*
    4656         407 : generate_window_bound_call(mvc *sql, sql_exp **estart, sql_exp **eend, sql_exp *pe, sql_exp *e,
    4657             :                                                    sql_exp *start, sql_exp *fend, int frame_type, int excl, tokens t1, tokens t2)
    4658             : {
    4659         407 :         list *rargs1 = sa_list(sql->sa), *rargs2 = sa_list(sql->sa), *targs1 = sa_list(sql->sa), *targs2 = sa_list(sql->sa);
    4660         407 :         sql_subfunc *dc1, *dc2;
    4661         407 :         sql_subtype *it = sql_bind_localtype("int");
    4662             : 
    4663         407 :         if (pe) {
    4664         186 :                 append(targs1, exp_subtype(pe));
    4665         186 :                 append(targs2, exp_subtype(pe));
    4666         186 :                 append(rargs1, exp_copy(sql, pe));
    4667         186 :                 append(rargs2, exp_copy(sql, pe));
    4668             :         }
    4669         407 :         append(rargs1, exp_copy(sql, e));
    4670         407 :         append(rargs2, exp_copy(sql, e));
    4671         407 :         append(targs1, exp_subtype(e));
    4672         407 :         append(targs2, exp_subtype(e));
    4673         407 :         append(targs1, it);
    4674         407 :         append(targs2, it);
    4675         407 :         append(targs1, it);
    4676         407 :         append(targs2, it);
    4677         407 :         append(targs1, it);
    4678         407 :         append(targs2, it);
    4679         407 :         append(targs1, exp_subtype(start));
    4680         407 :         append(targs2, exp_subtype(fend));
    4681             : 
    4682         407 :         dc1 = sql_bind_func_(sql, "sys", "window_bound", targs1, F_ANALYTIC, true, false);
    4683         407 :         dc2 = sql_bind_func_(sql, "sys", "window_bound", targs2, F_ANALYTIC, true, false);
    4684         407 :         if (!dc1 || !dc2)
    4685           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "SELECT: function 'window_bound' not found");
    4686         407 :         append(rargs1, exp_atom_int(sql->sa, frame_type));
    4687         407 :         append(rargs2, exp_atom_int(sql->sa, frame_type));
    4688         407 :         append(rargs1, exp_atom_int(sql->sa, generate_window_bound(t1, true)));
    4689         407 :         append(rargs2, exp_atom_int(sql->sa, generate_window_bound(t2, false)));
    4690         407 :         append(rargs1, exp_atom_int(sql->sa, excl));
    4691         407 :         append(rargs2, exp_atom_int(sql->sa, excl));
    4692         407 :         append(rargs1, start);
    4693         407 :         append(rargs2, fend);
    4694             : 
    4695         407 :         *estart = exp_op(sql->sa, rargs1, dc1);
    4696         407 :         *eend = exp_op(sql->sa, rargs2, dc2);
    4697         407 :         return e; /* return something to say there were no errors */
    4698             : }
    4699             : 
    4700             : #define EC_NUMERIC(e) (e==EC_NUM||EC_INTERVAL(e)||e==EC_DEC||e==EC_FLT)
    4701             : 
    4702             : static sql_exp*
    4703         601 : calculate_window_bound(sql_query *query, sql_rel *p, tokens token, symbol *bound, sql_exp *ie, int frame_type, int f)
    4704             : {
    4705         601 :         mvc *sql = query->sql;
    4706         601 :         sql_subtype *bt, *bound_tp = sql_bind_localtype("lng"), *iet = exp_subtype(ie);
    4707         601 :         sql_exp *res = NULL;
    4708             : 
    4709         792 :         if ((bound->token == SQL_PRECEDING || bound->token == SQL_FOLLOWING || bound->token == SQL_CURRENT_ROW) && bound->type == type_int) {
    4710         191 :                 atom *a = NULL;
    4711         191 :                 bt = (frame_type == FRAME_ROWS || frame_type == FRAME_GROUPS) ? bound_tp : iet;
    4712             : 
    4713         191 :                 if ((bound->data.i_val == UNBOUNDED_PRECEDING_BOUND || bound->data.i_val == UNBOUNDED_FOLLOWING_BOUND)) {
    4714          69 :                         a = atom_max_value(sql->sa, EC_NUMERIC(bt->type->eclass) ? bt : bound_tp);
    4715         122 :                 } else if (bound->data.i_val == CURRENT_ROW_BOUND) {
    4716         124 :                         a = atom_zero_value(sql->sa, EC_NUMERIC(bt->type->eclass) ? bt : bound_tp);
    4717             :                 } else {
    4718           0 :                         assert(0);
    4719             :                 }
    4720         191 :                 res = exp_atom(sql->sa, a);
    4721             :         } else { /* arbitrary expression case */
    4722         410 :                 exp_kind ek = {type_value, card_column, FALSE};
    4723         410 :                 const char *bound_desc = (token == SQL_PRECEDING) ? "PRECEDING" : "FOLLOWING";
    4724             : 
    4725         410 :                 assert(token == SQL_PRECEDING || token == SQL_FOLLOWING);
    4726         410 :                 if (!(res = rel_value_exp2(query, &p, bound, f, ek)))
    4727           3 :                         return NULL;
    4728         410 :                 if (!(bt = exp_subtype(res))) { /* frame bound is a parameter */
    4729           3 :                         sql_subtype *t = (frame_type == FRAME_ROWS || frame_type == FRAME_GROUPS) ? bound_tp : iet;
    4730           3 :                         if (rel_set_type_param(sql, t, p, res, 0) < 0) /* workaround */
    4731             :                                 return NULL;
    4732           3 :                         bt = exp_subtype(res);
    4733             :                 }
    4734         410 :                 if (exp_is_null_no_value_opt(res))
    4735           1 :                         return sql_error(sql, 02, SQLSTATE(42000) "%s offset must not be NULL", bound_desc);
    4736         409 :                 if ((frame_type == FRAME_ROWS || frame_type == FRAME_GROUPS) && bt->type->eclass != EC_NUM && !(res = exp_check_type(sql, bound_tp, p, res, type_equal)))
    4737             :                         return NULL;
    4738         409 :                 if (frame_type == FRAME_RANGE) {
    4739          70 :                         sql_class iet_class = iet->type->eclass;
    4740             : 
    4741          70 :                         if (!EC_NUMERIC(iet_class) && !EC_TEMP(iet_class))
    4742           1 :                                 return sql_error(sql, 02, SQLSTATE(42000) "Ranges with arbitrary expressions are available to numeric, interval and temporal types only");
    4743          12 :                         if (EC_NUMERIC(iet_class) && !(res = exp_check_type(sql, iet, p, res, type_equal)))
    4744             :                                 return NULL;
    4745          69 :                         if ((iet_class == EC_TIME || iet_class == EC_TIME_TZ) && bt->type->eclass != EC_SEC) {
    4746           1 :                                 (void) sql_error(sql, 02, SQLSTATE(42000) "For %s input the %s boundary must be an interval type up to the day", subtype2string2(sql->ta, iet), bound_desc);
    4747           1 :                                 sa_reset(sql->ta);
    4748           1 :                                 return NULL;
    4749             :                         }
    4750          68 :                         if (EC_TEMP(iet->type->eclass) && !EC_INTERVAL(bt->type->eclass)) {
    4751           0 :                                 (void) sql_error(sql, 02, SQLSTATE(42000) "For %s input the %s boundary must be an interval type", subtype2string2(sql->ta, iet), bound_desc);
    4752           0 :                                 sa_reset(sql->ta);
    4753           0 :                                 return NULL;
    4754             :                         }
    4755             :                 }
    4756             :         }
    4757             :         return res;
    4758             : }
    4759             : 
    4760             : static dlist*
    4761          16 : get_window_clauses(mvc *sql, char* ident, symbol **partition_by_clause, symbol **order_by_clause, symbol **frame_clause)
    4762             : {
    4763          16 :         dlist *window_specification = NULL;
    4764          16 :         char *window_ident;
    4765          16 :         int pos;
    4766             : 
    4767          16 :         if (mvc_highwater(sql))
    4768           0 :                 return sql_error(sql, 10, SQLSTATE(42000) "Query too complex: running out of stack space");
    4769             : 
    4770          16 :         if ((window_specification = frame_get_window_def(sql, ident, &pos)) == NULL)
    4771           1 :                 return sql_error(sql, 02, SQLSTATE(42000) "SELECT: window '%s' not found", ident);
    4772             : 
    4773             :         /* avoid infinite lookups */
    4774          15 :         if (frame_check_var_visited(sql, pos))
    4775           1 :                 return sql_error(sql, 02, SQLSTATE(42000) "SELECT: cyclic references to window '%s' found", ident);
    4776          14 :         frame_set_var_visited(sql, pos);
    4777             : 
    4778          14 :         if (window_specification->h->next->data.sym) {
    4779           2 :                 if (*partition_by_clause)
    4780           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "SELECT: redefinition of PARTITION BY clause from window '%s'", ident);
    4781           2 :                 *partition_by_clause = window_specification->h->next->data.sym;
    4782             :         }
    4783          14 :         if (window_specification->h->next->next->data.sym) {
    4784           4 :                 if (*order_by_clause)
    4785           1 :                         return sql_error(sql, 02, SQLSTATE(42000) "SELECT: redefinition of ORDER BY clause from window '%s'", ident);
    4786           3 :                 *order_by_clause = window_specification->h->next->next->data.sym;
    4787             :         }
    4788          13 :         if (window_specification->h->next->next->next->data.sym) {
    4789           0 :                 if (*frame_clause)
    4790           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "SELECT: redefinition of frame clause from window '%s'", ident);
    4791           0 :                 *frame_clause = window_specification->h->next->next->next->data.sym;
    4792             :         }
    4793             : 
    4794          13 :         window_ident = window_specification->h->data.sval;
    4795          13 :         if (window_ident && !get_window_clauses(sql, window_ident, partition_by_clause, order_by_clause, frame_clause))
    4796             :                 return NULL; /* the error was already set */
    4797             : 
    4798             :         return window_specification; /* return something to say there were no errors */
    4799             : }
    4800             : 
    4801             : /*
    4802             :  * select x, y, rank_op() over (partition by x order by y) as, ...
    4803             :                 aggr_op(z) over (partition by y order by x) as, ...
    4804             :  * from table [x,y,z,w,v]
    4805             :  *
    4806             :  * project and order by over x,y / y,x
    4807             :  * a = project( table ) [ x, y, z, w, v ], [ x, y]
    4808             :  * b = project( table ) [ x, y, z, w, v ], [ y, x]
    4809             :  *
    4810             :  * project with order dependent operators, ie combined prev/current value
    4811             :  * aa = project (a) [ x, y, r = rank_op(diff(x) (marks a new partition), rediff(diff(x), y) (marks diff value with in partition)), z, w, v ]
    4812             :  * project(aa) [ aa.x, aa.y, aa.r ] -- only keep current output list
    4813             :  * bb = project (b) [ x, y, a = aggr_op(z, diff(y), rediff(diff(y), x)), z, w, v ]
    4814             :  * project(bb) [ bb.x, bb.y, bb.a ]  -- only keep current output list
    4815             :  */
    4816             : static sql_exp *
    4817       17947 : rel_rankop(sql_query *query, sql_rel **rel, symbol *se, int f)
    4818             : {
    4819       17947 :         mvc *sql = query->sql;
    4820       17947 :         node *n;
    4821       17947 :         dlist *l = se->data.lval, *window_specification = NULL;
    4822       17947 :         symbol *window_function = l->h->data.sym, *partition_by_clause = NULL, *order_by_clause = NULL, *frame_clause = NULL;
    4823       17947 :         char *aname = NULL, *sname = NULL, *window_ident = NULL;
    4824       17947 :         sql_subfunc *wf = NULL;
    4825       17947 :         sql_exp *in = NULL, *pe = NULL, *oe = NULL, *call = NULL, *start = NULL, *eend = NULL, *fstart = NULL, *fend = NULL, *ie = NULL;
    4826       17947 :         sql_rel *p;
    4827       17947 :         list *gbe = NULL, *obe = NULL, *args = NULL, *types = NULL, *fargs = NULL;
    4828       17947 :         dnode *dn = window_function->data.lval->h, *dargs = NULL;
    4829       17947 :         int distinct = 0, frame_type, pos, nf = f, nfargs = 0;
    4830       17947 :         bool is_nth_value, supports_frames, found = false;
    4831             : 
    4832       17947 :         frame_clear_visited_flag(sql); /* clear visited flags before iterating */
    4833             : 
    4834       17947 :         if (l->h->next->type == type_list) {
    4835       17931 :                 window_specification = l->h->next->data.lval;
    4836          16 :         } else if (l->h->next->type == type_string) {
    4837          16 :                 const char* window_alias = l->h->next->data.sval;
    4838          16 :                 if ((window_specification = frame_get_window_def(sql, window_alias, &pos)) == NULL)
    4839           1 :                         return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "SELECT: window '%s' not found", window_alias);
    4840          15 :                 frame_set_var_visited(sql, pos);
    4841             :         } else {
    4842           0 :                 assert(0);
    4843             :         }
    4844             : 
    4845       17946 :         window_ident = window_specification->h->data.sval;
    4846       17946 :         partition_by_clause = window_specification->h->next->data.sym;
    4847       17946 :         order_by_clause = window_specification->h->next->next->data.sym;
    4848       17946 :         frame_clause = window_specification->h->next->next->next->data.sym;
    4849             : 
    4850       17946 :         if (window_ident && !get_window_clauses(sql, window_ident, &partition_by_clause, &order_by_clause, &frame_clause))
    4851             :                 return NULL;
    4852             : 
    4853       17943 :         frame_type = frame_clause ? frame_clause->data.lval->h->next->next->data.i_val : FRAME_RANGE;
    4854       17943 :         aname = qname_schema_object(dn->data.lval);
    4855       17943 :         sname = qname_schema(dn->data.lval);
    4856             : 
    4857       17943 :         is_nth_value = !strcmp(aname, "nth_value");
    4858       17943 :         bool is_value = is_nth_value || !strcmp(aname, "first_value") || !strcmp(aname, "last_value");
    4859       17943 :         supports_frames = window_function->token != SQL_RANK || is_value;
    4860             : 
    4861       17943 :         if (is_sql_update_set(f) || is_sql_psm(f) || is_sql_values(f) || is_sql_join(f) || is_sql_where(f) || is_sql_groupby(f) || is_sql_having(f) || is_psm_call(f) || is_sql_from(f)) {
    4862          13 :                 char *uaname = SA_NEW_ARRAY(sql->ta, char, strlen(aname) + 1);
    4863          13 :                 const char *clause = is_sql_update_set(f)||is_sql_psm(f)?"in SET, WHILE, IF, ELSE, CASE, WHEN, RETURN, ANALYZE clauses (use subquery)":is_sql_values(f)?"on an unique value":
    4864           8 :                                                          is_sql_join(f)?"in JOIN conditions":is_sql_where(f)?"in WHERE clause":is_sql_groupby(f)?"in GROUP BY clause":
    4865           4 :                                                          is_psm_call(f)?"in CALL":is_sql_from(f)?"in functions in FROM":"in HAVING clause";
    4866          13 :                 return sql_error(sql, 02, SQLSTATE(42000) "%s: window function '%s' not allowed %s", toUpperCopy(uaname, aname), aname, clause);
    4867       17930 :         } else if (is_sql_aggr(f)) {
    4868           2 :                 char *uaname = SA_NEW_ARRAY(sql->ta, char, strlen(aname) + 1);
    4869           2 :                 return sql_error(sql, 02, SQLSTATE(42000) "%s: window functions not allowed inside aggregation functions", toUpperCopy(uaname, aname));
    4870       17928 :         } else if (is_sql_window(f)) {
    4871           6 :                 char *uaname = SA_NEW_ARRAY(sql->ta, char, strlen(aname) + 1);
    4872           6 :                 return sql_error(sql, 02, SQLSTATE(42000) "%s: window functions cannot be nested", toUpperCopy(uaname, aname));
    4873             :         }
    4874       17922 :         if (window_function->token == SQL_UNOP || window_function->token == SQL_OP) {
    4875        3246 :                 window_function->token = SQL_NOP;
    4876        3246 :                 dn->next->next->data.lval = dlist_append_symbol(sql->sa, dlist_create( sql->sa ), dn->next->next->data.sym); /* make a list */
    4877             :         }
    4878       17922 :         if (window_function->token == SQL_BINOP) {
    4879        5149 :                 window_function->token = SQL_NOP;
    4880        5149 :                 dn->next->next->data.lval = dlist_append_symbol(sql->sa, dlist_append_symbol(sql->sa, dlist_create( sql->sa ), dn->next->next->data.sym), dn->next->next->next->data.sym); /* make a list */
    4881        5149 :                 dn->next->next->next = dn->next->next->next->next; /* skip second arg */
    4882             :         }
    4883       17922 :         if (window_function->token == SQL_AGGR)
    4884         106 :                 dn->next->next->data.lval = dlist_append_symbol(sql->sa, dlist_create( sql->sa ), dn->next->next->data.sym); /* make a list */
    4885       17922 :         if (window_function->token == SQL_NOP)
    4886        8397 :                 window_function->token = SQL_AGGR;
    4887       17922 :         if (window_function->token != SQL_RANK && window_function->token != SQL_AGGR) {
    4888           0 :                 char *uaname = SA_NEW_ARRAY(sql->ta, char, strlen(aname) + 1);
    4889           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "SELECT: window function '%s' unknown", toUpperCopy(uaname, aname));
    4890             :         }
    4891             : 
    4892             :         /* window operations are only allowed in the projection */
    4893       17922 :         if (!is_sql_sel(f))
    4894           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "OVER: only possible within the selection");
    4895             : 
    4896       17922 :         p = *rel;
    4897             :         /* Partition By */
    4898       17922 :         if (partition_by_clause) {
    4899        8208 :                 gbe = rel_partition_groupings(query, &p, partition_by_clause, NULL /* cannot use (selection) column references, as this result is a selection column */, nf | sql_window);
    4900        8208 :                 if (!gbe)
    4901             :                         return NULL;
    4902       16432 :                 for (n = gbe->h ; n ; n = n->next) {
    4903        8232 :                         sql_exp *en = n->data;
    4904             : 
    4905        8232 :                         set_ascending(en);
    4906        8232 :                         set_nulls_first(en);
    4907             :                 }
    4908             :         }
    4909             : 
    4910             :         /* Order By */
    4911       17914 :         if (order_by_clause) {
    4912        8883 :                 obe = rel_order_by(query, &p, order_by_clause, 0, sql_window);
    4913        8883 :                 if (!obe)
    4914             :                         return NULL;
    4915             :         }
    4916             : 
    4917       17907 :         fargs = sa_list(sql->sa);
    4918       17907 :         if (window_function->token == SQL_RANK) { /* rank function call */
    4919        9414 :                 dlist *dl = dn->next->next->data.lval;
    4920        9414 :                 bool is_lag = !strcmp(aname, "lag"), is_lead = !strcmp(aname, "lead"),
    4921        9414 :                          extra_input = !strcmp(aname, "ntile") || !strcmp(aname, "rank") || !strcmp(aname, "dense_rank") || !strcmp(aname, "row_number") || !strcmp(aname, "percent_rank") || !strcmp(aname, "cume_dist");
    4922             : 
    4923        9414 :                 distinct = dn->next->data.i_val;
    4924        9414 :                 if (extra_input) { /* pass an input column for analytic functions that don't require it */
    4925        9230 :                         sql_subfunc *star = sql_bind_func(sql, "sys", "star", NULL, NULL, F_FUNC, true, true);
    4926        9230 :                         in = exp_op(sql->sa, NULL, star);
    4927        9230 :                         append(fargs, in);
    4928             :                 }
    4929        9414 :                 if (dl)
    4930         593 :                         for (dargs = dl->h ; dargs ; dargs = dargs->next) {
    4931         339 :                                 exp_kind ek = {type_value, card_column, FALSE};
    4932         339 :                                 sql_subtype *empty = sql_bind_localtype("void"), *bte = sql_bind_localtype("bte");
    4933             : 
    4934         339 :                                 in = rel_value_exp2(query, &p, dargs->data.sym, f | sql_window | sql_farg, ek);
    4935         339 :                                 if (!in)
    4936           0 :                                         return NULL;
    4937         339 :                                 if (!exp_subtype(in)) { /* we also do not expect parameters here */
    4938           0 :                                         char *uaname = SA_NEW_ARRAY(sql->ta, char, strlen(aname) + 1);
    4939           0 :                                         return sql_error(sql, 02, SQLSTATE(42000) "%s: parameters not allowed as arguments to window functions", toUpperCopy(uaname, aname));
    4940             :                                 }
    4941         339 :                                 if (!exp_name(in))
    4942         151 :                                         exp_label(sql->sa, in, ++sql->label);
    4943             : 
    4944             :                                 /* corner case, if the argument is null convert it into something countable such as bte */
    4945         339 :                                 if (subtype_cmp(exp_subtype(in), empty) == 0)
    4946          20 :                                         in = exp_convert(sql, in, empty, bte);
    4947         339 :                                 if ((is_lag || is_lead) && nfargs == 2) { /* lag and lead 3rd arg must have same type as 1st arg */
    4948          10 :                                         sql_exp *first = (sql_exp*) fargs->h->data;
    4949          10 :                                         if (!(in = exp_check_type(sql, exp_subtype(first), p, in, type_equal)))
    4950             :                                                 return NULL;
    4951             :                                 }
    4952         329 :                                 if (!in)
    4953             :                                         return NULL;
    4954             : 
    4955         339 :                                 append(fargs, in);
    4956         339 :                                 in = exp_ref_save(sql, in);
    4957         339 :                                 nfargs++;
    4958             :                         }
    4959             :         } else { /* aggregation function call */
    4960        8493 :                 distinct = dn->next->data.i_val;
    4961       22022 :                 for (dargs = dn->next->next->data.lval->h ; dargs && dargs->data.sym ; dargs = dargs->next) {
    4962       13537 :                         exp_kind ek = {type_value, card_column, FALSE};
    4963       13537 :                         sql_subtype *empty = sql_bind_localtype("void"), *bte = sql_bind_localtype("bte");
    4964             : 
    4965       13537 :                         in = rel_value_exp2(query, &p, dargs->data.sym, f | sql_window | sql_farg, ek);
    4966       13537 :                         if (!in)
    4967           8 :                                 return NULL;
    4968       13530 :                         if (!exp_subtype(in)) { /* we also do not expect parameters here */
    4969           1 :                                 char *uaname = SA_NEW_ARRAY(sql->ta, char, strlen(aname) + 1);
    4970           1 :                                 return sql_error(sql, 02, SQLSTATE(42000) "%s: parameters not allowed as arguments to window functions", toUpperCopy(uaname, aname));
    4971             :                         }
    4972       13529 :                         if (!exp_name(in))
    4973       10112 :                                 exp_label(sql->sa, in, ++sql->label);
    4974             : 
    4975             :                         /* corner case, if the argument is null convert it into something countable such as bte */
    4976       13529 :                         if (subtype_cmp(exp_subtype(in), empty) == 0)
    4977          46 :                                 in = exp_convert(sql, in, empty, bte);
    4978       13529 :                         if (!in)
    4979             :                                 return NULL;
    4980             : 
    4981       13529 :                         append(fargs, in);
    4982       13529 :                         in = exp_ref_save(sql, in);
    4983       13529 :                         nfargs++;
    4984             : 
    4985       13529 :                         if (!strcmp(aname, "count"))
    4986          86 :                                 append(fargs, exp_atom_bool(sql->sa, 1)); /* ignore nills */
    4987             :                 }
    4988             : 
    4989        8485 :                 if (!nfargs) { /* count(*) */
    4990         109 :                         if (window_function->token == SQL_AGGR && strcmp(aname, "count") != 0) {
    4991           2 :                                 char *uaname = SA_NEW_ARRAY(sql->ta, char, strlen(aname) + 1);
    4992           2 :                                 return sql_error(sql, 02, SQLSTATE(42000) "%s: unable to perform '%s(*)'", toUpperCopy(uaname, aname), aname);
    4993             :                         }
    4994         107 :                         sql_subfunc *star = sql_bind_func(sql, "sys", "star", NULL, NULL, F_FUNC, true, true);
    4995         107 :                         in = exp_op(sql->sa, NULL, star);
    4996         107 :                         append(fargs, in);
    4997         107 :                         append(fargs, exp_atom_bool(sql->sa, 0)); /* don't ignore nills */
    4998             :                 }
    4999             :         }
    5000             : 
    5001       17897 :         if (distinct)
    5002           4 :                 return sql_error(sql, 02, SQLSTATE(42000) "SELECT: DISTINCT clause is not implemented for window functions");
    5003             : 
    5004             :         /* diff for partitions */
    5005       17893 :         if (gbe) {
    5006        8191 :                 sql_subtype *bt = sql_bind_localtype("bit");
    5007             : 
    5008       16413 :                 for( n = gbe->h; n; n = n->next) {
    5009        8223 :                         sql_subfunc *df;
    5010        8223 :                         sql_exp *e = n->data;
    5011             : 
    5012        8223 :                         if (!exp_subtype(e))
    5013           1 :                                 return sql_error(sql, 02, SQLSTATE(42000) "SELECT: parameters not allowed at PARTITION BY clause from window functions");
    5014             : 
    5015        8222 :                         e = exp_copy(sql, e);
    5016        8222 :                         args = sa_list(sql->sa);
    5017        8222 :                         if (pe) {
    5018          32 :                                 df = sql_bind_func(sql, "sys", "diff", bt, exp_subtype(e), F_ANALYTIC, true, true);
    5019          32 :                                 append(args, pe);
    5020             :                         } else {
    5021        8190 :                                 df = sql_bind_func(sql, "sys", "diff", exp_subtype(e), NULL, F_ANALYTIC, true, true);
    5022             :                         }
    5023        8222 :                         if (!df)
    5024           0 :                                 return sql_error(sql, 02, SQLSTATE(42000) "SELECT: function 'diff' not found");
    5025        8222 :                         append(args, e);
    5026        8222 :                         pe = exp_op(sql->sa, args, df);
    5027             :                 }
    5028             :         } else {
    5029        9702 :                 pe = exp_atom_bool(sql->sa, 0);
    5030             :         }
    5031             : 
    5032             :         /* diff for orderby */
    5033       17892 :         if (obe) {
    5034        8874 :                 sql_subtype *bt = sql_bind_localtype("bit");
    5035             : 
    5036       17904 :                 for( n = obe->h; n; n = n->next) {
    5037        9030 :                         sql_subfunc *df;
    5038        9030 :                         sql_exp *e = n->data;
    5039             : 
    5040        9030 :                         if (!exp_subtype(e))
    5041           0 :                                 return sql_error(sql, 02, SQLSTATE(42000) "SELECT: parameters not allowed at ORDER BY clause from window functions");
    5042             : 
    5043        9030 :                         e = exp_copy(sql, e);
    5044        9030 :                         args = sa_list(sql->sa);
    5045        9030 :                         if (oe) {
    5046         156 :                                 df = sql_bind_func(sql, "sys", "diff", bt, exp_subtype(e), F_ANALYTIC, true, true);
    5047         156 :                                 append(args, oe);
    5048             :                         } else {
    5049        8874 :                                 df = sql_bind_func(sql, "sys", "diff", exp_subtype(e), NULL, F_ANALYTIC, true, true);
    5050             :                         }
    5051        9030 :                         if (!df)
    5052           0 :                                 return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "SELECT: function 'diff' not found");
    5053        9030 :                         append(args, e);
    5054        9030 :                         oe = exp_op(sql->sa, args, df);
    5055             :                 }
    5056             :         } else {
    5057        9018 :                 oe = exp_atom_bool(sql->sa, 0);
    5058             :         }
    5059             : 
    5060       17892 :         if (frame_clause || supports_frames) {
    5061        8596 :                 if (frame_type == FRAME_RANGE)
    5062        8285 :                         ie = obe ? (sql_exp*) obe->t->data : in;
    5063             :                 else
    5064         311 :                         ie = obe ? oe : in;
    5065             :         }
    5066       17892 :         assert(oe && pe);
    5067             : 
    5068       17892 :         types = exp_types(sql->sa, fargs);
    5069       17892 :         wf = bind_func_(sql, sname, aname, types, F_ANALYTIC, false, &found, false);
    5070       17892 :         if (wf && !list_empty(fargs) && !(fargs = check_arguments_and_find_largest_any_type(sql, NULL, fargs, wf, 0, false)))
    5071             :                 wf = NULL;
    5072       17887 :         if (!wf) {
    5073           5 :                 char *arg_list = nfargs ? nary_function_arg_types_2str(sql, types, nfargs) : NULL;
    5074          10 :                 return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "SELECT: %s window function %s%s%s'%s'(%s)",
    5075           5 :                                                  found ? "insufficient privileges for" : "no such", sname ? "'":"", sname ? sname : "", sname ? "'.":"", aname, arg_list ? arg_list : "");
    5076             :         }
    5077             : 
    5078             :         /* Frame */
    5079       17887 :         if (frame_clause) {
    5080         410 :                 dnode *d = frame_clause->data.lval->h;
    5081         410 :                 symbol *wstart = d->data.sym, *wend = d->next->data.sym, *rstart = wstart->data.lval->h->data.sym,
    5082         410 :                            *rend = wend->data.lval->h->data.sym;
    5083         410 :                 int excl = d->next->next->next->data.i_val;
    5084         410 :                 bool shortcut = false;
    5085             : 
    5086         410 :                 if (!supports_frames)
    5087           4 :                         return sql_error(sql, 02, SQLSTATE(42000) "OVER: frame extend only possible with aggregation and first_value, last_value and nth_value functions");
    5088         406 :                 if (excl != EXCLUDE_NONE)
    5089           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "Only EXCLUDE NO OTHERS exclusion is currently implemented");
    5090         406 :                 if (list_empty(obe) && frame_type == FRAME_GROUPS)
    5091           2 :                         return sql_error(sql, 02, SQLSTATE(42000) "GROUPS frame requires an order by expression");
    5092         404 :                 if (wstart->token == SQL_FOLLOWING && wend->token == SQL_PRECEDING)
    5093           1 :                         return sql_error(sql, 02, SQLSTATE(42000) "FOLLOWING offset must come after PRECEDING offset");
    5094         403 :                 if (wstart->token == SQL_CURRENT_ROW && wend->token == SQL_PRECEDING)
    5095           1 :                         return sql_error(sql, 02, SQLSTATE(42000) "CURRENT ROW offset must come after PRECEDING offset");
    5096         402 :                 if (wstart->token == SQL_FOLLOWING && wend->token == SQL_CURRENT_ROW)
    5097           1 :                         return sql_error(sql, 02, SQLSTATE(42000) "FOLLOWING offset must come after CURRENT ROW offset");
    5098         401 :                 if (wstart->token != SQL_CURRENT_ROW && wend->token != SQL_CURRENT_ROW && wstart->token == wend->token && frame_type != FRAME_ROWS)
    5099           4 :                         return sql_error(sql, 02, SQLSTATE(42000) "Non-centered windows are only supported in row frames");
    5100         367 :                 if (frame_type == FRAME_RANGE) {
    5101          97 :                         if (((wstart->token == SQL_PRECEDING || wstart->token == SQL_FOLLOWING) && rstart->token != SQL_PRECEDING && rstart->token != SQL_CURRENT_ROW && rstart->token != SQL_FOLLOWING) ||
    5102          59 :                                 ((wend->token == SQL_PRECEDING || wend->token == SQL_FOLLOWING) && rend->token != SQL_PRECEDING && rend->token != SQL_CURRENT_ROW && rend->token != SQL_FOLLOWING)) {
    5103          40 :                                 if (list_empty(obe))
    5104           2 :                                         return sql_error(sql, 02, SQLSTATE(42000) "RANGE frame with PRECEDING/FOLLOWING offset requires an order by expression");
    5105          38 :                                 if (list_length(obe) > 1)
    5106           0 :                                         return sql_error(sql, 02, SQLSTATE(42000) "RANGE with offset PRECEDING/FOLLOWING requires exactly one ORDER BY column");
    5107             :                         }
    5108             :                 }
    5109             : 
    5110         395 :                 if (list_empty(obe) && frame_type == FRAME_RANGE) { /* window functions are weird */
    5111             :                         frame_type = FRAME_ALL;
    5112             :                         shortcut = true;
    5113         383 :                 } else if (!is_value && (rstart->token == SQL_PRECEDING || rstart->token == SQL_CURRENT_ROW || rstart->token == SQL_FOLLOWING) && rstart->type == type_int &&
    5114         150 :                         (rend->token == SQL_PRECEDING || rend->token == SQL_CURRENT_ROW || rend->token == SQL_FOLLOWING) && rend->type == type_int) {
    5115             :                          /* special cases, don't calculate bounds */
    5116         146 :                         if (frame_type != FRAME_ROWS && rstart->data.i_val == UNBOUNDED_PRECEDING_BOUND && rend->data.i_val == CURRENT_ROW_BOUND) {
    5117             :                                 frame_type = FRAME_UNBOUNDED_TILL_CURRENT_ROW;
    5118             :                                 shortcut = true;
    5119          21 :                         } else if (frame_type != FRAME_ROWS && rstart->data.i_val == CURRENT_ROW_BOUND && rend->data.i_val == UNBOUNDED_FOLLOWING_BOUND) {
    5120             :                                 frame_type = FRAME_CURRENT_ROW_TILL_UNBOUNDED;
    5121             :                                 shortcut = true;
    5122          86 :                         } else if (rstart->data.i_val == UNBOUNDED_PRECEDING_BOUND && rend->data.i_val == UNBOUNDED_FOLLOWING_BOUND) {
    5123             :                                 frame_type = FRAME_ALL;
    5124             :                                 shortcut = true;
    5125          76 :                         } else if (rstart->data.i_val == CURRENT_ROW_BOUND && rend->data.i_val == CURRENT_ROW_BOUND) {
    5126         395 :                                 frame_type = FRAME_CURRENT_ROW;
    5127         395 :                                 shortcut = true;
    5128             :                         }
    5129             :                 }
    5130         395 :                 if (!shortcut) {
    5131         302 :                         if (!(fstart = calculate_window_bound(query, p, wstart->token, rstart, ie, frame_type, f | sql_window)))
    5132             :                                 return NULL;
    5133         299 :                         if (!(fend = calculate_window_bound(query, p, wend->token, rend, ie, frame_type, f | sql_window)))
    5134             :                                 return NULL;
    5135         471 :                         if (!generate_window_bound_call(sql, &start, &eend, gbe ? pe : NULL, ie, fstart, fend, frame_type, excl,
    5136             :                                                                                         wstart->token, wend->token))
    5137             :                                 return NULL;
    5138             :                 }
    5139       17477 :         } else if (supports_frames) { /* for analytic functions with no frame clause, we use the standard default values */
    5140        8183 :                 if (is_value) {
    5141         108 :                         sql_subtype *bound_tp = sql_bind_localtype("lng"), *bt = (frame_type == FRAME_ROWS || frame_type == FRAME_GROUPS) ? bound_tp : exp_subtype(ie);
    5142         108 :                         unsigned char sclass = bt->type->eclass;
    5143             : 
    5144         136 :                         fstart = exp_atom(sql->sa, atom_max_value(sql->sa, EC_NUMERIC(sclass) ? bt : bound_tp));
    5145         108 :                         fend = order_by_clause ? exp_atom(sql->sa, atom_zero_value(sql->sa, EC_NUMERIC(sclass) ? bt : bound_tp)) :
    5146          23 :                                                                          exp_atom(sql->sa, atom_max_value(sql->sa, EC_NUMERIC(sclass) ? bt : bound_tp));
    5147             : 
    5148         157 :                         if (generate_window_bound_call(sql, &start, &eend, gbe ? pe : NULL, ie, fstart, fend, frame_type, EXCLUDE_NONE, SQL_PRECEDING, SQL_FOLLOWING) == NULL)
    5149             :                                 return NULL;
    5150             :                 } else {
    5151        8075 :                         frame_type = list_empty(obe) ? FRAME_ALL : FRAME_UNBOUNDED_TILL_CURRENT_ROW;
    5152             :                 }
    5153             :         }
    5154             : 
    5155       17869 :         args = sa_list(sql->sa);
    5156       41217 :         for (node *n = fargs->h ; n ; n = n->next)
    5157       23348 :                 list_append(args, n->data);
    5158       17869 :         list_append(args, pe);
    5159       17869 :         list_append(args, oe);
    5160       17869 :         if (supports_frames) {
    5161        8575 :                 list_append(args, exp_atom_int(sql->sa, frame_type));
    5162        8575 :                 list_append(args, start ? start : exp_atom_oid(sql->sa, 1));
    5163        8575 :                 list_append(args, eend ? eend : exp_atom_oid(sql->sa, 1));
    5164             :         }
    5165       17869 :         call = exp_rank_op(sql->sa, list_empty(args) ? NULL : args, gbe, obe, wf);
    5166       17869 :         *rel = p;
    5167       17869 :         return call;
    5168             : }
    5169             : 
    5170             : sql_exp *
    5171     5748960 : rel_value_exp2(sql_query *query, sql_rel **rel, symbol *se, int f, exp_kind ek)
    5172             : {
    5173     5748960 :         mvc *sql = query->sql;
    5174     5748960 :         if (!se)
    5175             :                 return NULL;
    5176             : 
    5177     5748960 :         if (mvc_highwater(sql))
    5178           0 :                 return sql_error(sql, 10, SQLSTATE(42000) "Query too complex: running out of stack space");
    5179             : 
    5180     5748960 :         if (rel && *rel && (*rel)->card == CARD_AGGR) { /* group by expression case, handle it before */
    5181      105577 :                 sql_exp *exp = NULL;
    5182      105577 :                 if (!is_sql_aggr(f) && !is_sql_window(f))
    5183      105118 :                         exp = frame_get_groupby_expression(sql, se);
    5184      105577 :                 if (sql->errstr[0] != '\0')
    5185             :                         return NULL;
    5186      105577 :                 if (exp) {
    5187          78 :                         sql_exp *res = exp_ref(sql, exp);
    5188          78 :                         res->card = (*rel)->card;
    5189          78 :                         if (se->token == SQL_AGGR) {
    5190           0 :                                 dlist *l = se->data.lval;
    5191           0 :                                 int distinct = l->h->next->data.i_val;
    5192           0 :                                 if (distinct)
    5193           0 :                                         set_distinct(res);
    5194             :                         }
    5195          78 :                         if (!query_has_outer(query) && is_groupby((*rel)->op))
    5196          74 :                                 res = rel_groupby_add_aggr(sql, *rel, res);
    5197          78 :                         return res;
    5198             :                 }
    5199             :         }
    5200             : 
    5201     5748882 :         switch (se->token) {
    5202        1827 :         case SQL_OP:
    5203        1827 :                 return rel_op(query, rel, se, f, ek);
    5204       77657 :         case SQL_UNOP:
    5205       77657 :                 return rel_unop(query, rel, se, f, ek);
    5206      373954 :         case SQL_BINOP:
    5207      373954 :                 return rel_binop(query, rel, se, f, ek);
    5208       84771 :         case SQL_NOP:
    5209       84771 :                 return rel_nop(query, rel, se, f, ek);
    5210       18726 :         case SQL_AGGR:
    5211       18726 :                 return rel_aggr(query, rel, se, f);
    5212       17947 :         case SQL_WINDOW:
    5213       17947 :                 return rel_rankop(query, rel, se, f);
    5214     2318804 :         case SQL_IDENT:
    5215             :         case SQL_COLUMN:
    5216     2318804 :                 return rel_column_ref(query, rel, se, f );
    5217        3007 :         case SQL_NAME: {
    5218        3007 :                 dlist *l = se->data.lval;
    5219        3007 :                 const char *sname = qname_schema(l);
    5220        3007 :                 const char *vname = qname_schema_object(l);
    5221        3007 :                 return rel_exp_variable_on_scope(sql, sname, vname);
    5222             :         }
    5223       48172 :         case SQL_VALUES:
    5224             :         case SQL_WITH:
    5225             :         case SQL_SELECT: {
    5226       48172 :                 sql_rel *r = NULL;
    5227             : 
    5228       48172 :                 if (is_psm_call(f) || is_sql_merge(f))
    5229           8 :                         return sql_error(sql, 02, SQLSTATE(42000) "%s: subqueries not supported inside %s", is_psm_call(f) ? "CALL" : "MERGE", is_psm_call(f) ? "CALL statements" : "MERGE conditions");
    5230       48168 :                 if (rel && *rel)
    5231       34143 :                         query_push_outer(query, *rel, f);
    5232       48168 :                 if (se->token == SQL_WITH) {
    5233          30 :                         r = rel_with_query(query, se);
    5234       48138 :                 } else if (se->token == SQL_VALUES) {
    5235         218 :                         r = rel_values(query, se, NULL);
    5236             :                 } else {
    5237       47920 :                         assert(se->token == SQL_SELECT);
    5238       47920 :                         exp_kind nek = ek;
    5239       47920 :                         nek.aggr = is_sql_aggr(f);
    5240       47920 :                         if (is_sql_no_subquery(f))
    5241           2 :                                 return sql_error(sql, 02, SQLSTATE(42000) "SELECT: subquery not allowed");
    5242             : 
    5243       47918 :                         r = rel_subquery(query, se, nek);
    5244       47918 :                         if (r)
    5245       47818 :                                 exps_label(sql, r->exps);
    5246             :                 }
    5247       48166 :                 if (rel && *rel) {
    5248       34141 :                         *rel = query_pop_outer(query);
    5249       34141 :                         if (is_sql_join(f) && is_groupby((*rel)->op)) {
    5250           0 :                                 return sql_error(sql, 05, SQLSTATE(42000) "SELECT: aggregate functions not allowed in JOIN conditions");
    5251       34141 :                         } else if (is_sql_where(f) && is_groupby((*rel)->op)) {
    5252           0 :                                 return sql_error(sql, 02, SQLSTATE(42000) "SELECT: aggregate functions not allowed in WHERE clause");
    5253       34141 :                         } else if ((is_sql_update_set(f) || is_sql_psm(f)) && is_groupby((*rel)->op)) {
    5254           0 :                                 return sql_error(sql, 02, SQLSTATE(42000) "SELECT: aggregate functions not allowed in SET, WHILE, IF, ELSE, CASE, WHEN, RETURN, ANALYZE clauses");
    5255             :                         }
    5256             :                 }
    5257       48166 :                 if (!r)
    5258             :                         return NULL;
    5259       48063 :                 if (ek.type == type_value && ek.card <= card_set && is_project(r->op) && list_length(r->exps) > 1)
    5260          16 :                         return sql_error(sql, 02, SQLSTATE(42000) "SELECT: subquery must return only one column");
    5261       48047 :                 if (ek.type == type_relation && is_project(r->op) && list_length(r->exps) != ek.type)
    5262           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "SELECT: subquery has too %s columns", list_length(r->exps) < ek.type ? "few" : "many");
    5263       48047 :                 if (ek.type == type_value && list_length(r->exps) == 1 && !is_sql_psm(f)) /* for now don't rename multi attribute results */
    5264       35880 :                         r = rel_zero_or_one(sql, r, ek);
    5265       48047 :                 return exp_rel(sql, r);
    5266             :         }
    5267         215 :         case SQL_TABLE: {
    5268             :                 /* turn a subquery into a tabular result */
    5269         215 :                 *rel = rel_selects(query, se->data.sym);
    5270         215 :                 if (*rel)
    5271         213 :                         return lastexp(*rel);
    5272             :                 return NULL;
    5273             :         }
    5274        1612 :         case SQL_PARAMETER: {
    5275        1612 :                 assert(se->type == type_int);
    5276        1612 :                 sql_arg *a = sql_bind_paramnr(sql, se->data.i_val);
    5277        1612 :                 if (sql->emode != m_prepare) {
    5278           1 :                         if (a && a->name && a->name[0])
    5279           1 :                                 return sql_error(sql, 02, SQLSTATE(42000) "SELECT: named placeholder ('%s') but named values list is missing", a->name);
    5280             :                         else
    5281           0 :                                 return sql_error(sql, 02, SQLSTATE(42000) "SELECT: parameters ('?') not allowed in normal queries, use PREPARE");
    5282             :                 }
    5283        1611 :                 return exp_atom_ref(sql->sa, se->data.i_val, a?&a->type:NULL);
    5284             :         }
    5285      105270 :         case SQL_NULL:
    5286      105270 :                 return exp_null(sql->sa, sql_bind_localtype("void"));
    5287        1590 :         case SQL_NEXT:
    5288        1590 :                 return rel_next_value_for(sql, se);
    5289      169087 :         case SQL_CAST:
    5290      169087 :                 return rel_cast(query, rel, se, f);
    5291      104571 :         case SQL_CASE:
    5292             :         case SQL_COALESCE:
    5293             :         case SQL_NULLIF:
    5294      104571 :                 return rel_case_exp(query, rel, se, f);
    5295           1 :         case SQL_RANK:
    5296           1 :                 return sql_error(sql, 02, SQLSTATE(42000) "SELECT: window function %s requires an OVER clause", qname_schema_object(se->data.lval->h->data.lval));
    5297          25 :         case SQL_XMLELEMENT:
    5298             :         case SQL_XMLFOREST:
    5299             :         case SQL_XMLCOMMENT:
    5300             :         case SQL_XMLATTRIBUTE:
    5301             :         case SQL_XMLCONCAT:
    5302             :         case SQL_XMLDOCUMENT:
    5303             :         case SQL_XMLPI:
    5304             :         case SQL_XMLTEXT:
    5305          25 :                 return rel_xml(query, rel, se, f, ek);
    5306     2421646 :         default:
    5307     2421646 :                 return rel_logical_value_exp(query, rel, se, f, ek);
    5308             :         }
    5309             : }
    5310             : 
    5311             : static int exps_has_rank(list *exps);
    5312             : 
    5313             : static int
    5314             : exp_has_rank(sql_exp *e)
    5315             : {
    5316             :         switch(e->type) {
    5317             :         case e_convert:
    5318             :                 return exp_has_rank(e->l);
    5319             :         case e_func:
    5320             :                 if (e->r)
    5321             :                         return 1;
    5322             :                 /* fall through */
    5323             :         case e_aggr:
    5324             :                 return exps_has_rank(e->l);
    5325             :         case e_cmp:
    5326             :                 if (e->flag == cmp_or || e->flag == cmp_filter)
    5327             :                         return exps_has_rank(e->l) || exps_has_rank(e->r);
    5328             :                 if (e->flag == cmp_in || e->flag == cmp_notin)
    5329             :                         return exp_has_rank(e->l) || exps_has_rank(e->r);
    5330             :                 return exp_has_rank(e->l) || exp_has_rank(e->r) || (e->f && exp_has_rank(e->f));
    5331             :         default:
    5332             :                 return 0;
    5333             :         }
    5334             : }
    5335             : 
    5336             : /* TODO create exps_has (list, fptr ) */
    5337             : static int
    5338             : exps_has_rank(list *exps)
    5339             : {
    5340             :         if (!exps || list_empty(exps))
    5341             :                 return 0;
    5342             :         for(node *n = exps->h; n; n=n->next){
    5343             :                 sql_exp *e = n->data;
    5344             : 
    5345             :                 if (exp_has_rank(e))
    5346             :                         return 1;
    5347             :         }
    5348             :         return 0;
    5349             : }
    5350             : 
    5351             : sql_exp *
    5352     4428009 : rel_value_exp(sql_query *query, sql_rel **rel, symbol *se, int f, exp_kind ek)
    5353             : {
    5354     4428009 :         if (!se)
    5355             :                 return NULL;
    5356             : 
    5357     4428009 :         if (mvc_highwater(query->sql))
    5358           0 :                 return sql_error(query->sql, 10, SQLSTATE(42000) "Query too complex: running out of stack space");
    5359             : 
    5360     4428009 :         sql_exp *e = rel_value_exp2(query, rel, se, f, ek);
    5361     4427998 :         if (e && (se->token == SQL_SELECT || se->token == SQL_TABLE) && !exp_is_rel(e)) {
    5362           0 :                 assert(*rel);
    5363           0 :                 return rel_lastexp(query->sql, *rel);
    5364             :         }
    5365             :         return e;
    5366             : }
    5367             : 
    5368             : static sql_exp *
    5369     1305222 : column_exp(sql_query *query, sql_rel **rel, symbol *column_e, int f)
    5370             : {
    5371     1305222 :         dlist *l = column_e->data.lval;
    5372     1305222 :         exp_kind ek = {type_value, card_column, FALSE};
    5373     1305222 :         sql_exp *ve;
    5374             : 
    5375     1305222 :         if (f == sql_sel && rel && *rel && (*rel)->card < CARD_AGGR)
    5376       82420 :                 ek.card = card_value;
    5377     1305222 :         ve = rel_value_exp(query, rel, l->h->data.sym, f, ek);
    5378     1305212 :         if (!ve)
    5379             :                 return NULL;
    5380             :         /* AS name */
    5381     1304104 :         if (ve && l->h->next->data.sval)
    5382      492792 :                 exp_setname(query->sql, ve, NULL, l->h->next->data.sval);
    5383             :         return ve;
    5384             : }
    5385             : 
    5386             : static int
    5387      806984 : exp_is_not_intern(sql_exp *e)
    5388             : {
    5389      806984 :         return is_intern(e)?-1:0;
    5390             : }
    5391             : 
    5392             : static void
    5393      122291 : rel_remove_internal_exp(sql_rel *rel)
    5394             : {
    5395      122291 :         if (rel->exps) {
    5396      122291 :                 list *n_exps = list_select(rel->exps, rel, (fcmp)&exp_is_not_intern, (fdup)NULL);
    5397             : 
    5398      122291 :                 rel->exps = n_exps;
    5399             :         }
    5400      122291 : }
    5401             : 
    5402             : static inline int
    5403          16 : exp_key(sql_exp *e)
    5404             : {
    5405          16 :         if (e->alias.name)
    5406          16 :                 return hash_key(e->alias.name);
    5407             :         return 0;
    5408             : }
    5409             : 
    5410             : static list *
    5411           5 : group_merge_exps(mvc *sql, list *gexps, list *exps)
    5412             : {
    5413           5 :         int nexps = list_length(gexps) + list_length(exps);
    5414             : 
    5415           5 :         sql_hash *ht = hash_new(sql->ta, nexps, (fkeyvalue)&exp_key);
    5416             : 
    5417           9 :         for (node *n = gexps->h; n ; n = n->next) { /* first add grouping expressions */
    5418           4 :                 sql_exp *e = n->data;
    5419           4 :                 int key = ht->key(e);
    5420             : 
    5421           4 :                 hash_add(ht, key, e);
    5422             :         }
    5423             : 
    5424          17 :         for (node *n = exps->h; n ; n = n->next) { /* then test if the new grouping expressions are already there */
    5425          12 :                 sql_exp *e = n->data;
    5426          12 :                 int key = ht->key(e);
    5427          12 :                 sql_hash_e *he = ht->buckets[key&(ht->size-1)];
    5428          12 :                 bool duplicates = false;
    5429             : 
    5430          16 :                 for (; he && !duplicates; he = he->chain) {
    5431           4 :                         sql_exp *f = he->value;
    5432             : 
    5433           4 :                         if (!exp_equal(e, f))
    5434           4 :                                 duplicates = true;
    5435             :                 }
    5436          12 :                 hash_add(ht, key, e);
    5437          12 :                 if (!duplicates) {
    5438           8 :                         list_append(gexps, e);
    5439           8 :                         n->data = exp_ref(sql, e);
    5440             :                 }
    5441             :         }
    5442           5 :         return gexps;
    5443             : }
    5444             : 
    5445             : static list *
    5446       58133 : rel_table_exp(sql_query *query, sql_rel **rel, symbol *column_e, bool single_exp )
    5447             : {
    5448       58133 :         mvc *sql = query->sql;
    5449       58133 :         if (column_e->token == SQL_TABLE && column_e->data.lval->h->type == type_symbol) {
    5450           0 :                 sql_rel *r;
    5451             : 
    5452           0 :                 if (!is_project((*rel)->op))
    5453             :                         return NULL;
    5454           0 :                 r = rel_named_table_function(query, (*rel)->l, column_e, 0, NULL);
    5455           0 :                 if (!r)
    5456             :                         return NULL;
    5457           0 :                 *rel = r;
    5458           0 :                 return sa_list(sql->sa);
    5459       58133 :         } else if (column_e->token == SQL_TABLE) {
    5460       57025 :                 char *tname = column_e->data.lval->h->data.sval;
    5461       57025 :                 list *exps = NULL;
    5462       57025 :                 sql_rel *project = *rel, *groupby = NULL;
    5463             : 
    5464             :                 /* if there's a group by relation in the tree, skip it for the '*' case and use the underlying projection */
    5465       57025 :                 if (project) {
    5466       66570 :                         while (is_groupby(project->op) || is_select(project->op)) {
    5467        9545 :                                 if (is_groupby(project->op))
    5468           5 :                                         groupby = project;
    5469        9545 :                                 if (project->l)
    5470        9545 :                                         project = project->l;
    5471             :                         }
    5472             :                         assert(project);
    5473             :                 }
    5474             : 
    5475       57025 :                 if (project->op == op_project && project->l && project == *rel && !tname && !rel_is_ref(project) && !need_distinct(project) && single_exp) {
    5476         412 :                         sql_rel *l = project->l;
    5477         412 :                         if (!l || !is_project(l->op) || list_length(project->exps) == list_length(l->exps)) {
    5478         405 :                                 rel_remove_internal_exp(*rel);
    5479         405 :                                 exps = project->exps;
    5480         405 :                                 *rel = project->l;
    5481             :                         }
    5482             :                 }
    5483       57025 :                 if ((exps || (exps = rel_table_projections(sql, project, tname, 0)) != NULL) && !list_empty(exps)) {
    5484       57021 :                         if (!(exps = check_distinct_exp_names(sql, exps)))
    5485           0 :                                 return sql_error(sql, 02, SQLSTATE(42000) "Duplicate column names in table%s%s%s projection list", tname ? " '" : "", tname ? tname : "", tname ? "'" : "");
    5486       57021 :                         if (groupby) {
    5487           5 :                                 groupby->exps = group_merge_exps(sql, groupby->exps, exps);
    5488           9 :                                 for (node *n = groupby->exps->h ; n ; n = n->next) {
    5489           8 :                                         sql_exp *e = n->data;
    5490             : 
    5491           8 :                                         if (e->card > groupby->card) {
    5492           4 :                                                 if (exp_name(e) && !has_label(e))
    5493           4 :                                                         return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column '%s' in query results without an aggregate function", exp_name(e));
    5494           0 :                                                 return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column in query results without an aggregate function");
    5495             :                                         }
    5496             :                                 }
    5497             :                         }
    5498       57017 :                         return exps;
    5499             :                 }
    5500           4 :                 if (!tname)
    5501           2 :                         return sql_error(sql, 02, SQLSTATE(42000) "Table expression without table name");
    5502           2 :                 return sql_error(sql, 02, SQLSTATE(42000) "Column expression Table '%s' unknown", tname);
    5503             :         }
    5504             :         return NULL;
    5505             : }
    5506             : 
    5507             : sql_exp *
    5508     1362247 : rel_column_exp(sql_query *query, sql_rel **rel, symbol *column_e, int f)
    5509             : {
    5510     1362247 :         if (column_e->token == SQL_COLUMN || column_e->token == SQL_IDENT)
    5511     1305220 :                 return column_exp(query, rel, column_e, f);
    5512             :         return NULL;
    5513             : }
    5514             : 
    5515             : static sql_rel*
    5516      369005 : rel_where_groupby_nodes(sql_query *query, sql_rel *rel, SelectNode *sn, int *group_totals)
    5517             : {
    5518      369005 :         mvc *sql = query->sql;
    5519             : 
    5520      369005 :         if (sn->where) {
    5521      145147 :                 rel = rel_logical_exp(query, rel, sn->where, sql_where);
    5522      145147 :                 if (!rel) {
    5523          73 :                         if (sql->errstr[0] == 0)
    5524           0 :                                 return sql_error(sql, 02, SQLSTATE(42000) "Subquery result missing");
    5525             :                         return NULL;
    5526             :                 }
    5527             :         }
    5528      368932 :         query_processed(query);
    5529             : 
    5530      368930 :         if (rel && sn->groupby) {
    5531        9771 :                 list *gbe = NULL, *sets = NULL;
    5532        9771 :                 int all = 0;
    5533        9771 :                 if (sn->groupby->data.lval == NULL) { /* ALL */
    5534             :                         all = 1;
    5535             :                 } else {
    5536       33106 :                         for (dnode *o = sn->groupby->data.lval->h; o ; o = o->next) {
    5537       23462 :                                 symbol *grouping = o->data.sym;
    5538       23462 :                                 if (grouping->token == SQL_ROLLUP || grouping->token == SQL_CUBE || grouping->token == SQL_GROUPING_SETS) {
    5539         119 :                                         *group_totals |= sql_group_totals;
    5540         119 :                                         break;
    5541             :                                 }
    5542             :                         }
    5543        9763 :                         gbe = rel_groupings(query, &rel, sn->groupby, sn->selection, sql_sel | sql_groupby | *group_totals, false, &sets);
    5544        9763 :                         if (!gbe)
    5545          19 :                                 return NULL;
    5546             :                 }
    5547        9752 :                 rel = rel_groupby(sql, rel, gbe);
    5548        9752 :                 if (rel && all)
    5549           8 :                         rel->flag = 2;
    5550        9752 :                 if (sets && list_length(sets) > 1) { /* if there is only one combination, there is no reason to generate unions */
    5551         114 :                         prop *p = prop_create(sql->sa, PROP_GROUPINGS, rel->p);
    5552         114 :                         p->value.pval = sets;
    5553         114 :                         rel->p = p;
    5554             :                 }
    5555             :         }
    5556             : 
    5557      368911 :         if (rel && sn->having) {
    5558             :                 /* having implies group by, ie if not supplied do a group by */
    5559        1764 :                 if (rel->op != op_groupby)
    5560         128 :                         rel = rel_groupby(sql, rel, NULL);
    5561             :         }
    5562      368911 :         query_processed(query);
    5563      368910 :         return rel;
    5564             : }
    5565             : 
    5566             : static sql_rel*
    5567      367783 : rel_having_limits_nodes(sql_query *query, sql_rel *rel, SelectNode *sn, exp_kind ek, int group_totals)
    5568             : {
    5569      367783 :         mvc *sql = query->sql;
    5570      367783 :         sql_rel *inner = NULL;
    5571      367783 :         int single_value = 1;
    5572             : 
    5573      367783 :         if (is_project(rel->op) && rel->l) {
    5574      367783 :                 inner = rel->l;
    5575      367783 :                 single_value = 0;
    5576             :         }
    5577             : 
    5578      367783 :         if (sn->having) {
    5579        1752 :                 if (inner && is_groupby(inner->op))
    5580        1752 :                         set_processed(inner);
    5581        1752 :                 if (!(inner = rel_logical_exp(query, inner, sn->having, sql_having | group_totals)))
    5582             :                         return NULL;
    5583        1713 :                 if (inner->exps && exps_card(inner->exps) > CARD_AGGR)
    5584           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "SELECT: cannot compare sets with values, probably an aggregate function missing");
    5585        1713 :                 if (!single_value)
    5586        1713 :                         rel->l = inner;
    5587             :         }
    5588             : 
    5589      367744 :         if (rel && sn->distinct)
    5590        1027 :                 rel = rel_distinct(rel);
    5591             : 
    5592      367744 :         if (rel && sn->orderby) {
    5593       36634 :                 list *obe = NULL;
    5594       36634 :                 sql_rel *sel = NULL, *l = rel->l;
    5595             : 
    5596             :                 /* project( select ) */
    5597       36634 :                 if (sn->having && is_select(l->op)) {
    5598          52 :                         sel = l;
    5599          52 :                         rel->l = l->l;
    5600             :                 }
    5601       36634 :                 rel = rel_orderby(sql, rel);
    5602       36634 :                 set_processed(rel);
    5603       36634 :                 obe = rel_order_by(query, &rel, sn->orderby, sn->distinct, sql_orderby | group_totals);
    5604       36634 :                 if (!obe)
    5605             :                         return NULL;
    5606       36594 :                 rel->r = obe;
    5607       36594 :                 if (sel) {
    5608          51 :                         sql_rel *o = rel, *p = o->l;
    5609          51 :                         p->l = sel;
    5610             :                 }
    5611             :         }
    5612      367704 :         if (!rel)
    5613             :                 return NULL;
    5614             : 
    5615      367704 :         if (sn->limit || sn->offset) {
    5616       16505 :                 sql_subtype *lng = sql_bind_localtype("lng");
    5617       16508 :                 list *exps = new_exp_list(sql->sa);
    5618             : 
    5619       16508 :                 if (sn->limit) {
    5620       16482 :                         sql_exp *l = rel_value_exp(query, NULL, sn->limit, 0, ek);
    5621             : 
    5622       16482 :                         if (!l || !(l=exp_check_type(sql, lng, NULL, l, type_equal)))
    5623           0 :                                 return NULL;
    5624       16482 :                         if ((ek.card != card_relation && sn->limit) &&
    5625             :                                 (ek.card == card_value && sn->limit)) {
    5626          11 :                                 sql_subfunc *zero_or_one = sql_bind_func(sql, "sys", "zero_or_one", exp_subtype(l), NULL, F_AGGR, true, true);
    5627          11 :                                 l = exp_aggr1(sql->sa, l, zero_or_one, 0, 0, CARD_ATOM, has_nil(l));
    5628             :                         }
    5629       16482 :                         list_append(exps, l);
    5630             :                 } else
    5631          26 :                         list_append(exps, exp_atom(sql->sa, atom_general(sql->sa, lng, NULL, 0)));
    5632       16508 :                 if (sn->offset) {
    5633          85 :                         sql_exp *o = rel_value_exp( query, NULL, sn->offset, 0, ek);
    5634          85 :                         if (!o || !(o=exp_check_type(sql, lng, NULL, o, type_equal)))
    5635           0 :                                 return NULL;
    5636          85 :                         list_append(exps, o);
    5637             :                 }
    5638       16508 :                 rel = rel_topn(sql->sa, rel, exps);
    5639             :         }
    5640             : 
    5641      367707 :         if (sn->sample || sn->seed) {
    5642          22 :                 list *exps = new_exp_list(sql->sa);
    5643             : 
    5644          24 :                 if (sn->sample) {
    5645          23 :                         sql_exp *s = rel_value_exp(query, NULL, sn->sample, 0, ek);
    5646          23 :                         if (!s)
    5647             :                                 return NULL;
    5648          23 :                         if (!exp_subtype(s) && rel_set_type_param(sql, sql_bind_localtype("lng"), NULL, s, 0) < 0)
    5649             :                                 return NULL;
    5650          23 :                         list_append(exps, s);
    5651             :                 } else {
    5652           1 :                         assert(sn->seed);
    5653           1 :                         return sql_error(sql, 02, SQLSTATE(42000) "SEED: cannot have SEED without SAMPLE");
    5654             :                 }
    5655          23 :                 if (sn->seed) {
    5656          12 :                         sql_exp *e = rel_value_exp(query, NULL, sn->seed, 0, ek);
    5657          12 :                         if (!e || !(e=exp_check_type(sql, sql_bind_localtype("int"), NULL, e, type_equal)))
    5658           0 :                                 return NULL;
    5659          12 :                         list_append(exps, e);
    5660             :                 }
    5661          23 :                 rel = rel_sample(sql->sa, rel, exps);
    5662             :         }
    5663             : 
    5664             :         /* after parsing the current query, set the group by relation as processed */
    5665      367708 :         if (!sn->having && inner && is_groupby(inner->op))
    5666       28265 :                 set_processed(inner);
    5667      367708 :         if (rel)
    5668      367708 :                 set_processed(rel);
    5669             :         return rel;
    5670             : }
    5671             : 
    5672             : static sql_rel *
    5673          60 : join_on_column_name(sql_query *query, sql_rel *rel, sql_rel *t1, sql_rel *t2, int op, int l_nil, int r_nil)
    5674             : {
    5675          60 :         mvc *sql = query->sql;
    5676          60 :         int found = 0, full = (op == op_full), right = (op == op_right);
    5677          60 :         list *exps = rel_projections(sql, t1, NULL, 1, 0);
    5678          60 :         list *r_exps = rel_projections(sql, t2, NULL, 1, 0);
    5679          60 :         list *outexps = new_exp_list(sql->sa);
    5680             : 
    5681          60 :         if (!exps || !r_exps)
    5682             :                 return NULL;
    5683         236 :         for (node *n = exps->h; n; n = n->next) {
    5684         179 :                 sql_exp *le = n->data;
    5685         179 :                 int multi = 0;
    5686         179 :                 const char *rname = exp_relname(le), *name = exp_name(le);
    5687         179 :                 sql_exp *re = exps_bind_column(r_exps, name, NULL, &multi, 0);
    5688             : 
    5689         179 :                 if (re) {
    5690         136 :                         if (multi)
    5691           3 :                                 return sql_error(sql, ERR_AMBIGUOUS, SQLSTATE(42000) "NATURAL JOIN: common column name '%s' appears more than once in right table", rname);
    5692         134 :                         multi = 0;
    5693         134 :                         le = exps_bind_column(exps, name, NULL, &multi, 0);
    5694         134 :                         if (multi)
    5695           0 :                                 return sql_error(sql, ERR_AMBIGUOUS, SQLSTATE(42000) "NATURAL JOIN: common column name '%s' appears more than once in left table", rname);
    5696             : 
    5697         134 :                         found = 1;
    5698         134 :                         if (!(rel = rel_compare_exp(query, rel, le, re, "=", TRUE, 0, 0, 0, 0)))
    5699             :                                 return NULL;
    5700         133 :                         list_remove_data(r_exps, NULL, re);
    5701         133 :                         if (full) {
    5702          12 :                                 sql_exp *cond = rel_unop_(sql, rel, le, "sys", "isnull", card_value);
    5703          12 :                                 if (!cond)
    5704             :                                         return NULL;
    5705          12 :                                 set_has_no_nil(cond);
    5706          12 :                                 if (rel_convert_types(sql, NULL, NULL, &le, &re, 1, type_equal_no_any) < 0)
    5707             :                                         return NULL;
    5708          12 :                                 if (!(le = rel_nop_(sql, rel, cond, re, le, NULL, "sys", "ifthenelse", card_value)))
    5709             :                                         return NULL;
    5710         121 :                         } else if (right) {
    5711           3 :                                 le = re;
    5712             :                         }
    5713         133 :                         exp_setname(sql, le, rname, name);
    5714         133 :                         set_not_unique(le);
    5715         133 :                         append(outexps, le);
    5716             :                 } else {
    5717          43 :                         if (l_nil)
    5718           5 :                                 set_has_nil(le);
    5719          43 :                         set_not_unique(le);
    5720          43 :                         append(outexps, le);
    5721             :                 }
    5722             :         }
    5723          57 :         if (!found)
    5724           2 :                 return sql_error(sql, 02, SQLSTATE(42000) "JOIN: no columns of tables '%s' and '%s' match", rel_name(t1)?rel_name(t1):"", rel_name(t2)?rel_name(t2):"");
    5725          86 :         for (node *n = r_exps->h; n; n = n->next) {
    5726          31 :                 sql_exp *re = n->data;
    5727          31 :                 if (r_nil)
    5728           3 :                         set_has_nil(re);
    5729          31 :                 set_not_unique(re);
    5730          31 :                 append(outexps, re);
    5731             :         }
    5732          55 :         rel = rel_project(sql->sa, rel, outexps);
    5733          55 :         return rel;
    5734             : }
    5735             : 
    5736             : static sql_rel *
    5737      369005 : rel_select_exp(sql_query *query, sql_rel *rel, SelectNode *sn, exp_kind ek)
    5738             : {
    5739      369005 :         mvc *sql = query->sql;
    5740      369005 :         sql_rel *inner = NULL;
    5741      369005 :         int group_totals = 0;
    5742      369005 :         list *pexps = NULL;
    5743             : 
    5744      369005 :         assert(sn->s.token == SQL_SELECT);
    5745      369005 :         if (!sn->selection)
    5746           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "SELECT: the selection or from part is missing");
    5747             : 
    5748      369005 :         if (!rel)
    5749       72570 :                 rel = rel_project_exp(sql, exp_atom_bool(sql->sa, 1));
    5750      369003 :         rel = rel_where_groupby_nodes(query, rel, sn, &group_totals);
    5751      369003 :         if (sql->session->status) /* rel might be NULL as input, so we have to check for the session status for errors */
    5752             :                 return NULL;
    5753             : 
    5754      368911 :         inner = rel;
    5755      368911 :         pexps = sa_list(sql->sa);
    5756     1730032 :         for (dnode *n = sn->selection->h; n; n = n->next) {
    5757             :                 /* Here we could get real column expressions
    5758             :                  * (including single atoms) but also table results.
    5759             :                  * Therefore we try both rel_column_exp
    5760             :                  * and rel_table_exp.
    5761             :                  */
    5762     1362246 :                 list *te = NULL;
    5763     2724433 :                 sql_exp *ce = rel_column_exp(query, &inner, n->data.sym, sql_sel | group_totals | (ek.aggr?sql_aggr:0));
    5764             : 
    5765     1362237 :                 if (ce) {
    5766     1304104 :                         if (inner && inner->flag && is_groupby(inner->op)) {
    5767          62 :                                 int found = 0;
    5768          62 :                                 list *gbe = inner->r;
    5769             :                                 /* flag == 2 just add to group by/ aggrs and ref-to pexps*/
    5770             :                                 /* flag == 1 find group by exp referencing this column nr */
    5771          62 :                                 if (inner->flag == 2) {
    5772          25 :                                         if (ce->card > CARD_AGGR) {
    5773           9 :                                                 if (!gbe)
    5774           8 :                                                         inner->r = gbe = sa_list(sql->sa);
    5775           9 :                                                 append(gbe, ce);
    5776           9 :                                                 ce = exp_ref(sql, ce);
    5777           9 :                                                 ce->card = CARD_AGGR;
    5778           9 :                                                 list_append(inner->exps, ce);
    5779           9 :                                                 ce = exp_ref(sql, ce);
    5780           9 :                                                 found = 1;
    5781             :                                         }
    5782             :                                 } else {
    5783          81 :                                         for(node *n = gbe->h; n && !found; n = n->next) {
    5784          44 :                                                 sql_exp *e = n->data;
    5785          44 :                                                 if (is_atom(e->type) && !e->alias.name) {
    5786          29 :                                                         atom *a = e->l;
    5787          29 :                                                         int nr = (int)atom_get_int(a);
    5788          29 :                                                         if (nr == (list_length(pexps) + 1)) {
    5789          27 :                                                                 n->data = ce;
    5790          27 :                                                                 ce = exp_ref(sql, ce);
    5791          27 :                                                                 ce->card = CARD_AGGR;
    5792          27 :                                                                 list_append(inner->exps, ce);
    5793          27 :                                                                 ce = exp_ref(sql, ce);
    5794          27 :                                                                 found = 1;
    5795             :                                                         }
    5796             :                                                 }
    5797             :                                         }
    5798             :                                 }
    5799             :                         }
    5800     1304104 :                         pexps = append(pexps, ce);
    5801     1304104 :                         rel = inner;
    5802     1304104 :                         continue;
    5803             :                 } else {
    5804       63145 :                         te = rel_table_exp(query, &rel, n->data.sym, !list_length(pexps) && !n->next);
    5805             :                 }
    5806       58133 :                 if (!ce && !te) {
    5807        1116 :                         if (sql->errstr[0])
    5808             :                                 return NULL;
    5809           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "SELECT: subquery result missing");
    5810             :                 }
    5811             :                 /* here we should merge the column expressions we
    5812             :                  * obtained so far with the table expression, ie
    5813             :                  * t1.* or a subquery.
    5814             :                  */
    5815       57017 :                 pexps = list_merge(pexps, te, (fdup)NULL);
    5816             :         }
    5817      367786 :         if (rel && is_groupby(rel->op) && rel->flag) {
    5818          34 :                 list *gbe = rel->r;
    5819          34 :                 if (!list_empty(gbe)) {
    5820          68 :                         for (node *n=gbe->h; n; n = n->next) {
    5821          37 :                                 sql_exp *e = n->data;
    5822          37 :                                 if (rel->flag == 1 && is_atom(e->type) && !e->alias.name) {
    5823           1 :                                         atom *a = e->l;
    5824           1 :                                         int nr = (int)atom_get_int(a);
    5825           1 :                                         return sql_error(sql, 02, SQLSTATE(42000) "SELECT: GROUP BY position %d is not in select list", nr);
    5826             :                                 }
    5827          36 :                                 if (exp_has_aggr(rel, e))
    5828           2 :                                         return sql_error(sql, 02, SQLSTATE(42000) "SELECT: aggregate functions are not allowed in GROUP BY");
    5829             :                         }
    5830             :                 }
    5831             :         }
    5832      367783 :         if (rel && is_groupby(rel->op) && (!sn->groupby || rel->flag) && !is_processed(rel)) {
    5833       41666 :                 for (node *n=pexps->h; n; n = n->next) {
    5834       21296 :                         sql_exp *ce = n->data;
    5835       21296 :                         if (rel->card < ce->card && !exp_is_aggr(rel, ce)) {
    5836           0 :                                 if (exp_name(ce) && !has_label(ce))
    5837           0 :                                         return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column '%s' in query results without an aggregate function", exp_name(ce));
    5838           0 :                                 return sql_error(sql, ERR_GROUPBY, SQLSTATE(42000) "SELECT: cannot use non GROUP BY column in query results without an aggregate function");
    5839             :                         }
    5840             :                 }
    5841       20370 :                 set_processed(rel);
    5842             :         }
    5843      367783 :         rel = rel_project(sql->sa, rel, pexps);
    5844             : 
    5845      367782 :         rel = rel_having_limits_nodes(query, rel, sn, ek, group_totals);
    5846      367782 :         return rel;
    5847             : }
    5848             : 
    5849             : static sql_rel*
    5850      121838 : rel_unique_names(mvc *sql, sql_rel *rel)
    5851             : {
    5852      121838 :         list *l;
    5853             : 
    5854      121838 :         if (!is_project(rel->op))
    5855             :                 return rel;
    5856      121838 :         l = sa_list(sql->sa);
    5857      926362 :         for (node *n = rel->exps->h; n; n = n->next) {
    5858      804524 :                 sql_exp *e = n->data;
    5859      804524 :                 const char *name = exp_name(e);
    5860             : 
    5861             :                 /* If there are two identical expression names, there will be ambiguity */
    5862      804524 :                 if (!name || exps_bind_column(l, name, NULL, NULL, 0))
    5863       55459 :                         exp_label(sql->sa, e, ++sql->label);
    5864      804524 :                 append(l,e);
    5865             :         }
    5866      121838 :         rel->exps = l;
    5867      121838 :         return rel;
    5868             : }
    5869             : 
    5870             : static sql_rel *
    5871      369298 : rel_query(sql_query *query, symbol *sq, exp_kind ek)
    5872             : {
    5873      369298 :         mvc *sql = query->sql;
    5874      369298 :         sql_rel *res = NULL;
    5875      369298 :         SelectNode *sn = NULL;
    5876             : 
    5877      369298 :         if (sq->token != SQL_SELECT)
    5878          22 :                 return table_ref(query, sq, 0, NULL);
    5879             : 
    5880             :         /* select ... into is currently not handled here ! */
    5881      369276 :         sn = (SelectNode *) sq;
    5882      369276 :         if (sn->into)
    5883             :                 return NULL;
    5884             : 
    5885      369276 :         if (ek.card != card_relation && sn->orderby)
    5886           0 :                 return sql_error(sql, 01, SQLSTATE(42000) "SELECT: ORDER BY only allowed on outermost SELECT");
    5887             : 
    5888      369276 :         if (sn->window) {
    5889          20 :                 dlist *wl = sn->window->data.lval;
    5890          51 :                 for (dnode *n = wl->h; n ; n = n->next) {
    5891          32 :                         dlist *wd = n->data.sym->data.lval;
    5892          32 :                         const char *name = wd->h->data.sval;
    5893          32 :                         dlist *wdef = wd->h->next->data.lval;
    5894          32 :                         if (frame_get_window_def(sql, name, NULL)) {
    5895           1 :                                 return sql_error(sql, 01, SQLSTATE(42000) "SELECT: Redefinition of window '%s'", name);
    5896          31 :                         } else if (!frame_push_window_def(sql, name, wdef)) {
    5897           0 :                                 return sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    5898             :                         }
    5899             :                 }
    5900             :         }
    5901             : 
    5902      369275 :         if (sn->from) {
    5903      296705 :                 dlist *fl = sn->from->data.lval;
    5904      296705 :                 sql_rel *fnd = NULL;
    5905      296705 :                 list *refs = new_exp_list(sql->sa); /* Keep list of relation names in order to test for duplicates */
    5906             : 
    5907      735576 :                 for (dnode *n = fl->h; n ; n = n->next) {
    5908      439141 :                         int lateral = check_is_lateral(n->data.sym);
    5909             : 
    5910             :                         /* just used current expression */
    5911      439141 :                         if (lateral && res)
    5912          11 :                                 query_push_outer(query, res, sql_from);
    5913      439141 :                         fnd = table_ref(query, n->data.sym, lateral, refs);
    5914      439141 :                         if (lateral && res)
    5915          11 :                                 res = query_pop_outer(query);
    5916      439141 :                         if (!fnd)
    5917             :                                 break;
    5918      438871 :                         if (res) {
    5919      142422 :                                 res = rel_crossproduct(sql->sa, res, fnd, op_join);
    5920      142422 :                                 if (lateral)
    5921          11 :                                         set_dependent(res);
    5922             :                         } else {
    5923             :                                 res = fnd;
    5924             :                         }
    5925             :                 }
    5926      296705 :                 if (!fnd) {
    5927         270 :                         if (res)
    5928          14 :                                 rel_destroy(res);
    5929         270 :                         return NULL;
    5930             :                 }
    5931       72570 :         } else if (!query_has_outer(query) || !res) {/* only on top level query */
    5932       72570 :                 return rel_select_exp(query, NULL, sn, ek);
    5933             :         }
    5934             : 
    5935      296435 :         sql_rel *rel = NULL;
    5936      296435 :         if (res)
    5937      296435 :                 rel = rel_select_exp(query, res, sn, ek);
    5938      296435 :         if (!rel && res)
    5939        1048 :                 rel_destroy(res);
    5940             :         return rel;
    5941             : }
    5942             : 
    5943             : /* NOTE: does NOT "set" query but instead generate set ops (union, except, intersect) */
    5944             : static sql_rel *
    5945          23 : rel_setquery_corresponding(sql_query *query, sql_rel *l, sql_rel *r, dlist *cols, int op, int outer, bool n_ary_op)
    5946             : {
    5947          23 :         mvc *sql = query->sql;
    5948          23 :         const char *opname = op==SQL_EXCEPT?"EXCEPT":op==SQL_INTERSECT?"INTERSECT":outer?"OUTER UNION":"UNION";
    5949          23 :         list *lexps = sa_list(query->sql->sa), *rexps = sa_list(query->sql->sa);
    5950          23 :         if (!lexps || !rexps)
    5951             :                 return NULL;
    5952          23 :         assert(cols);
    5953          23 :         if (dlist_length(cols)) {
    5954          22 :                 for (dnode *dn = cols->h; dn; dn = dn ->next) {
    5955          13 :                         char *nm = dn->data.sym->data.lval->h->data.sval;
    5956          13 :                         sql_exp *ls, *rs;
    5957             : 
    5958          13 :                         if (!(ls = rel_bind_column(sql, l, nm, sql_where | sql_join, 0)) && sql->session->status == -ERR_AMBIGUOUS)
    5959             :                                 return NULL;
    5960          13 :                         if (!(rs = rel_bind_column(sql, r, nm, sql_where | sql_join, 0)) && sql->session->status == -ERR_AMBIGUOUS)
    5961             :                                 return NULL;
    5962          13 :                         if ((!outer && (!ls || !rs)) || (outer && !ls && !rs))
    5963           2 :                                 return sql_error(sql, 02, SQLSTATE(42000) "%s: tables '%s' and '%s' do not have a matching column '%s'", opname, rel_name(l)?rel_name(l):"", rel_name(r)?rel_name(r):"", nm);
    5964          11 :                         if (outer && !ls)
    5965           4 :                                 ls = exp_null(sql->sa, exp_subtype(rs));
    5966          11 :                         if (outer && !rs)
    5967           4 :                                 rs = exp_null(sql->sa, exp_subtype(ls));
    5968          11 :                         append(lexps, ls);
    5969          11 :                         append(rexps, rs);
    5970             :                 }
    5971             :         } else {
    5972          12 :                 int found = 0;
    5973          12 :                 list *exps = rel_projections(sql, l, NULL, 1, 0), *r_exps = rel_projections(sql, r, NULL, 1, 0);
    5974          12 :                 if (!exps || !r_exps)
    5975             :                         return NULL;
    5976             :                 /* find cols which exist on both sides */
    5977          35 :                 for (node *n = exps->h; n; n = n->next) {
    5978          23 :                         sql_exp *le = n->data;
    5979          23 :                         int multi = 0;
    5980          23 :                         const char *rname = exp_relname(le), *name = exp_name(le);
    5981          23 :                         sql_exp *re = exps_bind_column(r_exps, name, NULL, &multi, 0);
    5982          23 :                         if (re) {
    5983          13 :                                 if (multi)
    5984           0 :                                         return sql_error(sql, ERR_AMBIGUOUS, SQLSTATE(42000) "%s: common column name '%s' appears more than once in right table", opname, rname);
    5985          13 :                                 multi = 0;
    5986          13 :                                 le = exps_bind_column(exps, name, NULL, &multi, 0);
    5987          13 :                                 if (multi)
    5988           0 :                                         return sql_error(sql, ERR_AMBIGUOUS, SQLSTATE(42000) "%s: common column name '%s' appears more than once in left table", opname, rname);
    5989             : 
    5990          13 :                                 found = 1;
    5991          13 :                                 append(lexps, le);
    5992          13 :                                 append(rexps, re);
    5993          13 :                                 list_remove_data(r_exps, NULL, re);
    5994          10 :                         } else if (outer) {
    5995           3 :                                 append(lexps, le);
    5996           3 :                                 re = exp_null(sql->sa, exp_subtype(le));
    5997           3 :                                 append(rexps, re); /* nils */
    5998             :                         }
    5999             :                 }
    6000          12 :                 if (!found)
    6001           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "%s: no columns of tables '%s' and '%s' match", opname, rel_name(l)?rel_name(l):"", rel_name(r)?rel_name(r):"");
    6002          12 :                 if (outer) {
    6003           7 :                         for (node *n = r_exps->h; n; n = n->next) {
    6004           4 :                                 sql_exp *re = n->data, *le;
    6005           4 :                                 append(rexps, re);
    6006           4 :                                 le = exp_null(sql->sa, exp_subtype(re));
    6007           4 :                                 append(lexps, le); /* nils */
    6008             :                         }
    6009             :                 }
    6010             :         }
    6011          21 :         return n_ary_op ?
    6012          21 :                 rel_setop_n_ary_check_types(sql, l, r, lexps, rexps, (operator_type)op) :
    6013           2 :                 rel_setop_check_types(sql, l, r, lexps, rexps, (operator_type)op);
    6014             : }
    6015             : 
    6016             : static sql_rel *
    6017        2481 : rel_setquery_(sql_query *query, sql_rel *l, sql_rel *r, dlist *cols, int op, int outer)
    6018             : {
    6019        2481 :         mvc *sql = query->sql;
    6020        2481 :         sql_rel *rel;
    6021             : 
    6022        2481 :         if (outer && !cols)
    6023           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "UNION: OUTER needs to be combined with CORRESPONDING [ BY ( column list ) ]");
    6024        2481 :         if (!cols) {
    6025        2479 :                 list *ls, *rs;
    6026             : 
    6027        2479 :                 l = rel_unique_names(sql, l);
    6028        2479 :                 r = rel_unique_names(sql, r);
    6029        2479 :                 ls = rel_projections(sql, l, NULL, 0, 1);
    6030        2479 :                 rs = rel_projections(sql, r, NULL, 0, 1);
    6031        2479 :                 rel = rel_setop_check_types(sql, l, r, ls, rs, (operator_type)op);
    6032             :         } else {
    6033           2 :                 rel = rel_setquery_corresponding(query, l, r, cols, op, outer, false);
    6034             :         }
    6035        2481 :         if (rel) {
    6036        2478 :                 rel_setop_set_exps(sql, rel, rel_projections(sql, rel, NULL, 0, 1), false);
    6037        2478 :                 set_processed(rel);
    6038             :         }
    6039             :         return rel;
    6040             : }
    6041             : 
    6042             : /* Generate n-ary set operator */
    6043             : static sql_rel *
    6044       58461 : rel_setquery_n_ary_(sql_query *query, sql_rel *l, sql_rel *r, dlist *cols, int op, int outer)
    6045             : {
    6046             :         /* even though this is for a general n-ary operators in this phase of the query
    6047             :          * processing we gonna have only two operands (so technically it's binary). In
    6048             :          * general this op supports arbitrary number of operands.
    6049             :          */
    6050             :         // TODO: for now we support only multi-union
    6051       58461 :         assert(op == op_munion);
    6052             : 
    6053       58461 :         mvc *sql = query->sql;
    6054       58461 :         sql_rel *rel;
    6055             : 
    6056       58461 :         if (outer && !cols)
    6057           0 :                         return sql_error(sql, 02, SQLSTATE(42000) "UNION: OUTER needs to be combined with CORRESPONDING [ BY ( column list ) ]");
    6058       58461 :         if (!cols) {
    6059             :                 // TODO: make rel_setop_n_ary_check_types to accept a list of rels
    6060             :                 // and a list of lists of exps
    6061       58440 :                 list *ls, *rs;
    6062             : 
    6063       58440 :                 l = rel_unique_names(sql, l);
    6064       58440 :                 r = rel_unique_names(sql, r);
    6065       58440 :                 ls = rel_projections(sql, l, NULL, 0, 1);
    6066       58440 :                 rs = rel_projections(sql, r, NULL, 0, 1);
    6067       58440 :                 rel = rel_setop_n_ary_check_types(sql, l, r, ls, rs, (operator_type)op);
    6068             :         } else {
    6069          21 :                 rel = rel_setquery_corresponding(query, l, r, cols, op, outer, true);
    6070             :         }
    6071             : 
    6072       58461 :         if (rel) {
    6073       58455 :                 rel_setop_n_ary_set_exps(sql, rel, rel_projections(sql, rel, NULL, 0, 1), false);
    6074       58455 :                 set_processed(rel);
    6075             :         }
    6076             : 
    6077             :         return rel;
    6078             : 
    6079             : }
    6080             : 
    6081             : static sql_rel *
    6082       60949 : rel_setquery(sql_query *query, symbol *q)
    6083             : {
    6084       60949 :         mvc *sql = query->sql;
    6085       60949 :         sql_rel *res = NULL;
    6086       60949 :         dnode *n = q->data.lval->h;
    6087       60949 :         symbol *tab_ref1 = n->data.sym;
    6088       60949 :         int distinct = n->next->data.i_val;
    6089       60949 :         dlist *corresponding = n->next->next->data.lval;
    6090       60949 :         symbol *tab_ref2 = n->next->next->next->data.sym;
    6091       60949 :         sql_rel *t1, *t2;
    6092             : 
    6093       60949 :         assert(n->next->type == type_int);
    6094       60949 :         t1 = table_ref(query, tab_ref1, 0, NULL);
    6095       60949 :         if (!t1)
    6096             :                 return NULL;
    6097       60945 :         t2 = table_ref(query, tab_ref2, 0, NULL);
    6098       60945 :         if (!t2)
    6099             :                 return NULL;
    6100             : 
    6101       60943 :         rel_remove_internal_exp(t1);
    6102       60943 :         rel_remove_internal_exp(t2);
    6103       60943 :         if (!corresponding && list_length(t1->exps) != list_length(t2->exps)) {
    6104           1 :                 int t1nrcols = list_length(t1->exps);
    6105           1 :                 int t2nrcols = list_length(t2->exps);
    6106           1 :                 const char *op = "UNION";
    6107           1 :                 if (q->token == SQL_EXCEPT)
    6108             :                         op = "EXCEPT";
    6109           0 :                 else if (q->token == SQL_INTERSECT)
    6110           0 :                         op = "INTERSECT";
    6111           1 :                 rel_destroy(t1);
    6112           1 :                 rel_destroy(t2);
    6113           1 :                 return sql_error(sql, 02, SQLSTATE(42000) "%s: column counts (%d and %d) do not match", op, t1nrcols, t2nrcols);
    6114             :         }
    6115       60942 :         if ( q->token == SQL_UNION) {
    6116       58461 :                 int outer = n->next->next->next->next->data.i_val;
    6117             :                 /* For EXCEPT/INTERSECT the group by is always done within the implementation */
    6118             :                 /* TODO add those later in an optimizer ! */
    6119       58461 :                 if (t1 && distinct)
    6120        2006 :                         t1 = rel_distinct(t1);
    6121       58461 :                 if (t2 && distinct)
    6122        2006 :                         t2 = rel_distinct(t2);
    6123             :                 // TODO: this has to be fixed
    6124             :                 /*res = rel_setquery_(query, t1, t2, corresponding, op_union, outer);*/
    6125       58461 :                 res = rel_setquery_n_ary_(query, t1, t2, corresponding, op_munion, outer);
    6126        2481 :         } else if ( q->token == SQL_EXCEPT)
    6127        2023 :                 res = rel_setquery_(query, t1, t2, corresponding, op_except, 0);
    6128         458 :         else if ( q->token == SQL_INTERSECT)
    6129         458 :                 res = rel_setquery_(query, t1, t2, corresponding, op_inter, 0);
    6130       60942 :         if (res) {
    6131       60933 :                 set_processed(res);
    6132       60933 :                 if (distinct)
    6133        4361 :                         res = rel_distinct(res);
    6134             :         }
    6135             :         return res;
    6136             : }
    6137             : 
    6138             : static sql_rel *
    6139       58718 : rel_joinquery_(sql_query *query, symbol *tab1, int natural, jt jointype, symbol *tab2, symbol *js, list *refs)
    6140             : {
    6141       58718 :         mvc *sql = query->sql;
    6142       58718 :         operator_type op = op_join;
    6143       58718 :         sql_rel *t1 = NULL, *t2 = NULL, *inner, *rel = NULL;
    6144       58718 :         int l_nil = 0, r_nil = 0, lateral = 0;
    6145             : 
    6146       58718 :         switch(jointype) {
    6147             :         case jt_inner:
    6148             :         case jt_cross: op = op_join;
    6149             :                 break;
    6150             :         case jt_left: op = op_left;
    6151             :                 r_nil = 1;
    6152             :                 break;
    6153             :         case jt_right: op = op_right;
    6154             :                 l_nil = 1;
    6155             :                 break;
    6156             :         case jt_full: op = op_full;
    6157             :                 l_nil = 1;
    6158             :                 r_nil = 1;
    6159             :                 break;
    6160             :         }
    6161             : 
    6162             :         /* a dependent join cannot depend on the right side, so disable lateral check for right and full joins */
    6163       58718 :         lateral = (op == op_join || op == op_left) && check_is_lateral(tab2);
    6164       58718 :         t1 = table_ref(query, tab1, 0, refs);
    6165       58718 :         if (t1) {
    6166       58713 :                 if (!lateral) {
    6167       58678 :                         t2 = table_ref(query, tab2, 0, refs);
    6168             :                 } else {
    6169          35 :                         query_processed(query);
    6170          35 :                         query_push_outer(query, t1, sql_from);
    6171          35 :                         t2 = table_ref(query, tab2, 0, refs);
    6172          35 :                         t1 = query_pop_outer(query);
    6173             :                 }
    6174             :         }
    6175       58718 :         if (!t1 || !t2)
    6176             :                 return NULL;
    6177             : 
    6178       58695 :         query_processed(query);
    6179       58695 :         if (strcmp(rel_name(t1), rel_name(t2)) == 0) {
    6180           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "SELECT: ERROR:  table name '%s' specified more than once", rel_name(t1));
    6181             :         }
    6182       58695 :         inner = rel = rel_crossproduct(sql->sa, t1, t2, op);
    6183       58695 :         if (!rel)
    6184             :                 return NULL;
    6185       58695 :         if (lateral)
    6186          34 :                 set_dependent(rel);
    6187             : 
    6188       58695 :         assert(jointype != jt_cross || (!natural && !js)); /* there are no natural cross joins, or cross joins with conditions */
    6189       58695 :         if (js && natural)
    6190           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "SELECT: cannot have a NATURAL JOIN with a join specification (ON or USING)");
    6191       58695 :         if (jointype != jt_cross && !js && !natural)
    6192           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "SELECT: must have NATURAL JOIN or a JOIN with a join specification (ON or USING)");
    6193             : 
    6194       58695 :         if (js && js->token != SQL_USING) {  /* On sql_logical_exp */
    6195       58524 :                 rel = rel_logical_exp(query, rel, js, sql_where | sql_join);
    6196         171 :         } else if (js) {        /* using */
    6197          20 :                 char rname[16], *rnme;
    6198          20 :                 dnode *n = js->data.lval->h;
    6199          20 :                 list *outexps = new_exp_list(sql->sa), *exps;
    6200          20 :                 node *m;
    6201             : 
    6202          20 :                 rnme = sa_strdup(sql->sa, number2name(rname, sizeof(rname), ++sql->label));
    6203          46 :                 for (; n; n = n->next) {
    6204          26 :                         char *nm = n->data.sval;
    6205          26 :                         sql_exp *cond, *ls, *rs;
    6206             : 
    6207          26 :                         if (!(ls = rel_bind_column(sql, t1, nm, sql_where | sql_join, 0)) && sql->session->status == -ERR_AMBIGUOUS)
    6208           0 :                                 return NULL;
    6209          26 :                         if (!(rs = rel_bind_column(sql, t2, nm, sql_where | sql_join, 0)) && sql->session->status == -ERR_AMBIGUOUS)
    6210             :                                 return NULL;
    6211          26 :                         if (!ls || !rs)
    6212           0 :                                 return sql_error(sql, 02, SQLSTATE(42000) "JOIN: tables '%s' and '%s' do not have a matching column '%s'", rel_name(t1)?rel_name(t1):"", rel_name(t2)?rel_name(t2):"", nm);
    6213          26 :                         if (!(rel = rel_compare_exp(query, rel, ls, rs, "=", TRUE, 0, 0, 0, 0)))
    6214             :                                 return NULL;
    6215          26 :                         if (op != op_join) {
    6216          12 :                                 if (!(cond = rel_unop_(sql, rel, ls, "sys", "isnull", card_value)))
    6217             :                                         return NULL;
    6218          12 :                                 set_has_no_nil(cond);
    6219          12 :                                 if (rel_convert_types(sql, t1, t2, &ls, &rs, 1, type_equal) < 0)
    6220             :                                         return NULL;
    6221          12 :                                 if (!(ls = rel_nop_(sql, rel, cond, rs, ls, NULL, "sys", "ifthenelse", card_value)))
    6222             :                                         return NULL;
    6223             :                         }
    6224          26 :                         exp_setname(sql, ls, rnme, nm);
    6225          26 :                         append(outexps, ls);
    6226          26 :                         if (!rel)
    6227             :                                 return NULL;
    6228             :                 }
    6229          20 :                 exps = rel_projections(sql, t1, NULL, 1, 1);
    6230         199 :                 for (m = exps->h; m; m = m->next) {
    6231         179 :                         const char *nm = exp_name(m->data);
    6232         179 :                         int fnd = 0;
    6233             : 
    6234         429 :                         for (n = js->data.lval->h; n; n = n->next) {
    6235         276 :                                 if (strcmp(nm, n->data.sval) == 0) {
    6236             :                                         fnd = 1;
    6237             :                                         break;
    6238             :                                 }
    6239             :                         }
    6240         179 :                         if (!fnd) {
    6241         153 :                                 sql_exp *ls = m->data;
    6242         153 :                                 if (l_nil)
    6243          18 :                                         set_has_nil(ls);
    6244         153 :                                 set_not_unique(ls);
    6245         153 :                                 append(outexps, ls);
    6246             :                         }
    6247             :                 }
    6248          20 :                 exps = rel_projections(sql, t2, NULL, 1, 1);
    6249         100 :                 for (m = exps->h; m; m = m->next) {
    6250          80 :                         const char *nm = exp_name(m->data);
    6251          80 :                         int fnd = 0;
    6252             : 
    6253         230 :                         for (n = js->data.lval->h; n; n = n->next) {
    6254         176 :                                 if (strcmp(nm, n->data.sval) == 0) {
    6255             :                                         fnd = 1;
    6256             :                                         break;
    6257             :                                 }
    6258             :                         }
    6259          80 :                         if (!fnd) {
    6260          54 :                                 sql_exp *rs = m->data;
    6261          54 :                                 if (r_nil)
    6262          14 :                                         set_has_nil(rs);
    6263          54 :                                 set_not_unique(rs);
    6264          54 :                                 append(outexps, rs);
    6265             :                         }
    6266             :                 }
    6267          20 :                 rel = rel_project(sql->sa, rel, outexps);
    6268         151 :         } else if (jointype != jt_cross) {              /* ! js -> natural join */
    6269          60 :                 rel = join_on_column_name(query, rel, t1, t2, op, l_nil, r_nil);
    6270             :         }
    6271       58695 :         if (!rel)
    6272             :                 return NULL;
    6273       58677 :         if (inner && is_outerjoin(inner->op))
    6274       21689 :                 set_processed(inner);
    6275       58677 :         set_processed(rel);
    6276       58677 :         query_processed(query);
    6277       58677 :         return rel;
    6278             : }
    6279             : 
    6280             : static sql_rel *
    6281       58718 : rel_joinquery(sql_query *query, symbol *q, list *refs)
    6282             : {
    6283       58718 :         dnode *n = q->data.lval->h;
    6284       58718 :         symbol *tab_ref1 = n->data.sym;
    6285       58718 :         int natural = n->next->data.i_val;
    6286       58718 :         jt jointype = (jt) n->next->next->data.i_val;
    6287       58718 :         symbol *tab_ref2 = n->next->next->next->data.sym;
    6288       58718 :         symbol *joinspec = n->next->next->next->next->data.sym;
    6289             : 
    6290       58718 :         assert(n->next->type == type_int);
    6291       58718 :         assert(n->next->next->type == type_int);
    6292       58718 :         return rel_joinquery_(query, tab_ref1, natural, jointype, tab_ref2, joinspec, refs);
    6293             : }
    6294             : 
    6295             : sql_rel *
    6296      369298 : rel_subquery(sql_query *query, symbol *sq, exp_kind ek)
    6297             : {
    6298      369298 :         mvc *sql = query->sql;
    6299             : 
    6300      369298 :         query_processed(query);
    6301      369298 :         if (!stack_push_frame(sql, NULL))
    6302           0 :                 return sql_error(sql, 02, SQLSTATE(HY013) MAL_MALLOC_FAIL);
    6303      369296 :         sql_rel *rel = rel_query(query, sq, ek);
    6304      369296 :         stack_pop_frame(sql);
    6305      369298 :         if (!query_has_outer(query))
    6306      333247 :                 query_processed(query);
    6307      369298 :         if (rel && ek.type == type_relation && ek.card < card_set && rel->card >= CARD_AGGR)
    6308          15 :                 return rel_zero_or_one(sql, rel, ek);
    6309             :         return rel;
    6310             : }
    6311             : 
    6312             : sql_rel *
    6313      238368 : rel_selects(sql_query *query, symbol *s)
    6314             : {
    6315      238368 :         mvc *sql = query->sql;
    6316      238368 :         sql_rel *ret = NULL;
    6317             : 
    6318      238368 :         switch (s->token) {
    6319        5663 :         case SQL_WITH:
    6320        5663 :                 ret = rel_with_query(query, s);
    6321        5663 :                 sql->type = Q_TABLE;
    6322        5663 :                 break;
    6323        2177 :         case SQL_VALUES:
    6324        2177 :                 ret = rel_values(query, s, NULL);
    6325        2177 :                 sql->type = Q_TABLE;
    6326        2177 :                 break;
    6327      211387 :         case SQL_SELECT: {
    6328      211387 :                 exp_kind ek = {type_value, card_relation, TRUE};
    6329      211387 :                 SelectNode *sn = (SelectNode *) s;
    6330             : 
    6331      211387 :                 if (sn->into) {
    6332           1 :                         sql->type = Q_SCHEMA;
    6333           1 :                         ret = rel_select_with_into(query, s);
    6334             :                 } else {
    6335      211386 :                         ret = rel_subquery(query, s, ek);
    6336      211386 :                         sql->type = Q_TABLE;
    6337             :                 }
    6338      211387 :         }       break;
    6339           0 :         case SQL_JOIN:
    6340           0 :                 ret = rel_joinquery(query, s, NULL);
    6341           0 :                 sql->type = Q_TABLE;
    6342           0 :                 break;
    6343       19141 :         case SQL_UNION:
    6344             :         case SQL_EXCEPT:
    6345             :         case SQL_INTERSECT:
    6346       19141 :                 ret = rel_setquery(query, s);
    6347       19141 :                 sql->type = Q_TABLE;
    6348       19141 :                 break;
    6349             :         default:
    6350             :                 return NULL;
    6351             :         }
    6352      238368 :         if (!ret && sql->errstr[0] == 0)
    6353           0 :                 (void) sql_error(sql, 02, SQLSTATE(42000) "relational query without result");
    6354             :         return ret;
    6355             : }
    6356             : 
    6357             : sql_rel *
    6358       79046 : schema_selects(sql_query *query, sql_schema *schema, symbol *s)
    6359             : {
    6360       79046 :         mvc *sql = query->sql;
    6361       79046 :         sql_rel *res;
    6362       79046 :         sql_schema *os = cur_schema(sql);
    6363             : 
    6364       79046 :         sql->session->schema = schema;
    6365       79046 :         res = rel_selects(query, s);
    6366       79046 :         sql->session->schema = os;
    6367       79046 :         return res;
    6368             : }
    6369             : 
    6370             : sql_rel *
    6371          26 : rel_loader_function(sql_query *query, symbol* fcall, list *fexps, sql_subfunc **loader_function)
    6372             : {
    6373          26 :         mvc *sql = query->sql;
    6374          26 :         sql_rel *sq = NULL;
    6375          26 :         dnode *l = fcall->data.lval->h;
    6376          26 :         char *sname = qname_schema(l->data.lval);
    6377          26 :         char *fname = qname_schema_object(l->data.lval);
    6378             : 
    6379          26 :         list *tl = sa_list(sql->sa);
    6380          26 :         list *exps = sa_list(sql->sa);
    6381          26 :         if (l->next)
    6382          26 :                 l = l->next; /* skip distinct */
    6383          26 :         if (l->next) { /* table call with subquery */
    6384          22 :                 if (l->next->type == type_symbol || l->next->type == type_list) {
    6385          22 :                         int count = 0;
    6386          22 :                         symbol *subquery = NULL;
    6387          22 :                         dnode *n = NULL;
    6388             : 
    6389          22 :                         if (l->next->type == type_symbol)
    6390             :                                 n = l->next;
    6391             :                         else
    6392           1 :                                 n = l->next->data.lval->h;
    6393             : 
    6394          66 :                         for (dnode *m = n; m; m = m->next) {
    6395          44 :                                 if (m->type == type_symbol && m->data.sym->token == SQL_SELECT)
    6396          44 :                                         subquery = m->data.sym;
    6397          44 :                                 count++;
    6398             :                         }
    6399          22 :                         if (subquery && count > 1)
    6400           0 :                                 return sql_error(sql, 02, SQLSTATE(42000) "SELECT: The input for the loader function '%s' must be either a single sub query, or a list of values", fname);
    6401             : 
    6402          22 :                         if (subquery) {
    6403           0 :                                 exp_kind ek = { type_value, card_relation, TRUE };
    6404           0 :                                 if (!(sq = rel_subquery(query, subquery, ek)))
    6405           0 :                                         return NULL;
    6406             :                         } else {
    6407          22 :                                 exp_kind ek = { type_value, card_column, TRUE };
    6408          22 :                                 list *exps = sa_list(sql->sa);
    6409          88 :                                 for ( ; n; n = n->next) {
    6410          44 :                                         sql_exp *e = rel_value_exp(query, NULL, n->data.sym, sql_sel | sql_from, ek);
    6411             : 
    6412          44 :                                         if (!e)
    6413           0 :                                                 return NULL;
    6414          44 :                                         append(exps, e);
    6415             :                                 }
    6416          22 :                                 sq = rel_project(sql->sa, NULL, exps);
    6417             :                         }
    6418             :                 }
    6419          22 :                 if (!sq)
    6420           0 :                         return sql_error(sql, ERR_NOTFOUND, SQLSTATE(42000) "SELECT: no such loader function %s%s%s'%s'", sname ? "'":"", sname ? sname : "", sname ? "'.":"", fname);
    6421          66 :                 for (node *en = sq->exps->h; en; en = en->next) {
    6422          44 :                         sql_exp *e = en->data;
    6423             : 
    6424          44 :                         append(exps, e = exp_ref(sql, e));
    6425          44 :                         append(tl, exp_subtype(e));
    6426             :                 }
    6427             :         }
    6428             : 
    6429          26 :         sql_exp *e = NULL;
    6430          26 :         if (!(e = find_table_function(sql, sname, fname, exps, tl, F_LOADER)))
    6431             :                 return NULL;
    6432          25 :         sql_subfunc *sf = e->f;
    6433          25 :         if (sq) {
    6434          66 :                 for (node *n = sq->exps->h, *m = sf->func->ops->h ; n && m ; n = n->next, m = m->next) {
    6435          44 :                         sql_exp *e = (sql_exp*) n->data;
    6436          44 :                         sql_arg *a = (sql_arg*) m->data;
    6437          44 :                         if (!exp_subtype(e) && rel_set_type_param(sql, &(a->type), sq, e, 0) < 0)
    6438             :                                 return NULL;
    6439             :                 }
    6440             :         }
    6441             : 
    6442          25 :         if (loader_function)
    6443          25 :                 *loader_function = sf;
    6444             : 
    6445          28 :         return rel_table_func(sql->sa, sq, e, fexps, (sq)?TABLE_FROM_RELATION:TABLE_PROD_FUNC);
    6446             : }

Generated by: LCOV version 1.14