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 <casacore/casa/Containers/ValueHolder.h>
40 #include <vector>
41 
42 namespace casacore { //# NAMESPACE CASACORE - BEGIN
43 
44 //# Forward Declarations
45 class TaQLNodeHRValue;
46 
47 
48 // <summary>
49 // Class to handle the nodes in the raw TaQL parse tree.
50 // </summary>
51 
52 // <use visibility=local>
53 
54 // <reviewed reviewer="" date="" tests="tTableGram">
55 // </reviewed>
56 
57 // <prerequisite>
58 //# Classes you should understand before using this one.
59 // <li> <linkto class=TaQLNode>TaQLNode</linkto>
60 // <li> Note 199 describing
61 // <a href="../notes/199.html">
62 // TaQL</a>
63 // </prerequisite>
64 
65 // <synopsis>
66 // TaQLNodeHandler is a specialization of class
67 // <linkto class=TaQLNodeVisitor>TaQLNodeVisitor</linkto>.
68 // It processes the raw TaQL parse tree generated by TableGram.
69 // The processing is done in a recursive way. It starts at the top
70 // (which is a SELECT, UPDATE, etc. expression) and the processing
71 // results of a query are stored in a TableParseSelect object.
72 // These objects are kept in a stack for possible nested queries.
73 // After a query is fully processed, it is executed. Usually the result
74 // is a table; only a CALC command gives a TableExprNode as result.
75 // </synopsis>
76 
77 // <motivation>
78 // Separating the raw query parsing from the query processing has
79 // several advantages compared to the old situation where parsing
80 // and processing were combined.
81 // <ul>
82 // <li> The full command is parsed before any processing is done.
83 // So in case of a parse error, no possibly expensive processing
84 // has been done yet.
85 // <li> In the future query optimization can be done in an easier way.
86 // <li> Nested parsing is not possible. In case a Table is opened
87 // with a virtual TaQL column, the parsing of that TaQL string
88 // does not interfere with parsing the TaQL command.
89 // <li> It is possible to use expressions in the column list.
90 // That could not be done before, because the column list was
91 // parsed/processed before the table list.
92 // </ul>
93 // </motivation>
94 
96 {
97 public:
98  virtual ~TaQLNodeHandler();
99 
100  // Handle and process the raw parse tree.
101  // The result contains a Table or TableExprNode object.
102  TaQLNodeResult handleTree (const TaQLNode& tree,
103  const std::vector<const Table*>&);
104 
105  // Define the functions to visit each node type.
106  // <group>
107  virtual TaQLNodeResult visitConstNode (const TaQLConstNodeRep& node);
108  virtual TaQLNodeResult visitRegexNode (const TaQLRegexNodeRep& node);
109  virtual TaQLNodeResult visitUnaryNode (const TaQLUnaryNodeRep& node);
110  virtual TaQLNodeResult visitBinaryNode (const TaQLBinaryNodeRep& node);
111  virtual TaQLNodeResult visitMultiNode (const TaQLMultiNodeRep& node);
112  virtual TaQLNodeResult visitFuncNode (const TaQLFuncNodeRep& node);
113  virtual TaQLNodeResult visitRangeNode (const TaQLRangeNodeRep& node);
114  virtual TaQLNodeResult visitIndexNode (const TaQLIndexNodeRep& node);
115  virtual TaQLNodeResult visitKeyColNode (const TaQLKeyColNodeRep& node);
116  virtual TaQLNodeResult visitTableNode (const TaQLTableNodeRep& node);
117  virtual TaQLNodeResult visitColNode (const TaQLColNodeRep& node);
118  virtual TaQLNodeResult visitColumnsNode (const TaQLColumnsNodeRep& node);
119  virtual TaQLNodeResult visitJoinNode (const TaQLJoinNodeRep& node);
120  virtual TaQLNodeResult visitGroupNode (const TaQLGroupNodeRep& node);
121  virtual TaQLNodeResult visitSortKeyNode (const TaQLSortKeyNodeRep& node);
122  virtual TaQLNodeResult visitSortNode (const TaQLSortNodeRep& node);
124  virtual TaQLNodeResult visitGivingNode (const TaQLGivingNodeRep& node);
125  virtual TaQLNodeResult visitUpdExprNode (const TaQLUpdExprNodeRep& node);
126  virtual TaQLNodeResult visitSelectNode (const TaQLSelectNodeRep& node);
127  virtual TaQLNodeResult visitUpdateNode (const TaQLUpdateNodeRep& node);
128  virtual TaQLNodeResult visitInsertNode (const TaQLInsertNodeRep& node);
129  virtual TaQLNodeResult visitDeleteNode (const TaQLDeleteNodeRep& node);
130  virtual TaQLNodeResult visitCountNode (const TaQLCountNodeRep& node);
131  virtual TaQLNodeResult visitCalcNode (const TaQLCalcNodeRep& node);
132  virtual TaQLNodeResult visitCreTabNode (const TaQLCreTabNodeRep& node);
133  virtual TaQLNodeResult visitColSpecNode (const TaQLColSpecNodeRep& node);
134  virtual TaQLNodeResult visitRecFldNode (const TaQLRecFldNodeRep& node);
135  virtual TaQLNodeResult visitUnitNode (const TaQLUnitNodeRep& node);
136  virtual TaQLNodeResult visitAltTabNode (const TaQLAltTabNodeRep& node);
137  virtual TaQLNodeResult visitAddColNode (const TaQLAddColNodeRep& node);
138  virtual TaQLNodeResult visitSetKeyNode (const TaQLSetKeyNodeRep& node);
139  virtual TaQLNodeResult visitRenDropNode (const TaQLRenDropNodeRep& node);
140  virtual TaQLNodeResult visitAddRowNode (const TaQLAddRowNodeRep& node);
141  virtual TaQLNodeResult visitConcTabNode (const TaQLConcTabNodeRep& node);
142  virtual TaQLNodeResult visitShowNode (const TaQLShowNodeRep& node);
143  // </group>
144 
145  // Get the actual result object from the result.
146  static const TaQLNodeHRValue& getHR (const TaQLNodeResult&);
147 
148 private:
149  // Push a new TableParseSelect on the stack.
151 
152  // Get the top of the TableParseSelect stack.
153  TableParseSelect* topStack() const;
154 
155  // Pop the top from the TableParseSelect stack.
156  void popStack();
157 
158  // Clear the select stack.
159  void clearStack();
160 
161  // Handle the select command.
162  // Optionally the command is not executed (needed for the EXISTS operator).
163  TaQLNodeResult handleSelect (const TaQLSelectNodeRep& node, Bool doExec);
164 
165  // Handle a MultiNode containing table info.
166  void handleTables (const TaQLMultiNode&);
167 
168  // Make a ConcatTable from a nested set of tables.
169  Table makeConcatTable (const TaQLMultiNodeRep& node);
170 
171  // Handle the WHERE clause.
172  void handleWhere (const TaQLNode&);
173 
174  // Handle the HAVING clause.
175  void handleHaving (const TaQLNode&);
176 
177  // Handle the UPDATE SET clause.
178  void handleUpdate (const TaQLMultiNode&);
179 
180  // Handle the INSERT columns.
181  void handleInsCol (const TaQLMultiNode&);
182 
183  // Handle the INSERT values.
184  void handleInsVal (const TaQLNode&);
185 
186  // Handle a column specification in a create table.
187  void handleColSpecs (const TaQLMultiNode&);
188 
189  // Handle a Multi RecFld representing a Record.
190  Record handleMultiRecFld (const TaQLNode& node);
191 
192 
193  //# Use vector instead of stack because it has random access
194  //# (which is used in TableParse.cc).
195  std::vector<TableParseSelect*> itsStack;
196  //# The temporary tables referred to by $i in the TaQL string.
197  std::vector<const Table*> itsTempTables;
198 };
199 
200 
201 // <summary>
202 // Class containing the result value of the handling of a TaQLNode.
203 // </summary>
204 
205 // <use visibility=local>
206 
207 // <reviewed reviewer="" date="" tests="tTableGram">
208 // </reviewed>
209 
210 // <prerequisite>
211 //# Classes you should understand before using this one.
212 // <li> <linkto class=TaQLNode>TaQLNodeResult</linkto>
213 // <li> <linkto class=TaQLNode>TaQLNodeHandler</linkto>
214 // <li> Note 199 describing
215 // <a href="../notes/199.html">
216 // TaQL</a>
217 // </prerequisite>
218 
219 // <synopsis>
220 // TaQLNodeHRValue is a specialization of class
221 // <linkto class=TaQLNodeResultRep>TaQLNodeResultRep</linkto>.
222 // It contains the values resulting from handling a particular node.
223 // The object is effectively a collection of all possible values that
224 // need to be returned. Which values are filled in, depends on which node
225 // has been processed.
226 // <note> The getHR function in TaQLNodeHandler is very useful to
227 // extract/cast the TaQLNodeHRValue object from the general
228 // TaQLNodeResult object.
229 // </note>
230 // </synopsis>
231 
233 {
234 public:
236  : itsInt(-1), itsElem(0), itsSet(0), itsNames(0) {}
238  : itsInt(-1), itsExpr(expr), itsElem(0), itsSet(0), itsNames(0) {}
239  virtual ~TaQLNodeHRValue();
240 
241  // Get the values.
242  // <group>
243  Int getInt() const
244  { return itsInt; }
245  const String& getString() const
246  { return itsString; }
247  const String& getAlias() const
248  { return itsAlias; }
249  const String& getNameMask() const
250  { return itsNameMask; }
251  const String& getDtype() const
252  { return itsDtype; }
253  const Record& getRecord() const
254  { return itsRecord; }
256  { return itsVH; }
257  const Table& getTable() const
258  { return itsTable; }
259  const TableExprNode& getExpr() const
260  { return itsExpr; }
262  { return itsElem; }
264  { return *itsSet; }
265  const Vector<String>* getNames() const
266  { return itsNames; }
267  // </group>
268 
269  // Set the values.
270  // If a pointer is given, it takes over the pointer.
271  // <group>
272  void setInt (Int ival)
273  { itsInt = ival; }
274  void setString (const String& str)
275  { itsString = str; }
276  void setAlias (const String& alias)
277  { itsAlias = alias; }
278  void setNameMask (const String& nameMask)
279  { itsNameMask = nameMask; }
280  void setDtype (const String& dtype)
281  { itsDtype = dtype; }
282  void setRecord (const Record& record)
283  { itsRecord = record; }
284  void setValueHolder (const ValueHolder& vh)
285  { itsVH = vh; }
286  void setTable (const Table& table)
287  { itsTable = table; }
288  void setExpr (const TableExprNode& expr)
289  { itsExpr = expr; }
291  { itsElem = elem; }
293  { itsSet = set; }
294  void setNames (Vector<String>* names)
295  { itsNames = names; }
296  // </group>
297 
298 private:
311 };
312 
313 
314 //# This function can only be implemented after TaQLNodeHRBase is declared.
316 {
317  return *(TaQLNodeHRValue*)(res.getRep());
318 }
319 
320 
321 
322 } //# NAMESPACE CASACORE - END
323 
324 #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:852
int Int
Definition: aipstype.h:50
void popStack()
Pop the top from the TableParseSelect stack.
std::vector< TableParseSelect * > itsStack
Abstract base class to hold the result of a visit to the node tree.
TableExprNodeSetElem * itsElem
void setAlias(const String &alias)
const TableExprNodeSetElem * getElem() const
Raw TaQL parse tree node defining an alter table add rows command.
Definition: TaQLNodeDer.h:1266
void setNames(Vector< String > *names)
Raw TaQL parse tree node defining a select command.
Definition: TaQLNodeDer.h:807
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:885
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)
const Table & getTable() const
Class to hold multiple table expression nodes.
Definition: ExprNodeSet.h:306
virtual TaQLNodeResult visitTableNode(const TaQLTableNodeRep &node)
const String & getAlias() const
Handle class for a table column expression tree.
Definition: ExprNode.h:614
void setExprSet(TableExprNodeSet *set)
void setRecord(const Record &record)
Raw TaQL parse tree node defining a range.
Definition: TaQLNodeDer.h:329
Raw TaQL parse tree node defining an alter table set keyword command.
Definition: TaQLNodeDer.h:1238
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:1020
void handleHaving(const TaQLNode &)
Handle the HAVING clause.
const String & getString() const
Raw TaQL parse tree node defining an index in a array.
Definition: TaQLNodeDer.h:364
virtual TaQLNodeResult visitSortNode(const TaQLSortNodeRep &node)
Raw TaQL parse tree node defining a calc command.
Definition: TaQLNodeDer.h:986
virtual TaQLNodeResult visitCreTabNode(const TaQLCreTabNodeRep &node)
Raw TaQL parse tree node defining a list of nodes.
Definition: TaQLNodeDer.h:250
virtual TaQLNodeResult visitUpdExprNode(const TaQLUpdExprNodeRep &node)
void clearStack()
Clear the select stack.
virtual TaQLNodeResult visitAddRowNode(const TaQLAddRowNodeRep &node)
void handleColSpecs(const TaQLMultiNode &)
Handle a column specification in a create table.
std::vector< const Table * > itsTempTables
virtual TaQLNodeResult visitShowNode(const TaQLShowNodeRep &node)
Select-class for flex/bison scanner/parser for TableParse.
Definition: TableParse.h:380
Class to handle the nodes in the raw TaQL parse tree.
Raw TaQL parse tree node defining a delete command.
Definition: TaQLNodeDer.h:954
void handleUpdate(const TaQLMultiNode &)
Handle the UPDATE SET clause.
const TableExprNodeSet & getExprSet() const
TableParseSelect * pushStack(TableParseSelect::CommandType)
Push a new TableParseSelect on the stack.
virtual TaQLNodeResult visitRegexNode(const TaQLRegexNodeRep &node)
Envelope class for a node containing a list of nodes.
Definition: TaQLNode.h:223
Raw TaQL parse tree node defining a table.
Definition: TaQLNodeDer.h:457
const Vector< String > * getNames() const
const String & getDtype() const
Class to hold the table expression nodes for an element in a set.
Definition: ExprNodeSet.h:94
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:553
Raw TaQL parse tree node defining a giving expression list.
Definition: TaQLNodeDer.h:684
const String & getNameMask() const
const Record & getRecord() const
Raw TaQL parse tree node defining a unary operator.
Definition: TaQLNodeDer.h:155
Raw TaQL parse tree node defining a constant regex value.
Definition: TaQLNodeDer.h:118
Raw TaQL parse tree node defining a record field.
Definition: TaQLNodeDer.h:1084
A holder for a value of any basic Casacore data type.
Definition: ValueHolder.h:67
virtual TaQLNodeResult visitJoinNode(const TaQLJoinNodeRep &node)
virtual TaQLNodeResult visitBinaryNode(const TaQLBinaryNodeRep &node)
const TaQLNodeResultRep * getRep() const
Get the actual underlying object.
TaQLNodeResult handleSelect(const TaQLSelectNodeRep &node, Bool doExec)
Handle the select command.
Raw TaQL parse tree node defining an alter table command.
Definition: TaQLNodeDer.h:1294
virtual TaQLNodeResult visitIndexNode(const TaQLIndexNodeRep &node)
const TableExprNode & getExpr() const
Raw TaQL parse tree node defining a keyword or column name.
Definition: TaQLNodeDer.h:426
virtual TaQLNodeResult visitSortKeyNode(const TaQLSortKeyNodeRep &node)
virtual TaQLNodeResult visitRenDropNode(const TaQLRenDropNodeRep &node)
virtual TaQLNodeResult visitConcTabNode(const TaQLConcTabNodeRep &node)
virtual TaQLNodeResult visitUnaryNode(const TaQLUnaryNodeRep &node)
A hierarchical collection of named fields of various types.
Definition: Record.h:180
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
Raw TaQL parse tree node defining a function.
Definition: TaQLNodeDer.h:298
virtual TaQLNodeResult visitUnitNode(const TaQLUnitNodeRep &node)
void handleInsVal(const TaQLNode &)
Handle the INSERT values.
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.
Record handleMultiRecFld(const TaQLNode &node)
Handle a Multi RecFld representing a Record.
Raw TaQL parse tree node defining an alter table rename or drop command.
Definition: TaQLNodeDer.h:1209
Raw TaQL parse tree node defining a create column specification.
Definition: TaQLNodeDer.h:1053
virtual TaQLNodeResult visitSetKeyNode(const TaQLSetKeyNodeRep &node)
virtual TaQLNodeResult visitFuncNode(const TaQLFuncNodeRep &node)
Raw TaQL parse tree node defining a unit.
Definition: TaQLNodeDer.h:1119
Raw TaQL parse tree node defining a column update expression.
Definition: TaQLNodeDer.h:718
virtual TaQLNodeResult visitColumnsNode(const TaQLColumnsNodeRep &node)
TaQLNodeResult handleTree(const TaQLNode &tree, const std::vector< const Table *> &)
Handle and process the raw parse tree.
virtual TaQLNodeResult visitColSpecNode(const TaQLColSpecNodeRep &node)
void setNameMask(const String &nameMask)
Vector< String > * itsNames
Int getInt() const
Get the values.
void setValueHolder(const ValueHolder &vh)
void setDtype(const String &dtype)
Envelope class to hold the result of a visit to the node tree.
Raw TaQL parse tree node defining a limit/offset expression.
Definition: TaQLNodeDer.h:654
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:586
Raw TaQL parse tree node defining a sort list.
Definition: TaQLNodeDer.h:620
virtual TaQLNodeResult visitLimitOffNode(const TaQLLimitOffNodeRep &node)
Raw TaQL parse tree node defining an alter table add column command.
Definition: TaQLNodeDer.h:1180
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:490
void setInt(Int ival)
Set the values.
Raw TaQL parse tree node defining a show command.
Definition: TaQLNodeDer.h:1326
Raw TaQL parse tree node defining a select column list.
Definition: TaQLNodeDer.h:523
virtual TaQLNodeResult visitGroupNode(const TaQLGroupNodeRep &node)
Raw TaQL parse tree node defining a join operation.
Definition: TaQLNodeDer.h:396
Envelope class for a node in the raw TaQL parse tree.
Definition: TaQLNode.h:81
virtual TaQLNodeResult visitAltTabNode(const TaQLAltTabNodeRep &node)
void handleInsCol(const TaQLMultiNode &)
Handle the INSERT columns.
virtual TaQLNodeResult visitAddColNode(const TaQLAddColNodeRep &node)
const ValueHolder & getValueHolder() const
Class to visit the nodes in the raw TaQL parse tree.
Class containing the result value of the handling of a TaQLNode.
virtual TaQLNodeResult visitCalcNode(const TaQLCalcNodeRep &node)
TableParseSelect * topStack() const
Get the top of the TableParseSelect stack.
void handleWhere(const TaQLNode &)
Handle the WHERE clause.
Raw TaQL parse tree node defining a binary operator.
Definition: TaQLNodeDer.h:193
virtual TaQLNodeResult visitColNode(const TaQLColNodeRep &node)
this file contains all the compiler specific defines
Definition: mainpage.dox:28
Raw TaQL parse tree node defining an alter table command.
Definition: TaQLNodeDer.h:1148
virtual TaQLNodeResult visitCountNode(const TaQLCountNodeRep &node)
Raw TaQL parse tree node defining an insert command.
Definition: TaQLNodeDer.h:921
virtual TaQLNodeResult visitUpdateNode(const TaQLUpdateNodeRep &node)
Table makeConcatTable(const TaQLMultiNodeRep &node)
Make a ConcatTable from a nested set of tables.