From a0e13e04fe0de2a2232fd2ab6ebb448fd992b253 Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Thu, 3 Nov 2022 18:27:43 +0800 Subject: [PATCH] fix(query): add mode with selectivity rows --- source/libs/function/src/builtins.c | 2 +- source/libs/function/src/builtinsimpl.c | 30 +++++++++++++++++++++---- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/source/libs/function/src/builtins.c b/source/libs/function/src/builtins.c index afe639b856..d3f03e8e9c 100644 --- a/source/libs/function/src/builtins.c +++ b/source/libs/function/src/builtins.c @@ -2734,7 +2734,7 @@ const SBuiltinFuncDefinition funcMgtBuiltins[] = { { .name = "mode", .type = FUNCTION_TYPE_MODE, - .classification = FUNC_MGT_AGG_FUNC, + .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SELECT_FUNC, .translateFunc = translateMode, .getEnvFunc = getModeFuncEnv, .initFunc = modeFunctionSetup, diff --git a/source/libs/function/src/builtinsimpl.c b/source/libs/function/src/builtinsimpl.c index c8f0b3d826..43a8ef78d8 100644 --- a/source/libs/function/src/builtinsimpl.c +++ b/source/libs/function/src/builtinsimpl.c @@ -254,8 +254,9 @@ typedef struct SUniqueInfo { } SUniqueInfo; typedef struct SModeItem { - int64_t count; - char data[]; + int64_t count; + STuplePos tuplePos; + char data[]; } SModeItem; typedef struct SModeInfo { @@ -263,6 +264,10 @@ typedef struct SModeInfo { uint8_t colType; int16_t colBytes; SHashObj* pHash; + + STuplePos nullTuplePos; + bool nullTupleSaved; + char pItems[]; } SModeInfo; @@ -5377,10 +5382,13 @@ bool modeFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) { } else { pInfo->pHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK); } + pInfo->nullTupleSaved = false; + pInfo->nullTuplePos.pageId = -1; + return true; } -static void doModeAdd(SModeInfo* pInfo, char* data) { +static void doModeAdd(SModeInfo* pInfo, int32_t rowIndex, SqlFunctionCtx* pCtx, char* data) { int32_t hashKeyBytes = IS_STR_DATA_TYPE(pInfo->colType) ? varDataTLen(data) : pInfo->colBytes; SModeItem** pHashItem = taosHashGet(pInfo->pHash, data, hashKeyBytes); if (pHashItem == NULL) { @@ -5389,10 +5397,17 @@ static void doModeAdd(SModeInfo* pInfo, char* data) { memcpy(pItem->data, data, hashKeyBytes); pItem->count += 1; + if (pCtx->subsidiaries.num > 0) { + pItem->tuplePos = saveTupleData(pCtx, rowIndex, pCtx->pSrcBlock, NULL); + } + taosHashPut(pInfo->pHash, data, hashKeyBytes, &pItem, sizeof(SModeItem*)); pInfo->numOfPoints++; } else { (*pHashItem)->count += 1; + if (pCtx->subsidiaries.num > 0) { + updateTupleData(pCtx, rowIndex, pCtx->pSrcBlock, &((*pHashItem)->tuplePos)); + } } } @@ -5414,7 +5429,7 @@ int32_t modeFunction(SqlFunctionCtx* pCtx) { } numOfElems++; - doModeAdd(pInfo, data); + doModeAdd(pInfo, i, pCtx, data); if (sizeof(SModeInfo) + pInfo->numOfPoints * (sizeof(SModeItem) + pInfo->colBytes) >= MODE_MAX_RESULT_SIZE) { taosHashCleanup(pInfo->pHash); @@ -5422,6 +5437,11 @@ int32_t modeFunction(SqlFunctionCtx* pCtx) { } } + if (numOfElems == 0 && pCtx->subsidiaries.num > 0 && !pInfo->nullTupleSaved) { + pInfo->nullTuplePos = saveTupleData(pCtx, pInput->startRowIndex, pCtx->pSrcBlock, NULL); + pInfo->nullTupleSaved = true; + } + SET_VAL(pResInfo, numOfElems, 1); return TSDB_CODE_SUCCESS; @@ -5447,8 +5467,10 @@ int32_t modeFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { if (maxCount != 0) { SModeItem* pResItem = (SModeItem*)(pInfo->pItems + resIndex * (sizeof(SModeItem) + pInfo->colBytes)); colDataAppend(pCol, currentRow, pResItem->data, false); + setSelectivityValue(pCtx, pBlock, &pResItem->tuplePos, currentRow); } else { colDataAppendNULL(pCol, currentRow); + setSelectivityValue(pCtx, pBlock, &pInfo->nullTuplePos, currentRow); } return pResInfo->numOfRes; -- GitLab