提交 a0e13e04 编写于 作者: G Ganlin Zhao

fix(query): add mode with selectivity rows

上级 794bca79
...@@ -2734,7 +2734,7 @@ const SBuiltinFuncDefinition funcMgtBuiltins[] = { ...@@ -2734,7 +2734,7 @@ const SBuiltinFuncDefinition funcMgtBuiltins[] = {
{ {
.name = "mode", .name = "mode",
.type = FUNCTION_TYPE_MODE, .type = FUNCTION_TYPE_MODE,
.classification = FUNC_MGT_AGG_FUNC, .classification = FUNC_MGT_AGG_FUNC | FUNC_MGT_SELECT_FUNC,
.translateFunc = translateMode, .translateFunc = translateMode,
.getEnvFunc = getModeFuncEnv, .getEnvFunc = getModeFuncEnv,
.initFunc = modeFunctionSetup, .initFunc = modeFunctionSetup,
......
...@@ -254,8 +254,9 @@ typedef struct SUniqueInfo { ...@@ -254,8 +254,9 @@ typedef struct SUniqueInfo {
} SUniqueInfo; } SUniqueInfo;
typedef struct SModeItem { typedef struct SModeItem {
int64_t count; int64_t count;
char data[]; STuplePos tuplePos;
char data[];
} SModeItem; } SModeItem;
typedef struct SModeInfo { typedef struct SModeInfo {
...@@ -263,6 +264,10 @@ typedef struct SModeInfo { ...@@ -263,6 +264,10 @@ typedef struct SModeInfo {
uint8_t colType; uint8_t colType;
int16_t colBytes; int16_t colBytes;
SHashObj* pHash; SHashObj* pHash;
STuplePos nullTuplePos;
bool nullTupleSaved;
char pItems[]; char pItems[];
} SModeInfo; } SModeInfo;
...@@ -5377,10 +5382,13 @@ bool modeFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) { ...@@ -5377,10 +5382,13 @@ bool modeFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResInfo) {
} else { } else {
pInfo->pHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK); pInfo->pHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK);
} }
pInfo->nullTupleSaved = false;
pInfo->nullTuplePos.pageId = -1;
return true; 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; int32_t hashKeyBytes = IS_STR_DATA_TYPE(pInfo->colType) ? varDataTLen(data) : pInfo->colBytes;
SModeItem** pHashItem = taosHashGet(pInfo->pHash, data, hashKeyBytes); SModeItem** pHashItem = taosHashGet(pInfo->pHash, data, hashKeyBytes);
if (pHashItem == NULL) { if (pHashItem == NULL) {
...@@ -5389,10 +5397,17 @@ static void doModeAdd(SModeInfo* pInfo, char* data) { ...@@ -5389,10 +5397,17 @@ static void doModeAdd(SModeInfo* pInfo, char* data) {
memcpy(pItem->data, data, hashKeyBytes); memcpy(pItem->data, data, hashKeyBytes);
pItem->count += 1; pItem->count += 1;
if (pCtx->subsidiaries.num > 0) {
pItem->tuplePos = saveTupleData(pCtx, rowIndex, pCtx->pSrcBlock, NULL);
}
taosHashPut(pInfo->pHash, data, hashKeyBytes, &pItem, sizeof(SModeItem*)); taosHashPut(pInfo->pHash, data, hashKeyBytes, &pItem, sizeof(SModeItem*));
pInfo->numOfPoints++; pInfo->numOfPoints++;
} else { } else {
(*pHashItem)->count += 1; (*pHashItem)->count += 1;
if (pCtx->subsidiaries.num > 0) {
updateTupleData(pCtx, rowIndex, pCtx->pSrcBlock, &((*pHashItem)->tuplePos));
}
} }
} }
...@@ -5414,7 +5429,7 @@ int32_t modeFunction(SqlFunctionCtx* pCtx) { ...@@ -5414,7 +5429,7 @@ int32_t modeFunction(SqlFunctionCtx* pCtx) {
} }
numOfElems++; numOfElems++;
doModeAdd(pInfo, data); doModeAdd(pInfo, i, pCtx, data);
if (sizeof(SModeInfo) + pInfo->numOfPoints * (sizeof(SModeItem) + pInfo->colBytes) >= MODE_MAX_RESULT_SIZE) { if (sizeof(SModeInfo) + pInfo->numOfPoints * (sizeof(SModeItem) + pInfo->colBytes) >= MODE_MAX_RESULT_SIZE) {
taosHashCleanup(pInfo->pHash); taosHashCleanup(pInfo->pHash);
...@@ -5422,6 +5437,11 @@ int32_t modeFunction(SqlFunctionCtx* pCtx) { ...@@ -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); SET_VAL(pResInfo, numOfElems, 1);
return TSDB_CODE_SUCCESS; return TSDB_CODE_SUCCESS;
...@@ -5447,8 +5467,10 @@ int32_t modeFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { ...@@ -5447,8 +5467,10 @@ int32_t modeFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) {
if (maxCount != 0) { if (maxCount != 0) {
SModeItem* pResItem = (SModeItem*)(pInfo->pItems + resIndex * (sizeof(SModeItem) + pInfo->colBytes)); SModeItem* pResItem = (SModeItem*)(pInfo->pItems + resIndex * (sizeof(SModeItem) + pInfo->colBytes));
colDataAppend(pCol, currentRow, pResItem->data, false); colDataAppend(pCol, currentRow, pResItem->data, false);
setSelectivityValue(pCtx, pBlock, &pResItem->tuplePos, currentRow);
} else { } else {
colDataAppendNULL(pCol, currentRow); colDataAppendNULL(pCol, currentRow);
setSelectivityValue(pCtx, pBlock, &pInfo->nullTuplePos, currentRow);
} }
return pResInfo->numOfRes; return pResInfo->numOfRes;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册