From 5d63ff236c85ffaeac3c0acc54279a5dd09242e2 Mon Sep 17 00:00:00 2001 From: wpan Date: Thu, 26 Aug 2021 17:49:45 +0800 Subject: [PATCH] fix irate wrong value issue --- src/query/src/qAggMain.c | 14 +++++++++++++- tests/script/general/parser/function.sim | 11 +++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/query/src/qAggMain.c b/src/query/src/qAggMain.c index 9019a01eef..1db8f8233e 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 556292b21b..48e52cc7d7 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; -- GitLab