TeraData 中的 IRR(Return 的内部利率)UDF
IRR(Internal Rate of Return) UDF in TeraData
我正在尝试在 Teradata 中为 IRR 创建一个 UDF。
我想以数组的形式将 row 的所有元素存储在我的中间存储中(一次添加一个)。这有助于我多次使用这些元素(采用试错逻辑)
我只是想知道,在 "typedef" 中针对 teradata 中的此要求使用哪种兼容数据类型?
由于 IRR 逻辑有些冗长,我在这里借助新的 sum_redefined 函数来说明我的问题。
我只是想计算传递的参数(行元素)的总和,方法是将它们存储到一个数组中,然后再添加它们。
这是我的代码:
注意:以下显然无法编译,因为我仍在尝试理解 Teradata 中 C 数组的正确数据类型。
#define SQL_TEXT Latin_Text
#include <sqltypes_td.h>
#include <string.h>
#include <math.h>
typedef struct agr_storage {
int count;
ARRAY_HANDLE elements[100];
} AGR_Storage;
void SUM_REDEFINED( FNC_Phase phase,
FNC_Context_t *fctx,
FLOAT *x, //here im passing row element one by one
FLOAT *result,
int *x_i,
int *result_i,
char sqlstate[6],
SQL_TEXT fncname[129],
SQL_TEXT sfncname[129],
SQL_TEXT error_message[257] )
{
AGR_Storage *s1 = fctx->interim1;
switch (phase)
{
case AGR_INIT:
s1 = FNC_DefMem(sizeof(AGR_Storage));
if (s1 == NULL)
{
strcpy(sqlstate, "U0001");
return;
}
s1->count = 0;
s1->elements[100]={0};
case AGR_DETAIL:
if (*x_i != -1)
{
s1->count++;
s1->elemnts[s1->count]=*x;//appending elemts one by one
break;
case AGR_COMBINE:
case AGR_FINAL:
{
int no_of_elements=s1->count;
int i=0;
FLOAT sum=0;
for(i=0;i<no_of_elements;i++){
sum+=s1->elements[i]; //adding all the elements
}
*result = sum; //returning the sum
break;
}
case AGR_NODATA:
*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;
}
我们可以像在 C 中那样在 Teradata 中声明数组。但请记住,对于 UDF,每个中间存储块 space 限制为 64 bytes.so 如果您定义整数数组你不能使数组大于 16 个整数(整数需要 4 个字节 16*4=64)。其他数据类型也是如此。
所以将您的代码更改为
typedef struct agr_storage {
int count;
int elements[*Number here*];
} AGR_Storage;
声明的所有数据类型的总和应小于 64 字节。
我正在尝试在 Teradata 中为 IRR 创建一个 UDF。
我想以数组的形式将 row 的所有元素存储在我的中间存储中(一次添加一个)。这有助于我多次使用这些元素(采用试错逻辑)
我只是想知道,在 "typedef" 中针对 teradata 中的此要求使用哪种兼容数据类型?
由于 IRR 逻辑有些冗长,我在这里借助新的 sum_redefined 函数来说明我的问题。
我只是想计算传递的参数(行元素)的总和,方法是将它们存储到一个数组中,然后再添加它们。
这是我的代码:
注意:以下显然无法编译,因为我仍在尝试理解 Teradata 中 C 数组的正确数据类型。
#define SQL_TEXT Latin_Text
#include <sqltypes_td.h>
#include <string.h>
#include <math.h>
typedef struct agr_storage {
int count;
ARRAY_HANDLE elements[100];
} AGR_Storage;
void SUM_REDEFINED( FNC_Phase phase,
FNC_Context_t *fctx,
FLOAT *x, //here im passing row element one by one
FLOAT *result,
int *x_i,
int *result_i,
char sqlstate[6],
SQL_TEXT fncname[129],
SQL_TEXT sfncname[129],
SQL_TEXT error_message[257] )
{
AGR_Storage *s1 = fctx->interim1;
switch (phase)
{
case AGR_INIT:
s1 = FNC_DefMem(sizeof(AGR_Storage));
if (s1 == NULL)
{
strcpy(sqlstate, "U0001");
return;
}
s1->count = 0;
s1->elements[100]={0};
case AGR_DETAIL:
if (*x_i != -1)
{
s1->count++;
s1->elemnts[s1->count]=*x;//appending elemts one by one
break;
case AGR_COMBINE:
case AGR_FINAL:
{
int no_of_elements=s1->count;
int i=0;
FLOAT sum=0;
for(i=0;i<no_of_elements;i++){
sum+=s1->elements[i]; //adding all the elements
}
*result = sum; //returning the sum
break;
}
case AGR_NODATA:
*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;
}
我们可以像在 C 中那样在 Teradata 中声明数组。但请记住,对于 UDF,每个中间存储块 space 限制为 64 bytes.so 如果您定义整数数组你不能使数组大于 16 个整数(整数需要 4 个字节 16*4=64)。其他数据类型也是如此。
所以将您的代码更改为
typedef struct agr_storage {
int count;
int elements[*Number here*];
} AGR_Storage;
声明的所有数据类型的总和应小于 64 字节。