From f3b34b1e5dec8031c74f8769a3ce5d9ae6b3de41 Mon Sep 17 00:00:00 2001 From: shenglian zhou Date: Fri, 22 Oct 2021 15:55:23 +0800 Subject: [PATCH] [TD-10735]:use different cumsum type for signed/unsigned/double input type --- src/query/src/qAggMain.c | 31 +++++++++++++++++++-------- tests/script/general/compute/csum.sim | 12 +++++++++++ 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/query/src/qAggMain.c b/src/query/src/qAggMain.c index 3fe99e1742..cef76bb6cc 100644 --- a/src/query/src/qAggMain.c +++ b/src/query/src/qAggMain.c @@ -20,7 +20,6 @@ #include "tdigest.h" #include "ttype.h" #include "tsdb.h" -#include "tglobal.h" #include "qAggMain.h" #include "qFill.h" @@ -172,7 +171,11 @@ typedef struct SDerivInfo { } SDerivInfo; typedef struct { - double cumSum; + union { + double d64CumSum; + int64_t i64CumSum; + uint64_t u64CumSum; + }; } SCumSumInfo; typedef struct { @@ -4709,7 +4712,7 @@ static bool csum_function_setup(SQLFunctionCtx *pCtx, SResultRowCellInfo* pResIn } SCumSumInfo* pCumSumInfo = GET_ROWCELL_INTERBUF(pResInfo); - pCumSumInfo->cumSum = 0; + pCumSumInfo->i64CumSum = 0; return true; } @@ -4733,20 +4736,30 @@ static void csum_function(SQLFunctionCtx *pCtx) { continue; } - double v = 0; - GET_TYPED_DATA(v, double, pCtx->inputType, pData); - pCumSumInfo->cumSum += v; + if (IS_SIGNED_NUMERIC_TYPE(pCtx->inputType)) { + int64_t v = 0; + GET_TYPED_DATA(v, int64_t, pCtx->inputType, pData); + pCumSumInfo->i64CumSum += v; + } else if (IS_UNSIGNED_NUMERIC_TYPE(pCtx->inputType)) { + uint64_t v = 0; + GET_TYPED_DATA(v, uint64_t, pCtx->inputType, pData); + pCumSumInfo->u64CumSum += v; + } else if (IS_FLOAT_TYPE(pCtx->inputType)) { + double v = 0; + GET_TYPED_DATA(v, double, pCtx->inputType, pData); + pCumSumInfo->d64CumSum += v; + } *pTimestamp = (tsList != NULL) ? tsList[i] : 0; if (IS_SIGNED_NUMERIC_TYPE(pCtx->inputType)) { int64_t *retVal = (int64_t *)pCtx->pOutput; - *retVal = (int64_t)(pCumSumInfo->cumSum); + *retVal = (int64_t)(pCumSumInfo->i64CumSum); } else if (IS_UNSIGNED_NUMERIC_TYPE(pCtx->inputType)) { uint64_t *retVal = (uint64_t *)pCtx->pOutput; - *retVal = (uint64_t)(pCumSumInfo->cumSum); + *retVal = (uint64_t)(pCumSumInfo->u64CumSum); } else if (IS_FLOAT_TYPE(pCtx->inputType)) { double *retVal = (double*) pCtx->pOutput; - SET_DOUBLE_VAL(retVal, pCumSumInfo->cumSum); + SET_DOUBLE_VAL(retVal, pCumSumInfo->d64CumSum); } ++notNullElems; diff --git a/tests/script/general/compute/csum.sim b/tests/script/general/compute/csum.sim index 1f291d784f..4d6f748566 100644 --- a/tests/script/general/compute/csum.sim +++ b/tests/script/general/compute/csum.sim @@ -88,6 +88,18 @@ sql select csum(tbcol) as b from $tb where ts <= $ms interval(1m) -x step6 return -1 step6: +print =============== csum result overflow follow sum behavior +sql create table overflow(ts timestamp, c1 bigint) +sql insert into overflow values(now-1s, NULL)(now, 9223372036854775807)(now+1s, 9223372036854775807) +sql select csum(c1) from overflow +print $data00 , $data01, $data10, $data11 +if $data01 != 9223372036854775807 then + return -1 +endi +if $data11 != -2 then + return -1 +endi + print =============== clear sql drop database $db sql show databases -- GitLab