Skip to content

Commit 3b886c2

Browse files
committed
morefix: not allow duplicate table names in stmt2 bind with interlace mode
1 parent 4f06b3b commit 3b886c2

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

source/client/src/clientMain.c

+30
Original file line numberDiff line numberDiff line change
@@ -2167,9 +2167,37 @@ int taos_stmt2_bind_param(TAOS_STMT2 *stmt, TAOS_STMT2_BINDV *bindv, int32_t col
21672167
pStmt->semWaited = true;
21682168
}
21692169

2170+
/*
2171+
* Insert with interlace mode doesn't allow duplicate table names per bind,
2172+
* so initialize a tbnames hash table for duplication check.
2173+
*
2174+
* Note, STMT doesn't have such issue as it's unable to bind multiple tables.
2175+
*/
2176+
SSHashObj *hashTbnames = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR));
2177+
if (NULL == hashTbnames) {
2178+
tscError("stmt2 bind failed: out of memory");
2179+
terrno = TSDB_CODE_OUT_OF_MEMORY;
2180+
return terrno;
2181+
}
2182+
21702183
int32_t code = 0;
21712184
for (int i = 0; i < bindv->count; ++i) {
21722185
if (bindv->tbnames && bindv->tbnames[i]) {
2186+
if (pStmt->sql.stbInterlaceMode) {
2187+
if (tSimpleHashGet(hashTbnames, bindv->tbnames[i], strlen(bindv->tbnames[i])) != NULL) {
2188+
tSimpleHashCleanup(hashTbnames);
2189+
tscError("stmt2 bind failed: duplicate table name %s is not allowed in interlace mode.", bindv->tbnames[i]);
2190+
terrno = TSDB_CODE_PAR_TBNAME_DUPLICATED;
2191+
return terrno;
2192+
}
2193+
2194+
code = tSimpleHashPut(hashTbnames, bindv->tbnames[i], strlen(bindv->tbnames[i]), NULL, 0);
2195+
if (TSDB_CODE_SUCCESS != code) {
2196+
tSimpleHashCleanup(hashTbnames);
2197+
return code;
2198+
}
2199+
}
2200+
21732201
code = stmtSetTbName2(stmt, bindv->tbnames[i]);
21742202
if (code) {
21752203
return code;
@@ -2207,6 +2235,8 @@ int taos_stmt2_bind_param(TAOS_STMT2 *stmt, TAOS_STMT2_BINDV *bindv, int32_t col
22072235
}
22082236
}
22092237

2238+
tSimpleHashCleanup(hashTbnames);
2239+
22102240
return TSDB_CODE_SUCCESS;
22112241
}
22122242

tests/script/api/stmt2-insert-dupkeys.c

+25-2
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ void insert_dist(TAOS* taos, const char *sql) {
145145
UINIT(tbs, ts, ts_len, b, b_len, tags, paramv);
146146
}
147147

148-
void insert_dup(TAOS* taos, const char *sql) {
148+
void insert_dup_rows(TAOS* taos, const char *sql) {
149149
char **tbs, **b;
150150
int64_t **ts;
151151
int *ts_len, *b_len;
@@ -169,6 +169,27 @@ void insert_dup(TAOS* taos, const char *sql) {
169169
UINIT(tbs, ts, ts_len, b, b_len, tags, paramv);
170170
}
171171

172+
void insert_dup_tables(TAOS* taos, const char *sql) {
173+
char **tbs, **b;
174+
int64_t **ts;
175+
int *ts_len, *b_len;
176+
TAOS_STMT2_BIND **paramv, **tags;
177+
178+
INIT(tbs, ts, ts_len, b, b_len, tags, paramv);
179+
180+
for (int i = 0; i < CTB_NUMS; i++) {
181+
sprintf(tbs[i], "ctb_%d", i % 2);
182+
}
183+
184+
for (int i = 0; i < CTB_NUMS; i++) {
185+
paramv[i][0] = (TAOS_STMT2_BIND){TSDB_DATA_TYPE_TIMESTAMP, &ts[i][0], &ts_len[0], NULL, ROW_NUMS};
186+
paramv[i][1] = (TAOS_STMT2_BIND){TSDB_DATA_TYPE_BINARY, &b[i][0], &b_len[0], NULL, ROW_NUMS};
187+
}
188+
insert(taos, tbs, tags, paramv, sql);
189+
190+
UINIT(tbs, ts, ts_len, b, b_len, tags, paramv);
191+
}
192+
172193
int main() {
173194
TAOS* taos = taos_connect("localhost", "root", "taosdata", "", 0);
174195
if (!taos) {
@@ -180,7 +201,9 @@ int main() {
180201
// insert distinct rows
181202
insert_dist(taos, "insert into db.? using db.stb tags(?,?)values(?,?)");
182203
// insert duplicate rows
183-
insert_dup(taos, "insert into db.? values(?,?)");
204+
insert_dup_rows(taos, "insert into db.? values(?,?)");
205+
// insert duplicate tables
206+
insert_dup_tables(taos, "insert into db.? values(?,?)");
184207

185208
taos_close(taos);
186209
taos_cleanup();

0 commit comments

Comments
 (0)