Skip to content

Commit

Permalink
Merge pull request #729 from taosdata/fix/TS-4451
Browse files Browse the repository at this point in the history
create table add if not exists clause
  • Loading branch information
DuanKuanJun committed Jan 18, 2024
2 parents 0e0a63f + d041ed2 commit 58f0677
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions src/benchInsert.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,8 @@ static int createSuperTable(SDataBase* database, SSuperTable* stbInfo) {
int length = snprintf(
command, TSDB_MAX_ALLOWED_SQL_LEN,
g_arguments->escape_character
? "CREATE TABLE `%s`.`%s` (ts TIMESTAMP%s) TAGS %s"
: "CREATE TABLE %s.%s (ts TIMESTAMP%s) TAGS %s",
? "CREATE TABLE IF NOT EXISTS `%s`.`%s` (ts TIMESTAMP%s) TAGS %s"
: "CREATE TABLE IF NOT EXISTS %s.%s (ts TIMESTAMP%s) TAGS %s",
database->dbName, stbInfo->stbName, colsBuf, tagsBuf);
tmfree(colsBuf);
tmfree(tagsBuf);
Expand Down Expand Up @@ -753,7 +753,7 @@ static int generateChildTblName(int len, char *buffer, SDataBase *database,
if (0 == len) {
memset(buffer, 0, TSDB_MAX_ALLOWED_SQL_LEN);
len += snprintf(buffer + len,
TSDB_MAX_ALLOWED_SQL_LEN - len, "CREATE TABLE ");
TSDB_MAX_ALLOWED_SQL_LEN - len, "CREATE TABLE IF NOT EXISTS ");
}

len += snprintf(
Expand Down Expand Up @@ -835,15 +835,15 @@ static void *createTable(void *sarg) {
if (stbInfo->childTblCount == 1) {
snprintf(pThreadInfo->buffer, TSDB_MAX_ALLOWED_SQL_LEN,
g_arguments->escape_character
? "CREATE TABLE `%s`.`%s` %s;"
: "CREATE TABLE %s.%s %s;",
? "CREATE TABLE IF NOT EXISTS `%s`.`%s` %s;"
: "CREATE TABLE IF NOT EXISTS %s.%s %s;",
database->dbName, stbInfo->stbName,
stbInfo->colsOfCreateChildTable);
} else {
snprintf(pThreadInfo->buffer, TSDB_MAX_ALLOWED_SQL_LEN,
g_arguments->escape_character
? "CREATE TABLE `%s`.`%s` %s;"
: "CREATE TABLE %s.%s %s;",
? "CREATE TABLE IF NOT EXISTS `%s`.`%s` %s;"
: "CREATE TABLE IF NOT EXISTS %s.%s %s;",
database->dbName,
stbInfo->childTblArray[i]->name,
stbInfo->colsOfCreateChildTable);
Expand Down Expand Up @@ -1363,8 +1363,8 @@ static int smartContinueIfFail(threadInfo *pThreadInfo,
snprintf(
buffer, TSDB_MAX_ALLOWED_SQL_LEN,
g_arguments->escape_character ?
"CREATE TABLE `%s`.`%s` USING `%s`.`%s` TAGS (%s) %s "
: "CREATE TABLE %s.%s USING %s.%s TAGS (%s) %s ",
"CREATE TABLE IF NOT EXISTS `%s`.`%s` USING `%s`.`%s` TAGS (%s) %s "
: "CREATE TABLE IF NOT EXISTS %s.%s USING %s.%s TAGS (%s) %s ",
database->dbName, childTbl->name, database->dbName,
stbInfo->stbName,
stbInfo->tagDataBuf + i * stbInfo->lenOfTags, ttl);
Expand Down Expand Up @@ -3024,6 +3024,14 @@ static int startMultiThreadInsertData(SDataBase* database,
}
}

// valid check
if(threads <= 0) {
errorPrint("db: %s threads num is invalid. threads=%d\n",
database->dbName,
threads);
return -1;
}

int32_t vgFrom = 0;
#else
a = ntables / threads;
Expand Down Expand Up @@ -3624,6 +3632,21 @@ int insertTestProcess() {
return -1;
}
succPrint("created database (%s)\n", database->dbName);
} else {
// database already exist, get vgroups from server
#ifdef TD_VER_COMPATIBLE_3_0_0_0
if (database->superTbls) {
SBenchConn* conn = initBenchConn();
int32_t vgroups = getVgroupsOfDb(conn, database);
if (vgroups <=0) {
closeBenchConn(conn);
errorPrint("Database %s's vgroups is zero.\n", database->dbName);
return -1;
}
closeBenchConn(conn);
succPrint("Database (%s) get vgroups num is %d from server.\n", database->dbName, vgroups);
}
#endif // TD_VER_COMPATIBLE_3_0_0_0
}
}

Expand Down

0 comments on commit 58f0677

Please sign in to comment.