From 948845ea3516b8ca2b5839d031d53e26b9c2bbb0 Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Fri, 2 Feb 2024 10:38:09 +0800 Subject: [PATCH] fix: join order by not work issue --- source/libs/planner/src/planOptimizer.c | 58 +++++++++-------- tests/script/tsim/query/join_order.sim | 85 +++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 28 deletions(-) diff --git a/source/libs/planner/src/planOptimizer.c b/source/libs/planner/src/planOptimizer.c index af9ec93f65..cda1614612 100644 --- a/source/libs/planner/src/planOptimizer.c +++ b/source/libs/planner/src/planOptimizer.c @@ -1455,28 +1455,25 @@ static int32_t sortForJoinOptimizeImpl(SOptimizeContext* pCxt, SLogicSubplan* pL SLogicNode* pLeft = (SLogicNode*)nodesListGetNode(pJoin->node.pChildren, 0); SLogicNode* pRight = (SLogicNode*)nodesListGetNode(pJoin->node.pChildren, 1); SScanLogicNode* pScan = NULL; + SLogicNode* pChild = NULL; + SNode** pChildPos = NULL; + EOrder targetOrder = 0; + SSHashObj* pTables = NULL; if (QUERY_NODE_LOGIC_PLAN_SCAN == nodeType(pLeft) && ((SScanLogicNode*)pLeft)->node.outputTsOrder != SCAN_ORDER_BOTH) { pScan = (SScanLogicNode*)pLeft; + pChild = pRight; + pChildPos = &pJoin->node.pChildren->pTail->pNode; + targetOrder = pScan->node.outputTsOrder; } else if (QUERY_NODE_LOGIC_PLAN_SCAN == nodeType(pRight) && ((SScanLogicNode*)pRight)->node.outputTsOrder != SCAN_ORDER_BOTH) { pScan = (SScanLogicNode*)pRight; - } - - if (NULL != pScan) { - switch (pScan->node.outputTsOrder) { - case SCAN_ORDER_ASC: - pScan->scanSeq[0] = 0; - pScan->scanSeq[1] = 1; - pScan->node.outputTsOrder = ORDER_DESC; - goto _return; - case SCAN_ORDER_DESC: - pScan->scanSeq[0] = 1; - pScan->scanSeq[1] = 0; - pScan->node.outputTsOrder = ORDER_ASC; - goto _return; - default: - break; - } + pChild = pLeft; + pChildPos = &pJoin->node.pChildren->pHead->pNode; + targetOrder = pScan->node.outputTsOrder; + } else { + pChild = pRight; + pChildPos = &pJoin->node.pChildren->pTail->pNode; + targetOrder = pLeft->outputTsOrder; } if (QUERY_NODE_OPERATOR != nodeType(pJoin->pPrimKeyEqCond)) { @@ -1490,16 +1487,15 @@ static int32_t sortForJoinOptimizeImpl(SOptimizeContext* pCxt, SLogicSubplan* pL } SNode* pOrderByNode = NULL; - SSHashObj* pLeftTables = NULL; - collectTableAliasFromNodes(nodesListGetNode(pJoin->node.pChildren, 0), &pLeftTables); - - if (NULL != tSimpleHashGet(pLeftTables, ((SColumnNode*)pOp->pLeft)->tableAlias, strlen(((SColumnNode*)pOp->pLeft)->tableAlias))) { + + collectTableAliasFromNodes((SNode*)pChild, &pTables); + if (NULL != tSimpleHashGet(pTables, ((SColumnNode*)pOp->pLeft)->tableAlias, strlen(((SColumnNode*)pOp->pLeft)->tableAlias))) { pOrderByNode = pOp->pLeft; - } else if (NULL != tSimpleHashGet(pLeftTables, ((SColumnNode*)pOp->pRight)->tableAlias, strlen(((SColumnNode*)pOp->pRight)->tableAlias))) { + } else if (NULL != tSimpleHashGet(pTables, ((SColumnNode*)pOp->pRight)->tableAlias, strlen(((SColumnNode*)pOp->pRight)->tableAlias))) { pOrderByNode = pOp->pRight; } - tSimpleHashCleanup(pLeftTables); + tSimpleHashCleanup(pTables); if (NULL == pOrderByNode) { return TSDB_CODE_PLAN_INTERNAL_ERROR; @@ -1510,7 +1506,13 @@ static int32_t sortForJoinOptimizeImpl(SOptimizeContext* pCxt, SLogicSubplan* pL return TSDB_CODE_OUT_OF_MEMORY; } - pSort->node.outputTsOrder = (ORDER_ASC == pLeft->outputTsOrder) ? ORDER_DESC : ORDER_ASC; + pSort->node.outputTsOrder = targetOrder; + pSort->node.pTargets = nodesCloneList(pChild->pTargets); + if (NULL == pSort->node.pTargets) { + nodesDestroyNode((SNode *)pSort); + return TSDB_CODE_OUT_OF_MEMORY; + } + pSort->groupSort = false; SOrderByExprNode* pOrder = (SOrderByExprNode*)nodesMakeNode(QUERY_NODE_ORDER_BY_EXPR); if (NULL == pOrder) { @@ -1519,7 +1521,7 @@ static int32_t sortForJoinOptimizeImpl(SOptimizeContext* pCxt, SLogicSubplan* pL } nodesListMakeAppend(&pSort->pSortKeys, (SNode*)pOrder); - pOrder->order = (ORDER_ASC == pLeft->outputTsOrder) ? ORDER_DESC : ORDER_ASC; + pOrder->order = targetOrder; pOrder->pExpr = nodesCloneNode(pOrderByNode); pOrder->nullOrder = (ORDER_ASC == pOrder->order) ? NULL_ORDER_FIRST : NULL_ORDER_LAST; if (!pOrder->pExpr) { @@ -1527,9 +1529,9 @@ static int32_t sortForJoinOptimizeImpl(SOptimizeContext* pCxt, SLogicSubplan* pL return TSDB_CODE_OUT_OF_MEMORY; } - pLeft->pParent = (SLogicNode*)pSort; - nodesListMakeAppend(&pSort->node.pChildren, (SNode*)pLeft); - pJoin->node.pChildren->pHead->pNode = (SNode*)pSort; + pChild->pParent = (SLogicNode*)pSort; + nodesListMakeAppend(&pSort->node.pChildren, (SNode*)pChild); + *pChildPos = (SNode*)pSort; pSort->node.pParent = (SLogicNode*)pJoin;; _return: diff --git a/tests/script/tsim/query/join_order.sim b/tests/script/tsim/query/join_order.sim index bd772ff407..534ab5d631 100644 --- a/tests/script/tsim/query/join_order.sim +++ b/tests/script/tsim/query/join_order.sim @@ -47,5 +47,90 @@ sql select a.*,b.* from (select * from tba1 order by ts desc) a, (select * from if $rows != 4 then return -1 endi +sql select a.*,b.* from (select * from tba1 order by ts desc) a, (select * from tba1 order by ts) b where a.ts=b.ts order by a.ts; +if $rows != 4 then + return -1 +endi +if $data00 != @23-11-17 16:29:00.000@ then + return -1 +endi +sql select a.*,b.* from (select * from tba1 order by ts desc) a, (select * from tba1 order by ts) b where a.ts=b.ts order by a.ts desc; +if $rows != 4 then + return -1 +endi +if $data00 != @23-11-17 16:29:04.000@ then + return -1 +endi +sql select a.*,b.* from (select * from tba1 order by ts) a, (select * from tba1 order by ts) b where a.ts=b.ts order by a.ts; +if $rows != 4 then + return -1 +endi +if $data00 != @23-11-17 16:29:00.000@ then + return -1 +endi +sql select a.*,b.* from (select * from tba1 order by ts) a, (select * from tba1 order by ts) b where a.ts=b.ts order by a.ts desc; +if $rows != 4 then + return -1 +endi +if $data00 != @23-11-17 16:29:04.000@ then + return -1 +endi +sql select a.*,b.* from (select * from tba1 order by ts limit 2) a, (select * from tba1 order by ts desc limit 2) b where a.ts=b.ts order by a.ts desc; +if $rows != 0 then + return -1 +endi +sql select a.*,b.* from (select * from tba1 order by ts limit 3) a, (select * from tba1 order by ts desc limit 3) b where a.ts=b.ts order by a.ts desc; +if $rows != 2 then + return -1 +endi +if $data00 != @23-11-17 16:29:03.000@ then + return -1 +endi + +sql select a.*,b.* from (select * from tba1 order by ts limit 3) a, (select * from tba1 order by ts desc limit 3) b where a.ts=b.ts order by a.ts; +if $rows != 2 then + return -1 +endi +if $data00 != @23-11-17 16:29:02.000@ then + return -1 +endi + +sql select a.*,b.* from tba1 a, (select * from tba1 order by ts desc limit 3) b where a.ts=b.ts order by a.ts; +if $rows != 3 then + return -1 +endi +if $data00 != @23-11-17 16:29:02.000@ then + return -1 +endi +sql select a.*,b.* from tba1 a, (select * from tba1 order by ts limit 3) b where a.ts=b.ts order by a.ts desc limit 2; +if $rows != 2 then + return -1 +endi +if $data00 != @23-11-17 16:29:03.000@ then + return -1 +endi + +sql select a.*,b.* from (select * from tba1 order by ts limit 3) a, tba1 b where a.ts=b.ts order by a.ts desc; +if $rows != 3 then + return -1 +endi +if $data00 != @23-11-17 16:29:03.000@ then + return -1 +endi +sql select a.*,b.* from (select * from tba1 order by ts desc limit 3) a, tba1 b where a.ts=b.ts order by a.ts desc; +if $rows != 3 then + return -1 +endi +if $data00 != @23-11-17 16:29:04.000@ then + return -1 +endi + +sql select a.*,b.* from (select * from tba1 order by ts desc limit 3) a, tba1 b where a.ts=b.ts order by a.ts; +if $rows != 3 then + return -1 +endi +if $data00 != @23-11-17 16:29:02.000@ then + return -1 +endi system sh/exec.sh -n dnode1 -s stop -x SIGINT