casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
casacore::UDFBase Class Referenceabstract

Abstract base class for a user-defined TaQL function. More...

#include <UDFBase.h>

Inheritance diagram for casacore::UDFBase:
casacore::DirectionUDF casacore::DopplerUDF casacore::EarthMagneticUDF casacore::EpochUDF casacore::FrequencyUDF casacore::HelpMeasUDF casacore::HelpMsCalUDF casacore::PositionUDF casacore::RadialVelocityUDF casacore::UDFMSCal

Public Types

typedef UDFBaseMakeUDFObject (const String &functionName)
 The signature of a global or static member function creating an object of the UDF. More...
 

Public Member Functions

 UDFBase ()
 Only default constructor is needed. More...
 
virtual ~UDFBase ()
 Destructor. More...
 
virtual Bool getBool (const TableExprId &id)
 Evaluate the function and return the result. More...
 
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< BoolgetArrayBool (const TableExprId &id)
 
virtual MArray< Int64getArrayInt (const TableExprId &id)
 
virtual MArray< DoublegetArrayDouble (const TableExprId &id)
 
virtual MArray< DComplex > getArrayDComplex (const TableExprId &id)
 
virtual MArray< StringgetArrayString (const TableExprId &id)
 
virtual MArray< MVTimegetArrayDate (const TableExprId &id)
 
const StringgetUnit () const
 Get the unit. More...
 
const RecordgetAttributes () const
 Get the attributes. More...
 
void getAggrNodes (vector< TableExprNodeRep * > &aggr)
 Get the nodes in the function operands representing an aggregate function. More...
 
void getColumnNodes (vector< TableExprNodeRep * > &cols)
 Get the nodes in the function operands representing a table column. More...
 
void init (const std::vector< TENShPtr > &arg, const Table &table, const TaQLStyle &)
 Initialize the function object. More...
 
TableExprNodeRep::NodeDataType dataType () const
 Get the data type. More...
 
Int ndim () const
 Get the dimensionality of the results. More...
 
const IPositionshape () const
 Get the result shape if the same for all results. More...
 
Bool isConstant () const
 Tell if the UDF gives a constant result. More...
 
Bool isAggregate () const
 Tell if the UDF is an aggregate function. More...
 
void disableApplySelection ()
 Do not apply the selection. More...
 
void applySelection (const Vector< uInt > &rownrs)
 If needed, let the UDF re-create column objects for a selection of rows. More...
 

Static Public Member Functions

static void registerUDF (const String &name, MakeUDFObject *func)
 Register the name and construction function of a UDF (thread-safe). More...
 
static UDFBasecreateUDF (const String &name, const TaQLStyle &style)
 Create a UDF object (thread-safe). More...
 

Protected Member Functions

std::vector< TENShPtr > & operands ()
 Get the operands. More...
 
void setDataType (TableExprNodeRep::NodeDataType)
 Set the data type. More...
 
void setNDim (Int ndim)
 Set the dimensionality of the results. More...
 
void setShape (const IPosition &shape)
 Set the shape of the results if it is fixed and known. More...
 
void setUnit (const String &unit)
 Set the unit of the result. More...
 
void setAttributes (const Record &attributes)
 Set the attributes of the result. More...
 
void setConstant (Bool isConstant)
 Define if the result is constant (e.g. More...
 
void setAggregate (Bool isAggregate)
 Define if the UDF is an aggregate function (usually used in GROUPBY). More...
 
virtual void recreateColumnObjects (const Vector< uInt > &rownrs)
 Let a derived class recreate its column objects in case a selection has to be applied. More...
 

Private Member Functions

virtual void setup (const Table &table, const TaQLStyle &)=0
 Set up the function object. More...
 

Private Attributes

std::vector< TENShPtritsOperands
 
TableExprNodeRep::NodeDataType itsDataType
 
Int itsNDim
 
IPosition itsShape
 
String itsUnit
 
Record itsAttributes
 
Bool itsIsConstant
 
Bool itsIsAggregate
 
Bool itsApplySelection
 

Static Private Attributes

static map< String,
MakeUDFObject * > 
theirRegistry
 
static Mutex theirMutex
 

Detailed Description

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: 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 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:

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.

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);
AipsError);
AipsError);
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; }
};

Example

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]->valueType() == TableExprNodeRep::VTScalar, AipsError);
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;
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 236 of file UDFBase.h.

Member Typedef Documentation

typedef UDFBase* casacore::UDFBase::MakeUDFObject(const String &functionName)

The signature of a global or static member function creating an object of the UDF.

Definition at line 241 of file UDFBase.h.

Constructor & Destructor Documentation

casacore::UDFBase::UDFBase ( )

Only default constructor is needed.

virtual casacore::UDFBase::~UDFBase ( )
virtual

Destructor.

Member Function Documentation

void casacore::UDFBase::applySelection ( const Vector< uInt > &  rownrs)

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

Get the data type.

Definition at line 339 of file UDFBase.h.

References itsDataType.

void casacore::UDFBase::disableApplySelection ( )
inline

Do not apply the selection.

Definition at line 360 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

Reimplemented in casacore::DirectionUDF.

virtual MArray<DComplex> casacore::UDFBase::getArrayDComplex ( const TableExprId id)
virtual

Reimplemented in casacore::UDFMSCal.

virtual MArray<Double> casacore::UDFBase::getArrayDouble ( const TableExprId id)
virtual
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.

const Record& casacore::UDFBase::getAttributes ( ) const
inline

Get the attributes.

Definition at line 272 of file UDFBase.h.

References itsAttributes.

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
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
const String& casacore::UDFBase::getUnit ( ) const
inline

Get the unit.

Definition at line 268 of file UDFBase.h.

References itsUnit.

void casacore::UDFBase::init ( const std::vector< TENShPtr > &  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 356 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 352 of file UDFBase.h.

References itsIsConstant.

Int casacore::UDFBase::ndim ( ) const
inline

Get the dimensionality of the results.

(0=scalar, -1=array with variable ndim, >0=array with fixed ndim

Definition at line 344 of file UDFBase.h.

References itsNDim.

std::vector<TENShPtr>& casacore::UDFBase::operands ( )
inlineprotected

Get the operands.

Definition at line 288 of file UDFBase.h.

References itsOperands.

virtual void casacore::UDFBase::recreateColumnObjects ( const Vector< uInt > &  rownrs)
protectedvirtual

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::setAttributes ( const Record attributes)
protected

Set the attributes of the result.

If this function is not called by the setup function of the derived class, the result has no attributes.

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  
)
privatepure virtual
const IPosition& casacore::UDFBase::shape ( ) const
inline

Get the result shape if the same for all results.

Definition at line 348 of file UDFBase.h.

References itsShape.

Member Data Documentation

Bool casacore::UDFBase::itsApplySelection
private

Definition at line 382 of file UDFBase.h.

Referenced by disableApplySelection().

Record casacore::UDFBase::itsAttributes
private

Definition at line 379 of file UDFBase.h.

Referenced by getAttributes().

TableExprNodeRep::NodeDataType casacore::UDFBase::itsDataType
private

Definition at line 375 of file UDFBase.h.

Referenced by dataType().

Bool casacore::UDFBase::itsIsAggregate
private

Definition at line 381 of file UDFBase.h.

Referenced by isAggregate().

Bool casacore::UDFBase::itsIsConstant
private

Definition at line 380 of file UDFBase.h.

Referenced by isConstant().

Int casacore::UDFBase::itsNDim
private

Definition at line 376 of file UDFBase.h.

Referenced by ndim().

std::vector<TENShPtr> casacore::UDFBase::itsOperands
private

Definition at line 374 of file UDFBase.h.

Referenced by operands().

IPosition casacore::UDFBase::itsShape
private

Definition at line 377 of file UDFBase.h.

Referenced by shape().

String casacore::UDFBase::itsUnit
private

Definition at line 378 of file UDFBase.h.

Referenced by getUnit().

Mutex casacore::UDFBase::theirMutex
staticprivate

Definition at line 389 of file UDFBase.h.

map<String, MakeUDFObject*> casacore::UDFBase::theirRegistry
staticprivate

Definition at line 388 of file UDFBase.h.


The documentation for this class was generated from the following file: