From 53928fffd9256bdbf4a1a3e3f02b16bee78a484e Mon Sep 17 00:00:00 2001 From: Xiaoyu Wang Date: Thu, 23 Dec 2021 03:27:59 -0500 Subject: [PATCH] TD-12450 ut of combination of insert parser and insert planner --- include/libs/parser/parser.h | 1 - source/libs/parser/src/insertParser.c | 1 + source/libs/parser/test/mockCatalog.cpp | 8 +-- .../libs/parser/test/mockCatalogService.cpp | 14 ++++- source/libs/planner/test/phyPlanTests.cpp | 59 ++++++++++++++++--- 5 files changed, 68 insertions(+), 15 deletions(-) diff --git a/include/libs/parser/parser.h b/include/libs/parser/parser.h index 03750a16a9..6fedb6b66b 100644 --- a/include/libs/parser/parser.h +++ b/include/libs/parser/parser.h @@ -35,7 +35,6 @@ typedef struct SParseContext { void *pRpc; struct SCatalog *pCatalog; const SEpSet *pEpSet; - int64_t id; // query id, generated by uuid generator int8_t schemaAttached; // denote if submit block is built with table schema or not const char *pSql; // sql string size_t sqlLen; // length of the sql string diff --git a/source/libs/parser/src/insertParser.c b/source/libs/parser/src/insertParser.c index 68aa755483..fb7790452d 100644 --- a/source/libs/parser/src/insertParser.c +++ b/source/libs/parser/src/insertParser.c @@ -909,6 +909,7 @@ int32_t parseInsertSql(SParseContext* pContext, SInsertStmtInfo** pInfo) { } *pInfo = context.pOutput; + context.pOutput->nodeType = TSDB_SQL_INSERT; context.pOutput->schemaAttache = pContext->schemaAttached; context.pOutput->payloadType = PAYLOAD_TYPE_KV; diff --git a/source/libs/parser/test/mockCatalog.cpp b/source/libs/parser/test/mockCatalog.cpp index 6d89733668..d7f410a01e 100644 --- a/source/libs/parser/test/mockCatalog.cpp +++ b/source/libs/parser/test/mockCatalog.cpp @@ -23,20 +23,20 @@ namespace { void generateTestT1(MockCatalogService* mcs) { - ITableBuilder& builder = mcs->createTableBuilder("root.test", "t1", TSDB_NORMAL_TABLE, 3) + ITableBuilder& builder = mcs->createTableBuilder("test", "t1", TSDB_NORMAL_TABLE, 3) .setPrecision(TSDB_TIME_PRECISION_MILLI).setVgid(1).addColumn("ts", TSDB_DATA_TYPE_TIMESTAMP) .addColumn("c1", TSDB_DATA_TYPE_INT).addColumn("c2", TSDB_DATA_TYPE_BINARY, 20); builder.done(); } void generateTestST1(MockCatalogService* mcs) { - ITableBuilder& builder = mcs->createTableBuilder("root.test", "st1", TSDB_SUPER_TABLE, 3, 2) + ITableBuilder& builder = mcs->createTableBuilder("test", "st1", TSDB_SUPER_TABLE, 3, 2) .setPrecision(TSDB_TIME_PRECISION_MILLI).addColumn("ts", TSDB_DATA_TYPE_TIMESTAMP) .addTag("tag1", TSDB_DATA_TYPE_INT).addTag("tag2", TSDB_DATA_TYPE_BINARY, 20) .addColumn("c1", TSDB_DATA_TYPE_INT).addColumn("c2", TSDB_DATA_TYPE_BINARY, 20); builder.done(); - mcs->createSubTable("root.test", "st1", "st1s1", 1); - mcs->createSubTable("root.test", "st1", "st1s2", 2); + mcs->createSubTable("test", "st1", "st1s1", 1); + mcs->createSubTable("test", "st1", "st1s2", 2); } } diff --git a/source/libs/parser/test/mockCatalogService.cpp b/source/libs/parser/test/mockCatalogService.cpp index e234f82da9..520ef3a89b 100644 --- a/source/libs/parser/test/mockCatalogService.cpp +++ b/source/libs/parser/test/mockCatalogService.cpp @@ -94,9 +94,9 @@ public: return 0; } - int32_t catalogGetTableMeta(const char* pDBName, const char* pTableName, STableMeta** pTableMeta) const { + int32_t catalogGetTableMeta(const char* pDbFullName, const char* pTableName, STableMeta** pTableMeta) const { std::unique_ptr table; - int32_t code = copyTableSchemaMeta(pDBName, pTableName, &table); + int32_t code = copyTableSchemaMeta(toDbname(pDbFullName), pTableName, &table); if (TSDB_CODE_SUCCESS != code) { return code; } @@ -104,7 +104,7 @@ public: return TSDB_CODE_SUCCESS; } - int32_t catalogGetTableHashVgroup(const char* pDBName, const char* pTableName, SVgroupInfo* vgInfo) const { + int32_t catalogGetTableHashVgroup(const char* pDbFullName, const char* pTableName, SVgroupInfo* vgInfo) const { // todo return 0; } @@ -195,6 +195,14 @@ private: typedef std::map> TableMetaCache; typedef std::map DbMetaCache; + std::string toDbname(const std::string& dbFullName) const { + std::string::size_type n = dbFullName.find("."); + if (n == std::string::npos) { + return dbFullName; + } + return dbFullName.substr(n + 1); + } + std::string ttToString(int8_t tableType) const { switch (tableType) { case TSDB_SUPER_TABLE: diff --git a/source/libs/planner/test/phyPlanTests.cpp b/source/libs/planner/test/phyPlanTests.cpp index ddd1151742..cafa22df24 100644 --- a/source/libs/planner/test/phyPlanTests.cpp +++ b/source/libs/planner/test/phyPlanTests.cpp @@ -33,10 +33,6 @@ protected: void pushScan(const string& db, const string& table, int32_t scanOp) { shared_ptr meta = mockCatalogService->getTableMeta(db, table); EXPECT_TRUE(meta); -// typedef struct SQueryPlanNode { -// SArray *pExpr; // the query functions or sql aggregations -// int32_t numOfExpr; // number of result columns, which is also the number of pExprs -// } SQueryPlanNode; unique_ptr scan((SQueryPlanNode*)calloc(1, sizeof(SQueryPlanNode))); scan->info.type = scanOp; scan->numOfCols = meta->schema->tableInfo.numOfColumns; @@ -54,6 +50,27 @@ protected: return code; } + int32_t run(const string& db, const string& sql) { + int32_t code = TSDB_CODE_SUCCESS; + SParseContext cxt; + buildParseContext(db, sql, &cxt); + SQueryNode* query; + if (qIsInsertSql(cxt.pSql, cxt.sqlLen)) { + code = qParseInsertSql(&cxt, (SInsertStmtInfo**)&query); + } else { + // todo + code = TSDB_CODE_FAILED; + } + if (TSDB_CODE_SUCCESS != code) { + cout << "error no:" << code << ", msg:" << cxt.pMsg << endl; + return code; + } + SQueryDag* dag = nullptr; + code = qCreateQueryDag(query, nullptr, &dag); + dag_.reset(dag); + return code; + } + void explain() { size_t level = taosArrayGetSize(dag_->pSubplans); for (size_t i = 0; i < level; ++i) { @@ -64,7 +81,8 @@ protected: std::cout << "no " << j << ":" << std::endl; int32_t len = 0; char* str = nullptr; - ASSERT_EQ (TSDB_CODE_SUCCESS, qSubPlanToString((const SSubplan*)taosArrayGetP(subplans, j), &str, &len)); + ASSERT_EQ(TSDB_CODE_SUCCESS, qSubPlanToString((const SSubplan*)taosArrayGetP(subplans, j), &str, &len)); + std::cout << "len:" << len << std::endl; std::cout << str << std::endl; free(str); } @@ -108,6 +126,25 @@ private: return info; } + void buildParseContext(const string& db, const string& sql, SParseContext* pCxt) { + static string _db; + static string _sql; + static const int32_t _msgMaxLen = 4096; + static char _msg[_msgMaxLen]; + + _db = db; + _sql = sql; + memset(_msg, 0, _msgMaxLen); + + pCxt->ctx.acctId = 1; + pCxt->ctx.db = _db.c_str(); + pCxt->ctx.requestId = 1; + pCxt->pSql = _sql.c_str(); + pCxt->sqlLen = _sql.length(); + pCxt->pMsg = _msg; + pCxt->msgLen = _msgMaxLen; + } + shared_ptr meta_; unique_ptr logicPlan_; unique_ptr dag_; @@ -115,7 +152,7 @@ private: // select * from table TEST_F(PhyPlanTest, tableScanTest) { - pushScan("root.test", "t1", QNODE_TABLESCAN); + pushScan("test", "t1", QNODE_TABLESCAN); ASSERT_EQ(run(), TSDB_CODE_SUCCESS); explain(); SQueryDag* dag = reslut(); @@ -124,9 +161,17 @@ TEST_F(PhyPlanTest, tableScanTest) { // select * from supertable TEST_F(PhyPlanTest, superTableScanTest) { - pushScan("root.test", "st1", QNODE_TABLESCAN); + pushScan("test", "st1", QNODE_TABLESCAN); ASSERT_EQ(run(), TSDB_CODE_SUCCESS); explain(); SQueryDag* dag = reslut(); // todo check } + +// insert into t values(...) +TEST_F(PhyPlanTest, insertTest) { + ASSERT_EQ(run("test", "insert into t1 values (now, 1, \"beijing\")"), TSDB_CODE_SUCCESS); + explain(); + SQueryDag* dag = reslut(); + // todo check +} -- GitLab