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