Abstract base class for a user-defined TaQL function
Synopsis
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.
makeObject | a static function to create an object of the UDF class. This function needs to be registered. |
setup | 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 operands() . The setup function should perform the following:
-
Define the data type of the result using
setDataType<src>. The data type should be derived from the data types of the function arguments. The possible data types are defined in class TableExprNodeRep. Note that a UDF can support multiple data types. For example, a function like <src>min can be used for Int, Double, or a mix. Function 'checkDT' in class TableExprNodeMulti can be used to check the data types of the operands and determine the result data type.
-
Define if the function is an aggregate function calculating an aggregated value in a group (e.g., minimum or mean).
setAggregate can be used to tell so.
-
Define the dimensionality of the result using
setNDim . A value of 0 means a scalar. A value of -1 means an array with a dimensionality that can vary from row to row.
-
Optionally use
setShape to define the shape if the results are arrays with a shape that is the same for all rows. It will also set ndim if setNDim was not used yet, otherwise it checks if it ndim matches.
-
Optionally set the unit of the result using
setUnit . TaQL has full support of units, so UDFs should behave the same. It is possible to change the unit of the function arguments. For example:
-
a function like 'sin' can force its argument to be in radians; TaQL will scale the argument as needed. This can be done like
TableExprNodeUnit::adaptUnit (operands()[i], "rad");
-
A function like 'asin' will have a result in radians. Such a UDF should set its result unit to rad.
-
A function like 'min' wants its arguments to have the same unit and will set its result unit to it. It can be done like:
setUnit (TableExprFuncNode::makeEqualUnits (operands(), 0, operands().size()));
See class TableExprFuncNode for more info about these functions.
-
Optionally define attributes as a Record object. They can be used by UDFs to tell something more about the type of value.
-
Optionally define if the result is a constant value using
setConstant . It means that the function is not dependent on the row number in the table being queried. This is usually the case if all UDF arguments are constant.
|
getXXX | 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 get functions have an argument TableExprId defining the table row (or record) for which the function has to be evaluated. If the UDF is an aggregate functions the TableExprId has to be upcasted to an TableExprIdAggr object from which all TableExprId objects in an aggregation group can be retrieved.
const vector<TableExprId>& ids = aid.result().ids(id.rownr());
static const TableExprIdAggr & cast(const TableExprId &id) Cast a TableExprId object to TableExprIdAggr.
|
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 UDF name used in TaQL consists of two parts: a library name and a function name separated by a dot. Both parts need to be given. Note that the library name can also be seen as a UDF scope, so different UDFs with equal names can be used from different libraries. A UDF should be registered with this full name.
The "USING STYLE" clause can be used to define a synonym for a (long) library name in the TaQLStyle object. The library part of the UDF will always be looked up in this synonym map.
-
If a UDF is not found in the registry, it will be tried to load a shared library using the library name part. The libraries tried to be loaded are lib<library>.so and libcasa_<library>.so. On Mac.dylib will be tried. If loaded successfully, a special function 'register_libname' will be called first. It should register each UDF in the shared library using UDFBase::register.
Example
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.
{
public:
TestUDF() {}
static UDFBase* makeObject (
const String&)
{ return new TestUDF(); }
virtual void setup (
const Table&,
const TaQLStyle&)
{
AipsError);
AipsError);
}
{
return operands()[0]->getInt(
id) == 1; }
};
#define AlwaysAssert(expr, exception)
These marcos are provided for use instead of simply using the constructors of assert_ to allow additi...
std::vector< TENShPtr > & operands()
Get the operands.
void setDataType(TableExprNodeRep::NodeDataType)
Set the data type.
TableExprNodeRep::NodeDataType dataType() const
Get the data type.
UDFBase()
Only default constructor is needed.
virtual Bool getBool(const TableExprId &id)
Evaluate the function and return the result.
void setConstant(Bool isConstant)
Define if the result is constant (e.g.
void setNDim(Int ndim)
Set the dimensionality of the results.
Bool isConstant() const
Tell if the UDF gives a constant result.
virtual void setup(const Table &table, const TaQLStyle &)=0
Set up the function object.
bool Bool
Define the standard types used by Casacore.
Example
The following example shows an aggregate UDF function. It calculates the sum of the cubes of the values in a group.
{
public:
TestUDFAggr() {}
static UDFBase* makeObject (
const String&) {
return new TestUDFAggr(); }
virtual void setup (
const Table&,
const TaQLStyle&)
{
}
{
const vector<TableExprId>& ids = aid.result().ids(id.rownr());
for (vector<TableExprId>::const_iterator it=ids.begin();
it!=ids.end(); ++it){
sum3 += v*v*v;
}
return sum3;
}
};
virtual Int64 getInt(const TableExprId &id)
void setAggregate(Bool isAggregate)
Define if the UDF is an aggregate function (usually used in GROUPBY).
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size)
More examples of UDF functions can be found in classes UDFMSCal and DirectionUDF.
Definition at line 235 of file UDFBase.h.