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 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
|
//# ExprAggrNode.cc: TaQL node representing an aggregate function
//# Copyright (C) 2013
//# 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
//# Includes
#include <casacore/tables/TaQL/ExprAggrNode.h>
#include <casacore/tables/TaQL/ExprGroupAggrFunc.h>
#include <casacore/tables/TaQL/ExprGroupAggrFuncArray.h>
#include <casacore/tables/TaQL/TableExprIdAggr.h>
#include <casacore/tables/TaQL/ExprNodeUtil.h>
#include <casacore/tables/Tables/TableError.h>
namespace casacore { //# NAMESPACE CASACORE - BEGIN
TableExprAggrNode::TableExprAggrNode (FunctionType ftype,
NodeDataType dtype,
ValueType vtype,
const TableExprNodeSet& source,
const vector<TENShPtr>& nodes,
const Block<Int>& dtypeOper)
: TableExprFuncNode (ftype, dtype, vtype, source, nodes, dtypeOper)
{
// Always treat an aggregate as a variable expression.
// Otherwise it might be treated as constant and evaluated immediately
// which cannot be done.
exprtype_p = Variable;
}
Bool TableExprAggrNode::isAggregate() const
{
return True;
}
TableExprFuncNode::NodeDataType TableExprAggrNode::checkOperands
(Block<Int>& dtypeOper, ValueType& resVT, FunctionType ftype,
vector<TENShPtr>& nodes)
{
if (ftype >= FirstAggrArrayFunc && ftype < LastAggrArrayFunc &&
nodes.size() > 0 && nodes[0]->valueType() != VTArray) {
throw TableInvExpr ("Argument of GxxxS functions has to be an array");
}
for (const TENShPtr& n : nodes) {
if (! TableExprNodeUtil::getAggrNodes(n.get()).empty()) {
throw TableInvExpr ("The argument of an aggregate function cannot use "
"an aggregate function");
}
}
resVT = VTScalar;
switch (ftype) {
case countallFUNC:
checkNumOfArg (0, 0, nodes);
return NTInt;
case gcountFUNC:
checkNumOfArg (1, 1, nodes);
return checkDT (dtypeOper, NTAny, NTInt, nodes);
case gfirstFUNC:
case glastFUNC:
checkNumOfArg (1, 1, nodes);
resVT = nodes[0]->valueType();
return checkDT (dtypeOper, NTAny, NTAny, nodes);
case gexpridFUNC:
checkNumOfArg (0, 0, nodes);
return NTInt;
case gaggrFUNC:
checkNumOfArg (1, 1, nodes);
resVT = VTArray;
return checkDT (dtypeOper, NTAny, NTAny, nodes);
case ghistFUNC:
checkNumOfArg (4, 4, nodes);
if (nodes[1]->dataType() != NTInt) {
throw TableInvExpr ("2nd argument of function GHIST "
"has to be a constant integer scalar");
}
for (int i=1; i<4; ++i) {
if (nodes[i]->valueType() != VTScalar ||
! nodes[i]->isConstant()) {
throw TableInvExpr ("2nd, 3rd and 4th argument of function GHIST "
"have to be constant scalars");
}
}
resVT = VTArray;
return checkDT (dtypeOper, NTReal, NTInt, nodes);
case growidFUNC:
checkNumOfArg (0, 0, nodes);
resVT = VTArray;
return checkDT (dtypeOper, NTAny, NTInt, nodes);
case gminsFUNC:
case gmaxsFUNC:
resVT = VTArray;
CASACORE_FALLTHROUGH;
case gminFUNC:
case gmaxFUNC:
checkNumOfArg (1, 1, nodes);
return checkDT (dtypeOper, NTReal, NTReal, nodes);
case gsumsFUNC:
case gproductsFUNC:
case gsumsqrsFUNC:
resVT = VTArray;
CASACORE_FALLTHROUGH;
case gsumFUNC:
case gproductFUNC:
case gsumsqrFUNC:
checkNumOfArg (1, 1, nodes);
return checkDT (dtypeOper, NTNumeric, NTNumeric, nodes);
case gmeansFUNC:
resVT = VTArray;
CASACORE_FALLTHROUGH;
case gmeanFUNC:
checkNumOfArg (1, 1, nodes);
return checkDT (dtypeOper, NTNumeric, NTDouCom, nodes);
case gvariances0FUNC:
case gvariances1FUNC:
case gstddevs0FUNC:
case gstddevs1FUNC:
resVT = VTArray;
CASACORE_FALLTHROUGH;
case gvariance0FUNC:
case gvariance1FUNC:
case gstddev0FUNC:
case gstddev1FUNC:
checkNumOfArg (1, 1, nodes);
return checkDT (dtypeOper, NTNumeric, NTDouble, nodes);
case grmssFUNC:
resVT = VTArray;
CASACORE_FALLTHROUGH;
case grmsFUNC:
case gmedianFUNC:
checkNumOfArg (1, 1, nodes);
return checkDT (dtypeOper, NTReal, NTDouble, nodes);
case gfractileFUNC:
checkNumOfArg (2, 2, nodes);
if (nodes[1]->valueType() != VTScalar ||
! nodes[1]->isConstant()) {
throw TableInvExpr ("2nd argument of function GFRACTILE "
"has to be a constant scalar");
}
return checkDT (dtypeOper, NTReal, NTDouble, nodes);
case ganysFUNC:
case gallsFUNC:
resVT = VTArray;
CASACORE_FALLTHROUGH;
case ganyFUNC:
case gallFUNC:
checkNumOfArg (1, 1, nodes);
return checkDT (dtypeOper, NTBool, NTBool, nodes);
case gntruesFUNC:
case gnfalsesFUNC:
resVT = VTArray;
CASACORE_FALLTHROUGH;
case gntrueFUNC:
case gnfalseFUNC:
checkNumOfArg (1, 1, nodes);
return checkDT (dtypeOper, NTBool, NTInt, nodes);
default:
throw TableInvExpr ("Unhandled aggregate function " +
String::toString(ftype));
}
}
std::shared_ptr<TableExprGroupFuncBase> TableExprAggrNode::makeGroupAggrFunc()
{
// Create a new function object because each FuncSet needs its own one.
itsFunc.reset (doMakeGroupAggrFunc());
return itsFunc;
}
Bool TableExprAggrNode::isLazyAggregate() const
{
return itsFunc->isLazy();
}
TableExprGroupFuncBase* TableExprAggrNode::doMakeGroupAggrFunc()
{
if (funcType() == countallFUNC) {
return new TableExprGroupCountAll(this);
} else if (funcType() == gcountFUNC) {
return new TableExprGroupCount(this);
} else if (funcType() == gfirstFUNC) {
return new TableExprGroupFirst(this);
} else if (funcType() == glastFUNC) {
return new TableExprGroupLast(this);
} else if (funcType() == gexpridFUNC) {
return new TableExprGroupExprId(this);
} else if (funcType() == gaggrFUNC) {
return new TableExprGroupAggr(this);
} else if (funcType() == growidFUNC) {
return new TableExprGroupRowid(this);
}
if (operands()[0]->valueType() == VTScalar) {
switch (operands()[0]->dataType()) {
case NTBool:
switch (funcType()) {
case ganyFUNC:
return new TableExprGroupAny(this);
case gallFUNC:
return new TableExprGroupAll(this);
case gntrueFUNC:
return new TableExprGroupNTrue(this);
case gnfalseFUNC:
return new TableExprGroupNFalse(this);
default:
throw TableInvExpr ("Aggregate function " +
String::toString(funcType()) +
" cannot be used with a bool argument");
}
case NTInt:
switch (funcType()) {
case gminFUNC:
return new TableExprGroupMinInt(this);
case gmaxFUNC:
return new TableExprGroupMaxInt(this);
case gsumFUNC:
return new TableExprGroupSumInt(this);
case gproductFUNC:
return new TableExprGroupProductInt(this);
case gsumsqrFUNC:
return new TableExprGroupSumSqrInt(this);
default:
break;
}
// Fall through, so e.g. mean of ints can be done
CASACORE_FALLTHROUGH;
case NTDouble:
switch (funcType()) {
case gminFUNC:
return new TableExprGroupMinDouble(this);
case gmaxFUNC:
return new TableExprGroupMaxDouble(this);
case gsumFUNC:
return new TableExprGroupSumDouble(this);
case gproductFUNC:
return new TableExprGroupProductDouble(this);
case gsumsqrFUNC:
return new TableExprGroupSumSqrDouble(this);
case gmeanFUNC:
return new TableExprGroupMeanDouble(this);
case gvariance0FUNC:
return new TableExprGroupVarianceDouble(this, 0);
case gvariance1FUNC:
return new TableExprGroupVarianceDouble(this, 1);
case gstddev0FUNC:
return new TableExprGroupStdDevDouble(this, 0);
case gstddev1FUNC:
return new TableExprGroupStdDevDouble(this, 1);
case grmsFUNC:
return new TableExprGroupRmsDouble(this);
case gmedianFUNC:
return new TableExprGroupFractileDouble(this, 0.5);
case gfractileFUNC:
return new TableExprGroupFractileDouble
(this, operands()[1]->getDouble(0));
case ghistFUNC:
return new TableExprGroupHistDouble
(this, operands()[1]->getInt(0),
operands()[2]->getDouble(0),
operands()[3]->getDouble(0));
default:
throw TableInvExpr ("Aggregate function " +
String::toString(funcType()) +
" cannot be used with an integer/double"
" argument");
}
case NTComplex:
switch (funcType()) {
case gsumFUNC:
return new TableExprGroupSumDComplex(this);
case gproductFUNC:
return new TableExprGroupProductDComplex(this);
case gsumsqrFUNC:
return new TableExprGroupSumSqrDComplex(this);
case gmeanFUNC:
return new TableExprGroupMeanDComplex(this);
case gvariance0FUNC:
return new TableExprGroupVarianceDComplex(this, 0);
case gvariance1FUNC:
return new TableExprGroupVarianceDComplex(this, 1);
case gstddev0FUNC:
return new TableExprGroupStdDevDComplex(this, 0);
case gstddev1FUNC:
return new TableExprGroupStdDevDComplex(this, 1);
default:
throw TableInvExpr ("Aggregate function " +
String::toString(funcType()) +
" cannot be used with a dcomplex argument");
}
default:
break;
}
throw TableInvExpr ("Aggregate function " +
String::toString(funcType()) +
" is unknown for scalar data type " +
String::toString(operands()[0]->dataType()));
}
// The operand is an array.
switch (operands()[0]->dataType()) {
case NTBool:
switch (funcType()) {
case ganyFUNC:
return new TableExprGroupArrayAny(this);
case gallFUNC:
return new TableExprGroupArrayAll(this);
case gntrueFUNC:
return new TableExprGroupArrayNTrue(this);
case gnfalseFUNC:
return new TableExprGroupArrayNFalse(this);
default:
throw TableInvExpr ("Aggregate function " +
String::toString(funcType()) +
" cannot be used with a bool argument");
}
case NTInt:
switch (funcType()) {
case gminFUNC:
return new TableExprGroupMinArrayInt(this);
case gmaxFUNC:
return new TableExprGroupMaxArrayInt(this);
case gsumFUNC:
return new TableExprGroupSumArrayInt(this);
case gproductFUNC:
return new TableExprGroupProductArrayInt(this);
case gsumsqrFUNC:
return new TableExprGroupSumSqrArrayInt(this);
default:
break;
}
// Fall through, so e.g. mean of ints can be done
CASACORE_FALLTHROUGH;
case NTDouble:
switch (funcType()) {
case gminFUNC:
return new TableExprGroupMinArrayDouble(this);
case gmaxFUNC:
return new TableExprGroupMaxArrayDouble(this);
case gsumFUNC:
return new TableExprGroupSumArrayDouble(this);
case gproductFUNC:
return new TableExprGroupProductArrayDouble(this);
case gsumsqrFUNC:
return new TableExprGroupSumSqrArrayDouble(this);
case gmeanFUNC:
return new TableExprGroupMeanArrayDouble(this);
case gvariance0FUNC:
return new TableExprGroupVarianceArrayDouble(this, 0);
case gvariance1FUNC:
return new TableExprGroupVarianceArrayDouble(this, 1);
case gstddev0FUNC:
return new TableExprGroupStdDevArrayDouble(this, 0);
case gstddev1FUNC:
return new TableExprGroupStdDevArrayDouble(this, 1);
case grmsFUNC:
return new TableExprGroupRmsArrayDouble(this);
case gmedianFUNC:
return new TableExprGroupFractileArrayDouble(this, 0.5);
case gfractileFUNC:
return new TableExprGroupFractileArrayDouble
(this, operands()[1]->getDouble(0));
default:
throw TableInvExpr ("Aggregate function " +
String::toString(funcType()) +
" cannot be used with an integer/double argument");
}
case NTComplex:
switch (funcType()) {
case gsumFUNC:
return new TableExprGroupSumArrayDComplex(this);
case gproductFUNC:
return new TableExprGroupProductArrayDComplex(this);
case gsumsqrFUNC:
return new TableExprGroupSumSqrArrayDComplex(this);
case gmeanFUNC:
return new TableExprGroupMeanArrayDComplex(this);
case gvariance0FUNC:
return new TableExprGroupVarianceArrayDComplex(this, 0);
case gvariance1FUNC:
return new TableExprGroupVarianceArrayDComplex(this, 1);
case gstddev0FUNC:
return new TableExprGroupStdDevArrayDComplex(this, 0);
case gstddev1FUNC:
return new TableExprGroupStdDevArrayDComplex(this, 1);
default:
throw TableInvExpr ("Aggregate function " +
String::toString(funcType()) +
" cannot be used with a dcomplex argument");
}
default:
break;
}
throw TableInvExpr ("Aggregate function " +
String::toString(funcType()) +
" is unknown for array data type " +
String::toString(operands()[0]->dataType()));
}
Bool TableExprAggrNode::getBool (const TableExprId& id)
{
const TableExprIdAggr& aid = TableExprIdAggr::cast (id);
if (itsFunc->isLazy()) {
return itsFunc->getBool (aid.result().ids(id.rownr()));
}
TableExprGroupFuncSet& set = aid.result().funcSet(id.rownr());
return set.getFuncs()[itsFunc->seqnr()]->getBool();
}
Int64 TableExprAggrNode::getInt (const TableExprId& id)
{
const TableExprIdAggr& aid = TableExprIdAggr::cast (id);
if (itsFunc->isLazy()) {
return itsFunc->getInt (aid.result().ids(id.rownr()));
}
TableExprGroupFuncSet& set = aid.result().funcSet(id.rownr());
return set.getFuncs()[itsFunc->seqnr()]->getInt();
}
Double TableExprAggrNode::getDouble (const TableExprId& id)
{
if (dataType() != NTDouble) {
return TableExprNodeRep::getDouble (id);
}
const TableExprIdAggr& aid = TableExprIdAggr::cast (id);
if (itsFunc->isLazy()) {
return itsFunc->getDouble (aid.result().ids(id.rownr()));
}
TableExprGroupFuncSet& set = aid.result().funcSet(id.rownr());
return set.getFuncs()[itsFunc->seqnr()]->getDouble();
}
DComplex TableExprAggrNode::getDComplex (const TableExprId& id)
{
if (dataType() != NTComplex) {
return TableExprNodeRep::getDComplex (id);
}
const TableExprIdAggr& aid = TableExprIdAggr::cast (id);
if (itsFunc->isLazy()) {
return itsFunc->getDComplex (aid.result().ids(id.rownr()));
}
TableExprGroupFuncSet& set = aid.result().funcSet(id.rownr());
return set.getFuncs()[itsFunc->seqnr()]->getDComplex();
}
String TableExprAggrNode::getString (const TableExprId& id)
{
const TableExprIdAggr& aid = TableExprIdAggr::cast (id);
if (itsFunc->isLazy()) {
return itsFunc->getString (aid.result().ids(id.rownr()));
}
TableExprGroupFuncSet& set = aid.result().funcSet(id.rownr());
return set.getFuncs()[itsFunc->seqnr()]->getString();
}
MVTime TableExprAggrNode::getDate (const TableExprId& id)
{
const TableExprIdAggr& aid = TableExprIdAggr::cast (id);
if (itsFunc->isLazy()) {
return itsFunc->getDate (aid.result().ids(id.rownr()));
}
TableExprGroupFuncSet& set = aid.result().funcSet(id.rownr());
return set.getFuncs()[itsFunc->seqnr()]->getDate();
}
} //# NAMESPACE CASACORE - END
|