chore: erase duplicated func node for window logic node

This commit is contained in:
kailixu 2023-06-08 11:49:49 +08:00
parent bcac24b6b7
commit 576e3d1156
3 changed files with 23 additions and 21 deletions

View File

@ -4940,7 +4940,7 @@ static int32_t buildTableForSampleAst(SSampleAstInfo* pInfo, SNode** pOutput) {
} }
snprintf(pTable->table.dbName, sizeof(pTable->table.dbName), "%s", pInfo->pDbName); snprintf(pTable->table.dbName, sizeof(pTable->table.dbName), "%s", pInfo->pDbName);
snprintf(pTable->table.tableName, sizeof(pTable->table.tableName), "%s", pInfo->pTableName); snprintf(pTable->table.tableName, sizeof(pTable->table.tableName), "%s", pInfo->pTableName);
snprintf(pTable->table.tableAlias, sizeof(pTable->table.tableName), "%s", pInfo->pTableName); snprintf(pTable->table.tableAlias, sizeof(pTable->table.tableAlias), "%s", pInfo->pTableName);
TSWAP(pTable->pMeta, pInfo->pRollupTableMeta); TSWAP(pTable->pMeta, pInfo->pRollupTableMeta);
*pOutput = (SNode*)pTable; *pOutput = (SNode*)pTable;
return TSDB_CODE_SUCCESS; return TSDB_CODE_SUCCESS;

View File

@ -749,6 +749,25 @@ static int32_t createWindowLogicNodeFinalize(SLogicPlanContext* pCxt, SSelectStm
code = rewriteExprsForSelect(pWindow->pFuncs, pSelect, SQL_CLAUSE_WINDOW, NULL); code = rewriteExprsForSelect(pWindow->pFuncs, pSelect, SQL_CLAUSE_WINDOW, NULL);
} }
// erase duplicated funcNode by filtering colNode in pSelect->pProjectionList
int32_t funcIndex = 0;
SNode * pFunc = NULL, *pProject = NULL;
FOREACH(pFunc, pWindow->pFuncs) {
bool exist = false;
FOREACH(pProject, pSelect->pProjectionList) {
if (0 != ((SFunctionNode*)pFunc)->node.aliasName[0] &&
0 == strncmp(((SFunctionNode*)pFunc)->node.aliasName, ((SColumnNode*)pProject)->colName, TSDB_COL_NAME_LEN)) {
exist = true;
break;
}
}
if (!exist) {
nodesListErase(pWindow->pFuncs, nodesListGetCell(pWindow->pFuncs, funcIndex));
} else {
++funcIndex;
}
}
if (TSDB_CODE_SUCCESS == code) { if (TSDB_CODE_SUCCESS == code) {
code = createColumnByRewriteExprs(pWindow->pFuncs, &pWindow->node.pTargets); code = createColumnByRewriteExprs(pWindow->pFuncs, &pWindow->node.pTargets);
} }

View File

@ -1385,18 +1385,7 @@ static SNode* smaIndexOptFindWStartFunc(SNodeList* pSmaFuncs) {
return NULL; return NULL;
} }
static SNode* smaIndexOptFuncInProject(SNodeList* pProjects, SFunctionNode* pFunc) { static int32_t smaIndexOptCreateSmaCols(SNodeList* pFuncs, uint64_t tableId, SNodeList* pSmaFuncs,
SNode* pProject = NULL;
FOREACH(pProject, pProjects) {
if (0 != pFunc->node.aliasName[0] &&
0 == strncmp(pFunc->node.aliasName, ((SColumnNode*)pProject)->colName, TSDB_COL_NAME_LEN)) {
return pProject;
}
}
return NULL;
}
static int32_t smaIndexOptCreateSmaCols(SWindowLogicNode* pWindow, uint64_t tableId, SNodeList* pSmaFuncs,
SNodeList** pOutput) { SNodeList** pOutput) {
SNodeList* pCols = NULL; SNodeList* pCols = NULL;
SNode* pFunc = NULL; SNode* pFunc = NULL;
@ -1404,16 +1393,11 @@ static int32_t smaIndexOptCreateSmaCols(SWindowLogicNode* pWindow, uint64_t tabl
int32_t index = 0; int32_t index = 0;
int32_t smaFuncIndex = -1; int32_t smaFuncIndex = -1;
bool hasWStart = false; bool hasWStart = false;
FOREACH(pFunc, pFuncs) {
SProjectLogicNode* pProject = (SProjectLogicNode*)pWindow->node.pParent;
FOREACH(pFunc, pWindow->pFuncs) {
smaFuncIndex = smaIndexOptFindSmaFunc(pFunc, pSmaFuncs); smaFuncIndex = smaIndexOptFindSmaFunc(pFunc, pSmaFuncs);
if (smaFuncIndex < 0) { if (smaFuncIndex < 0) {
break; break;
} else { } else {
if (pProject && !smaIndexOptFuncInProject(pProject->pProjections, (SFunctionNode*)pFunc)) {
continue;
}
code = nodesListMakeStrictAppend(&pCols, smaIndexOptCreateSmaCol(pFunc, tableId, smaFuncIndex + 1)); code = nodesListMakeStrictAppend(&pCols, smaIndexOptCreateSmaCol(pFunc, tableId, smaFuncIndex + 1));
if (TSDB_CODE_SUCCESS != code) { if (TSDB_CODE_SUCCESS != code) {
break; break;
@ -1460,11 +1444,10 @@ static int32_t smaIndexOptCouldApplyIndex(SScanLogicNode* pScan, STableIndexInfo
if (!smaIndexOptEqualInterval(pScan, pWindow, pIndex)) { if (!smaIndexOptEqualInterval(pScan, pWindow, pIndex)) {
return TSDB_CODE_SUCCESS; return TSDB_CODE_SUCCESS;
} }
SNodeList* pSmaFuncs = NULL; SNodeList* pSmaFuncs = NULL;
int32_t code = nodesStringToList(pIndex->expr, &pSmaFuncs); int32_t code = nodesStringToList(pIndex->expr, &pSmaFuncs);
if (TSDB_CODE_SUCCESS == code) { if (TSDB_CODE_SUCCESS == code) {
code = smaIndexOptCreateSmaCols(pWindow, pIndex->dstTbUid, pSmaFuncs, pCols); code = smaIndexOptCreateSmaCols(pWindow->pFuncs, pIndex->dstTbUid, pSmaFuncs, pCols);
} }
nodesDestroyList(pSmaFuncs); nodesDestroyList(pSmaFuncs);
return code; return code;