From 1686c70a6048ecf132be8cc0ccc33a70aadbe399 Mon Sep 17 00:00:00 2001 From: slzhou Date: Tue, 31 Oct 2023 13:14:21 +0800 Subject: [PATCH] fix: not use ctb meta from hash of stmt node --- examples/c/insert_stb.c | 162 ++++++++++++++++++++++++++ examples/c/makefile | 2 + source/libs/parser/src/parInsertSql.c | 3 +- 3 files changed, 166 insertions(+), 1 deletion(-) create mode 100644 examples/c/insert_stb.c diff --git a/examples/c/insert_stb.c b/examples/c/insert_stb.c new file mode 100644 index 0000000000..8316bb929a --- /dev/null +++ b/examples/c/insert_stb.c @@ -0,0 +1,162 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +// TAOS standard API example. The same syntax as MySQL, but only a subset +// to compile: gcc -o demo demo.c -ltaos + +#include +#include +#include +#include +#include + +#include "taos.h" // TAOS header file + +static void executeSql(TAOS *taos, char *command) { + int i; + TAOS_RES *pSql = NULL; + int32_t code = -1; + + for (i = 0; i < 5; i++) { + if (NULL != pSql) { + taos_free_result(pSql); + pSql = NULL; + } + + pSql = taos_query(taos, command); + code = taos_errno(pSql); + if (0 == code) { + break; + } + } + + if (code != 0) { + fprintf(stderr, "Failed to run %s, reason: %s\n", command, taos_errstr(pSql)); + taos_free_result(pSql); + taos_close(taos); + exit(EXIT_FAILURE); + } + + taos_free_result(pSql); +} + +void TestInsert(TAOS *taos, char *qstr) { + executeSql(taos, "drop database if exists demo2"); + executeSql(taos, "create database demo2"); + executeSql(taos, "use demo2"); + + executeSql(taos, "create table st (ts timestamp, ti tinyint, si smallint, i int, bi bigint, f float, d double, b binary(10)) tags(t1 int, t2 float, t3 binary(10))"); + printf("success to create table\n"); + + struct timeval start_time; + gettimeofday(&start_time, NULL); + + for (int tblIdx = 0; tblIdx < 10; ++tblIdx) { + int len = 0; + len += sprintf(qstr+len, "insert into ct%d using st tags(%d, %f, '%s')", tblIdx, tblIdx, (float)tblIdx, "childtable"); + int batchStart = len; + for (int batchIdx = 0; batchIdx < 10000; ++batchIdx) { + len = batchStart; + len += sprintf(qstr+len, " values"); + if (batchIdx % 1000 == 0) + printf("%s %d\n", qstr, batchIdx); + + for (int rowIdx = 0; rowIdx < 100; ++ rowIdx) { + int i = rowIdx + batchIdx * 100 + tblIdx*10000*100; + len += sprintf(qstr+len, " (%" PRId64 ", %d, %d, %d, %d, %f, %lf, '%s')", (uint64_t)(1546300800000 + i), (int8_t)i, (int16_t)i, i, i, i*1.0, i*2.0, "hello"); + } + TAOS_RES *result1 = taos_query(taos, qstr); + if (result1 == NULL || taos_errno(result1) != 0) { + printf("failed to insert row, reason:%s. qstr: %s\n", taos_errstr(result1), qstr); + taos_free_result(result1); + exit(1); + } + taos_free_result(result1); + } + } + struct timeval end_time; + gettimeofday(&end_time, NULL); + double elapsed_time = (end_time.tv_sec - start_time.tv_sec) + + (end_time.tv_usec - start_time.tv_usec) / 1000000.0; + printf("elapsed time: %.3f\n", elapsed_time); + executeSql(taos, "drop database if exists demo2"); +} + +void TestInsertStb(TAOS *taos, char *qstr) { + executeSql(taos, "drop database if exists demo"); + executeSql(taos, "create database demo"); + executeSql(taos, "use demo"); + + executeSql(taos, "create table st (ts timestamp, ti tinyint, si smallint, i int, bi bigint, f float, d double, b binary(10)) tags(t1 int, t2 float, t3 binary(10))"); + printf("success to create table\n"); + + struct timeval start_time; + gettimeofday(&start_time, NULL); + + for (int tblIdx = 0; tblIdx < 10; ++tblIdx) { + int len = 0; + len += sprintf(qstr+len, "insert into st(tbname, t1, t2, t3, ts, ti, si, i, bi, f, d, b)"); + int batchStart = len; + for (int batchIdx = 0; batchIdx < 10000; ++batchIdx) { + len = batchStart; + len += sprintf(qstr+len, " values"); + if (batchIdx % 1000 == 0) + printf("%s %d\n", qstr, batchIdx); + + for (int rowIdx = 0; rowIdx < 100; ++rowIdx) { + int i = rowIdx + batchIdx * 100 + tblIdx*10000*100; + len += sprintf(qstr+len, " ('ct%d', %d, %f, '%s', %" PRId64 ", %d, %d, %d, %d, %f, %lf, '%s')", tblIdx, tblIdx, (float)tblIdx, "childtable", + (uint64_t)(1546300800000 + i), (int8_t)i, (int16_t)i, i, i, i*1.0, i*2.0, "hello"); + } + TAOS_RES *result1 = taos_query(taos, qstr); + if (result1 == NULL || taos_errno(result1) != 0) { + printf("failed to insert row, reason:%s. qstr: %s\n", taos_errstr(result1), qstr); + taos_free_result(result1); + exit(1); + } + taos_free_result(result1); + } + } + struct timeval end_time; + gettimeofday(&end_time, NULL); + double elapsed_time = (end_time.tv_sec - start_time.tv_sec) + + (end_time.tv_usec - start_time.tv_usec) / 1000000.0; + + printf("elapsed time: %.3f\n", elapsed_time); + executeSql(taos, "drop database if exists demo"); +} + + +int main(int argc, char *argv[]) { + + // connect to server + if (argc < 2) { + printf("please input server-ip \n"); + return 0; + } + + TAOS *taos = taos_connect(argv[1], "root", "taosdata", NULL, 0); + if (taos == NULL) { + printf("failed to connect to server, reason:%s\n", taos_errstr(NULL)); + exit(1); + } + char* qstr = malloc(1024*1024); + //TestInsert(taos, qstr); + TestInsertStb(taos, qstr); + free(qstr); + taos_close(taos); + taos_cleanup(); +} + diff --git a/examples/c/makefile b/examples/c/makefile index 244d13fad7..5fc590f424 100644 --- a/examples/c/makefile +++ b/examples/c/makefile @@ -17,6 +17,7 @@ exe: gcc $(CFLAGS) ./stream_demo.c -o $(ROOT)stream_demo $(LFLAGS) gcc $(CFLAGS) ./tmq.c -o $(ROOT)tmq $(LFLAGS) gcc $(CFLAGS) ./schemaless.c -o $(ROOT)schemaless $(LFLAGS) + gcc $(CFLAGS) ./insert_stb.c -o $(ROOT)insert_stb $(LFLAGS) clean: rm $(ROOT)asyncdemo @@ -25,3 +26,4 @@ clean: rm $(ROOT)stream_demo rm $(ROOT)tmq rm $(ROOT)schemaless + rm $(ROOT)insert_stb diff --git a/source/libs/parser/src/parInsertSql.c b/source/libs/parser/src/parInsertSql.c index 231e28878a..663a636ebc 100644 --- a/source/libs/parser/src/parInsertSql.c +++ b/source/libs/parser/src/parInsertSql.c @@ -1660,7 +1660,8 @@ static int32_t getStbRowValues(SInsertParseContext* pCxt, SVnodeModifyOpStmt* pS STableMeta** pCtbMeta = taosHashGet(pStmt->pSubTableHashObj, ctbFName, strlen(ctbFName)); ctbFirst = *pCtbFirst = (pCtbMeta == NULL); if (!ctbFirst) { - pStbRowsCxt->pCtbMeta = *pCtbMeta; + pStbRowsCxt->pCtbMeta->uid = (*pCtbMeta)->uid; + pStbRowsCxt->pCtbMeta->vgId = (*pCtbMeta)->vgId; } } if (code == TSDB_CODE_SUCCESS && ctbFirst) {