From 70761fd9590c125608b1a1ee6b1f143cca6acb90 Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Fri, 28 Apr 2023 17:56:52 +0800 Subject: [PATCH] enh: optimize tbname condition --- source/libs/catalog/src/ctgCache.c | 2 +- source/libs/parser/src/parTranslater.c | 67 ++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/source/libs/catalog/src/ctgCache.c b/source/libs/catalog/src/ctgCache.c index ec087c1168..433eacd30e 100644 --- a/source/libs/catalog/src/ctgCache.c +++ b/source/libs/catalog/src/ctgCache.c @@ -1082,7 +1082,7 @@ int32_t ctgUpdateUserEnqueue(SCatalog *pCtg, SGetUserAuthRsp *pAuth, bool syncOp if (NULL == msg) { ctgError("malloc %d failed", (int32_t)sizeof(SCtgUpdateUserMsg)); taosMemoryFree(op); - CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY); + CTG_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY); } msg->pCtg = pCtg; diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index e14b750aba..fcdc6418a0 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -30,6 +30,11 @@ #define SYSTABLE_SHOW_TYPE_OFFSET QUERY_NODE_SHOW_DNODES_STMT +typedef struct SRewriteTbNameContext { + int32_t errCode; + char* pTbName; +} SRewriteTbNameContext; + typedef struct STranslateContext { SParseContext* pParseCxt; int32_t errCode; @@ -2541,6 +2546,64 @@ static int32_t setTableCacheLastMode(STranslateContext* pCxt, SSelectStmt* pSele return code; } +static EDealRes doTranslateTbName(SNode** pNode, void* pContext) { + switch (nodeType(*pNode)) { + case QUERY_NODE_FUNCTION: { + SFunctionNode *pFunc = (SFunctionNode *)*pNode; + if (FUNCTION_TYPE_TBNAME == pFunc->funcType) { + SRewriteTbNameContext *pCxt = (SRewriteTbNameContext*)pContext; + SValueNode* pVal = (SValueNode*)nodesMakeNode(QUERY_NODE_VALUE); + if (NULL == pVal) { + pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY; + return DEAL_RES_ERROR; + } + + int32_t tbLen = strlen(pCxt->pTbName); + pVal->literal = taosStrdup(pCxt->pTbName); + if (NULL == pVal->literal) { + pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY; + return DEAL_RES_ERROR; + } + pVal->isDuration = false; + pVal->translate = true; + pVal->node.resType.type = TSDB_DATA_TYPE_BINARY; + pVal->node.resType.bytes = tbLen + VARSTR_HEADER_SIZE; + pVal->datum.p = taosMemoryCalloc(1, tbLen + VARSTR_HEADER_SIZE + 1); + varDataSetLen(pVal->datum.p, tbLen); + strncpy(varDataVal(pVal->datum.p), pVal->literal, tbLen); + strcpy(pVal->node.userAlias, pFunc->node.userAlias); + strcpy(pVal->node.aliasName, pFunc->node.aliasName); + + nodesDestroyNode(*pNode); + *pNode = (SNode*)pVal; + } + break; + } + default: + break; + } + return DEAL_RES_CONTINUE; +} + +static int32_t replaceTbName(STranslateContext* pCxt, SSelectStmt* pSelect) { + if (QUERY_NODE_REAL_TABLE != nodeType(pSelect->pFromTable)) { + return TSDB_CODE_SUCCESS; + } + + SRealTableNode* pTable = (SRealTableNode*)pSelect->pFromTable; + if (TSDB_CHILD_TABLE != pTable->pMeta->tableType && TSDB_NORMAL_TABLE != pTable->pMeta->tableType && TSDB_SYSTEM_TABLE != pTable->pMeta->tableType) { + return TSDB_CODE_SUCCESS; + } + + SNode** pNode = NULL; + SRewriteTbNameContext pRewriteCxt = {0}; + pRewriteCxt.pTbName = pTable->table.tableName; + + nodesRewriteExprPostOrder(&pSelect->pWhere, doTranslateTbName, &pRewriteCxt); + + return pRewriteCxt.errCode; +} + static int32_t checkJoinTable(STranslateContext* pCxt, SJoinTableNode* pJoinTable) { if ((QUERY_NODE_TEMP_TABLE == nodeType(pJoinTable->pLeft) && !isTimeLineQuery(((STempTableNode*)pJoinTable->pLeft)->pSubquery)) || @@ -3649,6 +3712,10 @@ static int32_t translateSelectFrom(STranslateContext* pCxt, SSelectStmt* pSelect if (TSDB_CODE_SUCCESS == code) { code = setTableCacheLastMode(pCxt, pSelect); } + if (TSDB_CODE_SUCCESS == code) { + code = replaceTbName(pCxt, pSelect); + } + return code; } -- GitLab