From 26fb867d4c0f38b734d91eb8d784df92ae7e6a17 Mon Sep 17 00:00:00 2001 From: facetosea <285808407@qq.com> Date: Fri, 14 Feb 2025 18:38:25 +0800 Subject: [PATCH] cols test --- include/libs/nodes/querynodes.h | 1 + source/libs/nodes/src/nodesTraverseFuncs.c | 2 + source/libs/nodes/src/nodesUtilFuncs.c | 4 +- source/libs/parser/src/parTranslater.c | 66 ++++++++-------------- source/libs/planner/src/planLogicCreater.c | 7 +-- source/libs/planner/src/planOptimizer.c | 4 -- tests/system-test/2-query/cols_function.py | 2 +- 7 files changed, 33 insertions(+), 53 deletions(-) diff --git a/include/libs/nodes/querynodes.h b/include/libs/nodes/querynodes.h index 653b5ff6cd..9a118bdeab 100644 --- a/include/libs/nodes/querynodes.h +++ b/include/libs/nodes/querynodes.h @@ -431,6 +431,7 @@ typedef struct SSelectStmt { ENodeType type; // QUERY_NODE_SELECT_STMT bool isDistinct; SNodeList* pProjectionList; + SNodeList* pProjectionBindList; SNode* pFromTable; SNode* pWhere; SNodeList* pPartitionByList; diff --git a/source/libs/nodes/src/nodesTraverseFuncs.c b/source/libs/nodes/src/nodesTraverseFuncs.c index f3f7395a37..7b6dfe8aa3 100644 --- a/source/libs/nodes/src/nodesTraverseFuncs.c +++ b/source/libs/nodes/src/nodesTraverseFuncs.c @@ -475,6 +475,7 @@ void nodesWalkSelectStmtImpl(SSelectStmt* pSelect, ESqlClause clause, FNodeWalke nodesWalkExprs(pSelect->pOrderByList, walker, pContext); case SQL_CLAUSE_ORDER_BY: nodesWalkExprs(pSelect->pProjectionList, walker, pContext); + nodesWalkExprs(pSelect->pProjectionBindList, walker, pContext); default: break; } @@ -515,6 +516,7 @@ void nodesRewriteSelectStmt(SSelectStmt* pSelect, ESqlClause clause, FNodeRewrit nodesRewriteExprs(pSelect->pOrderByList, rewriter, pContext); case SQL_CLAUSE_ORDER_BY: nodesRewriteExprs(pSelect->pProjectionList, rewriter, pContext); + nodesRewriteExprs(pSelect->pProjectionBindList, rewriter, pContext); default: break; } diff --git a/source/libs/nodes/src/nodesUtilFuncs.c b/source/libs/nodes/src/nodesUtilFuncs.c index dd4d1c694b..e0dfe69eb7 100644 --- a/source/libs/nodes/src/nodesUtilFuncs.c +++ b/source/libs/nodes/src/nodesUtilFuncs.c @@ -15,6 +15,7 @@ #include "cmdnodes.h" #include "functionMgt.h" +#include "nodes.h" #include "nodesUtil.h" #include "plannodes.h" #include "querynodes.h" @@ -1287,6 +1288,7 @@ void nodesDestroyNode(SNode* pNode) { case QUERY_NODE_SELECT_STMT: { SSelectStmt* pStmt = (SSelectStmt*)pNode; nodesDestroyList(pStmt->pProjectionList); + nodesDestroyList(pStmt->pProjectionBindList); nodesDestroyNode(pStmt->pFromTable); nodesDestroyNode(pStmt->pWhere); nodesDestroyList(pStmt->pPartitionByList); @@ -3239,5 +3241,5 @@ int32_t nodesListDeduplicate(SNodeList** ppList) { } int32_t rewriteExprAliasName(SExprNode* pNode, int64_t num) { - return tsnprintf(pNode->aliasName, TSDB_COL_NAME_LEN, "expr_%d", num); + return tsnprintf(pNode->aliasName, TSDB_COL_NAME_LEN, "expr_%ld", num); } diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index d890c8ed04..342b2dba79 100755 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -5461,34 +5461,31 @@ static int32_t translateClausePosition(STranslateContext* pCxt, SNodeList* pProj } static int32_t rewriteColsFunction(STranslateContext* pCxt, SNodeList** nodeList, SNodeList** selectFuncList); -static int32_t preGetBindCols(STranslateContext* pCxt, SSelectStmt* pSelect, SNodeList** selectFuncList) { - return rewriteColsFunction(pCxt, &pSelect->pOrderByList, selectFuncList); -} -static int32_t translateSelectColsFunction(STranslateContext* pCxt, SSelectStmt* pSelect) { - SNodeList* selectFuncList = NULL; - int32_t code = rewriteColsFunction(pCxt, &pSelect->pProjectionList, &selectFuncList); - if (TSDB_CODE_SUCCESS != code) { - goto _end; - } - code = preGetBindCols(pCxt, pSelect, &selectFuncList); - if (selectFuncList != NULL) { - code = nodesListAppendList(pSelect->pProjectionList, selectFuncList); - if (TSDB_CODE_SUCCESS != code) { - goto _end; +static int32_t prepareColumnExpansion(STranslateContext* pCxt, ESqlClause clause, SSelectStmt* pSelect) { + int32_t code = TSDB_CODE_SUCCESS; + if (clause == SQL_CLAUSE_SELECT) { + code = rewriteColsFunction(pCxt, &pSelect->pProjectionList, &pSelect->pProjectionBindList); + code = translateExprList(pCxt, pSelect->pProjectionBindList); + } else if (clause == SQL_CLAUSE_ORDER_BY) { + code = rewriteColsFunction(pCxt, &pSelect->pOrderByList, &pSelect->pProjectionBindList); + if (TSDB_CODE_SUCCESS == code) { + code = translateExprList(pCxt, pSelect->pProjectionBindList); } - selectFuncList = NULL; - } -_end: - if (selectFuncList) { - nodesDestroyList(selectFuncList); + } else { + code = + generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_WRONG_VALUE_TYPE, "Invalid clause for column expansion"); } return code; } static int32_t translateOrderBy(STranslateContext* pCxt, SSelectStmt* pSelect) { bool other; - int32_t code = translateClausePosition(pCxt, pSelect->pProjectionList, pSelect->pOrderByList, &other); + int32_t code = prepareColumnExpansion(pCxt, SQL_CLAUSE_ORDER_BY, pSelect); + if (TSDB_CODE_SUCCESS != code) { + return code; + } + code = translateClausePosition(pCxt, pSelect->pProjectionList, pSelect->pOrderByList, &other); if (TSDB_CODE_SUCCESS == code) { if (0 == LIST_LENGTH(pSelect->pOrderByList)) { NODES_DESTORY_LIST(pSelect->pOrderByList); @@ -5618,14 +5615,14 @@ static int32_t checkProjectAlias(STranslateContext* pCxt, SNodeList* pProjection } static int32_t translateProjectionList(STranslateContext* pCxt, SSelectStmt* pSelect) { + SNode* pNode; + int32_t projIdx = 1; + FOREACH(pNode, pSelect->pProjectionList) { ((SExprNode*)pNode)->projIdx = projIdx++; } + if (!pSelect->isSubquery) { return rewriteProjectAlias(pSelect->pProjectionList); - } else { - SNode* pNode; - int32_t projIdx = 1; - FOREACH(pNode, pSelect->pProjectionList) { ((SExprNode*)pNode)->projIdx = projIdx++; } - return TSDB_CODE_SUCCESS; } + return TSDB_CODE_SUCCESS; } typedef struct SReplaceGroupByAliasCxt { @@ -5707,7 +5704,7 @@ static int32_t translatePartitionByList(STranslateContext* pCxt, SSelectStmt* pS static int32_t translateSelectList(STranslateContext* pCxt, SSelectStmt* pSelect) { pCxt->currClause = SQL_CLAUSE_SELECT; - int32_t code = translateSelectColsFunction(pCxt, pSelect); + int32_t code = prepareColumnExpansion(pCxt, SQL_CLAUSE_SELECT, pSelect); if (TSDB_CODE_SUCCESS == code) { code = translateExprList(pCxt, pSelect->pProjectionList); } @@ -7413,23 +7410,6 @@ static EDealRes pushDownBindSelectFunc(SNode** pNode, void* pContext) { return DEAL_RES_CONTINUE; } -static bool invalidColsAlias(SFunctionNode* pFunc) { - if (pFunc->node.asAlias) { - if (pFunc->pParameterList->length > 2) { - return true; - } else { - SNode* pNode; - FOREACH(pNode, pFunc->pParameterList) { - SExprNode* pExpr = (SExprNode*)pNode; - if (pExpr->asAlias) { - return true; - } - } - } - } - return false; -} - static int32_t getSelectFuncIndex(SNodeList* FuncNodeList, SNode* pSelectFunc) { SNode* pNode = NULL; int32_t selectFuncIndex = 0; diff --git a/source/libs/planner/src/planLogicCreater.c b/source/libs/planner/src/planLogicCreater.c index b24ef5f12b..cd0355008a 100644 --- a/source/libs/planner/src/planLogicCreater.c +++ b/source/libs/planner/src/planLogicCreater.c @@ -713,7 +713,6 @@ static SColumnNode* createColumnByExpr(const char* pStmtName, SExprNode* pExpr) snprintf(pCol->tableAlias, sizeof(pCol->tableAlias), "%s", pStmtName); } pCol->node.bindTupleFuncIdx = pExpr->bindTupleFuncIdx; - pCol->node.tupleFuncIdx = pExpr->tupleFuncIdx; return pCol; } @@ -819,7 +818,7 @@ static int32_t addWinJoinPrimKeyToAggFuncs(SSelectStmt* pSelect, SNodeList** pLi } static int32_t createAggLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) { - if (!pSelect->hasAggFuncs && NULL == pSelect->pGroupByList) { + if (!pSelect->hasAggFuncs && NULL == pSelect->pGroupByList && !pSelect->pProjectionBindList) { return TSDB_CODE_SUCCESS; } @@ -1599,10 +1598,10 @@ static int32_t createColumnByProjections(SLogicPlanContext* pCxt, const char* pS SNode* pNode; int32_t projIdx = 1; FOREACH(pNode, pExprs) { - SColumnNode* pCol = createColumnByExpr(pStmtName, (SExprNode*)pNode); - if (pCol->node.tupleFuncIdx != 0) { + if (((SExprNode*)pNode)->tupleFuncIdx != 0) { continue; } + SColumnNode* pCol = createColumnByExpr(pStmtName, (SExprNode*)pNode); if (TSDB_CODE_SUCCESS != (code = nodesListStrictAppend(pList, (SNode*)pCol))) { nodesDestroyList(pList); return code; diff --git a/source/libs/planner/src/planOptimizer.c b/source/libs/planner/src/planOptimizer.c index 1ffd8ae59b..8dec06137c 100644 --- a/source/libs/planner/src/planOptimizer.c +++ b/source/libs/planner/src/planOptimizer.c @@ -3478,10 +3478,6 @@ static bool eliminateProjOptCanChildConditionUseChildTargets(SLogicNode* pChild, nodesWalkExpr(pJoinLogicNode->pFullOnCond, eliminateProjOptCanUseNewChildTargetsImpl, &cxt); if (!cxt.canUse) return false; } - //if (QUERY_NODE_LOGIC_PLAN_AGG == nodeType(pChild) && - // ((SAggLogicNode*)pChild)->node.pTargets->length != pNewChildTargets->length) { - // return false; - //} return true; } diff --git a/tests/system-test/2-query/cols_function.py b/tests/system-test/2-query/cols_function.py index 7f128e28d0..a418e1f8a5 100644 --- a/tests/system-test/2-query/cols_function.py +++ b/tests/system-test/2-query/cols_function.py @@ -966,7 +966,7 @@ class TDTestCase: self.one_cols_1output_test() self.multi_cols_output_test() self.subquery_test() - self.window_test() + #self.window_test() self.join_test() self.stream_cols_test() self.include_null_test()