LCOV - code coverage report
Current view: top level - monetdb5/optimizer - opt_wlc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 5 95 5.3 %
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             : /* author M.Kersten
      10             :  * This optimizer prepares the code for workload-capture-replay processing
      11             :  * by injection of the proper calls.
      12             :  */
      13             : #include "monetdb_config.h"
      14             : #include "opt_wlc.h"
      15             : #include "wlc.h"
      16             : 
      17             : str
      18      353093 : OPTwlcImplementation(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
      19             : {       int i, j, limit, slimit, updates=0, query=1;
      20             :         InstrPtr p, q, def = 0;
      21             :         InstrPtr *old;
      22             :         str msg = MAL_SUCCEED;
      23             : 
      24             :         (void) cntxt;
      25             :         (void) stk;             /* to fool compilers */
      26             : 
      27      353093 :         if( ! WLCused() )
      28      353093 :                 goto wrapup;
      29             : 
      30           0 :         old= mb->stmt;
      31           0 :         limit= mb->stop;
      32           0 :         slimit = mb->ssize;
      33             : 
      34             :         /* check if we are dealing with an update  and move definition to front*/
      35           0 :         for (i = 0; i < limit; i++) {
      36           0 :                 p = old[i];
      37           0 :                 if( getModuleId(p) == querylogRef && getFunctionId(p) == defineRef){
      38             :                         def = p;
      39           0 :                         for(j = i; j>1; j--)
      40           0 :                                 old[j]= old[j-1];
      41           0 :                         old[j]= def;
      42             :                 }
      43           0 :                 if( getModuleId(p) == sqlcatalogRef)
      44             :                         query = 0;
      45           0 :                 if( getModuleId(p) == sqlRef &&
      46           0 :                         ( getFunctionId(p) == appendRef  ||
      47           0 :                           getFunctionId(p) == updateRef  ||
      48           0 :                           getFunctionId(p) == claimRef  ||
      49           0 :                           getFunctionId(p) == deleteRef  ||
      50           0 :                           getFunctionId(p) == clear_tableRef ))
      51             :                         query = 0;
      52             :         }
      53             :         def = 0;
      54             : 
      55           0 :         if(query) // nothing to log
      56             :                 return MAL_SUCCEED;
      57             : 
      58             :         // We use a fake collection of objects to speed up the checking later.
      59             : 
      60             :         // Now optimize the code
      61           0 :         if (newMalBlkStmt(mb,mb->ssize + updates) < 0)
      62           0 :                 return createException(MAL, "wlcr.optimizer", SQLSTATE(HY013) MAL_MALLOC_FAIL);
      63           0 :         for (i = 0; i < limit; i++) {
      64           0 :                 p = old[i];
      65           0 :                 pushInstruction(mb,p);
      66           0 :                 if( getModuleId(p) == querylogRef && getFunctionId(p) == defineRef){
      67           0 :                         if((q= copyInstruction(p)) == NULL) {
      68           0 :                                 for(i=0; i<mb->stop; i++)
      69           0 :                                         if( mb->stmt[i])
      70           0 :                                                 freeInstruction(mb->stmt[i]);
      71           0 :                                 GDKfree(mb->stmt);
      72           0 :                                 mb->stmt = old;
      73           0 :                                 mb->stop = limit;
      74           0 :                                 mb->ssize = slimit;
      75           0 :                                 return createException(MAL, "wlcr.optimizer", SQLSTATE(HY013) MAL_MALLOC_FAIL);
      76             :                         }
      77           0 :                         setModuleId(q, wlcRef);
      78           0 :                         setFunctionId(q,queryRef);
      79           0 :                         getArg(q,0) = newTmpVariable(mb,TYPE_any);
      80           0 :                         pushInstruction(mb,q);
      81             :                         def = q;
      82           0 :                         def->argc -=2;
      83           0 :                         updates++;
      84             :                 } else
      85             :                 /* the catalog operations all need to be re-executed */
      86           0 :                 if( def && getModuleId(p) == sqlcatalogRef &&
      87           0 :                         strcmp( getVarConstant(mb,getArg(p,1)).val.sval, "tmp") != 0 ){
      88             :                         assert( def);// should always be there
      89           0 :                         setFunctionId(def,catalogRef);
      90           0 :                         updates++;
      91             :                 } else
      92           0 :                 if( def && getModuleId(p) == sqlRef && getFunctionId(p) == clear_tableRef &&
      93           0 :                         strcmp( getVarConstant(mb,getArg(p,1)).val.sval, "tmp") != 0 ){
      94           0 :                         setFunctionId(def,actionRef);
      95             :                                 assert(def);
      96           0 :                                 if((q= copyInstruction(p)) == NULL) {
      97           0 :                                         for(i=0; i<mb->stop; i++)
      98           0 :                                                 if( mb->stmt[i])
      99           0 :                                                         freeInstruction(mb->stmt[i]);
     100           0 :                                         GDKfree(mb->stmt);
     101           0 :                                         mb->stmt = old;
     102           0 :                                         mb->stop = limit;
     103           0 :                                         mb->ssize = slimit;
     104           0 :                                         return createException(MAL, "wlcr.optimizer", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     105             :                                 }
     106           0 :                                 setModuleId(q, wlcRef);
     107           0 :                                 for( j=0; j< p->retc; j++)
     108           0 :                                         getArg(q,j) = newTmpVariable(mb,TYPE_any);
     109           0 :                                 pushInstruction(mb,q);
     110           0 :                                 updates++;
     111             :                 } else
     112           0 :                 if( def && getModuleId(p) == sqlRef &&
     113           0 :                         ( getFunctionId(p) == appendRef  ||
     114           0 :                           getFunctionId(p) == updateRef  ||
     115           0 :                           getFunctionId(p) == claimRef  ||
     116           0 :                           getFunctionId(p) == deleteRef  ||
     117           0 :                           getFunctionId(p) == clear_tableRef ) &&
     118           0 :                         strcmp( getVarConstant(mb,getArg(p,2+(getFunctionId(p) == claimRef))).val.sval, "tmp") != 0 ){
     119             :                                 assert( def);// should always be there, temporary tables are always ignored
     120           0 :                                 setFunctionId(def,actionRef);
     121           0 :                                 if((q= copyInstruction(p)) == NULL) {
     122           0 :                                         for(i=0; i<mb->stop; i++)
     123           0 :                                                 if( mb->stmt[i])
     124           0 :                                                         freeInstruction(mb->stmt[i]);
     125           0 :                                         GDKfree(mb->stmt);
     126           0 :                                         mb->stmt = old;
     127           0 :                                         mb->stop = limit;
     128           0 :                                         mb->ssize = slimit;
     129           0 :                                         return createException(MAL, "wlcr.optimizer", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     130             :                                 }
     131           0 :                                 delArgument(q, q->retc);
     132           0 :                                 setModuleId(q, wlcRef);
     133           0 :                                 for( j=0; j< p->retc; j++)
     134           0 :                                         getArg(q,j) = newTmpVariable(mb,TYPE_any);
     135           0 :                                 pushInstruction(mb,q);
     136           0 :                                 updates++;
     137             :                 }
     138             :         }
     139           0 :         for(; i<slimit; i++)
     140           0 :                 if( old[i])
     141           0 :                         pushInstruction(mb, old[i]);
     142           0 :         GDKfree(old);
     143             : 
     144             :         /* Defense line against incorrect plans */
     145           0 :         msg = chkTypes(cntxt->usermodule, mb, FALSE);
     146           0 :         if (!msg)
     147           0 :                 msg = chkFlow(mb);
     148             :         //if (!msg)
     149             :         //      msg = chkDeclarations(mb);
     150             : 
     151           0 : wrapup:
     152             :         /* keep actions taken as a fake argument*/
     153      353093 :         (void) pushInt(mb, pci, updates);
     154      353093 :         return msg;
     155             : }

Generated by: LCOV version 1.14