casacore
TaQLNodeHandler.h
Go to the documentation of this file.
1 //# TaQLNodeHandler.h: Classes to handle the nodes in the raw TaQL parse tree
2 //# Copyright (C) 2005
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id$
27 
28 #ifndef TABLES_TAQLNODEHANDLER_H
29 #define TABLES_TAQLNODEHANDLER_H
30 
31 //# Includes
32 #include <casacore/casa/aips.h>
33 #include <casacore/tables/TaQL/TaQLNodeVisitor.h>
34 #include <casacore/tables/TaQL/TaQLNodeDer.h>
35 #include <casacore/tables/TaQL/TableParse.h>
36 #include <casacore/tables/TaQL/ExprNode.h>
37 #include <casacore/tables/TaQL/ExprNodeSet.h>
38 #include <casacore/casa/Containers/Record.h>
39 #include <vector>
40 
41 namespace casacore { //# NAMESPACE CASACORE - BEGIN
42 
43 //# Forward Declarations
44 class TaQLNodeHRValue;
45 
46 
47 // <summary>
48 // Class to handle the nodes in the raw TaQL parse tree.
49 // </summary>
50 
51 // <use visibility=local>
52 
53 // <reviewed reviewer="" date="" tests="tTableGram">
54 // </reviewed>
55 
56 // <prerequisite>
57 //# Classes you should understand before using this one.
58 // <li> <linkto class=TaQLNode>TaQLNode</linkto>
59 // <li> Note 199 describing
60 // <a href="../notes/199.html">
61 // TaQL</a>
62 // </prerequisite>
63 
64 // <synopsis>
65 // TaQLNodeHandler is a specialization of class
66 // <linkto class=TaQLNodeVisitor>TaQLNodeVisitor</linkto>.
67 // It processes the raw TaQL parse tree generated by TableGram.
68 // The processing is done in a recursive way. It starts at the top
69 // (which is a SELECT, UPDATE, etc. expression) and the processing
70 // results of a query are stored in a TableParseSelect object.
71 // These objects are kept in a stack for possible nested queries.
72 // After a query is fully processed, it is executed. Usually the result
73 // is a table; only a CALC command gives a TableExprNode as result.
74 // </synopsis>
75 
76 // <motivation>
77 // Separating the raw query parsing from the query processing has
78 // several advantages compared to the old situation where parsing
79 // and processing were combined.
80 // <ul>
81 // <li> The full command is parsed before any processing is done.
82 // So in case of a parse error, no possibly expensive processing
83 // has been done yet.
84 // <li> In the future query optimization can be done in an easier way.
85 // <li> Nested parsing is not possible. In case a Table is opened
86 // with a virtual TaQL column, the parsing of that TaQL string
87 // does not interfere with parsing the TaQL command.
88 // <li> It is possible to use expressions in the column list.
89 // That could not be done before, because the column list was
90 // parsed/processed before the table list.
91 // </ul>
92 // </motivation>
93 
95 {
96 public:
97  virtual ~TaQLNodeHandler();
98 
99  // Handle and process the raw parse tree.
100  // The result contains a Table or TableExprNode object.
101  TaQLNodeResult handleTree (const TaQLNode& tree,
102  const std::vector<const Table*>&);
103 
104  // Define the functions to visit each node type.
105  // <group>
106  virtual TaQLNodeResult visitConstNode (const TaQLConstNodeRep& node);
107  virtual TaQLNodeResult visitRegexNode (const TaQLRegexNodeRep& node);
108  virtual TaQLNodeResult visitUnaryNode (const TaQLUnaryNodeRep& node);
109  virtual TaQLNodeResult visitBinaryNode (const TaQLBinaryNodeRep& node);
110  virtual TaQLNodeResult visitMultiNode (const TaQLMultiNodeRep& node);
111  virtual TaQLNodeResult visitFuncNode (const TaQLFuncNodeRep& node);
112  virtual TaQLNodeResult visitRangeNode (const TaQLRangeNodeRep& node);
113  virtual TaQLNodeResult visitIndexNode (const TaQLIndexNodeRep& node);
114  virtual TaQLNodeResult visitKeyColNode (const TaQLKeyColNodeRep& node);
115  virtual TaQLNodeResult visitTableNode (const TaQLTableNodeRep& node);
116  virtual TaQLNodeResult visitColNode (const TaQLColNodeRep& node);
117  virtual TaQLNodeResult visitColumnsNode (const TaQLColumnsNodeRep& node);
118  virtual TaQLNodeResult visitJoinNode (const TaQLJoinNodeRep& node);
119  virtual TaQLNodeResult visitGroupNode (const TaQLGroupNodeRep& node);
120  virtual TaQLNodeResult visitSortKeyNode (const TaQLSortKeyNodeRep& node);
121  virtual TaQLNodeResult visitSortNode (const TaQLSortNodeRep& node);
123  virtual TaQLNodeResult visitGivingNode (const TaQLGivingNodeRep& node);
124  virtual TaQLNodeResult visitUpdExprNode (const TaQLUpdExprNodeRep& node);
125  virtual TaQLNodeResult visitSelectNode (const TaQLSelectNodeRep& node);
126  virtual TaQLNodeResult visitUpdateNode (const TaQLUpdateNodeRep& node);
127  virtual TaQLNodeResult visitInsertNode (const TaQLInsertNodeRep& node);
128  virtual TaQLNodeResult visitDeleteNode (const TaQLDeleteNodeRep& node);
129  virtual TaQLNodeResult visitCountNode (const TaQLCountNodeRep& node);
130  virtual TaQLNodeResult visitCalcNode (const TaQLCalcNodeRep& node);
131  virtual TaQLNodeResult visitCreTabNode (const TaQLCreTabNodeRep& node);
132  virtual TaQLNodeResult visitColSpecNode (const TaQLColSpecNodeRep& node);
133  virtual TaQLNodeResult visitRecFldNode (const TaQLRecFldNodeRep& node);
134  virtual TaQLNodeResult visitUnitNode (const TaQLUnitNodeRep& node);
135  // </group>
136 
137  // Get the actual result object from the result.
138  static const TaQLNodeHRValue& getHR (const TaQLNodeResult&);
139 
140 private:
141  // Push a new TableParseSelect on the stack.
143 
144  // Get the top of the TableParseSelect stack.
145  TableParseSelect* topStack() const;
146 
147  // Pop the top from the TableParseSelect stack.
148  void popStack();
149 
150  // Clear the select stack.
151  void clearStack();
152 
153  // Handle the select command.
154  // Optionally the command is not executed (needed for the EXISTS operator).
155  TaQLNodeResult handleSelect (const TaQLSelectNodeRep& node, Bool doExec);
156 
157  // Handle a MultiNode containing table info.
158  void handleTables (const TaQLMultiNode&);
159 
160  // Handle the WHERE clause.
161  void handleWhere (const TaQLNode&);
162 
163  // Handle the HAVING clause.
164  void handleHaving (const TaQLNode&);
165 
166  // Handle the UPDATE SET clause.
167  void handleUpdate (const TaQLMultiNode&);
168 
169  // Handle the INSERT columns.
170  void handleInsCol (const TaQLMultiNode&);
171 
172  // Handle the INSERT values.
173  void handleInsVal (const TaQLNode&);
174 
175  // Handle a column specification in a create table.
176  void handleColSpec (const TaQLMultiNode&);
177 
178  // Handle a record specification.
180 
181  // Handle a record field and add it to the Record.
182  void handleRecFld (const TaQLNode&, Record&);
183 
184  // Handle a record field with multiple values and add it to the Record.
185  // The field can be a record or a vector of values.
186  void handleMultiRecFld (const String& fldName,
187  const TaQLMultiNodeRep* node,
188  Record& rec);
189 
190  // Determine 'highest' constant data type and check if they match.
191  int checkConstDtype (int dt1, int dt2);
192 
193 
194  //# Use vector instead of stack because it has random access
195  //# (which is used in TableParse.cc).
196  std::vector<TableParseSelect*> itsStack;
197  //# The temporary tables referred to by $i in the TaQL string.
198  std::vector<const Table*> itsTempTables;
199 };
200 
201 
202 // <summary>
203 // Class containing the result value of the handling of a TaQLNode.
204 // </summary>
205 
206 // <use visibility=local>
207 
208 // <reviewed reviewer="" date="" tests="tTableGram">
209 // </reviewed>
210 
211 // <prerequisite>
212 //# Classes you should understand before using this one.
213 // <li> <linkto class=TaQLNode>TaQLNodeResult</linkto>
214 // <li> <linkto class=TaQLNode>TaQLNodeHandler</linkto>
215 // <li> Note 199 describing
216 // <a href="../notes/199.html">
217 // TaQL</a>
218 // </prerequisite>
219 
220 // <synopsis>
221 // TaQLNodeHRValue is a specialization of class
222 // <linkto class=TaQLNodeResultRep>TaQLNodeResultRep</linkto>.
223 // It contains the values resulting from handling a particular node.
224 // The object is effectively a collection of all possible values that
225 // need to be returned. Which values are filled in, depends on which node
226 // has been processed.
227 // <note> The getHR function in TaQLNodeHandler is very useful to
228 // extract/cast the TaQLNodeHRValue object from the general
229 // TaQLNodeResult object.
230 // </note>
231 // </synopsis>
232 
234 {
235 public:
237  : itsInt(-1), itsElem(0), itsSet(0), itsNames(0) {}
239  : itsInt(-1), itsExpr(expr), itsElem(0), itsSet(0), itsNames(0) {}
240  virtual ~TaQLNodeHRValue();
241 
242  // Get the values.
243  // <group>
244  Int getInt() const
245  { return itsInt; }
246  const String& getString() const
247  { return itsString; }
248  const String& getAlias() const
249  { return itsAlias; }
250  const String& getDtype() const
251  { return itsDtype; }
252  const Record& getRecord() const
253  { return itsRecord; }
254  const Table& getTable() const
255  { return itsTable; }
256  const TableExprNode& getExpr() const
257  { return itsExpr; }
259  { return itsElem; }
261  { return *itsSet; }
262  const Vector<String>* getNames() const
263  { return itsNames; }
264  // </group>
265 
266  // Set the values.
267  // If a pointer is given, it takes over the pointer.
268  // <group>
269  void setInt (Int ival)
270  { itsInt = ival; }
271  void setString (const String& str)
272  { itsString = str; }
273  void setAlias (const String& alias)
274  { itsAlias = alias; }
275  void setDtype (const String& dtype)
276  { itsDtype = dtype; }
277  void setRecord (const Record& record)
278  { itsRecord = record; }
279  void setTable (const Table& table)
280  { itsTable = table; }
281  void setExpr (const TableExprNode& expr)
282  { itsExpr = expr; }
284  { itsElem = elem; }
286  { itsSet = set; }
287  void setNames (Vector<String>* names)
288  { itsNames = names; }
289  // </group>
290 
291 private:
302 };
303 
304 
305 //# This function can only be implemented after TaQLNodeHRBase is declared.
307 {
308  return *(TaQLNodeHRValue*)(res.getRep());
309 }
310 
311 
312 
313 } //# NAMESPACE CASACORE - END
314 
315 #endif
virtual TaQLNodeResult visitKeyColNode(const TaQLKeyColNodeRep &node)
void setExpr(const TableExprNode &expr)
A 1-D Specialization of the Array class.
Definition: ArrayIO.h:45
Raw TaQL parse tree node defining a count command.
Definition: TaQLNodeDer.h:904
int Int
Definition: aipstype.h:47
void popStack()
Pop the top from the TableParseSelect stack.
std::vector< TableParseSelect * > itsStack
const TableExprNode & getExpr() const
Abstract base class to hold the result of a visit to the node tree.
TableExprNodeSetElem * itsElem
void setAlias(const String &alias)
void setNames(Vector< String > *names)
Raw TaQL parse tree node defining a select command.
Definition: TaQLNodeDer.h:864
Main interface class to a read/write table.
Definition: Table.h:149
void setElem(TableExprNodeSetElem *elem)
Raw TaQL parse tree node defining an update command.
Definition: TaQLNodeDer.h:937
void setString(const String &str)
virtual TaQLNodeResult visitConstNode(const TaQLConstNodeRep &node)
Define the functions to visit each node type.
virtual TaQLNodeResult visitDeleteNode(const TaQLDeleteNodeRep &node)
Class to hold multiple table expression nodes.
Definition: ExprNodeSet.h:305
virtual TaQLNodeResult visitTableNode(const TaQLTableNodeRep &node)
Handle class for a table column expression tree.
Definition: ExprNode.h:578
void setExprSet(TableExprNodeSet *set)
void setRecord(const Record &record)
Raw TaQL parse tree node defining a range.
Definition: TaQLNodeDer.h:355
virtual TaQLNodeResult visitInsertNode(const TaQLInsertNodeRep &node)
Raw TaQL parse tree node defining a constant value.
Definition: TaQLNodeDer.h:61
virtual TaQLNodeResult visitSelectNode(const TaQLSelectNodeRep &node)
Raw TaQL parse tree node defining a create table command.
Definition: TaQLNodeDer.h:1082
void handleHaving(const TaQLNode &)
Handle the HAVING clause.
Raw TaQL parse tree node defining an index in a array.
Definition: TaQLNodeDer.h:399
int checkConstDtype(int dt1, int dt2)
Determine &#39;highest&#39; constant data type and check if they match.
virtual TaQLNodeResult visitSortNode(const TaQLSortNodeRep &node)
Raw TaQL parse tree node defining a calc command.
Definition: TaQLNodeDer.h:1045
virtual TaQLNodeResult visitCreTabNode(const TaQLCreTabNodeRep &node)
Int getInt() const
Get the values.
Raw TaQL parse tree node defining a list of nodes.
Definition: TaQLNodeDer.h:275
virtual TaQLNodeResult visitUpdExprNode(const TaQLUpdExprNodeRep &node)
Record handleRecord(const TaQLMultiNodeRep *)
Handle a record specification.
void clearStack()
Clear the select stack.
const Record & getRecord() const
std::vector< const Table * > itsTempTables
Select-class for flex/bison scanner/parser for TableParse.
Definition: TableParse.h:346
Class to handle the nodes in the raw TaQL parse tree.
Raw TaQL parse tree node defining a delete command.
Definition: TaQLNodeDer.h:1010
void handleUpdate(const TaQLMultiNode &)
Handle the UPDATE SET clause.
TableParseSelect * pushStack(TableParseSelect::CommandType)
Push a new TableParseSelect on the stack.
virtual TaQLNodeResult visitRegexNode(const TaQLRegexNodeRep &node)
Envelope class for a node containing a select command.
Definition: TaQLNode.h:223
Raw TaQL parse tree node defining a table.
Definition: TaQLNodeDer.h:497
Class to hold the table expression nodes for an element in a set.
Definition: ExprNodeSet.h:93
virtual TaQLNodeResult visitMultiNode(const TaQLMultiNodeRep &node)
virtual TaQLNodeResult visitRangeNode(const TaQLRangeNodeRep &node)
virtual TaQLNodeResult visitGivingNode(const TaQLGivingNodeRep &node)
Raw TaQL parse tree node defining a groupby list.
Definition: TaQLNodeDer.h:598
Raw TaQL parse tree node defining a giving expression list.
Definition: TaQLNodeDer.h:737
Raw TaQL parse tree node defining a unary operator.
Definition: TaQLNodeDer.h:176
Raw TaQL parse tree node defining a constant regex value.
Definition: TaQLNodeDer.h:136
Raw TaQL parse tree node defining a record field.
Definition: TaQLNodeDer.h:1149
const TableExprNodeSet & getExprSet() const
virtual TaQLNodeResult visitJoinNode(const TaQLJoinNodeRep &node)
virtual TaQLNodeResult visitBinaryNode(const TaQLBinaryNodeRep &node)
TaQLNodeResult handleSelect(const TaQLSelectNodeRep &node, Bool doExec)
Handle the select command.
const String & getAlias() const
virtual TaQLNodeResult visitIndexNode(const TaQLIndexNodeRep &node)
Raw TaQL parse tree node defining a keyword or column name.
Definition: TaQLNodeDer.h:465
virtual TaQLNodeResult visitSortKeyNode(const TaQLSortKeyNodeRep &node)
virtual TaQLNodeResult visitUnaryNode(const TaQLUnaryNodeRep &node)
A hierarchical collection of named fields of various types.
Definition: Record.h:181
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:39
Raw TaQL parse tree node defining a function.
Definition: TaQLNodeDer.h:320
virtual TaQLNodeResult visitUnitNode(const TaQLUnitNodeRep &node)
void handleInsVal(const TaQLNode &)
Handle the INSERT values.
void handleColSpec(const TaQLMultiNode &)
Handle a column specification in a create table.
virtual TaQLNodeResult visitRecFldNode(const TaQLRecFldNodeRep &node)
void setTable(const Table &table)
static const TaQLNodeHRValue & getHR(const TaQLNodeResult &)
Get the actual result object from the result.
const TaQLNodeResultRep * getRep() const
Get the actual underlying object.
Raw TaQL parse tree node defining a create column specification.
Definition: TaQLNodeDer.h:1116
virtual TaQLNodeResult visitFuncNode(const TaQLFuncNodeRep &node)
Raw TaQL parse tree node defining a unit.
Definition: TaQLNodeDer.h:1180
TableParseSelect * topStack() const
Get the top of the TableParseSelect stack.
Raw TaQL parse tree node defining a column update expression.
Definition: TaQLNodeDer.h:781
virtual TaQLNodeResult visitColumnsNode(const TaQLColumnsNodeRep &node)
virtual TaQLNodeResult visitColSpecNode(const TaQLColSpecNodeRep &node)
void handleRecFld(const TaQLNode &, Record &)
Handle a record field and add it to the Record.
const String & getString() const
Vector< String > * itsNames
void setDtype(const String &dtype)
const String & getDtype() const
Envelope class to hold the result of a visit to the node tree.
const TableExprNodeSetElem * getElem() const
Raw TaQL parse tree node defining a limit/offset expression.
Definition: TaQLNodeDer.h:705
TaQLNodeHRValue(const TableExprNode &expr)
void handleTables(const TaQLMultiNode &)
Handle a MultiNode containing table info.
TableExprNodeSet * itsSet
Raw TaQL parse tree node defining a sort key.
Definition: TaQLNodeDer.h:633
Raw TaQL parse tree node defining a sort list.
Definition: TaQLNodeDer.h:669
virtual TaQLNodeResult visitLimitOffNode(const TaQLLimitOffNodeRep &node)
String: the storage and methods of handling collections of characters.
Definition: String.h:223
Raw TaQL parse tree node defining a select column expression.
Definition: TaQLNodeDer.h:532
void setInt(Int ival)
Set the values.
const Table & getTable() const
Raw TaQL parse tree node defining a select column list.
Definition: TaQLNodeDer.h:566
virtual TaQLNodeResult visitGroupNode(const TaQLGroupNodeRep &node)
Raw TaQL parse tree node defining a join operation.
Definition: TaQLNodeDer.h:433
Envelope class for a node in the raw TaQL parse tree.
Definition: TaQLNode.h:81
void handleMultiRecFld(const String &fldName, const TaQLMultiNodeRep *node, Record &rec)
Handle a record field with multiple values and add it to the Record.
void handleInsCol(const TaQLMultiNode &)
Handle the INSERT columns.
Class to visit the nodes in the raw TaQL parse tree.
Class containing the result value of the handling of a TaQLNode.
TaQLNodeResult handleTree(const TaQLNode &tree, const std::vector< const Table * > &)
Handle and process the raw parse tree.
virtual TaQLNodeResult visitCalcNode(const TaQLCalcNodeRep &node)
void handleWhere(const TaQLNode &)
Handle the WHERE clause.
Raw TaQL parse tree node defining a binary operator.
Definition: TaQLNodeDer.h:216
virtual TaQLNodeResult visitColNode(const TaQLColNodeRep &node)
this file contains all the compiler specific defines
Definition: mainpage.dox:28
virtual TaQLNodeResult visitCountNode(const TaQLCountNodeRep &node)
Raw TaQL parse tree node defining an insert command.
Definition: TaQLNodeDer.h:976
virtual TaQLNodeResult visitUpdateNode(const TaQLUpdateNodeRep &node)
const Vector< String > * getNames() const