• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #4987

16 Mar 2026 12:26PM UTC coverage: 73.883% (+36.6%) from 37.305%
#4987

push

travis-ci

web-flow
feat: support secure delete option. (#34591)

209 of 391 new or added lines in 24 files covered. (53.45%)

3062 existing lines in 140 files now uncovered.

261133 of 353439 relevant lines covered (73.88%)

121262425.02 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.41
/source/client/src/clientStmt2.c
1
#include "clientInt.h"
2
#include "clientLog.h"
3
#include "tdef.h"
4
#include "tglobal.h"
5

6
#include "clientStmt.h"
7
#include "clientStmt2.h"
8

9
char* gStmt2StatusStr[] = {"unknown",     "init", "prepare", "settbname", "settags",
10
                           "fetchFields", "bind", "bindCol", "addBatch",  "exec"};
11

12

13
static FORCE_INLINE int32_t stmtAllocQNodeFromBuf(STableBufInfo* pTblBuf, void** pBuf) {
14
  if (pTblBuf->buffOffset < pTblBuf->buffSize) {
1,638,253✔
15
    *pBuf = (char*)pTblBuf->pCurBuff + pTblBuf->buffOffset;
1,638,214✔
16
    pTblBuf->buffOffset += pTblBuf->buffUnit;
1,637,911✔
17
  } else if (pTblBuf->buffIdx < taosArrayGetSize(pTblBuf->pBufList)) {
×
18
    pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, pTblBuf->buffIdx++);
×
19
    if (NULL == pTblBuf->pCurBuff) {
×
20
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
21
    }
22
    *pBuf = pTblBuf->pCurBuff;
×
23
    pTblBuf->buffOffset = pTblBuf->buffUnit;
×
24
  } else {
25
    void* buff = taosMemoryMalloc(pTblBuf->buffSize);
×
26
    if (NULL == buff) {
×
27
      return terrno;
×
28
    }
29

30
    if (taosArrayPush(pTblBuf->pBufList, &buff) == NULL) {
×
31
      return terrno;
×
32
    }
33

34
    pTblBuf->buffIdx++;
×
35
    pTblBuf->pCurBuff = buff;
×
36
    *pBuf = buff;
×
37
    pTblBuf->buffOffset = pTblBuf->buffUnit;
×
38
  }
39

40
  return TSDB_CODE_SUCCESS;
1,638,202✔
41
}
42

43
static bool stmtDequeue(STscStmt2* pStmt, SStmtQNode** param) {
1,638,592✔
44
  int i = 0;
1,638,592✔
45
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
7,581,140✔
46
    if (pStmt->queue.stopQueue) {
6,057,526✔
47
      return false;
114,643✔
48
    }
49
    if (i < 10) {
5,942,485✔
50
      taosUsleep(1);
5,555,744✔
51
      i++;
5,555,504✔
52
    } else {
53
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
386,741✔
54
      if (pStmt->queue.stopQueue) {
387,214✔
55
        (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
×
56
        return false;
×
57
      }
58
      if (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
387,214✔
59
        (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
386,994✔
60
      }
61
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
387,005✔
62
    }
63
  }
64

65
  if (pStmt->queue.stopQueue && 0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
1,523,487✔
66
    return false;
×
67
  }
68

69
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
1,523,487✔
70
  if (pStmt->queue.head == pStmt->queue.tail) {
1,524,074✔
71
    pStmt->queue.qRemainNum = 0;
×
72
    (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
×
73
    STMT2_ELOG_E("interlace queue is empty, cannot dequeue");
×
74
    return false;
×
75
  }
76

77
  SStmtQNode* node = pStmt->queue.head->next;
1,523,992✔
78
  pStmt->queue.head->next = node->next;
1,524,033✔
79
  if (pStmt->queue.tail == node) {
1,524,074✔
80
    pStmt->queue.tail = pStmt->queue.head;
821,890✔
81
  }
82
  node->next = NULL;
1,524,144✔
83
  *param = node;
1,524,109✔
84

85
  (void)atomic_sub_fetch_64((int64_t*)&pStmt->queue.qRemainNum, 1);
1,524,109✔
86
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
1,524,020✔
87

88
  STMT2_TLOG("dequeue success, node:%p, remainNum:%" PRId64, node, pStmt->queue.qRemainNum);
1,524,007✔
89

90
  return true;
1,524,053✔
91
}
92

93
static void stmtEnqueue(STscStmt2* pStmt, SStmtQNode* param) {
1,523,474✔
94
  if (param == NULL) {
1,523,474✔
95
    STMT2_ELOG_E("enqueue param is NULL");
×
96
    return;
×
97
  }
98

99
  param->next = NULL;
1,523,474✔
100

101
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
1,523,544✔
102

103
  pStmt->queue.tail->next = param;
1,523,771✔
104
  pStmt->queue.tail = param;
1,523,853✔
105
  pStmt->stat.bindDataNum++;
1,523,853✔
106

107
  (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
1,523,853✔
108
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
1,523,903✔
109

110
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
1,524,067✔
111

112
  STMT2_TLOG("enqueue param:%p, remainNum:%" PRId64 ", restoreTbCols:%d", param, pStmt->queue.qRemainNum,
1,524,109✔
113
             param->restoreTbCols);
114
}
115

116
static int32_t stmtCreateRequest(STscStmt2* pStmt) {
44,487,995✔
117
  int32_t code = 0;
44,487,995✔
118

119
  if (pStmt->exec.pRequest == NULL) {
44,487,995✔
120
    code = buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false, &pStmt->exec.pRequest,
128,052✔
121
                        pStmt->reqid);
122
    if (pStmt->reqid != 0) {
128,052✔
123
      pStmt->reqid++;
8✔
124
    }
125
    pStmt->exec.pRequest->type = RES_TYPE__QUERY;
128,052✔
126
    if (pStmt->db != NULL) {
128,011✔
127
      taosMemoryFreeClear(pStmt->exec.pRequest->pDb);
123,019✔
128
      pStmt->exec.pRequest->pDb = taosStrdup(pStmt->db);
123,060✔
129
    }
130
    if (TSDB_CODE_SUCCESS == code) {
128,021✔
131
      pStmt->exec.pRequest->syncQuery = true;
128,052✔
132
      pStmt->exec.pRequest->stmtBindVersion = 2;
127,980✔
133
    }
134
    STMT2_DLOG("create request:0x%" PRIx64 ", QID:0x%" PRIx64, pStmt->exec.pRequest->self,
128,021✔
135
               pStmt->exec.pRequest->requestId);
136
  }
137

138
  return code;
43,974,107✔
139
}
140

141
static int32_t stmtSwitchStatus(STscStmt2* pStmt, STMT_STATUS newStatus) {
85,515,772✔
142
  int32_t code = 0;
85,515,772✔
143

144
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
85,515,772✔
145
    STMT2_LOG_SEQ(newStatus);
86,630,954✔
146
  }
147

148
  if (pStmt->errCode && newStatus != STMT_PREPARE) {
86,381,876✔
149
    STMT2_ELOG("stmt already failed with err:%s, please use stmt prepare", tstrerror(pStmt->errCode));
×
150
    return pStmt->errCode;
×
151
  }
152

153
  switch (newStatus) {
86,041,143✔
154
    case STMT_PREPARE:
122,983✔
155
      pStmt->errCode = 0;
122,983✔
156
      break;
88,818✔
157
    case STMT_SETTBNAME:
1,049,217✔
158
      if (STMT_STATUS_EQ(INIT)) {
1,049,217✔
159
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
160
      }
161
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
1,049,217✔
162
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
163
      }
164
      break;
1,049,170✔
165
    case STMT_SETTAGS:
652,393✔
166
      if (STMT_STATUS_EQ(INIT)) {
652,393✔
167
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
168
      }
169
      break;
652,393✔
170
    case STMT_FETCH_FIELDS:
8,661✔
171
      if (STMT_STATUS_EQ(INIT)) {
8,661✔
172
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
173
      }
174
      break;
8,661✔
175
    case STMT_BIND:
42,065,413✔
176
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
42,065,413✔
177
        code = TSDB_CODE_TSC_STMT_API_ERROR;
96✔
178
      }
179
      /*
180
            if ((pStmt->sql.type == STMT_TYPE_MULTI_INSERT) && ()) {
181
              code = TSDB_CODE_TSC_STMT_API_ERROR;
182
            }
183
      */
184
      break;
42,604,948✔
185
    case STMT_BIND_COL:
×
186
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND)) {
×
187
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
188
      }
189
      break;
×
190
    case STMT_ADD_BATCH:
41,540,472✔
191
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
41,540,472✔
192
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
193
      }
194
      break;
41,760,894✔
195
    case STMT_EXECUTE:
602,004✔
196
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
602,004✔
197
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
3,656✔
198
            STMT_STATUS_NE(BIND_COL)) {
×
199
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
200
        }
201
      } else {
202
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS)) {
598,316✔
203
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
204
        }
205
      }
206
      break;
602,007✔
207
    default:
×
208
      code = TSDB_CODE_APP_ERROR;
×
209
      break;
×
210
  }
211

212
  STMT_ERR_RET(code);
86,338,063✔
213

214
  pStmt->sql.status = newStatus;
86,337,967✔
215

216
  return TSDB_CODE_SUCCESS;
86,663,376✔
217
}
218

219
static int32_t stmtGetTbName(TAOS_STMT2* stmt, char** tbName) {
15,060✔
220
  STscStmt2* pStmt = (STscStmt2*)stmt;
15,060✔
221

222
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
15,060✔
223

224
  if ('\0' == pStmt->bInfo.tbName[0]) {
15,101✔
225
    tscWarn("no table name set, OK if it is a stmt get fields");
4,245✔
226
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_TBNAME_ERROR);
4,245✔
227
  }
228

229
  *tbName = pStmt->bInfo.tbName;
10,815✔
230

231
  return TSDB_CODE_SUCCESS;
10,856✔
232
}
233

234
static int32_t stmtUpdateBindInfo(TAOS_STMT2* stmt, STableMeta* pTableMeta, void* tags, SSHashObj** cols, SName* tbName,
118,444✔
235
                                  const char* sTableName, bool autoCreateTbl, int8_t tbNameFlag) {
236
  STscStmt2* pStmt = (STscStmt2*)stmt;
118,444✔
237
  char       tbFName[TSDB_TABLE_FNAME_LEN];
95,651✔
238
  int32_t    code = tNameExtractFullName(tbName, tbFName);
118,444✔
239
  if (code != 0) {
118,573✔
240
    return code;
×
241
  }
242

243
  if ((tags != NULL && ((SBoundColInfo*)tags)->numOfCols == 0) || !autoCreateTbl) {
118,573✔
244
    pStmt->sql.autoCreateTbl = false;
107,794✔
245
  }
246

247
  (void)memcpy(&pStmt->bInfo.sname, tbName, sizeof(*tbName));
118,532✔
248
  tstrncpy(pStmt->bInfo.tbFName, tbFName, sizeof(pStmt->bInfo.tbFName));
118,532✔
249
  pStmt->bInfo.tbFName[sizeof(pStmt->bInfo.tbFName) - 1] = 0;
118,506✔
250

251
  pStmt->bInfo.tbUid = autoCreateTbl ? 0 : pTableMeta->uid;
118,480✔
252
  pStmt->bInfo.tbSuid = pTableMeta->suid;
118,573✔
253
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
118,480✔
254
  pStmt->bInfo.tbType = pTableMeta->tableType;
118,506✔
255

256
  if (!pStmt->bInfo.tagsCached) {
118,532✔
257
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
117,893✔
258
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
117,897✔
259
  }
260

261
  // transfer ownership of cols to stmt
262
  if (cols) {
118,399✔
263
    pStmt->bInfo.fixedValueCols = *cols;
118,469✔
264
    *cols = NULL;
118,510✔
265
  }
266

267
  pStmt->bInfo.boundTags = tags;
118,158✔
268
  pStmt->bInfo.tagsCached = false;
118,143✔
269
  pStmt->bInfo.tbNameFlag = tbNameFlag;
118,381✔
270
  tstrncpy(pStmt->bInfo.stbFName, sTableName, sizeof(pStmt->bInfo.stbFName));
118,146✔
271

272
  if (pTableMeta->tableType != TSDB_CHILD_TABLE && pTableMeta->tableType != TSDB_SUPER_TABLE) {
118,410✔
273
    pStmt->sql.stbInterlaceMode = false;
2,765✔
274
  }
275

276
  return TSDB_CODE_SUCCESS;
118,355✔
277
}
278

279
static int32_t stmtUpdateExecInfo(TAOS_STMT2* stmt, SHashObj* pVgHash, SHashObj* pBlockHash) {
118,407✔
280
  STscStmt2* pStmt = (STscStmt2*)stmt;
118,407✔
281

282
  pStmt->sql.pVgHash = pVgHash;
118,407✔
283
  pStmt->exec.pBlockHash = pBlockHash;
118,407✔
284

285
  return TSDB_CODE_SUCCESS;
118,355✔
286
}
287

288
static int32_t stmtUpdateInfo(TAOS_STMT2* stmt, STableMeta* pTableMeta, void* tags, SSHashObj** cols, SName* tbName,
118,481✔
289
                              bool autoCreateTbl, SHashObj* pVgHash, SHashObj* pBlockHash, const char* sTableName,
290
                              uint8_t tbNameFlag) {
291
  STscStmt2* pStmt = (STscStmt2*)stmt;
118,481✔
292

293
  STMT_ERR_RET(stmtUpdateBindInfo(stmt, pTableMeta, tags, cols, tbName, sTableName, autoCreateTbl, tbNameFlag));
118,481✔
294
  STMT_ERR_RET(stmtUpdateExecInfo(stmt, pVgHash, pBlockHash));
118,381✔
295

296
  pStmt->sql.autoCreateTbl = autoCreateTbl;
118,392✔
297

298
  return TSDB_CODE_SUCCESS;
118,444✔
299
}
300

301
static int32_t stmtGetExecInfo(TAOS_STMT2* stmt, SHashObj** pVgHash, SHashObj** pBlockHash) {
1,749✔
302
  STscStmt2* pStmt = (STscStmt2*)stmt;
1,749✔
303

304
  *pVgHash = pStmt->sql.pVgHash;
1,749✔
305
  pStmt->sql.pVgHash = NULL;
1,749✔
306

307
  *pBlockHash = pStmt->exec.pBlockHash;
1,749✔
308
  pStmt->exec.pBlockHash = NULL;
1,749✔
309

310
  return TSDB_CODE_SUCCESS;
1,749✔
311
}
312

313
static int32_t stmtParseSql(STscStmt2* pStmt) {
122,907✔
314
  pStmt->exec.pCurrBlock = NULL;
122,907✔
315

316
  SStmtCallback stmtCb = {
123,052✔
317
      .pStmt = pStmt,
318
      .getTbNameFn = stmtGetTbName,
319
      .setInfoFn = stmtUpdateInfo,
320
      .getExecInfoFn = stmtGetExecInfo,
321
  };
322

323
  STMT_ERR_RET(stmtCreateRequest(pStmt));
123,052✔
324
  pStmt->exec.pRequest->stmtBindVersion = 2;
123,052✔
325

326
  pStmt->stat.parseSqlNum++;
123,052✔
327

328
  STMT2_DLOG("start to parse, QID:0x%" PRIx64, pStmt->exec.pRequest->requestId);
123,055✔
329
  STMT_ERR_RET(parseSql(pStmt->exec.pRequest, false, &pStmt->sql.pQuery, &stmtCb));
123,055✔
330

331
  pStmt->sql.siInfo.pQuery = pStmt->sql.pQuery;
121,276✔
332

333
  pStmt->bInfo.needParse = false;
121,317✔
334

335
  if (pStmt->sql.type == 0) {
121,250✔
336
    if (pStmt->sql.pQuery->pRoot && LEGAL_INSERT(nodeType(pStmt->sql.pQuery->pRoot))) {
106,777✔
337
      pStmt->sql.type = STMT_TYPE_INSERT;
104,011✔
338
      pStmt->sql.stbInterlaceMode = false;
103,985✔
339
    } else if (pStmt->sql.pQuery->pPrepareRoot && LEGAL_SELECT(nodeType(pStmt->sql.pQuery->pPrepareRoot))) {
2,792✔
340
      pStmt->sql.type = STMT_TYPE_QUERY;
2,792✔
341
      pStmt->sql.stbInterlaceMode = false;
2,792✔
342

343
      return TSDB_CODE_SUCCESS;
2,792✔
344
    } else {
345
      STMT2_ELOG_E("only support select or insert sql");
×
346
      if (pStmt->exec.pRequest->msgBuf) {
×
347
        tstrncpy(pStmt->exec.pRequest->msgBuf, "stmt only support select or insert", pStmt->exec.pRequest->msgBufLen);
×
348
      }
349
      return TSDB_CODE_PAR_SYNTAX_ERROR;
×
350
    }
351
  } else if (pStmt->sql.type == STMT_TYPE_QUERY) {
14,473✔
352
    pStmt->sql.stbInterlaceMode = false;
×
353
    return TSDB_CODE_SUCCESS;
×
354
  } else if (pStmt->sql.type == STMT_TYPE_INSERT) {
14,473✔
355
    pStmt->sql.stbInterlaceMode = false;
×
356
  }
357

358
  STableDataCxt** pSrc =
359
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
118,484✔
360
  if (NULL == pSrc || NULL == *pSrc) {
118,532✔
361
    STMT2_ELOG("fail to get exec.pBlockHash, maybe parse failed, tbFName:%s", pStmt->bInfo.tbFName);
×
362
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
363
  }
364

365
  STableDataCxt* pTableCtx = *pSrc;
118,491✔
366
  if (pStmt->sql.stbInterlaceMode && pTableCtx->pData->pCreateTbReq && (pStmt->bInfo.tbNameFlag & USING_CLAUSE) == 0) {
118,532✔
367
    STMT2_TLOG("destroy pCreateTbReq for no-using insert, tbFName:%s", pStmt->bInfo.tbFName);
1,728✔
368
    tdDestroySVCreateTbReq(pTableCtx->pData->pCreateTbReq);
1,728✔
369
    taosMemoryFreeClear(pTableCtx->pData->pCreateTbReq);
1,728✔
370
    pTableCtx->pData->pCreateTbReq = NULL;
1,728✔
371
  }
372
  // if (pStmt->sql.stbInterlaceMode) {
373
  //   int16_t lastIdx = -1;
374

375
  //   for (int32_t i = 0; i < pTableCtx->boundColsInfo.numOfBound; ++i) {
376
  //     if (pTableCtx->boundColsInfo.pColIndex[i] < lastIdx) {
377
  //       pStmt->sql.stbInterlaceMode = false;
378
  //       break;
379
  //     }
380

381
  //     lastIdx = pTableCtx->boundColsInfo.pColIndex[i];
382
  //   }
383
  // }
384

385
  if (NULL == pStmt->sql.pBindInfo) {
118,573✔
386
    pStmt->sql.pBindInfo = taosMemoryMalloc(pTableCtx->boundColsInfo.numOfBound * sizeof(*pStmt->sql.pBindInfo));
116,523✔
387
    if (NULL == pStmt->sql.pBindInfo) {
116,698✔
388
      STMT2_ELOG_E("fail to malloc pBindInfo");
×
389
      return terrno;
×
390
    }
391
  }
392

393
  return TSDB_CODE_SUCCESS;
118,495✔
394
}
395

396
static int32_t stmtPrintBindv(TAOS_STMT2* stmt, TAOS_STMT2_BIND* bindv, int32_t col_idx, bool isTags) {
×
397
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
398
  int32_t    count = 0;
×
399
  int32_t    code = 0;
×
400

401
  if (bindv == NULL) {
×
402
    STMT2_TLOG("bindv is NULL, col_idx:%d, isTags:%d", col_idx, isTags);
×
403
    return TSDB_CODE_SUCCESS;
×
404
  }
405

406
  if (col_idx >= 0) {
×
407
    count = 1;
×
408
    STMT2_TLOG("single col bind, col_idx:%d", col_idx);
×
409
  } else {
410
    if (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type ||
×
411
        (pStmt->sql.type == 0 && stmt2IsInsert(stmt))) {
×
412
      if (pStmt->sql.placeholderOfTags == 0 && pStmt->sql.placeholderOfCols == 0) {
×
413
        code = stmtGetStbColFields2(pStmt, NULL, NULL);
×
414
        if (code != TSDB_CODE_SUCCESS) {
×
415
          return code;
×
416
        }
417
      }
418
      if (isTags) {
×
419
        count = pStmt->sql.placeholderOfTags;
×
420
        STMT2_TLOG("print tags bindv, cols:%d", count);
×
421
      } else {
422
        count = pStmt->sql.placeholderOfCols;
×
423
        STMT2_TLOG("print cols bindv, cols:%d", count);
×
424
      }
425
    } else if (STMT_TYPE_QUERY == pStmt->sql.type || (pStmt->sql.type == 0 && stmt2IsSelect(stmt))) {
×
426
      count = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
×
427
      STMT2_TLOG("print query bindv, cols:%d", count);
×
428
    }
429
  }
430

431
  if (code != TSDB_CODE_SUCCESS) {
×
432
    STMT2_ELOG("failed to get param count, code:%d", code);
×
433
    return code;
×
434
  }
435

436
  for (int i = 0; i < count; i++) {
×
437
    int32_t type = bindv[i].buffer_type;
×
438
    int32_t num = bindv[i].num;
×
439
    char*   current_buf = (char*)bindv[i].buffer;
×
440

441
    for (int j = 0; j < num; j++) {
×
442
      char    buf[256] = {0};
×
443
      int32_t len = 0;
×
444
      bool    isNull = (bindv[i].is_null && bindv[i].is_null[j]);
×
445

446
      if (IS_VAR_DATA_TYPE(type) && bindv[i].length) {
×
447
        len = bindv[i].length[j];
×
448
      } else {
449
        len = tDataTypes[type].bytes;
×
450
      }
451

452
      if (isNull) {
×
453
        snprintf(buf, sizeof(buf), "NULL");
×
454
      } else {
455
        if (current_buf == NULL) {
×
456
          snprintf(buf, sizeof(buf), "NULL(Buf)");
×
457
        } else {
458
          switch (type) {
×
459
            case TSDB_DATA_TYPE_BOOL:
×
460
              snprintf(buf, sizeof(buf), "%d", *(int8_t*)current_buf);
×
461
              break;
×
462
            case TSDB_DATA_TYPE_TINYINT:
×
463
              snprintf(buf, sizeof(buf), "%d", *(int8_t*)current_buf);
×
464
              break;
×
465
            case TSDB_DATA_TYPE_SMALLINT:
×
466
              snprintf(buf, sizeof(buf), "%d", *(int16_t*)current_buf);
×
467
              break;
×
468
            case TSDB_DATA_TYPE_INT:
×
469
              snprintf(buf, sizeof(buf), "%d", *(int32_t*)current_buf);
×
470
              break;
×
471
            case TSDB_DATA_TYPE_BIGINT:
×
472
              snprintf(buf, sizeof(buf), "%" PRId64, *(int64_t*)current_buf);
×
473
              break;
×
474
            case TSDB_DATA_TYPE_FLOAT:
×
475
              snprintf(buf, sizeof(buf), "%f", *(float*)current_buf);
×
476
              break;
×
477
            case TSDB_DATA_TYPE_DOUBLE:
×
478
              snprintf(buf, sizeof(buf), "%f", *(double*)current_buf);
×
479
              break;
×
480
            case TSDB_DATA_TYPE_BINARY:
×
481
            case TSDB_DATA_TYPE_NCHAR:
482
            case TSDB_DATA_TYPE_GEOMETRY:
483
            case TSDB_DATA_TYPE_VARBINARY:
484
              snprintf(buf, sizeof(buf), "len:%d, val:%.*s", len, len, current_buf);
×
485
              break;
×
486
            case TSDB_DATA_TYPE_TIMESTAMP:
×
487
              snprintf(buf, sizeof(buf), "%" PRId64, *(int64_t*)current_buf);
×
488
              break;
×
489
            case TSDB_DATA_TYPE_UTINYINT:
×
490
              snprintf(buf, sizeof(buf), "%u", *(uint8_t*)current_buf);
×
491
              break;
×
492
            case TSDB_DATA_TYPE_USMALLINT:
×
493
              snprintf(buf, sizeof(buf), "%u", *(uint16_t*)current_buf);
×
494
              break;
×
495
            case TSDB_DATA_TYPE_UINT:
×
496
              snprintf(buf, sizeof(buf), "%u", *(uint32_t*)current_buf);
×
497
              break;
×
498
            case TSDB_DATA_TYPE_UBIGINT:
×
499
              snprintf(buf, sizeof(buf), "%" PRIu64, *(uint64_t*)current_buf);
×
500
              break;
×
501
            default:
×
502
              snprintf(buf, sizeof(buf), "UnknownType:%d", type);
×
503
              break;
×
504
          }
505
        }
506
      }
507

508
      STMT2_TLOG("bindv[%d] row[%d]: type:%s, val:%s", i, j, tDataTypes[type].name, buf);
×
509

510
      if (!isNull && current_buf) {
×
511
        current_buf += len;
×
512
      }
513
    }
514
  }
515

516
  return TSDB_CODE_SUCCESS;
×
517
}
518

519
static void resetRequest(STscStmt2* pStmt) {
232,793✔
520
  if (pStmt->exec.pRequest) {
232,793✔
521
    taos_free_result(pStmt->exec.pRequest);
125,556✔
522
    pStmt->exec.pRequest = NULL;
125,556✔
523
  }
524
  pStmt->asyncResultAvailable = false;
232,793✔
525
}
232,793✔
526

527
static int32_t stmtCleanBindInfo(STscStmt2* pStmt) {
738,459✔
528
  pStmt->bInfo.tbUid = 0;
738,459✔
529
  pStmt->bInfo.tbVgId = -1;
738,459✔
530
  pStmt->bInfo.tbType = 0;
738,284✔
531
  pStmt->bInfo.needParse = true;
738,496✔
532
  pStmt->bInfo.inExecCache = false;
738,566✔
533

534
  pStmt->bInfo.tbName[0] = 0;
738,319✔
535
  pStmt->bInfo.tbFName[0] = 0;
738,389✔
536
  if (!pStmt->bInfo.tagsCached) {
738,177✔
537
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
518,328✔
538
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
518,761✔
539
    pStmt->bInfo.boundTags = NULL;
518,835✔
540
  }
541

542
  if (!pStmt->bInfo.boundColsCached) {
738,404✔
543
    tSimpleHashCleanup(pStmt->bInfo.fixedValueCols);
249,813✔
544
    pStmt->bInfo.fixedValueCols = NULL;
249,813✔
545
  }
546

547
  if (!pStmt->sql.autoCreateTbl) {
738,509✔
548
    pStmt->bInfo.stbFName[0] = 0;
509,927✔
549
    pStmt->bInfo.tbSuid = 0;
509,927✔
550
  }
551

552
  STMT2_TLOG("finish clean bind info, tagsCached:%d, autoCreateTbl:%d", pStmt->bInfo.tagsCached,
738,439✔
553
             pStmt->sql.autoCreateTbl);
554

555
  return TSDB_CODE_SUCCESS;
738,440✔
556
}
557

558
static void stmtFreeTableBlkList(STableColsData* pTb) {
×
559
  (void)qResetStmtColumns(pTb->aCol, true);
×
560
  taosArrayDestroy(pTb->aCol);
×
561
}
×
562

563
static void stmtResetQueueTableBuf(STableBufInfo* pTblBuf, SStmtQueue* pQueue) {
488,597✔
564
  pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, 0);
488,597✔
565
  if (NULL == pTblBuf->pCurBuff) {
488,936✔
566
    tscError("QInfo:%p, fail to get buffer from list", pTblBuf);
130✔
567
    return;
×
568
  }
569
  pTblBuf->buffIdx = 1;
488,806✔
570
  pTblBuf->buffOffset = sizeof(*pQueue->head);
488,806✔
571

572
  pQueue->head = pQueue->tail = pTblBuf->pCurBuff;
488,806✔
573
  pQueue->qRemainNum = 0;
488,736✔
574
  pQueue->head->next = NULL;
488,736✔
575
}
576

577
static int32_t stmtCleanExecInfo(STscStmt2* pStmt, bool keepTable, bool deepClean) {
724,121✔
578
  if (pStmt->sql.stbInterlaceMode) {
724,121✔
579
    if (deepClean) {
497,476✔
580
      taosHashCleanup(pStmt->exec.pBlockHash);
8,771✔
581
      pStmt->exec.pBlockHash = NULL;
8,771✔
582

583
      if (NULL != pStmt->exec.pCurrBlock) {
8,771✔
584
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->boundColsInfo.pColIndex);
7,283✔
585
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->pData);
7,283✔
586
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
7,283✔
587
        pStmt->exec.pCurrBlock = NULL;
7,283✔
588
      }
589
      if (STMT_TYPE_QUERY != pStmt->sql.type) {
8,771✔
590
        resetRequest(pStmt);
8,771✔
591
      }
592
    } else {
593
      pStmt->sql.siInfo.pTableColsIdx = 0;
488,705✔
594
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
488,734✔
595
      tSimpleHashClear(pStmt->sql.siInfo.pTableRowDataHash);
488,609✔
596
    }
597
    if (NULL != pStmt->exec.pRequest) {
497,694✔
598
      pStmt->exec.pRequest->body.resInfo.numOfRows = 0;
488,923✔
599
    }
600
  } else {
601
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
226,732✔
602
      resetRequest(pStmt);
223,041✔
603
    }
604

605
    size_t keyLen = 0;
226,814✔
606
    void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
226,814✔
607
    while (pIter) {
450,001✔
608
      STableDataCxt* pBlocks = *(STableDataCxt**)pIter;
223,187✔
609
      char*          key = taosHashGetKey(pIter, &keyLen);
223,187✔
610
      STableMeta*    pMeta = qGetTableMetaInDataBlock(pBlocks);
223,187✔
611

612
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
223,187✔
613
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
109,097✔
614
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
127,621✔
615

616
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
109,097✔
617
        continue;
109,097✔
618
      }
619

620
      qDestroyStmtDataBlock(pBlocks);
114,090✔
621
      STMT_ERR_RET(taosHashRemove(pStmt->exec.pBlockHash, key, keyLen));
114,090✔
622

623
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
114,090✔
624
    }
625

626
    if (keepTable) {
226,814✔
627
      STMT2_TLOG("finish clean exec info, stbInterlaceMode:%d, keepTable:%d, deepClean:%d", pStmt->sql.stbInterlaceMode,
112,753✔
628
                 keepTable, deepClean);
629
      return TSDB_CODE_SUCCESS;
112,753✔
630
    }
631

632
    taosHashCleanup(pStmt->exec.pBlockHash);
114,061✔
633
    pStmt->exec.pBlockHash = NULL;
114,061✔
634

635
    tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
114,061✔
636
    taosMemoryFreeClear(pStmt->exec.pCurrTbData);
114,061✔
637
  }
638

639
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
611,755✔
640
  STMT2_TLOG("finish clean exec info, stbInterlaceMode:%d, keepTable:%d, deepClean:%d", pStmt->sql.stbInterlaceMode,
611,295✔
641
             keepTable, deepClean);
642

643
  return TSDB_CODE_SUCCESS;
611,428✔
644
}
645

646
static void stmtFreeTbBuf(void* buf) {
114,643✔
647
  void* pBuf = *(void**)buf;
114,643✔
648
  taosMemoryFree(pBuf);
114,643✔
649
}
114,643✔
650

651
static void stmtFreeTbCols(void* buf) {
7,283,000✔
652
  SArray* pCols = *(SArray**)buf;
7,283,000✔
653
  taosArrayDestroy(pCols);
7,283,000✔
654
}
7,283,000✔
655

656
static int32_t stmtCleanSQLInfo(STscStmt2* pStmt) {
122,832✔
657
  STMT2_TLOG_E("start to free SQL info");
122,832✔
658

659
  taosMemoryFree(pStmt->sql.pBindInfo);
122,832✔
660
  taosMemoryFree(pStmt->sql.queryRes.fields);
122,832✔
661
  taosMemoryFree(pStmt->sql.queryRes.userFields);
122,832✔
662
  taosMemoryFree(pStmt->sql.sqlStr);
122,832✔
663
  qDestroyQuery(pStmt->sql.pQuery);
122,832✔
664
  taosArrayDestroy(pStmt->sql.nodeList);
122,832✔
665
  taosHashCleanup(pStmt->sql.pVgHash);
122,832✔
666
  pStmt->sql.pVgHash = NULL;
122,832✔
667
  if (pStmt->sql.fixValueTags) {
122,832✔
668
    pStmt->sql.fixValueTags = false;
1,710✔
669
    tdDestroySVCreateTbReq(pStmt->sql.fixValueTbReq);
1,710✔
670
    taosMemoryFreeClear(pStmt->sql.fixValueTbReq);
1,710✔
671
    pStmt->sql.fixValueTbReq = NULL;
1,710✔
672
  }
673

674
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
122,832✔
675
  while (pIter) {
125,733✔
676
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
2,901✔
677

678
    qDestroyStmtDataBlock(pCache->pDataCtx);
2,901✔
679
    qDestroyBoundColInfo(pCache->boundTags);
2,901✔
680
    taosMemoryFreeClear(pCache->boundTags);
2,901✔
681

682
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
2,901✔
683
  }
684
  taosHashCleanup(pStmt->sql.pTableCache);
122,832✔
685
  pStmt->sql.pTableCache = NULL;
122,620✔
686

687
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
122,620✔
688
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
122,832✔
689

690
  taos_free_result(pStmt->sql.siInfo.pRequest);
122,832✔
691
  taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
122,832✔
692
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
122,832✔
693
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableRowDataHash);
122,832✔
694
  taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
122,832✔
695
  taosMemoryFree(pStmt->sql.siInfo.pTSchema);
122,832✔
696
  qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
122,832✔
697
  taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
122,832✔
698
  pStmt->sql.siInfo.pTableCols = NULL;
122,832✔
699

700
  (void)memset(&pStmt->sql, 0, sizeof(pStmt->sql));
122,832✔
701
  pStmt->sql.siInfo.tableColsReady = true;
122,832✔
702

703
  STMT2_TLOG_E("end to free SQL info");
122,832✔
704

705
  return TSDB_CODE_SUCCESS;
122,832✔
706
}
707

708
static int32_t stmtTryAddTableVgroupInfo(STscStmt2* pStmt, int32_t* vgId) {
646,072✔
709
  if (*vgId >= 0 && taosHashGet(pStmt->sql.pVgHash, (const char*)vgId, sizeof(*vgId))) {
646,072✔
710
    return TSDB_CODE_SUCCESS;
960✔
711
  }
712

713
  SVgroupInfo      vgInfo = {0};
645,112✔
714
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
645,112✔
715
                           .requestId = pStmt->exec.pRequest->requestId,
645,112✔
716
                           .requestObjRefId = pStmt->exec.pRequest->self,
645,112✔
717
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
645,112✔
718

719
  int32_t code = catalogGetTableHashVgroup(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &vgInfo);
645,398✔
720
  if (TSDB_CODE_SUCCESS != code) {
645,307✔
721
    STMT2_ELOG("fail to get vgroup info from catalog, code:%d", code);
×
722
    return code;
×
723
  }
724

725
  code =
726
      taosHashPut(pStmt->sql.pVgHash, (const char*)&vgInfo.vgId, sizeof(vgInfo.vgId), (char*)&vgInfo, sizeof(vgInfo));
645,307✔
727
  if (TSDB_CODE_SUCCESS != code) {
645,294✔
728
    STMT2_ELOG("fail to put vgroup info, code:%d", code);
×
729
    return code;
×
730
  }
731

732
  *vgId = vgInfo.vgId;
645,320✔
733

734
  return TSDB_CODE_SUCCESS;
645,320✔
735
}
736

737
int32_t stmtGetTableMetaAndValidate(STscStmt2* pStmt, uint64_t* uid, uint64_t* suid, int32_t* vgId, int8_t* tableType) {
5,526✔
738
  STableMeta*      pTableMeta = NULL;
5,526✔
739
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
5,611✔
740
                           .requestId = pStmt->exec.pRequest->requestId,
5,611✔
741
                           .requestObjRefId = pStmt->exec.pRequest->self,
5,570✔
742
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
5,570✔
743
  int32_t          code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
5,622✔
744

745
  pStmt->stat.ctgGetTbMetaNum++;
5,581✔
746

747
  if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
5,622✔
748
    STMT2_ELOG("tb %s not exist", pStmt->bInfo.tbFName);
480✔
749
    (void)stmtCleanBindInfo(pStmt);
480✔
750

751
    if (!pStmt->sql.autoCreateTbl) {
480✔
752
      STMT2_ELOG("table %s does not exist and autoCreateTbl is disabled", pStmt->bInfo.tbFName);
480✔
753
      STMT_ERR_RET(TSDB_CODE_PAR_TABLE_NOT_EXIST);
480✔
754
    }
755

756
    STMT_ERR_RET(code);
×
757
  }
758

759
  STMT_ERR_RET(code);
5,090✔
760

761
  *uid = pTableMeta->uid;
5,090✔
762
  *suid = pTableMeta->suid;
5,090✔
763
  *tableType = pTableMeta->tableType;
5,090✔
764
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
5,090✔
765
  *vgId = pTableMeta->vgId;
5,090✔
766

767
  taosMemoryFree(pTableMeta);
5,090✔
768

769
  return TSDB_CODE_SUCCESS;
5,131✔
770
}
771

772
static int32_t stmtRebuildDataBlock(STscStmt2* pStmt, STableDataCxt* pDataBlock, STableDataCxt** newBlock, uint64_t uid,
7,045✔
773
                                    uint64_t suid, int32_t vgId) {
774
  STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
7,045✔
775
  STMT_ERR_RET(qRebuildStmtDataBlock(newBlock, pDataBlock, uid, suid, vgId, pStmt->sql.autoCreateTbl));
7,045✔
776

777
  STMT2_DLOG("uid:%" PRId64 ", rebuild table data context, vgId:%d", uid, vgId);
7,045✔
778

779
  return TSDB_CODE_SUCCESS;
7,045✔
780
}
781

782
static int32_t stmtGetFromCache(STscStmt2* pStmt) {
21,220✔
783
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx) {
21,220✔
784
    pStmt->bInfo.needParse = false;
×
785
    pStmt->bInfo.inExecCache = false;
×
786
    return TSDB_CODE_SUCCESS;
×
787
  }
788

789
  pStmt->bInfo.needParse = true;
21,220✔
790
  pStmt->bInfo.inExecCache = false;
21,182✔
791

792
  STableDataCxt** pCxtInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
21,182✔
793
  if (pCxtInExec) {
21,261✔
794
    pStmt->bInfo.needParse = false;
3,264✔
795
    pStmt->bInfo.inExecCache = true;
3,264✔
796

797
    pStmt->exec.pCurrBlock = *pCxtInExec;
3,264✔
798

799
    if (pStmt->sql.autoCreateTbl) {
3,264✔
800
      STMT2_DLOG("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
2,880✔
801
      return TSDB_CODE_SUCCESS;
2,880✔
802
    }
803
  }
804

805
  if (NULL == pStmt->pCatalog) {
18,381✔
806
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
8,415✔
807
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
8,374✔
808
  }
809

810
  if (NULL == pStmt->sql.pTableCache || taosHashGetSize(pStmt->sql.pTableCache) <= 0) {
18,302✔
811
    if (pStmt->bInfo.inExecCache) {
10,818✔
812
      pStmt->bInfo.needParse = false;
×
813
      STMT2_DLOG("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
×
814
      return TSDB_CODE_SUCCESS;
×
815
    }
816

817
    STMT2_DLOG("no stmt block cache for tb %s", pStmt->bInfo.tbFName);
10,856✔
818

819
    return TSDB_CODE_SUCCESS;
10,815✔
820
  }
821

822
  if (pStmt->sql.autoCreateTbl) {
7,525✔
823
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &pStmt->bInfo.tbSuid, sizeof(pStmt->bInfo.tbSuid));
5,893✔
824
    if (pCache) {
5,893✔
825
      pStmt->bInfo.needParse = false;
5,893✔
826
      pStmt->bInfo.tbUid = 0;
5,893✔
827

828
      STableDataCxt* pNewBlock = NULL;
5,893✔
829
      STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, 0, pStmt->bInfo.tbSuid, -1));
5,893✔
830

831
      if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
5,893✔
832
                      POINTER_BYTES)) {
833
        STMT_ERR_RET(terrno);
×
834
      }
835

836
      pStmt->exec.pCurrBlock = pNewBlock;
5,893✔
837

838
      STMT2_DLOG("reuse stmt block for tb %s in sqlBlock, suid:0x%" PRIx64, pStmt->bInfo.tbFName, pStmt->bInfo.tbSuid);
5,893✔
839

840
      return TSDB_CODE_SUCCESS;
5,893✔
841
    }
842

843
    STMT_RET(stmtCleanBindInfo(pStmt));
×
844
  }
845

UNCOV
846
  uint64_t uid, suid;
×
UNCOV
847
  int32_t  vgId;
×
UNCOV
848
  int8_t   tableType;
×
849

850
  STMT_ERR_RET(stmtGetTableMetaAndValidate(pStmt, &uid, &suid, &vgId, &tableType));
1,632✔
851

852
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
1,536✔
853

854
  if (uid == pStmt->bInfo.tbUid) {
1,536✔
855
    pStmt->bInfo.needParse = false;
×
856

857
    STMT2_DLOG("tb %s is current table", pStmt->bInfo.tbFName);
×
858

859
    return TSDB_CODE_SUCCESS;
×
860
  }
861

862
  if (pStmt->bInfo.inExecCache) {
1,536✔
863
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
384✔
864
    if (NULL == pCache) {
384✔
865
      STMT2_ELOG("table [%s, %" PRIx64 ", %" PRIx64 "] found in exec blockHash, but not in sql blockHash",
×
866
                 pStmt->bInfo.tbFName, uid, cacheUid);
867

868
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
869
    }
870

871
    pStmt->bInfo.needParse = false;
384✔
872

873
    pStmt->bInfo.tbUid = uid;
384✔
874
    pStmt->bInfo.tbSuid = suid;
384✔
875
    pStmt->bInfo.tbType = tableType;
384✔
876
    pStmt->bInfo.boundTags = pCache->boundTags;
384✔
877
    pStmt->bInfo.tagsCached = true;
384✔
878

879
    STMT2_DLOG("tb %s in execBlock list, set to current", pStmt->bInfo.tbFName);
384✔
880

881
    return TSDB_CODE_SUCCESS;
384✔
882
  }
883

884
  SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
1,152✔
885
  if (pCache) {
1,152✔
886
    pStmt->bInfo.needParse = false;
1,152✔
887

888
    pStmt->bInfo.tbUid = uid;
1,152✔
889
    pStmt->bInfo.tbSuid = suid;
1,152✔
890
    pStmt->bInfo.tbType = tableType;
1,152✔
891
    pStmt->bInfo.boundTags = pCache->boundTags;
1,152✔
892
    pStmt->bInfo.tagsCached = true;
1,152✔
893

894
    STableDataCxt* pNewBlock = NULL;
1,152✔
895
    STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, uid, suid, vgId));
1,152✔
896

897
    if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
1,152✔
898
                    POINTER_BYTES)) {
899
      STMT_ERR_RET(terrno);
×
900
    }
901

902
    pStmt->exec.pCurrBlock = pNewBlock;
1,152✔
903

904
    tscDebug("tb %s in sqlBlock list, set to current", pStmt->bInfo.tbFName);
1,152✔
905

906
    return TSDB_CODE_SUCCESS;
1,152✔
907
  }
908

909
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
910

911
  return TSDB_CODE_SUCCESS;
×
912
}
913

UNCOV
914
static int32_t stmtResetStmt(STscStmt2* pStmt) {
×
UNCOV
915
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
×
916

UNCOV
917
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
×
UNCOV
918
  if (NULL == pStmt->sql.pTableCache) {
×
919
    STMT2_ELOG("fail to allocate memory for pTableCache in stmtResetStmt:%s", tstrerror(terrno));
×
920
    STMT_ERR_RET(terrno);
×
921
  }
922

UNCOV
923
  pStmt->sql.status = STMT_INIT;
×
924

UNCOV
925
  return TSDB_CODE_SUCCESS;
×
926
}
927

928
static void stmtAsyncOutput(STscStmt2* pStmt, void* param) {
1,523,877✔
929
  SStmtQNode* pParam = (SStmtQNode*)param;
1,523,877✔
930

931
  if (pParam->restoreTbCols) {
1,523,877✔
932
    for (int32_t i = 0; i < pStmt->sql.siInfo.pTableColsIdx; ++i) {
1,523,687✔
933
      SArray** p = (SArray**)TARRAY_GET_ELEM(pStmt->sql.siInfo.pTableCols, i);
1,034,733✔
934
      *p = taosArrayInit(20, POINTER_BYTES);
1,034,733✔
935
      if (*p == NULL) {
1,034,589✔
936
        pStmt->errCode = terrno;
26✔
937
      }
938
    }
939
    atomic_store_8((int8_t*)&pStmt->sql.siInfo.tableColsReady, true);
488,954✔
940
    STMT2_TLOG_E("restore pTableCols finished");
489,019✔
941
  } else {
942
    int code = qAppendStmt2TableOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, &pParam->tblData, pStmt->exec.pCurrBlock,
1,034,899✔
943
                                       &pStmt->sql.siInfo, pParam->pCreateTbReq);
944
    // taosMemoryFree(pParam->pTbData);
945
    if (code != TSDB_CODE_SUCCESS) {
1,034,858✔
946
      STMT2_ELOG("async append stmt output failed, tbname:%s, err:%s", pParam->tblData.tbName, tstrerror(code));
96✔
947
      pStmt->errCode = code;
96✔
948
    }
949
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
1,034,858✔
950
  }
951
}
1,524,103✔
952

953
static void* stmtBindThreadFunc(void* param) {
114,798✔
954
  setThreadName("stmt2Bind");
114,798✔
955

956
  STscStmt2* pStmt = (STscStmt2*)param;
114,835✔
957
  STMT2_DLOG_E("stmt2 bind thread started");
114,835✔
958

959
  while (true) {
1,523,990✔
960
    SStmtQNode* asyncParam = NULL;
1,638,825✔
961

962
    if (!stmtDequeue(pStmt, &asyncParam)) {
1,638,790✔
963
      if (pStmt->queue.stopQueue && 0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
114,643✔
964
        STMT2_DLOG_E("queue is empty and stopQueue is set, thread will exit");
114,611✔
965
        break;
114,643✔
966
      }
967
      continue;
×
968
    }
969

970
    stmtAsyncOutput(pStmt, asyncParam);
1,523,903✔
971
  }
972

973
  STMT2_DLOG_E("stmt2 bind thread stopped");
114,643✔
974
  return NULL;
114,643✔
975
}
976

977
static int32_t stmtStartBindThread(STscStmt2* pStmt) {
114,623✔
978
  TdThreadAttr thAttr;
96,683✔
979
  if (taosThreadAttrInit(&thAttr) != 0) {
114,835✔
980
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
981
  }
982
  if (taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE) != 0) {
114,835✔
983
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
984
  }
985

986
  if (taosThreadCreate(&pStmt->bindThread, &thAttr, stmtBindThreadFunc, pStmt) != 0) {
114,835✔
987
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
988
    STMT_ERR_RET(terrno);
×
989
  }
990

991
  pStmt->bindThreadInUse = true;
114,835✔
992

993
  (void)taosThreadAttrDestroy(&thAttr);
114,835✔
994
  return TSDB_CODE_SUCCESS;
114,835✔
995
}
996

997
static int32_t stmtInitQueue(STscStmt2* pStmt) {
114,835✔
998
  (void)taosThreadCondInit(&pStmt->queue.waitCond, NULL);
114,835✔
999
  (void)taosThreadMutexInit(&pStmt->queue.mutex, NULL);
114,835✔
1000
  STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&pStmt->queue.head));
229,670✔
1001
  pStmt->queue.tail = pStmt->queue.head;
114,835✔
1002

1003
  return TSDB_CODE_SUCCESS;
114,835✔
1004
}
1005

1006
static int32_t stmtIniAsyncBind(STscStmt2* pStmt) {
122,544✔
1007
  (void)taosThreadCondInit(&pStmt->asyncBindParam.waitCond, NULL);
122,544✔
1008
  (void)taosThreadMutexInit(&pStmt->asyncBindParam.mutex, NULL);
122,544✔
1009
  pStmt->asyncBindParam.asyncBindNum = 0;
122,544✔
1010

1011
  return TSDB_CODE_SUCCESS;
122,544✔
1012
}
1013

1014
static int32_t stmtInitTableBuf(STableBufInfo* pTblBuf) {
114,835✔
1015
  pTblBuf->buffUnit = sizeof(SStmtQNode);
114,835✔
1016
  pTblBuf->buffSize = pTblBuf->buffUnit * 1000;
114,835✔
1017
  pTblBuf->pBufList = taosArrayInit(100, POINTER_BYTES);
114,835✔
1018
  if (NULL == pTblBuf->pBufList) {
114,835✔
1019
    return terrno;
×
1020
  }
1021
  void* buff = taosMemoryMalloc(pTblBuf->buffSize);
114,835✔
1022
  if (NULL == buff) {
114,835✔
1023
    return terrno;
×
1024
  }
1025

1026
  if (taosArrayPush(pTblBuf->pBufList, &buff) == NULL) {
229,670✔
1027
    return terrno;
×
1028
  }
1029

1030
  pTblBuf->pCurBuff = buff;
114,835✔
1031
  pTblBuf->buffIdx = 1;
114,835✔
1032
  pTblBuf->buffOffset = 0;
114,835✔
1033

1034
  return TSDB_CODE_SUCCESS;
114,835✔
1035
}
1036

1037
TAOS_STMT2* stmtInit2(STscObj* taos, TAOS_STMT2_OPTION* pOptions) {
119,880✔
1038
  STscObj*   pObj = (STscObj*)taos;
119,880✔
1039
  STscStmt2* pStmt = NULL;
119,880✔
1040
  int32_t    code = 0;
119,880✔
1041

1042
  pStmt = taosMemoryCalloc(1, sizeof(STscStmt2));
119,880✔
1043
  if (NULL == pStmt) {
119,911✔
1044
    STMT2_ELOG_E("fail to allocate memory for pStmt");
×
1045
    return NULL;
×
1046
  }
1047

1048
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
119,911✔
1049
  if (NULL == pStmt->sql.pTableCache) {
119,911✔
1050
    STMT2_ELOG("fail to allocate memory for pTableCache in stmtInit2:%s", tstrerror(terrno));
×
1051
    taosMemoryFree(pStmt);
×
1052
    return NULL;
×
1053
  }
1054

1055
  pStmt->taos = pObj;
119,911✔
1056
  if (taos->db[0] != '\0') {
119,911✔
1057
    pStmt->db = taosStrdup(taos->db);
114,631✔
1058
  }
1059
  pStmt->bInfo.needParse = true;
119,911✔
1060
  pStmt->sql.status = STMT_INIT;
119,911✔
1061
  pStmt->errCode = TSDB_CODE_SUCCESS;
119,911✔
1062

1063
  if (NULL != pOptions) {
119,911✔
1064
    (void)memcpy(&pStmt->options, pOptions, sizeof(pStmt->options));
119,890✔
1065
    if (pOptions->singleStbInsert && pOptions->singleTableBindOnce) {
119,890✔
1066
      pStmt->stbInterlaceMode = true;
112,202✔
1067
    }
1068

1069
    pStmt->reqid = pOptions->reqid;
119,890✔
1070
  }
1071

1072
  if (pStmt->stbInterlaceMode) {
119,911✔
1073
    pStmt->sql.siInfo.transport = taos->pAppInfo->pTransporter;
112,202✔
1074
    pStmt->sql.siInfo.acctId = taos->acctId;
112,202✔
1075
    pStmt->sql.siInfo.dbname = taos->db;
112,202✔
1076
    pStmt->sql.siInfo.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
112,202✔
1077

1078
    pStmt->sql.siInfo.pTableHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
112,202✔
1079
    if (NULL == pStmt->sql.siInfo.pTableHash) {
112,202✔
1080
      STMT2_ELOG("fail to allocate memory for pTableHash:%s", tstrerror(terrno));
×
1081
      (void)stmtClose2(pStmt);
×
1082
      return NULL;
×
1083
    }
1084

1085
    pStmt->sql.siInfo.pTableRowDataHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
112,202✔
1086
    if (NULL == pStmt->sql.siInfo.pTableRowDataHash) {
112,202✔
1087
      STMT2_ELOG("fail to allocate memory for pTableRowDataHash:%s", tstrerror(terrno));
×
1088
      (void)stmtClose2(pStmt);
×
1089
      return NULL;
×
1090
    }
1091

1092
    pStmt->sql.siInfo.pTableCols = taosArrayInit(STMT_TABLE_COLS_NUM, POINTER_BYTES);
112,202✔
1093
    if (NULL == pStmt->sql.siInfo.pTableCols) {
112,202✔
1094
      STMT2_ELOG("fail to allocate memory for pTableCols:%s", tstrerror(terrno));
×
1095
      (void)stmtClose2(pStmt);
×
1096
      return NULL;
×
1097
    }
1098

1099
    code = stmtInitTableBuf(&pStmt->sql.siInfo.tbBuf);
112,202✔
1100
    if (TSDB_CODE_SUCCESS == code) {
112,202✔
1101
      code = stmtInitQueue(pStmt);
112,202✔
1102
    }
1103
    if (TSDB_CODE_SUCCESS == code) {
112,202✔
1104
      code = stmtStartBindThread(pStmt);
112,202✔
1105
    }
1106
    if (TSDB_CODE_SUCCESS != code) {
112,202✔
1107
      terrno = code;
×
1108
      STMT2_ELOG("fail to init stmt2 bind thread:%s", tstrerror(code));
×
1109
      (void)stmtClose2(pStmt);
×
1110
      return NULL;
×
1111
    }
1112
  }
1113

1114
  pStmt->sql.siInfo.tableColsReady = true;
119,911✔
1115
  if (pStmt->options.asyncExecFn) {
119,911✔
1116
    if (tsem_init(&pStmt->asyncExecSem, 0, 1) != 0) {
1,056✔
1117
      terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
1118
      STMT2_ELOG("fail to init asyncExecSem:%s", tstrerror(terrno));
×
1119
      (void)stmtClose2(pStmt);
×
1120
      return NULL;
×
1121
    }
1122
  }
1123
  code = stmtIniAsyncBind(pStmt);
119,911✔
1124
  if (TSDB_CODE_SUCCESS != code) {
119,911✔
1125
    terrno = code;
×
1126
    STMT2_ELOG("fail to start init asyncExecSem:%s", tstrerror(code));
×
1127

1128
    (void)stmtClose2(pStmt);
×
1129
    return NULL;
×
1130
  }
1131

1132
  pStmt->execSemWaited = false;
119,911✔
1133

1134
  // STMT_LOG_SEQ(STMT_INIT);
1135

1136
  STMT2_DLOG("stmt2 initialize finished, seqId:%d, db:%s, interlaceMode:%d, asyncExec:%d", pStmt->seqId, pStmt->db,
119,911✔
1137
             pStmt->stbInterlaceMode, pStmt->options.asyncExecFn != NULL);
1138

1139
  return pStmt;
119,911✔
1140
}
1141

1142
static int stmtSetDbName2(TAOS_STMT2* stmt, const char* dbName) {
112,928✔
1143
  STscStmt2* pStmt = (STscStmt2*)stmt;
112,928✔
1144
  if (dbName == NULL || dbName[0] == '\0') {
112,928✔
1145
    STMT2_ELOG_E("dbname in sql is illegal");
×
1146
    return TSDB_CODE_TSC_SQL_SYNTAX_ERROR;
×
1147
  }
1148

1149
  STMT2_DLOG("dbname is specified in sql:%s", dbName);
112,928✔
1150
  if (pStmt->db == NULL || pStmt->db[0] == '\0') {
112,928✔
1151
    taosMemoryFreeClear(pStmt->db);
4,704✔
1152
    STMT2_DLOG("dbname:%s is by sql, not by taosconnect", dbName);
4,704✔
1153
    pStmt->db = taosStrdup(dbName);
4,704✔
1154
    (void)strdequote(pStmt->db);
4,704✔
1155
  }
1156
  STMT_ERR_RET(stmtCreateRequest(pStmt));
112,928✔
1157

1158
  // The SQL statement specifies a database name, overriding the previously specified database
1159
  taosMemoryFreeClear(pStmt->exec.pRequest->pDb);
112,897✔
1160
  pStmt->exec.pRequest->pDb = taosStrdup(dbName);
112,897✔
1161
  (void)strdequote(pStmt->exec.pRequest->pDb);
112,928✔
1162
  if (pStmt->exec.pRequest->pDb == NULL) {
112,897✔
1163
    return terrno;
×
1164
  }
1165
  if (pStmt->sql.stbInterlaceMode) {
112,897✔
1166
    pStmt->sql.siInfo.dbname = pStmt->exec.pRequest->pDb;
107,456✔
1167
  }
1168
  return TSDB_CODE_SUCCESS;
112,897✔
1169
}
1170
static int32_t stmtResetStbInterlaceCache(STscStmt2* pStmt) {
2,633✔
1171
  int32_t code = TSDB_CODE_SUCCESS;
2,633✔
1172

1173
  pStmt->sql.siInfo.pTableHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
2,633✔
1174
  if (NULL == pStmt->sql.siInfo.pTableHash) {
2,633✔
1175
    return terrno;
×
1176
  }
1177

1178
  pStmt->sql.siInfo.pTableRowDataHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
2,633✔
1179
  if (NULL == pStmt->sql.siInfo.pTableRowDataHash) {
2,633✔
1180
    return terrno;
×
1181
  }
1182

1183
  pStmt->sql.siInfo.pTableCols = taosArrayInit(STMT_TABLE_COLS_NUM, POINTER_BYTES);
2,633✔
1184
  if (NULL == pStmt->sql.siInfo.pTableCols) {
2,633✔
1185
    return terrno;
×
1186
  }
1187

1188
  code = stmtInitTableBuf(&pStmt->sql.siInfo.tbBuf);
2,633✔
1189

1190
  if (TSDB_CODE_SUCCESS == code) {
2,633✔
1191
    code = stmtInitQueue(pStmt);
2,633✔
1192
    pStmt->queue.stopQueue = false;
2,633✔
1193
  }
1194
  if (TSDB_CODE_SUCCESS == code) {
2,633✔
1195
    code = stmtStartBindThread(pStmt);
2,633✔
1196
  }
1197
  if (TSDB_CODE_SUCCESS != code) {
2,633✔
1198
    return code;
×
1199
  }
1200

1201
  return TSDB_CODE_SUCCESS;
2,633✔
1202
}
1203

1204
static int32_t stmtDeepReset(STscStmt2* pStmt) {
3,401✔
1205
  // Save state that needs to be preserved
1206
  char*             db = pStmt->db;
3,401✔
1207
  TAOS_STMT2_OPTION options = pStmt->options;
3,401✔
1208
  uint32_t          reqid = pStmt->reqid;
3,401✔
1209
  bool              stbInterlaceMode = pStmt->stbInterlaceMode;
3,401✔
1210

1211
  pStmt->errCode = 0;
3,401✔
1212

1213
  // Wait for async execution to complete
1214
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
3,401✔
1215
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
192✔
1216
      STMT2_ELOG_E("bind param wait asyncExecSem failed");
×
1217
    }
1218
    pStmt->execSemWaited = true;
192✔
1219
  }
1220

1221
  // Stop bind thread if in use (similar to stmtClose2)
1222
  if (stbInterlaceMode && pStmt->bindThreadInUse) {
3,401✔
1223
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
2,633✔
1224
      taosUsleep(1);
×
1225
    }
1226
    (void)taosThreadMutexLock(&pStmt->queue.mutex);
2,633✔
1227
    pStmt->queue.stopQueue = true;
2,633✔
1228
    (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
2,633✔
1229
    (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
2,633✔
1230

1231
    (void)taosThreadJoin(pStmt->bindThread, NULL);
2,633✔
1232
    pStmt->bindThreadInUse = false;
2,633✔
1233
    pStmt->queue.head = NULL;
2,633✔
1234
    pStmt->queue.tail = NULL;
2,633✔
1235
    pStmt->queue.qRemainNum = 0;
2,633✔
1236

1237
    (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
2,633✔
1238
    (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
2,633✔
1239
  }
1240

1241
  // Clean all SQL and execution info (stmtCleanSQLInfo already handles most cleanup)
1242
  pStmt->bInfo.boundColsCached = false;
3,401✔
1243
  if (stbInterlaceMode) {
3,401✔
1244
    pStmt->bInfo.tagsCached = false;
2,633✔
1245
  }
1246
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
3,401✔
1247

1248
  // Reinitialize resources (similar to stmtInit2)
1249
  if (stbInterlaceMode) {
3,401✔
1250
    pStmt->sql.siInfo.transport = pStmt->taos->pAppInfo->pTransporter;
2,633✔
1251
    pStmt->sql.siInfo.acctId = pStmt->taos->acctId;
2,633✔
1252
    pStmt->sql.siInfo.dbname = pStmt->taos->db;
2,633✔
1253
    pStmt->sql.siInfo.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
2,633✔
1254

1255
    if (NULL == pStmt->pCatalog) {
2,633✔
1256
      STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
288✔
1257
    }
1258
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
2,633✔
1259

1260
    STMT_ERR_RET(stmtResetStbInterlaceCache(pStmt));
2,633✔
1261

1262
    int32_t code = stmtIniAsyncBind(pStmt);
2,633✔
1263
    if (TSDB_CODE_SUCCESS != code) {
2,633✔
1264
      STMT2_ELOG("fail to reinit async bind in stmtDeepReset:%s", tstrerror(code));
×
1265
      return code;
×
1266
    }
1267
  }
1268

1269
  // Restore preserved state
1270
  pStmt->db = db;
3,401✔
1271
  pStmt->options = options;
3,401✔
1272
  pStmt->reqid = reqid;
3,401✔
1273
  pStmt->stbInterlaceMode = stbInterlaceMode;
3,401✔
1274

1275
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
3,401✔
1276
  if (NULL == pStmt->sql.pTableCache) {
3,401✔
1277
    STMT2_ELOG("fail to allocate memory for pTableCache in stmtDeepReset:%s", tstrerror(terrno));
×
1278
    return terrno;
×
1279
  }
1280

1281
  pStmt->bInfo.needParse = true;
3,401✔
1282
  pStmt->sql.status = STMT_INIT;
3,401✔
1283
  pStmt->sql.siInfo.tableColsReady = true;
3,401✔
1284

1285
  return TSDB_CODE_SUCCESS;
3,401✔
1286
}
1287

1288
int stmtPrepare2(TAOS_STMT2* stmt, const char* sql, unsigned long length) {
123,004✔
1289
  STscStmt2* pStmt = (STscStmt2*)stmt;
123,004✔
1290
  int32_t    code = 0;
123,004✔
1291

1292
  STMT2_DLOG("start to prepare with sql:%s", sql);
123,004✔
1293

1294
  if (stmt == NULL || sql == NULL) {
123,004✔
1295
    STMT2_ELOG_E("stmt or sql is NULL");
×
1296
    return TSDB_CODE_INVALID_PARA;
×
1297
  }
1298

1299
  if (pStmt->sql.status >= STMT_PREPARE) {
123,079✔
1300
    STMT2_DLOG("stmt status is %d, need to reset stmt2 cache before prepare", pStmt->sql.status);
3,401✔
1301
    STMT_ERR_RET(stmtDeepReset(pStmt));
3,401✔
1302
  }
1303

1304
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
123,120✔
1305
    STMT2_ELOG("errCode is not success before, ErrCode: 0x%x, errorsyt: %s\n. ", pStmt->errCode,
96✔
1306
               tstrerror(pStmt->errCode));
1307
    return pStmt->errCode;
96✔
1308
  }
1309

1310
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
123,024✔
1311

1312
  if (length <= 0) {
122,983✔
1313
    length = strlen(sql);
120,136✔
1314
  }
1315
  pStmt->sql.sqlStr = taosStrndup(sql, length);
122,983✔
1316
  if (!pStmt->sql.sqlStr) {
123,024✔
1317
    STMT2_ELOG("fail to allocate memory for sqlStr:%s", tstrerror(terrno));
×
1318
    STMT_ERR_RET(terrno);
×
1319
  }
1320
  pStmt->sql.sqlLen = length;
123,024✔
1321
  STMT_ERR_RET(stmtCreateRequest(pStmt));
123,024✔
1322

1323
  if (stmt2IsInsert(pStmt)) {
123,024✔
1324
    pStmt->sql.stbInterlaceMode = pStmt->stbInterlaceMode;
119,807✔
1325
    char* dbName = NULL;
119,807✔
1326
    if (qParseDbName(sql, length, &dbName)) {
119,807✔
1327
      STMT_ERR_RET(stmtSetDbName2(stmt, dbName));
112,928✔
1328
      taosMemoryFreeClear(dbName);
112,897✔
1329
    } else if (pStmt->db != NULL && pStmt->db[0] != '\0') {
6,879✔
1330
      taosMemoryFreeClear(pStmt->exec.pRequest->pDb);
6,824✔
1331
      pStmt->exec.pRequest->pDb = taosStrdup(pStmt->db);
6,824✔
1332
      if (pStmt->exec.pRequest->pDb == NULL) {
6,783✔
1333
        STMT_ERR_RET(terrno);
×
1334
      }
1335
      (void)strdequote(pStmt->exec.pRequest->pDb);
6,783✔
1336

1337
      if (pStmt->sql.stbInterlaceMode) {
6,783✔
1338
        pStmt->sql.siInfo.dbname = pStmt->exec.pRequest->pDb;
4,403✔
1339
      }
1340
    }
1341

1342
  } else if (stmt2IsSelect(pStmt)) {
3,176✔
1343
    pStmt->sql.stbInterlaceMode = false;
2,888✔
1344
    STMT_ERR_RET(stmtParseSql(pStmt));
2,888✔
1345
  } else {
1346
    return stmtBuildErrorMsgWithCode(pStmt, "stmt only support 'SELECT' or 'INSERT'", TSDB_CODE_PAR_SYNTAX_ERROR);
288✔
1347
  }
1348
  return TSDB_CODE_SUCCESS;
122,568✔
1349
}
1350

1351
static int32_t stmtInitStbInterlaceTableInfo(STscStmt2* pStmt) {
7,231✔
1352
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
7,231✔
1353
  if (!pSrc) {
7,242✔
1354
    return terrno;
×
1355
  }
1356
  STableDataCxt* pDst = NULL;
7,242✔
1357

1358
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
7,242✔
1359
  pStmt->sql.siInfo.pDataCtx = pDst;
7,272✔
1360

1361
  SArray* pTblCols = NULL;
7,272✔
1362
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
7,278,522✔
1363
    pTblCols = taosArrayInit(20, POINTER_BYTES);
7,271,274✔
1364
    if (NULL == pTblCols) {
7,265,284✔
1365
      return terrno;
×
1366
    }
1367

1368
    if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
14,537,037✔
1369
      return terrno;
×
1370
    }
1371
  }
1372

1373
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
7,248✔
1374

1375
  STMT2_TLOG("init stb interlace table info, tbName:%s, pDataCtx:%p, boundTags:%p", pStmt->bInfo.tbFName,
7,248✔
1376
             pStmt->sql.siInfo.pDataCtx, pStmt->sql.siInfo.boundTags);
1377

1378
  return TSDB_CODE_SUCCESS;
7,283✔
1379
}
1380

1381
bool stmt2IsInsert(TAOS_STMT2* stmt) {
1,178,476✔
1382
  STscStmt2* pStmt = (STscStmt2*)stmt;
1,178,476✔
1383
  if (pStmt->sql.type) {
1,178,476✔
1384
    return (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
1,040,331✔
1385
  }
1386

1387
  return qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
138,224✔
1388
}
1389

1390
bool stmt2IsSelect(TAOS_STMT2* stmt) {
115,714✔
1391
  STscStmt2* pStmt = (STscStmt2*)stmt;
115,714✔
1392

1393
  if (pStmt->sql.type) {
115,714✔
1394
    return STMT_TYPE_QUERY == pStmt->sql.type;
×
1395
  }
1396
  return qIsSelectFromSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
115,755✔
1397
}
1398

1399
int stmtSetTbName2(TAOS_STMT2* stmt, const char* tbName) {
1,049,264✔
1400
  STscStmt2* pStmt = (STscStmt2*)stmt;
1,049,264✔
1401

1402
  int64_t startUs = taosGetTimestampUs();
1,049,350✔
1403

1404
  STMT2_TLOG("start to set tbName:%s", tbName);
1,049,350✔
1405

1406
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
1,049,415✔
1407
    return pStmt->errCode;
×
1408
  }
1409

1410
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
1,049,143✔
1411

1412
  int32_t insert = 0;
1,049,311✔
1413
  if (!stmt2IsInsert(stmt)) {
1,049,311✔
1414
    STMT2_ELOG_E("set tb name not available for no-insert statement");
×
1415
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1416
  }
1417
  // process tbname
1418
  STMT_ERR_RET(stmtCreateRequest(pStmt));
1,049,469✔
1419

1420
  STMT_ERR_RET(qCreateSName2(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
1,049,270✔
1421
                             pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1422
  STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
1,048,695✔
1423
  tstrncpy(pStmt->bInfo.tbName, (char*)tNameGetTableName(&pStmt->bInfo.sname), TSDB_TABLE_NAME_LEN);
1,048,663✔
1424

1425
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
1,048,688✔
1426
    STMT_ERR_RET(stmtGetFromCache(pStmt));
21,463✔
1427

1428
    if (pStmt->bInfo.needParse) {
21,165✔
1429
      STMT_ERR_RET(stmtParseSql(pStmt));
10,815✔
1430
      if (!pStmt->sql.autoCreateTbl) {
10,815✔
1431
        uint64_t uid, suid;
2,366✔
1432
        int32_t  vgId;
2,366✔
1433
        int8_t   tableType;
2,366✔
1434

1435
        int32_t code = stmtGetTableMetaAndValidate(pStmt, &uid, &suid, &vgId, &tableType);
3,990✔
1436
        if (code != TSDB_CODE_SUCCESS) {
3,979✔
1437
          return code;
384✔
1438
        }
1439
      }
1440
    }
1441

1442
  } else {
1443
    pStmt->exec.pRequest->requestId++;
1,027,084✔
1444
    pStmt->bInfo.needParse = false;
1,027,411✔
1445
  }
1446

1447
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
1,047,766✔
1448
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
7,283✔
1449
  }
1450

1451
  int64_t startUs2 = taosGetTimestampUs();
1,048,435✔
1452
  pStmt->stat.setTbNameUs += startUs2 - startUs;
1,048,435✔
1453

1454
  return TSDB_CODE_SUCCESS;
1,048,362✔
1455
}
1456

1457
int stmtSetTbTags2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* tags, SVCreateTbReq** pCreateTbReq) {
545,560✔
1458
  STscStmt2* pStmt = (STscStmt2*)stmt;
545,560✔
1459

1460
  STMT2_TLOG_E("start to set tbTags");
545,560✔
1461
  if (qDebugFlag & DEBUG_TRACE) {
545,560✔
1462
    (void)stmtPrintBindv(stmt, tags, -1, true);
×
1463
  }
1464

1465
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
545,573✔
1466
    return pStmt->errCode;
×
1467
  }
1468

1469
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
545,573✔
1470

1471
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
545,508✔
1472
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1473
    pStmt->bInfo.needParse = false;
×
1474
  }
1475
  STMT_ERR_RET(stmtCreateRequest(pStmt));
545,508✔
1476

1477
  if (pStmt->bInfo.needParse) {
545,534✔
1478
    STMT_ERR_RET(stmtParseSql(pStmt));
192✔
1479
  }
1480
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
545,534✔
1481
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1482
  }
1483

1484
  SBoundColInfo* tags_info = (SBoundColInfo*)pStmt->bInfo.boundTags;
545,508✔
1485

1486
  STableDataCxt** pDataBlock = NULL;
545,508✔
1487
  if (pStmt->exec.pCurrBlock) {
545,508✔
1488
    pDataBlock = &pStmt->exec.pCurrBlock;
540,160✔
1489
  } else {
1490
    pDataBlock =
1491
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
5,348✔
1492
    if (NULL == pDataBlock) {
5,348✔
1493
      STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1494
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1495
    }
1496
    if (pStmt->sql.stbInterlaceMode && (*pDataBlock)->pData->pCreateTbReq) {
5,348✔
1497
      tdDestroySVCreateTbReq((*pDataBlock)->pData->pCreateTbReq);
192✔
1498
      taosMemoryFreeClear((*pDataBlock)->pData->pCreateTbReq);
192✔
1499
      (*pDataBlock)->pData->pCreateTbReq = NULL;
192✔
1500
    }
1501
  }
1502
  if (pStmt->bInfo.inExecCache && !pStmt->sql.autoCreateTbl) {
545,508✔
1503
    return TSDB_CODE_SUCCESS;
×
1504
  }
1505

1506
  STMT2_TLOG_E("start to bind stmt tag values");
545,508✔
1507

1508
  void* boundTags = NULL;
545,508✔
1509
  if (pStmt->sql.stbInterlaceMode) {
545,508✔
1510
    boundTags = pStmt->sql.siInfo.boundTags;
533,930✔
1511
    *pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
533,930✔
1512
    if (NULL == pCreateTbReq) {
533,995✔
1513
      return terrno;
×
1514
    }
1515
    int32_t vgId = -1;
533,995✔
1516
    STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
533,995✔
1517
    (*pCreateTbReq)->uid = vgId;
534,060✔
1518
  } else {
1519
    boundTags = pStmt->bInfo.boundTags;
11,578✔
1520
  }
1521

1522
  STMT_ERR_RET(qBindStmtTagsValue2(*pDataBlock, boundTags, pStmt->bInfo.tbSuid, pStmt->bInfo.stbFName,
545,638✔
1523
                                   pStmt->bInfo.sname.tname, tags, pStmt->exec.pRequest->msgBuf,
1524
                                   pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt, *pCreateTbReq));
1525

1526
  return TSDB_CODE_SUCCESS;
545,516✔
1527
}
1528

1529
int stmtCheckTags2(TAOS_STMT2* stmt, SVCreateTbReq** pCreateTbReq) {
106,833✔
1530
  STscStmt2* pStmt = (STscStmt2*)stmt;
106,833✔
1531

1532
  STMT2_TLOG_E("start to clone createTbRequest for fixed tags");
106,833✔
1533

1534
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
106,872✔
1535
    return pStmt->errCode;
×
1536
  }
1537

1538
  if (!pStmt->sql.stbInterlaceMode) {
106,872✔
1539
    return TSDB_CODE_SUCCESS;
×
1540
  }
1541

1542
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
106,872✔
1543

1544
  if (pStmt->sql.fixValueTags) {
106,820✔
1545
    STMT2_TLOG_E("tags are fixed, use one createTbReq");
105,110✔
1546
    STMT_ERR_RET(cloneSVreateTbReq(pStmt->sql.fixValueTbReq, pCreateTbReq));
105,110✔
1547
    if ((*pCreateTbReq)->name) {
105,149✔
1548
      taosMemoryFree((*pCreateTbReq)->name);
105,175✔
1549
    }
1550
    (*pCreateTbReq)->name = taosStrdup(pStmt->bInfo.tbName);
105,045✔
1551
    int32_t vgId = -1;
105,162✔
1552
    STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
105,162✔
1553
    (*pCreateTbReq)->uid = vgId;
105,110✔
1554
    return TSDB_CODE_SUCCESS;
105,110✔
1555
  }
1556

1557
  STMT_ERR_RET(stmtCreateRequest(pStmt));
1,710✔
1558
  if (pStmt->bInfo.needParse) {
1,710✔
1559
    STMT_ERR_RET(stmtParseSql(pStmt));
×
1560
    if (!pStmt->sql.autoCreateTbl) {
×
1561
      STMT2_WLOG_E("don't need to create table, will not check tags");
×
1562
      return TSDB_CODE_SUCCESS;
×
1563
    }
1564
  }
1565

1566
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
1,710✔
1567
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1568
  }
1569

1570
  STableDataCxt** pDataBlock = NULL;
1,710✔
1571
  if (pStmt->exec.pCurrBlock) {
1,710✔
1572
    pDataBlock = &pStmt->exec.pCurrBlock;
×
1573
  } else {
1574
    pDataBlock =
1575
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
1,710✔
1576
    if (NULL == pDataBlock) {
1,710✔
1577
      STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1578
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1579
    }
1580
  }
1581

1582
  if (!((*pDataBlock)->pData->flags & SUBMIT_REQ_AUTO_CREATE_TABLE)) {
1,710✔
1583
    STMT2_DLOG_E("don't need to create, will not check tags");
×
1584
    return TSDB_CODE_SUCCESS;
×
1585
  }
1586

1587

1588
  if ((*pDataBlock)->pData->pCreateTbReq) {
1,710✔
1589
    STMT2_TLOG_E("tags are fixed, set createTbReq first time");
1,710✔
1590
    pStmt->sql.fixValueTags = true;
1,710✔
1591
    STMT_ERR_RET(cloneSVreateTbReq((*pDataBlock)->pData->pCreateTbReq, &pStmt->sql.fixValueTbReq));
1,710✔
1592
    STMT_ERR_RET(cloneSVreateTbReq(pStmt->sql.fixValueTbReq, pCreateTbReq));
1,710✔
1593
    (*pCreateTbReq)->uid = (*pDataBlock)->pMeta->vgId;
1,710✔
1594

1595
    // destroy the createTbReq in the data block
1596
    tdDestroySVCreateTbReq((*pDataBlock)->pData->pCreateTbReq);
1,710✔
1597
    taosMemoryFreeClear((*pDataBlock)->pData->pCreateTbReq);
1,710✔
1598
    (*pDataBlock)->pData->pCreateTbReq = NULL;
1,710✔
1599
  }
1600

1601
  return TSDB_CODE_SUCCESS;
1,710✔
1602
}
1603

1604
static int stmtFetchColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
×
1605
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1606
    return pStmt->errCode;
×
1607
  }
1608

1609
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1610
    tscError("invalid operation to get query column fileds");
×
1611
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1612
  }
1613

1614
  STableDataCxt** pDataBlock = NULL;
×
1615

1616
  if (pStmt->sql.stbInterlaceMode) {
×
1617
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
×
1618
  } else {
1619
    pDataBlock =
1620
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
×
1621
    if (NULL == pDataBlock) {
×
1622
      STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1623
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1624
    }
1625
  }
1626

1627
  STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields));
×
1628

1629
  return TSDB_CODE_SUCCESS;
×
1630
}
1631

1632
static int stmtFetchStbColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_ALL** fields) {
5,205✔
1633
  int32_t code = 0;
5,205✔
1634
  int32_t preCode = pStmt->errCode;
5,205✔
1635

1636
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
5,205✔
1637
    return pStmt->errCode;
×
1638
  }
1639

1640
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
5,205✔
1641
    STMT2_ELOG_E("stmtFetchStbColFields2 only for insert statement");
×
1642
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1643
  }
1644

1645
  STableDataCxt** pDataBlock = NULL;
5,205✔
1646
  bool            cleanStb = false;
5,205✔
1647

1648
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx != NULL) {
5,205✔
1649
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
576✔
1650
  } else {
1651
    cleanStb = true;
4,629✔
1652
    pDataBlock =
1653
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
4,629✔
1654
  }
1655

1656
  if (NULL == pDataBlock || NULL == *pDataBlock) {
5,205✔
1657
    STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1658
    STMT_ERRI_JRET(TSDB_CODE_APP_ERROR);
×
1659
  }
1660

1661
  pStmt->sql.placeholderOfTags = 0;
5,205✔
1662
  pStmt->sql.placeholderOfCols = 0;
5,205✔
1663
  int32_t totalNum = 0;
5,205✔
1664
  STMT_ERRI_JRET(qBuildStmtStbColFields(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.fixedValueCols,
5,205✔
1665
                                        pStmt->bInfo.tbNameFlag, &totalNum, fields, &pStmt->sql.placeholderOfTags,
1666
                                        &pStmt->sql.placeholderOfCols));
1667

1668
  if (pStmt->bInfo.tbType == TSDB_SUPER_TABLE && cleanStb) {
5,205✔
1669
    taosMemoryFreeClear((*pDataBlock)->boundColsInfo.pColIndex);
3,669✔
1670
    qDestroyStmtDataBlock(*pDataBlock);
3,669✔
1671
    *pDataBlock = NULL;
3,669✔
1672
    if (taosHashRemove(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName)) != 0) {
3,669✔
1673
      STMT2_ELOG("fail to remove remove stb:%s exec blockHash", pStmt->bInfo.tbFName);
×
1674
      STMT_ERRI_JRET(TSDB_CODE_APP_ERROR);
×
1675
    }
1676
    pStmt->sql.autoCreateTbl = false;
3,669✔
1677
    pStmt->bInfo.tagsCached = false;
3,669✔
1678
    pStmt->bInfo.sname = (SName){0};
3,669✔
1679
    STMT_ERR_RET(stmtCleanBindInfo(pStmt));
3,669✔
1680
  }
1681

1682
  if (fieldNum != NULL) {
5,205✔
1683
    *fieldNum = totalNum;
5,205✔
1684
  }
1685

1686
  STMT2_DLOG("get insert fields totalNum:%d, tagsNum:%d, colsNum:%d", totalNum, pStmt->sql.placeholderOfTags,
5,205✔
1687
             pStmt->sql.placeholderOfCols);
1688

1689
_return:
5,205✔
1690

1691
  pStmt->errCode = preCode;
5,205✔
1692

1693
  return code;
5,205✔
1694
}
1695
/*
1696
SArray* stmtGetFreeCol(STscStmt2* pStmt, int32_t* idx) {
1697
  while (true) {
1698
    if (pStmt->exec.smInfo.pColIdx >= STMT_COL_BUF_SIZE) {
1699
      pStmt->exec.smInfo.pColIdx = 0;
1700
    }
1701

1702
    if ((pStmt->exec.smInfo.pColIdx + 1) == atomic_load_32(&pStmt->exec.smInfo.pColFreeIdx)) {
1703
      taosUsleep(1);
1704
      continue;
1705
    }
1706

1707
    *idx = pStmt->exec.smInfo.pColIdx;
1708
    return pStmt->exec.smInfo.pCols[pStmt->exec.smInfo.pColIdx++];
1709
  }
1710
}
1711
*/
1712
static int32_t stmtAppendTablePostHandle(STscStmt2* pStmt, SStmtQNode* param) {
1,034,535✔
1713
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
1,034,535✔
1714
    pStmt->sql.siInfo.pVgroupHash =
489,233✔
1715
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
489,050✔
1716
  }
1717
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
1,034,934✔
1718
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
489,237✔
1719
  }
1720

1721
  if (NULL == pStmt->sql.siInfo.pRequest) {
1,034,830✔
1722
    STMT_ERR_RET(buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false,
7,091✔
1723
                              (SRequestObj**)&pStmt->sql.siInfo.pRequest, pStmt->reqid));
1724

1725
    if (pStmt->reqid != 0) {
7,050✔
1726
      pStmt->reqid++;
8✔
1727
    }
1728
    pStmt->exec.pRequest->syncQuery = true;
7,091✔
1729

1730
    pStmt->sql.siInfo.requestId = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->requestId;
7,050✔
1731
    pStmt->sql.siInfo.requestSelf = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->self;
7,050✔
1732
  }
1733

1734
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
1,034,865✔
1735
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
22,993✔
1736
    pStmt->sql.siInfo.tbFromHash = true;
3,000✔
1737
  }
1738

1739
  if (0 == pStmt->sql.siInfo.firstName[0]) {
1,034,783✔
1740
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
7,080✔
1741
  }
1742

1743
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
1,034,865✔
1744
  param->next = NULL;
1,034,830✔
1745

1746
  (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
1,034,836✔
1747

1748
  if (pStmt->queue.stopQueue) {
1,035,125✔
1749
    STMT2_ELOG_E("bind thread already stopped, cannot enqueue");
×
1750
    return TSDB_CODE_TSC_STMT_API_ERROR;
×
1751
  }
1752
  stmtEnqueue(pStmt, param);
1,035,049✔
1753

1754
  return TSDB_CODE_SUCCESS;
1,034,986✔
1755
}
1756

1757
static FORCE_INLINE int32_t stmtGetTableColsFromCache(STscStmt2* pStmt, SArray** pTableCols) {
1758
  while (true) {
1759
    if (pStmt->sql.siInfo.pTableColsIdx < taosArrayGetSize(pStmt->sql.siInfo.pTableCols)) {
1,034,620✔
1760
      *pTableCols = (SArray*)taosArrayGetP(pStmt->sql.siInfo.pTableCols, pStmt->sql.siInfo.pTableColsIdx++);
1,035,039✔
1761
      break;
1,034,991✔
1762
    } else {
1763
      SArray* pTblCols = NULL;
×
1764
      for (int32_t i = 0; i < 100; i++) {
×
1765
        pTblCols = taosArrayInit(20, POINTER_BYTES);
×
1766
        if (NULL == pTblCols) {
×
1767
          return terrno;
×
1768
        }
1769

1770
        if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
×
1771
          return terrno;
×
1772
        }
1773
      }
1774
    }
1775
  }
1776

1777
  return TSDB_CODE_SUCCESS;
1,034,991✔
1778
}
1779

1780
static int32_t stmtCacheBlock(STscStmt2* pStmt) {
40,927,538✔
1781
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
40,927,538✔
1782
    return TSDB_CODE_SUCCESS;
41,270,680✔
1783
  }
1784

1785
  uint64_t uid = pStmt->bInfo.tbUid;
13,056✔
1786
  uint64_t cacheUid = (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) ? pStmt->bInfo.tbSuid : uid;
13,056✔
1787

1788
  if (taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid))) {
13,056✔
1789
    STMT2_TLOG("table %s already cached, no need to cache again", pStmt->bInfo.tbFName);
10,213✔
1790
    return TSDB_CODE_SUCCESS;
10,213✔
1791
  }
1792

1793
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
2,997✔
1794
  if (!pSrc) {
2,997✔
1795
    STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1796
    return terrno;
×
1797
  }
1798
  STableDataCxt* pDst = NULL;
2,997✔
1799

1800
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
2,997✔
1801

1802
  SStmtTableCache cache = {
2,997✔
1803
      .pDataCtx = pDst,
1804
      .boundTags = pStmt->bInfo.boundTags,
2,997✔
1805
  };
1806

1807
  if (taosHashPut(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid), &cache, sizeof(cache))) {
2,997✔
1808
    STMT2_ELOG("fail to put table cache:%s", tstrerror(terrno));
×
1809
    return terrno;
×
1810
  }
1811

1812
  if (pStmt->sql.autoCreateTbl) {
2,997✔
1813
    pStmt->bInfo.tagsCached = true;
2,517✔
1814
  } else {
1815
    pStmt->bInfo.boundTags = NULL;
480✔
1816
  }
1817

1818
  return TSDB_CODE_SUCCESS;
2,997✔
1819
}
1820

1821
static int stmtAddBatch2(TAOS_STMT2* stmt) {
41,353,023✔
1822
  STscStmt2* pStmt = (STscStmt2*)stmt;
41,353,023✔
1823

1824
  int64_t startUs = taosGetTimestampUs();
41,872,107✔
1825

1826
  // STMT2_TLOG_E("start to add batch");
1827

1828
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
41,872,107✔
1829
    return pStmt->errCode;
×
1830
  }
1831

1832
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
42,023,476✔
1833

1834
  if (pStmt->sql.stbInterlaceMode) {
41,872,480✔
1835
    int64_t startUs2 = taosGetTimestampUs();
488,936✔
1836
    pStmt->stat.addBatchUs += startUs2 - startUs;
488,936✔
1837

1838
    pStmt->sql.siInfo.tableColsReady = false;
488,898✔
1839

1840
    SStmtQNode* param = NULL;
488,863✔
1841
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
977,677✔
1842
    param->restoreTbCols = true;
488,779✔
1843
    param->next = NULL;
488,788✔
1844

1845
    if (pStmt->sql.autoCreateTbl) {
488,785✔
1846
      pStmt->bInfo.tagsCached = true;
214,423✔
1847
    }
1848
    pStmt->bInfo.boundColsCached = true;
488,715✔
1849

1850
    if (pStmt->queue.stopQueue) {
488,715✔
1851
      STMT2_ELOG_E("stmt bind thread is stopped,cannot enqueue bind request");
×
1852
      return TSDB_CODE_TSC_STMT_API_ERROR;
×
1853
    }
1854

1855
    stmtEnqueue(pStmt, param);
488,785✔
1856

1857
    return TSDB_CODE_SUCCESS;
489,006✔
1858
  }
1859

1860
  STMT_ERR_RET(stmtCacheBlock(pStmt));
41,481,414✔
1861

1862
  return TSDB_CODE_SUCCESS;
41,262,405✔
1863
}
1864
/*
1865
static int32_t stmtBackupQueryFields(STscStmt2* pStmt) {
1866
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
1867
  pRes->numOfCols = pStmt->exec.pRequest->body.resInfo.numOfCols;
1868
  pRes->precision = pStmt->exec.pRequest->body.resInfo.precision;
1869

1870
  int32_t size = pRes->numOfCols * sizeof(TAOS_FIELD);
1871
  pRes->fields = taosMemoryMalloc(size);
1872
  pRes->userFields = taosMemoryMalloc(size);
1873
  if (NULL == pRes->fields || NULL == pRes->userFields) {
1874
    STMT_ERR_RET(terrno);
1875
  }
1876
  (void)memcpy(pRes->fields, pStmt->exec.pRequest->body.resInfo.fields, size);
1877
  (void)memcpy(pRes->userFields, pStmt->exec.pRequest->body.resInfo.userFields, size);
1878

1879
  return TSDB_CODE_SUCCESS;
1880
}
1881

1882
static int32_t stmtRestoreQueryFields(STscStmt2* pStmt) {
1883
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
1884
  int32_t            size = pRes->numOfCols * sizeof(TAOS_FIELD);
1885

1886
  pStmt->exec.pRequest->body.resInfo.numOfCols = pRes->numOfCols;
1887
  pStmt->exec.pRequest->body.resInfo.precision = pRes->precision;
1888

1889
  if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
1890
    pStmt->exec.pRequest->body.resInfo.fields = taosMemoryMalloc(size);
1891
    if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
1892
      STMT_ERR_RET(terrno);
1893
    }
1894
    (void)memcpy(pStmt->exec.pRequest->body.resInfo.fields, pRes->fields, size);
1895
  }
1896

1897
  if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
1898
    pStmt->exec.pRequest->body.resInfo.userFields = taosMemoryMalloc(size);
1899
    if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
1900
      STMT_ERR_RET(terrno);
1901
    }
1902
    (void)memcpy(pStmt->exec.pRequest->body.resInfo.userFields, pRes->userFields, size);
1903
  }
1904

1905
  return TSDB_CODE_SUCCESS;
1906
}
1907
*/
1908

1909
/**
1910
 * Fetch metadata for query statement after parameter binding.
1911
 * This function collects metadata requirements from the query (after binding),
1912
 * fetches metadata synchronously from catalog, and returns it for parsing.
1913
 *
1914
 * Note: We fetch metadata on every bind because:
1915
 * 1. Parameter values in WHERE conditions (e.g., dataname IN (?,?)) may change
1916
 * 2. Different parameter values may require different vgroup lists for virtual tables
1917
 * 3. Metadata requirements can only be determined after parameters are bound
1918
 *
1919
 * @param pStmt Statement handle
1920
 * @param pCxt Parse context (must have catalog handle initialized)
1921
 * @param pMetaData Output: Fetched metadata (caller responsible for cleanup)
1922
 * @return TSDB_CODE_SUCCESS on success, error code otherwise
1923
 */
1924
// Callback parameter structure for synchronous catalog metadata fetch
1925
typedef struct {
1926
  SMetaData* pRsp;
1927
  int32_t    code;
1928
  tsem_t     sem;
1929
} SCatalogSyncCbParam;
1930

1931
// Callback function for catalogAsyncGetAllMeta to make it synchronous
1932
static void stmtCatalogSyncGetAllMetaCb(SMetaData* pResultMeta, void* param, int32_t code) {
3,752✔
1933
  SCatalogSyncCbParam* pCbParam = (SCatalogSyncCbParam*)param;
3,752✔
1934
  if (TSDB_CODE_SUCCESS == code && pResultMeta) {
3,752✔
1935
    *pCbParam->pRsp = *pResultMeta;
3,752✔
1936
    TAOS_MEMSET(pResultMeta, 0, sizeof(SMetaData));  // Clear to avoid double free
3,752✔
1937
  }
1938
  pCbParam->code = code;
3,752✔
1939
  if (tsem_post(&pCbParam->sem) != 0) {
3,752✔
1940
    tscError("failed to post semaphore");
×
1941
  }
1942
}
3,752✔
1943

1944
static int32_t stmtFetchMetadataForQuery(STscStmt2* pStmt, SParseContext* pCxt, SMetaData* pMetaData) {
3,752✔
1945
  int32_t          code = 0;
3,752✔
1946
  SParseMetaCache  metaCache = {0};
3,752✔
1947
  SCatalogReq      catalogReq = {0};
3,752✔
1948
  SRequestConnInfo conn = {.pTrans = pCxt->pTransporter,
3,752✔
1949
                           .requestId = pCxt->requestId,
3,752✔
1950
                           .requestObjRefId = pCxt->requestRid,
3,752✔
1951
                           .mgmtEps = pCxt->mgmtEpSet};
1952

1953
  TAOS_MEMSET(pMetaData, 0, sizeof(SMetaData));
3,752✔
1954

1955
  code = collectMetaKey(pCxt, pStmt->sql.pQuery, &metaCache);
3,752✔
1956
  if (TSDB_CODE_SUCCESS == code) {
3,752✔
1957
    code = buildCatalogReq(&metaCache, &catalogReq);
3,752✔
1958
  }
1959
  if (TSDB_CODE_SUCCESS == code) {
3,752✔
1960
    SCatalogSyncCbParam cbParam = {.pRsp = pMetaData, .code = TSDB_CODE_SUCCESS};
3,752✔
1961
    if (tsem_init(&cbParam.sem, 0, 0) != 0) {
3,752✔
1962
      code = TSDB_CODE_CTG_INTERNAL_ERROR;
×
1963
    } else {
1964
      code = catalogAsyncGetAllMeta(pCxt->pCatalog, &conn, &catalogReq, stmtCatalogSyncGetAllMetaCb, &cbParam, NULL);
3,752✔
1965
      if (TSDB_CODE_SUCCESS == code) {
3,752✔
1966
        code = tsem_wait(&cbParam.sem);
3,752✔
1967
        if (code != TSDB_CODE_SUCCESS) {
3,752✔
1968
          catalogFreeMetaData(pMetaData);
×
1969
          TAOS_MEMSET(pMetaData, 0, sizeof(SMetaData));
×
1970
        } else {
1971
          code = cbParam.code;
3,752✔
1972
        }
1973
      }
1974

1975
      if (tsem_destroy(&cbParam.sem) != 0) {
3,752✔
1976
        tscError("failed to destroy semaphore");
×
1977
        code = TSDB_CODE_CTG_INTERNAL_ERROR;
×
1978
        catalogFreeMetaData(pMetaData);
×
1979
        TAOS_MEMSET(pMetaData, 0, sizeof(SMetaData));
×
1980
      }
1981
    }
1982
  }
1983

1984
  destoryParseMetaCache(&metaCache, false);
3,752✔
1985
  destoryCatalogReq(&catalogReq);
3,752✔
1986

1987
  if (TSDB_CODE_SUCCESS != code) {
3,752✔
1988
    catalogFreeMetaData(pMetaData);
×
1989
  }
1990

1991
  return code;
3,752✔
1992
}
1993

1994
int stmtBindBatch2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* bind, int32_t colIdx, SVCreateTbReq* pCreateTbReq) {
41,974,897✔
1995
  STscStmt2* pStmt = (STscStmt2*)stmt;
41,974,897✔
1996
  int32_t    code = 0;
41,974,897✔
1997

1998
  int64_t startUs = taosGetTimestampUs();
42,281,519✔
1999

2000
  if (qDebugFlag & DEBUG_TRACE) {
42,281,519✔
2001
    (void)stmtPrintBindv(stmt, bind, colIdx, false);
×
2002
  }
2003

2004
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
42,255,287✔
2005
    return pStmt->errCode;
96✔
2006
  }
2007

2008
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
42,533,599✔
2009

2010
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
42,530,868✔
2011
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
2012
    pStmt->bInfo.needParse = false;
×
2013
  }
2014

2015
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
42,239,317✔
2016
    resetRequest(pStmt);
864✔
2017
  }
2018

2019
  STMT_ERR_RET(stmtCreateRequest(pStmt));
42,258,540✔
2020
  if (pStmt->bInfo.needParse) {
42,130,064✔
2021
    code = stmtParseSql(pStmt);
102,992✔
2022
    if (code != TSDB_CODE_SUCCESS) {
102,955✔
2023
      goto cleanup_root;
×
2024
    }
2025
  }
2026

2027
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
42,165,100✔
2028
    code = qStmtBindParams2(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt);
3,752✔
2029
    if (code != TSDB_CODE_SUCCESS) {
3,752✔
2030
      goto cleanup_root;
×
2031
    }
2032
    SParseContext ctx = {.requestId = pStmt->exec.pRequest->requestId,
3,752✔
2033
                         .acctId = pStmt->taos->acctId,
3,752✔
2034
                         .db = pStmt->exec.pRequest->pDb,
3,752✔
2035
                         .topicQuery = false,
2036
                         .pSql = pStmt->sql.sqlStr,
3,752✔
2037
                         .sqlLen = pStmt->sql.sqlLen,
3,752✔
2038
                         .pMsg = pStmt->exec.pRequest->msgBuf,
3,752✔
2039
                         .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
2040
                         .pTransporter = pStmt->taos->pAppInfo->pTransporter,
3,752✔
2041
                         .pStmtCb = NULL,
2042
                         .pUser = pStmt->taos->user,
3,752✔
2043
                         .stmtBindVersion = pStmt->exec.pRequest->stmtBindVersion};
3,752✔
2044
    ctx.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
3,752✔
2045
    code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &ctx.pCatalog);
3,752✔
2046
    if (code != TSDB_CODE_SUCCESS) {
3,752✔
2047
      goto cleanup_root;
×
2048
    }
2049

2050
    // Fetch metadata for query(vtable need)
2051
    SMetaData metaData = {0};
3,752✔
2052
    code = stmtFetchMetadataForQuery(pStmt, &ctx, &metaData);
3,752✔
2053
    if (TSDB_CODE_SUCCESS != code) {
3,752✔
2054
      goto cleanup_root;
×
2055
    }
2056

2057
    code = qStmtParseQuerySql(&ctx, pStmt->sql.pQuery, &metaData);
3,752✔
2058
    if (TSDB_CODE_SUCCESS == code) {
3,752✔
2059
      // Copy metaData to pRequest->parseMeta for potential future use
2060
      // Similar to doAsyncQueryFromAnalyse when parseOnly is true
2061
      (void)memcpy(&pStmt->exec.pRequest->parseMeta, &metaData, sizeof(SMetaData));
3,656✔
2062
      (void)memset(&metaData, 0, sizeof(SMetaData));  // Clear to avoid double free
3,656✔
2063
    } else {
2064
      // Clean up metaData on failure - free all arrays
2065
      if (metaData.pVStbRefDbs) {
96✔
2066
        taosArrayDestroy(metaData.pVStbRefDbs);
96✔
2067
        metaData.pVStbRefDbs = NULL;
96✔
2068
      }
2069
      // Note: Other fields in metaData are managed by catalog module if ctgFree is true
2070
      goto cleanup_root;
96✔
2071
    }
2072

2073
    if (pStmt->sql.pQuery->haveResultSet) {
3,656✔
2074
      STMT_ERR_RET(setResSchemaInfo(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->pResSchema,
7,408✔
2075
                                    pStmt->sql.pQuery->numOfResCols, pStmt->sql.pQuery->pResExtSchema, true));
2076
      taosMemoryFreeClear(pStmt->sql.pQuery->pResSchema);
3,656✔
2077
      taosMemoryFreeClear(pStmt->sql.pQuery->pResExtSchema);
3,656✔
2078
      setResPrecision(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->precision);
3,656✔
2079
    }
2080

2081
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
3,656✔
2082
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
3,656✔
2083
    TSWAP(pStmt->exec.pRequest->targetTableList, pStmt->sql.pQuery->pTargetTableList);
3,656✔
2084

2085
    // if (STMT_TYPE_QUERY == pStmt->sql.queryRes) {
2086
    //   STMT_ERR_RET(stmtRestoreQueryFields(pStmt));
2087
    // }
2088

2089
    // STMT_ERR_RET(stmtBackupQueryFields(pStmt));
2090

2091
    return TSDB_CODE_SUCCESS;
3,656✔
2092

2093
  cleanup_root:
96✔
2094
    STMT2_ELOG("parse query statment unexpected failed code:%d, need to clean node", code);
96✔
2095
    if (pStmt->sql.pQuery && pStmt->sql.pQuery->pRoot) {
96✔
2096
      nodesDestroyNode(pStmt->sql.pQuery->pRoot);
96✔
2097
      pStmt->sql.pQuery->pRoot = NULL;
96✔
2098
    }
2099
    STMT_ERR_RET(code);
96✔
2100
  }
2101

2102
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
42,249,523✔
2103
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
2104
  }
2105

2106
  STableDataCxt** pDataBlock = NULL;
41,972,905✔
2107

2108
  if (pStmt->exec.pCurrBlock) {
41,972,905✔
2109
    pDataBlock = &pStmt->exec.pCurrBlock;
42,289,427✔
2110
  } else {
2111
    pDataBlock =
2112
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
113,486✔
2113
    if (NULL == pDataBlock) {
113,560✔
2114
      STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
2115
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
2116
    }
2117
    pStmt->exec.pCurrBlock = *pDataBlock;
113,560✔
2118
    if (pStmt->sql.stbInterlaceMode) {
113,560✔
2119
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
7,272✔
2120
      (*pDataBlock)->pData->aCol = NULL;
7,272✔
2121
    }
2122
    if (colIdx < -1) {
113,560✔
2123
      pStmt->sql.bindRowFormat = true;
96✔
2124
      taosArrayDestroy((*pDataBlock)->pData->aCol);
96✔
2125
      (*pDataBlock)->pData->aCol = taosArrayInit(20, POINTER_BYTES);
96✔
2126
    }
2127
  }
2128

2129
  int64_t startUs2 = taosGetTimestampUs();
42,384,260✔
2130
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
42,384,260✔
2131

2132
  SStmtQNode* param = NULL;
42,564,729✔
2133
  if (pStmt->sql.stbInterlaceMode) {
42,497,835✔
2134
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
2,069,026✔
2135
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
2,069,506✔
2136
    taosArrayClear(param->tblData.aCol);
1,034,991✔
2137

2138
    // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES);
2139

2140
    param->restoreTbCols = false;
1,034,040✔
2141
    param->tblData.isOrdered = true;
1,034,040✔
2142
    param->tblData.isDuplicateTs = false;
1,034,221✔
2143
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
1,034,183✔
2144

2145
    param->pCreateTbReq = pCreateTbReq;
1,034,084✔
2146
  }
2147

2148
  int64_t startUs3 = taosGetTimestampUs();
42,255,119✔
2149
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
42,255,119✔
2150

2151
  SArray*   pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
41,946,394✔
2152
  SBlobSet* pBlob = NULL;
42,543,119✔
2153
  if (colIdx < 0) {
42,580,672✔
2154
    if (pStmt->sql.stbInterlaceMode) {
42,726,344✔
2155
      (*pDataBlock)->pData->flags &= ~SUBMIT_REQ_COLUMN_DATA_FORMAT;
1,034,715✔
2156
      code = qBindStmtStbColsValue2(*pDataBlock, pCols, pStmt->bInfo.fixedValueCols, bind, pStmt->exec.pRequest->msgBuf,
1,306,247✔
2157
                                    pStmt->exec.pRequest->msgBufLen, &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo,
1,034,911✔
2158
                                    pStmt->taos->optionInfo.charsetCxt, &pBlob);
1,034,724✔
2159
      param->tblData.isOrdered = (*pDataBlock)->ordered;
1,034,744✔
2160
      param->tblData.isDuplicateTs = (*pDataBlock)->duplicateTs;
1,035,034✔
2161
    } else {
2162
      if (colIdx == -1) {
41,704,907✔
2163
        if (pStmt->sql.bindRowFormat) {
41,192,914✔
2164
          STMT2_ELOG_E("can't mix bind row format and bind column format");
96✔
2165
          STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
96✔
2166
        }
2167
        code = qBindStmtColsValue2(*pDataBlock, pCols, pStmt->bInfo.fixedValueCols, bind, pStmt->exec.pRequest->msgBuf,
77,338,971✔
2168
                                   pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt);
77,124,638✔
2169
      } else {
2170
        code =
2171
            qBindStmt2RowValue(*pDataBlock, (*pDataBlock)->pData->aRowP, pStmt->bInfo.fixedValueCols, bind,
192✔
2172
                               pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
192✔
2173
                               &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt);
526,286✔
2174
      }
2175
    }
2176

2177
    if (code) {
42,079,957✔
2178
      STMT2_ELOG("bind cols or rows failed, error:%s", tstrerror(code));
192✔
2179
      STMT_ERR_RET(code);
192✔
2180
    }
2181
  } else {
2182
    if (pStmt->sql.stbInterlaceMode) {
2,787✔
2183
      STMT2_ELOG_E("bind single column not allowed in stb insert mode");
×
2184
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2185
    }
2186

2187
    if (pStmt->sql.bindRowFormat) {
576✔
2188
      STMT2_ELOG_E("can't mix bind row format and bind column format");
×
2189
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2190
    }
2191

2192
    if (colIdx != (pStmt->bInfo.sBindLastIdx + 1) && colIdx != 0) {
576✔
2193
      STMT2_ELOG_E("bind column index not in sequence");
×
2194
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2195
    }
2196

2197
    pStmt->bInfo.sBindLastIdx = colIdx;
576✔
2198

2199
    if (0 == colIdx) {
576✔
2200
      pStmt->bInfo.sBindRowNum = bind->num;
288✔
2201
    }
2202

2203
    code = qBindStmtSingleColValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
576✔
2204
                                    pStmt->exec.pRequest->msgBufLen, colIdx, pStmt->bInfo.sBindRowNum,
576✔
2205
                                    pStmt->taos->optionInfo.charsetCxt);
576✔
2206
    if (code) {
576✔
2207
      STMT2_ELOG("bind single col failed, error:%s", tstrerror(code));
×
2208
      STMT_ERR_RET(code);
×
2209
    }
2210
  }
2211

2212
  int64_t startUs4 = taosGetTimestampUs();
42,218,202✔
2213
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
42,218,202✔
2214

2215
  if (pStmt->stbInterlaceMode) {
42,351,306✔
2216
    if (param) param->tblData.pBlobSet = pBlob;
42,491,182✔
2217
  }
2218

2219
  if (pStmt->sql.stbInterlaceMode) {
42,397,735✔
2220
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
1,034,783✔
2221
  } else {
2222
    STMT_ERR_RET(stmtAddBatch2(pStmt));
41,541,929✔
2223
  }
2224

2225
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
42,335,954✔
2226
  return TSDB_CODE_SUCCESS;
42,480,278✔
2227
}
2228

2229
/*
2230
int stmtUpdateTableUid(STscStmt2* pStmt, SSubmitRsp* pRsp) {
2231
  tscDebug("stmt start to update tbUid, blockNum:%d", pRsp->nBlocks);
2232

2233
  int32_t code = 0;
2234
  int32_t finalCode = 0;
2235
  size_t  keyLen = 0;
2236
  void*   pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
2237
  while (pIter) {
2238
    STableDataCxt* pBlock = *(STableDataCxt**)pIter;
2239
    char*          key = taosHashGetKey(pIter, &keyLen);
2240

2241
    STableMeta* pMeta = qGetTableMetaInDataBlock(pBlock);
2242
    if (pMeta->uid) {
2243
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2244
      continue;
2245
    }
2246

2247
    SSubmitBlkRsp* blkRsp = NULL;
2248
    int32_t        i = 0;
2249
    for (; i < pRsp->nBlocks; ++i) {
2250
      blkRsp = pRsp->pBlocks + i;
2251
      if (strlen(blkRsp->tblFName) != keyLen) {
2252
        continue;
2253
      }
2254

2255
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
2256
        continue;
2257
      }
2258

2259
      break;
2260
    }
2261

2262
    if (i < pRsp->nBlocks) {
2263
      tscDebug("auto created table %s uid updated from %" PRIx64 " to %" PRIx64, blkRsp->tblFName, pMeta->uid,
2264
               blkRsp->uid);
2265

2266
      pMeta->uid = blkRsp->uid;
2267
      pStmt->bInfo.tbUid = blkRsp->uid;
2268
    } else {
2269
      tscDebug("table %s not found in submit rsp, will update from catalog", pStmt->bInfo.tbFName);
2270
      if (NULL == pStmt->pCatalog) {
2271
        code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog);
2272
        if (code) {
2273
          pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2274
          finalCode = code;
2275
          continue;
2276
        }
2277
      }
2278

2279
      code = stmtCreateRequest(pStmt);
2280
      if (code) {
2281
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2282
        finalCode = code;
2283
        continue;
2284
      }
2285

2286
      STableMeta*      pTableMeta = NULL;
2287
      SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
2288
                               .requestId = pStmt->exec.pRequest->requestId,
2289
                               .requestObjRefId = pStmt->exec.pRequest->self,
2290
                               .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
2291
      code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
2292

2293
      pStmt->stat.ctgGetTbMetaNum++;
2294

2295
      taos_free_result(pStmt->exec.pRequest);
2296
      pStmt->exec.pRequest = NULL;
2297

2298
      if (code || NULL == pTableMeta) {
2299
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2300
        finalCode = code;
2301
        taosMemoryFree(pTableMeta);
2302
        continue;
2303
      }
2304

2305
      pMeta->uid = pTableMeta->uid;
2306
      pStmt->bInfo.tbUid = pTableMeta->uid;
2307
      taosMemoryFree(pTableMeta);
2308
    }
2309

2310
    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2311
  }
2312

2313
  return finalCode;
2314
}
2315
*/
2316
/*
2317
int stmtStaticModeExec(TAOS_STMT* stmt) {
2318
  STscStmt2*   pStmt = (STscStmt2*)stmt;
2319
  int32_t     code = 0;
2320
  SSubmitRsp* pRsp = NULL;
2321
  if (pStmt->sql.staticMode) {
2322
    return TSDB_CODE_TSC_STMT_API_ERROR;
2323
  }
2324

2325
  STMT_DLOG_E("start to exec");
2326

2327
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
2328

2329
  STMT_ERR_RET(qBuildStmtOutputFromTbList(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pTbBlkList,
2330
pStmt->exec.pCurrBlock, pStmt->exec.tbBlkNum));
2331

2332
  launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
2333

2334
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
2335
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
2336
    if (code) {
2337
      pStmt->exec.pRequest->code = code;
2338
    } else {
2339
      tFreeSSubmitRsp(pRsp);
2340
      STMT_ERR_RET(stmtResetStmt(pStmt));
2341
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
2342
    }
2343
  }
2344

2345
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
2346

2347
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
2348
  pStmt->affectedRows += pStmt->exec.affectedRows;
2349

2350
_return:
2351

2352
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
2353

2354
  tFreeSSubmitRsp(pRsp);
2355

2356
  ++pStmt->sql.runTimes;
2357

2358
  STMT_RET(code);
2359
}
2360
*/
2361

2362
static int32_t createParseContext(const SRequestObj* pRequest, SParseContext** pCxt, SSqlCallbackWrapper* pWrapper) {
2,400✔
2363
  const STscObj* pTscObj = pRequest->pTscObj;
2,400✔
2364

2365
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
2,400✔
2366
  if (*pCxt == NULL) {
2,400✔
2367
    return terrno;
×
2368
  }
2369

2370
  **pCxt = (SParseContext){.requestId = pRequest->requestId,
×
2371
                           .requestRid = pRequest->self,
2,400✔
2372
                           .acctId = pTscObj->acctId,
2,400✔
2373
                           .db = pRequest->pDb,
2,400✔
2374
                           .topicQuery = false,
2375
                           .pSql = pRequest->sqlstr,
2,400✔
2376
                           .sqlLen = pRequest->sqlLen,
2,400✔
2377
                           .pMsg = pRequest->msgBuf,
2,400✔
2378
                           .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
2379
                           .pTransporter = pTscObj->pAppInfo->pTransporter,
2,400✔
2380
                           .pStmtCb = NULL,
2381
                           .pUser = pTscObj->user,
2,400✔
2382
                           .userId = pTscObj->userId,
2,400✔
2383
                           .pEffectiveUser = pRequest->effectiveUser,
2,400✔
2384
                           .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
2,400✔
2385
                           .enableSysInfo = pTscObj->sysInfo,
2,400✔
2386
                           .privInfo = pWrapper->pParseCtx ? pWrapper->pParseCtx->privInfo : 0,
2,400✔
2387
                           .async = true,
2388
                           .svrVer = pTscObj->sVer,
2,400✔
2389
                           .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
2,400✔
2390
                           .allocatorId = pRequest->allocatorRefId,
2,400✔
2391
                           .parseSqlFp = clientParseSql,
2392
                           .parseSqlParam = pWrapper};
2393
  int8_t biMode = atomic_load_8(&((STscObj*)pTscObj)->biMode);
2,400✔
2394
  (*pCxt)->biMode = biMode;
2,400✔
2395
  return TSDB_CODE_SUCCESS;
2,400✔
2396
}
2397

2398
static void asyncQueryCb(void* userdata, TAOS_RES* res, int code) {
2,400✔
2399
  STscStmt2*        pStmt = userdata;
2,400✔
2400
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
2,400✔
2401
  pStmt->asyncResultAvailable = true;
2,400✔
2402
  pStmt->exec.pRequest->inCallback = true;
2,400✔
2403

2404
  if (code == TSDB_CODE_SUCCESS) {
2,400✔
2405
    pStmt->exec.affectedRows = taos_affected_rows(res);
2,304✔
2406
    pStmt->affectedRows += pStmt->exec.affectedRows;
2,304✔
2407
  }
2408

2409
  if (fp) {
2,400✔
2410
    fp(pStmt->options.userdata, res, code);
2,400✔
2411
  }
2412

2413
  while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
2,400✔
2414
    taosUsleep(1);
×
2415
  }
2416
  (void)stmtCleanExecInfo(pStmt, (code ? false : true), false);
2,400✔
2417
  ++pStmt->sql.runTimes;
2,400✔
2418
  if (pStmt->exec.pRequest != NULL) {
2,400✔
2419
    pStmt->exec.pRequest->inCallback = false;
1,536✔
2420
  }
2421

2422
  if (tsem_post(&pStmt->asyncExecSem) != 0) {
2,400✔
2423
    STMT2_ELOG_E("fail to post asyncExecSem");
×
2424
  }
2425
}
2,400✔
2426

2427
int stmtExec2(TAOS_STMT2* stmt, int* affected_rows) {
601,861✔
2428
  STscStmt2* pStmt = (STscStmt2*)stmt;
601,861✔
2429
  int32_t    code = 0;
601,861✔
2430
  int64_t    startUs = taosGetTimestampUs();
602,077✔
2431

2432
  STMT2_DLOG_E("start to exec");
602,077✔
2433

2434
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
602,051✔
2435
    return pStmt->errCode;
96✔
2436
  }
2437

2438
  STMT_ERR_RET(taosThreadMutexLock(&pStmt->asyncBindParam.mutex));
601,999✔
2439
  while (atomic_load_8((int8_t*)&pStmt->asyncBindParam.asyncBindNum) > 0) {
602,086✔
2440
    (void)taosThreadCondWait(&pStmt->asyncBindParam.waitCond, &pStmt->asyncBindParam.mutex);
×
2441
  }
2442
  STMT_ERR_RET(taosThreadMutexUnlock(&pStmt->asyncBindParam.mutex));
601,977✔
2443

2444
  if (pStmt->sql.stbInterlaceMode) {
601,981✔
2445
    STMT_ERR_RET(stmtAddBatch2(pStmt));
488,914✔
2446
  }
2447

2448
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
602,013✔
2449

2450
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
602,012✔
2451
    if (pStmt->sql.stbInterlaceMode) {
598,291✔
2452
      int64_t startTs = taosGetTimestampUs();
488,997✔
2453
      // wait for stmt bind thread to finish
2454
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
2,007,411✔
2455
        taosUsleep(1);
1,518,424✔
2456
      }
2457

2458
      if (pStmt->errCode != TSDB_CODE_SUCCESS) {
488,936✔
2459
        return pStmt->errCode;
96✔
2460
      }
2461

2462
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
488,875✔
2463
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
488,840✔
2464
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
488,901✔
2465
      pStmt->sql.siInfo.pVgroupHash = NULL;
488,901✔
2466
      pStmt->sql.siInfo.pVgroupList = NULL;
488,901✔
2467
    } else {
2468
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
109,371✔
2469
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
109,385✔
2470

2471
      STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
109,385✔
2472

2473
      STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
109,289✔
2474
    }
2475
  }
2476

2477
  pStmt->asyncResultAvailable = false;
601,832✔
2478
  SRequestObj*      pRequest = pStmt->exec.pRequest;
601,806✔
2479
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
601,876✔
2480
  STMT2_DLOG("EXEC INFO :req:0x%" PRIx64 ", QID:0x%" PRIx64 ", exec sql:%s,  conn:%" PRId64, pRequest->self,
601,911✔
2481
             pRequest->requestId, pStmt->sql.sqlStr, pRequest->pTscObj->id);
2482

2483
  if (!fp) {
601,685✔
2484
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
599,285✔
2485

2486
    if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
599,272✔
UNCOV
2487
      STMT2_ELOG_E("exec failed errorcode:NEED_CLIENT_HANDLE_ERROR, need to refresh meta and retry");
×
UNCOV
2488
      code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
×
UNCOV
2489
      if (code) {
×
2490
        pStmt->exec.pRequest->code = code;
×
2491

2492
      } else {
UNCOV
2493
        STMT_ERR_RET(stmtResetStmt(pStmt));
×
UNCOV
2494
        STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
×
2495
      }
2496
    }
2497

2498
    STMT_ERR_JRET(pStmt->exec.pRequest->code);
599,094✔
2499

2500
    pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
599,038✔
2501
    if (affected_rows) {
598,912✔
2502
      *affected_rows = pStmt->exec.affectedRows;
594,333✔
2503
    }
2504
    pStmt->affectedRows += pStmt->exec.affectedRows;
599,068✔
2505

2506
    // wait for stmt bind thread to finish
2507
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
599,856✔
2508
      taosUsleep(1);
627✔
2509
    }
2510

2511
    STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
598,891✔
2512

2513
    ++pStmt->sql.runTimes;
598,967✔
2514
  } else {
2515
    SSqlCallbackWrapper* pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
2,400✔
2516
    if (pWrapper == NULL) {
2,400✔
2517
      code = terrno;
×
2518
    } else {
2519
      pWrapper->pRequest = pRequest;
2,400✔
2520
      pRequest->pWrapper = pWrapper;
2,400✔
2521
    }
2522
    if (TSDB_CODE_SUCCESS == code) {
2,400✔
2523
      code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
2,400✔
2524
    }
2525
    pRequest->syncQuery = false;
2,400✔
2526
    pRequest->body.queryFp = asyncQueryCb;
2,400✔
2527
    ((SSyncQueryParam*)(pRequest)->body.interParam)->userParam = pStmt;
2,400✔
2528

2529
    pStmt->execSemWaited = false;
2,400✔
2530
    launchAsyncQuery(pRequest, pStmt->sql.pQuery, NULL, pWrapper);
2,400✔
2531
  }
2532

2533
_return:
601,559✔
2534
  if (code) {
601,559✔
2535
    STMT2_ELOG("exec failed, error:%s", tstrerror(code));
192✔
2536
  }
2537
  pStmt->stat.execUseUs += taosGetTimestampUs() - startUs;
601,474✔
2538

2539
  STMT_RET(code);
601,614✔
2540
}
2541

2542
int stmtClose2(TAOS_STMT2* stmt) {
119,307✔
2543
  STscStmt2* pStmt = (STscStmt2*)stmt;
119,307✔
2544

2545
  STMT2_DLOG_E("start to close stmt");
119,307✔
2546
  taosMemoryFreeClear(pStmt->db);
119,307✔
2547

2548
  if (pStmt->bindThreadInUse) {
119,394✔
2549
    // wait for stmt bind thread to finish
2550
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
112,010✔
UNCOV
2551
      taosUsleep(1);
×
2552
    }
2553

2554
    (void)taosThreadMutexLock(&pStmt->queue.mutex);
111,979✔
2555
    pStmt->queue.stopQueue = true;
112,010✔
2556
    (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
112,010✔
2557
    (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
112,010✔
2558

2559
    (void)taosThreadJoin(pStmt->bindThread, NULL);
112,010✔
2560
    pStmt->bindThreadInUse = false;
112,010✔
2561

2562
    (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
112,010✔
2563
    (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
112,010✔
2564
  }
2565

2566
  TSC_ERR_RET(taosThreadMutexLock(&pStmt->asyncBindParam.mutex));
119,394✔
2567
  while (atomic_load_8((int8_t*)&pStmt->asyncBindParam.asyncBindNum) > 0) {
119,431✔
2568
    (void)taosThreadCondWait(&pStmt->asyncBindParam.waitCond, &pStmt->asyncBindParam.mutex);
×
2569
  }
2570
  TSC_ERR_RET(taosThreadMutexUnlock(&pStmt->asyncBindParam.mutex));
119,431✔
2571

2572
  (void)taosThreadCondDestroy(&pStmt->asyncBindParam.waitCond);
119,431✔
2573
  (void)taosThreadMutexDestroy(&pStmt->asyncBindParam.mutex);
119,431✔
2574

2575
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
119,431✔
2576
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
1,056✔
2577
      STMT2_ELOG_E("fail to wait asyncExecSem");
×
2578
    }
2579
  }
2580

2581
  /* On macOS dispatch_semaphore_dispose requires value >= orig (1). After tsem_wait above value is 0; post once before
2582
   * destroy. */
2583
  if (pStmt->options.asyncExecFn) {
119,431✔
2584
    if (tsem_post(&pStmt->asyncExecSem) != 0) {
1,056✔
2585
      STMT2_ELOG_E("fail to post asyncExecSem");
×
2586
    }
2587
  }
2588

2589
  STMT2_DLOG("stbInterlaceMode:%d, statInfo: ctgGetTbMetaNum=>%" PRId64 ", getCacheTbInfo=>%" PRId64
119,431✔
2590
             ", parseSqlNum=>%" PRId64 ", pStmt->stat.bindDataNum=>%" PRId64
2591
             ", settbnameAPI:%u, bindAPI:%u, addbatchAPI:%u, execAPI:%u"
2592
             ", setTbNameUs:%" PRId64 ", bindDataUs:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64 " addBatchUs:%" PRId64
2593
             ", execWaitUs:%" PRId64 ", execUseUs:%" PRId64,
2594
             pStmt->sql.stbInterlaceMode, pStmt->stat.ctgGetTbMetaNum, pStmt->stat.getCacheTbInfo,
2595
             pStmt->stat.parseSqlNum, pStmt->stat.bindDataNum, pStmt->seqIds[STMT_SETTBNAME], pStmt->seqIds[STMT_BIND],
2596
             pStmt->seqIds[STMT_ADD_BATCH], pStmt->seqIds[STMT_EXECUTE], pStmt->stat.setTbNameUs,
2597
             pStmt->stat.bindDataUs1, pStmt->stat.bindDataUs2, pStmt->stat.bindDataUs3, pStmt->stat.bindDataUs4,
2598
             pStmt->stat.addBatchUs, pStmt->stat.execWaitUs, pStmt->stat.execUseUs);
2599
  if (pStmt->sql.stbInterlaceMode) {
119,431✔
2600
    pStmt->bInfo.tagsCached = false;
6,714✔
2601
  }
2602
  pStmt->bInfo.boundColsCached = false;
119,307✔
2603

2604
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
119,431✔
2605

2606
  if (pStmt->options.asyncExecFn) {
119,431✔
2607
    if (tsem_destroy(&pStmt->asyncExecSem) != 0) {
1,056✔
2608
      STMT2_ELOG_E("fail to destroy asyncExecSem");
×
2609
    }
2610
  }
2611
  taosMemoryFree(stmt);
119,431✔
2612

2613
  return TSDB_CODE_SUCCESS;
119,431✔
2614
}
2615

2616
const char* stmt2Errstr(TAOS_STMT2* stmt) {
4,704✔
2617
  STscStmt2* pStmt = (STscStmt2*)stmt;
4,704✔
2618

2619
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
4,704✔
2620
    return (char*)tstrerror(terrno);
384✔
2621
  }
2622

2623
  // if stmt async exec ,error code is pStmt->exec.pRequest->code
2624
  if (!(pStmt->sql.status >= STMT_EXECUTE && pStmt->options.asyncExecFn != NULL && pStmt->asyncResultAvailable)) {
4,320✔
2625
    pStmt->exec.pRequest->code = terrno;
4,320✔
2626
  }
2627

2628
  SRequestObj* pRequest = pStmt->exec.pRequest;
4,320✔
2629
  if (NULL != pRequest->msgBuf && (strlen(pRequest->msgBuf) > 0 || pRequest->code == TSDB_CODE_RPC_FQDN_ERROR)) {
4,320✔
2630
    return pRequest->msgBuf;
2,976✔
2631
  }
2632
  return (const char*)tstrerror(pRequest->code);
1,344✔
2633
}
2634
/*
2635
int stmtAffectedRows(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->affectedRows; }
2636

2637
int stmtAffectedRowsOnce(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->exec.affectedRows; }
2638
*/
2639

2640
int stmtParseColFields2(TAOS_STMT2* stmt) {
6,837✔
2641
  int32_t    code = 0;
6,837✔
2642
  STscStmt2* pStmt = (STscStmt2*)stmt;
6,837✔
2643
  int32_t    preCode = pStmt->errCode;
6,837✔
2644

2645
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
6,837✔
2646
    return pStmt->errCode;
×
2647
  }
2648

2649
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
6,837✔
2650
    STMT2_ELOG_E("stmtParseColFields2 only for insert");
×
2651
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2652
  }
2653

2654
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
6,837✔
2655

2656
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
6,837✔
2657
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
288✔
2658
    pStmt->bInfo.needParse = false;
×
2659
  }
2660
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx != NULL) {
6,837✔
2661
    pStmt->bInfo.needParse = false;
576✔
2662
  }
2663

2664
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
6,837✔
2665

2666
  if (pStmt->bInfo.needParse) {
6,837✔
2667
    STMT_ERRI_JRET(stmtParseSql(pStmt));
6,165✔
2668
  }
2669

2670
_return:
5,205✔
2671
  // compatible with previous versions
2672
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && (pStmt->bInfo.tbNameFlag & NO_DATA_USING_CLAUSE) == 0x0) {
6,837✔
2673
    code = TSDB_CODE_TSC_STMT_TBNAME_ERROR;
384✔
2674
  }
2675

2676
  pStmt->errCode = preCode;
6,837✔
2677

2678
  return code;
6,837✔
2679
}
2680

2681
int stmtGetStbColFields2(TAOS_STMT2* stmt, int* nums, TAOS_FIELD_ALL** fields) {
6,837✔
2682
  int32_t code = stmtParseColFields2(stmt);
6,837✔
2683
  if (code != TSDB_CODE_SUCCESS) {
6,837✔
2684
    return code;
1,632✔
2685
  }
2686

2687
  return stmtFetchStbColFields2(stmt, nums, fields);
5,205✔
2688
}
2689

2690
int stmtGetParamNum2(TAOS_STMT2* stmt, int* nums) {
1,824✔
2691
  int32_t    code = 0;
1,824✔
2692
  STscStmt2* pStmt = (STscStmt2*)stmt;
1,824✔
2693
  int32_t    preCode = pStmt->errCode;
1,824✔
2694

2695
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
1,824✔
2696
    return pStmt->errCode;
×
2697
  }
2698

2699
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
1,824✔
2700

2701
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
1,824✔
2702
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
2703
    pStmt->bInfo.needParse = false;
×
2704
  }
2705

2706
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
1,824✔
2707
    resetRequest(pStmt);
×
2708
  }
2709

2710
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
1,824✔
2711

2712
  if (pStmt->bInfo.needParse) {
1,824✔
2713
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
2714
  }
2715

2716
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
1,824✔
2717
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
1,824✔
2718
  } else {
2719
    STMT_ERRI_JRET(stmtFetchColFields2(stmt, nums, NULL));
×
2720
  }
2721

2722
  STMT2_DLOG("get param num success, nums:%d", *nums);
1,824✔
2723

2724
_return:
1,824✔
2725

2726
  pStmt->errCode = preCode;
1,824✔
2727

2728
  return code;
1,824✔
2729
}
2730

2731
TAOS_RES* stmtUseResult2(TAOS_STMT2* stmt) {
3,272✔
2732
  STscStmt2* pStmt = (STscStmt2*)stmt;
3,272✔
2733

2734
  STMT2_TLOG_E("start to use result");
3,272✔
2735

2736
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
3,272✔
UNCOV
2737
    STMT2_ELOG_E("useResult only for query statement");
×
UNCOV
2738
    return NULL;
×
2739
  }
2740

2741
  if (pStmt->options.asyncExecFn != NULL && !pStmt->asyncResultAvailable) {
3,272✔
2742
    STMT2_ELOG_E("use result after callBackFn return");
96✔
2743
    return NULL;
96✔
2744
  }
2745

2746
  if (tsUseAdapter) {
3,176✔
2747
    TAOS_RES* res = (TAOS_RES*)pStmt->exec.pRequest;
2,208✔
2748
    pStmt->exec.pRequest = NULL;
2,208✔
2749
    return res;
2,208✔
2750
  }
2751

2752
  return pStmt->exec.pRequest;
968✔
2753
}
2754

2755
int32_t stmtAsyncBindThreadFunc(void* args) {
×
2756
  qInfo("async stmt bind thread started");
×
2757

2758
  ThreadArgs* targs = (ThreadArgs*)args;
×
2759
  STscStmt2*  pStmt = (STscStmt2*)targs->stmt;
×
2760

2761
  int code = taos_stmt2_bind_param(targs->stmt, targs->bindv, targs->col_idx);
×
2762
  targs->fp(targs->param, NULL, code);
×
2763
  (void)taosThreadMutexLock(&(pStmt->asyncBindParam.mutex));
×
2764
  (void)atomic_sub_fetch_8(&pStmt->asyncBindParam.asyncBindNum, 1);
×
2765
  (void)taosThreadCondSignal(&(pStmt->asyncBindParam.waitCond));
×
2766
  (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2767
  taosMemoryFree(args);
×
2768

2769
  qInfo("async stmt bind thread stopped");
×
2770

2771
  return code;
×
2772
}
2773

2774
void stmtBuildErrorMsg(STscStmt2* pStmt, const char* msg) {
288✔
2775
  if (pStmt == NULL || msg == NULL) {
288✔
2776
    return;
×
2777
  }
2778

2779
  if (pStmt->exec.pRequest == NULL) {
288✔
2780
    return;
×
2781
  }
2782

2783
  if (pStmt->exec.pRequest->msgBuf == NULL) {
288✔
2784
    return;
×
2785
  }
2786

2787
  size_t msgLen = strlen(msg);
288✔
2788
  size_t bufLen = pStmt->exec.pRequest->msgBufLen;
288✔
2789

2790
  if (msgLen >= bufLen) {
288✔
2791
    tstrncpy(pStmt->exec.pRequest->msgBuf, msg, bufLen - 1);
×
2792
    pStmt->exec.pRequest->msgBuf[bufLen - 1] = '\0';
×
2793
    pStmt->exec.pRequest->msgBufLen = bufLen - 1;
×
2794
  } else {
2795
    tstrncpy(pStmt->exec.pRequest->msgBuf, msg, bufLen);
288✔
2796
    pStmt->exec.pRequest->msgBufLen = msgLen;
288✔
2797
  }
2798

2799
  return;
288✔
2800
}
2801

2802
int32_t stmtBuildErrorMsgWithCode(STscStmt2* pStmt, const char* msg, int32_t errorCode) {
288✔
2803
  stmtBuildErrorMsg(pStmt, msg);
288✔
2804
  pStmt->errCode = errorCode;
288✔
2805

2806
  return errorCode;
288✔
2807
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc