From 3257c7c08216af559cf6a7aa656719380e630d61 Mon Sep 17 00:00:00 2001 From: factosea <285808407@qq.com> Date: Mon, 1 Apr 2024 19:44:26 +0800 Subject: [PATCH] fix: tag is null when table is empty --- source/libs/executor/src/executil.c | 3 +++ source/libs/executor/src/scanoperator.c | 5 +++-- source/libs/parser/src/parTranslater.c | 22 +++++++++++++++++----- source/libs/planner/src/planLogicCreater.c | 3 +++ 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/source/libs/executor/src/executil.c b/source/libs/executor/src/executil.c index bb89fb587b..1d58666625 100644 --- a/source/libs/executor/src/executil.c +++ b/source/libs/executor/src/executil.c @@ -2132,6 +2132,9 @@ int32_t buildGroupIdMapForAllTables(STableListInfo* pTableListInfo, SReadHandle* } pTableListInfo->oneTableForEachGroup = groupByTbname; + if (numOfTables == 1 && pTableListInfo->idInfo.tableType == TSDB_CHILD_TABLE) { + pTableListInfo->oneTableForEachGroup = true; + } if (groupSort && groupByTbname) { taosArraySort(pTableListInfo->pTableList, orderbyGroupIdComparFn); diff --git a/source/libs/executor/src/scanoperator.c b/source/libs/executor/src/scanoperator.c index 51edfcb42c..49006186bb 100644 --- a/source/libs/executor/src/scanoperator.c +++ b/source/libs/executor/src/scanoperator.c @@ -889,14 +889,15 @@ static SSDataBlock* doGroupedTableScan(SOperatorInfo* pOperator) { if (pTableScanInfo->countState < TABLE_COUNT_STATE_END) { STableListInfo* pTableListInfo = pTableScanInfo->base.pTableListInfo; - if (pTableListInfo->oneTableForEachGroup || pTableListInfo->groupOffset) { // group by tbname, group by tag + sort + if (pTableListInfo->oneTableForEachGroup || pTableListInfo->groupOffset) { // group by tbname, group by tag + sort if (pTableScanInfo->countState < TABLE_COUNT_STATE_PROCESSED) { pTableScanInfo->countState = TABLE_COUNT_STATE_PROCESSED; STableKeyInfo* pStart = (STableKeyInfo*)tableListGetInfo(pTableScanInfo->base.pTableListInfo, pTableScanInfo->tableStartIndex); + if (NULL == pStart) return NULL; return getBlockForEmptyTable(pOperator, pStart); } - } else { // group by tag + no sort + } else { // group by tag + no sort int32_t numOfTables = tableListGetSize(pTableListInfo); if (pTableScanInfo->tableEndIndex + 1 >= numOfTables) { // get empty group, mark processed & rm from hash diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index 6e34ab1b3b..72cc2df632 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -2702,6 +2702,15 @@ static bool hasTbnameFunction(SNodeList* pPartitionByList) { return false; } +static bool fromSubtable(SNode* table) { + if (NULL == table) return false; + if (table->type == QUERY_NODE_REAL_TABLE && ((SRealTableNode*)table)->pMeta && + ((SRealTableNode*)table)->pMeta->tableType == TSDB_CHILD_TABLE) { + return true; + } + return false; +} + static EDealRes doCheckExprForGroupBy(SNode** pNode, void* pContext) { STranslateContext* pCxt = (STranslateContext*)pContext; SSelectStmt* pSelect = (SSelectStmt*)pCxt->pCurrStmt; @@ -2795,15 +2804,18 @@ static EDealRes doCheckAggColCoexist(SNode** pNode, void* pContext) { return DEAL_RES_IGNORE_CHILD; } SNode* pPartKey = NULL; - bool partionByTbname = hasTbnameFunction(((SSelectStmt*)pCxt->pTranslateCxt->pCurrStmt)->pPartitionByList); + bool partionByTbname = false; + if (fromSubtable(((SSelectStmt*)pCxt->pTranslateCxt->pCurrStmt)->pFromTable) || + hasTbnameFunction(((SSelectStmt*)pCxt->pTranslateCxt->pCurrStmt)->pPartitionByList)) { + partionByTbname = true; + } FOREACH(pPartKey, ((SSelectStmt*)pCxt->pTranslateCxt->pCurrStmt)->pPartitionByList) { if (nodesEqualNode(pPartKey, *pNode)) { return rewriteExprToGroupKeyFunc(pCxt->pTranslateCxt, pNode); } - if (partionByTbname && QUERY_NODE_COLUMN == nodeType(*pNode) && - ((SColumnNode*)*pNode)->colType == COLUMN_TYPE_TAG) { - return rewriteExprToGroupKeyFunc(pCxt->pTranslateCxt, pNode); - } + } + if (partionByTbname && QUERY_NODE_COLUMN == nodeType(*pNode) && ((SColumnNode*)*pNode)->colType == COLUMN_TYPE_TAG) { + return rewriteExprToGroupKeyFunc(pCxt->pTranslateCxt, pNode); } if (isScanPseudoColumnFunc(*pNode) || QUERY_NODE_COLUMN == nodeType(*pNode)) { pCxt->existCol = true; diff --git a/source/libs/planner/src/planLogicCreater.c b/source/libs/planner/src/planLogicCreater.c index c34d8ac64f..7ce623e788 100644 --- a/source/libs/planner/src/planLogicCreater.c +++ b/source/libs/planner/src/planLogicCreater.c @@ -494,6 +494,9 @@ static int32_t createScanLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect } else if (pSelect->pPartitionByList) { isCountByTag = !keysHasCol(pSelect->pPartitionByList); } + if (pScan->tableType == TSDB_CHILD_TABLE) { + isCountByTag = true; + } } pScan->isCountByTag = isCountByTag;