refine code

This commit is contained in:
wangjiaming0909 2024-11-14 10:19:44 +08:00
parent 1c6976ca09
commit ab7b0026b3
2 changed files with 33 additions and 17 deletions

View File

@ -3501,6 +3501,7 @@ static int32_t eliminateProjOptFindProjPrefixWithOrderCheck(SProjectLogicNode* p
*orderMatch = false; *orderMatch = false;
break; break;
} }
if (pNewChildTargets) {
SNode* pNew = NULL; SNode* pNew = NULL;
code = nodesCloneNode(pChildTarget, &pNew); code = nodesCloneNode(pChildTarget, &pNew);
if (TSDB_CODE_SUCCESS == code) { if (TSDB_CODE_SUCCESS == code) {
@ -3512,6 +3513,7 @@ static int32_t eliminateProjOptFindProjPrefixWithOrderCheck(SProjectLogicNode* p
break; break;
} }
} }
}
return code; return code;
} }
@ -3543,6 +3545,8 @@ static int32_t eliminateProjOptimizeImpl(SOptimizeContext* pCxt, SLogicSubplan*
int32_t code = 0; int32_t code = 0;
bool isSetOpProj = false; bool isSetOpProj = false;
bool orderMatch = false; bool orderMatch = false;
bool sizeMatch = LIST_LENGTH(pProjectNode->pProjections) == LIST_LENGTH(pChild->pTargets);
bool needReplaceTargets = true;
if (NULL == pProjectNode->node.pParent) { if (NULL == pProjectNode->node.pParent) {
SNodeList* pNewChildTargets = NULL; SNodeList* pNewChildTargets = NULL;
@ -3553,7 +3557,12 @@ static int32_t eliminateProjOptimizeImpl(SOptimizeContext* pCxt, SLogicSubplan*
// For sql: select ... from (select ... union all select ...); // For sql: select ... from (select ... union all select ...);
// When eliminating the outer proj (the outer select), we have to make sure that the outer proj projections and // When eliminating the outer proj (the outer select), we have to make sure that the outer proj projections and
// union all project targets have same columns in the same order. See detail in TD-30188 // union all project targets have same columns in the same order. See detail in TD-30188
code = eliminateProjOptFindProjPrefixWithOrderCheck(pProjectNode, (SProjectLogicNode*)pChild, &pNewChildTargets, &orderMatch); code = eliminateProjOptFindProjPrefixWithOrderCheck(pProjectNode, (SProjectLogicNode*)pChild,
sizeMatch ? NULL : &pNewChildTargets, &orderMatch);
if (TSDB_CODE_SUCCESS == code && sizeMatch && orderMatch) {
pNewChildTargets = pChild->pTargets;
needReplaceTargets = false;
}
} else { } else {
FOREACH(pProjection, pProjectNode->pProjections) { FOREACH(pProjection, pProjectNode->pProjections) {
FOREACH(pChildTarget, pChild->pTargets) { FOREACH(pChildTarget, pChild->pTargets) {
@ -3577,10 +3586,12 @@ static int32_t eliminateProjOptimizeImpl(SOptimizeContext* pCxt, SLogicSubplan*
} }
if (eliminateProjOptCanChildConditionUseChildTargets(pChild, pNewChildTargets) && (!isSetOpProj || orderMatch)) { if (eliminateProjOptCanChildConditionUseChildTargets(pChild, pNewChildTargets) && (!isSetOpProj || orderMatch)) {
if (needReplaceTargets) {
nodesDestroyList(pChild->pTargets); nodesDestroyList(pChild->pTargets);
pChild->pTargets = pNewChildTargets; pChild->pTargets = pNewChildTargets;
}
} else { } else {
nodesDestroyList(pNewChildTargets); if (needReplaceTargets) nodesDestroyList(pNewChildTargets);
OPTIMIZE_FLAG_SET_MASK(pProjectNode->node.optimizedFlag, OPTIMIZE_FLAG_ELIMINATE_PROJ); OPTIMIZE_FLAG_SET_MASK(pProjectNode->node.optimizedFlag, OPTIMIZE_FLAG_ELIMINATE_PROJ);
pCxt->optimized = true; pCxt->optimized = true;
return TSDB_CODE_SUCCESS; return TSDB_CODE_SUCCESS;
@ -3603,10 +3614,10 @@ static int32_t eliminateProjOptimizeImpl(SOptimizeContext* pCxt, SLogicSubplan*
NODES_CLEAR_LIST(pProjectNode->node.pChildren); NODES_CLEAR_LIST(pProjectNode->node.pChildren);
nodesDestroyNode((SNode*)pProjectNode); nodesDestroyNode((SNode*)pProjectNode);
// if pChild is a project logic node, remove its projection which is not reference by its target. // if pChild is a project logic node, remove its projection which is not reference by its target.
if (needReplaceTargets) {
alignProjectionWithTarget(pChild); alignProjectionWithTarget(pChild);
if (isSetOpProj && orderMatch) {
// Since we have eliminated the outer proj, we need to push down the new targets to the children of the set operation. // Since we have eliminated the outer proj, we need to push down the new targets to the children of the set operation.
code = eliminateProjOptPushTargetsToSetOpChildren((SProjectLogicNode*)pChild); if (isSetOpProj && orderMatch && !sizeMatch) code = eliminateProjOptPushTargetsToSetOpChildren((SProjectLogicNode*)pChild);
} }
} }
pCxt->optimized = true; pCxt->optimized = true;

View File

@ -399,7 +399,12 @@ class TDTestCase:
tdSql.query(sql, queryTimes=1) tdSql.query(sql, queryTimes=1)
tdSql.checkRows(6) tdSql.checkRows(6)
tdSql.checkCols(3) tdSql.checkCols(3)
tdSql.execute("drop database ep_iot")
sql = "select scdw_code, scdw_name, jzmc, fdgl, jzzt,ts from ((select '01072016' as scdw_code, '盛鲁电厂' as scdw_name, '机组1' as jzmc, last(jz1fdjyggl) as fdgl, '填报' as jzzt, last(ts) as ts from ep_iot.sldc_dp) union all ( select '01072016' as scdw_code, '盛鲁电厂' as scdw_name, '机组2' as jzmc, last(jz2fdjyggl) as fdgl, '填报' as jzzt, last(ts) as ts from ep_iot.sldc_dp) union all ( select '00103673' as scdw_code, '鲁西电厂' as scdw_name, '机组1'as jzmc, last(jz1fdjyggl) as fdgl, '填报' as jzzt, last(ts) as ts from ep_iot.sldc_dp) union all ( select '00103673' as scdw_code, '鲁西电厂' as scdw_name, '机组2'as jzmc, last(jz2fdjyggl) as fdgl, '填报' as jzzt, last(ts) as ts from ep_iot.sldc_dp) union all ( select '01061584' as scdw_code, '富源热电' as scdw_name, '机组1'as jzmc, last(jz1fdjyggl) as fdgl, '填报' as jzzt ,last(ts) as ts from ep_iot.sldc_dp) union all ( select '01061584' as scdw_code, '富源热电' as scdw_name, '机组2'as jzmc, last(jz2fdjyggl) as fdgl, '填报' as jzzt ,last(ts) as ts from ep_iot.sldc_dp)) where scdw_code like '%%';"
tdSql.query(sql, queryTimes=1)
tdSql.checkCols(6)
tdSql.checkRows(6)
##tdSql.execute("drop database ep_iot")
def run(self): def run(self):
tdSql.prepare() tdSql.prepare()