From 363f062a85a7c23d2303ae28b08893bf85e08db1 Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Thu, 29 Sep 2022 16:59:20 +0800 Subject: [PATCH 1/3] fix(query): fix tbname mismatch with multiple rows output functions when used with group by/partition by tbname. TD-19273 --- source/libs/executor/src/executorimpl.c | 8 ++++++++ source/libs/function/src/builtinsimpl.c | 5 ++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 9917791312..8f76a3d72f 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -1273,6 +1273,14 @@ static void doCopyResultToDataBlock(SExprInfo* pExprInfo, int32_t numOfExprs, SR pCtx[j].resultInfo = getResultEntryInfo(pRow, j, rowEntryOffset); if (pCtx[j].fpSet.finalize) { + if (strcmp(pCtx[j].pExpr->pExpr->_function.functionName, "_group_key") == 0) { + // for groupkey along with functions output multiple lines(e.g. Histogram) + // need to fill groupkey result for each output row. + if (pCtx[j].resultInfo->numOfRes != 0) { + pCtx[j].resultInfo->numOfRes = pRow->numOfRows; + } + } + int32_t code = pCtx[j].fpSet.finalize(&pCtx[j], pBlock); if (TAOS_FAILED(code)) { qError("%s build result data block error, code %s", GET_TASKID(pTaskInfo), tstrerror(code)); diff --git a/source/libs/function/src/builtinsimpl.c b/source/libs/function/src/builtinsimpl.c index a8d51905ab..42512d3a11 100644 --- a/source/libs/function/src/builtinsimpl.c +++ b/source/libs/function/src/builtinsimpl.c @@ -6157,7 +6157,10 @@ int32_t groupKeyFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { SGroupKeyInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo); if (pInfo->hasResult) { - colDataAppend(pCol, pBlock->info.rows, pInfo->data, pInfo->isNull ? true : false); + int32_t currentRow = pBlock->info.rows; + for (; currentRow < pBlock->info.rows + pResInfo->numOfRes; ++currentRow) { + colDataAppend(pCol, currentRow, pInfo->data, pInfo->isNull ? true : false); + } } else { pResInfo->numOfRes = 0; } From 3abf6abad6da88c71a7803bf6898146e40c77dcc Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Thu, 29 Sep 2022 16:59:20 +0800 Subject: [PATCH 2/3] fix(query): fix group_key output mismatch with multiple rows output functions TD-19273 --- source/libs/executor/src/executorimpl.c | 8 ++++++++ source/libs/function/src/builtinsimpl.c | 5 ++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 9917791312..8f76a3d72f 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -1273,6 +1273,14 @@ static void doCopyResultToDataBlock(SExprInfo* pExprInfo, int32_t numOfExprs, SR pCtx[j].resultInfo = getResultEntryInfo(pRow, j, rowEntryOffset); if (pCtx[j].fpSet.finalize) { + if (strcmp(pCtx[j].pExpr->pExpr->_function.functionName, "_group_key") == 0) { + // for groupkey along with functions output multiple lines(e.g. Histogram) + // need to fill groupkey result for each output row. + if (pCtx[j].resultInfo->numOfRes != 0) { + pCtx[j].resultInfo->numOfRes = pRow->numOfRows; + } + } + int32_t code = pCtx[j].fpSet.finalize(&pCtx[j], pBlock); if (TAOS_FAILED(code)) { qError("%s build result data block error, code %s", GET_TASKID(pTaskInfo), tstrerror(code)); diff --git a/source/libs/function/src/builtinsimpl.c b/source/libs/function/src/builtinsimpl.c index a8d51905ab..42512d3a11 100644 --- a/source/libs/function/src/builtinsimpl.c +++ b/source/libs/function/src/builtinsimpl.c @@ -6157,7 +6157,10 @@ int32_t groupKeyFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock) { SGroupKeyInfo* pInfo = GET_ROWCELL_INTERBUF(pResInfo); if (pInfo->hasResult) { - colDataAppend(pCol, pBlock->info.rows, pInfo->data, pInfo->isNull ? true : false); + int32_t currentRow = pBlock->info.rows; + for (; currentRow < pBlock->info.rows + pResInfo->numOfRes; ++currentRow) { + colDataAppend(pCol, currentRow, pInfo->data, pInfo->isNull ? true : false); + } } else { pResInfo->numOfRes = 0; } From 3af613ac7356fb4095058bd5603b2cd627f1d792 Mon Sep 17 00:00:00 2001 From: Ganlin Zhao Date: Thu, 29 Sep 2022 17:04:31 +0800 Subject: [PATCH 3/3] Merge branch 'fix/TD-19273' of github.com:taosdata/TDengine into fix/TD-19273 --- source/libs/executor/src/executorimpl.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 8f76a3d72f..ff28199712 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -1274,8 +1274,8 @@ static void doCopyResultToDataBlock(SExprInfo* pExprInfo, int32_t numOfExprs, SR pCtx[j].resultInfo = getResultEntryInfo(pRow, j, rowEntryOffset); if (pCtx[j].fpSet.finalize) { if (strcmp(pCtx[j].pExpr->pExpr->_function.functionName, "_group_key") == 0) { - // for groupkey along with functions output multiple lines(e.g. Histogram) - // need to fill groupkey result for each output row. + // for groupkey along with functions that output multiple lines(e.g. Histogram) + // need to match groupkey result for each output row of that function. if (pCtx[j].resultInfo->numOfRes != 0) { pCtx[j].resultInfo->numOfRes = pRow->numOfRows; }