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-11-12 19:36:54 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       79907 : rel_rename_exps( mvc *sql, list *src_exps, list *dest_exps)
      34             : {
      35       79907 :         node *n, *m;
      36             : 
      37       79907 :         (void)sql;
      38             :         /* check if a column uses an alias earlier in the list */
      39             : 
      40       79907 :         assert(list_length(src_exps) <= list_length(dest_exps));
      41      670229 :         for (n = src_exps->h, m = dest_exps->h; n && m; n = n->next, m = m->next) {
      42      590322 :                 sql_exp *s = n->data;
      43      590322 :                 sql_exp *d = m->data;
      44      590322 :                 const char *rname = exp_relname(s);
      45             : 
      46      590322 :                 exp_setalias(d, s->alias.label, rname, exp_name(s));
      47             :         }
      48       79907 :         list_hash_clear(dest_exps);
      49       79907 : }
      50             : 
      51             : sql_rel *
      52      164513 : rel_find_ref( sql_rel *r)
      53             : {
      54      499771 :         while (!rel_is_ref(r) && r->l &&
      55      408764 :               (is_project(r->op) || is_select(r->op) /*|| is_join(r->op)*/))
      56             :                 r = r->l;
      57      164513 :         if (rel_is_ref(r))
      58       23970 :                 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       56629 : exps_push_down_prj(mvc *sql, list *exps, sql_rel *f, sql_rel *t, bool keepalias)
      69             : {
      70       56629 :         node *n;
      71       56629 :         list *nl = new_exp_list(sql->sa);
      72             : 
      73      193570 :         for(n = exps->h; n; n = n->next) {
      74      139322 :                 sql_exp *arg = n->data, *narg = NULL;
      75             : 
      76      139322 :                 narg = exp_push_down_prj(sql, arg, f, t);
      77      139322 :                 if (!narg)
      78             :                         return NULL;
      79      136941 :                 narg = exp_propagate(sql->sa, narg, arg);
      80      136941 :                 if (!keepalias && narg->type == e_column)
      81       39686 :                         exp_setalias(narg, arg->alias.label, narg->l, narg->r);
      82      136941 :                 append(nl, narg);
      83             :         }
      84             :         return nl;
      85             : }
      86             : 
      87             : sql_exp *
      88     2656599 : exp_push_down_prj(mvc *sql, sql_exp *e, sql_rel *f, sql_rel *t)
      89             : {
      90     2656599 :         sql_exp *ne = NULL, *l = NULL, *r = NULL, *r2 = NULL;
      91             : 
      92     2656599 :         assert(is_project(f->op));
      93             : 
      94     2656599 :         switch(e->type) {
      95     2236105 :         case e_column:
      96     2236105 :                 assert(e->nid);
      97     2236105 :                 ne = exps_bind_nid(f->exps, e->nid);
      98     2236105 :                 if (!ne || (ne->type != e_column && (ne->type != e_atom || ne->f)))
      99             :                         return NULL;
     100     1908917 :                 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     1908518 :                                 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     1908518 :                 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     1908518 :                 return exp_copy(sql, ne);
     127       60370 :         case e_cmp:
     128       60370 :                 if (e->flag == cmp_or || e->flag == cmp_filter) {
     129        4220 :                         list *l = NULL, *r = NULL;
     130             : 
     131        4220 :                         if (!(l = exps_push_down_prj(sql, e->l, f, t, true)) || !(r = exps_push_down_prj(sql, e->r, f, t, true)))
     132        1234 :                                 return NULL;
     133        2986 :                         if (e->flag == cmp_filter) {
     134         989 :                                 ne = exp_filter(sql->sa, l, r, e->f, is_anti(e));
     135             :                         } else {
     136        1997 :                                 ne = exp_or(sql->sa, l, r, is_anti(e));
     137             :                         }
     138       56150 :                 } 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       50456 :                         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       24267 :                                 return NULL;
     147       26189 :                         if (e->f) {
     148         741 :                                 ne = exp_compare2(sql->sa, l, r, r2, e->flag, is_symmetric(e));
     149             :                         } else {
     150       25448 :                                 ne = exp_compare(sql->sa, l, r, e->flag);
     151             :                         }
     152             :                 }
     153       29666 :                 if (!ne)
     154             :                         return NULL;
     155       29666 :                 return exp_propagate(sql->sa, ne, e);
     156      211416 :         case e_convert:
     157      211416 :                 if (!(l = exp_push_down_prj(sql, e->l, f, t)))
     158             :                         return NULL;
     159      143755 :                 ne = exp_convert(sql, l, exp_fromtype(e), exp_totype(e));
     160      143755 :                 return exp_propagate(sql->sa, ne, e);
     161       48794 :         case e_aggr:
     162             :         case e_func: {
     163       48794 :                 list *l = e->l, *nl = NULL;
     164       48794 :                 sql_exp *ne = NULL;
     165             : 
     166       48794 :                 if (e->type == e_func && exp_unsafe(e, false, false))
     167             :                         return NULL;
     168       48778 :                 if (!list_empty(l)) {
     169       48778 :                         nl = exps_push_down_prj(sql, l, f, t, false);
     170       48778 :                         if (!nl)
     171             :                                 return NULL;
     172             :                 }
     173       47631 :                 if (e->type == e_func)
     174       47631 :                         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       47631 :                 return exp_propagate(sql->sa, ne, e);
     178             :         }
     179       99914 :         case e_atom: {
     180       99914 :                 list *l = e->f, *nl = NULL;
     181             : 
     182       99914 :                 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       99914 :                         ne = exp_copy(sql, e);
     189             :                 }
     190       99914 :                 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      800658 : exps_mark_used(sql_rel *subrel, list *l, int local_proj)
     251             : {
     252      800658 :         int nr = 0;
     253      800658 :         if (list_empty(l))
     254             :                 return nr;
     255             : 
     256     2584537 :         for (node *n = l->h; n != NULL; n = n->next)
     257     1783943 :                 nr += exp_mark_used(subrel, n->data, local_proj);
     258             :         return nr;
     259             : }
     260             : 
     261             : static int
     262     6913076 : exp_mark_used(sql_rel *subrel, sql_exp *e, int local_proj)
     263             : {
     264     7123126 :         int nr = 0;
     265     7123126 :         sql_exp *ne = NULL;
     266             : 
     267     7123126 :         switch(e->type) {
     268     4706904 :         case e_column:
     269     4706904 :                 ne = rel_find_exp(subrel, e);
     270             :                 /* if looking in the same projection, make sure 'ne' is projected before the searched column */
     271     4706939 :                 if (ne && local_proj > -1 && list_position(subrel->exps, ne) >= local_proj)
     272     5913829 :                         ne = NULL;
     273             :                 break;
     274      210050 :         case e_convert:
     275      210050 :                 return exp_mark_used(subrel, e->l, local_proj);
     276      761581 :         case e_aggr:
     277             :         case e_func: {
     278      761581 :                 if (e->l)
     279      729716 :                         nr += exps_mark_used(subrel, e->l, local_proj);
     280      761581 :                 assert(!e->r);
     281             :                 break;
     282             :         }
     283      445312 :         case e_cmp:
     284      445312 :                 if (e->flag == cmp_or || e->flag == cmp_filter) {
     285       22604 :                         nr += exps_mark_used(subrel, e->l, local_proj);
     286       22604 :                         nr += exps_mark_used(subrel, e->r, local_proj);
     287      422708 :                 } else if (e->flag == cmp_in || e->flag == cmp_notin) {
     288       20562 :                         nr += exp_mark_used(subrel, e->l, local_proj);
     289       20562 :                         nr += exps_mark_used(subrel, e->r, local_proj);
     290             :                 } else {
     291      402146 :                         nr += exp_mark_used(subrel, e->l, local_proj);
     292      402146 :                         nr += exp_mark_used(subrel, e->r, local_proj);
     293      402146 :                         if (e->f)
     294        6424 :                                 nr += exp_mark_used(subrel, e->f, local_proj);
     295             :                 }
     296             :                 break;
     297      999279 :         case e_atom:
     298             :                 /* atoms are used in e_cmp */
     299      999279 :                 e->used = 1;
     300             :                 /* return 0 as constants may require a full column ! */
     301      999279 :                 if (e->f)
     302        5172 :                         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     5913829 :         if (ne && e != ne) {
     317     2567664 :                 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     2443905 :                         ne->used = 1;
     319     2567664 :                 return ne->used;
     320             :         }
     321             :         return nr;
     322             : }
     323             : 
     324             : static void
     325       44047 : positional_exps_mark_used( sql_rel *rel, sql_rel *subrel )
     326             : {
     327       44047 :         assert(rel->exps);
     328             : 
     329       44047 :         if ((is_topn(subrel->op) || is_sample(subrel->op)) && subrel->l)
     330       44047 :                 subrel = subrel->l;
     331             :         /* everything is used within the set operation */
     332       44047 :         if (rel->exps && subrel->exps) {
     333       44047 :                 node *m;
     334      372501 :                 for (m=subrel->exps->h; m; m = m->next) {
     335      328454 :                         sql_exp *se = m->data;
     336             : 
     337      328454 :                         se->used = 1;
     338             :                 }
     339             :         }
     340       44047 : }
     341             : 
     342             : static void
     343      766406 : rel_exps_mark_used(allocator *sa, sql_rel *rel, sql_rel *subrel)
     344             : {
     345      766406 :         int nr = 0;
     346             : 
     347      766406 :         if (rel->r && (is_simple_project(rel->op) || is_groupby(rel->op))) {
     348       32412 :                 list *l = rel->r;
     349       32412 :                 node *n;
     350             : 
     351      111418 :                 for (n=l->h; n; n = n->next) {
     352       79004 :                         sql_exp *e = n->data;
     353             : 
     354       79004 :                         e->used = 1;
     355       79004 :                         exp_mark_used(rel, e, -1);
     356             :                 }
     357             :         }
     358      766408 :         if (rel->attr) {
     359       26206 :                 for (node *n = rel->attr->h; n; n = n->next) {
     360       13102 :                         sql_exp *e = n->data;
     361             : 
     362       13102 :                         if (e->type != e_aggr) /* keep all group by's */
     363       13102 :                                 e->used = 1;
     364       13102 :                         if (e->used)
     365       13102 :                                 nr += exp_mark_used(subrel, e, -2);
     366             :                 }
     367             :         }
     368      766410 :         if (rel->exps) {
     369      727398 :                 node *n;
     370      727398 :                 int len = list_length(rel->exps), i;
     371      727401 :                 sql_exp **exps = SA_NEW_ARRAY(sa, sql_exp*, len);
     372             : 
     373     3572220 :                 for (n=rel->exps->h, i = 0; n; n = n->next, i++) {
     374     2844818 :                         sql_exp *e = exps[i] = n->data;
     375             : 
     376     2844818 :                         nr += e->used;
     377             :                 }
     378             : 
     379      727402 :                 if (!nr && is_project(rel->op) && len > 0) /* project at least one column if exists */
     380        6657 :                         exps[0]->used = 1;
     381             : 
     382     3572275 :                 for (i = len-1; i >= 0; i--) {
     383     2844869 :                         sql_exp *e = exps[i];
     384             : 
     385     2844869 :                         if (!is_project(rel->op) || (e->used && !is_set(rel->op))) {
     386     2272673 :                                 if (is_project(rel->op))
     387     1853937 :                                         nr += exp_mark_used(rel, e, i);
     388     2272681 :                                 nr += exp_mark_used(subrel, e, -2);
     389             :                         }
     390             :                 }
     391             :         }
     392             :         /* for count/rank we need at least one column */
     393      766418 :         if (!nr && subrel && (is_project(subrel->op) || is_base(subrel->op)) && !list_empty(subrel->exps) &&
     394       23983 :                 (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      766418 :         if (rel->r && (is_simple_project(rel->op) || is_groupby(rel->op))) {
     399       32412 :                 list *l = rel->r;
     400       32412 :                 node *n;
     401             : 
     402      111418 :                 for (n=l->h; n; n = n->next) {
     403       79006 :                         sql_exp *e = n->data;
     404             : 
     405       79006 :                         e->used = 1;
     406             :                         /* possibly project/groupby uses columns from the inner */
     407       79006 :                         exp_mark_used(subrel, e, -2);
     408             :                 }
     409             :         }
     410      766418 : }
     411             : 
     412             : static void exps_used(list *l);
     413             : 
     414             : static void
     415     3346057 : exp_used(sql_exp *e)
     416             : {
     417     3377597 :         if (e) {
     418     3359185 :                 e->used = 1;
     419             : 
     420     3359185 :                 switch (e->type) {
     421       30346 :                 case e_convert:
     422       30346 :                         exp_used(e->l);
     423       30346 :                         break;
     424       96542 :                 case e_func:
     425             :                 case e_aggr:
     426       96542 :                         exps_used(e->l);
     427       96542 :                         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     3346057 : }
     447             : 
     448             : static void
     449      860691 : exps_used(list *l)
     450             : {
     451      860691 :         if (l) {
     452     4192106 :                 for (node *n = l->h; n; n = n->next)
     453     3331370 :                         exp_used(n->data);
     454             :         }
     455      860847 : }
     456             : 
     457             : static void
     458      796829 : rel_used(sql_rel *rel)
     459             : {
     460      796829 :         if (!rel)
     461             :                 return;
     462      753219 :         if (is_join(rel->op) || is_set(rel->op) || is_semi(rel->op) || is_modify(rel->op)) {
     463       64709 :                 rel_used(rel->l);
     464       64988 :                 rel_used(rel->r);
     465             :         } else if (rel->op == op_munion) {
     466       11964 :                 list *l = rel->l;
     467       36307 :                 for(node *n = l->h; n; n = n->next)
     468       24343 :                         rel_used(n->data);
     469             :         } else if (is_topn(rel->op) || is_select(rel->op) || is_sample(rel->op)) {
     470       20626 :                 rel_used(rel->l);
     471       20637 :                 rel = rel->l;
     472             :         } else if (is_ddl(rel->op)) {
     473      399950 :                 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       34473 :                         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        1090 :                 if (IS_TABLE_PROD_FUNC(rel->flag) || rel->flag == TABLE_FROM_RELATION)
     483        1090 :                         rel_used(rel->l);
     484        1090 :                 exp_used(rel->r);
     485             :         }
     486      854437 :         if (rel && rel->exps) {
     487      742274 :                 exps_used(rel->exps);
     488      742516 :                 if (rel->r && (is_simple_project(rel->op) || is_groupby(rel->op)))
     489       19514 :                         exps_used(rel->r);
     490             :         }
     491             : }
     492             : 
     493             : static void
     494     1260382 : rel_mark_used(mvc *sql, sql_rel *rel, int proj)
     495             : {
     496     1873425 :         if (proj && (need_distinct(rel)))
     497       10169 :                 rel_used(rel);
     498             : 
     499     1873403 :         switch(rel->op) {
     500             :         case op_basetable:
     501             :         case op_truncate:
     502             :         case op_insert:
     503             :                 break;
     504             : 
     505       13410 :         case op_table:
     506             : 
     507       13410 :                 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       16614 :         case op_topn:
     516             :         case op_sample:
     517       16614 :                 if (proj) {
     518       16531 :                         rel = rel ->l;
     519       16531 :                         rel_mark_used(sql, rel, proj);
     520       16531 :                         break;
     521             :                 }
     522             :                 /* fall through */
     523             :         case op_project:
     524             :         case op_groupby:
     525      512766 :                 if (proj && rel->l) {
     526      293345 :                         rel_exps_mark_used(sql->sa, rel, rel->l);
     527      293357 :                         rel_mark_used(sql, rel->l, 0);
     528       10976 :                 } else if (proj) {
     529       10893 :                         rel_exps_mark_used(sql->sa, rel, NULL);
     530             :                 }
     531             :                 break;
     532        7658 :         case op_update:
     533             :         case op_delete:
     534        7658 :                 if (proj && rel->r) {
     535        6114 :                         sql_rel *r = rel->r;
     536             : 
     537        6114 :                         if (!list_empty(r->exps)) {
     538        7215 :                                 for (node *n = r->exps->h; n; n = n->next) {
     539        6115 :                                         sql_exp *e = n->data;
     540        6115 :                                         const char *nname = exp_name(e);
     541             : 
     542        6115 :                                         if (nname && nname[0] == '%' && strcmp(nname, TID) == 0) { /* TID is used */
     543        5014 :                                                 e->used = 1;
     544        5014 :                                                 break;
     545             :                                         }
     546             :                                 }
     547             :                         }
     548        6114 :                         rel_exps_mark_used(sql->sa, rel, rel->r);
     549        6114 :                         rel_mark_used(sql, rel->r, 0);
     550             :                 }
     551             :                 break;
     552             : 
     553      398592 :         case op_ddl:
     554      398592 :                 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       33115 :                         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      110147 :         case op_select:
     566      110147 :                 if (rel->l) {
     567      110147 :                         rel_exps_mark_used(sql->sa, rel, rel->l);
     568      110147 :                         rel_mark_used(sql, rel->l, 0);
     569             :                 }
     570             :                 break;
     571             : 
     572        5146 :         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        5146 :                 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         483 :                 } else if (proj && !need_distinct(rel)) {
     591         483 :                         sql_rel *l = rel->l;
     592             : 
     593         483 :                         positional_exps_mark_used(rel, l);
     594         483 :                         rel_exps_mark_used(sql->sa, rel, l);
     595         483 :                         rel_mark_used(sql, rel->l, 0);
     596             :                         /* based on child check set expression list */
     597         483 :                         if (is_project(l->op) && need_distinct(l))
     598           0 :                                 positional_exps_mark_used(l, rel);
     599         483 :                         positional_exps_mark_used(rel, rel->r);
     600         483 :                         rel_exps_mark_used(sql->sa, rel, rel->r);
     601         483 :                         rel_mark_used(sql, rel->r, 0);
     602             :                 }
     603             :                 break;
     604             : 
     605       48015 :         case op_munion:
     606       48015 :                 assert(rel->l);
     607             :                 // TODO: here we blindly follow the same logic as op_union. RE-evaluate
     608       48015 :                 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       21377 :                 } else if (proj && !need_distinct(rel)) {
     617       21377 :                         bool first = true;
     618       64458 :                         for (node *n = ((list*)rel->l)->h; n; n = n->next) {
     619       43081 :                                 sql_rel *l = n->data;
     620             : 
     621       43081 :                                 positional_exps_mark_used(rel, l);
     622       43081 :                                 rel_exps_mark_used(sql->sa, rel, l);
     623       43081 :                                 rel_mark_used(sql, l, 0);
     624             :                                 /* based on child check set expression list */
     625       43081 :                                 if (first && is_project(l->op) && need_distinct(l))
     626           0 :                                         positional_exps_mark_used(l, rel);
     627       43081 :                                 first = false;
     628             :                         }
     629             :                 }
     630             :                 break;
     631      150930 :         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      150930 :                 rel_exps_mark_used(sql->sa, rel, rel->l);
     639      150930 :                 rel_exps_mark_used(sql->sa, rel, rel->r);
     640      150930 :                 rel_mark_used(sql, rel->l, 0);
     641      150930 :                 rel_mark_used(sql, rel->r, 0);
     642      150930 :                 break;
     643             :         }
     644     1260372 : }
     645             : 
     646             : static sql_rel * rel_dce_sub(mvc *sql, sql_rel *rel);
     647             : 
     648             : static sql_rel *
     649     1119566 : rel_remove_unused(mvc *sql, sql_rel *rel)
     650             : {
     651     1119566 :         int needed = 0;
     652             : 
     653     1119566 :         if (!rel)
     654             :                 return rel;
     655             : 
     656     1112708 :         switch(rel->op) {
     657      302001 :         case op_basetable: {
     658      302001 :                 sql_table *t = rel->l;
     659             : 
     660      302001 :                 if (t && isReplicaTable(t)) /* TODO fix rewriting in rel_distribute.c */
     661             :                         return rel;
     662             :         }
     663             :         /* fall through */
     664             :         case op_table:
     665      308842 :                 if (rel->exps && (rel->op != op_table || !IS_TABLE_PROD_FUNC(rel->flag))) {
     666     1344565 :                         for(node *n=rel->exps->h; n && !needed; n = n->next) {
     667     1042581 :                                 sql_exp *e = n->data;
     668             : 
     669     1042581 :                                 if (!e->used)
     670      226778 :                                         needed = 1;
     671             :                         }
     672             : 
     673      301984 :                         if (!needed)
     674             :                                 return rel;
     675             : 
     676     1339296 :                         for(node *n=rel->exps->h; n;) {
     677     1112529 :                                 node *next = n->next;
     678     1112529 :                                 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     1112529 :                                 if (!e->used && list_length(rel->exps) > 1)
     682      374828 :                                         list_remove_node(rel->exps, NULL, n);
     683             :                                 n = next;
     684             :                         }
     685             :                 }
     686      233625 :                 if (rel->op == op_table && (IS_TABLE_PROD_FUNC(rel->flag) || rel->flag == TABLE_FROM_RELATION))
     687        6858 :                         rel->l = rel_remove_unused(sql, rel->l);
     688             :                 return rel;
     689             : 
     690       16531 :         case op_topn:
     691             :         case op_sample:
     692             : 
     693       16531 :                 if (rel->l)
     694       16531 :                         rel->l = rel_remove_unused(sql, rel->l);
     695             :                 return rel;
     696             : 
     697      304249 :         case op_project:
     698             :         case op_groupby:
     699             : 
     700      304249 :                 if (/*rel->l &&*/ rel->exps) {
     701     1904790 :                         for(node *n=rel->exps->h; n && !needed; n = n->next) {
     702     1600541 :                                 sql_exp *e = n->data;
     703             : 
     704     1600541 :                                 if (!e->used)
     705       25455 :                                         needed = 1;
     706             :                         }
     707      304249 :                         if (!needed)
     708             :                                 return rel;
     709             : 
     710      589157 :                         for(node *n=rel->exps->h; n;) {
     711      563702 :                                 node *next = n->next;
     712      563702 :                                 sql_exp *e = n->data;
     713             : 
     714             :                                 /* at least one (needed for crossproducts, count(*), rank() and single value projections) */
     715      563702 :                                 if (!e->used && list_length(rel->exps) > 1)
     716      427955 :                                         list_remove_node(rel->exps, NULL, n);
     717             :                                 n = next;
     718             :                         }
     719             :                 }
     720             :                 return rel;
     721             : 
     722        3149 :         case op_join:
     723             :         case op_left:
     724             :         case op_right:
     725             :         case op_full:
     726        3149 :                 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      398276 :         case op_ddl:
     764      398276 :                 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       32843 :                         if (rel->l)
     766       32483 :                                 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     1307385 : rel_dce_refs(mvc *sql, sql_rel *rel, list *refs)
     780             : {
     781     1307385 :         if (!rel || (rel_is_ref(rel) && list_find(refs, rel, NULL)))
     782       14171 :                 return ;
     783             : 
     784     1293214 :         switch(rel->op) {
     785      400919 :         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      400919 :                 if (rel->l && (rel->op != op_table || rel->flag != TRIGGER_WRAPPER))
     793      384936 :                         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        6464 :         case op_update:
     802             :         case op_delete:
     803             : 
     804        6464 :                 if (rel->r)
     805        6114 :                         rel_dce_refs(sql, rel->r, refs);
     806             :                 break;
     807             : 
     808      144344 :         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      144344 :                 if (rel->l)
     820      144344 :                         rel_dce_refs(sql, rel->l, refs);
     821      144344 :                 if (rel->r)
     822      144344 :                         rel_dce_refs(sql, rel->r, refs);
     823             :                 break;
     824       22645 :         case op_munion:
     825       22645 :                 assert(rel->l);
     826       68555 :                 for (node *n = ((list*)rel->l)->h; n; n = n->next)
     827       45910 :                         rel_dce_refs(sql, n->data, refs);
     828             :                 break;
     829      399692 :         case op_ddl:
     830             : 
     831      399692 :                 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       34215 :                         if (rel->l)
     833       33855 :                                 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     1293220 :         if (rel_is_ref(rel) && !list_find(refs, rel, NULL))
     843       16590 :                 list_prepend(refs, rel);
     844             : }
     845             : 
     846             : static sql_rel *
     847     1862768 : rel_dce_down(mvc *sql, sql_rel *rel, int skip_proj)
     848             : {
     849     1862768 :         if (!rel)
     850             :                 return rel;
     851             : 
     852     1862768 :         if (!skip_proj && rel_is_ref(rel))
     853             :                 return rel;
     854             : 
     855     1832546 :         switch(rel->op) {
     856      552581 :         case op_basetable:
     857             :         case op_table:
     858             : 
     859      552581 :                 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      276045 :                 if (!skip_proj)
     862      276045 :                         rel_dce_sub(sql, rel);
     863             :                 /* fall through */
     864             : 
     865             :         case op_truncate:
     866             :                 return rel;
     867             : 
     868        7461 :         case op_insert:
     869        7461 :                 rel_used(rel->r);
     870        7461 :                 rel_dce_sub(sql, rel->r);
     871        7461 :                 return rel;
     872             : 
     873        7658 :         case op_update:
     874             :         case op_delete:
     875             : 
     876        7658 :                 if (skip_proj && rel->r)
     877        6114 :                         rel->r = rel_dce_down(sql, rel->r, 0);
     878        1194 :                 if (!skip_proj)
     879        1194 :                         rel_dce_sub(sql, rel);
     880             :                 return rel;
     881             : 
     882      524746 :         case op_topn:
     883             :         case op_sample:
     884             :         case op_project:
     885             :         case op_groupby:
     886             : 
     887      524746 :                 if (skip_proj && rel->l)
     888      309845 :                         rel->l = rel_dce_down(sql, rel->l, is_topn(rel->op) || is_sample(rel->op));
     889      204030 :                 if (!skip_proj)
     890      204030 :                         rel_dce_sub(sql, rel);
     891             :                 return rel;
     892             : 
     893        5139 :         case op_union:
     894             :         case op_inter:
     895             :         case op_except:
     896        5139 :                 if (skip_proj) {
     897        2817 :                         if (rel->l)
     898        2817 :                                 rel->l = rel_dce_down(sql, rel->l, 0);
     899        2817 :                         if (rel->r)
     900        2817 :                                 rel->r = rel_dce_down(sql, rel->r, 0);
     901             :                 }
     902        2322 :                 if (!skip_proj)
     903        2322 :                         rel_dce_sub(sql, rel);
     904             :                 return rel;
     905             : 
     906       45047 :         case op_munion:
     907       45047 :                 if (skip_proj) {
     908       70396 :                         for (node *n = ((list*)rel->l)->h; n; n = n->next)
     909       47041 :                                 n->data = rel_dce_down(sql, n->data, 0);
     910             :                 }
     911       21692 :                 if (!skip_proj)
     912       21692 :                         rel_dce_sub(sql, rel);
     913             :                 return rel;
     914      102596 :         case op_select:
     915      102596 :                 if (rel->l)
     916      102596 :                         rel->l = rel_dce_down(sql, rel->l, 0);
     917             :                 return rel;
     918             : 
     919      147467 :         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      147467 :                 if (rel->l)
     927      147467 :                         rel->l = rel_dce_down(sql, rel->l, 0);
     928      147467 :                 if (rel->r)
     929      147467 :                         rel->r = rel_dce_down(sql, rel->r, 0);
     930      147467 :                 if (!skip_proj && !list_empty(rel->attr))
     931        3149 :                         rel_dce_sub(sql, rel);
     932             :                 return rel;
     933             : 
     934      398398 :         case op_ddl:
     935      398398 :                 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       32921 :                         if (rel->l)
     937       32755 :                                 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     1063330 : rel_dce_sub(mvc *sql, sql_rel *rel)
     957             : {
     958     1063330 :         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     1063330 :         rel_mark_used(sql, rel, 1);
     967     1063250 :         rel = rel_remove_unused(sql, rel);
     968     1063287 :         rel_dce_down(sql, rel, 1);
     969     1063287 :         return rel;
     970             : }
     971             : 
     972             : /* add projects under set ops */
     973             : static sql_rel *
     974     2077938 : rel_add_projects(mvc *sql, sql_rel *rel)
     975             : {
     976     2077938 :         if (!rel)
     977             :                 return rel;
     978             : 
     979     2077938 :         switch(rel->op) {
     980             :         case op_basetable:
     981             :         case op_truncate:
     982             :                 return rel;
     983       13925 :         case op_insert:
     984             :         case op_update:
     985             :         case op_delete:
     986       13925 :                 if (rel->r)
     987       13575 :                         rel->r = rel_add_projects(sql, rel->r);
     988             :                 return rel;
     989        2913 :         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        2913 :                 if (rel->l) {
     996        2913 :                         sql_rel *l = rel->l;
     997             : 
     998        2913 :                         if (!is_project(l->op) && !need_distinct(rel))
     999           1 :                                 l = rel_project(sql->sa, l, rel_projections(sql, l, NULL, 1, 1));
    1000        2913 :                         rel->l = rel_add_projects(sql, l);
    1001             :                 }
    1002        2913 :                 if (rel->r) {
    1003        2913 :                         sql_rel *r = rel->r;
    1004             : 
    1005        2913 :                         if (!is_project(r->op) && !need_distinct(rel))
    1006           1 :                                 r = rel_project(sql->sa, r, rel_projections(sql, r, NULL, 1, 1));
    1007        2913 :                         rel->r = rel_add_projects(sql, r);
    1008             :                 }
    1009             :                 return rel;
    1010       33856 :         case op_munion:
    1011       33856 :                 assert(rel->l);
    1012      151303 :                 for (node *n = ((list*)rel->l)->h; n; n = n->next) {
    1013      117447 :                         sql_rel* r = n->data;
    1014      117447 :                         if (!is_project(r->op) && !need_distinct(rel))
    1015          23 :                                 r = rel_project(sql->sa, r, rel_projections(sql, r, NULL, 1, 1));
    1016      117447 :                         r = rel_add_projects(sql, r);
    1017      117447 :                         n->data = r;
    1018             :                 }
    1019             :                 return rel;
    1020      788939 :         case op_topn:
    1021             :         case op_sample:
    1022             :         case op_project:
    1023             :         case op_groupby:
    1024             :         case op_select:
    1025             :         case op_table:
    1026      788939 :                 if (rel->l && (rel->op != op_table || rel->flag != TRIGGER_WRAPPER))
    1027      758567 :                         rel->l = rel_add_projects(sql, rel->l);
    1028             :                 return rel;
    1029      300260 :         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      300260 :                 if (rel->l)
    1037      300260 :                         rel->l = rel_add_projects(sql, rel->l);
    1038      300260 :                 if (rel->r)
    1039      300260 :                         rel->r = rel_add_projects(sql, rel->r);
    1040             :                 return rel;
    1041      399960 :         case op_ddl:
    1042      399960 :                 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       34483 :                         if (rel->l)
    1044       34123 :                                 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      547366 : rel_dce_(mvc *sql, sql_rel *rel)
    1058             : {
    1059      547366 :         list *refs = sa_list(sql->sa);
    1060             : 
    1061      547464 :         rel_dce_refs(sql, rel, refs);
    1062      547385 :         if (refs) {
    1063      563949 :                 for(node *n = refs->h; n; n = n->next) {
    1064       16590 :                         sql_rel *i = n->data;
    1065             : 
    1066       16590 :                         while (!rel_is_ref(i) && i->l && !is_base(i->op))
    1067             :                                 i = i->l;
    1068       16590 :                         if (i)
    1069       16590 :                                 rel_used(i);
    1070             :                 }
    1071             :         }
    1072      547359 :         rel = rel_add_projects(sql, rel);
    1073      547462 :         rel_used(rel);
    1074      547785 :         rel_dce_sub(sql, rel);
    1075      547353 :         return rel;
    1076             : }
    1077             : 
    1078             : 
    1079             : /* Remove unused expressions */
    1080             : static sql_rel *
    1081      547430 : rel_dce(visitor *v, global_props *gp, sql_rel *rel)
    1082             : {
    1083      547430 :         (void) gp;
    1084      547430 :         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      623234 : bind_dce(visitor *v, global_props *gp)
    1096             : {
    1097      623234 :         int flag = v->sql->sql_optimizer;
    1098      623234 :         return gp->opt_cycle == 0 && gp->opt_level == 1 && (flag & dce) ? rel_dce : NULL;
    1099             : }
    1100             : 
    1101             : 
    1102             : static int
    1103       84226 : topn_sample_safe_exps( list *exps, bool nil_limit )
    1104             : {
    1105             :         /* Limit only expression lists are always save */
    1106       84226 :         if (list_length(exps) == 1)
    1107             :                 return 1;
    1108        1352 :         for (node *n = exps->h; n; n = n->next ) {
    1109         917 :                 sql_exp *e = n->data;
    1110             : 
    1111         917 :                 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      137045 : rel_push_topn_and_sample_down_(visitor *v, sql_rel *rel)
    1150             : {
    1151      137045 :         sql_rel *rp = NULL, *r = rel->l, *rpp = NULL;
    1152             : 
    1153      137045 :         if (is_topn(rel->op) && !rel_is_ref(rel) &&
    1154       50158 :                         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      137045 :         if ((is_topn(rel->op) || is_sample(rel->op)) && topn_sample_safe_exps(rel->exps, true)) {
    1181       50255 :                 sql_rel *(*func) (allocator *, sql_rel *, list *) = is_topn(rel->op) ? rel_topn : rel_sample;
    1182             : 
    1183             :                 /* nested topN relations */
    1184       50255 :                 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       50255 :                 if (r && is_simple_project(r->op) && need_distinct(r))
    1228             :                         return rel;
    1229             : 
    1230             :                 /* push topn/sample under projections */
    1231       50250 :                 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       32531 :                         while (is_simple_project(x->op) && !need_distinct(x) && !rel_is_ref(x) && x->l && list_empty(x->r)) {
    1235       16277 :                                 px = x;
    1236       16277 :                                 x = x->l;
    1237             :                         }
    1238             :                         /* only push topn once */
    1239       16251 :                         if (x && x->op == rel->op)
    1240             :                                 return rel;
    1241             : 
    1242       16245 :                         rel->l = x;
    1243       16245 :                         px->l = rel;
    1244       16245 :                         rel = r;
    1245       16245 :                         v->changes++;
    1246       16245 :                         return rel;
    1247             :                 }
    1248             : 
    1249       33998 :                 if (!topn_sample_safe_exps(rel->exps, false))
    1250             :                         return rel;
    1251             : 
    1252             :                 /* duplicate topn/sample direct under union or crossproduct */
    1253       33930 :                 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       33780 :                         rp = r->l;
    1287       33780 :                 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       33782 :                 if (r && !rp)
    1372          66 :                         rp = r->l;
    1373       33782 :                 if (r && rp && is_simple_project(rp->op) && !rp->r && rp->l)
    1374       33782 :                         rpp = rp->l;
    1375       33782 :                 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       33510 : rel_push_topn_and_sample_down(visitor *v, global_props *gp, sql_rel *rel)
    1420             : {
    1421       33510 :         (void) gp;
    1422       33510 :         return rel_visitor_topdown(v, rel, &rel_push_topn_and_sample_down_);
    1423             : }
    1424             : 
    1425             : run_optimizer
    1426      623283 : bind_push_topn_and_sample_down(visitor *v, global_props *gp)
    1427             : {
    1428      623283 :         int flag = v->sql->sql_optimizer;
    1429      623056 :         return gp->opt_level == 1 && (gp->cnt[op_topn] || gp->cnt[op_sample]) &&
    1430      656632 :                    (flag & push_topn_and_sample_down) ? rel_push_topn_and_sample_down : NULL;
    1431             : }

Generated by: LCOV version 1.14