Oracle Pipelined Table Functions
Oracle Pipelined Table Functions
Basically, when you would like a PLSQL (or java or c) routine to be the «source»
of data -- instead of a table -- you would use a pipelined function.
PIPELINED functions will operate like a table.
A PL/SQL function may be used in a data warehouse database to transform large amounts of data. This might also involve massaging the data in a series of transformations, each performed by different functions. Prior to Oracle Database 9, large transformations required either significant memory overhead, or storing the data in intermediate tables between each stage of the transformation. The loading process caused immense performance degradations in both cases.
Using PL/SQL table functions can significantly lower the over-head of doing such transformations. PL/SQL table functions accept and return multiple rows, delivering them as they are ready rather than all at once, and can be made to execute as parallel operations.
Simple Example - Generating Some Random Data
How could you create six unique random numbers between 1 and 49 with one SQL statement?
We would generate the set of numbers to pick from (see the innermost query that follows); any table with 49 or more records would do it. First the quick-and-dirty solution without a pipelined function.
from (select r
from (select rownum r
where rownum < 50)
order by dbms_random.value)
where rownum <= 6;
That query works by generating the numbers 1 .. 49, using the inline view. We wrap that innermost query as an inline view and sort it by a random value, using DBMS_RANDOM.VALUE. We wrap that result set in yet another inline view and just take the first six rows. If we run that query over and over, we'll get a different set of six rows each time.
This sort of question comes up frequently—maybe not about how to generate a set of six random numbers but rather, "how can we get N rows?" For example, we'd like the inclusive set of all dates between 25-FEB-2004 and 10-MAR-2004. The question becomes how to do this without a "real" table, and the answer lies in Oracle9i/10g with its PIPELINED functioncapability. We can write a PL/SQL function that will operate like a table. We need to start with a SQL collection type; this describes what the PIPELINED function will return. In this case, we are choosing a table of numbers; the virtual table we are creating will simply return the numbers 1, 2, 3, ... N:
create type array
as table of number
Next, we create the actual PIPELINED function. This function will accept an input to limit the number of rows returned. If no input is provided, this function will just keep generating rows for a very long time (so be careful and make sure to use ROWNUM or some other limit in the query itself!). The PIPELINED keyword on line 4 allows this function to work as if it were a table:
gen_numbers(n in number default null)
for i in 1 .. nvl(n,999999999)
Suppose we needed three rows for something. We can now do that in one of two ways:
select * from TABLE(gen_numbers(3));
select * from TABLE(gen_numbers)
where rownum <= 3;
Now we are ready to re-answer the original question, using the following functionality:
from (select * from table(gen_numbers(49)))
order by dbms_random.random
where rownum <= 6
We can use this virtual table functionality for many things, such as generating that range of dates:
Note the name of the column we used: COLUMN_VALUE. That is the default name for the column coming back from the PIPELINED function.
Typical Pipelined Example
This are the typical steps to perform when using PL/SQL Table Functions:
o The producer function must use the PIPELINED keyword in its declaration.
o The producer function must use an OUT parameter that is a record, corresponding to a row in the result set.
o Once each output record is completed, it is sent to the consumer function through the use of the PIPE ROW keyword.
o The producer function must end with a RETURN statement that does not specify any return value.
o The consumer function or SQL statement then must use the TABLE keyword to treat the resulting rows from the PIPELINE function like a regular table.
The first step is to define the format of the rows that are going to be returned. In this case here, we're going to return a INT, DATE followed by a VARCHAR2(25).
CREATE OR REPLACE TYPE myObjectFormat
Next a collection type for the type previously defined must be created.
CREATE OR REPLACE TYPE myTableType
AS TABLE OF myObjectFormat
Finally, the producer function is packaged in a package. It is a pipelined function as indicated by the keyword pipelined.
CREATE OR REPLACE PACKAGE myDemoPack
FUNCTION prodFunc RETURN myTableType PIPELINED;
CREATE OR REPLACE PACKAGE BODY myDemoPack AS
FUNCTION prodFunc RETURN myTableType PIPELINED IS
FOR i in 1 .. 5
PIPE ROW (myObjectFormat(i,SYSDATE+i,'Row '||i));
ALTER SESSION SET NLS_DATE_FORMAT='dd.mm.yyyy';
SELECT * FROM TABLE(myDemoPack.prodFunc());
A B C
---------- ---------- ---------
1 31.05.2004 Row 1
2 01.06.2004 Row 2
3 02.06.2004 Row 3
4 03.06.2004 Row 4
5 04.06.2004 Row 5
Pipelined functions are useful if there is a need for a data source other than a table in a select statement.