diff --git a/src/query/src/qAggMain.c b/src/query/src/qAggMain.c index 9019a01eeff87414e917923666d2e4d842e3d12c..1db8f8233e487d47d4af8bab51767e22b6e42fe6 100644 --- a/src/query/src/qAggMain.c +++ b/src/query/src/qAggMain.c @@ -4032,11 +4032,23 @@ static void irate_function(SQLFunctionCtx *pCtx) { double v = 0; GET_TYPED_DATA(v, double, pCtx->inputType, pData); - if ((INT64_MIN == pRateInfo->lastKey) || primaryKey[i] > pRateInfo->lastKey) { + if (INT64_MIN == pRateInfo->lastKey) { pRateInfo->lastValue = v; pRateInfo->lastKey = primaryKey[i]; continue; } + + if (primaryKey[i] > pRateInfo->lastKey) { + if ((INT64_MIN == pRateInfo->firstKey) || pRateInfo->lastKey > pRateInfo->firstKey) { + pRateInfo->firstValue = pRateInfo->lastValue; + pRateInfo->firstKey = pRateInfo->lastKey; + } + + pRateInfo->lastValue = v; + pRateInfo->lastKey = primaryKey[i]; + + continue; + } if ((INT64_MIN == pRateInfo->firstKey) || primaryKey[i] > pRateInfo->firstKey) { pRateInfo->firstValue = v; diff --git a/tests/script/general/parser/function.sim b/tests/script/general/parser/function.sim index 556292b21b218f4df2aaa034d8babe35903a23b8..48e52cc7d7f946a18c366fc1ba667daf3fcf55eb 100644 --- a/tests/script/general/parser/function.sim +++ b/tests/script/general/parser/function.sim @@ -941,6 +941,17 @@ if $data32 != 0.000144445 then return -1 endi +sql insert into t1 values('2015-09-18 00:30:00', 3.0); +sql select irate(k) from t1 +if $rows != 1 then + return -1 +endi + +if $data00 != 0.000000354 then + return -1 +endi + + print ===========================> derivative sql drop table t1 sql drop table tx;