casacore
Loading...
Searching...
No Matches
ExprGroup.h
Go to the documentation of this file.
1// # ExprGroup.h: Classes handling TaQL's GROUPBY functionality
2// # Copyright (C) 2013
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_EXPRGROUP_H
27#define TABLES_EXPRGROUP_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/BasicSL/String.h>
32#include <casacore/tables/TaQL/ExprAggrNode.h>
33#include <vector>
34
35namespace casacore { // # NAMESPACE CASACORE - BEGIN
36
37// <summary>
38// Class representing a key in the groupby clause.
39// </summary>
40// <use visibility=local>
41// <reviewed reviewer="" date="" tests="tTableGram">
42// </reviewed>
43// <synopsis>
44// The GROUPBY clause consists of one or more keys, each being a scalar
45// TaQL expression with an arbitrary data type.
46// This class contains the value of a key for a particular table row.
47// It is part of a TableExprGroupKeySet object.
48// </synopsis>
50 public:
51 // Construct for a given data type.
53
54 // Get the data type.
56
57 // Set the key's value.
58 // <group>
59 void set(Bool v) { itsBool = v; }
60 void set(Int64 v) { itsInt64 = v; }
61 void set(Double v) { itsDouble = v; }
62 void set(const String& v) { itsString = v; }
63 // </group>
64
65 // Compare this and that key.
66 // <group>
67 bool operator==(const TableExprGroupKey&) const;
68 bool operator<(const TableExprGroupKey&) const;
69 // </group>
70
71 private:
73 Bool itsBool = false;
77};
78
79// <summary>
80// Class representing all keys in the groupby clause.
81// </summary>
82// <use visibility=local>
83// <reviewed reviewer="" date="" tests="tTableGram">
84// </reviewed>
85// <synopsis>
86// The GROUPBY clause consists of one or more keys, each being a scalar
87// TaQL expression with an arbitrary data type.
88// This class contains a set of TableExprGroupKey objects, each containing
89// the value of a key for a particular table row.
90// <br>It contains comparison functions to make it possible to use them
91// in a std::map object to map the groupby keyset to a group.
92// </synopsis>
94 public:
95 // Form the object from the given groupby nodes.
96 TableExprGroupKeySet(const vector<TableExprNode>& nodes);
97
98 // Add a key to end the set.
100
101 // Fill the keys with the values from the nodes for this rowid.
102 void fill(const vector<TableExprNode>& nodes, const TableExprId& id);
103
104 // Compare all keys in the set.
105 // The keyset is compared in order of key, thus the first key defines
106 // the major ordering.
108 bool operator<(const TableExprGroupKeySet&) const;
109
110 private:
111 vector<TableExprGroupKey> itsKeys;
112};
113
114// <summary>
115// Class holding the results of groupby and aggregation
116// </summary>
117// <use visibility=local>
118// <reviewed reviewer="" date="" tests="tTableGram">
119// </reviewed>
120// <synopsis>
121// The SELECT (and HAVING) clause can contain aggregate functions
122// of which the results can be grouped using the GROUPBY clause.
123// This class holds the results of the (immediate) aggregate functions
124// and, if needed, the TableExprId ids of all rows belonging to each group.
125// These ids are used to evaluate the lazy aggregate functions.
126// <br>An object of this class is part of the TableExprIdAggr object
127// used to get the aggregated values of each group.
128// </synopsis>
130 public:
131 // Create from the possible set of immediate aggregate functions.
132 // No immediate functions were used, thus no TableExprIds needed.
133 explicit TableExprGroupResult(const vector<std::shared_ptr<TableExprGroupFuncSet>>& funcSets);
134 // Create from the possible set of immediate aggregate functions
135 // and the set of TableExprIds per group for lazy aggregate functions.
136 TableExprGroupResult(const vector<std::shared_ptr<TableExprGroupFuncSet>>& funcSets,
137 const vector<std::shared_ptr<vector<TableExprId>>>& ids);
138 // Get the nr of groups.
139 uInt ngroup() const { return itsFuncSets.size(); }
140 // Get the set of functions (and their results) for the given group.
141 TableExprGroupFuncSet& funcSet(uInt group) const { return *itsFuncSets[group]; }
142 // Get the set of TableExprIds for the given group.
143 const vector<TableExprId>& ids(uInt group) const { return *itsIds[group]; }
144
145 private:
146 vector<std::shared_ptr<TableExprGroupFuncSet>> itsFuncSets;
147 vector<std::shared_ptr<vector<TableExprId>>> itsIds;
148};
149
150// <summary>
151// Abstract base class for classes calculating an aggregated group result.
152// </summary>
153// <use visibility=local>
154// <reviewed reviewer="" date="" tests="tExprGroup">
155// </reviewed>
156// <synopsis>
157// The GROUPBY clause divides a table into groups for which aggregated
158// results can be calculated like the mean or minimum. These results are
159// calculated in classes derived from this abstract base class.
160// <br>There is one such function object per aggregation per group. All
161// aggregation objects of a group are combined in a std::vector.
162// This vector is mapped to a TableExprGroupKeySet object to keep track
163// of all groups and aggregations.
164// <br> There are two types of aggregation function classes.
165// <ul>
166// <li> Immediate classes implement the 'apply' function to immediately
167// apply the operand's value in the aggregation.
168// Such classes do not keep the operand's values.
169// <li> Lazy classes do not need the 'apply' function. Instead they
170// read all values of the group in the 'getXXX' function and do the
171// aggregation. Such classes are meant for aggregation functions
172// like 'median' that need to keep all values. When applying it
173// immediately, all groups need to keep their values which might need
174// too much memory. Lazy classes need the values of only one group
175// at a time, but have the disadvantage that reading the values from
176// the table might be done in a non-sequential order.
177// </ul>
178// Most derived classes are immediate classes.
179// </synopsis>
181 public:
182 // Construct from the TaQL aggregation node. It keeps the operand
183 // of the aggregation node.
186 // Copying is not needed, thus not allowed.
189 // Does the aggregate function use lazy semantics?
190 // The default implementation returns False.
191 virtual Bool isLazy() const;
192 // Get the function's sequence nr.
193 uInt seqnr() const { return itsSeqnr; }
194 // Set the function's sequence nr.
196 // Get the operand's value for the given row and apply it to the aggregation.
197 // This function should not be called for lazy classes.
198 virtual void apply(const TableExprId& id) = 0;
199 // If needed, finish the aggregation.
200 // By default nothing is done.
201 virtual void finish();
202 // Get the assembled TableExprIds of a group. It is specifically meant
203 // for TableExprGroupExprId used for lazy aggregation.
204 virtual std::shared_ptr<vector<TableExprId>> getIds() const;
205 // Get the aggregated value.
206 // Immediate classes can return the already calculated value, while
207 // lazy classes will get the values of all rows given by the TableExprIds
208 // and do the aggregation.
209 // <group>
210 virtual Bool getBool(const vector<TableExprId>& = vector<TableExprId>());
211 virtual Int64 getInt(const vector<TableExprId>& = vector<TableExprId>());
212 virtual Double getDouble(const vector<TableExprId>& = vector<TableExprId>());
213 virtual DComplex getDComplex(const vector<TableExprId>& = vector<TableExprId>());
214 virtual MVTime getDate(const vector<TableExprId>& = vector<TableExprId>());
215 virtual String getString(const vector<TableExprId>& = vector<TableExprId>());
216 virtual MArray<Bool> getArrayBool(const vector<TableExprId>& = vector<TableExprId>());
217 virtual MArray<Int64> getArrayInt(const vector<TableExprId>& = vector<TableExprId>());
218 virtual MArray<Double> getArrayDouble(const vector<TableExprId>& = vector<TableExprId>());
219 virtual MArray<DComplex> getArrayDComplex(const vector<TableExprId>& = vector<TableExprId>());
220 virtual MArray<MVTime> getArrayDate(const vector<TableExprId>& = vector<TableExprId>());
221 virtual MArray<String> getArrayString(const vector<TableExprId>& = vector<TableExprId>());
222 // <group>
223 protected:
224 // # Data member
225 TableExprNodeRep* itsNode; // refers the node (not owned)
226 TableExprNodeRep* itsOperand; // refers the operand (not owned)
228};
229
230// <summary>
231// Class derived from TableExprGroupFuncBase representing a no function
232// </summary>
233// <use visibility=local>
234// <reviewed reviewer="" date="" tests="tExprGroup">
235// </reviewed>
236// <synopsis>
237// This class represents a null aggregate function which is meant for
238// possible aggregate functionality in UDFs.
239// </synopsis>
241 public:
244 virtual Bool isLazy() const;
245 virtual void apply(const TableExprId& id);
246};
247
248// <summary>
249// Class derived from TableExprGroupFuncBase for the first value in a group
250// </summary>
251// <use visibility=local>
252// <reviewed reviewer="" date="" tests="tExprGroup">
253// </reviewed>
254// <synopsis>
255// This class keeps the TableExprId of the first value in a group.
256// The 'getXXX' functions get the value for that TableExprId.
257// </synopsis>
259 public:
262 virtual void apply(const TableExprId& id);
263 virtual Bool getBool(const vector<TableExprId>&);
264 virtual Int64 getInt(const vector<TableExprId>&);
265 virtual Double getDouble(const vector<TableExprId>&);
266 virtual DComplex getDComplex(const vector<TableExprId>&);
267 virtual MVTime getDate(const vector<TableExprId>&);
268 virtual String getString(const vector<TableExprId>&);
269 virtual MArray<Bool> getArrayBool(const vector<TableExprId>&);
270 virtual MArray<Int64> getArrayInt(const vector<TableExprId>&);
271 virtual MArray<Double> getArrayDouble(const vector<TableExprId>&);
272 virtual MArray<DComplex> getArrayDComplex(const vector<TableExprId>&);
273 virtual MArray<MVTime> getArrayDate(const vector<TableExprId>&);
274 virtual MArray<String> getArrayString(const vector<TableExprId>&);
275
276 protected:
278};
279
280// <summary>
281// Class derived from TableExprGroupFuncBase for the first value in a group
282// </summary>
283// <use visibility=local>
284// <reviewed reviewer="" date="" tests="tExprGroup">
285// </reviewed>
286// <synopsis>
287// This class keeps the TableExprId of the last value in a group.
288// The 'getXXX' functions get the value for that TableExprId.
289// <br>For ease of use this class is derived from TableExprGroupFirst.
290// </synopsis>
292 public:
295 virtual void apply(const TableExprId& id);
296};
297
298// <summary>
299// Class derived from TableExprGroupFuncBase collecting the ids in a group
300// </summary>
301// <use visibility=local>
302// <reviewed reviewer="" date="" tests="tExprGroup">
303// </reviewed>
304// <synopsis>
305// This class keeps all TableExprIds in a group.
306// It is meant for lazy aggregation classes which use the collected
307// TableExprIds in their 'getXXX' functions.
308// </synopsis>
310 public:
313 virtual Bool isLazy() const;
314 virtual void apply(const TableExprId& id);
315 virtual std::shared_ptr<vector<TableExprId>> getIds() const;
316
317 private:
318 std::shared_ptr<vector<TableExprId>> itsIds;
319};
320
321// <summary>
322// Class collecting the rowids of entries in a group.
323// </summary>
324// <use visibility=local>
325// <reviewed reviewer="" date="" tests="tExprGroup">
326// </reviewed>
327// <synopsis>
328// This class collects the row numbers of the rows in a group.
329// </synopsis>
331 public:
334 virtual Bool isLazy() const;
335 virtual void apply(const TableExprId& id);
336 virtual MArray<Int64> getArrayInt(const vector<TableExprId>&);
337};
338
339// <summary>
340// Class collecting the arrays in a group.
341// </summary>
342// <use visibility=local>
343// <reviewed reviewer="" date="" tests="tExprGroup">
344// </reviewed>
345// <synopsis>
346// This class collects the non-empty arrays in a group into an array with
347// one more axis. All arrays (if not empty) must have the same shape.
348// </synopsis>
350 public:
353 virtual Bool isLazy() const;
354 virtual void apply(const TableExprId& id);
355 virtual MArray<Bool> getArrayBool(const vector<TableExprId>&);
356 virtual MArray<Int64> getArrayInt(const vector<TableExprId>&);
357 virtual MArray<Double> getArrayDouble(const vector<TableExprId>&);
358 virtual MArray<DComplex> getArrayDComplex(const vector<TableExprId>&);
359 virtual MArray<MVTime> getArrayDate(const vector<TableExprId>&);
360 virtual MArray<String> getArrayString(const vector<TableExprId>&);
361
362 protected:
363 template <typename T>
364 MArray<T> getArray(const vector<TableExprId>& ids) {
365 // Return scalar values as a Vector.
366 if (itsOperand->valueType() == TableExprNodeRep::VTScalar) {
367 Vector<T> result(ids.size());
368 for (size_t i = 0; i < ids.size(); ++i) {
369 itsOperand->get(ids[i], result[i]);
370 }
371 return MArray<T>(result);
372 }
373 // Array values are returned as an array with one more axis.
374 // Use the first non-null value to determine the shape and if masked.
375 MArray<T> arr;
376 size_t id;
377 Bool hasMask = False;
378 IPosition shp;
379 for (id = 0; id < ids.size(); ++id) {
380 itsOperand->get(ids[id], arr);
381 if (!arr.isNull()) {
382 hasMask = arr.hasMask();
383 shp = arr.shape();
384 shp.append(IPosition(1, ids.size()));
385 break;
386 }
387 }
388 size_t ndef = 0;
389 if (id == ids.size()) {
390 // All arrays are null.
391 return MArray<T>();
392 }
393 Array<T> result(shp);
394 ArrayIterator<T> iter(result, arr.ndim());
396 std::shared_ptr<ArrayIterator<Bool>> miter;
397 if (hasMask) {
398 mask.resize(shp);
399 miter.reset(new ArrayIterator<Bool>(mask, arr.ndim()));
400 }
401 for (; id < ids.size(); ++id) {
402 MArray<T> values;
403 itsOperand->get(ids[id], values);
404 if (!values.isNull()) {
405 ndef++;
406 iter.array() = values.array();
407 iter.next();
408 if (hasMask) {
409 miter->array() = values.mask();
410 miter->next();
411 }
412 }
413 }
414 if (ndef < ids.size()) {
415 shp[shp.size() - 1] = ndef;
416 result.resize(shp, True);
417 if (hasMask) {
418 mask.resize(shp, True);
419 }
420 }
421 return MArray<T>(result, mask);
422 }
423};
424
425// <summary>
426// Abstract base class for aggregate functions giving a bool scalar.
427// </summary>
428// <use visibility=local>
429// <reviewed reviewer="" date="" tests="tExprGroup">
430// </reviewed>
431// <synopsis>
432// This class is derived from TableExprGroupFuncBase and acts as the
433// abstract base class for aggregate functions resulting in a bool scalar.
434// <br>Derived classes can use <src>itsValue</src> to contain the
435// aggregated value. It that case they do not need to implement the
436// <src>get</src> function.
437// </synopsis>
439 public:
442 : TableExprGroupFuncBase(node), itsValue(initValue) {}
444 virtual Bool getBool(const vector<TableExprId>&);
445
446 protected:
448};
449
450// <summary>
451// Abstract base class for aggregate functions giving an integer scalar.
452// </summary>
453// <use visibility=local>
454// <reviewed reviewer="" date="" tests="tExprGroup">
455// </reviewed>
456// <synopsis>
457// This class is derived from TableExprGroupFuncBase and acts as the
458// abstract base class for aggregate functions resulting in an integer scalar.
459// <br>Derived classes can use <src>itsValue</src> to contain the
460// aggregated value. It that case they do not need to implement the
461// <src>get</src> function.
462// </synopsis>
464 public:
465 explicit TableExprGroupFuncInt(TableExprNodeRep* node, Int64 initValue = 0)
466 : TableExprGroupFuncBase(node), itsValue(initValue) {}
468 virtual Int64 getInt(const vector<TableExprId>&);
469 virtual Double getDouble(const vector<TableExprId>&);
470
471 protected:
473};
474
475// <summary>
476// Abstract base class for aggregate functions giving a double scalar.
477// </summary>
478// <use visibility=local>
479// <reviewed reviewer="" date="" tests="tExprGroup">
480// </reviewed>
481// <synopsis>
482// This class is derived from TableExprGroupFuncBase and acts as the
483// abstract base class for aggregate functions resulting in a double scalar.
484// <br>Derived classes can use <src>itsValue</src> to contain the
485// aggregated value. It that case they do not need to implement the
486// <src>get</src> function.
487// </synopsis>
489 public:
490 explicit TableExprGroupFuncDouble(TableExprNodeRep* node, Double initValue = 0)
491 : TableExprGroupFuncBase(node), itsValue(initValue) {}
493 virtual Double getDouble(const vector<TableExprId>&);
494
495 protected:
497};
498
499// <summary>
500// Abstract base class for aggregate functions giving a dcomplex scalar.
501// </summary>
502// <use visibility=local>
503// <reviewed reviewer="" date="" tests="tExprGroup">
504// </reviewed>
505// <synopsis>
506// This class is derived from TableExprGroupFuncBase and acts as the
507// abstract base class for aggregate functions resulting in a dcomplex scalar.
508// <br>Derived classes can use <src>itsValue</src> to contain the
509// aggregated value. It that case they do not need to implement the
510// <src>get</src> function.
511// </synopsis>
513 public:
515 const DComplex& initValue = DComplex())
516 : TableExprGroupFuncBase(node), itsValue(initValue) {}
518 virtual DComplex getDComplex(const vector<TableExprId>&);
519
520 protected:
521 DComplex itsValue;
522};
523
524// <summary>
525// Abstract base class for aggregate functions giving a date/time scalar.
526// </summary>
527// <use visibility=local>
528// <reviewed reviewer="" date="" tests="tExprGroup">
529// </reviewed>
530// <synopsis>
531// This class is derived from TableExprGroupFuncBase and acts as the
532// abstract base class for aggregate functions resulting in a date/time scalar.
533// <br>Derived classes can use <src>itsValue</src> to contain the
534// aggregated value. It that case they do not need to implement the
535// <src>get</src> function.
536// </synopsis>
538 public:
539 explicit TableExprGroupFuncDate(TableExprNodeRep* node, const MVTime& initValue = MVTime())
540 : TableExprGroupFuncBase(node), itsValue(initValue) {}
542 virtual MVTime getDate(const vector<TableExprId>&);
543
544 protected:
546};
547
548// <summary>
549// Abstract base class for aggregate functions giving a string scalar.
550// </summary>
551// <use visibility=local>
552// <reviewed reviewer="" date="" tests="tExprGroup">
553// </reviewed>
554// <synopsis>
555// This class is derived from TableExprGroupFuncBase and acts as the
556// abstract base class for aggregate functions resulting in a string scalar.
557// <br>Derived classes can use <src>itsValue</src> to contain the
558// aggregated value. It that case they do not need to implement the
559// <src>get</src> function.
560// </synopsis>
562 public:
563 explicit TableExprGroupFuncString(TableExprNodeRep* node, const String& initValue = String())
564 : TableExprGroupFuncBase(node), itsValue(initValue) {}
566 virtual String getString(const vector<TableExprId>&);
567
568 protected:
570};
571
572// <summary>
573// Abstract base class for aggregate functions giving a bool array.
574// </summary>
575// <use visibility=local>
576// <reviewed reviewer="" date="" tests="tExprGroup">
577// </reviewed>
578// <synopsis>
579// This class is derived from TableExprGroupFuncBase and acts as the
580// abstract base class for aggregate functions resulting in a bool array.
581// <br>Derived classes can use <src>itsValue</src> to contain the
582// aggregated value. It that case they do not need to implement the
583// <src>get</src> function.
584// </synopsis>
586 public:
589 virtual MArray<Bool> getArrayBool(const vector<TableExprId>&);
590
591 protected:
592 // If not empty, check if the shape matches that of <src>itsValue</src>.
593 // If <src>itsValue</src> is still empty, it is sized.
594 Bool checkShape(const MArrayBase& arr, const String& func);
596};
597
598// <summary>
599// Abstract base class for aggregate functions giving an integer array.
600// </summary>
601// <use visibility=local>
602// <reviewed reviewer="" date="" tests="tExprGroup">
603// </reviewed>
604// <synopsis>
605// This class is derived from TableExprGroupFuncBase and acts as the
606// abstract base class for aggregate functions resulting in an integer array.
607// <br>Derived classes can use <src>itsValue</src> to contain the
608// aggregated value. It that case they do not need to implement the
609// <src>get</src> function.
610// </synopsis>
612 public:
615 virtual MArray<Int64> getArrayInt(const vector<TableExprId>&);
616
617 protected:
618 // If not empty, check if the shape matches that of <src>itsValue</src>.
619 // If <src>itsValue</src> is still empty, it is sized.
620 Bool checkShape(const MArrayBase& arr, const String& func);
622};
623
624// <summary>
625// Abstract base class for aggregate functions giving a double array.
626// </summary>
627// <use visibility=local>
628// <reviewed reviewer="" date="" tests="tExprGroup">
629// </reviewed>
630// <synopsis>
631// This class is derived from TableExprGroupFuncBase and acts as the
632// abstract base class for aggregate functions resulting in a double array.
633// <br>Derived classes can use <src>itsValue</src> to contain the
634// aggregated value. It that case they do not need to implement the
635// <src>get</src> function.
636// </synopsis>
638 public:
641 virtual MArray<Double> getArrayDouble(const vector<TableExprId>&);
642
643 protected:
644 // If not empty, check if the shape matches that of <src>itsValue</src>.
645 // If <src>itsValue</src> is still empty, it is sized.
646 Bool checkShape(const MArrayBase& arr, const String& func);
648};
649
650// <summary>
651// Abstract base class for aggregate functions giving a dcomplex array.
652// </summary>
653// <use visibility=local>
654// <reviewed reviewer="" date="" tests="tExprGroup">
655// </reviewed>
656// <synopsis>
657// This class is derived from TableExprGroupFuncBase and acts as the
658// abstract base class for aggregate functions resulting in a dcomplex array.
659// <br>Derived classes can use <src>itsValue</src> to contain the
660// aggregated value. It that case they do not need to implement the
661// <src>get</src> function.
662// </synopsis>
664 public:
667 virtual MArray<DComplex> getArrayDComplex(const vector<TableExprId>&);
668
669 protected:
670 // If not empty, check if the shape matches that of <src>itsValue</src>.
671 // If <src>itsValue</src> is still empty, it is sized.
672 Bool checkShape(const MArrayBase& arr, const String& func);
674};
675
676// <summary>
677// Abstract base class for aggregate functions giving a date/time array.
678// </summary>
679// <use visibility=local>
680// <reviewed reviewer="" date="" tests="tExprGroup">
681// </reviewed>
682// <synopsis>
683// This class is derived from TableExprGroupFuncBase and acts as the
684// abstract base class for aggregate functions resulting in a date/time array.
685// <br>Derived classes can use <src>itsValue</src> to contain the
686// aggregated value. It that case they do not need to implement the
687// <src>get</src> function.
688// </synopsis>
690 public:
693 virtual MArray<MVTime> getArrayDate(const vector<TableExprId>&);
694
695 protected:
696 // If not empty, check if the shape matches that of <src>itsValue</src>.
697 // If <src>itsValue</src> is still empty, it is sized.
698 Bool checkShape(const MArrayBase& arr, const String& func);
700};
701
702// <summary>
703// Abstract base class for aggregate functions giving a string array.
704// </summary>
705// <use visibility=local>
706// <reviewed reviewer="" date="" tests="tExprGroup">
707// </reviewed>
708// <synopsis>
709// This class is derived from TableExprGroupFuncBase and acts as the
710// abstract base class for aggregate functions resulting in a string array.
711// <br>Derived classes can use <src>itsValue</src> to contain the
712// aggregated value. It that case they do not need to implement the
713// <src>get</src> function.
714// </synopsis>
716 public:
719 virtual MArray<String> getArrayString(const vector<TableExprId>&);
720
721 protected:
722 // If not empty, check if the shape matches that of <src>itsValue</src>.
723 // If <src>itsValue</src> is still empty, it is sized.
724 Bool checkShape(const MArrayBase& arr, const String& func);
726};
727
728// <summary>
729// Class containing the results of aggregated values in a group.
730// </summary>
731// <use visibility=local>
732// <reviewed reviewer="" date="" tests="tExprGroup">
733// </reviewed>
734// <synopsis>
735// This class contains the set of aggregate function objects containing
736// all aggregate results of a particular GROUPBY group.
737// It also contains the TableExprId of the last row in the group.
738// It is used for possible non-aggregate expressions.
739// </synopsis>
741 public:
743
744 // Let the aggregate node objects construct the function set.
745 TableExprGroupFuncSet(const vector<TableExprNodeRep*>& aggrNodes);
746
747 // Copying is not needed, thus not allowed.
750
751 // Add a function object.
752 void add(const std::shared_ptr<TableExprGroupFuncBase>& func);
753
754 // Apply the functions to the given row.
755 void apply(const TableExprId& id);
756
757 // Get the vector of functions.
758 const vector<std::shared_ptr<TableExprGroupFuncBase>>& getFuncs() const { return itsFuncs; }
759
760 // Get the TableExprId.
761 const TableExprId& getId() const { return itsId; }
762
763 private:
764 // # Data members.
765 vector<std::shared_ptr<TableExprGroupFuncBase>> itsFuncs;
766 TableExprId itsId; // # row containing the non-aggregate variables
767};
768
769} // namespace casacore
770
771#endif
Array< T > & array()
Return the cursor.
Definition ArrayIter.h:111
virtual void next() override
Move the cursor to the next position.
void resize()
Make this array a different shape.
size_t size() const
Definition IPosition.h:552
void append(const IPosition &other)
Append this IPosition with another one (causing a resize).
Bool isNull() const
Is the array null?
Definition MArrayBase.h:103
uInt ndim() const
Get the dimensionality.
Definition MArrayBase.h:131
const Array< Bool > & mask() const
Get the mask.
Definition MArrayBase.h:118
Bool hasMask() const
Is there a mask?
Definition MArrayBase.h:112
const IPosition & shape() const
Get the shape.
Definition MArrayBase.h:134
const Array< T > & array() const
Get access to the array.
Definition MArray.h:136
String: the storage and methods of handling collections of characters.
Definition String.h:355
virtual MArray< String > getArrayString(const vector< TableExprId > &)
virtual Bool isLazy() const
Does the aggregate function use lazy semantics?
virtual MArray< DComplex > getArrayDComplex(const vector< TableExprId > &)
virtual MArray< MVTime > getArrayDate(const vector< TableExprId > &)
virtual MArray< Double > getArrayDouble(const vector< TableExprId > &)
virtual MArray< Int64 > getArrayInt(const vector< TableExprId > &)
virtual MArray< Bool > getArrayBool(const vector< TableExprId > &)
MArray< T > getArray(const vector< TableExprId > &ids)
Definition ExprGroup.h:364
TableExprGroupAggr(TableExprNodeRep *node)
virtual void apply(const TableExprId &id)
Get the operand's value for the given row and apply it to the aggregation.
virtual void apply(const TableExprId &id)
Get the operand's value for the given row and apply it to the aggregation.
virtual Bool isLazy() const
Does the aggregate function use lazy semantics?
std::shared_ptr< vector< TableExprId > > itsIds
Definition ExprGroup.h:318
virtual std::shared_ptr< vector< TableExprId > > getIds() const
Get the assembled TableExprIds of a group.
TableExprGroupExprId(TableExprNodeRep *node)
virtual Bool getBool(const vector< TableExprId > &)
Get the aggregated value.
virtual MArray< String > getArrayString(const vector< TableExprId > &)
virtual MVTime getDate(const vector< TableExprId > &)
virtual void apply(const TableExprId &id)
Get the operand's value for the given row and apply it to the aggregation.
virtual MArray< MVTime > getArrayDate(const vector< TableExprId > &)
virtual DComplex getDComplex(const vector< TableExprId > &)
virtual Int64 getInt(const vector< TableExprId > &)
virtual String getString(const vector< TableExprId > &)
virtual MArray< Double > getArrayDouble(const vector< TableExprId > &)
TableExprGroupFirst(TableExprNodeRep *node)
virtual MArray< DComplex > getArrayDComplex(const vector< TableExprId > &)
virtual Double getDouble(const vector< TableExprId > &)
virtual MArray< Int64 > getArrayInt(const vector< TableExprId > &)
virtual MArray< Bool > getArrayBool(const vector< TableExprId > &)
virtual MArray< Bool > getArrayBool(const vector< TableExprId > &)
Bool checkShape(const MArrayBase &arr, const String &func)
If not empty, check if the shape matches that of itsValue.
TableExprGroupFuncArrayBool(TableExprNodeRep *node)
Definition ExprGroup.h:587
TableExprGroupFuncArrayDComplex(TableExprNodeRep *node)
Definition ExprGroup.h:665
virtual MArray< DComplex > getArrayDComplex(const vector< TableExprId > &)
Bool checkShape(const MArrayBase &arr, const String &func)
If not empty, check if the shape matches that of itsValue.
virtual MArray< MVTime > getArrayDate(const vector< TableExprId > &)
Bool checkShape(const MArrayBase &arr, const String &func)
If not empty, check if the shape matches that of itsValue.
TableExprGroupFuncArrayDate(TableExprNodeRep *node)
Definition ExprGroup.h:691
virtual MArray< Double > getArrayDouble(const vector< TableExprId > &)
TableExprGroupFuncArrayDouble(TableExprNodeRep *node)
Definition ExprGroup.h:639
Bool checkShape(const MArrayBase &arr, const String &func)
If not empty, check if the shape matches that of itsValue.
virtual MArray< Int64 > getArrayInt(const vector< TableExprId > &)
Bool checkShape(const MArrayBase &arr, const String &func)
If not empty, check if the shape matches that of itsValue.
TableExprGroupFuncArrayInt(TableExprNodeRep *node)
Definition ExprGroup.h:613
virtual MArray< String > getArrayString(const vector< TableExprId > &)
Bool checkShape(const MArrayBase &arr, const String &func)
If not empty, check if the shape matches that of itsValue.
TableExprGroupFuncArrayString(TableExprNodeRep *node)
Definition ExprGroup.h:717
virtual void finish()
If needed, finish the aggregation.
TableExprGroupFuncBase(TableExprNodeRep *node)
Construct from the TaQL aggregation node.
virtual void apply(const TableExprId &id)=0
Get the operand's value for the given row and apply it to the aggregation.
virtual std::shared_ptr< vector< TableExprId > > getIds() const
Get the assembled TableExprIds of a group.
virtual MArray< Bool > getArrayBool(const vector< TableExprId > &=vector< TableExprId >())
TableExprGroupFuncBase & operator=(const TableExprGroupFuncBase &)=delete
virtual Int64 getInt(const vector< TableExprId > &=vector< TableExprId >())
virtual MVTime getDate(const vector< TableExprId > &=vector< TableExprId >())
virtual MArray< Int64 > getArrayInt(const vector< TableExprId > &=vector< TableExprId >())
virtual MArray< MVTime > getArrayDate(const vector< TableExprId > &=vector< TableExprId >())
uInt seqnr() const
Get the function's sequence nr.
Definition ExprGroup.h:193
virtual MArray< String > getArrayString(const vector< TableExprId > &=vector< TableExprId >())
virtual Double getDouble(const vector< TableExprId > &=vector< TableExprId >())
virtual Bool isLazy() const
Does the aggregate function use lazy semantics?
TableExprGroupFuncBase(const TableExprGroupFuncBase &)=delete
Copying is not needed, thus not allowed.
virtual MArray< Double > getArrayDouble(const vector< TableExprId > &=vector< TableExprId >())
virtual DComplex getDComplex(const vector< TableExprId > &=vector< TableExprId >())
void setSeqnr(uInt seqnr)
Set the function's sequence nr.
Definition ExprGroup.h:195
TableExprNodeRep * itsOperand
Definition ExprGroup.h:226
virtual Bool getBool(const vector< TableExprId > &=vector< TableExprId >())
Get the aggregated value.
virtual MArray< DComplex > getArrayDComplex(const vector< TableExprId > &=vector< TableExprId >())
virtual String getString(const vector< TableExprId > &=vector< TableExprId >())
virtual Bool getBool(const vector< TableExprId > &)
Get the aggregated value.
TableExprGroupFuncBool(TableExprNodeRep *node, Bool initValue)
Definition ExprGroup.h:441
TableExprGroupFuncBool(TableExprNodeRep *node)
Definition ExprGroup.h:440
TableExprGroupFuncDComplex(TableExprNodeRep *node, const DComplex &initValue=DComplex())
Definition ExprGroup.h:514
virtual DComplex getDComplex(const vector< TableExprId > &)
virtual MVTime getDate(const vector< TableExprId > &)
TableExprGroupFuncDate(TableExprNodeRep *node, const MVTime &initValue=MVTime())
Definition ExprGroup.h:539
virtual Double getDouble(const vector< TableExprId > &)
TableExprGroupFuncDouble(TableExprNodeRep *node, Double initValue=0)
Definition ExprGroup.h:490
virtual Double getDouble(const vector< TableExprId > &)
virtual Int64 getInt(const vector< TableExprId > &)
TableExprGroupFuncInt(TableExprNodeRep *node, Int64 initValue=0)
Definition ExprGroup.h:465
Class containing the results of aggregated values in a group.
Definition ExprGroup.h:740
const vector< std::shared_ptr< TableExprGroupFuncBase > > & getFuncs() const
Get the vector of functions.
Definition ExprGroup.h:758
const TableExprId & getId() const
Get the TableExprId.
Definition ExprGroup.h:761
TableExprGroupFuncSet(const vector< TableExprNodeRep * > &aggrNodes)
Let the aggregate node objects construct the function set.
void add(const std::shared_ptr< TableExprGroupFuncBase > &func)
Add a function object.
void apply(const TableExprId &id)
Apply the functions to the given row.
TableExprGroupFuncSet & operator=(const TableExprGroupFuncSet &)=delete
TableExprGroupFuncSet(const TableExprGroupFuncSet &)=delete
Copying is not needed, thus not allowed.
vector< std::shared_ptr< TableExprGroupFuncBase > > itsFuncs
Definition ExprGroup.h:765
TableExprGroupFuncString(TableExprNodeRep *node, const String &initValue=String())
Definition ExprGroup.h:563
virtual String getString(const vector< TableExprId > &)
TableExprGroupKeySet(const vector< TableExprNode > &nodes)
Form the object from the given groupby nodes.
bool operator<(const TableExprGroupKeySet &) const
void fill(const vector< TableExprNode > &nodes, const TableExprId &id)
Fill the keys with the values from the nodes for this rowid.
void addKey(TableExprNodeRep::NodeDataType dtype)
Add a key to end the set.
Definition ExprGroup.h:99
bool operator==(const TableExprGroupKeySet &) const
Compare all keys in the set.
vector< TableExprGroupKey > itsKeys
Definition ExprGroup.h:111
bool operator<(const TableExprGroupKey &) const
void set(const String &v)
Definition ExprGroup.h:62
TableExprGroupKey(TableExprNodeRep::NodeDataType dtype)
Construct for a given data type.
Definition ExprGroup.h:52
bool operator==(const TableExprGroupKey &) const
Compare this and that key.
TableExprNodeRep::NodeDataType itsDT
Definition ExprGroup.h:72
TableExprNodeRep::NodeDataType dataType() const
Get the data type.
Definition ExprGroup.h:55
void set(Bool v)
Set the key's value.
Definition ExprGroup.h:59
virtual void apply(const TableExprId &id)
Get the operand's value for the given row and apply it to the aggregation.
TableExprGroupLast(TableExprNodeRep *node)
TableExprGroupNull(TableExprNodeRep *node)
virtual void apply(const TableExprId &id)
Get the operand's value for the given row and apply it to the aggregation.
virtual Bool isLazy() const
Does the aggregate function use lazy semantics?
TableExprGroupResult(const vector< std::shared_ptr< TableExprGroupFuncSet > > &funcSets, const vector< std::shared_ptr< vector< TableExprId > > > &ids)
Create from the possible set of immediate aggregate functions and the set of TableExprIds per group f...
vector< std::shared_ptr< TableExprGroupFuncSet > > itsFuncSets
Definition ExprGroup.h:146
uInt ngroup() const
Get the nr of groups.
Definition ExprGroup.h:139
vector< std::shared_ptr< vector< TableExprId > > > itsIds
Definition ExprGroup.h:147
TableExprGroupFuncSet & funcSet(uInt group) const
Get the set of functions (and their results) for the given group.
Definition ExprGroup.h:141
TableExprGroupResult(const vector< std::shared_ptr< TableExprGroupFuncSet > > &funcSets)
Create from the possible set of immediate aggregate functions.
const vector< TableExprId > & ids(uInt group) const
Get the set of TableExprIds for the given group.
Definition ExprGroup.h:143
virtual Bool isLazy() const
Does the aggregate function use lazy semantics?
virtual MArray< Int64 > getArrayInt(const vector< TableExprId > &)
TableExprGroupRowid(TableExprNodeRep *node)
virtual void apply(const TableExprId &id)
Get the operand's value for the given row and apply it to the aggregation.
Abstract base class for a node in a table column expression tree.
NodeDataType
Define the data types of a node.
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
unsigned int uInt
Definition aipstype.h:49
LatticeExprNode mask(const LatticeExprNode &expr)
This function returns the mask of the given expression.
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size).
Definition aipsxtype.h:36
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
const Bool True
Definition aipstype.h:41
double Double
Definition aipstype.h:53