您的位置:首页 > 数据库 > Oracle

ORACLE 自定义聚合函数

2016-05-31 20:31 357 查看
用户可以自定义聚合函数ODCIAggregate,定义了四个聚集函数:初始化、迭代、合并和终止。

InitializationisaccomplishedbytheODCIAggregateInitialize()routine,whichisinvokedbyOracletoinitializethecomputationoftheuser-definedaggregate.TheinitializedaggregationcontextispassedbacktoOracleasanobjecttypeinstance.

IterationisperformedthroughtheODCIAggregateIterate()routine,whichisrepeatedlyinvokedbyOracle.Oneachinvocation,anewvalueorasetofnewvaluesandthecurrentaggregationcontextarepassedin.Theroutineprocessesthenewvaluesandreturnstheupdatedaggregationcontext.Thisroutineisinvokedforeverynon-
NULL
valueintheunderlyinggroup.
NULL
valuesareignoredduringaggregationandarenotpassedtotheroutine.

MergingisperformedbyODCIAggregateMerge(),aroutineinvokedbyOracletocombinetwoaggregationcontexts.Thisroutinetakesthetwocontextsasinputs,combinesthem,andreturnsasingleaggregationcontext.

TerminationtakesplacewhentheODCIAggregateTerminate()routineisinvokedbyOracleasthefinalstepofaggregation.Theroutinetakestheaggregationcontextasinputandreturnstheresultingaggregatevalue.

Considertheaggregatefunction
AVG()
inthefollowingstatement:

SELECTAVG(T.Sales)
FROMAnnualSalesT
GROUPBYT.State;

Toperformthiscomputation,theaggregatefunction
AVG()
goesthroughthesesteps:

Initializesthecomputationbyinitializingtheaggregationcontext,ortherowsoverwhichaggregationisperformed:

runningSum=0;runningCount=0;


Iterativelyprocesseseachsuccessiveinputvalueandupdatesthecontext:

runningSum+=inputval;runningCount++;


[Optional]Mergebycombiningthetwoaggregationcontextsandreturnasinglecontext.Thisoperationcombinestheresultsofaggregationoversubsetstoobtaintheaggregateovertheentireset.Thisextrastepcanberequiredduringeitherserialorparallelevaluationofanaggregate.Ifneeded,itisperformedbeforestep4:

runningSum=runningSum1+runningSum2;
runningCount=runningCount1+runningCount2

Section"EvaluatingUser-DefinedAggregatesinParallel"describesthisstepingreaterdetail.

Terminatesbycomputingtheresult;usesthecontexttoreturntheresultantaggregatevalue:

return(runningSum/runningCount);


If
AVG()
wereauser-definedfunction,theobjecttypethatembodiesitwouldimplementamethodforacorresponding
ODCIAggregate
routineforeachofthesesteps.Thevariables
runningSum
and
runningCount
,whichdeterminethestateoftheaggregationintheexample,wouldbeattributesofthatobjecttype.

Example12-2ImplementingtheODCIAggregateInterface

The
ODCIAggregate
routinesareimplementedasmethodswithinanobjecttype
SpatialUnionRoutines
.TheactualimplementationcouldbeinanyOracle-supportedlanguagefortypemethods,suchasPL/SQL,C,C++orJava.

CREATETYPESpatialUnionRoutines(
STATICFUNCTIONODCIAggregateInitialize(...)...,
MEMBERFUNCTIONODCIAggregateIterate(...)...,
MEMBERFUNCTIONODCIAggregateMerge(...)...,
MEMBERFUNCTIONODCIAggregateTerminate(...)
);

CREATETYPEBODYSpatialUnionRoutinesIS
...
END;

Example12-3DefiningaUser-DefinedAggregateFunction

Thisfunctiondefinitioncreatesthe
SpatialUnion()
aggregatefunctionbyspecifyingitssignatureandtheobjecttypethatimplementsthe
ODCIAggregate
interface:

CREATEFUNCTIONSpatialUnion(xGeometry)RETURNGeometry
AGGREGATEUSINGSpatialUnionRoutines;
http://docs.oracle.com/database/121/ADDCI/ext_agg_ref.htm#ADDCI5129



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: