提交 c524cc8a 编写于 作者: X Xiaoyu Wang

TD-13338 SELECT statement translate code

上级 67f91c6d
...@@ -661,8 +661,12 @@ static bool isAliasColumn(SColumnNode* pCol) { ...@@ -661,8 +661,12 @@ static bool isAliasColumn(SColumnNode* pCol) {
return ('\0' == pCol->tableAlias[0]); 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) { 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->pProjectionList;
} }
return pCxt->pCurrStmt->pGroupByList; return pCxt->pCurrStmt->pGroupByList;
...@@ -676,7 +680,7 @@ static SNode* getGroupByNode(SNode* pNode) { ...@@ -676,7 +680,7 @@ static SNode* getGroupByNode(SNode* pNode) {
} }
static int32_t getGroupByErrorCode(STranslateContext* pCxt) { 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_NOT_SELECTED_EXPRESSION;
} }
return TSDB_CODE_PAR_GROUPBY_LACK_EXPRESSION; return TSDB_CODE_PAR_GROUPBY_LACK_EXPRESSION;
...@@ -687,7 +691,7 @@ static EDealRes doCheckExprForGroupBy(SNode* pNode, void* pContext) { ...@@ -687,7 +691,7 @@ static EDealRes doCheckExprForGroupBy(SNode* pNode, void* pContext) {
if (!nodesIsExprNode(pNode) || (QUERY_NODE_COLUMN == nodeType(pNode) && isAliasColumn((SColumnNode*)pNode))) { if (!nodesIsExprNode(pNode) || (QUERY_NODE_COLUMN == nodeType(pNode) && isAliasColumn((SColumnNode*)pNode))) {
return DEAL_RES_CONTINUE; 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; return DEAL_RES_IGNORE_CHILD;
} }
SNode* pGroupNode; SNode* pGroupNode;
...@@ -696,7 +700,8 @@ static EDealRes doCheckExprForGroupBy(SNode* pNode, void* pContext) { ...@@ -696,7 +700,8 @@ static EDealRes doCheckExprForGroupBy(SNode* pNode, void* pContext) {
return DEAL_RES_IGNORE_CHILD; 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)); generateSyntaxErrMsg(pCxt, getGroupByErrorCode(pCxt));
return DEAL_RES_ERROR; return DEAL_RES_ERROR;
} }
......
...@@ -548,6 +548,9 @@ TEST_F(NewParserTest, selectClause) { ...@@ -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"); bind("SELECT DISTINCT c1 + 10 cc1, c2 cc2 FROM t1 WHERE c1 > 0 ORDER BY cc1, c2");
ASSERT_TRUE(run()); 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) { TEST_F(NewParserTest, selectSyntaxError) {
...@@ -647,4 +650,10 @@ TEST_F(NewParserTest, selectSemanticError) { ...@@ -647,4 +650,10 @@ TEST_F(NewParserTest, selectSemanticError) {
// TSDB_CODE_PAR_NOT_SELECTED_EXPRESSION // TSDB_CODE_PAR_NOT_SELECTED_EXPRESSION
bind("SELECT DISTINCT c1, c2 FROM t1 WHERE c1 > 0 ORDER BY ts"); 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)); 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));
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册