Function resolution

After a function is invoked, DB2® must determine which function to execute. This process is called function resolution and it applies to both built-in and user-defined function.

A function is invoked by its function name, which is implicitly or explicitly qualified with a schema name, followed by parentheses that enclose the arguments to the function. Within the database, each function is uniquely identified by its function signature, which is its schema name, function name, the number of parameters, and the data types of the parameters. Thus, a schema can contain several functions that have the same name but each of which have a different number of parameters or parameters with different data types. Also, a function with the same name, number of parameters, and types of parameters can exist in multiple schemas.

Start of changeFunction resolution has two steps:
  1. DB2 determines the set of candidate functions based on the qualification of the name of the invoked function, the unqualified name of the invoked function, and the number of arguments that are specified.
  2. DB2 determines the best fit from the set of candidate functions based on the data types of the arguments of the invoked function as compared with the data types of the parameters of the functions in the set of candidate functions.
End of change

Function resolution is similar for functions that are invoked with a qualified or unqualified function name with the exception that for an unqualified name, DB2 needs to search more than one schema.

Start of changeTo improve performance of function resolution and to prevent potential issues as new functions are added, consider invoking user-defined functions by using a fully qualified name, including the schema name.End of change

Start of changeFor a function invocation that passes a transition table, the data type, length, precision, and scale of each column in the transition table must exactly match the data type, length, precision, and scale of each column of the table that is named in the function definition.End of change

Start of changeThe timestamp for the creation of a user-defined function must be older than the timestamp that results from an explicit bind for the plan or package that contains the function invocation. During autobind, built-in functions that are introduced in a DB2 release that is later than the DB2 release that is used to explicitly bind the package or plan are not considered for function resolution.End of change

Start of changeIn a CREATE VIEW statement, function resolution occurs at the time the view is created. If another function with the same name is subsequently created, the view is not affected, even if the new function is a better fit than the one that was chosen at the time the view was created.End of change

Qualified function resolution: When a function is invoked with a schema name and a function name, DB2 only searches the specified schema to resolve which function to execute.

Start of changeDB2 selects candidate functions based on the following criteria:
  • The name of the function instance must match the name in the function invocation.
  • The number of input parameters in the function instance must match the number of arguments in the function invocation.
  • The authorization ID of the statement must have the EXECUTE privilege to the function instance.
End of change

Start of changeIf no function meets these criteria, an error is returned. If one or more candidate functions are found in the schema, this set of candidate functions is processed for best fit.End of change

Start of changeFor a function invocation that contains untyped parameter markers, the data types of those parameter markers are considered to match or be promotable to the data types of the parameters in the function instance.End of change

Start of changeUnqualified function resolution: When a function is invoked without a qualifier, DB2 searches the list of schemas in the SQL path to resolve which function instance to execute. For each schema in the SQL path, DB2 searches the schema for candidate functions based on the following criteria:
  • The name of the function instance must match the name in the function invocation.
  • The number of input parameters in the function instance must match the number of function arguments in the function invocation.
  • The authorization ID of the statement must have the EXECUTE privilege on the function instance.
If DB2 does not find any candidate functions, an error is returned.End of change

Start of changeIf no function meets these criteria, an error is returned. If one or more candidate functions are found in the schema, this set of candidate functions is processed for best fit.End of change

Start of changeFor a function invocation that contains untyped parameter markers, the data types of those parameter markers are considered to match or be promotable to the data types of the parameters in the function instance.End of change