summaryrefslogtreecommitdiffstats
path: root/qlparser/querytree.hh
blob: 002bca27c96fb4c4c9978af85e2ffae8a572fd15 (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
/*
* 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>.
*/
#ifndef _QUERYTREE_
#define _QUERYTREE_

#ifndef CPPSTDLIB
#include <ospace/string.h> // STL<ToolKit>
#else
#include <string>
#endif
#include <algorithm>

#include "mddmgr/mddcoll.hh"

#include "qlparser/qtnode.hh"
#include "qlparser/qtatomicdata.hh"
#include "qlparser/qtbinaryinduce.hh"
#include "qlparser/qtconst.hh"
#include "qlparser/qtdomainoperation.hh"
#include "qlparser/qtmarrayop2.hh"
#include "qlparser/qtpointop.hh"
#include "qlparser/symtab.hh"

// forward declarations
class MDDObj;
class QtONCStream;
class QtDomainOperation;


/*************************************************************
 *
 *
 * INCLUDE: querytree.hh
 *
 * MODULE:  qlparser
 * CLASS:   QueryTree
 *
 * COMMENTS:
 *
 ************************************************************/

//@ManMemo: Module: {\bf qlparser}

/*@Doc:

The Query Tree is the internal representation of a RasML query string. It
consists of instances of the above class hierarchy. The tree is created in
the Query Parser Modul in the action sections of the yacc grammar. According
to the parser semantics, the tree is built bottom up which means that the leaf
objects are created first. They are given up via the parse stack. Inner objects
get references to the subobjects after they are created. The whole tree is a
dynamic data structure. The semantics is that if an object is deleted also its
subobjects are deleted which means that the deletion of the root object cleans
up the whole tree.

The class QueryTree encapsulates a query tree consisting of suclasses of type
QtNode. It consists of the entry point and methods working on the whole tree
(e.g. traversing).

*/

class QueryTree
{
  public:


    enum OptimizationLevel
    {
	NO_OPTIMIZATIONS = 0,
	STANDARDIZATION = 1,
	SIMPLIFICATION = 2,
	SUBEXPRESSIONS = 3
    };

    /// default constructor
    QueryTree();

    /// constructor getting the root of the query tree
    QueryTree( QtNode* root );

    /// destructor (deletes the whole query tree)
    ~QueryTree();

    /// checks semantics (e.g., type checking)
    void checkSemantics();

    /// optimizes the tree
    void optimize( unsigned int optimizationLevel );

    /*@Doc:
      The method optimizes the query tree.
    */

    /// recognize common subexpressions
    vector<QtNode::QtNodeList>* seeSubexpression();

    /*@Doc:
      The method returns a list of all common subexpressions in the query tree.
      The private methods seeSubexpression(..) are used.
    */

    /// build in common subexpressions in the query tree
    void insertSubexpression( vector<QtNode::QtNodeList>* nodeList );

    /*@Doc:
      The method manipulates the query tree to handle common subexpressions.
    */

    /// executes a retrieval tree and gives back the result collection
    vector<QtData*>* evaluateRetrieval() throw (r_Error, ParseInfo);

    /*@Doc:
      The method evaluates a retrieval tree and returns the result collection. For this purpose,
      first, the {\tt open()} message is sent to the root node of the tree. Then {\tt next()}
      is invoked on the root node which, each time, returns one element of the result
      collection. It indicates the end of the evaluation process through returning a null pointer.
      At the end, {\tt close()} is called to clean up the ressources. If errors occur, various exceptions
      are thrown.
    */

    /// executes an update tree and throws a ParseInfo if query does not begin with INSERT, DELETE, UPDATE, ...
    void evaluateUpdate() throw (r_Error,ParseInfo);

    /// debugging method
    void printTree( int tab, ostream& s = cout );

    //@Man: read/write methods
    //@{
    ///

      ///
      inline QtNode* getRoot() const;
      ///
      inline void setRoot( QtNode* root );
      ///
      inline void setOptimizationLevel( unsigned int level );
      ///
      inline unsigned int getOptimizationLevel();

    ///
    //@}

    //@Man: methods used to maintain pointers to dynamic data structures used in the parse process
    //@{
    ///

      ///
      void addDynamicObject( QtNode* );
      ///
      void removeDynamicObject( QtNode* );
      ///
      void addDynamicObject( QtData* );
      ///
      void removeDynamicObject( QtData* );
      ///
      void addDynamicObject( ParseInfo* );
      ///
      void removeDynamicObject( ParseInfo* );
      ///
      void addDynamicObject( vector<QtONCStream*>* );
      ///
      void removeDynamicObject( vector<QtONCStream*>* );
      ///
      void releaseDynamicObjects();      
      ///
      void addDomainObject( QtDomainOperation * );
      ///
      void removeDomainObject( QtDomainOperation * );
      ///
      void rewriteDomainObjects(r_Minterval *greatDomain, std::string *greatIterator, QtMarrayOp2::mddIntervalListType *greatList);
      ///
      void printDomainObjects();
      ///
      void releaseDomainObjects();
      ///
      void addCString( char * );
    ///
    //@}

    ///
    static SymbolTable<int> symtab;
      
  private:
    /// attribute storing the root of the query tree
    QtNode* rootNode;

    static void (*optimizationFnc)(unsigned int, QtNode*);

    // Validity checks for the level of optimization
    bool isValidOptimizationLevel( int level );

    /// used by public seeSubexpression()
    vector<QtNode::QtNodeList>* seeSubexpression( QtNode::QtNodeList* leafList );

    /// used by public seeSubexpression()
    QtNode::QtNodeList* seeSubexpression( QtNode::QtNodeList* leafList, vector<QtNode::QtNodeList>* leafListsNew );

    /// level of optimization restricted for this query
    unsigned int optimizationLevel;

    /// attribute carrying next free number for CSE iterator
    static unsigned int nextCSENo;

    /// list of unlinked subtrees
    std::list<QtNode*> qtNodeList;
    /**
      This list is used to store subtrees of type \Ref{QtNode} generated in the parse 
      process and not linked to the result tree yet. In case of an error 
      during the parse process, this list is freed.
    */

    /// list of unlinked subtrees
    std::list<QtData*> qtDataList;
    /**
      This list is used to store subtrees of type \Ref{QtData} generated in the parse 
      process and not linked to the result tree yet. In case of an error 
      during the parse process, this list is freed.
    */

    /// list of unlinked subtrees
    std::list<ParseInfo*> parseInfoList;
    /**
      This list is used to store elements of type \Ref{ParseInfo} generated in the parse 
      process and not linked to the result tree yet. In case of an error 
      during the parse process, this list is freed.
    */

    /// list of unlinked lists
    std::list<vector<QtONCStream*>*> vectorList;
    /**
      This list is used to store elements of type \Ref{vector<QtONCStream*>} generated 
      in the parse process and not linked to the result tree yet. In case of an error 
      during the parse process, this list is freed.
    */

    /// list of domain operations relevant to variables in an MArray.
    std::list<QtDomainOperation *> dopList; // contains basically QtDomainOperation (everything else is evil :-) )
    /**
      This list is used to store elements of type \Ref{QtDomainOperation *} generated 
      in the parse process for the purpose of a tree rewrite. In case of an error 
      during the parse process, this list is freed.
    */

    /// list of lexed c-strings
    std::list<char *> lexedCStringList;
    /**
      This list is used to store elements of type char* generated during the lexing 
      process and not freed yet. In case of an error this list is freed.
    */

};

#include "querytree.icc"

#endif