基本アルゴリズムでのswitch文の使用 - Advanced SQL Engine - Teradata Database

Teradata Vantage™ - SQL外部ルーチン プログラミング

Product
Advanced SQL Engine
Teradata Database
Release Number
17.05
17.00
Published
2020年6月
Language
日本語
Last Update
2021-03-30
dita:mapPath
ja-JP/qwr1571437338192.ditamap
dita:ditavalPath
ja-JP/qwr1571437338192.ditaval
dita:id
B035-1147
Product Category
Software
Teradata Vantage

各集約フェーズに必要な適切なコードを実行するよう集約UDFを作成するための1つの方法は、Cのswitch文を使用することです。

必要な処理を示すため、ここでは標準偏差を計算するための簡単な集約UDFから抜粋したコードを使用します。完全なコード例については、C集約関数を参照してください。

サンプルは次のとおりです。

#define SQL_TEXT Latin_Text
#include <sqltypes_td.h>
#include <string.h>
#include <math.h>
void STD_DEV ( FNC_Phase     phase,
               FNC_Context_t *fctx,
               FLOAT         *x,
               FLOAT         *result,
               int           *x_i,
               int           *result_i,
               char          sqlstate[6],
               SQL_TEXT      fncname[129],
               SQL_TEXT      sfncname[129],
               SQL_TEXT      error_message[257])
{
   /* switch to determine the aggregation phase */
   switch (phase)
   {
      /* This phase is executed once per group and */
      /* allocates and initializes intermediate    */
      /* aggregate storage */
      case AGR_INIT:
         /* Get storage for intermediate aggregate values. */
         ...
         /* Initialize the intermediate aggregate values   */
         ...
      /**************************************************/
      /* Fall through to detail phase, because the      */
      /* AGR_INIT phase passes in the first set of      */
      /* values for the group.                          */
      /**************************************************/
      /* This phase is executed once for each selected    */
      /* row to aggregate. One copy will run on each AMP. */
      case AGR_DETAIL:
         ...
         break;
      /* This phase combines the results of ALL           */
      /* individual AMPs for each group.                  */
      case AGR_COMBINE:
         ...
         break;
      /* This phase returns the final result. */
      /* It is called once for each group.    */
      case AGR_FINAL:
      {
         ...
         break;
      }
      case AGR_NODATA:
         /* return null if no data */
         *result_i = -1;
         break;
      default:
         /* If it gets here there must be an error because this */
         /* function does not accept any other phase options */
         strcpy(sqlstate, "U0005");
   }
   return;
}