summaryrefslogtreecommitdiffstats
path: root/qlparser/qtmarrayop2.cc
blob: 4393486123e384371802897930506bfbd06f327d (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
/*
* 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>.
*/
/*************************************************************
 *
 *
 * PURPOSE:
 *
 *
 * COMMENTS:
 *
 ************************************************************/

static const char rcsid[] = "@(#)qlparser, QtMarrayOp2: $Header: /home/rasdev/CVS-repository/rasdaman/qlparser/qtmarrayop2.cc,v 1.17 2003/12/27 20:51:28 rasdev Exp $";

#include "raslib/rmdebug.hh"

#include "qlparser/qtmarrayop2.hh"
#include "qlparser/qtdata.hh"
#include "qlparser/qtmdd.hh"
#include "qlparser/qtpointdata.hh"
#include "qlparser/qtmintervaldata.hh"
#include "qlparser/qtmintervalop.hh"

#include "mddmgr/mddobj.hh"
#include "qlparser/querytree.hh"

#include "catalogmgr/typefactory.hh"
#include "relcatalogif/basetype.hh"
#include "relcatalogif/longtype.hh"

#include "catalogmgr/algebraops.hh"

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

extern QueryTree* parseQueryTree;

QtMarrayOp2::QtMarrayOp2( mddIntervalListType* & aList, QtOperation* & cellExp )
:  iterators( *aList ), qtOperation( cellExp ) {
}

QtMarrayOp2::~QtMarrayOp2(){
}

bool QtMarrayOp2::concatenateIntervals() {
  QtData* data=NULL;
  RMDBGENTER( 2, RMDebug::module_qlparser, "QtMarrayOp2", "Validity Check: " )
   
  // check for validity
  bool eflag=true;
  mddIntervalListType::const_iterator ii;
  QtNode::QtOperationList::const_iterator j;
  QtNode::QtOperationList *ddd=NULL;
  QtOperation *op1=NULL;
  QtOperation *op2=NULL;
  
  for (ii = iterators.begin(); ii != iterators.end() ; ii++) {
    ddd = ((QtMintervalOp *)(ii->tree))->getInputs();
    for (j = ddd->begin(); j != ddd->end(); j++) {
     if((QtNode *)*j) {
      if ((((QtNode *)*j)->getNodeType() == QtNode::QT_INTERVALOP)) 
      {
          op1 = ((QtBinaryOperation *)*j)->getInput1();
	  if (((QtNode *)op1)->getNodeType() != QtNode::QT_CONST) eflag=false;
          op2 = ((QtBinaryOperation *)*j)->getInput2();
	  if (((QtNode *)op2)->getNodeType() != QtNode::QT_CONST) eflag=false;
      } else
      if (((QtNode *)*j)->getNodeType() != QtNode::QT_CONST)
        eflag=false;
    }
   }
  }
  RMDBGMIDDLE( 2, RMDebug::module_qlparser, "QtMarrayOp2", "Validity check completed." )   
  if (eflag) {
    RMDBGMIDDLE( 2, RMDebug::module_qlparser, "QtMarrayOp2", "eflag is true!" << endl )
    // compute total dimension
    RMDBGMIDDLE( 2, RMDebug::module_qlparser, "QtMarrayOp2", endl << "QtMarrayOp2: Dimensions: " )
    greatDimension=0; 
    mddIntervalListType::const_iterator i;
    for (i = iterators.begin(); i != iterators.end() ; i++) {
      // evaluate intervals
      data = (i->tree)->evaluate(0);
      r_Dimension dimension = ((QtMintervalData*)data)->getMintervalData().dimension();
      RMDBGMIDDLE( 2, RMDebug::module_qlparser, "QtMarrayOp2", dimension << " | " );
      greatDimension += dimension;    
    }
    RMDBGMIDDLE( 2, RMDebug::module_qlparser, "QtMarrayOp2", " Total " << greatDimension << endl )

    // concatenate the data of the intervals into one big interval
    RMDBGMIDDLE( 2, RMDebug::module_qlparser, "QtMarrayOp2", "QtMarray2: Domains: " ) 
    greatDomain = r_Minterval( greatDimension );
    for (i = iterators.begin(); i != iterators.end() ; i++) {
      // evaluate intervals
      data = i->tree->evaluate(0);
      r_Minterval domain    = ((QtMintervalData*)data)->getMintervalData();
      r_Dimension dimension = domain.dimension();
      r_Dimension jj;
      for (jj=0; jj != dimension; jj++) {
	r_Sinterval part = domain[jj];
	greatDomain << part;  
      }
      RMDBGMIDDLE( 2, RMDebug::module_qlparser, "QtMarrayOp2", domain << " | " )    
    }
    RMDBGMIDDLE( 2, RMDebug::module_qlparser, "QtMarrayOp2", " Total: " << greatDomain << endl )

    // concatenate the identifier names to one big identifier name
    RMDBGMIDDLE( 2, RMDebug::module_qlparser, "QtMarrayOp2", "QtMarray2: Iterators: " )
    greatIterator = string("");
    string iname("");
    for (i = iterators.begin(); i != iterators.end() ; i++) {
      // get iterator name
      iname = string(i->variable);
      greatIterator = greatIterator + string(" ") + string(i->variable);       
      RMDBGMIDDLE( 2, RMDebug::module_qlparser, "QtMarrayOp2", i->variable << " | " )
    }
    RMDBGEXIT( 2, RMDebug::module_qlparser, "QtMarrayOp2", " Total: " << greatIterator << endl )
  } else
  RMDBGEXIT( 2, RMDebug::module_qlparser, "QtMarrayOp2", " eflag is false! " << greatIterator << endl )
  return eflag;
}



void QtMarrayOp2::rewriteVars( ) {
  if (!oldMarray) {
    RMDBGMIDDLE( 2, RMDebug::module_qlparser, "QtMarrayOp2", "concatenateIteratorNames: " )   
    mddIntervalListType::const_iterator i;
    // concatenate the identifier names to one big identifier name
    RMDBGMIDDLE( 2, RMDebug::module_qlparser, "QtMarrayOp2", "QtMarray2: Iterators: " )
    greatIterator = string("");
    string iname("");
    for (i = iterators.begin(); i != iterators.end() ; i++) {
      // get iterator name
      iname = string(i->variable);
      greatIterator = greatIterator + string(" ") + string(i->variable);       
      RMDBGMIDDLE( 2, RMDebug::module_qlparser, "QtMarrayOp2", i->variable << " | " )
    }
    RMDBGMIDDLE( 2, RMDebug::module_qlparser, "QtMarrayOp2", " Total: " << greatIterator << endl )
  }
  traverse(qtOperation);
}

void QtMarrayOp2::traverse(QtOperation *&node) {

  if (node != 0) 
  {  
    QtOperation* temp = NULL;
    
    
    if (((QtNode *)node)->getNodeType() == QtNode::QT_DOMAIN_OPERATION) 
    {
      // syntax: dinput [ dmiop ]

      // traverse dmiop
      QtOperation *dmiop  = ((QtDomainOperation *)node)->getMintervalOp();      
      temp = (QtOperation*)dmiop;
      traverse(temp);
      dmiop = temp;
      ((QtDomainOperation *)node)->setMintervalOp(dmiop);

      // if dinput == QtVariable then rewrite it
      QtOperation *dinput = ((QtDomainOperation *)node)->getInput();

      // if not a variable, then recurse
      if (((QtNode *)dinput)->getNodeType() != QtNode::QT_MDD_VAR)
      {	     
	// traverse dinput 
	temp = (QtOperation *)dinput;
	traverse(temp);
	dinput = temp;
	((QtDomainOperation *)node)->setInput(dinput);

	// get childs and traverse them
	QtNode::QtNodeList* childList = node->getChilds(QtNode::QT_DIRECT_CHILDS);
	for( QtNode::QtNodeList::iterator iter = childList->begin(); iter != childList->end(); iter++ )
	{
	  temp = (QtOperation*)*iter;
	  traverse(temp);
	  *iter = temp;
	};
        delete childList;
        childList=NULL;
      };
    } else
    {

      if (node->getNodeType() == QtNode::QT_MDD_VAR) {
	if (QueryTree::symtab.lookupSymbol(((QtVariable *)node)->getIteratorName()))
	{
          if (!oldMarray)
	  { 
            RMDBGMIDDLE( 2, RMDebug::module_qlparser, "QtMarrayOp2", endl << "marray2 var identifier:" << ((QtVariable *)node)->getIteratorName() << " replacing with " << greatIterator << endl )
            ((QtVariable *)node)->setIteratorName(greatIterator);
	  };
          // replace with var[0]	
	  QtDomainOperation *dop = new QtDomainOperation( new QtConst (new QtAtomicData((r_Long)0, sizeof(r_Long))) /*new QtPointOp( lop )*/ );
	  dop->setInput( (QtVariable *)node );	  
          node = dop;	
	  parseQueryTree->addDomainObject( dop );
	}; 	
      } else
      {

	// traverse inputs	
	switch (((QtNode *)node)->getNodeType()) {      
/*
	  // with no input
	  case QtNode::QT_UNDEFINED_NODE:
	  case QtNode::QT_MDD_ACCESS:
	  case QtNode::QT_OPERATION_ITERATOR:
	  case QtNode::QT_SELECTION_ITERATOR:
	  case QtNode::QT_JOIN_ITERATOR:
	  case QtNode::QT_UPDATE:
	  case QtNode::QT_INSERT:
	  case QtNode::QT_DELETE:
	  case QtNode::QT_COMMAND:
	  case QtNode::QT_PYRAMID:
	  case QtNode::QT_MDD_VAR:
	  case QtNode::QT_UDFCALLOP:
	  case QtNode::QT_CONST:
	  case QtNode::QT_MDD_STREAM:
*/
	  // from Unary
	  case QtNode::QT_TANH:
	  case QtNode::QT_TAN:
	  case QtNode::QT_SQRT:
	  case QtNode::QT_SINH:
	  case QtNode::QT_SIN:
	  case QtNode::QT_NOT:
	  case QtNode::QT_LOG:
	  case QtNode::QT_LN:
	  case QtNode::QT_EXP:
	  case QtNode::QT_DOT:
	  case QtNode::QT_COSH:
	  case QtNode::QT_COS:
	  case QtNode::QT_ARCTAN:
	  case QtNode::QT_ARCSIN:
	  case QtNode::QT_ARCCOS:
	  case QtNode::QT_ABS:                                   
	  case QtNode::QT_REALPART:
	  case QtNode::QT_IMAGINARPART:
	  case QtNode::QT_CAST:
	  case QtNode::QT_SDOM:
	  case QtNode::QT_OID:
	  case QtNode::QT_LO:
	  case QtNode::QT_HI:
//	  case QtNode::QT_DOMAIN_OPERATION:
	  case QtNode::QT_CONVERSION:
	  case QtNode::QT_SOME:
	  case QtNode::QT_MINCELLS:
	  case QtNode::QT_MAXCELLS:
	  case QtNode::QT_COUNTCELLS:
	  case QtNode::QT_AVGCELLS:
	  case QtNode::QT_ALL:
	  case QtNode::QT_ADDCELLS:
	  case QtNode::QT_CSE_ROOT: 
	  {
	     QtOperation *uinput = ((QtUnaryOperation *)node)->getInput();
	     
	     temp = (QtOperation *)uinput;
	     traverse(temp);
	     uinput = temp;
	     
	     ((QtUnaryOperation *)node)->setInput(uinput);
	  };
	  break;

	  // from Binary
	  case QtNode::QT_SHIFT:
	  case QtNode::QT_SCALE:
	  case QtNode::QT_MARRAYOP:
	  case QtNode::QT_INTERVALOP:
	  case QtNode::QT_CONDENSEOP:
	  case QtNode::QT_XOR:
	  case QtNode::QT_PLUS:
	  case QtNode::QT_OR:
	  case QtNode::QT_NOT_EQUAL:
	  case QtNode::QT_MULT:
	  case QtNode::QT_MINUS:
	  case QtNode::QT_LESS_EQUAL:
	  case QtNode::QT_LESS:
	  case QtNode::QT_IS:
	  case QtNode::QT_EQUAL:
	  case QtNode::QT_DIV:
	  case QtNode::QT_AND:
	  case QtNode::QT_OVERLAY:
	  case QtNode::QT_BIT:
	  {
	     QtOperation *binput1 = ((QtBinaryOperation *)node)->getInput1();
	     QtOperation *binput2 = ((QtBinaryOperation *)node)->getInput2();
	     
	     QtOperation* temp1 = 0;
	     QtOperation* temp2 = 0;
	     temp1 = (QtOperation *)binput1;
	     temp2 = (QtOperation *)binput2;
	     traverse(temp1);
	     traverse(temp2);
	     binput1 = temp1;
	     binput2 = temp2;
	     
	     ((QtBinaryOperation *)node)->setInput1(binput1);
	     ((QtBinaryOperation *)node)->setInput2(binput2);
	  };
	  break;

	  // from Nary
	  case QtNode::QT_POINTOP:
	  case QtNode::QT_MINTERVALOP:
	  {
	     QtNode::QtOperationList *ninput = ((QtNaryOperation *)node)->getInputs();
	     	     
	     for( QtNode::QtOperationList::iterator iter = ninput->begin(); iter != ninput->end(); iter++ )
	     {
	       temp = (QtOperation *)*iter;
	       traverse(temp);
	       *iter = temp;
	     };
	     
	     ((QtNaryOperation *)node)->setInputs(ninput);
	  };
	  break;
          default: {
            // do nothing
          }; break;
	};
	
	// get childs and traverse them
	QtNode::QtNodeList* childList = node->getChilds(QtNode::QT_DIRECT_CHILDS);
	for( QtNode::QtNodeList::iterator iter = childList->begin(); iter != childList->end(); iter++ )
	{
	  temp = (QtOperation*)*iter;
	  traverse(temp);
	  *iter = temp;
	};
        delete childList;
        childList=NULL;
      };
    };
  };
}