LCOV - code coverage report
Current view: top level - sql/server - rel_exp.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 1963 2232 87.9 %
Date: 2025-03-25 21:27:32 Functions: 183 197 92.9 %

          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, 2025 MonetDB Foundation;
       9             :  * Copyright August 2008 - 2023 MonetDB B.V.;
      10             :  * Copyright 1997 - July 2008 CWI.
      11             :  */
      12             : 
      13             : #include "monetdb_config.h"
      14             : #include "sql_relation.h"
      15             : #include "sql_semantic.h"
      16             : #include "sql_decimal.h"
      17             : #include "rel_exp.h"
      18             : #include "rel_rel.h"
      19             : #include "rel_basetable.h"
      20             : #include "rel_prop.h"
      21             : 
      22             : comp_type
      23      834762 : compare_str2type(const char *compare_op)
      24             : {
      25      834762 :         comp_type type = cmp_filter;
      26             : 
      27      834762 :         if (compare_op[0] == '=') {
      28             :                 type = cmp_equal;
      29      148861 :         } else if (compare_op[0] == '<') {
      30       91269 :                 type = cmp_lt;
      31       91269 :                 if (compare_op[1] == '>')
      32             :                         type = cmp_notequal;
      33       23613 :                 else if (compare_op[1] == '=')
      34        4181 :                         type = cmp_lte;
      35       57592 :         } else if (compare_op[0] == '>') {
      36       57592 :                 type = cmp_gt;
      37       57592 :                 if (compare_op[1] == '=')
      38        3359 :                         type = cmp_gte;
      39             :         }
      40      834762 :         return type;
      41             : }
      42             : 
      43             : comp_type
      44       52394 : swap_compare( comp_type t )
      45             : {
      46       52394 :         switch(t) {
      47             :         case cmp_equal:
      48             :                 return cmp_equal;
      49             :         case cmp_lt:
      50             :                 return cmp_gt;
      51             :         case cmp_lte:
      52             :                 return cmp_gte;
      53             :         case cmp_gte:
      54             :                 return cmp_lte;
      55             :         case cmp_gt:
      56             :                 return cmp_lt;
      57             :         case cmp_notequal:
      58             :                 return cmp_notequal;
      59             :         default:
      60             :                 return cmp_equal;
      61             :         }
      62             : }
      63             : 
      64             : comp_type
      65           0 : negate_compare( comp_type t )
      66             : {
      67           0 :         switch(t) {
      68             :         case cmp_equal:
      69             :                 return cmp_notequal;
      70           0 :         case cmp_notequal:
      71           0 :                 return cmp_equal;
      72           0 :         case cmp_lt:
      73           0 :                 return cmp_gte;
      74           0 :         case cmp_lte:
      75           0 :                 return cmp_gt;
      76           0 :         case cmp_gte:
      77           0 :                 return cmp_lt;
      78           0 :         case cmp_gt:
      79           0 :                 return cmp_lte;
      80             : 
      81           0 :         case cmp_in:
      82           0 :                 return cmp_notin;
      83           0 :         case cmp_notin:
      84           0 :                 return cmp_in;
      85             : 
      86           0 :         default:
      87           0 :                 return t;
      88             :         }
      89             : }
      90             : 
      91             : comp_type
      92        3190 : range2lcompare( int r )
      93             : {
      94        3190 :         if (r&1) {
      95             :                 return cmp_gte;
      96             :         } else {
      97        1216 :                 return cmp_gt;
      98             :         }
      99             : }
     100             : 
     101             : comp_type
     102        3247 : range2rcompare( int r )
     103             : {
     104        3247 :         if (r&2) {
     105             :                 return cmp_lte;
     106             :         } else {
     107        1253 :                 return cmp_lt;
     108             :         }
     109             : }
     110             : 
     111             : int
     112        1913 : compare2range( int l, int r )
     113             : {
     114        1913 :         if (l == cmp_gt) {
     115        1790 :                 if (r == cmp_lt)
     116             :                         return 0;
     117          23 :                 else if (r == cmp_lte)
     118          23 :                         return 2;
     119         123 :         } else if (l == cmp_gte) {
     120         123 :                 if (r == cmp_lt)
     121             :                         return 1;
     122          85 :                 else if (r == cmp_lte)
     123          85 :                         return 3;
     124             :         }
     125             :         return -1;
     126             : }
     127             : 
     128             : int
     129          99 : compare_funcs2range(const char *l_op, const char *r_op)
     130             : {
     131          99 :         assert(l_op[0] == '>' && r_op[0] == '<');
     132          99 :         if (!l_op[1] && !r_op[1])
     133             :                 return 0;
     134          97 :         if (!l_op[1] && r_op[1] == '=')
     135             :                 return 2;
     136          97 :         if (l_op[1] == '=' && !r_op[1])
     137             :                 return 1;
     138           5 :         if (l_op[1] == '=' && r_op[1] == '=')
     139             :                 return 3;
     140           0 :         assert(0);
     141             :         return 0;
     142             : }
     143             : 
     144             : static sql_exp *
     145    21926162 : exp_create(allocator *sa, int type)
     146             : {
     147    21926162 :         sql_exp *e = SA_NEW(sa, sql_exp);
     148             : 
     149    21926704 :         if (!e)
     150             :                 return NULL;
     151    21926704 :         *e = (sql_exp) {
     152    21926704 :                 .type = (expression_type) type,
     153             :         };
     154    21926704 :         return e;
     155             : }
     156             : 
     157             : sql_exp *
     158      490520 : exp_compare(allocator *sa, sql_exp *l, sql_exp *r, int cmptype)
     159             : {
     160      490520 :         sql_exp *e = exp_create(sa, e_cmp);
     161      490520 :         if (e == NULL)
     162             :                 return NULL;
     163      490520 :         e->card = MAX(l->card,r->card);
     164      490520 :         e->l = l;
     165      490520 :         e->r = r;
     166      490520 :         e->flag = cmptype;
     167      490520 :         if (!has_nil(l) && !has_nil(r))
     168      352343 :                 set_has_no_nil(e);
     169             :         return e;
     170             : }
     171             : 
     172             : sql_exp *
     173        6613 : exp_compare2(allocator *sa, sql_exp *l, sql_exp *r, sql_exp *f, int cmptype, int symmetric)
     174             : {
     175        6613 :         sql_exp *e = exp_create(sa, e_cmp);
     176        6613 :         if (e == NULL)
     177             :                 return NULL;
     178        6613 :         assert(f);
     179        6613 :         e->card = MAX(MAX(l->card,r->card),f->card);
     180        6613 :         e->l = l;
     181        6613 :         e->r = r;
     182        6613 :         e->f = f;
     183        6613 :         e->flag = cmptype;
     184        6613 :         if (symmetric)
     185          76 :                 set_symmetric(e);
     186        6613 :         if (!has_nil(l) && !has_nil(r) && !has_nil(f))
     187        1628 :                 set_has_no_nil(e);
     188             :         return e;
     189             : }
     190             : 
     191             : sql_exp *
     192        8223 : exp_filter(allocator *sa, list *l, list *r, sql_subfunc *f, int anti)
     193             : {
     194        8223 :         sql_exp *e = exp_create(sa, e_cmp);
     195             : 
     196        8223 :         if (e == NULL)
     197             :                 return NULL;
     198        8223 :         e->card = MAX(exps_card(l),exps_card(r));
     199        8223 :         if (!r) { /* split l */
     200        1593 :                 list *nl = sa_list(sa), *nr = sa_list(sa);
     201        1593 :                 node *n = l->h;
     202        1593 :                 append(nl, n->data); /* split 1, 3 */
     203        1593 :                 if (list_length(l) > 4) {
     204           4 :                         n = n->next;
     205           4 :                         append(nl, n->data); /* split 2, 3 */
     206             :                 }
     207        4065 :                 for(n = n->next; n; n = n->next)
     208        2472 :                         append(nr, n->data);
     209             :                 l = nl;
     210             :                 r = nr;
     211             :         }
     212        8223 :         e->l = l;
     213        8223 :         e->r = r;
     214        8223 :         e->f = f;
     215        8223 :         e->flag = cmp_filter;
     216        8223 :         if (anti)
     217        1665 :                 set_anti(e);
     218        8223 :         if (!have_nil(l) && !have_nil(r))
     219        5083 :                 set_has_no_nil(e);
     220             :         return e;
     221             : }
     222             : 
     223             : sql_exp *
     224       46867 : exp_or(allocator *sa, list *l, list *r, int anti)
     225             : {
     226       46867 :         sql_exp *e = exp_create(sa, e_cmp);
     227             : 
     228       46867 :         if (e == NULL)
     229             :                 return NULL;
     230       46867 :         e->card = MAX(exps_card(l),exps_card(r));
     231       46867 :         e->l = l;
     232       46867 :         e->r = r;
     233       46867 :         e->flag = cmp_or;
     234       46867 :         if (anti)
     235           2 :                 set_anti(e);
     236       46867 :         if (!have_nil(l) && !have_nil(r))
     237       30376 :                 set_has_no_nil(e);
     238             :         return e;
     239             : }
     240             : 
     241             : sql_exp *
     242       29078 : exp_in(allocator *sa, sql_exp *l, list *r, int cmptype)
     243             : {
     244       29078 :         sql_exp *e = exp_create(sa, e_cmp);
     245       29078 :         unsigned int exps_card = CARD_ATOM;
     246             : 
     247       29078 :         if (e == NULL)
     248             :                 return NULL;
     249             : 
     250             :         /* ignore the cardinalites of sub-relations */
     251      139355 :         for (node *n = r->h; n ; n = n->next) {
     252      110277 :                 sql_exp *next = n->data;
     253             : 
     254      110277 :                 if (!exp_is_rel(next) && exps_card < next->card)
     255      110277 :                         exps_card = next->card;
     256             :         }
     257       29078 :         e->card = MAX(l->card, exps_card);
     258       29078 :         e->l = l;
     259       29078 :         e->r = r;
     260       29078 :         assert( cmptype == cmp_in || cmptype == cmp_notin);
     261       29078 :         e->flag = cmptype;
     262       29078 :         if (!has_nil(l) && !have_nil(r))
     263       21906 :                 set_has_no_nil(e);
     264             :         return e;
     265             : }
     266             : 
     267             : sql_exp *
     268       37234 : exp_in_func(mvc *sql, sql_exp *le, sql_exp *vals, int anyequal, int is_tuple)
     269             : {
     270       37234 :         sql_subfunc *a_func = NULL;
     271       37234 :         sql_exp *e = le;
     272             : 
     273       37234 :         if (is_tuple) {
     274        4225 :                 list *l = exp_get_values(e);
     275        4225 :                 e = l->h->data;
     276             :         }
     277       43414 :         if (!(a_func = sql_bind_func(sql, "sys", anyequal ? "sql_anyequal" : "sql_not_anyequal", exp_subtype(e), exp_subtype(e), F_FUNC, true, true)))
     278           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "(NOT) IN operator on type %s missing", exp_subtype(e) ? exp_subtype(e)->type->base.name : "unknown");
     279       37234 :         e = exp_binop(sql->sa, le, vals, a_func);
     280       37234 :         if (e) {
     281       37234 :                 unsigned int exps_card = CARD_ATOM;
     282             : 
     283             :                 /* ignore the cardinalites of sub-relations */
     284       37234 :                 if (vals->type == e_atom && vals->f) {
     285      182140 :                         for (node *n = ((list*)vals->f)->h ; n ; n = n->next) {
     286      144906 :                                 sql_exp *next = n->data;
     287             : 
     288      144906 :                                 if (!exp_is_rel(next) && exps_card < next->card)
     289      144906 :                                         exps_card = next->card;
     290             :                         }
     291           0 :                 } else if (!exp_is_rel(vals))
     292           0 :                         exps_card = vals->card;
     293             : 
     294       37234 :                 e->card = MAX(le->card, exps_card);
     295       37234 :                 if (!has_nil(le) && !has_nil(vals))
     296           0 :                         set_has_no_nil(e);
     297             :         }
     298             :         return e;
     299             : }
     300             : 
     301             : sql_exp *
     302        5682 : exp_in_aggr(mvc *sql, sql_exp *le, sql_exp *vals, int anyequal, int is_tuple)
     303             : {
     304        5682 :         sql_subfunc *a_func = NULL;
     305        5682 :         sql_exp *e = le;
     306             : 
     307        5682 :         if (is_tuple) {
     308           0 :                 list *l = exp_get_values(e);
     309           0 :                 e = l->h->data;
     310             :         }
     311        5691 :         if (!(a_func = sql_bind_func(sql, "sys", anyequal ? "anyequal" : "allnotequal", exp_subtype(e), exp_subtype(e), F_AGGR, true, true)))
     312           0 :                 return sql_error(sql, 02, SQLSTATE(42000) "(NOT) IN operator on type %s missing", exp_subtype(e) ? exp_subtype(e)->type->base.name : "unknown");
     313        5682 :         e = exp_aggr2(sql->sa, le, vals, a_func, need_distinct(e), need_no_nil(e), e->card, has_nil(e));
     314        5682 :         if (e) {
     315        5682 :                 unsigned int exps_card = CARD_ATOM;
     316             : 
     317             :                 /* ignore the cardinalites of sub-relations */
     318        5682 :                 if (vals->type == e_atom && vals->f) {
     319       32152 :                         for (node *n = ((list*)vals->f)->h ; n ; n = n->next) {
     320       26470 :                                 sql_exp *next = n->data;
     321             : 
     322       26470 :                                 if (!exp_is_rel(next) && exps_card < next->card)
     323       26470 :                                         exps_card = next->card;
     324             :                         }
     325           0 :                 } else if (!exp_is_rel(vals))
     326           0 :                         exps_card = vals->card;
     327             : 
     328        5682 :                 e->card = MAX(le->card, exps_card);
     329        5682 :                 if (!has_nil(le) && !has_nil(vals))
     330           0 :                         set_has_no_nil(e);
     331             :         }
     332             :         return e;
     333             : }
     334             : 
     335             : sql_exp *
     336      132663 : exp_compare_func(mvc *sql, sql_exp *le, sql_exp *re, const char *compareop, int quantifier)
     337             : {
     338      132663 :         sql_subfunc *cmp_func = sql_bind_func(sql, "sys", compareop, exp_subtype(le), exp_subtype(le), F_FUNC, true, true);
     339      132663 :         sql_exp *e = NULL;
     340             : 
     341      132663 :         if (cmp_func == NULL)
     342             :                 return NULL;
     343             : 
     344      132663 :         e = exp_binop(sql->sa, le, re, cmp_func);
     345      132663 :         if (e) {
     346      132663 :                 e->flag = quantifier;
     347             :                 /* At ANY and ALL operators, the cardinality on the right side is ignored if it is a sub-relation */
     348      132663 :                 e->card = quantifier && exp_is_rel(re) ? le->card : MAX(le->card, re->card);
     349      132663 :                 if (!has_nil(le) && !has_nil(re))
     350       87658 :                         set_has_no_nil(e);
     351             :         }
     352             :         return e;
     353             : }
     354             : 
     355             : static sql_subtype*
     356      891472 : dup_subtype(allocator *sa, sql_subtype *st)
     357             : {
     358      891472 :         sql_subtype *res = SA_NEW(sa, sql_subtype);
     359             : 
     360      891472 :         if (res == NULL)
     361             :                 return NULL;
     362      891472 :         *res = *st;
     363      891472 :         return res;
     364             : }
     365             : 
     366             : static list *
     367         177 : nested_exps(mvc *sql, sql_subtype *t, sql_alias *p, const char *name)
     368             : {
     369         177 :         sql_alias *atname = name?a_create(sql->sa, name):NULL;
     370          92 :         if (atname)
     371          92 :                 atname->parent = p;
     372         177 :         list *nested = sa_list(sql->sa);
     373         177 :         if (t->type->composite) {
     374         413 :                 for(node *n = t->type->d.fields->h; n; n = n->next) {
     375         275 :                         sql_arg *f = n->data;
     376             : 
     377         275 :             sql_exp *e = exp_alias(sql, atname, f->name, atname, f->name, &f->type, CARD_MULTI, true, false, 1);
     378         275 :                         if (f->type.multiset || f->type.type->composite)
     379          92 :                                 e->f = nested_exps(sql, &f->type, atname, f->name);
     380         275 :                         append(nested, e);
     381             :                 }
     382             :         } else {
     383          39 :         sql_exp *e = exp_alias(sql, atname, MSEL_NAME, atname, MSEL_NAME, t, CARD_MULTI, true, false, 1);
     384          39 :                 append(nested, e);
     385             :         }
     386         177 :         sql_subtype *it = sql_bind_localtype(MSID_TYPENAME);
     387         177 :         if (t->multiset) {
     388         103 :                 sql_exp *e = exp_alias(sql, atname, MSID_NAME, atname, MSID_NAME, it, CARD_MULTI, true, false, 1);
     389         103 :                 set_intern(e);
     390         103 :                 append(nested, e);
     391             :         }
     392         177 :         if (t->multiset == MS_ARRAY) {
     393         103 :                 sql_subtype *it = sql_bind_localtype(MSNR_TYPENAME);
     394         103 :                 sql_exp *e = exp_alias(sql, atname, MSNR_NAME, atname, MSNR_NAME, it, CARD_MULTI, true, false, 1);
     395         103 :                 set_intern(e);
     396         103 :                 append(nested, e);
     397             :         }
     398         177 :         if (t->multiset) {
     399         103 :                 sql_exp *e = exp_alias(sql, atname, "rowid", atname, "rowid", it, CARD_MULTI, true, false, 1);
     400         103 :                 set_intern(e);
     401         103 :                 append(nested, e);
     402             :         }
     403         177 :         return nested;
     404             : }
     405             : 
     406             : sql_exp *
     407      445736 : exp_convert(mvc *sql, sql_exp *exp, sql_subtype *fromtype, sql_subtype *totype )
     408             : {
     409      445736 :         sql_exp *e = exp_create(sql->sa, e_convert);
     410      445736 :         if (e == NULL)
     411             :                 return NULL;
     412      445736 :         e->card = exp->card;
     413      445736 :         e->l = exp;
     414      445736 :         totype = dup_subtype(sql->sa, totype);
     415      445736 :         e->r = append(append(sa_list(sql->sa), dup_subtype(sql->sa, fromtype)),totype);
     416      445737 :         e->tpe = *totype;
     417      445737 :         e->alias = exp->alias;
     418      445737 :         if (e->alias.label)
     419      289929 :                 e->alias.label = -(sql->nid++);
     420      445737 :         if (!has_nil(exp))
     421      174815 :                 set_has_no_nil(e);
     422      445737 :         if (totype->multiset || totype->type->composite) {
     423          85 :                 e->f = nested_exps(sql, totype, NULL, NULL);
     424             :         }
     425             :         return e;
     426             : }
     427             : 
     428             : sql_exp *
     429     1205612 : exp_op( allocator *sa, list *l, sql_subfunc *f )
     430             : {
     431     1205612 :         if (f->func->type == F_FILT)
     432        1593 :                 return exp_filter(sa, l, NULL, f, false);
     433     1204019 :         sql_exp *e = exp_create(sa, e_func);
     434     1204043 :         if (e == NULL)
     435             :                 return NULL;
     436     1204043 :         e->card = exps_card(l);
     437     1204094 :         e->l = l;
     438     1204094 :         e->f = f;
     439     1204094 :         e->semantics = f->func->semantics;
     440     1204094 :         if (!is_semantics(e) && !is_any(e) && l && !have_nil(l))
     441      284746 :                 set_has_no_nil(e);
     442             :         return e;
     443             : }
     444             : 
     445             : sql_exp *
     446       12806 : exp_rank_op( allocator *sa, list *l, list *gbe, list *obe, sql_subfunc *f )
     447             : {
     448       12806 :         sql_exp *e = exp_create(sa, e_func);
     449       12806 :         if (e == NULL)
     450             :                 return NULL;
     451       12806 :         e->card = list_empty(l)?CARD_MULTI:exps_card(l);
     452       12806 :         e->l = l;
     453       12806 :         e->r = append(append(sa_list(sa), gbe), obe);
     454       12806 :         e->f = f;
     455       12806 :         if (!f->func->s && strcmp(f->func->base.name, "count") == 0)
     456         180 :                 set_has_no_nil(e);
     457       12806 :         e->semantics = f->func->semantics;
     458       12806 :         return e;
     459             : }
     460             : 
     461             : sql_exp *
     462       65539 : exp_aggr( allocator *sa, list *l, sql_subfunc *a, int distinct, int no_nils, unsigned int card, int has_nils )
     463             : {
     464       65539 :         sql_exp *e = exp_create(sa, e_aggr);
     465       65539 :         if (e == NULL)
     466             :                 return NULL;
     467       65539 :         e->card = card;
     468       65539 :         e->l = l;
     469       65539 :         e->f = a;
     470       65539 :         e->semantics = a->func->semantics;
     471       65539 :         if (distinct)
     472         378 :                 set_distinct(e);
     473       65539 :         if (no_nils)
     474       28615 :                 set_no_nil(e);
     475       65539 :         if ((!a->func->semantics && !has_nils) || (!a->func->s && strcmp(a->func->base.name, "count") == 0))
     476       26752 :                 set_has_no_nil(e);
     477             :         return e;
     478             : }
     479             : 
     480             : sql_exp *
     481     5223565 : exp_atom(allocator *sa, atom *a)
     482             : {
     483     5223565 :         sql_exp *e = exp_create(sa, e_atom);
     484     5222760 :         if (e == NULL)
     485             :                 return NULL;
     486     5222760 :         e->card = CARD_ATOM;
     487     5222760 :         e->tpe = a->tpe;
     488     5222760 :         e->l = a;
     489     5222760 :         if (!a->isnull)
     490     4937919 :                 set_has_no_nil(e);
     491             :         return e;
     492             : }
     493             : 
     494             : sql_exp *
     495           0 : exp_atom_max(allocator *sa, sql_subtype *tpe)
     496             : {
     497           0 :         if (tpe->type->localtype == TYPE_bte) {
     498           0 :                 return exp_atom_bte(sa, GDK_bte_max);
     499             :         } else if (tpe->type->localtype == TYPE_sht) {
     500           0 :                 return exp_atom_sht(sa, GDK_sht_max);
     501             :         } else if (tpe->type->localtype == TYPE_int) {
     502           0 :                 return exp_atom_int(sa, GDK_int_max);
     503             :         } else if (tpe->type->localtype == TYPE_lng) {
     504           0 :                 return exp_atom_lng(sa, GDK_lng_max);
     505             : #ifdef HAVE_HGE
     506             :         } else if (tpe->type->localtype == TYPE_hge) {
     507           0 :                 return exp_atom_hge(sa, GDK_hge_max);
     508             : #endif
     509             :         }
     510             :         return NULL;
     511             : }
     512             : 
     513             : sql_exp *
     514      156837 : exp_atom_bool(allocator *sa, int b)
     515             : {
     516      156837 :         sql_subtype bt;
     517             : 
     518      156837 :         sql_find_subtype(&bt, "boolean", 0, 0);
     519      156863 :         if (b)
     520      100046 :                 return exp_atom(sa, atom_bool(sa, &bt, TRUE ));
     521             :         else
     522       56817 :                 return exp_atom(sa, atom_bool(sa, &bt, FALSE ));
     523             : }
     524             : 
     525             : sql_exp *
     526           0 : exp_atom_bte(allocator *sa, bte i)
     527             : {
     528           0 :         sql_subtype it;
     529             : 
     530           0 :         sql_find_subtype(&it, "tinyint", 3, 0);
     531           0 :         return exp_atom(sa, atom_int(sa, &it, i ));
     532             : }
     533             : 
     534             : sql_exp *
     535           0 : exp_atom_sht(allocator *sa, sht i)
     536             : {
     537           0 :         sql_subtype it;
     538             : 
     539           0 :         sql_find_subtype(&it, "smallint", 5, 0);
     540           0 :         return exp_atom(sa, atom_int(sa, &it, i ));
     541             : }
     542             : 
     543             : sql_exp *
     544      883914 : exp_atom_int(allocator *sa, int i)
     545             : {
     546      883914 :         sql_subtype it;
     547             : 
     548      883914 :         sql_find_subtype(&it, "int", 9, 0);
     549      884061 :         return exp_atom(sa, atom_int(sa, &it, i ));
     550             : }
     551             : 
     552             : sql_exp *
     553       16597 : exp_atom_lng(allocator *sa, lng i)
     554             : {
     555       16597 :         sql_subtype it;
     556             : 
     557             : #ifdef HAVE_HGE
     558       16597 :         sql_find_subtype(&it, "bigint", 18, 0);
     559             : #else
     560             :         sql_find_subtype(&it, "bigint", 19, 0);
     561             : #endif
     562       16606 :         return exp_atom(sa, atom_int(sa, &it, i ));
     563             : }
     564             : 
     565             : sql_exp *
     566        4610 : exp_atom_oid(allocator *sa, oid i)
     567             : {
     568        4610 :         sql_subtype it;
     569             : 
     570             : #if SIZEOF_OID == SIZEOF_INT
     571             :         sql_find_subtype(&it, "oid", 31, 0);
     572             : #else
     573        4610 :         sql_find_subtype(&it, "oid", 63, 0);
     574             : #endif
     575        4610 :         return exp_atom(sa, atom_int(sa, &it, i ));
     576             : }
     577             : 
     578             : #ifdef HAVE_HGE
     579             : sql_exp *
     580           1 : exp_atom_hge(allocator *sa, hge i)
     581             : {
     582           1 :         sql_subtype it;
     583             : 
     584           1 :         sql_find_subtype(&it, "hugeint", 39, 0);
     585           1 :         return exp_atom(sa, atom_int(sa, &it, i ));
     586             : }
     587             : #endif
     588             : 
     589             : sql_exp *
     590           0 : exp_atom_flt(allocator *sa, flt f)
     591             : {
     592           0 :         sql_subtype it;
     593             : 
     594           0 :         sql_find_subtype(&it, "real", 24, 0);
     595           0 :         return exp_atom(sa, atom_float(sa, &it, (dbl)f ));
     596             : }
     597             : 
     598             : sql_exp *
     599           0 : exp_atom_dbl(allocator *sa, dbl f)
     600             : {
     601           0 :         sql_subtype it;
     602             : 
     603           0 :         sql_find_subtype(&it, "double", 53, 0);
     604           0 :         return exp_atom(sa, atom_float(sa, &it, (dbl)f ));
     605             : }
     606             : 
     607             : sql_exp *
     608       87304 : exp_atom_str(allocator *sa, const char *s, sql_subtype *st)
     609             : {
     610      169969 :         return exp_atom(sa, atom_string(sa, st, s?sa_strdup(sa, s):NULL));
     611             : }
     612             : 
     613             : sql_exp *
     614      769947 : exp_atom_clob(allocator *sa, const char *s)
     615             : {
     616      769947 :         sql_subtype clob;
     617             : 
     618      769947 :         sql_find_subtype(&clob, "varchar", 0, 0);
     619     1538284 :         return exp_atom(sa, atom_string(sa, &clob, s?sa_strdup(sa, s):NULL));
     620             : }
     621             : 
     622             : sql_exp *
     623      277247 : exp_atom_ptr(allocator *sa, void *s)
     624             : {
     625      277247 :         sql_subtype *t = sql_bind_localtype("ptr");
     626      277247 :         return exp_atom(sa, atom_ptr(sa, t, s));
     627             : }
     628             : 
     629             : sql_exp *
     630        2159 : exp_atom_ref(allocator *sa, int i, sql_subtype *tpe)
     631             : {
     632        2159 :         sql_exp *e = exp_create(sa, e_atom);
     633        2159 :         if (e == NULL)
     634             :                 return NULL;
     635        2159 :         e->card = CARD_ATOM;
     636        2159 :         e->flag = i;
     637        2159 :         if (tpe)
     638        2159 :                 e->tpe = *tpe;
     639             :         return e;
     640             : }
     641             : 
     642             : sql_exp *
     643      112858 : exp_null(allocator *sa, sql_subtype *tpe)
     644             : {
     645      112858 :         atom *a = atom_general(sa, tpe, NULL, 0);
     646      112858 :         return exp_atom(sa, a);
     647             : }
     648             : 
     649             : sql_exp *
     650           2 : exp_zero(allocator *sa, sql_subtype *tpe)
     651             : {
     652           2 :         atom *a = atom_zero_value(sa, tpe);
     653           2 :         return exp_atom(sa, a);
     654             : }
     655             : 
     656             : atom *
     657         385 : exp_value(mvc *sql, sql_exp *e)
     658             : {
     659         385 :         if (!e || e->type != e_atom)
     660             :                 return NULL;
     661         380 :         if (e->l) { /* literal */
     662             :                 return e->l;
     663           0 :         } else if (e->r) { /* param (ie not set) */
     664           0 :                 sql_var_name *vname = (sql_var_name*) e->r;
     665             : 
     666           0 :                 assert(e->flag != 0 || vname->sname); /* global variables must have a schema */
     667           0 :                 sql_var *var = e->flag == 0 ? find_global_var(sql, mvc_bind_schema(sql, vname->sname), vname->name) :
     668           0 :                                                                           stack_find_var_at_level(sql, vname->name, e->flag);
     669           0 :                 if (var)
     670           0 :                         return &(var->var);
     671             :         }
     672             :         return NULL;
     673             : }
     674             : 
     675             : sql_exp *
     676      127078 : exp_param_or_declared(allocator *sa, const char *sname, const char *name, sql_subtype *tpe, int frame)
     677             : {
     678      127078 :         sql_var_name *vname;
     679      127078 :         sql_exp *e = exp_create(sa, e_atom);
     680      127078 :         if (e == NULL)
     681             :                 return NULL;
     682             : 
     683      127078 :         e->r = sa_alloc(sa, sizeof(sql_var_name));
     684      127078 :         vname = (sql_var_name*) e->r;
     685      127078 :         vname->sname = sname;
     686      127078 :         vname->name = name;
     687      127078 :         e->card = CARD_ATOM;
     688      127078 :         e->flag = frame;
     689      127078 :         if (tpe)
     690      127078 :                 e->tpe = *tpe;
     691             :         return e;
     692             : }
     693             : 
     694             : sql_exp *
     695      216859 : exp_values(allocator *sa, list *exps)
     696             : {
     697      216859 :         sql_exp *e = exp_create(sa, e_atom);
     698      217022 :         if (e == NULL)
     699             :                 return NULL;
     700      217022 :         e->card = exps_card(exps);
     701      217123 :         e->f = exps;
     702      217123 :         return e;
     703             : }
     704             : 
     705             : list *
     706      308427 : exp_get_values(sql_exp *e)
     707             : {
     708      308427 :         if (is_atom(e->type) && e->f)
     709             :                 return e->f;
     710             :         return NULL;
     711             : }
     712             : 
     713             : list *
     714       50480 : exp_types(allocator *sa, list *exps)
     715             : {
     716       50480 :         list *l = sa_list(sa);
     717             : 
     718       50480 :         if (exps)
     719      102884 :                 for (node *n = exps->h; n; n = n->next)
     720       52404 :                         list_append(l, exp_subtype(n->data));
     721       50480 :         return l;
     722             : }
     723             : 
     724             : int
     725     1094658 : have_nil(list *exps)
     726             : {
     727     1094658 :         int has_nil = 0;
     728             : 
     729     1094658 :         if (exps)
     730     2771348 :                 for (node *n = exps->h; n && !has_nil; n = n->next) {
     731     1676690 :                         sql_exp *e = n->data;
     732     1676690 :                         has_nil |= has_nil(e);
     733             :                 }
     734     1094658 :         return has_nil;
     735             : }
     736             : 
     737             : int
     738         226 : have_semantics(list *exps)
     739             : {
     740         226 :         int has_semantics = 0;
     741             : 
     742         226 :         if (exps)
     743          65 :                 for (node *n = exps->h; n && !has_semantics; n = n->next) {
     744          33 :                         sql_exp *e = n->data;
     745          66 :                         has_semantics |= is_compare(e->type) && is_semantics(e);
     746             :                 }
     747         226 :         return has_semantics;
     748             : }
     749             : 
     750             : sql_exp *
     751     7120223 : exp_column(allocator *sa, sql_alias *parent, const char *cname, sql_subtype *t, unsigned int card, int has_nils, int unique, int intern)
     752             : {
     753     7120223 :         sql_exp *e = exp_create(sa, e_column);
     754             : 
     755     7120208 :         if (e == NULL)
     756             :                 return NULL;
     757     7120208 :         assert(cname);
     758     7120208 :         e->card = card;
     759     7120208 :         e->alias.name = cname;
     760     7120208 :         e->alias.parent = parent;
     761     7120208 :         e->r = (char*)e->alias.name;
     762     7120208 :         e->l = e->alias.parent;
     763     7120208 :         if (t)
     764     7119808 :                 e->tpe = *t;
     765     7120208 :         if (!has_nils)
     766     1955592 :                 set_has_no_nil(e);
     767     7120208 :         if (unique)
     768      944232 :                 set_unique(e);
     769     7120208 :         if (intern)
     770      605779 :                 set_intern(e);
     771             :         return e;
     772             : }
     773             : 
     774             : sql_exp *
     775    10153984 : exp_propagate(allocator *sa, sql_exp *ne, sql_exp *oe)
     776             : {
     777    10153984 :         if (has_label(oe) &&
     778      512339 :            (oe->alias.parent == ne->alias.parent || (oe->alias.parent && ne->alias.parent && a_match(oe->alias.parent, ne->alias.parent))) &&
     779      499972 :            (oe->alias.name == ne->alias.name || (oe->alias.name && ne->alias.name && strcmp(oe->alias.name, ne->alias.name) == 0)))
     780      499972 :                 ne->alias.label = oe->alias.label;
     781    10153984 :         if (is_intern(oe))
     782      308566 :                 set_intern(ne);
     783    10153984 :         if (is_anti(oe))
     784        3247 :                 set_anti(ne);
     785    10153984 :         if (is_semantics(oe))
     786      661902 :                 set_semantics(ne);
     787    10153984 :         if (is_any(oe))
     788          34 :                 set_any(ne);
     789    10153984 :         if (is_symmetric(oe))
     790          13 :                 set_symmetric(ne);
     791    10153984 :         if (is_partitioning(oe))
     792        1530 :                 set_partitioning(ne);
     793    10153984 :         if (is_ascending(oe))
     794        8965 :                 set_ascending(ne);
     795    10153984 :         if (nulls_last(oe))
     796        1822 :                 set_nulls_last(ne);
     797    10153984 :         if (need_distinct(oe))
     798         636 :                 set_distinct(ne);
     799    10153984 :         if (zero_if_empty(oe))
     800           0 :                 set_zero_if_empty(ne);
     801    10153984 :         if (need_no_nil(oe))
     802       97429 :                 set_no_nil(ne);
     803    10153984 :         if (!has_nil(oe))
     804     6026019 :                 set_has_no_nil(ne);
     805    10153984 :         if (has_nil(oe))
     806     4127967 :                 set_has_nil(ne);
     807    10153984 :         if (is_unique(oe))
     808     1141356 :                 set_unique(ne);
     809    10153984 :         if (is_basecol(oe))
     810     8298064 :                 set_basecol(ne);
     811    10153984 :         if (oe->virt)
     812           0 :                 ne->virt = 1;
     813    10153984 :         ne->p = prop_copy(sa, oe->p);
     814    10153986 :         return ne;
     815             : }
     816             : 
     817             : static sql_exp *
     818     6782396 : exp_ref_by_label(mvc *sql, sql_exp *o)
     819             : {
     820     6782396 :         allocator *sa = sql->sa;
     821     6782396 :         sql_exp *e = exp_create(sa, e_column);
     822             : 
     823     6782406 :         if (e == NULL)
     824             :                 return NULL;
     825     6782406 :         e->card = o->card;
     826     6782406 :         e->alias = o->alias;
     827     6782406 :         assert(e->alias.label);
     828     6782406 :         e->r = (char*)e->alias.name;
     829     6782406 :         e->l = e->alias.parent;
     830     6782406 :         e->nid = o->alias.label;
     831     6782406 :         assert(e->nid);
     832     6782406 :         sql_subtype *t = exp_subtype(o);
     833     6782406 :         if (t)
     834     6782266 :                 e->tpe = *t;
     835     6782406 :         if (!has_nil(o))
     836     4212339 :                 set_has_no_nil(e);
     837     6782406 :         if (has_nil(o))
     838     2570069 :                 set_has_nil(e);
     839     6782406 :         if (is_unique(o))
     840      905958 :                 set_unique(e);
     841     6782406 :         if (is_intern(o))
     842      273995 :                 set_intern(e);
     843     6782406 :         if (o->virt)
     844           0 :                 e->virt = 1;
     845     6782406 :         if ((o->type == e_column || o->type == e_convert) && o->f)
     846         461 :                 e->f = o->f;
     847     6782406 :         return exp_propagate(sa, e, o);
     848             : }
     849             : 
     850             : sql_exp *
     851     6782393 : exp_ref(mvc *sql, sql_exp *e)
     852             : {
     853     6782393 :         if (!has_label(e) && !exp_name(e))
     854        2304 :                 exp_label(sql->sa, e, ++sql->label);
     855     6782395 :         if (e->alias.label)
     856     6782395 :                 return exp_ref_by_label(sql, e);
     857           0 :         sql_exp *ne = exp_propagate(sql->sa, exp_column(sql->sa, exp_relname(e), exp_name(e), exp_subtype(e), exp_card(e), has_nil(e), is_unique(e), is_intern(e)), e);
     858           0 :         if (ne) {
     859           0 :                 ne->nid = e->alias.label;
     860           0 :                 assert(ne->nid);
     861           0 :                 assert(!ne->nid || ne->alias.name);
     862           0 :                 ne->alias.label = ne->nid;
     863             :         }
     864             :         return ne;
     865             : }
     866             : 
     867             : sql_exp *
     868        3932 : exp_ref_save(mvc *sql, sql_exp *e)
     869             : {
     870        3932 :         if (e->type == e_column)
     871             :                 return e;
     872        2128 :         if (is_atom(e->type))
     873        1122 :                 return exp_copy(sql, e);
     874        1006 :         if (!e->alias.label || !exp_name(e))
     875           8 :                 exp_label(sql->sa, e, ++sql->label);
     876        1006 :         if (e->type != e_column) /* ref as referenced within the (same) rank expression */
     877        1006 :                 e->ref = 1;
     878        1006 :         sql_exp *ne = exp_ref(sql, e);
     879        1006 :         if (ne && is_freevar(e))
     880           0 :                 set_freevar(ne, is_freevar(e)-1);
     881             :         return ne;
     882             : }
     883             : 
     884             : sql_exp *
     885     2267395 : exp_alias(mvc *sql, sql_alias *arname, const char *acname, sql_alias *org_rname, const char *org_cname, sql_subtype *t, unsigned int card, int has_nils, int unique, int intern)
     886             : {
     887     2267395 :         sql_exp *e = exp_column(sql->sa, org_rname, org_cname, t, card, has_nils, unique, intern);
     888             : 
     889     2267793 :         if (e == NULL)
     890             :                 return NULL;
     891     2267793 :         assert(acname && org_cname);
     892     2267793 :         exp_setname(sql, e, arname?arname:org_rname, acname);
     893     2267793 :         return e;
     894             : }
     895             : 
     896             : sql_exp *
     897           2 : exp_alias_ref(mvc *sql, sql_exp *e)
     898             : {
     899           2 :         sql_alias *tname = exp_relname(e);
     900           2 :         const char *cname = exp_name(e);
     901             : 
     902           2 :         if (!has_label(e))
     903           2 :                 exp_label(sql->sa, e, ++sql->label);
     904           2 :         sql_exp *ne = exp_ref(sql, e);
     905           2 :         if (ne == NULL)
     906             :                 return NULL;
     907           2 :         exp_setname(sql, ne, tname, cname);
     908           2 :         return ne;
     909             : }
     910             : 
     911             : sql_exp *
     912       16477 : exp_set(allocator *sa, const char *sname, const char *name, sql_exp *val, int level)
     913             : {
     914       16477 :         sql_exp *e = exp_create(sa, e_psm);
     915             : 
     916       16477 :         if (e == NULL)
     917             :                 return NULL;
     918       16477 :         e->alias.parent = sname?a_create(sa, sname):NULL;
     919       16477 :         e->alias.name = name;
     920       16477 :         e->l = val;
     921       16477 :         e->flag = PSM_SET + SET_PSM_LEVEL(level);
     922       16477 :         return e;
     923             : }
     924             : 
     925             : sql_exp *
     926        9693 : exp_var(allocator *sa, const char *sname, const char *name, sql_subtype *type, int level)
     927             : {
     928        9693 :         sql_exp *e = exp_create(sa, e_psm);
     929             : 
     930        9693 :         if (e == NULL)
     931             :                 return NULL;
     932        9693 :         e->alias.parent = sname?a_create(sa, sname):NULL;
     933        9693 :         e->alias.name = name;
     934        9693 :         e->tpe = *type;
     935        9693 :         e->flag = PSM_VAR + SET_PSM_LEVEL(level);
     936        9693 :         return e;
     937             : }
     938             : 
     939             : sql_exp *
     940         119 : exp_table(allocator *sa, const char *name, sql_table *t, int level)
     941             : {
     942         119 :         sql_exp *e = exp_create(sa, e_psm);
     943             : 
     944         119 :         if (e == NULL)
     945             :                 return NULL;
     946         119 :         e->alias.parent = NULL;
     947         119 :         e->alias.name = name;
     948         119 :         e->f = t;
     949         119 :         e->flag = PSM_VAR + SET_PSM_LEVEL(level);
     950         119 :         return e;
     951             : }
     952             : 
     953             : sql_exp *
     954       24833 : exp_return(allocator *sa, sql_exp *val, int level)
     955             : {
     956       24833 :         sql_exp *e = exp_create(sa, e_psm);
     957             : 
     958       24833 :         if (e == NULL)
     959             :                 return NULL;
     960       24833 :         e->l = val;
     961       24833 :         e->flag = PSM_RETURN + SET_PSM_LEVEL(level);
     962       24833 :         return e;
     963             : }
     964             : 
     965             : sql_exp *
     966        1039 : exp_while(allocator *sa, sql_exp *cond, list *stmts)
     967             : {
     968        1039 :         sql_exp *e = exp_create(sa, e_psm);
     969             : 
     970        1039 :         if (e == NULL)
     971             :                 return NULL;
     972        1039 :         e->l = cond;
     973        1039 :         e->r = stmts;
     974        1039 :         e->flag = PSM_WHILE;
     975        1039 :         return e;
     976             : }
     977             : 
     978             : sql_exp *
     979       11936 : exp_if(allocator *sa, sql_exp *cond, list *if_stmts, list *else_stmts)
     980             : {
     981       11936 :         sql_exp *e = exp_create(sa, e_psm);
     982             : 
     983       11936 :         if (e == NULL)
     984             :                 return NULL;
     985       11936 :         e->l = cond;
     986       11936 :         e->r = if_stmts;
     987       11936 :         e->f = else_stmts;
     988       11936 :         e->flag = PSM_IF;
     989       11936 :         return e;
     990             : }
     991             : 
     992             : sql_exp *
     993       82783 : exp_rel(mvc *sql, sql_rel *rel)
     994             : {
     995       82783 :         sql_exp *e = exp_create(sql->sa, e_psm);
     996             : 
     997       82783 :         if (e == NULL)
     998             :                 return NULL;
     999       82783 :         e->l = rel;
    1000       82783 :         e->flag = PSM_REL;
    1001       82783 :         e->card = is_single(rel)?CARD_ATOM:rel->card;
    1002       82783 :         assert(rel);
    1003       82783 :         if (is_topn(rel->op))
    1004           4 :                 rel = rel->l;
    1005       82783 :         if (is_project(rel->op)) {
    1006       63242 :                 sql_exp *last = rel->exps->t->data;
    1007       63242 :                 sql_subtype *t = exp_subtype(last);
    1008       63242 :                 e->tpe = t ? *t : (sql_subtype) {0};
    1009             :         }
    1010             :         return e;
    1011             : }
    1012             : 
    1013             : sql_exp *
    1014         157 : exp_exception(allocator *sa, sql_exp *cond, const char *error_message)
    1015             : {
    1016         157 :         sql_exp *e = exp_create(sa, e_psm);
    1017             : 
    1018         157 :         if (e == NULL)
    1019             :                 return NULL;
    1020         157 :         e->l = cond;
    1021         157 :         e->r = sa_strdup(sa, error_message);
    1022         157 :         e->flag = PSM_EXCEPTION;
    1023         157 :         return e;
    1024             : }
    1025             : 
    1026             : static void
    1027          57 : exps_setname(mvc *sql, list *exps, sql_alias *p, const char *name)
    1028             : {
    1029          57 :         sql_alias *ta = a_create(sql->sa, name);
    1030          57 :         ta->parent = p;
    1031          57 :         if (!list_empty(exps)) {
    1032         225 :                 for(node *n = exps->h; n; n = n->next) {
    1033         168 :                         sql_exp *e = n->data;
    1034         168 :                         exp_setname(sql, e, ta, exp_name(e));
    1035             :                 }
    1036             :         }
    1037          57 : }
    1038             : 
    1039             : /* Set a name (alias) for the expression, such that we can refer
    1040             :    to this expression by this simple name.
    1041             :  */
    1042             : void
    1043     3852764 : exp_setname(mvc *sql, sql_exp *e, sql_alias *p, const char *name )
    1044             : {
    1045     3852764 :         assert(name || p);
    1046     3852764 :         e->alias.label = -(sql->nid++);
    1047             :         //e->alias.label = 0;
    1048     3852764 :         if (name)
    1049     3693176 :                 e->alias.name = name;
    1050     3852764 :         e->alias.parent = p;
    1051     3852764 :         if (is_nested(e))
    1052          57 :                 exps_setname(sql, e->f, p, name);
    1053     3852764 : }
    1054             : 
    1055             : void
    1056      159716 : noninternexp_setname(mvc *sql, sql_exp *e, sql_alias *rname, const char *name )
    1057             : {
    1058      159716 :         if (!is_intern(e))
    1059      159714 :                 exp_setname(sql, e, rname, name);
    1060      159716 : }
    1061             : 
    1062             : void
    1063      115152 : noninternexp_settname(mvc *sql, sql_exp *e, sql_alias *rname)
    1064             : {
    1065      115152 :         char *name = NULL;
    1066             : 
    1067      115152 :         if (!e->alias.label && e->type == e_psm && e->l) {
    1068           5 :                 sql_rel *r = e->l;
    1069           5 :                 if (is_project(r->op)) {
    1070           5 :                         sql_exp *ie = r->exps->t->data;
    1071           5 :                         e->alias = ie->alias;
    1072           5 :                         e->alias.parent = rname;
    1073           5 :                         return;
    1074             :                 }
    1075             :         }
    1076      115147 :         if (!exp_name(e))
    1077         109 :                 name = make_label(sql->sa, ++sql->label);
    1078      115147 :         noninternexp_setname(sql, e, rname, name);
    1079             : }
    1080             : 
    1081             : void
    1082      698612 : exp_setalias(sql_exp *e, int label, sql_alias *p, const char *name )
    1083             : {
    1084      698612 :         e->alias.label = label;
    1085      698612 :         assert(e->alias.label);
    1086      698612 :         e->alias.name = name;
    1087      698612 :         e->alias.parent = p;
    1088      698612 : }
    1089             : 
    1090             : void
    1091     4279159 : exp_prop_alias(allocator *sa, sql_exp *e, sql_exp *oe )
    1092             : {
    1093     4279159 :         e->ref = oe->ref;
    1094     4279159 :         if (oe->alias.name == NULL && exp_has_rel(oe)) {
    1095        8206 :                 sql_rel *r = exp_rel_get_rel(sa, oe);
    1096        8206 :                 if (!is_project(r->op))
    1097             :                         return ;
    1098        8206 :                 oe = r->exps->t->data;
    1099             :         }
    1100     4279159 :         e->alias = oe->alias;
    1101             : }
    1102             : 
    1103             : str
    1104     2160010 : number2name(str s, int len, int i)
    1105             : {
    1106     2160010 :         s[--len] = 0;
    1107     6501492 :         while(i>0) {
    1108     4341482 :                 s[--len] = '0' + (i & 7);
    1109     4341482 :                 i >>= 3;
    1110             :         }
    1111     2160010 :         s[--len] = '%';
    1112     2160010 :         return s + len;
    1113             : }
    1114             : 
    1115             : void
    1116           0 : exp_setrelname(allocator *sa, sql_exp *e, int nr)
    1117             : {
    1118           0 :         char name[16], *nme;
    1119             : 
    1120           0 :         nme = number2name(name, sizeof(name), nr);
    1121           0 :         e->alias.label = 0;
    1122           0 :         e->alias.parent = a_create(sa, sa_strdup(sa, nme));
    1123           0 : }
    1124             : 
    1125             : char *
    1126     2098106 : make_label(allocator *sa, int nr)
    1127             : {
    1128     2098106 :         char name[16], *nme;
    1129             : 
    1130     2098106 :         nme = number2name(name, sizeof(name), nr);
    1131     2098866 :         return sa_strdup(sa, nme);
    1132             : }
    1133             : 
    1134             : sql_exp*
    1135     2088064 : exp_label(allocator *sa, sql_exp *e, int nr)
    1136             : {
    1137     2088064 :         assert(nr > 0);
    1138             :         //assert (e->alias.label == 0);
    1139     2088064 :         e->alias.label = nr;
    1140     2088064 :         e->alias.name = make_label(sa, nr);
    1141     2089901 :         e->alias.parent = a_create(sa, e->alias.name);
    1142     2089304 :         return e;
    1143             : }
    1144             : 
    1145             : list*
    1146       55356 : exps_label(mvc *sql, list *exps)
    1147             : {
    1148       55356 :         if (!exps)
    1149             :                 return NULL;
    1150             : 
    1151       55356 :         int nr = sql->label+1;
    1152       55356 :         sql->label += list_length(exps);
    1153      126795 :         for (node *n = exps->h; n; n = n->next)
    1154       71439 :                 n->data = exp_label(sql->sa, n->data, nr++);
    1155       55356 :         list_hash_clear(exps);
    1156       55356 :         return exps;
    1157             : }
    1158             : 
    1159             : void
    1160       21284 : exp_swap( sql_exp *e )
    1161             : {
    1162       21284 :         sql_exp *s = e->l;
    1163             : 
    1164       21284 :         e->l = e->r;
    1165       21284 :         e->r = s;
    1166       21284 :         e->flag = swap_compare((comp_type)e->flag);
    1167       21284 :         assert(!e->f);
    1168       21284 : }
    1169             : 
    1170             : sql_subtype *
    1171    34836716 : exp_subtype( sql_exp *e )
    1172             : {
    1173    34838789 :         switch(e->type) {
    1174     8378645 :         case e_atom: {
    1175     8378645 :                 if (e->l) {
    1176     7526400 :                         atom *a = e->l;
    1177     7526400 :                         return atom_type(a);
    1178      852245 :                 } else if (e->tpe.type) { /* atom reference */
    1179      848380 :                         return &e->tpe;
    1180        3865 :                 } else if (e->f) {
    1181        2073 :                         list *vals = exp_get_values(e);
    1182        2073 :                         if (!list_empty(vals))
    1183        2073 :                                 return exp_subtype(vals->h->data);
    1184             :                 }
    1185             :                 break;
    1186             :         }
    1187    21660468 :         case e_convert:
    1188             :         case e_column:
    1189    21660468 :                 if (e->tpe.type)
    1190    21660334 :                         return &e->tpe;
    1191             :                 break;
    1192     4664755 :         case e_aggr:
    1193             :         case e_func: {
    1194     4664755 :                 if (e->f) {
    1195     4664755 :                         sql_subfunc *f = e->f;
    1196     4664755 :                         if (f->res && list_length(f->res) == 1)
    1197     4621357 :                                 return f->res->h->data;
    1198             :                 }
    1199             :                 return NULL;
    1200             :         }
    1201        7497 :         case e_cmp:
    1202        7497 :                 return sql_bind_localtype("bit");
    1203      127424 :         case e_psm:
    1204      127424 :                 if (e->tpe.type)
    1205      127409 :                         return &e->tpe;
    1206             :                 /* fall through */
    1207             :         default:
    1208             :                 return NULL;
    1209             :         }
    1210             :         return NULL;
    1211             : }
    1212             : 
    1213             : const char *
    1214    35325790 : exp_name( sql_exp *e )
    1215             : {
    1216    35340264 :         if (e->alias.name)
    1217             :                 return e->alias.name;
    1218     1646957 :         if (e->type == e_convert && e->l)
    1219             :                 return exp_name(e->l);
    1220     1642875 :         if (e->type == e_psm && e->l) { /* subquery return name of last expression */
    1221       10392 :                 sql_rel *r = e->l;
    1222       10392 :                 if (is_project(r->op))
    1223       10392 :                         return exp_name(r->exps->t->data);
    1224             :         }
    1225             :         return NULL;
    1226             : }
    1227             : 
    1228             : sql_alias *
    1229     4024437 : exp_relname( sql_exp *e )
    1230             : {
    1231     4024448 :         if (e->alias.parent)
    1232             :                 return a_parent(&e->alias);
    1233      479753 :         if (!e->alias.name && e->type == e_convert && e->l)
    1234             :                 return exp_relname(e->l);
    1235      479742 :         if (!e->alias.name && e->type == e_psm && e->l) { /* subquery return name of last expression */
    1236           0 :                 sql_rel *r = e->l;
    1237           0 :                 if (is_project(r->op))
    1238           0 :                         return exp_relname(r->exps->t->data);
    1239             :         }
    1240             :         return NULL;
    1241             : }
    1242             : 
    1243             : sql_alias *
    1244       17079 : exp_find_rel_name(sql_exp *e)
    1245             : {
    1246       17079 :         if (e->alias.parent)
    1247             :                 return e->alias.parent;
    1248         557 :         switch(e->type) {
    1249             :         case e_column:
    1250             :                 break;
    1251           0 :         case e_convert:
    1252           0 :                 return exp_find_rel_name(e->l);
    1253             :         default:
    1254             :                 return NULL;
    1255             :         }
    1256             :         return NULL;
    1257             : }
    1258             : 
    1259             : unsigned int
    1260     1003581 : exp_card( sql_exp *e )
    1261             : {
    1262     1003581 :         return e->card;
    1263             : }
    1264             : 
    1265             : unsigned int
    1266     1675210 : exp_get_label( sql_exp *e )
    1267             : {
    1268     1675221 :         if (e->alias.label)
    1269     1675210 :                 return e->alias.label;
    1270          11 :         if (e->type == e_convert && e->l)
    1271             :                 return exp_get_label(e->l);
    1272           0 :         if (e->type == e_psm && e->l) { /* subquery return name of last expression */
    1273           0 :                 sql_rel *r = e->l;
    1274           0 :                 if (is_project(r->op))
    1275           0 :                         return exp_get_label(r->exps->t->data);
    1276             :         }
    1277             :         return 0;
    1278             : }
    1279             : 
    1280             : 
    1281             : const char *
    1282           0 : exp_func_name( sql_exp *e )
    1283             : {
    1284           0 :         if (e->type == e_func && e->f) {
    1285           0 :                 sql_subfunc *f = e->f;
    1286           0 :                 return f->func->base.name;
    1287             :         }
    1288           0 :         if (e->alias.name)
    1289             :                 return e->alias.name;
    1290           0 :         if (e->type == e_convert && e->l)
    1291           0 :                 return exp_name(e->l);
    1292             :         return NULL;
    1293             : }
    1294             : 
    1295             : int
    1296    45177816 : exp_cmp( sql_exp *e1, sql_exp *e2)
    1297             : {
    1298    45177816 :         return (e1 == e2)?0:-1;
    1299             : }
    1300             : 
    1301             : int
    1302      319400 : exp_equal( sql_exp *e1, sql_exp *e2)
    1303             : {
    1304      319400 :         if (e1 == e2)
    1305             :                 return 0;
    1306      319400 :         if (e1->alias.label && e1->alias.label == e2->alias.label)
    1307        8688 :                 return 0;
    1308             :         return -1;
    1309             : }
    1310             : 
    1311             : int
    1312       25837 : is_conflict( sql_exp *e1, sql_exp *e2)
    1313             : {
    1314       25837 :         if (e1->alias.label && e1->alias.label == e2->alias.label &&
    1315        2741 :                 e1->nid && e1->nid != e2->nid)
    1316           3 :                 return 0;
    1317             :         return -1;
    1318             : }
    1319             : 
    1320             : int
    1321    45177712 : exp_match( sql_exp *e1, sql_exp *e2)
    1322             : {
    1323    45177712 :         if (exp_cmp(e1, e2) == 0)
    1324             :                 return 1;
    1325    44933043 :         if (e1->type == e2->type && e1->type == e_column) {
    1326    20446505 :                 if (e1->nid && e1->nid == e2->nid)
    1327             :                         return 1;
    1328    19187318 :                 if (e1->alias.label != e2->alias.label || !e1->alias.label || !e2->alias.label)
    1329             :                         return 0;
    1330             :                 return 1;
    1331             :         }
    1332    24486538 :         if (e1->type == e2->type && e1->type == e_func) {
    1333     8746051 :                 if (is_identity(e1, NULL) && is_identity(e2, NULL)) {
    1334           0 :                         list *args1 = e1->l;
    1335           0 :                         list *args2 = e2->l;
    1336             : 
    1337           0 :                         if (list_length(args1) == list_length(args2) && list_length(args1) == 1) {
    1338           0 :                                 sql_exp *ne1 = args1->h->data;
    1339           0 :                                 sql_exp *ne2 = args2->h->data;
    1340             : 
    1341           0 :                                 if (exp_match(ne1,ne2))
    1342             :                                         return 1;
    1343             :                         }
    1344             :                 }
    1345             :         }
    1346             :         return 0;
    1347             : }
    1348             : 
    1349             : /* list already contains matching expression */
    1350             : sql_exp*
    1351      182566 : exps_find_exp( list *l, sql_exp *e)
    1352             : {
    1353      182566 :         node *n;
    1354             : 
    1355      182566 :         if (!l || !l->h)
    1356             :                 return NULL;
    1357             : 
    1358      420287 :         for(n=l->h; n; n = n->next) {
    1359      369299 :                 if (exp_match(n->data, e) || exp_refers(n->data, e))
    1360      112954 :                         return n->data;
    1361             :         }
    1362             :         return NULL;
    1363             : }
    1364             : 
    1365             : /* c refers to the parent p */
    1366             : int
    1367     3569969 : exp_refers( sql_exp *p, sql_exp *c)
    1368             : {
    1369     3569969 :         if (c->type == e_column && c->nid)
    1370      359482 :                 return c->nid == p->alias.label;
    1371             :         return 0;
    1372             : }
    1373             : 
    1374             : sql_exp*
    1375         220 : exps_refers(sql_exp *p, list *l)
    1376             : {
    1377         220 :         node *n;
    1378             : 
    1379         220 :         if (!l || !l->h)
    1380             :                 return NULL;
    1381             : 
    1382         552 :         for(n=l->h; n; n = n->next) {
    1383         336 :                 if (exp_refers(p, n->data))
    1384           4 :                         return n->data;
    1385             :         }
    1386             :         return NULL;
    1387             : }
    1388             : 
    1389             : int
    1390           0 : exp_match_col_exps( sql_exp *e, list *l)
    1391             : {
    1392           0 :         node *n;
    1393             : 
    1394           0 :         for(n=l->h; n; n = n->next) {
    1395           0 :                 sql_exp *re = n->data;
    1396           0 :                 sql_exp *re_r = re->r;
    1397             : 
    1398           0 :                 if (re->type == e_cmp && re->flag == cmp_or)
    1399           0 :                         return exp_match_col_exps(e, re->l) &&
    1400           0 :                                exp_match_col_exps(e, re->r);
    1401             : 
    1402           0 :                 if (re->type != e_cmp || !re_r || re_r->card != 1 || !exp_match_exp(e, re->l))
    1403           0 :                         return 0;
    1404             :         }
    1405             :         return 1;
    1406             : }
    1407             : 
    1408             : int
    1409        8922 : exps_match_col_exps( sql_exp *e1, sql_exp *e2)
    1410             : {
    1411        8922 :         sql_exp *e1_r = e1->r;
    1412        8922 :         sql_exp *e2_r = e2->r;
    1413             : 
    1414        8922 :         if (e1->type != e_cmp || e2->type != e_cmp)
    1415             :                 return 0;
    1416             : 
    1417        8851 :         if (!is_complex_exp(e1->flag) && e1_r && e1_r->card == CARD_ATOM &&
    1418        3651 :             !is_complex_exp(e2->flag) && e2_r && e2_r->card == CARD_ATOM)
    1419        2838 :                 return exp_match_exp(e1->l, e2->l);
    1420             : 
    1421        6013 :         if (!is_complex_exp(e1->flag) && e1_r && e1_r->card == CARD_ATOM &&
    1422         813 :             (e2->flag == cmp_in || e2->flag == cmp_notin))
    1423         729 :                 return exp_match_exp(e1->l, e2->l);
    1424        5284 :         if ((e1->flag == cmp_in || e1->flag == cmp_notin) &&
    1425        2779 :             !is_complex_exp(e2->flag) && e2_r && e2_r->card == CARD_ATOM)
    1426        1935 :                 return exp_match_exp(e1->l, e2->l);
    1427             : 
    1428        3349 :         if ((e1->flag == cmp_in || e1->flag == cmp_notin) &&
    1429         844 :             (e2->flag == cmp_in || e2->flag == cmp_notin))
    1430         844 :                 return exp_match_exp(e1->l, e2->l);
    1431             : 
    1432        2505 :         if (!is_complex_exp(e1->flag) && e1_r && e1_r->card == CARD_ATOM &&
    1433          84 :             e2->flag == cmp_or)
    1434           0 :                 return exp_match_col_exps(e1->l, e2->l) &&
    1435           0 :                        exp_match_col_exps(e1->l, e2->r);
    1436             : 
    1437        2505 :         if (e1->flag == cmp_or &&
    1438           0 :             !is_complex_exp(e2->flag) && e2_r && e2_r->card == CARD_ATOM)
    1439           0 :                 return exp_match_col_exps(e2->l, e1->l) &&
    1440           0 :                        exp_match_col_exps(e2->l, e1->r);
    1441             : 
    1442        2505 :         if (e1->flag == cmp_or && e2->flag == cmp_or) {
    1443           0 :                 list *l = e1->l, *r = e1->r;
    1444           0 :                 sql_exp *el = l->h->data;
    1445           0 :                 sql_exp *er = r->h->data;
    1446             : 
    1447           0 :                 return list_length(l) == 1 && list_length(r) == 1 &&
    1448           0 :                        exps_match_col_exps(el, e2) &&
    1449           0 :                        exps_match_col_exps(er, e2);
    1450             :         }
    1451             :         return 0;
    1452             : }
    1453             : 
    1454             : int
    1455       45088 : exp_match_list( list *l, list *r)
    1456             : {
    1457       45088 :         node *n, *m;
    1458       45088 :         char *lu, *ru;
    1459       45088 :         int lc = 0, rc = 0, match = 0;
    1460             : 
    1461       45088 :         if (!l || !r)
    1462           0 :                 return l == r;
    1463       45088 :         if (list_length(l) != list_length(r) || list_length(l) == 0 || list_length(r) == 0)
    1464         231 :                 return 0;
    1465       44857 :         if (list_length(l) > 10 || list_length(r) > 10)
    1466           5 :                 return 0;/* to expensive */
    1467             : 
    1468       44852 :         lu = ZNEW_ARRAY(char, list_length(l));
    1469       44852 :         ru = ZNEW_ARRAY(char, list_length(r));
    1470       44852 :         if (!lu || !ru) {
    1471           0 :                 _DELETE(lu);
    1472           0 :                 _DELETE(ru);
    1473           0 :                 return 0;
    1474             :         }
    1475      133544 :         for (n = l->h, lc = 0; n; n = n->next, lc++) {
    1476       88692 :                 sql_exp *le = n->data;
    1477             : 
    1478      265696 :                 for ( m = r->h, rc = 0; m; m = m->next, rc++) {
    1479      177004 :                         sql_exp *re = m->data;
    1480             : 
    1481      177004 :                         if (!ru[rc] && exp_match_exp(le,re)) {
    1482        6689 :                                 lu[lc] = 1;
    1483        6689 :                                 ru[rc] = 1;
    1484        6689 :                                 match = 1;
    1485             :                         }
    1486             :                 }
    1487             :         }
    1488       51973 :         for (n = l->h, lc = 0; n && match; n = n->next, lc++)
    1489        7121 :                 if (!lu[lc])
    1490        1246 :                         match = 0;
    1491       50264 :         for (n = r->h, rc = 0; n && match; n = n->next, rc++)
    1492        5412 :                 if (!ru[rc])
    1493           0 :                         match = 0;
    1494       44852 :         _DELETE(lu);
    1495       44852 :         _DELETE(ru);
    1496       44852 :         return match;
    1497             : }
    1498             : 
    1499             : static int
    1500     2462306 : exps_equal( list *l, list *r)
    1501             : {
    1502     2462306 :         node *n, *m;
    1503             : 
    1504     2462306 :         if (!l || !r)
    1505       52559 :                 return l == r;
    1506     2409747 :         if (list_length(l) != list_length(r))
    1507             :                 return 0;
    1508     3545154 :         for (n = l->h, m = r->h; n && m; n = n->next, m = m->next) {
    1509     3496555 :                 sql_exp *le = n->data, *re = m->data;
    1510             : 
    1511     3496555 :                 if (!exp_match_exp(le,re))
    1512             :                         return 0;
    1513             :         }
    1514             :         return 1;
    1515             : }
    1516             : 
    1517             : int
    1518    41556282 : exp_match_exp_semantics( sql_exp *e1, sql_exp *e2, bool semantics)
    1519             : {
    1520    41556282 :         if (exp_match(e1, e2))
    1521             :                 return 1;
    1522             : 
    1523    40306136 :         if (is_ascending(e1) != is_ascending(e2) ||
    1524    40293680 :                 nulls_last(e1) != nulls_last(e2) ||
    1525    40293680 :                 zero_if_empty(e1) != zero_if_empty(e2) ||
    1526    40144483 :                 need_no_nil(e1) != need_no_nil(e2) ||
    1527    40144483 :                 is_anti(e1) != is_anti(e2) ||
    1528    40141472 :                 (semantics && is_semantics(e1) != is_semantics(e2)) ||
    1529    29562806 :                 (semantics && is_any(e1) != is_any(e2)) ||
    1530    29562888 :                 is_symmetric(e1) != is_symmetric(e2) ||
    1531    29562876 :                 is_unique(e1) != is_unique(e2) ||
    1532             :                 need_distinct(e1) != need_distinct(e2))
    1533             :                 return 0;
    1534             : 
    1535    29170001 :         if (e1->type == e2->type) {
    1536    20861103 :                 switch(e1->type) {
    1537      228050 :                 case e_cmp:
    1538      406679 :                         if (e1->flag == e2->flag && !is_complex_exp(e1->flag) &&
    1539      180913 :                             exp_match_exp(e1->l, e2->l) && exp_match_exp(e1->r, e2->r) &&
    1540         254 :                             ((!e1->f && !e2->f) || (e1->f && e2->f && exp_match_exp(e1->f, e2->f))))
    1541         247 :                                 return 1;
    1542      229017 :                         else if (e1->flag == e2->flag && e1->flag == cmp_or &&
    1543        1226 :                             exp_match_list(e1->l, e2->l) && exp_match_list(e1->r, e2->r))
    1544             :                                 return 1;
    1545      227797 :                         else if (e1->flag == e2->flag &&
    1546      184111 :                                 (e1->flag == cmp_in || e1->flag == cmp_notin) &&
    1547        1961 :                             exp_match_exp(e1->l, e2->l) && exp_match_list(e1->r, e2->r))
    1548             :                                 return 1;
    1549      403835 :                         else if (e1->flag == e2->flag && (e1->flag == cmp_equal || e1->flag == cmp_notequal) &&
    1550      176077 :                                 exp_match_exp(e1->l, e2->r) && exp_match_exp(e1->r, e2->l))
    1551             :                                 return 1; /* = and <> operations are reflective, so exp_match_exp can be called crossed */
    1552             :                         break;
    1553      297473 :                 case e_convert:
    1554      358963 :                         if (!subtype_cmp(exp_totype(e1), exp_totype(e2)) &&
    1555       91701 :                             !subtype_cmp(exp_fromtype(e1), exp_fromtype(e2)) &&
    1556       30211 :                             exp_match_exp(e1->l, e2->l))
    1557             :                                 return 1;
    1558             :                         break;
    1559       47274 :                 case e_aggr:
    1560       81801 :                         if (!subfunc_cmp(e1->f, e2->f) && /* equal aggregation*/
    1561       34527 :                             exps_equal(e1->l, e2->l))
    1562             :                                 return 1;
    1563             :                         break;
    1564     4462602 :                 case e_func: {
    1565     4462602 :                         sql_subfunc *e1f = (sql_subfunc*) e1->f;
    1566     4462602 :                         const char *sname = e1f->func->s ? e1f->func->s->base.name : NULL;
    1567     4462602 :                         int (*comp)(list*, list*) = is_commutative(sname, e1f->func->base.name) ? exp_match_list : exps_equal;
    1568             : 
    1569     8925204 :                         if (!e1f->func->side_effect &&
    1570     6883028 :                                 !subfunc_cmp(e1f, e2->f) && /* equal functions */
    1571     2471428 :                                 comp(e1->l, e2->l) &&
    1572             :                                 /* optional order by expressions */
    1573       51002 :                                 exps_equal(e1->r, e2->r))
    1574             :                                         return 1;
    1575             :                         } break;
    1576     1090883 :                 case e_atom:
    1577     1090883 :                         if (e1->l && e2->l && !atom_cmp(e1->l, e2->l))
    1578             :                                 return 1;
    1579     1048525 :                         if (e1->f && e2->f && exps_equal(e1->f, e2->f))
    1580             :                                 return 1;
    1581     1048524 :                         if (e1->r && e2->r && e1->flag == e2->flag && !subtype_cmp(&e1->tpe, &e2->tpe)) {
    1582          54 :                                 sql_var_name *v1 = (sql_var_name*) e1->r, *v2 = (sql_var_name*) e2->r;
    1583          54 :                                 if (((!v1->sname && !v2->sname) || (v1->sname && v2->sname && strcmp(v1->sname, v2->sname) == 0)) &&
    1584          54 :                                         ((!v1->name && !v2->name) || (v1->name && v2->name && strcmp(v1->name, v2->name) == 0)))
    1585             :                                         return 1;
    1586             :                         }
    1587     1048524 :                         if (!e1->l && !e1->r && !e1->f && !e2->l && !e2->r && !e2->f && e1->flag == e2->flag && !subtype_cmp(&e1->tpe, &e2->tpe))
    1588             :                                 return 1;
    1589             :                         break;
    1590             :                 default:
    1591             :                         break;
    1592             :                 }
    1593             :         }
    1594             :         return 0;
    1595             : }
    1596             : 
    1597             : int
    1598    41363156 : exp_match_exp( sql_exp *e1, sql_exp *e2)
    1599             : {
    1600    41363156 :         return exp_match_exp_semantics( e1, e2, true);
    1601             : }
    1602             : 
    1603             : sql_exp *
    1604      167508 : exps_any_match(list *l, sql_exp *e)
    1605             : {
    1606      167508 :         if (!l)
    1607             :                 return NULL;
    1608      631699 :         for (node *n = l->h; n ; n = n->next) {
    1609      571652 :                 sql_exp *ne = (sql_exp *) n->data;
    1610      571652 :                 if (exp_match_exp(ne, e))
    1611      107461 :                         return ne;
    1612             :         }
    1613             :         return NULL;
    1614             : }
    1615             : 
    1616             : static int
    1617          24 : exps_are_joins( list *l )
    1618             : {
    1619          24 :         if (l)
    1620          52 :                 for (node *n = l->h; n; n = n->next) {
    1621          28 :                         sql_exp *e = n->data;
    1622             : 
    1623          28 :                         if (exp_is_join_exp(e))
    1624             :                                 return -1;
    1625             :                 }
    1626             :         return 0;
    1627             : }
    1628             : 
    1629             : int
    1630         202 : exp_is_join_exp(sql_exp *e)
    1631             : {
    1632         202 :         if (exp_is_join(e, NULL) == 0)
    1633             :                 return 0;
    1634          26 :         if (e->type == e_cmp && e->flag == cmp_or && e->card >= CARD_AGGR)
    1635          12 :                 if (exps_are_joins(e->l) == 0 && exps_are_joins(e->r) == 0)
    1636             :                         return 0;
    1637             :         return -1;
    1638             : }
    1639             : 
    1640             : static int
    1641      783285 : exp_is_complex_select( sql_exp *e )
    1642             : {
    1643      805053 :         switch (e->type) {
    1644         429 :         case e_atom: {
    1645         429 :                 if (e->f) {
    1646           0 :                         int r = (e->card == CARD_ATOM);
    1647           0 :                         list *l = e->f;
    1648             : 
    1649           0 :                         if (r)
    1650           0 :                                 for (node *n = l->h; n && !r; n = n->next)
    1651           0 :                                         r |= exp_is_complex_select(n->data);
    1652           0 :                         return r;
    1653             :                 }
    1654             :                 return 0;
    1655             :         }
    1656       21768 :         case e_convert:
    1657       21768 :                 return exp_is_complex_select(e->l);
    1658        2052 :         case e_func:
    1659             :         case e_aggr:
    1660             :         {
    1661        2052 :                 int r = (e->card == CARD_ATOM);
    1662        2052 :                 list *l = e->l;
    1663             : 
    1664        2052 :                 if (r && l)
    1665          37 :                         for (node *n = l->h; n && !r; n = n->next)
    1666           0 :                                 r |= exp_is_complex_select(n->data);
    1667             :                 return r;
    1668             :         }
    1669             :         case e_psm:
    1670             :                 return 1;
    1671             :         case e_column:
    1672             :         case e_cmp:
    1673             :         default:
    1674             :                 return 0;
    1675             :         }
    1676             : }
    1677             : 
    1678             : static int
    1679      391648 : complex_select(sql_exp *e)
    1680             : {
    1681      391648 :         sql_exp *l = e->l, *r = e->r;
    1682             : 
    1683      391648 :         if (exp_is_complex_select(l) || exp_is_complex_select(r))
    1684          37 :                 return 1;
    1685             :         return 0;
    1686             : }
    1687             : 
    1688             : static int
    1689         909 : distinct_rel(sql_exp *e, sql_alias **rname)
    1690             : {
    1691        1049 :         sql_alias *e_rname = NULL;
    1692             : 
    1693        1049 :         switch(e->type) {
    1694         637 :         case e_column:
    1695         637 :                 e_rname = exp_relname(e);
    1696             : 
    1697         637 :                 if (*rname && e_rname && a_match(e_rname, *rname))
    1698             :                         return 1;
    1699         492 :                 if (!*rname) {
    1700         313 :                         *rname = e_rname;
    1701         313 :                         return 1;
    1702             :                 }
    1703             :                 break;
    1704         141 :         case e_aggr:
    1705             :         case e_func:
    1706         141 :                 if (e->l) {
    1707         141 :                         int m = 1;
    1708         141 :                         list *l = e->l;
    1709         141 :                         node *n;
    1710             : 
    1711         426 :                         for(n=l->h; n && m; n = n->next) {
    1712         285 :                                 sql_exp *ae = n->data;
    1713             : 
    1714         285 :                                 m = distinct_rel(ae, rname);
    1715             :                         }
    1716         141 :                         return m;
    1717             :                 }
    1718             :                 return 0;
    1719             :         case e_atom:
    1720             :                 return 1;
    1721         140 :         case e_convert:
    1722         140 :                 return distinct_rel(e->l, rname);
    1723             :         default:
    1724             :                 return 0;
    1725             :         }
    1726             :         return 0;
    1727             : }
    1728             : 
    1729             : int
    1730    20505766 : rel_has_exp(sql_rel *rel, sql_exp *e, bool subexp)
    1731             : {
    1732    20505766 :         if (rel_find_exp_and_corresponding_rel(rel, e, subexp, NULL, NULL))
    1733     4538051 :                 return 0;
    1734             :         return -1;
    1735             : }
    1736             : 
    1737             : int
    1738           0 : rel_has_exps(sql_rel *rel, list *exps, bool subexp)
    1739             : {
    1740           0 :         if (list_empty(exps))
    1741             :                 return 0;
    1742           0 :         for (node *n = exps->h; n; n = n->next)
    1743           0 :                 if (rel_has_exp(rel, n->data, subexp) >= 0)
    1744             :                         return 0;
    1745             :         return -1;
    1746             : }
    1747             : 
    1748             : int
    1749           0 : rel_has_all_exps(sql_rel *rel, list *exps)
    1750             : {
    1751           0 :         if (list_empty(exps))
    1752             :                 return 1;
    1753           0 :         for (node *n = exps->h; n; n = n->next)
    1754           0 :                 if (rel_has_exp(rel, n->data, false) < 0)
    1755             :                         return 0;
    1756             :         return 1;
    1757             : }
    1758             : 
    1759             : static int
    1760    15481828 : rel_has_exp2(sql_rel *rel, sql_exp *e)
    1761             : {
    1762    15481828 :         return rel_has_exp(rel, e, false);
    1763             : }
    1764             : 
    1765             : sql_rel *
    1766     5893292 : find_rel(list *rels, sql_exp *e)
    1767             : {
    1768     5893292 :         node *n = list_find(rels, e, (fcmp)&rel_has_exp2);
    1769     5893292 :         if (n)
    1770     3375518 :                 return n->data;
    1771             :         return NULL;
    1772             : }
    1773             : 
    1774             : sql_rel *
    1775           0 : find_one_rel(list *rels, sql_exp *e)
    1776             : {
    1777           0 :         node *n;
    1778           0 :         sql_rel *fnd = NULL;
    1779             : 
    1780           0 :         for(n = rels->h; n; n = n->next) {
    1781           0 :                 if (rel_has_exp(n->data, e, false) == 0) {
    1782           0 :                         if (fnd)
    1783             :                                 return NULL;
    1784           0 :                         fnd = n->data;
    1785             :                 }
    1786             :         }
    1787             :         return fnd;
    1788             : }
    1789             : 
    1790             : static int
    1791         318 : exp_is_rangejoin(sql_exp *e, list *rels)
    1792             : {
    1793             :         /* assume e is a e_cmp with 3 args
    1794             :          * Need to check e->r and e->f only touch one table.
    1795             :          */
    1796         318 :         sql_alias *rname = NULL;
    1797             : 
    1798         318 :         if (distinct_rel(e->r, &rname) && distinct_rel(e->f, &rname))
    1799             :                 return 0;
    1800         179 :         if (rels) {
    1801         137 :                 sql_rel *r = find_rel(rels, e->r);
    1802         137 :                 sql_rel *f = find_rel(rels, e->f);
    1803         137 :                 if (r && f && r == f)
    1804             :                         return 0;
    1805             :         }
    1806             :         return -1;
    1807             : }
    1808             : 
    1809             : int
    1810      393372 : exp_is_join(sql_exp *e, list *rels)
    1811             : {
    1812             :         /* only simple compare expressions, ie not or lists
    1813             :                 or range expressions (e->f)
    1814             :          */
    1815      393372 :         if (e->type == e_cmp && !is_complex_exp(e->flag) && e->l && e->r && !e->f && e->card >= CARD_AGGR && !complex_select(e))
    1816             :                 return 0;
    1817        2079 :         if (e->type == e_cmp && e->flag == cmp_filter && e->l && e->r && e->card >= CARD_AGGR)
    1818             :                 return 0;
    1819             :         /* range expression */
    1820        1934 :         if (e->type == e_cmp && !is_complex_exp(e->flag) && e->l && e->r && e->f && e->card >= CARD_AGGR && !complex_select(e))
    1821         318 :                 return exp_is_rangejoin(e, rels);
    1822             :         return -1;
    1823             : }
    1824             : 
    1825             : int
    1826      387107 : exp_is_eqjoin(sql_exp *e)
    1827             : {
    1828      387107 :         if (e->flag == cmp_equal) {
    1829      377356 :                 sql_exp *l = e->l;
    1830      377356 :                 sql_exp *r = e->r;
    1831             : 
    1832      377356 :                 if (!is_func(l->type) && !is_func(r->type))
    1833      376228 :                         return 0;
    1834             :         }
    1835             :         return -1;
    1836             : }
    1837             : 
    1838             : sql_exp *
    1839      124000 : exps_find_prop(list *exps, rel_prop kind)
    1840             : {
    1841      124000 :         if (list_empty(exps))
    1842             :                 return NULL;
    1843      247517 :         for (node *n = exps->h ; n ; n = n->next) {
    1844      124000 :                 sql_exp *e = n->data;
    1845             : 
    1846      124000 :                 if (find_prop(e->p, kind))
    1847         483 :                         return e;
    1848             :         }
    1849             :         return NULL;
    1850             : }
    1851             : 
    1852             : /* check is one of the exps can be found in this relation */
    1853             : static sql_exp* rel_find_exp_and_corresponding_rel_(sql_rel *rel, sql_exp *e, bool subexp, sql_rel **res);
    1854             : 
    1855             : static bool
    1856      612353 : rel_find_exps_and_corresponding_rel_(sql_rel *rel, list *l, bool subexp, sql_rel **res)
    1857             : {
    1858      612353 :         int all = 1;
    1859             : 
    1860      612353 :         if (list_empty(l))
    1861             :                 return true;
    1862     1631339 :         for(node *n = l->h; n && (subexp || all); n = n->next) {
    1863     1039051 :                 sql_exp *ne = rel_find_exp_and_corresponding_rel_(rel, n->data, subexp, res);
    1864     1039051 :                 if (subexp && ne)
    1865             :                         return true;
    1866     1023214 :                 all &= (ne?1:0);
    1867             :         }
    1868      592288 :         if (all)
    1869             :                 return true;
    1870             :         return false;
    1871             : }
    1872             : 
    1873             : static sql_exp *
    1874    64726266 : rel_find_exp_and_corresponding_rel_(sql_rel *rel, sql_exp *e, bool subexp, sql_rel **res)
    1875             : {
    1876    64726266 :         sql_exp *ne = NULL;
    1877             : 
    1878    64726266 :         if (!rel)
    1879             :                 return NULL;
    1880    64726152 :         switch(e->type) {
    1881    62411349 :         case e_column:
    1882    62411349 :                 if (is_basetable(rel->op) && !rel->exps) {
    1883       26806 :                         assert(e->nid);
    1884       26806 :                         if (rel_base_has_nid(rel, e->nid))
    1885    62411307 :                                 ne = e;
    1886    75946046 :                 } else if ((!list_empty(rel->exps) && (is_project(rel->op) || is_base(rel->op))) ||
    1887    13828307 :                                         (!list_empty(rel->attr) && is_join(rel->op))) {
    1888    49089815 :                         list *l = rel->attr ? rel->attr : rel->exps;
    1889    49089815 :                         assert(e->nid);
    1890    49089815 :                         ne = exps_bind_nid(l, e->nid);
    1891             :                 }
    1892    62411307 :                 if (ne && res)
    1893       50938 :                         *res = rel;
    1894             :                 return ne;
    1895     1094099 :         case e_convert:
    1896     1094099 :                 return rel_find_exp_and_corresponding_rel_(rel, e->l, subexp, res);
    1897      612335 :         case e_aggr:
    1898             :         case e_func:
    1899      612335 :                 if (e->l)
    1900      612335 :                         if (rel_find_exps_and_corresponding_rel_(rel, e->l, subexp, res))
    1901             :                                 return e;
    1902             :                 return NULL;
    1903         401 :         case e_cmp:
    1904         401 :                 if (!subexp)
    1905             :                         return NULL;
    1906             : 
    1907          34 :                 if (e->flag == cmp_or || e->flag == cmp_filter) {
    1908          16 :                         if (rel_find_exps_and_corresponding_rel_(rel, e->l, subexp, res) ||
    1909           4 :                                 rel_find_exps_and_corresponding_rel_(rel, e->r, subexp, res))
    1910          12 :                                 return e;
    1911          22 :                 } else if (e->flag == cmp_in || e->flag == cmp_notin) {
    1912           4 :                         if (rel_find_exp_and_corresponding_rel_(rel, e->l, subexp, res) ||
    1913           2 :                                 rel_find_exps_and_corresponding_rel_(rel, e->r, subexp, res))
    1914           2 :                                 return e;
    1915          26 :                 } else if (rel_find_exp_and_corresponding_rel_(rel, e->l, subexp, res) ||
    1916           6 :                             rel_find_exp_and_corresponding_rel_(rel, e->r, subexp, res) ||
    1917           2 :                             (!e->f || rel_find_exp_and_corresponding_rel_(rel, e->f, subexp, res))) {
    1918          18 :                                 return e;
    1919             :                 }
    1920             :                 return NULL;
    1921             :         case e_psm:
    1922             :                 return NULL;
    1923      607908 :         case e_atom:
    1924      607908 :                 if (e->f) { /* values */
    1925          12 :                         list *l = e->f;
    1926          12 :                         node *n = l->h;
    1927             : 
    1928          12 :                         ne = n->data;
    1929          31 :                         while ((subexp || ne != NULL) && n != NULL) {
    1930          19 :                                 ne = rel_find_exp_and_corresponding_rel_(rel, n->data, subexp, res);
    1931          19 :                                 if (subexp && ne)
    1932             :                                         break;
    1933          19 :                                 n = n->next;
    1934             :                         }
    1935          12 :                         return ne;
    1936             :                 }
    1937             :                 return e;
    1938             :         }
    1939             :         return ne;
    1940             : }
    1941             : 
    1942             : sql_exp *
    1943    62593001 : rel_find_exp_and_corresponding_rel(sql_rel *rel, sql_exp *e, bool subexp, sql_rel **res, bool *under_join)
    1944             : {
    1945    62593001 :         sql_exp *ne = rel_find_exp_and_corresponding_rel_(rel, e, subexp, res);
    1946             : 
    1947    62593027 :         if (rel && !ne) {
    1948    45869527 :                 switch(rel->op) {
    1949     9042373 :                 case op_left:
    1950             :                 case op_right:
    1951             :                 case op_full:
    1952             :                 case op_join:
    1953             :                 case op_semi:
    1954             :                 case op_anti:
    1955     9042373 :                         ne = rel_find_exp_and_corresponding_rel(rel->l, e, subexp, res, under_join);
    1956     9042373 :                         if (!ne && is_join(rel->op))
    1957     5978779 :                                 ne = rel_find_exp_and_corresponding_rel(rel->r, e, subexp, res, under_join);
    1958             :                         break;
    1959             :                 case op_table:
    1960             :                 case op_basetable:
    1961             :                         break;
    1962      546837 :                 case op_munion:
    1963     1995885 :                         for (node* n = ((list*)rel->l)->h; n && !ne; n = n->next)
    1964     1449048 :                                 ne = rel_find_exp_and_corresponding_rel(n->data, e, subexp, res, under_join);
    1965             :                         break;
    1966    12173469 :                 default:
    1967    12173469 :                         if (!is_project(rel->op) && rel->l)
    1968     4498009 :                                 ne = rel_find_exp_and_corresponding_rel(rel->l, e, subexp, res, under_join);
    1969             :                 }
    1970             :         }
    1971    62593027 :         if (ne && under_join && is_join(rel->op))
    1972     2157982 :                 *under_join = true;
    1973    62593027 :         return ne;
    1974             : }
    1975             : 
    1976             : sql_exp *
    1977    17726671 : rel_find_exp(sql_rel *rel, sql_exp *e)
    1978             : {
    1979    17726671 :         return rel_find_exp_and_corresponding_rel(rel, e, false, NULL, NULL);
    1980             : }
    1981             : 
    1982             : bool
    1983       66363 : rel_find_nid(sql_rel *rel, int nid)
    1984             : {
    1985       66778 :         if (rel) {
    1986       66292 :                 switch(rel->op) {
    1987        5635 :                 case op_left:
    1988             :                 case op_right:
    1989             :                 case op_full:
    1990             :                 case op_join:
    1991             :                 case op_semi:
    1992             :                 case op_anti:
    1993        5635 :                         if (rel_find_nid(rel->l, nid))
    1994             :                                 return true;
    1995         499 :                         if (is_join(rel->op))
    1996         415 :                                 return rel_find_nid(rel->r, nid);
    1997             :                         break;
    1998       57276 :                 case op_table:
    1999             :                 case op_basetable:
    2000             :                 case op_munion:
    2001             :                 case op_union:
    2002             :                 case op_inter:
    2003             :                 case op_except:
    2004             :                 case op_project:
    2005             :                 case op_groupby:
    2006       57276 :                         if (rel->exps) {
    2007       57276 :                                 if (exps_bind_nid(rel->exps, nid))
    2008             :                                         return true;
    2009           0 :                         } else if (rel->op == op_basetable)
    2010           0 :                                 return rel_base_has_nid(rel, nid);
    2011             :                         break;
    2012        3381 :                 case op_select:
    2013             :                 case op_topn:
    2014             :                 case op_sample:
    2015        3381 :                         if (rel_find_nid(rel->l, nid))
    2016             :                                 return true;
    2017             :                         break;
    2018             :                 case op_ddl:
    2019             :                 case op_insert:
    2020             :                 case op_update:
    2021             :                 case op_delete:
    2022             :                 case op_truncate:
    2023             :                 case op_merge:
    2024             :                         return false;
    2025             : 
    2026             :                 }
    2027             :         }
    2028             :         return false;
    2029             : }
    2030             : 
    2031             : int
    2032     2426820 : exp_is_true(sql_exp *e)
    2033             : {
    2034     2426820 :         if (e->type == e_atom && e->l)
    2035       51198 :                 return atom_is_true(e->l);
    2036     2375622 :         if (e->type == e_cmp && e->flag == cmp_equal)
    2037     1897017 :                 return (exp_is_true(e->l) && exp_is_true(e->r) && exp_match_exp(e->l, e->r));
    2038             :         return 0;
    2039             : }
    2040             : 
    2041             : static inline bool
    2042      112164 : exp_is_cmp_exp_is_false(sql_exp* e)
    2043             : {
    2044      112164 :         sql_exp *l = e->l;
    2045      112164 :         sql_exp *r = e->r;
    2046      112164 :         assert(e->type == e_cmp && e->f == NULL && l && r);
    2047             : 
    2048             :         /* Handle 'v is x' and 'v is not x' expressions.
    2049             :         * Other cases in is-semantics are unspecified.
    2050             :         */
    2051      112164 :         if (e->flag != cmp_equal && e->flag != cmp_notequal)
    2052             :                 return false;
    2053      112164 :         if (e->flag == cmp_equal && !is_anti(e))
    2054      165110 :                 return ((exp_is_null(l) && exp_is_not_null(r)) || (exp_is_not_null(l) && exp_is_null(r)));
    2055       29609 :         if ((e->flag == cmp_notequal && !is_anti(e)) || (e->flag == cmp_equal && is_anti(e)))
    2056       59081 :                 return exp_is_null(l) && exp_is_null(r);
    2057             :         return false;
    2058             : }
    2059             : 
    2060             : static inline bool
    2061     4732077 : exp_single_bound_cmp_exp_is_false(sql_exp* e)
    2062             : {
    2063     4732077 :     assert(e->type == e_cmp);
    2064     4732077 :     sql_exp* l = e->l;
    2065     4732077 :     sql_exp* r = e->r;
    2066     4732077 :     assert(e->f == NULL);
    2067     4732077 :     assert (l && r);
    2068             : 
    2069     4732077 :     return exp_is_null(l) || exp_is_null(r);
    2070             : }
    2071             : 
    2072             : static inline bool
    2073       63153 : exp_two_sided_bound_cmp_exp_is_false(sql_exp* e)
    2074             : {
    2075       63153 :     assert(e->type == e_cmp);
    2076       63153 :     sql_exp* v = e->l;
    2077       63153 :     sql_exp* l = e->r;
    2078       63153 :     sql_exp* h = e->f;
    2079       63153 :     assert (v && l && h);
    2080             : 
    2081       63153 :     return is_anti(e) ? exp_is_null(v) || (exp_is_null(l) && exp_is_null(h)) : false;
    2082             : }
    2083             : 
    2084             : static inline bool
    2085     4920396 : exp_regular_cmp_exp_is_false(sql_exp* e)
    2086             : {
    2087     4920396 :     assert(e->type == e_cmp);
    2088             : 
    2089     4920396 :     if (is_semantics(e) && !is_any(e)) return exp_is_cmp_exp_is_false(e);
    2090     4808232 :         if (is_any(e)) return false;
    2091     4795230 :     if (e -> f)         return exp_two_sided_bound_cmp_exp_is_false(e);
    2092     4732077 :     else                return exp_single_bound_cmp_exp_is_false(e);
    2093             : }
    2094             : 
    2095             : static inline bool
    2096      424870 : exp_or_exp_is_false(sql_exp* e)
    2097             : {
    2098      424870 :     assert(e->type == e_cmp && e->flag == cmp_or);
    2099             : 
    2100      424870 :         list* left = e->l;
    2101      424870 :         list* right = e->r;
    2102             : 
    2103      424870 :         bool left_is_false = false;
    2104      894717 :         for(node* n = left->h; n; n=n->next) {
    2105      470461 :                 if (exp_is_false(n->data)) {
    2106             :                         left_is_false=true;
    2107             :                         break;
    2108             :                 }
    2109             :         }
    2110             : 
    2111      424870 :         if (!left_is_false) {
    2112             :                 return false;
    2113             :         }
    2114             : 
    2115        1176 :         for(node* n = right->h; n; n=n->next) {
    2116         643 :                 if (exp_is_false(n->data)) {
    2117             :                         return true;
    2118             :                 }
    2119             :         }
    2120             : 
    2121             :     return false;
    2122             : }
    2123             : 
    2124             : static inline bool
    2125     5642928 : exp_cmp_exp_is_false(sql_exp* e)
    2126             : {
    2127     5642928 :     assert(e->type == e_cmp);
    2128             : 
    2129     5642928 :     switch (e->flag) {
    2130     4920396 :     case cmp_gt:
    2131             :     case cmp_gte:
    2132             :     case cmp_lte:
    2133             :     case cmp_lt:
    2134             :     case cmp_equal:
    2135             :     case cmp_notequal:
    2136     4920396 :                 return exp_regular_cmp_exp_is_false(e);
    2137      424870 :     case cmp_or:
    2138      424870 :                 return exp_or_exp_is_false(e);
    2139             :     default:
    2140             :                 return false;
    2141             :         }
    2142             : }
    2143             : 
    2144             : int
    2145     5747193 : exp_is_false(sql_exp *e)
    2146             : {
    2147     5747193 :         if (e->type == e_atom && e->l)
    2148       57473 :                 return atom_is_false(e->l);
    2149     5689720 :         else if (e->type == e_cmp)
    2150     5642928 :                 return exp_cmp_exp_is_false(e);
    2151             :         return 0;
    2152             : }
    2153             : 
    2154             : int
    2155       17825 : exp_is_zero(sql_exp *e)
    2156             : {
    2157       17825 :         if (e->type == e_atom && e->l)
    2158       17594 :                 return atom_is_zero(e->l);
    2159             :         return 0;
    2160             : }
    2161             : 
    2162             : int
    2163      236568 : exp_is_not_null(sql_exp *e)
    2164             : {
    2165      236755 :         if (!has_nil(e))
    2166             :                 return true;
    2167             : 
    2168       83632 :         switch (e->type) {
    2169        3086 :         case e_atom:
    2170        3086 :                 if (e->f) /* values list */
    2171             :                         return false;
    2172        3086 :                 if (e->l)
    2173        2805 :                         return !(atom_null(e->l));
    2174             :                 return false;
    2175         187 :         case e_convert:
    2176         187 :                 return exp_is_not_null(e->l);
    2177        2512 :         case e_func:
    2178        2512 :                 if (!is_semantics(e) && e->l) {
    2179         264 :                         list *l = e->l;
    2180         311 :                         for (node *n = l->h; n; n=n->next) {
    2181         308 :                                 sql_exp *p = n->data;
    2182         308 :                                 if (!exp_is_not_null(p))
    2183             :                                         return false;
    2184             :                         }
    2185             :                         return true;
    2186             :                 }
    2187             :                 return false;
    2188             :         case e_aggr:
    2189             :         case e_column:
    2190             :         case e_cmp:
    2191             :         case e_psm:
    2192             :                 return false;
    2193             :         }
    2194             :         return false;
    2195             : }
    2196             : 
    2197             : static int
    2198        7374 : exps_have_null(list *l)
    2199             : {
    2200        7374 :         if (!l)
    2201             :                 return false;
    2202       15730 :         for(node *n = l->h; n; n = n->next)
    2203        8360 :                 if (exp_is_null(n->data))
    2204             :                         return true;
    2205             :         return false;
    2206             : }
    2207             : 
    2208             : int
    2209    10408696 : exp_is_null(sql_exp *e )
    2210             : {
    2211    10451878 :         if (!has_nil(e))
    2212             :                 return false;
    2213             : 
    2214     1396659 :         switch (e->type) {
    2215      108718 :         case e_atom:
    2216      108718 :                 if (e->f) /* values list */
    2217             :                         return 0;
    2218      108599 :                 if (e->l)
    2219       32945 :                         return (atom_null(e->l));
    2220             :                 return 0;
    2221       43182 :         case e_convert:
    2222       43182 :                 return exp_is_null(e->l);
    2223       90902 :         case e_func:
    2224       90902 :                 if (!is_semantics(e) && e->l) {
    2225             :                         /* This is a call to a function with no-nil semantics.
    2226             :                          * If one of the parameters is null the expression itself is null
    2227             :                          */
    2228       56457 :                         list* l = e->l;
    2229      168497 :                         for(node* n = l->h; n; n=n->next) {
    2230      112229 :                                 sql_exp* p = n->data;
    2231      112229 :                                 if (exp_is_null(p)) {
    2232             :                                         return true;
    2233             :                                 }
    2234             :                         }
    2235             :                 }
    2236             :                 return 0;
    2237       60406 :         case e_cmp:
    2238       60406 :                 if (!is_semantics(e)) {
    2239       57790 :                         if (e->flag == cmp_or || e->flag == cmp_filter) {
    2240       13038 :                                 return (exps_have_null(e->l) && exps_have_null(e->r));
    2241       51271 :                         } else if (e->flag == cmp_in || e->flag == cmp_notin) {
    2242        3799 :                                 return ((e->flag == cmp_in && exp_is_null(e->l)) ||
    2243        3797 :                                                 (e->flag == cmp_notin && (exp_is_null(e->l) || exps_have_null(e->r))));
    2244       47472 :                         } else if (e->f) {
    2245        6519 :                                 return exp_is_null(e->l) && exp_is_null(e->r) && exp_is_null(e->f);
    2246             :                         } else {
    2247       44214 :                                 return exp_is_null(e->l) || exp_is_null(e->r);
    2248             :                         }
    2249             :                 }
    2250             :                 return 0;
    2251             :         case e_aggr:
    2252             :         case e_column:
    2253             :         case e_psm:
    2254             :                 return 0;
    2255             :         }
    2256             :         return 0;
    2257             : }
    2258             : 
    2259             : int
    2260     1861436 : exp_is_rel( sql_exp *e )
    2261             : {
    2262     1871372 :         if (e) {
    2263     1871372 :                 switch(e->type){
    2264        9936 :                 case e_convert:
    2265        9936 :                         return exp_is_rel(e->l);
    2266      320704 :                 case e_psm:
    2267      320704 :                         return e->flag == PSM_REL && e->l;
    2268             :                 default:
    2269             :                         return 0;
    2270             :                 }
    2271             :         }
    2272             :         return 0;
    2273             : }
    2274             : 
    2275             : int
    2276        8097 : exps_one_is_rel(list *exps)
    2277             : {
    2278        8097 :         if (list_empty(exps))
    2279             :                 return 0;
    2280       24267 :         for(node *n = exps->h ; n ; n = n->next)
    2281       16179 :                 if (exp_is_rel(n->data))
    2282             :                         return 1;
    2283             :         return 0;
    2284             : }
    2285             : 
    2286             : int
    2287     8448057 : exp_is_atom( sql_exp *e )
    2288             : {
    2289     8789777 :         switch (e->type) {
    2290     1671006 :         case e_atom:
    2291     1671006 :                 if (e->f) /* values list */
    2292        8714 :                         return exps_are_atoms(e->f);
    2293             :                 return 1;
    2294      341720 :         case e_convert:
    2295      341720 :                 return exp_is_atom(e->l);
    2296      930276 :         case e_func:
    2297             :         case e_aggr:
    2298      930276 :                 return e->card == CARD_ATOM && exps_are_atoms(e->l);
    2299        2739 :         case e_cmp:
    2300        2739 :                 if (e->card != CARD_ATOM)
    2301             :                         return 0;
    2302         146 :                 if (e->flag == cmp_or || e->flag == cmp_filter)
    2303          77 :                         return exps_are_atoms(e->l) && exps_are_atoms(e->r);
    2304          73 :                 if (e->flag == cmp_in || e->flag == cmp_notin)
    2305           0 :                         return exp_is_atom(e->l) && exps_are_atoms(e->r);
    2306          73 :                 return exp_is_atom(e->l) && exp_is_atom(e->r) && (!e->f || exp_is_atom(e->f));
    2307             :         case e_column:
    2308             :         case e_psm:
    2309             :                 return 0;
    2310             :         }
    2311             :         return 0;
    2312             : }
    2313             : 
    2314             : static int
    2315           1 : exps_are_aggr(sql_rel *r, list *exps)
    2316             : {
    2317           1 :         int aggr = 1;
    2318           1 :         if (!list_empty(exps))
    2319           3 :                 for(node *n=exps->h; n && aggr; n=n->next)
    2320           2 :                         aggr &= exp_is_aggr(r, n->data);
    2321           1 :         return aggr;
    2322             : }
    2323             : 
    2324             : /* is expression e an aggregated result of r */
    2325             : int
    2326          11 : exp_is_aggr(sql_rel *r, sql_exp *e)
    2327             : {
    2328          11 :         sql_exp *ne = NULL;
    2329             : 
    2330          11 :         switch (e->type) {
    2331             :         case e_atom:
    2332             :                 return true;
    2333           0 :         case e_convert:
    2334           0 :                 return exp_is_aggr(r, e->l);
    2335           1 :         case e_func:
    2336           1 :                 return exps_are_aggr(r, e->l);
    2337             :         case e_aggr:
    2338             :                 return true;
    2339           0 :         case e_cmp:
    2340           0 :                 if (e->card != CARD_ATOM)
    2341             :                         return false;
    2342           0 :                 if (e->flag == cmp_or || e->flag == cmp_filter)
    2343           0 :                         return exps_are_aggr(r, e->l) && exps_are_aggr(r, e->r);
    2344           0 :                 if (e->flag == cmp_in || e->flag == cmp_notin)
    2345           0 :                         return exp_is_aggr(r, e->l) && exps_are_aggr(r, e->r);
    2346           0 :                 return exp_is_aggr(r, e->l) && exp_is_aggr(r, e->r) && (!e->f || exp_is_aggr(r, e->f));
    2347           9 :         case e_column:
    2348           9 :                 if (e->freevar)
    2349             :                         return true;
    2350           9 :                 ne = rel_find_exp(r, e);
    2351           9 :                 if (ne) /* found local */
    2352             :                         return true;
    2353             :                 else
    2354             :                         return false;
    2355             :         case e_psm:
    2356             :                 return false;
    2357             :         }
    2358             :         return false;
    2359             : }
    2360             : 
    2361             : static int
    2362          21 : exps_have_aggr(sql_rel *r, list *exps)
    2363             : {
    2364          21 :         int aggr = 0;
    2365          21 :         if (!list_empty(exps))
    2366          69 :                 for(node *n=exps->h; n && !aggr; n=n->next)
    2367          48 :                         aggr |= exp_has_aggr(r, n->data);
    2368          21 :         return aggr;
    2369             : }
    2370             : 
    2371             : int
    2372          89 : exp_has_aggr(sql_rel *r, sql_exp *e )
    2373             : {
    2374          97 :         sql_exp *ne = NULL;
    2375             : 
    2376          97 :         switch (e->type) {
    2377             :         case e_atom:
    2378             :                 return false;
    2379           8 :         case e_convert:
    2380           8 :                 return exp_has_aggr(r, e->l);
    2381          21 :         case e_func:
    2382          21 :                 return exps_have_aggr(r, e->l);
    2383             :         case e_aggr:
    2384             :                 return true;
    2385           0 :         case e_cmp:
    2386           0 :                 if (e->card != CARD_ATOM)
    2387             :                         return false;
    2388           0 :                 if (e->flag == cmp_or || e->flag == cmp_filter)
    2389           0 :                         return exps_have_aggr(r, e->l) && exps_have_aggr(r, e->r);
    2390           0 :                 if (e->flag == cmp_in || e->flag == cmp_notin)
    2391           0 :                         return exp_has_aggr(r, e->l) && exps_have_aggr(r, e->r);
    2392           0 :                 return exp_has_aggr(r, e->l) && exp_has_aggr(r, e->r) && (!e->f || exp_has_aggr(r, e->f));
    2393          39 :         case e_column:
    2394          39 :                 if (e->freevar)
    2395             :                         return false;
    2396          26 :                 ne = rel_find_exp(r->l, e);
    2397          26 :                 if (ne) /* found lower */
    2398             :                         return false;
    2399             :                 else
    2400             :                         return true;
    2401             :         case e_psm:
    2402             :                 return false;
    2403             :         }
    2404             :         return false;
    2405             : }
    2406             : 
    2407             : int
    2408    20626662 : exp_has_rel( sql_exp *e )
    2409             : {
    2410    21009444 :         if (!e)
    2411             :                 return 0;
    2412    21009444 :         switch(e->type){
    2413     2315189 :         case e_func:
    2414             :         case e_aggr:
    2415     2315189 :                 return exps_have_rel_exp(e->l);
    2416      557955 :         case e_cmp:
    2417      557955 :                 if (e->flag == cmp_or || e->flag == cmp_filter) {
    2418       61478 :                         return (exps_have_rel_exp(e->l) || exps_have_rel_exp(e->r));
    2419      496929 :                 } else if (e->flag == cmp_in || e->flag == cmp_notin) {
    2420       37361 :                         return (exp_has_rel(e->l) || exps_have_rel_exp(e->r));
    2421             :                 } else {
    2422      919136 :                         return (exp_has_rel(e->l) || exp_has_rel(e->r) || (e->f && exp_has_rel(e->f)));
    2423             :                 }
    2424      382782 :         case e_convert:
    2425      382782 :                 return exp_has_rel(e->l);
    2426      129275 :         case e_psm:
    2427      129275 :                 return exp_is_rel(e);
    2428    10041378 :         case e_atom:
    2429    10041378 :                 return (e->f && exps_have_rel_exp(e->f));
    2430             :         case e_column:
    2431             :                 return 0;
    2432             :         }
    2433             :         return 0;
    2434             : }
    2435             : 
    2436             : int
    2437     2983388 : exps_have_rel_exp( list *exps)
    2438             : {
    2439     2983388 :         if (list_empty(exps))
    2440             :                 return 0;
    2441    10508070 :         for(node *n=exps->h; n; n=n->next) {
    2442     7614813 :                 sql_exp *e = n->data;
    2443             : 
    2444     7614813 :                 if (exp_has_rel(e))
    2445             :                         return 1;
    2446             :         }
    2447             :         return 0;
    2448             : }
    2449             : 
    2450             : static sql_rel *
    2451         581 : exps_rel_get_rel(allocator *sa, list *exps )
    2452             : {
    2453         581 :         sql_rel *r = NULL, *xp = NULL;
    2454             : 
    2455         581 :         if (list_empty(exps))
    2456             :                 return NULL;
    2457        1706 :         for (node *n = exps->h; n; n=n->next){
    2458        1125 :                 sql_exp *e = n->data;
    2459             : 
    2460        1125 :                 if (exp_has_rel(e)) {
    2461         587 :                         if (!(r = exp_rel_get_rel(sa, e)))
    2462             :                                 return NULL;
    2463         587 :                         if (xp) {
    2464           6 :                                 xp = rel_crossproduct(sa, xp, r, op_full);
    2465           6 :                                 set_processed(xp);
    2466             :                         } else {
    2467             :                                 xp = r;
    2468             :                         }
    2469             :                 }
    2470             :         }
    2471             :         return xp;
    2472             : }
    2473             : 
    2474             : int
    2475          62 : exp_rel_depth(sql_exp *e)
    2476             : {
    2477          62 :         if (!e)
    2478             :                 return 0;
    2479          62 :         switch(e->type){
    2480             :         case e_func:
    2481             :         case e_aggr:
    2482             :         case e_cmp:
    2483             :                 return 1;
    2484             :         case e_convert:
    2485             :                 return 0;
    2486          41 :         case e_psm:
    2487          41 :                 if (exp_is_rel(e))
    2488             :                         return 0;
    2489             :                 return 1;
    2490             :         case e_atom:
    2491             :         case e_column:
    2492             :                 return 0;
    2493             :         }
    2494             :         return 0;
    2495             : }
    2496             : 
    2497             : sql_rel *
    2498       81194 : exp_rel_get_rel(allocator *sa, sql_exp *e)
    2499             : {
    2500       82306 :         if (!e)
    2501             :                 return NULL;
    2502             : 
    2503       82306 :         switch(e->type){
    2504         553 :         case e_func:
    2505             :         case e_aggr:
    2506         553 :                 return exps_rel_get_rel(sa, e->l);
    2507          38 :         case e_cmp: {
    2508          38 :                 sql_rel *r = NULL, *xp = NULL;
    2509             : 
    2510          38 :                 if (e->flag == cmp_or || e->flag == cmp_filter) {
    2511          11 :                         if (exps_have_rel_exp(e->l))
    2512           7 :                                 xp = exps_rel_get_rel(sa, e->l);
    2513          11 :                         if (exps_have_rel_exp(e->r)) {
    2514           6 :                                 if (!(r = exps_rel_get_rel(sa, e->r)))
    2515             :                                         return NULL;
    2516           6 :                                 if (xp) {
    2517           2 :                                         xp = rel_crossproduct(sa, xp, r, op_join);
    2518           2 :                                         set_processed(xp);
    2519             :                                 } else {
    2520             :                                         xp = r;
    2521             :                                 }
    2522             :                         }
    2523          27 :                 } else if (e->flag == cmp_in || e->flag == cmp_notin) {
    2524           0 :                         if (exp_has_rel(e->l))
    2525           0 :                                 xp = exp_rel_get_rel(sa, e->l);
    2526           0 :                         if (exps_have_rel_exp(e->r)) {
    2527           0 :                                 if (!(r = exps_rel_get_rel(sa, e->r)))
    2528             :                                         return NULL;
    2529           0 :                                 if (xp) {
    2530           0 :                                         xp = rel_crossproduct(sa, xp, r, op_join);
    2531           0 :                                         set_processed(xp);
    2532             :                                 } else {
    2533             :                                         xp = r;
    2534             :                                 }
    2535             :                         }
    2536             :                 } else {
    2537          27 :                         if (exp_has_rel(e->l))
    2538          25 :                                 xp = exp_rel_get_rel(sa, e->l);
    2539          27 :                         if (exp_has_rel(e->r)) {
    2540           7 :                                 if (!(r = exp_rel_get_rel(sa, e->r)))
    2541             :                                         return NULL;
    2542           7 :                                 if (xp) {
    2543           5 :                                         xp = rel_crossproduct(sa, xp, r, op_join);
    2544           5 :                                         set_processed(xp);
    2545             :                                 } else {
    2546             :                                         xp = r;
    2547             :                                 }
    2548             :                         }
    2549          27 :                         if (e->f && exp_has_rel(e->f)) {
    2550           0 :                                 if (!(r = exp_rel_get_rel(sa, e->f)))
    2551             :                                         return NULL;
    2552           0 :                                 if (xp) {
    2553           0 :                                         xp = rel_crossproduct(sa, xp, r, op_join);
    2554           0 :                                         set_processed(xp);
    2555             :                                 } else {
    2556             :                                         xp = r;
    2557             :                                 }
    2558             :                         }
    2559             :                 }
    2560             :                 return xp;
    2561             :         }
    2562        1112 :         case e_convert:
    2563        1112 :                 return exp_rel_get_rel(sa, e->l);
    2564       80588 :         case e_psm:
    2565       80588 :                 if (exp_is_rel(e))
    2566       80588 :                         return e->l;
    2567             :                 return NULL;
    2568          15 :         case e_atom:
    2569          15 :                 if (e->f && exps_have_rel_exp(e->f))
    2570          15 :                         return exps_rel_get_rel(sa, e->f);
    2571             :                 return NULL;
    2572             :         case e_column:
    2573             :                 return NULL;
    2574             :         }
    2575             :         return NULL;
    2576             : }
    2577             : 
    2578             : static void exp_rel_update_set_freevar(sql_exp *e);
    2579             : 
    2580             : static void
    2581         942 : exps_rel_update_set_freevar(list *exps)
    2582             : {
    2583         942 :         if (!list_empty(exps))
    2584        3070 :                 for (node *n=exps->h; n ; n=n->next)
    2585        2128 :                         exp_rel_update_set_freevar(n->data);
    2586         942 : }
    2587             : 
    2588             : static void
    2589        2423 : exp_rel_update_set_freevar(sql_exp *e)
    2590             : {
    2591        2435 :         if (!e)
    2592             :                 return ;
    2593             : 
    2594        2435 :         switch(e->type){
    2595         940 :         case e_func:
    2596             :         case e_aggr:
    2597         940 :                 exps_rel_update_set_freevar(e->l);
    2598         940 :                 break;
    2599           8 :         case e_cmp:
    2600           8 :                 if (e->flag == cmp_or || e->flag == cmp_filter) {
    2601           0 :                         exps_rel_update_set_freevar(e->l);
    2602           0 :                         exps_rel_update_set_freevar(e->r);
    2603           8 :                 } else if (e->flag == cmp_in || e->flag == cmp_notin) {
    2604           0 :                         exp_rel_update_set_freevar(e->l);
    2605           0 :                         exps_rel_update_set_freevar(e->r);
    2606             :                 } else {
    2607           8 :                         exp_rel_update_set_freevar(e->l);
    2608           8 :                         exp_rel_update_set_freevar(e->r);
    2609           8 :                         if (e->f)
    2610             :                                 exp_rel_update_set_freevar(e->f);
    2611             :                 }
    2612             :                 break;
    2613           9 :         case e_convert:
    2614           9 :                 exp_rel_update_set_freevar(e->l);
    2615           9 :                 break;
    2616        1191 :         case e_atom:
    2617        1191 :                 if (e->f)
    2618           2 :                         exps_rel_update_set_freevar(e->f);
    2619             :                 break;
    2620         287 :         case e_column:
    2621         287 :                 set_freevar(e, 1);
    2622         287 :                 break;
    2623             :         case e_psm:
    2624             :                 break;
    2625             :         }
    2626             : }
    2627             : 
    2628             : static list *
    2629         581 : exp_rel_update_exps(mvc *sql, list *exps, bool up)
    2630             : {
    2631         581 :         if (list_empty(exps))
    2632             :                 return exps;
    2633        1706 :         for (node *n = exps->h; n; n=n->next){
    2634        1125 :                 sql_exp *e = n->data;
    2635             : 
    2636        1125 :                 if (exp_has_rel(e))
    2637         587 :                         n->data = exp_rel_update_exp(sql, e, up);
    2638         538 :                 else if (!exp_is_atom(e) && !up)
    2639         273 :                         exp_rel_update_set_freevar(e);
    2640             :         }
    2641             :         return exps;
    2642             : }
    2643             : 
    2644             : static sql_exp *
    2645          57 : exp_rel_update_exp_(mvc *sql, sql_exp *e, bool up)
    2646             : {
    2647          57 :         if (exp_has_rel(e))
    2648          31 :                 e = exp_rel_update_exp(sql, e, up);
    2649          26 :         else if (!exp_is_atom(e) && !up)
    2650           6 :                 exp_rel_update_set_freevar(e);
    2651          57 :         return e;
    2652             : }
    2653             : 
    2654             : sql_exp *
    2655       14001 : exp_rel_update_exp(mvc *sql, sql_exp *e, bool up)
    2656             : {
    2657       14001 :         if (!e)
    2658             :                 return NULL;
    2659             : 
    2660       14001 :         switch(e->type){
    2661         553 :         case e_func:
    2662             :         case e_aggr:
    2663         553 :                 if (exps_have_rel_exp(e->l))
    2664         553 :                         e->l = exp_rel_update_exps(sql, e->l, up);
    2665             :                 return e;
    2666          37 :         case e_cmp:
    2667          37 :                 if (e->flag == cmp_or || e->flag == cmp_filter) {
    2668          11 :                         if (exps_have_rel_exp(e->l))
    2669           7 :                                 e->l = exp_rel_update_exps(sql, e->l, up);
    2670          11 :                         if (exps_have_rel_exp(e->r))
    2671           6 :                                 e->r = exp_rel_update_exps(sql, e->r, up);
    2672          26 :                 } else if (e->flag == cmp_in || e->flag == cmp_notin) {
    2673           0 :                         if (exp_has_rel(e->l))
    2674           0 :                                 e->l = exp_rel_update_exp(sql, e->l, up);
    2675           0 :                         if (exps_have_rel_exp(e->r))
    2676           0 :                                 e->r = exp_rel_update_exps(sql, e->r, up);
    2677             :                 } else {
    2678             :                         //if (exp_has_rel(e->l))
    2679          26 :                                 e->l = exp_rel_update_exp_(sql, e->l, up);
    2680             :                         //if (exp_has_rel(e->r))
    2681          26 :                                 e->r = exp_rel_update_exp_(sql, e->r, up);
    2682          26 :                         if (e->f /*&& exp_has_rel(e->f)*/)
    2683           5 :                                 e->f = exp_rel_update_exp_(sql, e->f, up);
    2684             :                 }
    2685             :                 return e;
    2686        1112 :         case e_convert:
    2687        1112 :                 if (exp_has_rel(e->l))
    2688        1112 :                         e->l = exp_rel_update_exp(sql, e->l, up);
    2689             :                 return e;
    2690       12284 :         case e_psm:
    2691       12284 :                 if (exp_is_rel(e)) {
    2692       12284 :                         sql_rel *r = exp_rel_get_rel(sql->sa, e), *nr = r;
    2693       12284 :                         if (is_topn(r->op)) {
    2694           2 :                                 nr = r->l;
    2695           2 :                                 if (nr && !is_project(nr->op))
    2696           0 :                                         r->l = nr = rel_project(sql->sa, nr, rel_projections(sql, nr, NULL, 1, 0));
    2697             :                         }
    2698       12284 :                         e = nr->exps->t->data;
    2699       12284 :                         e = exp_ref(sql, e);
    2700       12284 :                         if (up)
    2701           0 :                                 set_freevar(e, 1);
    2702       12284 :                         return e;
    2703             :                 }
    2704             :                 return e;
    2705          15 :         case e_atom:
    2706          15 :                 if (e->f && exps_have_rel_exp(e->f))
    2707          15 :                         e->f = exp_rel_update_exps(sql, e->f, up);
    2708             :                 return e;
    2709             :         case e_column:
    2710             :                 return e;
    2711             :         }
    2712             :         return e;
    2713             : }
    2714             : 
    2715             : sql_exp *
    2716        4229 : exp_rel_label(mvc *sql, sql_exp *e)
    2717             : {
    2718        4229 :         if (exp_is_rel(e))
    2719        4229 :                 e->l = rel_label(sql, e->l, 1);
    2720        4229 :         return e;
    2721             : }
    2722             : 
    2723             : int
    2724      141141 : exps_are_atoms( list *exps)
    2725             : {
    2726      141141 :         int atoms = 1;
    2727      141141 :         if (!list_empty(exps))
    2728      389855 :                 for(node *n=exps->h; n && atoms; n=n->next)
    2729      280281 :                         atoms &= exp_is_atom(n->data);
    2730      141143 :         return atoms;
    2731             : }
    2732             : 
    2733             : int
    2734         151 : exps_have_func(list *exps)
    2735             : {
    2736         151 :         if (list_empty(exps))
    2737             :                 return 0;
    2738         183 :         for(node *n=exps->h; n; n=n->next) {
    2739         155 :                 sql_exp *e = n->data;
    2740             : 
    2741         155 :                 if (exp_has_func(e))
    2742             :                         return 1;
    2743             :         }
    2744             :         return 0;
    2745             : }
    2746             : 
    2747             : static int exp_has_func_or_cmp(sql_exp *e, bool cmp);
    2748             : 
    2749             : static int
    2750       47861 : exps_have_func_or_cmp(list *exps, bool cmp)
    2751             : {
    2752       47861 :         if (list_empty(exps))
    2753             :                 return 0;
    2754      132588 :         for(node *n=exps->h; n; n=n->next) {
    2755       91969 :                 sql_exp *e = n->data;
    2756             : 
    2757       91969 :                 if (exp_has_func_or_cmp(e, cmp))
    2758             :                         return 1;
    2759             :         }
    2760             :         return 0;
    2761             : }
    2762             : 
    2763             : static int
    2764     1657768 : exp_has_func_or_cmp(sql_exp *e, bool cmp)
    2765             : {
    2766     1657768 :         if (!e)
    2767             :                 return 0;
    2768     1657768 :         switch (e->type) {
    2769      154417 :         case e_atom:
    2770      154417 :                 if (e->f)
    2771           0 :                         return exps_have_func_or_cmp(e->f, true);
    2772             :                 return 0;
    2773       16526 :         case e_convert:
    2774             :                 {
    2775       16526 :                         sql_subtype *t = exp_totype(e);
    2776       16526 :                         sql_subtype *f = exp_fromtype(e);
    2777       16526 :                         if (t->type->eclass == EC_FLT && (f->type->eclass == EC_DEC || f->type->eclass == EC_NUM))
    2778         166 :                                 return exp_has_func_or_cmp(e->l, cmp);
    2779       16360 :                         if (f->type->localtype > t->type->localtype)
    2780             :                                 return true;
    2781             :                 }
    2782       14148 :                 return exp_has_func_or_cmp(e->l, cmp);
    2783             :         case e_func:
    2784             :                 return 1;
    2785       20979 :         case e_aggr:
    2786       20979 :                 if (e->l)
    2787       18122 :                         return exps_have_func_or_cmp(e->l, true);
    2788             :                 return 0;
    2789      130017 :         case e_cmp:
    2790      130017 :                 if (cmp)
    2791             :                         return 1;
    2792      122856 :                 if (e->flag == cmp_or || e->flag == cmp_filter) {
    2793       18500 :                         return (exps_have_func_or_cmp(e->l, true) || exps_have_func_or_cmp(e->r, true));
    2794      111530 :                 } else if (e->flag == cmp_in || e->flag == cmp_notin) {
    2795       14310 :                         return (exp_has_func_or_cmp(e->l, true) || exps_have_func_or_cmp(e->r, true));
    2796             :                 } else {
    2797      194534 :                         return (exp_has_func_or_cmp(e->l, true) || exp_has_func_or_cmp(e->r, true) ||
    2798       89292 :                                         (e->f && exp_has_func_or_cmp(e->f, true)));
    2799             :                 }
    2800             :         case e_column:
    2801             :         case e_psm:
    2802             :                 return 0;
    2803             :         }
    2804             :         return 0;
    2805             : }
    2806             : 
    2807             : int
    2808     1348582 : exp_has_func(sql_exp *e)
    2809             : {
    2810     1348582 :         return exp_has_func_or_cmp(e, false);
    2811             : }
    2812             : 
    2813             : static int
    2814      763488 : exps_have_sideeffect( list *exps)
    2815             : {
    2816      763488 :         node *n;
    2817      763488 :         int has_sideeffect = 0;
    2818             : 
    2819     2342847 :         for(n=exps->h; n && !has_sideeffect; n=n->next)
    2820     1579358 :                 has_sideeffect |= exp_has_sideeffect(n->data);
    2821      763489 :         return has_sideeffect;
    2822             : }
    2823             : 
    2824             : int
    2825     1758533 : exp_has_sideeffect( sql_exp *e )
    2826             : {
    2827     1783716 :         switch (e->type) {
    2828       25183 :         case e_convert:
    2829       25183 :                 return exp_has_sideeffect(e->l);
    2830      763504 :         case e_func:
    2831             :                 {
    2832      763504 :                         sql_subfunc *f = e->f;
    2833             : 
    2834      763504 :                         if (f->func->side_effect)
    2835             :                                 return 1;
    2836      763490 :                         if (e->l)
    2837      763488 :                                 return exps_have_sideeffect(e->l);
    2838             :                         return 0;
    2839             :                 }
    2840      492422 :         case e_atom:
    2841      492422 :                 if (e->f)
    2842           0 :                         return exps_have_sideeffect(e->f);
    2843             :                 return 0;
    2844             :         case e_aggr:
    2845             :         case e_cmp:
    2846             :         case e_column:
    2847             :         case e_psm:
    2848             :                 return 0;
    2849             :         }
    2850             :         return 0;
    2851             : }
    2852             : 
    2853             : bool
    2854     1094510 : exps_have_unsafe(list *exps, bool allow_identity, bool card)
    2855             : {
    2856     1094510 :         int unsafe = 0;
    2857             : 
    2858     1094510 :         if (list_empty(exps))
    2859             :                 return 0;
    2860     3786206 :         for (node *n = exps->h; n && !unsafe; n = n->next)
    2861     2712588 :                 unsafe |= exp_unsafe(n->data, allow_identity, card);
    2862     1073618 :         return unsafe;
    2863             : }
    2864             : 
    2865             : bool
    2866    12125528 : exp_unsafe(sql_exp *e, bool allow_identity, bool card)
    2867             : {
    2868    12125528 :         switch (e->type) {
    2869      770600 :         case e_convert:
    2870      770600 :                 if (card) {
    2871        8905 :                         sql_subtype *t = exp_totype(e);
    2872        8905 :                         sql_subtype *f = exp_fromtype(e);
    2873        8905 :                         if (t->type->eclass == EC_FLT && (f->type->eclass == EC_DEC || f->type->eclass == EC_NUM))
    2874             :                                 return false;
    2875        8820 :                         if (f->type->localtype > t->type->localtype)
    2876             :                                 return true;
    2877             :                         return false;
    2878             :                 }
    2879      761695 :                 return exp_unsafe(e->l, allow_identity, card);
    2880     1033743 :         case e_aggr:
    2881             :         case e_func: {
    2882     1033743 :                 sql_subfunc *f = e->f;
    2883             : 
    2884     1033743 :                 if (IS_ANALYTIC(f->func) || !LANG_INT_OR_MAL(f->func->lang) || f->func->side_effect || (!allow_identity && is_identity(e, NULL)))
    2885       45094 :                         return 1;
    2886      988649 :                 return exps_have_unsafe(e->l, allow_identity, card);
    2887       50708 :         } break;
    2888       50708 :         case e_cmp: {
    2889       50708 :                 if (e->flag == cmp_in || e->flag == cmp_notin) {
    2890        6667 :                         return exp_unsafe(e->l, allow_identity, card) || exps_have_unsafe(e->r, allow_identity, card);
    2891       44041 :                 } else if (e->flag == cmp_or || e->flag == cmp_filter) {
    2892        9946 :                         return exps_have_unsafe(e->l, allow_identity, card) || exps_have_unsafe(e->r, allow_identity, card);
    2893             :                 } else {
    2894       68222 :                         return exp_unsafe(e->l, allow_identity, card) || exp_unsafe(e->r, allow_identity, card) || (e->f && exp_unsafe(e->f, allow_identity, card));
    2895             :                 }
    2896     1173868 :         } break;
    2897     1173868 :         case e_atom: {
    2898     1173868 :                 if (e->f)
    2899        8463 :                         return exps_have_unsafe(e->f, allow_identity, card);
    2900             :                 return 0;
    2901             :         } break;
    2902             :         case e_column:
    2903             :         case e_psm:
    2904             :                 return 0;
    2905             :         }
    2906             :         return 0;
    2907             : }
    2908             : 
    2909             : static inline int
    2910     3713524 : exp_key( sql_exp *e )
    2911             : {
    2912     3713524 :         if (e->alias.name)
    2913     3713523 :                 return hash_key(e->alias.name);
    2914             :         return 0;
    2915             : }
    2916             : 
    2917             : sql_exp *
    2918          82 : exps_uses_nid(list *exps, int nid)
    2919             : {
    2920          82 :         if (exps) {
    2921         153 :                 for (node *en = exps->h; en; en = en->next ) {
    2922         147 :                         sql_exp *e = en->data;
    2923             : 
    2924         147 :                         if (e->nid == nid)
    2925          76 :                                 return e;
    2926             :                 }
    2927             :         }
    2928             :         return NULL;
    2929             : }
    2930             : 
    2931             : sql_exp *
    2932    58893703 : exps_bind_nid(list *exps, int nid)
    2933             : {
    2934    58893703 :         if (exps) {
    2935  5358821748 :                 for (node *en = exps->h; en; en = en->next ) {
    2936  5323119242 :                         sql_exp *e = en->data;
    2937             : 
    2938  5323119242 :                         if (e->alias.label == nid)
    2939    23189278 :                                 return e;
    2940  5299929964 :                         if (is_nested(e)) {
    2941       18979 :                                 e = exps_bind_nid(e->f, nid);
    2942       18990 :                                 if (e)
    2943         806 :                                         return e;
    2944             :                         }
    2945             :                 }
    2946             :         }
    2947             :         return NULL;
    2948             : }
    2949             : 
    2950             : sql_exp *
    2951     1303224 : exps_bind_column(list *exps, const char *cname, int *ambiguous, int *multiple, int no_tname)
    2952             : {
    2953     1303224 :         sql_exp *res = NULL;
    2954             : 
    2955     1303224 :         if (exps && cname) {
    2956     1300262 :                 node *en;
    2957             : 
    2958     1300262 :                 if (exps) {
    2959     1300262 :                         if (!exps->ht && list_length(exps) > HASH_MIN_SIZE) {
    2960      112311 :                                 exps->ht = hash_new(exps->sa, list_length(exps), (fkeyvalue)&exp_key);
    2961      112311 :                                 if (exps->ht == NULL)
    2962             :                                         return NULL;
    2963      847007 :                                 for (en = exps->h; en; en = en->next ) {
    2964      734696 :                                         sql_exp *e = en->data;
    2965      734696 :                                         if (e->alias.name) {
    2966      734691 :                                                 int key = exp_key(e);
    2967             : 
    2968      734691 :                                                 if (hash_add(exps->ht, key, e) == NULL)
    2969             :                                                         return NULL;
    2970             :                                         }
    2971             :                                 }
    2972             :                         }
    2973     1300261 :                         if (exps->ht) {
    2974      696011 :                                 int key = hash_key(cname);
    2975      696011 :                                 sql_hash_e *he = exps->ht->buckets[key&(exps->ht->size-1)];
    2976             : 
    2977     1332377 :                                 for (; he; he = he->chain) {
    2978      636371 :                                         sql_exp *e = he->value;
    2979             : 
    2980      636371 :                                         if (e->alias.name && strcmp(e->alias.name, cname) == 0 && (!no_tname || !e->alias.parent)) {
    2981      147500 :                                                 if (res && multiple)
    2982           5 :                                                         *multiple = 1;
    2983      147500 :                                                 if (!res)
    2984      147500 :                                                         res = e;
    2985             : 
    2986      147500 :                                                 if (res && res != e && e->alias.parent && res->alias.parent && !a_match(e->alias.parent, res->alias.parent)) {
    2987           5 :                                                         if (ambiguous)
    2988           5 :                                                                 *ambiguous = 1;
    2989           5 :                                                         return NULL;
    2990             :                                                 }
    2991             :                                                 res = e;
    2992             :                                         }
    2993             :                                 }
    2994      696006 :                                 return res;
    2995             :                         }
    2996             :                 }
    2997     1778566 :                 for (en = exps->h; en; en = en->next ) {
    2998     1174321 :                         sql_exp *e = en->data;
    2999             : 
    3000     1174321 :                         if (e->alias.name && strcmp(e->alias.name, cname) == 0 && (!no_tname || !e->alias.parent)) {
    3001       64570 :                                 if (res && multiple)
    3002           8 :                                         *multiple = 1;
    3003       64570 :                                 if (!res)
    3004       64570 :                                         res = e;
    3005             : 
    3006       64570 :                                 if (res && res != e && e->alias.parent && res->alias.parent && !a_match(e->alias.parent, res->alias.parent)) {
    3007           5 :                                         if (ambiguous)
    3008           5 :                                                 *ambiguous = 1;
    3009           5 :                                         return NULL;
    3010             :                                 }
    3011             :                                 res = e;
    3012             :                         }
    3013             :                 }
    3014             :         }
    3015             :         return res;
    3016             : }
    3017             : 
    3018             : sql_exp *
    3019     1294073 : exps_bind_column2(list *exps, sql_alias *rname, const char *cname, int *multiple)
    3020             : {
    3021     1294073 :         sql_exp *res = NULL;
    3022             : 
    3023     1294073 :         if (exps) {
    3024     1294043 :                 node *en;
    3025             : 
    3026     1294043 :                 if (!exps->ht && list_length(exps) > HASH_MIN_SIZE) {
    3027      132150 :                         exps->ht = hash_new(exps->sa, list_length(exps), (fkeyvalue)&exp_key);
    3028      132150 :                         if (exps->ht == NULL)
    3029             :                                 return res;
    3030             : 
    3031     2241899 :                         for (en = exps->h; en; en = en->next ) {
    3032     2109749 :                                 sql_exp *e = en->data;
    3033     2109749 :                                 if (e->alias.name) {
    3034     2109749 :                                         int key = exp_key(e);
    3035             : 
    3036     2109749 :                                         if (hash_add(exps->ht, key, e) == NULL)
    3037             :                                                 return res;
    3038             :                                 }
    3039             :                         }
    3040             :                 }
    3041     1294044 :                 if (exps->ht) {
    3042      920392 :                         int key = hash_key(cname);
    3043      920392 :                         sql_hash_e *he = exps->ht->buckets[key&(exps->ht->size-1)];
    3044             : 
    3045     2737495 :                         for (; he; he = he->chain) {
    3046     1818381 :                                 sql_exp *e = he->value;
    3047             : 
    3048     1818381 :                                 if (e && e->alias.name && e->alias.parent && strcmp(e->alias.name, cname) == 0 && a_match_obj(e->alias.parent, rname)) {
    3049      660409 :                                         if (res && multiple)
    3050           0 :                                                 *multiple = 1;
    3051      660409 :                                         if (!res)
    3052             :                                                 res = e;
    3053      660409 :                                         if (res && has_label(res)) /* aliases maybe used multiple times without problems */
    3054        1278 :                                                 return res;
    3055             :                                 }
    3056             :                         }
    3057      919114 :                         return res;
    3058             :                 }
    3059     1293018 :                 for (en = exps->h; en; en = en->next ) {
    3060      923226 :                         sql_exp *e = en->data;
    3061             : 
    3062      923226 :                         if (e && e->alias.name && e->alias.parent && strcmp(e->alias.name, cname) == 0 && a_match_obj(e->alias.parent, rname)) {
    3063      275151 :                                 if (res && multiple)
    3064           1 :                                         *multiple = 1;
    3065      275151 :                                 if (!res)
    3066             :                                         res = e;
    3067      275151 :                                 if (res && has_label(res)) /* aliases maybe used multiple times without problems */
    3068        3863 :                                         return res;
    3069             :                         }
    3070             :                 }
    3071             :         }
    3072             :         return res;
    3073             : }
    3074             : 
    3075             : unsigned int
    3076     3281311 : exps_card( list *l )
    3077             : {
    3078     3281311 :         node *n;
    3079     3281311 :         unsigned int card = CARD_ATOM;
    3080             : 
    3081    14857671 :         if (l) for(n = l->h; n; n = n->next) {
    3082    11576360 :                 sql_exp *e = n->data;
    3083             : 
    3084    11576360 :                 if (e && card < e->card)
    3085    11576360 :                         card = e->card;
    3086             :         }
    3087     3281311 :         return card;
    3088             : }
    3089             : 
    3090             : void
    3091       43595 : exps_fix_card( list *exps, unsigned int card)
    3092             : {
    3093       43595 :         if (exps)
    3094     1086002 :                 for (node *n = exps->h; n; n = n->next) {
    3095     1042407 :                 sql_exp *e = n->data;
    3096             : 
    3097     1042407 :                 if (e && e->card > card)
    3098           0 :                         e->card = card;
    3099             :         }
    3100       43595 : }
    3101             : 
    3102             : void
    3103        6191 : exps_setcard( list *exps, unsigned int card)
    3104             : {
    3105        6191 :         if (exps)
    3106       31706 :                 for (node *n = exps->h; n; n = n->next) {
    3107       25515 :                         sql_exp *e = n->data;
    3108             : 
    3109       25515 :                         if (e && e->card != CARD_ATOM)
    3110       25487 :                                 e->card = card;
    3111             :                 }
    3112        6191 : }
    3113             : 
    3114             : int
    3115           0 : exps_intern(list *exps)
    3116             : {
    3117           0 :         if (exps)
    3118           0 :                 for (node *n=exps->h; n; n = n->next) {
    3119           0 :                         sql_exp *e = n->data;
    3120             : 
    3121           0 :                         if (is_intern(e))
    3122             :                                 return 1;
    3123             :                 }
    3124             :         return 0;
    3125             : }
    3126             : 
    3127             : sql_exp *
    3128        4042 : exps_find_one_multi_exp(list *exps)
    3129             : {
    3130        4042 :         sql_exp *l = NULL;
    3131        4042 :         int skip = 0;
    3132             : 
    3133             :         /* Find one and only 1 expression with card > CARD_ATOM */
    3134        4042 :         if (!list_empty(exps)) {
    3135        8097 :                 for (node *m = exps->h ; m && !skip ; m = m->next) {
    3136        4055 :                         sql_exp *e = m->data;
    3137             : 
    3138        4055 :                         if (e->card > CARD_ATOM) {
    3139        4051 :                                 skip |= l != NULL;
    3140        4051 :                                 l = e;
    3141             :                         }
    3142             :                 }
    3143             :         }
    3144        4042 :         if (skip)
    3145           9 :                 l = NULL;
    3146        4042 :         return l;
    3147             : }
    3148             : 
    3149             : const char *
    3150      132339 : compare_func( comp_type t, int anti )
    3151             : {
    3152      132339 :         switch(t) {
    3153       84103 :         case cmp_equal:
    3154       84103 :                 return anti?"<>":"=";
    3155        8215 :         case cmp_lt:
    3156        8215 :                 return anti?">":"<";
    3157        2123 :         case cmp_lte:
    3158        2123 :                 return anti?">=":"<=";
    3159        1184 :         case cmp_gte:
    3160        1184 :                 return anti?"<=":">=";
    3161       32185 :         case cmp_gt:
    3162       32185 :                 return anti?"<":">";
    3163        4529 :         case cmp_notequal:
    3164        4529 :                 return anti?"=":"<>";
    3165             :         default:
    3166             :                 return NULL;
    3167             :         }
    3168             : }
    3169             : 
    3170             : int
    3171     9746387 : is_identity( sql_exp *e, sql_rel *r)
    3172             : {
    3173     9756271 :         switch(e->type) {
    3174       34153 :         case e_column:
    3175       34153 :                 if (r && is_project(r->op) && !is_set(r->op)) {
    3176       12488 :                         sql_exp *re = NULL;
    3177       12488 :                         assert(e->nid);
    3178       12488 :                         re = exps_bind_nid(r->exps, e->nid);
    3179       12488 :                         if (re)
    3180        9884 :                                 return is_identity(re, r->l);
    3181             :                 }
    3182             :                 return 0;
    3183     9715156 :         case e_func: {
    3184     9715156 :                 sql_subfunc *f = e->f;
    3185     9715156 :                 return !f->func->s && strcmp(f->func->base.name, "identity") == 0;
    3186             :         }
    3187             :         default:
    3188             :                 return 0;
    3189             :         }
    3190             : }
    3191             : 
    3192             : list *
    3193          82 : exps_alias(mvc *sql, list *exps)
    3194             : {
    3195          82 :         list *nl = new_exp_list(sql->sa);
    3196             : 
    3197          82 :         if (exps)
    3198         356 :                 for (node *n = exps->h; n; n = n->next) {
    3199         274 :                         sql_exp *e = n->data, *ne;
    3200             : 
    3201         274 :                         assert(exp_name(e));
    3202         274 :                         ne = exp_ref(sql, e);
    3203         274 :                         append(nl, ne);
    3204             :                 }
    3205          82 :         return nl;
    3206             : }
    3207             : 
    3208             : list *
    3209      143225 : exps_copy(mvc *sql, list *exps)
    3210             : {
    3211      143225 :         list *nl;
    3212             : 
    3213      143225 :         if (mvc_highwater(sql))
    3214           0 :                 return sql_error(sql, 10, SQLSTATE(42000) "Query too complex: running out of stack space");
    3215             : 
    3216      143225 :         if (!exps)
    3217             :                 return NULL;
    3218      124805 :         nl = new_exp_list(sql->sa);
    3219      573506 :         for (node *n = exps->h; n; n = n->next) {
    3220      448701 :                 sql_exp *arg = n->data;
    3221             : 
    3222      448701 :                 arg = exp_copy(sql, arg);
    3223      448701 :                 if (!arg)
    3224             :                         return NULL;
    3225      448701 :                 append(nl, arg);
    3226             :         }
    3227             :         return nl;
    3228             : }
    3229             : 
    3230             : sql_exp *
    3231     2633998 : exp_copy(mvc *sql, sql_exp * e)
    3232             : {
    3233     2633998 :         sql_exp *l, *r, *r2, *ne = NULL;
    3234             : 
    3235     2633998 :         if (mvc_highwater(sql))
    3236           0 :                 return sql_error(sql, 10, SQLSTATE(42000) "Query too complex: running out of stack space");
    3237             : 
    3238     2633998 :         if (!e)
    3239             :                 return NULL;
    3240     2633998 :         switch(e->type){
    3241     2233395 :         case e_column:
    3242     2233395 :                 ne = exp_column(sql->sa, e->l, e->r, exp_subtype(e), e->card, has_nil(e), is_unique(e), is_intern(e));
    3243     2233395 :                 ne->flag = e->flag;
    3244     2233395 :                 ne->alias.label = e->alias.label;
    3245     2233395 :                 ne->nid = e->nid;
    3246     2233395 :                 if (e->f)
    3247         125 :                         ne->f = exps_copy(sql, e->f);
    3248             :                 break;
    3249       42295 :         case e_cmp:
    3250       42295 :                 if (e->flag == cmp_or || e->flag == cmp_filter) {
    3251        2618 :                         list *l = exps_copy(sql, e->l);
    3252        2618 :                         list *r = exps_copy(sql, e->r);
    3253             : 
    3254        2618 :                         if (e->flag == cmp_filter)
    3255         731 :                                 ne = exp_filter(sql->sa, l, r, e->f, is_anti(e));
    3256             :                         else
    3257        1887 :                                 ne = exp_or(sql->sa, l, r, is_anti(e));
    3258       39677 :                 } else if (e->flag == cmp_in || e->flag == cmp_notin) {
    3259        1439 :                         sql_exp *l = exp_copy(sql, e->l);
    3260        1439 :                         list *r = exps_copy(sql, e->r);
    3261             : 
    3262        1439 :                         ne = exp_in(sql->sa, l, r, e->flag);
    3263             :                 } else {
    3264       38238 :                         l = exp_copy(sql, e->l);
    3265       38238 :                         r = exp_copy(sql, e->r);
    3266             : 
    3267       38238 :                         if (e->f) {
    3268         635 :                                 r2 = exp_copy(sql, e->f);
    3269         635 :                                 ne = exp_compare2(sql->sa, l, r, r2, e->flag, is_symmetric(e));
    3270             :                         } else {
    3271       37603 :                                 ne = exp_compare(sql->sa, l, r, e->flag);
    3272             :                         }
    3273             :                 }
    3274             :                 break;
    3275       29382 :         case e_convert:
    3276       29382 :                 ne = exp_convert(sql, exp_copy(sql, e->l), exp_fromtype(e), exp_totype(e));
    3277       29382 :                 if (e->f)
    3278           0 :                         ne->f = exps_copy(sql, e->f);
    3279             :                 break;
    3280       13574 :         case e_aggr:
    3281             :         case e_func: {
    3282       13574 :                 list *l = exps_copy(sql, e->l);
    3283             : 
    3284       13574 :                 if (e->type == e_func)
    3285       11800 :                         ne = exp_op(sql->sa, l, e->f);
    3286             :                 else
    3287        1774 :                         ne = exp_aggr(sql->sa, l, e->f, need_distinct(e), need_no_nil(e), e->card, has_nil(e));
    3288       13574 :                 if (e->r) { /* copy obe and gbe lists */
    3289           1 :                         list *er = (list*) e->r;
    3290           1 :                         assert(list_length(er) <= 2);
    3291           1 :                         if (list_length(er) == 2)
    3292           0 :                                 ne->r = list_append(list_append(sa_list(sql->sa), exps_copy(sql, er->h->data)), exps_copy(sql, er->h->next->data));
    3293             :                         else
    3294           1 :                                 ne->r = list_append(sa_list(sql->sa), exps_copy(sql, er->h->data));
    3295             :                 }
    3296             :                 break;
    3297             :         }
    3298      315348 :         case e_atom:
    3299      315348 :                 if (e->l)
    3300      310394 :                         ne = exp_atom(sql->sa, e->l);
    3301        4954 :                 else if (e->r) {
    3302        3634 :                         sql_var_name *vname = (sql_var_name*) e->r;
    3303        3634 :                         ne = exp_param_or_declared(sql->sa, vname->sname, vname->name, &e->tpe, e->flag);
    3304        1320 :                 } else if (e->f)
    3305         771 :                         ne = exp_values(sql->sa, exps_copy(sql, e->f));
    3306             :                 else
    3307         549 :                         ne = exp_atom_ref(sql->sa, e->flag, &e->tpe);
    3308             :                 break;
    3309           4 :         case e_psm:
    3310           4 :                 if (e->flag & PSM_SET) {
    3311           0 :                         ne = exp_set(sql->sa, e->alias.parent->name, e->alias.name, exp_copy(sql, e->l), GET_PSM_LEVEL(e->flag));
    3312           4 :                 } else if (e->flag & PSM_VAR) {
    3313           0 :                         if (e->f)
    3314           0 :                                 ne = exp_table(sql->sa, e->alias.name, e->f, GET_PSM_LEVEL(e->flag));
    3315             :                         else
    3316           0 :                                 ne = exp_var(sql->sa, e->alias.parent->name, e->alias.name, &e->tpe, GET_PSM_LEVEL(e->flag));
    3317           4 :                 } else if (e->flag & PSM_RETURN) {
    3318           0 :                         ne = exp_return(sql->sa, exp_copy(sql, e->l), GET_PSM_LEVEL(e->flag));
    3319           4 :                 } else if (e->flag & PSM_WHILE) {
    3320           0 :                         ne = exp_while(sql->sa, exp_copy(sql, e->l), exps_copy(sql, e->r));
    3321           4 :                 } else if (e->flag & PSM_IF) {
    3322           0 :                         ne = exp_if(sql->sa, exp_copy(sql, e->l), exps_copy(sql, e->r), exps_copy(sql, e->f));
    3323           4 :                 } else if (e->flag & PSM_REL) {
    3324           4 :                         if (!e->alias.label)
    3325           4 :                                 exp_label(sql->sa, e, ++sql->label);
    3326           4 :                         return exp_ref(sql, e);
    3327           0 :                 } else if (e->flag & PSM_EXCEPTION) {
    3328           0 :                         ne = exp_exception(sql->sa, exp_copy(sql, e->l), (const char *) e->r);
    3329             :                 }
    3330             :                 break;
    3331             :         }
    3332      400724 :         if (!ne)
    3333           0 :                 return ne;
    3334     2633994 :         if (e->alias.name)
    3335     2387473 :                 exp_prop_alias(sql->sa, ne, e);
    3336     2633994 :         ne = exp_propagate(sql->sa, ne, e);
    3337     2633994 :         if (is_freevar(e))
    3338        8386 :                 set_freevar(ne, is_freevar(e)-1);
    3339             :         return ne;
    3340             : }
    3341             : 
    3342             : /* scaling for the division operator */
    3343             : static sql_exp *
    3344        2631 : exp_scale_algebra(mvc *sql, sql_subfunc *f, sql_rel *rel, sql_exp *l, sql_exp *r)
    3345             : {
    3346        2631 :         sql_subtype *lt = exp_subtype(l);
    3347        2631 :         sql_subtype *rt = exp_subtype(r);
    3348             : 
    3349        2631 :         if (!EC_INTERVAL(lt->type->eclass) && lt->type->scale == SCALE_FIX &&
    3350        2581 :                 (lt->scale || rt->scale) && strcmp(sql_func_imp(f->func), "/") == 0) {
    3351         170 :                 sql_subtype *res = f->res->h->data;
    3352         170 :                 unsigned int scale, digits, digL, scaleL;
    3353         170 :                 sql_subtype nlt;
    3354             : 
    3355             :                 /* scale fixing may require a larger type ! */
    3356         170 :                 scaleL = (lt->scale < sql->div_min_scale) ? sql->div_min_scale : lt->scale;
    3357         170 :                 scaleL += (scaleL < rt->scale) ? rt->scale - scaleL : 0;
    3358         170 :                 scale = scaleL;
    3359         170 :                 scaleL += rt->scale;
    3360         170 :                 digL = lt->digits + (scaleL - lt->scale);
    3361         170 :                 digits = (digL > rt->digits) ? digL : rt->digits;
    3362             : 
    3363             :                 /* HACK alert: digits should be less than max */
    3364             : #ifdef HAVE_HGE
    3365         170 :                 if (res->type->radix == 10 && digits > 38)
    3366         170 :                         digits = 38;
    3367         170 :                 if (res->type->radix == 2 && digits > 127)
    3368         170 :                         digits = 127;
    3369             : #else
    3370             :                 if (res->type->radix == 10 && digits > 18)
    3371             :                         digits = 18;
    3372             :                 if (res->type->radix == 2 && digits > 63)
    3373             :                         digits = 63;
    3374             : #endif
    3375             : 
    3376         170 :                 sql_find_subtype(&nlt, lt->type->base.name, digL, scaleL);
    3377         170 :                 if (nlt.digits < scaleL)
    3378           2 :                         return sql_error(sql, 01, SQLSTATE(42000) "Scale (%d) overflows type", scaleL);
    3379         168 :                 l = exp_check_type(sql, &nlt, rel, l, type_equal);
    3380             : 
    3381         168 :                 sql_find_subtype(res, lt->type->base.name, digits, scale);
    3382        2461 :         } else if (lt->type->scale == SCALE_FIX) {
    3383        2264 :                 sql_subtype *res = f->res->h->data;
    3384        2264 :                 if (res->type->eclass == EC_NUM)
    3385        2243 :                         res->digits = MAX(lt->digits, rt->digits);
    3386             :         }
    3387             :         return l;
    3388             : }
    3389             : 
    3390             : sql_exp *
    3391        2631 : exps_scale_algebra(mvc *sql, sql_subfunc *f, sql_rel *rel, list *exps)
    3392             : {
    3393        2631 :         if (list_length(exps) != 2)
    3394             :                 return NULL;
    3395        2631 :         sql_exp *e = exp_scale_algebra(sql, f, rel, exps->h->data, exps->h->next->data);
    3396        2631 :         if (e)
    3397        2629 :                 exps->h->data = e;
    3398             :         return e;
    3399             : }
    3400             : 
    3401             : void
    3402      189722 : exps_digits_add(sql_subfunc *f, list *exps)
    3403             : {
    3404             :         /* concat and friends need larger results */
    3405      189722 :         if (!f->func->res)
    3406             :                 return;
    3407      189722 :         int digits = 0;
    3408      337576 :         for(node *n = exps->h; n; n = n->next) {
    3409      276391 :                 sql_subtype *t = exp_subtype(n->data);
    3410             : 
    3411      276391 :                 if (!t->digits) {
    3412             :                         digits = 0;
    3413             :                         break;
    3414             :                 }
    3415      147854 :                 digits += t->digits;
    3416             :         }
    3417      189722 :         sql_subtype *res = f->res->h->data;
    3418      189722 :         res->digits = digits;
    3419             : }
    3420             : 
    3421             : void
    3422       25387 : exps_sum_scales(sql_subfunc *f, list *exps)
    3423             : {
    3424             :         /* sum scales and digits for multiply operation */
    3425       25387 :         sql_arg *ares = f->func->res->h->data;
    3426             : 
    3427       25387 :         if (ares->type.type->scale == SCALE_FIX && strcmp(sql_func_imp(f->func), "*") == 0) {
    3428       24118 :                 unsigned int digits = 0, scale = 0;
    3429       24118 :                 sql_type *largesttype = ares->type.type;
    3430             : 
    3431       72354 :                 for(node *n = exps->h; n; n = n->next) {
    3432       48236 :                         sql_exp *e = n->data;
    3433       48236 :                         sql_subtype *t = exp_subtype(e);
    3434             : 
    3435       48236 :                         scale += t->scale;
    3436       48236 :                         digits += t->digits;
    3437       48236 :                         if (largesttype->localtype < t->type->localtype)
    3438           0 :                                 largesttype = t->type;
    3439             :                 }
    3440       24118 :                 sql_subtype *res = f->res->h->data;
    3441             : 
    3442       24118 :                 res->scale = scale;
    3443       24118 :                 res->digits = digits;
    3444             : 
    3445             :                 /* HACK alert: digits should be less than max */
    3446             : #ifdef HAVE_HGE
    3447       24118 :                 if (ares->type.type->radix == 10 && res->digits > 38) {
    3448        2523 :                         res->digits = 38;
    3449        2523 :                         res->scale = MIN(res->scale, res->digits - 1);
    3450             :                 }
    3451       24118 :                 if (ares->type.type->radix == 2 && res->digits > 127) {
    3452          25 :                         res->digits = 127;
    3453          25 :                         res->scale = MIN(res->scale, res->digits - 1);
    3454             :                 }
    3455             : #else
    3456             :                 if (ares->type.type->radix == 10 && res->digits > 18) {
    3457             :                         res->digits = 18;
    3458             :                         res->scale = MIN(res->scale, res->digits - 1);
    3459             :                 }
    3460             :                 if (ares->type.type->radix == 2 && res->digits > 63) {
    3461             :                         res->digits = 63;
    3462             :                         res->scale = MIN(res->scale, res->digits - 1);
    3463             :                 }
    3464             : #endif
    3465             : 
    3466       24118 :                 sql_subtype t;
    3467             :                 /* numeric types are fixed length */
    3468       24118 :                 if (ares->type.type->eclass == EC_NUM) {
    3469             : #ifdef HAVE_HGE
    3470       21306 :                         if (ares->type.type->localtype == TYPE_hge && res->digits == 127)
    3471          25 :                                 t = *sql_bind_localtype("hge");
    3472             :                         else
    3473             : #endif
    3474       21281 :                         if (ares->type.type->localtype == TYPE_lng && res->digits == 63)
    3475           3 :                                 t = *sql_bind_localtype("lng");
    3476       21278 :                         else if (res->type->digits >= res->digits)
    3477        8855 :                                 t = *res; /* we cannot reduce types! */
    3478             :                         else
    3479       12423 :                                 sql_find_numeric(&t, ares->type.type->localtype, res->digits);
    3480             :                 } else {
    3481        2812 :                         if (res->digits > largesttype->digits)
    3482         216 :                                 sql_find_subtype(&t, largesttype->base.name, res->digits, res->scale);
    3483             :                         else
    3484        2596 :                                 sql_init_subtype(&t, largesttype, res->digits, res->scale);
    3485             :                 }
    3486       24118 :                 *res = t;
    3487             :         }
    3488       25387 : }
    3489             : 
    3490             : void
    3491      114873 : exps_max_bits(sql_subfunc *f, list *exps)
    3492             : {
    3493             :         /* + and - have max_bits + 1 */
    3494      114873 :         if (!f->func->res)
    3495             :                 return;
    3496      114873 :         unsigned int digits = 0;
    3497      344620 :         for(node *n = exps->h; n; n = n->next) {
    3498      229746 :                 sql_subtype *t = exp_subtype(n->data);
    3499             : 
    3500      229747 :                 if (!t)
    3501           0 :                         continue;
    3502      229747 :                 if (digits < t->digits)
    3503      229747 :                         digits = t->digits;
    3504             :         }
    3505             :         /* + and - (because of negative numbers) could need one extra bit (or digit) */
    3506      114874 :         digits += 1;
    3507      114874 :         sql_subtype *res = f->res->h->data;
    3508      114874 :         if (digits > res->type->digits)
    3509       49727 :                 res = sql_find_numeric(res, res->type->localtype, digits);
    3510             :         else
    3511       65147 :                 res->digits = digits;
    3512             : }
    3513             : 
    3514             : void
    3515       19851 : exps_inout(sql_subfunc *f, list *exps)
    3516             : {
    3517             :         /* output == first input */
    3518       19851 :         if (!f->func->res)
    3519             :                 return;
    3520       19851 :         sql_subtype *res = f->res->h->data;
    3521       19851 :         bool is_decimal = (res->type->eclass == EC_DEC);
    3522       19851 :         unsigned int digits = 0, scale = 0;
    3523       19851 :         sql_type *largesttype = NULL;
    3524       19851 :         for(node *n = exps->h; n; n = n->next) {
    3525       19851 :                 sql_subtype *t = exp_subtype(n->data);
    3526             : 
    3527       19851 :                 if (!t)
    3528           0 :                         continue;
    3529             : 
    3530       19851 :                 if (is_decimal && t->type->eclass == EC_DEC && (!largesttype || largesttype->localtype < t->type->localtype))
    3531         399 :                         largesttype = t->type;
    3532         399 :                 if (is_decimal && t->type->eclass == EC_NUM) {
    3533           0 :                         unsigned int d = bits2digits(t->digits);
    3534           0 :                         digits = d>digits?d:digits;
    3535       19851 :                 } else if (digits < t->digits)
    3536             :                         digits = t->digits;
    3537       19851 :                 if (scale < t->scale)
    3538             :                         scale = t->scale;
    3539             :                 break;
    3540             :         }
    3541       19851 :         if (digits > res->digits || scale > res->scale) {
    3542        9574 :                 if (largesttype)
    3543         385 :                         sql_init_subtype(res, largesttype, digits, scale);
    3544             :                 else
    3545        9189 :                         sql_find_subtype(res, res->type->base.name, digits, scale);
    3546             :         } else
    3547       10277 :                 res->digits = digits;
    3548             : }
    3549             : 
    3550             : /* for aggregates we can reduce the result types size based on real digits/bits used number of known input rows */
    3551             : void
    3552        9518 : exps_largest_int(sql_subfunc *f, list *exps, lng cnt)
    3553             : {
    3554        9518 :         if (!f->func->res || cnt == 0)
    3555             :                 return;
    3556         571 :         sql_subtype *res = f->res->h->data;
    3557         571 :         if (res->type->eclass != EC_DEC && res->type->eclass != EC_NUM)
    3558             :                 return;
    3559         495 :         bool is_decimal = (res->type->eclass == EC_DEC);
    3560         495 :         unsigned int digits = 0, scale = 0, mdigits = is_decimal ? decimal_digits(cnt) : number_bits(cnt);
    3561         495 :         sql_type *largesttype = NULL;
    3562         495 :         for(node *n = exps->h; n; n = n->next) {
    3563         495 :                 sql_subtype *t = exp_subtype(n->data);
    3564             : 
    3565         495 :                 if (!t)
    3566           0 :                         continue;
    3567             : 
    3568         495 :                 largesttype = t->type;
    3569         495 :                 if (is_decimal && t->type->eclass == EC_NUM) {
    3570           0 :                         unsigned int d = bits2digits(t->digits);
    3571           0 :                         digits = d>digits?d:digits;
    3572         495 :                 } else if (digits < t->digits)
    3573             :                         digits = t->digits;
    3574         495 :                 if (scale < t->scale)
    3575             :                         scale = t->scale;
    3576             :                 break;
    3577             :         }
    3578         495 :         digits += mdigits;
    3579         495 :         if (largesttype && digits <= largesttype->digits)
    3580         421 :                 sql_init_subtype(res, largesttype, digits, scale);
    3581          74 :         else if (is_decimal)
    3582           8 :                 sql_find_subtype(res, res->type->base.name, digits, scale);
    3583             :         else
    3584          66 :                 sql_find_numeric(res, 1, digits);
    3585             : }
    3586             : 
    3587             : #define is_addition(fname) (strcmp(fname, "sql_add") == 0)
    3588             : #define is_subtraction(fname) (strcmp(fname, "sql_sub") == 0)
    3589             : void
    3590      300040 : exps_scale_fix(sql_subfunc *f, list *exps, sql_subtype *atp)
    3591             : {
    3592      300040 :         if (!f->func->res)
    3593             :                 return;
    3594             : 
    3595      300040 :         sql_subtype *res = f->res->h->data;
    3596      300040 :         if (res->type->eclass != EC_ANY && res->type->eclass != EC_DEC)
    3597             :                 return;
    3598             : 
    3599       62209 :         unsigned int digits = 0, scale = 0;
    3600       62209 :         sql_type *largesttype = NULL;
    3601      245750 :         for(node *n = exps->h; n; n = n->next) {
    3602      183541 :                 sql_subtype *t = exp_subtype(n->data);
    3603             : 
    3604      183541 :                 if (!t)
    3605           0 :                         continue;
    3606      183541 :                 if (digits < t->digits)
    3607             :                         digits = t->digits;
    3608      183541 :                 if (scale < t->scale)
    3609             :                         scale = t->scale;
    3610      183541 :                 if (t->type->eclass == EC_DEC && (!largesttype || largesttype->localtype < t->type->localtype))
    3611      183541 :                         largesttype = t->type;
    3612             :         }
    3613       62209 :         res->scale = scale;
    3614       62209 :         if (res->type->eclass == EC_DEC)
    3615         646 :                 digits += (is_addition(f->func->base.name) || is_subtraction(f->func->base.name));
    3616       62209 :         if (digits > res->type->digits) {
    3617       61887 :                 if (largesttype && largesttype->localtype > res->type->localtype)
    3618          75 :                         sql_init_subtype(res, largesttype, digits, scale);
    3619             :                 else
    3620       61812 :                         sql_find_subtype(res, res->type->localtype?res->type->base.name:atp->type->base.name, digits, scale);
    3621         322 :         } else if (res->type->eclass == EC_DEC || res->type->eclass == EC_NUM)
    3622         252 :                 res->digits = digits;
    3623             : }
    3624             : 
    3625             : int
    3626       97046 : exp_aggr_is_count(sql_exp *e)
    3627             : {
    3628       97046 :         if (e->type == e_aggr && !((sql_subfunc *)e->f)->func->s && strcmp(((sql_subfunc *)e->f)->func->base.name, "count") == 0)
    3629       34522 :                 return 1;
    3630             :         return 0;
    3631             : }
    3632             : 
    3633             : list *
    3634       68684 : check_distinct_exp_names(mvc *sql, list *exps)
    3635             : {
    3636       68684 :         list *distinct_exps = NULL;
    3637       68684 :         bool duplicates = false;
    3638             : 
    3639       68684 :         if (list_length(exps) < 2) {
    3640             :                 return exps; /* always true */
    3641       66776 :         } else if (list_length(exps) < 5) {
    3642       15346 :                 distinct_exps = list_distinct(exps, (fcmp) exp_equal, (fdup) NULL);
    3643             :         } else { /* for longer lists, use hashing */
    3644       51430 :                 sql_hash *ht = hash_new(sql->ta, list_length(exps), (fkeyvalue)&exp_key);
    3645             : 
    3646      552666 :                 for (node *n = exps->h; n && !duplicates; n = n->next) {
    3647      501236 :                         sql_exp *e = n->data;
    3648      501236 :                         int key = ht->key(e);
    3649      501236 :                         sql_hash_e *he = ht->buckets[key&(ht->size-1)];
    3650             : 
    3651      699857 :                         for (; he && !duplicates; he = he->chain) {
    3652      198621 :                                 sql_exp *f = he->value;
    3653             : 
    3654      198621 :                                 if (!exp_equal(e, f))
    3655           0 :                                         duplicates = true;
    3656             :                         }
    3657      501236 :                         hash_add(ht, key, e);
    3658             :                 }
    3659             :         }
    3660       66776 :         if ((distinct_exps && list_length(distinct_exps) != list_length(exps)) || duplicates)
    3661           1 :                 return NULL;
    3662             :         return exps;
    3663             : }
    3664             : 
    3665             : static int rel_find_parameter(mvc *sql, sql_subtype *type, sql_rel *rel, int nid, sql_alias *relname, const char *expname);
    3666             : 
    3667             : static int
    3668        1633 : set_exp_type(mvc *sql, sql_subtype *type, sql_rel *rel, sql_exp *e)
    3669             : {
    3670        1639 :         if (mvc_highwater(sql)) {
    3671           0 :                 (void) sql_error(sql, 10, SQLSTATE(42000) "Query too complex: running out of stack space");
    3672           0 :                 return -1;
    3673             :         }
    3674        1639 :         if (e->type == e_column) {
    3675          60 :                 sql_alias *nrname = e->l;
    3676          60 :                 const char *nename = (const char*) e->r;
    3677             :                 /* find all the column references and set the type */
    3678          60 :                 e->tpe = *type;
    3679          60 :                 assert(e->nid);
    3680          60 :                 return rel_find_parameter(sql, type, rel, e->nid, nrname, nename);
    3681        1579 :         } else if (e->type == e_atom && e->f) {
    3682          25 :                 list *atoms = e->f;
    3683          25 :                 if (!list_empty(atoms))
    3684          61 :                         for (node *n = atoms->h; n; n = n->next)
    3685          36 :                                 if (set_exp_type(sql, type, rel, n->data) < 0) /* set recursively */
    3686             :                                         return -1;
    3687          25 :                 e->tpe = *type;
    3688          25 :                 return 1; /* on a list of atoms, everything should be found */
    3689        1554 :         } else if (e->type == e_atom && !e->l && !e->r && !e->f) {
    3690        1548 :                 e->tpe = *type;
    3691        1548 :                 return set_type_param(sql, type, e->flag) == 0 ? 1 : 0;
    3692           6 :         } else if (exp_is_rel(e)) { /* for relation expressions, restart cycle */
    3693           6 :                 rel = (sql_rel*) e->l;
    3694             :                 /* limiting to these cases */
    3695           6 :                 if (!is_project(rel->op) || list_length(rel->exps) != 1)
    3696           0 :                         return 0;
    3697           6 :                 sql_exp *re = rel->exps->h->data;
    3698             : 
    3699           6 :                 e->tpe = *type;
    3700           6 :                 return set_exp_type(sql, type, rel, re); /* set recursively */
    3701             :         }
    3702             :         return 0;
    3703             : }
    3704             : 
    3705             : int
    3706        1541 : rel_set_type_param(mvc *sql, sql_subtype *type, sql_rel *rel, sql_exp *exp, int upcast)
    3707             : {
    3708        1541 :         if (!type || !exp || (exp->type != e_atom && exp->type != e_column && !exp_is_rel(exp)))
    3709           0 :                 return -1;
    3710             : 
    3711             :         /* use largest numeric types */
    3712        1541 :         if (upcast && type->type->eclass == EC_NUM)
    3713             : #ifdef HAVE_HGE
    3714           9 :                 type = sql_bind_localtype("hge");
    3715             : #else
    3716             :                 type = sql_bind_localtype("lng");
    3717             : #endif
    3718           6 :         else if (upcast && type->type->eclass == EC_FLT)
    3719           1 :                 type = sql_bind_localtype("dbl");
    3720             : 
    3721             :         /* TODO we could use the sql_query* struct to set parameters used as freevars,
    3722             :            but it requires to change a lot of interfaces */
    3723             :         /* if (is_freevar(exp))
    3724             :                 rel = query_fetch_outer(query, is_freevar(exp)-1); */
    3725        1541 :         return set_exp_type(sql, type, rel, exp);
    3726             : }
    3727             : 
    3728             : /* try to do an in-place conversion
    3729             :  *
    3730             :  * in-place conversion is only possible if the exp is a variable.
    3731             :  * This is only done to be able to map more cached queries onto the same
    3732             :  * interface.
    3733             :  */
    3734             : sql_exp *
    3735     5369483 : exp_convert_inplace(mvc *sql, sql_subtype *t, sql_exp *exp)
    3736             : {
    3737     5369483 :         atom *a, *na;
    3738             : 
    3739             :         /* exclude named variables and variable lists */
    3740     5369483 :         if (exp->type != e_atom || exp->r /* named */ || exp->f /* list */ || !exp->l /* not direct atom */)
    3741             :                 return NULL;
    3742             : 
    3743     3099485 :         a = exp->l;
    3744     3099485 :         if (!a->isnull && t->scale && t->type->eclass != EC_FLT)
    3745             :                 return NULL;
    3746             : 
    3747     3094125 :         if ((na = atom_cast(sql->sa, a, t))) {
    3748     3090673 :                 exp->l = na;
    3749     3090673 :                 return exp;
    3750             :         }
    3751             :         return NULL;
    3752             : }
    3753             : 
    3754             : sql_exp *
    3755           0 : exp_numeric_supertype(mvc *sql, sql_exp *e )
    3756             : {
    3757           0 :         sql_subtype *tp = exp_subtype(e);
    3758             : 
    3759           0 :         if (tp->type->eclass == EC_DEC) {
    3760           0 :                 sql_subtype *dtp = sql_bind_localtype("dbl");
    3761             : 
    3762           0 :                 return exp_check_type(sql, dtp, NULL, e, type_cast);
    3763             :         }
    3764           0 :         if (tp->type->eclass == EC_NUM) {
    3765             : #ifdef HAVE_HGE
    3766           0 :                 sql_subtype *ltp = sql_bind_localtype("hge");
    3767             : #else
    3768             :                 sql_subtype *ltp = sql_bind_localtype("lng");
    3769             : #endif
    3770             : 
    3771           0 :                 return exp_check_type(sql, ltp, NULL, e, type_cast);
    3772             :         }
    3773             :         return e;
    3774             : }
    3775             : 
    3776             : static sql_exp *
    3777          69 : exp_check_composite_type(mvc *sql, sql_subtype *t, sql_rel *rel, sql_exp *exp, check_type tpe)
    3778             : {
    3779          69 :         node *n, *m;
    3780          69 :         list *vals = NULL;
    3781          69 :         assert(t->type->composite);
    3782          69 :         if (!exp_is_rel(exp) && is_row(exp) && !is_values(exp))
    3783             :                 return sql_error( sql, 03, SQLSTATE(42000) "cannot convert value into composite type '%s'", t->type->base.name);
    3784             : 
    3785          69 :         if (exp_is_rel(exp)) {
    3786           0 :                 assert(0);
    3787             :                 sql_rel *valr = exp_rel_get_rel(sql->sa, exp);
    3788             :                 if (!valr || !is_project(valr->op) || !valr->exps)
    3789             :                         return sql_error( sql, 03, SQLSTATE(42000) "cannot convert value into composite type '%s'", t->type->base.name);
    3790             :                 vals = valr->exps;
    3791             :         } else { /* attributes */
    3792          69 :                 vals = exp_get_values(exp);
    3793             :         }
    3794         205 :         for(n = t->type->d.fields->h, m = vals->h; n && m; n = n->next, m = m->next) {
    3795         137 :                 sql_arg *f = n->data;
    3796         137 :                 sql_exp *e = m->data;
    3797         137 :                 sql_subtype *ftype = &f->type;
    3798         137 :                 if (!ftype->multiset && ftype->type->composite && list_length(ftype->type->d.fields) == 1) {
    3799           0 :                         sql_arg *f1 = ftype->type->d.fields->h->data;
    3800           0 :                         ftype = &f1->type;
    3801             :                 }
    3802         137 :                 e = exp_check_type(sql, ftype, rel, e, tpe);
    3803         137 :                 if (!e)
    3804             :                         return NULL;
    3805         136 :                 m->data = e;
    3806             :         }
    3807          68 :         if (n || m) {
    3808           0 :                 if (m)
    3809           0 :                         return sql_error( sql, 03, SQLSTATE(42000) "cannot convert values into composite type '%s', too many values given", t->type->base.name);
    3810           0 :                 return sql_error( sql, 03, SQLSTATE(42000) "cannot convert values into composite type '%s', missing values", t->type->base.name);
    3811             :         }
    3812          68 :         if (!exp_is_rel(exp))
    3813          68 :                 exp->tpe = *t;
    3814             :         return exp;
    3815             : }
    3816             : 
    3817             : static sql_exp *
    3818          65 : exp_check_multiset_type(mvc *sql, sql_subtype *t, sql_rel *rel, sql_exp *exp, check_type tpe)
    3819             : {
    3820          65 :         if (t->multiset && exp_is_null(exp))
    3821             :                 return exp;
    3822          65 :         assert(t->type->composite || t->multiset);
    3823          65 :         if (!exp_is_rel(exp) && !is_values(exp)) {
    3824          22 :                 sql_subtype *et = exp_subtype(exp);
    3825             :                 /* hard code conversion from json allowed */
    3826          22 :                 if (strcmp(et->type->base.name, "json") == 0)
    3827           7 :                         return exp_convert(sql, exp, et, t);
    3828          15 :                 if (EC_VARCHAR(et->type->eclass))
    3829          15 :                         return exp_convert(sql, exp, et, t);
    3830           0 :                 if (et && et->multiset == t->multiset && subtype_cmp(et, t) == 0)
    3831             :                         return exp;
    3832           0 :                 if (t->type->composite)
    3833           0 :                         return sql_error( sql, 03, SQLSTATE(42000) "cannot convert value into composite type '%s'", t->type->base.name);
    3834           0 :                 return sql_error( sql, 03, SQLSTATE(42000) "cannot convert value into multiset type '%s[]'", t->type->base.name);
    3835             :         }
    3836             : 
    3837          43 :         list *msvals = NULL;
    3838          43 :         if (exp_is_rel(exp)) {
    3839           0 :                 assert(0);
    3840             :                 sql_rel *valr = exp_rel_get_rel(sql->sa, exp);
    3841             :                 if (!valr || !is_project(valr->op) || !valr->exps)
    3842             :                         return sql_error( sql, 03, SQLSTATE(42000) "cannot convert value into composite type '%s'", t->type->base.name);
    3843             :                 msvals = valr->exps;
    3844             :         } else { /* values within the multiset */
    3845          43 :                 msvals = exp_get_values(exp);
    3846             :         }
    3847          43 :         sql_subtype ct = *t;
    3848          43 :         ct.multiset = false;
    3849             : 
    3850          43 :         if (ct.type->composite && list_length(ct.type->d.fields) == 1) {
    3851           0 :                 sql_arg *f1 = ct.type->d.fields->h->data;
    3852           0 :                 ct = f1->type;
    3853             :         }
    3854         124 :         for(node *v = msvals->h; v; v = v->next) {
    3855          81 :                 sql_exp *r = v->data;
    3856             : 
    3857          81 :                 if (!is_row(r))
    3858          39 :                         if (t->type->composite)
    3859           0 :                                 r = exp_check_multiset_type(sql, &ct, rel, r, tpe);
    3860             :                         else
    3861          39 :                                 r = exp_check_type(sql, &ct, rel, r, tpe);
    3862             :                 else
    3863          42 :                         r = exp_check_composite_type(sql, &ct, rel, r, tpe);
    3864          81 :                 if (!r)
    3865             :                         return r;
    3866          81 :                 v->data = r;
    3867             :         }
    3868          43 :         exp->tpe = *t;
    3869             :         /* keep a list of column names with the conversion, for later name resolving */
    3870          43 :         return exp_convert(sql, exp, &ct, t);
    3871             : }
    3872             : 
    3873             : sql_exp *
    3874           1 : exp_check_multiset(mvc *sql, sql_exp *e)
    3875             : {
    3876           1 :         if (is_values(e)) { /* check for single tuple type */
    3877           1 :                 sql_subtype t = *exp_subtype(e);
    3878           1 :                 t.multiset = MS_ARRAY;
    3879           1 :                 return exp_check_multiset_type(sql, &t, NULL, e, type_equal);
    3880             :         }
    3881           0 :         sql_subtype *st = exp_subtype(e);
    3882           0 :         if (st->multiset)
    3883           0 :                 return e;
    3884             :         return NULL;
    3885             : }
    3886             : 
    3887             : sql_exp *
    3888     5368946 : exp_check_type(mvc *sql, sql_subtype *t, sql_rel *rel, sql_exp *exp, check_type tpe)
    3889             : {
    3890     5368946 :         int c, err = 0;
    3891     5368946 :         sql_exp* nexp = NULL;
    3892     5368946 :         sql_subtype *fromtype = exp_subtype(exp);
    3893             : 
    3894     5369005 :         if ((!fromtype || !fromtype->type) && rel_set_type_param(sql, t, rel, exp, 0) == 0)
    3895             :                 return exp;
    3896             : 
    3897     5369004 :         if (t->type->composite || t->multiset) {
    3898         154 :                 if (fromtype && subtype_cmp(t, fromtype) == 0)
    3899             :                         return exp;
    3900         112 :                 if (t->multiset && !is_row(exp))
    3901          64 :                         return exp_check_multiset_type(sql, t, rel, exp, tpe);
    3902          48 :                 if (t->type->composite && (is_row(exp) || is_values(exp)))
    3903          27 :                         return exp_check_composite_type(sql, t, rel, exp, tpe);
    3904          21 :                 if (strcmp(fromtype->type->base.name, "json") == 0)
    3905          13 :                         return exp_convert(sql, exp, fromtype, t);
    3906           8 :                 if (EC_VARCHAR(fromtype->type->eclass))
    3907           7 :                         return exp_convert(sql, exp, fromtype, t);
    3908           1 :                 if (is_values(exp))
    3909             :                         return NULL;
    3910             :         }
    3911             : 
    3912     5368850 :         if (fromtype && (fromtype->type->composite || fromtype->multiset)) {
    3913           0 :                 if (strcmp(t->type->base.name, "json") == 0)
    3914           0 :                         return exp_convert(sql, exp, fromtype, t);
    3915             :         }
    3916             : 
    3917             :         /* first try cheap internal (in-place) conversions ! */
    3918     5368850 :         if ((nexp = exp_convert_inplace(sql, t, exp)) != NULL)
    3919             :                 return nexp;
    3920             : 
    3921     2278429 :         if (fromtype && subtype_cmp(t, fromtype) != 0) {
    3922      269145 :                 if (EC_INTERVAL(fromtype->type->eclass) && (t->type->eclass == EC_NUM || t->type->eclass == EC_POS) && t->digits < fromtype->digits) {
    3923             :                         err = 1; /* conversion from interval to num depends on the number of digits */
    3924             :                 } else {
    3925      269145 :                         c = sql_type_convert(fromtype->type->eclass, t->type->eclass);
    3926      269145 :                         if (!c || (c == 2 && tpe == type_set) || (c == 3 && tpe != type_cast)) {
    3927             :                                 err = 1;
    3928             :                         } else {
    3929      268631 :                                 exp = exp_convert(sql, exp, fromtype, t);
    3930             :                         }
    3931             :                 }
    3932             :         }
    3933      268631 :         if (err) {
    3934         514 :                 const char *name = (exp->type == e_column && !has_label(exp)) ? exp_name(exp) : "%";
    3935         576 :                 sql_exp *res = sql_error( sql, 03, SQLSTATE(42000) "types %s(%u,%u) and %s(%u,%u) are not equal%s%s%s",
    3936         514 :                         fromtype->type->base.name,
    3937             :                         fromtype->digits,
    3938             :                         fromtype->scale,
    3939         514 :                         t->type->base.name,
    3940             :                         t->digits,
    3941             :                         t->scale,
    3942             :                         (name[0] != '%' ? " for column '" : ""),
    3943             :                         (name[0] != '%' ? name : ""),
    3944         514 :                         (name[0] != '%' ? "'" : "")
    3945             :                 );
    3946         514 :                 return res;
    3947             :         }
    3948             :         return exp;
    3949             : }
    3950             : 
    3951             : sql_exp *
    3952        8600 : exp_values_set_supertype(mvc *sql, sql_exp *values, sql_subtype *opt_super)
    3953             : {
    3954        8600 :         assert(is_values(values));
    3955        8600 :         list *vals = exp_get_values(values), *nexps;
    3956        8600 :         sql_subtype *tpe = opt_super?opt_super:exp_subtype(vals->h->data);
    3957             : 
    3958        8600 :         if (!opt_super && tpe)
    3959        8500 :                 values->tpe = *tpe;
    3960             : 
    3961       34263 :         for (node *m = vals->h; m; m = m->next) {
    3962       25663 :                 sql_exp *e = m->data;
    3963       25663 :                 sql_subtype super, *ttpe;
    3964             : 
    3965             :                 /* if the expression is a parameter set its type */
    3966       25663 :                 if (tpe && e->type == e_atom && !e->l && !e->r && !e->f && !e->tpe.type) {
    3967           4 :                         if (set_type_param(sql, tpe, e->flag) == 0)
    3968           4 :                                 e->tpe = *tpe;
    3969             :                         else
    3970           0 :                                 return NULL;
    3971             :                 }
    3972       25663 :                 ttpe = exp_subtype(e);
    3973       25663 :                 if (tpe && ttpe) {
    3974       25610 :                         supertype(&super, ttpe, tpe);
    3975       25610 :                         values->tpe = super;
    3976       25610 :                         tpe = &values->tpe;
    3977             :                 } else {
    3978             :                         tpe = ttpe;
    3979             :                 }
    3980             :         }
    3981             : 
    3982        8600 :         if (tpe) {
    3983             :                 /* if the expression is a parameter set its type */
    3984       34176 :                 for (node *m = vals->h; m; m = m->next) {
    3985       25614 :                         sql_exp *e = m->data;
    3986       25614 :                         if (e->type == e_atom && !e->l && !e->r && !e->f && !e->tpe.type) {
    3987           1 :                                 if (set_type_param(sql, tpe, e->flag) == 0)
    3988           1 :                                         e->tpe = *tpe;
    3989             :                                 else
    3990             :                                         return NULL;
    3991             :                         }
    3992             :                 }
    3993        8562 :                 values->tpe = *tpe;
    3994        8562 :                 nexps = sa_list(sql->sa);
    3995       34171 :                 for (node *m = vals->h; m; m = m->next) {
    3996       25612 :                         sql_exp *e = m->data;
    3997       25612 :                         e = exp_check_type(sql, &values->tpe, NULL, e, type_equal);
    3998       25612 :                         if (!e)
    3999             :                                 return NULL;
    4000       25609 :                         exp_label(sql->sa, e, ++sql->label);
    4001       25609 :                         append(nexps, e);
    4002             :                 }
    4003        8559 :                 values->f = nexps;
    4004             :         }
    4005             :         return values;
    4006             : }
    4007             : 
    4008             : /* return -1 on error, 0 not found, 1 found */
    4009             : static int
    4010          86 : rel_find_parameter(mvc *sql, sql_subtype *type, sql_rel *rel, int nid, sql_alias *relname, const char *expname)
    4011             : {
    4012         138 :         int res = 0;
    4013             : 
    4014         138 :         if (mvc_highwater(sql)) {
    4015           0 :                 (void) sql_error(sql, 10, SQLSTATE(42000) "Query too complex: running out of stack space");
    4016           0 :                 return -1;
    4017             :         }
    4018         138 :         if (!rel)
    4019             :                 return 0;
    4020             : 
    4021         135 :         sql_alias *nrname = relname;
    4022         135 :     const char *nename = expname;
    4023         135 :         if (is_project(rel->op) && !list_empty(rel->exps)) {
    4024         111 :                 sql_exp *e = NULL;
    4025             : 
    4026         111 :                 assert(nid);
    4027         111 :                 e = exps_bind_nid(rel->exps, nid);
    4028         111 :                 if (!e)
    4029             :                         return 0; /* not found */
    4030         108 :                 if (is_mset(rel->op)) { /* TODO for set relations this needs further improvement */
    4031           0 :                         (void) sql_error(sql, 10, SQLSTATE(42000) "Cannot set parameter types under set relations at the moment");
    4032           0 :                         return -1;
    4033             :                 }
    4034             :                 /* set order by column types */
    4035         108 :                 if (is_simple_project(rel->op) && !list_empty(rel->r)) {
    4036           0 :                         sql_exp *ordere = NULL;
    4037           0 :                         ordere = exps_bind_nid(rel->r, nid);
    4038           0 :                         if (ordere && ordere->type == e_column)
    4039           0 :                                 ordere->tpe = *type;
    4040             :                 }
    4041         108 :                 if (e->type == e_column) {
    4042          52 :                         nid = e->nid;
    4043          52 :                         nrname = e->l;
    4044          52 :                         nename = (const char*) e->r;
    4045          52 :                         e->tpe = *type;
    4046          52 :                         res = 1; /* found */
    4047          56 :                 } else if ((e->type == e_atom || exp_is_rel(e)) && (res = set_exp_type(sql, type, rel, e)) <= 0) {
    4048             :                         return res; /* don't search further */
    4049             :                 }
    4050             :                 /* group by columns can have aliases! */
    4051         108 :                 if (is_groupby(rel->op) && !list_empty(rel->r)) {
    4052           0 :                         e = exps_bind_nid(rel->r, nid);
    4053           0 :                         if (!e)
    4054             :                                 return res; /* don't search further */
    4055           0 :                         if (e->type == e_column) {
    4056           0 :                                 nid = e->nid;
    4057           0 :                                 nrname = e->l;
    4058           0 :                                 nename = (const char*) e->r;
    4059           0 :                                 e->tpe = *type;
    4060           0 :                         } else if ((e->type == e_atom || exp_is_rel(e)) && (res = set_exp_type(sql, type, rel, e)) <= 0) {
    4061             :                                 return res; /* don't search further */
    4062             :                         }
    4063             :                 }
    4064         108 :                 if (e->type != e_column)
    4065             :                         return res; /* don't search further */
    4066             :         }
    4067             : 
    4068          76 :         switch (rel->op) {
    4069          14 :                 case op_join:
    4070             :                 case op_left:
    4071             :                 case op_right:
    4072             :                 case op_full:
    4073          14 :                         if (rel->l)
    4074          14 :                                 res = rel_find_parameter(sql, type, rel->l, nid, nrname, nename);
    4075          14 :                         if (rel->r && res <= 0) { /* try other relation if not found */
    4076          12 :                                 int err = sql->session->status, lres = res;
    4077          12 :                                 char buf[ERRSIZE];
    4078             : 
    4079          12 :                                 strcpy(buf, sql->errstr); /* keep error found and try other join relation */
    4080          12 :                                 sql->session->status = 0;
    4081          12 :                                 sql->errstr[0] = '\0';
    4082          12 :                                 res = rel_find_parameter(sql, type, rel->r, nid, nrname, nename);
    4083          12 :                                 if (res == 0) { /* parameter wasn't found, set error */
    4084           1 :                                         res = lres;
    4085           1 :                                         sql->session->status = err;
    4086           1 :                                         strcpy(sql->errstr, buf);
    4087             :                                 }
    4088             :                         }
    4089             :                         break;
    4090          52 :                 case op_semi:
    4091             :                 case op_anti:
    4092             :                 case op_groupby:
    4093             :                 case op_project:
    4094             :                 case op_select:
    4095             :                 case op_topn:
    4096             :                 case op_sample:
    4097          52 :                         if (rel->l)
    4098             :                                 res = rel_find_parameter(sql, type, rel->l, nid, nrname, nename);
    4099             :                         break;
    4100           0 :                 case op_union: /* TODO for set relations this needs further improvement */
    4101             :                 case op_inter:
    4102             :                 case op_except:
    4103             :                 case op_munion: {
    4104           0 :                         (void) sql_error(sql, 10, SQLSTATE(42000) "Cannot set parameter types under set relations at the moment");
    4105           0 :                         return -1;
    4106             :                 }
    4107             :                 default: /* For table returning functions, the type must be set when the relation is created */
    4108             :                         return 0;
    4109             :         }
    4110             :         return res;
    4111             : }
    4112             : 
    4113             : sql_exp *
    4114       19700 : list_find_exp( list *exps, sql_exp *e)
    4115             : {
    4116       19700 :         if (e->type != e_column)
    4117             :                 return NULL;
    4118       19659 :         return exps_bind_nid(exps, e->nid);
    4119             : }
    4120             : 
    4121             : sql_alias *
    4122     5434318 : a_create(allocator *sa, const char *name)
    4123             : {
    4124     5434318 :         sql_alias *a = SA_NEW(sa, sql_alias);
    4125             : 
    4126     5434516 :         a->label = 0;
    4127     5434516 :         a->name = name;
    4128     5434516 :         a->parent = NULL;
    4129     5434516 :         return a;
    4130             : }
    4131             : 
    4132             : sql_alias *
    4133      386246 : schema_alias(allocator *sa, sql_schema *s)
    4134             : {
    4135      386246 :         if (!s)
    4136             :                 return NULL;
    4137      386050 :         sql_alias *a = SA_NEW(sa, sql_alias);
    4138             : 
    4139      386123 :         a->label = 0;
    4140      386123 :         a->name = s->base.name;
    4141      386123 :         a->parent = NULL;
    4142      386123 :         return a;
    4143             : }
    4144             : 
    4145             : sql_alias *
    4146      592616 : table_alias(allocator *sa, sql_table *t, sql_alias *p)
    4147             : {
    4148      592616 :         sql_alias *a = SA_NEW(sa, sql_alias);
    4149             : 
    4150      592750 :         a->label = 0;
    4151      592750 :         a->name = t->base.name;
    4152      592750 :         a->parent = p;
    4153      592750 :         return a;
    4154             : }
    4155             : 
    4156             : bool
    4157      761166 : a_cmp_obj_name(sql_alias *n, const char *name)
    4158             : {
    4159      761166 :         if (n->name)
    4160      761165 :                 return strcmp(n->name, name) == 0;
    4161             :         return false;
    4162             : }
    4163             : 
    4164             : bool
    4165     3977514 : a_cmp_obj_names(sql_alias *n, sql_alias *n2)
    4166             : {
    4167     3977514 :         if (n->name && n2->name)
    4168     3977514 :                 return strcmp(n->name, n2->name) == 0;
    4169             :         return false;
    4170             : }
    4171             : 
    4172             : bool
    4173      905957 : a_match(sql_alias *l, sql_alias *r)
    4174             : {
    4175      951254 :         if (!l && !r)
    4176             :                 return true;
    4177      915195 :         if (!l || !r)
    4178             :                 return false;
    4179      915191 :         if (a_cmp_obj_names(l, r))
    4180       45297 :                 return a_match(l->parent, r->parent);
    4181             :         return false;
    4182             : }
    4183             : 
    4184             : bool
    4185      795562 : a_cmp(sql_alias *l, sql_alias *r)
    4186             : {
    4187      795562 :         return !a_match(l, r);
    4188             : }
    4189             : 
    4190             : /* match as long as we have names */
    4191             : bool
    4192     2853851 : a_match_obj(sql_alias *l, sql_alias *r)
    4193             : {
    4194     3062269 :         if (!l && !r)
    4195             :                 return true;
    4196     3062269 :         if (!l || !r)
    4197             :                 return false;
    4198     3062269 :         if (a_cmp_obj_names(l, r)) {
    4199     1838184 :                 if (!r->parent)
    4200             :                         return true;
    4201      208418 :                 return a_match_obj(l->parent, r->parent);
    4202             :         }
    4203             :         return false;
    4204             : }

Generated by: LCOV version 1.14