Lab 6 Developing and executing SQL user1defined functions
Big SQL enables users to create their own SQL functions that can be invoked in queries. User5defined
functions (UDFs) promote code re5use and reduce query complexity. They can be written to return a
single (scalar) value or a result set (table). Programmers can write UDFs in SQL or any supported
programming languages (such as Java and C). For simplicity, this lab focuses on SQL UDFs.
After you complete this lab, you will understand how to:
• Create scalar and table UDFs written in SQL.
• Incorporate procedural logic in your UDFs.
• Invoke UDFs in Big SQL queries.
• Drop UDFs.
Allow 1 5 1.5 hours to complete this lab.
Please note that this lab discusses only some of the capabilities of Big SQL scalar and table UDFs. For
an exhaustive list of all the capabilities, please see the BigInsights 4.0 knowledge center (http://www5
01.ibm.com/support/knowledgecenter/SSPT3X_4.0.0/com.ibm.swg.im.infosphere.biginsights.welcome.d
oc/doc/welcome.html).
Prior to starting this lab, you must be familiar with how to use the Big SQL command line (JSqsh), and
you must have created the sample GOSALESDW tables. If necessary, work through earlier lab
exercises on these topics.
This UDF lab was originally developed by Uttam Jain (
[email protected]) with contributions from
Cynthia M. Saracco. Please post questions or comments to the forum on Hadoop Dev at
https://developer.ibm.com/hadoop/support/.
6.1. Understanding UDFs
Big SQL provides many built5in functions to perform common computations. An example is dayname(),
which takes a date/timestamp and returns the corresponding day name, such as Friday.
Often, organizations need to perform some customized or complex operation on their data that’s beyond
the scope of any built5in5function. Big SQL allows users to embed their customized business logic inside
a user5defined function (UDF) and write queries that call these UDFs.
As mentioned earlier, Big SQL supports two types of UDFs:
1. Scalar UDF: These functions take one or more values as input and return a single value as
output. For example, a scalar UDF can take three values (price of an item, percent discount on
that item, and percent sales tax) to compute the final price of that item.
2. Table UDF: These functions take one or more values as input and return a whole table as output.
For example, a table UDF can take single value (department5id) as input and return a table of
employees who work in that department. This result set could have multiple columns, such as
employee5id, employee5first5name, employee5last5name.