From 28edf40c2adf98463b5750877cad3827abe2c614 Mon Sep 17 00:00:00 2001 From: kailixu Date: Wed, 18 Sep 2024 13:57:17 +0800 Subject: [PATCH] enh: support drop table with uid --- include/libs/qcom/query.h | 5 ++ source/dnode/vnode/src/vnd/vnodeQuery.c | 3 +- source/libs/catalog/src/ctgAsync.c | 12 +++- source/libs/catalog/src/ctgRemote.c | 6 +- source/libs/parser/inc/parUtil.h | 2 +- source/libs/parser/src/parTranslater.c | 80 +++++++++++++++++++++++-- 6 files changed, 98 insertions(+), 10 deletions(-) diff --git a/include/libs/qcom/query.h b/include/libs/qcom/query.h index c8c4fcad44..4de16bab3c 100644 --- a/include/libs/qcom/query.h +++ b/include/libs/qcom/query.h @@ -123,6 +123,11 @@ typedef struct STableMeta { // the schema content. SSchema schema[]; } STableMeta; +typedef struct STableMetaEx { + STableMeta* pMeta; + // END: KEEP THIS PART SAME WITH STableMeta + char tbName[TSDB_TABLE_NAME_LEN]; +} STableMetaEx; #pragma pack(pop) typedef struct SViewMeta { diff --git a/source/dnode/vnode/src/vnd/vnodeQuery.c b/source/dnode/vnode/src/vnd/vnodeQuery.c index bba2238ec5..a80491228c 100644 --- a/source/dnode/vnode/src/vnd/vnodeQuery.c +++ b/source/dnode/vnode/src/vnd/vnodeQuery.c @@ -75,7 +75,7 @@ int32_t vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg, bool direct) { (void)memcpy(metaRsp.dbFName, infoReq.dbFName, sizeof(metaRsp.dbFName)); if (!reqFromUid) { - (void)sprintf(tableFName, "%s.%s", infoReq.dbFName, infoReq.tbName); + TAOS_UNUSED(sprintf(tableFName, "%s.%s", infoReq.dbFName, infoReq.tbName)); code = vnodeValidateTableHash(pVnode, tableFName); if (code) { goto _exit4; @@ -95,6 +95,7 @@ int32_t vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg, bool direct) { code = terrno; goto _exit3; } + tstrncpy(metaRsp.tbName, tbName + VARSTR_HEADER_SIZE, TSDB_TABLE_NAME_LEN); if (metaGetTableEntryByName(&mer1, tbName + VARSTR_HEADER_SIZE) < 0) { code = terrno; goto _exit3; diff --git a/source/libs/catalog/src/ctgAsync.c b/source/libs/catalog/src/ctgAsync.c index 78ba738411..9a2da7bf92 100644 --- a/source/libs/catalog/src/ctgAsync.c +++ b/source/libs/catalog/src/ctgAsync.c @@ -2080,9 +2080,17 @@ static int32_t ctgHandleGetTbNamesRsp(SCtgTaskReq* tReq, int32_t reqType, const CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR); } - pRes->code = 0; - pRes->pRes = pOut->tbMeta; + STableMetaEx* pMetaEx = taosMemoryMalloc(sizeof(STableMetaEx)); + if (NULL == pMetaEx) { + CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY); + } + + pMetaEx->pMeta = pOut->tbMeta; pOut->tbMeta = NULL; + tstrncpy(pMetaEx->tbName, pOut->tbName, TSDB_TABLE_NAME_LEN); + + pRes->code = 0; + pRes->pRes = pMetaEx; if (0 == atomic_sub_fetch_32(&ctx->fetchNum, 1)) { TSWAP(pTask->res, ctx->pResList); taskDone = true; diff --git a/source/libs/catalog/src/ctgRemote.c b/source/libs/catalog/src/ctgRemote.c index d9ddff8474..4caa9f056d 100644 --- a/source/libs/catalog/src/ctgRemote.c +++ b/source/libs/catalog/src/ctgRemote.c @@ -702,7 +702,7 @@ int32_t ctgAddBatch(SCatalog* pCtg, int32_t vgId, SRequestConnInfo* pConn, SCtgT if (TDMT_VND_TABLE_CFG == msgType) { SCtgTbCfgCtx* ctx = (SCtgTbCfgCtx*)pTask->taskCtx; pName = ctx->pName; - } else if (TDMT_VND_TABLE_META == msgType) { + } else if (TDMT_VND_TABLE_META == msgType || TDMT_VND_TABLE_NAME == msgType) { if (CTG_TASK_GET_TB_META_BATCH == pTask->type) { SCtgTbMetasCtx* ctx = (SCtgTbMetasCtx*)pTask->taskCtx; SCtgFetch* fetch = taosArrayGet(ctx->pFetchs, tReq->msgIdx); @@ -716,6 +716,10 @@ int32_t ctgAddBatch(SCatalog* pCtg, int32_t vgId, SRequestConnInfo* pConn, SCtgT ctgError("fail to get %d SName, totalTables:%d", pFetch->tbIdx, (int32_t)taosArrayGetSize(pTbReq->pTables)); CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR); } + } else if (CTG_TASK_GET_TB_NAME == pTask->type) { + SCtgTbMetasCtx* ctx = (SCtgTbMetasCtx*)pTask->taskCtx; + SCtgFetch* fetch = taosArrayGet(ctx->pFetchs, tReq->msgIdx); + CTG_ERR_JRET(ctgGetFetchName(ctx->pNames, fetch, &pName)); } else { SCtgTbMetaCtx* ctx = (SCtgTbMetaCtx*)pTask->taskCtx; pName = ctx->pName; diff --git a/source/libs/parser/inc/parUtil.h b/source/libs/parser/inc/parUtil.h index 49f8375eb1..20af3ed47e 100644 --- a/source/libs/parser/inc/parUtil.h +++ b/source/libs/parser/inc/parUtil.h @@ -112,7 +112,7 @@ typedef struct SParseMetaCache { SHashObj* pViews; // key is viewFName, element is SViewMeta* SHashObj* pTableTSMAs; // key is tbFName, elements are SArray SHashObj* pTSMAs; // key is tsmaFName, elements are STableTSMAInfo* - SHashObj* pTableName; // key is tbFUid, elements is tbName + SHashObj* pTableName; // key is tbFUid, elements is STableMetaEx* SArray* pDnodes; // element is SEpSet bool dnodeRequired; bool qnodeRequired; diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index 133d0d3a7e..52b36467eb 100755 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -509,6 +509,71 @@ static int32_t getTargetName(STranslateContext* pCxt, const SName* pName, char* return code; } +static int32_t rewriteDropMetaCache(STranslateContext* pCxt) { + int32_t code = TSDB_CODE_SUCCESS; + SParseContext* pParCxt = pCxt->pParseCxt; + SParseMetaCache* pMetaCache = pCxt->pMetaCache; + int32_t tbMetaSize = taosHashGetSize(pMetaCache->pTableMeta); + int32_t tbMetaExSize = taosHashGetSize(pMetaCache->pTableName); + + if (tbMetaSize > 0 || tbMetaExSize <= 0) { + return TSDB_CODE_PAR_INTERNAL_ERROR; + } + if (!pMetaCache->pTableMeta && + !(pMetaCache->pTableMeta = + taosHashInit(tbMetaExSize, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK))) { + return TSDB_CODE_OUT_OF_MEMORY; + } + + SMetaRes** ppMetaRes = NULL; + char dbName[TSDB_DB_NAME_LEN] = {0}; + while ((ppMetaRes = taosHashIterate(pMetaCache->pTableName, ppMetaRes))) { + if (!(*ppMetaRes)) { + taosHashCancelIterate(pMetaCache->pTableName, ppMetaRes); + return TSDB_CODE_PAR_INTERNAL_ERROR; + } + + char* pKey = taosHashGetKey(ppMetaRes, NULL); + STableMetaEx* pMetaEx = (STableMetaEx*)(*ppMetaRes)->pRes; + if (!pMetaEx || !pMetaEx->pMeta) { + taosHashCancelIterate(pMetaCache->pTableName, ppMetaRes); + return TSDB_CODE_PAR_INTERNAL_ERROR; + } + char* pDbStart = strstr(pKey, "."); + char* pDbEnd = pDbStart ? strstr(pDbStart + 1, ".") : NULL; + if (!pDbEnd) { + taosHashCancelIterate(pMetaCache->pTableName, ppMetaRes); + return TSDB_CODE_PAR_INTERNAL_ERROR; + } + tstrncpy(dbName, pDbStart + 1, pDbEnd - pDbStart); + SName name = {0}; + toName(pParCxt->acctId, dbName, pMetaEx->tbName, &name); + + char fullName[TSDB_TABLE_FNAME_LEN]; + code = tNameExtractFullName(&name, fullName); + if (TSDB_CODE_SUCCESS != code) { + taosHashCancelIterate(pMetaCache->pTableName, ppMetaRes); + return code; + } + + if ((code = taosHashPut(pMetaCache->pTableMeta, fullName, strlen(fullName), ppMetaRes, POINTER_BYTES))) { + taosHashCancelIterate(pMetaCache->pTableName, ppMetaRes); + return code; + } + + SMetaRes **qqMetaRes = taosHashGet(pMetaCache->pTableMeta, fullName, strlen(fullName)); + if (!qqMetaRes) { + taosHashCancelIterate(pMetaCache->pTableName, ppMetaRes); + return TSDB_CODE_OUT_OF_MEMORY; + } + } + taosHashCleanup(pMetaCache->pTableName); + if (code) { + assert(0); + } + return code; +} + int32_t getTargetMetaImpl(SParseContext* pParCxt, SParseMetaCache* pMetaCache, const SName* pName, STableMeta** pMeta, bool couldBeView) { int32_t code = TSDB_CODE_SUCCESS; @@ -14459,8 +14524,10 @@ int32_t serializeVgroupsDropTableBatch(SHashObj* pVgroupHashmap, SArray** pOut) return code; } -static int32_t rewriteDropTablewithOpt(STranslateContext* pCxt, SDropTableStmt* pStmt) { - if (!pStmt->withOpt) return TSDB_CODE_SUCCESS; +static int32_t rewriteDropTablewithOpt(STranslateContext* pCxt, SQuery* pQuery) { + int32_t code = TSDB_CODE_SUCCESS; + SDropTableStmt* pStmt = (SDropTableStmt*)pQuery->pRoot; + if (!pStmt->withOpt) return code; SNode* pNode = NULL; char pTableName[TSDB_TABLE_NAME_LEN] = {0}; @@ -14473,9 +14540,12 @@ static int32_t rewriteDropTablewithOpt(STranslateContext* pCxt, SDropTableStmt* if (TSDB_CODE_SUCCESS != code) { return generateSyntaxErrMsgExt(&pCxt->msgBuf, code, "Table uid does not exist: '%s'", pClause->tableName); } - // tstrncpy(pClause->tableName, pTableMeta->, TSDB_TABLE_NAME_LEN); // rewrite table uid to table name + tstrncpy(pClause->tableName, pTableName, TSDB_TABLE_NAME_LEN); // rewrite table uid to table name } - return TSDB_CODE_SUCCESS; + + code = rewriteDropMetaCache(pCxt); + + TAOS_RETURN(code); } static int32_t rewriteDropTable(STranslateContext* pCxt, SQuery* pQuery) { @@ -14484,7 +14554,7 @@ static int32_t rewriteDropTable(STranslateContext* pCxt, SQuery* pQuery) { SNode* pNode; SArray* pTsmas = NULL; - TAOS_CHECK_RETURN(rewriteDropTablewithOpt(pCxt, pStmt)); + TAOS_CHECK_RETURN(rewriteDropTablewithOpt(pCxt, pQuery)); SHashObj* pVgroupHashmap = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK); if (NULL == pVgroupHashmap) {