LCOV - code coverage report
Current view: top level - sql/server - rel_optimize_others.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 634 769 82.4 %
Date: 2024-10-03 20:03:20 Functions: 27 29 93.1 %

          Line data    Source code
       1             : /*
       2             :  * SPDX-License-Identifier: MPL-2.0
       3             :  *
       4             :  * This Source Code Form is subject to the terms of the Mozilla Public
       5             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       6             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       7             :  *
       8             :  * Copyright 2024 MonetDB Foundation;
       9             :  * Copyright August 2008 - 2023 MonetDB B.V.;
      10             :  * Copyright 1997 - July 2008 CWI.
      11             :  */
      12             : 
      13             : #include "monetdb_config.h"
      14             : #include "rel_optimizer.h"
      15             : #include "rel_optimizer_private.h"
      16             : #include "rel_exp.h"
      17             : #include "rel_select.h"
      18             : 
      19             : static void
      20           0 : rel_no_rename_exps( list *exps )
      21             : {
      22           0 :         for (node *n = exps->h; n; n = n->next) {
      23           0 :                 sql_exp *e = n->data;
      24             : 
      25           0 :                 exp_setalias(e, e->alias.label, e->l, e->r);
      26             :         }
      27           0 :         list_hash_clear(exps);
      28           0 : }
      29             : 
      30             : /* positional renames ! */
      31             : /* get the names (aka aliases) from the src_exps list and rename the dest_exps list */
      32             : void
      33       80015 : rel_rename_exps( mvc *sql, list *src_exps, list *dest_exps)
      34             : {
      35       80015 :         node *n, *m;
      36             : 
      37       80015 :         (void)sql;
      38             :         /* check if a column uses an alias earlier in the list */
      39             : 
      40       80015 :         assert(list_length(src_exps) <= list_length(dest_exps));
      41      671161 :         for (n = src_exps->h, m = dest_exps->h; n && m; n = n->next, m = m->next) {
      42      591146 :                 sql_exp *s = n->data;
      43      591146 :                 sql_exp *d = m->data;
      44      591146 :                 const char *rname = exp_relname(s);
      45             : 
      46      591146 :                 exp_setalias(d, s->alias.label, rname, exp_name(s));
      47             :         }
      48       80015 :         list_hash_clear(dest_exps);
      49       80015 : }
      50             : 
      51             : sql_rel *
      52      169015 : rel_find_ref( sql_rel *r)
      53             : {
      54      515373 :         while (!rel_is_ref(r) && r->l &&
      55      424513 :               (is_project(r->op) || is_select(r->op) /*|| is_join(r->op)*/))
      56             :                 r = r->l;
      57      169015 :         if (rel_is_ref(r))
      58       28257 :                 return r;
      59             :         return NULL;
      60             : }
      61             : 
      62             : /* merge projection */
      63             : 
      64             : /* push an expression through a projection.
      65             :  * The result should again be used in a projection.
      66             :  */
      67             : static list *
      68       56484 : exps_push_down_prj(mvc *sql, list *exps, sql_rel *f, sql_rel *t, bool keepalias)
      69             : {
      70       56484 :         node *n;
      71       56484 :         list *nl = new_exp_list(sql->sa);
      72             : 
      73      193342 :         for(n = exps->h; n; n = n->next) {
      74      139159 :                 sql_exp *arg = n->data, *narg = NULL;
      75             : 
      76      139159 :                 narg = exp_push_down_prj(sql, arg, f, t);
      77      139159 :                 if (!narg)
      78             :                         return NULL;
      79      136858 :                 narg = exp_propagate(sql->sa, narg, arg);
      80      136858 :                 if (!keepalias && narg->type == e_column)
      81       39685 :                         exp_setalias(narg, arg->alias.label, narg->l, narg->r);
      82      136858 :                 append(nl, narg);
      83             :         }
      84             :         return nl;
      85             : }
      86             : 
      87             : sql_exp *
      88     2657619 : exp_push_down_prj(mvc *sql, sql_exp *e, sql_rel *f, sql_rel *t)
      89             : {
      90     2657619 :         sql_exp *ne = NULL, *l = NULL, *r = NULL, *r2 = NULL;
      91             : 
      92     2657619 :         assert(is_project(f->op));
      93             : 
      94     2657619 :         switch(e->type) {
      95     2237240 :         case e_column:
      96     2237240 :                 assert(e->nid);
      97     2237240 :                 ne = exps_bind_nid(f->exps, e->nid);
      98     2237240 :                 if (!ne || (ne->type != e_column && (ne->type != e_atom || ne->f)))
      99             :                         return NULL;
     100     1909911 :                 while (ne && (has_label(ne) || is_selfref(ne) /*inside this list */) && is_simple_project(f->op) && ne->type == e_column) {
     101       11873 :                         sql_exp *oe = e, *one = ne;
     102             : 
     103       11873 :                         e = ne;
     104       11873 :                         ne = NULL;
     105       11873 :                         if (e->nid)
     106       11873 :                                 ne = exps_bind_nid(f->exps, e->nid);
     107       11873 :                         if (ne && ne != one && list_position(f->exps, ne) >= list_position(f->exps, one))
     108       11873 :                                 ne = NULL;
     109       11873 :                         if (!ne || ne == one) {
     110             :                                 ne = one;
     111     1909512 :                                 e = oe;
     112             :                                 break;
     113             :                         }
     114         399 :                         if (ne->type != e_column && (ne->type != e_atom || ne->f))
     115             :                                 return NULL;
     116             :                 }
     117             :                 /* possibly a groupby/project column is renamed */
     118     1909512 :                 if (is_groupby(f->op) && !list_empty(f->r) && ne->type == e_column) {
     119         130 :                         sql_exp *gbe = NULL;
     120         130 :                         if (ne->nid)
     121         130 :                                 gbe = exps_bind_nid(f->exps, ne->nid);
     122         130 :                         ne = gbe;
     123         130 :                         if (!ne || (ne->type != e_column && (ne->type != e_atom || ne->f)))
     124           0 :                                 return NULL;
     125             :                 }
     126     1909512 :                 return exp_copy(sql, ne);
     127       60270 :         case e_cmp:
     128       60270 :                 if (e->flag == cmp_or || e->flag == cmp_filter) {
     129        4108 :                         list *l = NULL, *r = NULL;
     130             : 
     131        4108 :                         if (!(l = exps_push_down_prj(sql, e->l, f, t, true)) || !(r = exps_push_down_prj(sql, e->r, f, t, true)))
     132        1154 :                                 return NULL;
     133        2954 :                         if (e->flag == cmp_filter) {
     134         981 :                                 ne = exp_filter(sql->sa, l, r, e->f, is_anti(e));
     135             :                         } else {
     136        1973 :                                 ne = exp_or(sql->sa, l, r, is_anti(e));
     137             :                         }
     138       56162 :                 } else if (e->flag == cmp_in || e->flag == cmp_notin) {
     139        5694 :                         list *r = NULL;
     140             : 
     141        5694 :                         if (!(l = exp_push_down_prj(sql, e->l, f, t)) || !(r = exps_push_down_prj(sql, e->r, f, t, true)))
     142        5203 :                                 return NULL;
     143         491 :                         ne = exp_in(sql->sa, l, r, e->flag);
     144             :                 } else {
     145       50468 :                         if (!(l = exp_push_down_prj(sql, e->l, f, t)) || !(r = exp_push_down_prj(sql, e->r, f, t)) || (e->f && !(r2 = exp_push_down_prj(sql, e->f, f, t))))
     146       24263 :                                 return NULL;
     147       26205 :                         if (e->f) {
     148         741 :                                 ne = exp_compare2(sql->sa, l, r, r2, e->flag, is_symmetric(e));
     149             :                         } else {
     150       25464 :                                 ne = exp_compare(sql->sa, l, r, e->flag);
     151             :                         }
     152             :                 }
     153       29650 :                 if (!ne)
     154             :                         return NULL;
     155       29650 :                 return exp_propagate(sql->sa, ne, e);
     156      211421 :         case e_convert:
     157      211421 :                 if (!(l = exp_push_down_prj(sql, e->l, f, t)))
     158             :                         return NULL;
     159      143760 :                 ne = exp_convert(sql, l, exp_fromtype(e), exp_totype(e));
     160      143760 :                 return exp_propagate(sql->sa, ne, e);
     161       48793 :         case e_aggr:
     162             :         case e_func: {
     163       48793 :                 list *l = e->l, *nl = NULL;
     164       48793 :                 sql_exp *ne = NULL;
     165             : 
     166       48793 :                 if (e->type == e_func && exp_unsafe(e, false, false))
     167             :                         return NULL;
     168       48777 :                 if (!list_empty(l)) {
     169       48777 :                         nl = exps_push_down_prj(sql, l, f, t, false);
     170       48777 :                         if (!nl)
     171             :                                 return NULL;
     172             :                 }
     173       47630 :                 if (e->type == e_func)
     174       47630 :                         ne = exp_op(sql->sa, nl, e->f);
     175             :                 else
     176           0 :                         ne = exp_aggr(sql->sa, nl, e->f, need_distinct(e), need_no_nil(e), e->card, has_nil(e));
     177       47630 :                 return exp_propagate(sql->sa, ne, e);
     178             :         }
     179       99895 :         case e_atom: {
     180       99895 :                 list *l = e->f, *nl = NULL;
     181             : 
     182       99895 :                 if (!list_empty(l)) {
     183           0 :                         nl = exps_push_down_prj(sql, l, f, t, false);
     184           0 :                         if (!nl)
     185             :                                 return NULL;
     186           0 :                         ne = exp_values(sql->sa, nl);
     187             :                 } else {
     188       99895 :                         ne = exp_copy(sql, e);
     189             :                 }
     190       99895 :                 return exp_propagate(sql->sa, ne, e);
     191             :         }
     192             :         case e_psm:
     193             :                 if (e->type == e_atom && e->f) /* value list */
     194             :                         return NULL;
     195             :                 return e;
     196             :         }
     197             :         return NULL;
     198             : }
     199             : 
     200             : atom *
     201         636 : exp_flatten(mvc *sql, bool value_based_opt, sql_exp *e)
     202             : {
     203         636 :         if (e->type == e_atom) {
     204         379 :                 return value_based_opt ? exp_value(sql, e) : (atom *) e->l;
     205         257 :         } else if (e->type == e_convert) {
     206          59 :                 atom *v = exp_flatten(sql, value_based_opt, e->l);
     207             : 
     208          59 :                 if (v)
     209          32 :                         return atom_cast(sql->sa, v, exp_subtype(e));
     210         198 :         } else if (e->type == e_func) {
     211         198 :                 sql_subfunc *f = e->f;
     212         198 :                 list *l = e->l;
     213         198 :                 sql_arg *res = (f->func->res)?(f->func->res->h->data):NULL;
     214             : 
     215             :                 /* TODO handle date + x months */
     216         198 :                 if (!f->func->s && strcmp(f->func->base.name, "sql_add") == 0 && list_length(l) == 2 && res && EC_NUMBER(res->type.type->eclass)) {
     217          24 :                         atom *l1 = exp_flatten(sql, value_based_opt, l->h->data);
     218          24 :                         atom *l2 = exp_flatten(sql, value_based_opt, l->h->next->data);
     219          24 :                         if (l1 && l2)
     220           3 :                                 return atom_add(sql->sa, l1, l2);
     221         174 :                 } else if (!f->func->s && strcmp(f->func->base.name, "sql_sub") == 0 && list_length(l) == 2 && res && EC_NUMBER(res->type.type->eclass)) {
     222          10 :                         atom *l1 = exp_flatten(sql, value_based_opt, l->h->data);
     223          10 :                         atom *l2 = exp_flatten(sql, value_based_opt, l->h->next->data);
     224          10 :                         if (l1 && l2)
     225           9 :                                 return atom_sub(sql->sa, l1, l2);
     226             :                 }
     227             :         }
     228             :         return NULL;
     229             : }
     230             : 
     231             : int
     232         150 : exp_range_overlap(atom *min, atom *max, atom *emin, atom *emax, bool min_exclusive, bool max_exclusive)
     233             : {
     234         150 :         if (!min || !max || !emin || !emax || min->isnull || max->isnull || emin->isnull || emax->isnull)
     235             :                 return 0;
     236             : 
     237         150 :         if ((!min_exclusive && VALcmp(&(emax->data), &(min->data)) < 0) || (min_exclusive && VALcmp(&(emax->data), &(min->data)) <= 0))
     238          29 :                 return 0;
     239         121 :         if ((!max_exclusive && VALcmp(&(emin->data), &(max->data)) > 0) || (max_exclusive && VALcmp(&(emin->data), &(max->data)) >= 0))
     240          34 :                 return 0;
     241             :         return 1;
     242             : }
     243             : 
     244             : 
     245             : /* if local_proj is >= -1, the current expression is from the same projection
     246             :    if local_proj is -1, then we don't care about self references (eg used to check for order by exps) */
     247             : static int exp_mark_used(sql_rel *subrel, sql_exp *e, int local_proj);
     248             : 
     249             : static int
     250      800605 : exps_mark_used(sql_rel *subrel, list *l, int local_proj)
     251             : {
     252      800605 :         int nr = 0;
     253      800605 :         if (list_empty(l))
     254             :                 return nr;
     255             : 
     256     2584012 :         for (node *n = l->h; n != NULL; n = n->next)
     257     1783471 :                 nr += exp_mark_used(subrel, n->data, local_proj);
     258             :         return nr;
     259             : }
     260             : 
     261             : static int
     262     6900648 : exp_mark_used(sql_rel *subrel, sql_exp *e, int local_proj)
     263             : {
     264     7110634 :         int nr = 0;
     265     7110634 :         sql_exp *ne = NULL;
     266             : 
     267     7110634 :         switch(e->type) {
     268     4700532 :         case e_column:
     269     4700532 :                 ne = rel_find_exp(subrel, e);
     270             :                 /* if looking in the same projection, make sure 'ne' is projected before the searched column */
     271     4700559 :                 if (ne && local_proj > -1 && list_position(subrel->exps, ne) >= local_proj)
     272     5905893 :                         ne = NULL;
     273             :                 break;
     274      209986 :         case e_convert:
     275      209986 :                 return exp_mark_used(subrel, e->l, local_proj);
     276      762015 :         case e_aggr:
     277             :         case e_func: {
     278      762015 :                 if (e->l)
     279      729920 :                         nr += exps_mark_used(subrel, e->l, local_proj);
     280      762015 :                 assert(!e->r);
     281             :                 break;
     282             :         }
     283      443318 :         case e_cmp:
     284      443318 :                 if (e->flag == cmp_or || e->flag == cmp_filter) {
     285       22566 :                         nr += exps_mark_used(subrel, e->l, local_proj);
     286       22566 :                         nr += exps_mark_used(subrel, e->r, local_proj);
     287      420752 :                 } else if (e->flag == cmp_in || e->flag == cmp_notin) {
     288       20503 :                         nr += exp_mark_used(subrel, e->l, local_proj);
     289       20503 :                         nr += exps_mark_used(subrel, e->r, local_proj);
     290             :                 } else {
     291      400249 :                         nr += exp_mark_used(subrel, e->l, local_proj);
     292      400249 :                         nr += exp_mark_used(subrel, e->r, local_proj);
     293      400249 :                         if (e->f)
     294        6424 :                                 nr += exp_mark_used(subrel, e->f, local_proj);
     295             :                 }
     296             :                 break;
     297      994783 :         case e_atom:
     298             :                 /* atoms are used in e_cmp */
     299      994783 :                 e->used = 1;
     300             :                 /* return 0 as constants may require a full column ! */
     301      994783 :                 if (e->f)
     302        5050 :                         nr += exps_mark_used(subrel, e->f, local_proj);
     303             :                 return nr;
     304           0 :         case e_psm:
     305           0 :                 if (e->flag & PSM_SET || e->flag & PSM_RETURN || e->flag & PSM_EXCEPTION) {
     306           0 :                         nr += exp_mark_used(subrel, e->l, local_proj);
     307           0 :                 } else if (e->flag & PSM_WHILE || e->flag & PSM_IF) {
     308           0 :                         nr += exp_mark_used(subrel, e->l, local_proj);
     309           0 :                         nr += exps_mark_used(subrel, e->r, local_proj);
     310           0 :                         if (e->flag == PSM_IF && e->f)
     311           0 :                                 nr += exps_mark_used(subrel, e->f, local_proj);
     312             :                 }
     313           0 :                 e->used = 1;
     314           0 :                 break;
     315             :         }
     316     5905893 :         if (ne && e != ne) {
     317     2563754 :                 if (local_proj == -2 || ne->type != e_column || (has_label(ne) || (ne->alias.rname && ne->alias.rname[0] == '%')) || (subrel->l && !is_munion(subrel->op) && !rel_find_exp(subrel->l, e)))
     318     2440293 :                         ne->used = 1;
     319     2563754 :                 return ne->used;
     320             :         }
     321             :         return nr;
     322             : }
     323             : 
     324             : static void
     325       44069 : positional_exps_mark_used( sql_rel *rel, sql_rel *subrel )
     326             : {
     327       44069 :         assert(rel->exps);
     328             : 
     329       44069 :         if ((is_topn(subrel->op) || is_sample(subrel->op)) && subrel->l)
     330       44069 :                 subrel = subrel->l;
     331             :         /* everything is used within the set operation */
     332       44069 :         if (rel->exps && subrel->exps) {
     333       44069 :                 node *m;
     334      372757 :                 for (m=subrel->exps->h; m; m = m->next) {
     335      328688 :                         sql_exp *se = m->data;
     336             : 
     337      328688 :                         se->used = 1;
     338             :                 }
     339             :         }
     340       44069 : }
     341             : 
     342             : static void
     343      763429 : rel_exps_mark_used(allocator *sa, sql_rel *rel, sql_rel *subrel)
     344             : {
     345      763429 :         int nr = 0;
     346             : 
     347      763429 :         if (rel->r && (is_simple_project(rel->op) || is_groupby(rel->op))) {
     348       32249 :                 list *l = rel->r;
     349       32249 :                 node *n;
     350             : 
     351      110989 :                 for (n=l->h; n; n = n->next) {
     352       78739 :                         sql_exp *e = n->data;
     353             : 
     354       78739 :                         e->used = 1;
     355       78739 :                         exp_mark_used(rel, e, -1);
     356             :                 }
     357             :         }
     358      763430 :         if (rel->attr) {
     359       26179 :                 for (node *n = rel->attr->h; n; n = n->next) {
     360       13090 :                         sql_exp *e = n->data;
     361             : 
     362       13090 :                         if (e->type != e_aggr) /* keep all group by's */
     363       13090 :                                 e->used = 1;
     364       13090 :                         if (e->used)
     365       13090 :                                 nr += exp_mark_used(subrel, e, -2);
     366             :                 }
     367             :         }
     368      763429 :         if (rel->exps) {
     369      724720 :                 node *n;
     370      724720 :                 int len = list_length(rel->exps), i;
     371      724720 :                 sql_exp **exps = SA_NEW_ARRAY(sa, sql_exp*, len);
     372             : 
     373     3563140 :                 for (n=rel->exps->h, i = 0; n; n = n->next, i++) {
     374     2838420 :                         sql_exp *e = exps[i] = n->data;
     375             : 
     376     2838420 :                         nr += e->used;
     377             :                 }
     378             : 
     379      724720 :                 if (!nr && is_project(rel->op) && len > 0) /* project at least one column if exists */
     380        6656 :                         exps[0]->used = 1;
     381             : 
     382     3563135 :                 for (i = len-1; i >= 0; i--) {
     383     2838415 :                         sql_exp *e = exps[i];
     384             : 
     385     2838415 :                         if (!is_project(rel->op) || (e->used && !is_set(rel->op))) {
     386     2267835 :                                 if (is_project(rel->op))
     387     1851236 :                                         nr += exp_mark_used(rel, e, i);
     388     2267832 :                                 nr += exp_mark_used(subrel, e, -2);
     389             :                         }
     390             :                 }
     391             :         }
     392             :         /* for count/rank we need at least one column */
     393      763429 :         if (!nr && subrel && (is_project(subrel->op) || is_base(subrel->op)) && !list_empty(subrel->exps) &&
     394       23636 :                 (is_simple_project(rel->op) && project_unsafe(rel, false))) {
     395          34 :                 sql_exp *e = subrel->exps->h->data;
     396          34 :                 e->used = 1;
     397             :         }
     398      763429 :         if (rel->r && (is_simple_project(rel->op) || is_groupby(rel->op))) {
     399       32249 :                 list *l = rel->r;
     400       32249 :                 node *n;
     401             : 
     402      110989 :                 for (n=l->h; n; n = n->next) {
     403       78740 :                         sql_exp *e = n->data;
     404             : 
     405       78740 :                         e->used = 1;
     406             :                         /* possibly project/groupby uses columns from the inner */
     407       78740 :                         exp_mark_used(subrel, e, -2);
     408             :                 }
     409             :         }
     410      763429 : }
     411             : 
     412             : static void exps_used(list *l);
     413             : 
     414             : static void
     415     3344401 : exp_used(sql_exp *e)
     416             : {
     417     3375936 :         if (e) {
     418     3357524 :                 e->used = 1;
     419             : 
     420     3357524 :                 switch (e->type) {
     421       30341 :                 case e_convert:
     422       30341 :                         exp_used(e->l);
     423       30341 :                         break;
     424       96531 :                 case e_func:
     425             :                 case e_aggr:
     426       96531 :                         exps_used(e->l);
     427       96531 :                         break;
     428        7916 :                 case e_cmp:
     429        7916 :                         if (e->flag == cmp_or || e->flag == cmp_filter) {
     430        1042 :                                 exps_used(e->l);
     431        1042 :                                 exps_used(e->r);
     432        6874 :                         } else if (e->flag == cmp_in || e->flag == cmp_notin) {
     433         232 :                                 exp_used(e->l);
     434         232 :                                 exps_used(e->r);
     435             :                         } else {
     436        6642 :                                 exp_used(e->l);
     437        6642 :                                 exp_used(e->r);
     438        6642 :                                 if (e->f)
     439             :                                         exp_used(e->f);
     440             :                         }
     441             :                         break;
     442             :                 default:
     443             :                         break;
     444             :                 }
     445             :         }
     446     3344401 : }
     447             : 
     448             : static void
     449      860535 : exps_used(list *l)
     450             : {
     451      860535 :         if (l) {
     452     4190222 :                 for (node *n = l->h; n; n = n->next)
     453     3329800 :                         exp_used(n->data);
     454             :         }
     455      860533 : }
     456             : 
     457             : static void
     458      797507 : rel_used(sql_rel *rel)
     459             : {
     460      797507 :         if (!rel)
     461             :                 return;
     462      753707 :         if (is_join(rel->op) || is_set(rel->op) || is_semi(rel->op) || is_modify(rel->op)) {
     463       65177 :                 rel_used(rel->l);
     464       65177 :                 rel_used(rel->r);
     465             :         } else if (rel->op == op_munion) {
     466       11962 :                 list *l = rel->l;
     467       36301 :                 for(node *n = l->h; n; n = n->next)
     468       24339 :                         rel_used(n->data);
     469             :         } else if (is_topn(rel->op) || is_select(rel->op) || is_sample(rel->op)) {
     470       20596 :                 rel_used(rel->l);
     471       20597 :                 rel = rel->l;
     472             :         } else if (is_ddl(rel->op)) {
     473      399883 :                 if (rel->flag == ddl_output || rel->flag == ddl_create_seq || rel->flag == ddl_alter_seq || rel->flag == ddl_alter_table || rel->flag == ddl_create_table || rel->flag == ddl_create_view) {
     474       34442 :                         rel_used(rel->l);
     475             :                 } else if (rel->flag == ddl_list || rel->flag == ddl_exception) {
     476         282 :                         rel_used(rel->l);
     477         282 :                         rel_used(rel->r);
     478             :                 } else if (rel->flag == ddl_psm) {
     479           7 :                         exps_used(rel->exps);
     480             :                 }
     481             :         } else if (rel->op == op_table) {
     482        1088 :                 if (IS_TABLE_PROD_FUNC(rel->flag) || rel->flag == TABLE_FROM_RELATION)
     483        1088 :                         rel_used(rel->l);
     484        1088 :                 exp_used(rel->r);
     485             :         }
     486      854702 :         if (rel && rel->exps) {
     487      742329 :                 exps_used(rel->exps);
     488      742326 :                 if (rel->r && (is_simple_project(rel->op) || is_groupby(rel->op)))
     489       19353 :                         exps_used(rel->r);
     490             :         }
     491             : }
     492             : 
     493             : static void
     494     1258546 : rel_mark_used(mvc *sql, sql_rel *rel, int proj)
     495             : {
     496     1869503 :         if (proj && (need_distinct(rel)))
     497       10169 :                 rel_used(rel);
     498             : 
     499     1869501 :         switch(rel->op) {
     500             :         case op_basetable:
     501             :         case op_truncate:
     502             :         case op_insert:
     503             :                 break;
     504             : 
     505       13637 :         case op_table:
     506             : 
     507       13637 :                 if (rel->l && rel->flag != TRIGGER_WRAPPER) {
     508         144 :                         rel_used(rel);
     509         144 :                         if (rel->r)
     510         144 :                                 exp_mark_used(rel->l, rel->r, -2);
     511         144 :                         rel_mark_used(sql, rel->l, proj);
     512             :                 }
     513             :                 break;
     514             : 
     515       16585 :         case op_topn:
     516             :         case op_sample:
     517       16585 :                 if (proj) {
     518       16502 :                         rel = rel ->l;
     519       16502 :                         rel_mark_used(sql, rel, proj);
     520       16502 :                         break;
     521             :                 }
     522             :                 /* fall through */
     523             :         case op_project:
     524             :         case op_groupby:
     525      511788 :                 if (proj && rel->l) {
     526      292947 :                         rel_exps_mark_used(sql->sa, rel, rel->l);
     527      292946 :                         rel_mark_used(sql, rel->l, 0);
     528       10732 :                 } else if (proj) {
     529       10649 :                         rel_exps_mark_used(sql->sa, rel, NULL);
     530             :                 }
     531             :                 break;
     532        7655 :         case op_update:
     533             :         case op_delete:
     534        7655 :                 if (proj && rel->r) {
     535        6112 :                         sql_rel *r = rel->r;
     536             : 
     537        6112 :                         if (!list_empty(r->exps)) {
     538        7212 :                                 for (node *n = r->exps->h; n; n = n->next) {
     539        6113 :                                         sql_exp *e = n->data;
     540        6113 :                                         const char *nname = exp_name(e);
     541             : 
     542        6113 :                                         if (nname && nname[0] == '%' && strcmp(nname, TID) == 0) { /* TID is used */
     543        5013 :                                                 e->used = 1;
     544        5013 :                                                 break;
     545             :                                         }
     546             :                                 }
     547             :                         }
     548        6112 :                         rel_exps_mark_used(sql->sa, rel, rel->r);
     549        6112 :                         rel_mark_used(sql, rel->r, 0);
     550             :                 }
     551             :                 break;
     552             : 
     553      398526 :         case op_ddl:
     554      398526 :                 if (rel->flag == ddl_output || rel->flag == ddl_create_seq || rel->flag == ddl_alter_seq || rel->flag == ddl_alter_table || rel->flag == ddl_create_table || rel->flag == ddl_create_view) {
     555       33085 :                         if (rel->l)
     556             :                                 rel_mark_used(sql, rel->l, 0);
     557             :                 } else if (rel->flag == ddl_list || rel->flag == ddl_exception) {
     558         282 :                         if (rel->l)
     559         282 :                                 rel_mark_used(sql, rel->l, 0);
     560         282 :                         if (rel->r)
     561             :                                 rel_mark_used(sql, rel->r, 0);
     562             :                 }
     563             :                 break;
     564             : 
     565      109276 :         case op_select:
     566      109276 :                 if (rel->l) {
     567      109276 :                         rel_exps_mark_used(sql->sa, rel, rel->l);
     568      109276 :                         rel_mark_used(sql, rel->l, 0);
     569             :                 }
     570             :                 break;
     571             : 
     572        5144 :         case op_union:
     573             :         case op_inter:
     574             :         case op_except:
     575             :                 /* For now we mark all union expression as used */
     576             : 
     577             :                 /* Later we should (in case of union all) remove unused
     578             :                  * columns from the projection.
     579             :                  *
     580             :                  * Project part of union is based on column position.
     581             :                  */
     582        5144 :                 if (proj && (need_distinct(rel) || !rel->exps)) {
     583        2334 :                         rel_used(rel);
     584        2334 :                         if (!rel->exps) {
     585           0 :                                 rel_used(rel->l);
     586           0 :                                 rel_used(rel->r);
     587             :                         }
     588        2334 :                         rel_mark_used(sql, rel->l, 0);
     589        2334 :                         rel_mark_used(sql, rel->r, 0);
     590         482 :                 } else if (proj && !need_distinct(rel)) {
     591         482 :                         sql_rel *l = rel->l;
     592             : 
     593         482 :                         positional_exps_mark_used(rel, l);
     594         482 :                         rel_exps_mark_used(sql->sa, rel, l);
     595         482 :                         rel_mark_used(sql, rel->l, 0);
     596             :                         /* based on child check set expression list */
     597         482 :                         if (is_project(l->op) && need_distinct(l))
     598           0 :                                 positional_exps_mark_used(l, rel);
     599         482 :                         positional_exps_mark_used(rel, rel->r);
     600         482 :                         rel_exps_mark_used(sql->sa, rel, rel->r);
     601         482 :                         rel_mark_used(sql, rel->r, 0);
     602             :                 }
     603             :                 break;
     604             : 
     605       48040 :         case op_munion:
     606       48040 :                 assert(rel->l);
     607             :                 // TODO: here we blindly follow the same logic as op_union. RE-evaluate
     608       48040 :                 if (proj && (need_distinct(rel) || !rel->exps)) {
     609        1980 :                         rel_used(rel);
     610        1980 :                         if (!rel->exps) {
     611           0 :                                 for (node *n = ((list*)rel->l)->h; n; n = n->next) {
     612           0 :                                         rel_used(n->data);
     613           0 :                                         rel_mark_used(sql, n->data, 0);
     614             :                                 }
     615             :                         }
     616       21389 :                 } else if (proj && !need_distinct(rel)) {
     617       21389 :                         bool first = true;
     618       64494 :                         for (node *n = ((list*)rel->l)->h; n; n = n->next) {
     619       43105 :                                 sql_rel *l = n->data;
     620             : 
     621       43105 :                                 positional_exps_mark_used(rel, l);
     622       43105 :                                 rel_exps_mark_used(sql->sa, rel, l);
     623       43105 :                                 rel_mark_used(sql, l, 0);
     624             :                                 /* based on child check set expression list */
     625       43105 :                                 if (first && is_project(l->op) && need_distinct(l))
     626           0 :                                         positional_exps_mark_used(l, rel);
     627       43105 :                                 first = false;
     628             :                         }
     629             :                 }
     630             :                 break;
     631      150188 :         case op_join:
     632             :         case op_left:
     633             :         case op_right:
     634             :         case op_full:
     635             :         case op_semi:
     636             :         case op_anti:
     637             :         case op_merge:
     638      150188 :                 rel_exps_mark_used(sql->sa, rel, rel->l);
     639      150188 :                 rel_exps_mark_used(sql->sa, rel, rel->r);
     640      150188 :                 rel_mark_used(sql, rel->l, 0);
     641      150188 :                 rel_mark_used(sql, rel->r, 0);
     642      150188 :                 break;
     643             :         }
     644     1258543 : }
     645             : 
     646             : static sql_rel * rel_dce_sub(mvc *sql, sql_rel *rel);
     647             : 
     648             : static sql_rel *
     649     1118552 : rel_remove_unused(mvc *sql, sql_rel *rel)
     650             : {
     651     1118552 :         int needed = 0;
     652             : 
     653     1118552 :         if (!rel)
     654             :                 return rel;
     655             : 
     656     1111581 :         switch(rel->op) {
     657      301009 :         case op_basetable: {
     658      301009 :                 sql_table *t = rel->l;
     659             : 
     660      301009 :                 if (t && isReplicaTable(t)) /* TODO fix rewriting in rel_distribute.c */
     661             :                         return rel;
     662             :         }
     663             :         /* fall through */
     664             :         case op_table:
     665      307960 :                 if (rel->exps && (rel->op != op_table || !IS_TABLE_PROD_FUNC(rel->flag))) {
     666     1340759 :                         for(node *n=rel->exps->h; n && !needed; n = n->next) {
     667     1039770 :                                 sql_exp *e = n->data;
     668             : 
     669     1039770 :                                 if (!e->used)
     670      225856 :                                         needed = 1;
     671             :                         }
     672             : 
     673      300989 :                         if (!needed)
     674             :                                 return rel;
     675             : 
     676     1335356 :                         for(node *n=rel->exps->h; n;) {
     677     1109500 :                                 node *next = n->next;
     678     1109500 :                                 sql_exp *e = n->data;
     679             : 
     680             :                                 /* at least one (needed for crossproducts, count(*), rank() and single value projections) !, handled by rel_exps_mark_used */
     681     1109500 :                                 if (!e->used && list_length(rel->exps) > 1)
     682      373649 :                                         list_remove_node(rel->exps, NULL, n);
     683             :                                 n = next;
     684             :                         }
     685             :                 }
     686      232827 :                 if (rel->op == op_table && (IS_TABLE_PROD_FUNC(rel->flag) || rel->flag == TABLE_FROM_RELATION))
     687        6971 :                         rel->l = rel_remove_unused(sql, rel->l);
     688             :                 return rel;
     689             : 
     690       16501 :         case op_topn:
     691             :         case op_sample:
     692             : 
     693       16501 :                 if (rel->l)
     694       16501 :                         rel->l = rel_remove_unused(sql, rel->l);
     695             :                 return rel;
     696             : 
     697      303606 :         case op_project:
     698             :         case op_groupby:
     699             : 
     700      303606 :                 if (/*rel->l &&*/ rel->exps) {
     701     1901243 :                         for(node *n=rel->exps->h; n && !needed; n = n->next) {
     702     1597637 :                                 sql_exp *e = n->data;
     703             : 
     704     1597637 :                                 if (!e->used)
     705       25207 :                                         needed = 1;
     706             :                         }
     707      303606 :                         if (!needed)
     708             :                                 return rel;
     709             : 
     710      585659 :                         for(node *n=rel->exps->h; n;) {
     711      560452 :                                 node *next = n->next;
     712      560452 :                                 sql_exp *e = n->data;
     713             : 
     714             :                                 /* at least one (needed for crossproducts, count(*), rank() and single value projections) */
     715      560452 :                                 if (!e->used && list_length(rel->exps) > 1)
     716      426461 :                                         list_remove_node(rel->exps, NULL, n);
     717             :                                 n = next;
     718             :                         }
     719             :                 }
     720             :                 return rel;
     721             : 
     722        3146 :         case op_join:
     723             :         case op_left:
     724             :         case op_right:
     725             :         case op_full:
     726        3146 :                 if (list_length(rel->attr) > 1) {
     727           0 :                         for(node *n=rel->attr->h; n && !needed; n = n->next) {
     728           0 :                                 sql_exp *e = n->data;
     729             : 
     730           0 :                                 if (!e->used)
     731           0 :                                         needed = 1;
     732             :                         }
     733           0 :                         if (!needed)
     734             :                                 return rel;
     735             : 
     736           0 :                         for(node *n=rel->attr->h; n;) {
     737           0 :                                 node *next = n->next;
     738           0 :                                 sql_exp *e = n->data;
     739             : 
     740           0 :                                 if (!e->used)
     741           0 :                                         list_remove_node(rel->attr, NULL, n);
     742             :                                 n = next;
     743             :                         }
     744             :                 }
     745             :                 return rel;
     746             : 
     747             :         case op_union:
     748             :         case op_inter:
     749             :         case op_except:
     750             :         case op_munion:
     751             : 
     752             :         case op_insert:
     753             :         case op_update:
     754             :         case op_delete:
     755             :         case op_truncate:
     756             :         case op_merge:
     757             : 
     758             :         case op_select:
     759             : 
     760             :         case op_semi:
     761             :         case op_anti:
     762             :                 return rel;
     763      398210 :         case op_ddl:
     764      398210 :                 if (rel->flag == ddl_output || rel->flag == ddl_create_seq || rel->flag == ddl_alter_seq || rel->flag == ddl_alter_table || rel->flag == ddl_create_table || rel->flag == ddl_create_view) {
     765       32813 :                         if (rel->l)
     766       32453 :                                 rel->l = rel_remove_unused(sql, rel->l);
     767             :                 } else if (rel->flag == ddl_list || rel->flag == ddl_exception) {
     768         238 :                         if (rel->l)
     769         238 :                                 rel->l = rel_remove_unused(sql, rel->l);
     770         238 :                         if (rel->r)
     771         238 :                                 rel->r = rel_remove_unused(sql, rel->r);
     772             :                 }
     773             :                 return rel;
     774             :         }
     775             :         return rel;
     776             : }
     777             : 
     778             : static void
     779     1304749 : rel_dce_refs(mvc *sql, sql_rel *rel, list *refs)
     780             : {
     781     1304749 :         if (!rel || (rel_is_ref(rel) && list_find(refs, rel, NULL)))
     782       14180 :                 return ;
     783             : 
     784     1290569 :         switch(rel->op) {
     785      399519 :         case op_table:
     786             :         case op_topn:
     787             :         case op_sample:
     788             :         case op_project:
     789             :         case op_groupby:
     790             :         case op_select:
     791             : 
     792      399519 :                 if (rel->l && (rel->op != op_table || rel->flag != TRIGGER_WRAPPER))
     793      383657 :                         rel_dce_refs(sql, rel->l, refs);
     794             :                 break;
     795             : 
     796             :         case op_basetable:
     797             :         case op_insert:
     798             :         case op_truncate:
     799             :                 break;
     800             : 
     801        6462 :         case op_update:
     802             :         case op_delete:
     803             : 
     804        6462 :                 if (rel->r)
     805        6112 :                         rel_dce_refs(sql, rel->r, refs);
     806             :                 break;
     807             : 
     808      143619 :         case op_union:
     809             :         case op_inter:
     810             :         case op_except:
     811             :         case op_join:
     812             :         case op_left:
     813             :         case op_right:
     814             :         case op_full:
     815             :         case op_semi:
     816             :         case op_anti:
     817             :         case op_merge:
     818             : 
     819      143619 :                 if (rel->l)
     820      143619 :                         rel_dce_refs(sql, rel->l, refs);
     821      143619 :                 if (rel->r)
     822      143619 :                         rel_dce_refs(sql, rel->r, refs);
     823             :                 break;
     824       22660 :         case op_munion:
     825       22660 :                 assert(rel->l);
     826       68600 :                 for (node *n = ((list*)rel->l)->h; n; n = n->next)
     827       45940 :                         rel_dce_refs(sql, n->data, refs);
     828             :                 break;
     829      399625 :         case op_ddl:
     830             : 
     831      399625 :                 if (rel->flag == ddl_output || rel->flag == ddl_create_seq || rel->flag == ddl_alter_seq || rel->flag == ddl_alter_table || rel->flag == ddl_create_table || rel->flag == ddl_create_view) {
     832       34184 :                         if (rel->l)
     833       33824 :                                 rel_dce_refs(sql, rel->l, refs);
     834             :                 } else if (rel->flag == ddl_list || rel->flag == ddl_exception) {
     835         282 :                         if (rel->l)
     836         282 :                                 rel_dce_refs(sql, rel->l, refs);
     837         282 :                         if (rel->r)
     838         248 :                                 rel_dce_refs(sql, rel->r, refs);
     839             :                 } break;
     840             :         }
     841             : 
     842     1290566 :         if (rel_is_ref(rel) && !list_find(refs, rel, NULL))
     843       16599 :                 list_prepend(refs, rel);
     844             : }
     845             : 
     846             : static sql_rel *
     847     1858880 : rel_dce_down(mvc *sql, sql_rel *rel, int skip_proj)
     848             : {
     849     1858880 :         if (!rel)
     850             :                 return rel;
     851             : 
     852     1858880 :         if (!skip_proj && rel_is_ref(rel))
     853             :                 return rel;
     854             : 
     855     1828646 :         switch(rel->op) {
     856      550883 :         case op_basetable:
     857             :         case op_table:
     858             : 
     859      550883 :                 if (skip_proj && rel->l && rel->op == op_table && rel->flag != TRIGGER_WRAPPER)
     860          83 :                         rel->l = rel_dce_down(sql, rel->l, 0);
     861      275198 :                 if (!skip_proj)
     862      275198 :                         rel_dce_sub(sql, rel);
     863             :                 /* fall through */
     864             : 
     865             :         case op_truncate:
     866             :                 return rel;
     867             : 
     868        7451 :         case op_insert:
     869        7451 :                 rel_used(rel->r);
     870        7451 :                 rel_dce_sub(sql, rel->r);
     871        7451 :                 return rel;
     872             : 
     873        7655 :         case op_update:
     874             :         case op_delete:
     875             : 
     876        7655 :                 if (skip_proj && rel->r)
     877        6112 :                         rel->r = rel_dce_down(sql, rel->r, 0);
     878        1193 :                 if (!skip_proj)
     879        1193 :                         rel_dce_sub(sql, rel);
     880             :                 return rel;
     881             : 
     882      523727 :         case op_topn:
     883             :         case op_sample:
     884             :         case op_project:
     885             :         case op_groupby:
     886             : 
     887      523727 :                 if (skip_proj && rel->l)
     888      309405 :                         rel->l = rel_dce_down(sql, rel->l, is_topn(rel->op) || is_sample(rel->op));
     889      203695 :                 if (!skip_proj)
     890      203695 :                         rel_dce_sub(sql, rel);
     891             :                 return rel;
     892             : 
     893        5137 :         case op_union:
     894             :         case op_inter:
     895             :         case op_except:
     896        5137 :                 if (skip_proj) {
     897        2816 :                         if (rel->l)
     898        2816 :                                 rel->l = rel_dce_down(sql, rel->l, 0);
     899        2816 :                         if (rel->r)
     900        2816 :                                 rel->r = rel_dce_down(sql, rel->r, 0);
     901             :                 }
     902        2321 :                 if (!skip_proj)
     903        2321 :                         rel_dce_sub(sql, rel);
     904             :                 return rel;
     905             : 
     906       45072 :         case op_munion:
     907       45072 :                 if (skip_proj) {
     908       70432 :                         for (node *n = ((list*)rel->l)->h; n; n = n->next)
     909       47065 :                                 n->data = rel_dce_down(sql, n->data, 0);
     910             :                 }
     911       21705 :                 if (!skip_proj)
     912       21705 :                         rel_dce_sub(sql, rel);
     913             :                 return rel;
     914      101725 :         case op_select:
     915      101725 :                 if (rel->l)
     916      101725 :                         rel->l = rel_dce_down(sql, rel->l, 0);
     917             :                 return rel;
     918             : 
     919      146725 :         case op_join:
     920             :         case op_left:
     921             :         case op_right:
     922             :         case op_full:
     923             :         case op_semi:
     924             :         case op_anti:
     925             :         case op_merge:
     926      146725 :                 if (rel->l)
     927      146725 :                         rel->l = rel_dce_down(sql, rel->l, 0);
     928      146725 :                 if (rel->r)
     929      146725 :                         rel->r = rel_dce_down(sql, rel->r, 0);
     930      146725 :                 if (!skip_proj && !list_empty(rel->attr))
     931        3146 :                         rel_dce_sub(sql, rel);
     932             :                 return rel;
     933             : 
     934      398332 :         case op_ddl:
     935      398332 :                 if (rel->flag == ddl_output || rel->flag == ddl_create_seq || rel->flag == ddl_alter_seq || rel->flag == ddl_alter_table || rel->flag == ddl_create_table || rel->flag == ddl_create_view) {
     936       32891 :                         if (rel->l)
     937       32725 :                                 rel->l = rel_dce_down(sql, rel->l, 0);
     938             :                 } else if (rel->flag == ddl_list || rel->flag == ddl_exception) {
     939         282 :                         if (rel->l)
     940         282 :                                 rel->l = rel_dce_down(sql, rel->l, 0);
     941         282 :                         if (rel->r)
     942         248 :                                 rel->r = rel_dce_down(sql, rel->r, 0);
     943             :                 }
     944             :                 return rel;
     945             :         }
     946             :         return rel;
     947             : }
     948             : 
     949             : /* DCE
     950             :  *
     951             :  * Based on top relation expressions mark sub expressions as used.
     952             :  * Then recurse down until the projections. Clean them up and repeat.
     953             :  */
     954             : 
     955             : static sql_rel *
     956     1062155 : rel_dce_sub(mvc *sql, sql_rel *rel)
     957             : {
     958     1062155 :         if (!rel)
     959             :                 return rel;
     960             : 
     961             :         /*
     962             :          * Mark used up until the next project
     963             :          * For setops we need to first mark, then remove
     964             :          * because of positional dependency
     965             :          */
     966     1062155 :         rel_mark_used(sql, rel, 1);
     967     1062153 :         rel = rel_remove_unused(sql, rel);
     968     1062156 :         rel_dce_down(sql, rel, 1);
     969     1062156 :         return rel;
     970             : }
     971             : 
     972             : /* add projects under set ops */
     973             : static sql_rel *
     974     2075241 : rel_add_projects(mvc *sql, sql_rel *rel)
     975             : {
     976     2075241 :         if (!rel)
     977             :                 return rel;
     978             : 
     979     2075241 :         switch(rel->op) {
     980             :         case op_basetable:
     981             :         case op_truncate:
     982             :                 return rel;
     983       13913 :         case op_insert:
     984             :         case op_update:
     985             :         case op_delete:
     986       13913 :                 if (rel->r)
     987       13563 :                         rel->r = rel_add_projects(sql, rel->r);
     988             :                 return rel;
     989        2912 :         case op_union:
     990             :         case op_inter:
     991             :         case op_except:
     992             :                 /* We can only reduce the list of expressions of an set op
     993             :                  * if the projection under it can also be reduced.
     994             :                  */
     995        2912 :                 if (rel->l) {
     996        2912 :                         sql_rel *l = rel->l;
     997             : 
     998        2912 :                         if (!is_project(l->op) && !need_distinct(rel))
     999           1 :                                 l = rel_project(sql->sa, l, rel_projections(sql, l, NULL, 1, 1));
    1000        2912 :                         rel->l = rel_add_projects(sql, l);
    1001             :                 }
    1002        2912 :                 if (rel->r) {
    1003        2912 :                         sql_rel *r = rel->r;
    1004             : 
    1005        2912 :                         if (!is_project(r->op) && !need_distinct(rel))
    1006           1 :                                 r = rel_project(sql->sa, r, rel_projections(sql, r, NULL, 1, 1));
    1007        2912 :                         rel->r = rel_add_projects(sql, r);
    1008             :                 }
    1009             :                 return rel;
    1010       33871 :         case op_munion:
    1011       33871 :                 assert(rel->l);
    1012      151348 :                 for (node *n = ((list*)rel->l)->h; n; n = n->next) {
    1013      117477 :                         sql_rel* r = n->data;
    1014      117477 :                         if (!is_project(r->op) && !need_distinct(rel))
    1015          23 :                                 r = rel_project(sql->sa, r, rel_projections(sql, r, NULL, 1, 1));
    1016      117477 :                         r = rel_add_projects(sql, r);
    1017      117477 :                         n->data = r;
    1018             :                 }
    1019             :                 return rel;
    1020      787495 :         case op_topn:
    1021             :         case op_sample:
    1022             :         case op_project:
    1023             :         case op_groupby:
    1024             :         case op_select:
    1025             :         case op_table:
    1026      787495 :                 if (rel->l && (rel->op != op_table || rel->flag != TRIGGER_WRAPPER))
    1027      757254 :                         rel->l = rel_add_projects(sql, rel->l);
    1028             :                 return rel;
    1029      299527 :         case op_join:
    1030             :         case op_left:
    1031             :         case op_right:
    1032             :         case op_full:
    1033             :         case op_semi:
    1034             :         case op_anti:
    1035             :         case op_merge:
    1036      299527 :                 if (rel->l)
    1037      299527 :                         rel->l = rel_add_projects(sql, rel->l);
    1038      299527 :                 if (rel->r)
    1039      299527 :                         rel->r = rel_add_projects(sql, rel->r);
    1040             :                 return rel;
    1041      399893 :         case op_ddl:
    1042      399893 :                 if (rel->flag == ddl_output || rel->flag == ddl_create_seq || rel->flag == ddl_alter_seq || rel->flag == ddl_alter_table || rel->flag == ddl_create_table || rel->flag == ddl_create_view) {
    1043       34452 :                         if (rel->l)
    1044       34092 :                                 rel->l = rel_add_projects(sql, rel->l);
    1045             :                 } else if (rel->flag == ddl_list || rel->flag == ddl_exception) {
    1046         282 :                         if (rel->l)
    1047         282 :                                 rel->l = rel_add_projects(sql, rel->l);
    1048         282 :                         if (rel->r)
    1049         248 :                                 rel->r = rel_add_projects(sql, rel->r);
    1050             :                 }
    1051             :                 return rel;
    1052             :         }
    1053             :         return rel;
    1054             : }
    1055             : 
    1056             : static sql_rel *
    1057      547448 : rel_dce_(mvc *sql, sql_rel *rel)
    1058             : {
    1059      547448 :         list *refs = sa_list(sql->sa);
    1060             : 
    1061      547445 :         rel_dce_refs(sql, rel, refs);
    1062      547448 :         if (refs) {
    1063      564047 :                 for(node *n = refs->h; n; n = n->next) {
    1064       16599 :                         sql_rel *i = n->data;
    1065             : 
    1066       16599 :                         while (!rel_is_ref(i) && i->l && !is_base(i->op))
    1067             :                                 i = i->l;
    1068       16599 :                         if (i)
    1069       16599 :                                 rel_used(i);
    1070             :                 }
    1071             :         }
    1072      547448 :         rel = rel_add_projects(sql, rel);
    1073      547448 :         rel_used(rel);
    1074      547445 :         rel_dce_sub(sql, rel);
    1075      547445 :         return rel;
    1076             : }
    1077             : 
    1078             : 
    1079             : /* Remove unused expressions */
    1080             : static sql_rel *
    1081      547448 : rel_dce(visitor *v, global_props *gp, sql_rel *rel)
    1082             : {
    1083      547448 :         (void) gp;
    1084      547448 :         return rel_dce_(v->sql, rel);
    1085             : }
    1086             : 
    1087             : /* keep export for other projects */
    1088             : sql_rel *
    1089           0 : rel_deadcode_elimination(mvc *sql, sql_rel *rel)
    1090             : {
    1091           0 :         return rel_dce_(sql, rel);
    1092             : }
    1093             : 
    1094             : run_optimizer
    1095      622729 : bind_dce(visitor *v, global_props *gp)
    1096             : {
    1097      622729 :         int flag = v->sql->sql_optimizer;
    1098      622729 :         return gp->opt_cycle == 0 && gp->opt_level == 1 && (flag & dce) ? rel_dce : NULL;
    1099             : }
    1100             : 
    1101             : 
    1102             : static int
    1103       84036 : topn_sample_safe_exps( list *exps, bool nil_limit )
    1104             : {
    1105             :         /* Limit only expression lists are always save */
    1106       84036 :         if (list_length(exps) == 1)
    1107             :                 return 1;
    1108        1352 :         for (node *n = exps->h; n; n = n->next ) {
    1109         921 :                 sql_exp *e = n->data;
    1110             : 
    1111         921 :                 if (!e || e->type != e_atom || (!nil_limit && exp_is_null(e)))
    1112          59 :                         return 0;
    1113             :         }
    1114             :         return 1;
    1115             : }
    1116             : 
    1117             : static list *
    1118         129 : sum_limit_offset(mvc *sql, sql_rel *rel)
    1119             : {
    1120             :         /* for sample we always propagate, or if the expression list only consists of a limit expression, we copy it */
    1121         129 :         if (is_sample(rel->op) || list_length(rel->exps) == 1)
    1122         126 :                 return exps_copy(sql, rel->exps);
    1123           3 :         assert(list_length(rel->exps) == 2);
    1124           3 :         sql_subtype *lng = sql_bind_localtype("lng");
    1125           3 :         sql_exp *add = rel_binop_(sql, NULL, exp_copy(sql, rel->exps->h->data), exp_copy(sql, rel->exps->h->next->data), "sys", "sql_add", card_value, true);
    1126             :         /* for remote plans, make sure the output type is a bigint */
    1127           3 :         if (subtype_cmp(lng, exp_subtype(add)) != 0)
    1128           3 :                 add = exp_convert(sql, add, exp_subtype(add), lng);
    1129           3 :         return list_append(sa_list(sql->sa), add);
    1130             : }
    1131             : 
    1132             : /*
    1133             :  * Push TopN (only LIMIT, no ORDER BY) down through projections underneath crossproduct, i.e.,
    1134             :  *
    1135             :  *     topn(                          topn(
    1136             :  *         project(                       project(
    1137             :  *             crossproduct(                  crossproduct(
    1138             :  *                 L,           =>                topn( L )[ n ],
    1139             :  *                 R                              topn( R )[ n ]
    1140             :  *             )                              )
    1141             :  *         )[ Cs ]*                       )[ Cs ]*
    1142             :  *     )[ n ]                         )[ n ]
    1143             :  *
    1144             :  *  (TODO: in case of n==1 we can omit the original top-level TopN)
    1145             :  *
    1146             :  * also push topn under (non reordering) projections.
    1147             :  */
    1148             : static sql_rel *
    1149      136661 : rel_push_topn_and_sample_down_(visitor *v, sql_rel *rel)
    1150             : {
    1151      136661 :         sql_rel *rp = NULL, *r = rel->l, *rpp = NULL;
    1152             : 
    1153      136661 :         if (is_topn(rel->op) && !rel_is_ref(rel) &&
    1154       50046 :                         r && r->op == op_table && r->flag != TRIGGER_WRAPPER && !rel_is_ref(r) && r->r) {
    1155           3 :                 sql_exp *op = r->r;
    1156           3 :                 sql_subfunc *f = op->f;
    1157             : 
    1158           3 :                 if (is_func(op->type) && strcmp(f->func->base.name, "file_loader") == 0 && !sql_func_mod(f->func)[0] && !sql_func_imp(f->func)[0]) {
    1159             :                         /* push limit, to arguments of file_loader */
    1160           0 :                         list *args = op->l;
    1161           0 :                         if (list_length(args) == 2) {
    1162           0 :                                 sql_exp *topN = rel->exps->h->data;
    1163           0 :                                 sql_exp *offset = rel->exps->h->next? rel->exps->h->next->data:NULL;
    1164           0 :                                 atom *topn = topN->l;
    1165           0 :                                 if (offset) {
    1166           0 :                                                 atom *b1 = (atom *)offset->l, *c = atom_add(v->sql->sa, b1, topn);
    1167             : 
    1168           0 :                                                 if (!c)
    1169             :                                                         return rel;
    1170           0 :                                                 if (atom_cmp(c, topn) < 0) /* overflow */
    1171             :                                                         return rel;
    1172             :                                                 topn = c;
    1173             :                                 }
    1174           0 :                                 append(args, exp_atom(v->sql->sa, topn));
    1175           0 :                                 v->changes++;
    1176             :                         }
    1177             :                 }
    1178             :         }
    1179             : 
    1180      136661 :         if ((is_topn(rel->op) || is_sample(rel->op)) && topn_sample_safe_exps(rel->exps, true)) {
    1181       50129 :                 sql_rel *(*func) (allocator *, sql_rel *, list *) = is_topn(rel->op) ? rel_topn : rel_sample;
    1182             : 
    1183             :                 /* nested topN relations */
    1184       50129 :                 if (r && is_topn(rel->op) && is_topn(r->op) && !rel_is_ref(r)) {
    1185           0 :                         sql_exp *topN1 = rel->exps->h->data, *topN2 = r->exps->h->data;
    1186           0 :                         sql_exp *offset1 = list_length(rel->exps) > 1 ? rel->exps->h->next->data : NULL;
    1187           0 :                         sql_exp *offset2 = list_length(r->exps) > 1 ? r->exps->h->next->data : NULL;
    1188             : 
    1189           0 :                         if (topN1->l && topN2->l && (!offset1 || offset1->l) && (!offset2 || offset2->l)) { /* no parameters */
    1190           0 :                                 bool changed = false;
    1191             : 
    1192           0 :                                 if ((!offset1 || (offset1->type == e_atom && offset1->l)) && (!offset2 || (offset2->type == e_atom && offset2->l))) { /* only atoms */
    1193           0 :                                         if (!offset1 && offset2) {
    1194           0 :                                                 list_append(rel->exps, exp_copy(v->sql, offset2));
    1195           0 :                                                 changed = true;
    1196           0 :                                         } else if (offset1 && offset2) { /* sum offsets */
    1197           0 :                                                 atom *b1 = (atom *)offset1->l, *b2 = (atom *)offset2->l, *c = atom_add(v->sql->sa, b1, b2);
    1198             : 
    1199           0 :                                                 if (!c) /* error, don't apply optimization, WARNING because of this the offset optimization must come before the limit one */
    1200             :                                                         return rel;
    1201           0 :                                                 if (atom_cmp(c, b2) < 0) /* overflow */
    1202           0 :                                                         c = atom_int(v->sql->sa, sql_bind_localtype("lng"), GDK_lng_max);
    1203           0 :                                                 offset1->l = c;
    1204           0 :                                                 changed = true;
    1205             :                                         }
    1206             :                                 }
    1207             : 
    1208           0 :                                 if (topN1->type == e_atom && topN1->l && topN2->type == e_atom && topN2->l) { /* only atoms */
    1209           0 :                                         atom *a1 = (atom *)topN1->l, *a2 = (atom *)topN2->l;
    1210             : 
    1211           0 :                                         if (!a2->isnull && (a1->isnull || atom_cmp(a1, a2) >= 0)) { /* topN1 is not set or is larger than topN2 */
    1212           0 :                                                 rel->exps->h->data = exp_copy(v->sql, topN2);
    1213           0 :                                                 changed = true;
    1214             :                                         }
    1215             :                                 }
    1216             : 
    1217           0 :                                 if (changed) {
    1218           0 :                                         rel->l = r->l;
    1219           0 :                                         r->l = NULL;
    1220           0 :                                         rel_destroy(r);
    1221           0 :                                         v->changes++;
    1222           0 :                                         return rel;
    1223             :                                 }
    1224             :                         }
    1225             :                 }
    1226             : 
    1227       50129 :                 if (r && is_simple_project(r->op) && need_distinct(r))
    1228             :                         return rel;
    1229             : 
    1230             :                 /* push topn/sample under projections */
    1231       50124 :                 if (!rel_is_ref(rel) && r && is_simple_project(r->op) && !need_distinct(r) && !rel_is_ref(r) && r->l && list_empty(r->r)) {
    1232             :                         sql_rel *x = r, *px = x;
    1233             : 
    1234       32453 :                         while (is_simple_project(x->op) && !need_distinct(x) && !rel_is_ref(x) && x->l && list_empty(x->r)) {
    1235       16238 :                                 px = x;
    1236       16238 :                                 x = x->l;
    1237             :                         }
    1238             :                         /* only push topn once */
    1239       16213 :                         if (x && x->op == rel->op)
    1240             :                                 return rel;
    1241             : 
    1242       16206 :                         rel->l = x;
    1243       16206 :                         px->l = rel;
    1244       16206 :                         rel = r;
    1245       16206 :                         v->changes++;
    1246       16206 :                         return rel;
    1247             :                 }
    1248             : 
    1249       33909 :                 if (!topn_sample_safe_exps(rel->exps, false))
    1250             :                         return rel;
    1251             : 
    1252             :                 /* duplicate topn/sample direct under union or crossproduct */
    1253       33850 :                 if (r && !rel_is_ref(r) && r->l && r->r && ((is_union(r->op) && r->exps) || (r->op == op_join && list_empty(r->exps)))) {
    1254         148 :                         sql_rel *u = r, *x;
    1255         148 :                         sql_rel *ul = u->l;
    1256         148 :                         sql_rel *ur = u->r;
    1257         148 :                         bool changed = false;
    1258             : 
    1259         148 :                         x = ul;
    1260         154 :                         while (is_simple_project(x->op) && !need_distinct(x) && !rel_is_ref(x) && x->l && list_empty(x->r))
    1261           6 :                                 x = x->l;
    1262         148 :                         if (x && x->op != rel->op) { /* only push topn once */
    1263          60 :                                 ul = func(v->sql->sa, ul, sum_limit_offset(v->sql, rel));
    1264          60 :                                 set_processed(ul);
    1265          60 :                                 u->l = ul;
    1266          60 :                                 changed = true;
    1267             :                         }
    1268             : 
    1269         148 :                         x = ur;
    1270         156 :                         while (is_simple_project(x->op) && !need_distinct(x) && !rel_is_ref(x) && x->l && list_empty(x->r))
    1271           8 :                                 x = x->l;
    1272         148 :                         if (x && x->op != rel->op) { /* only push topn once */
    1273          61 :                                 ur = func(v->sql->sa, ur, sum_limit_offset(v->sql, rel));
    1274          61 :                                 set_processed(ur);
    1275          61 :                                 u->r = ur;
    1276          61 :                                 changed = true;
    1277             :                         }
    1278             : 
    1279         148 :                         if (changed)
    1280          61 :                                 v->changes++;
    1281         148 :                         return rel;
    1282             :                 }
    1283             : 
    1284             :                 /* duplicate topn/sample + [ project-order ] under union */
    1285             :                 if (r && !rp)
    1286       33703 :                         rp = r->l;
    1287       33703 :                 if (r && r->exps && is_simple_project(r->op) && !rel_is_ref(r) && !list_empty(r->r) && r->l && is_union(rp->op)) {
    1288           0 :                         sql_rel *u = rp, *ou = u, *x, *ul = u->l, *ur = u->r;
    1289           0 :                         list *rcopy = NULL;
    1290             : 
    1291             :                         /* only push topn/sample once */
    1292           0 :                         x = ul;
    1293           0 :                         while (is_simple_project(x->op) && !need_distinct(x) && !rel_is_ref(x) && x->l && list_empty(x->r))
    1294           0 :                                 x = x->l;
    1295           0 :                         if (x && x->op == rel->op)
    1296             :                                 return rel;
    1297             :                         x = ur;
    1298           0 :                         while (is_simple_project(x->op) && !need_distinct(x) && !rel_is_ref(x) && x->l && list_empty(x->r))
    1299           0 :                                 x = x->l;
    1300           0 :                         if (x && x->op == rel->op)
    1301             :                                 return rel;
    1302             : 
    1303           0 :                         rcopy = exps_copy(v->sql, r->r);
    1304           0 :                         for (node *n = rcopy->h ; n ; n = n->next) {
    1305           0 :                                 sql_exp *e = n->data;
    1306           0 :                                 set_descending(e); /* remove ordering properties for projected columns */
    1307           0 :                                 set_nulls_first(e);
    1308             :                         }
    1309           0 :                         ul = rel_dup(ul);
    1310           0 :                         ur = rel_dup(ur);
    1311           0 :                         if (!is_project(ul->op))
    1312           0 :                                 ul = rel_project(v->sql->sa, ul,
    1313             :                                         rel_projections(v->sql, ul, NULL, 1, 1));
    1314           0 :                         if (!is_project(ur->op))
    1315           0 :                                 ur = rel_project(v->sql->sa, ur,
    1316             :                                         rel_projections(v->sql, ur, NULL, 1, 1));
    1317           0 :                         rel_rename_exps(v->sql, u->exps, ul->exps);
    1318           0 :                         rel_rename_exps(v->sql, u->exps, ur->exps);
    1319             : 
    1320             :                         /* introduce projects under the set */
    1321           0 :                         ul = rel_project(v->sql->sa, ul, NULL);
    1322           0 :                         ul->exps = exps_copy(v->sql, r->exps);
    1323             :                         /* possibly add order by column */
    1324           0 :                         ul->exps = list_distinct(list_merge(ul->exps, exps_copy(v->sql, rcopy), NULL), (fcmp) exp_equal, (fdup) NULL);
    1325           0 :                         ul->nrcols = list_length(ul->exps);
    1326           0 :                         ul->r = exps_copy(v->sql, r->r);
    1327           0 :                         set_processed(ul);
    1328           0 :                         ul = func(v->sql->sa, ul, sum_limit_offset(v->sql, rel));
    1329           0 :                         set_processed(ul);
    1330             : 
    1331           0 :                         ur = rel_project(v->sql->sa, ur, NULL);
    1332           0 :                         ur->exps = exps_copy(v->sql, r->exps);
    1333             :                         /* possibly add order by column */
    1334           0 :                         ur->exps = list_distinct(list_merge(ur->exps, exps_copy(v->sql, rcopy), NULL), (fcmp) exp_equal, (fdup) NULL);
    1335           0 :                         ur->nrcols = list_length(ur->exps);
    1336           0 :                         ur->r = exps_copy(v->sql, r->r);
    1337           0 :                         set_processed(ur);
    1338           0 :                         ur = func(v->sql->sa, ur, sum_limit_offset(v->sql, rel));
    1339           0 :                         set_processed(ur);
    1340             : 
    1341           0 :                         u = rel_setop(v->sql->sa, ul, ur, op_union);
    1342           0 :                         u->exps = exps_alias(v->sql, r->exps);
    1343           0 :                         u->nrcols = list_length(u->exps);
    1344           0 :                         set_processed(u);
    1345             :                         /* possibly add order by column */
    1346           0 :                         u->exps = list_distinct(list_merge(u->exps, rcopy, NULL), (fcmp) exp_equal, (fdup) NULL);
    1347           0 :                         if (need_distinct(r)) {
    1348           0 :                                 set_distinct(ul);
    1349           0 :                                 set_distinct(ur);
    1350             :                         }
    1351             : 
    1352             :                         /* zap names */
    1353           0 :                         rel_no_rename_exps(u->exps);
    1354           0 :                         rel_destroy(ou);
    1355             : 
    1356           0 :                         ur = rel_project(v->sql->sa, u, exps_alias(v->sql, r->exps));
    1357           0 :                         ur->r = r->r;
    1358           0 :                         r->l = NULL;
    1359             : 
    1360           0 :                         if (need_distinct(r))
    1361           0 :                                 set_distinct(ur);
    1362             : 
    1363           0 :                         rel_destroy(r);
    1364           0 :                         rel->l = ur;
    1365           0 :                         v->changes++;
    1366           0 :                         return rel;
    1367             :                 }
    1368             :                 /* a  left outer join b order by a.* limit L, can be copied into a */
    1369             :                 /* topn ( project (orderby)( optional project ( left ())
    1370             :                  * rel    r                                     rp */
    1371       33702 :                 if (r && !rp)
    1372          66 :                         rp = r->l;
    1373       33702 :                 if (r && rp && is_simple_project(rp->op) && !rp->r && rp->l)
    1374       33702 :                         rpp = rp->l;
    1375       33702 :                 if (r && r->exps && is_simple_project(r->op) && !rel_is_ref(r) && r->r && r->l && ((!rpp && is_left(rp->op)) ||
    1376         300 :                                 (rpp && is_left(rpp->op)))) {
    1377          24 :                         sql_rel *lj = rpp?rpp:rp;
    1378          24 :                         sql_rel *l = lj->l;
    1379          24 :                         list *obes = r->r, *nobes = sa_list(v->sql->sa);
    1380          24 :                         int fnd = 1;
    1381          63 :                         for (node *n = obes->h; n && fnd; n = n->next) {
    1382          41 :                                 sql_exp *obe = n->data;
    1383          41 :                                 int asc = is_ascending(obe);
    1384          41 :                                 int nl = nulls_last(obe);
    1385             :                                 /* only simple rename expressions */
    1386          41 :                                 sql_exp *pe = exps_find_exp(r->exps, obe);
    1387          41 :                                 if (pe && rpp)
    1388          33 :                                         pe = exps_find_exp(rp->exps, pe);
    1389          41 :                                 if (pe)
    1390          41 :                                         pe = rel_find_exp(l, pe);
    1391          41 :                                 if (pe) {
    1392          41 :                                         if (exp_is_atom(pe))
    1393             :                                                 return rel;
    1394          39 :                                         pe = exp_ref(v->sql, pe);
    1395          39 :                                         if (asc)
    1396          29 :                                                 set_ascending(pe);
    1397          39 :                                         if (nl)
    1398           5 :                                                 set_nulls_last(pe);
    1399          39 :                                         append(nobes, pe);
    1400             :                                 }
    1401             :                                 else
    1402             :                                         fnd = 0;
    1403             :                         }
    1404          22 :                         if (fnd && ((is_topn(rel->op) && !is_topn(l->op)) || (is_sample(rel->op) && !is_sample(l->op)))) {
    1405             :                                 /* inject topn */
    1406             :                                 /* Todo add order by */
    1407           8 :                                 sql_rel *ob = lj->l = rel_project(v->sql->sa, lj->l, rel_projections(v->sql, lj->l, NULL, 1, 1));
    1408           8 :                                 ob->r = nobes;
    1409           8 :                                 lj->l = func(v->sql->sa, lj->l, sum_limit_offset(v->sql, rel));
    1410           8 :                                 v->changes++;
    1411           8 :                                 return rel;
    1412             :                         }
    1413             :                 }
    1414             :         }
    1415             :         return rel;
    1416             : }
    1417             : 
    1418             : static sql_rel *
    1419       33444 : rel_push_topn_and_sample_down(visitor *v, global_props *gp, sql_rel *rel)
    1420             : {
    1421       33444 :         (void) gp;
    1422       33444 :         return rel_visitor_topdown(v, rel, &rel_push_topn_and_sample_down_);
    1423             : }
    1424             : 
    1425             : run_optimizer
    1426      622726 : bind_push_topn_and_sample_down(visitor *v, global_props *gp)
    1427             : {
    1428      622726 :         int flag = v->sql->sql_optimizer;
    1429      622530 :         return gp->opt_level == 1 && (gp->cnt[op_topn] || gp->cnt[op_sample]) &&
    1430      656170 :                    (flag & push_topn_and_sample_down) ? rel_push_topn_and_sample_down : NULL;
    1431             : }

Generated by: LCOV version 1.14