From b9795fab9574e75b64128c41a10a56a656dae3bd Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Mon, 11 Jul 2022 19:49:48 +0800 Subject: [PATCH] fix(query): histogram function interval query for super table missed some child table records. TD-17021 --- source/libs/function/inc/builtinsimpl.h | 1 + source/libs/function/src/builtins.c | 2 +- source/libs/function/src/builtinsimpl.c | 27 +++++++++++++++++++------ 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/source/libs/function/inc/builtinsimpl.h b/source/libs/function/inc/builtinsimpl.h index 06944edadd..e8e37e5c6b 100644 --- a/source/libs/function/inc/builtinsimpl.h +++ b/source/libs/function/inc/builtinsimpl.h @@ -157,6 +157,7 @@ int32_t elapsedCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx); bool getHistogramFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); bool histogramFunctionSetup(SqlFunctionCtx *pCtx, SResultRowEntryInfo* pResultInfo); int32_t histogramFunction(SqlFunctionCtx* pCtx); +int32_t histogramFunctionPartial(SqlFunctionCtx* pCtx); int32_t histogramFunctionMerge(SqlFunctionCtx* pCtx); int32_t histogramFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock); int32_t histogramPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock); diff --git a/source/libs/function/src/builtins.c b/source/libs/function/src/builtins.c index fc87ba964a..61a26a7b40 100644 --- a/source/libs/function/src/builtins.c +++ b/source/libs/function/src/builtins.c @@ -2323,7 +2323,7 @@ const SBuiltinFuncDefinition funcMgtBuiltins[] = { .translateFunc = translateHistogramPartial, .getEnvFunc = getHistogramFuncEnv, .initFunc = histogramFunctionSetup, - .processFunc = histogramFunction, + .processFunc = histogramFunctionPartial, .finalizeFunc = histogramPartialFinalize, .invertFunc = NULL, .combineFunc = histogramCombine, diff --git a/source/libs/function/src/builtinsimpl.c b/source/libs/function/src/builtinsimpl.c index 932bfb8793..359352951f 100644 --- a/source/libs/function/src/builtinsimpl.c +++ b/source/libs/function/src/builtinsimpl.c @@ -4098,7 +4098,7 @@ bool histogramFunctionSetup(SqlFunctionCtx* pCtx, SResultRowEntryInfo* pResultIn return true; } -int32_t histogramFunction(SqlFunctionCtx* pCtx) { +static int32_t histogramFunctionImpl(SqlFunctionCtx* pCtx, bool isPartial) { SHistoFuncInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx)); SInputColumnInfoData* pInput = &pCtx->input; @@ -4130,10 +4130,22 @@ int32_t histogramFunction(SqlFunctionCtx* pCtx) { } } - SET_VAL(GET_RES_INFO(pCtx), numOfElems, pInfo->numOfBins); + if (!isPartial) { + SET_VAL(GET_RES_INFO(pCtx), numOfElems, pInfo->numOfBins); + } else { + SET_VAL(GET_RES_INFO(pCtx), numOfElems, 1); + } return TSDB_CODE_SUCCESS; } +int32_t histogramFunction(SqlFunctionCtx* pCtx) { + return histogramFunctionImpl(pCtx, false); +} + +int32_t histogramFunctionPartial(SqlFunctionCtx* pCtx) { + return histogramFunctionImpl(pCtx, true); +} + static void histogramTransferInfo(SHistoFuncInfo* pInput, SHistoFuncInfo* pOutput) { pOutput->normalized = pInput->normalized; pOutput->numOfBins = pInput->numOfBins; @@ -4153,10 +4165,12 @@ int32_t histogramFunctionMerge(SqlFunctionCtx* pCtx) { SHistoFuncInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx)); int32_t start = pInput->startRowIndex; - char* data = colDataGetData(pCol, start); - SHistoFuncInfo* pInputInfo = (SHistoFuncInfo*)varDataVal(data); - histogramTransferInfo(pInputInfo, pInfo); + for(int32_t i = start; i < start + pInput->numOfRows; ++i) { + char* data = colDataGetData(pCol, i); + SHistoFuncInfo* pInputInfo = (SHistoFuncInfo*)varDataVal(data); + histogramTransferInfo(pInputInfo, pInfo); + } SET_VAL(GET_RES_INFO(pCtx), pInfo->numOfBins, pInfo->numOfBins); return TSDB_CODE_SUCCESS; @@ -4199,6 +4213,7 @@ int32_t histogramFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { } int32_t histogramPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { + SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx); SHistoFuncInfo* pInfo = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx)); int32_t resultBytes = getHistogramInfoSize(); char* res = taosMemoryCalloc(resultBytes + VARSTR_HEADER_SIZE, sizeof(char)); @@ -4212,7 +4227,7 @@ int32_t histogramPartialFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { colDataAppend(pCol, pBlock->info.rows, res, false); taosMemoryFree(res); - return 1; + return pResInfo->numOfRes; } int32_t histogramCombine(SqlFunctionCtx* pDestCtx, SqlFunctionCtx* pSourceCtx) { -- GitLab