From f506604d653ccc832ab2dd1af915eb2a881c454e Mon Sep 17 00:00:00 2001 From: dapan Date: Wed, 11 May 2022 14:41:39 +0800 Subject: [PATCH] stmt auto create table --- include/libs/parser/parser.h | 2 +- source/client/src/clientStmt.c | 25 +++++++++++++++++++------ source/libs/parser/src/parInsertData.c | 5 ++++- tests/script/api/batchprepare.c | 13 +++++++------ 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/include/libs/parser/parser.h b/include/libs/parser/parser.h index 04f441e0e4..61e4bb3723 100644 --- a/include/libs/parser/parser.h +++ b/include/libs/parser/parser.h @@ -58,7 +58,7 @@ int32_t qBuildStmtOutput(SQuery* pQuery, SHashObj* pVgHash, SHashObj* pBlockHash int32_t qResetStmtDataBlock(void* block, bool keepBuf); int32_t qCloneStmtDataBlock(void** pDst, void* pSrc); void qFreeStmtDataBlock(void* pDataBlock); -int32_t qRebuildStmtDataBlock(void** pDst, void* pSrc, uint64_t uid); +int32_t qRebuildStmtDataBlock(void** pDst, void* pSrc, uint64_t uid, int32_t vgId); void qDestroyStmtDataBlock(void* pBlock); STableMeta *qGetTableMetaInDataBlock(void* pDataBlock); diff --git a/source/client/src/clientStmt.c b/source/client/src/clientStmt.c index d8262bc9a2..3a2363f882 100644 --- a/source/client/src/clientStmt.c +++ b/source/client/src/clientStmt.c @@ -321,6 +321,18 @@ int32_t stmtCleanSQLInfo(STscStmt* pStmt) { return TSDB_CODE_SUCCESS; } +int32_t stmtRebuildDataBlock(STscStmt* pStmt, STableDataBlocks *pDataBlock, STableDataBlocks **newBlock, uint64_t uid) { + SEpSet ep = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp); + SVgroupInfo vgInfo = {0}; + + STMT_ERR_RET(catalogGetTableHashVgroup(pStmt->pCatalog, pStmt->taos->pAppInfo->pTransporter, &ep, &pStmt->bInfo.sname, &vgInfo)); + STMT_ERR_RET(taosHashPut(pStmt->exec.pVgHash, (const char*)&vgInfo.vgId, sizeof(vgInfo.vgId), (char*)&vgInfo, sizeof(vgInfo))); + + STMT_ERR_RET(qRebuildStmtDataBlock(newBlock, pDataBlock, uid, vgInfo.vgId)); + + return TSDB_CODE_SUCCESS; +} + int32_t stmtGetFromCache(STscStmt* pStmt) { pStmt->bInfo.needParse = true; pStmt->bInfo.inExecCache = false; @@ -344,14 +356,18 @@ int32_t stmtGetFromCache(STscStmt* pStmt) { return TSDB_CODE_SUCCESS; } + if (NULL == pStmt->pCatalog) { + STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog)); + } + if (pStmt->sql.autoCreateTbl) { SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &pStmt->bInfo.tbSuid, sizeof(pStmt->bInfo.tbSuid)); if (pCache) { pStmt->bInfo.needParse = false; STableDataBlocks* pNewBlock = NULL; - STMT_ERR_RET(qRebuildStmtDataBlock(&pNewBlock, pCache->pDataBlock, 0)); - + STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataBlock, &pNewBlock, 0)); + if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock, POINTER_BYTES)) { STMT_ERR_RET(TSDB_CODE_OUT_OF_MEMORY); } @@ -362,9 +378,6 @@ int32_t stmtGetFromCache(STscStmt* pStmt) { STMT_RET(stmtCleanBindInfo(pStmt)); } - if (NULL == pStmt->pCatalog) { - STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog)); - } STableMeta *pTableMeta = NULL; SEpSet ep = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp); @@ -419,7 +432,7 @@ int32_t stmtGetFromCache(STscStmt* pStmt) { pStmt->bInfo.tagsCached = true; STableDataBlocks* pNewBlock = NULL; - STMT_ERR_RET(qRebuildStmtDataBlock(&pNewBlock, pCache->pDataBlock, uid)); + STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataBlock, &pNewBlock, uid)); if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock, POINTER_BYTES)) { STMT_ERR_RET(TSDB_CODE_OUT_OF_MEMORY); diff --git a/source/libs/parser/src/parInsertData.c b/source/libs/parser/src/parInsertData.c index d164df6db7..8deaad6091 100644 --- a/source/libs/parser/src/parInsertData.c +++ b/source/libs/parser/src/parInsertData.c @@ -620,7 +620,7 @@ int32_t qCloneStmtDataBlock(void** pDst, void* pSrc) { return qResetStmtDataBlock(*pDst, false); } -int32_t qRebuildStmtDataBlock(void** pDst, void* pSrc, uint64_t uid) { +int32_t qRebuildStmtDataBlock(void** pDst, void* pSrc, uint64_t uid, int32_t vgId) { int32_t code = qCloneStmtDataBlock(pDst, pSrc); if (code) { return code; @@ -633,8 +633,11 @@ int32_t qRebuildStmtDataBlock(void** pDst, void* pSrc, uint64_t uid) { return TSDB_CODE_OUT_OF_MEMORY; } + pBlock->vgId = vgId; + if (pBlock->pTableMeta) { pBlock->pTableMeta->uid = uid; + pBlock->pTableMeta->vgId = vgId; } memset(pBlock->pData, 0, sizeof(SSubmitBlk)); diff --git a/tests/script/api/batchprepare.c b/tests/script/api/batchprepare.c index 9016dca21e..4423ee3bc8 100644 --- a/tests/script/api/batchprepare.c +++ b/tests/script/api/batchprepare.c @@ -230,12 +230,12 @@ CaseCtrl gCaseCtrl = { .optrIdxListNum = 0, .optrIdxList = NULL, .checkParamNum = false, - .printRes = true, + .printRes = false, .runTimes = 0, .caseIdx = -1, - .caseNum = 15, - .caseRunIdx = 8, - .caseRunNum = 15, + .caseNum = -1, + .caseRunIdx = -1, + .caseRunNum = -1, }; #endif @@ -3465,16 +3465,17 @@ void* runCaseList(TAOS *taos) { } void runAll(TAOS *taos) { -/* strcpy(gCaseCtrl.caseCatalog, "Normal Test"); printf("%s Begin\n", gCaseCtrl.caseCatalog); runCaseList(taos); -*/ + +#if 0 strcpy(gCaseCtrl.caseCatalog, "Auto Create Table Test"); gCaseCtrl.autoCreateTbl = true; printf("%s Begin\n", gCaseCtrl.caseCatalog); runCaseList(taos); gCaseCtrl.autoCreateTbl = false; +#endif /* strcpy(gCaseCtrl.caseCatalog, "Null Test"); -- GitLab