External scalar functions are scalar functions that have their
logic implemented in an external programming language.
These functions can be developed and used to extend the set of
existing SQL functions and can be invoked in the same manner as DB2® built-in functions such as LENGTH
and COUNT. That is, they can be referenced in SQL statements wherever
an expression is valid.
The execution of external scalar function logic takes place on
the DB2 database server, however
unlike built-in or user-defined SQL scalar functions, the logic of
external functions can access the database server filesystem, perform
system calls or access a network.
External scalar functions can read SQL data, but cannot modify
SQL data.
External scalar functions can be repeatedly invoked for a single
reference of the function and can maintain state between these invocations
by using a scratchpad, which is a memory buffer. This can be powerful
if a function requires some initial, but expensive, setup logic.
The setup logic can be done on a first invocation using the scratchpad
to store some values that can be accessed or updated in subsequent
invocations of the scalar function.
- Features of external scalar functions
-
- Can be referenced as part of an SQL statement anywhere an expression
is supported.
- The output of a scalar function can be used directly by the invoking
SQL statement.
- For external scalar user-defined functions, state can be maintained
between the iterative invocations of the function by using a scratchpad.
- Can provide a performance advantage when used in predicates, because
they are executed at the server. If a function can be applied to
a candidate row at the server, it can often eliminate the row from
consideration before transmitting it to the client machine, reducing
the amount of data that must be passed from server to client.
- Limitations
-
- Cannot do transaction management within a scalar function. That
is, you cannot issue a COMMIT or a ROLLBACK within a scalar function.
- Cannot return result sets.
- Scalar functions are intended to return a single scalar value
per set of inputs.
- External scalar functions are not intended to be used for a single
invocation. They are designed such that for a single reference to
the function and a given set of inputs, that the function be invoked
once per input, and return a single scalar value. On the first invocation,
scalar functions can be designed to do some setup work, or store some
information that can be accessed in subsequent invocations. SQL
scalar functions are better suited to functionality that requires
a single invocation.
- In a single partition database external scalar functions can contain
SQL statements. These statements can read data from tables, but cannot
modify data in tables. If the database has more than one partition
then there must be no SQL statements in an external scalar function.
SQL scalar functions can contain SQL statements that read or modify
data.
- Common uses
-
- Extend the set of DB2 built-in
functions.
- Perform logic inside an SQL statement that SQL cannot natively
perform.
- Encapsulate a scalar query that is commonly reused as a subquery
in SQL statements. For example, given a postal code, search a table
for the city where the postal code is found.
- Supported languages
-
- C
- C++
- Java™
- OLE
- .NET common language runtime languages
Note: - There is a limited capability for creating aggregate functions.
Also known as column functions, these functions receive a
set of like values (a column of data) and return a single answer.
A user-defined aggregate function can only be created if it is sourced
upon a built-in aggregate function. For example, if a distinct type SHOESIZE exists
that is defined with base type INTEGER, you could define a function, AVG(SHOESIZE),
as an aggregate function sourced on the existing built-in aggregate
function, AVG(INTEGER).
- You can also create function that return a row. These are known
as row functions and can only be used as a transform function for
structured types. The output of a row function is a single row.