Developing a Table UDF

The general steps for developing a table UDF include determining input and output, declaring the v4 library, defining the a_v4_extfn_proc descriptor, defining a library entry point function, defining how the server gets row information, implementing the a_v4_extfn_proc structure functions, and implement the a_v4_extfn_table_func structure functions.

  1. Determine the input and output for the table UDF.

    The input is defined by the parameters the procedure accepts, and the output is defined by how the RESULT clause for the procedure is declared. The declaration of the table UDF in SQL is separate from the implementation of the table UDF. This means that a particular implementation of a table UDF may be bound to a specific declaration. When developing a table UDF, ensure that the implementation and declaration match.

  2. Declare the library as a v4 Library.

    For SAP Sybase IQ to recognize the library as a v4 library, the library must include the extfnapiv4.h header file located in the subdirectory of your SAP Sybase IQ installation directory.

    This header defines the v4 API features and functions and is a superset of the v3 API; extfnapiv4.h includes extfnapiv3.h.

    To create table UDF or TPFs, the library must provide the extfn_use_new_api() entry point. For v4 libraries, extfn_use_new_api() must return EXTFN_V4_API.

  3. Define the a_v4_extfn_proc descriptor.

    When developing a v4 table UDF or TPF, the library must declare what functions are available for the server to call.

    Create a variable of type a_v4_extfn_proc and set each member of this structure to the address of the function within the table UDF that implements the function. The information in this variable is made available to the server via a library entry pointer. Not all members of a_v4_extfn_proc are required and there are two reserved fields which you must set to NULL.

    Use this descriptor function as a model when developing your own function:

    static a_v4_extfn_proc udf_proc_descriptor =
    {
    	udf_proc_start,      // optional
    	udf_proc_finish,     // optional
    	udf_proc_evaluate,   // required
    	udf_proc_describe,   // required
    	udf_proc_enter_state,// optional
    	udf_proc_leave_state,// optional
    	NULL,                // Reserved: must be NULL
    	NULL                 // Reserved: must be NULL
    };
  4. Define a library entry-point function.

    The table UDF library must provide a function entry point that returns an a_v4_extfn_proc descriptor pointer. This is the same descriptor as described in step 3.

    This callback function is the main required entry point for the library.

    Use this function as a model when developing your own library entry point:

    extern "C"
    a_v4_extfn_proc * SQL_CALLBACK udf_rg_proc()
    /******************************************/
    {
    	return &udf_proc_descriptor;
    }
  5. Define how the server gets row information from the table UDF.

    When developing a v4 table UDF or TPF, the library must declare how row information is transferred to the server.

    Create a variable of type a_v4_extfn_table_func and set each member of this structure to the address of the function within the table UDF that implements the function. The information in this variable is made available to the server at runtime.

    Not all members of a_v4_extfn_table_func are required and there are two reserved fields which must be set to NULL.

    Use this descriptor as a model when developing your own table UDF:

    {
    	udf_table_func_open,        // required
    	udf_table_func_fetch_into,  // one of fetch_into or fetch_block required
    	udf_table_func_fetch_block, // one of fetch_into or fetch_block required
    	udf_table_func_rewind,      // optional
    	udf_table_func_close,       // required
    	NULL,                       // Reserved: must be NULL
    	NULL                        // Reserved: must be NULL
    };

    At the start of execution, the server calls the a_v4_extfn_proc function _evaluate_extfn to give the table UDF an opportunity to tell the server what table functions it is implementing. To do this, the table UDF must create an instance of a_v4_extfn_table that is given to the server. This structure contains a pointer to the a_v4_extfn_table_func descriptor and the number of columns in the result set.

    Use this descriptor as a model when developing your own table UDF:

    static a_v4_extfn_table_udf_rg_table = {
        &udf_table_funcs, // Table function descriptor
        1                 // number_of_columns
    };
  6. Implement the a_v4_extfn_proc structure functions.

    The table UDF must provide an implementation for each of the a_v4_extfn_proc functions that it declares in the a_v4_extfn_proc descriptor in step 3.

  7. Implement the a_v4_extfn_table_func structure functions.

    The table UDF must provide an implementation for each of the a_v4_extfn_table_func functions that it declares in the a_v4_extfn_table_func descriptor in step 5.

Related concepts
Scalar and Aggregate UDF Calling Patterns
udf_rg_2
udf_rg_3
Table UDF Implementation Examples
Related tasks
Implementing Sample Table UDF udf_rg_1
Related reference
External Function (a_v4_extfn_proc)
Table Functions (a_v4_extfn_table_func)
_evaluate_extfn