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