casacore
Loading...
Searching...
No Matches
TaQLJoin.h
Go to the documentation of this file.
1// # TaQLJoin.h: Class handling the condition of the join clause
2// # Copyright (C) 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_TAQLJOIN_H
27#define TABLES_TAQLJOIN_H
28
29#include <casacore/casa/aips.h>
30#include <casacore/tables/TaQL/ExprDerNode.h>
31#include <casacore/tables/TaQL/ExprNodeSetOpt.h>
32#include <vector>
33
34namespace casacore { // # NAMESPACE CASACORE - BEGIN
35
36// # Forward declarations.
37class TableExprNode;
39class TableParseJoin;
40
41// <summary>
42// Base class for handling a comparison in the join clause in a TaQL command
43// </summary>
44// <use visibility=local>
45// <reviewed reviewer="Tammo Jan Dijkema" date="2022/12/15" tests="tTableGramJoin">
46// </reviewed>
47// <synopsis>
48// </synopsis>
49
51 public:
52 virtual ~TaQLJoinBase() = default;
53
54 // Find the row number. <0 means not found.
55 virtual Int64 findRow(const TableExprId&) = 0;
56};
57
58// <summary>
59// Class holding the row number as the final level in the comparison tree
60// </summary>
61// <use visibility=local>
62// <reviewed reviewer="Tammo Jan Dijkema" date="2022/12/15" tests="tTableGramJoin">
63// </reviewed>
64// <synopsis>
65// Objects of this class form the lowest level in a TaQLJoin tree. It only
66// contains a row number telling which row in the join table contains the
67// values at the higher levels in the tree.
68// </synopsis>
69
70class TaQLJoinRow : public TaQLJoinBase {
71 public:
72 TaQLJoinRow(Int64 row) : itsRow(row) {}
73 ~TaQLJoinRow() override = default;
74
75 // Return the row number.
76 Int64 findRow(const TableExprId&) override;
77
78 private:
80};
81
82// <summary>
83// Class holding a comparison part of a join condition
84// </summary>
85// <use visibility=local>
86// <reviewed reviewer="Tammo Jan Dijkema" date="2022/12/15" tests="tTableGramJoin">
87// </reviewed>
88// <synopsis>
89// TaQLJoin holds a vector of nested TaQLJoinBase objects, one for each
90// unique value or interval in the join table. It uses TableExprNodeSetOptBase
91// to hold the values and the index in the vector of TaQLJoinBase objects.
92// The data types that can be used in a join condition are the same as those
93// supported by TableExprNodeSetOptBase; that is: integer and string for a
94// discrete value and int, double, datetime and string for an interval.
95// Note that in the interval case int and datetime are handled as double.
96//
97// Note that at the lowest level the vector of TaQLJoinBase objects contains
98// TaQLJoinRow objects holding the join table row number. The TaQLJoin objects
99// at the higher levels contain the index in the vector at the next level.
100//
101// The class contains static functions to build the tree given the left and
102// right part of a comparison, where left is an expression using the main table
103// and right using the join table. The right expression gives the values to
104// build the tree as sketched above.
105//
106// <example>
107// <srcblock>
108// SELECT t1.COL1, t2.NAME FROM maintab t1 JOIN jointab t2 ON
109// t1.COL1 = t2.IDCOL
110// AND t1.TIME AROUND t2.TIME IN t2.INTERVAL
111// </srcblock>
112// In this example the main and join table are joined on 2 condition parts.
113// First the equality match of COL1 and IDCOL, second the interval match on TIME.
114// Note that the NAME column from the join table are selected, thus
115// the join is used to find the name for each row in the main table.
116// <br>The join table columns may contain something like below.
117// Thus 4 unique IDCOL values, each having 2 time intervals.
118// <srcblock>
119// IDCOL TIME INTERVAL NAME
120// 0 tm1 d1 name01
121// 1 tm1 d1 name11
122// 2 tm1 d1 name21
123// 3 tm1 d1 name31
124// 0 tm2 d2 name02
125// 1 tm2 d2 name12
126// 2 tm2 d2 name22
127// 3 tm2 d2 name32
128// </srcblock>
129// The TaQLJoin tree will consist of 3 levels. The first level handles the
130// equality match. It consists of a single TaQLJoin object containing a vector
131// of second level TaQLJoin objects, one for each unique IDCOL value.
132// The second level handles the TIME match. The third level contains the row
133// number for each IDCOL/TIME pair. Schematically:
134// <srcblock>
135// level 1 TaQLJoin, IDCOL: 0 1 2 3
136// level 2 TaQLJoin, TIME: tm1 tm2 tm1 tm2 tm1 tm2 tm1 tm2
137// level 3 TaQLJoinRow: 0 1 2 3 4 5 6 7
138// </srcblock>
139// To find the matching join table row means that first the matching
140// IDCOL is found at level 1, thereafter at level 2 the matching interval
141// for that IDCOL.Level 3 gives the correct row number in the join table.
142// </example>
143// </synopsis>
144
145class TaQLJoin : public TaQLJoinBase {
146 public:
147 TaQLJoin(const TENShPtr& mainNode, const TENShPtr& joinNode,
148 const std::vector<std::shared_ptr<TaQLJoinBase>>& children);
149
150 ~TaQLJoin() override = default;
151
152 // Find the row number in the join table for the given row in the main table.
153 Int64 findRow(const TableExprId&) override;
154
155 // From the given level on create nested TaQLJoin nodes.
156 // It use makeOptDiscrete or makeOptInterval to create the appropriate
157 // TableExprNodeSetOptBase object.
158 static std::shared_ptr<TaQLJoinBase> createRecursive(const std::vector<TableExprNode>& mainNodes,
159 const std::vector<TableExprNode>& joinNodes,
160 const std::vector<rownr_t>& rows,
161 size_t level);
162
163 // Create nested TaQLJoin nodes for a join expression part at the given level
164 // using discrete values.
165 // Thereafter createRecursive is called for the next level.
166 template <typename T>
167 static std::shared_ptr<TaQLJoinBase> makeOptDiscrete(TableExprNodeRep& node,
168 const std::vector<TableExprNode>& mainNodes,
169 const std::vector<TableExprNode>& joinNodes,
170 const std::vector<rownr_t>& rows,
171 size_t level);
172
173 // Create nested TaQLJoin nodes for a join expression part at the given level
174 // using intervals.
175 // Thereafter createRecursive is called for the next level.
176 template <typename T>
177 static std::shared_ptr<TaQLJoinBase> makeOptInterval(const TableExprNodeSet& set,
178 const std::vector<TableExprNode>& mainNodes,
179 const std::vector<TableExprNode>& joinNodes,
180 const std::vector<rownr_t>& rows,
181 size_t level);
182
183 private:
185 TENShPtr itsJoinNode; // only used for automatic deletion
186 TableExprNodeSetOptBase* itsOptSet; // same ptr as itsJoinNode
187 std::vector<std::shared_ptr<TaQLJoinBase>> itsChildren;
188};
189
190// <summary>
191// A column in a join table
192// </summary>
193// <use visibility=local>
194// <reviewed reviewer="Tammo Jan Dijkema" date="2022/12/15" tests="tTableGramJoin">
195// </reviewed>
196// <synopsis>
197// TaQLJoinColumn contains the TableExprNodeColumn object for a
198// column in a join table. It is used to find a value in the join column
199// for a row in the main table. It uses the TableParseJoin object to find
200// the row in the join table given the row in the main table.
201// </synopsis>
202
204 public:
205 TaQLJoinColumn(const TENShPtr& columnNode, const TableParseJoin&);
206
207 ~TaQLJoinColumn() override = default;
208
209 // Get the table info for this column.
210 TableExprInfo getTableInfo() const override;
211
212 // Get the data for the given id.
213 // Using the Join object it maps the row number in the main table
214 // to the row number in the join table.
215 // <group>
222 // </group>
223
224 // Clear the internal data vector (in derived classes).
225 virtual void clear();
226
227 // Make the appropriate TaQLJoinColumn object.
228 static TableExprNode makeColumnNode(const TENShPtr& columnNode, const TableParseJoin&);
229
230 protected:
233};
234
235// <summary>
236// A scalar column of the given type in a join table
237// </summary>
238// <use visibility=local>
239// <reviewed reviewer="Tammo Jan Dijkema" date="2022/12/15" tests="tTableGramJoin">
240// </reviewed>
241// <synopsis>
242// TaQLJoinArrayColumn contains the TableExprNodeColumn object for an array
243// column in a join table. It is used to find a value in the join column
244// for a row in the main table. It uses the TableParseJoin object to find
245// the row in the join table given the row in the main table.
246// </synopsis>
247
249 public:
250 TaQLJoinColumnBool(const TENShPtr& columnNode, const TableParseJoin&);
251 ~TaQLJoinColumnBool() override = default;
252 Bool getBool(const TableExprId& id) override;
253 void clear() override;
254
255 private:
257};
258
260 public:
261 TaQLJoinColumnInt(const TENShPtr& columnNode, const TableParseJoin&);
262 ~TaQLJoinColumnInt() override = default;
263 Int64 getInt(const TableExprId& id) override;
264 void clear() override;
265
266 private:
268};
269
271 public:
272 TaQLJoinColumnDouble(const TENShPtr& columnNode, const TableParseJoin&);
273 ~TaQLJoinColumnDouble() override = default;
274 Double getDouble(const TableExprId& id) override;
275 void clear() override;
276
277 private:
279};
280
282 public:
284 ~TaQLJoinColumnDComplex() override = default;
285 DComplex getDComplex(const TableExprId& id) override;
286 void clear() override;
287
288 private:
290};
291
293 public:
294 TaQLJoinColumnString(const TENShPtr& columnNode, const TableParseJoin&);
295 ~TaQLJoinColumnString() override = default;
296 String getString(const TableExprId& id) override;
297 void clear() override;
298
299 private:
301};
302
304 public:
305 TaQLJoinColumnDate(const TENShPtr& columnNode, const TableParseJoin&);
306 ~TaQLJoinColumnDate() override = default;
307 MVTime getDate(const TableExprId& id) override;
308 void clear() override;
309
310 private:
312};
313
314// <summary>
315// The rowid in a join table
316// </summary>
317// <use visibility=local>
318// <reviewed reviewer="Tammo Jan Dijkema" date="2022/12/15" tests="tTableGramJoin">
319// </reviewed>
320// <synopsis>
321// TaQLJoinRowid contains row sequence numbers of the matching rows
322// in the join table.
323// </synopsis>
324
326 public:
328 ~TaQLJoinRowid() override = default;
329 TableExprInfo getTableInfo() const override;
330 // Get the data (rowid in join table) for the given id.
331 // Using the Join object it maps the row number in the main table
332 // to the row number in the join table.
333 Int64 getInt(const TableExprId& id) override;
334
335 private:
338};
339
340} // namespace casacore
341
342#endif
String: the storage and methods of handling collections of characters.
Definition String.h:355
virtual Int64 findRow(const TableExprId &)=0
Find the row number.
virtual ~TaQLJoinBase()=default
Bool getBool(const TableExprId &id) override
Get a scalar value for this node in the given row.
TaQLJoinColumnBool(const TENShPtr &columnNode, const TableParseJoin &)
~TaQLJoinColumnBool() override=default
void clear() override
Clear the internal data vector (in derived classes).
Vector< DComplex > itsData
Definition TaQLJoin.h:289
DComplex getDComplex(const TableExprId &id) override
TaQLJoinColumnDComplex(const TENShPtr &columnNode, const TableParseJoin &)
void clear() override
Clear the internal data vector (in derived classes).
~TaQLJoinColumnDComplex() override=default
Vector< MVTime > itsData
Definition TaQLJoin.h:311
void clear() override
Clear the internal data vector (in derived classes).
TaQLJoinColumnDate(const TENShPtr &columnNode, const TableParseJoin &)
~TaQLJoinColumnDate() override=default
MVTime getDate(const TableExprId &id) override
void clear() override
Clear the internal data vector (in derived classes).
~TaQLJoinColumnDouble() override=default
Double getDouble(const TableExprId &id) override
TaQLJoinColumnDouble(const TENShPtr &columnNode, const TableParseJoin &)
~TaQLJoinColumnInt() override=default
Vector< Int64 > itsData
Definition TaQLJoin.h:267
Int64 getInt(const TableExprId &id) override
TaQLJoinColumnInt(const TENShPtr &columnNode, const TableParseJoin &)
void clear() override
Clear the internal data vector (in derived classes).
TaQLJoinColumnString(const TENShPtr &columnNode, const TableParseJoin &)
String getString(const TableExprId &id) override
~TaQLJoinColumnString() override=default
void clear() override
Clear the internal data vector (in derived classes).
MArray< Double > getArrayDouble(const TableExprId &id) override
TableExprInfo getTableInfo() const override
Get the table info for this column.
MArray< MVTime > getArrayDate(const TableExprId &id) override
static TableExprNode makeColumnNode(const TENShPtr &columnNode, const TableParseJoin &)
Make the appropriate TaQLJoinColumn object.
MArray< DComplex > getArrayDComplex(const TableExprId &id) override
~TaQLJoinColumn() override=default
MArray< Bool > getArrayBool(const TableExprId &id) override
Get the data for the given id.
MArray< String > getArrayString(const TableExprId &id) override
TaQLJoinColumn(const TENShPtr &columnNode, const TableParseJoin &)
virtual void clear()
Clear the internal data vector (in derived classes).
MArray< Int64 > getArrayInt(const TableExprId &id) override
const TableParseJoin & itsJoin
Definition TaQLJoin.h:232
TaQLJoinRow(Int64 row)
Definition TaQLJoin.h:72
~TaQLJoinRow() override=default
Int64 findRow(const TableExprId &) override
Return the row number.
const TableParseJoin & itsJoin
Definition TaQLJoin.h:337
TableExprInfo itsTabInfo
Definition TaQLJoin.h:336
~TaQLJoinRowid() override=default
Int64 getInt(const TableExprId &id) override
Get the data (rowid in join table) for the given id.
TaQLJoinRowid(const TableExprInfo &, const TableParseJoin &)
TableExprInfo getTableInfo() const override
Get the table info.
static std::shared_ptr< TaQLJoinBase > makeOptDiscrete(TableExprNodeRep &node, const std::vector< TableExprNode > &mainNodes, const std::vector< TableExprNode > &joinNodes, const std::vector< rownr_t > &rows, size_t level)
Create nested TaQLJoin nodes for a join expression part at the given level using discrete values.
TENShPtr itsMainNode
Definition TaQLJoin.h:184
static std::shared_ptr< TaQLJoinBase > createRecursive(const std::vector< TableExprNode > &mainNodes, const std::vector< TableExprNode > &joinNodes, const std::vector< rownr_t > &rows, size_t level)
From the given level on create nested TaQLJoin nodes.
Int64 findRow(const TableExprId &) override
Find the row number in the join table for the given row in the main table.
TENShPtr itsJoinNode
Definition TaQLJoin.h:185
std::vector< std::shared_ptr< TaQLJoinBase > > itsChildren
Definition TaQLJoin.h:187
static std::shared_ptr< TaQLJoinBase > makeOptInterval(const TableExprNodeSet &set, const std::vector< TableExprNode > &mainNodes, const std::vector< TableExprNode > &joinNodes, const std::vector< rownr_t > &rows, size_t level)
Create nested TaQLJoin nodes for a join expression part at the given level using intervals.
~TaQLJoin() override=default
TaQLJoin(const TENShPtr &mainNode, const TENShPtr &joinNode, const std::vector< std::shared_ptr< TaQLJoinBase > > &children)
TableExprNodeSetOptBase * itsOptSet
Definition TaQLJoin.h:186
Class to connect a Table and its alias name.
Abstract base class for a node in a table column expression tree.
TableExprNodeRep(NodeDataType, ValueType, OperType, ArgType, ExprType, Int ndim, const IPosition &shape)
Construct a node.
Class defining a set element containing a continuous interval.
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
void set(const T &val)
Definition Block.h:541
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size).
Definition aipsxtype.h:36
std::shared_ptr< TableExprNodeRep > TENShPtr
Definition ExprNodeRep.h:56
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
double Double
Definition aipstype.h:53