設計上の考慮事項 - 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

定数モードのテーブル関数はすべてのAMP vprocに送信され、各コピーに同じ入力引数が渡されます。各コピーは、関数が終了したことを示すまでは繰り返し呼び出されます。

テーブル関数の設計時に、すべてのAMP vprocですべてのテーブル関数ループが処理に参加することに意味があるかどうかを判別する必要があります。

例えば、標準的な定数モードのテーブル関数は通常、データベースの外部からデータを読み取って結果行を生成します。外部データが1ノードだけで利用可能な場合は、1つのvproc上の1つの関数コピーだけに何かの有用な機能を持たせるほうが実用的です。一方、外部データが各ノードで利用可能な場合は、すべてのAMP vprocからデータを読み取れるようにしておくほうが望ましいと言えます。

ユーザー 結果
テーブル関数のすべてのコピーが処理に参加するようにする ガイドラインとして以下のコード抜粋を使用して関数を実装します。
FNC_Phase  Phase;

if ( FNC_GetPhase(& Phase) != TBL_MODE_CONST)
{
   /* set sqlstate to an error and return */
   strcpy(sqlstate, "U0005");
   return;
}

/* depending on the phase decide what to do */
switch(Phase)
{
   case TBL_PRE_INIT:
   {
      break;
   }
   case TBL_INIT:
   {
      /* Open any files here. */
      ...

      break;
   }   
   case TBL_BUILD:
   {
      /* Read from files and build the result row here.     */
      /* On EOF, set sqlstate to "02000" (no row to build). */
      ...
      break;
   }
   case TBL_END:
   {
      /* Everyone done. Close files. */
      ...
      break;
   }
   case TBL_ABORT:
   {
      /* A copy called FNC_TblAbort. Close files. */
      ...
      break;
   }
}
どのAMP上でも実行可能であり、参加するコピーを1つだけ必要とするテーブル関数 FNC_TblFirstParticipantライブラリ関数をテーブル関数のすべてのコピーから呼び出します。呼び出しを行なった最初のコピーが、参加するコピーです。他のすべてのコピーは、FNC_TblOptOutを呼び出して戻る必要があります。

ガイドラインとして以下のコード抜粋を使用して関数を実装します。

FNC_Phase  Phase;

if ( FNC_GetPhase(& Phase) != TBL_MODE_CONST)
{
   /* set sqlstate to an error and return */
   strcpy(sqlstate, "U0005");
   return;
}

/* depending on the phase decide what to do */
switch(Phase)
{
   case TBL_PRE_INIT:
   {
      switch (FNC_TblFirstParticipant() )
      {
         case 1:   /* participant */
            return;
         case 0:  /* not participant */
            if (FNC_TblOptOut())
               strcpy(sqlstate, "U0006"); /* error */
            return;
         default:  /* -1 or other error */
            strcpy(sqlstate, "U0007");
            return;
      }
      break;
   }
   case TBL_INIT:
   {
      /* Open any files here. */
      ...
      break;
   }
   case TBL_BUILD:
   {
      /* Read from files and build the result row here.     */
      /* On EOF, set sqlstate to "02000" (no row to build). */
      ...
      break;
   }   
   case TBL_END:
   {
      /* Everyone done. Close files. */
      ...
      break;
   }
   case TBL_ABORT:
   {
      /* A copy called FNC_TblAbort. Close files. */
      ...
      break;
   }
}
テーブル関数の1つのコピーを、他のAMP vproc上で実行するすべてのコピーの制御コピーにする FNC_TblControlライブラリ関数を呼び出して、テーブル関数の1つのコピーを制御コピーとして指定します。FNC_TblAllocCtrlCtxを呼び出すことで制御スクラッチパッドを割り振り、他のテーブル関数コピーにデータを分散させます。

ガイドラインとして以下のコード抜粋を使用して関数を実装します。完全なコード例については、Cテーブル関数を参照してください。

typedef struct {
  unsigned short cntrl_fnc_AMP
  int            qfd;
  ...
} ctrl_ctx;
ctrl_ctx    *options;
FNC_Phase    Phase;
AMP_Info_t  *LocalConfig;

if ( FNC_GetPhase(& Phase) != TBL_MODE_CONST)
{
   /* Set sqlstate to an error and return. */
   strcpy(sqlstate, "U0005");
   return;
}

/* Depending on the phase decide what to do. */
switch(Phase)
{
  case TBL_PRE_INIT:
  {
    LocalConfig = FNC_AMPInfo();
    /* Run controlling copy on the lowest AMP on the node. */
    if (LocalConfig->LowestAMPOnNode)
    {
      /* Run on the node that can access external file. */
      if (access('filetoread')
      {
        if ( FNC_TblControl() )
        {
          /* Use scratchpad to distribute data to other */
          /* function copies during the TBL_INIT phase. */
          options = FNC_TblAllocCtrlCtx(sizeof(ctrl_ctx));
          options->ctrl_fnc_AMP = LocalConfig->AMPId;
          ...
        }
      }
    }
  }
  case TBL_INIT:
  {
    /* Get the data from the controlling copy. */
    options = FNC_TblGetCtrlCtx();
    ...

    break;
  }  
  case TBL_BUILD:
  {
    /* Build result row or set sqlstate to "02000" if no data */
    ...
    break;
  }
  case TBL_END:
  {
    /* Everyone done. Close files. */
    /* Controlling copy must do extra cleanup. */
    ...
    break;
  }
  case TBL_ABORT:
  {
    /* A copy called FNC_TblAbort. Close files. */
    ...
    break;
  }
}