From c524cc8a55afafb3b35b0b4a489d267dcf067325 Mon Sep 17 00:00:00 2001 From: Xiaoyu Wang Date: Thu, 10 Feb 2022 00:40:59 -0500 Subject: [PATCH] TD-13338 SELECT statement translate code --- source/libs/parser/src/parserImpl.c | 13 +++++++++---- source/libs/parser/test/newParserTest.cpp | 9 +++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/source/libs/parser/src/parserImpl.c b/source/libs/parser/src/parserImpl.c index 5c30813215..cd42b9ad64 100644 --- a/source/libs/parser/src/parserImpl.c +++ b/source/libs/parser/src/parserImpl.c @@ -661,8 +661,12 @@ static bool isAliasColumn(SColumnNode* pCol) { return ('\0' == pCol->tableAlias[0]); } +static bool isDistinctOrderBy(STranslateContext* pCxt) { + return (SQL_CLAUSE_ORDER_BY == pCxt->currClause && pCxt->pCurrStmt->isDistinct); +} + static SNodeList* getGroupByList(STranslateContext* pCxt) { - if (SQL_CLAUSE_ORDER_BY == pCxt->currClause && pCxt->pCurrStmt->isDistinct) { + if (isDistinctOrderBy(pCxt)) { return pCxt->pCurrStmt->pProjectionList; } return pCxt->pCurrStmt->pGroupByList; @@ -676,7 +680,7 @@ static SNode* getGroupByNode(SNode* pNode) { } static int32_t getGroupByErrorCode(STranslateContext* pCxt) { - if (SQL_CLAUSE_ORDER_BY == pCxt->currClause && pCxt->pCurrStmt->isDistinct) { + if (isDistinctOrderBy(pCxt)) { return TSDB_CODE_PAR_NOT_SELECTED_EXPRESSION; } return TSDB_CODE_PAR_GROUPBY_LACK_EXPRESSION; @@ -687,7 +691,7 @@ static EDealRes doCheckExprForGroupBy(SNode* pNode, void* pContext) { if (!nodesIsExprNode(pNode) || (QUERY_NODE_COLUMN == nodeType(pNode) && isAliasColumn((SColumnNode*)pNode))) { return DEAL_RES_CONTINUE; } - if (QUERY_NODE_FUNCTION == nodeType(pNode) && fmIsAggFunc(((SFunctionNode*)pNode)->funcId)) { + if (QUERY_NODE_FUNCTION == nodeType(pNode) && fmIsAggFunc(((SFunctionNode*)pNode)->funcId) && !isDistinctOrderBy(pCxt)) { return DEAL_RES_IGNORE_CHILD; } SNode* pGroupNode; @@ -696,7 +700,8 @@ static EDealRes doCheckExprForGroupBy(SNode* pNode, void* pContext) { return DEAL_RES_IGNORE_CHILD; } } - if (QUERY_NODE_COLUMN == nodeType(pNode)) { + if (QUERY_NODE_COLUMN == nodeType(pNode) || + (QUERY_NODE_FUNCTION == nodeType(pNode) && fmIsAggFunc(((SFunctionNode*)pNode)->funcId) && isDistinctOrderBy(pCxt))) { generateSyntaxErrMsg(pCxt, getGroupByErrorCode(pCxt)); return DEAL_RES_ERROR; } diff --git a/source/libs/parser/test/newParserTest.cpp b/source/libs/parser/test/newParserTest.cpp index c91427f03f..2616c6c251 100644 --- a/source/libs/parser/test/newParserTest.cpp +++ b/source/libs/parser/test/newParserTest.cpp @@ -548,6 +548,9 @@ TEST_F(NewParserTest, selectClause) { bind("SELECT DISTINCT c1 + 10 cc1, c2 cc2 FROM t1 WHERE c1 > 0 ORDER BY cc1, c2"); ASSERT_TRUE(run()); + + bind("SELECT DISTINCT count(c2) FROM t1 WHERE c1 > 0 GROUP BY c1 ORDER BY count(c2)"); + ASSERT_TRUE(run()); } TEST_F(NewParserTest, selectSyntaxError) { @@ -647,4 +650,10 @@ TEST_F(NewParserTest, selectSemanticError) { // TSDB_CODE_PAR_NOT_SELECTED_EXPRESSION bind("SELECT DISTINCT c1, c2 FROM t1 WHERE c1 > 0 ORDER BY ts"); ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_PAR_NOT_SELECTED_EXPRESSION)); + + bind("SELECT DISTINCT c1 FROM t1 WHERE c1 > 0 ORDER BY count(c2)"); + ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_PAR_NOT_SELECTED_EXPRESSION)); + + bind("SELECT DISTINCT c2 FROM t1 WHERE c1 > 0 ORDER BY count(c2)"); + ASSERT_TRUE(run(TSDB_CODE_SUCCESS, TSDB_CODE_PAR_NOT_SELECTED_EXPRESSION)); }