各集約フェーズに必要な適切なコードを実行するよう集約UDFを作成するための1つの方法は、Javaのswitch文を使用することです。
必要な処理を示すため、ここでは標準偏差を計算するための簡単な集約UDFから抜粋したコードを使用します。完全なコード例については、UDFのサンプル コードを参照してください。
サンプルは次のとおりです。
import com.teradata.fnc.*;
import java.io.*;
import java.sql.*;
...
public class UDFExample {
public static Double stdDev(Phase phase, Context[] context, double x)
throws SQLException
{
...
/* switch to determine the aggregation phase */
switch (phase.getPhase()) {
/* The AGR_INIT phase is executed once per group and */
/* allocates and initializes intermediate storage. */
case Phase.AGR_INIT:
/* Get storage for intermediate aggregate values. */
...
/* Initialize the intermediate aggregate values. */
...
/* Fall through to the next phase because this phase */
/* passes in the first set of values for the group. */
/* The AGR_DETAIL phase is executed once for each selected */
/* row to aggregate. One copy will run on each AMP. */
case Phase.AGR_DETAIL:
...
break;
/* The AGR_COMBINE phase combines the results of */
/* ALL individual AMPs for each group. */
case Phase.AGR_COMBINE:
...
break;
/* The AGR_FINAL phase returns the final result. */
/* It is called once for each group. */
case Phase.AGR_FINAL:
...
case Phase.AGR_NODATA:
/* Not expecting no data. */
return -1;
default:
/* If it gets here there must be an error because this */
/* UDF does not accept any other phase options */
throw new SQLException("Invalid Phase", "38U05");
}
/* Save the intermediate results in the aggregate storage. */
...
...
}
}