summaryrefslogtreecommitdiffstats
path: root/qlparser/qtmintervalop.cc
blob: e112f1840a1176b958a68a1f206e87d228630e2b (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
/*
* 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, QtMintervalOp: $Header: /home/rasdev/CVS-repository/rasdaman/qlparser/qtmintervalop.cc,v 1.12 2003/12/27 20:51:28 rasdev Exp $";

#include "raslib/rmdebug.hh"

#include "qlparser/qtmintervalop.hh"
#include "qlparser/qtdata.hh"
#include "qlparser/qtmintervaldata.hh"
#include "qlparser/qtintervaldata.hh"
#include "qlparser/qtatomicdata.hh"

#include "catalogmgr/ops.hh"
#include "relcatalogif/type.hh"

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


const QtNode::QtNodeType QtMintervalOp::nodeType = QT_MINTERVALOP;

QtMintervalOp::QtMintervalOp( QtOperationList* opList )
  :  QtNaryOperation( opList )
{
}



QtData*
QtMintervalOp::evaluate( QtDataList* inputList )
{
  RMDBCLASS( "QtMintervalOp", "evaluate( QtDataList* )", "qlparser", __FILE__, __LINE__ )

  QtData*     returnValue = NULL;
  QtDataList* operandList = NULL;

  if( getOperands( inputList, operandList ) )
  {
    vector<QtData*>::iterator dataIter;
    bool              goOn=true;

    // check for point operand
    if( operandList->size() == 1 && ((*operandList)[0])->getDataType() == QT_MINTERVAL )
    {
      // pass point as minterval projection
      returnValue = (*operandList)[0];

      delete operandList;
      operandList=NULL;
    }
    else
    {
      // first check operand types
      for( dataIter=operandList->begin(); dataIter!=operandList->end() && goOn; dataIter++ )
        if (!( (*dataIter)->getDataType() == QT_SHORT || (*dataIter)->getDataType() == QT_USHORT ||
                (*dataIter)->getDataType() == QT_LONG  || (*dataIter)->getDataType() == QT_ULONG  ||
                (*dataIter)->getDataType() == QT_OCTET || (*dataIter)->getDataType() == QT_CHAR   ||
                (*dataIter)->getDataType() == QT_INTERVAL))
        {
          goOn=false; 
          break;
        }

      if( !goOn )
      {
        RMInit::logOut << "Error: QtMintervalOp::evaluate() - expressions for minterval dimensions must be either of type integer or interval." << endl;
        parseInfo.setErrorNo(390);

        // delete the old operands
        if( operandList )
        {
          for( dataIter=operandList->begin(); dataIter!=operandList->end(); dataIter++ )
            if( (*dataIter) ) (*dataIter)->deleteRef();

          delete operandList;
          operandList=NULL;
        }

        throw parseInfo;
      }

      //
      // create a QtMintervalData object and fill it
      //
      r_Minterval   domainData( operandList->size() );
      vector<bool>* trimFlags = new vector<bool>( operandList->size() );
      int           pos;

      for( dataIter=operandList->begin(), pos=0; dataIter!=operandList->end(); dataIter++,pos++ )
      {
        if( (*dataIter)->getDataType() == QT_INTERVAL )
        {
          domainData << ((QtIntervalData*)(*dataIter))->getIntervalData();
  	  (*trimFlags)[pos] = true;
        }else
        {
          if( (*dataIter)->getDataType() == QT_SHORT || 
              (*dataIter)->getDataType() == QT_LONG  || 
              (*dataIter)->getDataType() == QT_OCTET )
            domainData << ((QtAtomicData*)(*dataIter))->getSignedValue();
          else
            domainData << ((QtAtomicData*)(*dataIter))->getUnsignedValue();

	  (*trimFlags)[pos] = false;
        }
      }    
    
      returnValue = new QtMintervalData( domainData, trimFlags ); 

      // delete the old operands
      if( operandList )
      {
        for( dataIter=operandList->begin(); dataIter!=operandList->end(); dataIter++ )
          if( (*dataIter) ) (*dataIter)->deleteRef();

        delete operandList;
        operandList=NULL;
      }
    }
  }

  return returnValue;
}



void
QtMintervalOp::printTree( int tab, ostream& s, QtChildType mode )
{
  s << SPACE_STR(tab).c_str() << "QtMintervalOp Object " << getNodeType() << endl;

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



void
QtMintervalOp::printAlgebraicExpression( ostream& s )
{
  s << "[";
 
  QtNaryOperation::printAlgebraicExpression( s );
 
  s << "]";
}



const QtTypeElement&
QtMintervalOp::checkType( QtTypeTuple* typeTuple )
{
  RMDBCLASS( "QtMintervalOp", "checkType( QtTypeTuple* )", "qlparser", __FILE__, __LINE__ )

  dataStreamType.setDataType( QT_TYPE_UNKNOWN );  

  QtOperationList::iterator iter;
  bool              opTypesValid = true;
    
  for( iter=operationList->begin(); iter!=operationList->end() && opTypesValid; iter++ )
  {
    const QtTypeElement& type = (*iter)->checkType( typeTuple );

    // valid types: interval, integers
    if (!( type.getDataType() == QT_INTERVAL ||
                       type.getDataType() == QT_SHORT    ||
                       type.getDataType() == QT_LONG     ||
                       type.getDataType() == QT_OCTET    ||
                       type.getDataType() == QT_USHORT   ||
                       type.getDataType() == QT_ULONG    ||
                       type.getDataType() == QT_CHAR))
    {
      opTypesValid=false;
      break;
    }
  }

  if( !opTypesValid )
  {
    RMInit::logOut << "Error: QtMintervalOp::checkType() - expressions for minterval dimensions must be either of type integer or interval." << endl;
    parseInfo.setErrorNo(390);
    throw parseInfo; 
  }

  dataStreamType.setDataType( QT_MINTERVAL );

  return dataStreamType;
}