oracle odciaggregateiterate,ORACLE 自定义聚合函数

用户可以自定义聚合函数  ODCIAggregate,定义了四个聚集函数:初始化、迭代、合并和终止。 Initialization is accomplished by the ODCIAggregateInitialize() routine, which is invoked by Oracle to initialize the computation of the user-defined aggregate. The initialized aggregation context is passed back to Oracle as an object type instance.

Iteration is performed through the ODCIAggregateIterate() routine, which is repeatedly invoked by Oracle. On each invocation, a new value or a set of new values and the current aggregation context are passed in. The routine processes the new values and returns the updated aggregation context. This routine is invoked for every non-NULL value in the underlying group. NULL values are ignored during aggregation and are not passed to the routine.

Merging is performed by ODCIAggregateMerge(), a routine invoked by Oracle to combine two aggregation contexts. This routine takes the two contexts as inputs, combines them, and returns a single aggregation context.

Termination takes place when the ODCIAggregateTerminate() routine is invoked by Oracle as the final step of aggregation. The routine takes the aggregation context as input and returns the resulting aggregate value.

Consider the aggregate function AVG() in the following statement:SELECT AVG(T.Sales)

FROM AnnualSales T

GROUP BY T.State;

To perform this computation, the aggregate function AVG() goes through these steps:

Initializes the computation by initializing the aggregation context, or the rows over which aggregation is performed:runningSum = 0; runningCount = 0;

Iteratively processes each successive input value and updates the context:runningSum += inputval; runningCount++;

[Optional] Merge by combining the two aggregation contexts and return a single context. This operation combines the results of aggregation over subsets to obtain the aggregate over the entire set. This extra step can be required during either serial or parallel evaluation of an aggregate. If needed, it is performed before step 4:runningSum = runningSum1 + runningSum2;

runningCount = runningCount1 + runningCount2

Terminates by computing the result; uses the context to return the resultant aggregate value:return (runningSum/runningCount);

If AVG() were a user-defined function, the object type that embodies it would implement a method for a corresponding ODCIAggregate routine for each of these steps. The variables runningSum and runningCount, which determine the state of the aggregation in the example, would be attributes of that object type.

Example 12-2 Implementing the ODCIAggregate Interface

The ODCIAggregate routines are implemented as methods within an object type SpatialUnionRoutines. The actual implementation could be in any Oracle-supported language for type methods, such as PL/SQL, C, C++ or Java.CREATE TYPE SpatialUnionRoutines(

STATIC FUNCTION ODCIAggregateInitialize( ... ) ...,

MEMBER FUNCTION ODCIAggregateIterate(...) ... ,

MEMBER FUNCTION ODCIAggregateMerge(...) ...,

MEMBER FUNCTION ODCIAggregateTerminate(...)

);

CREATE TYPE BODY SpatialUnionRoutines IS

...

END;

Example 12-3 Defining a User-Defined Aggregate Function

This function definition creates the SpatialUnion() aggregate function by specifying its signature and the object type that implements the ODCIAggregate interface:CREATE FUNCTION SpatialUnion(x Geometry) RETURN Geometry

AGGREGATE USING SpatialUnionRoutines;

c712289e56f34195d1b3bf7d248191b5.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值