diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index 38737d7a17..6e34ab1b3b 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -2688,6 +2688,20 @@ static EDealRes rewriteExprToGroupKeyFunc(STranslateContext* pCxt, SNode** pNode return (TSDB_CODE_SUCCESS == pCxt->errCode ? DEAL_RES_IGNORE_CHILD : DEAL_RES_ERROR); } +static bool isTbnameFuction(SNode* pNode) { + return QUERY_NODE_FUNCTION == nodeType(pNode) && FUNCTION_TYPE_TBNAME == ((SFunctionNode*)pNode)->funcType; +} + +static bool hasTbnameFunction(SNodeList* pPartitionByList) { + SNode* pPartKey = NULL; + FOREACH(pPartKey, pPartitionByList) { + if (isTbnameFuction(pPartKey)) { + return true; + } + } + return false; +} + static EDealRes doCheckExprForGroupBy(SNode** pNode, void* pContext) { STranslateContext* pCxt = (STranslateContext*)pContext; SSelectStmt* pSelect = (SSelectStmt*)pCxt->pCurrStmt; @@ -2699,15 +2713,25 @@ static EDealRes doCheckExprForGroupBy(SNode** pNode, void* pContext) { } SNode* pGroupNode = NULL; FOREACH(pGroupNode, getGroupByList(pCxt)) { - if (nodesEqualNode(getGroupByNode(pGroupNode), *pNode)) { + SNode* pActualNode = getGroupByNode(pGroupNode); + if (nodesEqualNode(pActualNode, *pNode)) { return DEAL_RES_IGNORE_CHILD; } + if (isTbnameFuction(pActualNode) && QUERY_NODE_COLUMN == nodeType(*pNode) && + ((SColumnNode*)*pNode)->colType == COLUMN_TYPE_TAG) { + return rewriteExprToGroupKeyFunc(pCxt, pNode); + } } SNode* pPartKey = NULL; + bool partionByTbname = hasTbnameFunction(pSelect->pPartitionByList); FOREACH(pPartKey, pSelect->pPartitionByList) { if (nodesEqualNode(pPartKey, *pNode)) { return rewriteExprToGroupKeyFunc(pCxt, pNode); } + if ((partionByTbname) && QUERY_NODE_COLUMN == nodeType(*pNode) && + ((SColumnNode*)*pNode)->colType == COLUMN_TYPE_TAG) { + return rewriteExprToGroupKeyFunc(pCxt, pNode); + } } if (NULL != pSelect->pWindow && QUERY_NODE_STATE_WINDOW == nodeType(pSelect->pWindow)) { if (nodesEqualNode(((SStateWindowNode*)pSelect->pWindow)->pExpr, *pNode)) { @@ -2771,10 +2795,15 @@ static EDealRes doCheckAggColCoexist(SNode** pNode, void* pContext) { return DEAL_RES_IGNORE_CHILD; } SNode* pPartKey = NULL; + bool partionByTbname = hasTbnameFunction(((SSelectStmt*)pCxt->pTranslateCxt->pCurrStmt)->pPartitionByList); 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 (isScanPseudoColumnFunc(*pNode) || QUERY_NODE_COLUMN == nodeType(*pNode)) { pCxt->existCol = true; @@ -3946,22 +3975,12 @@ static int32_t checkStateExpr(STranslateContext* pCxt, SNode* pNode) { return TSDB_CODE_SUCCESS; } -static bool hasPartitionByTbname(SNodeList* pPartitionByList) { - SNode* pPartKey = NULL; - FOREACH(pPartKey, pPartitionByList) { - if (QUERY_NODE_FUNCTION == nodeType(pPartKey) && FUNCTION_TYPE_TBNAME == ((SFunctionNode*)pPartKey)->funcType) { - return true; - } - } - return false; -} - static int32_t checkStateWindowForStream(STranslateContext* pCxt, SSelectStmt* pSelect) { if (!pCxt->createStream) { return TSDB_CODE_SUCCESS; } if (TSDB_SUPER_TABLE == ((SRealTableNode*)pSelect->pFromTable)->pMeta->tableType && - !hasPartitionByTbname(pSelect->pPartitionByList)) { + !hasTbnameFunction(pSelect->pPartitionByList)) { return generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_STREAM_QUERY, "Unsupported stream query"); } return TSDB_CODE_SUCCESS; @@ -7539,12 +7558,12 @@ static int32_t translateKillTransaction(STranslateContext* pCxt, SKillStmt* pStm static bool crossTableWithoutAggOper(SSelectStmt* pSelect) { return NULL == pSelect->pWindow && !pSelect->hasAggFuncs && !pSelect->hasIndefiniteRowsFunc && !pSelect->hasInterpFunc && TSDB_SUPER_TABLE == ((SRealTableNode*)pSelect->pFromTable)->pMeta->tableType && - !hasPartitionByTbname(pSelect->pPartitionByList); + !hasTbnameFunction(pSelect->pPartitionByList); } static bool crossTableWithUdaf(SSelectStmt* pSelect) { return pSelect->hasUdaf && TSDB_SUPER_TABLE == ((SRealTableNode*)pSelect->pFromTable)->pMeta->tableType && - !hasPartitionByTbname(pSelect->pPartitionByList); + !hasTbnameFunction(pSelect->pPartitionByList); } static int32_t checkCreateStream(STranslateContext* pCxt, SCreateStreamStmt* pStmt) { @@ -7802,7 +7821,7 @@ static int32_t checkStreamQuery(STranslateContext* pCxt, SCreateStreamStmt* pStm SSelectStmt* pSelect = (SSelectStmt*)pStmt->pQuery; if ( (SRealTableNode*)pSelect->pFromTable && ((SRealTableNode*)pSelect->pFromTable)->pMeta && TSDB_SUPER_TABLE == ((SRealTableNode*)pSelect->pFromTable)->pMeta->tableType - && !hasPartitionByTbname(pSelect->pPartitionByList) + && !hasTbnameFunction(pSelect->pPartitionByList) && pSelect->pWindow != NULL && pSelect->pWindow->type == QUERY_NODE_EVENT_WINDOW) { return generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_STREAM_QUERY, "Event window for stream on super table must patitioned by table name"); @@ -7830,7 +7849,7 @@ static int32_t checkStreamQuery(STranslateContext* pCxt, SCreateStreamStmt* pStm if (pSelect->pWindow != NULL && pSelect->pWindow->type == QUERY_NODE_COUNT_WINDOW) { if ( (SRealTableNode*)pSelect->pFromTable && ((SRealTableNode*)pSelect->pFromTable)->pMeta && TSDB_SUPER_TABLE == ((SRealTableNode*)pSelect->pFromTable)->pMeta->tableType - && !hasPartitionByTbname(pSelect->pPartitionByList) ) { + && !hasTbnameFunction(pSelect->pPartitionByList) ) { return generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_STREAM_QUERY, "Count window for stream on super table must patitioned by table name"); } diff --git a/tests/develop-test/2-query/pseudo_column.py b/tests/develop-test/2-query/pseudo_column.py index 1d94df4cff..61ea53433f 100644 --- a/tests/develop-test/2-query/pseudo_column.py +++ b/tests/develop-test/2-query/pseudo_column.py @@ -66,10 +66,10 @@ class TDTestCase: tdSql.query('select * from (select tbname, avg(f) from st partition by tbname) a partition by a.tbname order by a.tbname'); tdSql.checkRows(2) tdSql.checkCols(2) - tdSql.checkData(0, 0, 'ct1'); - tdSql.checkData(0, 1, 6.0); - tdSql.checkData(1, 0, 'ct2'); - tdSql.checkData(1, 1, 12.0); + tdSql.checkData(0, 0, 'ct1') + tdSql.checkData(0, 1, 6.0) + tdSql.checkData(1, 0, 'ct2') + tdSql.checkData(1, 1, 12.0) tdSql.error('select tbname from (select * from st)') tdSql.error('select st.tbname from (select st.tbname from st)') diff --git a/tests/system-test/2-query/count.py b/tests/system-test/2-query/count.py index c06ee28d02..f94e9eac00 100644 --- a/tests/system-test/2-query/count.py +++ b/tests/system-test/2-query/count.py @@ -103,6 +103,8 @@ class TDTestCase: tdSql.checkRows(row) tdSql.query(f'select {function_name}(c1),sum(c1) from {self.stbname} partition by tbname') tdSql.checkRows(row) + tdSql.query(f'select t0, {function_name}(c1),sum(c1) from {self.stbname} partition by tbname') + tdSql.checkRows(row) tdSql.query(f'select {function_name}(c1),sum(c1) from {self.stbname} partition by c1') tdSql.checkRows(0) tdSql.query(f'select {function_name}(c1),sum(c1) from {self.stbname} partition by t0') diff --git a/tests/system-test/2-query/csum.py b/tests/system-test/2-query/csum.py index b16f511491..f5e7456049 100644 --- a/tests/system-test/2-query/csum.py +++ b/tests/system-test/2-query/csum.py @@ -470,7 +470,7 @@ class TDTestCase: tdSql.checkRows(40) # bug need fix - tdSql.query("select tbname , csum(c1), csum(c12) from db.stb1 partition by tbname") + tdSql.query("select tbname , st1, csum(c1), csum(c12) from db.stb1 partition by tbname") tdSql.checkRows(40) tdSql.query("select tbname , csum(st1) from db.stb1 partition by tbname") tdSql.checkRows(70) diff --git a/tests/system-test/2-query/group_partition.py b/tests/system-test/2-query/group_partition.py index a20b124c33..2d206ce228 100644 --- a/tests/system-test/2-query/group_partition.py +++ b/tests/system-test/2-query/group_partition.py @@ -100,6 +100,12 @@ class TDTestCase: tdSql.query(f"select t2, t3, tbname, count(*) from {self.dbname}.{self.stable} partition by t2, t3, tbname") tdSql.checkRows(check_num) + tdSql.query(f"select t2, t3, tbname, count(*) from {self.dbname}.{self.stable} group by tbname") + tdSql.checkRows(check_num) + + tdSql.query(f"select t2, t3, tbname, count(*) from {self.dbname}.{self.stable} partition by tbname") + tdSql.checkRows(check_num) + # multi tag + col tdSql.query(f"select t1, t2, c1, count(*) from {self.dbname}.{self.stable} partition by t1, t2, c1 ") tdSql.checkRows(nonempty_tb_num * self.row_nums)