summaryrefslogtreecommitdiffstats
path: root/qlparser/querytree.cc
blob: 18497dff39d4f94ea4233757ab71333441e63052 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
*/
/*************************************************************
 *
 * SOURCE: querytree.cc
 *
 * MODULE: qlparser
 * CLASS:  QueryTree
 *
 * PURPOSE:
 *
 * COMMENTS:
 *
 ************************************************************/

static const char rcsid[] = "@(#)qlparser, QueryTree: $Id: querytree.cc,v 1.52 2005/06/28 08:42:13 rasdev Exp $";


#ifndef CPPSTDLIB
#include <ospace/string.h> // STL<ToolKit>
#else
#include <string>
using namespace std;
#endif
#include <iostream>
#include <dlfcn.h>

#include "raslib/rmdebug.hh"
#include "globals.hh"

#include "qlparser/querytree.hh"
#include "qlparser/qtnode.hh"
#include "qlparser/qtoperationiterator.hh"
#include "qlparser/qtselectioniterator.hh"
#include "qlparser/qtjoiniterator.hh"
#include "qlparser/qtvariable.hh"
#include "qlparser/qtmdd.hh"
#include "qlparser/qtdomainoperation.hh"
#include "qlparser/qtexecute.hh"

#include "catalogmgr/typefactory.hh"
#include "relcatalogif/mdddomaintype.hh"
#include "relcatalogif/settype.hh"

void (*QueryTree::optimizationFnc)(unsigned int, QtNode*) = NULL;

unsigned int QueryTree::nextCSENo = 0;

SymbolTable<int> QueryTree::symtab;

QueryTree::QueryTree()
  : rootNode(NULL),
    optimizationLevel(SUBEXPRESSIONS)
{
}


QueryTree::QueryTree( QtNode* root )
  :  rootNode( root ),
    optimizationLevel(SUBEXPRESSIONS)
{
}


QueryTree::~QueryTree()
{
  if( rootNode )
  {
    delete rootNode;
    rootNode=NULL;
  }
  releaseDynamicObjects();
}


void
QueryTree::checkSemantics()
{
  RMDBCLASS( "QueryTree", "checkSemantics()", "qlparser", __FILE__, __LINE__ )

  switch( rootNode->getNodeType() )
  {
    case QtNode::QT_MDD_ACCESS:
    case QtNode::QT_OPERATION_ITERATOR:
    case QtNode::QT_JOIN_ITERATOR:
    case QtNode::QT_SELECTION_ITERATOR: 
      {
        const QtTypeTuple& resultType = ((QtONCStream*)rootNode)->checkType();
        // RMInit::logOut << "result type: " << flush;
        // resultType.printStatus( RMInit::logOut );
      }
      break;

    case QtNode::QT_UPDATE:
    case QtNode::QT_INSERT:
    case QtNode::QT_DELETE:
    case QtNode::QT_COMMAND:
    case QtNode::QT_PYRAMID:
      ((QtExecute*)rootNode)->checkType();
      break;
      
    default:
      {
        const QtTypeElement& resultType = ((QtOperation*)rootNode)->checkType();
        // RMInit::logOut << "result type: " << flush;
        // resultType.printStatus( RMInit::logOut );
      }
      break;
  }
}


void
QueryTree::optimize( unsigned int currentOptimizationLevel )
{
  RMDBCLASS( "QueryTree", "optimize( unsigned int )", "qlparser", __FILE__, __LINE__ )

    if (optimizationFnc == NULL) {
      char *dir = CONFDIR;
      char libName[255];
      sprintf(libName,"%s/lib/libqloptimizer.so", dir);
      
      if (access(libName, X_OK | R_OK) == 0) {
	void *handle = dlopen(libName, RTLD_NOW);
	if (handle == NULL) {
	  RMInit::logOut << "Optimization library found, however could not be loaded" << endl;	    
	  printf("DLERROR = %s\n", dlerror());
	  RMInit::logOut << dlerror() << endl;	    
	  return;
	}
		
	*(void **)(&optimizationFnc) = dlsym(handle, "runOptimizations");
	if (optimizationFnc == NULL) {
	  RMInit::logOut << "Optimization library found, however the entry point was not found" << endl;	    
	  return;
	}
      } else {
	  RMInit::logOut << "No optimization library found" << endl;	    
	  return;
      }	
    }
  
  if (isValidOptimizationLevel(currentOptimizationLevel) == false)
	throw r_Error(r_Error::r_Error_InvalidOptimizationLevel);

  optimizationFnc(currentOptimizationLevel, rootNode);

  RMDBGIF( 1, RMDebug::module_qlparser, "QueryTree", \
    RMInit::logOut << endl << "     "; \
    rootNode->printAlgebraicExpression( RMInit::logOut ); \
    RMInit::logOut << endl << endl; \
    rootNode->printTree( 2, RMInit::logOut ); \
    RMInit::logOut << endl; \
  )
}

vector<QtData*>*
QueryTree::evaluateRetrieval() throw (r_Error, ParseInfo)
{
  vector<QtData*>* returnValue=NULL;

  if( rootNode )
  {
    if( rootNode->getNodeType() != QtNode::QT_MDD_ACCESS &&
        rootNode->getNodeType() != QtNode::QT_OPERATION_ITERATOR &&
        rootNode->getNodeType() != QtNode::QT_JOIN_ITERATOR &&
        rootNode->getNodeType() != QtNode::QT_SELECTION_ITERATOR    )
    {
      RMInit::logOut << "QueryTree::evaluateRetrieval() - Retrieval query must start with an ONC node." << endl;
      ParseInfo errorInfo = rootNode->getParseInfo();
      errorInfo.setErrorNo(371);
      throw errorInfo;
    }

    QtNode::QtDataList*          dataList=NULL;
    QtNode::QtDataList::iterator dataIter;
    QtONCStream*                 oncRootNode = (QtONCStream*)rootNode;

    try
    {
      oncRootNode->open();
    }
    catch( ... )
    {
      oncRootNode->close();
      RMInit::logOut << "QueryTree::evaluateRetrieval() - rethrow exception from oncRootNode->open()." << endl;
      throw;
    }

    // removed to have uniform, compact log output -- PB 2003-nov-20
    // RMInit::logOut << endl;

    // create result collection
    vector<QtData*>* resultData = new vector<QtData*>();

    try
    {
      while( (dataList = oncRootNode->next()) )
      {
        if( dataList->size() > 1 || (*dataList)[0] == NULL )
        {
          // Delete the tupel vector received by next(). Just tupel elements which are not
          // further referenced are deleted.
          for( dataIter=dataList->begin(); dataIter!=dataList->end(); dataIter++ )
            if( *dataIter ) (*dataIter)->deleteRef();
          delete dataList;
          dataList=NULL;

          if( resultData )
	  {
            // Delete the result vector
            for( dataIter=resultData->begin(); dataIter!=resultData->end(); dataIter++ )
              if( *dataIter ) (*dataIter)->deleteRef();
            delete resultData;
            resultData = NULL;
          }

          RMInit::logOut << "QueryTree::evaluateTree() - multiple query targets are not supported." << endl;
          ParseInfo errorInfo = oncRootNode->getParseInfo();
	  errorInfo.setErrorNo(361);
          throw errorInfo;
        }

        QtData* resultElement = (*dataList)[0];

	// take the data element as result data and reset it in the tupel vector
        resultData->push_back( resultElement );
        (*dataList)[0] = NULL;        

	RMDBGMIDDLE( 2, RMDebug::module_qlparser, "QueryTree", endl << endl << "NEXT RESULT ITEM OF THE QUERY INSERTED" << endl << endl )

        // Delete the tupel vector received by next(). Just tupel elements which are not
        // set to zero and which are not further referenced are deleted.
	for( dataIter=dataList->begin(); dataIter!=dataList->end(); dataIter++ )
	  if( *dataIter ) (*dataIter)->deleteRef();

        // delete the tuple vector
        delete dataList;
        dataList=NULL;
      }
    }  
    catch(r_Error& myErr) {
      RMInit::logOut << endl << "Caught BAD exception when evaluating query! " << endl;
      RMInit::logOut << myErr.what() << endl;
      if( resultData )
      {
        // Delete the result vector
        for( dataIter=resultData->begin(); dataIter!=resultData->end(); dataIter++ )
          if( *dataIter ) (*dataIter)->deleteRef();
        delete resultData;
        resultData = NULL;
      }

      oncRootNode->close();
      RMInit::logOut << "QueryTree::evaluateTree() - rethrow exception." << endl;
      throw;
    }
    catch( ... )
    {
      if( resultData )
      {
        // Delete the result vector
        for( dataIter=resultData->begin(); dataIter!=resultData->end(); dataIter++ )
          if( *dataIter ) (*dataIter)->deleteRef();
        delete resultData;
        resultData = NULL;
      }

      oncRootNode->close();
      RMInit::logOut << "QueryTree::evaluateTree() - rethrow exception." << endl;
      throw;
    }

    oncRootNode->close();

    returnValue = resultData;
  }

  return returnValue;
}



void
QueryTree::evaluateUpdate() throw (r_Error,ParseInfo)
{
  if( rootNode )
  {
    if( rootNode->getNodeType() != QtNode::QT_UPDATE  &&
        rootNode->getNodeType() != QtNode::QT_INSERT  &&
        rootNode->getNodeType() != QtNode::QT_DELETE  &&
        rootNode->getNodeType() != QtNode::QT_COMMAND &&
	rootNode->getNodeType() != QtNode::QT_PYRAMID
      )
    {
      RMInit::logOut << "QueryTree::evaluateUpdate() - update query must start with an INSERT, UPDATE, DELETE, DROP or CREATE statement." << endl;
      ParseInfo errorInfo = rootNode->getParseInfo();
      errorInfo.setErrorNo(372);
      throw errorInfo;
    }

    QtExecute* executeNode = (QtExecute*) rootNode;

    // evaluate the update query
    executeNode->evaluate();
  }
}



void QueryTree::printTree( int tab, ostream& s ) {
  if ( rootNode ) {
    s <<  SPACE_STR(tab).c_str() << "QueryTree:" << endl;
    rootNode->printTree( tab + 2, s );
  } else
    s << SPACE_STR(tab).c_str() << "QueryTree: Qt has no root node." << endl;
}

void QueryTree::addDynamicObject( QtNode *node ) {
  qtNodeList.push_back( node );
}

void QueryTree::removeDynamicObject( QtNode *node ) {
  qtNodeList.remove( node );
}

void QueryTree::addDynamicObject( QtData *node ) {
  qtDataList.push_back( node );
}

void QueryTree::removeDynamicObject( QtData *node ) {
  qtDataList.remove( node );
}

void QueryTree::addDynamicObject( ParseInfo *node ) {
  parseInfoList.push_back( node );
}

void QueryTree::removeDynamicObject( ParseInfo *node ) {
  parseInfoList.remove( node );
}

void QueryTree::addDynamicObject( vector<QtONCStream *> *node ) {
  vectorList.push_back( node );
}

void QueryTree::removeDynamicObject( vector<QtONCStream*> *node ) {
  vectorList.remove( node );
}

void QueryTree::releaseDynamicObjects() {
  list<QtNode*>::iterator iter1;
  for( iter1 = qtNodeList.begin(); iter1 != qtNodeList.end(); iter1++ )
  {
    delete *iter1;
    *iter1=NULL;
  }

  list<QtData*>::iterator iter2;
  for( iter2 = qtDataList.begin(); iter2 != qtDataList.end(); iter2++ )
  {
    delete *iter2;
    *iter2=NULL;
  }

  list<ParseInfo*>::iterator iter3;
  for( iter3 = parseInfoList.begin(); iter3 != parseInfoList.end(); iter3++ )
  {
    delete *iter3;
    *iter3=NULL;
  }

  list<vector<QtONCStream*>*>::iterator iter4;
  for( iter4 = vectorList.begin(); iter4 != vectorList.end(); iter4++ )
  {
    delete *iter4;
    *iter4=NULL;
  }

  list<char *>::iterator iter5;
  for( iter5 = lexedCStringList.begin(); iter5 != lexedCStringList.end(); iter5++ )
  {
    free(*iter5);
    *iter5=NULL;
  }
}

void QueryTree::addDomainObject( QtDomainOperation *dop ) {
  dopList.push_back( dop );
}

void QueryTree::removeDomainObject( QtDomainOperation *dop ) {
  dopList.remove( dop );
}

void QueryTree::printDomainObjects() {
  list<QtDomainOperation*>::iterator iter;
  for( iter = dopList.begin(); iter != dopList.end(); iter++ ) { 
    cout << endl;
    (*iter)->printTree( 2 );
  }
}

void QueryTree::releaseDomainObjects() {
  list<QtDomainOperation*>::iterator iter;
  for( iter = dopList.begin(); iter != dopList.end(); iter++ )
  {
    delete *iter;
    *iter=NULL;
  }
}

void QueryTree::rewriteDomainObjects(r_Minterval *greatDomain, string *greatIterator, QtMarrayOp2::mddIntervalListType *greatList) {

  RMDBGENTER( 2, RMDebug::module_qlparser, "QueryTree", endl << "QueryTree: Iterator: <" << *greatIterator << "> Domain: " << *greatDomain << endl )
  list<QtDomainOperation*>::iterator iter;

  for( iter = dopList.begin(); iter != dopList.end(); iter++ ) { 

    // 1. get var name from iter
    QtVariable *qtVar = ((QtVariable *)((*iter)->getInput()));
    string stVar = qtVar->getIteratorName();
    const char *varname = stVar.c_str(); 

    // 2. get position of varname in varList    
    bool bcond = false;    
    QtMarrayOp2::mddIntervalListType *varList = greatList;          
    QtMarrayOp2::mddIntervalListType::iterator varIter;	  
    r_Long varpos = 0;	  	  
    for (varIter = varList->begin(); varIter != varList->end(); varIter++) {

      if (!strcmp(varname, varIter->variable.c_str())) {
	bcond = true; 
	break;
      };
      QtData *data = varIter->tree->evaluate(0);
      r_Dimension dimension = ((QtMintervalData*)data)->getMintervalData().dimension();
      varpos = varpos+dimension;
    };
    // postcond: bcond == false: varname not found in list. else varpos gives the position.

    if (bcond) {

      // 3. set domain expression to old one incremented by varpos
      QtNode::QtOperationList *lop = new QtNode::QtOperationList(1);
      (*lop)[0] = 
    	    new QtPlus(
	      (*iter)->getMintervalOp(), 
	      new QtConst(
		new QtAtomicData(
                  varpos, sizeof(varpos)
		)
	      )
            );

      (*iter)->setMintervalOp( new QtPointOp( lop ) );

      // 4. set varname to greatIterator
      QtVariable *var1 = new QtVariable( string(*greatIterator) );
      (*iter)->setInput(var1);

    } else
    {
      // TODO: insert some error notify code here!      
      RMDBGMIDDLE( 1, RMDebug::module_qlparser, "QueryTree", " variable name not found in list " )
    }
  }
}

void QueryTree::addCString( char *str ) {
  lexedCStringList.push_back( str );
}

bool QueryTree::isValidOptimizationLevel( int level )
{
    switch (level)
    {
	case NO_OPTIMIZATIONS:
	case STANDARDIZATION:
	case SIMPLIFICATION: 
	case SUBEXPRESSIONS:
		return true;

	default:
		return false;
    }
}