LCOV - code coverage report
Current view: top level - monetdb5/optimizer - opt_costModel.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 59 59 100.0 %
Date: 2021-10-13 02:24:04 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /*
       2             :  * This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       5             :  *
       6             :  * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
       7             :  */
       8             : 
       9             : #include "monetdb_config.h"
      10             : #include "opt_costModel.h"
      11             : 
      12             : /*
      13             :  * The cost formula are repetative
      14             :  */
      15             : #define newRows(W,X,Y,Z) {\
      16             :                 c1 = getRowCnt(mb, getArg(p,W));\
      17             :                 c2 = getRowCnt(mb, getArg(p,X));\
      18             :                 /* just to ensure that rowcnt was/is never set to -1 */\
      19             :                 if (c1 == (BUN) -1 || c2 == (BUN) -1 || c1 == BUN_NONE || c2 == BUN_NONE) \
      20             :                         continue;\
      21             :                 setRowCnt(mb, getArg(p,Z), (Y));\
      22             : }
      23             : /*
      24             :  * The cost will be used in many places to make decisions.
      25             :  * Access should be fast.
      26             :  * The SQL front-end also makes the BAT index available as the
      27             :  * property bid. This can be used to access the BAT and involve
      28             :  * more properties into the decision procedure.
      29             :  * [to be done]
      30             :  * Also make sure you don't re-use variables, because then the
      31             :  * row count becomes non-deterministic.
      32             :  */
      33             : str
      34      353097 : OPTcostModelImplementation(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
      35             : {
      36             :         int i;
      37             :         BUN c1, c2;
      38             :         InstrPtr p;
      39             :         str msg = MAL_SUCCEED;
      40             : 
      41             :         (void) cntxt;
      42             :         (void) stk;
      43             : 
      44      353097 :         if ( mb->inlineProp )
      45             :                 return MAL_SUCCEED;
      46             : 
      47    22782088 :         for (i = 0; i < mb->stop; i++) {
      48    22428991 :                 p = getInstrPtr(mb, i);
      49    22428991 :                 if (getModuleId(p)==algebraRef) {
      50     4017009 :                          if (getFunctionId(p) == selectRef ||
      51     3978961 :                                 getFunctionId(p) == thetaselectRef) {
      52      218742 :                                 newRows(1,2, (c1 > 2 ? c2 / 2 +1: c1/2+1),0);
      53     3798267 :                         } else if (
      54     3798267 :                                 getFunctionId(p) == selectNotNilRef  ||
      55     3798267 :                                 getFunctionId(p) == sortRef  ||
      56     3765631 :                                 getFunctionId(p) == projectRef  ){
      57      147315 :                                 newRows(1,1,c1,0);
      58     3650952 :                         } else if (getFunctionId(p) == joinRef ||
      59     3538264 :                                 getFunctionId(p) == projectionRef ||
      60       71865 :                                 getFunctionId(p) == bandjoinRef ||
      61       71865 :                                 getFunctionId(p) == projectionpathRef ) {
      62             :                                 /* assume 1-1 joins */
      63     3579087 :                                 newRows(1,2,(c1 < c2 ? c1 : c2),0);
      64             :                         } else
      65       71865 :                         if (getFunctionId(p) == crossRef) {
      66       13612 :                                 newRows(1,2,((log((double) c1) + log((double) c2) > log(INT_MAX) ? INT_MAX : c1 * c2 +1)),0);
      67             :                                 /* log sets errno if it cannot compute the log. This will then screw with code that checks errno */
      68       13612 :                                 if (errno == ERANGE || errno == EDOM) {
      69       13612 :                                         errno = 0;
      70             :                                 }
      71             :                         }
      72    18411982 :                 } else if (getModuleId(p) == batcalcRef) {
      73      221061 :                         if( getFunctionId(p) == ifthenelseRef) {
      74        3659 :                                 if( isaBatType(getArgType(mb,p,2) ) ) {
      75          21 :                                         newRows(2,2, c1,0);
      76             :                                 } else {
      77        3638 :                                         newRows(3,3, c1,0);
      78             :                                 }
      79      217402 :                         } else if( isaBatType(getArgType(mb,p,1)) ){
      80      216069 :                                         newRows(1,1, c1,0);
      81             :                                 } else {
      82        1333 :                                         newRows(2, 2, c2,0);
      83             :                                 }
      84    18190921 :                 } else if (getModuleId(p) == batstrRef) {
      85        4714 :                                 newRows(1,1, c1,0);
      86    18186207 :                 } else if (getModuleId(p) == batRef) {
      87     1230334 :                         if (getFunctionId(p) == appendRef ){
      88             :                                 /*
      89             :                                  * Updates are a little more complicated, because you have to
      90             :                                  * propagate changes in the expected size up the expression tree.
      91             :                                  * For example, the SQL snippet:
      92             :                                  *       _49:bat[:oid,:oid]{rows=0,bid=622}  := sql.bind_dbat("sys","example",3);
      93             :                                  *       _54 := bat.setWriteMode(_49);
      94             :                                  *       bat.append(_54,_47,true);
      95             :                                  * shows what is produced when it encounters a deletion. If a non-empty
      96             :                                  * append is not properly passed back to _49, the emptySet
      97             :                                  * optimizer might remove the complete deletion code.
      98             :                                  * The same holds for replacement operations, which add information to
      99             :                                  * an initially empty insertion BAT.
     100             :                                  */
     101      539001 :                                 if( isaBatType(getArgType(mb,p,2)) ){
     102             :                                         /* insert BAT */
     103      537199 :                                         newRows(1,2, (c1 + c2+1),1);
     104             :                                 } else {
     105             :                                         /* insert scalars */
     106        1802 :                                         newRows(1,1, (c1 +1),1);
     107             :                                 }
     108      691333 :                         } else if (getFunctionId(p) == deleteRef){
     109           6 :                                 if( isaBatType(getArgType(mb,p,2)) ){
     110             :                                         /* delete BAT */
     111           2 :                                         newRows(1, 2, (c2 >= c1 ? 1 : c1 - c2), 1);
     112             :                                 } else {
     113             :                                         /* insert scalars */
     114           4 :                                         newRows(1, 1, (c1 <= 1 ? 1 : c1 - 1), 1);
     115             :                                 }
     116             :                         }
     117    16955873 :                 } else if (getModuleId(p)==groupRef) {
     118       32876 :                         if (getFunctionId(p) ==subgroupRef || getFunctionId(p) ==groupRef ) {
     119        9869 :                                 newRows(1,1,( c1 / 10+1),0);
     120             :                         } else {
     121       23007 :                                 newRows(1,1, c1,0);
     122             :                         }
     123    16922997 :                 } else if (getModuleId(p)== aggrRef) {
     124       48920 :                         if (getFunctionId(p) == sumRef ||
     125       44329 :                                 getFunctionId(p) == minRef ||
     126       44086 :                                 getFunctionId(p) == maxRef ||
     127       43907 :                                 getFunctionId(p) == avgRef) {
     128        6124 :                                 newRows(1, 1, (c1 != 0 ? c1 : 1), 0);
     129       42796 :                         } else  if (getFunctionId(p) == countRef){
     130       33925 :                                 newRows(1,1, 1,0);
     131             :                         }
     132    16874077 :                 } else if( p->token == ASSIGNsymbol && p->argc== 2){
     133             :                         /* copy the rows property */
     134     2329949 :                         c1 = getRowCnt(mb, getArg(p,1));
     135             :                         /* just to ensure that rowcnt was/is never set to -1 */
     136     2329949 :                         if (c1 != (BUN) -1 && c1 != BUN_NONE)
     137     2329949 :                                 setRowCnt(mb, getArg(p,0), c1);
     138             :                 }
     139             :         }
     140             :         /* Defense line against incorrect plans */
     141             :         /* plan remains unaffected */
     142             :         // msg = chkTypes(cntxt->usermodule, mb, FALSE);
     143             :         // if (!msg)
     144             :         //      msg = chkFlow(mb);
     145             :         // if (!msg)
     146             :         //      msg = chkDeclarations(mb);
     147             : 
     148             :         /* keep actions taken as a fake argument*/
     149      353097 :         (void) pushInt(mb, pci, 1);
     150      353097 :         return msg;
     151             : }

Generated by: LCOV version 1.14