Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Semestr2 / 1 - Oracle / Oracle selected docs / Database concepts.pdf
Скачиваний:
29
Добавлен:
12.05.2015
Размер:
6.96 Mб
Скачать

User-Defined Aggregate Functions

User-Defined Aggregate Functions

Oracle supports a fixed set of aggregate functions, such as MAX, MIN, and SUM. These is also a mechanism to implement new aggregate functions with user-defined aggregation logic.

Why Have User-Defined Aggregate Functions?

User-defined aggregate functions (UDAGs) refer to aggregate functions with user-specified aggregation semantics. Users can create a new aggregate function and provide the aggregation logic through a set of routines. After it is created, the user-defined aggregate function can be used in SQL DML statements in a manner similar to built-in aggregates. The Oracle server evaluates the UDAG by invoking the user-provided aggregation routines appropriately.

Databases are increasingly being used to store complex data such as image, spatial, audio, video, and so on. The complex data is typically stored in the database using object types, opaque types, or LOBs. User-defined aggregates are primarily useful in specifying aggregation over such new domains of data.

Furthermore, UDAGs can be used to create new aggregate functions over traditional scalar data types for financial or scientific applications. Because it is not possible to provide native support for all forms of aggregates, it is desirable to provide application developers with a flexible mechanism to add new aggregate functions.

See Also:

Oracle9i Data Cartridge Developer’s Guide for information about implementing user-defined aggregates

Oracle9i Data Warehousing Guide for more information about using UDAGs in data warehousing

Chapter 12, "Native Datatypes" for more information on opaque types

Creation and Use of UDAGs

The following is the procedure for implementing user-defined aggregates:

1.Implement the ODCIAggregate interface routines as methods of an object type.

Object Datatypes and Object Views 13-15

User-Defined Aggregate Functions

2.Create a UDAG, using the CREATE FUNCTION statement and specify the implementation type created in Step 1:

CREATE FUNCTION MyUDAG ... AGGREGATE USING MyUDAGRoutines;

3.Use the UDAG in SQL DML statements the same way you use built-in aggregates:

SELECT col1, MyUDAG(col2) FROM tab GROUP BY col1;

How Do Aggregate Functions Work?

An aggregate function conceptually takes a set of values as input and returns a single value. The sets of values for aggregation are typically identified using a GROUP BY clause. For example:

SELECT AVG(T.Sales)

FROM AnnualSales T

GROUP BY T.State

The evaluation of an aggregate function can be decomposed into three primitive operations. Considering the preceding example of AVG(), they are:

1.Initialize : initialize the computation runningSum = 0; runningCount = 0;

2.Iterate : process new input value runningSum += inputval; runningCount++;

3.Terminate : compute the result return (runningSum/runningCount);

The variables runningSum and runningCount, in the preceding example, determine the state of the aggregation. Thus, the aggregation context can be viewed as an object that contains runningSum and runningCount attributes. The Initialize method initializes the aggregation context, Iterate updates it and Terminate method uses the context to return the resultant aggregate value.

In addition, we require one more primitive operation to merge two aggregation contexts and create a new context. This operation is needed to combine the results of aggregation over subsets and obtain the aggregate over the entire set. This situation can arise during both serial and parallel evaluations of the aggregate.

4.Merge: combine the two aggregation contexts and return a single context

13-16 Oracle9i Database Concepts

Соседние файлы в папке Oracle selected docs