From d0c1e660c2ef09c0f913633e20078db6a577c3fc Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Sun, 28 Apr 2024 11:31:46 +0800 Subject: [PATCH 1/3] fix: ctable tag scan issue --- source/libs/planner/inc/planInt.h | 1 + source/libs/planner/src/planLogicCreater.c | 54 -------------------- source/libs/planner/src/planOptimizer.c | 3 +- source/libs/planner/src/planUtil.c | 58 ++++++++++++++++++++++ tests/script/tsim/join/inner_join.sim | 8 +++ 5 files changed, 69 insertions(+), 55 deletions(-) diff --git a/source/libs/planner/inc/planInt.h b/source/libs/planner/inc/planInt.h index 24b31f9f37..79cf87d941 100644 --- a/source/libs/planner/inc/planInt.h +++ b/source/libs/planner/inc/planInt.h @@ -60,6 +60,7 @@ bool keysHasCol(SNodeList* pKeys); bool keysHasTbname(SNodeList* pKeys); SFunctionNode* createGroupKeyAggFunc(SColumnNode* pGroupCol); int32_t getTimeRangeFromNode(SNode** pPrimaryKeyCond, STimeWindow* pTimeRange, bool* pIsStrict); +int32_t tagScanSetExecutionMode(SScanLogicNode* pScan); #define CLONE_LIMIT 1 #define CLONE_SLIMIT 1 << 1 diff --git a/source/libs/planner/src/planLogicCreater.c b/source/libs/planner/src/planLogicCreater.c index 60bce622be..23b8baf031 100644 --- a/source/libs/planner/src/planLogicCreater.c +++ b/source/libs/planner/src/planLogicCreater.c @@ -392,60 +392,6 @@ static int32_t makeScanLogicNode(SLogicPlanContext* pCxt, SRealTableNode* pRealT static bool needScanDefaultCol(EScanType scanType) { return SCAN_TYPE_TABLE_COUNT != scanType; } -static EDealRes tagScanNodeHasTbnameFunc(SNode* pNode, void* pContext) { - if (QUERY_NODE_FUNCTION == nodeType(pNode) && FUNCTION_TYPE_TBNAME == ((SFunctionNode*)pNode)->funcType || - (QUERY_NODE_COLUMN == nodeType(pNode) && COLUMN_TYPE_TBNAME == ((SColumnNode*)pNode)->colType)) { - *(bool*)pContext = true; - return DEAL_RES_END; - } - return DEAL_RES_CONTINUE; -} - -static bool tagScanNodeListHasTbname(SNodeList* pCols) { - bool hasTbname = false; - nodesWalkExprs(pCols, tagScanNodeHasTbnameFunc, &hasTbname); - return hasTbname; -} - -static bool tagScanNodeHasTbname(SNode* pKeys) { - bool hasTbname = false; - nodesWalkExpr(pKeys, tagScanNodeHasTbnameFunc, &hasTbname); - return hasTbname; -} - -static int32_t tagScanSetExecutionMode(SScanLogicNode* pScan) { - pScan->onlyMetaCtbIdx = false; - - if (pScan->tableType == TSDB_CHILD_TABLE) { - pScan->onlyMetaCtbIdx = false; - return TSDB_CODE_SUCCESS; - } - - if (tagScanNodeListHasTbname(pScan->pScanPseudoCols)) { - pScan->onlyMetaCtbIdx = false; - return TSDB_CODE_SUCCESS; - } - - if (pScan->node.pConditions == NULL) { - pScan->onlyMetaCtbIdx = true; - return TSDB_CODE_SUCCESS; - } - - SNode* pCond = nodesCloneNode(pScan->node.pConditions); - SNode* pTagCond = NULL; - SNode* pTagIndexCond = NULL; - filterPartitionCond(&pCond, NULL, &pTagIndexCond, &pTagCond, NULL); - if (pTagIndexCond || tagScanNodeHasTbname(pTagCond)) { - pScan->onlyMetaCtbIdx = false; - } else { - pScan->onlyMetaCtbIdx = true; - } - nodesDestroyNode(pCond); - nodesDestroyNode(pTagIndexCond); - nodesDestroyNode(pTagCond); - return TSDB_CODE_SUCCESS; -} - static int32_t createScanLogicNode(SLogicPlanContext* pCxt, SSelectStmt* pSelect, SRealTableNode* pRealTable, SLogicNode** pLogicNode) { SScanLogicNode* pScan = NULL; diff --git a/source/libs/planner/src/planOptimizer.c b/source/libs/planner/src/planOptimizer.c index eee0766589..191f7167e1 100644 --- a/source/libs/planner/src/planOptimizer.c +++ b/source/libs/planner/src/planOptimizer.c @@ -5142,7 +5142,6 @@ int32_t stbJoinOptRewriteToTagScan(SLogicNode* pJoin, SNode* pNode) { NODES_DESTORY_NODE(pScan->node.pConditions); pScan->node.requireDataOrder = DATA_ORDER_LEVEL_NONE; pScan->node.resultDataOrder = DATA_ORDER_LEVEL_NONE; - pScan->onlyMetaCtbIdx = true; SNodeList* pTags = nodesMakeList(); int32_t code = nodesCollectColumnsFromNode(pJoinNode->pTagEqCond, NULL, COLLECT_COL_TYPE_TAG, &pTags); @@ -5177,6 +5176,8 @@ int32_t stbJoinOptRewriteToTagScan(SLogicNode* pJoin, SNode* pNode) { code = stbJoinOptAddFuncToScanNode("_vgid", pScan); } + tagScanSetExecutionMode(pScan); + if (code) { nodesDestroyList(pTags); } diff --git a/source/libs/planner/src/planUtil.c b/source/libs/planner/src/planUtil.c index 3b9b348ff5..02572a1a90 100644 --- a/source/libs/planner/src/planUtil.c +++ b/source/libs/planner/src/planUtil.c @@ -615,3 +615,61 @@ int32_t getTimeRangeFromNode(SNode** pPrimaryKeyCond, STimeWindow* pTimeRange, b } +static EDealRes tagScanNodeHasTbnameFunc(SNode* pNode, void* pContext) { + if (QUERY_NODE_FUNCTION == nodeType(pNode) && FUNCTION_TYPE_TBNAME == ((SFunctionNode*)pNode)->funcType || + (QUERY_NODE_COLUMN == nodeType(pNode) && COLUMN_TYPE_TBNAME == ((SColumnNode*)pNode)->colType)) { + *(bool*)pContext = true; + return DEAL_RES_END; + } + return DEAL_RES_CONTINUE; +} + +static bool tagScanNodeListHasTbname(SNodeList* pCols) { + bool hasTbname = false; + nodesWalkExprs(pCols, tagScanNodeHasTbnameFunc, &hasTbname); + return hasTbname; +} + +static bool tagScanNodeHasTbname(SNode* pKeys) { + bool hasTbname = false; + nodesWalkExpr(pKeys, tagScanNodeHasTbnameFunc, &hasTbname); + return hasTbname; +} + + + +int32_t tagScanSetExecutionMode(SScanLogicNode* pScan) { + pScan->onlyMetaCtbIdx = false; + + if (pScan->tableType == TSDB_CHILD_TABLE) { + pScan->onlyMetaCtbIdx = false; + return TSDB_CODE_SUCCESS; + } + + if (tagScanNodeListHasTbname(pScan->pScanPseudoCols)) { + pScan->onlyMetaCtbIdx = false; + return TSDB_CODE_SUCCESS; + } + + if (pScan->node.pConditions == NULL) { + pScan->onlyMetaCtbIdx = true; + return TSDB_CODE_SUCCESS; + } + + SNode* pCond = nodesCloneNode(pScan->node.pConditions); + SNode* pTagCond = NULL; + SNode* pTagIndexCond = NULL; + filterPartitionCond(&pCond, NULL, &pTagIndexCond, &pTagCond, NULL); + if (pTagIndexCond || tagScanNodeHasTbname(pTagCond)) { + pScan->onlyMetaCtbIdx = false; + } else { + pScan->onlyMetaCtbIdx = true; + } + nodesDestroyNode(pCond); + nodesDestroyNode(pTagIndexCond); + nodesDestroyNode(pTagCond); + return TSDB_CODE_SUCCESS; +} + + + diff --git a/tests/script/tsim/join/inner_join.sim b/tests/script/tsim/join/inner_join.sim index 65a6a3e3ff..7b9209813d 100644 --- a/tests/script/tsim/join/inner_join.sim +++ b/tests/script/tsim/join/inner_join.sim @@ -194,4 +194,12 @@ if $rows != 144 then return -1 endi +sql select a.ts, b.ts from tba1 a join sta b on a.ts = b.ts and a.t1 = b.t1; +if $rows != 4 then + return -1 +endi +sql select a.ts, b.ts from sta a join sta b on a.ts = b.ts and a.t1 = b.t1; +if $rows != 8 then + return -1 +endi From d9c112244b50933bcc177f8dbc4e6a2cc50d2be7 Mon Sep 17 00:00:00 2001 From: Yihao Deng Date: Sun, 28 Apr 2024 03:48:00 +0000 Subject: [PATCH 2/3] profile opt --- source/dnode/mnode/impl/src/mndProfile.c | 33 +++++++++++++++++------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/source/dnode/mnode/impl/src/mndProfile.c b/source/dnode/mnode/impl/src/mndProfile.c index 22d2eb5a59..fafdb539fb 100644 --- a/source/dnode/mnode/impl/src/mndProfile.c +++ b/source/dnode/mnode/impl/src/mndProfile.c @@ -22,10 +22,10 @@ #include "mndPrivilege.h" #include "mndQnode.h" #include "mndShow.h" +#include "mndSma.h" #include "mndStb.h" #include "mndUser.h" #include "mndView.h" -#include "mndSma.h" #include "tglobal.h" #include "tversion.h" @@ -57,6 +57,13 @@ typedef struct { int64_t lastAccessTimeMs; } SAppObj; +typedef struct { + int32_t totalDnodes; + int32_t onlineDnodes; + SEpSet epSet; + SArray *pQnodeList; +} SConnPreparedObj; + static SConnObj *mndCreateConn(SMnode *pMnode, const char *user, int8_t connType, uint32_t ip, uint16_t port, int32_t pid, const char *app, int64_t startTime); static void mndFreeConn(SConnObj *pConn); @@ -460,7 +467,7 @@ static int32_t mndGetOnlineDnodeNum(SMnode *pMnode, int32_t *num) { } static int32_t mndProcessQueryHeartBeat(SMnode *pMnode, SRpcMsg *pMsg, SClientHbReq *pHbReq, - SClientHbBatchRsp *pBatchRsp) { + SClientHbBatchRsp *pBatchRsp, SConnPreparedObj *pObj) { SProfileMgmt *pMgmt = &pMnode->profileMgmt; SClientHbRsp hbRsp = {.connKey = pHbReq->connKey, .status = 0, .info = NULL, .query = NULL}; SRpcConnInfo connInfo = pMsg->info.conn; @@ -503,11 +510,11 @@ static int32_t mndProcessQueryHeartBeat(SMnode *pMnode, SRpcMsg *pMsg, SClientHb } rspBasic->connId = pConn->id; - rspBasic->totalDnodes = mndGetDnodeSize(pMnode); - mndGetOnlineDnodeNum(pMnode, &rspBasic->onlineDnodes); - mndGetMnodeEpSet(pMnode, &rspBasic->epSet); - - mndCreateQnodeList(pMnode, &rspBasic->pQnodeList, -1); + rspBasic->connId = pConn->id; + rspBasic->totalDnodes = pObj->totalDnodes; + rspBasic->onlineDnodes = pObj->onlineDnodes; + rspBasic->epSet = pObj->epSet; + rspBasic->pQnodeList = taosArrayDup(pObj->pQnodeList, NULL); mndReleaseConn(pMnode, pConn, true); @@ -608,7 +615,7 @@ static int32_t mndProcessQueryHeartBeat(SMnode *pMnode, SRpcMsg *pMsg, SClientHb } #endif case HEARTBEAT_KEY_TSMA: { - void * rspMsg = NULL; + void *rspMsg = NULL; int32_t rspLen = 0; mndValidateTSMAInfo(pMnode, kv->value, kv->valueLen / sizeof(STSMAVersion), &rspMsg, &rspLen); if (rspMsg && rspLen > 0) { @@ -641,6 +648,12 @@ static int32_t mndProcessHeartBeatReq(SRpcMsg *pReq) { return -1; } + SConnPreparedObj obj = {0}; + obj.totalDnodes = mndGetDnodeSize(pMnode); + mndGetOnlineDnodeNum(pMnode, &obj.onlineDnodes); + mndGetMnodeEpSet(pMnode, &obj.epSet); + mndCreateQnodeList(pMnode, &obj.pQnodeList, -1); + SClientHbBatchRsp batchRsp = {0}; batchRsp.svrTimestamp = taosGetTimestampSec(); batchRsp.rsps = taosArrayInit(0, sizeof(SClientHbRsp)); @@ -649,7 +662,7 @@ static int32_t mndProcessHeartBeatReq(SRpcMsg *pReq) { for (int i = 0; i < sz; i++) { SClientHbReq *pHbReq = taosArrayGet(batchReq.reqs, i); if (pHbReq->connKey.connType == CONN_TYPE__QUERY) { - mndProcessQueryHeartBeat(pMnode, pReq, pHbReq, &batchRsp); + mndProcessQueryHeartBeat(pMnode, pReq, pHbReq, &batchRsp, &obj); } else if (pHbReq->connKey.connType == CONN_TYPE__TMQ) { SClientHbRsp *pRsp = mndMqHbBuildRsp(pMnode, pHbReq); if (pRsp != NULL) { @@ -668,6 +681,8 @@ static int32_t mndProcessHeartBeatReq(SRpcMsg *pReq) { pReq->info.rspLen = tlen; pReq->info.rsp = buf; + taosArrayDestroy(obj.pQnodeList); + return 0; } From 0a88c4a9cf26b99884be1100b9c871c10d1e96d5 Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Sun, 28 Apr 2024 13:39:07 +0800 Subject: [PATCH 3/3] fix: correct join operator --- docs/en/12-taos-sql/30-join.md | 8 ++++---- docs/zh/12-taos-sql/30-join.md | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/en/12-taos-sql/30-join.md b/docs/en/12-taos-sql/30-join.md index 46573610cf..54dd8e2c6b 100755 --- a/docs/en/12-taos-sql/30-join.md +++ b/docs/en/12-taos-sql/30-join.md @@ -191,11 +191,11 @@ Left/Right ASOF Join are supported between super tables, normal tables, child ta | **Operator** | **Meaning for Left ASOF Join** | | :-------------: | ------------------------ | - | > | Match rows in the right table whose primary key timestamp is less than and the most closed to the left table's primary key timestamp | - | >= | Match rows in the right table whose primary key timestamp is less than or equal to and the most closed to the left table's primary key timestamp | + | > | Match rows in the right table whose primary key timestamp is less than and the most closed to the left table's primary key timestamp | + | >= | Match rows in the right table whose primary key timestamp is less than or equal to and the most closed to the left table's primary key timestamp | | = | Match rows in the right table whose primary key timestamp is equal to the left table's primary key timestamp | - | < | Match rows in the right table whose the primary key timestamp is greater than and the most closed to the left table's primary key timestamp | - | <= | Match rows in the right table whose primary key timestamp is greater than or equal to and the most closed to the left table's primary key timestamp | + | < | Match rows in the right table whose the primary key timestamp is greater than and the most closed to the left table's primary key timestamp | + | <= | Match rows in the right table whose primary key timestamp is greater than or equal to and the most closed to the left table's primary key timestamp | For Right ASOF Join, the above operators have the opposite meaning. diff --git a/docs/zh/12-taos-sql/30-join.md b/docs/zh/12-taos-sql/30-join.md index 35bf6d8cfe..c0daeb41c0 100755 --- a/docs/zh/12-taos-sql/30-join.md +++ b/docs/zh/12-taos-sql/30-join.md @@ -185,11 +185,11 @@ SELECT ... FROM table_name1 LEFT|RIGHT ASOF JOIN table_name2 [ON ...] [JLIMIT jl | **运算符** | **Left ASOF 时含义** | | :-------------: | ------------------------ | - | > | 匹配右表中主键时间戳小于左表主键时间戳且时间戳最接近的数据行 | - | >= | 匹配右表中主键时间戳小于等于左表主键时间戳且时间戳最接近的数据行 | + | > | 匹配右表中主键时间戳小于左表主键时间戳且时间戳最接近的数据行 | + | >= | 匹配右表中主键时间戳小于等于左表主键时间戳且时间戳最接近的数据行 | | = | 匹配右表中主键时间戳等于左表主键时间戳的行 | - | < | 匹配右表中主键时间戳大于左表主键时间戳且时间戳最接近的数据行 | - | <= | 匹配右表中主键时间戳大于等于左表主键时间戳且时间戳最接近的数据行 | + | < | 匹配右表中主键时间戳大于左表主键时间戳且时间戳最接近的数据行 | + | <= | 匹配右表中主键时间戳大于等于左表主键时间戳且时间戳最接近的数据行 | 对于 Right ASOF 来说,上述运算符含义正好相反。