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

taosdata / TDengine / #4918

08 Jan 2026 11:50AM UTC coverage: 65.916% (+0.5%) from 65.42%
#4918

push

travis-ci

web-flow
merge: from main to 3.0 branch #34215

16 of 22 new or added lines in 3 files covered. (72.73%)

788 existing lines in 116 files now uncovered.

204221 of 309822 relevant lines covered (65.92%)

126504701.11 hits per line

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

62.3
/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
static FORCE_INLINE int32_t stmtAllocQNodeFromBuf(STableBufInfo* pTblBuf, void** pBuf) {
13
  if (pTblBuf->buffOffset < pTblBuf->buffSize) {
1,456,073✔
14
    *pBuf = (char*)pTblBuf->pCurBuff + pTblBuf->buffOffset;
1,456,189✔
15
    pTblBuf->buffOffset += pTblBuf->buffUnit;
1,455,914✔
16
  } else if (pTblBuf->buffIdx < taosArrayGetSize(pTblBuf->pBufList)) {
×
17
    pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, pTblBuf->buffIdx++);
×
18
    if (NULL == pTblBuf->pCurBuff) {
×
UNCOV
19
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
20
    }
21
    *pBuf = pTblBuf->pCurBuff;
×
UNCOV
22
    pTblBuf->buffOffset = pTblBuf->buffUnit;
×
23
  } else {
24
    void* buff = taosMemoryMalloc(pTblBuf->buffSize);
×
25
    if (NULL == buff) {
×
UNCOV
26
      return terrno;
×
27
    }
28

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

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

39
  return TSDB_CODE_SUCCESS;
1,456,070✔
40
}
41

42
static bool stmtDequeue(STscStmt2* pStmt, SStmtQNode** param) {
1,456,099✔
43
  int i = 0;
1,456,099✔
44
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
6,250,509✔
45
    if (pStmt->queue.stopQueue) {
4,797,131✔
46
      return false;
2,647✔
47
    }
48
    if (i < 10) {
4,794,523✔
49
      taosUsleep(1);
4,500,113✔
50
      i++;
4,499,900✔
51
    } else {
52
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
294,410✔
53
      if (pStmt->queue.stopQueue) {
294,522✔
54
        (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
×
UNCOV
55
        return false;
×
56
      }
57
      if (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
294,522✔
58
        (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
294,411✔
59
      }
60
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
294,420✔
61
    }
62
  }
63

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

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

76
  SStmtQNode* node = pStmt->queue.head->next;
1,453,356✔
77
  pStmt->queue.head->next = node->next;
1,453,356✔
78
  if (pStmt->queue.tail == node) {
1,453,437✔
79
    pStmt->queue.tail = pStmt->queue.head;
818,085✔
80
  }
81
  node->next = NULL;
1,453,437✔
82
  *param = node;
1,453,437✔
83

84
  (void)atomic_sub_fetch_64((int64_t*)&pStmt->queue.qRemainNum, 1);
1,453,398✔
85
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
1,453,578✔
86

87
  STMT2_TLOG("dequeue success, node:%p, remainNum:%" PRId64, node, pStmt->queue.qRemainNum);
1,453,578✔
88

89
  return true;
1,453,374✔
90
}
91

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

98
  param->next = NULL;
1,453,362✔
99

100
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
1,453,362✔
101

102
  pStmt->queue.tail->next = param;
1,453,494✔
103
  pStmt->queue.tail = param;
1,453,494✔
104
  pStmt->stat.bindDataNum++;
1,453,494✔
105

106
  (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
1,453,494✔
107
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
1,453,554✔
108

109
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
1,453,470✔
110

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

115
static int32_t stmtCreateRequest(STscStmt2* pStmt) {
2,548,079✔
116
  int32_t code = 0;
2,548,079✔
117

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

137
  return code;
2,548,160✔
138
}
139

140
static int32_t stmtSwitchStatus(STscStmt2* pStmt, STMT_STATUS newStatus) {
3,490,100✔
141
  int32_t code = 0;
3,490,100✔
142

143
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
3,490,100✔
144
    STMT2_LOG_SEQ(newStatus);
3,490,339✔
145
  }
146

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

152
  switch (newStatus) {
3,490,351✔
153
    case STMT_PREPARE:
2,673✔
154
      pStmt->errCode = 0;
2,673✔
155
      break;
2,552✔
156
    case STMT_SETTBNAME:
981,142✔
157
      if (STMT_STATUS_EQ(INIT)) {
981,142✔
UNCOV
158
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
159
      }
160
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
981,181✔
UNCOV
161
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
162
      }
163
      break;
981,140✔
164
    case STMT_SETTAGS:
580,752✔
165
      if (STMT_STATUS_EQ(INIT)) {
580,752✔
UNCOV
166
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
167
      }
168
      break;
580,752✔
169
    case STMT_FETCH_FIELDS:
18✔
170
      if (STMT_STATUS_EQ(INIT)) {
18✔
UNCOV
171
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
172
      }
173
      break;
18✔
174
    case STMT_BIND:
980,994✔
175
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
980,994✔
UNCOV
176
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
177
      }
178
      /*
179
            if ((pStmt->sql.type == STMT_TYPE_MULTI_INSERT) && ()) {
180
              code = TSDB_CODE_TSC_STMT_API_ERROR;
181
            }
182
      */
183
      break;
980,994✔
184
    case STMT_BIND_COL:
×
185
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND)) {
×
UNCOV
186
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
187
      }
UNCOV
188
      break;
×
189
    case STMT_ADD_BATCH:
472,417✔
190
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
472,417✔
UNCOV
191
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
192
      }
193
      break;
472,417✔
194
    case STMT_EXECUTE:
472,355✔
195
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
472,355✔
196
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
8✔
197
            STMT_STATUS_NE(BIND_COL)) {
×
UNCOV
198
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
199
        }
200
      } else {
201
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS)) {
472,347✔
UNCOV
202
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
203
        }
204
      }
205
      break;
472,355✔
206
    default:
×
207
      code = TSDB_CODE_APP_ERROR;
×
UNCOV
208
      break;
×
209
  }
210

211
  STMT_ERR_RET(code);
3,490,228✔
212

213
  pStmt->sql.status = newStatus;
3,490,228✔
214

215
  return TSDB_CODE_SUCCESS;
3,490,306✔
216
}
217

218
static int32_t stmtGetTbName(TAOS_STMT2* stmt, char** tbName) {
2,683✔
219
  STscStmt2* pStmt = (STscStmt2*)stmt;
2,683✔
220

221
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
2,683✔
222

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

228
  *tbName = pStmt->bInfo.tbName;
2,665✔
229

230
  return TSDB_CODE_SUCCESS;
2,665✔
231
}
232

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

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

246
  (void)memcpy(&pStmt->bInfo.sname, tbName, sizeof(*tbName));
2,605✔
247
  tstrncpy(pStmt->bInfo.tbFName, tbFName, sizeof(pStmt->bInfo.tbFName));
2,642✔
248
  pStmt->bInfo.tbFName[sizeof(pStmt->bInfo.tbFName) - 1] = 0;
2,683✔
249

250
  pStmt->bInfo.tbUid = autoCreateTbl ? 0 : pTableMeta->uid;
2,683✔
251
  pStmt->bInfo.tbSuid = pTableMeta->suid;
2,646✔
252
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
2,683✔
253
  pStmt->bInfo.tbType = pTableMeta->tableType;
2,646✔
254

255
  if (!pStmt->bInfo.tagsCached) {
2,683✔
256
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
2,642✔
257
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
2,683✔
258
  }
259

260
  if (cols) {
2,683✔
261
    pStmt->bInfo.boundCols =
×
262
        tSimpleHashInit(taosArrayGetSize(cols), taosGetDefaultHashFunction(TSDB_DATA_TYPE_SMALLINT));
×
263
    if (pStmt->bInfo.boundCols) {
×
264
      for (int32_t i = 0; i < taosArrayGetSize(cols); i++) {
×
265
        SColVal* pColVal = taosArrayGet(cols, i);
×
266
        if (pColVal) {
×
267
          code = tSimpleHashPut(pStmt->bInfo.boundCols, &pColVal->cid, sizeof(int16_t), pColVal, sizeof(SColVal));
×
268
          if (code != 0) {
×
UNCOV
269
            return code;
×
270
          }
271
        }
272
      }
273
    }
274
  } else {
275
    pStmt->bInfo.boundCols = NULL;
2,683✔
276
  }
277
  pStmt->bInfo.boundTags = tags;
2,605✔
278
  pStmt->bInfo.tagsCached = false;
2,646✔
279
  pStmt->bInfo.tbNameFlag = tbNameFlag;
2,683✔
280
  tstrncpy(pStmt->bInfo.stbFName, sTableName, sizeof(pStmt->bInfo.stbFName));
2,683✔
281

282
  if (pTableMeta->tableType != TSDB_CHILD_TABLE && pTableMeta->tableType != TSDB_SUPER_TABLE) {
2,642✔
UNCOV
283
    pStmt->sql.stbInterlaceMode = false;
×
284
  }
285

286
  return TSDB_CODE_SUCCESS;
2,683✔
287
}
288

289
static int32_t stmtUpdateExecInfo(TAOS_STMT2* stmt, SHashObj* pVgHash, SHashObj* pBlockHash) {
2,683✔
290
  STscStmt2* pStmt = (STscStmt2*)stmt;
2,683✔
291

292
  pStmt->sql.pVgHash = pVgHash;
2,683✔
293
  pStmt->exec.pBlockHash = pBlockHash;
2,683✔
294

295
  return TSDB_CODE_SUCCESS;
2,646✔
296
}
297

298
static int32_t stmtUpdateInfo(TAOS_STMT2* stmt, STableMeta* pTableMeta, void* tags, SArray* cols, SName* tbName,
2,683✔
299
                              bool autoCreateTbl, SHashObj* pVgHash, SHashObj* pBlockHash, const char* sTableName,
300
                              uint8_t tbNameFlag) {
301
  STscStmt2* pStmt = (STscStmt2*)stmt;
2,683✔
302

303
  STMT_ERR_RET(stmtUpdateBindInfo(stmt, pTableMeta, tags, cols, tbName, sTableName, autoCreateTbl, tbNameFlag));
2,683✔
304
  STMT_ERR_RET(stmtUpdateExecInfo(stmt, pVgHash, pBlockHash));
2,605✔
305

306
  pStmt->sql.autoCreateTbl = autoCreateTbl;
2,642✔
307

308
  return TSDB_CODE_SUCCESS;
2,646✔
309
}
310

311
static int32_t stmtGetExecInfo(TAOS_STMT2* stmt, SHashObj** pVgHash, SHashObj** pBlockHash) {
18✔
312
  STscStmt2* pStmt = (STscStmt2*)stmt;
18✔
313

314
  *pVgHash = pStmt->sql.pVgHash;
18✔
315
  pStmt->sql.pVgHash = NULL;
18✔
316

317
  *pBlockHash = pStmt->exec.pBlockHash;
18✔
318
  pStmt->exec.pBlockHash = NULL;
18✔
319

320
  return TSDB_CODE_SUCCESS;
18✔
321
}
322

323
static int32_t stmtParseSql(STscStmt2* pStmt) {
2,691✔
324
  pStmt->exec.pCurrBlock = NULL;
2,691✔
325

326
  SStmtCallback stmtCb = {
2,691✔
327
      .pStmt = pStmt,
328
      .getTbNameFn = stmtGetTbName,
329
      .setInfoFn = stmtUpdateInfo,
330
      .getExecInfoFn = stmtGetExecInfo,
331
  };
332

333
  STMT_ERR_RET(stmtCreateRequest(pStmt));
2,691✔
334
  pStmt->exec.pRequest->stmtBindVersion = 2;
2,691✔
335

336
  pStmt->stat.parseSqlNum++;
2,691✔
337

338
  STMT2_DLOG("start to parse, QID:0x%" PRIx64, pStmt->exec.pRequest->requestId);
2,691✔
339
  STMT_ERR_RET(parseSql(pStmt->exec.pRequest, false, &pStmt->sql.pQuery, &stmtCb));
2,691✔
340

341
  pStmt->sql.siInfo.pQuery = pStmt->sql.pQuery;
2,691✔
342

343
  pStmt->bInfo.needParse = false;
2,691✔
344

345
  if (pStmt->sql.type == 0) {
2,691✔
346
    if (pStmt->sql.pQuery->pRoot && LEGAL_INSERT(nodeType(pStmt->sql.pQuery->pRoot))) {
8✔
347
      pStmt->sql.type = STMT_TYPE_INSERT;
×
UNCOV
348
      pStmt->sql.stbInterlaceMode = false;
×
349
    } else if (pStmt->sql.pQuery->pPrepareRoot && LEGAL_SELECT(nodeType(pStmt->sql.pQuery->pPrepareRoot))) {
8✔
350
      pStmt->sql.type = STMT_TYPE_QUERY;
8✔
351
      pStmt->sql.stbInterlaceMode = false;
8✔
352

353
      return TSDB_CODE_SUCCESS;
8✔
354
    } else {
355
      STMT2_ELOG_E("only support select or insert sql");
×
356
      if (pStmt->exec.pRequest->msgBuf) {
×
UNCOV
357
        tstrncpy(pStmt->exec.pRequest->msgBuf, "stmt only support select or insert", pStmt->exec.pRequest->msgBufLen);
×
358
      }
UNCOV
359
      return TSDB_CODE_PAR_SYNTAX_ERROR;
×
360
    }
361
  } else if (pStmt->sql.type == STMT_TYPE_QUERY) {
2,683✔
362
    pStmt->sql.stbInterlaceMode = false;
×
UNCOV
363
    return TSDB_CODE_SUCCESS;
×
364
  } else if (pStmt->sql.type == STMT_TYPE_INSERT) {
2,683✔
UNCOV
365
    pStmt->sql.stbInterlaceMode = false;
×
366
  }
367

368
  STableDataCxt** pSrc =
369
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
2,683✔
370
  if (NULL == pSrc || NULL == *pSrc) {
2,683✔
371
    STMT2_ELOG("fail to get exec.pBlockHash, maybe parse failed, tbFName:%s", pStmt->bInfo.tbFName);
×
UNCOV
372
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
373
  }
374

375
  STableDataCxt* pTableCtx = *pSrc;
2,683✔
376
  if (pStmt->sql.stbInterlaceMode && pTableCtx->pData->pCreateTbReq && (pStmt->bInfo.tbNameFlag & USING_CLAUSE) == 0) {
2,683✔
377
    STMT2_TLOG("destroy pCreateTbReq for no-using insert, tbFName:%s", pStmt->bInfo.tbFName);
×
378
    tdDestroySVCreateTbReq(pTableCtx->pData->pCreateTbReq);
×
379
    taosMemoryFreeClear(pTableCtx->pData->pCreateTbReq);
×
UNCOV
380
    pTableCtx->pData->pCreateTbReq = NULL;
×
381
  }
382
  // if (pStmt->sql.stbInterlaceMode) {
383
  //   int16_t lastIdx = -1;
384

385
  //   for (int32_t i = 0; i < pTableCtx->boundColsInfo.numOfBound; ++i) {
386
  //     if (pTableCtx->boundColsInfo.pColIndex[i] < lastIdx) {
387
  //       pStmt->sql.stbInterlaceMode = false;
388
  //       break;
389
  //     }
390

391
  //     lastIdx = pTableCtx->boundColsInfo.pColIndex[i];
392
  //   }
393
  // }
394

395
  if (NULL == pStmt->sql.pBindInfo) {
2,683✔
396
    pStmt->sql.pBindInfo = taosMemoryMalloc(pTableCtx->boundColsInfo.numOfBound * sizeof(*pStmt->sql.pBindInfo));
2,665✔
397
    if (NULL == pStmt->sql.pBindInfo) {
2,665✔
398
      STMT2_ELOG_E("fail to malloc pBindInfo");
×
UNCOV
399
      return terrno;
×
400
    }
401
  }
402

403
  return TSDB_CODE_SUCCESS;
2,683✔
404
}
405

406
static int32_t stmtPrintBindv(TAOS_STMT2* stmt, TAOS_STMT2_BIND* bindv, int32_t col_idx, bool isTags) {
×
407
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
408
  int32_t    count = 0;
×
UNCOV
409
  int32_t    code = 0;
×
410

411
  if (bindv == NULL) {
×
412
    STMT2_TLOG("bindv is NULL, col_idx:%d, isTags:%d", col_idx, isTags);
×
UNCOV
413
    return TSDB_CODE_SUCCESS;
×
414
  }
415

416
  if (col_idx >= 0) {
×
417
    count = 1;
×
UNCOV
418
    STMT2_TLOG("single col bind, col_idx:%d", col_idx);
×
419
  } else {
420
    if (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type ||
×
421
        (pStmt->sql.type == 0 && stmt2IsInsert(stmt))) {
×
422
      if (pStmt->sql.placeholderOfTags == 0 && pStmt->sql.placeholderOfCols == 0) {
×
423
        code = stmtGetStbColFields2(pStmt, NULL, NULL);
×
424
        if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
425
          return code;
×
426
        }
427
      }
428
      if (isTags) {
×
429
        count = pStmt->sql.placeholderOfTags;
×
UNCOV
430
        STMT2_TLOG("print tags bindv, cols:%d", count);
×
431
      } else {
432
        count = pStmt->sql.placeholderOfCols;
×
UNCOV
433
        STMT2_TLOG("print cols bindv, cols:%d", count);
×
434
      }
435
    } else if (STMT_TYPE_QUERY == pStmt->sql.type || (pStmt->sql.type == 0 && stmt2IsSelect(stmt))) {
×
436
      count = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
×
UNCOV
437
      STMT2_TLOG("print query bindv, cols:%d", count);
×
438
    }
439
  }
440

441
  if (code != TSDB_CODE_SUCCESS) {
×
442
    STMT2_ELOG("failed to get param count, code:%d", code);
×
UNCOV
443
    return code;
×
444
  }
445

446
  for (int i = 0; i < count; i++) {
×
447
    int32_t type = bindv[i].buffer_type;
×
448
    int32_t num = bindv[i].num;
×
UNCOV
449
    char*   current_buf = (char*)bindv[i].buffer;
×
450

451
    for (int j = 0; j < num; j++) {
×
452
      char    buf[256] = {0};
×
453
      int32_t len = 0;
×
UNCOV
454
      bool    isNull = (bindv[i].is_null && bindv[i].is_null[j]);
×
455

456
      if (IS_VAR_DATA_TYPE(type) && bindv[i].length) {
×
UNCOV
457
        len = bindv[i].length[j];
×
458
      } else {
UNCOV
459
        len = tDataTypes[type].bytes;
×
460
      }
461

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

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

520
      if (!isNull && current_buf) {
×
UNCOV
521
        current_buf += len;
×
522
      }
523
    }
524
  }
525

UNCOV
526
  return TSDB_CODE_SUCCESS;
×
527
}
528

529
static void resetRequest(STscStmt2* pStmt) {
2,787✔
530
  if (pStmt->exec.pRequest) {
2,787✔
531
    taos_free_result(pStmt->exec.pRequest);
2,673✔
532
    pStmt->exec.pRequest = NULL;
2,673✔
533
  }
534
  pStmt->asyncResultAvailable = false;
2,787✔
535
}
2,787✔
536

537
static int32_t stmtCleanBindInfo(STscStmt2* pStmt) {
477,513✔
538
  pStmt->bInfo.tbUid = 0;
477,513✔
539
  pStmt->bInfo.tbVgId = -1;
477,513✔
540
  pStmt->bInfo.tbType = 0;
477,474✔
541
  pStmt->bInfo.needParse = true;
477,474✔
542
  pStmt->bInfo.inExecCache = false;
477,513✔
543

544
  pStmt->bInfo.tbName[0] = 0;
477,513✔
545
  pStmt->bInfo.tbFName[0] = 0;
477,513✔
546
  if (!pStmt->bInfo.tagsCached) {
477,435✔
547
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
284,422✔
548
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
284,461✔
549
  }
550

551
  if (!pStmt->bInfo.boundColsCached) {
477,474✔
552
    tSimpleHashCleanup(pStmt->bInfo.boundCols);
5,172✔
553
    pStmt->bInfo.boundCols = NULL;
5,172✔
554
  }
555

556
  if (!pStmt->sql.autoCreateTbl) {
477,513✔
557
    pStmt->bInfo.stbFName[0] = 0;
284,361✔
558
    pStmt->bInfo.tbSuid = 0;
284,361✔
559
  }
560

561
  STMT2_TLOG("finish clean bind info, tagsCached:%d, autoCreateTbl:%d", pStmt->bInfo.tagsCached,
477,435✔
562
             pStmt->sql.autoCreateTbl);
563

564
  return TSDB_CODE_SUCCESS;
477,327✔
565
}
566

567
static void stmtFreeTableBlkList(STableColsData* pTb) {
×
568
  (void)qResetStmtColumns(pTb->aCol, true);
×
569
  taosArrayDestroy(pTb->aCol);
×
UNCOV
570
}
×
571

572
static void stmtResetQueueTableBuf(STableBufInfo* pTblBuf, SStmtQueue* pQueue) {
472,137✔
573
  pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, 0);
472,137✔
574
  if (NULL == pTblBuf->pCurBuff) {
472,192✔
575
    tscError("QInfo:%p, fail to get buffer from list", pTblBuf);
168✔
UNCOV
576
    return;
×
577
  }
578
  pTblBuf->buffIdx = 1;
472,065✔
579
  pTblBuf->buffOffset = sizeof(*pQueue->head);
472,065✔
580

581
  pQueue->head = pQueue->tail = pTblBuf->pCurBuff;
472,065✔
582
  pQueue->qRemainNum = 0;
472,024✔
583
  pQueue->head->next = NULL;
472,024✔
584
}
585

586
static int32_t stmtCleanExecInfo(STscStmt2* pStmt, bool keepTable, bool deepClean) {
474,812✔
587
  if (pStmt->sql.stbInterlaceMode) {
474,812✔
588
    if (deepClean) {
474,808✔
589
      taosHashCleanup(pStmt->exec.pBlockHash);
2,647✔
590
      pStmt->exec.pBlockHash = NULL;
2,647✔
591

592
      if (NULL != pStmt->exec.pCurrBlock) {
2,647✔
593
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->boundColsInfo.pColIndex);
2,647✔
594
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->pData);
2,647✔
595
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
2,647✔
596
        pStmt->exec.pCurrBlock = NULL;
2,647✔
597
      }
598
      if (STMT_TYPE_QUERY != pStmt->sql.type) {
2,647✔
599
        resetRequest(pStmt);
2,647✔
600
      }
601
    } else {
602
      pStmt->sql.siInfo.pTableColsIdx = 0;
472,161✔
603
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
472,161✔
604
      tSimpleHashClear(pStmt->sql.siInfo.pTableRowDataHash);
471,997✔
605
    }
606
    if (NULL != pStmt->exec.pRequest) {
474,976✔
607
      pStmt->exec.pRequest->body.resInfo.numOfRows = 0;
472,329✔
608
    }
609
  } else {
610
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
20✔
611
      resetRequest(pStmt);
12✔
612
    }
613

614
    size_t keyLen = 0;
52✔
615
    void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
52✔
616
    while (pIter) {
194✔
617
      STableDataCxt* pBlocks = *(STableDataCxt**)pIter;
142✔
618
      char*          key = taosHashGetKey(pIter, &keyLen);
142✔
619
      STableMeta*    pMeta = qGetTableMetaInDataBlock(pBlocks);
142✔
620

621
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
142✔
622
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
18✔
623
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
44✔
624

625
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
18✔
626
        continue;
18✔
627
      }
628

629
      qDestroyStmtDataBlock(pBlocks);
124✔
630
      STMT_ERR_RET(taosHashRemove(pStmt->exec.pBlockHash, key, keyLen));
124✔
631

632
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
124✔
633
    }
634

635
    if (keepTable) {
52✔
636
      STMT2_TLOG("finish clean exec info, stbInterlaceMode:%d, keepTable:%d, deepClean:%d", pStmt->sql.stbInterlaceMode,
26✔
637
                 keepTable, deepClean);
638
      return TSDB_CODE_SUCCESS;
26✔
639
    }
640

641
    taosHashCleanup(pStmt->exec.pBlockHash);
26✔
642
    pStmt->exec.pBlockHash = NULL;
26✔
643

644
    tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
26✔
645
    taosMemoryFreeClear(pStmt->exec.pCurrTbData);
26✔
646
  }
647

648
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
475,002✔
649
  STMT2_TLOG("finish clean exec info, stbInterlaceMode:%d, keepTable:%d, deepClean:%d", pStmt->sql.stbInterlaceMode,
474,744✔
650
             keepTable, deepClean);
651

652
  return TSDB_CODE_SUCCESS;
474,756✔
653
}
654

655
static void stmtFreeTbBuf(void* buf) {
2,647✔
656
  void* pBuf = *(void**)buf;
2,647✔
657
  taosMemoryFree(pBuf);
2,647✔
658
}
2,647✔
659

660
static void stmtFreeTbCols(void* buf) {
2,647,000✔
661
  SArray* pCols = *(SArray**)buf;
2,647,000✔
662
  taosArrayDestroy(pCols);
2,647,000✔
663
}
2,647,000✔
664

665
static int32_t stmtCleanSQLInfo(STscStmt2* pStmt) {
2,577✔
666
  STMT2_TLOG_E("start to free SQL info");
2,577✔
667

668
  taosMemoryFree(pStmt->sql.pBindInfo);
2,577✔
669
  taosMemoryFree(pStmt->sql.queryRes.fields);
2,577✔
670
  taosMemoryFree(pStmt->sql.queryRes.userFields);
2,577✔
671
  taosMemoryFree(pStmt->sql.sqlStr);
2,577✔
672
  qDestroyQuery(pStmt->sql.pQuery);
2,577✔
673
  taosArrayDestroy(pStmt->sql.nodeList);
2,577✔
674
  taosHashCleanup(pStmt->sql.pVgHash);
2,577✔
675
  pStmt->sql.pVgHash = NULL;
2,577✔
676
  if (pStmt->sql.fixValueTags) {
2,577✔
677
    tdDestroySVCreateTbReq(pStmt->sql.fixValueTbReq);
24✔
678
  }
679

680
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
2,577✔
681
  while (pIter) {
2,595✔
682
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
18✔
683

684
    qDestroyStmtDataBlock(pCache->pDataCtx);
18✔
685
    qDestroyBoundColInfo(pCache->boundTags);
18✔
686
    taosMemoryFreeClear(pCache->boundTags);
18✔
687

688
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
18✔
689
  }
690
  taosHashCleanup(pStmt->sql.pTableCache);
2,577✔
691
  pStmt->sql.pTableCache = NULL;
2,577✔
692

693
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
2,577✔
694
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
2,577✔
695

696
  taos_free_result(pStmt->sql.siInfo.pRequest);
2,577✔
697
  taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
2,577✔
698
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
2,577✔
699
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableRowDataHash);
2,577✔
700
  taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
2,577✔
701
  taosMemoryFree(pStmt->sql.siInfo.pTSchema);
2,577✔
702
  qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
2,577✔
703
  taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
2,577✔
704
  pStmt->sql.siInfo.pTableCols = NULL;
2,577✔
705

706
  (void)memset(&pStmt->sql, 0, sizeof(pStmt->sql));
2,577✔
707
  pStmt->sql.siInfo.tableColsReady = true;
2,577✔
708

709
  STMT2_TLOG_E("end to free SQL info");
2,577✔
710

711
  return TSDB_CODE_SUCCESS;
2,577✔
712
}
713

714
static int32_t stmtTryAddTableVgroupInfo(STscStmt2* pStmt, int32_t* vgId) {
580,698✔
715
  if (*vgId >= 0 && taosHashGet(pStmt->sql.pVgHash, (const char*)vgId, sizeof(*vgId))) {
580,698✔
UNCOV
716
    return TSDB_CODE_SUCCESS;
×
717
  }
718

719
  SVgroupInfo      vgInfo = {0};
580,698✔
720
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
580,698✔
721
                           .requestId = pStmt->exec.pRequest->requestId,
580,698✔
722
                           .requestObjRefId = pStmt->exec.pRequest->self,
580,698✔
723
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
580,698✔
724

725
  int32_t code = catalogGetTableHashVgroup(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &vgInfo);
580,794✔
726
  if (TSDB_CODE_SUCCESS != code) {
580,794✔
727
    STMT2_ELOG("fail to get vgroup info from catalog, code:%d", code);
×
UNCOV
728
    return code;
×
729
  }
730

731
  code =
732
      taosHashPut(pStmt->sql.pVgHash, (const char*)&vgInfo.vgId, sizeof(vgInfo.vgId), (char*)&vgInfo, sizeof(vgInfo));
580,794✔
733
  if (TSDB_CODE_SUCCESS != code) {
580,854✔
734
    STMT2_ELOG("fail to put vgroup info, code:%d", code);
96✔
UNCOV
735
    return code;
×
736
  }
737

738
  *vgId = vgInfo.vgId;
580,758✔
739

740
  return TSDB_CODE_SUCCESS;
580,758✔
741
}
742

743
int32_t stmtGetTableMetaAndValidate(STscStmt2* pStmt, uint64_t* uid, uint64_t* suid, int32_t* vgId, int8_t* tableType) {
2,483✔
744
  STableMeta*      pTableMeta = NULL;
2,483✔
745
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
2,483✔
746
                           .requestId = pStmt->exec.pRequest->requestId,
2,483✔
747
                           .requestObjRefId = pStmt->exec.pRequest->self,
2,483✔
748
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
2,483✔
749
  int32_t          code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
2,483✔
750

751
  pStmt->stat.ctgGetTbMetaNum++;
2,483✔
752

753
  if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
2,483✔
754
    STMT2_ELOG("tb %s not exist", pStmt->bInfo.tbFName);
×
UNCOV
755
    (void)stmtCleanBindInfo(pStmt);
×
756

757
    if (!pStmt->sql.autoCreateTbl) {
×
758
      STMT2_ELOG("table %s does not exist and autoCreateTbl is disabled", pStmt->bInfo.tbFName);
×
UNCOV
759
      STMT_ERR_RET(TSDB_CODE_PAR_TABLE_NOT_EXIST);
×
760
    }
761

UNCOV
762
    STMT_ERR_RET(code);
×
763
  }
764

765
  STMT_ERR_RET(code);
2,483✔
766

767
  *uid = pTableMeta->uid;
2,483✔
768
  *suid = pTableMeta->suid;
2,483✔
769
  *tableType = pTableMeta->tableType;
2,483✔
770
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
2,483✔
771
  *vgId = pTableMeta->vgId;
2,483✔
772

773
  taosMemoryFree(pTableMeta);
2,483✔
774

775
  return TSDB_CODE_SUCCESS;
2,483✔
776
}
777

778
static int32_t stmtRebuildDataBlock(STscStmt2* pStmt, STableDataCxt* pDataBlock, STableDataCxt** newBlock, uint64_t uid,
106✔
779
                                    uint64_t suid, int32_t vgId) {
780
  STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
106✔
781
  STMT_ERR_RET(qRebuildStmtDataBlock(newBlock, pDataBlock, uid, suid, vgId, pStmt->sql.autoCreateTbl));
106✔
782

783
  STMT2_DLOG("uid:%" PRId64 ", rebuild table data context, vgId:%d", uid, vgId);
106✔
784

785
  return TSDB_CODE_SUCCESS;
106✔
786
}
787

788
static int32_t stmtGetFromCache(STscStmt2* pStmt) {
2,771✔
789
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx) {
2,771✔
790
    pStmt->bInfo.needParse = false;
×
791
    pStmt->bInfo.inExecCache = false;
×
UNCOV
792
    return TSDB_CODE_SUCCESS;
×
793
  }
794

795
  pStmt->bInfo.needParse = true;
2,808✔
796
  pStmt->bInfo.inExecCache = false;
2,771✔
797

798
  STableDataCxt** pCxtInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
2,734✔
799
  if (pCxtInExec) {
2,771✔
800
    pStmt->bInfo.needParse = false;
×
UNCOV
801
    pStmt->bInfo.inExecCache = true;
×
802

UNCOV
803
    pStmt->exec.pCurrBlock = *pCxtInExec;
×
804

805
    if (pStmt->sql.autoCreateTbl) {
×
806
      STMT2_DLOG("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
×
UNCOV
807
      return TSDB_CODE_SUCCESS;
×
808
    }
809
  }
810

811
  if (NULL == pStmt->pCatalog) {
2,771✔
812
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
2,569✔
813
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
2,569✔
814
  }
815

816
  if (NULL == pStmt->sql.pTableCache || taosHashGetSize(pStmt->sql.pTableCache) <= 0) {
2,734✔
817
    if (pStmt->bInfo.inExecCache) {
2,628✔
818
      pStmt->bInfo.needParse = false;
×
819
      STMT2_DLOG("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
×
UNCOV
820
      return TSDB_CODE_SUCCESS;
×
821
    }
822

823
    STMT2_DLOG("no stmt block cache for tb %s", pStmt->bInfo.tbFName);
2,628✔
824

825
    return TSDB_CODE_SUCCESS;
2,665✔
826
  }
827

828
  if (pStmt->sql.autoCreateTbl) {
106✔
829
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &pStmt->bInfo.tbSuid, sizeof(pStmt->bInfo.tbSuid));
106✔
830
    if (pCache) {
106✔
831
      pStmt->bInfo.needParse = false;
106✔
832
      pStmt->bInfo.tbUid = 0;
106✔
833

834
      STableDataCxt* pNewBlock = NULL;
106✔
835
      STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, 0, pStmt->bInfo.tbSuid, -1));
106✔
836

837
      if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
106✔
838
                      POINTER_BYTES)) {
UNCOV
839
        STMT_ERR_RET(terrno);
×
840
      }
841

842
      pStmt->exec.pCurrBlock = pNewBlock;
106✔
843

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

846
      return TSDB_CODE_SUCCESS;
106✔
847
    }
848

UNCOV
849
    STMT_RET(stmtCleanBindInfo(pStmt));
×
850
  }
851

852
  uint64_t uid, suid;
×
853
  int32_t  vgId;
×
UNCOV
854
  int8_t   tableType;
×
855

UNCOV
856
  STMT_ERR_RET(stmtGetTableMetaAndValidate(pStmt, &uid, &suid, &vgId, &tableType));
×
857

UNCOV
858
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
×
859

860
  if (uid == pStmt->bInfo.tbUid) {
×
UNCOV
861
    pStmt->bInfo.needParse = false;
×
862

UNCOV
863
    STMT2_DLOG("tb %s is current table", pStmt->bInfo.tbFName);
×
864

UNCOV
865
    return TSDB_CODE_SUCCESS;
×
866
  }
867

868
  if (pStmt->bInfo.inExecCache) {
×
869
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
×
870
    if (NULL == pCache) {
×
UNCOV
871
      STMT2_ELOG("table [%s, %" PRIx64 ", %" PRIx64 "] found in exec blockHash, but not in sql blockHash",
×
872
                 pStmt->bInfo.tbFName, uid, cacheUid);
873

UNCOV
874
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
875
    }
876

UNCOV
877
    pStmt->bInfo.needParse = false;
×
878

879
    pStmt->bInfo.tbUid = uid;
×
880
    pStmt->bInfo.tbSuid = suid;
×
881
    pStmt->bInfo.tbType = tableType;
×
882
    pStmt->bInfo.boundTags = pCache->boundTags;
×
UNCOV
883
    pStmt->bInfo.tagsCached = true;
×
884

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

UNCOV
887
    return TSDB_CODE_SUCCESS;
×
888
  }
889

890
  SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
×
891
  if (pCache) {
×
UNCOV
892
    pStmt->bInfo.needParse = false;
×
893

894
    pStmt->bInfo.tbUid = uid;
×
895
    pStmt->bInfo.tbSuid = suid;
×
896
    pStmt->bInfo.tbType = tableType;
×
897
    pStmt->bInfo.boundTags = pCache->boundTags;
×
UNCOV
898
    pStmt->bInfo.tagsCached = true;
×
899

900
    STableDataCxt* pNewBlock = NULL;
×
UNCOV
901
    STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, uid, suid, vgId));
×
902

UNCOV
903
    if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
×
904
                    POINTER_BYTES)) {
UNCOV
905
      STMT_ERR_RET(terrno);
×
906
    }
907

UNCOV
908
    pStmt->exec.pCurrBlock = pNewBlock;
×
909

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

UNCOV
912
    return TSDB_CODE_SUCCESS;
×
913
  }
914

UNCOV
915
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
916

UNCOV
917
  return TSDB_CODE_SUCCESS;
×
918
}
919

920
static int32_t stmtResetStmt(STscStmt2* pStmt) {
×
UNCOV
921
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
×
922

923
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
×
924
  if (NULL == pStmt->sql.pTableCache) {
×
925
    STMT2_ELOG("fail to allocate memory for pTableCache in stmtResetStmt:%s", tstrerror(terrno));
×
UNCOV
926
    STMT_ERR_RET(terrno);
×
927
  }
928

UNCOV
929
  pStmt->sql.status = STMT_INIT;
×
930

UNCOV
931
  return TSDB_CODE_SUCCESS;
×
932
}
933

934
static void stmtAsyncOutput(STscStmt2* pStmt, void* param) {
1,453,341✔
935
  SStmtQNode* pParam = (SStmtQNode*)param;
1,453,341✔
936

937
  if (pParam->restoreTbCols) {
1,453,341✔
938
    for (int32_t i = 0; i < pStmt->sql.siInfo.pTableColsIdx; ++i) {
1,453,410✔
939
      SArray** p = (SArray**)TARRAY_GET_ELEM(pStmt->sql.siInfo.pTableCols, i);
981,048✔
940
      *p = taosArrayInit(20, POINTER_BYTES);
981,051✔
941
      if (*p == NULL) {
981,042✔
942
        pStmt->errCode = terrno;
12✔
943
      }
944
    }
945
    atomic_store_8((int8_t*)&pStmt->sql.siInfo.tableColsReady, true);
472,281✔
946
    STMT2_TLOG_E("restore pTableCols finished");
472,290✔
947
  } else {
948
    int code = qAppendStmt2TableOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, &pParam->tblData, pStmt->exec.pCurrBlock,
981,039✔
949
                                       &pStmt->sql.siInfo, pParam->pCreateTbReq);
950
    // taosMemoryFree(pParam->pTbData);
951
    if (code != TSDB_CODE_SUCCESS) {
981,117✔
952
      STMT2_ELOG("async append stmt output failed, tbname:%s, err:%s", pParam->tblData.tbName, tstrerror(code));
×
UNCOV
953
      pStmt->errCode = code;
×
954
    }
955
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
981,117✔
956
  }
957
}
1,453,431✔
958

959
static void* stmtBindThreadFunc(void* param) {
2,647✔
960
  setThreadName("stmt2Bind");
2,647✔
961

962
  STscStmt2* pStmt = (STscStmt2*)param;
2,647✔
963
  STMT2_DLOG_E("stmt2 bind thread started");
2,647✔
964

965
  while (true) {
1,453,404✔
966
    SStmtQNode* asyncParam = NULL;
1,456,051✔
967

968
    if (!stmtDequeue(pStmt, &asyncParam)) {
1,456,051✔
969
      if (pStmt->queue.stopQueue && 0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
2,647✔
970
        STMT2_DLOG_E("queue is empty and stopQueue is set, thread will exit");
2,647✔
971
        break;
2,647✔
972
      }
UNCOV
973
      continue;
×
974
    }
975

976
    stmtAsyncOutput(pStmt, asyncParam);
1,453,404✔
977
  }
978

979
  STMT2_DLOG_E("stmt2 bind thread stopped");
2,647✔
980
  return NULL;
2,647✔
981
}
982

983
static int32_t stmtStartBindThread(STscStmt2* pStmt) {
2,647✔
984
  TdThreadAttr thAttr;
2,423✔
985
  if (taosThreadAttrInit(&thAttr) != 0) {
2,647✔
UNCOV
986
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
987
  }
988
  if (taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE) != 0) {
2,647✔
UNCOV
989
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
990
  }
991

992
  if (taosThreadCreate(&pStmt->bindThread, &thAttr, stmtBindThreadFunc, pStmt) != 0) {
2,647✔
993
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
UNCOV
994
    STMT_ERR_RET(terrno);
×
995
  }
996

997
  pStmt->bindThreadInUse = true;
2,647✔
998

999
  (void)taosThreadAttrDestroy(&thAttr);
2,647✔
1000
  return TSDB_CODE_SUCCESS;
2,647✔
1001
}
1002

1003
static int32_t stmtInitQueue(STscStmt2* pStmt) {
2,647✔
1004
  (void)taosThreadCondInit(&pStmt->queue.waitCond, NULL);
2,647✔
1005
  (void)taosThreadMutexInit(&pStmt->queue.mutex, NULL);
2,647✔
1006
  STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&pStmt->queue.head));
5,294✔
1007
  pStmt->queue.tail = pStmt->queue.head;
2,647✔
1008

1009
  return TSDB_CODE_SUCCESS;
2,647✔
1010
}
1011

1012
static int32_t stmtIniAsyncBind(STscStmt2* pStmt) {
2,577✔
1013
  (void)taosThreadCondInit(&pStmt->asyncBindParam.waitCond, NULL);
2,577✔
1014
  (void)taosThreadMutexInit(&pStmt->asyncBindParam.mutex, NULL);
2,577✔
1015
  pStmt->asyncBindParam.asyncBindNum = 0;
2,577✔
1016

1017
  return TSDB_CODE_SUCCESS;
2,577✔
1018
}
1019

1020
static int32_t stmtInitTableBuf(STableBufInfo* pTblBuf) {
2,647✔
1021
  pTblBuf->buffUnit = sizeof(SStmtQNode);
2,647✔
1022
  pTblBuf->buffSize = pTblBuf->buffUnit * 1000;
2,647✔
1023
  pTblBuf->pBufList = taosArrayInit(100, POINTER_BYTES);
2,647✔
1024
  if (NULL == pTblBuf->pBufList) {
2,647✔
UNCOV
1025
    return terrno;
×
1026
  }
1027
  void* buff = taosMemoryMalloc(pTblBuf->buffSize);
2,647✔
1028
  if (NULL == buff) {
2,647✔
UNCOV
1029
    return terrno;
×
1030
  }
1031

1032
  if (taosArrayPush(pTblBuf->pBufList, &buff) == NULL) {
5,294✔
UNCOV
1033
    return terrno;
×
1034
  }
1035

1036
  pTblBuf->pCurBuff = buff;
2,647✔
1037
  pTblBuf->buffIdx = 1;
2,647✔
1038
  pTblBuf->buffOffset = 0;
2,647✔
1039

1040
  return TSDB_CODE_SUCCESS;
2,647✔
1041
}
1042

1043
TAOS_STMT2* stmtInit2(STscObj* taos, TAOS_STMT2_OPTION* pOptions) {
2,577✔
1044
  STscObj*   pObj = (STscObj*)taos;
2,577✔
1045
  STscStmt2* pStmt = NULL;
2,577✔
1046
  int32_t    code = 0;
2,577✔
1047

1048
  pStmt = taosMemoryCalloc(1, sizeof(STscStmt2));
2,577✔
1049
  if (NULL == pStmt) {
2,577✔
1050
    STMT2_ELOG_E("fail to allocate memory for pStmt");
×
UNCOV
1051
    return NULL;
×
1052
  }
1053

1054
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
2,577✔
1055
  if (NULL == pStmt->sql.pTableCache) {
2,577✔
1056
    STMT2_ELOG("fail to allocate memory for pTableCache in stmtInit2:%s", tstrerror(terrno));
×
1057
    taosMemoryFree(pStmt);
×
UNCOV
1058
    return NULL;
×
1059
  }
1060

1061
  pStmt->taos = pObj;
2,577✔
1062
  if (taos->db[0] != '\0') {
2,577✔
1063
    pStmt->db = taosStrdup(taos->db);
2,577✔
1064
  }
1065
  pStmt->bInfo.needParse = true;
2,577✔
1066
  pStmt->sql.status = STMT_INIT;
2,577✔
1067
  pStmt->errCode = TSDB_CODE_SUCCESS;
2,577✔
1068

1069
  if (NULL != pOptions) {
2,577✔
1070
    (void)memcpy(&pStmt->options, pOptions, sizeof(pStmt->options));
2,559✔
1071
    if (pOptions->singleStbInsert && pOptions->singleTableBindOnce) {
2,559✔
1072
      pStmt->stbInterlaceMode = true;
2,551✔
1073
    }
1074

1075
    pStmt->reqid = pOptions->reqid;
2,559✔
1076
  }
1077

1078
  if (pStmt->stbInterlaceMode) {
2,577✔
1079
    pStmt->sql.siInfo.transport = taos->pAppInfo->pTransporter;
2,551✔
1080
    pStmt->sql.siInfo.acctId = taos->acctId;
2,551✔
1081
    pStmt->sql.siInfo.dbname = taos->db;
2,551✔
1082
    pStmt->sql.siInfo.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
2,551✔
1083

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

1091
    pStmt->sql.siInfo.pTableRowDataHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
2,551✔
1092
    if (NULL == pStmt->sql.siInfo.pTableRowDataHash) {
2,551✔
1093
      STMT2_ELOG("fail to allocate memory for pTableRowDataHash:%s", tstrerror(terrno));
×
1094
      (void)stmtClose2(pStmt);
×
UNCOV
1095
      return NULL;
×
1096
    }
1097

1098
    pStmt->sql.siInfo.pTableCols = taosArrayInit(STMT_TABLE_COLS_NUM, POINTER_BYTES);
2,551✔
1099
    if (NULL == pStmt->sql.siInfo.pTableCols) {
2,551✔
1100
      STMT2_ELOG("fail to allocate memory for pTableCols:%s", tstrerror(terrno));
×
1101
      (void)stmtClose2(pStmt);
×
UNCOV
1102
      return NULL;
×
1103
    }
1104

1105
    code = stmtInitTableBuf(&pStmt->sql.siInfo.tbBuf);
2,551✔
1106
    if (TSDB_CODE_SUCCESS == code) {
2,551✔
1107
      code = stmtInitQueue(pStmt);
2,551✔
1108
    }
1109
    if (TSDB_CODE_SUCCESS == code) {
2,551✔
1110
      code = stmtStartBindThread(pStmt);
2,551✔
1111
    }
1112
    if (TSDB_CODE_SUCCESS != code) {
2,551✔
1113
      terrno = code;
×
1114
      STMT2_ELOG("fail to init stmt2 bind thread:%s", tstrerror(code));
×
1115
      (void)stmtClose2(pStmt);
×
UNCOV
1116
      return NULL;
×
1117
    }
1118
  }
1119

1120
  pStmt->sql.siInfo.tableColsReady = true;
2,577✔
1121
  if (pStmt->options.asyncExecFn) {
2,577✔
1122
    if (tsem_init(&pStmt->asyncExecSem, 0, 1) != 0) {
×
1123
      terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
1124
      STMT2_ELOG("fail to init asyncExecSem:%s", tstrerror(terrno));
×
1125
      (void)stmtClose2(pStmt);
×
UNCOV
1126
      return NULL;
×
1127
    }
1128
  }
1129
  code = stmtIniAsyncBind(pStmt);
2,577✔
1130
  if (TSDB_CODE_SUCCESS != code) {
2,577✔
1131
    terrno = code;
×
UNCOV
1132
    STMT2_ELOG("fail to start init asyncExecSem:%s", tstrerror(code));
×
1133

1134
    (void)stmtClose2(pStmt);
×
UNCOV
1135
    return NULL;
×
1136
  }
1137

1138
  pStmt->execSemWaited = false;
2,577✔
1139

1140
  // STMT_LOG_SEQ(STMT_INIT);
1141

1142
  STMT2_DLOG("stmt2 initialize finished, seqId:%d, db:%s, interlaceMode:%d, asyncExec:%d", pStmt->seqId, pStmt->db,
2,577✔
1143
             pStmt->stbInterlaceMode, pStmt->options.asyncExecFn != NULL);
1144

1145
  return pStmt;
2,577✔
1146
}
1147

1148
static int stmtSetDbName2(TAOS_STMT2* stmt, const char* dbName) {
×
1149
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
1150
  if (dbName == NULL || dbName[0] == '\0') {
×
1151
    STMT2_ELOG_E("dbname in sql is illegal");
×
UNCOV
1152
    return TSDB_CODE_TSC_SQL_SYNTAX_ERROR;
×
1153
  }
1154

1155
  STMT2_DLOG("dbname is specified in sql:%s", dbName);
×
1156
  if (pStmt->db == NULL || pStmt->db[0] == '\0') {
×
1157
    taosMemoryFreeClear(pStmt->db);
×
1158
    STMT2_DLOG("dbname:%s is by sql, not by taosconnect", dbName);
×
1159
    pStmt->db = taosStrdup(dbName);
×
UNCOV
1160
    (void)strdequote(pStmt->db);
×
1161
  }
UNCOV
1162
  STMT_ERR_RET(stmtCreateRequest(pStmt));
×
1163

1164
  // The SQL statement specifies a database name, overriding the previously specified database
1165
  taosMemoryFreeClear(pStmt->exec.pRequest->pDb);
×
1166
  pStmt->exec.pRequest->pDb = taosStrdup(dbName);
×
1167
  (void)strdequote(pStmt->exec.pRequest->pDb);
×
1168
  if (pStmt->exec.pRequest->pDb == NULL) {
×
UNCOV
1169
    return terrno;
×
1170
  }
1171
  if (pStmt->sql.stbInterlaceMode) {
×
UNCOV
1172
    pStmt->sql.siInfo.dbname = pStmt->exec.pRequest->pDb;
×
1173
  }
UNCOV
1174
  return TSDB_CODE_SUCCESS;
×
1175
}
1176
static int32_t stmtResetStbInterlaceCache(STscStmt2* pStmt) {
96✔
1177
  int32_t code = TSDB_CODE_SUCCESS;
96✔
1178

1179
  pStmt->sql.siInfo.pTableHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
96✔
1180
  if (NULL == pStmt->sql.siInfo.pTableHash) {
96✔
UNCOV
1181
    return terrno;
×
1182
  }
1183

1184
  pStmt->sql.siInfo.pTableRowDataHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
96✔
1185
  if (NULL == pStmt->sql.siInfo.pTableRowDataHash) {
96✔
UNCOV
1186
    return terrno;
×
1187
  }
1188

1189
  pStmt->sql.siInfo.pTableCols = taosArrayInit(STMT_TABLE_COLS_NUM, POINTER_BYTES);
96✔
1190
  if (NULL == pStmt->sql.siInfo.pTableCols) {
96✔
UNCOV
1191
    return terrno;
×
1192
  }
1193

1194
  code = stmtInitTableBuf(&pStmt->sql.siInfo.tbBuf);
96✔
1195

1196
  if (TSDB_CODE_SUCCESS == code) {
96✔
1197
    code = stmtInitQueue(pStmt);
96✔
1198
    pStmt->queue.stopQueue = false;
96✔
1199
  }
1200
  if (TSDB_CODE_SUCCESS == code) {
96✔
1201
    code = stmtStartBindThread(pStmt);
96✔
1202
  }
1203
  if (TSDB_CODE_SUCCESS != code) {
96✔
UNCOV
1204
    return code;
×
1205
  }
1206

1207
  return TSDB_CODE_SUCCESS;
96✔
1208
}
1209

1210
static int32_t stmtDeepReset(STscStmt2* pStmt) {
96✔
1211
  char*             db = pStmt->db;
96✔
1212
  TAOS_STMT2_OPTION options = pStmt->options;
96✔
1213
  uint32_t          reqid = pStmt->reqid;
96✔
1214

1215
  pStmt->errCode = 0;
96✔
1216
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
96✔
1217
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
×
UNCOV
1218
      STMT2_ELOG_E("bind param wait asyncExecSem failed");
×
1219
    }
UNCOV
1220
    pStmt->execSemWaited = true;
×
1221
  }
1222

1223
  if (pStmt->stbInterlaceMode) {
96✔
1224
    if (pStmt->bindThreadInUse) {
96✔
1225
      while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
96✔
UNCOV
1226
        taosUsleep(1);
×
1227
      }
1228
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
96✔
1229
      pStmt->queue.stopQueue = true;
96✔
1230
      (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
96✔
1231
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
96✔
1232

1233
      (void)taosThreadJoin(pStmt->bindThread, NULL);
96✔
1234
      pStmt->bindThreadInUse = false;
96✔
1235
      pStmt->queue.head = NULL;
96✔
1236
      pStmt->queue.tail = NULL;
96✔
1237
      pStmt->queue.qRemainNum = 0;
96✔
1238

1239
      (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
96✔
1240
      (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
96✔
1241
    }
1242
  }
1243

1244
  pStmt->sql.autoCreateTbl = false;
96✔
1245
  taosMemoryFree(pStmt->sql.pBindInfo);
96✔
1246
  pStmt->sql.pBindInfo = NULL;
96✔
1247

1248
  taosMemoryFree(pStmt->sql.queryRes.fields);
96✔
1249
  pStmt->sql.queryRes.fields = NULL;
96✔
1250

1251
  taosMemoryFree(pStmt->sql.queryRes.userFields);
96✔
1252
  pStmt->sql.queryRes.userFields = NULL;
96✔
1253

1254
  pStmt->sql.type = 0;
96✔
1255
  pStmt->sql.runTimes = 0;
96✔
1256
  taosMemoryFree(pStmt->sql.sqlStr);
96✔
1257
  pStmt->sql.sqlStr = NULL;
96✔
1258

1259
  qDestroyQuery(pStmt->sql.pQuery);
96✔
1260
  pStmt->sql.pQuery = NULL;
96✔
1261

1262
  taosArrayDestroy(pStmt->sql.nodeList);
96✔
1263
  pStmt->sql.nodeList = NULL;
96✔
1264

1265
  taosHashCleanup(pStmt->sql.pVgHash);
96✔
1266
  pStmt->sql.pVgHash = NULL;
96✔
1267

1268
  if (pStmt->sql.fixValueTags) {
96✔
1269
    tdDestroySVCreateTbReq(pStmt->sql.fixValueTbReq);
96✔
1270
    pStmt->sql.fixValueTbReq = NULL;
96✔
1271
  }
1272
  pStmt->sql.fixValueTags = false;
96✔
1273

1274
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
96✔
1275
  while (pIter) {
96✔
UNCOV
1276
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
×
1277

1278
    qDestroyStmtDataBlock(pCache->pDataCtx);
×
1279
    qDestroyBoundColInfo(pCache->boundTags);
×
UNCOV
1280
    taosMemoryFreeClear(pCache->boundTags);
×
1281

UNCOV
1282
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
×
1283
  }
1284
  taosHashCleanup(pStmt->sql.pTableCache);
96✔
1285

1286
  if (pStmt->sql.stbInterlaceMode) {
96✔
1287
    pStmt->bInfo.tagsCached = false;
96✔
1288
  }
1289
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
96✔
1290

1291
  resetRequest(pStmt);
96✔
1292

1293
  if (pStmt->sql.siInfo.pTableCols) {
96✔
1294
    taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
96✔
1295
    pStmt->sql.siInfo.pTableCols = NULL;
96✔
1296
  }
1297

1298
  if (pStmt->sql.siInfo.tbBuf.pBufList) {
96✔
1299
    taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
96✔
1300
    pStmt->sql.siInfo.tbBuf.pBufList = NULL;
96✔
1301
  }
1302

1303
  if (pStmt->sql.siInfo.pTableHash) {
96✔
1304
    tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
96✔
1305
    pStmt->sql.siInfo.pTableHash = NULL;
96✔
1306
  }
1307

1308
  if (pStmt->sql.siInfo.pTableRowDataHash) {
96✔
1309
    tSimpleHashCleanup(pStmt->sql.siInfo.pTableRowDataHash);
96✔
1310
    pStmt->sql.siInfo.pTableRowDataHash = NULL;
96✔
1311
  }
1312

1313
  if (pStmt->sql.siInfo.pVgroupHash) {
96✔
1314
    taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
×
UNCOV
1315
    pStmt->sql.siInfo.pVgroupHash = NULL;
×
1316
  }
1317

1318
  if (pStmt->sql.siInfo.pVgroupList) {
96✔
1319
    taosArrayDestroy(pStmt->sql.siInfo.pVgroupList);
×
UNCOV
1320
    pStmt->sql.siInfo.pVgroupList = NULL;
×
1321
  }
1322

1323
  if (pStmt->sql.siInfo.pDataCtx) {
96✔
1324
    qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
96✔
1325
    pStmt->sql.siInfo.pDataCtx = NULL;
96✔
1326
  }
1327

1328
  if (pStmt->sql.siInfo.pTSchema) {
96✔
1329
    taosMemoryFree(pStmt->sql.siInfo.pTSchema);
96✔
1330
    pStmt->sql.siInfo.pTSchema = NULL;
96✔
1331
  }
1332

1333
  if (pStmt->sql.siInfo.pRequest) {
96✔
1334
    taos_free_result(pStmt->sql.siInfo.pRequest);
96✔
1335
    pStmt->sql.siInfo.pRequest = NULL;
96✔
1336
  }
1337

1338
  if (pStmt->stbInterlaceMode) {
96✔
1339
    STMT_ERR_RET(stmtResetStbInterlaceCache(pStmt));
96✔
1340
  }
1341

1342
  pStmt->db = db;
96✔
1343
  pStmt->options = options;
96✔
1344
  pStmt->reqid = reqid;
96✔
1345

1346
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
96✔
1347
  if (NULL == pStmt->sql.pTableCache) {
96✔
1348
    STMT2_ELOG("fail to allocate memory for pTableCache in stmtResetStmt:%s", tstrerror(terrno));
×
UNCOV
1349
    return terrno;
×
1350
  }
1351

1352
  pStmt->sql.status = STMT_INIT;
96✔
1353

1354
  return TSDB_CODE_SUCCESS;
96✔
1355
}
1356

1357
int stmtPrepare2(TAOS_STMT2* stmt, const char* sql, unsigned long length) {
2,673✔
1358
  STscStmt2* pStmt = (STscStmt2*)stmt;
2,673✔
1359
  int32_t    code = 0;
2,673✔
1360

1361
  STMT2_DLOG("start to prepare with sql:%s", sql);
2,673✔
1362

1363
  if (stmt == NULL || sql == NULL) {
2,673✔
1364
    STMT2_ELOG_E("stmt or sql is NULL");
×
UNCOV
1365
    return TSDB_CODE_INVALID_PARA;
×
1366
  }
1367

1368
  if (pStmt->sql.status >= STMT_PREPARE) {
2,673✔
1369
    STMT2_DLOG("stmt status is %d, need to reset stmt2 cache before prepare", pStmt->sql.status);
96✔
1370
    STMT_ERR_RET(stmtDeepReset(pStmt));
96✔
1371
  }
1372

1373
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
2,673✔
UNCOV
1374
    STMT2_ELOG("errCode is not success before, ErrCode: 0x%x, errorsyt: %s\n. ", pStmt->errCode,
×
1375
               tstrerror(pStmt->errCode));
UNCOV
1376
    return pStmt->errCode;
×
1377
  }
1378

1379
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
2,673✔
1380

1381
  if (length <= 0) {
2,673✔
1382
    length = strlen(sql);
8✔
1383
  }
1384
  pStmt->sql.sqlStr = taosStrndup(sql, length);
2,673✔
1385
  if (!pStmt->sql.sqlStr) {
2,673✔
1386
    STMT2_ELOG("fail to allocate memory for sqlStr:%s", tstrerror(terrno));
×
UNCOV
1387
    STMT_ERR_RET(terrno);
×
1388
  }
1389
  pStmt->sql.sqlLen = length;
2,673✔
1390
  STMT_ERR_RET(stmtCreateRequest(pStmt));
2,673✔
1391

1392
  if (stmt2IsInsert(pStmt)) {
2,673✔
1393
    pStmt->sql.stbInterlaceMode = pStmt->stbInterlaceMode;
2,665✔
1394
    char* dbName = NULL;
2,665✔
1395
    if (qParseDbName(sql, length, &dbName)) {
2,665✔
1396
      STMT_ERR_RET(stmtSetDbName2(stmt, dbName));
×
UNCOV
1397
      taosMemoryFreeClear(dbName);
×
1398
    } else if (pStmt->db != NULL && pStmt->db[0] != '\0') {
2,665✔
1399
      taosMemoryFreeClear(pStmt->exec.pRequest->pDb);
2,665✔
1400
      pStmt->exec.pRequest->pDb = taosStrdup(pStmt->db);
2,665✔
1401
      if (pStmt->exec.pRequest->pDb == NULL) {
2,665✔
UNCOV
1402
        STMT_ERR_RET(terrno);
×
1403
      }
1404
      (void)strdequote(pStmt->exec.pRequest->pDb);
2,665✔
1405

1406
      if (pStmt->sql.stbInterlaceMode) {
2,665✔
1407
        pStmt->sql.siInfo.dbname = pStmt->exec.pRequest->pDb;
2,647✔
1408
      }
1409
    }
1410

1411
  } else if (stmt2IsSelect(pStmt)) {
8✔
1412
    pStmt->sql.stbInterlaceMode = false;
8✔
1413
    STMT_ERR_RET(stmtParseSql(pStmt));
8✔
1414
  } else {
UNCOV
1415
    return stmtBuildErrorMsgWithCode(pStmt, "stmt only support 'SELECT' or 'INSERT'", TSDB_CODE_PAR_SYNTAX_ERROR);
×
1416
  }
1417
  return TSDB_CODE_SUCCESS;
2,673✔
1418
}
1419

1420
static int32_t stmtInitStbInterlaceTableInfo(STscStmt2* pStmt) {
2,647✔
1421
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
2,647✔
1422
  if (!pSrc) {
2,647✔
UNCOV
1423
    return terrno;
×
1424
  }
1425
  STableDataCxt* pDst = NULL;
2,647✔
1426

1427
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
2,647✔
1428
  pStmt->sql.siInfo.pDataCtx = pDst;
2,647✔
1429

1430
  SArray* pTblCols = NULL;
2,647✔
1431
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
2,639,722✔
1432
    pTblCols = taosArrayInit(20, POINTER_BYTES);
2,637,075✔
1433
    if (NULL == pTblCols) {
2,633,637✔
UNCOV
1434
      return terrno;
×
1435
    }
1436

1437
    if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
5,271,024✔
UNCOV
1438
      return terrno;
×
1439
    }
1440
  }
1441

1442
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
2,647✔
1443

1444
  STMT2_TLOG("init stb interlace table info, tbName:%s, pDataCtx:%p, boundTags:%p", pStmt->bInfo.tbFName,
2,647✔
1445
             pStmt->sql.siInfo.pDataCtx, pStmt->sql.siInfo.boundTags);
1446

1447
  return TSDB_CODE_SUCCESS;
2,647✔
1448
}
1449

1450
bool stmt2IsInsert(TAOS_STMT2* stmt) {
983,886✔
1451
  STscStmt2* pStmt = (STscStmt2*)stmt;
983,886✔
1452
  if (pStmt->sql.type) {
983,886✔
1453
    return (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
978,590✔
1454
  }
1455

1456
  return qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
5,296✔
1457
}
1458

1459
bool stmt2IsSelect(TAOS_STMT2* stmt) {
2,655✔
1460
  STscStmt2* pStmt = (STscStmt2*)stmt;
2,655✔
1461

1462
  if (pStmt->sql.type) {
2,655✔
UNCOV
1463
    return STMT_TYPE_QUERY == pStmt->sql.type;
×
1464
  }
1465
  return qIsSelectFromSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
2,655✔
1466
}
1467

1468
int stmtSetTbName2(TAOS_STMT2* stmt, const char* tbName) {
981,063✔
1469
  STscStmt2* pStmt = (STscStmt2*)stmt;
981,063✔
1470

1471
  int64_t startUs = taosGetTimestampUs();
980,838✔
1472

1473
  STMT2_TLOG("start to set tbName:%s", tbName);
980,838✔
1474

1475
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
980,922✔
UNCOV
1476
    return pStmt->errCode;
×
1477
  }
1478

1479
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
980,922✔
1480

1481
  int32_t insert = 0;
981,128✔
1482
  if (!stmt2IsInsert(stmt)) {
981,128✔
1483
    STMT2_ELOG_E("set tb name not available for no-insert statement");
×
UNCOV
1484
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1485
  }
1486
  // process tbname
1487
  STMT_ERR_RET(stmtCreateRequest(pStmt));
981,087✔
1488

1489
  STMT_ERR_RET(qCreateSName2(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
981,118✔
1490
                             pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1491
  STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
981,142✔
1492
  tstrncpy(pStmt->bInfo.tbName, (char*)tNameGetTableName(&pStmt->bInfo.sname), TSDB_TABLE_NAME_LEN);
980,977✔
1493

1494
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
980,906✔
1495
    STMT_ERR_RET(stmtGetFromCache(pStmt));
2,919✔
1496

1497
    if (pStmt->bInfo.needParse) {
2,771✔
1498
      STMT_ERR_RET(stmtParseSql(pStmt));
2,665✔
1499
      if (!pStmt->sql.autoCreateTbl) {
2,665✔
1500
        uint64_t uid, suid;
2,423✔
1501
        int32_t  vgId;
2,423✔
1502
        int8_t   tableType;
2,423✔
1503

1504
        int32_t code = stmtGetTableMetaAndValidate(pStmt, &uid, &suid, &vgId, &tableType);
2,483✔
1505
        if (code != TSDB_CODE_SUCCESS) {
2,483✔
UNCOV
1506
          return code;
×
1507
        }
1508
      }
1509
    }
1510

1511
  } else {
1512
    pStmt->exec.pRequest->requestId++;
978,108✔
1513
    pStmt->bInfo.needParse = false;
978,227✔
1514
  }
1515

1516
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
980,957✔
1517
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
2,647✔
1518
  }
1519

1520
  int64_t startUs2 = taosGetTimestampUs();
981,130✔
1521
  pStmt->stat.setTbNameUs += startUs2 - startUs;
981,130✔
1522

1523
  return TSDB_CODE_SUCCESS;
981,128✔
1524
}
1525

1526
int stmtSetTbTags2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* tags, SVCreateTbReq** pCreateTbReq) {
484,908✔
1527
  STscStmt2* pStmt = (STscStmt2*)stmt;
484,908✔
1528

1529
  STMT2_TLOG_E("start to set tbTags");
484,908✔
1530
  if (qDebugFlag & DEBUG_TRACE) {
484,908✔
UNCOV
1531
    (void)stmtPrintBindv(stmt, tags, -1, true);
×
1532
  }
1533

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

1538
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
484,932✔
1539

1540
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
484,848✔
1541
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
UNCOV
1542
    pStmt->bInfo.needParse = false;
×
1543
  }
1544
  STMT_ERR_RET(stmtCreateRequest(pStmt));
484,848✔
1545

1546
  if (pStmt->bInfo.needParse) {
484,848✔
UNCOV
1547
    STMT_ERR_RET(stmtParseSql(pStmt));
×
1548
  }
1549
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
484,848✔
UNCOV
1550
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1551
  }
1552

1553
  SBoundColInfo* tags_info = (SBoundColInfo*)pStmt->bInfo.boundTags;
484,872✔
1554

1555
  STableDataCxt** pDataBlock = NULL;
484,872✔
1556
  if (pStmt->exec.pCurrBlock) {
484,872✔
1557
    pDataBlock = &pStmt->exec.pCurrBlock;
484,810✔
1558
  } else {
1559
    pDataBlock =
1560
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
62✔
1561
    if (NULL == pDataBlock) {
62✔
1562
      STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
UNCOV
1563
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1564
    }
1565
  }
1566
  if (pStmt->bInfo.inExecCache && !pStmt->sql.autoCreateTbl) {
484,872✔
UNCOV
1567
    return TSDB_CODE_SUCCESS;
×
1568
  }
1569

1570
  STMT2_TLOG_E("start to bind stmt tag values");
484,872✔
1571

1572
  void* boundTags = NULL;
484,812✔
1573
  if (pStmt->sql.stbInterlaceMode) {
484,812✔
1574
    boundTags = pStmt->sql.siInfo.boundTags;
484,688✔
1575
    *pCreateTbReq = taosMemoryCalloc(1, sizeof(SVCreateTbReq));
484,688✔
1576
    if (NULL == pCreateTbReq) {
484,724✔
UNCOV
1577
      return terrno;
×
1578
    }
1579
    int32_t vgId = -1;
484,724✔
1580
    STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
484,724✔
1581
    (*pCreateTbReq)->uid = vgId;
484,856✔
1582
  } else {
1583
    boundTags = pStmt->bInfo.boundTags;
124✔
1584
  }
1585

1586
  STMT_ERR_RET(qBindStmtTagsValue2(*pDataBlock, boundTags, pStmt->bInfo.tbSuid, pStmt->bInfo.stbFName,
484,980✔
1587
                                   pStmt->bInfo.sname.tname, tags, pStmt->exec.pRequest->msgBuf,
1588
                                   pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt, *pCreateTbReq));
1589

1590
  return TSDB_CODE_SUCCESS;
484,968✔
1591
}
1592

1593
int stmtCheckTags2(TAOS_STMT2* stmt, SVCreateTbReq** pCreateTbReq) {
95,940✔
1594
  STscStmt2* pStmt = (STscStmt2*)stmt;
95,940✔
1595

1596
  STMT2_TLOG_E("start to clone createTbRequest for fixed tags");
95,940✔
1597

1598
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
95,964✔
UNCOV
1599
    return pStmt->errCode;
×
1600
  }
1601

1602
  if (!pStmt->sql.stbInterlaceMode) {
95,964✔
UNCOV
1603
    return TSDB_CODE_SUCCESS;
×
1604
  }
1605

1606
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
95,964✔
1607

1608
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
95,928✔
1609
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
UNCOV
1610
    pStmt->bInfo.needParse = false;
×
1611
  }
1612
  STMT_ERR_RET(stmtCreateRequest(pStmt));
95,928✔
1613

1614
  if (pStmt->bInfo.needParse) {
95,928✔
1615
    STMT_ERR_RET(stmtParseSql(pStmt));
×
1616
    if (!pStmt->sql.autoCreateTbl) {
×
1617
      STMT2_WLOG_E("don't need to create table, will not check tags");
×
UNCOV
1618
      return TSDB_CODE_SUCCESS;
×
1619
    }
1620
  }
1621

1622
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
95,928✔
UNCOV
1623
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1624
  }
1625

1626
  STableDataCxt** pDataBlock = NULL;
95,940✔
1627
  if (pStmt->exec.pCurrBlock) {
95,940✔
1628
    pDataBlock = &pStmt->exec.pCurrBlock;
95,820✔
1629
  } else {
1630
    pDataBlock =
1631
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
120✔
1632
    if (NULL == pDataBlock) {
120✔
UNCOV
1633
      STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
1634
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
60✔
1635
    }
1636
  }
1637

1638
  if (!((*pDataBlock)->pData->flags & SUBMIT_REQ_AUTO_CREATE_TABLE)) {
96,000✔
1639
    STMT2_DLOG_E("don't need to create, will not check tags");
×
UNCOV
1640
    return TSDB_CODE_SUCCESS;
×
1641
  }
1642

1643
  if (pStmt->sql.fixValueTags) {
96,000✔
1644
    STMT2_TLOG_E("tags are fixed, use one createTbReq");
95,856✔
1645
    STMT_ERR_RET(cloneSVreateTbReq(pStmt->sql.fixValueTbReq, pCreateTbReq));
95,856✔
1646
    if ((*pCreateTbReq)->name) {
95,844✔
1647
      taosMemoryFree((*pCreateTbReq)->name);
95,832✔
1648
    }
1649
    (*pCreateTbReq)->name = taosStrdup(pStmt->bInfo.tbName);
95,832✔
1650
    int32_t vgId = -1;
95,832✔
1651
    STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
95,832✔
1652
    (*pCreateTbReq)->uid = vgId;
95,856✔
1653
    return TSDB_CODE_SUCCESS;
95,856✔
1654
  }
1655

1656
  if ((*pDataBlock)->pData->pCreateTbReq) {
144✔
1657
    STMT2_TLOG_E("tags are fixed, set createTbReq first time");
120✔
1658
    pStmt->sql.fixValueTags = true;
120✔
1659
    STMT_ERR_RET(cloneSVreateTbReq((*pDataBlock)->pData->pCreateTbReq, &pStmt->sql.fixValueTbReq));
120✔
1660
    STMT_ERR_RET(cloneSVreateTbReq(pStmt->sql.fixValueTbReq, pCreateTbReq));
120✔
1661
    (*pCreateTbReq)->uid = (*pDataBlock)->pMeta->vgId;
120✔
1662
  }
1663

1664
  return TSDB_CODE_SUCCESS;
144✔
1665
}
1666

1667
static int stmtFetchColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
×
1668
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
UNCOV
1669
    return pStmt->errCode;
×
1670
  }
1671

1672
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1673
    tscError("invalid operation to get query column fileds");
×
UNCOV
1674
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1675
  }
1676

UNCOV
1677
  STableDataCxt** pDataBlock = NULL;
×
1678

1679
  if (pStmt->sql.stbInterlaceMode) {
×
UNCOV
1680
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
×
1681
  } else {
1682
    pDataBlock =
1683
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
×
1684
    if (NULL == pDataBlock) {
×
1685
      STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
UNCOV
1686
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1687
    }
1688
  }
1689

UNCOV
1690
  STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields));
×
1691

UNCOV
1692
  return TSDB_CODE_SUCCESS;
×
1693
}
1694

1695
static int stmtFetchStbColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_ALL** fields) {
18✔
1696
  int32_t code = 0;
18✔
1697
  int32_t preCode = pStmt->errCode;
18✔
1698

1699
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
18✔
UNCOV
1700
    return pStmt->errCode;
×
1701
  }
1702

1703
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
18✔
1704
    STMT2_ELOG_E("stmtFetchStbColFields2 only for insert statement");
×
UNCOV
1705
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1706
  }
1707

1708
  STableDataCxt** pDataBlock = NULL;
18✔
1709
  bool            cleanStb = false;
18✔
1710

1711
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx != NULL) {
18✔
UNCOV
1712
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
×
1713
  } else {
1714
    cleanStb = true;
18✔
1715
    pDataBlock =
1716
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
18✔
1717
  }
1718

1719
  if (NULL == pDataBlock || NULL == *pDataBlock) {
18✔
1720
    STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
UNCOV
1721
    STMT_ERRI_JRET(TSDB_CODE_APP_ERROR);
×
1722
  }
1723

1724
  pStmt->sql.placeholderOfTags = 0;
18✔
1725
  pStmt->sql.placeholderOfCols = 0;
18✔
1726
  int32_t totalNum = 0;
18✔
1727
  STMT_ERRI_JRET(qBuildStmtStbColFields(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.boundCols,
18✔
1728
                                        pStmt->bInfo.tbNameFlag, &totalNum, fields, &pStmt->sql.placeholderOfTags,
1729
                                        &pStmt->sql.placeholderOfCols));
1730

1731
  if (pStmt->bInfo.tbType == TSDB_SUPER_TABLE && cleanStb) {
18✔
1732
    taosMemoryFreeClear((*pDataBlock)->boundColsInfo.pColIndex);
18✔
1733
    qDestroyStmtDataBlock(*pDataBlock);
18✔
1734
    *pDataBlock = NULL;
18✔
1735
    if (taosHashRemove(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName)) != 0) {
18✔
1736
      STMT2_ELOG("fail to remove remove stb:%s exec blockHash", pStmt->bInfo.tbFName);
×
UNCOV
1737
      STMT_ERRI_JRET(TSDB_CODE_APP_ERROR);
×
1738
    }
1739
    pStmt->sql.autoCreateTbl = false;
18✔
1740
    pStmt->bInfo.tagsCached = false;
18✔
1741
    pStmt->bInfo.sname = (SName){0};
18✔
1742
    STMT_ERR_RET(stmtCleanBindInfo(pStmt));
18✔
1743
  }
1744

1745
  if (fieldNum != NULL) {
18✔
1746
    *fieldNum = totalNum;
18✔
1747
  }
1748

1749
  STMT2_DLOG("get insert fields totalNum:%d, tagsNum:%d, colsNum:%d", totalNum, pStmt->sql.placeholderOfTags,
18✔
1750
             pStmt->sql.placeholderOfCols);
1751

1752
_return:
18✔
1753

1754
  pStmt->errCode = preCode;
18✔
1755

1756
  return code;
18✔
1757
}
1758
/*
1759
SArray* stmtGetFreeCol(STscStmt2* pStmt, int32_t* idx) {
1760
  while (true) {
1761
    if (pStmt->exec.smInfo.pColIdx >= STMT_COL_BUF_SIZE) {
1762
      pStmt->exec.smInfo.pColIdx = 0;
1763
    }
1764

1765
    if ((pStmt->exec.smInfo.pColIdx + 1) == atomic_load_32(&pStmt->exec.smInfo.pColFreeIdx)) {
1766
      taosUsleep(1);
1767
      continue;
1768
    }
1769

1770
    *idx = pStmt->exec.smInfo.pColIdx;
1771
    return pStmt->exec.smInfo.pCols[pStmt->exec.smInfo.pColIdx++];
1772
  }
1773
}
1774
*/
1775
static int32_t stmtAppendTablePostHandle(STscStmt2* pStmt, SStmtQNode* param) {
981,018✔
1776
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
981,018✔
1777
    pStmt->sql.siInfo.pVgroupHash =
472,281✔
1778
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
472,281✔
1779
  }
1780
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
981,138✔
1781
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
472,269✔
1782
  }
1783

1784
  if (NULL == pStmt->sql.siInfo.pRequest) {
981,189✔
1785
    STMT_ERR_RET(buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false,
2,647✔
1786
                              (SRequestObj**)&pStmt->sql.siInfo.pRequest, pStmt->reqid));
1787

1788
    if (pStmt->reqid != 0) {
2,647✔
1789
      pStmt->reqid++;
8✔
1790
    }
1791
    pStmt->exec.pRequest->syncQuery = true;
2,647✔
1792

1793
    pStmt->sql.siInfo.requestId = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->requestId;
2,647✔
1794
    pStmt->sql.siInfo.requestSelf = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->self;
2,647✔
1795
  }
1796

1797
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
981,189✔
1798
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
18,229✔
1799
    pStmt->sql.siInfo.tbFromHash = true;
1,563✔
1800
  }
1801

1802
  if (0 == pStmt->sql.siInfo.firstName[0]) {
981,189✔
1803
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
2,551✔
1804
  }
1805

1806
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
981,189✔
1807
  param->next = NULL;
981,189✔
1808

1809
  (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
981,111✔
1810

1811
  if (pStmt->queue.stopQueue) {
981,150✔
1812
    STMT2_ELOG_E("bind thread already stopped, cannot enqueue");
×
UNCOV
1813
    return TSDB_CODE_TSC_STMT_API_ERROR;
×
1814
  }
1815
  stmtEnqueue(pStmt, param);
981,189✔
1816

1817
  return TSDB_CODE_SUCCESS;
981,237✔
1818
}
1819

1820
static FORCE_INLINE int32_t stmtGetTableColsFromCache(STscStmt2* pStmt, SArray** pTableCols) {
1821
  while (true) {
1822
    if (pStmt->sql.siInfo.pTableColsIdx < taosArrayGetSize(pStmt->sql.siInfo.pTableCols)) {
981,135✔
1823
      *pTableCols = (SArray*)taosArrayGetP(pStmt->sql.siInfo.pTableCols, pStmt->sql.siInfo.pTableColsIdx++);
981,093✔
1824
      break;
981,060✔
1825
    } else {
1826
      SArray* pTblCols = NULL;
×
1827
      for (int32_t i = 0; i < 100; i++) {
×
1828
        pTblCols = taosArrayInit(20, POINTER_BYTES);
×
1829
        if (NULL == pTblCols) {
×
UNCOV
1830
          return terrno;
×
1831
        }
1832

1833
        if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
×
UNCOV
1834
          return terrno;
×
1835
        }
1836
      }
1837
    }
1838
  }
1839

1840
  return TSDB_CODE_SUCCESS;
981,060✔
1841
}
1842

1843
static int32_t stmtCacheBlock(STscStmt2* pStmt) {
124✔
1844
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
124✔
UNCOV
1845
    return TSDB_CODE_SUCCESS;
×
1846
  }
1847

1848
  uint64_t uid = pStmt->bInfo.tbUid;
124✔
1849
  uint64_t cacheUid = (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) ? pStmt->bInfo.tbSuid : uid;
124✔
1850

1851
  if (taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid))) {
124✔
1852
    STMT2_TLOG("table %s already cached, no need to cache again", pStmt->bInfo.tbFName);
106✔
1853
    return TSDB_CODE_SUCCESS;
106✔
1854
  }
1855

1856
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
18✔
1857
  if (!pSrc) {
18✔
1858
    STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
UNCOV
1859
    return terrno;
×
1860
  }
1861
  STableDataCxt* pDst = NULL;
18✔
1862

1863
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
18✔
1864

1865
  SStmtTableCache cache = {
18✔
1866
      .pDataCtx = pDst,
1867
      .boundTags = pStmt->bInfo.boundTags,
18✔
1868
  };
1869

1870
  if (taosHashPut(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid), &cache, sizeof(cache))) {
18✔
1871
    STMT2_ELOG("fail to put table cache:%s", tstrerror(terrno));
×
UNCOV
1872
    return terrno;
×
1873
  }
1874

1875
  if (pStmt->sql.autoCreateTbl) {
18✔
1876
    pStmt->bInfo.tagsCached = true;
18✔
1877
  } else {
UNCOV
1878
    pStmt->bInfo.boundTags = NULL;
×
1879
  }
1880

1881
  return TSDB_CODE_SUCCESS;
18✔
1882
}
1883

1884
static int stmtAddBatch2(TAOS_STMT2* stmt) {
472,390✔
1885
  STscStmt2* pStmt = (STscStmt2*)stmt;
472,390✔
1886

1887
  int64_t startUs = taosGetTimestampUs();
472,465✔
1888

1889
  // STMT2_TLOG_E("start to add batch");
1890

1891
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
472,465✔
UNCOV
1892
    return pStmt->errCode;
×
1893
  }
1894

1895
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
472,465✔
1896

1897
  if (pStmt->sql.stbInterlaceMode) {
472,417✔
1898
    int64_t startUs2 = taosGetTimestampUs();
472,341✔
1899
    pStmt->stat.addBatchUs += startUs2 - startUs;
472,341✔
1900

1901
    pStmt->sql.siInfo.tableColsReady = false;
472,341✔
1902

1903
    SStmtQNode* param = NULL;
472,341✔
1904
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
944,670✔
1905
    param->restoreTbCols = true;
472,329✔
1906
    param->next = NULL;
472,329✔
1907

1908
    if (pStmt->sql.autoCreateTbl) {
472,329✔
1909
      pStmt->bInfo.tagsCached = true;
192,968✔
1910
    }
1911
    pStmt->bInfo.boundColsCached = true;
472,290✔
1912

1913
    if (pStmt->queue.stopQueue) {
472,329✔
1914
      STMT2_ELOG_E("stmt bind thread is stopped,cannot enqueue bind request");
×
UNCOV
1915
      return TSDB_CODE_TSC_STMT_API_ERROR;
×
1916
    }
1917

1918
    stmtEnqueue(pStmt, param);
472,290✔
1919

1920
    return TSDB_CODE_SUCCESS;
472,281✔
1921
  }
1922

1923
  STMT_ERR_RET(stmtCacheBlock(pStmt));
124✔
1924

1925
  return TSDB_CODE_SUCCESS;
124✔
1926
}
1927
/*
1928
static int32_t stmtBackupQueryFields(STscStmt2* pStmt) {
1929
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
1930
  pRes->numOfCols = pStmt->exec.pRequest->body.resInfo.numOfCols;
1931
  pRes->precision = pStmt->exec.pRequest->body.resInfo.precision;
1932

1933
  int32_t size = pRes->numOfCols * sizeof(TAOS_FIELD);
1934
  pRes->fields = taosMemoryMalloc(size);
1935
  pRes->userFields = taosMemoryMalloc(size);
1936
  if (NULL == pRes->fields || NULL == pRes->userFields) {
1937
    STMT_ERR_RET(terrno);
1938
  }
1939
  (void)memcpy(pRes->fields, pStmt->exec.pRequest->body.resInfo.fields, size);
1940
  (void)memcpy(pRes->userFields, pStmt->exec.pRequest->body.resInfo.userFields, size);
1941

1942
  return TSDB_CODE_SUCCESS;
1943
}
1944

1945
static int32_t stmtRestoreQueryFields(STscStmt2* pStmt) {
1946
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
1947
  int32_t            size = pRes->numOfCols * sizeof(TAOS_FIELD);
1948

1949
  pStmt->exec.pRequest->body.resInfo.numOfCols = pRes->numOfCols;
1950
  pStmt->exec.pRequest->body.resInfo.precision = pRes->precision;
1951

1952
  if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
1953
    pStmt->exec.pRequest->body.resInfo.fields = taosMemoryMalloc(size);
1954
    if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
1955
      STMT_ERR_RET(terrno);
1956
    }
1957
    (void)memcpy(pStmt->exec.pRequest->body.resInfo.fields, pRes->fields, size);
1958
  }
1959

1960
  if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
1961
    pStmt->exec.pRequest->body.resInfo.userFields = taosMemoryMalloc(size);
1962
    if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
1963
      STMT_ERR_RET(terrno);
1964
    }
1965
    (void)memcpy(pStmt->exec.pRequest->body.resInfo.userFields, pRes->userFields, size);
1966
  }
1967

1968
  return TSDB_CODE_SUCCESS;
1969
}
1970
*/
1971

1972
int stmtBindBatch2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* bind, int32_t colIdx, SVCreateTbReq* pCreateTbReq) {
981,041✔
1973
  STscStmt2* pStmt = (STscStmt2*)stmt;
981,041✔
1974
  int32_t    code = 0;
981,041✔
1975

1976
  int64_t startUs = taosGetTimestampUs();
981,140✔
1977

1978
  if (qDebugFlag & DEBUG_TRACE) {
981,140✔
UNCOV
1979
    (void)stmtPrintBindv(stmt, bind, colIdx, false);
×
1980
  }
1981

1982
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
981,080✔
UNCOV
1983
    return pStmt->errCode;
×
1984
  }
1985

1986
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
981,043✔
1987

1988
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
981,063✔
1989
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
UNCOV
1990
    pStmt->bInfo.needParse = false;
×
1991
  }
1992

1993
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
981,063✔
UNCOV
1994
    resetRequest(pStmt);
×
1995
  }
1996

1997
  STMT_ERR_RET(stmtCreateRequest(pStmt));
981,141✔
1998
  if (pStmt->bInfo.needParse) {
981,124✔
1999
    code = stmtParseSql(pStmt);
×
2000
    if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
2001
      goto cleanup_root;
×
2002
    }
2003
  }
2004

2005
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
981,046✔
2006
    code = qStmtBindParams2(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt);
8✔
2007
    if (code != TSDB_CODE_SUCCESS) {
8✔
UNCOV
2008
      goto cleanup_root;
×
2009
    }
2010
    SParseContext ctx = {.requestId = pStmt->exec.pRequest->requestId,
8✔
2011
                         .acctId = pStmt->taos->acctId,
8✔
2012
                         .db = pStmt->exec.pRequest->pDb,
8✔
2013
                         .topicQuery = false,
2014
                         .pSql = pStmt->sql.sqlStr,
8✔
2015
                         .sqlLen = pStmt->sql.sqlLen,
8✔
2016
                         .pMsg = pStmt->exec.pRequest->msgBuf,
8✔
2017
                         .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
2018
                         .pTransporter = pStmt->taos->pAppInfo->pTransporter,
8✔
2019
                         .pStmtCb = NULL,
2020
                         .pUser = pStmt->taos->user,
8✔
2021
                         .stmtBindVersion = pStmt->exec.pRequest->stmtBindVersion};
8✔
2022
    ctx.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
8✔
2023
    code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &ctx.pCatalog);
8✔
2024
    if (code != TSDB_CODE_SUCCESS) {
8✔
UNCOV
2025
      goto cleanup_root;
×
2026
    }
2027
    code = qStmtParseQuerySql(&ctx, pStmt->sql.pQuery);
8✔
2028
    if (code != TSDB_CODE_SUCCESS) {
8✔
UNCOV
2029
      goto cleanup_root;
×
2030
    }
2031

2032
    if (pStmt->sql.pQuery->haveResultSet) {
8✔
2033
      STMT_ERR_RET(setResSchemaInfo(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->pResSchema,
16✔
2034
                                    pStmt->sql.pQuery->numOfResCols, pStmt->sql.pQuery->pResExtSchema, true));
2035
      taosMemoryFreeClear(pStmt->sql.pQuery->pResSchema);
8✔
2036
      taosMemoryFreeClear(pStmt->sql.pQuery->pResExtSchema);
8✔
2037
      setResPrecision(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->precision);
8✔
2038
    }
2039

2040
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
8✔
2041
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
8✔
2042
    TSWAP(pStmt->exec.pRequest->targetTableList, pStmt->sql.pQuery->pTargetTableList);
8✔
2043

2044
    // if (STMT_TYPE_QUERY == pStmt->sql.queryRes) {
2045
    //   STMT_ERR_RET(stmtRestoreQueryFields(pStmt));
2046
    // }
2047

2048
    // STMT_ERR_RET(stmtBackupQueryFields(pStmt));
2049

2050
    return TSDB_CODE_SUCCESS;
8✔
2051

2052
  cleanup_root:
×
2053
    STMT2_ELOG("parse query statment unexpected failed code:%d, need to clean node", code);
×
2054
    if (pStmt->sql.pQuery && pStmt->sql.pQuery->pRoot) {
×
2055
      nodesDestroyNode(pStmt->sql.pQuery->pRoot);
×
UNCOV
2056
      pStmt->sql.pQuery->pRoot = NULL;
×
2057
    }
UNCOV
2058
    STMT_ERR_RET(code);
×
2059
  }
2060

2061
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
981,038✔
UNCOV
2062
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
2063
  }
2064

2065
  STableDataCxt** pDataBlock = NULL;
981,121✔
2066

2067
  if (pStmt->exec.pCurrBlock) {
981,121✔
2068
    pDataBlock = &pStmt->exec.pCurrBlock;
978,496✔
2069
  } else {
2070
    pDataBlock =
2071
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
2,665✔
2072
    if (NULL == pDataBlock) {
2,665✔
2073
      STMT2_ELOG("table %s not found in exec blockHash:%p", pStmt->bInfo.tbFName, pStmt->exec.pBlockHash);
×
UNCOV
2074
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
2075
    }
2076
    pStmt->exec.pCurrBlock = *pDataBlock;
2,665✔
2077
    if (pStmt->sql.stbInterlaceMode) {
2,665✔
2078
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
2,647✔
2079
      (*pDataBlock)->pData->aCol = NULL;
2,647✔
2080
    }
2081
    if (colIdx < -1) {
2,665✔
2082
      pStmt->sql.bindRowFormat = true;
×
2083
      taosArrayDestroy((*pDataBlock)->pData->aCol);
×
UNCOV
2084
      (*pDataBlock)->pData->aCol = taosArrayInit(20, POINTER_BYTES);
×
2085
    }
2086
  }
2087

2088
  int64_t startUs2 = taosGetTimestampUs();
981,074✔
2089
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
981,074✔
2090

2091
  SStmtQNode* param = NULL;
980,993✔
2092
  if (pStmt->sql.stbInterlaceMode) {
981,074✔
2093
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
1,962,103✔
2094
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
1,962,195✔
2095
    taosArrayClear(param->tblData.aCol);
981,060✔
2096

2097
    // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES);
2098

2099
    param->restoreTbCols = false;
980,362✔
2100
    param->tblData.isOrdered = true;
980,403✔
2101
    param->tblData.isDuplicateTs = false;
980,442✔
2102
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
980,442✔
2103

2104
    param->pCreateTbReq = pCreateTbReq;
980,402✔
2105
  }
2106

2107
  int64_t startUs3 = taosGetTimestampUs();
980,756✔
2108
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
980,756✔
2109

2110
  SArray*   pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
980,758✔
2111
  SBlobSet* pBlob = NULL;
980,678✔
2112
  if (colIdx < 0) {
980,719✔
2113
    if (pStmt->sql.stbInterlaceMode) {
981,024✔
2114
      (*pDataBlock)->pData->flags &= ~SUBMIT_REQ_COLUMN_DATA_FORMAT;
981,001✔
2115
      code = qBindStmtStbColsValue2(*pDataBlock, pCols, pStmt->bInfo.boundCols, bind, pStmt->exec.pRequest->msgBuf,
1,274,902✔
2116
                                    pStmt->exec.pRequest->msgBufLen, &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo,
981,081✔
2117
                                    pStmt->taos->optionInfo.charsetCxt, &pBlob);
981,081✔
2118
      param->tblData.isOrdered = (*pDataBlock)->ordered;
981,012✔
2119
      param->tblData.isDuplicateTs = (*pDataBlock)->duplicateTs;
981,129✔
2120
    } else {
2121
      if (colIdx == -1) {
84✔
2122
        if (pStmt->sql.bindRowFormat) {
124✔
2123
          STMT2_ELOG_E("can't mix bind row format and bind column format");
×
UNCOV
2124
          STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2125
        }
2126
        code = qBindStmtColsValue2(*pDataBlock, pCols, pStmt->bInfo.boundCols, bind, pStmt->exec.pRequest->msgBuf,
124✔
2127
                                   pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt);
124✔
2128
      } else {
2129
        code =
2130
            qBindStmt2RowValue(*pDataBlock, (*pDataBlock)->pData->aRowP, pStmt->bInfo.boundCols, bind,
×
2131
                               pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
×
UNCOV
2132
                               &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt);
×
2133
      }
2134
    }
2135

2136
    if (code) {
981,175✔
2137
      STMT2_ELOG("bind cols or rows failed, error:%s", tstrerror(code));
×
UNCOV
2138
      STMT_ERR_RET(code);
×
2139
    }
2140
  } else {
2141
    if (pStmt->sql.stbInterlaceMode) {
43✔
2142
      STMT2_ELOG_E("bind single column not allowed in stb insert mode");
×
UNCOV
2143
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2144
    }
2145

2146
    if (pStmt->sql.bindRowFormat) {
×
2147
      STMT2_ELOG_E("can't mix bind row format and bind column format");
×
UNCOV
2148
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2149
    }
2150

2151
    if (colIdx != (pStmt->bInfo.sBindLastIdx + 1) && colIdx != 0) {
×
2152
      STMT2_ELOG_E("bind column index not in sequence");
×
UNCOV
2153
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2154
    }
2155

UNCOV
2156
    pStmt->bInfo.sBindLastIdx = colIdx;
×
2157

2158
    if (0 == colIdx) {
×
UNCOV
2159
      pStmt->bInfo.sBindRowNum = bind->num;
×
2160
    }
2161

2162
    code = qBindStmtSingleColValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
×
2163
                                    pStmt->exec.pRequest->msgBufLen, colIdx, pStmt->bInfo.sBindRowNum,
×
2164
                                    pStmt->taos->optionInfo.charsetCxt);
×
2165
    if (code) {
×
2166
      STMT2_ELOG("bind single col failed, error:%s", tstrerror(code));
×
UNCOV
2167
      STMT_ERR_RET(code);
×
2168
    }
2169
  }
2170

2171
  int64_t startUs4 = taosGetTimestampUs();
981,226✔
2172
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
981,226✔
2173

2174
  if (pStmt->stbInterlaceMode) {
981,226✔
2175
    if (param) param->tblData.pBlobSet = pBlob;
981,201✔
2176
  }
2177

2178
  if (pStmt->sql.stbInterlaceMode) {
981,226✔
2179
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
981,123✔
2180
  } else {
2181
    STMT_ERR_RET(stmtAddBatch2(pStmt));
162✔
2182
  }
2183

2184
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
981,349✔
2185
  return TSDB_CODE_SUCCESS;
981,349✔
2186
}
2187

2188
/*
2189
int stmtUpdateTableUid(STscStmt2* pStmt, SSubmitRsp* pRsp) {
2190
  tscDebug("stmt start to update tbUid, blockNum:%d", pRsp->nBlocks);
2191

2192
  int32_t code = 0;
2193
  int32_t finalCode = 0;
2194
  size_t  keyLen = 0;
2195
  void*   pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
2196
  while (pIter) {
2197
    STableDataCxt* pBlock = *(STableDataCxt**)pIter;
2198
    char*          key = taosHashGetKey(pIter, &keyLen);
2199

2200
    STableMeta* pMeta = qGetTableMetaInDataBlock(pBlock);
2201
    if (pMeta->uid) {
2202
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2203
      continue;
2204
    }
2205

2206
    SSubmitBlkRsp* blkRsp = NULL;
2207
    int32_t        i = 0;
2208
    for (; i < pRsp->nBlocks; ++i) {
2209
      blkRsp = pRsp->pBlocks + i;
2210
      if (strlen(blkRsp->tblFName) != keyLen) {
2211
        continue;
2212
      }
2213

2214
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
2215
        continue;
2216
      }
2217

2218
      break;
2219
    }
2220

2221
    if (i < pRsp->nBlocks) {
2222
      tscDebug("auto created table %s uid updated from %" PRIx64 " to %" PRIx64, blkRsp->tblFName, pMeta->uid,
2223
               blkRsp->uid);
2224

2225
      pMeta->uid = blkRsp->uid;
2226
      pStmt->bInfo.tbUid = blkRsp->uid;
2227
    } else {
2228
      tscDebug("table %s not found in submit rsp, will update from catalog", pStmt->bInfo.tbFName);
2229
      if (NULL == pStmt->pCatalog) {
2230
        code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog);
2231
        if (code) {
2232
          pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2233
          finalCode = code;
2234
          continue;
2235
        }
2236
      }
2237

2238
      code = stmtCreateRequest(pStmt);
2239
      if (code) {
2240
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2241
        finalCode = code;
2242
        continue;
2243
      }
2244

2245
      STableMeta*      pTableMeta = NULL;
2246
      SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
2247
                               .requestId = pStmt->exec.pRequest->requestId,
2248
                               .requestObjRefId = pStmt->exec.pRequest->self,
2249
                               .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
2250
      code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
2251

2252
      pStmt->stat.ctgGetTbMetaNum++;
2253

2254
      taos_free_result(pStmt->exec.pRequest);
2255
      pStmt->exec.pRequest = NULL;
2256

2257
      if (code || NULL == pTableMeta) {
2258
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2259
        finalCode = code;
2260
        taosMemoryFree(pTableMeta);
2261
        continue;
2262
      }
2263

2264
      pMeta->uid = pTableMeta->uid;
2265
      pStmt->bInfo.tbUid = pTableMeta->uid;
2266
      taosMemoryFree(pTableMeta);
2267
    }
2268

2269
    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
2270
  }
2271

2272
  return finalCode;
2273
}
2274
*/
2275
/*
2276
int stmtStaticModeExec(TAOS_STMT* stmt) {
2277
  STscStmt2*   pStmt = (STscStmt2*)stmt;
2278
  int32_t     code = 0;
2279
  SSubmitRsp* pRsp = NULL;
2280
  if (pStmt->sql.staticMode) {
2281
    return TSDB_CODE_TSC_STMT_API_ERROR;
2282
  }
2283

2284
  STMT_DLOG_E("start to exec");
2285

2286
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
2287

2288
  STMT_ERR_RET(qBuildStmtOutputFromTbList(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pTbBlkList,
2289
pStmt->exec.pCurrBlock, pStmt->exec.tbBlkNum));
2290

2291
  launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
2292

2293
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
2294
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
2295
    if (code) {
2296
      pStmt->exec.pRequest->code = code;
2297
    } else {
2298
      tFreeSSubmitRsp(pRsp);
2299
      STMT_ERR_RET(stmtResetStmt(pStmt));
2300
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
2301
    }
2302
  }
2303

2304
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
2305

2306
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
2307
  pStmt->affectedRows += pStmt->exec.affectedRows;
2308

2309
_return:
2310

2311
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
2312

2313
  tFreeSSubmitRsp(pRsp);
2314

2315
  ++pStmt->sql.runTimes;
2316

2317
  STMT_RET(code);
2318
}
2319
*/
2320

2321
static int32_t createParseContext(const SRequestObj* pRequest, SParseContext** pCxt, SSqlCallbackWrapper* pWrapper) {
×
UNCOV
2322
  const STscObj* pTscObj = pRequest->pTscObj;
×
2323

2324
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
×
2325
  if (*pCxt == NULL) {
×
UNCOV
2326
    return terrno;
×
2327
  }
2328

2329
  **pCxt = (SParseContext){.requestId = pRequest->requestId,
×
2330
                           .requestRid = pRequest->self,
×
2331
                           .acctId = pTscObj->acctId,
×
UNCOV
2332
                           .db = pRequest->pDb,
×
2333
                           .topicQuery = false,
2334
                           .pSql = pRequest->sqlstr,
×
2335
                           .sqlLen = pRequest->sqlLen,
×
UNCOV
2336
                           .pMsg = pRequest->msgBuf,
×
2337
                           .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
UNCOV
2338
                           .pTransporter = pTscObj->pAppInfo->pTransporter,
×
2339
                           .pStmtCb = NULL,
2340
                           .pUser = pTscObj->user,
×
2341
                           .userId = pTscObj->userId,
×
2342
                           .pEffectiveUser = pRequest->effectiveUser,
×
2343
                           .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
×
UNCOV
2344
                           .enableSysInfo = pTscObj->sysInfo,
×
2345
                           .async = true,
2346
                           .svrVer = pTscObj->sVer,
×
2347
                           .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
×
UNCOV
2348
                           .allocatorId = pRequest->allocatorRefId,
×
2349
                           .parseSqlFp = clientParseSql,
2350
                           .parseSqlParam = pWrapper};
2351
  int8_t biMode = atomic_load_8(&((STscObj*)pTscObj)->biMode);
×
2352
  (*pCxt)->biMode = biMode;
×
UNCOV
2353
  return TSDB_CODE_SUCCESS;
×
2354
}
2355

2356
static void asyncQueryCb(void* userdata, TAOS_RES* res, int code) {
×
2357
  STscStmt2*        pStmt = userdata;
×
2358
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
×
2359
  pStmt->asyncResultAvailable = true;
×
UNCOV
2360
  pStmt->exec.pRequest->inCallback = true;
×
2361

2362
  if (code == TSDB_CODE_SUCCESS) {
×
2363
    pStmt->exec.affectedRows = taos_affected_rows(res);
×
UNCOV
2364
    pStmt->affectedRows += pStmt->exec.affectedRows;
×
2365
  }
2366

2367
  if (fp) {
×
UNCOV
2368
    fp(pStmt->options.userdata, res, code);
×
2369
  }
2370

2371
  while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
×
UNCOV
2372
    taosUsleep(1);
×
2373
  }
2374
  (void)stmtCleanExecInfo(pStmt, (code ? false : true), false);
×
2375
  ++pStmt->sql.runTimes;
×
2376
  if (pStmt->exec.pRequest != NULL) {
×
UNCOV
2377
    pStmt->exec.pRequest->inCallback = false;
×
2378
  }
2379

2380
  if (tsem_post(&pStmt->asyncExecSem) != 0) {
×
UNCOV
2381
    STMT2_ELOG_E("fail to post asyncExecSem");
×
2382
  }
UNCOV
2383
}
×
2384

2385
int stmtExec2(TAOS_STMT2* stmt, int* affected_rows) {
472,307✔
2386
  STscStmt2* pStmt = (STscStmt2*)stmt;
472,307✔
2387
  int32_t    code = 0;
472,307✔
2388
  int64_t    startUs = taosGetTimestampUs();
472,343✔
2389

2390
  STMT2_DLOG_E("start to exec");
472,343✔
2391

2392
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
472,343✔
UNCOV
2393
    return pStmt->errCode;
×
2394
  }
2395

2396
  STMT_ERR_RET(taosThreadMutexLock(&pStmt->asyncBindParam.mutex));
472,343✔
2397
  while (atomic_load_8((int8_t*)&pStmt->asyncBindParam.asyncBindNum) > 0) {
472,343✔
UNCOV
2398
    (void)taosThreadCondWait(&pStmt->asyncBindParam.waitCond, &pStmt->asyncBindParam.mutex);
×
2399
  }
2400
  STMT_ERR_RET(taosThreadMutexUnlock(&pStmt->asyncBindParam.mutex));
472,331✔
2401

2402
  if (pStmt->sql.stbInterlaceMode) {
472,367✔
2403
    STMT_ERR_RET(stmtAddBatch2(pStmt));
472,341✔
2404
  }
2405

2406
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
472,316✔
2407

2408
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
472,319✔
2409
    if (pStmt->sql.stbInterlaceMode) {
472,311✔
2410
      int64_t startTs = taosGetTimestampUs();
472,329✔
2411
      // wait for stmt bind thread to finish
2412
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
1,954,688✔
2413
        taosUsleep(1);
1,482,287✔
2414
      }
2415

2416
      if (pStmt->errCode != TSDB_CODE_SUCCESS) {
472,317✔
UNCOV
2417
        return pStmt->errCode;
×
2418
      }
2419

2420
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
472,305✔
2421
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
472,266✔
2422
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
472,302✔
2423
      pStmt->sql.siInfo.pVgroupHash = NULL;
472,215✔
2424
      pStmt->sql.siInfo.pVgroupList = NULL;
472,254✔
2425
    } else {
2426
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
41✔
2427
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
18✔
2428

2429
      STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
18✔
2430

2431
      STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
18✔
2432
    }
2433
  }
2434

2435
  pStmt->asyncResultAvailable = false;
472,280✔
2436
  SRequestObj*      pRequest = pStmt->exec.pRequest;
472,241✔
2437
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
472,278✔
2438
  STMT2_DLOG("EXEC INFO :req:0x%" PRIx64 ", QID:0x%" PRIx64 ", exec sql:%s,  conn:%" PRId64, pRequest->self,
472,241✔
2439
             pRequest->requestId, pStmt->sql.sqlStr, pRequest->pTscObj->id);
2440

2441
  if (!fp) {
472,241✔
2442
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
472,241✔
2443

2444
    if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
472,271✔
2445
      STMT2_ELOG_E("exec failed errorcode:NEED_CLIENT_HANDLE_ERROR, need to refresh meta and retry");
×
2446
      code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
×
2447
      if (code) {
×
UNCOV
2448
        pStmt->exec.pRequest->code = code;
×
2449

2450
      } else {
2451
        STMT_ERR_RET(stmtResetStmt(pStmt));
×
UNCOV
2452
        STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
×
2453
      }
2454
    }
2455

2456
    STMT_ERR_JRET(pStmt->exec.pRequest->code);
472,331✔
2457

2458
    pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
472,331✔
2459
    if (affected_rows) {
472,343✔
2460
      *affected_rows = pStmt->exec.affectedRows;
472,139✔
2461
    }
2462
    pStmt->affectedRows += pStmt->exec.affectedRows;
472,343✔
2463

2464
    // wait for stmt bind thread to finish
2465
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
472,355✔
2466
      taosUsleep(1);
12✔
2467
    }
2468

2469
    STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
472,110✔
2470

2471
    ++pStmt->sql.runTimes;
472,155✔
2472
  } else {
2473
    SSqlCallbackWrapper* pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
×
2474
    if (pWrapper == NULL) {
×
UNCOV
2475
      code = terrno;
×
2476
    } else {
2477
      pWrapper->pRequest = pRequest;
×
UNCOV
2478
      pRequest->pWrapper = pWrapper;
×
2479
    }
2480
    if (TSDB_CODE_SUCCESS == code) {
×
UNCOV
2481
      code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
×
2482
    }
2483
    pRequest->syncQuery = false;
×
2484
    pRequest->body.queryFp = asyncQueryCb;
×
UNCOV
2485
    ((SSyncQueryParam*)(pRequest)->body.interParam)->userParam = pStmt;
×
2486

2487
    pStmt->execSemWaited = false;
×
UNCOV
2488
    launchAsyncQuery(pRequest, pStmt->sql.pQuery, NULL, pWrapper);
×
2489
  }
2490

2491
_return:
472,235✔
2492
  if (code) {
472,235✔
UNCOV
2493
    STMT2_ELOG("exec failed, error:%s", tstrerror(code));
×
2494
  }
2495
  pStmt->stat.execUseUs += taosGetTimestampUs() - startUs;
472,295✔
2496

2497
  STMT_RET(code);
472,256✔
2498
}
2499

2500
int stmtClose2(TAOS_STMT2* stmt) {
2,577✔
2501
  STscStmt2* pStmt = (STscStmt2*)stmt;
2,577✔
2502

2503
  STMT2_DLOG_E("start to close stmt");
2,577✔
2504
  taosMemoryFreeClear(pStmt->db);
2,577✔
2505

2506
  if (pStmt->bindThreadInUse) {
2,577✔
2507
    // wait for stmt bind thread to finish
2508
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
2,551✔
UNCOV
2509
      taosUsleep(1);
×
2510
    }
2511

2512
    (void)taosThreadMutexLock(&pStmt->queue.mutex);
2,551✔
2513
    pStmt->queue.stopQueue = true;
2,551✔
2514
    (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
2,551✔
2515
    (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
2,551✔
2516

2517
    (void)taosThreadJoin(pStmt->bindThread, NULL);
2,551✔
2518
    pStmt->bindThreadInUse = false;
2,551✔
2519

2520
    (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
2,551✔
2521
    (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
2,551✔
2522
  }
2523

2524
  TSC_ERR_RET(taosThreadMutexLock(&pStmt->asyncBindParam.mutex));
2,577✔
2525
  while (atomic_load_8((int8_t*)&pStmt->asyncBindParam.asyncBindNum) > 0) {
2,577✔
UNCOV
2526
    (void)taosThreadCondWait(&pStmt->asyncBindParam.waitCond, &pStmt->asyncBindParam.mutex);
×
2527
  }
2528
  TSC_ERR_RET(taosThreadMutexUnlock(&pStmt->asyncBindParam.mutex));
2,577✔
2529

2530
  (void)taosThreadCondDestroy(&pStmt->asyncBindParam.waitCond);
2,577✔
2531
  (void)taosThreadMutexDestroy(&pStmt->asyncBindParam.mutex);
2,577✔
2532

2533
  if (pStmt->options.asyncExecFn && !pStmt->execSemWaited) {
2,577✔
2534
    if (tsem_wait(&pStmt->asyncExecSem) != 0) {
×
UNCOV
2535
      STMT2_ELOG_E("fail to wait asyncExecSem");
×
2536
    }
2537
  }
2538

2539
  STMT2_DLOG("stbInterlaceMode:%d, statInfo: ctgGetTbMetaNum=>%" PRId64 ", getCacheTbInfo=>%" PRId64
2,577✔
2540
             ", parseSqlNum=>%" PRId64 ", pStmt->stat.bindDataNum=>%" PRId64
2541
             ", settbnameAPI:%u, bindAPI:%u, addbatchAPI:%u, execAPI:%u"
2542
             ", setTbNameUs:%" PRId64 ", bindDataUs:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64 " addBatchUs:%" PRId64
2543
             ", execWaitUs:%" PRId64 ", execUseUs:%" PRId64,
2544
             pStmt->sql.stbInterlaceMode, pStmt->stat.ctgGetTbMetaNum, pStmt->stat.getCacheTbInfo,
2545
             pStmt->stat.parseSqlNum, pStmt->stat.bindDataNum, pStmt->seqIds[STMT_SETTBNAME], pStmt->seqIds[STMT_BIND],
2546
             pStmt->seqIds[STMT_ADD_BATCH], pStmt->seqIds[STMT_EXECUTE], pStmt->stat.setTbNameUs,
2547
             pStmt->stat.bindDataUs1, pStmt->stat.bindDataUs2, pStmt->stat.bindDataUs3, pStmt->stat.bindDataUs4,
2548
             pStmt->stat.addBatchUs, pStmt->stat.execWaitUs, pStmt->stat.execUseUs);
2549
  if (pStmt->sql.stbInterlaceMode) {
2,577✔
2550
    pStmt->bInfo.tagsCached = false;
2,551✔
2551
  }
2552
  pStmt->bInfo.boundColsCached = false;
2,577✔
2553

2554
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
2,577✔
2555

2556
  if (pStmt->options.asyncExecFn) {
2,577✔
2557
    if (tsem_destroy(&pStmt->asyncExecSem) != 0) {
×
UNCOV
2558
      STMT2_ELOG_E("fail to destroy asyncExecSem");
×
2559
    }
2560
  }
2561
  taosMemoryFree(stmt);
2,577✔
2562

2563
  return TSDB_CODE_SUCCESS;
2,577✔
2564
}
2565

2566
const char* stmtErrstr2(TAOS_STMT2* stmt) {
×
UNCOV
2567
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
2568

2569
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
×
UNCOV
2570
    return (char*)tstrerror(terrno);
×
2571
  }
2572

2573
  // if stmt async exec ,error code is pStmt->exec.pRequest->code
2574
  if (!(pStmt->sql.status >= STMT_EXECUTE && pStmt->options.asyncExecFn != NULL && pStmt->asyncResultAvailable)) {
×
UNCOV
2575
    pStmt->exec.pRequest->code = terrno;
×
2576
  }
2577

2578
  SRequestObj* pRequest = pStmt->exec.pRequest;
×
2579
  if (NULL != pRequest->msgBuf && (strlen(pRequest->msgBuf) > 0 || pRequest->code == TSDB_CODE_RPC_FQDN_ERROR)) {
×
UNCOV
2580
    return pRequest->msgBuf;
×
2581
  }
UNCOV
2582
  return (const char*)tstrerror(pRequest->code);
×
2583
}
2584
/*
2585
int stmtAffectedRows(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->affectedRows; }
2586

2587
int stmtAffectedRowsOnce(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->exec.affectedRows; }
2588
*/
2589

2590
int stmtParseColFields2(TAOS_STMT2* stmt) {
18✔
2591
  int32_t    code = 0;
18✔
2592
  STscStmt2* pStmt = (STscStmt2*)stmt;
18✔
2593
  int32_t    preCode = pStmt->errCode;
18✔
2594

2595
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
18✔
UNCOV
2596
    return pStmt->errCode;
×
2597
  }
2598

2599
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
18✔
2600
    STMT2_ELOG_E("stmtParseColFields2 only for insert");
×
UNCOV
2601
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
2602
  }
2603

2604
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
18✔
2605

2606
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
18✔
2607
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
UNCOV
2608
    pStmt->bInfo.needParse = false;
×
2609
  }
2610
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx != NULL) {
18✔
UNCOV
2611
    pStmt->bInfo.needParse = false;
×
2612
  }
2613

2614
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
18✔
2615

2616
  if (pStmt->bInfo.needParse) {
18✔
2617
    STMT_ERRI_JRET(stmtParseSql(pStmt));
18✔
2618
  }
2619

2620
_return:
18✔
2621
  // compatible with previous versions
2622
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && (pStmt->bInfo.tbNameFlag & NO_DATA_USING_CLAUSE) == 0x0) {
18✔
UNCOV
2623
    code = TSDB_CODE_TSC_STMT_TBNAME_ERROR;
×
2624
  }
2625

2626
  pStmt->errCode = preCode;
18✔
2627

2628
  return code;
18✔
2629
}
2630

2631
int stmtGetStbColFields2(TAOS_STMT2* stmt, int* nums, TAOS_FIELD_ALL** fields) {
18✔
2632
  int32_t code = stmtParseColFields2(stmt);
18✔
2633
  if (code != TSDB_CODE_SUCCESS) {
18✔
UNCOV
2634
    return code;
×
2635
  }
2636

2637
  return stmtFetchStbColFields2(stmt, nums, fields);
18✔
2638
}
2639

2640
int stmtGetParamNum2(TAOS_STMT2* stmt, int* nums) {
×
2641
  int32_t    code = 0;
×
2642
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
UNCOV
2643
  int32_t    preCode = pStmt->errCode;
×
2644

2645
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
UNCOV
2646
    return pStmt->errCode;
×
2647
  }
2648

UNCOV
2649
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
×
2650

2651
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
×
2652
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
UNCOV
2653
    pStmt->bInfo.needParse = false;
×
2654
  }
2655

2656
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
×
UNCOV
2657
    resetRequest(pStmt);
×
2658
  }
2659

UNCOV
2660
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
2661

2662
  if (pStmt->bInfo.needParse) {
×
UNCOV
2663
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
2664
  }
2665

2666
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
UNCOV
2667
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
×
2668
  } else {
UNCOV
2669
    STMT_ERRI_JRET(stmtFetchColFields2(stmt, nums, NULL));
×
2670
  }
2671

UNCOV
2672
  STMT2_DLOG("get param num success, nums:%d", *nums);
×
2673

UNCOV
2674
_return:
×
2675

UNCOV
2676
  pStmt->errCode = preCode;
×
2677

UNCOV
2678
  return code;
×
2679
}
2680

2681
TAOS_RES* stmtUseResult2(TAOS_STMT2* stmt) {
8✔
2682
  STscStmt2* pStmt = (STscStmt2*)stmt;
8✔
2683

2684
  STMT2_TLOG_E("start to use result");
8✔
2685

2686
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
8✔
2687
    STMT2_ELOG_E("useResult only for query statement");
×
UNCOV
2688
    return NULL;
×
2689
  }
2690

2691
  if (pStmt->options.asyncExecFn != NULL && !pStmt->asyncResultAvailable) {
8✔
2692
    STMT2_ELOG_E("use result after callBackFn return");
×
UNCOV
2693
    return NULL;
×
2694
  }
2695

2696
  if (tsUseAdapter) {
8✔
UNCOV
2697
    TAOS_RES* res = (TAOS_RES*)pStmt->exec.pRequest;
×
2698
    pStmt->exec.pRequest = NULL;
×
2699
    return res;
×
2700
  }
2701

2702
  return pStmt->exec.pRequest;
8✔
2703
}
2704

2705
int32_t stmtAsyncBindThreadFunc(void* args) {
×
2706
  qInfo("async stmt bind thread started");
×
2707

2708
  ThreadArgs* targs = (ThreadArgs*)args;
×
2709
  STscStmt2*  pStmt = (STscStmt2*)targs->stmt;
×
2710

UNCOV
2711
  int code = taos_stmt2_bind_param(targs->stmt, targs->bindv, targs->col_idx);
×
2712
  targs->fp(targs->param, NULL, code);
×
UNCOV
2713
  (void)taosThreadMutexLock(&(pStmt->asyncBindParam.mutex));
×
2714
  (void)atomic_sub_fetch_8(&pStmt->asyncBindParam.asyncBindNum, 1);
×
UNCOV
2715
  (void)taosThreadCondSignal(&(pStmt->asyncBindParam.waitCond));
×
UNCOV
2716
  (void)taosThreadMutexUnlock(&(pStmt->asyncBindParam.mutex));
×
2717
  taosMemoryFree(args);
×
2718

2719
  qInfo("async stmt bind thread stopped");
×
2720

UNCOV
2721
  return code;
×
2722
}
2723

UNCOV
2724
void stmtBuildErrorMsg(STscStmt2* pStmt, const char* msg) {
×
UNCOV
2725
  if (pStmt == NULL || msg == NULL) {
×
2726
    return;
×
2727
  }
2728

UNCOV
2729
  if (pStmt->exec.pRequest == NULL) {
×
2730
    return;
×
2731
  }
2732

2733
  if (pStmt->exec.pRequest->msgBuf == NULL) {
×
2734
    return;
×
2735
  }
2736

UNCOV
2737
  size_t msgLen = strlen(msg);
×
2738
  size_t bufLen = pStmt->exec.pRequest->msgBufLen;
×
2739

UNCOV
2740
  if (msgLen >= bufLen) {
×
UNCOV
2741
    tstrncpy(pStmt->exec.pRequest->msgBuf, msg, bufLen - 1);
×
2742
    pStmt->exec.pRequest->msgBuf[bufLen - 1] = '\0';
×
UNCOV
2743
    pStmt->exec.pRequest->msgBufLen = bufLen - 1;
×
2744
  } else {
2745
    tstrncpy(pStmt->exec.pRequest->msgBuf, msg, bufLen);
×
2746
    pStmt->exec.pRequest->msgBufLen = msgLen;
×
2747
  }
2748

2749
  return;
×
2750
}
2751

UNCOV
2752
int32_t stmtBuildErrorMsgWithCode(STscStmt2* pStmt, const char* msg, int32_t errorCode) {
×
UNCOV
2753
  stmtBuildErrorMsg(pStmt, msg);
×
UNCOV
2754
  pStmt->errCode = errorCode;
×
2755

UNCOV
2756
  return errorCode;
×
2757
}
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