diff --git a/source/libs/parser/inc/parUtil.h b/source/libs/parser/inc/parUtil.h index 4d7a8e2a18b280e4f1882a0c96838c36d3f00959..171b406e1853f3d606adb5aebdaf51740594042a 100644 --- a/source/libs/parser/inc/parUtil.h +++ b/source/libs/parser/inc/parUtil.h @@ -23,6 +23,13 @@ extern "C" { #include "os.h" #include "query.h" +#define parserFatal(param, ...) qFatal("PARSER: " param, __VA_ARGS__) +#define parserError(param, ...) qError("PARSER: " param, __VA_ARGS__) +#define parserWarn(param, ...) qWarn("PARSER: " param, __VA_ARGS__) +#define parserInfo(param, ...) qInfo("PARSER: " param, __VA_ARGS__) +#define parserDebug(param, ...) qDebug("PARSER: " param, __VA_ARGS__) +#define parserTrace(param, ...) qTrace("PARSER: " param, __VA_ARGS__) + typedef struct SMsgBuf { int32_t len; char *buf; diff --git a/source/libs/parser/src/parInsert.c b/source/libs/parser/src/parInsert.c index 43cc30848365c11322a4495ab14291e2e01bf453..4e23901b85f12b8ddbd51965df9aa533fdcf2bc8 100644 --- a/source/libs/parser/src/parInsert.c +++ b/source/libs/parser/src/parInsert.c @@ -1024,7 +1024,6 @@ int32_t parseInsertSql(SParseContext* pContext, SQuery** pQuery) { }; if (NULL == context.pVgroupsHashObj || NULL == context.pTableBlockHashObj || NULL == context.pOutput) { - terrno = TSDB_CODE_TSC_OUT_OF_MEMORY; return TSDB_CODE_TSC_OUT_OF_MEMORY; } @@ -1043,6 +1042,5 @@ int32_t parseInsertSql(SParseContext* pContext, SQuery** pQuery) { code = parseInsertBody(&context); } destroyInsertParseContext(&context); - terrno = code; return code; } diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index 589dc5bf26ee189b92eca5e412dbd30019203e6c..a0c545c24fbb32e4f568f543601288ebd01b33f8 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -69,14 +69,78 @@ static int32_t addNamespace(STranslateContext* pCxt, void* pTable) { return TSDB_CODE_SUCCESS; } -static SName* toName(int32_t acctId, const SRealTableNode* pRealTable, SName* pName) { +static SName* toName(int32_t acctId, const char* pDbName, const char* pTableName, SName* pName) { pName->type = TSDB_TABLE_NAME_T; pName->acctId = acctId; - strcpy(pName->dbname, pRealTable->table.dbName); - strcpy(pName->tname, pRealTable->table.tableName); + strcpy(pName->dbname, pDbName); + strcpy(pName->tname, pTableName); return pName; } +static int32_t getTableMetaImpl(SParseContext* pCxt, const SName* pName, STableMeta** pMeta) { + int32_t code = catalogGetTableMeta(pCxt->pCatalog, pCxt->pTransporter, &pCxt->mgmtEpSet, pName, pMeta); + if (TSDB_CODE_SUCCESS != code) { + parserError("catalogGetTableMeta error, code:%s, dbName:%s, tbName:%s", tstrerror(code), pName->dbname, pName->tname); + } + return code; +} + +static int32_t getTableMeta(SParseContext* pCxt, const char* pDbName, const char* pTableName, STableMeta** pMeta) { + SName name = { .type = TSDB_TABLE_NAME_T, .acctId = pCxt->acctId }; + strcpy(name.dbname, pDbName); + strcpy(name.tname, pTableName); + return getTableMetaImpl(pCxt, &name, pMeta); +} + +static int32_t getTableDistVgInfo(SParseContext* pCxt, const SName* pName, SArray** pVgInfo) { + int32_t code = catalogGetTableDistVgInfo(pCxt->pCatalog, pCxt->pTransporter, &pCxt->mgmtEpSet, pName, pVgInfo); + if (TSDB_CODE_SUCCESS != code) { + parserError("catalogGetTableDistVgInfo error, code:%s, dbName:%s, tbName:%s", tstrerror(code), pName->dbname, pName->tname); + } + return code; +} + +static int32_t getDBVgInfoImpl(SParseContext* pCxt, const SName* pName, SArray** pVgInfo) { + char fullDbName[TSDB_DB_FNAME_LEN]; + tNameGetFullDbName(pName, fullDbName); + int32_t code = catalogGetDBVgInfo(pCxt->pCatalog, pCxt->pTransporter, &pCxt->mgmtEpSet, fullDbName, pVgInfo); + if (TSDB_CODE_SUCCESS != code) { + parserError("catalogGetDBVgInfo error, code:%s, dbFName:%s", tstrerror(code), fullDbName); + } + return code; +} + +static int32_t getDBVgInfo(SParseContext* pCxt, const char* pDbName, SArray** pVgInfo) { + SName name; + tNameSetDbName(&name, pCxt->acctId, pDbName, strlen(pDbName)); + char dbFname[TSDB_DB_FNAME_LEN] = {0}; + tNameGetFullDbName(&name, dbFname); + return getDBVgInfoImpl(pCxt, &name, pVgInfo); +} + +static int32_t getTableHashVgroupImpl(SParseContext* pCxt, const SName* pName, SVgroupInfo* pInfo) { + int32_t code = catalogGetTableHashVgroup(pCxt->pCatalog, pCxt->pTransporter, &pCxt->mgmtEpSet, pName, pInfo); + if (TSDB_CODE_SUCCESS != code) { + parserError("catalogGetTableHashVgroup error, code:%s, dbName:%s, tbName:%s", tstrerror(code), pName->dbname, pName->tname); + } + return code; +} + +static int32_t getTableHashVgroup(SParseContext* pCxt, const char* pDbName, const char* pTableName, SVgroupInfo* pInfo) { + SName name = { .type = TSDB_TABLE_NAME_T, .acctId = pCxt->acctId }; + strcpy(name.dbname, pDbName); + strcpy(name.tname, pTableName); + return getTableHashVgroupImpl(pCxt, &name, pInfo); +} + +static int32_t getDBVgVersion(SParseContext* pCxt, const char* pDbFName, int32_t* pVersion, int64_t* pDbId, int32_t* pTableNum) { + int32_t code = catalogGetDBVgVersion(pCxt->pCatalog, pDbFName, pVersion, pDbId, pTableNum); + if (TSDB_CODE_SUCCESS != code) { + parserError("catalogGetDBVgVersion error, code:%s, dbFName:%s", tstrerror(code), pDbFName); + } + return code; +} + static bool belongTable(const char* currentDb, const SColumnNode* pCol, const STableNode* pTable) { int cmp = 0; if ('\0' != pCol->dbName[0]) { @@ -517,17 +581,14 @@ static int32_t setTableVgroupList(SParseContext* pCxt, SName* pName, SRealTableN int32_t code = TSDB_CODE_SUCCESS; if (TSDB_SUPER_TABLE == pRealTable->pMeta->tableType) { SArray* vgroupList = NULL; - code = catalogGetTableDistVgInfo(pCxt->pCatalog, pCxt->pTransporter, &pCxt->mgmtEpSet, pName, &vgroupList); + code = getTableDistVgInfo(pCxt, pName, &vgroupList); if (TSDB_CODE_SUCCESS == code) { code = toVgroupsInfo(vgroupList, &pRealTable->pVgroupList); } taosArrayDestroy(vgroupList); } else if (TSDB_SYSTEM_TABLE == pRealTable->pMeta->tableType) { SArray* vgroupList = NULL; - char fullDbName[TSDB_DB_FNAME_LEN]; - // tNameGetFullDbName(pName, fullDbName); - snprintf(fullDbName, TSDB_DB_FNAME_LEN, "%d.%s", pCxt->acctId, "test"); - code = catalogGetDBVgInfo(pCxt->pCatalog, pCxt->pTransporter, &pCxt->mgmtEpSet, fullDbName, false, &vgroupList); + code = getDBVgInfoImpl(pCxt, pName, &vgroupList); if (TSDB_CODE_SUCCESS == code) { code = toVgroupsInfo(vgroupList, &pRealTable->pVgroupList); } @@ -538,7 +599,7 @@ static int32_t setTableVgroupList(SParseContext* pCxt, SName* pName, SRealTableN return TSDB_CODE_OUT_OF_MEMORY; } pRealTable->pVgroupList->numOfVgroups = 1; - code = catalogGetTableHashVgroup(pCxt->pCatalog, pCxt->pTransporter, &pCxt->mgmtEpSet, pName, pRealTable->pVgroupList->vgroups); + code = getTableHashVgroupImpl(pCxt, pName, pRealTable->pVgroupList->vgroups); } return code; } @@ -549,8 +610,8 @@ static int32_t translateTable(STranslateContext* pCxt, SNode* pTable) { case QUERY_NODE_REAL_TABLE: { SRealTableNode* pRealTable = (SRealTableNode*)pTable; SName name; - code = catalogGetTableMeta(pCxt->pParseCxt->pCatalog, pCxt->pParseCxt->pTransporter, &(pCxt->pParseCxt->mgmtEpSet), - toName(pCxt->pParseCxt->acctId, pRealTable, &name), &(pRealTable->pMeta)); + code = getTableMetaImpl(pCxt->pParseCxt, + toName(pCxt->pParseCxt->acctId, pRealTable->table.dbName, pRealTable->table.tableName, &name), &(pRealTable->pMeta)); if (TSDB_CODE_SUCCESS != code) { return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_PAR_TABLE_NOT_EXIST, pRealTable->table.tableName); } @@ -906,11 +967,10 @@ static int32_t doTranslateDropSuperTable(STranslateContext* pCxt, const SName* p static int32_t translateDropTable(STranslateContext* pCxt, SDropTableStmt* pStmt) { SDropTableClause* pClause = nodesListGetNode(pStmt->pTables, 0); - SName tableName = { .type = TSDB_TABLE_NAME_T, .acctId = pCxt->pParseCxt->acctId }; - strcpy(tableName.dbname, pClause->dbName); - strcpy(tableName.tname, pClause->tableName); STableMeta* pTableMeta = NULL; - int32_t code = catalogGetTableMeta(pCxt->pParseCxt->pCatalog, pCxt->pParseCxt->pTransporter, &(pCxt->pParseCxt->mgmtEpSet), &tableName, &pTableMeta); + SName tableName; + int32_t code = getTableMetaImpl( + pCxt->pParseCxt, toName(pCxt->pParseCxt->acctId, pClause->dbName, pClause->tableName, &tableName), &pTableMeta); if (TSDB_CODE_SUCCESS == code) { if (TSDB_SUPER_TABLE == pTableMeta->tableType) { code = doTranslateDropSuperTable(pCxt, &tableName, pClause->ignoreNotExists); @@ -918,8 +978,8 @@ static int32_t translateDropTable(STranslateContext* pCxt, SDropTableStmt* pStmt // todo : drop normal table or child table code = TSDB_CODE_FAILED; } + tfree(pTableMeta); } - tfree(pTableMeta); return code; } @@ -932,13 +992,14 @@ static int32_t translateDropSuperTable(STranslateContext* pCxt, SDropSuperTableS } static int32_t translateUseDatabase(STranslateContext* pCxt, SUseDatabaseStmt* pStmt) { + SUseDbReq usedbReq = {0}; SName name = {0}; tNameSetDbName(&name, pCxt->pParseCxt->acctId, pStmt->dbName, strlen(pStmt->dbName)); - - SUseDbReq usedbReq = {0}; tNameExtractFullName(&name, usedbReq.db); - - catalogGetDBVgVersion(pCxt->pParseCxt->pCatalog, usedbReq.db, &usedbReq.vgVersion, &usedbReq.dbId, &usedbReq.numOfTable); + int32_t code = getDBVgVersion(pCxt->pParseCxt, usedbReq.db, &usedbReq.vgVersion, &usedbReq.dbId, &usedbReq.numOfTable); + if (TSDB_CODE_SUCCESS != code) { + return code; + } pCxt->pCmdMsg = malloc(sizeof(SCmdMsgInfo)); if (NULL== pCxt->pCmdMsg) { @@ -1114,19 +1175,14 @@ static int32_t translateShow(STranslateContext* pCxt, SShowStmt* pStmt) { } static int32_t translateShowTables(STranslateContext* pCxt) { - SName name = {0}; SVShowTablesReq* pShowReq = calloc(1, sizeof(SVShowTablesReq)); if (pCxt->pParseCxt->db == NULL || strlen(pCxt->pParseCxt->db) == 0) { return generateSyntaxErrMsg(&pCxt->msgBuf, TSDB_CODE_TSC_INVALID_OPERATION, "db not specified"); } - tNameSetDbName(&name, pCxt->pParseCxt->acctId, pCxt->pParseCxt->db, strlen(pCxt->pParseCxt->db)); - char dbFname[TSDB_DB_FNAME_LEN] = {0}; - tNameGetFullDbName(&name, dbFname); - SArray* array = NULL; - int32_t code = catalogGetDBVgInfo(pCxt->pParseCxt->pCatalog, pCxt->pParseCxt->pTransporter, &pCxt->pParseCxt->mgmtEpSet, dbFname, &array); - if (code != TSDB_CODE_SUCCESS) { + int32_t code = getDBVgInfo(pCxt->pParseCxt, pCxt->pParseCxt->db, &array); + if (TSDB_CODE_SUCCESS != code) { return code; } SVgroupInfo* info = taosArrayGet(array, 0); @@ -1353,13 +1409,6 @@ static void destroyCreateTbReqBatch(SVgroupTablesBatch* pTbBatch) { taosArrayDestroy(pTbBatch->req.pArray); } -static int32_t getTableHashVgroup(SParseContext* pCxt, const char* pDbName, const char* pTableName, SVgroupInfo* pInfo) { - SName name = { .type = TSDB_TABLE_NAME_T, .acctId = pCxt->acctId }; - strcpy(name.dbname, pDbName); - strcpy(name.tname, pTableName); - return catalogGetTableHashVgroup(pCxt->pCatalog, pCxt->pTransporter, &pCxt->mgmtEpSet, &name, pInfo); -} - static int32_t rewriteToVnodeModifOpStmt(SQuery* pQuery, SArray* pBufArray) { SVnodeModifOpStmt* pNewStmt = nodesMakeNode(QUERY_NODE_VNODE_MODIF_STMT); if (pNewStmt == NULL) { @@ -1544,12 +1593,9 @@ static int32_t buildKVRowForAllTags(STranslateContext* pCxt, SCreateSubTableClau } static int32_t rewriteCreateSubTable(STranslateContext* pCxt, SCreateSubTableClause* pStmt, SHashObj* pVgroupHashmap) { - SName name = { .type = TSDB_TABLE_NAME_T, .acctId = pCxt->pParseCxt->acctId }; - strcpy(name.dbname, pStmt->useDbName); - strcpy(name.tname, pStmt->useTableName); STableMeta* pSuperTableMeta = NULL; - int32_t code = catalogGetTableMeta(pCxt->pParseCxt->pCatalog, pCxt->pParseCxt->pTransporter, &pCxt->pParseCxt->mgmtEpSet, &name, &pSuperTableMeta); - + int32_t code = getTableMeta(pCxt->pParseCxt, pStmt->useDbName, pStmt->useTableName, &pSuperTableMeta); + SKVRowBuilder kvRowBuilder = {0}; if (TSDB_CODE_SUCCESS == code) { code = tdInitKVRowBuilder(&kvRowBuilder); diff --git a/source/libs/parser/src/parser.c b/source/libs/parser/src/parser.c index 868bd755208ed1ac0cdafe3774f4d979dc3d197a..08f316b539e02dec5416f2aa0cc3e63f45c0ac3d 100644 --- a/source/libs/parser/src/parser.c +++ b/source/libs/parser/src/parser.c @@ -38,11 +38,14 @@ static int32_t parseSqlIntoAst(SParseContext* pCxt, SQuery** pQuery) { } int32_t qParseQuerySql(SParseContext* pCxt, SQuery** pQuery) { + int32_t code = TSDB_CODE_SUCCESS; if (isInsertSql(pCxt->pSql, pCxt->sqlLen)) { - return parseInsertSql(pCxt, pQuery); + code = parseInsertSql(pCxt, pQuery); } else { - return parseSqlIntoAst(pCxt, pQuery); + code = parseSqlIntoAst(pCxt, pQuery); } + terrno = code; + return code; } void qDestroyQuery(SQuery* pQueryNode) {