From 878e54f143d1c20aa28ea3b360b35050f780ed55 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Thu, 31 Mar 2022 22:23:12 +0800 Subject: [PATCH 01/24] fix test error --- source/libs/executor/src/tsort.c | 1 + source/libs/executor/test/sortTests.cpp | 13 +++---------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/source/libs/executor/src/tsort.c b/source/libs/executor/src/tsort.c index 7a57d62969..414f3ded6e 100644 --- a/source/libs/executor/src/tsort.c +++ b/source/libs/executor/src/tsort.c @@ -266,6 +266,7 @@ static int32_t adjustMergeTreeForNextTuple(SExternalMemSource *pSource, SMultiwa pSource->src.rowIndex = 0; if (pHandle->type == SORT_SINGLESOURCE_SORT) { + pSource->pageIndex ++; if (pSource->pageIndex >= taosArrayGetSize(pSource->pageIdList)) { (*numOfCompleted) += 1; pSource->src.rowIndex = -1; diff --git a/source/libs/executor/test/sortTests.cpp b/source/libs/executor/test/sortTests.cpp index ecea24135f..5a7edd816b 100644 --- a/source/libs/executor/test/sortTests.cpp +++ b/source/libs/executor/test/sortTests.cpp @@ -150,7 +150,7 @@ int32_t docomp(const void* p1, const void* p2, void* param) { } } // namespace -#if 0 +#if 1 TEST(testCase, inMem_sort_Test) { SArray* pOrderVal = taosArrayInit(4, sizeof(SOrder)); SOrder o = {.order = TSDB_ORDER_ASC}; @@ -187,20 +187,13 @@ TEST(testCase, inMem_sort_Test) { } TEST(testCase, external_mem_sort_Test) { - SArray* pOrderVal = taosArrayInit(4, sizeof(SOrder)); - SOrder o = {.order = TSDB_ORDER_ASC}; - o.col.info.colId = 1; - o.col.info.type = TSDB_DATA_TYPE_INT; - taosArrayPush(pOrderVal, &o); - SBlockOrderInfo oi = {0}; oi.order = TSDB_ORDER_ASC; - oi.colIndex = 0; + oi.slotId = 0; SArray* orderInfo = taosArrayInit(1, sizeof(SBlockOrderInfo)); taosArrayPush(orderInfo, &oi); - SSchema s = {.type = TSDB_DATA_TYPE_INT, .colId = 1, .bytes = 4, }; - SSortHandle* phandle = tsortCreateSortHandle(orderInfo, false, SORT_SINGLESOURCE_SORT, 1024, 5, &s, 1, "test_abc"); + SSortHandle* phandle = tsortCreateSortHandle(orderInfo, SORT_SINGLESOURCE_SORT, 1024, 5, &s, "test_abc"); tsortSetFetchRawDataFp(phandle, getSingleColDummyBlock); _info* pInfo = (_info*) taosMemoryCalloc(1, sizeof(_info)); From 69a5e55a24a7b8429a2fe3984ea7eee10c5278a1 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Fri, 1 Apr 2022 10:05:16 +0800 Subject: [PATCH 02/24] [TD-14422] fix error in sort logic & add test case --- source/common/src/tdatablock.c | 3 ++ source/libs/executor/src/tsort.c | 5 ++- source/libs/executor/test/sortTests.cpp | 51 ++++++++++++------------- source/util/src/tpagedbuf.c | 7 ++-- 4 files changed, 33 insertions(+), 33 deletions(-) diff --git a/source/common/src/tdatablock.c b/source/common/src/tdatablock.c index 7f25fd1e80..f22051f1c6 100644 --- a/source/common/src/tdatablock.c +++ b/source/common/src/tdatablock.c @@ -1201,6 +1201,9 @@ void* blockDataDestroy(SSDataBlock* pBlock) { } SSDataBlock* createOneDataBlock(const SSDataBlock* pDataBlock) { + if(pDataBlock == NULL){ + return NULL; + } int32_t numOfCols = pDataBlock->info.numOfCols; SSDataBlock* pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock)); diff --git a/source/libs/executor/src/tsort.c b/source/libs/executor/src/tsort.c index 414f3ded6e..0d69ed711c 100644 --- a/source/libs/executor/src/tsort.c +++ b/source/libs/executor/src/tsort.c @@ -148,7 +148,7 @@ static int32_t doAddToBuf(SSDataBlock* pDataBlock, SSortHandle* pHandle) { int32_t start = 0; if (pHandle->pBuf == NULL) { - int32_t code = createDiskbasedBuf(&pHandle->pBuf, pHandle->pageSize, pHandle->numOfPages * pHandle->pageSize, 0, "/tmp"); + int32_t code = createDiskbasedBuf(&pHandle->pBuf, pHandle->pageSize, pHandle->numOfPages * pHandle->pageSize, "doAddToBuf", "/tmp"); dBufSetPrintInfo(pHandle->pBuf); if (code != TSDB_CODE_SUCCESS) { return code; @@ -212,7 +212,7 @@ static int32_t sortComparInit(SMsortComparParam* cmpParam, SArray* pSources, int } else { // multi-pass internal merge sort is required if (pHandle->pBuf == NULL) { - code = createDiskbasedBuf(&pHandle->pBuf, pHandle->pageSize, pHandle->numOfPages * pHandle->pageSize, 0, "/tmp"); + code = createDiskbasedBuf(&pHandle->pBuf, pHandle->pageSize, pHandle->numOfPages * pHandle->pageSize, "sortComparInit", "/tmp"); dBufSetPrintInfo(pHandle->pBuf); if (code != TSDB_CODE_SUCCESS) { return code; @@ -411,6 +411,7 @@ int32_t msortComparFn(const void *pLeft, const void *pRight, void *param) { assert(0); } } + return 0; } static int32_t doInternalMergeSort(SSortHandle* pHandle) { diff --git a/source/libs/executor/test/sortTests.cpp b/source/libs/executor/test/sortTests.cpp index 5a7edd816b..1b64182266 100644 --- a/source/libs/executor/test/sortTests.cpp +++ b/source/libs/executor/test/sortTests.cpp @@ -152,23 +152,24 @@ int32_t docomp(const void* p1, const void* p2, void* param) { #if 1 TEST(testCase, inMem_sort_Test) { - SArray* pOrderVal = taosArrayInit(4, sizeof(SOrder)); - SOrder o = {.order = TSDB_ORDER_ASC}; - o.col.info.colId = 1; - o.col.info.type = TSDB_DATA_TYPE_INT; - taosArrayPush(pOrderVal, &o); - - int32_t numOfRows = 1000; SBlockOrderInfo oi = {0}; oi.order = TSDB_ORDER_ASC; - oi.colIndex = 0; + oi.slotId = 0; SArray* orderInfo = taosArrayInit(1, sizeof(SBlockOrderInfo)); taosArrayPush(orderInfo, &oi); SSchema s = {.type = TSDB_DATA_TYPE_INT, .colId = 1, .bytes = 4, }; - SSortHandle* phandle = tsortCreateSortHandle(orderInfo, false, SORT_SINGLESOURCE_SORT, 1024, 5, &s, 1, "test_abc"); + SSortHandle* phandle = tsortCreateSortHandle(orderInfo, SORT_SINGLESOURCE_SORT, 1024, 5, NULL, "test_abc"); tsortSetFetchRawDataFp(phandle, getSingleColDummyBlock); - tsortAddSource(phandle, &numOfRows); + + _info* pInfo = (_info*) taosMemoryCalloc(1, sizeof(_info)); + pInfo->startVal = 0; + pInfo->pageRows = 100; + pInfo->count = 6; + + SGenericSource* ps = static_cast(taosMemoryCalloc(1, sizeof(SGenericSource))); + ps->param = pInfo; + tsortAddSource(phandle, ps); int32_t code = tsortOpen(phandle); int32_t row = 1; @@ -180,7 +181,8 @@ TEST(testCase, inMem_sort_Test) { } void* v = tsortGetValue(pTupleHandle, 0); - printf("%d: %d\n", row++, *(int32_t*) v); + printf("%d: %d\n", row, *(int32_t*) v); + ASSERT_EQ(row++, *(int32_t*) v); } tsortDestroySortHandle(phandle); @@ -193,13 +195,13 @@ TEST(testCase, external_mem_sort_Test) { SArray* orderInfo = taosArrayInit(1, sizeof(SBlockOrderInfo)); taosArrayPush(orderInfo, &oi); - SSortHandle* phandle = tsortCreateSortHandle(orderInfo, SORT_SINGLESOURCE_SORT, 1024, 5, &s, "test_abc"); + SSortHandle* phandle = tsortCreateSortHandle(orderInfo, SORT_SINGLESOURCE_SORT, 32, 6, NULL, "test_abc"); tsortSetFetchRawDataFp(phandle, getSingleColDummyBlock); _info* pInfo = (_info*) taosMemoryCalloc(1, sizeof(_info)); - pInfo->startVal = 100000; - pInfo->pageRows = 1000; - pInfo->count = 50; + pInfo->startVal = 0; + pInfo->pageRows = 100; + pInfo->count = 6; SGenericSource* ps = static_cast(taosMemoryCalloc(1, sizeof(SGenericSource))); ps->param = pInfo; @@ -216,28 +218,22 @@ TEST(testCase, external_mem_sort_Test) { } void* v = tsortGetValue(pTupleHandle, 0); - printf("%d: %d\n", row++, *(int32_t*) v); + printf("%d: %d\n", row, *(int32_t*) v); + ASSERT_EQ(row++, *(int32_t*) v); } tsortDestroySortHandle(phandle); } TEST(testCase, ordered_merge_sort_Test) { - SArray* pOrderVal = taosArrayInit(4, sizeof(SOrder)); - SOrder o = {.order = TSDB_ORDER_ASC}; - o.col.info.colId = 1; - o.col.info.type = TSDB_DATA_TYPE_INT; - taosArrayPush(pOrderVal, &o); - - int32_t numOfRows = 1000; SBlockOrderInfo oi = {0}; oi.order = TSDB_ORDER_ASC; - oi.colIndex = 0; + oi.slotId = 0; SArray* orderInfo = taosArrayInit(1, sizeof(SBlockOrderInfo)); taosArrayPush(orderInfo, &oi); SSchema s = {.type = TSDB_DATA_TYPE_INT, .colId = 1, .bytes = 4}; - SSortHandle* phandle = tsortCreateSortHandle(orderInfo, false, SORT_MULTISOURCE_MERGE, 1024, 5, &s, 1,"test_abc"); + SSortHandle* phandle = tsortCreateSortHandle(orderInfo, SORT_MULTISOURCE_MERGE, 1024, 5, NULL,"test_abc"); tsortSetFetchRawDataFp(phandle, getSingleColDummyBlock); tsortSetComparFp(phandle, docomp); @@ -246,7 +242,7 @@ TEST(testCase, ordered_merge_sort_Test) { _info* c = static_cast<_info*>(taosMemoryCalloc(1, sizeof(_info))); c->count = 1; c->pageRows = 1000; - c->startVal = 0; + c->startVal = i*1000; p->param = c; tsortAddSource(phandle, p); @@ -262,7 +258,8 @@ TEST(testCase, ordered_merge_sort_Test) { } void* v = tsortGetValue(pTupleHandle, 0); - printf("%d: %d\n", row++, *(int32_t*) v); + printf("%d: %d\n", row, *(int32_t*) v); + ASSERT_EQ(row++, *(int32_t*) v); } tsortDestroySortHandle(phandle); diff --git a/source/util/src/tpagedbuf.c b/source/util/src/tpagedbuf.c index d834263b94..096a16ec94 100644 --- a/source/util/src/tpagedbuf.c +++ b/source/util/src/tpagedbuf.c @@ -138,7 +138,7 @@ static char* doFlushPageToDisk(SDiskbasedBuf* pBuf, SPageInfo* pg) { pBuf->nextPos += size; int32_t ret = taosLSeekFile(pBuf->pFile, pg->offset, SEEK_SET); - if (ret != 0) { + if (ret == -1) { terrno = TAOS_SYSTEM_ERROR(errno); return NULL; } @@ -169,7 +169,7 @@ static char* doFlushPageToDisk(SDiskbasedBuf* pBuf, SPageInfo* pg) { // 3. write to disk. int32_t ret = taosLSeekFile(pBuf->pFile, pg->offset, SEEK_SET); - if (ret != 0) { + if (ret == -1) { terrno = TAOS_SYSTEM_ERROR(errno); return NULL; } @@ -224,7 +224,7 @@ static char* flushPageToDisk(SDiskbasedBuf* pBuf, SPageInfo* pg) { // load file block data in disk static int32_t loadPageFromDisk(SDiskbasedBuf* pBuf, SPageInfo* pg) { int32_t ret = taosLSeekFile(pBuf->pFile, pg->offset, SEEK_SET); - if (ret != 0) { + if (ret == -1) { ret = TAOS_SYSTEM_ERROR(errno); return ret; } @@ -371,7 +371,6 @@ int32_t createDiskbasedBuf(SDiskbasedBuf** pBuf, int32_t pagesize, int32_t inMem pPBuf->totalBufSize = 0; pPBuf->inMemPages = inMemBufSize / pagesize; // maximum allowed pages, it is a soft limit. pPBuf->allocateId = -1; - pPBuf->comp = true; pPBuf->pFile = NULL; pPBuf->id = strdup(id); pPBuf->fileSize = 0; From 12c3a10a9bd06ba87cc0d523180a1d455bc87738 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Sat, 2 Apr 2022 14:29:43 +0800 Subject: [PATCH 03/24] fix error in order by logic & fix no return error --- cmake/cmake.define | 4 +- cmake/cmake.platform | 2 +- include/common/tdatablock.h | 12 ++--- source/common/src/tdatablock.c | 66 ++++++----------------- source/common/src/tmsg.c | 1 + source/dnode/mgmt/dm/src/dmMsg.c | 1 + source/dnode/mgmt/dm/src/dmWorker.c | 1 + source/dnode/mgmt/main/src/dndTransport.c | 1 + source/dnode/mnode/impl/src/mndDnode.c | 1 + source/dnode/vnode/src/tsdb/tsdbSma.c | 5 ++ source/libs/catalog/src/catalog.c | 4 +- source/libs/executor/inc/executil.h | 4 +- source/libs/executor/src/dataDispatcher.c | 1 + source/libs/executor/src/executorimpl.c | 21 ++++++-- source/libs/executor/src/tlinearhash.c | 1 + source/libs/executor/src/tsimplehash.c | 1 + source/libs/executor/src/tsort.c | 63 ++++++++++------------ source/libs/executor/test/sortTests.cpp | 24 +++------ source/libs/index/src/indexFstRegex.c | 1 + source/libs/scalar/src/scalar.c | 1 - source/libs/scalar/src/sclfunc.c | 3 ++ source/libs/scalar/src/sclvector.c | 1 - source/libs/scheduler/src/scheduler.c | 6 ++- source/util/src/tcompare.c | 44 +++++---------- source/util/src/tpagedbuf.c | 3 +- 25 files changed, 116 insertions(+), 156 deletions(-) diff --git a/cmake/cmake.define b/cmake/cmake.define index 9c2d5dc04c..298d102b87 100644 --- a/cmake/cmake.define +++ b/cmake/cmake.define @@ -36,8 +36,8 @@ IF (TD_WINDOWS) ENDIF () ELSE () - SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror -fPIC -gdwarf-2 -g3") - SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -fPIC -gdwarf-2 -g3") + SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror -Werror=return-type -fPIC -gdwarf-2 -g3") + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Werror=return-type -fPIC -gdwarf-2 -g3") MESSAGE("System processor ID: ${CMAKE_SYSTEM_PROCESSOR}") IF (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm64") diff --git a/cmake/cmake.platform b/cmake/cmake.platform index dfb79c0fae..0312f92a5b 100644 --- a/cmake/cmake.platform +++ b/cmake/cmake.platform @@ -42,7 +42,7 @@ IF (${CMAKE_SYSTEM_NAME} MATCHES "Linux" OR ${CMAKE_SYSTEM_NAME} MATCHES "Darwin SET(TD_DARWIN TRUE) SET(OSTYPE "macOS") - ADD_DEFINITIONS("-DDARWIN -Wno-tautological-pointer-compare -Wno-return-type") + ADD_DEFINITIONS("-DDARWIN -Wno-tautological-pointer-compare") MESSAGE("Current system processor is ${CMAKE_SYSTEM_PROCESSOR}.") IF (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm64" OR ${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86_64") diff --git a/include/common/tdatablock.h b/include/common/tdatablock.h index 4bbe42bc50..3afdcfba8e 100644 --- a/include/common/tdatablock.h +++ b/include/common/tdatablock.h @@ -113,39 +113,39 @@ static FORCE_INLINE void colDataAppendNULL(SColumnInfoData* pColumnInfoData, uin pColumnInfoData->hasNull = true; } -static FORCE_INLINE int32_t colDataAppendInt8(SColumnInfoData* pColumnInfoData, uint32_t currentRow, int8_t* v) { +static FORCE_INLINE void colDataAppendInt8(SColumnInfoData* pColumnInfoData, uint32_t currentRow, int8_t* v) { ASSERT(pColumnInfoData->info.type == TSDB_DATA_TYPE_TINYINT || pColumnInfoData->info.type == TSDB_DATA_TYPE_UTINYINT || pColumnInfoData->info.type == TSDB_DATA_TYPE_BOOL); char* p = pColumnInfoData->pData + pColumnInfoData->info.bytes * currentRow; *(int8_t*)p = *(int8_t*)v; } -static FORCE_INLINE int32_t colDataAppendInt16(SColumnInfoData* pColumnInfoData, uint32_t currentRow, int16_t* v) { +static FORCE_INLINE void colDataAppendInt16(SColumnInfoData* pColumnInfoData, uint32_t currentRow, int16_t* v) { ASSERT(pColumnInfoData->info.type == TSDB_DATA_TYPE_SMALLINT || pColumnInfoData->info.type == TSDB_DATA_TYPE_USMALLINT); char* p = pColumnInfoData->pData + pColumnInfoData->info.bytes * currentRow; *(int16_t*)p = *(int16_t*)v; } -static FORCE_INLINE int32_t colDataAppendInt32(SColumnInfoData* pColumnInfoData, uint32_t currentRow, int32_t* v) { +static FORCE_INLINE void colDataAppendInt32(SColumnInfoData* pColumnInfoData, uint32_t currentRow, int32_t* v) { ASSERT(pColumnInfoData->info.type == TSDB_DATA_TYPE_INT || pColumnInfoData->info.type == TSDB_DATA_TYPE_UINT); char* p = pColumnInfoData->pData + pColumnInfoData->info.bytes * currentRow; *(int32_t*)p = *(int32_t*)v; } -static FORCE_INLINE int32_t colDataAppendInt64(SColumnInfoData* pColumnInfoData, uint32_t currentRow, int64_t* v) { +static FORCE_INLINE void colDataAppendInt64(SColumnInfoData* pColumnInfoData, uint32_t currentRow, int64_t* v) { int32_t type = pColumnInfoData->info.type; ASSERT(type == TSDB_DATA_TYPE_BIGINT || type == TSDB_DATA_TYPE_UBIGINT || type == TSDB_DATA_TYPE_TIMESTAMP); char* p = pColumnInfoData->pData + pColumnInfoData->info.bytes * currentRow; *(int64_t*)p = *(int64_t*)v; } -static FORCE_INLINE int32_t colDataAppendFloat(SColumnInfoData* pColumnInfoData, uint32_t currentRow, float* v) { +static FORCE_INLINE void colDataAppendFloat(SColumnInfoData* pColumnInfoData, uint32_t currentRow, float* v) { ASSERT(pColumnInfoData->info.type == TSDB_DATA_TYPE_FLOAT); char* p = pColumnInfoData->pData + pColumnInfoData->info.bytes * currentRow; *(float*)p = *(float*)v; } -static FORCE_INLINE int32_t colDataAppendDouble(SColumnInfoData* pColumnInfoData, uint32_t currentRow, double* v) { +static FORCE_INLINE void colDataAppendDouble(SColumnInfoData* pColumnInfoData, uint32_t currentRow, double* v) { ASSERT(pColumnInfoData->info.type == TSDB_DATA_TYPE_DOUBLE); char* p = pColumnInfoData->pData + pColumnInfoData->info.bytes * currentRow; *(double*)p = *(double*)v; diff --git a/source/common/src/tdatablock.c b/source/common/src/tdatablock.c index f22051f1c6..67cc597fdf 100644 --- a/source/common/src/tdatablock.c +++ b/source/common/src/tdatablock.c @@ -17,6 +17,7 @@ #include "tdatablock.h" #include "tcompare.h" #include "tglobal.h" +#include "tlog.h" int32_t taosGetFqdnPortFromEp(const char* ep, SEp* pEp) { pEp->port = 0; @@ -424,7 +425,7 @@ int32_t blockDataSplitRows(SSDataBlock* pBlock, bool hasVarCol, int32_t startInd } size += sizeof(pColInfoData->varmeta.offset[0]); - } else { + } else { // this block is unreached, because hasVarCol = true size += pColInfoData->info.bytes; if (((j - startIndex) & 0x07) == 0) { @@ -665,23 +666,13 @@ int32_t dataBlockCompar(const void* p1, const void* p2, const void* param) { void* left1 = colDataGetData(pColInfoData, left); void* right1 = colDataGetData(pColInfoData, right); - switch (pColInfoData->info.type) { - case TSDB_DATA_TYPE_INT: { - int32_t leftx = *(int32_t*)left1; - int32_t rightx = *(int32_t*)right1; + __compar_fn_t fn = getKeyComparFunc(pColInfoData->info.type, pOrder->order); - if (leftx == rightx) { - break; - } else { - if (pOrder->order == TSDB_ORDER_ASC) { - return (leftx < rightx) ? -1 : 1; - } else { - return (leftx < rightx) ? 1 : -1; - } - } - } - default: - assert(0); + int ret = fn(left1, right1); + if (ret == 0) { + continue; + } else { + return ret; } } @@ -834,17 +825,14 @@ static void copyBackToBlock(SSDataBlock* pDataBlock, SColumnInfoData* pCols) { if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) { taosMemoryFreeClear(pColInfoData->varmeta.offset); - pColInfoData->varmeta = pCols[i].varmeta; } else { taosMemoryFreeClear(pColInfoData->nullbitmap); - pColInfoData->nullbitmap = pCols[i].nullbitmap; } taosMemoryFreeClear(pColInfoData->pData); - pColInfoData->pData = pCols[i].pData; } - taosMemoryFreeClear(pCols); + colDataDestroy(pCols); } static int32_t* createTupleIndex(size_t rows) { @@ -862,33 +850,6 @@ static int32_t* createTupleIndex(size_t rows) { static void destroyTupleIndex(int32_t* index) { taosMemoryFreeClear(index); } -static __compar_fn_t getComparFn(int32_t type, int32_t order) { - switch (type) { - case TSDB_DATA_TYPE_TINYINT: - return order == TSDB_ORDER_ASC ? compareInt8Val : compareInt8ValDesc; - case TSDB_DATA_TYPE_SMALLINT: - return order == TSDB_ORDER_ASC ? compareInt16Val : compareInt16ValDesc; - case TSDB_DATA_TYPE_INT: - return order == TSDB_ORDER_ASC ? compareInt32Val : compareInt32ValDesc; - case TSDB_DATA_TYPE_BIGINT: - return order == TSDB_ORDER_ASC ? compareInt64Val : compareInt64ValDesc; - case TSDB_DATA_TYPE_FLOAT: - return order == TSDB_ORDER_ASC ? compareFloatVal : compareFloatValDesc; - case TSDB_DATA_TYPE_DOUBLE: - return order == TSDB_ORDER_ASC ? compareDoubleVal : compareDoubleValDesc; - case TSDB_DATA_TYPE_UTINYINT: - return order == TSDB_ORDER_ASC ? compareUint8Val : compareUint8ValDesc; - case TSDB_DATA_TYPE_USMALLINT: - return order == TSDB_ORDER_ASC ? compareUint16Val : compareUint16ValDesc; - case TSDB_DATA_TYPE_UINT: - return order == TSDB_ORDER_ASC ? compareUint32Val : compareUint32ValDesc; - case TSDB_DATA_TYPE_UBIGINT: - return order == TSDB_ORDER_ASC ? compareUint64Val : compareUint64ValDesc; - default: - return order == TSDB_ORDER_ASC ? compareInt32Val : compareInt32ValDesc; - } -} - int32_t blockDataSort(SSDataBlock* pDataBlock, SArray* pOrderInfo) { ASSERT(pDataBlock != NULL && pOrderInfo != NULL); if (pDataBlock->info.rows <= 1) { @@ -922,11 +883,11 @@ int32_t blockDataSort(SSDataBlock* pDataBlock, SArray* pOrderInfo) { int64_t p0 = taosGetTimestampUs(); - __compar_fn_t fn = getComparFn(pColInfoData->info.type, pOrder->order); + __compar_fn_t fn = getKeyComparFunc(pColInfoData->info.type, pOrder->order); qsort(pColInfoData->pData, pDataBlock->info.rows, pColInfoData->info.bytes, fn); int64_t p1 = taosGetTimestampUs(); - printf("sort:%" PRId64 ", rows:%d\n", p1 - p0, pDataBlock->info.rows); + uDebug("blockDataSort easy cost:%" PRId64 ", rows:%d\n", p1 - p0, pDataBlock->info.rows); return TSDB_CODE_SUCCESS; } else { // var data type @@ -955,6 +916,7 @@ int32_t blockDataSort(SSDataBlock* pDataBlock, SArray* pOrderInfo) { SColumnInfoData* pCols = createHelpColInfoData(pDataBlock); if (pCols == NULL) { + destroyTupleIndex(index); terrno = TSDB_CODE_OUT_OF_MEMORY; return terrno; } @@ -963,6 +925,7 @@ int32_t blockDataSort(SSDataBlock* pDataBlock, SArray* pOrderInfo) { int32_t code = blockDataAssign(pCols, pDataBlock, index); if (code != TSDB_CODE_SUCCESS) { + destroyTupleIndex(index); terrno = code; return code; } @@ -972,7 +935,7 @@ int32_t blockDataSort(SSDataBlock* pDataBlock, SArray* pOrderInfo) { copyBackToBlock(pDataBlock, pCols); int64_t p4 = taosGetTimestampUs(); - printf("sort:%" PRId64 ", create:%" PRId64 ", assign:%" PRId64 ", copyback:%" PRId64 ", rows:%d\n", p1 - p0, p2 - p1, + uDebug("blockDataSort complex sort:%" PRId64 ", create:%" PRId64 ", assign:%" PRId64 ", copyback:%" PRId64 ", rows:%d\n", p1 - p0, p2 - p1, p3 - p2, p4 - p3, rows); destroyTupleIndex(index); @@ -1234,6 +1197,7 @@ void colDataDestroy(SColumnInfoData* pColData) { } taosMemoryFree(pColData->pData); + taosMemoryFree(pColData); } int32_t tEncodeDataBlock(void** buf, const SSDataBlock* pBlock) { diff --git a/source/common/src/tmsg.c b/source/common/src/tmsg.c index c85184ffba..7a9014dc43 100644 --- a/source/common/src/tmsg.c +++ b/source/common/src/tmsg.c @@ -2571,6 +2571,7 @@ int32_t tDeserializeSCreateVnodeReq(void *buf, int32_t bufLen, SCreateVnodeReq * int32_t tFreeSCreateVnodeReq(SCreateVnodeReq *pReq) { taosArrayDestroy(pReq->pRetensions); pReq->pRetensions = NULL; + return 0; } int32_t tSerializeSDropVnodeReq(void *buf, int32_t bufLen, SDropVnodeReq *pReq) { diff --git a/source/dnode/mgmt/dm/src/dmMsg.c b/source/dnode/mgmt/dm/src/dmMsg.c index b301ef478b..b08657d4dc 100644 --- a/source/dnode/mgmt/dm/src/dmMsg.c +++ b/source/dnode/mgmt/dm/src/dmMsg.c @@ -95,6 +95,7 @@ int32_t dmProcessStatusRsp(SDnodeMgmt *pMgmt, SNodeMsg *pMsg) { } pMgmt->statusSent = 0; + return TSDB_CODE_SUCCESS; } int32_t dmProcessAuthRsp(SDnodeMgmt *pMgmt, SNodeMsg *pMsg) { diff --git a/source/dnode/mgmt/dm/src/dmWorker.c b/source/dnode/mgmt/dm/src/dmWorker.c index 63b9704b78..ca86f42608 100644 --- a/source/dnode/mgmt/dm/src/dmWorker.c +++ b/source/dnode/mgmt/dm/src/dmWorker.c @@ -50,6 +50,7 @@ static void *dmThreadRoutine(void *param) { lastMonitorTime = curTime; } } + return TSDB_CODE_SUCCESS; } static void dmProcessQueue(SQueueInfo *pInfo, SNodeMsg *pMsg) { diff --git a/source/dnode/mgmt/main/src/dndTransport.c b/source/dnode/mgmt/main/src/dndTransport.c index 07ea0309a8..6398af21dc 100644 --- a/source/dnode/mgmt/main/src/dndTransport.c +++ b/source/dnode/mgmt/main/src/dndTransport.c @@ -333,6 +333,7 @@ int32_t dndSendReqToDnode(SMgmtWrapper *pWrapper, const SEpSet *pEpSet, SRpcMsg taosMsleep(1); } } + return TSDB_CODE_SUCCESS; } int32_t dndSendReqToMnode(SMgmtWrapper *pWrapper, SRpcMsg *pReq) { diff --git a/source/dnode/mnode/impl/src/mndDnode.c b/source/dnode/mnode/impl/src/mndDnode.c index 314e70db9b..4149cb5602 100644 --- a/source/dnode/mnode/impl/src/mndDnode.c +++ b/source/dnode/mnode/impl/src/mndDnode.c @@ -630,6 +630,7 @@ static int32_t mndProcessConfigDnodeReq(SNodeMsg *pReq) { static int32_t mndProcessConfigDnodeRsp(SNodeMsg *pRsp) { mInfo("app:%p config rsp from dnode", pRsp->rpcMsg.ahandle); + return TSDB_CODE_SUCCESS; } static int32_t mndGetConfigMeta(SNodeMsg *pReq, SShowObj *pShow, STableMetaRsp *pMeta) { diff --git a/source/dnode/vnode/src/tsdb/tsdbSma.c b/source/dnode/vnode/src/tsdb/tsdbSma.c index f04f4791b5..9cb444f667 100644 --- a/source/dnode/vnode/src/tsdb/tsdbSma.c +++ b/source/dnode/vnode/src/tsdb/tsdbSma.c @@ -331,6 +331,7 @@ int32_t tsdbDestroySmaState(SSmaStat *pSmaStat) { } taosHashCleanup(pSmaStat->smaStatItems); } + return TSDB_CODE_SUCCESS; } static int32_t tsdbCheckAndInitSmaEnv(STsdb *pTsdb, int8_t smaType) { @@ -433,6 +434,7 @@ static int32_t tsdbSetExpiredWindow(STsdb *pTsdb, SHashObj *pItemsHash, int64_t return TSDB_CODE_FAILED; } tsdbDebug("vgId:%d smaIndex %" PRIi64 " tsKey %" PRIi64 " is put to hash", REPO_ID(pTsdb), indexUid, winSKey); + return TSDB_CODE_SUCCESS; } /** @@ -1051,6 +1053,7 @@ static int32_t tsdbDropTSmaDataImpl(STsdb *pTsdb, int64_t indexUid) { } // clear sma data files // TODO: + return TSDB_CODE_SUCCESS; } static int32_t tsdbSetRSmaDataFile(STSmaWriteH *pSmaH, int32_t fid) { @@ -1173,6 +1176,7 @@ static int32_t tsdbInitTSmaReadH(STSmaReadH *pSmaH, STsdb *pTsdb, int64_t interv pSmaH->interval = tsdbGetIntervalByPrecision(interval, intervalUnit, REPO_CFG(pTsdb)->precision, true); pSmaH->storageLevel = tsdbGetSmaStorageLevel(interval, intervalUnit); pSmaH->days = tsdbGetTSmaDays(pTsdb, pSmaH->interval, pSmaH->storageLevel); + return TSDB_CODE_SUCCESS; } /** @@ -1192,6 +1196,7 @@ static int32_t tsdbInitTSmaFile(STSmaReadH *pSmaH, int64_t indexUid, TSKEY skey) pSmaH->dFile.path = strdup(tSmaFile); pSmaH->smaFsIter.iter = 0; pSmaH->smaFsIter.fid = fid; + return TSDB_CODE_SUCCESS; } /** diff --git a/source/libs/catalog/src/catalog.c b/source/libs/catalog/src/catalog.c index e87fdba71d..0305d045a9 100644 --- a/source/libs/catalog/src/catalog.c +++ b/source/libs/catalog/src/catalog.c @@ -2532,7 +2532,7 @@ _return: } int32_t catalogUpdateVgEpSet(SCatalog* pCtg, const char* dbFName, int32_t vgId, SEpSet *epSet) { - + return 0; } int32_t catalogRemoveTableMeta(SCatalog* pCtg, const SName* pTableName) { @@ -2599,7 +2599,7 @@ _return: } int32_t catalogGetIndexMeta(SCatalog* pCtg, void *pTrans, const SEpSet* pMgmtEps, const SName* pTableName, const char *pIndexName, SIndexMeta** pIndexMeta) { - + return 0; } int32_t catalogGetTableMeta(SCatalog* pCtg, void *pTrans, const SEpSet* pMgmtEps, const SName* pTableName, STableMeta** pTableMeta) { diff --git a/source/libs/executor/inc/executil.h b/source/libs/executor/inc/executil.h index bcbfeb7015..d375299d57 100644 --- a/source/libs/executor/inc/executil.h +++ b/source/libs/executor/inc/executil.h @@ -128,8 +128,8 @@ static FORCE_INLINE char* getPosInResultPage(struct STaskAttr* pQueryAttr, SFile int32_t offset) { assert(rowOffset >= 0 && pQueryAttr != NULL); -// int32_t numOfRows = (int32_t)getRowNumForMultioutput(pQueryAttr, pQueryAttr->topBotQuery, pQueryAttr->stableQuery); -// return ((char *)page->data) + rowOffset + offset * numOfRows; + // int32_t numOfRows = (int32_t)getRowNumForMultioutput(pQueryAttr, pQueryAttr->topBotQuery, pQueryAttr->stableQuery); + return ((char *)page->data); } static FORCE_INLINE char* getPosInResultPage_rv(SFilePage* page, int32_t rowOffset, int32_t offset) { diff --git a/source/libs/executor/src/dataDispatcher.c b/source/libs/executor/src/dataDispatcher.c index 34894c235b..0b03613ab5 100644 --- a/source/libs/executor/src/dataDispatcher.c +++ b/source/libs/executor/src/dataDispatcher.c @@ -236,6 +236,7 @@ static int32_t destroyDataSinker(SDataSinkHandle* pHandle) { } taosCloseQueue(pDispatcher->pDataBlocks); taosThreadMutexDestroy(&pDispatcher->mutex); + return TSDB_CODE_SUCCESS; } int32_t createDataDispatcher(SDataSinkManager* pManager, const SDataSinkNode* pDataSink, DataSinkHandle* pHandle) { diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 2df18f135c..209ffb9101 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -359,6 +359,10 @@ SSDataBlock* createOutputBuf_rv1(SDataBlockDescNode* pNode) { idata.info.precision = pDescNode->dataType.precision; taosArrayPush(pBlock->pDataBlock, &idata); + + if (IS_VAR_DATA_TYPE(idata.info.type)) { + pBlock->info.hasVarCol = true; + } } return pBlock; @@ -4924,6 +4928,7 @@ static SSDataBlock* doBlockInfoScan(SOperatorInfo* pOperator, bool* newgroup) { pOperator->status = OP_EXEC_DONE; return pBlock; #endif + return TSDB_CODE_SUCCESS; } static void doClearBufferedBlocks(SStreamBlockScanInfo* pInfo) { @@ -5000,6 +5005,7 @@ int32_t loadRemoteDataCallback(void* param, const SDataBuf* pMsg, int32_t code) pSourceDataInfo->status = EX_SOURCE_DATA_READY; tsem_post(&pSourceDataInfo->pEx->ready); + return TSDB_CODE_SUCCESS; } static void destroySendMsgInfo(SMsgSendInfo* pMsgBody) { @@ -5673,6 +5679,7 @@ static int32_t loadSysTableContentCb(void* param, const SDataBuf* pMsg, int32_t } tsem_post(&pScanResInfo->ready); + return TSDB_CODE_SUCCESS; } static SSDataBlock* doFilterResult(SSysTableScanInfo* pInfo) { @@ -6484,6 +6491,7 @@ static SSDataBlock* doSort(SOperatorInfo* pOperator, bool* newgroup) { tsortAddSource(pInfo->pSortHandle, ps); int32_t code = tsortOpen(pInfo->pSortHandle); + taosMemoryFreeClear(ps); if (code != TSDB_CODE_SUCCESS) { longjmp(pTaskInfo->env, terrno); } @@ -6496,15 +6504,18 @@ SOperatorInfo* createSortOperatorInfo(SOperatorInfo* downstream, SSDataBlock* pR SExecTaskInfo* pTaskInfo) { SSortOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SSortOperatorInfo)); SOperatorInfo* pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo)); - if (pInfo == NULL || pOperator == NULL) { + int32_t rowSize = pResBlock->info.rowSize; + + if (pInfo == NULL || pOperator == NULL || rowSize > 100 * 1024 * 1024) { taosMemoryFreeClear(pInfo); taosMemoryFreeClear(pOperator); terrno = TSDB_CODE_QRY_OUT_OF_MEMORY; return NULL; } - pInfo->sortBufSize = 1024 * 16; // 1MB, TODO dynamic set the available sort buffer - pInfo->bufPageSize = 1024; + pInfo->bufPageSize = rowSize < 1024 ? 1024 : rowSize; + + pInfo->sortBufSize = pInfo->bufPageSize * 16; // 1MB, TODO dynamic set the available sort buffer pInfo->numOfRowsInRes = 1024; pInfo->pDataBlock = pResBlock; pInfo->pSortInfo = pSortInfo; @@ -7590,6 +7601,7 @@ static int32_t initAggInfo(SOptrBasicInfo* pBasicInfo, SAggSupporter* pAggSup, S pBasicInfo->capacity = numOfRows; doInitAggInfoSup(pAggSup, pBasicInfo->pCtx, numOfCols, pkey); + return TSDB_CODE_SUCCESS; } static STableQueryInfo* initTableQueryInfo(const STableGroupInfo* pTableGroupInfo) { @@ -8389,6 +8401,7 @@ static SSDataBlock* doTagScan(SOperatorInfo* pOperator, bool* newgroup) { return (pRes->info.rows == 0)? NULL:pInfo->pRes; #endif + return TSDB_CODE_SUCCESS; } SOperatorInfo* createTagScanOperatorInfo(STaskRuntimeEnv* pRuntimeEnv, SExprInfo* pExpr, int32_t numOfOutput) { @@ -8906,6 +8919,7 @@ SOperatorInfo* createOperatorTree(SPhysiNode* pPhyNode, SExecTaskInfo* pTaskInfo return createMultiTableAggOperatorInfo(op, pPhyNode->pTargets, pTaskInfo, pTableGroupInfo); } }*/ + return NULL; } static tsdbReaderT createDataReaderImpl(STableScanPhysiNode* pTableScanNode, STableGroupInfo* pGroupInfo, @@ -9217,6 +9231,7 @@ int32_t createQueryFilter(char* data, uint16_t len, SFilterInfo** pFilters) { // tExprTreeDestroy(expr, NULL); // return ret; + return TSDB_CODE_SUCCESS; } // int32_t doCreateFilterInfo(SColumnInfo* pCols, int32_t numOfCols, int32_t numOfFilterCols, SSingleColumnFilterInfo** diff --git a/source/libs/executor/src/tlinearhash.c b/source/libs/executor/src/tlinearhash.c index 1b55467c85..21fd54b620 100644 --- a/source/libs/executor/src/tlinearhash.c +++ b/source/libs/executor/src/tlinearhash.c @@ -409,6 +409,7 @@ char* tHashGet(SLHashObj* pHashObj, const void *key, size_t keyLen) { int32_t tHashRemove(SLHashObj* pHashObj, const void *key, size_t keyLen) { // todo + return TSDB_CODE_SUCCESS; } void tHashPrint(const SLHashObj* pHashObj, int32_t type) { diff --git a/source/libs/executor/src/tsimplehash.c b/source/libs/executor/src/tsimplehash.c index 981da0415e..eb6a5dcabe 100644 --- a/source/libs/executor/src/tsimplehash.c +++ b/source/libs/executor/src/tsimplehash.c @@ -258,6 +258,7 @@ void *tSimpleHashGet(SSHashObj *pHashObj, const void *key) { int32_t tSimpleHashRemove(SSHashObj *pHashObj, const void *key) { // todo + return TSDB_CODE_SUCCESS; } void tSimpleHashClear(SSHashObj *pHashObj) { diff --git a/source/libs/executor/src/tsort.c b/source/libs/executor/src/tsort.c index 0d69ed711c..343efb5fbf 100644 --- a/source/libs/executor/src/tsort.c +++ b/source/libs/executor/src/tsort.c @@ -22,6 +22,7 @@ #include "tpagedbuf.h" #include "tsort.h" #include "tutil.h" +#include "tcompare.h" struct STupleHandle { SSDataBlock* pBlock; @@ -123,6 +124,7 @@ void tsortDestroySortHandle(SSortHandle* pSortHandle) { int32_t tsortAddSource(SSortHandle* pSortHandle, void* pSource) { taosArrayPush(pSortHandle->pOrderedSource, &pSource); + return TSDB_CODE_SUCCESS; } static int32_t doAddNewExternalMemSource(SDiskbasedBuf *pBuf, SArray* pAllSources, SSDataBlock* pBlock, int32_t* sourceId) { @@ -164,7 +166,7 @@ static int32_t doAddToBuf(SSDataBlock* pDataBlock, SSortHandle* pHandle) { } int32_t pageId = -1; - SFilePage* pPage = getNewBufPage(pHandle->pBuf, pHandle->sourceId, &pageId); + void* pPage = getNewBufPage(pHandle->pBuf, pHandle->sourceId, &pageId); if (pPage == NULL) { return terrno; } @@ -172,7 +174,7 @@ static int32_t doAddToBuf(SSDataBlock* pDataBlock, SSortHandle* pHandle) { int32_t size = blockDataGetSize(p) + sizeof(int32_t) + p->info.numOfCols * sizeof(int32_t); assert(size <= getBufPageSize(pHandle->pBuf)); - blockDataToBuf(pPage->data, p); + blockDataToBuf(pPage, p); setBufPageDirty(pPage, true); releaseBufPage(pHandle->pBuf, pPage); @@ -184,10 +186,7 @@ static int32_t doAddToBuf(SSDataBlock* pDataBlock, SSortHandle* pHandle) { blockDataCleanup(pDataBlock); SSDataBlock* pBlock = createOneDataBlock(pDataBlock); - int32_t code = doAddNewExternalMemSource(pHandle->pBuf, pHandle->pOrderedSource, pBlock, &pHandle->sourceId); - if (code != TSDB_CODE_SUCCESS) { - return code; - } + return doAddNewExternalMemSource(pHandle->pBuf, pHandle->pOrderedSource, pBlock, &pHandle->sourceId); } static int32_t sortComparInit(SMsortComparParam* cmpParam, SArray* pSources, int32_t startIndex, int32_t endIndex, SSortHandle* pHandle) { @@ -201,8 +200,8 @@ static int32_t sortComparInit(SMsortComparParam* cmpParam, SArray* pSources, int SExternalMemSource* pSource = cmpParam->pSources[i]; SPageInfo* pPgInfo = *(SPageInfo**)taosArrayGet(pSource->pageIdList, pSource->pageIndex); - SFilePage* pPage = getBufPage(pHandle->pBuf, getPageId(pPgInfo)); - code = blockDataFromBuf(pSource->src.pBlock, pPage->data); + void* pPage = getBufPage(pHandle->pBuf, getPageId(pPgInfo)); + code = blockDataFromBuf(pSource->src.pBlock, pPage); if (code != TSDB_CODE_SUCCESS) { return code; } @@ -236,6 +235,7 @@ static int32_t sortComparClearup(SMsortComparParam* cmpParam) { } cmpParam->numOfSources = 0; + return TSDB_CODE_SUCCESS; } static void appendOneRowToDataBlock(SSDataBlock *pBlock, const SSDataBlock* pSource, int32_t* rowIndex) { @@ -309,6 +309,7 @@ static int32_t adjustMergeTreeForNextTuple(SExternalMemSource *pSource, SMultiwa printf("\nafter adjust:\t"); tMergeTreePrint(pTree); #endif + return TSDB_CODE_SUCCESS; } static SSDataBlock* getSortedBlockData(SSortHandle* pHandle, SMsortComparParam* cmpParam, int32_t capacity) { @@ -392,23 +393,13 @@ int32_t msortComparFn(const void *pLeft, const void *pRight, void *param) { void* left1 = colDataGetData(pLeftColInfoData, pLeftSource->src.rowIndex); void* right1 = colDataGetData(pRightColInfoData, pRightSource->src.rowIndex); - switch(pLeftColInfoData->info.type) { - case TSDB_DATA_TYPE_INT: { - int32_t leftv = *(int32_t*)left1; - int32_t rightv = *(int32_t*)right1; + __compar_fn_t fn = getKeyComparFunc(pLeftColInfoData->info.type, pOrder->order); - if (leftv == rightv) { - break; - } else { - if (pOrder->order == TSDB_ORDER_ASC) { - return leftv < rightv? -1 : 1; - } else { - return leftv < rightv? 1 : -1; - } - } - } - default: - assert(0); + int ret = fn(left1, right1); + if (ret == 0) { + continue; + } else { + return ret; } } return 0; @@ -424,13 +415,13 @@ static int32_t doInternalMergeSort(SSortHandle* pHandle) { double sortPass = floorl(log2(numOfSources) / log2(pHandle->numOfPages)); pHandle->totalElapsed = taosGetTimestampUs() - pHandle->startTs; - qDebug("%s %d rounds mergesort required to complete the sort, first-round sorted data size:%"PRIzu", sort:%"PRId64", total elapsed:%"PRId64, + qDebug("%s %d rounds mergesort required to complete the sort, first-round sorted data size:%"PRIzu", sort elapsed:%"PRId64", total elapsed:%"PRId64, pHandle->idStr, (int32_t) (sortPass + 1), getTotalBufSize(pHandle->pBuf), pHandle->sortElapsed, pHandle->totalElapsed); size_t pgSize = pHandle->pageSize; int32_t numOfRows = (pgSize - blockDataGetSerialMetaSize(pHandle->pDataBlock))/ blockDataGetSerialRowSize(pHandle->pDataBlock); - blockDataEnsureCapacity(pHandle->pDataBlock, numOfRows); + // blockDataEnsureCapacity(pHandle->pDataBlock, numOfRows); // useless, it is already enough size_t numOfSorted = taosArrayGetSize(pHandle->pOrderedSource); for(int32_t t = 0; t < sortPass; ++t) { @@ -469,7 +460,7 @@ static int32_t doInternalMergeSort(SSortHandle* pHandle) { } int32_t pageId = -1; - SFilePage* pPage = getNewBufPage(pHandle->pBuf, pHandle->sourceId, &pageId); + void* pPage = getNewBufPage(pHandle->pBuf, pHandle->sourceId, &pageId); if (pPage == NULL) { return terrno; } @@ -477,7 +468,7 @@ static int32_t doInternalMergeSort(SSortHandle* pHandle) { int32_t size = blockDataGetSize(pDataBlock) + sizeof(int32_t) + pDataBlock->info.numOfCols * sizeof(int32_t); assert(size <= getBufPageSize(pHandle->pBuf)); - blockDataToBuf(pPage->data, pDataBlock); + blockDataToBuf(pPage, pDataBlock); setBufPageDirty(pPage, true); releaseBufPage(pHandle->pBuf, pPage); @@ -526,7 +517,6 @@ static int32_t createInitialSortedMultiSources(SSortHandle* pHandle) { if (pHandle->type == SORT_SINGLESOURCE_SORT) { SGenericSource* source = taosArrayGetP(pHandle->pOrderedSource, 0); taosArrayClear(pHandle->pOrderedSource); - while (1) { SSDataBlock* pBlock = pHandle->fetchfp(source->param); if (pBlock == NULL) { @@ -559,8 +549,13 @@ static int32_t createInitialSortedMultiSources(SSortHandle* pHandle) { size_t size = blockDataGetSize(pHandle->pDataBlock); // Perform the in-memory sort and then flush data in the buffer into disk. + int64_t p = taosGetTimestampUs(); + blockDataSort(pHandle->pDataBlock, pHandle->pSortInfo); + int64_t el = taosGetTimestampUs() - p; + pHandle->sortElapsed += el; + // All sorted data can fit in memory, external memory sort is not needed. Return to directly if (size <= sortBufSize) { pHandle->cmpParam.numOfSources = 1; @@ -573,8 +568,6 @@ static int32_t createInitialSortedMultiSources(SSortHandle* pHandle) { doAddToBuf(pHandle->pDataBlock, pHandle); } } - - taosMemoryFreeClear(source); } return TSDB_CODE_SUCCESS; @@ -616,22 +609,22 @@ int32_t tsortOpen(SSortHandle* pHandle) { return code; } - code = tMergeTreeCreate(&pHandle->pMergeTree, pHandle->cmpParam.numOfSources, &pHandle->cmpParam, pHandle->comparFn); - if (code != TSDB_CODE_SUCCESS) { - return code; - } + return tMergeTreeCreate(&pHandle->pMergeTree, pHandle->cmpParam.numOfSources, &pHandle->cmpParam, pHandle->comparFn); } int32_t tsortClose(SSortHandle* pHandle) { // do nothing + return TSDB_CODE_SUCCESS; } int32_t tsortSetFetchRawDataFp(SSortHandle* pHandle, _sort_fetch_block_fn_t fp) { pHandle->fetchfp = fp; + return TSDB_CODE_SUCCESS; } int32_t tsortSetComparFp(SSortHandle* pHandle, _sort_merge_compar_fn_t fp) { pHandle->comparFn = fp; + return TSDB_CODE_SUCCESS; } STupleHandle* tsortNextTuple(SSortHandle* pHandle) { diff --git a/source/libs/executor/test/sortTests.cpp b/source/libs/executor/test/sortTests.cpp index 1b64182266..3bff67eb69 100644 --- a/source/libs/executor/test/sortTests.cpp +++ b/source/libs/executor/test/sortTests.cpp @@ -33,6 +33,7 @@ #include "tdef.h" #include "trpc.h" #include "tvariant.h" +#include "tcompare.h" namespace { typedef struct { @@ -125,24 +126,13 @@ int32_t docomp(const void* p1, const void* p2, void* param) { void* left1 = colDataGetData(pLeftColInfoData, pLeftSource->src.rowIndex); void* right1 = colDataGetData(pRightColInfoData, pRightSource->src.rowIndex); + __compar_fn_t fn = getKeyComparFunc(pLeftColInfoData->info.type, pOrder->order); - switch(pLeftColInfoData->info.type) { - case TSDB_DATA_TYPE_INT: { - int32_t leftv = *(int32_t*)left1; - int32_t rightv = *(int32_t*)right1; - - if (leftv == rightv) { - break; - } else { - if (pOrder->order == TSDB_ORDER_ASC) { - return leftv < rightv? -1 : 1; - } else { - return leftv < rightv? 1 : -1; - } - } - } - default: - assert(0); + int ret = fn(left1, right1); + if (ret == 0) { + continue; + } else { + return ret; } } diff --git a/source/libs/index/src/indexFstRegex.c b/source/libs/index/src/indexFstRegex.c index ec41a7f58e..e89c94079e 100644 --- a/source/libs/index/src/indexFstRegex.c +++ b/source/libs/index/src/indexFstRegex.c @@ -26,6 +26,7 @@ FstRegex *regexCreate(const char *str) { memcpy(orig, str, sz); regex->orig = orig; + return regex; } void regexSetup(FstRegex *regex, uint32_t size, const char *str) { diff --git a/source/libs/scalar/src/scalar.c b/source/libs/scalar/src/scalar.c index 1cc259d4da..dd3edcf1a3 100644 --- a/source/libs/scalar/src/scalar.c +++ b/source/libs/scalar/src/scalar.c @@ -132,7 +132,6 @@ void sclFreeRes(SHashObj *res) { void sclFreeParam(SScalarParam *param) { if (param->columnData != NULL) { colDataDestroy(param->columnData); - taosMemoryFreeClear(param->columnData); } if (param->pHashFilter != NULL) { diff --git a/source/libs/scalar/src/sclfunc.c b/source/libs/scalar/src/sclfunc.c index d8e97e7e12..0486049296 100644 --- a/source/libs/scalar/src/sclfunc.c +++ b/source/libs/scalar/src/sclfunc.c @@ -385,16 +385,19 @@ bool getTimePseudoFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv int32_t qStartTsFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) { ASSERT(inputNum == 1); colDataAppendInt64(pOutput->columnData, pOutput->numOfRows, (int64_t *)colDataGetData(pInput->columnData, 0)); + return TSDB_CODE_SUCCESS; } int32_t qEndTsFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) { ASSERT(inputNum == 1); colDataAppendInt64(pOutput->columnData, pOutput->numOfRows, (int64_t *)colDataGetData(pInput->columnData, 1)); + return TSDB_CODE_SUCCESS; } int32_t winDurFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) { ASSERT(inputNum == 1); colDataAppendInt64(pOutput->columnData, pOutput->numOfRows, (int64_t *)colDataGetData(pInput->columnData, 2)); + return TSDB_CODE_SUCCESS; } int32_t winStartTsFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) { diff --git a/source/libs/scalar/src/sclvector.c b/source/libs/scalar/src/sclvector.c index f22f9a5c3c..d08352602d 100644 --- a/source/libs/scalar/src/sclvector.c +++ b/source/libs/scalar/src/sclvector.c @@ -476,7 +476,6 @@ static SColumnInfoData* doVectorConvert(SScalarParam* pInput, int32_t* doConvert static void doReleaseVec(SColumnInfoData* pCol, int32_t type) { if (type == VECTOR_DO_CONVERT) { colDataDestroy(pCol); - taosMemoryFree(pCol); } } diff --git a/source/libs/scheduler/src/scheduler.c b/source/libs/scheduler/src/scheduler.c index 5af13d97ca..86eaa09594 100644 --- a/source/libs/scheduler/src/scheduler.c +++ b/source/libs/scheduler/src/scheduler.c @@ -258,6 +258,7 @@ _return: SCH_JOB_ELOG("invalid job status update, from %s to %s", jobTaskStatusStr(oriStatus), jobTaskStatusStr(newStatus)); SCH_ERR_RET(code); + return TSDB_CODE_SUCCESS; } int32_t schBuildTaskRalation(SSchJob *pJob, SHashObj *planToTask) { @@ -791,7 +792,7 @@ _return: SCH_RET(schProcessOnJobFailure(pJob, code)); } -int32_t schProcessOnDataFetched(SSchJob *job) { +void schProcessOnDataFetched(SSchJob *job) { atomic_val_compare_exchange_32(&job->remoteFetch, 1, 0); tsem_post(&job->rspSem); } @@ -1150,6 +1151,7 @@ int32_t schHandleReadyCallback(void *param, const SDataBuf *pMsg, int32_t code) int32_t schHandleDropCallback(void *param, const SDataBuf *pMsg, int32_t code) { SSchTaskCallbackParam *pParam = (SSchTaskCallbackParam *)param; qDebug("QID:%" PRIx64 ",TID:%" PRIx64 " drop task rsp received, code:%x", pParam->queryId, pParam->taskId, code); + return TSDB_CODE_SUCCESS; } int32_t schHandleHbCallback(void *param, const SDataBuf *pMsg, int32_t code) { @@ -2029,7 +2031,7 @@ void schDropJobAllTasks(SSchJob *pJob) { int32_t schCancelJob(SSchJob *pJob) { // TODO - + return TSDB_CODE_SUCCESS; // TODO MOVE ALL TASKS FROM EXEC LIST TO FAIL LIST } diff --git a/source/util/src/tcompare.c b/source/util/src/tcompare.c index c98f6eb9be..d4abcb962a 100644 --- a/source/util/src/tcompare.c +++ b/source/util/src/tcompare.c @@ -436,7 +436,6 @@ int32_t compareWStrPatternMatch(const void *pLeft, const void *pRight) { int32_t compareWStrPatternNotMatch(const void *pLeft, const void *pRight) { return compareWStrPatternMatch(pLeft, pRight) ? 0 : 1; } - __compar_fn_t getComparFunc(int32_t type, int32_t optr) { __compar_fn_t comparFn = NULL; @@ -568,53 +567,36 @@ __compar_fn_t getComparFunc(int32_t type, int32_t optr) { } __compar_fn_t getKeyComparFunc(int32_t keyType, int32_t order) { - __compar_fn_t comparFn = NULL; - switch (keyType) { case TSDB_DATA_TYPE_TINYINT: case TSDB_DATA_TYPE_BOOL: - comparFn = (order == TSDB_ORDER_ASC) ? compareInt8Val : compareInt8ValDesc; - break; + return (order == TSDB_ORDER_ASC) ? compareInt8Val : compareInt8ValDesc; case TSDB_DATA_TYPE_SMALLINT: - comparFn = (order == TSDB_ORDER_ASC) ? compareInt16Val : compareInt16ValDesc; - break; + return (order == TSDB_ORDER_ASC) ? compareInt16Val : compareInt16ValDesc; case TSDB_DATA_TYPE_INT: - comparFn = (order == TSDB_ORDER_ASC) ? compareInt32Val : compareInt32ValDesc; - break; + return (order == TSDB_ORDER_ASC) ? compareInt32Val : compareInt32ValDesc; case TSDB_DATA_TYPE_BIGINT: case TSDB_DATA_TYPE_TIMESTAMP: - comparFn = (order == TSDB_ORDER_ASC) ? compareInt64Val : compareInt64ValDesc; - break; + return (order == TSDB_ORDER_ASC) ? compareInt64Val : compareInt64ValDesc; case TSDB_DATA_TYPE_FLOAT: - comparFn = (order == TSDB_ORDER_ASC) ? compareFloatVal : compareFloatValDesc; - break; + return (order == TSDB_ORDER_ASC) ? compareFloatVal : compareFloatValDesc; case TSDB_DATA_TYPE_DOUBLE: - comparFn = (order == TSDB_ORDER_ASC) ? compareDoubleVal : compareDoubleValDesc; - break; + return (order == TSDB_ORDER_ASC) ? compareDoubleVal : compareDoubleValDesc; case TSDB_DATA_TYPE_UTINYINT: - comparFn = (order == TSDB_ORDER_ASC) ? compareUint8Val : compareUint8ValDesc; - break; + return (order == TSDB_ORDER_ASC) ? compareUint8Val : compareUint8ValDesc; case TSDB_DATA_TYPE_USMALLINT: - comparFn = (order == TSDB_ORDER_ASC) ? compareUint16Val : compareUint16ValDesc; - break; + return (order == TSDB_ORDER_ASC) ? compareUint16Val : compareUint16ValDesc; case TSDB_DATA_TYPE_UINT: - comparFn = (order == TSDB_ORDER_ASC) ? compareUint32Val : compareUint32ValDesc; - break; + return (order == TSDB_ORDER_ASC) ? compareUint32Val : compareUint32ValDesc; case TSDB_DATA_TYPE_UBIGINT: - comparFn = (order == TSDB_ORDER_ASC) ? compareUint64Val : compareUint64ValDesc; - break; + return (order == TSDB_ORDER_ASC) ? compareUint64Val : compareUint64ValDesc; case TSDB_DATA_TYPE_BINARY: - comparFn = (order == TSDB_ORDER_ASC) ? compareLenPrefixedStr : compareLenPrefixedStrDesc; - break; + return (order == TSDB_ORDER_ASC) ? compareLenPrefixedStr : compareLenPrefixedStrDesc; case TSDB_DATA_TYPE_NCHAR: - comparFn = (order == TSDB_ORDER_ASC) ? compareLenPrefixedWStr : compareLenPrefixedWStrDesc; - break; + return (order == TSDB_ORDER_ASC) ? compareLenPrefixedWStr : compareLenPrefixedWStrDesc; default: - comparFn = (order == TSDB_ORDER_ASC) ? compareInt32Val : compareInt32ValDesc; - break; + return (order == TSDB_ORDER_ASC) ? compareInt32Val : compareInt32ValDesc; } - - return comparFn; } int32_t doCompare(const char *f1, const char *f2, int32_t type, size_t size) { diff --git a/source/util/src/tpagedbuf.c b/source/util/src/tpagedbuf.c index 096a16ec94..e6bc2787e8 100644 --- a/source/util/src/tpagedbuf.c +++ b/source/util/src/tpagedbuf.c @@ -350,8 +350,7 @@ static void lruListMoveToFront(SList* pList, SPageInfo* pi) { } static SPageInfo* getPageInfoFromPayload(void* page) { - int32_t offset = offsetof(SPageInfo, pData); - char* p = (char *)page - offset; + char* p = (char *)page - POINTER_BYTES; SPageInfo* ppi = ((SPageInfo**)p)[0]; return ppi; From eba69fdb917e541cb08e133d064e818784f4f2db Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Sat, 2 Apr 2022 16:03:48 +0800 Subject: [PATCH 04/24] fix error in order by logic --- source/libs/executor/src/tsort.c | 4 +- source/libs/executor/test/sortTests.cpp | 52 +++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/source/libs/executor/src/tsort.c b/source/libs/executor/src/tsort.c index 343efb5fbf..ba95aa0a47 100644 --- a/source/libs/executor/src/tsort.c +++ b/source/libs/executor/src/tsort.c @@ -275,8 +275,8 @@ static int32_t adjustMergeTreeForNextTuple(SExternalMemSource *pSource, SMultiwa } else { SPageInfo* pPgInfo = *(SPageInfo**)taosArrayGet(pSource->pageIdList, pSource->pageIndex); - SFilePage* pPage = getBufPage(pHandle->pBuf, getPageId(pPgInfo)); - int32_t code = blockDataFromBuf(pSource->src.pBlock, pPage->data); + void* pPage = getBufPage(pHandle->pBuf, getPageId(pPgInfo)); + int32_t code = blockDataFromBuf(pSource->src.pBlock, pPage); if (code != TSDB_CODE_SUCCESS) { return code; } diff --git a/source/libs/executor/test/sortTests.cpp b/source/libs/executor/test/sortTests.cpp index 3bff67eb69..73808f86b2 100644 --- a/source/libs/executor/test/sortTests.cpp +++ b/source/libs/executor/test/sortTests.cpp @@ -72,6 +72,39 @@ SSDataBlock* getSingleColDummyBlock(void* param) { return pBlock; } +SSDataBlock* getSingleColStrBlock(void* param) { + _info* pInfo = (_info*) param; + if (--pInfo->count < 0) { + return NULL; + } + + SSDataBlock* pBlock = static_cast(taosMemoryCalloc(1, sizeof(SSDataBlock))); + pBlock->pDataBlock = taosArrayInit(4, sizeof(SColumnInfoData)); + + SColumnInfoData colInfo = {0}; + colInfo.info.type = TSDB_DATA_TYPE_NCHAR; + colInfo.info.bytes = TSDB_NCHAR_SIZE * 32; + colInfo.info.colId = 1; + colInfo.varmeta.offset = static_cast(taosMemoryCalloc(pInfo->pageRows, sizeof(int32_t))); + + taosArrayPush(pBlock->pDataBlock, &colInfo); + + for (int32_t i = 0; i < pInfo->pageRows; ++i) { + SColumnInfoData* pColInfo = static_cast(TARRAY_GET_ELEM(pBlock->pDataBlock, 0)); + + int32_t size = taosRand() % 32; + char str[64] = {0}; + taosRandStr(varDataVal(str), size); + varDataSetLen(str, size); + colDataAppend(pColInfo, i, reinterpret_cast(str), false); + } + + pBlock->info.rows = pInfo->pageRows; + pBlock->info.numOfCols = 1; + return pBlock; +} + + int32_t docomp(const void* p1, const void* p2, void* param) { int32_t pLeftIdx = *(int32_t *)p1; int32_t pRightIdx = *(int32_t *)p2; @@ -210,7 +243,9 @@ TEST(testCase, external_mem_sort_Test) { void* v = tsortGetValue(pTupleHandle, 0); printf("%d: %d\n", row, *(int32_t*) v); ASSERT_EQ(row++, *(int32_t*) v); - + //char buf[64] = {0}; + //snprintf(buf, varDataLen(v), "%s", varDataVal(v)); + //printf("%d: %s\n", row, buf); } tsortDestroySortHandle(phandle); } @@ -222,8 +257,18 @@ TEST(testCase, ordered_merge_sort_Test) { SArray* orderInfo = taosArrayInit(1, sizeof(SBlockOrderInfo)); taosArrayPush(orderInfo, &oi); - SSchema s = {.type = TSDB_DATA_TYPE_INT, .colId = 1, .bytes = 4}; - SSortHandle* phandle = tsortCreateSortHandle(orderInfo, SORT_MULTISOURCE_MERGE, 1024, 5, NULL,"test_abc"); + SSDataBlock* pBlock = static_cast(taosMemoryCalloc(1, sizeof(SSDataBlock))); + pBlock->pDataBlock = taosArrayInit(1, sizeof(SColumnInfoData)); + pBlock->info.numOfCols = 1; + for (int32_t i = 0; i < pBlock->info.numOfCols; ++i) { + SColumnInfoData colInfo = {0}; + colInfo.info.type = TSDB_DATA_TYPE_INT; + colInfo.info.bytes = sizeof(int32_t); + colInfo.info.colId = 1; + taosArrayPush(pBlock->pDataBlock, &colInfo); + } + + SSortHandle* phandle = tsortCreateSortHandle(orderInfo, SORT_MULTISOURCE_MERGE, 1024, 5, pBlock,"test_abc"); tsortSetFetchRawDataFp(phandle, getSingleColDummyBlock); tsortSetComparFp(phandle, docomp); @@ -253,6 +298,7 @@ TEST(testCase, ordered_merge_sort_Test) { } tsortDestroySortHandle(phandle); + taosMemoryFree(pBlock); } #endif From 155b14388b622ad09a68737ae852806040b664b5 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Sat, 2 Apr 2022 16:31:19 +0800 Subject: [PATCH 05/24] fix error in order by logic --- source/common/src/tdatablock.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/source/common/src/tdatablock.c b/source/common/src/tdatablock.c index 67cc597fdf..67f4f251f2 100644 --- a/source/common/src/tdatablock.c +++ b/source/common/src/tdatablock.c @@ -825,14 +825,17 @@ static void copyBackToBlock(SSDataBlock* pDataBlock, SColumnInfoData* pCols) { if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) { taosMemoryFreeClear(pColInfoData->varmeta.offset); + pColInfoData->varmeta = pCols[i].varmeta; } else { taosMemoryFreeClear(pColInfoData->nullbitmap); + pColInfoData->nullbitmap = pCols[i].nullbitmap; } taosMemoryFreeClear(pColInfoData->pData); + pColInfoData->pData = pCols[i].pData; } - colDataDestroy(pCols); + taosMemoryFreeClear(pCols); } static int32_t* createTupleIndex(size_t rows) { @@ -1197,7 +1200,6 @@ void colDataDestroy(SColumnInfoData* pColData) { } taosMemoryFree(pColData->pData); - taosMemoryFree(pColData); } int32_t tEncodeDataBlock(void** buf, const SSDataBlock* pBlock) { From e7585db216d9a3d4d39b6b12ea5e4b6c56581d84 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Sat, 2 Apr 2022 18:52:41 +0800 Subject: [PATCH 06/24] fix error in order by logic --- source/libs/executor/src/executorimpl.c | 4 ++-- source/libs/executor/src/tsort.c | 6 +++--- source/libs/executor/test/sortTests.cpp | 8 +++++--- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 209ffb9101..f371103490 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -6513,9 +6513,9 @@ SOperatorInfo* createSortOperatorInfo(SOperatorInfo* downstream, SSDataBlock* pR return NULL; } - pInfo->bufPageSize = rowSize < 1024 ? 1024 : rowSize; + pInfo->bufPageSize = rowSize < 1024 ? 1024 : rowSize*2; - pInfo->sortBufSize = pInfo->bufPageSize * 16; // 1MB, TODO dynamic set the available sort buffer + pInfo->sortBufSize = pInfo->bufPageSize * 16; // TODO dynamic set the available sort buffer pInfo->numOfRowsInRes = 1024; pInfo->pDataBlock = pResBlock; pInfo->pSortInfo = pSortInfo; diff --git a/source/libs/executor/src/tsort.c b/source/libs/executor/src/tsort.c index ba95aa0a47..cc7416bc87 100644 --- a/source/libs/executor/src/tsort.c +++ b/source/libs/executor/src/tsort.c @@ -141,8 +141,8 @@ static int32_t doAddNewExternalMemSource(SDiskbasedBuf *pBuf, SArray* pAllSource (*sourceId) += 1; int32_t rowSize = blockDataGetSerialRowSize(pSource->src.pBlock); - int32_t numOfRows = (getBufPageSize(pBuf) - blockDataGetSerialMetaSize(pBlock))/rowSize; - + int32_t numOfRows = (getBufPageSize(pBuf) - blockDataGetSerialMetaSize(pBlock))/rowSize; // The value of numOfRows must be greater than 0, which is guaranteed by the previous memory allocation + ASSERT(numOfRows > 0); return blockDataEnsureCapacity(pSource->src.pBlock, numOfRows); } @@ -421,7 +421,7 @@ static int32_t doInternalMergeSort(SSortHandle* pHandle) { size_t pgSize = pHandle->pageSize; int32_t numOfRows = (pgSize - blockDataGetSerialMetaSize(pHandle->pDataBlock))/ blockDataGetSerialRowSize(pHandle->pDataBlock); - // blockDataEnsureCapacity(pHandle->pDataBlock, numOfRows); // useless, it is already enough + blockDataEnsureCapacity(pHandle->pDataBlock, numOfRows); // useless, it is already enough size_t numOfSorted = taosArrayGetSize(pHandle->pOrderedSource); for(int32_t t = 0; t < sortPass; ++t) { diff --git a/source/libs/executor/test/sortTests.cpp b/source/libs/executor/test/sortTests.cpp index 73808f86b2..229b84b923 100644 --- a/source/libs/executor/test/sortTests.cpp +++ b/source/libs/executor/test/sortTests.cpp @@ -83,7 +83,7 @@ SSDataBlock* getSingleColStrBlock(void* param) { SColumnInfoData colInfo = {0}; colInfo.info.type = TSDB_DATA_TYPE_NCHAR; - colInfo.info.bytes = TSDB_NCHAR_SIZE * 32; + colInfo.info.bytes = TSDB_NCHAR_SIZE * 16; colInfo.info.colId = 1; colInfo.varmeta.offset = static_cast(taosMemoryCalloc(pInfo->pageRows, sizeof(int32_t))); @@ -92,7 +92,7 @@ SSDataBlock* getSingleColStrBlock(void* param) { for (int32_t i = 0; i < pInfo->pageRows; ++i) { SColumnInfoData* pColInfo = static_cast(TARRAY_GET_ELEM(pBlock->pDataBlock, 0)); - int32_t size = taosRand() % 32; + int32_t size = taosRand() % 16; char str[64] = {0}; taosRandStr(varDataVal(str), size); varDataSetLen(str, size); @@ -101,6 +101,8 @@ SSDataBlock* getSingleColStrBlock(void* param) { pBlock->info.rows = pInfo->pageRows; pBlock->info.numOfCols = 1; + pBlock->info.hasVarCol = true; + return pBlock; } @@ -218,7 +220,7 @@ TEST(testCase, external_mem_sort_Test) { SArray* orderInfo = taosArrayInit(1, sizeof(SBlockOrderInfo)); taosArrayPush(orderInfo, &oi); - SSortHandle* phandle = tsortCreateSortHandle(orderInfo, SORT_SINGLESOURCE_SORT, 32, 6, NULL, "test_abc"); + SSortHandle* phandle = tsortCreateSortHandle(orderInfo, SORT_SINGLESOURCE_SORT, 128, 6, NULL, "test_abc"); tsortSetFetchRawDataFp(phandle, getSingleColDummyBlock); _info* pInfo = (_info*) taosMemoryCalloc(1, sizeof(_info)); From 9f959e17f8b0848cc3e58e2552a9925550207088 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Sat, 2 Apr 2022 18:53:13 +0800 Subject: [PATCH 07/24] fix error in order by logic --- source/libs/executor/src/tsort.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/libs/executor/src/tsort.c b/source/libs/executor/src/tsort.c index cc7416bc87..511cba972c 100644 --- a/source/libs/executor/src/tsort.c +++ b/source/libs/executor/src/tsort.c @@ -421,7 +421,7 @@ static int32_t doInternalMergeSort(SSortHandle* pHandle) { size_t pgSize = pHandle->pageSize; int32_t numOfRows = (pgSize - blockDataGetSerialMetaSize(pHandle->pDataBlock))/ blockDataGetSerialRowSize(pHandle->pDataBlock); - blockDataEnsureCapacity(pHandle->pDataBlock, numOfRows); // useless, it is already enough + blockDataEnsureCapacity(pHandle->pDataBlock, numOfRows); size_t numOfSorted = taosArrayGetSize(pHandle->pOrderedSource); for(int32_t t = 0; t < sortPass; ++t) { From 411086c81161e29aef6e1546d80e6c28ff9033d5 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Sun, 3 Apr 2022 10:42:55 +0800 Subject: [PATCH 08/24] fix error in sort operation --- source/common/src/tdatablock.c | 96 ++++++++++++++----------- source/libs/executor/test/sortTests.cpp | 2 +- 2 files changed, 55 insertions(+), 43 deletions(-) diff --git a/source/common/src/tdatablock.c b/source/common/src/tdatablock.c index 46aeb83943..772b1f69a3 100644 --- a/source/common/src/tdatablock.c +++ b/source/common/src/tdatablock.c @@ -176,6 +176,8 @@ int32_t colDataAppend(SColumnInfoData* pColumnInfoData, uint32_t currentRow, con static void doBitmapMerge(SColumnInfoData* pColumnInfoData, int32_t numOfRow1, const SColumnInfoData* pSource, int32_t numOfRow2) { + if (numOfRow2 <= 0) return; + uint32_t total = numOfRow1 + numOfRow2; if (BitmapLen(numOfRow1) < BitmapLen(total)) { @@ -190,22 +192,32 @@ static void doBitmapMerge(SColumnInfoData* pColumnInfoData, int32_t numOfRow1, c if (remindBits == 0) { // no need to shift bits of bitmap memcpy(pColumnInfoData->nullbitmap + BitmapLen(numOfRow1), pSource->nullbitmap, BitmapLen(numOfRow2)); - } else { - int32_t len = BitmapLen(numOfRow2); - int32_t i = 0; + return; + } - uint8_t* p = (uint8_t*)pSource->nullbitmap; - pColumnInfoData->nullbitmap[BitmapLen(numOfRow1) - 1] |= (p[0] >> remindBits); + uint8_t* p = (uint8_t*)pSource->nullbitmap; + pColumnInfoData->nullbitmap[BitmapLen(numOfRow1) - 1] |= (p[0] >> remindBits); // copy remind bits - uint8_t* start = (uint8_t*)&pColumnInfoData->nullbitmap[BitmapLen(numOfRow1)]; - while (i < len) { - start[i] |= (p[i] << shiftBits); - i += 1; + if (BitmapLen(numOfRow1) == BitmapLen(total)) { + return; + } - if (i > 1) { - start[i - 1] |= (p[i] >> remindBits); - } + int32_t len = BitmapLen(numOfRow2); + int32_t i = 0; + + uint8_t* start = (uint8_t*)&pColumnInfoData->nullbitmap[BitmapLen(numOfRow1)]; + int32_t overCount = BitmapLen(total) - BitmapLen(numOfRow1); + while (i < len) { // size limit of pSource->nullbitmap + if (i >= 1) { + start[i - 1] |= (p[i] >> remindBits); //copy remind bits } + + if (i >= overCount) { // size limit of pColumnInfoData->nullbitmap + return; + } + + start[i] |= (p[i] << shiftBits); //copy shift bits + i += 1; } } @@ -408,44 +420,44 @@ int32_t blockDataSplitRows(SSDataBlock* pBlock, bool hasVarCol, int32_t startInd } return TSDB_CODE_SUCCESS; - } else { - // iterate the rows that can be fit in this buffer page - int32_t size = (headerSize + colHeaderSize); + } + // iterate the rows that can be fit in this buffer page + int32_t size = (headerSize + colHeaderSize); - for (int32_t j = startIndex; j < numOfRows; ++j) { - for (int32_t i = 0; i < numOfCols; ++i) { - SColumnInfoData* pColInfoData = TARRAY_GET_ELEM(pBlock->pDataBlock, i); - if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) { - bool isNull = colDataIsNull(pColInfoData, numOfRows, j, NULL); - if (isNull) { - // do nothing - } else { - char* p = colDataGetData(pColInfoData, j); - size += varDataTLen(p); - } - - size += sizeof(pColInfoData->varmeta.offset[0]); - } else { // this block is unreached, because hasVarCol = true - size += pColInfoData->info.bytes; - - if (((j - startIndex) & 0x07) == 0) { - size += 1; // the space for null bitmap - } + for (int32_t j = startIndex; j < numOfRows; ++j) { + for (int32_t i = 0; i < numOfCols; ++i) { + SColumnInfoData* pColInfoData = TARRAY_GET_ELEM(pBlock->pDataBlock, i); + if (IS_VAR_DATA_TYPE(pColInfoData->info.type)) { + bool isNull = colDataIsNull(pColInfoData, numOfRows, j, NULL); + if (isNull) { + // do nothing + } else { + char* p = colDataGetData(pColInfoData, j); + size += varDataTLen(p); } - } - if (size > pageSize) { - *stopIndex = j - 1; - ASSERT(*stopIndex > startIndex); + size += sizeof(pColInfoData->varmeta.offset[0]); + } else { // this block is unreached, because hasVarCol = true + size += pColInfoData->info.bytes; - return TSDB_CODE_SUCCESS; + if (((j - startIndex) & 0x07) == 0) { + size += 1; // the space for null bitmap + } } } - // all fit in - *stopIndex = numOfRows - 1; - return TSDB_CODE_SUCCESS; + if (size > pageSize) { + *stopIndex = j - 1; + ASSERT(*stopIndex > startIndex); + + return TSDB_CODE_SUCCESS; + } } + + // all fit in + *stopIndex = numOfRows - 1; + return TSDB_CODE_SUCCESS; + } SSDataBlock* blockDataExtractBlock(SSDataBlock* pBlock, int32_t startIndex, int32_t rowCount) { diff --git a/source/libs/executor/test/sortTests.cpp b/source/libs/executor/test/sortTests.cpp index 229b84b923..7e2fd0fed2 100644 --- a/source/libs/executor/test/sortTests.cpp +++ b/source/libs/executor/test/sortTests.cpp @@ -83,7 +83,7 @@ SSDataBlock* getSingleColStrBlock(void* param) { SColumnInfoData colInfo = {0}; colInfo.info.type = TSDB_DATA_TYPE_NCHAR; - colInfo.info.bytes = TSDB_NCHAR_SIZE * 16; + colInfo.info.bytes = TSDB_NCHAR_SIZE * 16 + VARSTR_HEADER_SIZE; colInfo.info.colId = 1; colInfo.varmeta.offset = static_cast(taosMemoryCalloc(pInfo->pageRows, sizeof(int32_t))); From ff21ecd3648b7f9c3041722998a9d1bdeb0ca5e9 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Wed, 6 Apr 2022 11:43:17 +0800 Subject: [PATCH 09/24] optimize sort logic & fix memory leak --- source/common/src/tdatablock.c | 110 +++--------------------- source/libs/executor/src/executorimpl.c | 12 +-- source/libs/executor/src/tsort.c | 29 ++++--- source/libs/executor/test/sortTests.cpp | 6 +- 4 files changed, 38 insertions(+), 119 deletions(-) diff --git a/source/common/src/tdatablock.c b/source/common/src/tdatablock.c index 772b1f69a3..caf82924f4 100644 --- a/source/common/src/tdatablock.c +++ b/source/common/src/tdatablock.c @@ -131,44 +131,7 @@ int32_t colDataAppend(SColumnInfoData* pColumnInfoData, uint32_t currentRow, con memcpy(pColumnInfoData->pData + len, pData, varDataTLen(pData)); pColumnInfoData->varmeta.length += varDataTLen(pData); } else { - char* p = pColumnInfoData->pData + pColumnInfoData->info.bytes * currentRow; - switch (type) { - case TSDB_DATA_TYPE_BOOL: { - *(bool*)p = *(bool*)pData; - break; - } - case TSDB_DATA_TYPE_TINYINT: - case TSDB_DATA_TYPE_UTINYINT: { - *(int8_t*)p = *(int8_t*)pData; - break; - } - case TSDB_DATA_TYPE_SMALLINT: - case TSDB_DATA_TYPE_USMALLINT: { - *(int16_t*)p = *(int16_t*)pData; - break; - } - case TSDB_DATA_TYPE_INT: - case TSDB_DATA_TYPE_UINT: { - *(int32_t*)p = *(int32_t*)pData; - break; - } - case TSDB_DATA_TYPE_TIMESTAMP: - case TSDB_DATA_TYPE_BIGINT: - case TSDB_DATA_TYPE_UBIGINT: { - *(int64_t*)p = *(int64_t*)pData; - break; - } - case TSDB_DATA_TYPE_FLOAT: { - *(float*)p = *(float*)pData; - break; - } - case TSDB_DATA_TYPE_DOUBLE: { - *(double*)p = *(double*)pData; - break; - } - default: - assert(0); - } + memcpy(pColumnInfoData->pData + pColumnInfoData->info.bytes * currentRow, pData, pColumnInfoData->info.bytes); } return 0; @@ -562,6 +525,11 @@ int32_t blockDataFromBuf(SSDataBlock* pBlock, const char* buf) { size_t metaSize = pBlock->info.rows * sizeof(int32_t); if (IS_VAR_DATA_TYPE(pCol->info.type)) { + char* tmp = taosMemoryRealloc(pCol->varmeta.offset, metaSize); + if (tmp == NULL) { + return TSDB_CODE_OUT_OF_MEMORY; + } + pCol->varmeta.offset = (int32_t*)tmp; memcpy(pCol->varmeta.offset, pStart, metaSize); pStart += metaSize; } else { @@ -738,61 +706,12 @@ static int32_t blockDataAssign(SColumnInfoData* pCols, const SSDataBlock* pDataB pDst->varmeta.offset[j] = pSrc->varmeta.offset[index[j]]; } } else { - switch (pSrc->info.type) { - case TSDB_DATA_TYPE_UINT: - case TSDB_DATA_TYPE_INT: { - for (int32_t j = 0; j < pDataBlock->info.rows; ++j) { - int32_t* p = (int32_t*)pDst->pData; - int32_t* srclist = (int32_t*)pSrc->pData; - - p[j] = srclist[index[j]]; - if (colDataIsNull_f(pSrc->nullbitmap, index[j])) { - colDataSetNull_f(pDst->nullbitmap, j); - } - } - break; + for (int32_t j = 0; j < pDataBlock->info.rows; ++j) { + if (colDataIsNull_f(pSrc->nullbitmap, index[j])) { + colDataSetNull_f(pDst->nullbitmap, j); + continue; } - case TSDB_DATA_TYPE_UTINYINT: - case TSDB_DATA_TYPE_TINYINT: { - for (int32_t j = 0; j < pDataBlock->info.rows; ++j) { - int32_t* p = (int32_t*)pDst->pData; - int32_t* srclist = (int32_t*)pSrc->pData; - - p[j] = srclist[index[j]]; - if (colDataIsNull_f(pSrc->nullbitmap, index[j])) { - colDataSetNull_f(pDst->nullbitmap, j); - } - } - break; - } - case TSDB_DATA_TYPE_USMALLINT: - case TSDB_DATA_TYPE_SMALLINT: { - for (int32_t j = 0; j < pDataBlock->info.rows; ++j) { - int32_t* p = (int32_t*)pDst->pData; - int32_t* srclist = (int32_t*)pSrc->pData; - - p[j] = srclist[index[j]]; - if (colDataIsNull_f(pSrc->nullbitmap, index[j])) { - colDataSetNull_f(pDst->nullbitmap, j); - } - } - break; - } - case TSDB_DATA_TYPE_UBIGINT: - case TSDB_DATA_TYPE_BIGINT: { - for (int32_t j = 0; j < pDataBlock->info.rows; ++j) { - int32_t* p = (int32_t*)pDst->pData; - int32_t* srclist = (int32_t*)pSrc->pData; - - p[j] = srclist[index[j]]; - if (colDataIsNull_f(pSrc->nullbitmap, index[j])) { - colDataSetNull_f(pDst->nullbitmap, j); - } - } - break; - } - default: - assert(0); + memcpy(pDst->pData + j * pDst->info.bytes, pSrc->pData + index[j] * pDst->info.bytes, pDst->info.bytes); } } } @@ -938,12 +857,7 @@ int32_t blockDataSort(SSDataBlock* pDataBlock, SArray* pOrderInfo) { int64_t p2 = taosGetTimestampUs(); - int32_t code = blockDataAssign(pCols, pDataBlock, index); - if (code != TSDB_CODE_SUCCESS) { - destroyTupleIndex(index); - terrno = code; - return code; - } + blockDataAssign(pCols, pDataBlock, index); int64_t p3 = taosGetTimestampUs(); diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 2ccc92111a..f960993e3e 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -5769,10 +5769,11 @@ static void appendOneRowToDataBlock(SSDataBlock* pBlock, STupleHandle* pTupleHan pBlock->info.rows += 1; } -static SSDataBlock* getSortedBlockData(SSortHandle* pHandle, SSDataBlock* pDataBlock, bool hasVarCol, - int32_t capacity) { +static SSDataBlock* getSortedBlockData(SSortHandle* pHandle, SSDataBlock* pDataBlock, int32_t capacity) { blockDataCleanup(pDataBlock); + blockDataEnsureCapacity(pDataBlock, capacity); + while (1) { STupleHandle* pTupleHandle = tsortNextTuple(pHandle); if (pTupleHandle == NULL) { @@ -5971,7 +5972,7 @@ static SSDataBlock* doSortedMerge(SOperatorInfo* pOperator, bool* newgroup) { SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo; SSortedMergeOperatorInfo* pInfo = pOperator->info; if (pOperator->status == OP_RES_TO_RETURN) { - return getSortedBlockData(pInfo->pSortHandle, pInfo->binfo.pRes, pInfo->hasVarCol, pInfo->binfo.capacity); + return getSortedBlockData(pInfo->pSortHandle, pInfo->binfo.pRes, pInfo->binfo.capacity); } int32_t numOfBufPage = pInfo->sortBufSize / pInfo->bufPageSize; @@ -6116,10 +6117,9 @@ static SSDataBlock* doSort(SOperatorInfo* pOperator, bool* newgroup) { SExecTaskInfo* pTaskInfo = pOperator->pTaskInfo; SSortOperatorInfo* pInfo = pOperator->info; - bool hasVarCol = pInfo->pDataBlock->info.hasVarCol; if (pOperator->status == OP_RES_TO_RETURN) { - return getSortedBlockData(pInfo->pSortHandle, pInfo->pDataBlock, hasVarCol, pInfo->numOfRowsInRes); + return getSortedBlockData(pInfo->pSortHandle, pInfo->pDataBlock, pInfo->numOfRowsInRes); } int32_t numOfBufPage = pInfo->sortBufSize / pInfo->bufPageSize; @@ -6139,7 +6139,7 @@ static SSDataBlock* doSort(SOperatorInfo* pOperator, bool* newgroup) { } pOperator->status = OP_RES_TO_RETURN; - return getSortedBlockData(pInfo->pSortHandle, pInfo->pDataBlock, hasVarCol, pInfo->numOfRowsInRes); + return getSortedBlockData(pInfo->pSortHandle, pInfo->pDataBlock, pInfo->numOfRowsInRes); } SOperatorInfo* createSortOperatorInfo(SOperatorInfo* downstream, SSDataBlock* pResBlock, SArray* pSortInfo, diff --git a/source/libs/executor/src/tsort.c b/source/libs/executor/src/tsort.c index 511cba972c..49a99ec284 100644 --- a/source/libs/executor/src/tsort.c +++ b/source/libs/executor/src/tsort.c @@ -111,6 +111,17 @@ SSortHandle* tsortCreateSortHandle(SArray* pSortInfo, int32_t type, int32_t page return pSortHandle; } +static int32_t sortComparClearup(SMsortComparParam* cmpParam) { + for(int32_t i = 0; i < cmpParam->numOfSources; ++i) { + SExternalMemSource* pSource = cmpParam->pSources[i]; + blockDataDestroy(pSource->src.pBlock); + taosMemoryFreeClear(pSource); + } + + cmpParam->numOfSources = 0; + return TSDB_CODE_SUCCESS; +} + void tsortDestroySortHandle(SSortHandle* pSortHandle) { tsortClose(pSortHandle); if (pSortHandle->pMergeTree != NULL) { @@ -119,6 +130,8 @@ void tsortDestroySortHandle(SSortHandle* pSortHandle) { destroyDiskbasedBuf(pSortHandle->pBuf); taosMemoryFreeClear(pSortHandle->idStr); + blockDataDestroy(pSortHandle->pDataBlock); + sortComparClearup(&pSortHandle->cmpParam); // pOrderedSource is in cmpParam taosMemoryFreeClear(pSortHandle); } @@ -168,6 +181,7 @@ static int32_t doAddToBuf(SSDataBlock* pDataBlock, SSortHandle* pHandle) { int32_t pageId = -1; void* pPage = getNewBufPage(pHandle->pBuf, pHandle->sourceId, &pageId); if (pPage == NULL) { + blockDataDestroy(p); return terrno; } @@ -227,17 +241,6 @@ static int32_t sortComparInit(SMsortComparParam* cmpParam, SArray* pSources, int return code; } -static int32_t sortComparClearup(SMsortComparParam* cmpParam) { - for(int32_t i = 0; i < cmpParam->numOfSources; ++i) { - SExternalMemSource* pSource = cmpParam->pSources[i]; - blockDataDestroy(pSource->src.pBlock); - taosMemoryFreeClear(pSource); - } - - cmpParam->numOfSources = 0; - return TSDB_CODE_SUCCESS; -} - static void appendOneRowToDataBlock(SSDataBlock *pBlock, const SSDataBlock* pSource, int32_t* rowIndex) { for (int32_t i = 0; i < pBlock->info.numOfCols; ++i) { SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, i); @@ -312,7 +315,7 @@ static int32_t adjustMergeTreeForNextTuple(SExternalMemSource *pSource, SMultiwa return TSDB_CODE_SUCCESS; } -static SSDataBlock* getSortedBlockData(SSortHandle* pHandle, SMsortComparParam* cmpParam, int32_t capacity) { +static SSDataBlock* getSortedBlockDataInner(SSortHandle* pHandle, SMsortComparParam* cmpParam, int32_t capacity) { blockDataCleanup(pHandle->pDataBlock); while(1) { @@ -454,7 +457,7 @@ static int32_t doInternalMergeSort(SSortHandle* pHandle) { } while (1) { - SSDataBlock* pDataBlock = getSortedBlockData(pHandle, &pHandle->cmpParam, numOfRows); + SSDataBlock* pDataBlock = getSortedBlockDataInner(pHandle, &pHandle->cmpParam, numOfRows); if (pDataBlock == NULL) { break; } diff --git a/source/libs/executor/test/sortTests.cpp b/source/libs/executor/test/sortTests.cpp index 7e2fd0fed2..0674f05f33 100644 --- a/source/libs/executor/test/sortTests.cpp +++ b/source/libs/executor/test/sortTests.cpp @@ -198,6 +198,7 @@ TEST(testCase, inMem_sort_Test) { int32_t code = tsortOpen(phandle); int32_t row = 1; + taosMemoryFreeClear(ps); while(1) { STupleHandle* pTupleHandle = tsortNextTuple(phandle); @@ -235,6 +236,7 @@ TEST(testCase, external_mem_sort_Test) { int32_t code = tsortOpen(phandle); int32_t row = 1; + taosMemoryFreeClear(ps); while(1) { STupleHandle* pTupleHandle = tsortNextTuple(phandle); @@ -245,8 +247,8 @@ TEST(testCase, external_mem_sort_Test) { void* v = tsortGetValue(pTupleHandle, 0); printf("%d: %d\n", row, *(int32_t*) v); ASSERT_EQ(row++, *(int32_t*) v); - //char buf[64] = {0}; - //snprintf(buf, varDataLen(v), "%s", varDataVal(v)); + char buf[64] = {0}; + memcpy(buf, varDataVal(v), varDataLen(v)); //printf("%d: %s\n", row, buf); } tsortDestroySortHandle(phandle); From 9b66bb168293a09a1dfe5c2fcea3ab4622cc2524 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Wed, 6 Apr 2022 14:32:07 +0800 Subject: [PATCH 10/24] optimize sort logic & fix memory leak --- source/libs/executor/test/sortTests.cpp | 225 ++++++++++++++++-------- 1 file changed, 153 insertions(+), 72 deletions(-) diff --git a/source/libs/executor/test/sortTests.cpp b/source/libs/executor/test/sortTests.cpp index 0674f05f33..14d527f302 100644 --- a/source/libs/executor/test/sortTests.cpp +++ b/source/libs/executor/test/sortTests.cpp @@ -40,8 +40,22 @@ typedef struct { int32_t startVal; int32_t count; int32_t pageRows; + int16_t type; } _info; +int16_t VARCOUNT = 16; + +float rand_f2() +{ + unsigned r = taosRand(); + r &= 0x007fffff; + r |= 0x40800000; + return *(float*)&r - 6.0; +} + +static const int32_t TEST_NUMBER = 1; +#define bigendian() ((*(char *)&TEST_NUMBER) == 0) + SSDataBlock* getSingleColDummyBlock(void* param) { _info* pInfo = (_info*) param; if (--pInfo->count < 0) { @@ -52,19 +66,64 @@ SSDataBlock* getSingleColDummyBlock(void* param) { pBlock->pDataBlock = taosArrayInit(4, sizeof(SColumnInfoData)); SColumnInfoData colInfo = {0}; - colInfo.info.type = TSDB_DATA_TYPE_INT; - colInfo.info.bytes = sizeof(int32_t); + colInfo.info.type = pInfo->type; + if (pInfo->type == TSDB_DATA_TYPE_NCHAR){ + colInfo.info.bytes = TSDB_NCHAR_SIZE * VARCOUNT + VARSTR_HEADER_SIZE; + colInfo.varmeta.offset = static_cast(taosMemoryCalloc(pInfo->pageRows, sizeof(int32_t))); + } else if(pInfo->type == TSDB_DATA_TYPE_BINARY) { + colInfo.info.bytes = VARCOUNT + VARSTR_HEADER_SIZE; + colInfo.varmeta.offset = static_cast(taosMemoryCalloc(pInfo->pageRows, sizeof(int32_t))); + } else{ + colInfo.info.bytes = tDataTypes[pInfo->type].bytes; + colInfo.pData = static_cast(taosMemoryCalloc(pInfo->pageRows, colInfo.info.bytes)); + colInfo.nullbitmap = static_cast(taosMemoryCalloc(1, (pInfo->pageRows + 7) / 8)); + } colInfo.info.colId = 1; - colInfo.pData = static_cast(taosMemoryCalloc(pInfo->pageRows, sizeof(int32_t))); - colInfo.nullbitmap = static_cast(taosMemoryCalloc(1, (pInfo->pageRows + 7) / 8)); taosArrayPush(pBlock->pDataBlock, &colInfo); for (int32_t i = 0; i < pInfo->pageRows; ++i) { SColumnInfoData* pColInfo = static_cast(TARRAY_GET_ELEM(pBlock->pDataBlock, 0)); - int32_t v = ++pInfo->startVal; - colDataAppend(pColInfo, i, reinterpret_cast(&v), false); + if (pInfo->type == TSDB_DATA_TYPE_NCHAR){ + int32_t size = taosRand() % VARCOUNT; + char str[128] = {0}; + char strOri[128] = {0}; + taosRandStr(strOri, size); + int32_t len = 0; + bool ret = taosMbsToUcs4(strOri, size, (TdUcs4*)varDataVal(str), 128, &len); + if (!ret){ + printf("error\n"); + return NULL; + } + varDataSetLen(str, len); + colDataAppend(pColInfo, i, reinterpret_cast(str), false); + pBlock->info.hasVarCol = true; + printf("nchar: %s\n",strOri); + } else if(pInfo->type == TSDB_DATA_TYPE_BINARY){ + int32_t size = taosRand() % VARCOUNT; + char str[64] = {0}; + taosRandStr(varDataVal(str), size); + varDataSetLen(str, size); + colDataAppend(pColInfo, i, reinterpret_cast(str), false); + pBlock->info.hasVarCol = true; + printf("binary: %s\n", varDataVal(str)); + } else if(pInfo->type == TSDB_DATA_TYPE_DOUBLE || pInfo->type == TSDB_DATA_TYPE_FLOAT) { + double v = rand_f2(); + colDataAppend(pColInfo, i, reinterpret_cast(&v), false); + printf("float: %f\n", v); + } else{ + int64_t v = ++pInfo->startVal; + char *result = static_cast(taosMemoryCalloc(tDataTypes[pInfo->type].bytes, 1)); + if (!bigendian()){ + memcpy(result, &v, tDataTypes[pInfo->type].bytes); + }else{ + memcpy(result, (char*)(&v) + sizeof(int64_t) - tDataTypes[pInfo->type].bytes, tDataTypes[pInfo->type].bytes); + } + + colDataAppend(pColInfo, i, result, false); + printf("int: %lld\n", v); + } } pBlock->info.rows = pInfo->pageRows; @@ -72,40 +131,6 @@ SSDataBlock* getSingleColDummyBlock(void* param) { return pBlock; } -SSDataBlock* getSingleColStrBlock(void* param) { - _info* pInfo = (_info*) param; - if (--pInfo->count < 0) { - return NULL; - } - - SSDataBlock* pBlock = static_cast(taosMemoryCalloc(1, sizeof(SSDataBlock))); - pBlock->pDataBlock = taosArrayInit(4, sizeof(SColumnInfoData)); - - SColumnInfoData colInfo = {0}; - colInfo.info.type = TSDB_DATA_TYPE_NCHAR; - colInfo.info.bytes = TSDB_NCHAR_SIZE * 16 + VARSTR_HEADER_SIZE; - colInfo.info.colId = 1; - colInfo.varmeta.offset = static_cast(taosMemoryCalloc(pInfo->pageRows, sizeof(int32_t))); - - taosArrayPush(pBlock->pDataBlock, &colInfo); - - for (int32_t i = 0; i < pInfo->pageRows; ++i) { - SColumnInfoData* pColInfo = static_cast(TARRAY_GET_ELEM(pBlock->pDataBlock, 0)); - - int32_t size = taosRand() % 16; - char str[64] = {0}; - taosRandStr(varDataVal(str), size); - varDataSetLen(str, size); - colDataAppend(pColInfo, i, reinterpret_cast(str), false); - } - - pBlock->info.rows = pInfo->pageRows; - pBlock->info.numOfCols = 1; - pBlock->info.hasVarCol = true; - - return pBlock; -} - int32_t docomp(const void* p1, const void* p2, void* param) { int32_t pLeftIdx = *(int32_t *)p1; @@ -183,7 +208,6 @@ TEST(testCase, inMem_sort_Test) { SArray* orderInfo = taosArrayInit(1, sizeof(SBlockOrderInfo)); taosArrayPush(orderInfo, &oi); - SSchema s = {.type = TSDB_DATA_TYPE_INT, .colId = 1, .bytes = 4, }; SSortHandle* phandle = tsortCreateSortHandle(orderInfo, SORT_SINGLESOURCE_SORT, 1024, 5, NULL, "test_abc"); tsortSetFetchRawDataFp(phandle, getSingleColDummyBlock); @@ -191,6 +215,7 @@ TEST(testCase, inMem_sort_Test) { pInfo->startVal = 0; pInfo->pageRows = 100; pInfo->count = 6; + pInfo->type = TSDB_DATA_TYPE_USMALLINT; SGenericSource* ps = static_cast(taosMemoryCalloc(1, sizeof(SGenericSource))); ps->param = pInfo; @@ -207,51 +232,107 @@ TEST(testCase, inMem_sort_Test) { } void* v = tsortGetValue(pTupleHandle, 0); - printf("%d: %d\n", row, *(int32_t*) v); - ASSERT_EQ(row++, *(int32_t*) v); + printf("%d: %d\n", row, *(uint16_t*) v); + ASSERT_EQ(row++, *(uint16_t*) v); } + taosArrayDestroy(orderInfo); tsortDestroySortHandle(phandle); } TEST(testCase, external_mem_sort_Test) { - SBlockOrderInfo oi = {0}; - oi.order = TSDB_ORDER_ASC; - oi.slotId = 0; - SArray* orderInfo = taosArrayInit(1, sizeof(SBlockOrderInfo)); - taosArrayPush(orderInfo, &oi); - SSortHandle* phandle = tsortCreateSortHandle(orderInfo, SORT_SINGLESOURCE_SORT, 128, 6, NULL, "test_abc"); - tsortSetFetchRawDataFp(phandle, getSingleColDummyBlock); + _info* pInfo = (_info*) taosMemoryCalloc(7, sizeof(_info)); + pInfo[0].startVal = 0; + pInfo[0].pageRows = 10; + pInfo[0].count = 6; + pInfo[0].type = TSDB_DATA_TYPE_BOOL; - _info* pInfo = (_info*) taosMemoryCalloc(1, sizeof(_info)); - pInfo->startVal = 0; - pInfo->pageRows = 100; - pInfo->count = 6; + pInfo[1].startVal = 0; + pInfo[1].pageRows = 10; + pInfo[1].count = 6; + pInfo[1].type = TSDB_DATA_TYPE_TINYINT; - SGenericSource* ps = static_cast(taosMemoryCalloc(1, sizeof(SGenericSource))); - ps->param = pInfo; + pInfo[2].startVal = 0; + pInfo[2].pageRows = 100; + pInfo[2].count = 6; + pInfo[2].type = TSDB_DATA_TYPE_USMALLINT; - tsortAddSource(phandle, ps); + pInfo[2].startVal = 0; + pInfo[2].pageRows = 100; + pInfo[2].count = 6; + pInfo[2].type = TSDB_DATA_TYPE_INT; - int32_t code = tsortOpen(phandle); - int32_t row = 1; - taosMemoryFreeClear(ps); + pInfo[3].startVal = 0; + pInfo[3].pageRows = 100; + pInfo[3].count = 6; + pInfo[3].type = TSDB_DATA_TYPE_UBIGINT; - while(1) { - STupleHandle* pTupleHandle = tsortNextTuple(phandle); - if (pTupleHandle == NULL) { - break; + pInfo[4].startVal = 0; + pInfo[4].pageRows = 100; + pInfo[4].count = 6; + pInfo[4].type = TSDB_DATA_TYPE_DOUBLE; + + pInfo[5].startVal = 0; + pInfo[5].pageRows = 50; + pInfo[5].count = 6; + pInfo[5].type = TSDB_DATA_TYPE_NCHAR; + + pInfo[6].startVal = 0; + pInfo[6].pageRows = 100; + pInfo[6].count = 6; + pInfo[6].type = TSDB_DATA_TYPE_BINARY; + + for (int i = 0; i < 7; i++){ + SBlockOrderInfo oi = {0}; + oi.order = TSDB_ORDER_ASC; + oi.slotId = 0; + SArray* orderInfo = taosArrayInit(1, sizeof(SBlockOrderInfo)); + taosArrayPush(orderInfo, &oi); + + SSortHandle* phandle = tsortCreateSortHandle(orderInfo, SORT_SINGLESOURCE_SORT, 128, 3, NULL, "test_abc"); + tsortSetFetchRawDataFp(phandle, getSingleColDummyBlock); + + SGenericSource* ps = static_cast(taosMemoryCalloc(1, sizeof(SGenericSource))); + ps->param = &pInfo[i]; + + tsortAddSource(phandle, ps); + + int32_t code = tsortOpen(phandle); + int32_t row = 1; + taosMemoryFreeClear(ps); + + printf("--------start with %s-----------\n", tDataTypes[pInfo[i].type].name); + while(1) { + STupleHandle* pTupleHandle = tsortNextTuple(phandle); + if (pTupleHandle == NULL) { + break; + } + + void* v = tsortGetValue(pTupleHandle, 0); + + if(pInfo[i].type == TSDB_DATA_TYPE_NCHAR){ + char buf[128] = {0}; + int32_t len = taosUcs4ToMbs((TdUcs4 *)varDataVal(v), varDataLen(v), buf); + printf("%d: %s\n", row++, buf); + }else if(pInfo[i].type == TSDB_DATA_TYPE_BINARY){ + char buf[128] = {0}; + memcpy(buf, varDataVal(v), varDataLen(v)); + printf("%d: %s\n", row++, buf); + }else{ + int64_t result = 0; + if (!bigendian()){ + memcpy(&result, v, tDataTypes[pInfo[i].type].bytes); + }else{ + memcpy((char*)(&result) + sizeof(int64_t) - tDataTypes[pInfo[i].type].bytes, v, tDataTypes[pInfo[i].type].bytes); + } + printf("%d: %lld\n", row++, result); + } } - - void* v = tsortGetValue(pTupleHandle, 0); - printf("%d: %d\n", row, *(int32_t*) v); - ASSERT_EQ(row++, *(int32_t*) v); - char buf[64] = {0}; - memcpy(buf, varDataVal(v), varDataLen(v)); - //printf("%d: %s\n", row, buf); + taosArrayDestroy(orderInfo); + tsortDestroySortHandle(phandle); } - tsortDestroySortHandle(phandle); + taosMemoryFree(pInfo); } TEST(testCase, ordered_merge_sort_Test) { From 78acaed84ebcd5127f8a38c0904f6e51dd3aa4af Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Wed, 6 Apr 2022 15:44:14 +0800 Subject: [PATCH 11/24] fix error in sort logic --- cmake/cmake.define | 8 ++++--- source/common/src/tdatablock.c | 6 ++--- source/libs/executor/src/tsort.c | 7 +++++- source/libs/executor/test/sortTests.cpp | 32 +++++++++++++------------ 4 files changed, 31 insertions(+), 22 deletions(-) diff --git a/cmake/cmake.define b/cmake/cmake.define index 298d102b87..03feca37bd 100644 --- a/cmake/cmake.define +++ b/cmake/cmake.define @@ -36,11 +36,13 @@ IF (TD_WINDOWS) ENDIF () ELSE () - SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror -Werror=return-type -fPIC -gdwarf-2 -g3") - SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Werror=return-type -fPIC -gdwarf-2 -g3") + #SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror -Werror=return-type -fPIC -gdwarf-2 -g3") + #SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Werror=return-type -fPIC -gdwarf-2 -g3") + SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror -Werror=return-type -fPIC -gdwarf-2 -fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fno-sanitize=null -fno-sanitize=alignment -static-libasan -g3") + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Werror=return-type -fPIC -gdwarf-2 -fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fno-sanitize=null -fno-sanitize=alignment -static-libasan -g3") MESSAGE("System processor ID: ${CMAKE_SYSTEM_PROCESSOR}") -IF (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm64") +IF (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm64" OR ${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64") ADD_DEFINITIONS("-D_TD_ARM_") ELSE () ADD_DEFINITIONS("-msse4.2 -mfma") diff --git a/source/common/src/tdatablock.c b/source/common/src/tdatablock.c index caf82924f4..372ea9a2d7 100644 --- a/source/common/src/tdatablock.c +++ b/source/common/src/tdatablock.c @@ -373,9 +373,9 @@ int32_t blockDataSplitRows(SSDataBlock* pBlock, bool hasVarCol, int32_t startInd size_t payloadSize = pageSize - (headerSize + colHeaderSize); // TODO speedup by checking if the whole page can fit in firstly. - if (!hasVarCol) { + /*if (!hasVarCol) { size_t rowSize = blockDataGetRowSize(pBlock); - int32_t capacity = (payloadSize / (rowSize * 8 + bitmapChar * numOfCols)) * 8; + int32_t capacity = (payloadSize / (rowSize * 8 + bitmapChar * numOfCols)) * 8; //if pageSize = 128, rowSize = 2, it will core in doAddToBuf:assert(size <= getBufPageSize(pHandle->pBuf)); *stopIndex = startIndex + capacity; if (*stopIndex >= numOfRows) { @@ -383,7 +383,7 @@ int32_t blockDataSplitRows(SSDataBlock* pBlock, bool hasVarCol, int32_t startInd } return TSDB_CODE_SUCCESS; - } + }*/ // iterate the rows that can be fit in this buffer page int32_t size = (headerSize + colHeaderSize); diff --git a/source/libs/executor/src/tsort.c b/source/libs/executor/src/tsort.c index 49a99ec284..e5312921fa 100644 --- a/source/libs/executor/src/tsort.c +++ b/source/libs/executor/src/tsort.c @@ -131,7 +131,12 @@ void tsortDestroySortHandle(SSortHandle* pSortHandle) { destroyDiskbasedBuf(pSortHandle->pBuf); taosMemoryFreeClear(pSortHandle->idStr); blockDataDestroy(pSortHandle->pDataBlock); - sortComparClearup(&pSortHandle->cmpParam); // pOrderedSource is in cmpParam + for (size_t i = 0; i < taosArrayGetSize(pSortHandle->pOrderedSource); i++){ + SExternalMemSource* pSource = taosArrayGet(pSortHandle->pOrderedSource, i); + blockDataDestroy(pSource->src.pBlock); + taosMemoryFreeClear(pSource); + } + taosArrayDestroy(pSortHandle->pOrderedSource); taosMemoryFreeClear(pSortHandle); } diff --git a/source/libs/executor/test/sortTests.cpp b/source/libs/executor/test/sortTests.cpp index 14d527f302..9ad16669a4 100644 --- a/source/libs/executor/test/sortTests.cpp +++ b/source/libs/executor/test/sortTests.cpp @@ -122,7 +122,8 @@ SSDataBlock* getSingleColDummyBlock(void* param) { } colDataAppend(pColInfo, i, result, false); - printf("int: %lld\n", v); + printf("int: %ld\n", v); + taosMemoryFree(result); } } @@ -238,11 +239,12 @@ TEST(testCase, inMem_sort_Test) { } taosArrayDestroy(orderInfo); tsortDestroySortHandle(phandle); + taosMemoryFree(pInfo); } TEST(testCase, external_mem_sort_Test) { - _info* pInfo = (_info*) taosMemoryCalloc(7, sizeof(_info)); + _info* pInfo = (_info*) taosMemoryCalloc(8, sizeof(_info)); pInfo[0].startVal = 0; pInfo[0].pageRows = 10; pInfo[0].count = 6; @@ -258,32 +260,32 @@ TEST(testCase, external_mem_sort_Test) { pInfo[2].count = 6; pInfo[2].type = TSDB_DATA_TYPE_USMALLINT; - pInfo[2].startVal = 0; - pInfo[2].pageRows = 100; - pInfo[2].count = 6; - pInfo[2].type = TSDB_DATA_TYPE_INT; - pInfo[3].startVal = 0; pInfo[3].pageRows = 100; pInfo[3].count = 6; - pInfo[3].type = TSDB_DATA_TYPE_UBIGINT; + pInfo[3].type = TSDB_DATA_TYPE_INT; pInfo[4].startVal = 0; pInfo[4].pageRows = 100; pInfo[4].count = 6; - pInfo[4].type = TSDB_DATA_TYPE_DOUBLE; + pInfo[4].type = TSDB_DATA_TYPE_UBIGINT; pInfo[5].startVal = 0; - pInfo[5].pageRows = 50; + pInfo[5].pageRows = 100; pInfo[5].count = 6; - pInfo[5].type = TSDB_DATA_TYPE_NCHAR; + pInfo[5].type = TSDB_DATA_TYPE_DOUBLE; pInfo[6].startVal = 0; - pInfo[6].pageRows = 100; + pInfo[6].pageRows = 50; pInfo[6].count = 6; - pInfo[6].type = TSDB_DATA_TYPE_BINARY; + pInfo[6].type = TSDB_DATA_TYPE_NCHAR; - for (int i = 0; i < 7; i++){ + pInfo[7].startVal = 0; + pInfo[7].pageRows = 100; + pInfo[7].count = 6; + pInfo[7].type = TSDB_DATA_TYPE_BINARY; + + for (int i = 0; i < 8; i++){ SBlockOrderInfo oi = {0}; oi.order = TSDB_ORDER_ASC; oi.slotId = 0; @@ -326,7 +328,7 @@ TEST(testCase, external_mem_sort_Test) { }else{ memcpy((char*)(&result) + sizeof(int64_t) - tDataTypes[pInfo[i].type].bytes, v, tDataTypes[pInfo[i].type].bytes); } - printf("%d: %lld\n", row++, result); + printf("%d: %ld\n", row++, result); } } taosArrayDestroy(orderInfo); From 865a13c1c2bfa8000f2df1452587c34bdca2da11 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Wed, 6 Apr 2022 16:46:26 +0800 Subject: [PATCH 12/24] fix error in sort logic --- source/common/src/tdatablock.c | 2 +- source/libs/executor/src/tsort.c | 6 +++--- source/libs/executor/test/sortTests.cpp | 6 +++++- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/source/common/src/tdatablock.c b/source/common/src/tdatablock.c index 372ea9a2d7..63c095ecf5 100644 --- a/source/common/src/tdatablock.c +++ b/source/common/src/tdatablock.c @@ -411,7 +411,7 @@ int32_t blockDataSplitRows(SSDataBlock* pBlock, bool hasVarCol, int32_t startInd if (size > pageSize) { *stopIndex = j - 1; - ASSERT(*stopIndex > startIndex); + ASSERT(*stopIndex >= startIndex); return TSDB_CODE_SUCCESS; } diff --git a/source/libs/executor/src/tsort.c b/source/libs/executor/src/tsort.c index e5312921fa..7e01fe0ba8 100644 --- a/source/libs/executor/src/tsort.c +++ b/source/libs/executor/src/tsort.c @@ -132,9 +132,9 @@ void tsortDestroySortHandle(SSortHandle* pSortHandle) { taosMemoryFreeClear(pSortHandle->idStr); blockDataDestroy(pSortHandle->pDataBlock); for (size_t i = 0; i < taosArrayGetSize(pSortHandle->pOrderedSource); i++){ - SExternalMemSource* pSource = taosArrayGet(pSortHandle->pOrderedSource, i); - blockDataDestroy(pSource->src.pBlock); - taosMemoryFreeClear(pSource); + SExternalMemSource** pSource = taosArrayGet(pSortHandle->pOrderedSource, i); + blockDataDestroy((*pSource)->src.pBlock); + taosMemoryFreeClear(*pSource); } taosArrayDestroy(pSortHandle->pOrderedSource); taosMemoryFreeClear(pSortHandle); diff --git a/source/libs/executor/test/sortTests.cpp b/source/libs/executor/test/sortTests.cpp index 9ad16669a4..f1891f9c62 100644 --- a/source/libs/executor/test/sortTests.cpp +++ b/source/libs/executor/test/sortTests.cpp @@ -91,7 +91,7 @@ SSDataBlock* getSingleColDummyBlock(void* param) { char strOri[128] = {0}; taosRandStr(strOri, size); int32_t len = 0; - bool ret = taosMbsToUcs4(strOri, size, (TdUcs4*)varDataVal(str), 128, &len); + bool ret = taosMbsToUcs4(strOri, size, (TdUcs4*)varDataVal(str), size * TSDB_NCHAR_SIZE, &len); if (!ret){ printf("error\n"); return NULL; @@ -321,6 +321,10 @@ TEST(testCase, external_mem_sort_Test) { char buf[128] = {0}; memcpy(buf, varDataVal(v), varDataLen(v)); printf("%d: %s\n", row++, buf); + }else if(pInfo[i].type == TSDB_DATA_TYPE_DOUBLE) { + printf("double: %lf\n", *(double*)v); + }else if (pInfo[i].type == TSDB_DATA_TYPE_FLOAT) { + printf("float: %f\n", *(float*)v); }else{ int64_t result = 0; if (!bigendian()){ From f72c983792386b521ab6f4b009b585a4373997f5 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Wed, 6 Apr 2022 16:56:13 +0800 Subject: [PATCH 13/24] modify test case --- source/common/src/tdatablock.c | 2 +- source/libs/executor/src/executorimpl.c | 2 +- source/libs/executor/test/sortTests.cpp | 27 ++++++++++++++++--------- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/source/common/src/tdatablock.c b/source/common/src/tdatablock.c index 63c095ecf5..33dc3a2469 100644 --- a/source/common/src/tdatablock.c +++ b/source/common/src/tdatablock.c @@ -409,7 +409,7 @@ int32_t blockDataSplitRows(SSDataBlock* pBlock, bool hasVarCol, int32_t startInd } } - if (size > pageSize) { + if (size > pageSize) { // pageSize must be able to hold one row *stopIndex = j - 1; ASSERT(*stopIndex >= startIndex); diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index f960993e3e..b66107b4a7 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -6155,7 +6155,7 @@ SOperatorInfo* createSortOperatorInfo(SOperatorInfo* downstream, SSDataBlock* pR return NULL; } - pInfo->bufPageSize = rowSize < 1024 ? 1024 : rowSize*2; + pInfo->bufPageSize = rowSize < 1024 ? 1024*2 : rowSize*2; // there are headers, so pageSize = rowSize + header pInfo->sortBufSize = pInfo->bufPageSize * 16; // TODO dynamic set the available sort buffer pInfo->numOfRowsInRes = 1024; diff --git a/source/libs/executor/test/sortTests.cpp b/source/libs/executor/test/sortTests.cpp index f1891f9c62..a89b76e81e 100644 --- a/source/libs/executor/test/sortTests.cpp +++ b/source/libs/executor/test/sortTests.cpp @@ -287,7 +287,13 @@ TEST(testCase, external_mem_sort_Test) { for (int i = 0; i < 8; i++){ SBlockOrderInfo oi = {0}; - oi.order = TSDB_ORDER_ASC; + + if(pInfo[i].type == TSDB_DATA_TYPE_NCHAR){ + oi.order = TSDB_ORDER_DESC; + }else{ + oi.order = TSDB_ORDER_ASC; + } + oi.slotId = 0; SArray* orderInfo = taosArrayInit(1, sizeof(SBlockOrderInfo)); taosArrayPush(orderInfo, &oi); @@ -363,15 +369,16 @@ TEST(testCase, ordered_merge_sort_Test) { tsortSetFetchRawDataFp(phandle, getSingleColDummyBlock); tsortSetComparFp(phandle, docomp); + SGenericSource* p = static_cast(taosMemoryCalloc(10, sizeof(SGenericSource))); + _info* c = static_cast<_info*>(taosMemoryCalloc(10, sizeof(_info))); for(int32_t i = 0; i < 10; ++i) { - SGenericSource* p = static_cast(taosMemoryCalloc(1, sizeof(SGenericSource))); - _info* c = static_cast<_info*>(taosMemoryCalloc(1, sizeof(_info))); - c->count = 1; - c->pageRows = 1000; - c->startVal = i*1000; + c[i].count = 1; + c[i].pageRows = 1000; + c[i].startVal = i*1000; + c[i].type = TSDB_DATA_TYPE_INT; - p->param = c; - tsortAddSource(phandle, p); + p[i].param = c; + tsortAddSource(phandle, &p[i]); } int32_t code = tsortOpen(phandle); @@ -388,8 +395,10 @@ TEST(testCase, ordered_merge_sort_Test) { ASSERT_EQ(row++, *(int32_t*) v); } + taosMemoryFree(p); + taosMemoryFree(c); tsortDestroySortHandle(phandle); - taosMemoryFree(pBlock); + blockDataDestroy(pBlock); } #endif From 4bc0e507da6e383aa46221ba2743afb24385a3c0 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Wed, 6 Apr 2022 17:10:51 +0800 Subject: [PATCH 14/24] modify test case --- source/libs/executor/test/sortTests.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/source/libs/executor/test/sortTests.cpp b/source/libs/executor/test/sortTests.cpp index a89b76e81e..e774b8d2e3 100644 --- a/source/libs/executor/test/sortTests.cpp +++ b/source/libs/executor/test/sortTests.cpp @@ -369,16 +369,17 @@ TEST(testCase, ordered_merge_sort_Test) { tsortSetFetchRawDataFp(phandle, getSingleColDummyBlock); tsortSetComparFp(phandle, docomp); - SGenericSource* p = static_cast(taosMemoryCalloc(10, sizeof(SGenericSource))); - _info* c = static_cast<_info*>(taosMemoryCalloc(10, sizeof(_info))); + SGenericSource* p[10] = {0}; + _info c[10] = {0}; for(int32_t i = 0; i < 10; ++i) { + p[i] = static_cast(taosMemoryCalloc(1, sizeof(SGenericSource))); c[i].count = 1; c[i].pageRows = 1000; c[i].startVal = i*1000; c[i].type = TSDB_DATA_TYPE_INT; - p[i].param = c; - tsortAddSource(phandle, &p[i]); + p[i]->param = &c[i]; + tsortAddSource(phandle, p[i]); } int32_t code = tsortOpen(phandle); @@ -395,8 +396,9 @@ TEST(testCase, ordered_merge_sort_Test) { ASSERT_EQ(row++, *(int32_t*) v); } - taosMemoryFree(p); - taosMemoryFree(c); + for(int32_t i = 0; i < 10; ++i) { + taosMemoryFree(p[i]); + } tsortDestroySortHandle(phandle); blockDataDestroy(pBlock); } From aced34c4387a502895a15b690140afdaac2edcdb Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Wed, 6 Apr 2022 18:30:25 +0800 Subject: [PATCH 15/24] fix error in sort operation --- source/libs/executor/inc/tsort.h | 17 ++++++++-------- source/libs/executor/src/executorimpl.c | 4 ++-- source/libs/executor/src/tsort.c | 27 ++++++++++++------------- source/libs/executor/test/sortTests.cpp | 19 +++++++++-------- 4 files changed, 33 insertions(+), 34 deletions(-) diff --git a/source/libs/executor/inc/tsort.h b/source/libs/executor/inc/tsort.h index 8971ee33d3..cc5db32666 100644 --- a/source/libs/executor/inc/tsort.h +++ b/source/libs/executor/inc/tsort.h @@ -34,16 +34,17 @@ typedef struct SMultiMergeSource { SSDataBlock *pBlock; } SMultiMergeSource; -typedef struct SExternalMemSource { +typedef struct SSortSource { SMultiMergeSource src; - SArray* pageIdList; - int32_t pageIndex; -} SExternalMemSource; + union{ + struct{ + SArray* pageIdList; + int32_t pageIndex; + }; + void *param; + }; -typedef struct SGenericSource { - SMultiMergeSource src; - void *param; -} SGenericSource; +} SSortSource; typedef struct SMsortComparParam { void **pSources; diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index b66107b4a7..c418d78540 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -5982,7 +5982,7 @@ static SSDataBlock* doSortedMerge(SOperatorInfo* pOperator, bool* newgroup) { tsortSetFetchRawDataFp(pInfo->pSortHandle, loadNextDataBlock); for (int32_t i = 0; i < pOperator->numOfDownstream; ++i) { - SGenericSource* ps = taosMemoryCalloc(1, sizeof(SGenericSource)); + SSortSource* ps = taosMemoryCalloc(1, sizeof(SSortSource)); ps->param = pOperator->pDownstream[i]; tsortAddSource(pInfo->pSortHandle, ps); } @@ -6128,7 +6128,7 @@ static SSDataBlock* doSort(SOperatorInfo* pOperator, bool* newgroup) { tsortSetFetchRawDataFp(pInfo->pSortHandle, loadNextDataBlock); - SGenericSource* ps = taosMemoryCalloc(1, sizeof(SGenericSource)); + SSortSource* ps = taosMemoryCalloc(1, sizeof(SSortSource)); ps->param = pOperator->pDownstream[0]; tsortAddSource(pInfo->pSortHandle, ps); diff --git a/source/libs/executor/src/tsort.c b/source/libs/executor/src/tsort.c index 7e01fe0ba8..2aa8f6a09d 100644 --- a/source/libs/executor/src/tsort.c +++ b/source/libs/executor/src/tsort.c @@ -113,7 +113,7 @@ SSortHandle* tsortCreateSortHandle(SArray* pSortInfo, int32_t type, int32_t page static int32_t sortComparClearup(SMsortComparParam* cmpParam) { for(int32_t i = 0; i < cmpParam->numOfSources; ++i) { - SExternalMemSource* pSource = cmpParam->pSources[i]; + SSortSource* pSource = cmpParam->pSources[i]; // NOTICE: pSource may be SGenericSource *, if it is SORT_MULTISOURCE_MERGE blockDataDestroy(pSource->src.pBlock); taosMemoryFreeClear(pSource); } @@ -132,7 +132,7 @@ void tsortDestroySortHandle(SSortHandle* pSortHandle) { taosMemoryFreeClear(pSortHandle->idStr); blockDataDestroy(pSortHandle->pDataBlock); for (size_t i = 0; i < taosArrayGetSize(pSortHandle->pOrderedSource); i++){ - SExternalMemSource** pSource = taosArrayGet(pSortHandle->pOrderedSource, i); + SSortSource** pSource = taosArrayGet(pSortHandle->pOrderedSource, i); blockDataDestroy((*pSource)->src.pBlock); taosMemoryFreeClear(*pSource); } @@ -146,7 +146,7 @@ int32_t tsortAddSource(SSortHandle* pSortHandle, void* pSource) { } static int32_t doAddNewExternalMemSource(SDiskbasedBuf *pBuf, SArray* pAllSources, SSDataBlock* pBlock, int32_t* sourceId) { - SExternalMemSource* pSource = taosMemoryCalloc(1, sizeof(SExternalMemSource)); + SSortSource* pSource = taosMemoryCalloc(1, sizeof(SSortSource)); if (pSource == NULL) { return TSDB_CODE_QRY_OUT_OF_MEMORY; } @@ -216,7 +216,7 @@ static int32_t sortComparInit(SMsortComparParam* cmpParam, SArray* pSources, int if (pHandle->type == SORT_SINGLESOURCE_SORT) { for (int32_t i = 0; i < cmpParam->numOfSources; ++i) { - SExternalMemSource* pSource = cmpParam->pSources[i]; + SSortSource* pSource = cmpParam->pSources[i]; SPageInfo* pPgInfo = *(SPageInfo**)taosArrayGet(pSource->pageIdList, pSource->pageIndex); void* pPage = getBufPage(pHandle->pBuf, getPageId(pPgInfo)); @@ -238,7 +238,7 @@ static int32_t sortComparInit(SMsortComparParam* cmpParam, SArray* pSources, int } for (int32_t i = 0; i < cmpParam->numOfSources; ++i) { - SGenericSource* pSource = cmpParam->pSources[i]; + SSortSource* pSource = cmpParam->pSources[i]; pSource->src.pBlock = pHandle->fetchfp(pSource->param); } } @@ -265,7 +265,7 @@ static void appendOneRowToDataBlock(SSDataBlock *pBlock, const SSDataBlock* pSou *rowIndex += 1; } -static int32_t adjustMergeTreeForNextTuple(SExternalMemSource *pSource, SMultiwayMergeTreeInfo *pTree, SSortHandle *pHandle, int32_t* numOfCompleted) { +static int32_t adjustMergeTreeForNextTuple(SSortSource *pSource, SMultiwayMergeTreeInfo *pTree, SSortHandle *pHandle, int32_t* numOfCompleted) { /* * load a new SDataBlock into memory of a given intermediate data-set source, * since it's last record in buffer has been chosen to be processed, as the winner of loser-tree @@ -292,7 +292,7 @@ static int32_t adjustMergeTreeForNextTuple(SExternalMemSource *pSource, SMultiwa releaseBufPage(pHandle->pBuf, pPage); } } else { - pSource->src.pBlock = pHandle->fetchfp(((SGenericSource*)pSource)->param); + pSource->src.pBlock = pHandle->fetchfp(((SSortSource*)pSource)->param); if (pSource->src.pBlock == NULL) { (*numOfCompleted) += 1; pSource->src.rowIndex = -1; @@ -330,7 +330,7 @@ static SSDataBlock* getSortedBlockDataInner(SSortHandle* pHandle, SMsortComparPa int32_t index = tMergeTreeGetChosenIndex(pHandle->pMergeTree); - SExternalMemSource *pSource = (*cmpParam).pSources[index]; + SSortSource *pSource = (*cmpParam).pSources[index]; appendOneRowToDataBlock(pHandle->pDataBlock, pSource->src.pBlock, &pSource->src.rowIndex); int32_t code = adjustMergeTreeForNextTuple(pSource, pHandle->pMergeTree, pHandle, &pHandle->numOfCompletedSources); @@ -355,8 +355,8 @@ int32_t msortComparFn(const void *pLeft, const void *pRight, void *param) { SArray *pInfo = pParam->orderInfo; - SExternalMemSource* pLeftSource = pParam->pSources[pLeftIdx]; - SExternalMemSource* pRightSource = pParam->pSources[pRightIdx]; + SSortSource* pLeftSource = pParam->pSources[pLeftIdx]; + SSortSource* pRightSource = pParam->pSources[pRightIdx]; // this input is exhausted, set the special value to denote this if (pLeftSource->src.rowIndex == -1) { @@ -484,6 +484,7 @@ static int32_t doInternalMergeSort(SSortHandle* pHandle) { blockDataCleanup(pDataBlock); } + sortComparClearup(&pHandle->cmpParam); tMergeTreeDestroy(pHandle->pMergeTree); pHandle->numOfCompletedSources = 0; @@ -494,8 +495,6 @@ static int32_t doInternalMergeSort(SSortHandle* pHandle) { } } - sortComparClearup(&pHandle->cmpParam); - taosArrayClear(pHandle->pOrderedSource); taosArrayAddAll(pHandle->pOrderedSource, pResList); taosArrayDestroy(pResList); @@ -523,7 +522,7 @@ static int32_t createInitialSortedMultiSources(SSortHandle* pHandle) { size_t sortBufSize = pHandle->numOfPages * pHandle->pageSize; if (pHandle->type == SORT_SINGLESOURCE_SORT) { - SGenericSource* source = taosArrayGetP(pHandle->pOrderedSource, 0); + SSortSource* source = taosArrayGetP(pHandle->pOrderedSource, 0); taosArrayClear(pHandle->pOrderedSource); while (1) { SSDataBlock* pBlock = pHandle->fetchfp(source->param); @@ -652,7 +651,7 @@ STupleHandle* tsortNextTuple(SSortHandle* pHandle) { } int32_t index = tMergeTreeGetChosenIndex(pHandle->pMergeTree); - SExternalMemSource *pSource = pHandle->cmpParam.pSources[index]; + SSortSource *pSource = pHandle->cmpParam.pSources[index]; if (pHandle->needAdjust) { int32_t code = adjustMergeTreeForNextTuple(pSource, pHandle->pMergeTree, pHandle, &pHandle->numOfCompletedSources); diff --git a/source/libs/executor/test/sortTests.cpp b/source/libs/executor/test/sortTests.cpp index e774b8d2e3..bf77e30eaa 100644 --- a/source/libs/executor/test/sortTests.cpp +++ b/source/libs/executor/test/sortTests.cpp @@ -138,12 +138,12 @@ int32_t docomp(const void* p1, const void* p2, void* param) { int32_t pRightIdx = *(int32_t *)p2; SMsortComparParam *pParam = (SMsortComparParam *)param; - SGenericSource** px = reinterpret_cast(pParam->pSources); + SSortSource** px = reinterpret_cast(pParam->pSources); SArray *pInfo = pParam->orderInfo; - SGenericSource* pLeftSource = px[pLeftIdx]; - SGenericSource* pRightSource = px[pRightIdx]; + SSortSource* pLeftSource = px[pLeftIdx]; + SSortSource* pRightSource = px[pRightIdx]; // this input is exhausted, set the special value to denote this if (pLeftSource->src.rowIndex == -1) { @@ -218,7 +218,7 @@ TEST(testCase, inMem_sort_Test) { pInfo->count = 6; pInfo->type = TSDB_DATA_TYPE_USMALLINT; - SGenericSource* ps = static_cast(taosMemoryCalloc(1, sizeof(SGenericSource))); + SSortSource* ps = static_cast(taosMemoryCalloc(1, sizeof(SSortSource))); ps->param = pInfo; tsortAddSource(phandle, ps); @@ -301,7 +301,7 @@ TEST(testCase, external_mem_sort_Test) { SSortHandle* phandle = tsortCreateSortHandle(orderInfo, SORT_SINGLESOURCE_SORT, 128, 3, NULL, "test_abc"); tsortSetFetchRawDataFp(phandle, getSingleColDummyBlock); - SGenericSource* ps = static_cast(taosMemoryCalloc(1, sizeof(SGenericSource))); + SSortSource* ps = static_cast(taosMemoryCalloc(1, sizeof(SSortSource))); ps->param = &pInfo[i]; tsortAddSource(phandle, ps); @@ -369,10 +369,10 @@ TEST(testCase, ordered_merge_sort_Test) { tsortSetFetchRawDataFp(phandle, getSingleColDummyBlock); tsortSetComparFp(phandle, docomp); - SGenericSource* p[10] = {0}; + SSortSource* p[10] = {0}; _info c[10] = {0}; for(int32_t i = 0; i < 10; ++i) { - p[i] = static_cast(taosMemoryCalloc(1, sizeof(SGenericSource))); + p[i] = static_cast(taosMemoryCalloc(1, sizeof(SSortSource))); c[i].count = 1; c[i].pageRows = 1000; c[i].startVal = i*1000; @@ -396,9 +396,8 @@ TEST(testCase, ordered_merge_sort_Test) { ASSERT_EQ(row++, *(int32_t*) v); } - for(int32_t i = 0; i < 10; ++i) { - taosMemoryFree(p[i]); - } + + taosArrayDestroy(orderInfo); tsortDestroySortHandle(phandle); blockDataDestroy(pBlock); } From c4f67c25fbb05db4f9c6f5703666670f9aef336a Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Wed, 6 Apr 2022 18:47:56 +0800 Subject: [PATCH 16/24] fix no return error --- source/libs/function/src/builtinsimpl.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/source/libs/function/src/builtinsimpl.c b/source/libs/function/src/builtinsimpl.c index fb6abe3c5d..eb77bce5e5 100644 --- a/source/libs/function/src/builtinsimpl.c +++ b/source/libs/function/src/builtinsimpl.c @@ -97,6 +97,7 @@ int32_t countFunction(SqlFunctionCtx *pCtx) { *((int64_t *)buf) += numOfElem; SET_VAL(pResInfo, numOfElem, 1); + return TSDB_CODE_SUCCESS; } #define LIST_ADD_N(_res, _col, _start, _rows, _t, numOfElem) \ @@ -167,6 +168,7 @@ int32_t sumFunction(SqlFunctionCtx *pCtx) { // data in the check operation are all null, not output SET_VAL(GET_RES_INFO(pCtx), numOfElem, 1); + return TSDB_CODE_SUCCESS; } bool getSumFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv) { @@ -435,11 +437,13 @@ int32_t doMinMaxHelper(SqlFunctionCtx *pCtx, int32_t isMinFunc) { int32_t minFunction(SqlFunctionCtx *pCtx) { int32_t numOfElems = doMinMaxHelper(pCtx, 1); SET_VAL(GET_RES_INFO(pCtx), numOfElems, 1); + return TSDB_CODE_SUCCESS; } int32_t maxFunction(SqlFunctionCtx *pCtx) { int32_t numOfElems = doMinMaxHelper(pCtx, 0); SET_VAL(GET_RES_INFO(pCtx), numOfElems, 1); + return TSDB_CODE_SUCCESS; } typedef struct STopBotRes { @@ -589,6 +593,7 @@ int32_t stddevFunction(SqlFunctionCtx* pCtx) { // data in the check operation are all null, not output SET_VAL(GET_RES_INFO(pCtx), numOfElem, 1); + return TSDB_CODE_SUCCESS; } void stddevFinalize(SqlFunctionCtx* pCtx) { @@ -716,6 +721,7 @@ int32_t percentileFunction(SqlFunctionCtx *pCtx) { SET_VAL(pResInfo, notNullElems, 1); pResInfo->hasResult = DATA_SET_FLAG; + return TSDB_CODE_SUCCESS; } // TODO set the correct parameter. @@ -783,6 +789,7 @@ int32_t firstFunction(SqlFunctionCtx *pCtx) { } SET_VAL(pResInfo, numOfElems, 1); + return TSDB_CODE_SUCCESS; } int32_t lastFunction(SqlFunctionCtx *pCtx) { @@ -844,6 +851,7 @@ int32_t lastFunction(SqlFunctionCtx *pCtx) { } SET_VAL(pResInfo, numOfElems, 1); + return TSDB_CODE_SUCCESS; } typedef struct SDiffInfo { @@ -1072,6 +1080,7 @@ int32_t diffFunction(SqlFunctionCtx *pCtx) { * 2. current block may be null value */ assert(pCtx->hasNull); + return 0; } else { // for (int t = 0; t < pCtx->tagInfo.numOfTagCols; ++t) { // SqlFunctionCtx* tagCtx = pCtx->tagInfo.pTagCtxList[t]; From c9de8b3b15ad5d81dca55f25c072c87e1a22f77f Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Wed, 6 Apr 2022 19:50:24 +0800 Subject: [PATCH 17/24] fix error in sort logic --- source/libs/executor/inc/tsort.h | 1 - source/libs/executor/src/executorimpl.c | 1 + source/libs/executor/src/tsort.c | 4 ++-- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/source/libs/executor/inc/tsort.h b/source/libs/executor/inc/tsort.h index cc5db32666..3c7194de6f 100644 --- a/source/libs/executor/inc/tsort.h +++ b/source/libs/executor/inc/tsort.h @@ -50,7 +50,6 @@ typedef struct SMsortComparParam { void **pSources; int32_t numOfSources; SArray *orderInfo; // SArray - bool nullFirst; } SMsortComparParam; typedef struct SSortHandle SSortHandle; diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index f3f52455f0..34f3325a8d 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -4144,6 +4144,7 @@ int32_t loadRemoteDataCallback(void* param, const SDataBuf* pMsg, int32_t code) pSourceDataInfo->status = EX_SOURCE_DATA_READY; tsem_post(&pSourceDataInfo->pEx->ready); + return TSDB_CODE_SUCCESS; } static void destroySendMsgInfo(SMsgSendInfo* pMsgBody) { diff --git a/source/libs/executor/src/tsort.c b/source/libs/executor/src/tsort.c index 2aa8f6a09d..a052cbe846 100644 --- a/source/libs/executor/src/tsort.c +++ b/source/libs/executor/src/tsort.c @@ -391,11 +391,11 @@ int32_t msortComparFn(const void *pLeft, const void *pRight, void *param) { } if (rightNull) { - return pParam->nullFirst? 1:-1; + return pOrder->nullFirst? 1:-1; } if (leftNull) { - return pParam->nullFirst? -1:1; + return pOrder->nullFirst? -1:1; } void* left1 = colDataGetData(pLeftColInfoData, pLeftSource->src.rowIndex); From 4cc93f9984bdd8839212b3672e93d5c3c757bb1f Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Wed, 6 Apr 2022 19:51:28 +0800 Subject: [PATCH 18/24] fix error in sort logic --- source/libs/executor/src/scanoperator.c | 1 + 1 file changed, 1 insertion(+) diff --git a/source/libs/executor/src/scanoperator.c b/source/libs/executor/src/scanoperator.c index f39a18da68..4633300a44 100644 --- a/source/libs/executor/src/scanoperator.c +++ b/source/libs/executor/src/scanoperator.c @@ -576,6 +576,7 @@ static int32_t loadSysTableContentCb(void* param, const SDataBuf* pMsg, int32_t } tsem_post(&pScanResInfo->ready); + return TSDB_CODE_SUCCESS; } static SSDataBlock* doFilterResult(SSysTableScanInfo* pInfo) { From 288b50aef2438c8e72f5a01e78159e163118df9e Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Wed, 6 Apr 2022 19:53:17 +0800 Subject: [PATCH 19/24] fix error in sort logic --- source/libs/executor/test/sortTests.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/libs/executor/test/sortTests.cpp b/source/libs/executor/test/sortTests.cpp index bf77e30eaa..5bb184e5b7 100644 --- a/source/libs/executor/test/sortTests.cpp +++ b/source/libs/executor/test/sortTests.cpp @@ -178,11 +178,11 @@ int32_t docomp(const void* p1, const void* p2, void* param) { } if (rightNull) { - return pParam->nullFirst? 1:-1; + return pOrder->nullFirst? 1:-1; } if (leftNull) { - return pParam->nullFirst? -1:1; + return pOrder->nullFirst? -1:1; } void* left1 = colDataGetData(pLeftColInfoData, pLeftSource->src.rowIndex); From bb18e3c4850fb146fa936d11d3d485f01b378673 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Thu, 7 Apr 2022 17:50:06 +0800 Subject: [PATCH 20/24] fix error in sort logic --- source/common/src/tdatablock.c | 7 +++++-- source/libs/executor/inc/tsort.h | 5 +++++ source/libs/executor/src/executorimpl.c | 7 ++----- source/libs/executor/src/tsort.c | 5 ----- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/source/common/src/tdatablock.c b/source/common/src/tdatablock.c index 33dc3a2469..44d261bd31 100644 --- a/source/common/src/tdatablock.c +++ b/source/common/src/tdatablock.c @@ -192,6 +192,9 @@ int32_t colDataMergeCol(SColumnInfoData* pColumnInfoData, uint32_t numOfRow1, co return numOfRow1; } + if (pSource->hasNull) { + pColumnInfoData->hasNull = pSource->hasNull; + } if (IS_VAR_DATA_TYPE(pColumnInfoData->info.type)) { // Handle the bitmap char* p = taosMemoryRealloc(pColumnInfoData->varmeta.offset, sizeof(int32_t) * (numOfRow1 + numOfRow2)); @@ -313,9 +316,9 @@ int32_t blockDataUpdateTsWindow(SSDataBlock* pDataBlock) { } int32_t blockDataMerge(SSDataBlock* pDest, const SSDataBlock* pSrc) { - assert(pSrc != NULL && pDest != NULL && pDest->info.numOfCols == pSrc->info.numOfCols); + assert(pSrc != NULL && pDest != NULL); - int32_t numOfCols = pSrc->info.numOfCols; + int32_t numOfCols = pDest->info.numOfCols; for (int32_t i = 0; i < numOfCols; ++i) { SColumnInfoData* pCol2 = taosArrayGet(pDest->pDataBlock, i); SColumnInfoData* pCol1 = taosArrayGet(pSrc->pDataBlock, i); diff --git a/source/libs/executor/inc/tsort.h b/source/libs/executor/inc/tsort.h index 3c7194de6f..03bbe8098c 100644 --- a/source/libs/executor/inc/tsort.h +++ b/source/libs/executor/inc/tsort.h @@ -52,6 +52,11 @@ typedef struct SMsortComparParam { SArray *orderInfo; // SArray } SMsortComparParam; +struct STupleHandle { + SSDataBlock* pBlock; + int32_t rowIndex; +}; + typedef struct SSortHandle SSortHandle; typedef struct STupleHandle STupleHandle; diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 4cc1658e64..48b734f3aa 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -302,10 +302,6 @@ SSDataBlock* createOutputBuf_rv1(SDataBlockDescNode* pNode) { } taosArrayPush(pBlock->pDataBlock, &idata); - - if (IS_VAR_DATA_TYPE(idata.info.type)) { - pBlock->info.hasVarCol = true; - } } return pBlock; @@ -4748,7 +4744,8 @@ static void appendOneRowToDataBlock(SSDataBlock* pBlock, STupleHandle* pTupleHan for (int32_t i = 0; i < pBlock->info.numOfCols; ++i) { SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, i); - bool isNull = tsortIsNullVal(pTupleHandle, i); + SColumnInfoData* pColInfoSrc = taosArrayGet(pTupleHandle->pBlock->pDataBlock, i); + bool isNull = colDataIsNull(pColInfoSrc, 0, pTupleHandle->rowIndex, NULL); if (isNull) { colDataAppend(pColInfo, pBlock->info.rows, NULL, true); } else { diff --git a/source/libs/executor/src/tsort.c b/source/libs/executor/src/tsort.c index a052cbe846..b226106e01 100644 --- a/source/libs/executor/src/tsort.c +++ b/source/libs/executor/src/tsort.c @@ -24,11 +24,6 @@ #include "tutil.h" #include "tcompare.h" -struct STupleHandle { - SSDataBlock* pBlock; - int32_t rowIndex; -}; - struct SSortHandle { int32_t type; From 1f8d2ea4465d58f3a16c24f2fc37f187bcb8a8b3 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Fri, 8 Apr 2022 14:17:32 +0800 Subject: [PATCH 21/24] : fix index map error from table scan to sort output --- cmake/cmake.define | 8 +++--- include/common/tdatablock.h | 2 +- source/common/src/tdatablock.c | 8 ++++-- source/dnode/vnode/src/tsdb/tsdbSma.c | 2 ++ source/libs/executor/inc/executorimpl.h | 3 ++- source/libs/executor/inc/tsort.h | 2 +- source/libs/executor/src/executorimpl.c | 33 +++++++++++++++++++----- source/libs/executor/src/groupoperator.c | 4 +-- source/libs/executor/src/tsort.c | 6 +++-- source/libs/executor/test/sortTests.cpp | 6 ++--- 10 files changed, 52 insertions(+), 22 deletions(-) diff --git a/cmake/cmake.define b/cmake/cmake.define index 03feca37bd..639ae9ca3f 100644 --- a/cmake/cmake.define +++ b/cmake/cmake.define @@ -36,10 +36,10 @@ IF (TD_WINDOWS) ENDIF () ELSE () - #SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror -Werror=return-type -fPIC -gdwarf-2 -g3") - #SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Werror=return-type -fPIC -gdwarf-2 -g3") - SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror -Werror=return-type -fPIC -gdwarf-2 -fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fno-sanitize=null -fno-sanitize=alignment -static-libasan -g3") - SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Werror=return-type -fPIC -gdwarf-2 -fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fno-sanitize=null -fno-sanitize=alignment -static-libasan -g3") + SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror -Werror=return-type -fPIC -gdwarf-2 -g3") + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Werror=return-type -fPIC -gdwarf-2 -g3") + #SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror -Werror=return-type -fPIC -gdwarf-2 -fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fno-sanitize=null -fno-sanitize=alignment -static-libasan -g3") + #SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Werror=return-type -fPIC -gdwarf-2 -fsanitize=address -fsanitize=undefined -fno-sanitize-recover=all -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fno-sanitize=null -fno-sanitize=alignment -static-libasan -g3") MESSAGE("System processor ID: ${CMAKE_SYSTEM_PROCESSOR}") IF (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm64" OR ${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64") diff --git a/include/common/tdatablock.h b/include/common/tdatablock.h index d6d82875e0..0f9216a541 100644 --- a/include/common/tdatablock.h +++ b/include/common/tdatablock.h @@ -177,7 +177,7 @@ void colDataTrim(SColumnInfoData* pColumnInfoData); size_t blockDataGetNumOfCols(const SSDataBlock* pBlock); size_t blockDataGetNumOfRows(const SSDataBlock* pBlock); -int32_t blockDataMerge(SSDataBlock* pDest, const SSDataBlock* pSrc); +int32_t blockDataMerge(SSDataBlock* pDest, const SSDataBlock* pSrc, SArray* pIndexMap); int32_t blockDataSplitRows(SSDataBlock* pBlock, bool hasVarCol, int32_t startIndex, int32_t* stopIndex, int32_t pageSize); int32_t blockDataToBuf(char* buf, const SSDataBlock* pBlock); diff --git a/source/common/src/tdatablock.c b/source/common/src/tdatablock.c index 44d261bd31..abcb4283d2 100644 --- a/source/common/src/tdatablock.c +++ b/source/common/src/tdatablock.c @@ -315,13 +315,17 @@ int32_t blockDataUpdateTsWindow(SSDataBlock* pDataBlock) { return 0; } -int32_t blockDataMerge(SSDataBlock* pDest, const SSDataBlock* pSrc) { +int32_t blockDataMerge(SSDataBlock* pDest, const SSDataBlock* pSrc, SArray* pIndexMap) { assert(pSrc != NULL && pDest != NULL); int32_t numOfCols = pDest->info.numOfCols; for (int32_t i = 0; i < numOfCols; ++i) { + int32_t mapIndex = i; + if(pIndexMap) { + mapIndex = *(int32_t*)taosArrayGet(pIndexMap, i); + } SColumnInfoData* pCol2 = taosArrayGet(pDest->pDataBlock, i); - SColumnInfoData* pCol1 = taosArrayGet(pSrc->pDataBlock, i); + SColumnInfoData* pCol1 = taosArrayGet(pSrc->pDataBlock, mapIndex); uint32_t oldLen = colDataGetLength(pCol2, pDest->info.rows); uint32_t newLen = colDataGetLength(pCol1, pSrc->info.rows); diff --git a/source/dnode/vnode/src/tsdb/tsdbSma.c b/source/dnode/vnode/src/tsdb/tsdbSma.c index 87660e6fe1..ae0befd524 100644 --- a/source/dnode/vnode/src/tsdb/tsdbSma.c +++ b/source/dnode/vnode/src/tsdb/tsdbSma.c @@ -1437,6 +1437,7 @@ int32_t tsdbCreateTSma(STsdb *pTsdb, char *pMsg) { tdDestroyTSma(&vCreateSmaReq.tSma); // TODO: return directly or go on follow steps? + return TSDB_CODE_SUCCESS; } int32_t tsdbDropTSma(STsdb *pTsdb, char *pMsg) { @@ -1466,6 +1467,7 @@ int32_t tsdbDropTSma(STsdb *pTsdb, char *pMsg) { tsdbTSmaSub(pTsdb, 1); // TODO: return directly or go on follow steps? + return TSDB_CODE_SUCCESS; } #if 0 diff --git a/source/libs/executor/inc/executorimpl.h b/source/libs/executor/inc/executorimpl.h index 57edc40007..fc59412b75 100644 --- a/source/libs/executor/inc/executorimpl.h +++ b/source/libs/executor/inc/executorimpl.h @@ -614,6 +614,7 @@ typedef struct SSortOperatorInfo { SSDataBlock *pDataBlock; SArray* pSortInfo; SSortHandle *pSortHandle; + SArray* inputSlotMap; // for index map from table scan output int32_t bufPageSize; int32_t numOfRowsInRes; @@ -651,7 +652,7 @@ SOperatorInfo* createAggregateOperatorInfo(SOperatorInfo* downstream, SExprInfo* SExecTaskInfo* pTaskInfo, const STableGroupInfo* pTableGroupInfo); SOperatorInfo* createMultiTableAggOperatorInfo(SOperatorInfo* downstream, SExprInfo* pExprInfo, int32_t numOfCols, SSDataBlock* pResBlock, SExecTaskInfo* pTaskInfo, const STableGroupInfo* pTableGroupInfo); SOperatorInfo* createProjectOperatorInfo(SOperatorInfo* downstream, SExprInfo* pExprInfo, int32_t num, SSDataBlock* pResBlock, SLimit* pLimit, SExecTaskInfo* pTaskInfo); -SOperatorInfo *createSortOperatorInfo(SOperatorInfo* downstream, SSDataBlock* pResBlock, SArray* pSortInfo, SExecTaskInfo* pTaskInfo); +SOperatorInfo *createSortOperatorInfo(SOperatorInfo* downstream, SSDataBlock* pResBlock, SArray* pSortInfo, SArray* pIndexMap, SExecTaskInfo* pTaskInfo); SOperatorInfo* createSortedMergeOperatorInfo(SOperatorInfo** downstream, int32_t numOfDownstream, SExprInfo* pExprInfo, int32_t num, SArray* pSortInfo, SArray* pGroupInfo, SExecTaskInfo* pTaskInfo); SOperatorInfo* createSysTableScanOperatorInfo(void* pSysTableReadHandle, SSDataBlock* pResBlock, const SName* pName, SNode* pCondition, SEpSet epset, SArray* colList, SExecTaskInfo* pTaskInfo, bool showRewrite, int32_t accountId); diff --git a/source/libs/executor/inc/tsort.h b/source/libs/executor/inc/tsort.h index 03bbe8098c..f012cc2448 100644 --- a/source/libs/executor/inc/tsort.h +++ b/source/libs/executor/inc/tsort.h @@ -68,7 +68,7 @@ typedef int32_t (*_sort_merge_compar_fn_t)(const void* p1, const void* p2, void* * @param type * @return */ -SSortHandle* tsortCreateSortHandle(SArray* pOrderInfo, int32_t type, int32_t pageSize, int32_t numOfPages, SSDataBlock* pBlock, const char* idstr); +SSortHandle* tsortCreateSortHandle(SArray* pOrderInfo, SArray* pIndexMap, int32_t type, int32_t pageSize, int32_t numOfPages, SSDataBlock* pBlock, const char* idstr); /** * diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 20899098cc..9b2772ddfa 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -4953,7 +4953,7 @@ static SSDataBlock* doSortedMerge(SOperatorInfo* pOperator, bool* newgroup) { } int32_t numOfBufPage = pInfo->sortBufSize / pInfo->bufPageSize; - pInfo->pSortHandle = tsortCreateSortHandle(pInfo->pSortInfo, SORT_MULTISOURCE_MERGE, pInfo->bufPageSize, numOfBufPage, + pInfo->pSortHandle = tsortCreateSortHandle(pInfo->pSortInfo, NULL, SORT_MULTISOURCE_MERGE, pInfo->bufPageSize, numOfBufPage, pInfo->binfo.pRes, "GET_TASKID(pTaskInfo)"); tsortSetFetchRawDataFp(pInfo->pSortHandle, loadNextDataBlock); @@ -5100,7 +5100,7 @@ static SSDataBlock* doSort(SOperatorInfo* pOperator, bool* newgroup) { } int32_t numOfBufPage = pInfo->sortBufSize / pInfo->bufPageSize; - pInfo->pSortHandle = tsortCreateSortHandle(pInfo->pSortInfo, SORT_SINGLESOURCE_SORT, pInfo->bufPageSize, numOfBufPage, + pInfo->pSortHandle = tsortCreateSortHandle(pInfo->pSortInfo, pInfo->inputSlotMap, SORT_SINGLESOURCE_SORT, pInfo->bufPageSize, numOfBufPage, pInfo->pDataBlock, pTaskInfo->id.str); tsortSetFetchRawDataFp(pInfo->pSortHandle, loadNextDataBlock); @@ -5119,7 +5119,7 @@ static SSDataBlock* doSort(SOperatorInfo* pOperator, bool* newgroup) { return getSortedBlockData(pInfo->pSortHandle, pInfo->pDataBlock, pInfo->numOfRowsInRes); } -SOperatorInfo* createSortOperatorInfo(SOperatorInfo* downstream, SSDataBlock* pResBlock, SArray* pSortInfo, SExecTaskInfo* pTaskInfo) { +SOperatorInfo* createSortOperatorInfo(SOperatorInfo* downstream, SSDataBlock* pResBlock, SArray* pSortInfo, SArray* pIndexMap, SExecTaskInfo* pTaskInfo) { SSortOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SSortOperatorInfo)); SOperatorInfo* pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo)); int32_t rowSize = pResBlock->info.rowSize; @@ -5137,6 +5137,7 @@ SOperatorInfo* createSortOperatorInfo(SOperatorInfo* downstream, SSDataBlock* pR pInfo->numOfRowsInRes = 1024; pInfo->pDataBlock = pResBlock; pInfo->pSortInfo = pSortInfo; + pInfo->inputSlotMap = pIndexMap; pOperator->name = "SortOperator"; pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_SORT; @@ -6265,6 +6266,7 @@ static void destroyOrderOperatorInfo(void* param, int32_t numOfOutput) { pInfo->pDataBlock = blockDataDestroy(pInfo->pDataBlock); taosArrayDestroy(pInfo->pSortInfo); + taosArrayDestroy(pInfo->inputSlotMap); } void destroyExchangeOperatorInfo(void* param, int32_t numOfOutput) { @@ -7065,6 +7067,7 @@ static SArray* extractScanColumnId(SNodeList* pNodeList); static SArray* extractColumnInfo(SNodeList* pNodeList); static SArray* extractColMatchInfo(SNodeList* pNodeList, SDataBlockDescNode* pOutputNodeList, int32_t* numOfOutputCols); static SArray* createSortInfo(SNodeList* pNodeList); +static SArray* createIndexMap(SNodeList* pNodeList); SOperatorInfo* createOperatorTree(SPhysiNode* pPhyNode, SExecTaskInfo* pTaskInfo, SReadHandle* pHandle, uint64_t queryId, uint64_t taskId, STableGroupInfo* pTableGroupInfo) { @@ -7183,7 +7186,8 @@ SOperatorInfo* createOperatorTree(SPhysiNode* pPhyNode, SExecTaskInfo* pTaskInfo SSDataBlock* pResBlock = createOutputBuf_rv1(pPhyNode->pOutputDataBlockDesc); SArray* info = createSortInfo(pSortPhyNode->pSortKeys); - return createSortOperatorInfo(op, pResBlock, info, pTaskInfo); + SArray* slotMap = createIndexMap(pSortPhyNode->pTargets); + return createSortOperatorInfo(op, pResBlock, info, slotMap, pTaskInfo); } else if (QUERY_NODE_PHYSICAL_PLAN_SESSION_WINDOW == nodeType(pPhyNode)) { size_t size = LIST_LENGTH(pPhyNode->pChildren); assert(size == 1); @@ -7303,8 +7307,7 @@ SArray* createSortInfo(SNodeList* pNodeList) { } for (int32_t i = 0; i < numOfCols; ++i) { - STargetNode* pNode = (STargetNode*)nodesListGetNode(pNodeList, i); - SOrderByExprNode* pSortKey = (SOrderByExprNode*)pNode->pExpr; + SOrderByExprNode* pSortKey = (SOrderByExprNode*)nodesListGetNode(pNodeList, i); SBlockOrderInfo bi = {0}; bi.order = (pSortKey->order == ORDER_ASC) ? TSDB_ORDER_ASC : TSDB_ORDER_DESC; bi.nullFirst = (pSortKey->nullOrder == NULL_ORDER_FIRST); @@ -7326,6 +7329,24 @@ SArray* createSortInfo(SNodeList* pNodeList) { return pList; } +SArray* createIndexMap(SNodeList* pNodeList) { + size_t numOfCols = LIST_LENGTH(pNodeList); + SArray* pList = taosArrayInit(numOfCols, sizeof(int32_t)); + if (pList == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return pList; + } + + for (int32_t i = 0; i < numOfCols; ++i) { + STargetNode* pTarget = (STargetNode*)nodesListGetNode(pNodeList, i); + + SColumnNode* pColNode = (SColumnNode*)pTarget->pExpr; + taosArrayPush(pList, &pColNode->slotId); + } + + return pList; +} + SArray* extractColMatchInfo(SNodeList* pNodeList, SDataBlockDescNode* pOutputNodeList, int32_t* numOfOutputCols) { size_t numOfCols = LIST_LENGTH(pNodeList); SArray* pList = taosArrayInit(numOfCols, sizeof(SColMatchInfo)); diff --git a/source/libs/executor/src/groupoperator.c b/source/libs/executor/src/groupoperator.c index b3a8e09f16..7f5935d339 100644 --- a/source/libs/executor/src/groupoperator.c +++ b/source/libs/executor/src/groupoperator.c @@ -365,12 +365,12 @@ static SSDataBlock* doPartitionData(SOperatorInfo* pOperator, bool* newgroup) { } int32_t numOfBufPage = pInfo->sortBufSize / pInfo->bufPageSize; - pInfo->pSortHandle = tsortCreateSortHandle(pInfo->pSortInfo, SORT_SINGLESOURCE_SORT, pInfo->bufPageSize, numOfBufPage, + pInfo->pSortHandle = tsortCreateSortHandle(pInfo->pSortInfo, pInfo->inputSlotMap, SORT_SINGLESOURCE_SORT, pInfo->bufPageSize, numOfBufPage, pInfo->pDataBlock, pTaskInfo->id.str); tsortSetFetchRawDataFp(pInfo->pSortHandle, loadNextDataBlock); - SGenericSource* ps = taosMemoryCalloc(1, sizeof(SGenericSource)); + SSortSource* ps = taosMemoryCalloc(1, sizeof(SSortSource)); ps->param = pOperator->pDownstream[0]; tsortAddSource(pInfo->pSortHandle, ps); diff --git a/source/libs/executor/src/tsort.c b/source/libs/executor/src/tsort.c index b226106e01..965cf14400 100644 --- a/source/libs/executor/src/tsort.c +++ b/source/libs/executor/src/tsort.c @@ -32,6 +32,7 @@ struct SSortHandle { SDiskbasedBuf *pBuf; SArray *pSortInfo; + SArray *pIndexMap; SArray *pOrderedSource; _sort_fetch_block_fn_t fetchfp; @@ -85,13 +86,14 @@ static SSDataBlock* createDataBlock_rv(SSchema* pSchema, int32_t numOfCols) { * @param type * @return */ -SSortHandle* tsortCreateSortHandle(SArray* pSortInfo, int32_t type, int32_t pageSize, int32_t numOfPages, SSDataBlock* pBlock, const char* idstr) { +SSortHandle* tsortCreateSortHandle(SArray* pSortInfo, SArray* pIndexMap, int32_t type, int32_t pageSize, int32_t numOfPages, SSDataBlock* pBlock, const char* idstr) { SSortHandle* pSortHandle = taosMemoryCalloc(1, sizeof(SSortHandle)); pSortHandle->type = type; pSortHandle->pageSize = pageSize; pSortHandle->numOfPages = numOfPages; pSortHandle->pSortInfo = pSortInfo; + pSortHandle->pIndexMap = pIndexMap; pSortHandle->pDataBlock = createOneDataBlock(pBlock); pSortHandle->pOrderedSource = taosArrayInit(4, POINTER_BYTES); @@ -529,7 +531,7 @@ static int32_t createInitialSortedMultiSources(SSortHandle* pHandle) { pHandle->pDataBlock = createOneDataBlock(pBlock); } - int32_t code = blockDataMerge(pHandle->pDataBlock, pBlock); + int32_t code = blockDataMerge(pHandle->pDataBlock, pBlock, pHandle->pIndexMap); if (code != 0) { return code; } diff --git a/source/libs/executor/test/sortTests.cpp b/source/libs/executor/test/sortTests.cpp index 5bb184e5b7..efa0581fd0 100644 --- a/source/libs/executor/test/sortTests.cpp +++ b/source/libs/executor/test/sortTests.cpp @@ -209,7 +209,7 @@ TEST(testCase, inMem_sort_Test) { SArray* orderInfo = taosArrayInit(1, sizeof(SBlockOrderInfo)); taosArrayPush(orderInfo, &oi); - SSortHandle* phandle = tsortCreateSortHandle(orderInfo, SORT_SINGLESOURCE_SORT, 1024, 5, NULL, "test_abc"); + SSortHandle* phandle = tsortCreateSortHandle(orderInfo, NULL, SORT_SINGLESOURCE_SORT, 1024, 5, NULL, "test_abc"); tsortSetFetchRawDataFp(phandle, getSingleColDummyBlock); _info* pInfo = (_info*) taosMemoryCalloc(1, sizeof(_info)); @@ -298,7 +298,7 @@ TEST(testCase, external_mem_sort_Test) { SArray* orderInfo = taosArrayInit(1, sizeof(SBlockOrderInfo)); taosArrayPush(orderInfo, &oi); - SSortHandle* phandle = tsortCreateSortHandle(orderInfo, SORT_SINGLESOURCE_SORT, 128, 3, NULL, "test_abc"); + SSortHandle* phandle = tsortCreateSortHandle(orderInfo, NULL, SORT_SINGLESOURCE_SORT, 128, 3, NULL, "test_abc"); tsortSetFetchRawDataFp(phandle, getSingleColDummyBlock); SSortSource* ps = static_cast(taosMemoryCalloc(1, sizeof(SSortSource))); @@ -365,7 +365,7 @@ TEST(testCase, ordered_merge_sort_Test) { taosArrayPush(pBlock->pDataBlock, &colInfo); } - SSortHandle* phandle = tsortCreateSortHandle(orderInfo, SORT_MULTISOURCE_MERGE, 1024, 5, pBlock,"test_abc"); + SSortHandle* phandle = tsortCreateSortHandle(orderInfo, NULL, SORT_MULTISOURCE_MERGE, 1024, 5, pBlock,"test_abc"); tsortSetFetchRawDataFp(phandle, getSingleColDummyBlock); tsortSetComparFp(phandle, docomp); From 65a21314933ba9ec4892b1e5c301d18436591067 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Fri, 8 Apr 2022 14:40:02 +0800 Subject: [PATCH 22/24] : fix index map error from table scan to sort output --- source/libs/executor/src/executorimpl.c | 30 +++++++++++++++---------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 9b2772ddfa..0cc73bb2cd 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -7066,7 +7066,7 @@ static SArray* extractTableIdList(const STableGroupInfo* pTableGroupInfo); static SArray* extractScanColumnId(SNodeList* pNodeList); static SArray* extractColumnInfo(SNodeList* pNodeList); static SArray* extractColMatchInfo(SNodeList* pNodeList, SDataBlockDescNode* pOutputNodeList, int32_t* numOfOutputCols); -static SArray* createSortInfo(SNodeList* pNodeList); +static SArray* createSortInfo(SNodeList* pNodeList, SNodeList* pNodeListTarget); static SArray* createIndexMap(SNodeList* pNodeList); SOperatorInfo* createOperatorTree(SPhysiNode* pPhyNode, SExecTaskInfo* pTaskInfo, SReadHandle* pHandle, @@ -7185,7 +7185,7 @@ SOperatorInfo* createOperatorTree(SPhysiNode* pPhyNode, SExecTaskInfo* pTaskInfo SSortPhysiNode* pSortPhyNode = (SSortPhysiNode*)pPhyNode; SSDataBlock* pResBlock = createOutputBuf_rv1(pPhyNode->pOutputDataBlockDesc); - SArray* info = createSortInfo(pSortPhyNode->pSortKeys); + SArray* info = createSortInfo(pSortPhyNode->pSortKeys, pSortPhyNode->pTargets); SArray* slotMap = createIndexMap(pSortPhyNode->pTargets); return createSortOperatorInfo(op, pResBlock, info, slotMap, pTaskInfo); } else if (QUERY_NODE_PHYSICAL_PLAN_SESSION_WINDOW == nodeType(pPhyNode)) { @@ -7298,7 +7298,7 @@ SArray* extractColumnInfo(SNodeList* pNodeList) { return pList; } -SArray* createSortInfo(SNodeList* pNodeList) { +SArray* createSortInfo(SNodeList* pNodeList, SNodeList* pNodeListTarget) { size_t numOfCols = LIST_LENGTH(pNodeList); SArray* pList = taosArrayInit(numOfCols, sizeof(SBlockOrderInfo)); if (pList == NULL) { @@ -7313,16 +7313,22 @@ SArray* createSortInfo(SNodeList* pNodeList) { bi.nullFirst = (pSortKey->nullOrder == NULL_ORDER_FIRST); SColumnNode* pColNode = (SColumnNode*)pSortKey->pExpr; - bi.slotId = pColNode->slotId; - // pColNode->order; - // SColumn c = {0}; - // c.slotId = pColNode->slotId; - // c.colId = pColNode->colId; - // c.type = pColNode->node.resType.type; - // c.bytes = pColNode->node.resType.bytes; - // c.precision = pColNode->node.resType.precision; - // c.scale = pColNode->node.resType.scale; + bool found = false; + for (int32_t j = 0; j < LIST_LENGTH(pNodeListTarget); ++j) { + STargetNode* pTarget = (STargetNode*)nodesListGetNode(pNodeListTarget, j); + + SColumnNode* pColNodeT = (SColumnNode*)pTarget->pExpr; + if(pColNode->slotId == pColNodeT->slotId){ // to find slotId in PhysiSort OutputDataBlockDesc + bi.slotId = pTarget->slotId; + found = true; + break; + } + } + + if(!found){ + qError("sort slot id does not found"); + } taosArrayPush(pList, &bi); } From 460d43f22c4c16321cf371e6a4d79ee3accd3092 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Fri, 8 Apr 2022 16:53:09 +0800 Subject: [PATCH 23/24] : fix index map error from table scan to sort output --- source/common/src/tdatablock.c | 12 +++++++----- source/libs/executor/inc/tsort.h | 5 ----- source/libs/executor/src/executorimpl.c | 3 +-- source/libs/executor/src/tsort.c | 8 +++++++- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/source/common/src/tdatablock.c b/source/common/src/tdatablock.c index abcb4283d2..111dfc7377 100644 --- a/source/common/src/tdatablock.c +++ b/source/common/src/tdatablock.c @@ -315,6 +315,7 @@ int32_t blockDataUpdateTsWindow(SSDataBlock* pDataBlock) { return 0; } +// if pIndexMap = NULL, merger one column by on column int32_t blockDataMerge(SSDataBlock* pDest, const SSDataBlock* pSrc, SArray* pIndexMap) { assert(pSrc != NULL && pDest != NULL); @@ -380,17 +381,18 @@ int32_t blockDataSplitRows(SSDataBlock* pBlock, bool hasVarCol, int32_t startInd size_t payloadSize = pageSize - (headerSize + colHeaderSize); // TODO speedup by checking if the whole page can fit in firstly. - /*if (!hasVarCol) { + if (!hasVarCol) { size_t rowSize = blockDataGetRowSize(pBlock); - int32_t capacity = (payloadSize / (rowSize * 8 + bitmapChar * numOfCols)) * 8; //if pageSize = 128, rowSize = 2, it will core in doAddToBuf:assert(size <= getBufPageSize(pHandle->pBuf)); + int32_t capacity = payloadSize / (rowSize + numOfCols * bitmapChar / 8.0); + ASSERT(capacity > 0); - *stopIndex = startIndex + capacity; + *stopIndex = startIndex + capacity - 1; if (*stopIndex >= numOfRows) { *stopIndex = numOfRows - 1; } return TSDB_CODE_SUCCESS; - }*/ + } // iterate the rows that can be fit in this buffer page int32_t size = (headerSize + colHeaderSize); @@ -532,7 +534,7 @@ int32_t blockDataFromBuf(SSDataBlock* pBlock, const char* buf) { size_t metaSize = pBlock->info.rows * sizeof(int32_t); if (IS_VAR_DATA_TYPE(pCol->info.type)) { - char* tmp = taosMemoryRealloc(pCol->varmeta.offset, metaSize); + char* tmp = taosMemoryRealloc(pCol->varmeta.offset, metaSize); // preview calloc is too small if (tmp == NULL) { return TSDB_CODE_OUT_OF_MEMORY; } diff --git a/source/libs/executor/inc/tsort.h b/source/libs/executor/inc/tsort.h index f012cc2448..c584df05dd 100644 --- a/source/libs/executor/inc/tsort.h +++ b/source/libs/executor/inc/tsort.h @@ -52,11 +52,6 @@ typedef struct SMsortComparParam { SArray *orderInfo; // SArray } SMsortComparParam; -struct STupleHandle { - SSDataBlock* pBlock; - int32_t rowIndex; -}; - typedef struct SSortHandle SSortHandle; typedef struct STupleHandle STupleHandle; diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 0cc73bb2cd..a8857dbc1d 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -4734,8 +4734,7 @@ static void appendOneRowToDataBlock(SSDataBlock* pBlock, STupleHandle* pTupleHan for (int32_t i = 0; i < pBlock->info.numOfCols; ++i) { SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, i); - SColumnInfoData* pColInfoSrc = taosArrayGet(pTupleHandle->pBlock->pDataBlock, i); - bool isNull = colDataIsNull(pColInfoSrc, 0, pTupleHandle->rowIndex, NULL); + bool isNull = tsortIsNullVal(pTupleHandle, i); if (isNull) { colDataAppend(pColInfo, pBlock->info.rows, NULL, true); } else { diff --git a/source/libs/executor/src/tsort.c b/source/libs/executor/src/tsort.c index 965cf14400..3155b69a86 100644 --- a/source/libs/executor/src/tsort.c +++ b/source/libs/executor/src/tsort.c @@ -24,6 +24,11 @@ #include "tutil.h" #include "tcompare.h" +struct STupleHandle { + SSDataBlock* pBlock; + int32_t rowIndex; +}; + struct SSortHandle { int32_t type; @@ -678,7 +683,8 @@ STupleHandle* tsortNextTuple(SSortHandle* pHandle) { } bool tsortIsNullVal(STupleHandle* pVHandle, int32_t colIndex) { - return false; + SColumnInfoData* pColInfoSrc = taosArrayGet(pVHandle->pBlock->pDataBlock, colIndex); + return colDataIsNull(pColInfoSrc, 0, pVHandle->rowIndex, NULL); } void* tsortGetValue(STupleHandle* pVHandle, int32_t colIndex) { From 388ab2f50dcaceff343035f79f0b2be2f15ea6f1 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Sat, 9 Apr 2022 10:31:15 +0800 Subject: [PATCH 24/24] :fix compile error --- source/libs/executor/inc/executorimpl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/libs/executor/inc/executorimpl.h b/source/libs/executor/inc/executorimpl.h index ba49db2c6f..6a1ded6441 100644 --- a/source/libs/executor/inc/executorimpl.h +++ b/source/libs/executor/inc/executorimpl.h @@ -681,7 +681,7 @@ SOperatorInfo* createAggregateOperatorInfo(SOperatorInfo* downstream, SExprInfo* SExecTaskInfo* pTaskInfo, const STableGroupInfo* pTableGroupInfo); SOperatorInfo* createMultiTableAggOperatorInfo(SOperatorInfo* downstream, SExprInfo* pExprInfo, int32_t numOfCols, SSDataBlock* pResBlock, SExecTaskInfo* pTaskInfo, const STableGroupInfo* pTableGroupInfo); -SOperatorInfo* createProjectOperatorInfo(SOperatorInfo* downstream, SExprInfo* pExprInfo, int32_t num, SSDataBlock* pResBlock, SLimit* pLimit, SExecTaskInfo* pTaskInfo); +SOperatorInfo* createProjectOperatorInfo(SOperatorInfo* downstream, SExprInfo* pExprInfo, int32_t num, SSDataBlock* pResBlock, SLimit* pLimit, SLimit* pSlimit, SExecTaskInfo* pTaskInfo); SOperatorInfo *createSortOperatorInfo(SOperatorInfo* downstream, SSDataBlock* pResBlock, SArray* pSortInfo, SArray* pIndexMap, SExecTaskInfo* pTaskInfo); SOperatorInfo* createSortedMergeOperatorInfo(SOperatorInfo** downstream, int32_t numOfDownstream, SExprInfo* pExprInfo, int32_t num, SArray* pSortInfo, SArray* pGroupInfo, SExecTaskInfo* pTaskInfo);