From b9d4718c64430c591d0b0231955df9395a82920c Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Thu, 20 Oct 2022 14:19:20 +0800 Subject: [PATCH 1/2] fix(query): fix crash if block SMA is set and real data is not loaded. TD-19601 --- source/libs/function/src/builtinsimpl.c | 26 +++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/source/libs/function/src/builtinsimpl.c b/source/libs/function/src/builtinsimpl.c index 874822106e..c843de2b94 100644 --- a/source/libs/function/src/builtinsimpl.c +++ b/source/libs/function/src/builtinsimpl.c @@ -1163,13 +1163,13 @@ static int32_t findRowIndex(int32_t start, int32_t num, SColumnInfoData* pCol, c // the data is loaded, not only the block SMA value for (int32_t i = start; i < num + start; ++i) { char* p = colDataGetData(pCol, i); - if (memcpy((void*)tval, p, pCol->info.bytes) == 0) { + if (memcmp((void*)tval, p, pCol->info.bytes) == 0) { return i; } } - ASSERT(0); - return 0; + // if reach here means real data of block SMA is not set. + return -1; } int32_t doMinMaxHelper(SqlFunctionCtx* pCtx, int32_t isMinFunc) { @@ -1211,7 +1211,9 @@ int32_t doMinMaxHelper(SqlFunctionCtx* pCtx, int32_t isMinFunc) { pBuf->v = *(int64_t*)tval; if (pCtx->subsidiaries.num > 0) { index = findRowIndex(pInput->startRowIndex, pInput->numOfRows, pCol, tval); - pBuf->tuplePos = saveTupleData(pCtx, index, pCtx->pSrcBlock, NULL); + if (index >= 0) { + pBuf->tuplePos = saveTupleData(pCtx, index, pCtx->pSrcBlock, NULL); + } } } else { if (IS_SIGNED_NUMERIC_TYPE(type)) { @@ -1223,7 +1225,9 @@ int32_t doMinMaxHelper(SqlFunctionCtx* pCtx, int32_t isMinFunc) { *(int64_t*)&pBuf->v = val; if (pCtx->subsidiaries.num > 0) { index = findRowIndex(pInput->startRowIndex, pInput->numOfRows, pCol, tval); - pBuf->tuplePos = saveTupleData(pCtx, index, pCtx->pSrcBlock, NULL); + if (index >= 0) { + pBuf->tuplePos = saveTupleData(pCtx, index, pCtx->pSrcBlock, NULL); + } } } } else if (IS_UNSIGNED_NUMERIC_TYPE(type)) { @@ -1235,7 +1239,9 @@ int32_t doMinMaxHelper(SqlFunctionCtx* pCtx, int32_t isMinFunc) { *(uint64_t*)&pBuf->v = val; if (pCtx->subsidiaries.num > 0) { index = findRowIndex(pInput->startRowIndex, pInput->numOfRows, pCol, tval); - pBuf->tuplePos = saveTupleData(pCtx, index, pCtx->pSrcBlock, NULL); + if (index >= 0) { + pBuf->tuplePos = saveTupleData(pCtx, index, pCtx->pSrcBlock, NULL); + } } } } else if (type == TSDB_DATA_TYPE_DOUBLE) { @@ -1247,7 +1253,9 @@ int32_t doMinMaxHelper(SqlFunctionCtx* pCtx, int32_t isMinFunc) { *(double*)&pBuf->v = val; if (pCtx->subsidiaries.num > 0) { index = findRowIndex(pInput->startRowIndex, pInput->numOfRows, pCol, tval); - pBuf->tuplePos = saveTupleData(pCtx, index, pCtx->pSrcBlock, NULL); + if (index >= 0) { + pBuf->tuplePos = saveTupleData(pCtx, index, pCtx->pSrcBlock, NULL); + } } } } else if (type == TSDB_DATA_TYPE_FLOAT) { @@ -1261,7 +1269,9 @@ int32_t doMinMaxHelper(SqlFunctionCtx* pCtx, int32_t isMinFunc) { if (pCtx->subsidiaries.num > 0) { index = findRowIndex(pInput->startRowIndex, pInput->numOfRows, pCol, tval); - pBuf->tuplePos = saveTupleData(pCtx, index, pCtx->pSrcBlock, NULL); + if (index >= 0) { + pBuf->tuplePos = saveTupleData(pCtx, index, pCtx->pSrcBlock, NULL); + } } } } From 4ff9ab7fc259ed6c31b35749aed154046dda9fa9 Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Thu, 20 Oct 2022 14:19:20 +0800 Subject: [PATCH 2/2] fix(query): fix crash if block SMA is set and real data is not loaded. TD-19601 --- source/libs/function/src/builtinsimpl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/libs/function/src/builtinsimpl.c b/source/libs/function/src/builtinsimpl.c index c843de2b94..9085f00b29 100644 --- a/source/libs/function/src/builtinsimpl.c +++ b/source/libs/function/src/builtinsimpl.c @@ -1168,7 +1168,7 @@ static int32_t findRowIndex(int32_t start, int32_t num, SColumnInfoData* pCol, c } } - // if reach here means real data of block SMA is not set. + // if reach here means real data of block SMA is not set in pCtx->input. return -1; }