From eafbe63683c59ae85cf52bbbf145fcf2959839aa Mon Sep 17 00:00:00 2001 From: "pengrongkun94@qq.com" Date: Mon, 30 Dec 2024 11:14:05 +0800 Subject: [PATCH 1/4] add case --- source/client/test/stmt2Test.cpp | 160 ++++++++++++++++++++++++++++--- 1 file changed, 146 insertions(+), 14 deletions(-) diff --git a/source/client/test/stmt2Test.cpp b/source/client/test/stmt2Test.cpp index 4fea2452d07..b31ed9b2e4d 100644 --- a/source/client/test/stmt2Test.cpp +++ b/source/client/test/stmt2Test.cpp @@ -112,15 +112,14 @@ void do_query(TAOS* taos, const char* sql) { taos_free_result(result); } -void do_stmt(TAOS* taos, const char* sql, int CTB_NUMS, int ROW_NUMS, int CYC_NUMS, bool createTable) { +void do_stmt(TAOS* taos, TAOS_STMT2_OPTION* option, const char* sql, int CTB_NUMS, int ROW_NUMS, int CYC_NUMS, + bool hastags, bool createTable) { do_query(taos, "drop database if exists db"); do_query(taos, "create database db"); do_query(taos, "create table db.stb (ts timestamp, b binary(10)) tags(t1 int, t2 binary(10))"); do_query(taos, "use db"); - TAOS_STMT2_OPTION option = {0, true, true, NULL, NULL}; - - TAOS_STMT2* stmt = taos_stmt2_init(taos, &option); + TAOS_STMT2* stmt = taos_stmt2_init(taos, option); ASSERT_NE(stmt, nullptr); int code = taos_stmt2_prepare(stmt, sql, 0); ASSERT_EQ(code, 0); @@ -163,13 +162,18 @@ void do_stmt(TAOS* taos, const char* sql, int CTB_NUMS, int ROW_NUMS, int CYC_NU // bind params TAOS_STMT2_BIND** paramv = (TAOS_STMT2_BIND**)taosMemoryMalloc(CTB_NUMS * sizeof(TAOS_STMT2_BIND*)); - TAOS_STMT2_BIND** tags = (TAOS_STMT2_BIND**)taosMemoryMalloc(CTB_NUMS * sizeof(TAOS_STMT2_BIND*)); - for (int i = 0; i < CTB_NUMS; i++) { - // create tags - tags[i] = (TAOS_STMT2_BIND*)taosMemoryMalloc(2 * sizeof(TAOS_STMT2_BIND)); - tags[i][0] = {TSDB_DATA_TYPE_INT, &t1, &t1len, NULL, 0}; - tags[i][1] = {TSDB_DATA_TYPE_BINARY, (void*)"after", &t2len, NULL, 0}; + TAOS_STMT2_BIND** tags = NULL; + if (hastags) { + tags = (TAOS_STMT2_BIND**)taosMemoryMalloc(CTB_NUMS * sizeof(TAOS_STMT2_BIND*)); + for (int i = 0; i < CTB_NUMS; i++) { + // create tags + tags[i] = (TAOS_STMT2_BIND*)taosMemoryMalloc(2 * sizeof(TAOS_STMT2_BIND)); + tags[i][0] = {TSDB_DATA_TYPE_INT, &t1, &t1len, NULL, 0}; + tags[i][1] = {TSDB_DATA_TYPE_BINARY, (void*)"after", &t2len, NULL, 0}; + } + } + for (int i = 0; i < CTB_NUMS; i++) { // create col params paramv[i] = (TAOS_STMT2_BIND*)taosMemoryMalloc(2 * sizeof(TAOS_STMT2_BIND)); paramv[i][0] = {TSDB_DATA_TYPE_TIMESTAMP, &ts[i][0], &ts_len[0], NULL, ROW_NUMS}; @@ -187,7 +191,9 @@ void do_stmt(TAOS* taos, const char* sql, int CTB_NUMS, int ROW_NUMS, int CYC_NU ASSERT_EQ(errno, 0); for (int i = 0; i < CTB_NUMS; i++) { - taosMemoryFree(tags[i]); + if (hastags) { + taosMemoryFree(tags[i]); + } taosMemoryFree(paramv[i]); taosMemoryFree(ts[i]); taosMemoryFree(b[i]); @@ -197,7 +203,9 @@ void do_stmt(TAOS* taos, const char* sql, int CTB_NUMS, int ROW_NUMS, int CYC_NU taosMemoryFree(ts_len); taosMemoryFree(b_len); taosMemoryFree(paramv); - taosMemoryFree(tags); + if (hastags) { + taosMemoryFree(tags); + } } checkRows(taos, "select * from db.stb", CYC_NUMS * ROW_NUMS * CTB_NUMS); @@ -814,11 +822,135 @@ TEST(stmt2Case, stmt2_init_prepare_Test) { taos_close(taos); } -TEST(stmt2Case, stmt2_all) { +TEST(stmt2Case, stmt2_insert) { + TAOS* taos = taos_connect("localhost", "root", "taosdata", "", 0); + ASSERT_NE(taos, nullptr); + TAOS_STMT2_OPTION option = {0, true, true, NULL, NULL}; + { do_stmt(taos, &option, "insert into db.stb (tbname,ts,b,t1,t2) values(?,?,?,?,?)", 3, 3, 3, true, true); } + option = {0, true, true, stmtAsyncQueryCb, NULL}; + { do_stmt(taos, &option, "insert into db.? using db.stb tags(?,?) values(?,?)", 3, 3, 3, true, true); } + option = {0, false, false, NULL, NULL}; + { do_stmt(taos, &option, "insert into stb (tbname,ts,b,t1,t2) values(?,?,?,?,?)", 3, 3, 3, true, false); } + option = {0, true, true, NULL, NULL}; + { do_stmt(taos, &option, "insert into db.stb (tbname,ts,b) values(?,?,?)", 3, 3, 3, false, true); } + + taos_close(taos); +} + +TEST(stmt2Case, stmt2_query) { TAOS* taos = taos_connect("localhost", "root", "taosdata", "", 0); ASSERT_NE(taos, nullptr); + do_query(taos, "drop database if exists db"); + do_query(taos, "create database db"); + do_query(taos, "create table db.stb (ts timestamp, b binary(10)) tags(t1 int, t2 binary(10))"); + do_query(taos, + "insert into db.tb1 using db.stb tags(1,'abc') values(1591060628000, " + "'abc'),(1591060628001,'def'),(1591060628002, 'hij')"); + do_query(taos, + "insert into db.tb2 using db.stb tags(2,'xyz') values(1591060628000, " + "'abc'),(1591060628001,'def'),(1591060628002, 'hij')"); + do_query(taos, "use db"); - { do_stmt(taos, "insert into db.stb (tbname,ts,b,t1,t2) values(?,?,?,?,?)", 3, 3, 3, true); } + TAOS_STMT2_OPTION option = {0, true, true, NULL, NULL}; + + TAOS_STMT2* stmt = taos_stmt2_init(taos, &option); + ASSERT_NE(stmt, nullptr); + + const char* sql = "select * from db.stb where ts = ?"; + int code = taos_stmt2_prepare(stmt, sql, 0); + ASSERT_EQ(code, 0); + ASSERT_EQ(terrno, 0); + + int t64_len[2] = {sizeof(int64_t), sizeof(int64_t)}; + int b_len[2] = {5, 5}; + int64_t ts = 1591060628000; + TAOS_STMT2_BIND params[2] = {{TSDB_DATA_TYPE_TIMESTAMP, &ts, t64_len, NULL, 1}, + {TSDB_DATA_TYPE_BINARY, (void*)"tb1", b_len, NULL, 1}}; + TAOS_STMT2_BIND* paramv = ¶ms[0]; + TAOS_STMT2_BINDV bindv = {1, NULL, NULL, ¶mv}; + taos_stmt2_bind_param(stmt, &bindv, -1); + ASSERT_EQ(code, 0); + ASSERT_EQ(errno, 0); + + taos_stmt2_exec(stmt, NULL); + ASSERT_EQ(code, 0); + ASSERT_EQ(errno, 0); + + taos_stmt2_close(stmt); + taos_close(taos); +} + +TEST(stmt2Case, stmt2_interlace) { + TAOS* taos = taos_connect("localhost", "root", "taosdata", "", 0); + ASSERT_NE(taos, nullptr); + TAOS_STMT2_OPTION option = {0, true, true, NULL, NULL}; + do_query(taos, "drop database if exists db1"); + do_query(taos, "create database db1"); + do_query(taos, "create table db1.ntb(ts timestamp, b binary(10))"); + do_query(taos, "use db1"); + TAOS_STMT2* stmt = taos_stmt2_init(taos, &option); + ASSERT_NE(stmt, nullptr); + + const char* sql = "insert into db1.ntb values(?,?)"; + int code = taos_stmt2_prepare(stmt, sql, 0); + ASSERT_EQ(code, 0); + ASSERT_EQ(terrno, 0); + + + for (int r = 0; r < 3; r++) { + // col params + int64_t** ts = (int64_t**)taosMemoryMalloc(3 * sizeof(int64_t*)); + char** b = (char**)taosMemoryMalloc(3 * sizeof(char*)); + int* ts_len = (int*)taosMemoryMalloc(3 * sizeof(int)); + int* b_len = (int*)taosMemoryMalloc(3 * sizeof(int)); + for (int i = 0; i < 3; i++) { + ts_len[i] = sizeof(int64_t); + b_len[i] = 1; + } + for (int i = 0; i < 3; i++) { + ts[i] = (int64_t*)taosMemoryMalloc(3 * sizeof(int64_t)); + b[i] = (char*)taosMemoryMalloc(3 * sizeof(char)); + for (int j = 0; j < 3; j++) { + ts[i][j] = 1591060628000 + r * 100000 + j; + b[i][j] = 'a' + j; + } + } + + // bind params + TAOS_STMT2_BIND** paramv = (TAOS_STMT2_BIND**)taosMemoryMalloc(3 * sizeof(TAOS_STMT2_BIND*)); + + for (int i = 0; i < 3; i++) { + // create col params + paramv[i] = (TAOS_STMT2_BIND*)taosMemoryMalloc(2 * sizeof(TAOS_STMT2_BIND)); + paramv[i][0] = {TSDB_DATA_TYPE_TIMESTAMP, &ts[i][0], &ts_len[0], NULL, 3}; + paramv[i][1] = {TSDB_DATA_TYPE_BINARY, &b[i][0], &b_len[0], NULL, 3}; + } + // bind + TAOS_STMT2_BINDV bindv = {1, NULL, NULL, paramv}; + code = taos_stmt2_bind_param(stmt, &bindv, -1); + ASSERT_EQ(code, 0); + ASSERT_EQ(errno, 0); + + // exec + code = taos_stmt2_exec(stmt, NULL); + ASSERT_EQ(code, 0); + ASSERT_EQ(errno, 0); + + for (int i = 0; i < 3; i++) { + taosMemoryFree(paramv[i]); + taosMemoryFree(ts[i]); + taosMemoryFree(b[i]); + } + taosMemoryFree(ts); + taosMemoryFree(b); + taosMemoryFree(ts_len); + taosMemoryFree(b_len); + taosMemoryFree(paramv); + } + + checkRows(taos, "select * from db1.ntb", 9); + + taos_stmt2_close(stmt); taos_close(taos); } From 8d3c0a8d912d117705efc7c777721ced8958135a Mon Sep 17 00:00:00 2001 From: "pengrongkun94@qq.com" Date: Mon, 30 Dec 2024 17:43:36 +0800 Subject: [PATCH 2/4] refactor stmt2 stb interlace mode --- source/client/src/clientStmt2.c | 208 ++++++++++++++++--------------- source/client/test/stmt2Test.cpp | 69 +++------- 2 files changed, 124 insertions(+), 153 deletions(-) diff --git a/source/client/src/clientStmt2.c b/source/client/src/clientStmt2.c index 820a7a01105..5992493e5ba 100644 --- a/source/client/src/clientStmt2.c +++ b/source/client/src/clientStmt2.c @@ -58,13 +58,13 @@ static bool stmtDequeue(STscStmt2* pStmt, SStmtQNode** param) { return true; } -static void stmtEnqueue(STscStmt2* pStmt, SStmtQNode* param) { - pStmt->queue.tail->next = param; - pStmt->queue.tail = param; +// static void stmtEnqueue(STscStmt2* pStmt, SStmtQNode* param) { +// pStmt->queue.tail->next = param; +// pStmt->queue.tail = param; - pStmt->stat.bindDataNum++; - (void)atomic_add_fetch_64((int64_t*)&pStmt->queue.qRemainNum, 1); -} +// pStmt->stat.bindDataNum++; +// (void)atomic_add_fetch_64((int64_t*)&pStmt->queue.qRemainNum, 1); +// } static int32_t stmtCreateRequest(STscStmt2* pStmt) { int32_t code = 0; @@ -76,7 +76,7 @@ static int32_t stmtCreateRequest(STscStmt2* pStmt) { pStmt->reqid++; } if (pStmt->db != NULL) { - taosMemoryFreeClear(pStmt->exec.pRequest->pDb); + taosMemoryFreeClear(pStmt->exec.pRequest->pDb); pStmt->exec.pRequest->pDb = taosStrdup(pStmt->db); } if (TSDB_CODE_SUCCESS == code) { @@ -325,10 +325,10 @@ static int32_t stmtCleanBindInfo(STscStmt2* pStmt) { return TSDB_CODE_SUCCESS; } -static void stmtFreeTableBlkList(STableColsData* pTb) { - (void)qResetStmtColumns(pTb->aCol, true); - taosArrayDestroy(pTb->aCol); -} +// static void stmtFreeTableBlkList(STableColsData* pTb) { +// (void)qResetStmtColumns(pTb->aCol, true); +// taosArrayDestroy(pTb->aCol); +// } static void stmtResetQueueTableBuf(STableBufInfo* pTblBuf, SStmtQueue* pQueue) { pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, 0); @@ -895,32 +895,32 @@ int stmtPrepare2(TAOS_STMT2* stmt, const char* sql, unsigned long length) { return TSDB_CODE_SUCCESS; } -static int32_t stmtInitStbInterlaceTableInfo(STscStmt2* pStmt) { - STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName)); - if (!pSrc) { - return terrno; - } - STableDataCxt* pDst = NULL; +// static int32_t stmtInitStbInterlaceTableInfo(STscStmt2* pStmt) { +// STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName)); +// if (!pSrc) { +// return terrno; +// } +// STableDataCxt* pDst = NULL; - STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true)); - pStmt->sql.siInfo.pDataCtx = pDst; +// STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true)); +// pStmt->sql.siInfo.pDataCtx = pDst; - SArray* pTblCols = NULL; - for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) { - pTblCols = taosArrayInit(20, POINTER_BYTES); - if (NULL == pTblCols) { - return terrno; - } +// SArray* pTblCols = NULL; +// for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) { +// pTblCols = taosArrayInit(20, POINTER_BYTES); +// if (NULL == pTblCols) { +// return terrno; +// } - if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) { - return terrno; - } - } +// if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) { +// return terrno; +// } +// } - pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags; +// pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags; - return TSDB_CODE_SUCCESS; -} +// return TSDB_CODE_SUCCESS; +// } int stmtIsInsert2(TAOS_STMT2* stmt, int* insert) { STscStmt2* pStmt = (STscStmt2*)stmt; @@ -979,7 +979,7 @@ int stmtSetTbName2(TAOS_STMT2* stmt, const char* tbName) { } if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) { - STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt)); + // STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt)); } int64_t startUs2 = taosGetTimestampUs(); @@ -1009,7 +1009,7 @@ int stmtSetTbTags2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* tags) { STMT_ERR_RET(stmtParseSql(pStmt)); } if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) { - STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt)); + // STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt)); } SBoundColInfo* tags_info = (SBoundColInfo*)pStmt->bInfo.boundTags; @@ -1037,33 +1037,33 @@ int stmtSetTbTags2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* tags) { return TSDB_CODE_SUCCESS; } -static int stmtFetchColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) { - if (pStmt->errCode != TSDB_CODE_SUCCESS) { - return pStmt->errCode; - } +// static int stmtFetchColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) { +// if (pStmt->errCode != TSDB_CODE_SUCCESS) { +// return pStmt->errCode; +// } - if (STMT_TYPE_QUERY == pStmt->sql.type) { - tscError("invalid operation to get query column fileds"); - STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR); - } +// if (STMT_TYPE_QUERY == pStmt->sql.type) { +// tscError("invalid operation to get query column fileds"); +// STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR); +// } - STableDataCxt** pDataBlock = NULL; +// STableDataCxt** pDataBlock = NULL; - if (pStmt->sql.stbInterlaceMode) { - pDataBlock = &pStmt->sql.siInfo.pDataCtx; - } else { - pDataBlock = - (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName)); - if (NULL == pDataBlock) { - tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName); - STMT_ERR_RET(TSDB_CODE_APP_ERROR); - } - } +// if (pStmt->sql.stbInterlaceMode) { +// pDataBlock = &pStmt->sql.siInfo.pDataCtx; +// } else { +// pDataBlock = +// (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName)); +// if (NULL == pDataBlock) { +// tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName); +// STMT_ERR_RET(TSDB_CODE_APP_ERROR); +// } +// } - STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields)); +// STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields)); - return TSDB_CODE_SUCCESS; -} +// return TSDB_CODE_SUCCESS; +// } static int stmtFetchStbColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_ALL** fields) { if (pStmt->errCode != TSDB_CODE_SUCCESS) { @@ -1117,46 +1117,46 @@ SArray* stmtGetFreeCol(STscStmt2* pStmt, int32_t* idx) { } } */ -static int32_t stmtAppendTablePostHandle(STscStmt2* pStmt, SStmtQNode* param) { - if (NULL == pStmt->sql.siInfo.pVgroupHash) { - pStmt->sql.siInfo.pVgroupHash = - taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK); - } - if (NULL == pStmt->sql.siInfo.pVgroupList) { - pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES); - } +// static int32_t stmtAppendTablePostHandle(STscStmt2* pStmt, SStmtQNode* param) { +// if (NULL == pStmt->sql.siInfo.pVgroupHash) { +// pStmt->sql.siInfo.pVgroupHash = +// taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK); +// } +// if (NULL == pStmt->sql.siInfo.pVgroupList) { +// pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES); +// } - if (NULL == pStmt->sql.siInfo.pRequest) { - STMT_ERR_RET(buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false, - (SRequestObj**)&pStmt->sql.siInfo.pRequest, pStmt->reqid)); +// if (NULL == pStmt->sql.siInfo.pRequest) { +// STMT_ERR_RET(buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false, +// (SRequestObj**)&pStmt->sql.siInfo.pRequest, pStmt->reqid)); - if (pStmt->reqid != 0) { - pStmt->reqid++; - } - pStmt->exec.pRequest->syncQuery = true; +// if (pStmt->reqid != 0) { +// pStmt->reqid++; +// } +// pStmt->exec.pRequest->syncQuery = true; - pStmt->sql.siInfo.requestId = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->requestId; - pStmt->sql.siInfo.requestSelf = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->self; - } +// pStmt->sql.siInfo.requestId = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->requestId; +// pStmt->sql.siInfo.requestSelf = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->self; +// } - if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] && - 0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) { - pStmt->sql.siInfo.tbFromHash = true; - } +// if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] && +// 0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) { +// pStmt->sql.siInfo.tbFromHash = true; +// } - if (0 == pStmt->sql.siInfo.firstName[0]) { - tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN); - } +// if (0 == pStmt->sql.siInfo.firstName[0]) { +// tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN); +// } - param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash; - param->next = NULL; +// param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash; +// param->next = NULL; - (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1); +// (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1); - stmtEnqueue(pStmt, param); +// stmtEnqueue(pStmt, param); - return TSDB_CODE_SUCCESS; -} +// return TSDB_CODE_SUCCESS; +// } static FORCE_INLINE int32_t stmtGetTableColsFromCache(STscStmt2* pStmt, SArray** pTableCols) { while (true) { @@ -1233,19 +1233,20 @@ static int stmtAddBatch2(TAOS_STMT2* stmt) { STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH)); if (pStmt->sql.stbInterlaceMode) { - int64_t startUs2 = taosGetTimestampUs(); - pStmt->stat.addBatchUs += startUs2 - startUs; + // todo:add stb interlace mode + // int64_t startUs2 = taosGetTimestampUs(); + // pStmt->stat.addBatchUs += startUs2 - startUs; - pStmt->sql.siInfo.tableColsReady = false; + // pStmt->sql.siInfo.tableColsReady = false; - SStmtQNode* param = NULL; - STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)¶m)); - param->restoreTbCols = true; - param->next = NULL; + // SStmtQNode* param = NULL; + // STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)¶m)); + // param->restoreTbCols = true; + // param->next = NULL; - stmtEnqueue(pStmt, param); + // stmtEnqueue(pStmt, param); - return TSDB_CODE_SUCCESS; + // return TSDB_CODE_SUCCESS; } STMT_ERR_RET(stmtCacheBlock(pStmt)); @@ -1366,7 +1367,7 @@ int stmtBindBatch2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* bind, int32_t colIdx) { } if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) { - STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt)); + // STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt)); } STableDataCxt** pDataBlock = NULL; @@ -1450,7 +1451,8 @@ int stmtBindBatch2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* bind, int32_t colIdx) { pStmt->stat.bindDataUs3 += startUs4 - startUs3; if (pStmt->sql.stbInterlaceMode) { - STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param)); + // to do + // STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param)); } else { STMT_ERR_RET(stmtAddBatch2(pStmt)); } @@ -1876,11 +1878,11 @@ int stmtGetParamNum2(TAOS_STMT2* stmt, int* nums) { STMT_ERR_RET(stmtParseSql(pStmt)); } - if (STMT_TYPE_QUERY == pStmt->sql.type) { - *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues); - } else { - STMT_ERR_RET(stmtFetchColFields2(stmt, nums, NULL)); - } + // if (STMT_TYPE_QUERY == pStmt->sql.type) { + *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues); + // } else { + // // STMT_ERR_RET(stmtFetchColFields2(stmt, nums, NULL)); + // } return TSDB_CODE_SUCCESS; } diff --git a/source/client/test/stmt2Test.cpp b/source/client/test/stmt2Test.cpp index b31ed9b2e4d..03eed54f77b 100644 --- a/source/client/test/stmt2Test.cpp +++ b/source/client/test/stmt2Test.cpp @@ -880,75 +880,44 @@ TEST(stmt2Case, stmt2_query) { taos_close(taos); } -TEST(stmt2Case, stmt2_interlace) { +TEST(stmt2Case, stmt2_ntb_insert) { TAOS* taos = taos_connect("localhost", "root", "taosdata", "", 0); ASSERT_NE(taos, nullptr); TAOS_STMT2_OPTION option = {0, true, true, NULL, NULL}; - do_query(taos, "drop database if exists db1"); - do_query(taos, "create database db1"); - do_query(taos, "create table db1.ntb(ts timestamp, b binary(10))"); - do_query(taos, "use db1"); + do_query(taos, "drop database if exists db"); + do_query(taos, "create database db"); + do_query(taos, "create table db.ntb(ts timestamp, b binary(10))"); + do_query(taos, "use db"); TAOS_STMT2* stmt = taos_stmt2_init(taos, &option); ASSERT_NE(stmt, nullptr); - const char* sql = "insert into db1.ntb values(?,?)"; + const char* sql = "insert into db.ntb values(?,?)"; int code = taos_stmt2_prepare(stmt, sql, 0); ASSERT_EQ(code, 0); ASSERT_EQ(terrno, 0); + for (int i = 0; i < 3; i++) { + int64_t ts[3] = {1591060628000 + i * 3, 1591060628001 + i * 3, 1591060628002 + i * 3}; + int t64_len[3] = {sizeof(int64_t), sizeof(int64_t), sizeof(int64_t)}; + int b_len[3] = {5,5,5}; + TAOS_STMT2_BIND params1 = {TSDB_DATA_TYPE_TIMESTAMP, &ts[0], &t64_len[0], NULL, 3}; + TAOS_STMT2_BIND params2 = {TSDB_DATA_TYPE_BINARY, (void*)"abcdefghijklmnopqrstuvwxyz", &b_len[0], NULL, 3}; + TAOS_STMT2_BIND* paramv1 = ¶ms1; + TAOS_STMT2_BIND* paramv2 = ¶ms2; - for (int r = 0; r < 3; r++) { - // col params - int64_t** ts = (int64_t**)taosMemoryMalloc(3 * sizeof(int64_t*)); - char** b = (char**)taosMemoryMalloc(3 * sizeof(char*)); - int* ts_len = (int*)taosMemoryMalloc(3 * sizeof(int)); - int* b_len = (int*)taosMemoryMalloc(3 * sizeof(int)); - for (int i = 0; i < 3; i++) { - ts_len[i] = sizeof(int64_t); - b_len[i] = 1; - } - for (int i = 0; i < 3; i++) { - ts[i] = (int64_t*)taosMemoryMalloc(3 * sizeof(int64_t)); - b[i] = (char*)taosMemoryMalloc(3 * sizeof(char)); - for (int j = 0; j < 3; j++) { - ts[i][j] = 1591060628000 + r * 100000 + j; - b[i][j] = 'a' + j; - } - } + TAOS_STMT2_BINDV bindv1 = {1, NULL, NULL, ¶mv1}; + TAOS_STMT2_BINDV bindv2 = {1, NULL, NULL, ¶mv2}; - // bind params - TAOS_STMT2_BIND** paramv = (TAOS_STMT2_BIND**)taosMemoryMalloc(3 * sizeof(TAOS_STMT2_BIND*)); - - for (int i = 0; i < 3; i++) { - // create col params - paramv[i] = (TAOS_STMT2_BIND*)taosMemoryMalloc(2 * sizeof(TAOS_STMT2_BIND)); - paramv[i][0] = {TSDB_DATA_TYPE_TIMESTAMP, &ts[i][0], &ts_len[0], NULL, 3}; - paramv[i][1] = {TSDB_DATA_TYPE_BINARY, &b[i][0], &b_len[0], NULL, 3}; - } - // bind - TAOS_STMT2_BINDV bindv = {1, NULL, NULL, paramv}; - code = taos_stmt2_bind_param(stmt, &bindv, -1); + taos_stmt2_bind_param(stmt, &bindv1, 0); + taos_stmt2_bind_param(stmt, &bindv2, 1); ASSERT_EQ(code, 0); ASSERT_EQ(errno, 0); - // exec code = taos_stmt2_exec(stmt, NULL); ASSERT_EQ(code, 0); ASSERT_EQ(errno, 0); - - for (int i = 0; i < 3; i++) { - taosMemoryFree(paramv[i]); - taosMemoryFree(ts[i]); - taosMemoryFree(b[i]); - } - taosMemoryFree(ts); - taosMemoryFree(b); - taosMemoryFree(ts_len); - taosMemoryFree(b_len); - taosMemoryFree(paramv); } - - checkRows(taos, "select * from db1.ntb", 9); + checkRows(taos, "select * from db.ntb", 9); taos_stmt2_close(stmt); taos_close(taos); From 2ca9035b6f1149cfd68cea43243cfa82620f5526 Mon Sep 17 00:00:00 2001 From: "pengrongkun94@qq.com" Date: Mon, 30 Dec 2024 20:41:44 +0800 Subject: [PATCH 3/4] add test case --- source/client/test/stmt2Test.cpp | 68 ++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 21 deletions(-) diff --git a/source/client/test/stmt2Test.cpp b/source/client/test/stmt2Test.cpp index 03eed54f77b..45919be726f 100644 --- a/source/client/test/stmt2Test.cpp +++ b/source/client/test/stmt2Test.cpp @@ -123,7 +123,6 @@ void do_stmt(TAOS* taos, TAOS_STMT2_OPTION* option, const char* sql, int CTB_NUM ASSERT_NE(stmt, nullptr); int code = taos_stmt2_prepare(stmt, sql, 0); ASSERT_EQ(code, 0); - ASSERT_EQ(terrno, 0); // tbname char** tbs = (char**)taosMemoryMalloc(CTB_NUMS * sizeof(char*)); @@ -183,12 +182,10 @@ void do_stmt(TAOS* taos, TAOS_STMT2_OPTION* option, const char* sql, int CTB_NUM TAOS_STMT2_BINDV bindv = {CTB_NUMS, tbs, tags, paramv}; code = taos_stmt2_bind_param(stmt, &bindv, -1); ASSERT_EQ(code, 0); - ASSERT_EQ(errno, 0); // exec code = taos_stmt2_exec(stmt, NULL); ASSERT_EQ(code, 0); - ASSERT_EQ(errno, 0); for (int i = 0; i < CTB_NUMS; i++) { if (hastags) { @@ -793,12 +790,10 @@ TEST(stmt2Case, stmt2_init_prepare_Test) { ASSERT_EQ(terrno, 0); ASSERT_NE(stmt, nullptr); int code = taos_stmt2_prepare(stmt, "wrong sql", 0); - ASSERT_EQ(terrno, 0); ASSERT_NE(stmt, nullptr); ASSERT_EQ(((STscStmt2*)stmt)->db, nullptr); code = taos_stmt2_prepare(stmt, "insert into 'db'.stb(t1,t2,ts,b,tbname) values(?,?,?,?,?)", 0); - ASSERT_EQ(terrno, 0); ASSERT_NE(stmt, nullptr); ASSERT_STREQ(((STscStmt2*)stmt)->db, "db"); // add in main TD-33332 taos_stmt2_close(stmt); @@ -807,7 +802,6 @@ TEST(stmt2Case, stmt2_init_prepare_Test) { { TAOS_STMT2_OPTION option = {0, true, false, NULL, NULL}; TAOS_STMT2* stmt = taos_stmt2_init(taos, &option); - ASSERT_EQ(terrno, 0); ASSERT_NE(stmt, nullptr); taos_stmt2_close(stmt); } @@ -815,7 +809,6 @@ TEST(stmt2Case, stmt2_init_prepare_Test) { { TAOS_STMT2_OPTION option = {0, true, true, stmtAsyncQueryCb, NULL}; TAOS_STMT2* stmt = taos_stmt2_init(taos, &option); - ASSERT_EQ(terrno, 0); ASSERT_NE(stmt, nullptr); taos_stmt2_close(stmt); } @@ -856,25 +849,33 @@ TEST(stmt2Case, stmt2_query) { TAOS_STMT2* stmt = taos_stmt2_init(taos, &option); ASSERT_NE(stmt, nullptr); - const char* sql = "select * from db.stb where ts = ?"; + const char* sql = "select * from db.stb where ts = ? and tbname = ?"; int code = taos_stmt2_prepare(stmt, sql, 0); ASSERT_EQ(code, 0); - ASSERT_EQ(terrno, 0); - int t64_len[2] = {sizeof(int64_t), sizeof(int64_t)}; - int b_len[2] = {5, 5}; + int t64_len[1] = {sizeof(int64_t)}; + int b_len[1] = {3}; int64_t ts = 1591060628000; TAOS_STMT2_BIND params[2] = {{TSDB_DATA_TYPE_TIMESTAMP, &ts, t64_len, NULL, 1}, {TSDB_DATA_TYPE_BINARY, (void*)"tb1", b_len, NULL, 1}}; TAOS_STMT2_BIND* paramv = ¶ms[0]; TAOS_STMT2_BINDV bindv = {1, NULL, NULL, ¶mv}; - taos_stmt2_bind_param(stmt, &bindv, -1); + code = taos_stmt2_bind_param(stmt, &bindv, -1); ASSERT_EQ(code, 0); - ASSERT_EQ(errno, 0); taos_stmt2_exec(stmt, NULL); ASSERT_EQ(code, 0); - ASSERT_EQ(errno, 0); + + TAOS_RES* pRes = taos_stmt2_result(stmt); + ASSERT_NE(pRes, nullptr); + + int getRecordCounts = 0; + TAOS_ROW row; + while ((row = taos_fetch_row(pRes))) { + getRecordCounts++; + } + ASSERT_EQ(getRecordCounts, 1); + taos_free_result(pRes); taos_stmt2_close(stmt); taos_close(taos); @@ -894,11 +895,10 @@ TEST(stmt2Case, stmt2_ntb_insert) { const char* sql = "insert into db.ntb values(?,?)"; int code = taos_stmt2_prepare(stmt, sql, 0); ASSERT_EQ(code, 0); - ASSERT_EQ(terrno, 0); for (int i = 0; i < 3; i++) { int64_t ts[3] = {1591060628000 + i * 3, 1591060628001 + i * 3, 1591060628002 + i * 3}; int t64_len[3] = {sizeof(int64_t), sizeof(int64_t), sizeof(int64_t)}; - int b_len[3] = {5,5,5}; + int b_len[3] = {5, 5, 5}; TAOS_STMT2_BIND params1 = {TSDB_DATA_TYPE_TIMESTAMP, &ts[0], &t64_len[0], NULL, 3}; TAOS_STMT2_BIND params2 = {TSDB_DATA_TYPE_BINARY, (void*)"abcdefghijklmnopqrstuvwxyz", &b_len[0], NULL, 3}; @@ -908,14 +908,12 @@ TEST(stmt2Case, stmt2_ntb_insert) { TAOS_STMT2_BINDV bindv1 = {1, NULL, NULL, ¶mv1}; TAOS_STMT2_BINDV bindv2 = {1, NULL, NULL, ¶mv2}; - taos_stmt2_bind_param(stmt, &bindv1, 0); - taos_stmt2_bind_param(stmt, &bindv2, 1); + code = taos_stmt2_bind_param(stmt, &bindv1, 0); + code = taos_stmt2_bind_param(stmt, &bindv2, 1); ASSERT_EQ(code, 0); - ASSERT_EQ(errno, 0); code = taos_stmt2_exec(stmt, NULL); ASSERT_EQ(code, 0); - ASSERT_EQ(errno, 0); } checkRows(taos, "select * from db.ntb", 9); @@ -923,6 +921,34 @@ TEST(stmt2Case, stmt2_ntb_insert) { taos_close(taos); } -TEST(stmt2Case, stmt2_status_Test) {} +TEST(stmt2Case, stmt2_status_Test) { + TAOS* taos = taos_connect("localhost", "root", "taosdata", "", 0); + ASSERT_NE(taos, nullptr); + TAOS_STMT2_OPTION option = {0, true, true, NULL, NULL}; + TAOS_STMT2* stmt = taos_stmt2_init(taos, &option); + + int64_t ts[3] = {1591060628000, 1591060628001, 1591060628002}; + int t64_len[3] = {sizeof(int64_t), sizeof(int64_t), sizeof(int64_t)}; + + TAOS_STMT2_BIND params = {TSDB_DATA_TYPE_TIMESTAMP, &ts[0], &t64_len[0], NULL, 3}; + TAOS_STMT2_BIND* paramv = ¶ms; + TAOS_STMT2_BINDV bindv1 = {1, NULL, NULL, ¶mv}; + + int code = taos_stmt2_bind_param(stmt, &bindv1, 0); + ASSERT_EQ(code, TSDB_CODE_TSC_STMT_BIND_NUMBER_ERROR); + ASSERT_STREQ(taos_stmt2_error(stmt), "bind number out of range or not match"); + + code = taos_stmt2_exec(stmt, NULL); + ASSERT_EQ(code, TSDB_CODE_TSC_STMT_API_ERROR); + ASSERT_STREQ(taos_stmt2_error(stmt), "Stmt API usage error"); + + const char* sql = "insert into db.ntb values(?,?)"; + code = taos_stmt2_prepare(stmt, sql, 0); + ASSERT_EQ(code, TSDB_CODE_TSC_STMT_API_ERROR); + ASSERT_STREQ(taos_stmt2_error(stmt), "Stmt API usage error"); + + taos_stmt2_close(stmt); + taos_close(taos); +} #pragma GCC diagnostic pop From 7c400e85ac422dec96bcc3bd9a60144a4a18becc Mon Sep 17 00:00:00 2001 From: "pengrongkun94@qq.com" Date: Tue, 31 Dec 2024 14:37:58 +0800 Subject: [PATCH 4/4] reform stmt2 interlace --- include/libs/parser/parser.h | 4 +- source/client/src/clientStmt2.c | 133 +++++++------ source/client/test/stmt2Test.cpp | 56 +++++- source/libs/parser/src/parInsertStmt.c | 256 ++++++++++++------------- 4 files changed, 259 insertions(+), 190 deletions(-) diff --git a/include/libs/parser/parser.h b/include/libs/parser/parser.h index 95f522f5047..27da0c13d2c 100644 --- a/include/libs/parser/parser.h +++ b/include/libs/parser/parser.h @@ -156,8 +156,8 @@ int32_t qBindStmtTagsValue(void* pBlock, void* boundTags, int64_t suid, const ch TAOS_MULTI_BIND* bind, char* msgBuf, int32_t msgBufLen, void* charsetCxt); int32_t qStmtBindParams2(SQuery* pQuery, TAOS_STMT2_BIND* pParams, int32_t colIdx, void* charsetCxt); -int32_t qBindStmtStbColsValue2(void* pBlock, SArray* pCols, TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen, - STSchema** pTSchema, SBindInfo2* pBindInfos, void *charsetCxt); +// int32_t qBindStmtStbColsValue2(void* pBlock, SArray* pCols, TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen, +// STSchema** pTSchema, SBindInfo2* pBindInfos, void *charsetCxt); int32_t qBindStmtColsValue2(void* pBlock, SArray* pCols, TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen, void *charsetCxt); int32_t qBindStmtSingleColValue2(void* pBlock, SArray* pCols, TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen, int32_t colIdx, int32_t rowNum, void *charsetCxt); diff --git a/source/client/src/clientStmt2.c b/source/client/src/clientStmt2.c index 5992493e5ba..1d9acc69823 100644 --- a/source/client/src/clientStmt2.c +++ b/source/client/src/clientStmt2.c @@ -284,16 +284,17 @@ static int32_t stmtParseSql(STscStmt2* pStmt) { STableDataCxt* pTableCtx = *pSrc; if (pStmt->sql.stbInterlaceMode) { - int16_t lastIdx = -1; + return TSDB_CODE_TSC_STMT_API_ERROR; + // int16_t lastIdx = -1; - for (int32_t i = 0; i < pTableCtx->boundColsInfo.numOfBound; ++i) { - if (pTableCtx->boundColsInfo.pColIndex[i] < lastIdx) { - pStmt->sql.stbInterlaceMode = false; - break; - } + // for (int32_t i = 0; i < pTableCtx->boundColsInfo.numOfBound; ++i) { + // if (pTableCtx->boundColsInfo.pColIndex[i] < lastIdx) { + // pStmt->sql.stbInterlaceMode = false; + // break; + // } - lastIdx = pTableCtx->boundColsInfo.pColIndex[i]; - } + // lastIdx = pTableCtx->boundColsInfo.pColIndex[i]; + // } } if (NULL == pStmt->sql.pBindInfo) { @@ -497,9 +498,10 @@ static int32_t stmtRebuildDataBlock(STscStmt2* pStmt, STableDataCxt* pDataBlock, static int32_t stmtGetFromCache(STscStmt2* pStmt) { if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx) { - pStmt->bInfo.needParse = false; - pStmt->bInfo.inExecCache = false; - return TSDB_CODE_SUCCESS; + return TSDB_CODE_TSC_STMT_API_ERROR; + // pStmt->bInfo.needParse = false; + // pStmt->bInfo.inExecCache = false; + // return TSDB_CODE_SUCCESS; } pStmt->bInfo.needParse = true; @@ -979,6 +981,7 @@ int stmtSetTbName2(TAOS_STMT2* stmt, const char* tbName) { } if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) { + return TSDB_CODE_TSC_STMT_API_ERROR; // STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt)); } @@ -1009,6 +1012,7 @@ int stmtSetTbTags2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* tags) { STMT_ERR_RET(stmtParseSql(pStmt)); } if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) { + return TSDB_CODE_TSC_STMT_API_ERROR; // STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt)); } @@ -1078,7 +1082,8 @@ static int stmtFetchStbColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIEL STableDataCxt** pDataBlock = NULL; if (pStmt->sql.stbInterlaceMode) { - pDataBlock = &pStmt->sql.siInfo.pDataCtx; + return TSDB_CODE_TSC_STMT_API_ERROR; + // pDataBlock = &pStmt->sql.siInfo.pDataCtx; } else { pDataBlock = (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName)); @@ -1158,28 +1163,28 @@ SArray* stmtGetFreeCol(STscStmt2* pStmt, int32_t* idx) { // return TSDB_CODE_SUCCESS; // } -static FORCE_INLINE int32_t stmtGetTableColsFromCache(STscStmt2* pStmt, SArray** pTableCols) { - while (true) { - if (pStmt->sql.siInfo.pTableColsIdx < taosArrayGetSize(pStmt->sql.siInfo.pTableCols)) { - *pTableCols = (SArray*)taosArrayGetP(pStmt->sql.siInfo.pTableCols, pStmt->sql.siInfo.pTableColsIdx++); - break; - } else { - SArray* pTblCols = NULL; - for (int32_t i = 0; i < 100; i++) { - pTblCols = taosArrayInit(20, POINTER_BYTES); - if (NULL == pTblCols) { - return terrno; - } - - if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) { - return terrno; - } - } - } - } +// static FORCE_INLINE int32_t stmtGetTableColsFromCache(STscStmt2* pStmt, SArray** pTableCols) { +// while (true) { +// if (pStmt->sql.siInfo.pTableColsIdx < taosArrayGetSize(pStmt->sql.siInfo.pTableCols)) { +// *pTableCols = (SArray*)taosArrayGetP(pStmt->sql.siInfo.pTableCols, pStmt->sql.siInfo.pTableColsIdx++); +// break; +// } else { +// SArray* pTblCols = NULL; +// for (int32_t i = 0; i < 100; i++) { +// pTblCols = taosArrayInit(20, POINTER_BYTES); +// if (NULL == pTblCols) { +// return terrno; +// } + +// if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) { +// return terrno; +// } +// } +// } +// } - return TSDB_CODE_SUCCESS; -} +// return TSDB_CODE_SUCCESS; +// } static int32_t stmtCacheBlock(STscStmt2* pStmt) { if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) { @@ -1233,6 +1238,7 @@ static int stmtAddBatch2(TAOS_STMT2* stmt) { STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH)); if (pStmt->sql.stbInterlaceMode) { + return TSDB_CODE_TSC_STMT_API_ERROR; // todo:add stb interlace mode // int64_t startUs2 = taosGetTimestampUs(); // pStmt->stat.addBatchUs += startUs2 - startUs; @@ -1367,6 +1373,7 @@ int stmtBindBatch2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* bind, int32_t colIdx) { } if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) { + return TSDB_CODE_TSC_STMT_API_ERROR; // STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt)); } @@ -1393,14 +1400,15 @@ int stmtBindBatch2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* bind, int32_t colIdx) { SStmtQNode* param = NULL; if (pStmt->sql.stbInterlaceMode) { - STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)¶m)); - STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, ¶m->tblData.aCol)); - taosArrayClear(param->tblData.aCol); + return TSDB_CODE_TSC_STMT_API_ERROR; + // STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)¶m)); + // STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, ¶m->tblData.aCol)); + // taosArrayClear(param->tblData.aCol); - // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES); + // // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES); - param->restoreTbCols = false; - tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN); + // param->restoreTbCols = false; + // tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN); } int64_t startUs3 = taosGetTimestampUs(); @@ -1410,12 +1418,15 @@ int stmtBindBatch2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* bind, int32_t colIdx) { if (colIdx < 0) { if (pStmt->sql.stbInterlaceMode) { - (*pDataBlock)->pData->flags = 0; - code = qBindStmtStbColsValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, - pStmt->exec.pRequest->msgBufLen, &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt); + return TSDB_CODE_TSC_STMT_API_ERROR; + // todo + // (*pDataBlock)->pData->flags = 0; + // code = qBindStmtStbColsValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, + // pStmt->exec.pRequest->msgBufLen, &pStmt->sql.siInfo.pTSchema, + // pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt); } else { - code = - qBindStmtColsValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt); + code = qBindStmtColsValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, + pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt); } if (code) { @@ -1424,8 +1435,9 @@ int stmtBindBatch2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* bind, int32_t colIdx) { } } else { if (pStmt->sql.stbInterlaceMode) { - tscError("bind single column not allowed in stb insert mode"); - STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR); + return TSDB_CODE_TSC_STMT_API_ERROR; + // tscError("bind single column not allowed in stb insert mode"); + // STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR); } if (colIdx != (pStmt->bInfo.sBindLastIdx + 1) && colIdx != 0) { @@ -1440,7 +1452,8 @@ int stmtBindBatch2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* bind, int32_t colIdx) { } code = qBindStmtSingleColValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, - pStmt->exec.pRequest->msgBufLen, colIdx, pStmt->bInfo.sBindRowNum, pStmt->taos->optionInfo.charsetCxt); + pStmt->exec.pRequest->msgBufLen, colIdx, pStmt->bInfo.sBindRowNum, + pStmt->taos->optionInfo.charsetCxt); if (code) { tscError("qBindStmtSingleColValue failed, error:%s", tstrerror(code)); STMT_ERR_RET(code); @@ -1451,6 +1464,7 @@ int stmtBindBatch2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* bind, int32_t colIdx) { pStmt->stat.bindDataUs3 += startUs4 - startUs3; if (pStmt->sql.stbInterlaceMode) { + return TSDB_CODE_TSC_STMT_API_ERROR; // to do // STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param)); } else { @@ -1660,23 +1674,26 @@ int stmtExec2(TAOS_STMT2* stmt, int* affected_rows) { } if (pStmt->sql.stbInterlaceMode) { - STMT_ERR_RET(stmtAddBatch2(pStmt)); + return TSDB_CODE_TSC_STMT_API_ERROR; + // STMT_ERR_RET(stmtAddBatch2(pStmt)); } STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE)); if (STMT_TYPE_QUERY != pStmt->sql.type) { if (pStmt->sql.stbInterlaceMode) { - int64_t startTs = taosGetTimestampUs(); - while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) { - taosUsleep(1); - } - pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs; - - STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList)); - taosHashCleanup(pStmt->sql.siInfo.pVgroupHash); - pStmt->sql.siInfo.pVgroupHash = NULL; - pStmt->sql.siInfo.pVgroupList = NULL; + return TSDB_CODE_TSC_STMT_API_ERROR; + // todo:stb interlace mode + // int64_t startTs = taosGetTimestampUs(); + // while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) { + // taosUsleep(1); + // } + // pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs; + + // STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList)); + // taosHashCleanup(pStmt->sql.siInfo.pVgroupHash); + // pStmt->sql.siInfo.pVgroupHash = NULL; + // pStmt->sql.siInfo.pVgroupList = NULL; } else { tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE); taosMemoryFreeClear(pStmt->exec.pCurrTbData); diff --git a/source/client/test/stmt2Test.cpp b/source/client/test/stmt2Test.cpp index 45919be726f..ee6fa7a9e12 100644 --- a/source/client/test/stmt2Test.cpp +++ b/source/client/test/stmt2Test.cpp @@ -815,7 +815,7 @@ TEST(stmt2Case, stmt2_init_prepare_Test) { taos_close(taos); } -TEST(stmt2Case, stmt2_insert) { +TEST(stmt2Case, stmt2_stb_insert) { TAOS* taos = taos_connect("localhost", "root", "taosdata", "", 0); ASSERT_NE(taos, nullptr); TAOS_STMT2_OPTION option = {0, true, true, NULL, NULL}; @@ -830,6 +830,58 @@ TEST(stmt2Case, stmt2_insert) { taos_close(taos); } +// TEST(stmt2Case, stmt2_insert_all_types) { +// TAOS* taos = taos_connect("localhost", "root", "taosdata", "", 0); +// ASSERT_NE(taos, nullptr); +// do_query(taos, "drop database if exists example_all_type_stmt1"); +// do_query(taos, "create database example_all_type_stmt1"); +// do_query(taos, +// "create table example_all_type_stmt1.stb (ts timestamp, int_col int,long_col bigint,double_col " +// "double,bool_col bool,binary_col binary(20),nchar_col nchar(20),varbinary_col varbinary(20),geometry_col " +// "geometry(200)) tags(int_tag int,long_tag bigint,double_tag double,bool_tag bool,binary_tag " +// "binary(20),nchar_tag nchar(20),varbinary_tag varbinary(20),geometry_tag geometry(200));"); + +// TAOS_STMT2_OPTION option = {0, false, false, NULL, NULL}; + +// TAOS_STMT2* stmt = taos_stmt2_init(taos, &option); +// ASSERT_NE(stmt, nullptr); + +// const char* sql = +// "INSERT INTO `example_all_type_stmt1`.`stb1` " +// "(ts,int_col,long_col,double_col,bool_col,binary_col,nchar_col,varbinary_col,geometry_col,int_tag,long_tag," +// "double_tag,bool_tag,binary_tag,nchar_tag,varbinary_tag ,geometry_tag,tbname) VALUES " +// "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; +// int code = taos_stmt2_prepare(stmt, sql, 0); +// ASSERT_EQ(code, 0); + +// int t64_len[1] = {sizeof(int64_t)}; +// int b_len[1] = {3}; +// int64_t ts = 1591060628000; +// TAOS_STMT2_BIND params[18] = {{TSDB_DATA_TYPE_TIMESTAMP, &ts, t64_len, NULL, 1}}; +// TAOS_STMT2_BIND* paramv = ¶ms[0]; +// char* tbname = "tb1"; +// TAOS_STMT2_BINDV bindv = {1, &tbname, NULL, ¶mv}; +// code = taos_stmt2_bind_param(stmt, &bindv, -1); +// ASSERT_EQ(code, 0); + +// taos_stmt2_exec(stmt, NULL); +// ASSERT_EQ(code, 0); + +// TAOS_RES* pRes = taos_stmt2_result(stmt); +// ASSERT_NE(pRes, nullptr); + +// int getRecordCounts = 0; +// TAOS_ROW row; +// while ((row = taos_fetch_row(pRes))) { +// getRecordCounts++; +// } +// ASSERT_EQ(getRecordCounts, 1); +// taos_free_result(pRes); + +// taos_stmt2_close(stmt); +// taos_close(taos); +// } + TEST(stmt2Case, stmt2_query) { TAOS* taos = taos_connect("localhost", "root", "taosdata", "", 0); ASSERT_NE(taos, nullptr); @@ -875,7 +927,7 @@ TEST(stmt2Case, stmt2_query) { getRecordCounts++; } ASSERT_EQ(getRecordCounts, 1); - taos_free_result(pRes); + // taos_free_result(pRes); taos_stmt2_close(stmt); taos_close(taos); diff --git a/source/libs/parser/src/parInsertStmt.c b/source/libs/parser/src/parInsertStmt.c index 74fac463f12..666892e6f52 100644 --- a/source/libs/parser/src/parInsertStmt.c +++ b/source/libs/parser/src/parInsertStmt.c @@ -636,134 +636,134 @@ int32_t qBindStmtTagsValue2(void* pBlock, void* boundTags, int64_t suid, const c return code; } -static int32_t convertStmtStbNcharCol2(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_STMT2_BIND* src, TAOS_STMT2_BIND* dst, void *charsetCxt) { - int32_t output = 0; - const int32_t max_buf_len = pSchema->bytes - VARSTR_HEADER_SIZE; - - dst->buffer = taosMemoryCalloc(src->num, max_buf_len); - if (NULL == dst->buffer) { - return terrno; - } - - dst->length = taosMemoryCalloc(src->num, sizeof(int32_t)); - if (NULL == dst->length) { - taosMemoryFreeClear(dst->buffer); - return terrno; - } - - char* src_buf = src->buffer; - char* dst_buf = dst->buffer; - for (int32_t i = 0; i < src->num; ++i) { - if (src->is_null && src->is_null[i]) { - continue; - } - - if (!taosMbsToUcs4(src_buf, src->length[i], (TdUcs4*)dst_buf, max_buf_len, &output, charsetCxt)) { - if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) { - return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name); - } - char buf[512] = {0}; - snprintf(buf, tListLen(buf), "%s", strerror(terrno)); - return buildSyntaxErrMsg(pMsgBuf, buf, NULL); - } - - dst->length[i] = output; - src_buf += src->length[i]; - dst_buf += output; - } - - dst->buffer_type = src->buffer_type; - dst->is_null = src->is_null; - dst->num = src->num; - - return TSDB_CODE_SUCCESS; -} - -int32_t qBindStmtStbColsValue2(void* pBlock, SArray* pCols, TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen, - STSchema** pTSchema, SBindInfo2* pBindInfos, void *charsetCxt) { - STableDataCxt* pDataBlock = (STableDataCxt*)pBlock; - SSchema* pSchema = getTableColumnSchema(pDataBlock->pMeta); - SBoundColInfo* boundInfo = &pDataBlock->boundColsInfo; - SMsgBuf pBuf = {.buf = msgBuf, .len = msgBufLen}; - int32_t rowNum = bind->num; - SArray* ncharBinds = NULL; - TAOS_STMT2_BIND ncharBind = {0}; - int32_t code = 0; - int16_t lastColId = -1; - bool colInOrder = true; - int ncharColNums = 0; - - if (NULL == *pTSchema) { - *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion); - } - - for (int c = 0; c < boundInfo->numOfBound; ++c) { - if (TSDB_DATA_TYPE_NCHAR == pSchema[boundInfo->pColIndex[c]].type) { - ncharColNums++; - } - } - if (ncharColNums > 0) { - ncharBinds = taosArrayInit(ncharColNums, sizeof(ncharBind)); - if (!ncharBinds) { - code = terrno; - goto _return; - } - } - - for (int c = 0; c < boundInfo->numOfBound; ++c) { - SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]]; - if (pColSchema->colId <= lastColId) { - colInOrder = false; - } else { - lastColId = pColSchema->colId; - } - - if (bind[c].num != rowNum) { - code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same"); - goto _return; - } - - if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) && - bind[c].buffer_type != pColSchema->type) { // for rowNum ==1 , connector may not set buffer_type - code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type"); - goto _return; - } - - if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) { - code = convertStmtStbNcharCol2(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt); - if (code) { - goto _return; - } - if (!taosArrayPush(ncharBinds, &ncharBind)) { - code = terrno; - goto _return; - } - pBindInfos[c].bind = taosArrayGetLast(ncharBinds); - } else { - pBindInfos[c].bind = bind + c; - } - - pBindInfos[c].columnId = pColSchema->colId; - pBindInfos[c].type = pColSchema->type; - pBindInfos[c].bytes = pColSchema->bytes; - } - - code = tRowBuildFromBind2(pBindInfos, boundInfo->numOfBound, colInOrder, *pTSchema, pCols, &pDataBlock->ordered, &pDataBlock->duplicateTs); - - qDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum); - -_return: - if (ncharBinds) { - for (int i = 0; i < TARRAY_SIZE(ncharBinds); ++i) { - TAOS_STMT2_BIND* ncBind = TARRAY_DATA(ncharBinds); - taosMemoryFree(ncBind[i].buffer); - taosMemoryFree(ncBind[i].length); - } - taosArrayDestroy(ncharBinds); - } - - return code; -} +// static int32_t convertStmtStbNcharCol2(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_STMT2_BIND* src, TAOS_STMT2_BIND* dst, void *charsetCxt) { +// int32_t output = 0; +// const int32_t max_buf_len = pSchema->bytes - VARSTR_HEADER_SIZE; + +// dst->buffer = taosMemoryCalloc(src->num, max_buf_len); +// if (NULL == dst->buffer) { +// return terrno; +// } + +// dst->length = taosMemoryCalloc(src->num, sizeof(int32_t)); +// if (NULL == dst->length) { +// taosMemoryFreeClear(dst->buffer); +// return terrno; +// } + +// char* src_buf = src->buffer; +// char* dst_buf = dst->buffer; +// for (int32_t i = 0; i < src->num; ++i) { +// if (src->is_null && src->is_null[i]) { +// continue; +// } + +// if (!taosMbsToUcs4(src_buf, src->length[i], (TdUcs4*)dst_buf, max_buf_len, &output, charsetCxt)) { +// if (terrno == TAOS_SYSTEM_ERROR(E2BIG)) { +// return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name); +// } +// char buf[512] = {0}; +// snprintf(buf, tListLen(buf), "%s", strerror(terrno)); +// return buildSyntaxErrMsg(pMsgBuf, buf, NULL); +// } + +// dst->length[i] = output; +// src_buf += src->length[i]; +// dst_buf += output; +// } + +// dst->buffer_type = src->buffer_type; +// dst->is_null = src->is_null; +// dst->num = src->num; + +// return TSDB_CODE_SUCCESS; +// } + +// int32_t qBindStmtStbColsValue2(void* pBlock, SArray* pCols, TAOS_STMT2_BIND* bind, char* msgBuf, int32_t msgBufLen, +// STSchema** pTSchema, SBindInfo2* pBindInfos, void *charsetCxt) { +// STableDataCxt* pDataBlock = (STableDataCxt*)pBlock; +// SSchema* pSchema = getTableColumnSchema(pDataBlock->pMeta); +// SBoundColInfo* boundInfo = &pDataBlock->boundColsInfo; +// SMsgBuf pBuf = {.buf = msgBuf, .len = msgBufLen}; +// int32_t rowNum = bind->num; +// SArray* ncharBinds = NULL; +// TAOS_STMT2_BIND ncharBind = {0}; +// int32_t code = 0; +// int16_t lastColId = -1; +// bool colInOrder = true; +// int ncharColNums = 0; + +// if (NULL == *pTSchema) { +// *pTSchema = tBuildTSchema(pSchema, pDataBlock->pMeta->tableInfo.numOfColumns, pDataBlock->pMeta->sversion); +// } + +// for (int c = 0; c < boundInfo->numOfBound; ++c) { +// if (TSDB_DATA_TYPE_NCHAR == pSchema[boundInfo->pColIndex[c]].type) { +// ncharColNums++; +// } +// } +// if (ncharColNums > 0) { +// ncharBinds = taosArrayInit(ncharColNums, sizeof(ncharBind)); +// if (!ncharBinds) { +// code = terrno; +// goto _return; +// } +// } + +// for (int c = 0; c < boundInfo->numOfBound; ++c) { +// SSchema* pColSchema = &pSchema[boundInfo->pColIndex[c]]; +// if (pColSchema->colId <= lastColId) { +// colInOrder = false; +// } else { +// lastColId = pColSchema->colId; +// } + +// if (bind[c].num != rowNum) { +// code = buildInvalidOperationMsg(&pBuf, "row number in each bind param should be the same"); +// goto _return; +// } + +// if ((!(rowNum == 1 && bind[c].is_null && *bind[c].is_null)) && +// bind[c].buffer_type != pColSchema->type) { // for rowNum ==1 , connector may not set buffer_type +// code = buildInvalidOperationMsg(&pBuf, "column type mis-match with buffer type"); +// goto _return; +// } + +// if (TSDB_DATA_TYPE_NCHAR == pColSchema->type) { +// code = convertStmtStbNcharCol2(&pBuf, pColSchema, bind + c, &ncharBind, charsetCxt); +// if (code) { +// goto _return; +// } +// if (!taosArrayPush(ncharBinds, &ncharBind)) { +// code = terrno; +// goto _return; +// } +// pBindInfos[c].bind = taosArrayGetLast(ncharBinds); +// } else { +// pBindInfos[c].bind = bind + c; +// } + +// pBindInfos[c].columnId = pColSchema->colId; +// pBindInfos[c].type = pColSchema->type; +// pBindInfos[c].bytes = pColSchema->bytes; +// } + +// code = tRowBuildFromBind2(pBindInfos, boundInfo->numOfBound, colInOrder, *pTSchema, pCols, &pDataBlock->ordered, &pDataBlock->duplicateTs); + +// qDebug("stmt all %d columns bind %d rows data", boundInfo->numOfBound, rowNum); + +// _return: +// if (ncharBinds) { +// for (int i = 0; i < TARRAY_SIZE(ncharBinds); ++i) { +// TAOS_STMT2_BIND* ncBind = TARRAY_DATA(ncharBinds); +// taosMemoryFree(ncBind[i].buffer); +// taosMemoryFree(ncBind[i].length); +// } +// taosArrayDestroy(ncharBinds); +// } + +// return code; +// } static int32_t convertStmtNcharCol2(SMsgBuf* pMsgBuf, SSchema* pSchema, TAOS_STMT2_BIND* src, TAOS_STMT2_BIND* dst, void *charsetCxt) { int32_t output = 0;