casacore
Loading...
Searching...
No Matches
TableParseQuery.h
Go to the documentation of this file.
1//# TableParseQuery.h: Class getting the parser results and executing a query
2//# Copyright (C) 1994-2022
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: casa-feedback@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#ifndef TABLES_TABLEPARSEQUERY_H
27#define TABLES_TABLEPARSEQUERY_H
28
29//# Includes
30#include <casacore/casa/aips.h>
31#include <casacore/tables/TaQL/TableParseTableList.h>
32#include <casacore/tables/TaQL/TableParseJoin.h>
33#include <casacore/tables/TaQL/TableParseProject.h>
34#include <casacore/tables/TaQL/TableParseUpdate.h>
35#include <casacore/tables/TaQL/TableParseSortKey.h>
36#include <casacore/tables/TaQL/TableParseGroupby.h>
37#include <casacore/tables/Tables/Table.h>
38#include <casacore/tables/TaQL/ExprNode.h>
39#include <casacore/tables/TaQL/ExprGroup.h>
40#include <casacore/casa/Arrays/ArrayFwd.h>
41#include <casacore/casa/BasicSL/String.h>
42#include <casacore/casa/Utilities/Sort.h>
43#include <casacore/casa/Containers/Block.h>
44#include <vector>
45
46namespace casacore { //# NAMESPACE CASACORE - BEGIN
47
48 //# Forward Declarations
49 class TableExprNodeSet;
50 class TableExprNodeSetElem;
51 class TableExprNodeIndex;
52 class TableDesc;
53 class TableColumn;
54 class AipsIO;
55 class Record;
56 class TableRecord;
57 template<class T> class ArrayColumn;
58
59
60 // <summary>
61 // Class getting the parser results and executing a query
62 // </summary>
63
64 // <use visibility=local>
65
66 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
67 // </reviewed>
68
69 // <prerequisite>
70 //# Classes you should understand before using this one.
71 // <li> TableGram.ll and .yy (flex and bison grammar)
72 // <li> TaQLNodeHandler
73 // </prerequisite>
74
75 // <synopsis>
76 // The results of the bison parser TableGram.yy and flex scanner TableGram.ll
77 // are stored in a tree of TaQLNode objects. When the parsing of a TaQL command
78 // is fully done, the tree is traversed by TaQLNodeHandler which creates and
79 // fills a stack of TableParseQuery objects, one object per (nested) query.
80 // A nested query is executed once it is fully handled.
81 // </synopsis>
82
83 // <motivation>
84 // It is necessary to be able to give a table select command in ASCII.
85 // This can be used in a CLI or in the table browser to get a subset
86 // of a table or to sort a table.
87 // </motivation>
88
89 //# <todo asof="$DATE:$">
90 //# A List of bugs, limitations, extensions or planned refinements.
91 //# </todo>
92
94 {
95 public:
108
109 // Construct.
111
112 // Destructor.
114
115 // Return the command type.
117 { return commandType_p; }
118
119 // Return the expression node.
121 { return node_p; }
122
123 // Return the table name list.
126
129
130
131 // Execute the select command (select/sort/projection/groupby/having/giving).
132 // The setInGiving flag tells if a set in the GIVING part is allowed.
133 // The mustSelect flag tells if a SELECT command must do something.
134 // Usually that is required, but not for a SELECT in an INSERT command.
135 // Optionally the maximum nr of rows to be selected can be given.
136 // It will be used as the default value for the LIMIT clause.
137 // 0 = no maximum.
138 void execute (Bool showTimings, Bool setInGiving,
139 Bool mustSelect, rownr_t maxRow, Bool doTracing=False,
140 const std::vector<const Table*>& tempTables = std::vector<const Table*>(),
141 const std::vector<TableParseQuery*>& stack = std::vector<TableParseQuery*>());
142
143 // Execute a query in a FROM clause resulting in a Table.
144 Table doFromQuery (Bool showTimings);
145
146 // Execute a subquery and create an appropriate node for the result.
148
149 // Test if a subquery has sufficient elements.
150 // It uses default LIMIT=1, but that can be overidden in the subquery.
151 // The flag tells if NOT EXISTS or EXISTS was given.
152 TableExprNode doExists (Bool noexists, Bool showTimings);
153
154 // Show the expression tree.
155 void show (ostream& os) const;
156
157 // Create a temporary table if no tables are given in FROM.
159
160 // Keep the selection expression.
162
163 // Keep the groupby expressions.
164 // It checks if they are all scalar expressions.
165 void handleGroupby (const std::vector<TableExprNode>&, Bool rollup);
166
167 // Keep the having expression.
169
170 // Keep the expression of a calculate command.
172
173 // Handle the DROP TABLE command.
174 void handleDropTab (const std::vector<const Table*>& tempTables,
175 const std::vector<TableParseQuery*>& stack);
176
177 // Keep the create table command.
178 void handleCreTab (const Record& dmInfo,
179 const std::vector<const Table*>& tempTables,
180 const std::vector<TableParseQuery*>& stack);
181
182 // Keep the column specification in a create table command.
183 void handleColSpec (const String& columnName, const String& likeColName,
184 const String& dataType,
185 const Record& spec, Bool isCOrder=False);
186
187 // Reopen the table (for update) used in the ALTER TABLE command.
189
190 // Add columns to the table of ALTER TABLE.
191 // The column descriptions have already been added to tableDesc_p.
192 void handleAddCol (const Record& dmInfo);
193
194 // Handle copying of columns.
195 void handleCopyCol (Bool showTimings);
196
197 // Add a keyword or replace a keyword with a value.
198 // The keyword can be a table or column keyword (col::key).
199 // The data type string can be empty leaving the data type unchanged.
200 void handleSetKey (const String& name, const String& dtype,
201 const ValueHolder& value);
202
203 // Rename a table or column keyword.
204 void handleRenameKey (const String& oldName, const String& newName);
205
206 // Remove a table or column keyword.
207 void handleRemoveKey (const String& name);
208
209 // Keep the update expressions.
211
212 // Make ready for the insert expression.
213 // The first one uses values (added via addUpdate),
214 // the second one a subquery.
215 // <group>
218 // </group>
219
220 // Make ready for a COUNT command.
221 // It checks if all column expressions are scalar.
223
224 // Keep the sort expressions.
225 void handleSort (const std::vector<TableParseSortKey>& sortList,
226 Bool noDuplicates, Sort::Order defaultSortOrder);
227
228 // Evaluate and keep limit/offset/stride given as start:end:incr
230
231 // Evaluate and keep the limit value.
232 void handleLimit (const TableExprNode& expr);
233
234 // Evaluate and keep the offset value.
235 void handleOffset (const TableExprNode& expr);
236
237 // Evaluate and add the rows.
238 void handleAddRow (const TableExprNode& expr);
239
240 // Add a join object.
242
243 // Find the keyword or column name and create a TableExprNode from it.
244 // If <src>tryProj=True</src> it is first tried if the column is a column
245 // in the projected table (i.e., result from the SELECT part).
246 TableExprNode handleKeyCol (const String& name, Bool tryProj);
247
248 // Handle a slice operator.
250 const TableExprNodeSet& indices,
251 const TaQLStyle&);
252
253 // Handle a function.
255 const TableExprNodeSet& arguments,
256 const TaQLStyle&);
257
258 // Add a column to the list of column names.
259 void handleColumn (Int type, const String& name, const TableExprNode& expr,
260 const String& newName, const String& nameMask,
261 const String& newDtype);
262
263 // Finish the addition of columns to the list of column names.
264 void handleColumnFinish (Bool distinct);
265
266 // Handle the name and type given in a GIVING clause.
267 void handleGiving (const String& name, const Record& type);
268
269 // Handle the set given in a GIVING clause.
271
272 // Get the TableParseJoin objects.
273 const std::vector<TableParseJoin>& joins() const
274 { return joins_p; }
275
276 // Initialize the table and data manager descriptions.
277 void initDescriptions (const TableDesc&, const Record& dminfo);
278
279 // Add a keyword or replace a keyword with the value of another keyword.
280 // The keywords can be table or column keywords (col::key).
282
283 // Split the given name into optional shorthand, column and fields.
284 // Find the keywordset for it and fill in the final keyword name.
285 // It is a helper function for handleSetKey, etc.
286 // If update=True, rwKeywordSet() is used to ensure the table is updated.
287 TableRecord& findKeyword (const String& name, String& keyName,
288 Bool update=True);
289
290 // Add an update object.
291 void addUpdate (const std::shared_ptr<TableParseUpdate>& upd)
292 { update_p.push_back (upd); }
293
294 // Set the insert expressions for all rows.
295 void setInsertExprs (const std::vector<TableExprNode> exprs)
296 { insertExprs_p = exprs; }
297
298 // Replace the first table (used by CALC command).
299 void replaceTable (const Table& table);
300
301 // Set the DataManager info for a new table.
302 void setDMInfo (const Record& dminfo)
303 { tableProject_p.setDMInfo (dminfo); }
304
305 // Get the projected column names.
308
309 // Get the resulting table.
310 const Table& getTable() const
311 { return table_p; }
312
313 // Show the structure of fromTables_p[0] using the options given in parts[2:].
314 String getTableStructure (const Vector<String>& parts, const TaQLStyle& style);
315
316 // Add a column node to applySelNodes_p.
318 { applySelNodes_p.push_back (node); }
319
320 // Create a table using the given parameters.
321 // The variables set by handleGiven are used for name and type.
323 Int64 nrow, const Record& dmInfo,
324 const std::vector<const Table*>& tempTables,
325 const std::vector<TableParseQuery*>& stack);
326 private:
327 // Do the update step.
328 // Rows 0,1,2,.. in UpdTable are updated from the expression result
329 // for the rows in the given rownrs vector.
330 void doUpdate (Bool showTimings, const Table& origTable,
331 Table& updTable, const Vector<rownr_t>& rownrs,
332 const std::shared_ptr<TableExprGroupResult>& groups =
333 std::shared_ptr<TableExprGroupResult>());
334
335 // Do the insert step and return a selection containing the new rows.
336 Table doInsert (Bool showTimings, Table& table);
337
338 // Do the delete step.
339 void doDelete (Bool showTimings, Table& table);
340
341 // Do the count step returning a memory table containing the unique
342 // column values and the counts of the column values.
343 Table doCount (Bool showTimings, const Table&);
344
345 // Do the projection step returning a table containing the projection.
346 Table doProject (Bool showTimings, const Table&,
347 const std::shared_ptr<TableExprGroupResult>& groups =
348 std::shared_ptr<TableExprGroupResult>());
349
350 // Do the projection containing column expressions.
351 // Use the selected or unselected columns depending on <src>useSel</src>.
353 const std::shared_ptr<TableExprGroupResult>& groups);
354
355 // Create a subtable (used by createTable).
356 Table createSubTable (const String& subtableName,
357 const TableDesc& td, Int64 nrow,
358 const Record& dmInfo,
359 const std::vector<const Table*>& tempTables,
360 const std::vector<TableParseQuery*>& stack);
361
362 // Set the selected rows for the column objects in applySelNodes_p.
363 // These nodes refer the original table. They requires different row
364 // numbers than the selected groups and projected columns.
365 // rownrs_p is changed to use row 0..n.
366 // It returns the Table containing the subset of rows in the input Table.
368
369 // Do the groupby/aggregate step and return its result.
370 std::shared_ptr<TableExprGroupResult> doGroupby (bool showTimings);
371
372 // Do the HAVING step.
373 // It returns False if no HAVING step was given.
374 Bool doHaving (Bool showTimings,
375 const std::shared_ptr<TableExprGroupResult>& groups);
376
377 // Do the sort step.
378 void doSort (Bool showTimings);
379
380 // Do the limit/offset step.
381 void doLimOff (Bool showTimings);
382 Table doLimOff (Bool showTimings, const Table& table);
383
384 // Do the 'select distinct' step.
385 Table doDistinct (Bool showTimings, const Table& table);
386
387 // Finish the table (rename, copy, and/or flush).
388 Table doFinish (Bool showTimings, Table& table,
389 const std::vector<const Table*>& tempTables,
390 const std::vector<TableParseQuery*>& stack);
391
392 // Make an array from the contents of a column in a subquery.
394
395 // Make a set from the results of the subquery.
397
398 // Evaluate an int scalar expression.
399 Int64 evalIntScaExpr (const TableExprNode& expr) const;
400
401 //# Data mambers.
402 //# Command type.
404 //# List of TableParsePair objects (from WITH and FROM clause).
406 //# A join object per join clause.
407 std::vector<TableParseJoin> joins_p;
408 //# Object holding the info of table projection (i.e., column selection).
410 //# Name and type of the resulting table (from GIVING part).
412 uInt resultType_p; //# 0-unknown 1=memory 2=scratch 3=plain
413 Bool resultCreated_p; //# Has the result table been created?
417 //# Resulting set (from GIVING part).
419 //# The WHERE expression tree.
421 //# The GROUPBY, aggregate and HAVING info.
423 //# Distinct values in output?
425 //# The possible limit (= max nr of selected rows) (0 means no limit).
427 //# The possible last row (0 means no end; can be <0).
428 //# limit_p and endrow_p cannot be both !=0.
430 //# The possible offset (= nr of selected rows to skip).
432 //# The possible stride in offset:endrow:stride.
434 //# The update and insert list.
435 std::vector<std::shared_ptr<TableParseUpdate>> update_p;
436 //# The insert expressions (possibly for multiple rows).
437 std::vector<TableExprNode> insertExprs_p;
438 //# The table selection to be inserted.
440 //# The sort list.
441 std::vector<TableParseSortKey> sort_p;
442 //# The noDuplicates sort switch.
444 //# The default sort order.
446 //# All nodes that need to be adjusted for a selection of rownrs.
447 //# It can consist of column nodes and the rowid function node.
448 //# Some nodes (in aggregate functions) can later be disabled for adjustment.
449 std::vector<TableExprNode> applySelNodes_p;
450 //# The resulting table.
452 //# The table resulting from a projection with expressions.
454 //# The resulting row numbers.
456 };
457
458
459} //# NAMESPACE CASACORE - END
460
461#endif
simple 1-D array
Definition Block.h:198
Order
Enumerate the sort order:
Definition Sort.h:256
String: the storage and methods of handling collections of characters.
Definition String.h:223
Class to hold the table expression nodes for an element in a set.
void setDMInfo(const Record &dminfo)
Set the DataManager info for a new table.
const Block< String > & getColumnNames() const
Get the projected column names.
void handleOffset(const TableExprNode &expr)
Evaluate and keep the offset value.
void handleCount()
Make ready for a COUNT command.
const Table & getTable() const
Get the resulting table.
Table::EndianFormat endianFormat_p
const std::vector< TableParseJoin > & joins() const
Get the TableParseJoin objects.
void setInsertExprs(const std::vector< TableExprNode > exprs)
Set the insert expressions for all rows.
TableParseTableList & tableList()
Return the table name list.
TableExprNode getNode() const
Return the expression node.
std::vector< TableExprNode > applySelNodes_p
void handleLimit(const TableExprNode &expr)
Evaluate and keep the limit value.
Table doFinish(Bool showTimings, Table &table, const std::vector< const Table * > &tempTables, const std::vector< TableParseQuery * > &stack)
Finish the table (rename, copy, and/or flush).
std::vector< TableParseJoin > joins_p
void handleLimit(const TableExprNodeSetElem &expr)
Evaluate and keep limit/offset/stride given as start:end:incr.
TableExprNode doExists(Bool noexists, Bool showTimings)
Test if a subquery has sufficient elements.
Table doLimOff(Bool showTimings, const Table &table)
TableParseProject tableProject_p
Table doDistinct(Bool showTimings, const Table &table)
Do the 'select distinct' step.
void handleColSpec(const String &columnName, const String &likeColName, const String &dataType, const Record &spec, Bool isCOrder=False)
Keep the column specification in a create table command.
Table doCount(Bool showTimings, const Table &)
Do the count step returning a memory table containing the unique column values and the counts of the ...
void handleCalcComm(const TableExprNode &)
Keep the expression of a calculate command.
void doLimOff(Bool showTimings)
Do the limit/offset step.
TableExprNode handleKeyCol(const String &name, Bool tryProj)
Find the keyword or column name and create a TableExprNode from it.
void doSort(Bool showTimings)
Do the sort step.
TableExprNode makeSubSet() const
Make a set from the results of the subquery.
void handleInsert(TableParseQuery *sel)
TableRecord & findKeyword(const String &name, String &keyName, Bool update=True)
Split the given name into optional shorthand, column and fields.
TableParseJoin & addJoin()
Add a join object.
void handleGiving(const String &name, const Record &type)
Handle the name and type given in a GIVING clause.
Table createSubTable(const String &subtableName, const TableDesc &td, Int64 nrow, const Record &dmInfo, const std::vector< const Table * > &tempTables, const std::vector< TableParseQuery * > &stack)
Create a subtable (used by createTable).
void handleUpdate()
Keep the update expressions.
Int64 evalIntScaExpr(const TableExprNode &expr) const
Evaluate an int scalar expression.
std::vector< std::shared_ptr< TableParseUpdate > > update_p
void initDescriptions(const TableDesc &, const Record &dminfo)
Initialize the table and data manager descriptions.
void doDelete(Bool showTimings, Table &table)
Do the delete step.
void addUpdate(const std::shared_ptr< TableParseUpdate > &upd)
Add an update object.
std::vector< TableParseSortKey > sort_p
Bool doHaving(Bool showTimings, const std::shared_ptr< TableExprGroupResult > &groups)
Do the HAVING step.
TableParseGroupby groupby_p
std::shared_ptr< TableExprGroupResult > doGroupby(bool showTimings)
Do the groupby/aggregate step and return its result.
void handleSort(const std::vector< TableParseSortKey > &sortList, Bool noDuplicates, Sort::Order defaultSortOrder)
Keep the sort expressions.
TableExprNode doSubQuery(Bool showTimings)
Execute a subquery and create an appropriate node for the result.
void handleDropTab(const std::vector< const Table * > &tempTables, const std::vector< TableParseQuery * > &stack)
Handle the DROP TABLE command.
void handleGroupby(const std::vector< TableExprNode > &, Bool rollup)
Keep the groupby expressions.
std::vector< TableExprNode > insertExprs_p
Table doProject(Bool showTimings, const Table &, const std::shared_ptr< TableExprGroupResult > &groups=std::shared_ptr< TableExprGroupResult >())
Do the projection step returning a table containing the projection.
CommandType commandType() const
Return the command type.
void replaceTable(const Table &table)
Replace the first table (used by CALC command).
void setDMInfo(const Record &dminfo)
Set the DataManager info for a new table.
TableParseTableList tableList_p
TableExprNodeSet * resultSet_p
void handleColumn(Int type, const String &name, const TableExprNode &expr, const String &newName, const String &nameMask, const String &newDtype)
Add a column to the list of column names.
Table adjustApplySelNodes(const Table &)
Set the selected rows for the column objects in applySelNodes_p.
void handleRenameKey(const String &oldName, const String &newName)
Rename a table or column keyword.
void handleWhere(const TableExprNode &)
Keep the selection expression.
void addApplySelNode(const TableExprNode &node)
Add a column node to applySelNodes_p.
void handleGiving(const TableExprNodeSet &)
Handle the set given in a GIVING clause.
const Block< String > & getColumnNames() const
Get the projected column names.
static TableExprNode handleSlice(const TableExprNode &array, const TableExprNodeSet &indices, const TaQLStyle &)
Handle a slice operator.
void handleHaving(const TableExprNode &)
Keep the having expression.
TableExprNode handleFunc(const String &name, const TableExprNodeSet &arguments, const TaQLStyle &)
Handle a function.
Table createTable(const TableDesc &td, Int64 nrow, const Record &dmInfo, const std::vector< const Table * > &tempTables, const std::vector< TableParseQuery * > &stack)
Create a table using the given parameters.
void handleAltTab()
Reopen the table (for update) used in the ALTER TABLE command.
Table doInsert(Bool showTimings, Table &table)
Do the insert step and return a selection containing the new rows.
~TableParseQuery()
Destructor.
String getTableStructure(const Vector< String > &parts, const TaQLStyle &style)
Show the structure of fromTables_p[0] using the options given in parts[2:].
void handleTableNoFrom()
Create a temporary table if no tables are given in FROM.
void show(ostream &os) const
Show the expression tree.
void handleAddRow(const TableExprNode &expr)
Evaluate and add the rows.
TableExprNode getColSet()
Make an array from the contents of a column in a subquery.
ValueHolder getRecFld(const String &name)
Add a keyword or replace a keyword with the value of another keyword.
void handleInsert()
Make ready for the insert expression.
TableParseQuery(CommandType type)
Construct.
void handleSetKey(const String &name, const String &dtype, const ValueHolder &value)
Add a keyword or replace a keyword with a value.
void handleRemoveKey(const String &name)
Remove a table or column keyword.
void doUpdate(Bool showTimings, const Table &origTable, Table &updTable, const Vector< rownr_t > &rownrs, const std::shared_ptr< TableExprGroupResult > &groups=std::shared_ptr< TableExprGroupResult >())
Do the update step.
void handleAddCol(const Record &dmInfo)
Add columns to the table of ALTER TABLE.
Table doProjectExpr(Bool useSel, const std::shared_ptr< TableExprGroupResult > &groups)
Do the projection containing column expressions.
void handleCopyCol(Bool showTimings)
Handle copying of columns.
void execute(Bool showTimings, Bool setInGiving, Bool mustSelect, rownr_t maxRow, Bool doTracing=False, const std::vector< const Table * > &tempTables=std::vector< const Table * >(), const std::vector< TableParseQuery * > &stack=std::vector< TableParseQuery * >())
Execute the select command (select/sort/projection/groupby/having/giving).
void handleColumnFinish(Bool distinct)
Finish the addition of columns to the list of column names.
Table doFromQuery(Bool showTimings)
Execute a query in a FROM clause resulting in a Table.
void handleCreTab(const Record &dmInfo, const std::vector< const Table * > &tempTables, const std::vector< TableParseQuery * > &stack)
Keep the create table command.
Class containing two lists of TableParsePair objects.
EndianFormat
Define the possible endian formats in which table data can be stored.
Definition Table.h:195
this file contains all the compiler specific defines
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
unsigned int uInt
Definition aipstype.h:49
TableExprNode array(const TableExprNode &values, const TableExprNodeSet &shape)
Create an array of the given shape and fill it with the values.
Definition ExprNode.h:1933
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size)
Definition aipsxtype.h:36
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.
const Bool True
Definition aipstype.h:41
uInt64 rownr_t
Define the type of a row number in a table.
Definition aipsxtype.h:44