To define a non-pushdown function, a Java function must be provided that matches the metadata supplied either in the Designer or Dynamic VDB defined metadata. User Defined Function (or UDF) and User Defined Aggregate Function (or UDAF) may be called at runtime just like any other function or aggregate function respectively.
A user defined function created on any VDB on view model by creating a Function just as a base table. You would require all the information defined on User Defined Functions to create a UDF. Make sure you provide the JAVA code implementation details in the properties dialog for the UDF.
When defining the metadata using DDL in the Dynamic VDBs, user can define a UDF or UDAF (User Defined Aggregate Function) as shown below.
You must create a Java method that contains the function’s logic. This Java method should accept the necessary arguments, which the Teiid System will pass to it at runtime, and function should return the calculated or altered value.
See DDL Metadata for all possible options related to functions defined via DDL.
The number of input arguments and types must match the function metadata defined in Designer or on Dynamic VDB metadata.
Code Requirements For UDFs
- The java class containing the function method must be defined public.
|One implementation class can contain more than one UDF implementation methods.|
- The function method must be public and static.
Code Requirements For UDAFs
- The java class containing the function method must be defined public and extend org.teiid.UserDefinedAggregate
- The function method must be public.
- Any exception can be thrown, but Teiid will rethrow the exception as a FunctionExecutionException .
- You may optionally add an additional org.teiid.CommandContext argument as the first parameter. The CommandContext interface provides access to information about the current command, such as the executing user, Subject, the vdb, the session id, etc. This CommandContext parameter should not be declared in the function metadata.
The corresponding UDF would be declared as Timestamp sessionCreated().
- After coding the functions you should compile the Java code into a Java Archive (JAR) file.
The JAR file may be placed in your VDB under the "/lib" directory. It will automatically be used for the VDB classloader classpath when deployed.
Create a JBoss AS module with the JAR file under <jboss-as>/modules directory and define the module on the -vdb.xml file as shown below example
The lib property value may contain a space delimited list of module names if more than one dependency is needed.