casacore::UDFBase Class Reference

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

#include <UDFBase.h>

Inheritance diagram for casacore::UDFBase:
casacore::HelpMsCalUDF casacore::UDFMSCal

List of all members.

Public Types

typedef UDFBaseMakeUDFObject (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< 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.
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 IPositionshape () 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 UDFBasecreateUDF (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

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:
    • 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 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);
 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; }
 };

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]->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.


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 238 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 327 of file UDFBase.h.

References itsDataType.

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

Get the unit.

Definition at line 265 of file UDFBase.h.

References itsUnit.

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]

Get the dimensionality of the results.

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

Definition at line 332 of file UDFBase.h.

References itsNDim.

PtrBlock<TableExprNodeRep*>& casacore::UDFBase::operands (  )  [inline, protected]

Get the operands.

Definition at line 281 of file UDFBase.h.

References itsOperands.

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]

Get the result shape if the same for all results.

Definition at line 336 of file UDFBase.h.

References itsShape.


Member Data Documentation

Definition at line 369 of file UDFBase.h.

Referenced by disableApplySelection().

Definition at line 363 of file UDFBase.h.

Referenced by dataType().

Definition at line 368 of file UDFBase.h.

Referenced by isAggregate().

Definition at line 367 of file UDFBase.h.

Referenced by isConstant().

Definition at line 364 of file UDFBase.h.

Referenced by ndim().

Definition at line 362 of file UDFBase.h.

Referenced by operands().

Definition at line 365 of file UDFBase.h.

Referenced by shape().

Definition at line 366 of file UDFBase.h.

Referenced by getUnit().

Definition at line 376 of file UDFBase.h.

Definition at line 375 of file UDFBase.h.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1