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

taosdata / TDengine / #5006

29 Mar 2026 04:32AM UTC coverage: 72.274% (+0.1%) from 72.152%
#5006

push

travis-ci

web-flow
refactor: do some internal refactor for TDgpt. (#34955)

253711 of 351039 relevant lines covered (72.27%)

131490495.89 hits per line

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

76.6
/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,708,408✔
15
    *pBuf = (char*)pTblBuf->pCurBuff + pTblBuf->buffOffset;
1,708,722✔
16
    pTblBuf->buffOffset += pTblBuf->buffUnit;
1,708,488✔
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,708,525✔
41
}
42

43
static bool stmtDequeue(STscStmt2* pStmt, SStmtQNode** param) {
1,708,526✔
44
  int i = 0;
1,708,526✔
45
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
7,977,345✔
46
    if (pStmt->queue.stopQueue) {
6,396,253✔
47
      return false;
126,131✔
48
    }
49
    if (i < 10) {
6,270,374✔
50
      taosUsleep(1);
5,860,764✔
51
      i++;
5,859,393✔
52
    } else {
53
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
409,610✔
54
      if (pStmt->queue.stopQueue) {
409,581✔
55
        (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
×
56
        return false;
×
57
      }
58
      if (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
409,542✔
59
        (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
409,451✔
60
      }
61
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
409,452✔
62
    }
63
  }
64

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

69
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
1,581,814✔
70
  if (pStmt->queue.head == pStmt->queue.tail) {
1,582,347✔
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,582,308✔
78
  pStmt->queue.head->next = node->next;
1,582,308✔
79
  if (pStmt->queue.tail == node) {
1,582,267✔
80
    pStmt->queue.tail = pStmt->queue.head;
904,607✔
81
  }
82
  node->next = NULL;
1,582,308✔
83
  *param = node;
1,582,386✔
84

85
  (void)atomic_sub_fetch_64((int64_t*)&pStmt->queue.qRemainNum, 1);
1,582,345✔
86
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
1,582,386✔
87

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

90
  return true;
1,582,334✔
91
}
92

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

99
  param->next = NULL;
1,581,944✔
100

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

103
  pStmt->queue.tail->next = param;
1,582,308✔
104
  pStmt->queue.tail = param;
1,582,269✔
105
  pStmt->stat.bindDataNum++;
1,582,074✔
106

107
  (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
1,582,347✔
108
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
1,582,373✔
109

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

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

116
static int32_t stmtCreateRequest(STscStmt2* pStmt) {
45,597,176✔
117
  int32_t code = 0;
45,597,176✔
118

119
  if (pStmt->exec.pRequest == NULL) {
45,597,176✔
120
    code = buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false, &pStmt->exec.pRequest,
152,148✔
121
                        pStmt->reqid);
122
    if (pStmt->reqid != 0) {
152,148✔
123
      pStmt->reqid++;
8✔
124
    }
125
    pStmt->exec.pRequest->type = RES_TYPE__QUERY;
152,148✔
126
    if (pStmt->db != NULL) {
152,113✔
127
      taosMemoryFreeClear(pStmt->exec.pRequest->pDb);
142,025✔
128
      pStmt->exec.pRequest->pDb = taosStrdup(pStmt->db);
142,025✔
129
    }
130
    if (TSDB_CODE_SUCCESS == code) {
152,113✔
131
      pStmt->exec.pRequest->syncQuery = true;
152,113✔
132
      pStmt->exec.pRequest->stmtBindVersion = 2;
152,113✔
133
    }
134
    STMT2_DLOG("create request:0x%" PRIx64 ", QID:0x%" PRIx64, pStmt->exec.pRequest->self,
152,078✔
135
               pStmt->exec.pRequest->requestId);
136
  }
137

138
  return code;
45,872,615✔
139
}
140

141
static int32_t stmtSwitchStatus(STscStmt2* pStmt, STMT_STATUS newStatus) {
88,790,281✔
142
  int32_t code = 0;
88,790,281✔
143

144
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
88,790,281✔
145
    STMT2_LOG_SEQ(newStatus);
89,766,983✔
146
  }
147

148
  if (pStmt->errCode && newStatus != STMT_PREPARE) {
89,545,834✔
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) {
89,217,404✔
154
    case STMT_PREPARE:
142,846✔
155
      pStmt->errCode = 0;
142,846✔
156
      break;
95,764✔
157
    case STMT_SETTBNAME:
1,099,394✔
158
      if (STMT_STATUS_EQ(INIT)) {
1,099,394✔
159
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
160
      }
161
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
1,099,591✔
162
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
163
      }
164
      break;
1,099,511✔
165
    case STMT_SETTAGS:
676,091✔
166
      if (STMT_STATUS_EQ(INIT)) {
676,091✔
167
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
168
      }
169
      break;
676,091✔
170
    case STMT_FETCH_FIELDS:
17,483✔
171
      if (STMT_STATUS_EQ(INIT)) {
17,483✔
172
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
173
      }
174
      break;
17,483✔
175
    case STMT_BIND:
43,597,264✔
176
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
43,597,264✔
177
        code = TSDB_CODE_TSC_STMT_API_ERROR;
194✔
178
      }
179
      /*
180
            if ((pStmt->sql.type == STMT_TYPE_MULTI_INSERT) && ()) {
181
              code = TSDB_CODE_TSC_STMT_API_ERROR;
182
            }
183
      */
184
      break;
44,150,554✔
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:
43,046,485✔
191
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
43,046,485✔
192
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
193
      }
194
      break;
43,244,099✔
195
    case STMT_EXECUTE:
637,841✔
196
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
637,841✔
197
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
7,380✔
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)) {
630,309✔
203
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
204
        }
205
      }
206
      break;
637,728✔
207
    default:
×
208
      code = TSDB_CODE_APP_ERROR;
×
209
      break;
×
210
  }
211

212
  STMT_ERR_RET(code);
89,616,804✔
213

214
  pStmt->sql.status = newStatus;
89,616,610✔
215

216
  return TSDB_CODE_SUCCESS;
90,179,376✔
217
}
218

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

222
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
27,616✔
223

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

229
  *tbName = pStmt->bInfo.tbName;
19,057✔
230

231
  return TSDB_CODE_SUCCESS;
19,057✔
232
}
233

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

243
  if ((tags != NULL && ((SBoundColInfo*)tags)->numOfCols == 0) || !autoCreateTbl) {
134,965✔
244
    pStmt->sql.autoCreateTbl = false;
113,544✔
245
  }
246

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

251
  pStmt->bInfo.tbUid = autoCreateTbl ? 0 : pTableMeta->uid;
134,965✔
252
  pStmt->bInfo.tbSuid = pTableMeta->suid;
134,709✔
253
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
134,965✔
254
  pStmt->bInfo.tbType = pTableMeta->tableType;
134,965✔
255

256
  if (!pStmt->bInfo.tagsCached) {
134,965✔
257
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
133,761✔
258
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
133,585✔
259
  }
260

261
  // transfer ownership of cols to stmt
262
  if (cols) {
134,970✔
263
    pStmt->bInfo.fixedValueCols = *cols;
134,965✔
264
    *cols = NULL;
134,925✔
265
  }
266

267
  pStmt->bInfo.boundTags = tags;
134,935✔
268
  pStmt->bInfo.tagsCached = false;
134,890✔
269
  pStmt->bInfo.tbNameFlag = tbNameFlag;
134,965✔
270
  tstrncpy(pStmt->bInfo.stbFName, sTableName, sizeof(pStmt->bInfo.stbFName));
134,965✔
271

272
  if (pTableMeta->tableType != TSDB_CHILD_TABLE && pTableMeta->tableType != TSDB_SUPER_TABLE) {
134,932✔
273
    pStmt->sql.stbInterlaceMode = false;
4,298✔
274
  }
275

276
  return TSDB_CODE_SUCCESS;
134,930✔
277
}
278

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

282
  pStmt->sql.pVgHash = pVgHash;
134,965✔
283
  pStmt->exec.pBlockHash = pBlockHash;
134,965✔
284

285
  return TSDB_CODE_SUCCESS;
134,857✔
286
}
287

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

293
  STMT_ERR_RET(stmtUpdateBindInfo(stmt, pTableMeta, tags, cols, tbName, sTableName, autoCreateTbl, tbNameFlag));
134,930✔
294
  STMT_ERR_RET(stmtUpdateExecInfo(stmt, pVgHash, pBlockHash));
134,895✔
295

296
  pStmt->sql.autoCreateTbl = autoCreateTbl;
134,965✔
297

298
  return TSDB_CODE_SUCCESS;
134,925✔
299
}
300

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

304
  *pVgHash = pStmt->sql.pVgHash;
3,515✔
305
  pStmt->sql.pVgHash = NULL;
3,515✔
306

307
  *pBlockHash = pStmt->exec.pBlockHash;
3,515✔
308
  pStmt->exec.pBlockHash = NULL;
3,515✔
309

310
  return TSDB_CODE_SUCCESS;
3,515✔
311
}
312

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

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

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

326
  pStmt->stat.parseSqlNum++;
144,091✔
327

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

331
  pStmt->sql.siInfo.pQuery = pStmt->sql.pQuery;
140,529✔
332

333
  pStmt->bInfo.needParse = false;
140,529✔
334

335
  if (pStmt->sql.type == 0) {
140,566✔
336
    if (pStmt->sql.pQuery->pRoot && LEGAL_INSERT(nodeType(pStmt->sql.pQuery->pRoot))) {
114,079✔
337
      pStmt->sql.type = STMT_TYPE_INSERT;
108,406✔
338
      pStmt->sql.stbInterlaceMode = false;
108,476✔
339
    } else if (pStmt->sql.pQuery->pPrepareRoot && LEGAL_SELECT(nodeType(pStmt->sql.pQuery->pPrepareRoot))) {
5,673✔
340
      pStmt->sql.type = STMT_TYPE_QUERY;
5,634✔
341
      pStmt->sql.stbInterlaceMode = false;
5,634✔
342

343
      return TSDB_CODE_SUCCESS;
5,634✔
344
    } else {
345
      STMT2_ELOG_E("only support select or insert sql");
37✔
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) {
26,452✔
352
    pStmt->sql.stbInterlaceMode = false;
×
353
    return TSDB_CODE_SUCCESS;
×
354
  } else if (pStmt->sql.type == STMT_TYPE_INSERT) {
26,452✔
355
    pStmt->sql.stbInterlaceMode = false;
×
356
  }
357

358
  STableDataCxt** pSrc =
359
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
134,893✔
360
  if (NULL == pSrc || NULL == *pSrc) {
134,965✔
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;
134,965✔
366
  if (pStmt->sql.stbInterlaceMode && pTableCtx->pData->pCreateTbReq && (pStmt->bInfo.tbNameFlag & USING_CLAUSE) == 0) {
134,930✔
367
    STMT2_TLOG("destroy pCreateTbReq for no-using insert, tbFName:%s", pStmt->bInfo.tbFName);
3,492✔
368
    tdDestroySVCreateTbReq(pTableCtx->pData->pCreateTbReq);
3,492✔
369
    taosMemoryFreeClear(pTableCtx->pData->pCreateTbReq);
3,492✔
370
    pTableCtx->pData->pCreateTbReq = NULL;
3,492✔
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) {
134,965✔
386
    pStmt->sql.pBindInfo = taosMemoryMalloc(pTableCtx->boundColsInfo.numOfBound * sizeof(*pStmt->sql.pBindInfo));
131,415✔
387
    if (NULL == pStmt->sql.pBindInfo) {
131,450✔
388
      STMT2_ELOG_E("fail to malloc pBindInfo");
×
389
      return terrno;
×
390
    }
391
  }
392

393
  return TSDB_CODE_SUCCESS;
134,930✔
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) {
262,236✔
520
  if (pStmt->exec.pRequest) {
262,236✔
521
    taos_free_result(pStmt->exec.pRequest);
147,298✔
522
    pStmt->exec.pRequest = NULL;
147,265✔
523
  }
524
  pStmt->asyncResultAvailable = false;
262,203✔
525
}
262,203✔
526

527
static int32_t stmtCleanBindInfo(STscStmt2* pStmt) {
805,477✔
528
  pStmt->bInfo.tbUid = 0;
805,477✔
529
  pStmt->bInfo.tbVgId = -1;
805,477✔
530
  pStmt->bInfo.tbType = 0;
805,425✔
531
  pStmt->bInfo.needParse = true;
805,438✔
532
  pStmt->bInfo.inExecCache = false;
805,464✔
533

534
  pStmt->bInfo.tbName[0] = 0;
805,396✔
535
  pStmt->bInfo.tbFName[0] = 0;
805,464✔
536
  if (!pStmt->bInfo.tagsCached) {
805,292✔
537
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
574,773✔
538
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
574,880✔
539
    pStmt->bInfo.boundTags = NULL;
574,906✔
540
  }
541

542
  if (!pStmt->bInfo.boundColsCached) {
805,196✔
543
    tSimpleHashCleanup(pStmt->bInfo.fixedValueCols);
293,427✔
544
    pStmt->bInfo.fixedValueCols = NULL;
293,643✔
545
  }
546

547
  if (!pStmt->sql.autoCreateTbl) {
805,412✔
548
    pStmt->bInfo.stbFName[0] = 0;
557,324✔
549
    pStmt->bInfo.tbSuid = 0;
557,285✔
550
  }
551

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

555
  return TSDB_CODE_SUCCESS;
805,321✔
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) {
511,613✔
564
  pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, 0);
511,613✔
565
  if (NULL == pTblBuf->pCurBuff) {
511,873✔
566
    tscError("QInfo:%p, fail to get buffer from list", pTblBuf);
65✔
567
    return;
×
568
  }
569
  pTblBuf->buffIdx = 1;
511,808✔
570
  pTblBuf->buffOffset = sizeof(*pQueue->head);
511,808✔
571

572
  pQueue->head = pQueue->tail = pTblBuf->pCurBuff;
511,808✔
573
  pQueue->qRemainNum = 0;
511,769✔
574
  pQueue->head->next = NULL;
511,574✔
575
}
576

577
static int32_t stmtCleanExecInfo(STscStmt2* pStmt, bool keepTable, bool deepClean) {
779,199✔
578
  if (pStmt->sql.stbInterlaceMode) {
779,199✔
579
    if (deepClean) {
525,442✔
580
      taosHashCleanup(pStmt->exec.pBlockHash);
13,738✔
581
      pStmt->exec.pBlockHash = NULL;
13,712✔
582

583
      if (NULL != pStmt->exec.pCurrBlock) {
13,712✔
584
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->boundColsInfo.pColIndex);
11,856✔
585
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->pData);
11,856✔
586
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
11,856✔
587
        pStmt->exec.pCurrBlock = NULL;
11,856✔
588
      }
589
      if (STMT_TYPE_QUERY != pStmt->sql.type) {
13,712✔
590
        resetRequest(pStmt);
13,712✔
591
      }
592
    } else {
593
      pStmt->sql.siInfo.pTableColsIdx = 0;
511,704✔
594
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
511,743✔
595
      tSimpleHashClear(pStmt->sql.siInfo.pTableRowDataHash);
511,756✔
596
    }
597
    if (NULL != pStmt->exec.pRequest) {
525,650✔
598
      pStmt->exec.pRequest->body.resInfo.numOfRows = 0;
511,886✔
599
    }
600
  } else {
601
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
254,043✔
602
      resetRequest(pStmt);
246,663✔
603
    }
604

605
    size_t keyLen = 0;
253,883✔
606
    void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
253,883✔
607
    while (pIter) {
500,602✔
608
      STableDataCxt* pBlocks = *(STableDataCxt**)pIter;
246,507✔
609
      char*          key = taosHashGetKey(pIter, &keyLen);
246,507✔
610
      STableMeta*    pMeta = qGetTableMetaInDataBlock(pBlocks);
246,507✔
611

612
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
246,507✔
613
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
117,838✔
614
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
146,029✔
615

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

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

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

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

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

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

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

643
  return TSDB_CODE_SUCCESS;
654,356✔
644
}
645

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

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

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

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

674
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
142,652✔
675
  while (pIter) {
148,469✔
676
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
5,843✔
677

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

682
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
5,843✔
683
  }
684
  taosHashCleanup(pStmt->sql.pTableCache);
142,626✔
685
  pStmt->sql.pTableCache = NULL;
142,652✔
686

687
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
142,652✔
688
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
142,600✔
689

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

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

703
  STMT2_TLOG_E("end to free SQL info");
142,626✔
704

705
  return TSDB_CODE_SUCCESS;
142,652✔
706
}
707

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

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

719
  int32_t code = catalogGetTableHashVgroup(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &vgInfo);
661,789✔
720
  if (TSDB_CODE_SUCCESS != code) {
661,854✔
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));
661,854✔
727
  if (TSDB_CODE_SUCCESS != code) {
661,880✔
728
    STMT2_ELOG("fail to put vgroup info, code:%d", code);
91✔
729
    return code;
×
730
  }
731

732
  *vgId = vgInfo.vgId;
661,789✔
733

734
  return TSDB_CODE_SUCCESS;
661,789✔
735
}
736

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

745
  pStmt->stat.ctgGetTbMetaNum++;
8,717✔
746

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

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

756
    STMT_ERR_RET(code);
×
757
  }
758

759
  STMT_ERR_RET(code);
7,747✔
760

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

767
  taosMemoryFree(pTableMeta);
7,705✔
768

769
  return TSDB_CODE_SUCCESS;
7,747✔
770
}
771

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

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

779
  return TSDB_CODE_SUCCESS;
14,119✔
780
}
781

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

789
  pStmt->bInfo.needParse = true;
39,966✔
790
  pStmt->bInfo.inExecCache = false;
39,966✔
791

792
  STableDataCxt** pCxtInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
39,966✔
793
  if (pCxtInExec) {
39,966✔
794
    pStmt->bInfo.needParse = false;
6,596✔
795
    pStmt->bInfo.inExecCache = true;
6,596✔
796

797
    pStmt->exec.pCurrBlock = *pCxtInExec;
6,596✔
798

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

805
  if (NULL == pStmt->pCatalog) {
34,146✔
806
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
14,258✔
807
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
14,258✔
808
  }
809

810
  if (NULL == pStmt->sql.pTableCache || taosHashGetSize(pStmt->sql.pTableCache) <= 0) {
34,146✔
811
    if (pStmt->bInfo.inExecCache) {
19,057✔
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);
19,057✔
818

819
    return TSDB_CODE_SUCCESS;
19,057✔
820
  }
821

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

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

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

836
      pStmt->exec.pCurrBlock = pNewBlock;
11,791✔
837

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

840
      return TSDB_CODE_SUCCESS;
11,791✔
841
    }
842

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

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

850
  STMT_ERR_RET(stmtGetTableMetaAndValidate(pStmt, &uid, &suid, &vgId, &tableType));
3,298✔
851

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

854
  if (uid == pStmt->bInfo.tbUid) {
3,104✔
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) {
3,104✔
863
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
776✔
864
    if (NULL == pCache) {
776✔
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;
776✔
872

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

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

881
    return TSDB_CODE_SUCCESS;
776✔
882
  }
883

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

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

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

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

902
    pStmt->exec.pCurrBlock = pNewBlock;
2,328✔
903

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

906
    return TSDB_CODE_SUCCESS;
2,328✔
907
  }
908

909
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
910

911
  return TSDB_CODE_SUCCESS;
×
912
}
913

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

917
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
×
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

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

925
  return TSDB_CODE_SUCCESS;
×
926
}
927

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

931
  if (pParam->restoreTbCols) {
1,582,256✔
932
    for (int32_t i = 0; i < pStmt->sql.siInfo.pTableColsIdx; ++i) {
1,581,609✔
933
      SArray** p = (SArray**)TARRAY_GET_ELEM(pStmt->sql.siInfo.pTableCols, i);
1,069,633✔
934
      *p = taosArrayInit(20, POINTER_BYTES);
1,069,594✔
935
      if (*p == NULL) {
1,069,516✔
936
        pStmt->errCode = terrno;
×
937
      }
938
    }
939
    atomic_store_8((int8_t*)&pStmt->sql.siInfo.tableColsReady, true);
512,015✔
940
    STMT2_TLOG_E("restore pTableCols finished");
512,067✔
941
  } else {
942
    int code = qAppendStmt2TableOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, &pParam->tblData, pStmt->exec.pCurrBlock,
1,070,202✔
943
                                       &pStmt->sql.siInfo, pParam->pCreateTbReq);
944
    // taosMemoryFree(pParam->pTbData);
945
    if (code != TSDB_CODE_SUCCESS) {
1,070,267✔
946
      STMT2_ELOG("async append stmt output failed, tbname:%s, err:%s", pParam->tblData.tbName, tstrerror(code));
194✔
947
      pStmt->errCode = code;
194✔
948
    }
949
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
1,070,267✔
950
  }
951
}
1,582,399✔
952

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

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

959
  while (true) {
1,582,399✔
960
    SStmtQNode* asyncParam = NULL;
1,708,724✔
961

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

970
    stmtAsyncOutput(pStmt, asyncParam);
1,582,243✔
971
  }
972

973
  STMT2_DLOG_E("stmt2 bind thread stopped");
126,014✔
974
  return NULL;
126,131✔
975
}
976

977
static int32_t stmtStartBindThread(STscStmt2* pStmt) {
126,325✔
978
  TdThreadAttr thAttr;
99,708✔
979
  if (taosThreadAttrInit(&thAttr) != 0) {
126,325✔
980
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
981
  }
982
  if (taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE) != 0) {
126,325✔
983
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
984
  }
985

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

991
  pStmt->bindThreadInUse = true;
126,325✔
992

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

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

1003
  return TSDB_CODE_SUCCESS;
126,325✔
1004
}
1005

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

1011
  return TSDB_CODE_SUCCESS;
141,876✔
1012
}
1013

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

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

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

1034
  return TSDB_CODE_SUCCESS;
126,325✔
1035
}
1036

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

1042
  pStmt = taosMemoryCalloc(1, sizeof(STscStmt2));
136,345✔
1043
  if (NULL == pStmt) {
136,689✔
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);
136,689✔
1049
  if (NULL == pStmt->sql.pTableCache) {
136,689✔
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;
136,689✔
1056
  if (taos->db[0] != '\0') {
136,689✔
1057
    pStmt->db = taosStrdup(taos->db);
126,019✔
1058
  }
1059
  pStmt->bInfo.needParse = true;
136,689✔
1060
  pStmt->sql.status = STMT_INIT;
136,689✔
1061
  pStmt->errCode = TSDB_CODE_SUCCESS;
136,689✔
1062

1063
  if (NULL != pOptions) {
136,689✔
1064
    (void)memcpy(&pStmt->options, pOptions, sizeof(pStmt->options));
136,666✔
1065
    if (pOptions->singleStbInsert && pOptions->singleTableBindOnce) {
136,666✔
1066
      pStmt->stbInterlaceMode = true;
121,138✔
1067
    }
1068

1069
    pStmt->reqid = pOptions->reqid;
136,666✔
1070
  }
1071

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

1078
    pStmt->sql.siInfo.pTableHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
121,138✔
1079
    if (NULL == pStmt->sql.siInfo.pTableHash) {
121,138✔
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));
121,138✔
1086
    if (NULL == pStmt->sql.siInfo.pTableRowDataHash) {
121,138✔
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);
121,138✔
1093
    if (NULL == pStmt->sql.siInfo.pTableCols) {
121,138✔
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);
121,138✔
1100
    if (TSDB_CODE_SUCCESS == code) {
121,138✔
1101
      code = stmtInitQueue(pStmt);
121,138✔
1102
    }
1103
    if (TSDB_CODE_SUCCESS == code) {
121,138✔
1104
      code = stmtStartBindThread(pStmt);
121,138✔
1105
    }
1106
    if (TSDB_CODE_SUCCESS != code) {
121,138✔
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;
136,689✔
1115
  if (pStmt->options.asyncExecFn) {
136,689✔
1116
    if (tsem_init(&pStmt->asyncExecSem, 0, 1) != 0) {
2,134✔
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);
136,689✔
1124
  if (TSDB_CODE_SUCCESS != code) {
136,689✔
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;
136,689✔
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,
136,689✔
1137
             pStmt->stbInterlaceMode, pStmt->options.asyncExecFn != NULL);
1138

1139
  return pStmt;
136,689✔
1140
}
1141

1142
static int stmtSetDbName2(TAOS_STMT2* stmt, const char* dbName) {
125,261✔
1143
  STscStmt2* pStmt = (STscStmt2*)stmt;
125,261✔
1144
  if (dbName == NULL || dbName[0] == '\0') {
125,261✔
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);
125,261✔
1150
  if (pStmt->db == NULL || pStmt->db[0] == '\0') {
125,261✔
1151
    taosMemoryFreeClear(pStmt->db);
9,506✔
1152
    STMT2_DLOG("dbname:%s is by sql, not by taosconnect", dbName);
9,506✔
1153
    pStmt->db = taosStrdup(dbName);
9,506✔
1154
    (void)strdequote(pStmt->db);
9,506✔
1155
  }
1156
  STMT_ERR_RET(stmtCreateRequest(pStmt));
125,298✔
1157

1158
  // The SQL statement specifies a database name, overriding the previously specified database
1159
  taosMemoryFreeClear(pStmt->exec.pRequest->pDb);
125,261✔
1160
  pStmt->exec.pRequest->pDb = taosStrdup(dbName);
125,224✔
1161
  (void)strdequote(pStmt->exec.pRequest->pDb);
125,224✔
1162
  if (pStmt->exec.pRequest->pDb == NULL) {
125,333✔
1163
    return terrno;
×
1164
  }
1165
  if (pStmt->sql.stbInterlaceMode) {
125,298✔
1166
    pStmt->sql.siInfo.dbname = pStmt->exec.pRequest->pDb;
114,240✔
1167
  }
1168
  return TSDB_CODE_SUCCESS;
125,298✔
1169
}
1170
static int32_t stmtResetStbInterlaceCache(STscStmt2* pStmt) {
5,187✔
1171
  int32_t code = TSDB_CODE_SUCCESS;
5,187✔
1172

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

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

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

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

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

1201
  return TSDB_CODE_SUCCESS;
5,187✔
1202
}
1203

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

1211
  pStmt->errCode = 0;
6,739✔
1212

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

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

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

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

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

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

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

1260
    STMT_ERR_RET(stmtResetStbInterlaceCache(pStmt));
5,187✔
1261

1262
    int32_t code = stmtIniAsyncBind(pStmt);
5,187✔
1263
    if (TSDB_CODE_SUCCESS != code) {
5,187✔
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;
6,739✔
1271
  pStmt->options = options;
6,739✔
1272
  pStmt->reqid = reqid;
6,739✔
1273
  pStmt->stbInterlaceMode = stbInterlaceMode;
6,739✔
1274

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

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

1285
  return TSDB_CODE_SUCCESS;
6,739✔
1286
}
1287

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

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

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

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

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

1310
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
142,846✔
1311

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

1323
  if (stmt2IsInsert(pStmt)) {
142,811✔
1324
    pStmt->sql.stbInterlaceMode = pStmt->stbInterlaceMode;
136,436✔
1325
    char* dbName = NULL;
136,401✔
1326
    if (qParseDbName(sql, length, &dbName)) {
136,401✔
1327
      STMT_ERR_RET(stmtSetDbName2(stmt, dbName));
125,298✔
1328
      taosMemoryFreeClear(dbName);
125,335✔
1329
    } else if (pStmt->db != NULL && pStmt->db[0] != '\0') {
11,090✔
1330
      taosMemoryFreeClear(pStmt->exec.pRequest->pDb);
10,909✔
1331
      pStmt->exec.pRequest->pDb = taosStrdup(pStmt->db);
10,909✔
1332
      if (pStmt->exec.pRequest->pDb == NULL) {
10,909✔
1333
        STMT_ERR_RET(terrno);
×
1334
      }
1335
      (void)strdequote(pStmt->exec.pRequest->pDb);
10,909✔
1336

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

1342
  } else if (stmt2IsSelect(pStmt)) {
6,410✔
1343
    pStmt->sql.stbInterlaceMode = false;
5,828✔
1344
    STMT_ERR_RET(stmtParseSql(pStmt));
5,828✔
1345
  } else {
1346
    return stmtBuildErrorMsgWithCode(pStmt, "stmt only support 'SELECT' or 'INSERT'", TSDB_CODE_PAR_SYNTAX_ERROR);
582✔
1347
  }
1348
  return TSDB_CODE_SUCCESS;
142,035✔
1349
}
1350

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

1358
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
11,856✔
1359
  pStmt->sql.siInfo.pDataCtx = pDst;
11,816✔
1360

1361
  SArray* pTblCols = NULL;
11,856✔
1362
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
11,848,629✔
1363
    pTblCols = taosArrayInit(20, POINTER_BYTES);
11,836,773✔
1364
    if (NULL == pTblCols) {
11,831,205✔
1365
      return terrno;
×
1366
    }
1367

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

1373
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
11,856✔
1374

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

1378
  return TSDB_CODE_SUCCESS;
11,856✔
1379
}
1380

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

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

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

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

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

1402
  int64_t startUs = taosGetTimestampUs();
1,099,420✔
1403

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

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

1410
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
1,099,576✔
1411

1412
  int32_t insert = 0;
1,099,474✔
1413
  if (!stmt2IsInsert(stmt)) {
1,099,474✔
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,099,472✔
1419

1420
  STMT_ERR_RET(qCreateSName2(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
1,099,524✔
1421
                             pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1422
  STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
1,098,111✔
1423
  tstrncpy(pStmt->bInfo.tbName, (char*)tNameGetTableName(&pStmt->bInfo.sname), TSDB_TABLE_NAME_LEN);
1,098,345✔
1424

1425
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
1,098,228✔
1426
    STMT_ERR_RET(stmtGetFromCache(pStmt));
40,187✔
1427

1428
    if (pStmt->bInfo.needParse) {
39,772✔
1429
      STMT_ERR_RET(stmtParseSql(pStmt));
19,057✔
1430
      if (!pStmt->sql.autoCreateTbl) {
19,057✔
1431
        uint64_t uid, suid;
2,378✔
1432
        int32_t  vgId;
2,378✔
1433
        int8_t   tableType;
2,378✔
1434

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

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

1447
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
1,097,193✔
1448
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
11,814✔
1449
  }
1450

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

1454
  return TSDB_CODE_SUCCESS;
1,097,403✔
1455
}
1456

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

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

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

1469
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
566,219✔
1470

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

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

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

1486
  STableDataCxt** pDataBlock = NULL;
566,284✔
1487
  if (pStmt->exec.pCurrBlock) {
566,284✔
1488
    pDataBlock = &pStmt->exec.pCurrBlock;
555,544✔
1489
  } else {
1490
    pDataBlock =
1491
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
10,740✔
1492
    if (NULL == pDataBlock) {
10,740✔
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) {
10,740✔
1497
      tdDestroySVCreateTbReq((*pDataBlock)->pData->pCreateTbReq);
388✔
1498
      taosMemoryFreeClear((*pDataBlock)->pData->pCreateTbReq);
388✔
1499
      (*pDataBlock)->pData->pCreateTbReq = NULL;
388✔
1500
    }
1501
  }
1502
  if (pStmt->bInfo.inExecCache && !pStmt->sql.autoCreateTbl) {
566,284✔
1503
    return TSDB_CODE_SUCCESS;
×
1504
  }
1505

1506
  STMT2_TLOG_E("start to bind stmt tag values");
566,284✔
1507

1508
  void* boundTags = NULL;
566,193✔
1509
  if (pStmt->sql.stbInterlaceMode) {
566,193✔
1510
    boundTags = pStmt->sql.siInfo.boundTags;
542,933✔
1511
    *pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
542,933✔
1512
    if (NULL == pCreateTbReq) {
542,959✔
1513
      return terrno;
×
1514
    }
1515
    int32_t vgId = -1;
542,959✔
1516
    STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
542,959✔
1517
    (*pCreateTbReq)->uid = vgId;
543,050✔
1518
  } else {
1519
    boundTags = pStmt->bInfo.boundTags;
23,260✔
1520
  }
1521

1522
  STMT_ERR_RET(qBindStmtTagsValue2(*pDataBlock, boundTags, pStmt->bInfo.tbSuid, pStmt->bInfo.stbFName,
566,310✔
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;
566,168✔
1527
}
1528

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

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

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

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

1542
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
109,846✔
1543

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

1557
  STMT_ERR_RET(stmtCreateRequest(pStmt));
3,286✔
1558
  if (pStmt->bInfo.needParse) {
3,286✔
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) {
3,286✔
1567
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1568
  }
1569

1570
  STableDataCxt** pDataBlock = NULL;
3,286✔
1571
  if (pStmt->exec.pCurrBlock) {
3,286✔
1572
    pDataBlock = &pStmt->exec.pCurrBlock;
×
1573
  } else {
1574
    pDataBlock =
1575
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
3,286✔
1576
    if (NULL == pDataBlock) {
3,286✔
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)) {
3,286✔
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) {
3,286✔
1589
    STMT2_TLOG_E("tags are fixed, set createTbReq first time");
3,286✔
1590
    pStmt->sql.fixValueTags = true;
3,286✔
1591
    STMT_ERR_RET(cloneSVreateTbReq((*pDataBlock)->pData->pCreateTbReq, &pStmt->sql.fixValueTbReq));
3,286✔
1592
    STMT_ERR_RET(cloneSVreateTbReq(pStmt->sql.fixValueTbReq, pCreateTbReq));
3,286✔
1593
    (*pCreateTbReq)->uid = (*pDataBlock)->pMeta->vgId;
3,286✔
1594

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

1601
  return TSDB_CODE_SUCCESS;
3,286✔
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) {
10,499✔
1633
  int32_t code = 0;
10,499✔
1634
  int32_t preCode = pStmt->errCode;
10,499✔
1635

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

1640
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
10,499✔
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;
10,499✔
1646
  bool            cleanStb = false;
10,499✔
1647

1648
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx != NULL) {
10,499✔
1649
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
1,164✔
1650
  } else {
1651
    cleanStb = true;
9,335✔
1652
    pDataBlock =
1653
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
9,335✔
1654
  }
1655

1656
  if (NULL == pDataBlock || NULL == *pDataBlock) {
10,499✔
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;
10,499✔
1662
  pStmt->sql.placeholderOfCols = 0;
10,499✔
1663
  int32_t totalNum = 0;
10,499✔
1664
  STMT_ERRI_JRET(qBuildStmtStbColFields(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.fixedValueCols,
10,499✔
1665
                                        pStmt->bInfo.tbNameFlag, &totalNum, fields, &pStmt->sql.placeholderOfTags,
1666
                                        &pStmt->sql.placeholderOfCols));
1667

1668
  if (pStmt->bInfo.tbType == TSDB_SUPER_TABLE && cleanStb) {
10,499✔
1669
    taosMemoryFreeClear((*pDataBlock)->boundColsInfo.pColIndex);
7,395✔
1670
    qDestroyStmtDataBlock(*pDataBlock);
7,395✔
1671
    *pDataBlock = NULL;
7,395✔
1672
    if (taosHashRemove(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName)) != 0) {
7,395✔
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;
7,395✔
1677
    pStmt->bInfo.tagsCached = false;
7,395✔
1678
    pStmt->bInfo.sname = (SName){0};
7,395✔
1679
    STMT_ERR_RET(stmtCleanBindInfo(pStmt));
7,395✔
1680
  }
1681

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

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

1689
_return:
10,499✔
1690

1691
  pStmt->errCode = preCode;
10,499✔
1692

1693
  return code;
10,499✔
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,069,916✔
1713
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
1,069,916✔
1714
    pStmt->sql.siInfo.pVgroupHash =
512,545✔
1715
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
512,636✔
1716
  }
1717
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
1,070,020✔
1718
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
512,545✔
1719
  }
1720

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

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

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

1734
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
1,069,929✔
1735
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
27,496✔
1736
    pStmt->sql.siInfo.tbFromHash = true;
4,389✔
1737
  }
1738

1739
  if (0 == pStmt->sql.siInfo.firstName[0]) {
1,070,046✔
1740
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
11,468✔
1741
  }
1742

1743
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
1,070,007✔
1744
  param->next = NULL;
1,069,968✔
1745

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

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

1754
  return TSDB_CODE_SUCCESS;
1,070,174✔
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,070,304✔
1760
      *pTableCols = (SArray*)taosArrayGetP(pStmt->sql.siInfo.pTableCols, pStmt->sql.siInfo.pTableColsIdx++);
1,070,380✔
1761
      break;
1,070,365✔
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,070,365✔
1778
}
1779

1780
static int32_t stmtCacheBlock(STscStmt2* pStmt) {
42,364,747✔
1781
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
42,364,747✔
1782
    return TSDB_CODE_SUCCESS;
42,702,756✔
1783
  }
1784

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

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

1793
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
6,037✔
1794
  if (!pSrc) {
6,037✔
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;
6,037✔
1799

1800
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
6,037✔
1801

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

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

1812
  if (pStmt->sql.autoCreateTbl) {
6,037✔
1813
    pStmt->bInfo.tagsCached = true;
5,067✔
1814
  } else {
1815
    pStmt->bInfo.boundTags = NULL;
970✔
1816
  }
1817

1818
  return TSDB_CODE_SUCCESS;
6,037✔
1819
}
1820

1821
static int stmtAddBatch2(TAOS_STMT2* stmt) {
42,872,481✔
1822
  STscStmt2* pStmt = (STscStmt2*)stmt;
42,872,481✔
1823

1824
  int64_t startUs = taosGetTimestampUs();
43,481,247✔
1825

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

1828
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
43,481,247✔
1829
    return pStmt->errCode;
×
1830
  }
1831

1832
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
43,511,654✔
1833

1834
  if (pStmt->sql.stbInterlaceMode) {
43,297,787✔
1835
    int64_t startUs2 = taosGetTimestampUs();
512,093✔
1836
    pStmt->stat.addBatchUs += startUs2 - startUs;
512,093✔
1837

1838
    pStmt->sql.siInfo.tableColsReady = false;
511,937✔
1839

1840
    SStmtQNode* param = NULL;
512,054✔
1841
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
1,023,796✔
1842
    param->restoreTbCols = true;
511,859✔
1843
    param->next = NULL;
512,015✔
1844

1845
    if (pStmt->sql.autoCreateTbl) {
511,937✔
1846
      pStmt->bInfo.tagsCached = true;
219,945✔
1847
    }
1848
    pStmt->bInfo.boundColsCached = true;
511,820✔
1849

1850
    if (pStmt->queue.stopQueue) {
511,898✔
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);
511,859✔
1856

1857
    return TSDB_CODE_SUCCESS;
512,067✔
1858
  }
1859

1860
  STMT_ERR_RET(stmtCacheBlock(pStmt));
42,892,125✔
1861

1862
  return TSDB_CODE_SUCCESS;
42,674,569✔
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) {
7,574✔
1933
  SCatalogSyncCbParam* pCbParam = (SCatalogSyncCbParam*)param;
7,574✔
1934
  if (TSDB_CODE_SUCCESS == code && pResultMeta) {
7,574✔
1935
    *pCbParam->pRsp = *pResultMeta;
7,574✔
1936
    TAOS_MEMSET(pResultMeta, 0, sizeof(SMetaData));  // Clear to avoid double free
7,574✔
1937
  }
1938
  pCbParam->code = code;
7,574✔
1939
  if (tsem_post(&pCbParam->sem) != 0) {
7,574✔
1940
    tscError("failed to post semaphore");
×
1941
  }
1942
}
7,574✔
1943

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

1953
  TAOS_MEMSET(pMetaData, 0, sizeof(SMetaData));
7,574✔
1954

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

1975
      if (tsem_destroy(&cbParam.sem) != 0) {
7,574✔
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
  // metaCache currently holds "reserved/request" structures built by collectMetaKey/buildCatalogReq.
1985
  // It must be destroyed with request=true to release nested table-request hashes.
1986
  destoryParseMetaCache(&metaCache, true);
7,574✔
1987
  destoryCatalogReq(&catalogReq);
7,574✔
1988

1989
  if (TSDB_CODE_SUCCESS != code) {
7,574✔
1990
    catalogFreeMetaData(pMetaData);
×
1991
  }
1992

1993
  return code;
7,574✔
1994
}
1995

1996
int stmtBindBatch2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* bind, int32_t colIdx, SVCreateTbReq* pCreateTbReq) {
43,391,896✔
1997
  STscStmt2* pStmt = (STscStmt2*)stmt;
43,391,896✔
1998
  int32_t    code = 0;
43,391,896✔
1999

2000
  int64_t startUs = taosGetTimestampUs();
43,699,398✔
2001

2002
  if (qDebugFlag & DEBUG_TRACE) {
43,699,398✔
2003
    (void)stmtPrintBindv(stmt, bind, colIdx, false);
×
2004
  }
2005

2006
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
43,690,033✔
2007
    return pStmt->errCode;
194✔
2008
  }
2009

2010
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
43,644,301✔
2011

2012
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
44,166,535✔
2013
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
2014
    pStmt->bInfo.needParse = false;
×
2015
  }
2016

2017
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
43,564,696✔
2018
    resetRequest(pStmt);
1,746✔
2019
  }
2020

2021
  STMT_ERR_RET(stmtCreateRequest(pStmt));
43,602,954✔
2022
  if (pStmt->bInfo.needParse) {
43,763,998✔
2023
    code = stmtParseSql(pStmt);
106,379✔
2024
    if (code != TSDB_CODE_SUCCESS) {
106,346✔
2025
      goto cleanup_root;
×
2026
    }
2027
  }
2028

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

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

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

2075
    if (pStmt->sql.pQuery->haveResultSet) {
7,380✔
2076
      STMT_ERR_RET(setResSchemaInfo(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->pResSchema,
14,954✔
2077
                                    pStmt->sql.pQuery->numOfResCols, pStmt->sql.pQuery->pResExtSchema, true));
2078
      taosMemoryFreeClear(pStmt->sql.pQuery->pResSchema);
7,380✔
2079
      taosMemoryFreeClear(pStmt->sql.pQuery->pResExtSchema);
7,380✔
2080
      setResPrecision(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->precision);
7,380✔
2081
    }
2082

2083
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
7,380✔
2084
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
7,380✔
2085
    TSWAP(pStmt->exec.pRequest->targetTableList, pStmt->sql.pQuery->pTargetTableList);
7,380✔
2086

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

2091
    // STMT_ERR_RET(stmtBackupQueryFields(pStmt));
2092

2093
    return TSDB_CODE_SUCCESS;
7,380✔
2094

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

2104
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
43,824,894✔
2105
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
2106
  }
2107

2108
  STableDataCxt** pDataBlock = NULL;
43,432,983✔
2109

2110
  if (pStmt->exec.pCurrBlock) {
43,432,983✔
2111
    pDataBlock = &pStmt->exec.pCurrBlock;
43,864,215✔
2112
  } else {
2113
    pDataBlock =
2114
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
124,854✔
2115
    if (NULL == pDataBlock) {
124,854✔
2116
      STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
2117
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
2118
    }
2119
    pStmt->exec.pCurrBlock = *pDataBlock;
124,854✔
2120
    if (pStmt->sql.stbInterlaceMode) {
124,854✔
2121
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
11,856✔
2122
      (*pDataBlock)->pData->aCol = NULL;
11,856✔
2123
    }
2124
    if (colIdx < -1) {
124,854✔
2125
      pStmt->sql.bindRowFormat = true;
194✔
2126
      taosArrayDestroy((*pDataBlock)->pData->aCol);
194✔
2127
      (*pDataBlock)->pData->aCol = taosArrayInit(20, POINTER_BYTES);
194✔
2128
    }
2129
  }
2130

2131
  int64_t startUs2 = taosGetTimestampUs();
44,078,745✔
2132
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
44,078,745✔
2133

2134
  SStmtQNode* param = NULL;
44,146,440✔
2135
  if (pStmt->sql.stbInterlaceMode) {
42,789,386✔
2136
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
2,140,684✔
2137
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
2,140,667✔
2138
    taosArrayClear(param->tblData.aCol);
1,070,365✔
2139

2140
    // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES);
2141

2142
    param->restoreTbCols = false;
1,069,676✔
2143
    param->tblData.isOrdered = true;
1,069,676✔
2144
    param->tblData.isDuplicateTs = false;
1,069,793✔
2145
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
1,069,830✔
2146

2147
    param->pCreateTbReq = pCreateTbReq;
1,069,834✔
2148
  }
2149

2150
  int64_t startUs3 = taosGetTimestampUs();
44,025,437✔
2151
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
44,025,437✔
2152

2153
  SArray*   pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
43,844,833✔
2154
  SBlobSet* pBlob = NULL;
44,227,903✔
2155
  if (colIdx < 0) {
44,232,163✔
2156
    if (pStmt->sql.stbInterlaceMode) {
44,262,495✔
2157
      (*pDataBlock)->pData->flags &= ~SUBMIT_REQ_COLUMN_DATA_FORMAT;
1,070,395✔
2158
      code = qBindStmtStbColsValue2(*pDataBlock, pCols, pStmt->bInfo.fixedValueCols, bind, pStmt->exec.pRequest->msgBuf,
1,361,438✔
2159
                                    pStmt->exec.pRequest->msgBufLen, &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo,
1,070,590✔
2160
                                    pStmt->taos->optionInfo.charsetCxt, &pBlob);
1,070,434✔
2161
      param->tblData.isOrdered = (*pDataBlock)->ordered;
1,070,408✔
2162
      param->tblData.isDuplicateTs = (*pDataBlock)->duplicateTs;
1,070,408✔
2163
    } else {
2164
      if (colIdx == -1) {
43,214,731✔
2165
        if (pStmt->sql.bindRowFormat) {
42,242,992✔
2166
          STMT2_ELOG_E("can't mix bind row format and bind column format");
194✔
2167
          STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
194✔
2168
        }
2169
        code = qBindStmtColsValue2(*pDataBlock, pCols, pStmt->bInfo.fixedValueCols, bind, pStmt->exec.pRequest->msgBuf,
80,049,796✔
2170
                                   pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt);
79,996,474✔
2171
      } else {
2172
        code =
2173
            qBindStmt2RowValue(*pDataBlock, (*pDataBlock)->pData->aRowP, pStmt->bInfo.fixedValueCols, bind,
388✔
2174
                               pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
388✔
2175
                               &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt);
984,147✔
2176
      }
2177
    }
2178

2179
    if (code) {
43,400,185✔
2180
      STMT2_ELOG("bind cols or rows failed, error:%s", tstrerror(code));
388✔
2181
      STMT_ERR_RET(code);
388✔
2182
    }
2183
  } else {
2184
    if (pStmt->sql.stbInterlaceMode) {
1,164✔
2185
      STMT2_ELOG_E("bind single column not allowed in stb insert mode");
×
2186
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2187
    }
2188

2189
    if (pStmt->sql.bindRowFormat) {
1,164✔
2190
      STMT2_ELOG_E("can't mix bind row format and bind column format");
×
2191
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2192
    }
2193

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

2199
    pStmt->bInfo.sBindLastIdx = colIdx;
1,164✔
2200

2201
    if (0 == colIdx) {
1,164✔
2202
      pStmt->bInfo.sBindRowNum = bind->num;
582✔
2203
    }
2204

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

2214
  int64_t startUs4 = taosGetTimestampUs();
43,764,636✔
2215
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
43,764,636✔
2216

2217
  if (pStmt->stbInterlaceMode) {
43,943,812✔
2218
    if (param) param->tblData.pBlobSet = pBlob;
43,967,619✔
2219
  }
2220

2221
  if (pStmt->sql.stbInterlaceMode) {
44,052,890✔
2222
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
1,070,098✔
2223
  } else {
2224
    STMT_ERR_RET(stmtAddBatch2(pStmt));
42,622,021✔
2225
  }
2226

2227
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
43,871,916✔
2228
  return TSDB_CODE_SUCCESS;
44,147,260✔
2229
}
2230

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

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

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

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

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

2261
      break;
2262
    }
2263

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

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

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

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

2295
      pStmt->stat.ctgGetTbMetaNum++;
2296

2297
      taos_free_result(pStmt->exec.pRequest);
2298
      pStmt->exec.pRequest = NULL;
2299

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

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

2312
    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2313
  }
2314

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

2327
  STMT_DLOG_E("start to exec");
2328

2329
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
2330

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

2334
  launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
2335

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

2347
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
2348

2349
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
2350
  pStmt->affectedRows += pStmt->exec.affectedRows;
2351

2352
_return:
2353

2354
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
2355

2356
  tFreeSSubmitRsp(pRsp);
2357

2358
  ++pStmt->sql.runTimes;
2359

2360
  STMT_RET(code);
2361
}
2362
*/
2363

2364
static int32_t createParseContext(const SRequestObj* pRequest, SParseContext** pCxt, SSqlCallbackWrapper* pWrapper) {
4,850✔
2365
  const STscObj* pTscObj = pRequest->pTscObj;
4,850✔
2366

2367
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
4,850✔
2368
  if (*pCxt == NULL) {
4,850✔
2369
    return terrno;
×
2370
  }
2371

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

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

2406
  if (code == TSDB_CODE_SUCCESS) {
4,850✔
2407
    pStmt->exec.affectedRows = taos_affected_rows(res);
4,656✔
2408
    pStmt->affectedRows += pStmt->exec.affectedRows;
4,656✔
2409
  }
2410

2411
  if (fp) {
4,850✔
2412
    fp(pStmt->options.userdata, res, code);
4,850✔
2413
  }
2414

2415
  while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
4,948✔
2416
    taosUsleep(1);
98✔
2417
  }
2418
  (void)stmtCleanExecInfo(pStmt, (code ? false : true), false);
4,850✔
2419
  ++pStmt->sql.runTimes;
4,850✔
2420
  if (pStmt->exec.pRequest != NULL) {
4,850✔
2421
    pStmt->exec.pRequest->inCallback = false;
3,104✔
2422
  }
2423

2424
  if (tsem_post(&pStmt->asyncExecSem) != 0) {
4,850✔
2425
    STMT2_ELOG_E("fail to post asyncExecSem");
×
2426
  }
2427
}
4,850✔
2428

2429
int stmtExec2(TAOS_STMT2* stmt, int* affected_rows) {
637,840✔
2430
  STscStmt2* pStmt = (STscStmt2*)stmt;
637,840✔
2431
  int32_t    code = 0;
637,840✔
2432
  int64_t    startUs = taosGetTimestampUs();
638,035✔
2433

2434
  STMT2_DLOG_E("start to exec");
638,035✔
2435

2436
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
637,996✔
2437
    return pStmt->errCode;
194✔
2438
  }
2439

2440
  STMT_ERR_RET(taosThreadMutexLock(&pStmt->asyncBindParam.mutex));
637,763✔
2441
  while (atomic_load_8((int8_t*)&pStmt->asyncBindParam.asyncBindNum) > 0) {
637,802✔
2442
    (void)taosThreadCondWait(&pStmt->asyncBindParam.waitCond, &pStmt->asyncBindParam.mutex);
×
2443
  }
2444
  STMT_ERR_RET(taosThreadMutexUnlock(&pStmt->asyncBindParam.mutex));
637,806✔
2445

2446
  if (pStmt->sql.stbInterlaceMode) {
637,702✔
2447
    STMT_ERR_RET(stmtAddBatch2(pStmt));
511,989✔
2448
  }
2449

2450
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
637,637✔
2451

2452
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
637,763✔
2453
    if (pStmt->sql.stbInterlaceMode) {
630,396✔
2454
      int64_t startTs = taosGetTimestampUs();
512,080✔
2455
      // wait for stmt bind thread to finish
2456
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
2,169,559✔
2457
        taosUsleep(1);
1,657,453✔
2458
      }
2459

2460
      if (pStmt->errCode != TSDB_CODE_SUCCESS) {
511,976✔
2461
        return pStmt->errCode;
194✔
2462
      }
2463

2464
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
511,743✔
2465
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
511,821✔
2466
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
511,860✔
2467
      pStmt->sql.siInfo.pVgroupHash = NULL;
511,808✔
2468
      pStmt->sql.siInfo.pVgroupList = NULL;
511,808✔
2469
    } else {
2470
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
118,472✔
2471
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
118,420✔
2472

2473
      STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
118,420✔
2474

2475
      STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
118,191✔
2476
    }
2477
  }
2478

2479
  pStmt->asyncResultAvailable = false;
637,372✔
2480
  SRequestObj*      pRequest = pStmt->exec.pRequest;
637,294✔
2481
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
637,450✔
2482
  STMT2_DLOG("EXEC INFO :req:0x%" PRIx64 ", QID:0x%" PRIx64 ", exec sql:%s,  conn:%" PRId64, pRequest->self,
637,450✔
2483
             pRequest->requestId, pStmt->sql.sqlStr, pRequest->pTscObj->id);
2484

2485
  if (!fp) {
637,284✔
2486
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
632,434✔
2487

2488
    if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
632,395✔
2489
      STMT2_ELOG_E("exec failed errorcode:NEED_CLIENT_HANDLE_ERROR, need to refresh meta and retry");
×
2490
      code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
×
2491
      if (code) {
×
2492
        pStmt->exec.pRequest->code = code;
×
2493

2494
      } else {
2495
        STMT_ERR_RET(stmtResetStmt(pStmt));
×
2496
        STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
×
2497
      }
2498
    }
2499

2500
    STMT_ERR_JRET(pStmt->exec.pRequest->code);
632,544✔
2501

2502
    pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
631,745✔
2503
    if (affected_rows) {
631,947✔
2504
      *affected_rows = pStmt->exec.affectedRows;
623,783✔
2505
    }
2506
    pStmt->affectedRows += pStmt->exec.affectedRows;
631,779✔
2507

2508
    // wait for stmt bind thread to finish
2509
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
632,488✔
2510
      taosUsleep(1);
325✔
2511
    }
2512

2513
    STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
632,067✔
2514

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

2531
    pStmt->execSemWaited = false;
4,850✔
2532
    launchAsyncQuery(pRequest, pStmt->sql.pQuery, NULL, pWrapper);
4,850✔
2533
  }
2534

2535
_return:
637,377✔
2536
  if (code) {
637,377✔
2537
    STMT2_ELOG("exec failed, error:%s", tstrerror(code));
388✔
2538
  }
2539
  pStmt->stat.execUseUs += taosGetTimestampUs() - startUs;
637,284✔
2540

2541
  STMT_RET(code);
637,362✔
2542
}
2543

2544
int stmtClose2(TAOS_STMT2* stmt) {
135,913✔
2545
  STscStmt2* pStmt = (STscStmt2*)stmt;
135,913✔
2546

2547
  STMT2_DLOG_E("start to close stmt");
135,913✔
2548
  taosMemoryFreeClear(pStmt->db);
135,913✔
2549

2550
  if (pStmt->bindThreadInUse) {
135,697✔
2551
    // wait for stmt bind thread to finish
2552
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
121,236✔
2553
      taosUsleep(1);
292✔
2554
    }
2555

2556
    (void)taosThreadMutexLock(&pStmt->queue.mutex);
120,944✔
2557
    pStmt->queue.stopQueue = true;
120,944✔
2558
    (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
120,944✔
2559
    (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
120,944✔
2560

2561
    (void)taosThreadJoin(pStmt->bindThread, NULL);
120,944✔
2562
    pStmt->bindThreadInUse = false;
120,944✔
2563

2564
    (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
120,944✔
2565
    (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
120,944✔
2566
  }
2567

2568
  TSC_ERR_RET(taosThreadMutexLock(&pStmt->asyncBindParam.mutex));
135,913✔
2569
  while (atomic_load_8((int8_t*)&pStmt->asyncBindParam.asyncBindNum) > 0) {
135,887✔
2570
    (void)taosThreadCondWait(&pStmt->asyncBindParam.waitCond, &pStmt->asyncBindParam.mutex);
×
2571
  }
2572
  TSC_ERR_RET(taosThreadMutexUnlock(&pStmt->asyncBindParam.mutex));
135,887✔
2573

2574
  (void)taosThreadCondDestroy(&pStmt->asyncBindParam.waitCond);
135,913✔
2575
  (void)taosThreadMutexDestroy(&pStmt->asyncBindParam.mutex);
135,913✔
2576

2577
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
135,913✔
2578
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
2,134✔
2579
      STMT2_ELOG_E("fail to wait asyncExecSem");
×
2580
    }
2581
  }
2582

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

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

2606
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
135,887✔
2607

2608
  if (pStmt->options.asyncExecFn) {
135,887✔
2609
    if (tsem_destroy(&pStmt->asyncExecSem) != 0) {
2,134✔
2610
      STMT2_ELOG_E("fail to destroy asyncExecSem");
×
2611
    }
2612
  }
2613
  taosMemoryFree(stmt);
135,913✔
2614

2615
  return TSDB_CODE_SUCCESS;
135,887✔
2616
}
2617

2618
const char* stmt2Errstr(TAOS_STMT2* stmt) {
9,506✔
2619
  STscStmt2* pStmt = (STscStmt2*)stmt;
9,506✔
2620

2621
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
9,506✔
2622
    return (char*)tstrerror(terrno);
776✔
2623
  }
2624

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

2630
  SRequestObj* pRequest = pStmt->exec.pRequest;
8,730✔
2631
  if (NULL != pRequest->msgBuf && (strlen(pRequest->msgBuf) > 0 || pRequest->code == TSDB_CODE_RPC_FQDN_ERROR)) {
8,730✔
2632
    return pRequest->msgBuf;
6,014✔
2633
  }
2634
  return (const char*)tstrerror(pRequest->code);
2,716✔
2635
}
2636
/*
2637
int stmtAffectedRows(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->affectedRows; }
2638

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

2642
int stmtParseColFields2(TAOS_STMT2* stmt) {
13,797✔
2643
  int32_t    code = 0;
13,797✔
2644
  STscStmt2* pStmt = (STscStmt2*)stmt;
13,797✔
2645
  int32_t    preCode = pStmt->errCode;
13,797✔
2646

2647
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
13,797✔
2648
    return pStmt->errCode;
×
2649
  }
2650

2651
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
13,797✔
2652
    STMT2_ELOG_E("stmtParseColFields2 only for insert");
×
2653
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2654
  }
2655

2656
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
13,797✔
2657

2658
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
13,797✔
2659
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
582✔
2660
    pStmt->bInfo.needParse = false;
×
2661
  }
2662
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx != NULL) {
13,797✔
2663
    pStmt->bInfo.needParse = false;
1,164✔
2664
  }
2665

2666
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
13,797✔
2667

2668
  if (pStmt->bInfo.needParse) {
13,797✔
2669
    STMT_ERRI_JRET(stmtParseSql(pStmt));
12,439✔
2670
  }
2671

2672
_return:
10,499✔
2673
  // compatible with previous versions
2674
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && (pStmt->bInfo.tbNameFlag & NO_DATA_USING_CLAUSE) == 0x0) {
13,797✔
2675
    code = TSDB_CODE_TSC_STMT_TBNAME_ERROR;
776✔
2676
  }
2677

2678
  pStmt->errCode = preCode;
13,797✔
2679

2680
  return code;
13,797✔
2681
}
2682

2683
int stmtGetStbColFields2(TAOS_STMT2* stmt, int* nums, TAOS_FIELD_ALL** fields) {
13,797✔
2684
  int32_t code = stmtParseColFields2(stmt);
13,797✔
2685
  if (code != TSDB_CODE_SUCCESS) {
13,797✔
2686
    return code;
3,298✔
2687
  }
2688

2689
  return stmtFetchStbColFields2(stmt, nums, fields);
10,499✔
2690
}
2691

2692
int stmtGetParamNum2(TAOS_STMT2* stmt, int* nums) {
3,686✔
2693
  int32_t    code = 0;
3,686✔
2694
  STscStmt2* pStmt = (STscStmt2*)stmt;
3,686✔
2695
  int32_t    preCode = pStmt->errCode;
3,686✔
2696

2697
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
3,686✔
2698
    return pStmt->errCode;
×
2699
  }
2700

2701
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
3,686✔
2702

2703
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
3,686✔
2704
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
2705
    pStmt->bInfo.needParse = false;
×
2706
  }
2707

2708
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
3,686✔
2709
    resetRequest(pStmt);
×
2710
  }
2711

2712
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
3,686✔
2713

2714
  if (pStmt->bInfo.needParse) {
3,686✔
2715
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
2716
  }
2717

2718
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
3,686✔
2719
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
3,686✔
2720
  } else {
2721
    STMT_ERRI_JRET(stmtFetchColFields2(stmt, nums, NULL));
×
2722
  }
2723

2724
  STMT2_DLOG("get param num success, nums:%d", *nums);
3,686✔
2725

2726
_return:
3,686✔
2727

2728
  pStmt->errCode = preCode;
3,686✔
2729

2730
  return code;
3,686✔
2731
}
2732

2733
TAOS_RES* stmtUseResult2(TAOS_STMT2* stmt) {
6,604✔
2734
  STscStmt2* pStmt = (STscStmt2*)stmt;
6,604✔
2735

2736
  STMT2_TLOG_E("start to use result");
6,604✔
2737

2738
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
6,604✔
2739
    STMT2_ELOG_E("useResult only for query statement");
×
2740
    return NULL;
×
2741
  }
2742

2743
  if (pStmt->options.asyncExecFn != NULL && !pStmt->asyncResultAvailable) {
6,604✔
2744
    STMT2_ELOG_E("use result after callBackFn return");
194✔
2745
    return NULL;
194✔
2746
  }
2747

2748
  if (tsUseAdapter) {
6,410✔
2749
    TAOS_RES* res = (TAOS_RES*)pStmt->exec.pRequest;
4,462✔
2750
    pStmt->exec.pRequest = NULL;
4,462✔
2751
    return res;
4,462✔
2752
  }
2753

2754
  return pStmt->exec.pRequest;
1,948✔
2755
}
2756

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

2760
  ThreadArgs* targs = (ThreadArgs*)args;
×
2761
  STscStmt2*  pStmt = (STscStmt2*)targs->stmt;
×
2762

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

2771
  qInfo("async stmt bind thread stopped");
×
2772

2773
  return code;
×
2774
}
2775

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

2781
  if (pStmt->exec.pRequest == NULL) {
582✔
2782
    return;
×
2783
  }
2784

2785
  if (pStmt->exec.pRequest->msgBuf == NULL) {
582✔
2786
    return;
×
2787
  }
2788

2789
  size_t msgLen = strlen(msg);
582✔
2790
  size_t bufLen = pStmt->exec.pRequest->msgBufLen;
582✔
2791

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

2801
  return;
582✔
2802
}
2803

2804
int32_t stmtBuildErrorMsgWithCode(STscStmt2* pStmt, const char* msg, int32_t errorCode) {
582✔
2805
  stmtBuildErrorMsg(pStmt, msg);
582✔
2806
  pStmt->errCode = errorCode;
582✔
2807

2808
  return errorCode;
582✔
2809
}
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