Routine resolution in a type hierarchy
A type hierarchy is a relationship that you define among named row types in which subtypes inherit representation (data fields) and behavior (routines, operators, rules) from a named row above it (supertype) and can add additional fields and routines. The subtype is said to inherit the attributes and behavior from the supertype.
For information about creating type hierarchies, refer to the discussion of type and table hierarchies in the HCL OneDB™ Database Design and Implementation Guide.
When a UDR has named row types in its parameter list, the database server must resolve which type in the type hierarchy to pass to the UDR. When a data type in the argument list does not match the data type of the parameter in the same position of the routine signature, the database server searches for a routine with a parameter in the same position that is the closest supertype of that argument.
CREATE ROW TYPE emp
(name VARCHAR(30),
age INT,
salary DECIMAL(10,2));
CREATE ROW TYPE trainee UNDER emp ...
CREATE ROW TYPE student_emp (gpa FLOAT) UNDER trainee;
CREATE FUNCTION bonus (emp,INT) RETURNS DECIMAL(10,2) ...
CREATE FUNCTION bonus(trainee,FLOAT) RETURNS DECIMAL(10,2).
EXECUTE FUNCTION bonus(student_emp, INT);
- The database server processes the leftmost argument first:
- It looks for a candidate routine named bonus with
a row type parameter of student_emp.
No candidate routines exist with this parameter, so the database server continues with the next data type precedence, as described in Precedence list of data types.
- Because student_emp is a subtype of trainee, the
database server looks for a candidate routine with a parameter of
type trainee in the first position.
The first parameter of the second function, bonus(trainee,float), matches the first argument in the routine invocation. Therefore, this version of bonus() goes on the precedence list.
- It looks for a candidate routine named bonus with
a row type parameter of student_emp.
- The database server processes the second argument next:
- It looks for a candidate routine with a second parameter of data
type INTEGER.
The matching candidate routine from step 1b has a second parameter of data type FLOAT. Therefore, the database server continues with the next data type precedence as Precedence list of data types describes.
- Because the second parameter is the INTEGER built-in data type,
the database server goes to the precedence list that Precedence of
built-in data types shows.
The database server searches the candidate list of routines for a second parameter that matches one of the data types in the precedence list for the integer data type.
- Because a built-in cast exists from the integer data type to the float data type, the database server casts the integer argument to float before the execution of the bonus() function.
- It looks for a candidate routine with a second parameter of data
type INTEGER.
- Because of the left-to-right rule for processing the arguments, the database server executes the second function, bonus(trainee,float).