From c7c99ae34634be4ec6852bdc6f6961c132264c8b Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Wed, 19 Oct 2022 09:33:54 +0800 Subject: [PATCH] fix(query): update last_row callback function. --- source/dnode/vnode/src/tsdb/tsdbCacheRead.c | 27 +++++++++++++++++---- source/libs/function/src/builtins.c | 2 +- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/source/dnode/vnode/src/tsdb/tsdbCacheRead.c b/source/dnode/vnode/src/tsdb/tsdbCacheRead.c index 38c10e8354..63c889985a 100644 --- a/source/dnode/vnode/src/tsdb/tsdbCacheRead.c +++ b/source/dnode/vnode/src/tsdb/tsdbCacheRead.c @@ -188,15 +188,26 @@ int32_t tsdbRetrieveCacheRows(void* pReader, SSDataBlock* pResBlock, const int32 SArray* pRow = NULL; size_t numOfTables = taosArrayGetSize(pr->pTableList); bool hasRes = false; + SArray* pLastCols = NULL; void** pRes = taosMemoryCalloc(pr->numOfCols, POINTER_BYTES); + if (pRes == NULL) { + code = TSDB_CODE_OUT_OF_MEMORY; + goto _end; + } + for (int32_t j = 0; j < pr->numOfCols; ++j) { pRes[j] = taosMemoryCalloc(1, sizeof(SFirstLastRes) + pr->pSchema->columns[slotIds[j]].bytes + VARSTR_HEADER_SIZE); SFirstLastRes* p = (SFirstLastRes*)varDataVal(pRes[j]); p->ts = INT64_MIN; } - SArray* pLastCols = taosArrayInit(pr->pSchema->numOfCols, sizeof(SLastCol)); + pLastCols = taosArrayInit(pr->pSchema->numOfCols, sizeof(SLastCol)); + if (pLastCols == NULL) { + code = TSDB_CODE_OUT_OF_MEMORY; + goto _end; + } + for (int32_t i = 0; i < pr->numOfCols; ++i) { SLastCol p = {0}; p.ts = INT64_MIN; @@ -262,7 +273,6 @@ int32_t tsdbRetrieveCacheRows(void* pReader, SSDataBlock* pResBlock, const int32 } if (hasRes) { - pResBlock->info.rows = 1; saveOneRow(pLastCols, pResBlock, pr, slotIds, pRes); } @@ -286,12 +296,19 @@ int32_t tsdbRetrieveCacheRows(void* pReader, SSDataBlock* pResBlock, const int32 pr->tableIndex += 1; if (pResBlock->info.rows >= pResBlock->info.capacity) { - return TSDB_CODE_SUCCESS; + goto _end; } } } else { - return TSDB_CODE_INVALID_PARA; + code = TSDB_CODE_INVALID_PARA; } - return TSDB_CODE_SUCCESS; + _end: + for (int32_t j = 0; j < pr->numOfCols; ++j) { + taosMemoryFree(pRes[j]); + } + + taosMemoryFree(pRes); + taosArrayDestroy(pLastCols); + return code; } diff --git a/source/libs/function/src/builtins.c b/source/libs/function/src/builtins.c index 4644b15dea..ce440a525a 100644 --- a/source/libs/function/src/builtins.c +++ b/source/libs/function/src/builtins.c @@ -2402,7 +2402,7 @@ const SBuiltinFuncDefinition funcMgtBuiltins[] = { .translateFunc = translateFirstLast, .getEnvFunc = getFirstLastFuncEnv, .initFunc = functionSetup, - .processFunc = lastRowFunction, + .processFunc = cachedLastRowFunction, .finalizeFunc = firstLastFinalize }, { -- GitLab