From 3ed27805c37d5bbb2ca15d6b44700289444d0204 Mon Sep 17 00:00:00 2001 From: Xiaoyu Wang Date: Fri, 30 Sep 2022 14:43:51 +0800 Subject: [PATCH] fix: query information_schema.ins_tags error --- source/libs/parser/inc/parUtil.h | 1 + source/libs/parser/src/parAstParser.c | 21 +++++ source/libs/parser/src/parTranslater.c | 110 +++++++++++----------- source/libs/parser/src/parUtil.c | 67 ++++++++++++- source/libs/parser/test/parSelectTest.cpp | 6 +- source/libs/parser/test/parTestMain.cpp | 2 +- source/libs/planner/test/planTestMain.cpp | 2 +- 7 files changed, 149 insertions(+), 60 deletions(-) diff --git a/source/libs/parser/inc/parUtil.h b/source/libs/parser/inc/parUtil.h index 308afd467f..75b631ec9d 100644 --- a/source/libs/parser/inc/parUtil.h +++ b/source/libs/parser/inc/parUtil.h @@ -91,6 +91,7 @@ STableComInfo getTableInfo(const STableMeta* pTableMeta); STableMeta* tableMetaDup(const STableMeta* pTableMeta); int32_t trimString(const char* src, int32_t len, char* dst, int32_t dlen); +int32_t getInsTagsTableTargetName(int32_t acctId, SNode* pWhere, SName* pName); int32_t buildCatalogReq(SParseContext* pCxt, const SParseMetaCache* pMetaCache, SCatalogReq* pCatalogReq); int32_t putMetaDataToCache(const SCatalogReq* pCatalogReq, const SMetaData* pMetaData, SParseMetaCache* pMetaCache, diff --git a/source/libs/parser/src/parAstParser.c b/source/libs/parser/src/parAstParser.c index ce90bd3da0..27adb79e3c 100644 --- a/source/libs/parser/src/parAstParser.c +++ b/source/libs/parser/src/parAstParser.c @@ -125,6 +125,23 @@ static bool needGetTableIndex(SNode* pStmt) { return false; } +static int32_t collectMetaKeyFromInsTagsImpl(SCollectMetaKeyCxt* pCxt, SName* pName) { + if (TSDB_DB_NAME_T == pName->type) { + return reserveDbVgInfoInCache(pName->acctId, pName->dbname, pCxt->pMetaCache); + } + return reserveTableVgroupInCacheExt(pName, pCxt->pMetaCache); +} + +static int32_t collectMetaKeyFromInsTags(SCollectMetaKeyCxt* pCxt) { + SSelectStmt* pSelect = (SSelectStmt*)pCxt->pStmt; + SName name = {0}; + int32_t code = getInsTagsTableTargetName(pCxt->pParseCxt->acctId, pSelect->pWhere, &name); + if (TSDB_CODE_SUCCESS == code) { + code = collectMetaKeyFromInsTagsImpl(pCxt, &name); + } + return code; +} + static int32_t collectMetaKeyFromRealTableImpl(SCollectMetaKeyCxt* pCxt, const char* pDb, const char* pTable, AUTH_TYPE authType) { int32_t code = reserveTableMetaInCache(pCxt->pParseCxt->acctId, pDb, pTable, pCxt->pMetaCache); @@ -143,6 +160,10 @@ static int32_t collectMetaKeyFromRealTableImpl(SCollectMetaKeyCxt* pCxt, const c if (TSDB_CODE_SUCCESS == code && (0 == strcmp(pTable, TSDB_INS_TABLE_DNODE_VARIABLES))) { code = reserveDnodeRequiredInCache(pCxt->pMetaCache); } + if (TSDB_CODE_SUCCESS == code && (0 == strcmp(pTable, TSDB_INS_TABLE_TAGS)) && + QUERY_NODE_SELECT_STMT == nodeType(pCxt->pStmt)) { + code = collectMetaKeyFromInsTags(pCxt); + } return code; } diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index 9d805b8e33..b58626e6fb 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -2198,70 +2198,70 @@ static int32_t getTagsTableVgroupListImpl(STranslateContext* pCxt, SName* pTarge return code; } -static int32_t getTagsTableTargetNameFromOp(STranslateContext* pCxt, SOperatorNode* pOper, SName* pName) { - if (OP_TYPE_EQUAL != pOper->opType) { - return TSDB_CODE_SUCCESS; - } +// static int32_t getTagsTableTargetNameFromOp(STranslateContext* pCxt, SOperatorNode* pOper, SName* pName) { +// if (OP_TYPE_EQUAL != pOper->opType) { +// return TSDB_CODE_SUCCESS; +// } - SColumnNode* pCol = NULL; - SValueNode* pVal = NULL; - if (QUERY_NODE_COLUMN == nodeType(pOper->pLeft)) { - pCol = (SColumnNode*)pOper->pLeft; - } else if (QUERY_NODE_VALUE == nodeType(pOper->pLeft)) { - pVal = (SValueNode*)pOper->pLeft; - } - if (QUERY_NODE_COLUMN == nodeType(pOper->pRight)) { - pCol = (SColumnNode*)pOper->pRight; - } else if (QUERY_NODE_VALUE == nodeType(pOper->pRight)) { - pVal = (SValueNode*)pOper->pRight; - } - if (NULL == pCol || NULL == pVal) { - return TSDB_CODE_SUCCESS; - } +// SColumnNode* pCol = NULL; +// SValueNode* pVal = NULL; +// if (QUERY_NODE_COLUMN == nodeType(pOper->pLeft)) { +// pCol = (SColumnNode*)pOper->pLeft; +// } else if (QUERY_NODE_VALUE == nodeType(pOper->pLeft)) { +// pVal = (SValueNode*)pOper->pLeft; +// } +// if (QUERY_NODE_COLUMN == nodeType(pOper->pRight)) { +// pCol = (SColumnNode*)pOper->pRight; +// } else if (QUERY_NODE_VALUE == nodeType(pOper->pRight)) { +// pVal = (SValueNode*)pOper->pRight; +// } +// if (NULL == pCol || NULL == pVal) { +// return TSDB_CODE_SUCCESS; +// } - if (0 == strcmp(pCol->colName, "db_name")) { - return tNameSetDbName(pName, pCxt->pParseCxt->acctId, pVal->literal, strlen(pVal->literal)); - } else if (0 == strcmp(pCol->colName, "table_name")) { - return tNameAddTbName(pName, pVal->literal, strlen(pVal->literal)); - } +// if (0 == strcmp(pCol->colName, "db_name")) { +// return tNameSetDbName(pName, pCxt->pParseCxt->acctId, pVal->literal, strlen(pVal->literal)); +// } else if (0 == strcmp(pCol->colName, "table_name")) { +// return tNameAddTbName(pName, pVal->literal, strlen(pVal->literal)); +// } - return TSDB_CODE_SUCCESS; -} +// return TSDB_CODE_SUCCESS; +// } -static void getTagsTableTargetObjName(STranslateContext* pCxt, SNode* pNode, SName* pName) { - if (QUERY_NODE_OPERATOR == nodeType(pNode)) { - getTagsTableTargetNameFromOp(pCxt, (SOperatorNode*)pNode, pName); - } -} +// static void getTagsTableTargetObjName(STranslateContext* pCxt, SNode* pNode, SName* pName) { +// if (QUERY_NODE_OPERATOR == nodeType(pNode)) { +// getTagsTableTargetNameFromOp(pCxt, (SOperatorNode*)pNode, pName); +// } +// } -static int32_t getTagsTableTargetNameFromCond(STranslateContext* pCxt, SLogicConditionNode* pCond, SName* pName) { - if (LOGIC_COND_TYPE_AND != pCond->condType) { - return TSDB_CODE_SUCCESS; - } +// static int32_t getTagsTableTargetNameFromCond(STranslateContext* pCxt, SLogicConditionNode* pCond, SName* pName) { +// if (LOGIC_COND_TYPE_AND != pCond->condType) { +// return TSDB_CODE_SUCCESS; +// } - SNode* pNode = NULL; - FOREACH(pNode, pCond->pParameterList) { getTagsTableTargetObjName(pCxt, pNode, pName); } - if ('\0' == pName->dbname[0]) { - pName->type = 0; - } - return TSDB_CODE_SUCCESS; -} +// SNode* pNode = NULL; +// FOREACH(pNode, pCond->pParameterList) { getTagsTableTargetObjName(pCxt, pNode, pName); } +// if ('\0' == pName->dbname[0]) { +// pName->type = 0; +// } +// return TSDB_CODE_SUCCESS; +// } -static int32_t getTagsTableTargetName(STranslateContext* pCxt, SNode* pWhere, SName* pName) { - if (NULL == pWhere) { - return TSDB_CODE_SUCCESS; - } +// static int32_t getTagsTableTargetName(STranslateContext* pCxt, SNode* pWhere, SName* pName) { +// if (NULL == pWhere) { +// return TSDB_CODE_SUCCESS; +// } - if (QUERY_NODE_OPERATOR == nodeType(pWhere)) { - return getTagsTableTargetNameFromOp(pCxt, (SOperatorNode*)pWhere, pName); - } +// if (QUERY_NODE_OPERATOR == nodeType(pWhere)) { +// return getTagsTableTargetNameFromOp(pCxt, (SOperatorNode*)pWhere, pName); +// } - if (QUERY_NODE_LOGIC_CONDITION == nodeType(pWhere)) { - return getTagsTableTargetNameFromCond(pCxt, (SLogicConditionNode*)pWhere, pName); - } +// if (QUERY_NODE_LOGIC_CONDITION == nodeType(pWhere)) { +// return getTagsTableTargetNameFromCond(pCxt, (SLogicConditionNode*)pWhere, pName); +// } - return TSDB_CODE_SUCCESS; -} +// return TSDB_CODE_SUCCESS; +// } static int32_t getTagsTableVgroupList(STranslateContext* pCxt, SName* pName, SArray** pVgroupList) { if (!isSelectStmt(pCxt->pCurrStmt)) { @@ -2269,7 +2269,7 @@ static int32_t getTagsTableVgroupList(STranslateContext* pCxt, SName* pName, SAr } SSelectStmt* pSelect = (SSelectStmt*)pCxt->pCurrStmt; SName targetName = {0}; - int32_t code = getTagsTableTargetName(pCxt, pSelect->pWhere, &targetName); + int32_t code = getInsTagsTableTargetName(pCxt->pParseCxt->acctId, pSelect->pWhere, &targetName); if (TSDB_CODE_SUCCESS == code) { code = getTagsTableVgroupListImpl(pCxt, &targetName, pName, pVgroupList); } diff --git a/source/libs/parser/src/parUtil.c b/source/libs/parser/src/parUtil.c index 3075be1a00..c6a51f22e8 100644 --- a/source/libs/parser/src/parUtil.c +++ b/source/libs/parser/src/parUtil.c @@ -420,6 +420,71 @@ end: return retCode; } +static int32_t getInsTagsTableTargetNameFromOp(int32_t acctId, SOperatorNode* pOper, SName* pName) { + if (OP_TYPE_EQUAL != pOper->opType) { + return TSDB_CODE_SUCCESS; + } + + SColumnNode* pCol = NULL; + SValueNode* pVal = NULL; + if (QUERY_NODE_COLUMN == nodeType(pOper->pLeft)) { + pCol = (SColumnNode*)pOper->pLeft; + } else if (QUERY_NODE_VALUE == nodeType(pOper->pLeft)) { + pVal = (SValueNode*)pOper->pLeft; + } + if (QUERY_NODE_COLUMN == nodeType(pOper->pRight)) { + pCol = (SColumnNode*)pOper->pRight; + } else if (QUERY_NODE_VALUE == nodeType(pOper->pRight)) { + pVal = (SValueNode*)pOper->pRight; + } + if (NULL == pCol || NULL == pVal) { + return TSDB_CODE_SUCCESS; + } + + if (0 == strcmp(pCol->colName, "db_name")) { + return tNameSetDbName(pName, acctId, pVal->literal, strlen(pVal->literal)); + } else if (0 == strcmp(pCol->colName, "table_name")) { + return tNameAddTbName(pName, pVal->literal, strlen(pVal->literal)); + } + + return TSDB_CODE_SUCCESS; +} + +static void getInsTagsTableTargetObjName(int32_t acctId, SNode* pNode, SName* pName) { + if (QUERY_NODE_OPERATOR == nodeType(pNode)) { + getInsTagsTableTargetNameFromOp(acctId, (SOperatorNode*)pNode, pName); + } +} + +static int32_t getInsTagsTableTargetNameFromCond(int32_t acctId, SLogicConditionNode* pCond, SName* pName) { + if (LOGIC_COND_TYPE_AND != pCond->condType) { + return TSDB_CODE_SUCCESS; + } + + SNode* pNode = NULL; + FOREACH(pNode, pCond->pParameterList) { getInsTagsTableTargetObjName(acctId, pNode, pName); } + if ('\0' == pName->dbname[0]) { + pName->type = 0; + } + return TSDB_CODE_SUCCESS; +} + +int32_t getInsTagsTableTargetName(int32_t acctId, SNode* pWhere, SName* pName) { + if (NULL == pWhere) { + return TSDB_CODE_SUCCESS; + } + + if (QUERY_NODE_OPERATOR == nodeType(pWhere)) { + return getInsTagsTableTargetNameFromOp(acctId, (SOperatorNode*)pWhere, pName); + } + + if (QUERY_NODE_LOGIC_CONDITION == nodeType(pWhere)) { + return getInsTagsTableTargetNameFromCond(acctId, (SLogicConditionNode*)pWhere, pName); + } + + return TSDB_CODE_SUCCESS; +} + static int32_t userAuthToString(int32_t acctId, const char* pUser, const char* pDb, AUTH_TYPE type, char* pStr) { return sprintf(pStr, "%s*%d.%s*%d", pUser, acctId, pDb, type); } @@ -1173,7 +1238,7 @@ void destoryParseMetaCache(SParseMetaCache* pMetaCache, bool request) { taosArrayDestroy(p->pTableVgroupReq); p = taosHashIterate(pMetaCache->pInsertTables, p); - } + } taosHashCleanup(pMetaCache->pInsertTables); taosHashCleanup(pMetaCache->pDbVgroup); taosHashCleanup(pMetaCache->pDbCfg); diff --git a/source/libs/parser/test/parSelectTest.cpp b/source/libs/parser/test/parSelectTest.cpp index 79a7038401..0027ac9ca1 100644 --- a/source/libs/parser/test/parSelectTest.cpp +++ b/source/libs/parser/test/parSelectTest.cpp @@ -420,9 +420,11 @@ TEST_F(ParserSelectTest, setOperatorSemanticCheck) { } TEST_F(ParserSelectTest, informationSchema) { - useDb("root", "test"); + useDb("root", "information_schema"); - run("SELECT * FROM information_schema.ins_databases WHERE name = 'information_schema'"); + run("SELECT * FROM ins_databases WHERE name = 'information_schema'"); + + run("SELECT * FROM ins_tags WHERE db_name = 'test' and table_name = 'st1'"); } TEST_F(ParserSelectTest, withoutFrom) { diff --git a/source/libs/parser/test/parTestMain.cpp b/source/libs/parser/test/parTestMain.cpp index 9e67249238..de2ce55459 100644 --- a/source/libs/parser/test/parTestMain.cpp +++ b/source/libs/parser/test/parTestMain.cpp @@ -53,7 +53,7 @@ class ParserEnv : public testing::Environment { private: void initLog(const char* path) { - int32_t logLevel = getLogLevel(); + int32_t logLevel = getLogLevel() | DEBUG_SCREEN; dDebugFlag = logLevel; vDebugFlag = logLevel; mDebugFlag = logLevel; diff --git a/source/libs/planner/test/planTestMain.cpp b/source/libs/planner/test/planTestMain.cpp index df6e72ce46..4e013c44b8 100644 --- a/source/libs/planner/test/planTestMain.cpp +++ b/source/libs/planner/test/planTestMain.cpp @@ -48,7 +48,7 @@ class PlannerEnv : public testing::Environment { private: void initLog(const char* path) { - int32_t logLevel = getLogLevel(); + int32_t logLevel = getLogLevel() | DEBUG_SCREEN; dDebugFlag = logLevel; vDebugFlag = logLevel; mDebugFlag = logLevel;