cols test

This commit is contained in:
facetosea 2025-02-14 18:38:25 +08:00
parent a391cc49df
commit 26fb867d4c
7 changed files with 33 additions and 53 deletions

View File

@ -431,6 +431,7 @@ typedef struct SSelectStmt {
ENodeType type; // QUERY_NODE_SELECT_STMT ENodeType type; // QUERY_NODE_SELECT_STMT
bool isDistinct; bool isDistinct;
SNodeList* pProjectionList; SNodeList* pProjectionList;
SNodeList* pProjectionBindList;
SNode* pFromTable; SNode* pFromTable;
SNode* pWhere; SNode* pWhere;
SNodeList* pPartitionByList; SNodeList* pPartitionByList;

View File

@ -475,6 +475,7 @@ void nodesWalkSelectStmtImpl(SSelectStmt* pSelect, ESqlClause clause, FNodeWalke
nodesWalkExprs(pSelect->pOrderByList, walker, pContext); nodesWalkExprs(pSelect->pOrderByList, walker, pContext);
case SQL_CLAUSE_ORDER_BY: case SQL_CLAUSE_ORDER_BY:
nodesWalkExprs(pSelect->pProjectionList, walker, pContext); nodesWalkExprs(pSelect->pProjectionList, walker, pContext);
nodesWalkExprs(pSelect->pProjectionBindList, walker, pContext);
default: default:
break; break;
} }
@ -515,6 +516,7 @@ void nodesRewriteSelectStmt(SSelectStmt* pSelect, ESqlClause clause, FNodeRewrit
nodesRewriteExprs(pSelect->pOrderByList, rewriter, pContext); nodesRewriteExprs(pSelect->pOrderByList, rewriter, pContext);
case SQL_CLAUSE_ORDER_BY: case SQL_CLAUSE_ORDER_BY:
nodesRewriteExprs(pSelect->pProjectionList, rewriter, pContext); nodesRewriteExprs(pSelect->pProjectionList, rewriter, pContext);
nodesRewriteExprs(pSelect->pProjectionBindList, rewriter, pContext);
default: default:
break; break;
} }

View File

@ -15,6 +15,7 @@
#include "cmdnodes.h" #include "cmdnodes.h"
#include "functionMgt.h" #include "functionMgt.h"
#include "nodes.h"
#include "nodesUtil.h" #include "nodesUtil.h"
#include "plannodes.h" #include "plannodes.h"
#include "querynodes.h" #include "querynodes.h"
@ -1287,6 +1288,7 @@ void nodesDestroyNode(SNode* pNode) {
case QUERY_NODE_SELECT_STMT: { case QUERY_NODE_SELECT_STMT: {
SSelectStmt* pStmt = (SSelectStmt*)pNode; SSelectStmt* pStmt = (SSelectStmt*)pNode;
nodesDestroyList(pStmt->pProjectionList); nodesDestroyList(pStmt->pProjectionList);
nodesDestroyList(pStmt->pProjectionBindList);
nodesDestroyNode(pStmt->pFromTable); nodesDestroyNode(pStmt->pFromTable);
nodesDestroyNode(pStmt->pWhere); nodesDestroyNode(pStmt->pWhere);
nodesDestroyList(pStmt->pPartitionByList); nodesDestroyList(pStmt->pPartitionByList);
@ -3239,5 +3241,5 @@ int32_t nodesListDeduplicate(SNodeList** ppList) {
} }
int32_t rewriteExprAliasName(SExprNode* pNode, int64_t num) { 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);
} }

View File

@ -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 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) { static int32_t prepareColumnExpansion(STranslateContext* pCxt, ESqlClause clause, SSelectStmt* pSelect) {
SNodeList* selectFuncList = NULL; int32_t code = TSDB_CODE_SUCCESS;
int32_t code = rewriteColsFunction(pCxt, &pSelect->pProjectionList, &selectFuncList); if (clause == SQL_CLAUSE_SELECT) {
if (TSDB_CODE_SUCCESS != code) { code = rewriteColsFunction(pCxt, &pSelect->pProjectionList, &pSelect->pProjectionBindList);
goto _end; code = translateExprList(pCxt, pSelect->pProjectionBindList);
} } else if (clause == SQL_CLAUSE_ORDER_BY) {
code = preGetBindCols(pCxt, pSelect, &selectFuncList); code = rewriteColsFunction(pCxt, &pSelect->pOrderByList, &pSelect->pProjectionBindList);
if (selectFuncList != NULL) { if (TSDB_CODE_SUCCESS == code) {
code = nodesListAppendList(pSelect->pProjectionList, selectFuncList); code = translateExprList(pCxt, pSelect->pProjectionBindList);
if (TSDB_CODE_SUCCESS != code) {
goto _end;
} }
selectFuncList = NULL; } else {
} code =
_end: generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_WRONG_VALUE_TYPE, "Invalid clause for column expansion");
if (selectFuncList) {
nodesDestroyList(selectFuncList);
} }
return code; return code;
} }
static int32_t translateOrderBy(STranslateContext* pCxt, SSelectStmt* pSelect) { static int32_t translateOrderBy(STranslateContext* pCxt, SSelectStmt* pSelect) {
bool other; 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 (TSDB_CODE_SUCCESS == code) {
if (0 == LIST_LENGTH(pSelect->pOrderByList)) { if (0 == LIST_LENGTH(pSelect->pOrderByList)) {
NODES_DESTORY_LIST(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) { 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) { if (!pSelect->isSubquery) {
return rewriteProjectAlias(pSelect->pProjectionList); 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 { typedef struct SReplaceGroupByAliasCxt {
@ -5707,7 +5704,7 @@ static int32_t translatePartitionByList(STranslateContext* pCxt, SSelectStmt* pS
static int32_t translateSelectList(STranslateContext* pCxt, SSelectStmt* pSelect) { static int32_t translateSelectList(STranslateContext* pCxt, SSelectStmt* pSelect) {
pCxt->currClause = SQL_CLAUSE_SELECT; 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) { if (TSDB_CODE_SUCCESS == code) {
code = translateExprList(pCxt, pSelect->pProjectionList); code = translateExprList(pCxt, pSelect->pProjectionList);
} }
@ -7413,23 +7410,6 @@ static EDealRes pushDownBindSelectFunc(SNode** pNode, void* pContext) {
return DEAL_RES_CONTINUE; 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) { static int32_t getSelectFuncIndex(SNodeList* FuncNodeList, SNode* pSelectFunc) {
SNode* pNode = NULL; SNode* pNode = NULL;
int32_t selectFuncIndex = 0; int32_t selectFuncIndex = 0;

View File

@ -713,7 +713,6 @@ static SColumnNode* createColumnByExpr(const char* pStmtName, SExprNode* pExpr)
snprintf(pCol->tableAlias, sizeof(pCol->tableAlias), "%s", pStmtName); snprintf(pCol->tableAlias, sizeof(pCol->tableAlias), "%s", pStmtName);
} }
pCol->node.bindTupleFuncIdx = pExpr->bindTupleFuncIdx; pCol->node.bindTupleFuncIdx = pExpr->bindTupleFuncIdx;
pCol->node.tupleFuncIdx = pExpr->tupleFuncIdx;
return pCol; return pCol;
} }
@ -819,7 +818,7 @@ static int32_t addWinJoinPrimKeyToAggFuncs(SSelectStmt* pSelect, SNodeList** pLi
} }
static int32_t createAggLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SLogicNode** pLogicNode) { 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; return TSDB_CODE_SUCCESS;
} }
@ -1599,10 +1598,10 @@ static int32_t createColumnByProjections(SLogicPlanContext* pCxt, const char* pS
SNode* pNode; SNode* pNode;
int32_t projIdx = 1; int32_t projIdx = 1;
FOREACH(pNode, pExprs) { FOREACH(pNode, pExprs) {
SColumnNode* pCol = createColumnByExpr(pStmtName, (SExprNode*)pNode); if (((SExprNode*)pNode)->tupleFuncIdx != 0) {
if (pCol->node.tupleFuncIdx != 0) {
continue; continue;
} }
SColumnNode* pCol = createColumnByExpr(pStmtName, (SExprNode*)pNode);
if (TSDB_CODE_SUCCESS != (code = nodesListStrictAppend(pList, (SNode*)pCol))) { if (TSDB_CODE_SUCCESS != (code = nodesListStrictAppend(pList, (SNode*)pCol))) {
nodesDestroyList(pList); nodesDestroyList(pList);
return code; return code;

View File

@ -3478,10 +3478,6 @@ static bool eliminateProjOptCanChildConditionUseChildTargets(SLogicNode* pChild,
nodesWalkExpr(pJoinLogicNode->pFullOnCond, eliminateProjOptCanUseNewChildTargetsImpl, &cxt); nodesWalkExpr(pJoinLogicNode->pFullOnCond, eliminateProjOptCanUseNewChildTargetsImpl, &cxt);
if (!cxt.canUse) return false; if (!cxt.canUse) return false;
} }
//if (QUERY_NODE_LOGIC_PLAN_AGG == nodeType(pChild) &&
// ((SAggLogicNode*)pChild)->node.pTargets->length != pNewChildTargets->length) {
// return false;
//}
return true; return true;
} }

View File

@ -966,7 +966,7 @@ class TDTestCase:
self.one_cols_1output_test() self.one_cols_1output_test()
self.multi_cols_output_test() self.multi_cols_output_test()
self.subquery_test() self.subquery_test()
self.window_test() #self.window_test()
self.join_test() self.join_test()
self.stream_cols_test() self.stream_cols_test()
self.include_null_test() self.include_null_test()