summaryrefslogtreecommitdiffstats
path: root/qlparser/qtselectioniterator.cc
blob: f6d25291006038aaeae8feca72e60fe6ea02a2a3 (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
/*
* 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, QtSelectionIterator: $Id: qtselectioniterator.cc,v 1.24 2001/08/07 12:00:12 barbat Exp $";

#include "raslib/rmdebug.hh"

#include "qlparser/qtselectioniterator.hh"
#include "qlparser/qtatomicdata.hh"

#include "relcatalogif/type.hh"

#include <iostream>
#include <string>
#include <vector>
using namespace std;

const QtNode::QtNodeType QtSelectionIterator::nodeType = QtNode::QT_SELECTION_ITERATOR;


QtSelectionIterator::QtSelectionIterator()
  : QtIterator(),
    conditionTree(NULL)
{
}


QtSelectionIterator::QtSelectionIterator( QtNode* node )
  : QtIterator( node ),
    conditionTree(NULL)
{
}


QtSelectionIterator::~QtSelectionIterator()
{
  if( conditionTree )
  {
    delete conditionTree;
    conditionTree=NULL;
  }
}


QtNode::QtNodeList*
QtSelectionIterator::getChilds( QtChildType flag )
{
  QtNodeList* resultList=NULL;
  QtNodeList* subList=NULL;

  RMDBGENTER( 1, RMDebug::module_qlparser, "QtSelectionIterator", " QtSelectionIterator::getChilds() ")

  resultList = QtIterator::getChilds( flag );

  RMDBGIF( 4, RMDebug::module_qlparser, "QtSelectionIterator", \
    RMInit::dbgOut << "1. childs from stream subtree " << endl; \
    list<QtNode*>::iterator debugIter; \
    for( debugIter=resultList->begin(); debugIter!=resultList->end(); debugIter++ ) \
      (*debugIter)->printTree( 2, RMInit::dbgOut, QtNode::QT_DIRECT_CHILDS ); \
  )

  if( conditionTree )
  {
    if( flag == QT_LEAF_NODES || flag == QT_ALL_NODES )
    {
      subList = conditionTree->getChilds( flag );

      RMDBGIF( 4, RMDebug::module_qlparser, "QtSelectionIterator", \
        RMInit::dbgOut << "2. childs from operation subtree (without direct childs) " << endl; \
        list<QtNode*>::iterator debugIter; \
        for( debugIter=subList->begin(); debugIter!=subList->end(); debugIter++ ) \
          (*debugIter)->printTree( 2, RMInit::dbgOut, QtNode::QT_DIRECT_CHILDS ); \
      )

      // remove all elements in subList and insert them at the beginning in resultList
      resultList->splice( resultList->begin(), *subList );

      RMDBGIF( 4, RMDebug::module_qlparser, "QtSelectionIterator", \
        RMInit::dbgOut << "3. merge of the lists " << endl;  \
        list<QtNode*>::iterator debugIter; \
        for( debugIter=resultList->begin(); debugIter!=resultList->end(); debugIter++ ) \
          (*debugIter)->printTree( 2, RMInit::dbgOut, QtNode::QT_DIRECT_CHILDS ); \
      )

      
      RMDBGIF( 4, RMDebug::module_qlparser, "QtSelectionIterator", \
        RMInit::dbgOut << "4. old list (must be empty)" << endl; \
        list<QtNode*>::iterator debugIter; \
        for( debugIter=subList->begin(); debugIter!=subList->end(); debugIter++ ) \
          (*debugIter)->printTree( 2, RMInit::dbgOut, QtNode::QT_DIRECT_CHILDS ); \
      )

      // delete temporary subList
      delete subList;
      subList=NULL;
    };

    // add nodes of next level
    if( flag == QT_DIRECT_CHILDS || flag == QT_ALL_NODES )
      resultList->push_back( conditionTree );

    RMDBGIF( 4, RMDebug::module_qlparser, "QtSelectionIterator",
      RMInit::dbgOut << "4. current child list including direct childs " << endl; \
      list<QtNode*>::iterator debugIter; \
      for( debugIter=resultList->begin(); debugIter!=resultList->end(); debugIter++ ) \
        (*debugIter)->printTree( 2, RMInit::dbgOut, QtNode::QT_DIRECT_CHILDS ); \
    )

  };

  return resultList;
}


void
QtSelectionIterator::printTree( int tab, ostream& s, QtChildType mode )
{
  s << SPACE_STR(tab).c_str() << "QtSelectionIterator Object: type " << flush;
  dataStreamType.printStatus( s );
  s << endl;

  if( mode != QtNode::QT_DIRECT_CHILDS )
  {
    if( conditionTree )
    {
      s << SPACE_STR(tab).c_str() << "condition : " << endl;
      conditionTree->printTree( tab + 2, s );
    }
    else
      s << SPACE_STR(tab).c_str() << "no condition" << endl;
  }

  QtIterator::printTree( tab, s, mode );
}



void
QtSelectionIterator::printAlgebraicExpression( ostream& s )
{
  s << "sel<";

  conditionTree->printAlgebraicExpression( s );

  s << ">";

  QtIterator::printAlgebraicExpression( s );
}



QtNode::QtDataList*
QtSelectionIterator::next()
{
  RMDBGENTER( 1, RMDebug::module_qlparser, "QtSelectionIterator", "QtSelectionIterator::next()" )

  QtDataList* returnValue = NULL;

  if( inputs )
  {
    bool        nextTupelValid = false;
    QtDataList* actualTupel=NULL;

    while( !nextTupelValid )
    {
      actualTupel = (*inputs)[0]->next();

      if( actualTupel )
      {
        if( conditionTree )
        {
          // evaluate the condition tree
          QtData* resultData = conditionTree->evaluate( actualTupel );

          if( resultData )
          {
            if( resultData->getDataType() == QT_BOOL )
  	      nextTupelValid = (bool)((QtAtomicData*)resultData)->getUnsignedValue();
    	    else
	    {
              RMInit::logOut << "Error: QtSelectionIterator::next() - result of the WHERE part must be of type Bool." << endl;
              parseInfo.setErrorNo(359);
              throw parseInfo;
	    }

            resultData->deleteRef();

            if( !nextTupelValid )
            {
              // delete transient objects
              vector<QtData*>::iterator iter;

              for( iter=actualTupel->begin(); iter!=actualTupel->end(); iter++)
                if( *iter ) (*iter)->deleteRef();

              // delete vector itself
              delete actualTupel;
              actualTupel = NULL;
            };
          }
        }
        else
	  nextTupelValid = true;
      }
      else
        break;
    }

    returnValue = actualTupel;
  }

  RMDBGEXIT( 1, RMDebug::module_qlparser, "QtSelectionIterator", "QtSelectionIterator::next()" )

  return returnValue;
}

/*
inline void
QtSelectionIterator::preOptimize()
{
  if( conditionTree )
    conditionTree->optimizeLoad( new QtNode::QtTrimList );

  // pass it to the input streams
  QtIterator::preOptimize();
};
*/

const QtTypeTuple&
QtSelectionIterator::checkType()
{
  RMDBCLASS( "QtSelectionIterator", "checkType()", "qlparser", __FILE__, __LINE__ )

  // concatenate types of inputs
  getInputTypeTuple( dataStreamType );

  // type check for condition tree
  if( conditionTree )
  {
    const QtTypeElement& type = conditionTree->checkType( (QtTypeTuple*)&dataStreamType );

    if( type.getDataType() != QT_BOOL )
    {
      RMInit::logOut << "Error: QtSelectionIterator::next() - result of the WHERE part must be of type Bool." << endl;
      parseInfo.setErrorNo(359);
      throw parseInfo;
    }
  }

  // pass type tuple
  return dataStreamType;
}