Defining a Scalar UDF

The C/C++ code for defining a scalar user-defined function includes four mandatory pieces.

  • extfnapiv3.h – inclusion of the UDF interface definition header file.

  • _evaluate_extfn – An evaluation function. All evaluation functions take two arguments:

    • an instance of the scalar UDF context structure that is unique to each usage of a UDF that contains a set of callback function pointers, and a pointer where a UDF can store UDF-specific data.

    • a pointer to a data structure that allows access to the argument values and to the result value through the supplied callbacks.

  • a_v3_extfn_scalar – an instance of the scalar UDF descriptor structure that contains a pointer to the evaluation function.

  • Descriptor function – returns a pointer to the scalar UDF descriptor structure.

These parts are optional:
  • _start_extfn – an initialization function generally invoked once per SQL usage. If supplied, you must also place a pointer to this function into the scalar UDF descriptor structure. All initialization functions take one argument, a pointer to the scalar UDF context structure that is unique to each usage of a UDF. The context structure passed is the same one that is passed to the evaluation routine.

  • _finish_extfn – a shutdown function generally invoked once per SQL usage. If supplied, a pointer to this function must also be placed into the scalar UDF descriptor structure. All shutdown functions take one argument, a pointer to the scalar UDF context structure that is unique to each usage of a UDF. The context structure passed is the same one that is passed to the evaluation routine.

Related tasks
Declaring a Scalar UDF