提交 422a100c 编写于 作者: S shenglian zhou

fix review comments about data points number overflow by round robin next pos...

fix review comments about data points number overflow by round robin next pos and record whether k points reached
上级 8d43b3a1
...@@ -178,6 +178,7 @@ typedef struct { ...@@ -178,6 +178,7 @@ typedef struct {
double sum; double sum;
int32_t numPointsK; int32_t numPointsK;
double* points; double* points;
bool kPointsMeet;
} SMovingAvgInfo; } SMovingAvgInfo;
typedef struct { typedef struct {
...@@ -4611,6 +4612,7 @@ static bool mavg_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResIn ...@@ -4611,6 +4612,7 @@ static bool mavg_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResIn
SMovingAvgInfo* mavgInfo = GET_ROWCELL_INTERBUF(pResInfo); SMovingAvgInfo* mavgInfo = GET_ROWCELL_INTERBUF(pResInfo);
mavgInfo->pos = 0; mavgInfo->pos = 0;
mavgInfo->kPointsMeet = false;
mavgInfo->sum = 0; mavgInfo->sum = 0;
mavgInfo->numPointsK = (int32_t)pCtx->param[0].i64; mavgInfo->numPointsK = (int32_t)pCtx->param[0].i64;
mavgInfo->points = (double*)((char*)mavgInfo + sizeof(SMovingAvgInfo)); mavgInfo->points = (double*)((char*)mavgInfo + sizeof(SMovingAvgInfo));
...@@ -4639,18 +4641,17 @@ static void mavg_function(SQLFunctionCtx *pCtx) { ...@@ -4639,18 +4641,17 @@ static void mavg_function(SQLFunctionCtx *pCtx) {
double v = 0; double v = 0;
GET_TYPED_DATA(v, double, pCtx->inputType, pData); GET_TYPED_DATA(v, double, pCtx->inputType, pData);
if (mavgInfo->pos < mavgInfo->numPointsK - 1) { if (!mavgInfo->kPointsMeet && mavgInfo->pos < mavgInfo->numPointsK - 1) {
mavgInfo->points[mavgInfo->pos] = v; mavgInfo->points[mavgInfo->pos] = v;
mavgInfo->sum += v; mavgInfo->sum += v;
} else { } else {
int32_t pos = mavgInfo->pos % mavgInfo->numPointsK; if (!mavgInfo->kPointsMeet && mavgInfo->pos == mavgInfo->numPointsK - 1){
if (mavgInfo->pos != mavgInfo->numPointsK -1) {
mavgInfo->sum = mavgInfo->sum + v - mavgInfo->points[pos];
} else {
mavgInfo->sum += v; mavgInfo->sum += v;
mavgInfo->kPointsMeet = true;
} else {
mavgInfo->sum = mavgInfo->sum + v - mavgInfo->points[mavgInfo->pos];
} }
mavgInfo->points[mavgInfo->pos] = v;
mavgInfo->points[pos] = v;
*pTimestamp = (tsList != NULL) ? tsList[i] : 0; *pTimestamp = (tsList != NULL) ? tsList[i] : 0;
SET_DOUBLE_VAL(pOutput, mavgInfo->sum / mavgInfo->numPointsK) SET_DOUBLE_VAL(pOutput, mavgInfo->sum / mavgInfo->numPointsK)
...@@ -4661,6 +4662,9 @@ static void mavg_function(SQLFunctionCtx *pCtx) { ...@@ -4661,6 +4662,9 @@ static void mavg_function(SQLFunctionCtx *pCtx) {
} }
++mavgInfo->pos; ++mavgInfo->pos;
if (mavgInfo->pos == mavgInfo->numPointsK) {
mavgInfo->pos = 0;
}
} }
if (notNullElems <= 0) { if (notNullElems <= 0) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册