casacore
ExprFuncNode.h
Go to the documentation of this file.
1 //# ExprFuncNode.h: Class representing a function in table select expression
2 //# Copyright (C) 1994,1995,1996,1997,1998,2000,2001,2003
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$
27 
28 #ifndef TABLES_EXPRFUNCNODE_H
29 #define TABLES_EXPRFUNCNODE_H
30 
31 //# Includes
32 #include <casacore/casa/aips.h>
33 #include <casacore/tables/TaQL/ExprNodeRep.h>
34 #include <casacore/casa/Quanta/MVAngle.h>
35 
36 namespace casacore { //# NAMESPACE CASACORE - BEGIN
37 
38 //# Forward Declarations
39 class TableExprNodeSet;
40 
41 
42 // <summary>
43 // Class representing a function in table select expression
44 // </summary>
45 
46 // <use visibility=local>
47 
48 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="">
49 // </reviewed>
50 // <prerequisite>
51 //# Classes you should understand before using this one.
52 // <li> <linkto class=TableExprNodeMulti>TableExprNodeMulti</linkto>
53 // </prerequisite>
54 
55 // <synopsis>
56 // This class represents a function in a table select tree.
57 // The <src>rownumber</src> function is represented by class
58 // <linkto class=TableExprNodeRownr>TableExprNodeRownr</linkto>.
59 // The <src>rowid</src> function is represented by class
60 // <linkto class=TableExprNodeRowid>TableExprNodeRowid</linkto>.
61 // The <src>rand</src> function is represented by class
62 // <linkto class=TableExprNodeRandom>TableExprNodeRandom</linkto>.
63 // <p>
64 // When one wants to add a function to the table selection grammar,
65 // the following has to be done:
66 // <ul>
67 // <li> Add the function to the enum below.
68 // <li> Implement the function in the get functions in ExprFuncNode(Array).cc.
69 // <li> Implement the function in the checkOperands in ExprFuncNode.cc.
70 // <li> Declare and define the function in ExprNode.h (for C++ binding).
71 // <li> Add the function to findFunc in TableParse.cc (for TaQL).
72 // </ul>
73 // </synopsis>
74 
75 
77 {
78 public:
79  //# Define the function types.
80  enum FunctionType {
81  piFUNC, //# 0
82  eFUNC, //# 1
83  cFUNC, //# 2
84  // for Int, or Double or Complex returning Bool
85  // (2 is with default tolerance)
86  near2FUNC, //# 3
87  near3FUNC, //# 4
88  nearabs2FUNC, //# 5
89  nearabs3FUNC, //# 6
90  // for Int, Double or DComplex returning Double or Complex
91  sinFUNC, //# 7
92  sinhFUNC, //# 8
93  cosFUNC, //# 9
94  coshFUNC, //# 10
95  expFUNC, //# 11
96  logFUNC, //# 12
97  log10FUNC, //# 13
98  sqrtFUNC, //# 14
99  powFUNC, //# 15
100  conjFUNC, //# 16
101  // for Int, Double or DComplex returning Int, Double or Complex
102  squareFUNC, //# 17
103  cubeFUNC, //# 18
104  minFUNC, //# 19
105  maxFUNC, //# 20
106  // for Int, Double or DComplex returning Int or Double
107  normFUNC, //# 21
108  absFUNC, //# 22
109  argFUNC, //# 23
110  // for Int, Double or DComplex returning Double
111  realFUNC, //# 24
112  imagFUNC, //# 25
113  // for Int or Double returning Int (using floor)
114  intFUNC, //# 26
115  // for Int or Double returning Double
116  asinFUNC, //# 27
117  acosFUNC, //# 28
118  atanFUNC, //# 29
119  atan2FUNC, //# 30
120  tanFUNC, //# 31
121  tanhFUNC, //# 32
122  // for Int or Double returning Int or Double
123  signFUNC, //# 33
124  roundFUNC, //# 34
125  floorFUNC, //# 35
126  ceilFUNC, //# 36
127  fmodFUNC, //# 37
128  // for Int, Double or DComplex returning DComplex
129  complexFUNC, //# 38
130  // for Int, Double or Complex array returning the same
131  arrsumFUNC, //# 39
132  arrsumsFUNC, //# 40
137  // for Int or Double array returning Int or Double
138  arrminFUNC, //# 45
139  arrminsFUNC, //# 46
140  runminFUNC, //# 47
141  boxminFUNC, //# 48
142  arrmaxFUNC, //# 49
143  arrmaxsFUNC, //# 50
144  runmaxFUNC, //# 51
145  boxmaxFUNC, //# 52
146  // for Int or Double array returning Double
147  arrmeanFUNC, //# 53
148  arrmeansFUNC, //# 54
149  runmeanFUNC, //# 55
150  boxmeanFUNC, //# 56
159  arravdevFUNC, //# 65
161  runavdevFUNC, //# 67
162  boxavdevFUNC, //# 68
163  arrrmsFUNC, //# 69
164  arrrmssFUNC, //# 70
165  runrmsFUNC, //# 71
166  boxrmsFUNC, //# 72
173  // for Bool array returning Bool
174  anyFUNC, //# 79
175  anysFUNC, //# 80
176  runanyFUNC, //# 81
177  boxanyFUNC, //# 82
178  allFUNC, //# 83
179  allsFUNC, //# 84
180  runallFUNC, //# 85
181  boxallFUNC, //# 86
182  // for Bool array returning Int scalar
183  ntrueFUNC, //# 87
184  ntruesFUNC, //# 88
185  nfalseFUNC, //# 89
186  nfalsesFUNC, //# 90
187  // for any type returning array of that type
188  arrayFUNC, //# 91
190  resizeFUNC, // #93
191  // for Int, Double or DComplex array returning Bool
192  isnanFUNC, //# 94
193  isinfFUNC, //# 95
194  isfiniteFUNC, //# 96
195  // for any array returning Bool scalar
196  isdefFUNC, //# 97
197  // for any array returning Int scalar
198  ndimFUNC, //# 98
199  nelemFUNC, //# 99
200  // for any array returning Int array
201  shapeFUNC, //# 100
202  // for String
203  strlengthFUNC, //# 101 returning Int
204  upcaseFUNC, //# 102 returning String
205  downcaseFUNC, //# 103 returning String
206  capitalizeFUNC, //# 104 returning String
207  trimFUNC, //# 105 returning String
208  ltrimFUNC, //# 106 returning String
209  rtrimFUNC, //# 107 returning String
210  substrFUNC, //# 108 returning String
211  replaceFUNC, //# 109 returning String
212  regexFUNC, //# 110 returning TaqlRegex
213  patternFUNC, //# 111 returning TaqlRegex
214  sqlpatternFUNC, //# 112 returning TaqlRegex
215  // for Date
216  datetimeFUNC, //# 113 returning Date
217  mjdtodateFUNC, //# 114 returning Date
218  mjdFUNC, //# 115 returning Double
219  dateFUNC, //# 116 returning Date
220  timeFUNC, //# 117 returning Double (in radians)
221  yearFUNC, //# 118 returning Int
222  monthFUNC, //# 119 returning Int
223  dayFUNC, //# 120 returning Int
224  cmonthFUNC, //# 121 returning String
225  weekdayFUNC, //# 122 returning Int
226  cdowFUNC, //# 123 returning String
227  weekFUNC, //# 124 returning Int
228  ctodFUNC, //# 125 returning String
229  cdateFUNC, //# 126 returning String
230  ctimeFUNC, //# 127 returning String
231  // return values as strings
232  stringFUNC, //# 128
233  // return angles as hms strings
234  hmsFUNC, //# 129
235  // return angles as dms strings
236  dmsFUNC, //# 130
237  // return angles as hms/dms strings
238  hdmsFUNC, //# 131
239  // special function returning a random Double number
240  randFUNC, //# 132
241  // special function returning Int row number
242  rownrFUNC, //# 133
243  // special function returning Int row id (meant for GIVING)
244  rowidFUNC, //# 134
245  // special function resembling if statement
246  iifFUNC, //# 135
247  // angular distance returning radians
248  angdistFUNC, //# 136
249  angdistxFUNC, //# 137
250  // other functions, implemented in derived class
251  conesFUNC, //# 138
252  cones3FUNC, //# 139
253  anyconeFUNC, //# 140
254  anycone3FUNC, //# 141
255  findconeFUNC, //# 142
256  findcone3FUNC, //# 143
257  //# AGGREGATE functions must be the last ones.
258  FirstAggrFunc, //# 144
263  //# Grouping doing aggregation on the fly; reducing to a scalar per group
264  gminFUNC, //# 148
277  //# Grouping doing aggregation on the fly; reducing to an array per group
294  //# Grouping requiring aggregation of rows when getting result
295  gaggrFUNC, //# 175
299  gexpridFUNC, //# special function (can be inserted by TableParse)
300  NRFUNC //# should be last
301  };
302 
303  // Constructor
305  const TableExprNodeSet& source);
306 
307  // Destructor
309 
310  // 'get' Functions to get the desired result of a function
311  // <group>
312  Bool getBool (const TableExprId& id);
313  Int64 getInt (const TableExprId& id);
314  Double getDouble (const TableExprId& id);
315  DComplex getDComplex (const TableExprId& id);
316  String getString (const TableExprId& id);
317  TaqlRegex getRegex (const TableExprId& id);
318  MVTime getDate (const TableExprId& id);
319  // </group>
320 
321  // Check the data and value types of the operands.
322  // It sets the exptected data and value types of the operands.
323  // Set the value type of the function result and returns
324  // the data type of the function result.
325  static NodeDataType checkOperands (Block<Int>& dtypeOper,
326  ValueType& resVT,
327  Block<Int>& vtypeOper,
328  FunctionType,
330 
331  // Fill the result unit in the node.
332  // Adapt the children nodes if their units need to be converted.
333  // It returns a possible scale factor in case result unit is SI (for sqrt).
334  static Double fillUnits (TableExprNodeRep* node,
336  FunctionType func);
337 
338  // Link the children to the node and convert the children
339  // to constants if possible. Also convert the node to
340  // constant if possible.
341  static TableExprNodeRep* fillNode (TableExprFuncNode* thisNode,
343  const Block<Int>& dtypeOper);
344 
345  // Link the children to the node and convert the children
346  // to constants if possible.
347  static void fillChildNodes (TableExprFuncNode* thisNode,
349  const Block<Int>& dtypeOper);
350 
351  // Set unit scale factor (needed for sqrt).
352  void setScale (Double scale)
353  { scale_p = scale; }
354 
355  // Get possible unit scale factor (needed for sqrt).
356  Double getScale() const
357  { return scale_p; }
358 
359  // Some functions to be used by TableExprNodeFuncArray.
360  // <group>
362  { return operands_p; }
364  { return operands_p; }
366  { return funcType_p; }
368  { return argDataType_p; }
369  // </group>
370 
371  // Get the possible print format, width, and/or precision.
372  static void getPrintFormat (String& fmt, Int& width, Int& prec,
374  const TableExprId& id);
375 
376  // Convert the date and/or time to a string.
377  // <group>
378  static String stringDT (const MVTime& dt, Int prec, MVTime::formatTypes);
379  static String stringDateTime (const MVTime& dt, Int prec);
380  static String stringDate (const MVTime& dt);
381  static String stringTime (const MVTime& dt, Int prec);
382  // </group>
383 
384  // Convert a value to a string.
385  // If <src>fmt</src> is empty, ostringstream is used.
386  // Otherwise the printf-like format is used.
387  // If possible, a double value is converted to radians if formatted as angle.
388  // <group>
389  static String stringValue (Bool val, const String& fmt, Int width);
390  static String stringValue (Int64 val, const String& fmt, Int width);
391  static String stringValue (Double val, const String& fmt,
392  Int width, Int prec,
393  const std::pair<int,int>& mvFormat,
394  const Unit& unit);
395  static String stringValue (const DComplex& val, const String& fmt,
396  Int width, Int prec);
397  static String stringValue (const String& val, const String& fmt,
398  Int width);
399  static String stringValue (const MVTime& val, const String& fmt,
400  Int width,
401  const std::pair<int,int>& mvFormat);
402 
403  // Convert angle to a string (hms or dms).
404  // <group>
405  static String stringAngle (double val, Int prec,
406  MVAngle::formatTypes type);
407  static String stringHMS (double val, Int prec);
408  static String stringDMS (double val, Int prec);
409  // </group>
410 
411  // Get the MVTime/Angle format and optional precision.
412  // 0,0 is returned if empty or unknown format.
413  static std::pair<int,int> getMVFormat (const String& fmt);
414 
415  // Get the angular distance between two positions on a sphere.
416  static double angdist (double ra1, double dec1, double ra2, double dec2)
417  { return acos (sin(dec1)*sin(dec2) + cos(dec1)*cos(dec2)*cos(ra1-ra2)); }
418 
419 private:
420  // Try if the function gives a constant result.
421  // If so, set the expression type to Constant.
422  void tryToConst();
423 
424  // Make the units of nodes from <src>starg</src> till <src>endarg</src>
425  // equal. Return the unit found.
426  static const Unit& makeEqualUnits (PtrBlock<TableExprNodeRep*>& nodes,
427  uInt starg, uInt endarg);
428 
429  //# Data members.
430  FunctionType funcType_p; // which function
431  NodeDataType argDataType_p; // common argument data type
432  Double scale_p; // possible scaling for unit conversion
433  // (needed for sqrt)
434 };
435 
436 
437 } //# NAMESPACE CASACORE - END
438 
439 #endif
Class representing a function in table select expression.
Definition: ExprFuncNode.h:76
static String stringDateTime(const MVTime &dt, Int prec)
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size)
Definition: aipsxtype.h:38
int Int
Definition: aipstype.h:47
return angles as dms strings
Definition: ExprFuncNode.h:236
special function returning Int row id (meant for GIVING)
Definition: ExprFuncNode.h:244
Int64 getInt(const TableExprId &id)
for Int or Double returning Double
Definition: ExprFuncNode.h:116
static double angdist(double ra1, double dec1, double ra2, double dec2)
Get the angular distance between two positions on a sphere.
Definition: ExprFuncNode.h:416
for any array returning Bool scalar
Definition: ExprFuncNode.h:196
for Bool array returning Bool
Definition: ExprFuncNode.h:174
other functions, implemented in derived class
Definition: ExprFuncNode.h:251
DComplex getDComplex(const TableExprId &id)
Double getScale() const
Get possible unit scale factor (needed for sqrt).
Definition: ExprFuncNode.h:356
static TableExprNodeRep * fillNode(TableExprFuncNode *thisNode, PtrBlock< TableExprNodeRep *> &nodes, const Block< Int > &dtypeOper)
Link the children to the node and convert the children to constants if possible.
Class to hold multiple table expression nodes.
Definition: ExprNodeSet.h:305
MVTime getDate(const TableExprId &id)
void setScale(Double scale)
Set unit scale factor (needed for sqrt).
Definition: ExprFuncNode.h:352
PtrBlock< TableExprNodeRep * > operands_p
Definition: ExprNodeRep.h:692
PtrBlock< TableExprNodeRep * > & rwOperands()
Definition: ExprFuncNode.h:363
angular distance returning radians
Definition: ExprFuncNode.h:248
String getString(const TableExprId &id)
NodeDataType
Define the data types of a node.
Definition: ExprNodeRep.h:154
formatTypes
Format types.
Definition: MVAngle.h:251
Abstract base class for a node in a table column expression tree.
Definition: ExprNodeRep.h:150
void tryToConst()
Try if the function gives a constant result.
const PtrBlock< TableExprNodeRep * > & operands() const
Some functions to be used by TableExprNodeFuncArray.
Definition: ExprFuncNode.h:361
static String stringDMS(double val, Int prec)
FunctionType funcType() const
Definition: ExprFuncNode.h:365
LatticeExprNode cos(const LatticeExprNode &expr)
static String stringTime(const MVTime &dt, Int prec)
for any array returning Int scalar
Definition: ExprFuncNode.h:198
for any array returning Int array
Definition: ExprFuncNode.h:201
for Int or Double returning Int (using floor)
Definition: ExprFuncNode.h:114
static void getPrintFormat(String &fmt, Int &width, Int &prec, const PtrBlock< TableExprNodeRep *> &operands, const TableExprId &id)
Get the possible print format, width, and/or precision.
defines physical units
Definition: Unit.h:189
for any type returning array of that type
Definition: ExprFuncNode.h:188
for Int, Double or DComplex returning Int, Double or Complex
Definition: ExprFuncNode.h:102
special function resembling if statement
Definition: ExprFuncNode.h:246
special function returning Int row number
Definition: ExprFuncNode.h:242
for Int, Double or DComplex array returning Bool
Definition: ExprFuncNode.h:192
for Int, Double or DComplex returning Double or Complex
Definition: ExprFuncNode.h:91
formatTypes
Format types.
Definition: MVTime.h:272
Abstract base class for a node having multiple child nodes.
Definition: ExprNodeRep.h:656
for Int, or Double or Complex returning Bool (2 is with default tolerance)
Definition: ExprFuncNode.h:86
double Double
Definition: aipstype.h:52
static String stringAngle(double val, Int prec, MVAngle::formatTypes type)
Convert angle to a string (hms or dms).
TaqlRegex getRegex(const TableExprId &id)
return angles as hms/dms strings
Definition: ExprFuncNode.h:238
for Bool array returning Int scalar
Definition: ExprFuncNode.h:183
static void fillChildNodes(TableExprFuncNode *thisNode, PtrBlock< TableExprNodeRep *> &nodes, const Block< Int > &dtypeOper)
Link the children to the node and convert the children to constants if possible.
TableExprFuncNode(FunctionType, NodeDataType, ValueType, const TableExprNodeSet &source)
Constructor.
Double getDouble(const TableExprId &id)
NodeDataType argDataType() const
Definition: ExprFuncNode.h:367
for Int, Double or Complex array returning the same
Definition: ExprFuncNode.h:131
for Int or Double returning Int or Double
Definition: ExprFuncNode.h:123
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:39
static std::pair< int, int > getMVFormat(const String &fmt)
Get the MVTime/Angle format and optional precision.
ValueType
Define the value types.
Definition: ExprNodeRep.h:169
static Double fillUnits(TableExprNodeRep *node, PtrBlock< TableExprNodeRep *> &nodes, FunctionType func)
Fill the result unit in the node.
static String stringHMS(double val, Int prec)
for Int, Double or DComplex returning DComplex
Definition: ExprFuncNode.h:129
special function returning a random Double number
Definition: ExprFuncNode.h:240
return angles as hms strings
Definition: ExprFuncNode.h:234
const Unit & unit() const
Get the unit.
Definition: ExprNodeRep.h:722
A drop-in replacement for Block<T*>.
Definition: Block.h:861
~TableExprFuncNode()
Destructor.
static String stringValue(Bool val, const String &fmt, Int width)
Convert a value to a string.
Bool getBool(const TableExprId &id)
&#39;get&#39; Functions to get the desired result of a function
for Int or Double array returning Int or Double
Definition: ExprFuncNode.h:138
static const Unit & makeEqualUnits(PtrBlock< TableExprNodeRep *> &nodes, uInt starg, uInt endarg)
Make the units of nodes from starg till endarg equal.
static String stringDT(const MVTime &dt, Int prec, MVTime::formatTypes)
Convert the date and/or time to a string.
The identification of a TaQL selection subject.
Definition: TableExprId.h:98
for Int, Double or DComplex returning Int or Double
Definition: ExprFuncNode.h:107
Class to handle a Regex or StringDistance.
Definition: ExprNodeRep.h:74
LatticeExprNode acos(const LatticeExprNode &expr)
String: the storage and methods of handling collections of characters.
Definition: String.h:223
static String stringDate(const MVTime &dt)
for Int or Double array returning Double
Definition: ExprFuncNode.h:147
Class to handle date/time type conversions and I/O.
Definition: MVTime.h:266
static NodeDataType checkOperands(Block< Int > &dtypeOper, ValueType &resVT, Block< Int > &vtypeOper, FunctionType, PtrBlock< TableExprNodeRep *> &)
Check the data and value types of the operands.
this file contains all the compiler specific defines
Definition: mainpage.dox:28
LatticeExprNode sin(const LatticeExprNode &expr)
Numerical 1-argument functions.
for Int, Double or DComplex returning Double
Definition: ExprFuncNode.h:111
unsigned int uInt
Definition: aipstype.h:48