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

taosdata / TDengine / #4926

13 Jan 2026 05:43AM UTC coverage: 66.053% (-0.05%) from 66.107%
#4926

push

travis-ci

web-flow
feat: [6654385780] show snap progress (#34203)

48 of 59 new or added lines in 7 files covered. (81.36%)

582 existing lines in 124 files now uncovered.

200362 of 303334 relevant lines covered (66.05%)

132283104.31 hits per line

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

61.89
/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,593,370✔
15
    *pBuf = (char*)pTblBuf->pCurBuff + pTblBuf->buffOffset;
1,593,596✔
16
    pTblBuf->buffOffset += pTblBuf->buffUnit;
1,593,594✔
UNCOV
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,593,232✔
41
}
42

43
static bool stmtDequeue(STscStmt2* pStmt, SStmtQNode** param) {
1,594,070✔
44
  int i = 0;
1,594,070✔
45
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
6,854,300✔
46
    if (pStmt->queue.stopQueue) {
5,263,604✔
47
      return false;
3,152✔
48
    }
49
    if (i < 10) {
5,260,636✔
50
      taosUsleep(1);
4,939,435✔
51
      i++;
4,939,155✔
52
    } else {
53
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
321,201✔
54
      if (pStmt->queue.stopQueue) {
321,124✔
55
        (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
×
56
        return false;
×
57
      }
58
      if (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
321,124✔
59
        (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
321,059✔
60
      }
61
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
321,105✔
62
    }
63
  }
64

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

69
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
1,590,593✔
70
  if (pStmt->queue.head == pStmt->queue.tail) {
1,591,042✔
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,590,901✔
78
  pStmt->queue.head->next = node->next;
1,591,042✔
79
  if (pStmt->queue.tail == node) {
1,591,042✔
80
    pStmt->queue.tail = pStmt->queue.head;
878,609✔
81
  }
82
  node->next = NULL;
1,591,085✔
83
  *param = node;
1,591,085✔
84

85
  (void)atomic_sub_fetch_64((int64_t*)&pStmt->queue.qRemainNum, 1);
1,591,085✔
86
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
1,591,098✔
87

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

90
  return true;
1,590,914✔
91
}
92

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

99
  param->next = NULL;
1,590,508✔
100

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

103
  pStmt->queue.tail->next = param;
1,591,003✔
104
  pStmt->queue.tail = param;
1,591,046✔
105
  pStmt->stat.bindDataNum++;
1,590,954✔
106

107
  (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
1,590,733✔
108
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
1,590,980✔
109

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

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

116
static int32_t stmtCreateRequest(STscStmt2* pStmt) {
2,782,278✔
117
  int32_t code = 0;
2,782,278✔
118

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

138
  return code;
2,782,855✔
139
}
140

141
static int32_t stmtSwitchStatus(STscStmt2* pStmt, STMT_STATUS newStatus) {
3,812,511✔
142
  int32_t code = 0;
3,812,511✔
143

144
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
3,812,511✔
145
    STMT2_LOG_SEQ(newStatus);
3,813,459✔
146
  }
147

148
  if (pStmt->errCode && newStatus != STMT_PREPARE) {
3,813,279✔
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) {
3,813,181✔
154
    case STMT_PREPARE:
3,185✔
155
      pStmt->errCode = 0;
3,185✔
156
      break;
2,414✔
157
    case STMT_SETTBNAME:
1,074,338✔
158
      if (STMT_STATUS_EQ(INIT)) {
1,074,338✔
159
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
160
      }
161
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
1,074,528✔
162
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
163
      }
164
      break;
1,074,338✔
165
    case STMT_SETTAGS:
628,814✔
166
      if (STMT_STATUS_EQ(INIT)) {
628,814✔
167
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
168
      }
169
      break;
628,814✔
170
    case STMT_FETCH_FIELDS:
23✔
171
      if (STMT_STATUS_EQ(INIT)) {
23✔
172
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
173
      }
174
      break;
23✔
175
    case STMT_BIND:
1,074,052✔
176
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
1,074,052✔
177
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
178
      }
179
      /*
180
            if ((pStmt->sql.type == STMT_TYPE_MULTI_INSERT) && ()) {
181
              code = TSDB_CODE_TSC_STMT_API_ERROR;
182
            }
183
      */
184
      break;
1,074,402✔
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:
516,380✔
191
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
516,380✔
192
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
193
      }
194
      break;
516,380✔
195
    case STMT_EXECUTE:
516,389✔
196
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
516,389✔
197
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
8✔
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)) {
516,332✔
203
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
204
        }
205
      }
206
      break;
516,340✔
207
    default:
×
208
      code = TSDB_CODE_APP_ERROR;
×
209
      break;
×
210
  }
211

212
  STMT_ERR_RET(code);
3,812,711✔
213

214
  pStmt->sql.status = newStatus;
3,812,711✔
215

216
  return TSDB_CODE_SUCCESS;
3,813,278✔
217
}
218

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

222
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
3,200✔
223

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

229
  *tbName = pStmt->bInfo.tbName;
3,177✔
230

231
  return TSDB_CODE_SUCCESS;
3,177✔
232
}
233

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

243
  if ((tags != NULL && ((SBoundColInfo*)tags)->numOfCols == 0) || !autoCreateTbl) {
3,200✔
244
    pStmt->sql.autoCreateTbl = false;
2,976✔
245
  }
246

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

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

256
  if (!pStmt->bInfo.tagsCached) {
3,122✔
257
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
3,128✔
258
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
3,128✔
259
  }
260

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

267
  pStmt->bInfo.boundTags = tags;
3,182✔
268
  pStmt->bInfo.tagsCached = false;
3,188✔
269
  pStmt->bInfo.tbNameFlag = tbNameFlag;
3,200✔
270
  tstrncpy(pStmt->bInfo.stbFName, sTableName, sizeof(pStmt->bInfo.stbFName));
3,134✔
271

272
  if (pTableMeta->tableType != TSDB_CHILD_TABLE && pTableMeta->tableType != TSDB_SUPER_TABLE) {
3,128✔
273
    pStmt->sql.stbInterlaceMode = false;
×
274
  }
275

276
  return TSDB_CODE_SUCCESS;
3,128✔
277
}
278

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

282
  pStmt->sql.pVgHash = pVgHash;
3,122✔
283
  pStmt->exec.pBlockHash = pBlockHash;
3,122✔
284

285
  return TSDB_CODE_SUCCESS;
3,128✔
286
}
287

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

293
  STMT_ERR_RET(stmtUpdateBindInfo(stmt, pTableMeta, tags, cols, tbName, sTableName, autoCreateTbl, tbNameFlag));
3,140✔
294
  STMT_ERR_RET(stmtUpdateExecInfo(stmt, pVgHash, pBlockHash));
3,188✔
295

296
  pStmt->sql.autoCreateTbl = autoCreateTbl;
3,128✔
297

298
  return TSDB_CODE_SUCCESS;
3,188✔
299
}
300

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

304
  *pVgHash = pStmt->sql.pVgHash;
23✔
305
  pStmt->sql.pVgHash = NULL;
23✔
306

307
  *pBlockHash = pStmt->exec.pBlockHash;
23✔
308
  pStmt->exec.pBlockHash = NULL;
23✔
309

310
  return TSDB_CODE_SUCCESS;
23✔
311
}
312

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

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

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

326
  pStmt->stat.parseSqlNum++;
3,208✔
327

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

331
  pStmt->sql.siInfo.pQuery = pStmt->sql.pQuery;
3,142✔
332

333
  pStmt->bInfo.needParse = false;
3,208✔
334

335
  if (pStmt->sql.type == 0) {
3,208✔
336
    if (pStmt->sql.pQuery->pRoot && LEGAL_INSERT(nodeType(pStmt->sql.pQuery->pRoot))) {
8✔
337
      pStmt->sql.type = STMT_TYPE_INSERT;
×
338
      pStmt->sql.stbInterlaceMode = false;
×
339
    } else if (pStmt->sql.pQuery->pPrepareRoot && LEGAL_SELECT(nodeType(pStmt->sql.pQuery->pPrepareRoot))) {
8✔
340
      pStmt->sql.type = STMT_TYPE_QUERY;
8✔
341
      pStmt->sql.stbInterlaceMode = false;
8✔
342

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

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

393
  return TSDB_CODE_SUCCESS;
3,200✔
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) {
3,210✔
520
  if (pStmt->exec.pRequest) {
3,210✔
521
    taos_free_result(pStmt->exec.pRequest);
3,185✔
522
    pStmt->exec.pRequest = NULL;
3,185✔
523
  }
524
  pStmt->asyncResultAvailable = false;
3,210✔
525
}
3,210✔
526

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

534
  pStmt->bInfo.tbName[0] = 0;
522,618✔
535
  pStmt->bInfo.tbFName[0] = 0;
522,618✔
536
  if (!pStmt->bInfo.tagsCached) {
522,618✔
537
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
313,472✔
538
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
313,489✔
539
    pStmt->bInfo.boundTags = NULL;
313,551✔
540
  }
541

542
  if (!pStmt->bInfo.boundColsCached) {
522,562✔
543
    tSimpleHashCleanup(pStmt->bInfo.fixedValueCols);
6,393✔
544
    pStmt->bInfo.fixedValueCols = NULL;
6,393✔
545
  }
546

547
  if (!pStmt->sql.autoCreateTbl) {
522,605✔
548
    pStmt->bInfo.stbFName[0] = 0;
313,212✔
549
    pStmt->bInfo.tbSuid = 0;
313,212✔
550
  }
551

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

555
  return TSDB_CODE_SUCCESS;
522,398✔
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) {
515,888✔
564
  pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, 0);
515,888✔
565
  if (NULL == pTblBuf->pCurBuff) {
516,274✔
566
    tscError("QInfo:%p, fail to get buffer from list", pTblBuf);
286✔
567
    return;
×
568
  }
569
  pTblBuf->buffIdx = 1;
515,988✔
570
  pTblBuf->buffOffset = sizeof(*pQueue->head);
515,939✔
571

572
  pQueue->head = pQueue->tail = pTblBuf->pCurBuff;
515,939✔
573
  pQueue->qRemainNum = 0;
515,988✔
574
  pQueue->head->next = NULL;
515,896✔
575
}
576

577
static int32_t stmtCleanExecInfo(STscStmt2* pStmt, bool keepTable, bool deepClean) {
519,010✔
578
  if (pStmt->sql.stbInterlaceMode) {
519,010✔
579
    if (deepClean) {
519,139✔
580
      taosHashCleanup(pStmt->exec.pBlockHash);
3,152✔
581
      pStmt->exec.pBlockHash = NULL;
3,152✔
582

583
      if (NULL != pStmt->exec.pCurrBlock) {
3,152✔
584
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->boundColsInfo.pColIndex);
3,152✔
585
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->pData);
3,152✔
586
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
3,152✔
587
        pStmt->exec.pCurrBlock = NULL;
3,152✔
588
      }
589
      if (STMT_TYPE_QUERY != pStmt->sql.type) {
3,152✔
590
        resetRequest(pStmt);
3,152✔
591
      }
592
    } else {
593
      pStmt->sql.siInfo.pTableColsIdx = 0;
515,987✔
594
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
516,079✔
595
      tSimpleHashClear(pStmt->sql.siInfo.pTableRowDataHash);
515,853✔
596
    }
597
    if (NULL != pStmt->exec.pRequest) {
519,547✔
598
      pStmt->exec.pRequest->body.resInfo.numOfRows = 0;
516,395✔
599
    }
600
  } else {
601
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
22✔
602
      resetRequest(pStmt);
14✔
603
    }
604

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

612
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
219✔
613
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
25✔
614
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
58✔
615

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

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

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

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

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

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

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

643
  return TSDB_CODE_SUCCESS;
519,237✔
644
}
645

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

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

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

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

673
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
3,185✔
674
  while (pIter) {
3,210✔
675
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
25✔
676

677
    qDestroyStmtDataBlock(pCache->pDataCtx);
25✔
678
    qDestroyBoundColInfo(pCache->boundTags);
25✔
679
    taosMemoryFreeClear(pCache->boundTags);
25✔
680

681
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
25✔
682
  }
683
  taosHashCleanup(pStmt->sql.pTableCache);
3,185✔
684
  pStmt->sql.pTableCache = NULL;
3,185✔
685

686
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
3,185✔
687
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
3,185✔
688

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

699
  (void)memset(&pStmt->sql, 0, sizeof(pStmt->sql));
3,185✔
700
  pStmt->sql.siInfo.tableColsReady = true;
3,185✔
701

702
  STMT2_TLOG_E("end to free SQL info");
3,185✔
703

704
  return TSDB_CODE_SUCCESS;
3,185✔
705
}
706

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

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

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

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

731
  *vgId = vgInfo.vgId;
628,802✔
732

733
  return TSDB_CODE_SUCCESS;
628,802✔
734
}
735

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

744
  pStmt->stat.ctgGetTbMetaNum++;
2,976✔
745

746
  if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
2,976✔
747
    STMT2_ELOG("tb %s not exist", pStmt->bInfo.tbFName);
×
748
    (void)stmtCleanBindInfo(pStmt);
×
749

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

755
    STMT_ERR_RET(code);
×
756
  }
757

758
  STMT_ERR_RET(code);
2,976✔
759

760
  *uid = pTableMeta->uid;
2,976✔
761
  *suid = pTableMeta->suid;
2,976✔
762
  *tableType = pTableMeta->tableType;
2,976✔
763
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
2,976✔
764
  *vgId = pTableMeta->vgId;
2,976✔
765

766
  taosMemoryFree(pTableMeta);
2,976✔
767

768
  return TSDB_CODE_SUCCESS;
2,916✔
769
}
770

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

776
  STMT2_DLOG("uid:%" PRId64 ", rebuild table data context, vgId:%d", uid, vgId);
169✔
777

778
  return TSDB_CODE_SUCCESS;
169✔
779
}
780

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

788
  pStmt->bInfo.needParse = true;
3,346✔
789
  pStmt->bInfo.inExecCache = false;
3,346✔
790

791
  STableDataCxt** pCxtInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
3,346✔
792
  if (pCxtInExec) {
3,346✔
793
    pStmt->bInfo.needParse = false;
×
794
    pStmt->bInfo.inExecCache = true;
×
795

796
    pStmt->exec.pCurrBlock = *pCxtInExec;
×
797

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

804
  if (NULL == pStmt->pCatalog) {
3,346✔
805
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
3,073✔
806
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
3,073✔
807
  }
808

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

816
    STMT2_DLOG("no stmt block cache for tb %s", pStmt->bInfo.tbFName);
3,177✔
817

818
    return TSDB_CODE_SUCCESS;
3,177✔
819
  }
820

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

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

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

835
      pStmt->exec.pCurrBlock = pNewBlock;
169✔
836

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

839
      return TSDB_CODE_SUCCESS;
169✔
840
    }
841

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

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

849
  STMT_ERR_RET(stmtGetTableMetaAndValidate(pStmt, &uid, &suid, &vgId, &tableType));
×
850

851
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
×
852

853
  if (uid == pStmt->bInfo.tbUid) {
×
854
    pStmt->bInfo.needParse = false;
×
855

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

858
    return TSDB_CODE_SUCCESS;
×
859
  }
860

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

867
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
868
    }
869

870
    pStmt->bInfo.needParse = false;
×
871

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

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

880
    return TSDB_CODE_SUCCESS;
×
881
  }
882

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

887
    pStmt->bInfo.tbUid = uid;
×
888
    pStmt->bInfo.tbSuid = suid;
×
889
    pStmt->bInfo.tbType = tableType;
×
890
    pStmt->bInfo.boundTags = pCache->boundTags;
×
891
    pStmt->bInfo.tagsCached = true;
×
892

893
    STableDataCxt* pNewBlock = NULL;
×
894
    STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, uid, suid, vgId));
×
895

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

901
    pStmt->exec.pCurrBlock = pNewBlock;
×
902

903
    tscDebug("tb %s in sqlBlock list, set to current", pStmt->bInfo.tbFName);
×
904

905
    return TSDB_CODE_SUCCESS;
×
906
  }
907

908
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
909

910
  return TSDB_CODE_SUCCESS;
×
911
}
912

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

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

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

924
  return TSDB_CODE_SUCCESS;
×
925
}
926

927
static void stmtAsyncOutput(STscStmt2* pStmt, void* param) {
1,590,705✔
928
  SStmtQNode* pParam = (SStmtQNode*)param;
1,590,705✔
929

930
  if (pParam->restoreTbCols) {
1,590,705✔
931
    for (int32_t i = 0; i < pStmt->sql.siInfo.pTableColsIdx; ++i) {
1,590,628✔
932
      SArray** p = (SArray**)TARRAY_GET_ELEM(pStmt->sql.siInfo.pTableCols, i);
1,074,315✔
933
      *p = taosArrayInit(20, POINTER_BYTES);
1,074,321✔
934
      if (*p == NULL) {
1,074,246✔
UNCOV
935
        pStmt->errCode = terrno;
×
936
      }
937
    }
938
    atomic_store_8((int8_t*)&pStmt->sql.siInfo.tableColsReady, true);
516,313✔
939
    STMT2_TLOG_E("restore pTableCols finished");
516,352✔
940
  } else {
941
    int code = qAppendStmt2TableOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, &pParam->tblData, pStmt->exec.pCurrBlock,
1,074,441✔
942
                                       &pStmt->sql.siInfo, pParam->pCreateTbReq);
943
    // taosMemoryFree(pParam->pTbData);
944
    if (code != TSDB_CODE_SUCCESS) {
1,074,579✔
945
      STMT2_ELOG("async append stmt output failed, tbname:%s, err:%s", pParam->tblData.tbName, tstrerror(code));
×
946
      pStmt->errCode = code;
×
947
    }
948
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
1,074,579✔
949
  }
950
}
1,591,107✔
951

952
static void* stmtBindThreadFunc(void* param) {
3,152✔
953
  setThreadName("stmt2Bind");
3,152✔
954

955
  STscStmt2* pStmt = (STscStmt2*)param;
3,152✔
956
  STMT2_DLOG_E("stmt2 bind thread started");
3,152✔
957

958
  while (true) {
1,591,006✔
959
    SStmtQNode* asyncParam = NULL;
1,594,158✔
960

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

969
    stmtAsyncOutput(pStmt, asyncParam);
1,590,791✔
970
  }
971

972
  STMT2_DLOG_E("stmt2 bind thread stopped");
3,152✔
973
  return NULL;
3,152✔
974
}
975

976
static int32_t stmtStartBindThread(STscStmt2* pStmt) {
3,152✔
977
  TdThreadAttr thAttr;
2,913✔
978
  if (taosThreadAttrInit(&thAttr) != 0) {
3,152✔
979
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
980
  }
981
  if (taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE) != 0) {
3,152✔
982
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
983
  }
984

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

990
  pStmt->bindThreadInUse = true;
3,152✔
991

992
  (void)taosThreadAttrDestroy(&thAttr);
3,152✔
993
  return TSDB_CODE_SUCCESS;
3,152✔
994
}
995

996
static int32_t stmtInitQueue(STscStmt2* pStmt) {
3,152✔
997
  (void)taosThreadCondInit(&pStmt->queue.waitCond, NULL);
3,152✔
998
  (void)taosThreadMutexInit(&pStmt->queue.mutex, NULL);
3,152✔
999
  STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&pStmt->queue.head));
6,304✔
1000
  pStmt->queue.tail = pStmt->queue.head;
3,152✔
1001

1002
  return TSDB_CODE_SUCCESS;
3,152✔
1003
}
1004

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

1010
  return TSDB_CODE_SUCCESS;
3,185✔
1011
}
1012

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

1025
  if (taosArrayPush(pTblBuf->pBufList, &buff) == NULL) {
6,304✔
1026
    return terrno;
×
1027
  }
1028

1029
  pTblBuf->pCurBuff = buff;
3,152✔
1030
  pTblBuf->buffIdx = 1;
3,152✔
1031
  pTblBuf->buffOffset = 0;
3,152✔
1032

1033
  return TSDB_CODE_SUCCESS;
3,152✔
1034
}
1035

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

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

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

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

1062
  if (NULL != pOptions) {
3,081✔
1063
    (void)memcpy(&pStmt->options, pOptions, sizeof(pStmt->options));
3,058✔
1064
    if (pOptions->singleStbInsert && pOptions->singleTableBindOnce) {
3,058✔
1065
      pStmt->stbInterlaceMode = true;
3,048✔
1066
    }
1067

1068
    pStmt->reqid = pOptions->reqid;
3,058✔
1069
  }
1070

1071
  if (pStmt->stbInterlaceMode) {
3,081✔
1072
    pStmt->sql.siInfo.transport = taos->pAppInfo->pTransporter;
3,048✔
1073
    pStmt->sql.siInfo.acctId = taos->acctId;
3,048✔
1074
    pStmt->sql.siInfo.dbname = taos->db;
3,048✔
1075
    pStmt->sql.siInfo.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
3,048✔
1076

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

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

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

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

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

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

1131
  pStmt->execSemWaited = false;
3,081✔
1132

1133
  // STMT_LOG_SEQ(STMT_INIT);
1134

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

1138
  return pStmt;
3,081✔
1139
}
1140

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

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

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

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

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

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

1187
  code = stmtInitTableBuf(&pStmt->sql.siInfo.tbBuf);
104✔
1188

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

1200
  return TSDB_CODE_SUCCESS;
104✔
1201
}
1202

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

1210
  pStmt->errCode = 0;
104✔
1211

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

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

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

1236
    (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
104✔
1237
    (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
104✔
1238
  }
1239

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

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

1254
    if (NULL == pStmt->pCatalog) {
104✔
1255
      STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
×
1256
    }
1257
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
104✔
1258

1259
    STMT_ERR_RET(stmtResetStbInterlaceCache(pStmt));
104✔
1260

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

1268
  // Restore preserved state
1269
  pStmt->db = db;
104✔
1270
  pStmt->options = options;
104✔
1271
  pStmt->reqid = reqid;
104✔
1272
  pStmt->stbInterlaceMode = stbInterlaceMode;
104✔
1273

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

1280
  pStmt->bInfo.needParse = true;
104✔
1281
  pStmt->sql.status = STMT_INIT;
104✔
1282
  pStmt->sql.siInfo.tableColsReady = true;
104✔
1283

1284
  return TSDB_CODE_SUCCESS;
104✔
1285
}
1286

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

1291
  STMT2_DLOG("start to prepare with sql:%s", sql);
3,125✔
1292

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

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

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

1309
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
3,185✔
1310

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

1322
  if (stmt2IsInsert(pStmt)) {
3,185✔
1323
    pStmt->sql.stbInterlaceMode = pStmt->stbInterlaceMode;
3,177✔
1324
    char* dbName = NULL;
3,177✔
1325
    if (qParseDbName(sql, length, &dbName)) {
3,177✔
1326
      STMT_ERR_RET(stmtSetDbName2(stmt, dbName));
×
1327
      taosMemoryFreeClear(dbName);
×
1328
    } else if (pStmt->db != NULL && pStmt->db[0] != '\0') {
3,177✔
1329
      taosMemoryFreeClear(pStmt->exec.pRequest->pDb);
3,177✔
1330
      pStmt->exec.pRequest->pDb = taosStrdup(pStmt->db);
3,177✔
1331
      if (pStmt->exec.pRequest->pDb == NULL) {
3,177✔
1332
        STMT_ERR_RET(terrno);
×
1333
      }
1334
      (void)strdequote(pStmt->exec.pRequest->pDb);
3,177✔
1335

1336
      if (pStmt->sql.stbInterlaceMode) {
3,177✔
1337
        pStmt->sql.siInfo.dbname = pStmt->exec.pRequest->pDb;
3,152✔
1338
      }
1339
    }
1340

1341
  } else if (stmt2IsSelect(pStmt)) {
8✔
1342
    pStmt->sql.stbInterlaceMode = false;
8✔
1343
    STMT_ERR_RET(stmtParseSql(pStmt));
8✔
1344
  } else {
1345
    return stmtBuildErrorMsgWithCode(pStmt, "stmt only support 'SELECT' or 'INSERT'", TSDB_CODE_PAR_SYNTAX_ERROR);
×
1346
  }
1347
  return TSDB_CODE_SUCCESS;
3,185✔
1348
}
1349

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

1357
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
3,152✔
1358
  pStmt->sql.siInfo.pDataCtx = pDst;
3,092✔
1359

1360
  SArray* pTblCols = NULL;
3,092✔
1361
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
3,141,686✔
1362
    pTblCols = taosArrayInit(20, POINTER_BYTES);
3,138,534✔
1363
    if (NULL == pTblCols) {
3,132,137✔
1364
      return terrno;
×
1365
    }
1366

1367
    if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
6,269,428✔
1368
      return terrno;
×
1369
    }
1370
  }
1371

1372
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
3,152✔
1373

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

1377
  return TSDB_CODE_SUCCESS;
3,152✔
1378
}
1379

1380
bool stmt2IsInsert(TAOS_STMT2* stmt) {
1,077,241✔
1381
  STscStmt2* pStmt = (STscStmt2*)stmt;
1,077,241✔
1382
  if (pStmt->sql.type) {
1,077,241✔
1383
    return (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
1,071,284✔
1384
  }
1385

1386
  return qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
6,190✔
1387
}
1388

1389
bool stmt2IsSelect(TAOS_STMT2* stmt) {
3,162✔
1390
  STscStmt2* pStmt = (STscStmt2*)stmt;
3,162✔
1391

1392
  if (pStmt->sql.type) {
3,162✔
1393
    return STMT_TYPE_QUERY == pStmt->sql.type;
×
1394
  }
1395
  return qIsSelectFromSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
3,162✔
1396
}
1397

1398
int stmtSetTbName2(TAOS_STMT2* stmt, const char* tbName) {
1,074,286✔
1399
  STscStmt2* pStmt = (STscStmt2*)stmt;
1,074,286✔
1400

1401
  int64_t startUs = taosGetTimestampUs();
1,074,121✔
1402

1403
  STMT2_TLOG("start to set tbName:%s", tbName);
1,074,121✔
1404

1405
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
1,074,225✔
1406
    return pStmt->errCode;
×
1407
  }
1408

1409
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
1,074,342✔
1410

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

1419
  STMT_ERR_RET(qCreateSName2(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
1,074,083✔
1420
                             pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1421
  STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
1,073,862✔
1422
  tstrncpy(pStmt->bInfo.tbName, (char*)tNameGetTableName(&pStmt->bInfo.sname), TSDB_TABLE_NAME_LEN);
1,073,913✔
1423

1424
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
1,073,919✔
1425
    STMT_ERR_RET(stmtGetFromCache(pStmt));
3,513✔
1426

1427
    if (pStmt->bInfo.needParse) {
3,346✔
1428
      STMT_ERR_RET(stmtParseSql(pStmt));
3,177✔
1429
      if (!pStmt->sql.autoCreateTbl) {
3,177✔
1430
        uint64_t uid, suid;
2,913✔
1431
        int32_t  vgId;
2,913✔
1432
        int8_t   tableType;
2,913✔
1433

1434
        int32_t code = stmtGetTableMetaAndValidate(pStmt, &uid, &suid, &vgId, &tableType);
2,976✔
1435
        if (code != TSDB_CODE_SUCCESS) {
2,976✔
1436
          return code;
×
1437
        }
1438
      }
1439
    }
1440

1441
  } else {
1442
    pStmt->exec.pRequest->requestId++;
1,070,247✔
1443
    pStmt->bInfo.needParse = false;
1,070,603✔
1444
  }
1445

1446
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
1,073,857✔
1447
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
3,152✔
1448
  }
1449

1450
  int64_t startUs2 = taosGetTimestampUs();
1,074,698✔
1451
  pStmt->stat.setTbNameUs += startUs2 - startUs;
1,074,698✔
1452

1453
  return TSDB_CODE_SUCCESS;
1,074,361✔
1454
}
1455

1456
int stmtSetTbTags2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* tags, SVCreateTbReq** pCreateTbReq) {
524,892✔
1457
  STscStmt2* pStmt = (STscStmt2*)stmt;
524,892✔
1458

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

1464
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
524,801✔
1465
    return pStmt->errCode;
×
1466
  }
1467

1468
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
524,801✔
1469

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

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

1483
  SBoundColInfo* tags_info = (SBoundColInfo*)pStmt->bInfo.boundTags;
524,866✔
1484

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

1505
  STMT2_TLOG_E("start to bind stmt tag values");
524,866✔
1506

1507
  void* boundTags = NULL;
524,736✔
1508
  if (pStmt->sql.stbInterlaceMode) {
524,736✔
1509
    boundTags = pStmt->sql.siInfo.boundTags;
524,542✔
1510
    *pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
524,542✔
1511
    if (NULL == pCreateTbReq) {
524,724✔
1512
      return terrno;
×
1513
    }
1514
    int32_t vgId = -1;
524,724✔
1515
    STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
524,724✔
1516
    (*pCreateTbReq)->uid = vgId;
524,815✔
1517
  } else {
1518
    boundTags = pStmt->bInfo.boundTags;
194✔
1519
  }
1520

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

1525
  return TSDB_CODE_SUCCESS;
524,970✔
1526
}
1527

1528
int stmtCheckTags2(TAOS_STMT2* stmt, SVCreateTbReq** pCreateTbReq) {
103,896✔
1529
  STscStmt2* pStmt = (STscStmt2*)stmt;
103,896✔
1530

1531
  STMT2_TLOG_E("start to clone createTbRequest for fixed tags");
103,896✔
1532

1533
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
103,961✔
1534
    return pStmt->errCode;
×
1535
  }
1536

1537
  if (!pStmt->sql.stbInterlaceMode) {
103,961✔
1538
    return TSDB_CODE_SUCCESS;
×
1539
  }
1540

1541
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
103,961✔
1542

1543
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
103,909✔
1544
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1545
    pStmt->bInfo.needParse = false;
×
1546
  }
1547
  STMT_ERR_RET(stmtCreateRequest(pStmt));
103,909✔
1548

1549
  if (pStmt->bInfo.needParse) {
103,896✔
1550
    STMT_ERR_RET(stmtParseSql(pStmt));
×
1551
    if (!pStmt->sql.autoCreateTbl) {
×
1552
      STMT2_WLOG_E("don't need to create table, will not check tags");
×
1553
      return TSDB_CODE_SUCCESS;
×
1554
    }
1555
  }
1556

1557
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
103,896✔
1558
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1559
  }
1560

1561
  STableDataCxt** pDataBlock = NULL;
103,948✔
1562
  if (pStmt->exec.pCurrBlock) {
103,948✔
1563
    pDataBlock = &pStmt->exec.pCurrBlock;
103,818✔
1564
  } else {
1565
    pDataBlock =
1566
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
130✔
1567
    if (NULL == pDataBlock) {
130✔
1568
      STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1569
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
39✔
1570
    }
1571
  }
1572

1573
  if (!((*pDataBlock)->pData->flags & SUBMIT_REQ_AUTO_CREATE_TABLE)) {
103,987✔
1574
    STMT2_DLOG_E("don't need to create, will not check tags");
×
1575
    return TSDB_CODE_SUCCESS;
×
1576
  }
1577

1578
  if (pStmt->sql.fixValueTags) {
103,987✔
1579
    STMT2_TLOG_E("tags are fixed, use one createTbReq");
103,870✔
1580
    STMT_ERR_RET(cloneSVreateTbReq(pStmt->sql.fixValueTbReq, pCreateTbReq));
103,870✔
1581
    if ((*pCreateTbReq)->name) {
103,844✔
1582
      taosMemoryFree((*pCreateTbReq)->name);
103,844✔
1583
    }
1584
    (*pCreateTbReq)->name = taosStrdup(pStmt->bInfo.tbName);
103,844✔
1585
    int32_t vgId = -1;
103,831✔
1586
    STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
103,831✔
1587
    (*pCreateTbReq)->uid = vgId;
103,818✔
1588
    return TSDB_CODE_SUCCESS;
103,818✔
1589
  }
1590

1591
  if ((*pDataBlock)->pData->pCreateTbReq) {
117✔
1592
    STMT2_TLOG_E("tags are fixed, set createTbReq first time");
130✔
1593
    pStmt->sql.fixValueTags = true;
130✔
1594
    STMT_ERR_RET(cloneSVreateTbReq((*pDataBlock)->pData->pCreateTbReq, &pStmt->sql.fixValueTbReq));
130✔
1595
    STMT_ERR_RET(cloneSVreateTbReq(pStmt->sql.fixValueTbReq, pCreateTbReq));
130✔
1596
    (*pCreateTbReq)->uid = (*pDataBlock)->pMeta->vgId;
130✔
1597
  }
1598

1599
  return TSDB_CODE_SUCCESS;
117✔
1600
}
1601

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

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

1612
  STableDataCxt** pDataBlock = NULL;
×
1613

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

1625
  STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields));
×
1626

1627
  return TSDB_CODE_SUCCESS;
×
1628
}
1629

1630
static int stmtFetchStbColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_ALL** fields) {
23✔
1631
  int32_t code = 0;
23✔
1632
  int32_t preCode = pStmt->errCode;
23✔
1633

1634
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
23✔
1635
    return pStmt->errCode;
×
1636
  }
1637

1638
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
23✔
1639
    STMT2_ELOG_E("stmtFetchStbColFields2 only for insert statement");
×
1640
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1641
  }
1642

1643
  STableDataCxt** pDataBlock = NULL;
23✔
1644
  bool            cleanStb = false;
23✔
1645

1646
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx != NULL) {
23✔
1647
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
×
1648
  } else {
1649
    cleanStb = true;
23✔
1650
    pDataBlock =
1651
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
23✔
1652
  }
1653

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

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

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

1680
  if (fieldNum != NULL) {
23✔
1681
    *fieldNum = totalNum;
23✔
1682
  }
1683

1684
  STMT2_DLOG("get insert fields totalNum:%d, tagsNum:%d, colsNum:%d", totalNum, pStmt->sql.placeholderOfTags,
23✔
1685
             pStmt->sql.placeholderOfCols);
1686

1687
_return:
23✔
1688

1689
  pStmt->errCode = preCode;
23✔
1690

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

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

1705
    *idx = pStmt->exec.smInfo.pColIdx;
1706
    return pStmt->exec.smInfo.pCols[pStmt->exec.smInfo.pColIdx++];
1707
  }
1708
}
1709
*/
1710
static int32_t stmtAppendTablePostHandle(STscStmt2* pStmt, SStmtQNode* param) {
1,074,210✔
1711
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
1,074,210✔
1712
    pStmt->sql.siInfo.pVgroupHash =
516,214✔
1713
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
516,007✔
1714
  }
1715
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
1,074,595✔
1716
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
516,214✔
1717
  }
1718

1719
  if (NULL == pStmt->sql.siInfo.pRequest) {
1,074,569✔
1720
    STMT_ERR_RET(buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false,
3,152✔
1721
                              (SRequestObj**)&pStmt->sql.siInfo.pRequest, pStmt->reqid));
1722

1723
    if (pStmt->reqid != 0) {
3,152✔
1724
      pStmt->reqid++;
8✔
1725
    }
1726
    pStmt->exec.pRequest->syncQuery = true;
3,152✔
1727

1728
    pStmt->sql.siInfo.requestId = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->requestId;
3,152✔
1729
    pStmt->sql.siInfo.requestSelf = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->self;
3,152✔
1730
  }
1731

1732
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
1,074,569✔
1733
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
21,236✔
1734
    pStmt->sql.siInfo.tbFromHash = true;
2,027✔
1735
  }
1736

1737
  if (0 == pStmt->sql.siInfo.firstName[0]) {
1,074,526✔
1738
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
3,152✔
1739
  }
1740

1741
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
1,074,526✔
1742
  param->next = NULL;
1,074,526✔
1743

1744
  (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
1,074,434✔
1745

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

1752
  return TSDB_CODE_SUCCESS;
1,074,480✔
1753
}
1754

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

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

1775
  return TSDB_CODE_SUCCESS;
1,074,204✔
1776
}
1777

1778
static int32_t stmtCacheBlock(STscStmt2* pStmt) {
194✔
1779
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
194✔
1780
    return TSDB_CODE_SUCCESS;
×
1781
  }
1782

1783
  uint64_t uid = pStmt->bInfo.tbUid;
194✔
1784
  uint64_t cacheUid = (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) ? pStmt->bInfo.tbSuid : uid;
194✔
1785

1786
  if (taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid))) {
194✔
1787
    STMT2_TLOG("table %s already cached, no need to cache again", pStmt->bInfo.tbFName);
169✔
1788
    return TSDB_CODE_SUCCESS;
169✔
1789
  }
1790

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

1798
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
25✔
1799

1800
  SStmtTableCache cache = {
25✔
1801
      .pDataCtx = pDst,
1802
      .boundTags = pStmt->bInfo.boundTags,
25✔
1803
  };
1804

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

1810
  if (pStmt->sql.autoCreateTbl) {
25✔
1811
    pStmt->bInfo.tagsCached = true;
25✔
1812
  } else {
1813
    pStmt->bInfo.boundTags = NULL;
×
1814
  }
1815

1816
  return TSDB_CODE_SUCCESS;
25✔
1817
}
1818

1819
static int stmtAddBatch2(TAOS_STMT2* stmt) {
516,445✔
1820
  STscStmt2* pStmt = (STscStmt2*)stmt;
516,445✔
1821

1822
  int64_t startUs = taosGetTimestampUs();
516,450✔
1823

1824
  // STMT2_TLOG_E("start to add batch");
1825

1826
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
516,450✔
1827
    return pStmt->errCode;
×
1828
  }
1829

1830
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
516,546✔
1831

1832
  if (pStmt->sql.stbInterlaceMode) {
516,346✔
1833
    int64_t startUs2 = taosGetTimestampUs();
516,187✔
1834
    pStmt->stat.addBatchUs += startUs2 - startUs;
516,187✔
1835

1836
    pStmt->sql.siInfo.tableColsReady = false;
516,187✔
1837

1838
    SStmtQNode* param = NULL;
516,230✔
1839
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
1,032,361✔
1840
    param->restoreTbCols = true;
516,174✔
1841
    param->next = NULL;
516,264✔
1842

1843
    if (pStmt->sql.autoCreateTbl) {
516,309✔
1844
      pStmt->bInfo.tagsCached = true;
208,962✔
1845
    }
1846
    pStmt->bInfo.boundColsCached = true;
516,180✔
1847

1848
    if (pStmt->queue.stopQueue) {
516,266✔
1849
      STMT2_ELOG_E("stmt bind thread is stopped,cannot enqueue bind request");
×
1850
      return TSDB_CODE_TSC_STMT_API_ERROR;
×
1851
    }
1852

1853
    stmtEnqueue(pStmt, param);
516,174✔
1854

1855
    return TSDB_CODE_SUCCESS;
516,277✔
1856
  }
1857

1858
  STMT_ERR_RET(stmtCacheBlock(pStmt));
194✔
1859

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

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

1877
  return TSDB_CODE_SUCCESS;
1878
}
1879

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

1884
  pStmt->exec.pRequest->body.resInfo.numOfCols = pRes->numOfCols;
1885
  pStmt->exec.pRequest->body.resInfo.precision = pRes->precision;
1886

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

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

1903
  return TSDB_CODE_SUCCESS;
1904
}
1905
*/
1906

1907
int stmtBindBatch2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* bind, int32_t colIdx, SVCreateTbReq* pCreateTbReq) {
1,073,837✔
1908
  STscStmt2* pStmt = (STscStmt2*)stmt;
1,073,837✔
1909
  int32_t    code = 0;
1,073,837✔
1910

1911
  int64_t startUs = taosGetTimestampUs();
1,074,558✔
1912

1913
  if (qDebugFlag & DEBUG_TRACE) {
1,074,558✔
1914
    (void)stmtPrintBindv(stmt, bind, colIdx, false);
×
1915
  }
1916

1917
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
1,074,376✔
1918
    return pStmt->errCode;
×
1919
  }
1920

1921
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
1,074,069✔
1922

1923
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
1,074,204✔
1924
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1925
    pStmt->bInfo.needParse = false;
×
1926
  }
1927

1928
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
1,073,670✔
1929
    resetRequest(pStmt);
×
1930
  }
1931

1932
  STMT_ERR_RET(stmtCreateRequest(pStmt));
1,074,172✔
1933
  if (pStmt->bInfo.needParse) {
1,073,669✔
1934
    code = stmtParseSql(pStmt);
×
1935
    if (code != TSDB_CODE_SUCCESS) {
×
1936
      goto cleanup_root;
×
1937
    }
1938
  }
1939

1940
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
1,073,778✔
1941
    code = qStmtBindParams2(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt);
8✔
1942
    if (code != TSDB_CODE_SUCCESS) {
8✔
1943
      goto cleanup_root;
×
1944
    }
1945
    SParseContext ctx = {.requestId = pStmt->exec.pRequest->requestId,
8✔
1946
                         .acctId = pStmt->taos->acctId,
8✔
1947
                         .db = pStmt->exec.pRequest->pDb,
8✔
1948
                         .topicQuery = false,
1949
                         .pSql = pStmt->sql.sqlStr,
8✔
1950
                         .sqlLen = pStmt->sql.sqlLen,
8✔
1951
                         .pMsg = pStmt->exec.pRequest->msgBuf,
8✔
1952
                         .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1953
                         .pTransporter = pStmt->taos->pAppInfo->pTransporter,
8✔
1954
                         .pStmtCb = NULL,
1955
                         .pUser = pStmt->taos->user,
8✔
1956
                         .stmtBindVersion = pStmt->exec.pRequest->stmtBindVersion};
8✔
1957
    ctx.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
8✔
1958
    code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &ctx.pCatalog);
8✔
1959
    if (code != TSDB_CODE_SUCCESS) {
8✔
1960
      goto cleanup_root;
×
1961
    }
1962
    code = qStmtParseQuerySql(&ctx, pStmt->sql.pQuery);
8✔
1963
    if (code != TSDB_CODE_SUCCESS) {
8✔
1964
      goto cleanup_root;
×
1965
    }
1966

1967
    if (pStmt->sql.pQuery->haveResultSet) {
8✔
1968
      STMT_ERR_RET(setResSchemaInfo(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->pResSchema,
16✔
1969
                                    pStmt->sql.pQuery->numOfResCols, pStmt->sql.pQuery->pResExtSchema, true));
1970
      taosMemoryFreeClear(pStmt->sql.pQuery->pResSchema);
8✔
1971
      taosMemoryFreeClear(pStmt->sql.pQuery->pResExtSchema);
8✔
1972
      setResPrecision(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->precision);
8✔
1973
    }
1974

1975
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
8✔
1976
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
8✔
1977
    TSWAP(pStmt->exec.pRequest->targetTableList, pStmt->sql.pQuery->pTargetTableList);
8✔
1978

1979
    // if (STMT_TYPE_QUERY == pStmt->sql.queryRes) {
1980
    //   STMT_ERR_RET(stmtRestoreQueryFields(pStmt));
1981
    // }
1982

1983
    // STMT_ERR_RET(stmtBackupQueryFields(pStmt));
1984

1985
    return TSDB_CODE_SUCCESS;
8✔
1986

1987
  cleanup_root:
×
1988
    STMT2_ELOG("parse query statment unexpected failed code:%d, need to clean node", code);
×
1989
    if (pStmt->sql.pQuery && pStmt->sql.pQuery->pRoot) {
×
1990
      nodesDestroyNode(pStmt->sql.pQuery->pRoot);
×
1991
      pStmt->sql.pQuery->pRoot = NULL;
×
1992
    }
1993
    STMT_ERR_RET(code);
×
1994
  }
1995

1996
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
1,073,948✔
1997
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1998
  }
1999

2000
  STableDataCxt** pDataBlock = NULL;
1,073,666✔
2001

2002
  if (pStmt->exec.pCurrBlock) {
1,073,666✔
2003
    pDataBlock = &pStmt->exec.pCurrBlock;
1,070,852✔
2004
  } else {
2005
    pDataBlock =
2006
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
3,177✔
2007
    if (NULL == pDataBlock) {
3,177✔
2008
      STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
2009
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
2010
    }
2011
    pStmt->exec.pCurrBlock = *pDataBlock;
3,177✔
2012
    if (pStmt->sql.stbInterlaceMode) {
3,177✔
2013
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
3,152✔
2014
      (*pDataBlock)->pData->aCol = NULL;
3,152✔
2015
    }
2016
    if (colIdx < -1) {
3,177✔
2017
      pStmt->sql.bindRowFormat = true;
×
2018
      taosArrayDestroy((*pDataBlock)->pData->aCol);
×
2019
      (*pDataBlock)->pData->aCol = taosArrayInit(20, POINTER_BYTES);
×
2020
    }
2021
  }
2022

2023
  int64_t startUs2 = taosGetTimestampUs();
1,074,506✔
2024
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
1,074,506✔
2025

2026
  SStmtQNode* param = NULL;
1,074,420✔
2027
  if (pStmt->sql.stbInterlaceMode) {
1,074,322✔
2028
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
2,147,976✔
2029
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
2,147,965✔
2030
    taosArrayClear(param->tblData.aCol);
1,074,204✔
2031

2032
    // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES);
2033

2034
    param->restoreTbCols = false;
1,073,291✔
2035
    param->tblData.isOrdered = true;
1,073,291✔
2036
    param->tblData.isDuplicateTs = false;
1,073,334✔
2037
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
1,073,285✔
2038

2039
    param->pCreateTbReq = pCreateTbReq;
1,073,187✔
2040
  }
2041

2042
  int64_t startUs3 = taosGetTimestampUs();
1,073,943✔
2043
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
1,073,943✔
2044

2045
  SArray*   pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
1,073,440✔
2046
  SBlobSet* pBlob = NULL;
1,073,791✔
2047
  if (colIdx < 0) {
1,073,489✔
2048
    if (pStmt->sql.stbInterlaceMode) {
1,074,174✔
2049
      (*pDataBlock)->pData->flags &= ~SUBMIT_REQ_COLUMN_DATA_FORMAT;
1,074,194✔
2050
      code = qBindStmtStbColsValue2(*pDataBlock, pCols, pStmt->bInfo.fixedValueCols, bind, pStmt->exec.pRequest->msgBuf,
1,405,369✔
2051
                                    pStmt->exec.pRequest->msgBufLen, &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo,
1,074,334✔
2052
                                    pStmt->taos->optionInfo.charsetCxt, &pBlob);
1,074,334✔
2053
      param->tblData.isOrdered = (*pDataBlock)->ordered;
1,074,138✔
2054
      param->tblData.isDuplicateTs = (*pDataBlock)->duplicateTs;
1,074,353✔
2055
    } else {
2056
      if (colIdx == -1) {
135✔
2057
        if (pStmt->sql.bindRowFormat) {
194✔
2058
          STMT2_ELOG_E("can't mix bind row format and bind column format");
×
2059
          STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2060
        }
2061
        code = qBindStmtColsValue2(*pDataBlock, pCols, pStmt->bInfo.fixedValueCols, bind, pStmt->exec.pRequest->msgBuf,
194✔
2062
                                   pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt);
194✔
2063
      } else {
2064
        code =
2065
            qBindStmt2RowValue(*pDataBlock, (*pDataBlock)->pData->aRowP, pStmt->bInfo.fixedValueCols, bind,
×
2066
                               pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
×
2067
                               &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt);
86✔
2068
      }
2069
    }
2070

2071
    if (code) {
1,074,326✔
2072
      STMT2_ELOG("bind cols or rows failed, error:%s", tstrerror(code));
×
2073
      STMT_ERR_RET(code);
×
2074
    }
2075
  } else {
2076
    if (pStmt->sql.stbInterlaceMode) {
11✔
2077
      STMT2_ELOG_E("bind single column not allowed in stb insert mode");
×
2078
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2079
    }
2080

2081
    if (pStmt->sql.bindRowFormat) {
×
2082
      STMT2_ELOG_E("can't mix bind row format and bind column format");
×
2083
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2084
    }
2085

2086
    if (colIdx != (pStmt->bInfo.sBindLastIdx + 1) && colIdx != 0) {
×
2087
      STMT2_ELOG_E("bind column index not in sequence");
×
2088
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2089
    }
2090

2091
    pStmt->bInfo.sBindLastIdx = colIdx;
×
2092

2093
    if (0 == colIdx) {
×
2094
      pStmt->bInfo.sBindRowNum = bind->num;
×
2095
    }
2096

2097
    code = qBindStmtSingleColValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
×
2098
                                    pStmt->exec.pRequest->msgBufLen, colIdx, pStmt->bInfo.sBindRowNum,
×
2099
                                    pStmt->taos->optionInfo.charsetCxt);
×
2100
    if (code) {
×
2101
      STMT2_ELOG("bind single col failed, error:%s", tstrerror(code));
×
2102
      STMT_ERR_RET(code);
×
2103
    }
2104
  }
2105

2106
  int64_t startUs4 = taosGetTimestampUs();
1,074,576✔
2107
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
1,074,576✔
2108

2109
  if (pStmt->stbInterlaceMode) {
1,074,447✔
2110
    if (param) param->tblData.pBlobSet = pBlob;
1,074,572✔
2111
  }
2112

2113
  if (pStmt->sql.stbInterlaceMode) {
1,074,631✔
2114
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
1,074,245✔
2115
  } else {
2116
    STMT_ERR_RET(stmtAddBatch2(pStmt));
269✔
2117
  }
2118

2119
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
1,074,659✔
2120
  return TSDB_CODE_SUCCESS;
1,074,757✔
2121
}
2122

2123
/*
2124
int stmtUpdateTableUid(STscStmt2* pStmt, SSubmitRsp* pRsp) {
2125
  tscDebug("stmt start to update tbUid, blockNum:%d", pRsp->nBlocks);
2126

2127
  int32_t code = 0;
2128
  int32_t finalCode = 0;
2129
  size_t  keyLen = 0;
2130
  void*   pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
2131
  while (pIter) {
2132
    STableDataCxt* pBlock = *(STableDataCxt**)pIter;
2133
    char*          key = taosHashGetKey(pIter, &keyLen);
2134

2135
    STableMeta* pMeta = qGetTableMetaInDataBlock(pBlock);
2136
    if (pMeta->uid) {
2137
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2138
      continue;
2139
    }
2140

2141
    SSubmitBlkRsp* blkRsp = NULL;
2142
    int32_t        i = 0;
2143
    for (; i < pRsp->nBlocks; ++i) {
2144
      blkRsp = pRsp->pBlocks + i;
2145
      if (strlen(blkRsp->tblFName) != keyLen) {
2146
        continue;
2147
      }
2148

2149
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
2150
        continue;
2151
      }
2152

2153
      break;
2154
    }
2155

2156
    if (i < pRsp->nBlocks) {
2157
      tscDebug("auto created table %s uid updated from %" PRIx64 " to %" PRIx64, blkRsp->tblFName, pMeta->uid,
2158
               blkRsp->uid);
2159

2160
      pMeta->uid = blkRsp->uid;
2161
      pStmt->bInfo.tbUid = blkRsp->uid;
2162
    } else {
2163
      tscDebug("table %s not found in submit rsp, will update from catalog", pStmt->bInfo.tbFName);
2164
      if (NULL == pStmt->pCatalog) {
2165
        code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog);
2166
        if (code) {
2167
          pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2168
          finalCode = code;
2169
          continue;
2170
        }
2171
      }
2172

2173
      code = stmtCreateRequest(pStmt);
2174
      if (code) {
2175
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2176
        finalCode = code;
2177
        continue;
2178
      }
2179

2180
      STableMeta*      pTableMeta = NULL;
2181
      SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
2182
                               .requestId = pStmt->exec.pRequest->requestId,
2183
                               .requestObjRefId = pStmt->exec.pRequest->self,
2184
                               .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
2185
      code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
2186

2187
      pStmt->stat.ctgGetTbMetaNum++;
2188

2189
      taos_free_result(pStmt->exec.pRequest);
2190
      pStmt->exec.pRequest = NULL;
2191

2192
      if (code || NULL == pTableMeta) {
2193
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2194
        finalCode = code;
2195
        taosMemoryFree(pTableMeta);
2196
        continue;
2197
      }
2198

2199
      pMeta->uid = pTableMeta->uid;
2200
      pStmt->bInfo.tbUid = pTableMeta->uid;
2201
      taosMemoryFree(pTableMeta);
2202
    }
2203

2204
    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2205
  }
2206

2207
  return finalCode;
2208
}
2209
*/
2210
/*
2211
int stmtStaticModeExec(TAOS_STMT* stmt) {
2212
  STscStmt2*   pStmt = (STscStmt2*)stmt;
2213
  int32_t     code = 0;
2214
  SSubmitRsp* pRsp = NULL;
2215
  if (pStmt->sql.staticMode) {
2216
    return TSDB_CODE_TSC_STMT_API_ERROR;
2217
  }
2218

2219
  STMT_DLOG_E("start to exec");
2220

2221
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
2222

2223
  STMT_ERR_RET(qBuildStmtOutputFromTbList(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pTbBlkList,
2224
pStmt->exec.pCurrBlock, pStmt->exec.tbBlkNum));
2225

2226
  launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
2227

2228
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
2229
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
2230
    if (code) {
2231
      pStmt->exec.pRequest->code = code;
2232
    } else {
2233
      tFreeSSubmitRsp(pRsp);
2234
      STMT_ERR_RET(stmtResetStmt(pStmt));
2235
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
2236
    }
2237
  }
2238

2239
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
2240

2241
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
2242
  pStmt->affectedRows += pStmt->exec.affectedRows;
2243

2244
_return:
2245

2246
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
2247

2248
  tFreeSSubmitRsp(pRsp);
2249

2250
  ++pStmt->sql.runTimes;
2251

2252
  STMT_RET(code);
2253
}
2254
*/
2255

2256
static int32_t createParseContext(const SRequestObj* pRequest, SParseContext** pCxt, SSqlCallbackWrapper* pWrapper) {
×
2257
  const STscObj* pTscObj = pRequest->pTscObj;
×
2258

2259
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
×
2260
  if (*pCxt == NULL) {
×
2261
    return terrno;
×
2262
  }
2263

2264
  **pCxt = (SParseContext){.requestId = pRequest->requestId,
×
2265
                           .requestRid = pRequest->self,
×
2266
                           .acctId = pTscObj->acctId,
×
2267
                           .db = pRequest->pDb,
×
2268
                           .topicQuery = false,
2269
                           .pSql = pRequest->sqlstr,
×
2270
                           .sqlLen = pRequest->sqlLen,
×
2271
                           .pMsg = pRequest->msgBuf,
×
2272
                           .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
2273
                           .pTransporter = pTscObj->pAppInfo->pTransporter,
×
2274
                           .pStmtCb = NULL,
2275
                           .pUser = pTscObj->user,
×
2276
                           .userId = pTscObj->userId,
×
2277
                           .pEffectiveUser = pRequest->effectiveUser,
×
2278
                           .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
×
2279
                           .enableSysInfo = pTscObj->sysInfo,
×
2280
                           .async = true,
2281
                           .svrVer = pTscObj->sVer,
×
2282
                           .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
×
2283
                           .allocatorId = pRequest->allocatorRefId,
×
2284
                           .parseSqlFp = clientParseSql,
2285
                           .parseSqlParam = pWrapper};
2286
  int8_t biMode = atomic_load_8(&((STscObj*)pTscObj)->biMode);
×
2287
  (*pCxt)->biMode = biMode;
×
2288
  return TSDB_CODE_SUCCESS;
×
2289
}
2290

2291
static void asyncQueryCb(void* userdata, TAOS_RES* res, int code) {
×
2292
  STscStmt2*        pStmt = userdata;
×
2293
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
×
2294
  pStmt->asyncResultAvailable = true;
×
2295
  pStmt->exec.pRequest->inCallback = true;
×
2296

2297
  if (code == TSDB_CODE_SUCCESS) {
×
2298
    pStmt->exec.affectedRows = taos_affected_rows(res);
×
2299
    pStmt->affectedRows += pStmt->exec.affectedRows;
×
2300
  }
2301

2302
  if (fp) {
×
2303
    fp(pStmt->options.userdata, res, code);
×
2304
  }
2305

2306
  while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
×
2307
    taosUsleep(1);
×
2308
  }
2309
  (void)stmtCleanExecInfo(pStmt, (code ? false : true), false);
×
2310
  ++pStmt->sql.runTimes;
×
2311
  if (pStmt->exec.pRequest != NULL) {
×
2312
    pStmt->exec.pRequest->inCallback = false;
×
2313
  }
2314

2315
  if (tsem_post(&pStmt->asyncExecSem) != 0) {
×
2316
    STMT2_ELOG_E("fail to post asyncExecSem");
×
2317
  }
2318
}
×
2319

2320
int stmtExec2(TAOS_STMT2* stmt, int* affected_rows) {
516,232✔
2321
  STscStmt2* pStmt = (STscStmt2*)stmt;
516,232✔
2322
  int32_t    code = 0;
516,232✔
2323
  int64_t    startUs = taosGetTimestampUs();
516,355✔
2324

2325
  STMT2_DLOG_E("start to exec");
516,355✔
2326

2327
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
516,342✔
2328
    return pStmt->errCode;
×
2329
  }
2330

2331
  STMT_ERR_RET(taosThreadMutexLock(&pStmt->asyncBindParam.mutex));
516,297✔
2332
  while (atomic_load_8((int8_t*)&pStmt->asyncBindParam.asyncBindNum) > 0) {
516,428✔
2333
    (void)taosThreadCondWait(&pStmt->asyncBindParam.waitCond, &pStmt->asyncBindParam.mutex);
×
2334
  }
2335
  STMT_ERR_RET(taosThreadMutexUnlock(&pStmt->asyncBindParam.mutex));
516,320✔
2336

2337
  if (pStmt->sql.stbInterlaceMode) {
516,332✔
2338
    STMT_ERR_RET(stmtAddBatch2(pStmt));
516,346✔
2339
  }
2340

2341
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
516,303✔
2342

2343
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
516,232✔
2344
    if (pStmt->sql.stbInterlaceMode) {
516,267✔
2345
      int64_t startTs = taosGetTimestampUs();
516,300✔
2346
      // wait for stmt bind thread to finish
2347
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
2,122,693✔
2348
        taosUsleep(1);
1,606,236✔
2349
      }
2350

2351
      if (pStmt->errCode != TSDB_CODE_SUCCESS) {
516,382✔
2352
        return pStmt->errCode;
×
2353
      }
2354

2355
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
516,283✔
2356
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
516,330✔
2357
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
516,223✔
2358
      pStmt->sql.siInfo.pVgroupHash = NULL;
516,277✔
2359
      pStmt->sql.siInfo.pVgroupList = NULL;
516,277✔
2360
    } else {
2361
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
×
2362
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
25✔
2363

2364
      STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
25✔
2365

2366
      STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
25✔
2367
    }
2368
  }
2369

2370
  pStmt->asyncResultAvailable = false;
516,316✔
2371
  SRequestObj*      pRequest = pStmt->exec.pRequest;
516,224✔
2372
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
516,310✔
2373
  STMT2_DLOG("EXEC INFO :req:0x%" PRIx64 ", QID:0x%" PRIx64 ", exec sql:%s,  conn:%" PRId64, pRequest->self,
516,267✔
2374
             pRequest->requestId, pStmt->sql.sqlStr, pRequest->pTscObj->id);
2375

2376
  if (!fp) {
516,172✔
2377
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
516,172✔
2378

2379
    if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
515,874✔
2380
      STMT2_ELOG_E("exec failed errorcode:NEED_CLIENT_HANDLE_ERROR, need to refresh meta and retry");
×
2381
      code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
×
2382
      if (code) {
×
2383
        pStmt->exec.pRequest->code = code;
×
2384

2385
      } else {
2386
        STMT_ERR_RET(stmtResetStmt(pStmt));
×
2387
        STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
×
2388
      }
2389
    }
2390

2391
    STMT_ERR_JRET(pStmt->exec.pRequest->code);
516,281✔
2392

2393
    pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
516,146✔
2394
    if (affected_rows) {
516,251✔
2395
      *affected_rows = pStmt->exec.affectedRows;
516,034✔
2396
    }
2397
    pStmt->affectedRows += pStmt->exec.affectedRows;
516,036✔
2398

2399
    // wait for stmt bind thread to finish
2400
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
516,446✔
2401
      taosUsleep(1);
201✔
2402
    }
2403

2404
    STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
516,033✔
2405

2406
    ++pStmt->sql.runTimes;
516,091✔
2407
  } else {
2408
    SSqlCallbackWrapper* pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
×
2409
    if (pWrapper == NULL) {
×
2410
      code = terrno;
×
2411
    } else {
2412
      pWrapper->pRequest = pRequest;
×
2413
      pRequest->pWrapper = pWrapper;
×
2414
    }
2415
    if (TSDB_CODE_SUCCESS == code) {
×
2416
      code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
×
2417
    }
2418
    pRequest->syncQuery = false;
×
2419
    pRequest->body.queryFp = asyncQueryCb;
×
2420
    ((SSyncQueryParam*)(pRequest)->body.interParam)->userParam = pStmt;
×
2421

2422
    pStmt->execSemWaited = false;
×
2423
    launchAsyncQuery(pRequest, pStmt->sql.pQuery, NULL, pWrapper);
×
2424
  }
2425

2426
_return:
516,059✔
2427
  if (code) {
516,059✔
2428
    STMT2_ELOG("exec failed, error:%s", tstrerror(code));
×
2429
  }
2430
  pStmt->stat.execUseUs += taosGetTimestampUs() - startUs;
515,999✔
2431

2432
  STMT_RET(code);
516,145✔
2433
}
2434

2435
int stmtClose2(TAOS_STMT2* stmt) {
3,081✔
2436
  STscStmt2* pStmt = (STscStmt2*)stmt;
3,081✔
2437

2438
  STMT2_DLOG_E("start to close stmt");
3,081✔
2439
  taosMemoryFreeClear(pStmt->db);
3,081✔
2440

2441
  if (pStmt->bindThreadInUse) {
3,081✔
2442
    // wait for stmt bind thread to finish
2443
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
3,048✔
2444
      taosUsleep(1);
×
2445
    }
2446

2447
    (void)taosThreadMutexLock(&pStmt->queue.mutex);
3,048✔
2448
    pStmt->queue.stopQueue = true;
3,048✔
2449
    (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
3,048✔
2450
    (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
3,048✔
2451

2452
    (void)taosThreadJoin(pStmt->bindThread, NULL);
3,048✔
2453
    pStmt->bindThreadInUse = false;
3,048✔
2454

2455
    (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
3,048✔
2456
    (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
3,048✔
2457
  }
2458

2459
  TSC_ERR_RET(taosThreadMutexLock(&pStmt->asyncBindParam.mutex));
3,081✔
2460
  while (atomic_load_8((int8_t*)&pStmt->asyncBindParam.asyncBindNum) > 0) {
3,081✔
2461
    (void)taosThreadCondWait(&pStmt->asyncBindParam.waitCond, &pStmt->asyncBindParam.mutex);
×
2462
  }
2463
  TSC_ERR_RET(taosThreadMutexUnlock(&pStmt->asyncBindParam.mutex));
3,081✔
2464

2465
  (void)taosThreadCondDestroy(&pStmt->asyncBindParam.waitCond);
3,081✔
2466
  (void)taosThreadMutexDestroy(&pStmt->asyncBindParam.mutex);
3,081✔
2467

2468
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
3,081✔
2469
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
×
2470
      STMT2_ELOG_E("fail to wait asyncExecSem");
×
2471
    }
2472
  }
2473

2474
  STMT2_DLOG("stbInterlaceMode:%d, statInfo: ctgGetTbMetaNum=>%" PRId64 ", getCacheTbInfo=>%" PRId64
3,081✔
2475
             ", parseSqlNum=>%" PRId64 ", pStmt->stat.bindDataNum=>%" PRId64
2476
             ", settbnameAPI:%u, bindAPI:%u, addbatchAPI:%u, execAPI:%u"
2477
             ", setTbNameUs:%" PRId64 ", bindDataUs:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64 " addBatchUs:%" PRId64
2478
             ", execWaitUs:%" PRId64 ", execUseUs:%" PRId64,
2479
             pStmt->sql.stbInterlaceMode, pStmt->stat.ctgGetTbMetaNum, pStmt->stat.getCacheTbInfo,
2480
             pStmt->stat.parseSqlNum, pStmt->stat.bindDataNum, pStmt->seqIds[STMT_SETTBNAME], pStmt->seqIds[STMT_BIND],
2481
             pStmt->seqIds[STMT_ADD_BATCH], pStmt->seqIds[STMT_EXECUTE], pStmt->stat.setTbNameUs,
2482
             pStmt->stat.bindDataUs1, pStmt->stat.bindDataUs2, pStmt->stat.bindDataUs3, pStmt->stat.bindDataUs4,
2483
             pStmt->stat.addBatchUs, pStmt->stat.execWaitUs, pStmt->stat.execUseUs);
2484
  if (pStmt->sql.stbInterlaceMode) {
3,081✔
2485
    pStmt->bInfo.tagsCached = false;
3,048✔
2486
  }
2487
  pStmt->bInfo.boundColsCached = false;
3,081✔
2488

2489
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
3,081✔
2490

2491
  if (pStmt->options.asyncExecFn) {
3,081✔
2492
    if (tsem_destroy(&pStmt->asyncExecSem) != 0) {
×
2493
      STMT2_ELOG_E("fail to destroy asyncExecSem");
×
2494
    }
2495
  }
2496
  taosMemoryFree(stmt);
3,081✔
2497

2498
  return TSDB_CODE_SUCCESS;
3,081✔
2499
}
2500

2501
const char* stmtErrstr2(TAOS_STMT2* stmt) {
×
2502
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
2503

2504
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
×
2505
    return (char*)tstrerror(terrno);
×
2506
  }
2507

2508
  // if stmt async exec ,error code is pStmt->exec.pRequest->code
2509
  if (!(pStmt->sql.status >= STMT_EXECUTE && pStmt->options.asyncExecFn != NULL && pStmt->asyncResultAvailable)) {
×
2510
    pStmt->exec.pRequest->code = terrno;
×
2511
  }
2512

2513
  SRequestObj* pRequest = pStmt->exec.pRequest;
×
2514
  if (NULL != pRequest->msgBuf && (strlen(pRequest->msgBuf) > 0 || pRequest->code == TSDB_CODE_RPC_FQDN_ERROR)) {
×
2515
    return pRequest->msgBuf;
×
2516
  }
2517
  return (const char*)tstrerror(pRequest->code);
×
2518
}
2519
/*
2520
int stmtAffectedRows(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->affectedRows; }
2521

2522
int stmtAffectedRowsOnce(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->exec.affectedRows; }
2523
*/
2524

2525
int stmtParseColFields2(TAOS_STMT2* stmt) {
23✔
2526
  int32_t    code = 0;
23✔
2527
  STscStmt2* pStmt = (STscStmt2*)stmt;
23✔
2528
  int32_t    preCode = pStmt->errCode;
23✔
2529

2530
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
23✔
2531
    return pStmt->errCode;
×
2532
  }
2533

2534
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
23✔
2535
    STMT2_ELOG_E("stmtParseColFields2 only for insert");
×
2536
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2537
  }
2538

2539
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
23✔
2540

2541
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
23✔
2542
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
2543
    pStmt->bInfo.needParse = false;
×
2544
  }
2545
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx != NULL) {
23✔
2546
    pStmt->bInfo.needParse = false;
×
2547
  }
2548

2549
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
23✔
2550

2551
  if (pStmt->bInfo.needParse) {
23✔
2552
    STMT_ERRI_JRET(stmtParseSql(pStmt));
23✔
2553
  }
2554

2555
_return:
23✔
2556
  // compatible with previous versions
2557
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && (pStmt->bInfo.tbNameFlag & NO_DATA_USING_CLAUSE) == 0x0) {
23✔
2558
    code = TSDB_CODE_TSC_STMT_TBNAME_ERROR;
×
2559
  }
2560

2561
  pStmt->errCode = preCode;
23✔
2562

2563
  return code;
23✔
2564
}
2565

2566
int stmtGetStbColFields2(TAOS_STMT2* stmt, int* nums, TAOS_FIELD_ALL** fields) {
23✔
2567
  int32_t code = stmtParseColFields2(stmt);
23✔
2568
  if (code != TSDB_CODE_SUCCESS) {
23✔
2569
    return code;
×
2570
  }
2571

2572
  return stmtFetchStbColFields2(stmt, nums, fields);
23✔
2573
}
2574

2575
int stmtGetParamNum2(TAOS_STMT2* stmt, int* nums) {
×
2576
  int32_t    code = 0;
×
2577
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
2578
  int32_t    preCode = pStmt->errCode;
×
2579

2580
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
2581
    return pStmt->errCode;
×
2582
  }
2583

2584
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
×
2585

2586
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
×
2587
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
2588
    pStmt->bInfo.needParse = false;
×
2589
  }
2590

2591
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
×
2592
    resetRequest(pStmt);
×
2593
  }
2594

2595
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
2596

2597
  if (pStmt->bInfo.needParse) {
×
2598
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
2599
  }
2600

2601
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
2602
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
×
2603
  } else {
2604
    STMT_ERRI_JRET(stmtFetchColFields2(stmt, nums, NULL));
×
2605
  }
2606

2607
  STMT2_DLOG("get param num success, nums:%d", *nums);
×
2608

2609
_return:
×
2610

2611
  pStmt->errCode = preCode;
×
2612

2613
  return code;
×
2614
}
2615

2616
TAOS_RES* stmtUseResult2(TAOS_STMT2* stmt) {
8✔
2617
  STscStmt2* pStmt = (STscStmt2*)stmt;
8✔
2618

2619
  STMT2_TLOG_E("start to use result");
8✔
2620

2621
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
8✔
2622
    STMT2_ELOG_E("useResult only for query statement");
×
2623
    return NULL;
×
2624
  }
2625

2626
  if (pStmt->options.asyncExecFn != NULL && !pStmt->asyncResultAvailable) {
8✔
2627
    STMT2_ELOG_E("use result after callBackFn return");
×
2628
    return NULL;
×
2629
  }
2630

2631
  if (tsUseAdapter) {
8✔
2632
    TAOS_RES* res = (TAOS_RES*)pStmt->exec.pRequest;
×
2633
    pStmt->exec.pRequest = NULL;
×
2634
    return res;
×
2635
  }
2636

2637
  return pStmt->exec.pRequest;
8✔
2638
}
2639

2640
int32_t stmtAsyncBindThreadFunc(void* args) {
×
2641
  qInfo("async stmt bind thread started");
×
2642

2643
  ThreadArgs* targs = (ThreadArgs*)args;
×
2644
  STscStmt2*  pStmt = (STscStmt2*)targs->stmt;
×
2645

2646
  int code = taos_stmt2_bind_param(targs->stmt, targs->bindv, targs->col_idx);
×
2647
  targs->fp(targs->param, NULL, code);
×
2648
  (void)taosThreadMutexLock(&(pStmt->asyncBindParam.mutex));
×
2649
  (void)atomic_sub_fetch_8(&pStmt->asyncBindParam.asyncBindNum, 1);
×
2650
  (void)taosThreadCondSignal(&(pStmt->asyncBindParam.waitCond));
×
2651
  (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2652
  taosMemoryFree(args);
×
2653

2654
  qInfo("async stmt bind thread stopped");
×
2655

2656
  return code;
×
2657
}
2658

2659
void stmtBuildErrorMsg(STscStmt2* pStmt, const char* msg) {
×
2660
  if (pStmt == NULL || msg == NULL) {
×
2661
    return;
×
2662
  }
2663

2664
  if (pStmt->exec.pRequest == NULL) {
×
2665
    return;
×
2666
  }
2667

2668
  if (pStmt->exec.pRequest->msgBuf == NULL) {
×
2669
    return;
×
2670
  }
2671

2672
  size_t msgLen = strlen(msg);
×
2673
  size_t bufLen = pStmt->exec.pRequest->msgBufLen;
×
2674

2675
  if (msgLen >= bufLen) {
×
2676
    tstrncpy(pStmt->exec.pRequest->msgBuf, msg, bufLen - 1);
×
2677
    pStmt->exec.pRequest->msgBuf[bufLen - 1] = '\0';
×
2678
    pStmt->exec.pRequest->msgBufLen = bufLen - 1;
×
2679
  } else {
2680
    tstrncpy(pStmt->exec.pRequest->msgBuf, msg, bufLen);
×
2681
    pStmt->exec.pRequest->msgBufLen = msgLen;
×
2682
  }
2683

2684
  return;
×
2685
}
2686

2687
int32_t stmtBuildErrorMsgWithCode(STscStmt2* pStmt, const char* msg, int32_t errorCode) {
×
2688
  stmtBuildErrorMsg(pStmt, msg);
×
2689
  pStmt->errCode = errorCode;
×
2690

2691
  return errorCode;
×
2692
}
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