From 1286f7473cc2a4ece9f0ea1789f68f6a97d7a73c Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Wed, 28 Sep 2022 22:07:16 +0800 Subject: [PATCH] QRY: opt tbname in --- source/dnode/vnode/inc/vnode.h | 3 ++ source/dnode/vnode/src/meta/metaOpen.c | 12 ++--- source/dnode/vnode/src/meta/metaQuery.c | 15 ++++++ source/dnode/vnode/src/vnd/vnodeQuery.c | 3 ++ source/libs/executor/src/executil.c | 68 +++++++++++++++++++++---- 5 files changed, 83 insertions(+), 18 deletions(-) diff --git a/source/dnode/vnode/inc/vnode.h b/source/dnode/vnode/inc/vnode.h index 6ba10641f5..35ea512f81 100644 --- a/source/dnode/vnode/inc/vnode.h +++ b/source/dnode/vnode/inc/vnode.h @@ -62,7 +62,9 @@ void vnodeGetSnapshot(SVnode *pVnode, SSnapshot *pSnapshot); void vnodeGetInfo(SVnode *pVnode, const char **dbname, int32_t *vgId); int32_t vnodeProcessCreateTSma(SVnode *pVnode, void *pCont, uint32_t contLen); int32_t vnodeGetAllTableList(SVnode *pVnode, uint64_t uid, SArray *list); + int32_t vnodeGetCtbIdList(SVnode *pVnode, int64_t suid, SArray *list); +int32_t vnodeGetCtbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg), void *arg); int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list); void *vnodeGetIdx(SVnode *pVnode); void *vnodeGetIvtIdx(SVnode *pVnode); @@ -96,6 +98,7 @@ int32_t metaGetTableTags(SMeta *pMeta, uint64_t suid, SArray *uidList, SHash int32_t metaReadNext(SMetaReader *pReader); const void *metaGetTableTagVal(void *tag, int16_t type, STagVal *tagVal); int metaGetTableNameByUid(void *meta, uint64_t uid, char *tbName); +int metaGetTableUidByName(void *meta, char *tbName, uint64_t *uid); bool metaIsTableExist(SMeta *pMeta, tb_uid_t uid); typedef struct SMetaFltParam { diff --git a/source/dnode/vnode/src/meta/metaOpen.c b/source/dnode/vnode/src/meta/metaOpen.c index f8ecd17cb7..034d6260da 100644 --- a/source/dnode/vnode/src/meta/metaOpen.c +++ b/source/dnode/vnode/src/meta/metaOpen.c @@ -200,36 +200,30 @@ int metaClose(SMeta *pMeta) { int32_t metaRLock(SMeta *pMeta) { int32_t ret = 0; - metaTrace("meta rlock %p B", &pMeta->lock); + metaTrace("meta rlock %p", &pMeta->lock); ret = taosThreadRwlockRdlock(&pMeta->lock); - metaTrace("meta rlock %p E", &pMeta->lock); - return ret; } int32_t metaWLock(SMeta *pMeta) { int32_t ret = 0; - metaTrace("meta wlock %p B", &pMeta->lock); + metaTrace("meta wlock %p", &pMeta->lock); ret = taosThreadRwlockWrlock(&pMeta->lock); - metaTrace("meta wlock %p E", &pMeta->lock); - return ret; } int32_t metaULock(SMeta *pMeta) { int32_t ret = 0; - metaTrace("meta ulock %p B", &pMeta->lock); + metaTrace("meta ulock %p", &pMeta->lock); ret = taosThreadRwlockUnlock(&pMeta->lock); - metaTrace("meta ulock %p E", &pMeta->lock); - return ret; } diff --git a/source/dnode/vnode/src/meta/metaQuery.c b/source/dnode/vnode/src/meta/metaQuery.c index f549ef84f8..995e30bd4c 100644 --- a/source/dnode/vnode/src/meta/metaQuery.c +++ b/source/dnode/vnode/src/meta/metaQuery.c @@ -202,6 +202,21 @@ int metaGetTableNameByUid(void *meta, uint64_t uid, char *tbName) { return 0; } +int metaGetTableUidByName(void *meta, char *tbName, uint64_t *uid) { + int code = 0; + SMetaReader mr = {0}; + metaReaderInit(&mr, (SMeta *)meta, 0); + code = metaGetTableEntryByName(&mr, tbName); + if (code < 0) { + metaReaderClear(&mr); + return -1; + } + *uid = mr.me.uid; + + metaReaderClear(&mr); + + return 0; +} int metaReadNext(SMetaReader *pReader) { SMeta *pMeta = pReader->pMeta; diff --git a/source/dnode/vnode/src/vnd/vnodeQuery.c b/source/dnode/vnode/src/vnd/vnodeQuery.c index 825bf8a0a7..6806c034dc 100644 --- a/source/dnode/vnode/src/vnd/vnodeQuery.c +++ b/source/dnode/vnode/src/vnd/vnodeQuery.c @@ -409,6 +409,9 @@ int32_t vnodeGetAllTableList(SVnode *pVnode, uint64_t uid, SArray *list) { return TSDB_CODE_SUCCESS; } +int32_t vnodeGetCtbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg), void *arg) { + return 0; +} int32_t vnodeGetCtbIdList(SVnode *pVnode, int64_t suid, SArray *list) { SMCtbCursor *pCur = metaOpenCtbCursor(pVnode->pMeta, suid); diff --git a/source/libs/executor/src/executil.c b/source/libs/executor/src/executil.c index b26603b394..fa0142973b 100644 --- a/source/libs/executor/src/executil.c +++ b/source/libs/executor/src/executil.c @@ -26,6 +26,8 @@ #include "executorimpl.h" #include "tcompression.h" +static int32_t optimizeTbnameInCond(void* metaHandle, int64_t suid, SArray* list, SNode* pTagCond); + void initResultRowInfo(SResultRowInfo* pResultRowInfo) { pResultRowInfo->size = 0; pResultRowInfo->cur.pageId = -1; @@ -407,11 +409,15 @@ static SColumnInfoData* getColInfoResult(void* metaHandle, uint64_t suid, SArray // int64_t stt = taosGetTimestampUs(); tags = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK); - code = metaGetTableTags(metaHandle, suid, uidList, tags); - if (code != TSDB_CODE_SUCCESS) { - qError("failed to get table tags from meta, reason:%s, suid:%" PRIu64, tstrerror(code), suid); - terrno = code; - goto end; + + int32_t filter = optimizeTbnameInCond(metaHandle, suid, uidList, pTagCond); + if (filter == -1) { + code = metaGetTableTags(metaHandle, suid, uidList, tags); + if (code != TSDB_CODE_SUCCESS) { + qError("failed to get table tags from meta, reason:%s, suid:%" PRIu64, tstrerror(code), suid); + terrno = code; + goto end; + } } int32_t rows = taosArrayGetSize(uidList); @@ -742,6 +748,53 @@ end: return code; } +static int32_t optimizeTbnameInCond(void* metaHandle, int64_t suid, SArray* list, SNode* pTagCond) { + if (nodeType(pTagCond) != QUERY_NODE_OPERATOR) { + return -1; + } + SOperatorNode* pNode = (SOperatorNode*)pTagCond; + if (pNode->opType != OP_TYPE_IN) { + return -1; + } + if ((pNode->pLeft != NULL && nodeType(pNode->pLeft) == QUERY_NODE_COLUMN && + ((SColumnNode*)pNode->pLeft)->colType == COLUMN_TYPE_TBNAME) && + (pNode->pRight != NULL && nodeType(pNode->pRight) == QUERY_NODE_NODE_LIST)) { + SNodeListNode* pList = (SNodeListNode*)pNode->pRight; + + int32_t len = LIST_LENGTH(pList->pNodeList); + if (len <= 0) return -1; + + SListCell* cell = pList->pNodeList->pHead; + + SArray* pTbList = taosArrayInit(len, sizeof(void*)); + for (int i = 0; i < pList->pNodeList->length; i++) { + SValueNode* valueNode = (SValueNode*)cell->pNode; + if (!IS_VAR_DATA_TYPE(valueNode->node.resType.type)) { + taosArrayDestroy(pTbList); + return -1; + } + char* name = varDataVal(valueNode->datum.p); + taosArrayPush(pTbList, &name); + + cell = cell->pNext; + } + + for (int i = 0; i < taosArrayGetSize(pTbList); i++) { + char* name = taosArrayGetP(pTbList, i); + uint64_t uid = 0; + if (metaGetTableUidByName(metaHandle, name, &uid) == 0) { + taosArrayPush(list, &uid); + } else { + terrno = 0; + qError("failed to get tableIds from by table name: %s", name); + taosArrayDestroy(pTbList); + return -1; + } + } + taosArrayDestroy(pTbList); + } + return -1; +} int32_t getTableList(void* metaHandle, void* pVnode, SScanPhysiNode* pScanNode, SNode* pTagCond, SNode* pTagIndexCond, STableListInfo* pListInfo) { int32_t code = TSDB_CODE_SUCCESS; @@ -767,9 +820,6 @@ int32_t getTableList(void* metaHandle, void* pVnode, SScanPhysiNode* pScanNode, qError("failed to get tableIds from index, reason:%s, suid:%" PRIu64, tstrerror(code), tableUid); code = TDB_CODE_SUCCESS; } - - // int64_t stt1 = taosGetTimestampUs(); - // qDebug("generate table list, cost:%ld us", stt1-stt); } else if (!pTagCond) { vnodeGetCtbIdList(pVnode, pScanNode->suid, res); } @@ -813,7 +863,7 @@ int32_t getTableList(void* metaHandle, void* pVnode, SScanPhysiNode* pScanNode, size_t numOfTables = taosArrayGetSize(res); for (int i = 0; i < numOfTables; i++) { STableKeyInfo info = {.uid = *(uint64_t*)taosArrayGet(res, i), .groupId = 0}; - void* p = taosArrayPush(pListInfo->pTableList, &info); + void* p = taosArrayPush(pListInfo->pTableList, &info); if (p == NULL) { taosArrayDestroy(res); return TSDB_CODE_OUT_OF_MEMORY;