LCOV - code coverage report
Current view: top level - monetdb5/optimizer - opt_postfix.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 116 126 92.1 %
Date: 2024-12-19 20:05:57 Functions: 1 1 100.0 %

          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             : /* The SQL code generator can not always look ahead to avoid
      14             :  * generation of intermediates.
      15             :  * Some of these patterns are captured in a postfix optimalisation.
      16             :  */
      17             : #include "monetdb_config.h"
      18             : #include "mal_instruction.h"
      19             : #include "opt_postfix.h"
      20             : 
      21             : #define isCandidateList(M,P,I) ((M)->var[getArg(P,I)].id[0]== 'C')
      22             : str
      23      483983 : OPTpostfixImplementation(Client cntxt, MalBlkPtr mb, MalStkPtr stk,
      24             :                                                  InstrPtr pci)
      25             : {
      26      483983 :         int i, slimit, actions = 0;
      27      483983 :         str msg = MAL_SUCCEED;
      28      483983 :         InstrPtr p;
      29             : 
      30      483983 :         (void) cntxt;
      31      483983 :         (void) stk;
      32             : 
      33      483983 :         slimit = mb->stop;
      34      483983 :         setVariableScope(mb);
      35             :         /* Remove the result from any join/group instruction when it is not used later on */
      36    25465732 :         for (i = 0; i < slimit; i++) {
      37             : /* POSTFIX ACTION FOR THE JOIN CASE  */
      38    24497767 :                 p = getInstrPtr(mb, i);
      39    24497767 :                 if (getModuleId(p) == algebraRef && p->retc == 2) {
      40      272466 :                         if (getFunctionId(p) == leftjoinRef ||  /*getFunctionId(p) == outerjoinRef || */
      41      271818 :                                 getFunctionId(p) == bandjoinRef
      42      271818 :                                 || getFunctionId(p) == rangejoinRef
      43      271686 :                                 || getFunctionId(p) == likejoinRef) {
      44         825 :                                 if (getVarEolife(mb, getArg(p, p->retc - 1)) == i) {
      45          45 :                                         delArgument(p, p->retc - 1);
      46          45 :                                         typeChecker(cntxt->usermodule, mb, p, i, TRUE);
      47          45 :                                         actions++;
      48          45 :                                         continue;
      49             :                                 }
      50      271641 :                         } else if (getFunctionId(p) == semijoinRef
      51      271372 :                                            || getFunctionId(p) == joinRef
      52       54800 :                                            || getFunctionId(p) == thetajoinRef
      53       37959 :                                            || /*getFunctionId(p) == outerjoinRef || */
      54       37959 :                                            getFunctionId(p) == crossRef) {
      55      260483 :                                 int is_first_ret_not_used = getVarEolife(mb, getArg(p, p->retc - 2)) == i;
      56      260483 :                                 int is_second_ret_not_used = getVarEolife(mb, getArg(p, p->retc - 1)) == i;
      57             : 
      58      260483 :                                 if (getFunctionId(p) == semijoinRef
      59         269 :                                         &&
      60             :                                         ((is_first_ret_not_used
      61          58 :                                           && getVarConstant(mb, getArg(p, 7)).val.btval != 1 /*not single */ )
      62         269 :                                          || is_second_ret_not_used)) {
      63             :                                         /* Can't swap arguments on single semijoins */
      64          89 :                                         if (is_first_ret_not_used) {
      65             :                                                 /* semijoin with just the right output is a join */
      66           0 :                                                 getArg(p, 2) ^= getArg(p, 3);   /* swap join inputs */
      67           0 :                                                 getArg(p, 3) ^= getArg(p, 2);
      68           0 :                                                 getArg(p, 2) ^= getArg(p, 3);
      69             : 
      70           0 :                                                 getArg(p, 4) ^= getArg(p, 5);   /* swap candidate lists */
      71           0 :                                                 getArg(p, 5) ^= getArg(p, 4);
      72           0 :                                                 getArg(p, 4) ^= getArg(p, 5);
      73           0 :                                                 setFunctionId(p, joinRef);
      74           0 :                                                 delArgument(p, 7);      /* delete 'max_one' argument */
      75             :                                         } else {
      76             :                                                 /* semijoin with just the left output is an intersection */
      77          89 :                                                 setFunctionId(p, intersectRef);
      78             :                                         }
      79             : 
      80         178 :                                         delArgument(p,
      81          89 :                                                                 is_second_ret_not_used ? p->retc - 1 : p->retc -
      82             :                                                                 2);
      83          89 :                                         typeChecker(cntxt->usermodule, mb, p, i, TRUE);
      84          89 :                                         actions++;
      85          89 :                                         continue;
      86      260394 :                                 } else if (is_second_ret_not_used) {
      87       27172 :                                         delArgument(p, p->retc - 1);
      88       27172 :                                         typeChecker(cntxt->usermodule, mb, p, i, TRUE);
      89       27172 :                                         actions++;
      90       27172 :                                         continue;
      91      233222 :                                 } else if (is_first_ret_not_used &&
      92       13367 :                                                    (getFunctionId(p) == joinRef
      93        5565 :                                                         || (getFunctionId(p) == thetajoinRef
      94        5232 :                                                                 && isVarConstant(mb, getArg(p, 6)))
      95         333 :                                                         || (getFunctionId(p) == crossRef
      96         275 :                                                                 && getVarConstant(mb, getArg(p, 4)).val.btval != 1 /*not single */ ))) {
      97             :                                         /* Can't swap arguments on single cross products */
      98             :                                         /* swap join inputs */
      99       13305 :                                         getArg(p, 2) ^= getArg(p, 3);
     100       13305 :                                         getArg(p, 3) ^= getArg(p, 2);
     101       13305 :                                         getArg(p, 2) ^= getArg(p, 3);
     102             : 
     103       13305 :                                         if (getFunctionId(p) != crossRef) {     /* swap candidate lists */
     104       13034 :                                                 getArg(p, 4) ^= getArg(p, 5);
     105       13034 :                                                 getArg(p, 5) ^= getArg(p, 4);
     106       13034 :                                                 getArg(p, 4) ^= getArg(p, 5);
     107       13034 :                                                 if (getFunctionId(p) == thetajoinRef) { /* swap the comparison */
     108        5232 :                                                         ValRecord *x = &getVarConstant(mb, getArg(p, 6)),
     109        5232 :                                                                 cst = {
     110             :                                                                         .vtype = TYPE_int
     111             :                                                                 };
     112        5232 :                                                         switch (x->val.ival) {
     113        5184 :                                                         case JOIN_LT:
     114        5184 :                                                                 cst.val.ival = JOIN_GT;
     115        5184 :                                                                 break;
     116           9 :                                                         case JOIN_LE:
     117           9 :                                                                 cst.val.ival = JOIN_GE;
     118           9 :                                                                 break;
     119          35 :                                                         case JOIN_GT:
     120          35 :                                                                 cst.val.ival = JOIN_LT;
     121          35 :                                                                 break;
     122           4 :                                                         case JOIN_GE:
     123           4 :                                                                 cst.val.ival = JOIN_LE;
     124           4 :                                                                 break;
     125           0 :                                                         default:
     126           0 :                                                                 cst.val.ival = x->val.ival;
     127             :                                                         }
     128        5232 :                                                         setArg(p, 6, defConstant(mb, TYPE_int, &cst));
     129             :                                                 }
     130             :                                         }
     131       13305 :                                         delArgument(p, p->retc - 2);
     132       13305 :                                         typeChecker(cntxt->usermodule, mb, p, i, TRUE);
     133       13305 :                                         actions++;
     134       13305 :                                         continue;
     135             :                                 }
     136             :                         }
     137             :                 }
     138             : /* POSTFIX ACTION FOR THE EXTENT CASE  */
     139    24457156 :                 if (getModuleId(p) == groupRef && getFunctionId(p) == groupRef
     140       12615 :                         && getVarEolife(mb, getArg(p, p->retc - 1)) == i) {
     141       12573 :                         delArgument(p, p->retc - 1);
     142       12573 :                         typeChecker(cntxt->usermodule, mb, p, i, TRUE);
     143       12573 :                         actions++;
     144       12573 :                         continue;
     145             :                 }
     146    24444583 :                 if (getModuleId(p) == groupRef && getFunctionId(p) == subgroupRef
     147        3807 :                         && getVarEolife(mb, getArg(p, p->retc - 1)) == i) {
     148        3807 :                         delArgument(p, p->retc - 1);
     149        3807 :                         typeChecker(cntxt->usermodule, mb, p, i, TRUE);
     150        3807 :                         actions++;
     151        3807 :                         continue;
     152             :                 }
     153    24440776 :                 if (getModuleId(p) == groupRef && getFunctionId(p) == subgroupdoneRef
     154       12788 :                         && getVarEolife(mb, getArg(p, p->retc - 1)) == i) {
     155       12475 :                         delArgument(p, p->retc - 1);
     156       12475 :                         typeChecker(cntxt->usermodule, mb, p, i, TRUE);
     157       12475 :                         actions++;
     158       12475 :                         continue;
     159             :                 }
     160    24428301 :                 if (getModuleId(p) == groupRef && getFunctionId(p) == groupdoneRef
     161       29146 :                         && getVarEolife(mb, getArg(p, p->retc - 1)) == i) {
     162       19690 :                         delArgument(p, p->retc - 1);
     163       19690 :                         typeChecker(cntxt->usermodule, mb, p, i, TRUE);
     164       19690 :                         actions++;
     165       19690 :                         continue;
     166             :                 }
     167             : /* POSTFIX ACTION FOR SORT, could be dropping the last two */
     168    24408611 :                 if (getModuleId(p) == algebraRef && getFunctionId(p) == sortRef
     169       23917 :                         && getVarEolife(mb, getArg(p, p->retc - 1)) == i) {
     170       10255 :                         delArgument(p, p->retc - 1);
     171       10255 :                         typeChecker(cntxt->usermodule, mb, p, i, TRUE);
     172       10255 :                         actions++;
     173       10255 :                         if (getModuleId(p) == algebraRef && getFunctionId(p) == sortRef
     174       10255 :                                 && getVarEolife(mb, getArg(p, p->retc - 1)) == i) {
     175        3542 :                                 delArgument(p, p->retc - 1);
     176        3542 :                                 typeChecker(cntxt->usermodule, mb, p, i, TRUE);
     177        3542 :                                 actions++;
     178             :                         }
     179       10255 :                         continue;
     180             :                 }
     181             :         }
     182             :         /* Defense line against incorrect plans */
     183      483982 :         if (actions) {
     184             :                 // msg = chkTypes(cntxt->usermodule, mb, FALSE);
     185             :                 // if (!msg)
     186             :                 //      msg = chkFlow(mb);
     187             :                 // if (!msg)
     188             :                 //      msg = chkDeclarations(mb);
     189      483982 :         }
     190             :         /* keep actions taken as a fake argument */
     191      483982 :         (void) pushInt(mb, pci, actions);
     192      483979 :         return msg;
     193             : }

Generated by: LCOV version 1.14