From bcac24b6b7ede099377b99cb85ca14e4bf53d848 Mon Sep 17 00:00:00 2001 From: kailixu Date: Tue, 6 Jun 2023 17:19:59 +0800 Subject: [PATCH 01/11] fix: tsma query with order by _wstart/_wend --- source/libs/parser/src/parTranslater.c | 1 + source/libs/planner/src/planOptimizer.c | 23 +++++- .../script/tsim/sma/tsmaCreateInsertQuery.sim | 74 +++++++++++++++++++ 3 files changed, 95 insertions(+), 3 deletions(-) diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index d1c3da0041..a9fe65492d 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -4940,6 +4940,7 @@ static int32_t buildTableForSampleAst(SSampleAstInfo* pInfo, SNode** pOutput) { } 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.tableAlias, sizeof(pTable->table.tableName), "%s", pInfo->pTableName); TSWAP(pTable->pMeta, pInfo->pRollupTableMeta); *pOutput = (SNode*)pTable; return TSDB_CODE_SUCCESS; diff --git a/source/libs/planner/src/planOptimizer.c b/source/libs/planner/src/planOptimizer.c index 8b75fe6b33..36378a5414 100644 --- a/source/libs/planner/src/planOptimizer.c +++ b/source/libs/planner/src/planOptimizer.c @@ -1385,7 +1385,18 @@ static SNode* smaIndexOptFindWStartFunc(SNodeList* pSmaFuncs) { return NULL; } -static int32_t smaIndexOptCreateSmaCols(SNodeList* pFuncs, uint64_t tableId, SNodeList* pSmaFuncs, +static SNode* smaIndexOptFuncInProject(SNodeList* pProjects, SFunctionNode* pFunc) { + 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* pCols = NULL; SNode* pFunc = NULL; @@ -1393,11 +1404,16 @@ static int32_t smaIndexOptCreateSmaCols(SNodeList* pFuncs, uint64_t tableId, SNo int32_t index = 0; int32_t smaFuncIndex = -1; bool hasWStart = false; - FOREACH(pFunc, pFuncs) { + + SProjectLogicNode* pProject = (SProjectLogicNode*)pWindow->node.pParent; + FOREACH(pFunc, pWindow->pFuncs) { smaFuncIndex = smaIndexOptFindSmaFunc(pFunc, pSmaFuncs); if (smaFuncIndex < 0) { break; } else { + if (pProject && !smaIndexOptFuncInProject(pProject->pProjections, (SFunctionNode*)pFunc)) { + continue; + } code = nodesListMakeStrictAppend(&pCols, smaIndexOptCreateSmaCol(pFunc, tableId, smaFuncIndex + 1)); if (TSDB_CODE_SUCCESS != code) { break; @@ -1444,10 +1460,11 @@ static int32_t smaIndexOptCouldApplyIndex(SScanLogicNode* pScan, STableIndexInfo if (!smaIndexOptEqualInterval(pScan, pWindow, pIndex)) { return TSDB_CODE_SUCCESS; } + SNodeList* pSmaFuncs = NULL; int32_t code = nodesStringToList(pIndex->expr, &pSmaFuncs); if (TSDB_CODE_SUCCESS == code) { - code = smaIndexOptCreateSmaCols(pWindow->pFuncs, pIndex->dstTbUid, pSmaFuncs, pCols); + code = smaIndexOptCreateSmaCols(pWindow, pIndex->dstTbUid, pSmaFuncs, pCols); } nodesDestroyList(pSmaFuncs); return code; diff --git a/tests/script/tsim/sma/tsmaCreateInsertQuery.sim b/tests/script/tsim/sma/tsmaCreateInsertQuery.sim index 242231e408..60f769d2ae 100644 --- a/tests/script/tsim/sma/tsmaCreateInsertQuery.sim +++ b/tests/script/tsim/sma/tsmaCreateInsertQuery.sim @@ -340,6 +340,80 @@ if $data05 != 30.000000000 then return -1 endi +print =============== select with _wstart/order by _wstart from stb from file in designated vgroup +sql select _wstart, _wend, min(c1),max(c2),max(c1) from stb interval(5m,10s) sliding(5m) order by _wstart; +print $data00 $data01 $data02 $data03 $data04 +if $rows != 1 then + print rows $rows != 1 + return -1 +endi + +if $data02 != -13 then + print data02 $data02 != -13 + return -1 +endi + +if $data03 != 20.00000 then + print data03 $data03 != 20.00000 + return -1 +endi + +if $data04 != 20 then + print data04 $data04 != 20 + return -1 +endi + +print =============== select without _wstart/with order by _wstart from stb from file in designated vgroup +sql select _wend, min(c1),max(c2),max(c1) from stb interval(5m,10s) sliding(5m) order by _wstart; +print $data00 $data01 $data02 $data03 +if $rows != 1 then + print rows $rows != 1 + return -1 +endi + +if $data01 != -13 then + print data01 $data01 != -13 + return -1 +endi + +if $data02 != 20.00000 then + print data02 $data02 != 20.00000 + return -1 +endi + +if $data03 != 20 then + print data03 $data03 != 20 + return -1 +endi + +print =============== select * from stb from file in common vgroups +sql select _wstart, _wend, min(c1),max(c2),max(c1),max(c3) from stb interval(5m,10s) sliding(5m) order by _wstart; +print $data00 $data01 $data02 $data03 $data04 $data05 +if $rows != 1 then + print rows $rows != 1 + return -1 +endi + +if $data02 != -13 then + print data02 $data02 != -13 + return -1 +endi + +if $data03 != 20.00000 then + print data03 $data03 != 20.00000 + return -1 +endi + +if $data04 != 20 then + print data04 $data04 != 20 + return -1 +endi + +if $data05 != 30.000000000 then + print data05 $data05 != 30.000000000 + return -1 +endi + system sh/exec.sh -n dnode1 -s stop -x SIGINT From 576e3d1156ae9a529e6a305d7f712e6a04dabea0 Mon Sep 17 00:00:00 2001 From: kailixu Date: Thu, 8 Jun 2023 11:49:49 +0800 Subject: [PATCH 02/11] chore: erase duplicated func node for window logic node --- source/libs/parser/src/parTranslater.c | 2 +- source/libs/planner/src/planLogicCreater.c | 19 ++++++++++++++++++ source/libs/planner/src/planOptimizer.c | 23 +++------------------- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index a9fe65492d..9f94c7cb8c 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -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.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); *pOutput = (SNode*)pTable; return TSDB_CODE_SUCCESS; diff --git a/source/libs/planner/src/planLogicCreater.c b/source/libs/planner/src/planLogicCreater.c index 5bbc9acdad..8c30beffc9 100644 --- a/source/libs/planner/src/planLogicCreater.c +++ b/source/libs/planner/src/planLogicCreater.c @@ -749,6 +749,25 @@ static int32_t createWindowLogicNodeFinalize(SLogicPlanContext* pCxt, SSelectStm 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) { code = createColumnByRewriteExprs(pWindow->pFuncs, &pWindow->node.pTargets); } diff --git a/source/libs/planner/src/planOptimizer.c b/source/libs/planner/src/planOptimizer.c index 36378a5414..8b75fe6b33 100644 --- a/source/libs/planner/src/planOptimizer.c +++ b/source/libs/planner/src/planOptimizer.c @@ -1385,18 +1385,7 @@ static SNode* smaIndexOptFindWStartFunc(SNodeList* pSmaFuncs) { return NULL; } -static SNode* smaIndexOptFuncInProject(SNodeList* pProjects, SFunctionNode* pFunc) { - 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, +static int32_t smaIndexOptCreateSmaCols(SNodeList* pFuncs, uint64_t tableId, SNodeList* pSmaFuncs, SNodeList** pOutput) { SNodeList* pCols = NULL; SNode* pFunc = NULL; @@ -1404,16 +1393,11 @@ static int32_t smaIndexOptCreateSmaCols(SWindowLogicNode* pWindow, uint64_t tabl int32_t index = 0; int32_t smaFuncIndex = -1; bool hasWStart = false; - - SProjectLogicNode* pProject = (SProjectLogicNode*)pWindow->node.pParent; - FOREACH(pFunc, pWindow->pFuncs) { + FOREACH(pFunc, pFuncs) { smaFuncIndex = smaIndexOptFindSmaFunc(pFunc, pSmaFuncs); if (smaFuncIndex < 0) { break; } else { - if (pProject && !smaIndexOptFuncInProject(pProject->pProjections, (SFunctionNode*)pFunc)) { - continue; - } code = nodesListMakeStrictAppend(&pCols, smaIndexOptCreateSmaCol(pFunc, tableId, smaFuncIndex + 1)); if (TSDB_CODE_SUCCESS != code) { break; @@ -1460,11 +1444,10 @@ static int32_t smaIndexOptCouldApplyIndex(SScanLogicNode* pScan, STableIndexInfo if (!smaIndexOptEqualInterval(pScan, pWindow, pIndex)) { return TSDB_CODE_SUCCESS; } - SNodeList* pSmaFuncs = NULL; int32_t code = nodesStringToList(pIndex->expr, &pSmaFuncs); if (TSDB_CODE_SUCCESS == code) { - code = smaIndexOptCreateSmaCols(pWindow, pIndex->dstTbUid, pSmaFuncs, pCols); + code = smaIndexOptCreateSmaCols(pWindow->pFuncs, pIndex->dstTbUid, pSmaFuncs, pCols); } nodesDestroyList(pSmaFuncs); return code; From 90b4580ba9ccb784549cec2be8102e72bcabcdee Mon Sep 17 00:00:00 2001 From: kailixu Date: Thu, 8 Jun 2023 14:16:05 +0800 Subject: [PATCH 03/11] chore: only filter window pseudo column --- source/libs/planner/src/planLogicCreater.c | 37 +++++++++++++--------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/source/libs/planner/src/planLogicCreater.c b/source/libs/planner/src/planLogicCreater.c index 8c30beffc9..a253405404 100644 --- a/source/libs/planner/src/planLogicCreater.c +++ b/source/libs/planner/src/planLogicCreater.c @@ -749,22 +749,29 @@ static int32_t createWindowLogicNodeFinalize(SLogicPlanContext* pCxt, SSelectStm 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; + // erase duplicated Window Pseudo funcNode by filtering colNode in pSelect->pProjectionList + if (pSelect->pProjectionList) { + int32_t funcIndex = 0; + SNode * pFunc = NULL, *pProject = NULL; + FOREACH(pFunc, pWindow->pFuncs) { + if (!fmIsWindowPseudoColumnFunc(((SFunctionNode*)pFunc)->funcId)) { + ++funcIndex; + continue; + } + 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 (!exist) { - nodesListErase(pWindow->pFuncs, nodesListGetCell(pWindow->pFuncs, funcIndex)); - } else { - ++funcIndex; } } From e56c5950aa34dc31c5f074c1aeadcda9d088204a Mon Sep 17 00:00:00 2001 From: kailixu Date: Thu, 8 Jun 2023 14:29:44 +0800 Subject: [PATCH 04/11] chore: more check --- source/libs/planner/src/planLogicCreater.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/libs/planner/src/planLogicCreater.c b/source/libs/planner/src/planLogicCreater.c index a253405404..ffabfcc0dd 100644 --- a/source/libs/planner/src/planLogicCreater.c +++ b/source/libs/planner/src/planLogicCreater.c @@ -760,7 +760,7 @@ static int32_t createWindowLogicNodeFinalize(SLogicPlanContext* pCxt, SSelectStm } bool exist = false; FOREACH(pProject, pSelect->pProjectionList) { - if (0 != ((SFunctionNode*)pFunc)->node.aliasName[0] && + if (QUERY_NODE_COLUMN == nodeType(pProject) && 0 != ((SFunctionNode*)pFunc)->node.aliasName[0] && 0 == strncmp(((SFunctionNode*)pFunc)->node.aliasName, ((SColumnNode*)pProject)->colName, TSDB_COL_NAME_LEN)) { exist = true; From c3726b82643cd8eef15bd6941df6a8508f6bd3e7 Mon Sep 17 00:00:00 2001 From: kailixu Date: Thu, 8 Jun 2023 14:42:10 +0800 Subject: [PATCH 05/11] chore: more check --- source/libs/planner/src/planLogicCreater.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/libs/planner/src/planLogicCreater.c b/source/libs/planner/src/planLogicCreater.c index ffabfcc0dd..dd8fd3861d 100644 --- a/source/libs/planner/src/planLogicCreater.c +++ b/source/libs/planner/src/planLogicCreater.c @@ -750,7 +750,7 @@ static int32_t createWindowLogicNodeFinalize(SLogicPlanContext* pCxt, SSelectStm } // erase duplicated Window Pseudo funcNode by filtering colNode in pSelect->pProjectionList - if (pSelect->pProjectionList) { + if (WINDOW_TYPE_INTERVAL == pWindow->winType && pSelect->pProjectionList) { int32_t funcIndex = 0; SNode * pFunc = NULL, *pProject = NULL; FOREACH(pFunc, pWindow->pFuncs) { From e5e724a8baf4f0beed5c561a0ef8d5420a4175a7 Mon Sep 17 00:00:00 2001 From: kailixu Date: Thu, 8 Jun 2023 16:13:31 +0800 Subject: [PATCH 06/11] chore: code optimization --- source/libs/planner/src/planLogicCreater.c | 70 ++++++++++++++-------- 1 file changed, 46 insertions(+), 24 deletions(-) diff --git a/source/libs/planner/src/planLogicCreater.c b/source/libs/planner/src/planLogicCreater.c index dd8fd3861d..5a8512c12d 100644 --- a/source/libs/planner/src/planLogicCreater.c +++ b/source/libs/planner/src/planLogicCreater.c @@ -732,6 +732,49 @@ static int32_t createInterpFuncLogicNode(SLogicPlanContext* pCxt, SSelectStmt* p return code; } +static int32_t eraseDuplicatedWindowPseudoCol(SWindowLogicNode* pWindow, SNodeList* pProjections) { + int32_t code = 0; + int32_t funcIndex = 0; + SSHashObj* pHashFunc = NULL; + SNode* pFuncNode = NULL; + FOREACH(pFuncNode, pWindow->pFuncs) { + SFunctionNode* pFunc = (SFunctionNode*)pFuncNode; + if (!fmIsWindowPseudoColumnFunc(pFunc->funcId)) { + ++funcIndex; + continue; + } + if (!pHashFunc && !(pHashFunc = tSimpleHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT)))) { + code = TSDB_CODE_OUT_OF_MEMORY; + break; + } + + void* hashVal = tSimpleHashGet(pHashFunc, &pFunc->funcId, sizeof(pFunc->funcId)); + if (!hashVal) { + tSimpleHashPut(pHashFunc, &pFunc->funcId, sizeof(pFunc->funcId), &funcIndex, sizeof(funcIndex)); + ++funcIndex; + continue; + } + + bool exist = false; + SNode* pProject = NULL; + FOREACH(pProject, pProjections) { + if (QUERY_NODE_COLUMN == nodeType(pProject) && 0 != pFunc->node.aliasName[0] && + 0 == strncmp(pFunc->node.aliasName, ((SColumnNode*)pProject)->colName, TSDB_COL_NAME_LEN)) { + exist = true; + break; + } + } + if (!exist) { + nodesListErase(pWindow->pFuncs, nodesListGetCell(pWindow->pFuncs, funcIndex)); + } else { + nodesListErase(pWindow->pFuncs, nodesListGetCell(pWindow->pFuncs, *(int32_t*)hashVal)); + tSimpleHashPut(pHashFunc, &pFunc->funcId, sizeof(pFunc->funcId), &funcIndex, sizeof(funcIndex)); + } + } + tSimpleHashCleanup(pHashFunc); + return code; +} + static int32_t createWindowLogicNodeFinalize(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SWindowLogicNode* pWindow, SLogicNode** pLogicNode) { if (pCxt->pPlanCxt->streamQuery) { @@ -749,30 +792,9 @@ static int32_t createWindowLogicNodeFinalize(SLogicPlanContext* pCxt, SSelectStm code = rewriteExprsForSelect(pWindow->pFuncs, pSelect, SQL_CLAUSE_WINDOW, NULL); } - // erase duplicated Window Pseudo funcNode by filtering colNode in pSelect->pProjectionList - if (WINDOW_TYPE_INTERVAL == pWindow->winType && pSelect->pProjectionList) { - int32_t funcIndex = 0; - SNode * pFunc = NULL, *pProject = NULL; - FOREACH(pFunc, pWindow->pFuncs) { - if (!fmIsWindowPseudoColumnFunc(((SFunctionNode*)pFunc)->funcId)) { - ++funcIndex; - continue; - } - bool exist = false; - FOREACH(pProject, pSelect->pProjectionList) { - if (QUERY_NODE_COLUMN == nodeType(pProject) && 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 && WINDOW_TYPE_INTERVAL == pWindow->winType && pSelect->pProjectionList) { + // erase duplicated Window Pseudo funcNode by filtering colNode in pSelect->pProjectionList + code = eraseDuplicatedWindowPseudoCol(pWindow, pSelect->pProjectionList); } if (TSDB_CODE_SUCCESS == code) { From 79625df5e4dac8f906e39ca3597c04df579f92b7 Mon Sep 17 00:00:00 2001 From: kailixu Date: Thu, 8 Jun 2023 18:39:51 +0800 Subject: [PATCH 07/11] chore: code optimization --- source/libs/planner/src/planLogicCreater.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/source/libs/planner/src/planLogicCreater.c b/source/libs/planner/src/planLogicCreater.c index 5a8512c12d..953ca13d3b 100644 --- a/source/libs/planner/src/planLogicCreater.c +++ b/source/libs/planner/src/planLogicCreater.c @@ -732,25 +732,25 @@ static int32_t createInterpFuncLogicNode(SLogicPlanContext* pCxt, SSelectStmt* p return code; } -static int32_t eraseDuplicatedWindowPseudoCol(SWindowLogicNode* pWindow, SNodeList* pProjections) { +static int32_t eraseDuplicatedPseudoColumnFuncs(SNodeList* pFuncs, SNodeList* pProjections) { int32_t code = 0; int32_t funcIndex = 0; - SSHashObj* pHashFunc = NULL; + SSHashObj* pFuncHash = NULL; SNode* pFuncNode = NULL; - FOREACH(pFuncNode, pWindow->pFuncs) { + FOREACH(pFuncNode, pFuncs) { SFunctionNode* pFunc = (SFunctionNode*)pFuncNode; if (!fmIsWindowPseudoColumnFunc(pFunc->funcId)) { ++funcIndex; continue; } - if (!pHashFunc && !(pHashFunc = tSimpleHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT)))) { + if (!pFuncHash && !(pFuncHash = tSimpleHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT)))) { code = TSDB_CODE_OUT_OF_MEMORY; break; } - void* hashVal = tSimpleHashGet(pHashFunc, &pFunc->funcId, sizeof(pFunc->funcId)); + void* hashVal = tSimpleHashGet(pFuncHash, &pFunc->funcId, sizeof(pFunc->funcId)); if (!hashVal) { - tSimpleHashPut(pHashFunc, &pFunc->funcId, sizeof(pFunc->funcId), &funcIndex, sizeof(funcIndex)); + tSimpleHashPut(pFuncHash, &pFunc->funcId, sizeof(pFunc->funcId), &funcIndex, sizeof(funcIndex)); ++funcIndex; continue; } @@ -765,13 +765,13 @@ static int32_t eraseDuplicatedWindowPseudoCol(SWindowLogicNode* pWindow, SNodeLi } } if (!exist) { - nodesListErase(pWindow->pFuncs, nodesListGetCell(pWindow->pFuncs, funcIndex)); + nodesListErase(pFuncs, nodesListGetCell(pFuncs, funcIndex)); } else { - nodesListErase(pWindow->pFuncs, nodesListGetCell(pWindow->pFuncs, *(int32_t*)hashVal)); - tSimpleHashPut(pHashFunc, &pFunc->funcId, sizeof(pFunc->funcId), &funcIndex, sizeof(funcIndex)); + nodesListErase(pFuncs, nodesListGetCell(pFuncs, *(int32_t*)hashVal)); + tSimpleHashPut(pFuncHash, &pFunc->funcId, sizeof(pFunc->funcId), &funcIndex, sizeof(funcIndex)); } } - tSimpleHashCleanup(pHashFunc); + tSimpleHashCleanup(pFuncHash); return code; } @@ -794,7 +794,7 @@ static int32_t createWindowLogicNodeFinalize(SLogicPlanContext* pCxt, SSelectStm if (TSDB_CODE_SUCCESS == code && WINDOW_TYPE_INTERVAL == pWindow->winType && pSelect->pProjectionList) { // erase duplicated Window Pseudo funcNode by filtering colNode in pSelect->pProjectionList - code = eraseDuplicatedWindowPseudoCol(pWindow, pSelect->pProjectionList); + code = eraseDuplicatedPseudoColumnFuncs(pWindow->pFuncs, pSelect->pProjectionList); } if (TSDB_CODE_SUCCESS == code) { From 58c43901ede6b45a2f248d43c373e419a6c56d4b Mon Sep 17 00:00:00 2001 From: kailixu Date: Mon, 12 Jun 2023 09:14:38 +0800 Subject: [PATCH 08/11] chore: another logic --- source/libs/parser/src/parTranslater.c | 6 +++ source/libs/planner/src/planLogicCreater.c | 48 ---------------------- 2 files changed, 6 insertions(+), 48 deletions(-) diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index 3b94bae05c..5f3e402027 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -3108,6 +3108,12 @@ static int32_t rewriteProjectAlias(SNodeList* pProjectionList) { if ('\0' == pExpr->userAlias[0]) { strcpy(pExpr->userAlias, pExpr->aliasName); } + if (QUERY_NODE_COLUMN == nodeType(pProject) && + ((0 == strcasecmp("_wstart", pExpr->userAlias) || 0 == strcasecmp("_wend", pExpr->userAlias) || + 0 == strcasecmp("_wduration", pExpr->userAlias)) && + '\0' != pExpr->aliasName[0])) { + continue; + } sprintf(pExpr->aliasName, "#expr_%d", no++); } return TSDB_CODE_SUCCESS; diff --git a/source/libs/planner/src/planLogicCreater.c b/source/libs/planner/src/planLogicCreater.c index 953ca13d3b..5bbc9acdad 100644 --- a/source/libs/planner/src/planLogicCreater.c +++ b/source/libs/planner/src/planLogicCreater.c @@ -732,49 +732,6 @@ static int32_t createInterpFuncLogicNode(SLogicPlanContext* pCxt, SSelectStmt* p return code; } -static int32_t eraseDuplicatedPseudoColumnFuncs(SNodeList* pFuncs, SNodeList* pProjections) { - int32_t code = 0; - int32_t funcIndex = 0; - SSHashObj* pFuncHash = NULL; - SNode* pFuncNode = NULL; - FOREACH(pFuncNode, pFuncs) { - SFunctionNode* pFunc = (SFunctionNode*)pFuncNode; - if (!fmIsWindowPseudoColumnFunc(pFunc->funcId)) { - ++funcIndex; - continue; - } - if (!pFuncHash && !(pFuncHash = tSimpleHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT)))) { - code = TSDB_CODE_OUT_OF_MEMORY; - break; - } - - void* hashVal = tSimpleHashGet(pFuncHash, &pFunc->funcId, sizeof(pFunc->funcId)); - if (!hashVal) { - tSimpleHashPut(pFuncHash, &pFunc->funcId, sizeof(pFunc->funcId), &funcIndex, sizeof(funcIndex)); - ++funcIndex; - continue; - } - - bool exist = false; - SNode* pProject = NULL; - FOREACH(pProject, pProjections) { - if (QUERY_NODE_COLUMN == nodeType(pProject) && 0 != pFunc->node.aliasName[0] && - 0 == strncmp(pFunc->node.aliasName, ((SColumnNode*)pProject)->colName, TSDB_COL_NAME_LEN)) { - exist = true; - break; - } - } - if (!exist) { - nodesListErase(pFuncs, nodesListGetCell(pFuncs, funcIndex)); - } else { - nodesListErase(pFuncs, nodesListGetCell(pFuncs, *(int32_t*)hashVal)); - tSimpleHashPut(pFuncHash, &pFunc->funcId, sizeof(pFunc->funcId), &funcIndex, sizeof(funcIndex)); - } - } - tSimpleHashCleanup(pFuncHash); - return code; -} - static int32_t createWindowLogicNodeFinalize(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SWindowLogicNode* pWindow, SLogicNode** pLogicNode) { if (pCxt->pPlanCxt->streamQuery) { @@ -792,11 +749,6 @@ static int32_t createWindowLogicNodeFinalize(SLogicPlanContext* pCxt, SSelectStm code = rewriteExprsForSelect(pWindow->pFuncs, pSelect, SQL_CLAUSE_WINDOW, NULL); } - if (TSDB_CODE_SUCCESS == code && WINDOW_TYPE_INTERVAL == pWindow->winType && pSelect->pProjectionList) { - // erase duplicated Window Pseudo funcNode by filtering colNode in pSelect->pProjectionList - code = eraseDuplicatedPseudoColumnFuncs(pWindow->pFuncs, pSelect->pProjectionList); - } - if (TSDB_CODE_SUCCESS == code) { code = createColumnByRewriteExprs(pWindow->pFuncs, &pWindow->node.pTargets); } From b200fdba5120af4079b2e7dd82865c9e6ee0a87b Mon Sep 17 00:00:00 2001 From: kailixu Date: Mon, 12 Jun 2023 10:44:01 +0800 Subject: [PATCH 09/11] chore: node type --- source/libs/parser/src/parTranslater.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index 5f3e402027..ed109df1f9 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -3108,7 +3108,7 @@ static int32_t rewriteProjectAlias(SNodeList* pProjectionList) { if ('\0' == pExpr->userAlias[0]) { strcpy(pExpr->userAlias, pExpr->aliasName); } - if (QUERY_NODE_COLUMN == nodeType(pProject) && + if (QUERY_NODE_FUNCTION == nodeType(pProject) && ((0 == strcasecmp("_wstart", pExpr->userAlias) || 0 == strcasecmp("_wend", pExpr->userAlias) || 0 == strcasecmp("_wduration", pExpr->userAlias)) && '\0' != pExpr->aliasName[0])) { From f5d28cdcd1fb99faa9f558bf22ad6c018c768ddc Mon Sep 17 00:00:00 2001 From: kailixu Date: Mon, 12 Jun 2023 10:49:17 +0800 Subject: [PATCH 10/11] chore: more check --- source/libs/parser/src/parTranslater.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index ed109df1f9..a6d010a814 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -3108,12 +3108,12 @@ static int32_t rewriteProjectAlias(SNodeList* pProjectionList) { if ('\0' == pExpr->userAlias[0]) { strcpy(pExpr->userAlias, pExpr->aliasName); } - if (QUERY_NODE_FUNCTION == nodeType(pProject) && + if (QUERY_NODE_FUNCTION == nodeType(pProject) && fmIsWindowPseudoColumnFunc(((SFunctionNode*)pProject)->funcId) && ((0 == strcasecmp("_wstart", pExpr->userAlias) || 0 == strcasecmp("_wend", pExpr->userAlias) || 0 == strcasecmp("_wduration", pExpr->userAlias)) && '\0' != pExpr->aliasName[0])) { - continue; - } + continue; + } sprintf(pExpr->aliasName, "#expr_%d", no++); } return TSDB_CODE_SUCCESS; From 4cd039c38ea372f680f6c1c3fc2dabc218a13d2e Mon Sep 17 00:00:00 2001 From: kailixu Date: Mon, 12 Jun 2023 16:47:03 +0800 Subject: [PATCH 11/11] chore: compare funcNode without aliasName --- source/libs/nodes/src/nodesUtilFuncs.c | 6 +++--- source/libs/parser/src/parTranslater.c | 6 ------ 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/source/libs/nodes/src/nodesUtilFuncs.c b/source/libs/nodes/src/nodesUtilFuncs.c index 39e288f694..830092bbf2 100644 --- a/source/libs/nodes/src/nodesUtilFuncs.c +++ b/source/libs/nodes/src/nodesUtilFuncs.c @@ -1953,9 +1953,9 @@ static uint32_t funcNodeHash(const char* pKey, uint32_t len) { } static int32_t funcNodeEqual(const void* pLeft, const void* pRight, size_t len) { - if (0 != strcmp((*(const SExprNode**)pLeft)->aliasName, (*(const SExprNode**)pRight)->aliasName)) { - return 1; - } + // if (0 != strcmp((*(const SExprNode**)pLeft)->aliasName, (*(const SExprNode**)pRight)->aliasName)) { + // return 1; + // } return nodesEqualNode(*(const SNode**)pLeft, *(const SNode**)pRight) ? 0 : 1; } diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index a6d010a814..3b94bae05c 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -3108,12 +3108,6 @@ static int32_t rewriteProjectAlias(SNodeList* pProjectionList) { if ('\0' == pExpr->userAlias[0]) { strcpy(pExpr->userAlias, pExpr->aliasName); } - if (QUERY_NODE_FUNCTION == nodeType(pProject) && fmIsWindowPseudoColumnFunc(((SFunctionNode*)pProject)->funcId) && - ((0 == strcasecmp("_wstart", pExpr->userAlias) || 0 == strcasecmp("_wend", pExpr->userAlias) || - 0 == strcasecmp("_wduration", pExpr->userAlias)) && - '\0' != pExpr->aliasName[0])) { - continue; - } sprintf(pExpr->aliasName, "#expr_%d", no++); } return TSDB_CODE_SUCCESS;