File: ExprDerNode.cc

package info (click to toggle)
casacore 3.8.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,912 kB
  • sloc: cpp: 471,569; fortran: 16,372; ansic: 7,416; yacc: 4,714; lex: 2,346; sh: 1,865; python: 629; perl: 531; sed: 499; csh: 201; makefile: 32
file content (364 lines) | stat: -rw-r--r-- 11,637 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//# ExprDerNode.cc: Nodes representing scalar operators in table select expression tree
//# Copyright (C) 1994,1995,1996,1997,1998,1999,2000,2001
//# Associated Universities, Inc. Washington DC, USA.
//#
//# This library is free software; you can redistribute it and/or modify it
//# under the terms of the GNU Library General Public License as published by
//# the Free Software Foundation; either version 2 of the License, or (at your
//# option) any later version.
//#
//# This library is distributed in the hope that it will be useful, but WITHOUT
//# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
//# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
//# License for more details.
//#
//# You should have received a copy of the GNU Library General Public License
//# along with this library; if not, write to the Free Software Foundation,
//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
//#
//# Correspondence concerning AIPS++ should be addressed as follows:
//#        Internet email: casa-feedback@nrao.edu.
//#        Postal address: AIPS++ Project Office
//#                        National Radio Astronomy Observatory
//#                        520 Edgemont Road
//#                        Charlottesville, VA 22903-2475 USA

#include <casacore/tables/TaQL/ExprDerNode.h>
#include <casacore/tables/Tables/Table.h>
#include <casacore/tables/Tables/TableRecord.h>
#include <casacore/tables/Tables/ScalarColumn.h>
#include <casacore/tables/Tables/ColumnDesc.h>
#include <casacore/tables/Tables/TableError.h>
#include <casacore/casa/Arrays/Vector.h>
#include <casacore/casa/Arrays/ArrayLogical.h>
#include <casacore/casa/Utilities/DataType.h>
#include <casacore/casa/BasicMath/Math.h>
#include <casacore/casa/Quanta/MVTime.h>
#include <casacore/casa/OS/Time.h>
#include <casacore/casa/Utilities/Assert.h>
#include <casacore/casa/Exceptions/Error.h>



namespace casacore { //# NAMESPACE CASACORE - BEGIN

// Implement the constants for each data type.

TableExprNodeConstBool::TableExprNodeConstBool (const Bool& val)
: TableExprNodeBinary (NTBool, VTScalar, OtLiteral, Constant),
  value_p             (val)
{}
Bool TableExprNodeConstBool::getBool (const TableExprId&)
    { return value_p; }

TableExprNodeConstInt::TableExprNodeConstInt (const Int64& val)
: TableExprNodeBinary (NTInt, VTScalar, OtLiteral, Constant),
  value_p             (val)
{}
Int64 TableExprNodeConstInt::getInt (const TableExprId&)
    { return value_p; }
Double TableExprNodeConstInt::getDouble (const TableExprId&)
    { return value_p; }
DComplex TableExprNodeConstInt::getDComplex (const TableExprId&)
    { return double(value_p); }

TableExprNodeConstDouble::TableExprNodeConstDouble (const Double& val)
: TableExprNodeBinary (NTDouble, VTScalar, OtLiteral, Constant),
  value_p             (val)
{}
Double TableExprNodeConstDouble::getDouble (const TableExprId&)
    { return value_p; }
DComplex TableExprNodeConstDouble::getDComplex (const TableExprId&)
    { return value_p; }

TableExprNodeConstDComplex::TableExprNodeConstDComplex (const DComplex& val)
: TableExprNodeBinary (NTComplex, VTScalar, OtLiteral, Constant),
  value_p             (val)
{}
DComplex TableExprNodeConstDComplex::getDComplex (const TableExprId&)
    { return value_p; }

TableExprNodeConstString::TableExprNodeConstString (const String& val)
: TableExprNodeBinary (NTString, VTScalar, OtLiteral, Constant),
  value_p             (val)
{}
String TableExprNodeConstString::getString (const TableExprId&)
    { return value_p; }

TableExprNodeConstRegex::TableExprNodeConstRegex (const TaqlRegex& val)
: TableExprNodeBinary (NTRegex, VTScalar, OtLiteral, Constant),
  value_p             (val)
{}
TaqlRegex TableExprNodeConstRegex::getRegex (const TableExprId&)
    { return value_p; }

TableExprNodeConstDate::TableExprNodeConstDate (const MVTime& val)
: TableExprNodeBinary (NTDate, VTScalar, OtLiteral, Constant),
  value_p             (val)
{}
Double TableExprNodeConstDate::getDouble (const TableExprId&)
    { return value_p; }
MVTime TableExprNodeConstDate::getDate (const TableExprId&)
    { return value_p; }


// <thrown>
//   <li> TableInvExpr
// </thrown>
//# Create a table expression node for a column.
//# First use a "dummy" data type and fill it in later.
//# Similarly for the value type.
TableExprNodeColumn::TableExprNodeColumn (const TableExprInfo& tableInfo,
                                          const String& name)
  : TableExprNodeBinary (NTNumeric, VTScalar, OtColumn, Variable),
    tableInfo_p      (tableInfo),
    tabCol_p         (tableInfo.table(), name),
    applySelection_p (True)
{
    //# Check if the column is a scalar.
    if (! tabCol_p.columnDesc().isScalar()) {
        throw (TableInvExpr (name, " is no scalar column"));
    }
    //# Fill in the real data type and the base table pointer.
    switch (tabCol_p.columnDesc().dataType()) {
    case TpBool:
        dtype_p = NTBool;
        break;
    case TpString:
        dtype_p = NTString;
        break;
    case TpComplex:
    case TpDComplex:
        dtype_p = NTComplex;
        break;
    case TpFloat:
    case TpDouble:
        dtype_p = NTDouble;
        break;
    default:
        dtype_p = NTInt;
    }
    setUnit (getColumnUnit(tabCol_p));
}

TableExprInfo TableExprNodeColumn::getTableInfo() const
{
    return tableInfo_p;
}

Unit TableExprNodeColumn::getColumnUnit (const TableColumn& tabcol)
{
    Unit unit;
    //# Get the unit (if defined).
    const TableRecord& keyset = tabcol.keywordSet();
    if (keyset.isDefined ("QuantumUnits")) {
        const Array<String>& units = keyset.asArrayString("QuantumUnits");
        if (units.size() > 0) {
            unit = *(units.data());
        }
    } else if (keyset.isDefined ("UNIT")) {
        unit = keyset.asString("UNIT");
    }
    return unit;
}


void TableExprNodeColumn::disableApplySelection()
{
    applySelection_p = False;
}
  
void TableExprNodeColumn::applySelection (const Vector<rownr_t>& rownrs)
{
    if (applySelection_p) {
        // Attach the column to the selection of the table.
        // Get column name before doing selection!!!!
        String name = tabCol_p.columnDesc().name();
        tableInfo_p.apply (rownrs);
        tabCol_p = TableColumn(tableInfo_p.table(), name);
        // Reset switch, because the column object can be used multiple times.
        // when a select expression is used as e.g. sort key.
        applySelection_p = False;
    }
}

//# Return the TableColumn.
const TableColumn& TableExprNodeColumn::getColumn() const
    { return tabCol_p; }

Bool TableExprNodeColumn::getBool (const TableExprId& id)
{
    Bool val;
    tabCol_p.getScalar (id.rownr(), val);
    return val;
}
Int64 TableExprNodeColumn::getInt (const TableExprId& id)
{
    Int64 val;
    tabCol_p.getScalar (id.rownr(), val);
    return val;
}
Double TableExprNodeColumn::getDouble (const TableExprId& id)
{
    Double val;
    tabCol_p.getScalar (id.rownr(), val);
    return val;
}
DComplex TableExprNodeColumn::getDComplex (const TableExprId& id)
{
    DComplex val;
    tabCol_p.getScalar (id.rownr(), val);
    return val;
}
String TableExprNodeColumn::getString (const TableExprId& id)
{
    String val;
    tabCol_p.getScalar (id.rownr(), val);
    return val;
}

Bool TableExprNodeColumn::getColumnDataType (DataType& dt) const
{
    dt = tabCol_p.columnDesc().dataType();
    return True;
}

Array<Bool>     TableExprNodeColumn::getColumnBool (const Vector<rownr_t>& rownrs)
{
    ScalarColumn<Bool> col (tabCol_p);
    return col.getColumnCells (rownrs);
}
Array<uChar>    TableExprNodeColumn::getColumnuChar (const Vector<rownr_t>& rownrs)
{
    ScalarColumn<uChar> col (tabCol_p);
    return col.getColumnCells (rownrs);
}
Array<Short>    TableExprNodeColumn::getColumnShort (const Vector<rownr_t>& rownrs)
{
    ScalarColumn<Short> col (tabCol_p);
    return col.getColumnCells (rownrs);
}
Array<uShort>   TableExprNodeColumn::getColumnuShort (const Vector<rownr_t>& rownrs)
{
    ScalarColumn<uShort> col (tabCol_p);
    return col.getColumnCells (rownrs);
}
Array<Int>      TableExprNodeColumn::getColumnInt (const Vector<rownr_t>& rownrs)
{
    ScalarColumn<Int> col (tabCol_p);
    return col.getColumnCells (rownrs);
}
Array<uInt>     TableExprNodeColumn::getColumnuInt (const Vector<rownr_t>& rownrs)
{
    ScalarColumn<uInt> col (tabCol_p);
    return col.getColumnCells (rownrs);
}
Array<Int64>    TableExprNodeColumn::getColumnInt64 (const Vector<rownr_t>& rownrs)
{
    ScalarColumn<Int64> col (tabCol_p);
    return col.getColumnCells (rownrs);
}
Array<Float>    TableExprNodeColumn::getColumnFloat (const Vector<rownr_t>& rownrs)
{
    ScalarColumn<Float> col (tabCol_p);
    return col.getColumnCells (rownrs);
}
Array<Double>   TableExprNodeColumn::getColumnDouble (const Vector<rownr_t>& rownrs)
{
    ScalarColumn<Double> col (tabCol_p);
    return col.getColumnCells (rownrs);
}
Array<Complex>  TableExprNodeColumn::getColumnComplex (const Vector<rownr_t>& rownrs)
{
    ScalarColumn<Complex> col (tabCol_p);
    return col.getColumnCells (rownrs);
}
Array<DComplex> TableExprNodeColumn::getColumnDComplex (const Vector<rownr_t>& rownrs)
{
    ScalarColumn<DComplex> col (tabCol_p);
    return col.getColumnCells (rownrs);
}
Array<String>   TableExprNodeColumn::getColumnString (const Vector<rownr_t>& rownrs)
{
    ScalarColumn<String> col (tabCol_p);
    return col.getColumnCells (rownrs);
}



TableExprNodeRownr::TableExprNodeRownr (const TableExprInfo& tableInfo, uInt origin)
: TableExprNodeBinary (NTInt, VTScalar, OtRownr, Variable),
  tableInfo_p         (tableInfo),
  origin_p            (origin)
{}
TableExprInfo TableExprNodeRownr::getTableInfo() const
{
    return tableInfo_p;
}
Int64 TableExprNodeRownr::getInt (const TableExprId& id)
{
    AlwaysAssert (id.byRow(), AipsError);
    return id.rownr() + origin_p;
}



TableExprNodeRowid::TableExprNodeRowid (const TableExprInfo& tableInfo)
  : TableExprNodeBinary (NTInt, VTScalar, OtRownr, Variable),
    tableInfo_p         (tableInfo),
    rownrs_p            (tableInfo.table().nrow())
{
  indgen (rownrs_p);
}
TableExprInfo TableExprNodeRowid::getTableInfo() const
{
    return tableInfo_p;
}
void TableExprNodeRowid::applySelection (const Vector<rownr_t>& rownrs)
{
    // Append rows for an insert.
    if (rownrs.size() == 1  &&  rownrs[0] >= rownrs_p.size()) {
        rownr_t sz = rownrs_p.size();
        rownrs_p.resize (rownrs[0], True);
        for (rownr_t i=sz; i<rownrs_p.size(); ++i) {
            rownrs_p[i] = i;
        }
    } else {
        // Select the rowid-s of the given rows.
        Vector<rownr_t> newRows(rownrs.size());
        for (rownr_t i=0; i<rownrs.size(); ++i) {
            newRows[i] = rownrs_p[rownrs[i]];
        }
        rownrs_p.reference (newRows);
    }
}
Int64 TableExprNodeRowid::getInt (const TableExprId& id)
{
    AlwaysAssert (id.byRow(), AipsError);
    if (id.rownr() < Int64(rownrs_p.size())) {
      return rownrs_p[id.rownr()];
    }
    return 0;
}



//# Take the seed from the current time and date.
TableExprNodeRandom::TableExprNodeRandom (const TableExprInfo& tableInfo)
: TableExprNodeBinary (NTDouble, VTScalar, OtRandom, Variable),
  tableInfo_p         (tableInfo),
  generator_p         (Int (fmod (Time().modifiedJulianDay(), 1.) * 86400000),
                       Int (Time().modifiedJulianDay())),
  random_p            (&generator_p, 0, 1)
{}
TableExprInfo TableExprNodeRandom::getTableInfo() const
{
    return tableInfo_p;
}
Double TableExprNodeRandom::getDouble (const TableExprId&)
{
    return random_p();
}

} //# NAMESPACE CASACORE - END