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
35
36namespace casacore { //# NAMESPACE CASACORE - BEGIN
37
38 // <summary>
39 // Class representing a key in the groupby clause.
40 // </summary>
41 // <use visibility=local>
42 // <reviewed reviewer="" date="" tests="tTableGram">
43 // </reviewed>
44 // <synopsis>
45 // The GROUPBY clause consists of one or more keys, each being a scalar
46 // TaQL expression with an arbitrary data type.
47 // This class contains the value of a key for a particular table row.
48 // It is part of a TableExprGroupKeySet object.
49 // </synopsis>
51 {
52 public:
53 // Construct for a given data type.
55 : itsDT (dtype)
56 {}
57
58 // Get the data type.
61
62 // Set the key's value.
63 // <group>
64 void set (Bool v)
65 { itsBool = v; }
66 void set (Int64 v)
67 { itsInt64 = v; }
68 void set (Double v)
69 { itsDouble = v; }
70 void set (const String& v)
71 { itsString = v; }
72 // </group>
73
74 // Compare this and that key.
75 // <group>
76 bool operator== (const TableExprGroupKey&) const;
77 bool operator< (const TableExprGroupKey&) const;
78 // </group>
79
80 private:
82 Bool itsBool = false;
86 };
87
88
89 // <summary>
90 // Class representing all keys in the groupby clause.
91 // </summary>
92 // <use visibility=local>
93 // <reviewed reviewer="" date="" tests="tTableGram">
94 // </reviewed>
95 // <synopsis>
96 // The GROUPBY clause consists of one or more keys, each being a scalar
97 // TaQL expression with an arbitrary data type.
98 // This class contains a set of TableExprGroupKey objects, each containing
99 // the value of a key for a particular table row.
100 // <br>It contains comparison functions to make it possible to use them
101 // in a std::map object to map the groupby keyset to a group.
102 // </synopsis>
104 {
105 public:
106 // Form the object from the given groupby nodes.
107 TableExprGroupKeySet (const vector<TableExprNode>& nodes);
108
109 // Add a key to end the set.
111 { itsKeys.push_back (TableExprGroupKey(dtype)); }
112
113 // Fill the keys with the values from the nodes for this rowid.
114 void fill (const vector<TableExprNode>& nodes, const TableExprId& id);
115
116 // Compare all keys in the set.
117 // The keyset is compared in order of key, thus the first key defines
118 // the major ordering.
121
122 private:
123 vector<TableExprGroupKey> itsKeys;
124 };
125
126
127 // <summary>
128 // Class holding the results of groupby and aggregation
129 // </summary>
130 // <use visibility=local>
131 // <reviewed reviewer="" date="" tests="tTableGram">
132 // </reviewed>
133 // <synopsis>
134 // The SELECT (and HAVING) clause can contain aggregate functions
135 // of which the results can be grouped using the GROUPBY clause.
136 // This class holds the results of the (immediate) aggregate functions
137 // and, if needed, the TableExprId ids of all rows belonging to each group.
138 // These ids are used to evaluate the lazy aggregate functions.
139 // <br>An object of this class is part of the TableExprIdAggr object
140 // used to get the aggregated values of each group.
141 // </synopsis>
143 {
144 public:
145 // Create from the possible set of immediate aggregate functions.
146 // No immediate functions were used, thus no TableExprIds needed.
148 (const vector<std::shared_ptr<TableExprGroupFuncSet>>& funcSets);
149 // Create from the possible set of immediate aggregate functions
150 // and the set of TableExprIds per group for lazy aggregate functions.
152 (const vector<std::shared_ptr<TableExprGroupFuncSet>>& funcSets,
153 const vector<std::shared_ptr<vector<TableExprId>>>& ids);
154 // Get the nr of groups.
155 uInt ngroup() const
156 { return itsFuncSets.size(); }
157 // Get the set of functions (and their results) for the given group.
159 { return *itsFuncSets[group]; }
160 // Get the set of TableExprIds for the given group.
161 const vector<TableExprId>& ids (uInt group) const
162 { return *itsIds[group]; }
163 private:
164 vector<std::shared_ptr<TableExprGroupFuncSet>> itsFuncSets;
165 vector<std::shared_ptr<vector<TableExprId>>> itsIds;
166 };
167
168
169 // <summary>
170 // Abstract base class for classes calculating an aggregated group result.
171 // </summary>
172 // <use visibility=local>
173 // <reviewed reviewer="" date="" tests="tExprGroup">
174 // </reviewed>
175 // <synopsis>
176 // The GROUPBY clause divides a table into groups for which aggregated
177 // results can be calculated like the mean or minimum. These results are
178 // calculated in classes derived from this abstract base class.
179 // <br>There is one such function object per aggregation per group. All
180 // aggregation objects of a group are combined in a std::vector.
181 // This vector is mapped to a TableExprGroupKeySet object to keep track
182 // of all groups and aggregations.
183 // <br> There are two types of aggregation function classes.
184 // <ul>
185 // <li> Immediate classes implement the 'apply' function to immediately
186 // apply the operand's value in the aggregation.
187 // Such classes do not keep the operand's values.
188 // <li> Lazy classes do not need the 'apply' function. Instead they
189 // read all values of the group in the 'getXXX' function and do the
190 // aggregation. Such classes are meant for aggregation functions
191 // like 'median' that need to keep all values. When applying it
192 // immediately, all groups need to keep their values which might need
193 // too much memory. Lazy classes need the values of only one group
194 // at a time, but have the disadvantage that reading the values from
195 // the table might be done in a non-sequential order.
196 // </ul>
197 // Most derived classes are immediate classes.
198 // </synopsis>
200 {
201 public:
202 // Construct from the TaQL aggregation node. It keeps the operand
203 // of the aggregation node.
206 // Copying is not needed, thus not allowed.
209 // Does the aggregate function use lazy semantics?
210 // The default implementation returns False.
211 virtual Bool isLazy() const;
212 // Get the function's sequence nr.
213 uInt seqnr() const
214 { return itsSeqnr; }
215 // Set the function's sequence nr.
217 { itsSeqnr = seqnr; }
218 // Get the operand's value for the given row and apply it to the aggregation.
219 // This function should not be called for lazy classes.
220 virtual void apply (const TableExprId& id) = 0;
221 // If needed, finish the aggregation.
222 // By default nothing is done.
223 virtual void finish();
224 // Get the assembled TableExprIds of a group. It is specifically meant
225 // for TableExprGroupExprId used for lazy aggregation.
226 virtual std::shared_ptr<vector<TableExprId>> getIds() const;
227 // Get the aggregated value.
228 // Immediate classes can return the already calculated value, while
229 // lazy classes will get the values of all rows given by the TableExprIds
230 // and do the aggregation.
231 // <group>
232 virtual Bool getBool (const vector<TableExprId>& = vector<TableExprId>());
233 virtual Int64 getInt (const vector<TableExprId>& = vector<TableExprId>());
234 virtual Double getDouble (const vector<TableExprId>& = vector<TableExprId>());
235 virtual DComplex getDComplex (const vector<TableExprId>& = vector<TableExprId>());
236 virtual MVTime getDate (const vector<TableExprId>& = vector<TableExprId>());
237 virtual String getString (const vector<TableExprId>& = vector<TableExprId>());
238 virtual MArray<Bool> getArrayBool (const vector<TableExprId>& = vector<TableExprId>());
239 virtual MArray<Int64> getArrayInt (const vector<TableExprId>& = vector<TableExprId>());
240 virtual MArray<Double> getArrayDouble (const vector<TableExprId>& = vector<TableExprId>());
241 virtual MArray<DComplex> getArrayDComplex (const vector<TableExprId>& = vector<TableExprId>());
242 virtual MArray<MVTime> getArrayDate (const vector<TableExprId>& = vector<TableExprId>());
243 virtual MArray<String> getArrayString (const vector<TableExprId>& = vector<TableExprId>());
244 // <group>
245 protected:
246 //# Data member
247 TableExprNodeRep* itsNode; // refers the node (not owned)
248 TableExprNodeRep* itsOperand; // refers the operand (not owned)
250 };
251
252
253 // <summary>
254 // Class derived from TableExprGroupFuncBase representing a no function
255 // </summary>
256 // <use visibility=local>
257 // <reviewed reviewer="" date="" tests="tExprGroup">
258 // </reviewed>
259 // <synopsis>
260 // This class represents a null aggregate function which is meant for
261 // possible aggregate functionality in UDFs.
262 // </synopsis>
264 {
265 public:
268 virtual Bool isLazy() const;
269 virtual void apply (const TableExprId& id);
270 };
271
272 // <summary>
273 // Class derived from TableExprGroupFuncBase for the first value in a group
274 // </summary>
275 // <use visibility=local>
276 // <reviewed reviewer="" date="" tests="tExprGroup">
277 // </reviewed>
278 // <synopsis>
279 // This class keeps the TableExprId of the first value in a group.
280 // The 'getXXX' functions get the value for that TableExprId.
281 // </synopsis>
283 {
284 public:
287 virtual void apply (const TableExprId& id);
288 virtual Bool getBool (const vector<TableExprId>&);
289 virtual Int64 getInt (const vector<TableExprId>&);
290 virtual Double getDouble (const vector<TableExprId>&);
291 virtual DComplex getDComplex (const vector<TableExprId>&);
292 virtual MVTime getDate (const vector<TableExprId>&);
293 virtual String getString (const vector<TableExprId>&);
294 virtual MArray<Bool> getArrayBool (const vector<TableExprId>&);
295 virtual MArray<Int64> getArrayInt (const vector<TableExprId>&);
296 virtual MArray<Double> getArrayDouble (const vector<TableExprId>&);
297 virtual MArray<DComplex> getArrayDComplex (const vector<TableExprId>&);
298 virtual MArray<MVTime> getArrayDate (const vector<TableExprId>&);
299 virtual MArray<String> getArrayString (const vector<TableExprId>&);
300 protected:
302 };
303
304 // <summary>
305 // Class derived from TableExprGroupFuncBase for the first value in a group
306 // </summary>
307 // <use visibility=local>
308 // <reviewed reviewer="" date="" tests="tExprGroup">
309 // </reviewed>
310 // <synopsis>
311 // This class keeps the TableExprId of the last value in a group.
312 // The 'getXXX' functions get the value for that TableExprId.
313 // <br>For ease of use this class is derived from TableExprGroupFirst.
314 // </synopsis>
316 {
317 public:
320 virtual void apply (const TableExprId& id);
321 };
322
323 // <summary>
324 // Class derived from TableExprGroupFuncBase collecting the ids in a group
325 // </summary>
326 // <use visibility=local>
327 // <reviewed reviewer="" date="" tests="tExprGroup">
328 // </reviewed>
329 // <synopsis>
330 // This class keeps all TableExprIds in a group.
331 // It is meant for lazy aggregation classes which use the collected
332 // TableExprIds in their 'getXXX' functions.
333 // </synopsis>
335 {
336 public:
339 virtual Bool isLazy() const;
340 virtual void apply (const TableExprId& id);
341 virtual std::shared_ptr<vector<TableExprId>> getIds() const;
342 private:
343 std::shared_ptr<vector<TableExprId>> itsIds;
344 };
345
346 // <summary>
347 // Class collecting the rowids of entries in a group.
348 // </summary>
349 // <use visibility=local>
350 // <reviewed reviewer="" date="" tests="tExprGroup">
351 // </reviewed>
352 // <synopsis>
353 // This class collects the row numbers of the rows in a group.
354 // </synopsis>
356 {
357 public:
360 virtual Bool isLazy() const;
361 virtual void apply (const TableExprId& id);
362 virtual MArray<Int64> getArrayInt (const vector<TableExprId>&);
363 };
364
365 // <summary>
366 // Class collecting the arrays in a group.
367 // </summary>
368 // <use visibility=local>
369 // <reviewed reviewer="" date="" tests="tExprGroup">
370 // </reviewed>
371 // <synopsis>
372 // This class collects the non-empty arrays in a group into an array with
373 // one more axis. All arrays (if not empty) must have the same shape.
374 // </synopsis>
376 {
377 public:
380 virtual Bool isLazy() const;
381 virtual void apply (const TableExprId& id);
382 virtual MArray<Bool> getArrayBool (const vector<TableExprId>&);
383 virtual MArray<Int64> getArrayInt (const vector<TableExprId>&);
384 virtual MArray<Double> getArrayDouble (const vector<TableExprId>&);
385 virtual MArray<DComplex> getArrayDComplex (const vector<TableExprId>&);
386 virtual MArray<MVTime> getArrayDate (const vector<TableExprId>&);
387 virtual MArray<String> getArrayString (const vector<TableExprId>&);
388 protected:
389 template<typename T>
390 MArray<T> getArray (const vector<TableExprId>& ids)
391 {
392 // Return scalar values as a Vector.
393 if (itsOperand->valueType() == TableExprNodeRep::VTScalar) {
394 Vector<T> result(ids.size());
395 for (size_t i=0; i<ids.size(); ++i) {
396 itsOperand->get (ids[i], result[i]);
397 }
398 return MArray<T>(result);
399 }
400 // Array values are returned as an array with one more axis.
401 // Use the first non-null value to determine the shape and if masked.
402 MArray<T> arr;
403 size_t id;
404 Bool hasMask = False;
405 IPosition shp;
406 for (id=0; id<ids.size(); ++id) {
407 itsOperand->get (ids[id], arr);
408 if (! arr.isNull()) {
409 hasMask = arr.hasMask();
410 shp = arr.shape();
411 shp.append (IPosition (1, ids.size()));
412 break;
413 }
414 }
415 size_t ndef = 0;
416 if (id == ids.size()) {
417 // All arrays are null.
418 return MArray<T>();
419 }
420 Array<T> result(shp);
421 ArrayIterator<T> iter (result, arr.ndim());
423 std::shared_ptr<ArrayIterator<Bool>> miter;
424 if (hasMask) {
425 mask.resize (shp);
426 miter.reset (new ArrayIterator<Bool> (mask, arr.ndim()));
427 }
428 for (; id<ids.size(); ++id) {
429 MArray<T> values;
430 itsOperand->get (ids[id], values);
431 if (! values.isNull()) {
432 ndef++;
433 iter.array() = values.array();
434 iter.next();
435 if (hasMask) {
436 miter->array() = values.mask();
437 miter->next();
438 }
439 }
440 }
441 if (ndef < ids.size()) {
442 shp[shp.size() - 1] = ndef;
443 result.resize (shp, True);
444 if (hasMask) {
445 mask.resize (shp, True);
446 }
447 }
448 return MArray<T>(result, mask);
449 }
450 };
451
452
453 // <summary>
454 // Abstract base class for aggregate functions giving a bool scalar.
455 // </summary>
456 // <use visibility=local>
457 // <reviewed reviewer="" date="" tests="tExprGroup">
458 // </reviewed>
459 // <synopsis>
460 // This class is derived from TableExprGroupFuncBase and acts as the
461 // abstract base class for aggregate functions resulting in a bool scalar.
462 // <br>Derived classes can use <src>itsValue</src> to contain the
463 // aggregated value. It that case they do not need to implement the
464 // <src>get</src> function.
465 // </synopsis>
467 {
468 public:
473 : TableExprGroupFuncBase (node),
474 itsValue (initValue)
475 {}
477 virtual Bool getBool (const vector<TableExprId>&);
478 protected:
480 };
481
482 // <summary>
483 // Abstract base class for aggregate functions giving an integer scalar.
484 // </summary>
485 // <use visibility=local>
486 // <reviewed reviewer="" date="" tests="tExprGroup">
487 // </reviewed>
488 // <synopsis>
489 // This class is derived from TableExprGroupFuncBase and acts as the
490 // abstract base class for aggregate functions resulting in an integer scalar.
491 // <br>Derived classes can use <src>itsValue</src> to contain the
492 // aggregated value. It that case they do not need to implement the
493 // <src>get</src> function.
494 // </synopsis>
496 {
497 public:
498 explicit TableExprGroupFuncInt (TableExprNodeRep* node, Int64 initValue=0)
499 : TableExprGroupFuncBase (node),
500 itsValue (initValue)
501 {}
503 virtual Int64 getInt (const vector<TableExprId>&);
504 virtual Double getDouble (const vector<TableExprId>&);
505 protected:
507 };
508
509 // <summary>
510 // Abstract base class for aggregate functions giving a double scalar.
511 // </summary>
512 // <use visibility=local>
513 // <reviewed reviewer="" date="" tests="tExprGroup">
514 // </reviewed>
515 // <synopsis>
516 // This class is derived from TableExprGroupFuncBase and acts as the
517 // abstract base class for aggregate functions resulting in a double scalar.
518 // <br>Derived classes can use <src>itsValue</src> to contain the
519 // aggregated value. It that case they do not need to implement the
520 // <src>get</src> function.
521 // </synopsis>
523 {
524 public:
526 Double initValue = 0)
527 : TableExprGroupFuncBase (node),
528 itsValue (initValue)
529 {}
531 virtual Double getDouble (const vector<TableExprId>&);
532 protected:
534 };
535
536 // <summary>
537 // Abstract base class for aggregate functions giving a dcomplex scalar.
538 // </summary>
539 // <use visibility=local>
540 // <reviewed reviewer="" date="" tests="tExprGroup">
541 // </reviewed>
542 // <synopsis>
543 // This class is derived from TableExprGroupFuncBase and acts as the
544 // abstract base class for aggregate functions resulting in a dcomplex scalar.
545 // <br>Derived classes can use <src>itsValue</src> to contain the
546 // aggregated value. It that case they do not need to implement the
547 // <src>get</src> function.
548 // </synopsis>
550 {
551 public:
553 const DComplex& initValue = DComplex())
554 : TableExprGroupFuncBase (node),
555 itsValue (initValue)
556 {}
558 virtual DComplex getDComplex (const vector<TableExprId>&);
559 protected:
560 DComplex itsValue;
561 };
562
563 // <summary>
564 // Abstract base class for aggregate functions giving a date/time scalar.
565 // </summary>
566 // <use visibility=local>
567 // <reviewed reviewer="" date="" tests="tExprGroup">
568 // </reviewed>
569 // <synopsis>
570 // This class is derived from TableExprGroupFuncBase and acts as the
571 // abstract base class for aggregate functions resulting in a date/time scalar.
572 // <br>Derived classes can use <src>itsValue</src> to contain the
573 // aggregated value. It that case they do not need to implement the
574 // <src>get</src> function.
575 // </synopsis>
577 {
578 public:
580 const MVTime& initValue = MVTime())
581 : TableExprGroupFuncBase (node),
582 itsValue (initValue)
583 {}
585 virtual MVTime getDate (const vector<TableExprId>&);
586 protected:
588 };
589
590 // <summary>
591 // Abstract base class for aggregate functions giving a string scalar.
592 // </summary>
593 // <use visibility=local>
594 // <reviewed reviewer="" date="" tests="tExprGroup">
595 // </reviewed>
596 // <synopsis>
597 // This class is derived from TableExprGroupFuncBase and acts as the
598 // abstract base class for aggregate functions resulting in a string scalar.
599 // <br>Derived classes can use <src>itsValue</src> to contain the
600 // aggregated value. It that case they do not need to implement the
601 // <src>get</src> function.
602 // </synopsis>
604 {
605 public:
607 const String& initValue = String())
608 : TableExprGroupFuncBase (node),
609 itsValue (initValue)
610 {}
612 virtual String getString (const vector<TableExprId>&);
613 protected:
615 };
616
617 // <summary>
618 // Abstract base class for aggregate functions giving a bool array.
619 // </summary>
620 // <use visibility=local>
621 // <reviewed reviewer="" date="" tests="tExprGroup">
622 // </reviewed>
623 // <synopsis>
624 // This class is derived from TableExprGroupFuncBase and acts as the
625 // abstract base class for aggregate functions resulting in a bool array.
626 // <br>Derived classes can use <src>itsValue</src> to contain the
627 // aggregated value. It that case they do not need to implement the
628 // <src>get</src> function.
629 // </synopsis>
631 {
632 public:
637 virtual MArray<Bool> getArrayBool (const vector<TableExprId>&);
638 protected:
639 // If not empty, check if the shape matches that of <src>itsValue</src>.
640 // If <src>itsValue</src> is still empty, it is sized.
641 Bool checkShape (const MArrayBase& arr, const String& func);
643 };
644
645 // <summary>
646 // Abstract base class for aggregate functions giving an integer array.
647 // </summary>
648 // <use visibility=local>
649 // <reviewed reviewer="" date="" tests="tExprGroup">
650 // </reviewed>
651 // <synopsis>
652 // This class is derived from TableExprGroupFuncBase and acts as the
653 // abstract base class for aggregate functions resulting in an integer array.
654 // <br>Derived classes can use <src>itsValue</src> to contain the
655 // aggregated value. It that case they do not need to implement the
656 // <src>get</src> function.
657 // </synopsis>
659 {
660 public:
665 virtual MArray<Int64> getArrayInt (const vector<TableExprId>&);
666 protected:
667 // If not empty, check if the shape matches that of <src>itsValue</src>.
668 // If <src>itsValue</src> is still empty, it is sized.
669 Bool checkShape (const MArrayBase& arr, const String& func);
671 };
672
673 // <summary>
674 // Abstract base class for aggregate functions giving a double array.
675 // </summary>
676 // <use visibility=local>
677 // <reviewed reviewer="" date="" tests="tExprGroup">
678 // </reviewed>
679 // <synopsis>
680 // This class is derived from TableExprGroupFuncBase and acts as the
681 // abstract base class for aggregate functions resulting in a double array.
682 // <br>Derived classes can use <src>itsValue</src> to contain the
683 // aggregated value. It that case they do not need to implement the
684 // <src>get</src> function.
685 // </synopsis>
687 {
688 public:
693 virtual MArray<Double> getArrayDouble (const vector<TableExprId>&);
694 protected:
695 // If not empty, check if the shape matches that of <src>itsValue</src>.
696 // If <src>itsValue</src> is still empty, it is sized.
697 Bool checkShape (const MArrayBase& arr, const String& func);
699 };
700
701 // <summary>
702 // Abstract base class for aggregate functions giving a dcomplex array.
703 // </summary>
704 // <use visibility=local>
705 // <reviewed reviewer="" date="" tests="tExprGroup">
706 // </reviewed>
707 // <synopsis>
708 // This class is derived from TableExprGroupFuncBase and acts as the
709 // abstract base class for aggregate functions resulting in a dcomplex array.
710 // <br>Derived classes can use <src>itsValue</src> to contain the
711 // aggregated value. It that case they do not need to implement the
712 // <src>get</src> function.
713 // </synopsis>
715 {
716 public:
721 virtual MArray<DComplex> getArrayDComplex (const vector<TableExprId>&);
722 protected:
723 // If not empty, check if the shape matches that of <src>itsValue</src>.
724 // If <src>itsValue</src> is still empty, it is sized.
725 Bool checkShape (const MArrayBase& arr, const String& func);
727 };
728
729 // <summary>
730 // Abstract base class for aggregate functions giving a date/time array.
731 // </summary>
732 // <use visibility=local>
733 // <reviewed reviewer="" date="" tests="tExprGroup">
734 // </reviewed>
735 // <synopsis>
736 // This class is derived from TableExprGroupFuncBase and acts as the
737 // abstract base class for aggregate functions resulting in a date/time array.
738 // <br>Derived classes can use <src>itsValue</src> to contain the
739 // aggregated value. It that case they do not need to implement the
740 // <src>get</src> function.
741 // </synopsis>
743 {
744 public:
749 virtual MArray<MVTime> getArrayDate (const vector<TableExprId>&);
750 protected:
751 // If not empty, check if the shape matches that of <src>itsValue</src>.
752 // If <src>itsValue</src> is still empty, it is sized.
753 Bool checkShape (const MArrayBase& arr, const String& func);
755 };
756
757 // <summary>
758 // Abstract base class for aggregate functions giving a string array.
759 // </summary>
760 // <use visibility=local>
761 // <reviewed reviewer="" date="" tests="tExprGroup">
762 // </reviewed>
763 // <synopsis>
764 // This class is derived from TableExprGroupFuncBase and acts as the
765 // abstract base class for aggregate functions resulting in a string array.
766 // <br>Derived classes can use <src>itsValue</src> to contain the
767 // aggregated value. It that case they do not need to implement the
768 // <src>get</src> function.
769 // </synopsis>
771 {
772 public:
777 virtual MArray<String> getArrayString (const vector<TableExprId>&);
778 protected:
779 // If not empty, check if the shape matches that of <src>itsValue</src>.
780 // If <src>itsValue</src> is still empty, it is sized.
781 Bool checkShape (const MArrayBase& arr, const String& func);
783 };
784
785
786 // <summary>
787 // Class containing the results of aggregated values in a group.
788 // </summary>
789 // <use visibility=local>
790 // <reviewed reviewer="" date="" tests="tExprGroup">
791 // </reviewed>
792 // <synopsis>
793 // This class contains the set of aggregate function objects containing
794 // all aggregate results of a particular GROUPBY group.
795 // It also contains the TableExprId of the last row in the group.
796 // It is used for possible non-aggregate expressions.
797 // </synopsis>
799 {
800 public:
802 : itsId (0)
803 {}
804
805 // Let the aggregate node objects construct the function set.
806 TableExprGroupFuncSet (const vector<TableExprNodeRep*>& aggrNodes);
807
808 // Copying is not needed, thus not allowed.
811
812 // Add a function object.
813 void add (const std::shared_ptr<TableExprGroupFuncBase>& func);
814
815 // Apply the functions to the given row.
816 void apply (const TableExprId& id);
817
818 // Get the vector of functions.
819 const vector<std::shared_ptr<TableExprGroupFuncBase>>& getFuncs() const
820 { return itsFuncs; }
821
822 // Get the TableExprId.
823 const TableExprId& getId() const
824 { return itsId; }
825
826 private:
827 //# Data members.
828 vector<std::shared_ptr<TableExprGroupFuncBase>> itsFuncs;
829 TableExprId itsId; //# row containing the non-aggregate variables
830 };
831
832} //# NAMESPACE CASACORE - END
833
834#endif
Array< T > & array()
Return the cursor.
Definition ArrayIter.h:113
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:570
void append(const IPosition &other)
Append this IPosition with another one (causing a resize).
Bool isNull() const
Is the array null?
Definition MArrayBase.h:109
uInt ndim() const
Get the dimensionality.
Definition MArrayBase.h:141
const Array< Bool > & mask() const
Get the mask.
Definition MArrayBase.h:124
Bool hasMask() const
Is there a mask?
Definition MArrayBase.h:117
const IPosition & shape() const
Get the shape.
Definition MArrayBase.h:145
const Array< T > & array() const
Get access to the array.
Definition MArray.h:151
String: the storage and methods of handling collections of characters.
Definition String.h:223
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:390
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:343
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:633
TableExprGroupFuncArrayDComplex(TableExprNodeRep *node)
Definition ExprGroup.h:717
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:745
virtual MArray< Double > getArrayDouble(const vector< TableExprId > &)
TableExprGroupFuncArrayDouble(TableExprNodeRep *node)
Definition ExprGroup.h:689
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:661
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:773
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:213
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:216
TableExprNodeRep * itsOperand
Definition ExprGroup.h:248
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:472
TableExprGroupFuncBool(TableExprNodeRep *node)
Definition ExprGroup.h:469
TableExprGroupFuncDComplex(TableExprNodeRep *node, const DComplex &initValue=DComplex())
Definition ExprGroup.h:552
virtual DComplex getDComplex(const vector< TableExprId > &)
virtual MVTime getDate(const vector< TableExprId > &)
TableExprGroupFuncDate(TableExprNodeRep *node, const MVTime &initValue=MVTime())
Definition ExprGroup.h:579
virtual Double getDouble(const vector< TableExprId > &)
TableExprGroupFuncDouble(TableExprNodeRep *node, Double initValue=0)
Definition ExprGroup.h:525
virtual Double getDouble(const vector< TableExprId > &)
virtual Int64 getInt(const vector< TableExprId > &)
TableExprGroupFuncInt(TableExprNodeRep *node, Int64 initValue=0)
Definition ExprGroup.h:498
Class containing the results of aggregated values in a group.
Definition ExprGroup.h:799
const vector< std::shared_ptr< TableExprGroupFuncBase > > & getFuncs() const
Get the vector of functions.
Definition ExprGroup.h:819
const TableExprId & getId() const
Get the TableExprId.
Definition ExprGroup.h:823
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:828
TableExprGroupFuncString(TableExprNodeRep *node, const String &initValue=String())
Definition ExprGroup.h:606
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:110
bool operator==(const TableExprGroupKeySet &) const
Compare all keys in the set.
vector< TableExprGroupKey > itsKeys
Definition ExprGroup.h:123
bool operator<(const TableExprGroupKey &) const
void set(const String &v)
Definition ExprGroup.h:70
TableExprGroupKey(TableExprNodeRep::NodeDataType dtype)
Construct for a given data type.
Definition ExprGroup.h:54
bool operator==(const TableExprGroupKey &) const
Compare this and that key.
TableExprNodeRep::NodeDataType itsDT
Definition ExprGroup.h:81
TableExprNodeRep::NodeDataType dataType() const
Get the data type.
Definition ExprGroup.h:59
void set(Bool v)
Set the key's value.
Definition ExprGroup.h:64
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:164
uInt ngroup() const
Get the nr of groups.
Definition ExprGroup.h:155
vector< std::shared_ptr< vector< TableExprId > > > itsIds
Definition ExprGroup.h:165
TableExprGroupFuncSet & funcSet(uInt group) const
Get the set of functions (and their results) for the given group.
Definition ExprGroup.h:158
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:161
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.
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
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