Abstract base class for a user-defined TaQL function. More...
#include <UDFBase.h>
Public Types | |
typedef UDFBase * | MakeUDFObject (const String &functionName) |
The signature of a global or static member function creating an object of the UDF. | |
Public Member Functions | |
UDFBase () | |
Only default constructor is needed. | |
virtual | ~UDFBase () |
Destructor. | |
virtual Bool | getBool (const TableExprId &id) |
Evaluate the function and return the result. | |
virtual Int64 | getInt (const TableExprId &id) |
virtual Double | getDouble (const TableExprId &id) |
virtual DComplex | getDComplex (const TableExprId &id) |
virtual String | getString (const TableExprId &id) |
virtual TaqlRegex | getRegex (const TableExprId &id) |
virtual MVTime | getDate (const TableExprId &id) |
virtual MArray< Bool > | getArrayBool (const TableExprId &id) |
virtual MArray< Int64 > | getArrayInt (const TableExprId &id) |
virtual MArray< Double > | getArrayDouble (const TableExprId &id) |
virtual MArray< DComplex > | getArrayDComplex (const TableExprId &id) |
virtual MArray< String > | getArrayString (const TableExprId &id) |
virtual MArray< MVTime > | getArrayDate (const TableExprId &id) |
const String & | getUnit () const |
Get the unit. | |
void | getAggrNodes (vector< TableExprNodeRep * > &aggr) |
Get the nodes in the function operands representing an aggregate function. | |
void | getColumnNodes (vector< TableExprNodeRep * > &cols) |
Get the nodes in the function operands representing a table column. | |
void | init (const PtrBlock< TableExprNodeRep * > &arg, const Table &table, const TaQLStyle &) |
Initialize the function object. | |
TableExprNodeRep::NodeDataType | dataType () const |
Get the data type. | |
Int | ndim () const |
Get the dimensionality of the results. | |
const IPosition & | shape () const |
Get the result shape if the same for all results. | |
Bool | isConstant () const |
Tell if the UDF gives a constant result. | |
Bool | isAggregate () const |
Tell if the UDF is an aggregate function. | |
void | disableApplySelection () |
Do not apply the selection. | |
void | applySelection (const Vector< uInt > &rownrs) |
If needed, let the UDF re-create column objects for a selection of rows. | |
Static Public Member Functions | |
static void | registerUDF (const String &name, MakeUDFObject *func) |
Register the name and construction function of a UDF (thread-safe). | |
static UDFBase * | createUDF (const String &name, const TaQLStyle &style) |
Create a UDF object (thread-safe). | |
Protected Member Functions | |
PtrBlock< TableExprNodeRep * > & | operands () |
Get the operands. | |
void | setDataType (TableExprNodeRep::NodeDataType) |
Set the data type. | |
void | setNDim (Int ndim) |
Set the dimensionality of the results. | |
void | setShape (const IPosition &shape) |
Set the shape of the results if it is fixed and known. | |
void | setUnit (const String &unit) |
Set the unit of the result. | |
void | setConstant (Bool isConstant) |
Define if the result is constant (e.g. | |
void | setAggregate (Bool isAggregate) |
Define if the UDF is an aggregate function (usually used in GROUPBY). | |
virtual void | recreateColumnObjects (const Vector< uInt > &rownrs) |
Let a derived class recreate its column objects in case a selection has to be applied. | |
Private Member Functions | |
virtual void | setup (const Table &table, const TaQLStyle &)=0 |
Set up the function object. | |
Private Attributes | |
PtrBlock< TableExprNodeRep * > | itsOperands |
TableExprNodeRep::NodeDataType | itsDataType |
Int | itsNDim |
IPosition | itsShape |
String | itsUnit |
Bool | itsIsConstant |
Bool | itsIsAggregate |
Bool | itsApplySelection |
Static Private Attributes | |
static map< String, MakeUDFObject * > | theirRegistry |
static Mutex | theirMutex |
Abstract base class for a user-defined TaQL function.
This class makes it possible to add user-defined functions (UDF) to TaQL. A UDF has to be implemented in a class derived from this class and can contain one or more user-defined functions.
A few functions have to be implemented in the class as described below. In this way TaQL can be extended with arbitrary functions, which can be normal functions as well as aggregate functions (often used with GROUPBY).
A UDF is a class derived from this base class. It must contain the following member functions. See also the example below.
| a static function to create an object of the UDF class. This function needs to be registered. |
| this virtual function is called after the object has been created. It should initialize the object using the function arguments that can be obtained using the function
|
| these are virtual get functions for each possible data type. The get functions matching the data types set by the setup function need to be implemented. The const TableExprIdAggr& aid = TableExprIdAggr::cast (id); const vector<TableExprId>& ids = aid.result().ids(id.rownr()); |
A UDF has to be made known to TaQL by adding it to the UDF registry with its name and 'makeObject' function. UDFs will usually reside in a shared library that is loaded dynamically. TaQL will load a UDF in the following way:
The following examples show a normal UDF function.
It returns True if the function argument matches 1. It can be seen that it checks if the argument is an integer scalar.
class TestUDF: public UDFBase { public: TestUDF() {} // Registered function to create the UDF object. // The name of the function is not important here. static UDFBase* makeObject (const String&) { return new TestUDF(); } // Setup and check the details; result is a bool scalar value. virtual void setup (const Table&, const TaQLStyle&) { AlwaysAssert (operands().size() == 1, AipsError); AlwaysAssert (operands()[0]->dataType() == TableExprNodeRep::NTInt, AipsError); AlwaysAssert (operands()[0]->valueType() == TableExprNodeRep::VTScalar, AipsError); setDataType (TableExprNodeRep::NTBool); setNDim (0); // scalar result setConstant (operands()[0].isConstant()); // constant result? } // Get the value for the given id. // It gets the value of the operand and checks if it is 1. Bool getBool (const TableExprId& id) { return operands()[0]->getInt(id) == 1; } };
The following example shows an aggregate UDF function. It calculates the sum of the cubes of the values in a group.
class TestUDFAggr: public UDFBase { public: TestUDFAggr() {} // Registered function to create the UDF object. // The name of the function is not important here. static UDFBase* makeObject (const String&) { return new TestUDFAggr(); } // Setup and check the details; result is an integer scalar value. // It aggregates the values of multiple rows. virtual void setup (const Table&, const TaQLStyle&) { AlwaysAssert (operands().size() == 1, AipsError); AlwaysAssert (operands()[0]->dataType() == TableExprNodeRep::NTInt, AipsError); AlwaysAssert (operands()[0]->valueType() == TableExprNodeRep::VTScalar, AipsError); setDataType (TableExprNodeRep::NTInt); setNDim (0); // scalar setAggregate (True); // aggregate function } // Get the value of a group. // It aggregates the values of multiple rows. Int64 getInt (const TableExprId& id) { // Cast the id to a TableExprIdAggr object. const TableExprIdAggr& aid = TableExprIdAggr::cast (id); // Get the vector of ids for this group. const vector<TableExprId>& ids = aid.result().ids(id.rownr()); // Get the values for all ids and accumulate them. Int64 sum3 = 0; for (vector<TableExprId>::const_iterator it=ids.begin(); it!=ids.end(); ++it){ Int64 v = operands()[0]->getInt(*it); sum3 += v*v*v; } return sum3; } };
More examples of UDF functions can be found in classes UDFMSCal and DirectionUDF.
Definition at line 233 of file UDFBase.h.
typedef UDFBase* casacore::UDFBase::MakeUDFObject(const String &functionName) |
casacore::UDFBase::UDFBase | ( | ) |
Only default constructor is needed.
virtual casacore::UDFBase::~UDFBase | ( | ) | [virtual] |
Destructor.
If needed, let the UDF re-create column objects for a selection of rows.
It calls the function recreateColumnObjects.
static UDFBase* casacore::UDFBase::createUDF | ( | const String & | name, | |
const TaQLStyle & | style | |||
) | [static] |
Create a UDF object (thread-safe).
It looks in the map with fixed function names. If unknown, it looks if a wildcarded function name is supported (for PyTaQL).
TableExprNodeRep::NodeDataType casacore::UDFBase::dataType | ( | ) | const [inline] |
void casacore::UDFBase::disableApplySelection | ( | ) | [inline] |
Do not apply the selection.
Definition at line 348 of file UDFBase.h.
References casacore::False, and itsApplySelection.
void casacore::UDFBase::getAggrNodes | ( | vector< TableExprNodeRep * > & | aggr | ) |
Get the nodes in the function operands representing an aggregate function.
virtual MArray<Bool> casacore::UDFBase::getArrayBool | ( | const TableExprId & | id | ) | [virtual] |
Reimplemented in casacore::UDFMSCal.
virtual MArray<MVTime> casacore::UDFBase::getArrayDate | ( | const TableExprId & | id | ) | [virtual] |
virtual MArray<DComplex> casacore::UDFBase::getArrayDComplex | ( | const TableExprId & | id | ) | [virtual] |
Reimplemented in casacore::UDFMSCal.
virtual MArray<Double> casacore::UDFBase::getArrayDouble | ( | const TableExprId & | id | ) | [virtual] |
Reimplemented in casacore::UDFMSCal.
virtual MArray<Int64> casacore::UDFBase::getArrayInt | ( | const TableExprId & | id | ) | [virtual] |
Reimplemented in casacore::UDFMSCal.
virtual MArray<String> casacore::UDFBase::getArrayString | ( | const TableExprId & | id | ) | [virtual] |
Reimplemented in casacore::UDFMSCal.
virtual Bool casacore::UDFBase::getBool | ( | const TableExprId & | id | ) | [virtual] |
Evaluate the function and return the result.
Their default implementations throw a "not implemented" exception.
Reimplemented in casacore::UDFMSCal.
void casacore::UDFBase::getColumnNodes | ( | vector< TableExprNodeRep * > & | cols | ) |
Get the nodes in the function operands representing a table column.
virtual MVTime casacore::UDFBase::getDate | ( | const TableExprId & | id | ) | [virtual] |
virtual DComplex casacore::UDFBase::getDComplex | ( | const TableExprId & | id | ) | [virtual] |
Reimplemented in casacore::UDFMSCal.
virtual Double casacore::UDFBase::getDouble | ( | const TableExprId & | id | ) | [virtual] |
Reimplemented in casacore::UDFMSCal.
virtual Int64 casacore::UDFBase::getInt | ( | const TableExprId & | id | ) | [virtual] |
Reimplemented in casacore::UDFMSCal.
virtual TaqlRegex casacore::UDFBase::getRegex | ( | const TableExprId & | id | ) | [virtual] |
virtual String casacore::UDFBase::getString | ( | const TableExprId & | id | ) | [virtual] |
Reimplemented in casacore::HelpMsCalUDF, and casacore::UDFMSCal.
const String& casacore::UDFBase::getUnit | ( | ) | const [inline] |
void casacore::UDFBase::init | ( | const PtrBlock< TableExprNodeRep * > & | arg, | |
const Table & | table, | |||
const TaQLStyle & | ||||
) |
Initialize the function object.
Bool casacore::UDFBase::isAggregate | ( | ) | const [inline] |
Tell if the UDF is an aggregate function.
Definition at line 344 of file UDFBase.h.
References itsIsAggregate.
Referenced by casacore::TableExprUDFNode::isAggregate().
Bool casacore::UDFBase::isConstant | ( | ) | const [inline] |
Tell if the UDF gives a constant result.
Definition at line 340 of file UDFBase.h.
References itsIsConstant.
Int casacore::UDFBase::ndim | ( | ) | const [inline] |
PtrBlock<TableExprNodeRep*>& casacore::UDFBase::operands | ( | ) | [inline, protected] |
virtual void casacore::UDFBase::recreateColumnObjects | ( | const Vector< uInt > & | rownrs | ) | [protected, virtual] |
Let a derived class recreate its column objects in case a selection has to be applied.
The default implementation does nothing.
Reimplemented in casacore::UDFMSCal.
static void casacore::UDFBase::registerUDF | ( | const String & | name, | |
MakeUDFObject * | func | |||
) | [static] |
Register the name and construction function of a UDF (thread-safe).
An exception is thrown if this name already exists with a different construction function.
void casacore::UDFBase::setAggregate | ( | Bool | isAggregate | ) | [protected] |
Define if the UDF is an aggregate function (usually used in GROUPBY).
void casacore::UDFBase::setConstant | ( | Bool | isConstant | ) | [protected] |
Define if the result is constant (e.g.
if all arguments are constant). If this function is not called by the setup function of the derived class, the result is not constant.
void casacore::UDFBase::setDataType | ( | TableExprNodeRep::NodeDataType | ) | [protected] |
Set the data type.
This function must be called by the setup function of the derived class.
void casacore::UDFBase::setNDim | ( | Int | ndim | ) | [protected] |
Set the dimensionality of the results.
0 means that the results are scalars.
-1 means that the results are arrays with unknown dimensionality.
>0 means that the results are arrays with that dimensionality. This function must be called by the setup function of the derived class.
void casacore::UDFBase::setShape | ( | const IPosition & | shape | ) | [protected] |
Set the shape of the results if it is fixed and known.
void casacore::UDFBase::setUnit | ( | const String & | unit | ) | [protected] |
Set the unit of the result.
If this function is not called by the setup function of the derived class, the result has no unit.
virtual void casacore::UDFBase::setup | ( | const Table & | table, | |
const TaQLStyle & | ||||
) | [private, pure virtual] |
Set up the function object.
Implemented in casacore::HelpMsCalUDF, and casacore::UDFMSCal.
const IPosition& casacore::UDFBase::shape | ( | ) | const [inline] |
Bool casacore::UDFBase::itsApplySelection [private] |
Definition at line 369 of file UDFBase.h.
Referenced by disableApplySelection().
Definition at line 363 of file UDFBase.h.
Referenced by dataType().
Bool casacore::UDFBase::itsIsAggregate [private] |
Definition at line 368 of file UDFBase.h.
Referenced by isAggregate().
Bool casacore::UDFBase::itsIsConstant [private] |
Definition at line 367 of file UDFBase.h.
Referenced by isConstant().
Int casacore::UDFBase::itsNDim [private] |
PtrBlock<TableExprNodeRep*> casacore::UDFBase::itsOperands [private] |
Definition at line 362 of file UDFBase.h.
Referenced by operands().
IPosition casacore::UDFBase::itsShape [private] |
String casacore::UDFBase::itsUnit [private] |
Mutex casacore::UDFBase::theirMutex [static, private] |
map<String, MakeUDFObject*> casacore::UDFBase::theirRegistry [static, private] |