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

taosdata / TDengine / #3608

12 Feb 2025 05:57AM UTC coverage: 63.066% (+1.4%) from 61.715%
#3608

push

travis-ci

web-flow
Merge pull request #29746 from taosdata/merge/mainto3.02

merge: from main to 3.0 branch

140199 of 286257 branches covered (48.98%)

Branch coverage included in aggregate %.

89 of 161 new or added lines in 18 files covered. (55.28%)

3211 existing lines in 190 files now uncovered.

218998 of 283298 relevant lines covered (77.3%)

5949310.66 hits per line

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

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

5
#include "clientStmt.h"
6
#include "clientStmt2.h"
7
/*
8
char* gStmtStatusStr[] = {"unknown",     "init", "prepare", "settbname", "settags",
9
                          "fetchFields", "bind", "bindCol", "addBatch",  "exec"};
10
*/
11
static FORCE_INLINE int32_t stmtAllocQNodeFromBuf(STableBufInfo* pTblBuf, void** pBuf) {
12
  if (pTblBuf->buffOffset < pTblBuf->buffSize) {
62✔
13
    *pBuf = (char*)pTblBuf->pCurBuff + pTblBuf->buffOffset;
62✔
14
    pTblBuf->buffOffset += pTblBuf->buffUnit;
62✔
15
  } else if (pTblBuf->buffIdx < taosArrayGetSize(pTblBuf->pBufList)) {
×
16
    pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, pTblBuf->buffIdx++);
×
17
    if (NULL == pTblBuf->pCurBuff) {
×
18
      return TAOS_GET_TERRNO(TSDB_CODE_OUT_OF_MEMORY);
×
19
    }
20
    *pBuf = pTblBuf->pCurBuff;
×
21
    pTblBuf->buffOffset = pTblBuf->buffUnit;
×
22
  } else {
23
    void* buff = taosMemoryMalloc(pTblBuf->buffSize);
×
24
    if (NULL == buff) {
×
25
      return terrno;
×
26
    }
27

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

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

38
  return TSDB_CODE_SUCCESS;
62✔
39
}
40

41
static bool stmtDequeue(STscStmt2* pStmt, SStmtQNode** param) {
61✔
42
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
61✔
43
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
78✔
44
    (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
36✔
45
    if (atomic_load_8((int8_t*)&pStmt->queue.stopQueue)) {
36✔
46
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
19✔
47
      return false;
19✔
48
    }
49
  }
50
  SStmtQNode* orig = pStmt->queue.head;
42✔
51
  SStmtQNode* node = pStmt->queue.head->next;
42✔
52
  pStmt->queue.head = pStmt->queue.head->next;
42✔
53
  *param = node;
42✔
54

55
  (void)atomic_sub_fetch_64((int64_t*)&pStmt->queue.qRemainNum, 1);
42✔
56
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
42✔
57

58
  return true;
42✔
59
}
60

61
static void stmtEnqueue(STscStmt2* pStmt, SStmtQNode* param) {
41✔
62
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
41✔
63

64
  pStmt->queue.tail->next = param;
41✔
65
  pStmt->queue.tail = param;
41✔
66
  pStmt->stat.bindDataNum++;
41✔
67
  (void)atomic_add_fetch_64((int64_t*)&pStmt->queue.qRemainNum, 1);
41✔
68
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
41✔
69

70
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
42✔
71
}
42✔
72

73
static int32_t stmtCreateRequest(STscStmt2* pStmt) {
509✔
74
  int32_t code = 0;
509✔
75

76
  if (pStmt->exec.pRequest == NULL) {
509✔
77
    code = buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false, &pStmt->exec.pRequest,
115✔
78
                        pStmt->reqid);
79
    if (pStmt->reqid != 0) {
115!
80
      pStmt->reqid++;
×
81
    }
82
    if (pStmt->db != NULL) {
115✔
83
      taosMemoryFreeClear(pStmt->exec.pRequest->pDb); 
58!
84
      pStmt->exec.pRequest->pDb = taosStrdup(pStmt->db);
58!
85
    }
86
    if (TSDB_CODE_SUCCESS == code) {
115!
87
      pStmt->exec.pRequest->syncQuery = true;
115✔
88
      pStmt->exec.pRequest->isStmtBind = true;
115✔
89
    }
90
  }
91

92
  return code;
509✔
93
}
94

95
static int32_t stmtSwitchStatus(STscStmt2* pStmt, STMT_STATUS newStatus) {
696✔
96
  int32_t code = 0;
696✔
97

98
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
696!
99
    STMT_LOG_SEQ(newStatus);
696!
100
  }
101

102
  if (pStmt->errCode && newStatus != STMT_PREPARE) {
695!
103
    STMT_DLOG("stmt already failed with err: %s", tstrerror(pStmt->errCode));
×
104
    return pStmt->errCode;
×
105
  }
106

107
  switch (newStatus) {
695!
108
    case STMT_PREPARE:
72✔
109
      pStmt->errCode = 0;
72✔
110
      break;
72✔
111
    case STMT_SETTBNAME:
134✔
112
      if (STMT_STATUS_EQ(INIT)) {
134!
113
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
114
      }
115
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
134!
116
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
117
      }
118
      break;
134✔
119
    case STMT_SETTAGS:
97✔
120
      if (STMT_STATUS_EQ(INIT)) {
97!
121
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
122
      }
123
      break;
97✔
124
    case STMT_FETCH_FIELDS:
43✔
125
      if (STMT_STATUS_EQ(INIT)) {
43!
126
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
127
      }
128
      break;
43✔
129
    case STMT_BIND:
143✔
130
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
143!
131
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
132
      }
133
      /*
134
            if ((pStmt->sql.type == STMT_TYPE_MULTI_INSERT) && ()) {
135
              code = TSDB_CODE_TSC_STMT_API_ERROR;
136
            }
137
      */
138
      break;
143✔
139
    case STMT_BIND_COL:
×
140
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND)) {
×
141
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
142
      }
143
      break;
×
144
    case STMT_ADD_BATCH:
127✔
145
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
127!
146
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
147
      }
148
      break;
127✔
149
    case STMT_EXECUTE:
79✔
150
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
79✔
151
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
3!
152
            STMT_STATUS_NE(BIND_COL)) {
×
153
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
154
        }
155
      } else {
156
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS)) {
76!
157
          code = TSDB_CODE_TSC_STMT_API_ERROR;
1✔
158
        }
159
      }
160
      break;
79✔
161
    default:
×
162
      code = TSDB_CODE_APP_ERROR;
×
163
      break;
×
164
  }
165

166
  STMT_ERR_RET(code);
695✔
167

168
  pStmt->sql.status = newStatus;
694✔
169

170
  return TSDB_CODE_SUCCESS;
694✔
171
}
172

173
static int32_t stmtGetTbName(TAOS_STMT2* stmt, char** tbName) {
40✔
174
  STscStmt2* pStmt = (STscStmt2*)stmt;
40✔
175

176
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
40✔
177

178
  if ('\0' == pStmt->bInfo.tbName[0]) {
40✔
179
    tscError("no table name set");
17!
180
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_TBNAME_ERROR);
17!
181
  }
182

183
  *tbName = pStmt->bInfo.tbName;
23✔
184

185
  return TSDB_CODE_SUCCESS;
23✔
186
}
187

188
static int32_t stmtUpdateBindInfo(TAOS_STMT2* stmt, STableMeta* pTableMeta, void* tags, SName* tbName,
47✔
189
                                  const char* sTableName, bool autoCreateTbl, bool preCtbname) {
190
  STscStmt2* pStmt = (STscStmt2*)stmt;
47✔
191
  char       tbFName[TSDB_TABLE_FNAME_LEN];
192
  int32_t    code = tNameExtractFullName(tbName, tbFName);
47✔
193
  if (code != 0) {
47!
194
    return code;
×
195
  }
196

197
  (void)memcpy(&pStmt->bInfo.sname, tbName, sizeof(*tbName));
47✔
198
  tstrncpy(pStmt->bInfo.tbFName, tbFName, sizeof(pStmt->bInfo.tbFName));
47✔
199
  pStmt->bInfo.tbFName[sizeof(pStmt->bInfo.tbFName) - 1] = 0;
47✔
200

201
  pStmt->bInfo.tbUid = autoCreateTbl ? 0 : pTableMeta->uid;
47✔
202
  pStmt->bInfo.tbSuid = pTableMeta->suid;
47✔
203
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
47✔
204
  pStmt->bInfo.tbType = pTableMeta->tableType;
47✔
205

206
  if (!pStmt->bInfo.tagsCached) {
47✔
207
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
46✔
208
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
43!
209
  }
210

211
  pStmt->bInfo.boundTags = tags;
45✔
212
  pStmt->bInfo.tagsCached = false;
45✔
213
  pStmt->bInfo.preCtbname = preCtbname;
45✔
214
  tstrncpy(pStmt->bInfo.stbFName, sTableName, sizeof(pStmt->bInfo.stbFName));
45✔
215

216
  return TSDB_CODE_SUCCESS;
45✔
217
}
218

219
static int32_t stmtUpdateExecInfo(TAOS_STMT2* stmt, SHashObj* pVgHash, SHashObj* pBlockHash) {
44✔
220
  STscStmt2* pStmt = (STscStmt2*)stmt;
44✔
221

222
  pStmt->sql.pVgHash = pVgHash;
44✔
223
  pStmt->exec.pBlockHash = pBlockHash;
44✔
224

225
  return TSDB_CODE_SUCCESS;
44✔
226
}
227

228
static int32_t stmtUpdateInfo(TAOS_STMT2* stmt, STableMeta* pTableMeta, void* tags, SName* tbName, bool autoCreateTbl,
47✔
229
                              SHashObj* pVgHash, SHashObj* pBlockHash, const char* sTableName, bool preCtbname) {
230
  STscStmt2* pStmt = (STscStmt2*)stmt;
47✔
231

232
  STMT_ERR_RET(stmtUpdateBindInfo(stmt, pTableMeta, tags, tbName, sTableName, autoCreateTbl, preCtbname));
47!
233
  STMT_ERR_RET(stmtUpdateExecInfo(stmt, pVgHash, pBlockHash));
45!
234

235
  pStmt->sql.autoCreateTbl = autoCreateTbl;
43✔
236
  if (pStmt->sql.autoCreateTbl) {
43✔
237
    pStmt->sql.stbInterlaceMode = false;
30✔
238
  }
239

240
  return TSDB_CODE_SUCCESS;
43✔
241
}
242

243
static int32_t stmtGetExecInfo(TAOS_STMT2* stmt, SHashObj** pVgHash, SHashObj** pBlockHash) {
×
244
  STscStmt2* pStmt = (STscStmt2*)stmt;
×
245

246
  *pVgHash = pStmt->sql.pVgHash;
×
247
  pStmt->sql.pVgHash = NULL;
×
248

249
  *pBlockHash = pStmt->exec.pBlockHash;
×
250
  pStmt->exec.pBlockHash = NULL;
×
251

252
  return TSDB_CODE_SUCCESS;
×
253
}
254

255
static int32_t stmtParseSql(STscStmt2* pStmt) {
70✔
256
  pStmt->exec.pCurrBlock = NULL;
70✔
257

258
  SStmtCallback stmtCb = {
70✔
259
      .pStmt = pStmt,
260
      .getTbNameFn = stmtGetTbName,
261
      .setInfoFn = stmtUpdateInfo,
262
      .getExecInfoFn = stmtGetExecInfo,
263
  };
264

265
  STMT_ERR_RET(stmtCreateRequest(pStmt));
70!
266

267
  pStmt->stat.parseSqlNum++;
70✔
268
  STMT_ERR_RET(parseSql(pStmt->exec.pRequest, false, &pStmt->sql.pQuery, &stmtCb));
70✔
269
  pStmt->sql.siInfo.pQuery = pStmt->sql.pQuery;
51✔
270

271
  pStmt->bInfo.needParse = false;
51✔
272

273
  if (pStmt->sql.pQuery->pRoot && 0 == pStmt->sql.type) {
51✔
274
    pStmt->sql.type = STMT_TYPE_INSERT;
9✔
275
    pStmt->sql.stbInterlaceMode = false;
9✔
276
  } else if (pStmt->sql.pQuery->pPrepareRoot) {
42✔
277
    pStmt->sql.type = STMT_TYPE_QUERY;
4✔
278
    pStmt->sql.stbInterlaceMode = false;
4✔
279

280
    return TSDB_CODE_SUCCESS;
4✔
281
  }
282

283
  STableDataCxt** pSrc =
284
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
47✔
285
  if (NULL == pSrc || NULL == *pSrc) {
47!
286
    return terrno;
×
287
  }
288

289
  STableDataCxt* pTableCtx = *pSrc;
47✔
290
  if (pStmt->sql.stbInterlaceMode) {
47✔
291
    int16_t lastIdx = -1;
7✔
292

293
    for (int32_t i = 0; i < pTableCtx->boundColsInfo.numOfBound; ++i) {
33✔
294
      if (pTableCtx->boundColsInfo.pColIndex[i] < lastIdx) {
26!
295
        pStmt->sql.stbInterlaceMode = false;
×
296
        break;
×
297
      }
298

299
      lastIdx = pTableCtx->boundColsInfo.pColIndex[i];
26✔
300
    }
301
  }
302

303
  if (NULL == pStmt->sql.pBindInfo) {
47!
304
    pStmt->sql.pBindInfo = taosMemoryMalloc(pTableCtx->boundColsInfo.numOfBound * sizeof(*pStmt->sql.pBindInfo));
47!
305
    if (NULL == pStmt->sql.pBindInfo) {
47!
306
      return terrno;
×
307
    }
308
  }
309

310
  return TSDB_CODE_SUCCESS;
47✔
311
}
312

313
static int32_t stmtCleanBindInfo(STscStmt2* pStmt) {
163✔
314
  pStmt->bInfo.tbUid = 0;
163✔
315
  pStmt->bInfo.tbSuid = 0;
163✔
316
  pStmt->bInfo.tbVgId = -1;
163✔
317
  pStmt->bInfo.tbType = 0;
163✔
318
  pStmt->bInfo.needParse = true;
163✔
319
  pStmt->bInfo.inExecCache = false;
163✔
320

321
  pStmt->bInfo.tbName[0] = 0;
163✔
322
  pStmt->bInfo.tbFName[0] = 0;
163✔
323
  if (!pStmt->bInfo.tagsCached) {
163✔
324
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
131✔
325
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
131!
326
  }
327
  pStmt->bInfo.stbFName[0] = 0;
163✔
328

329
  return TSDB_CODE_SUCCESS;
163✔
330
}
331

332
static void stmtFreeTableBlkList(STableColsData* pTb) {
×
333
  (void)qResetStmtColumns(pTb->aCol, true);
×
334
  taosArrayDestroy(pTb->aCol);
×
335
}
×
336

337
static void stmtResetQueueTableBuf(STableBufInfo* pTblBuf, SStmtQueue* pQueue) {
15✔
338
  pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, 0);
15✔
339
  if (NULL == pTblBuf->pCurBuff) {
15!
340
    tscError("QInfo:%p, failed to get buffer from list", pTblBuf);
×
341
    return;
×
342
  }
343
  pTblBuf->buffIdx = 1;
15✔
344
  pTblBuf->buffOffset = sizeof(*pQueue->head);
15✔
345

346
  (void)taosThreadMutexLock(&pQueue->mutex);
15✔
347
  pQueue->head = pQueue->tail = pTblBuf->pCurBuff;
15✔
348
  pQueue->qRemainNum = 0;
15✔
349
  pQueue->head->next = NULL;
15✔
350
  (void)taosThreadMutexUnlock(&pQueue->mutex);
15✔
351
}
352

353
static int32_t stmtCleanExecInfo(STscStmt2* pStmt, bool keepTable, bool deepClean) {
152✔
354
  if (pStmt->sql.stbInterlaceMode) {
152✔
355
    if (deepClean) {
24✔
356
      taosHashCleanup(pStmt->exec.pBlockHash);
9✔
357
      pStmt->exec.pBlockHash = NULL;
9✔
358

359
      if (NULL != pStmt->exec.pCurrBlock) {
9✔
360
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->pData);
7!
361
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
7✔
362
      }
363
    } else {
364
      pStmt->sql.siInfo.pTableColsIdx = 0;
15✔
365
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
15✔
366
    }
367
    if (NULL != pStmt->exec.pRequest) {
24✔
368
      pStmt->exec.pRequest->body.resInfo.numOfRows = 0;
23✔
369
    }
370
  } else {
371
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
128✔
372
      // if (!pStmt->options.asyncExecFn) {
373
      taos_free_result(pStmt->exec.pRequest);
125✔
374
      pStmt->exec.pRequest = NULL;
125✔
375
      //}
376
    }
377

378
    size_t keyLen = 0;
128✔
379
    void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
128✔
380
    while (pIter) {
264✔
381
      STableDataCxt* pBlocks = *(STableDataCxt**)pIter;
136✔
382
      char*          key = taosHashGetKey(pIter, &keyLen);
136✔
383
      STableMeta*    pMeta = qGetTableMetaInDataBlock(pBlocks);
136✔
384

385
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
136✔
386
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
60✔
387
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
123!
388

389
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
60✔
390
        continue;
60✔
391
      }
392

393
      qDestroyStmtDataBlock(pBlocks);
76✔
394
      STMT_ERR_RET(taosHashRemove(pStmt->exec.pBlockHash, key, keyLen));
76!
395

396
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
76✔
397
    }
398

399
    if (keepTable) {
128✔
400
      return TSDB_CODE_SUCCESS;
63✔
401
    }
402

403
    taosHashCleanup(pStmt->exec.pBlockHash);
65✔
404
    pStmt->exec.pBlockHash = NULL;
65✔
405

406
    tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
65✔
407
    taosMemoryFreeClear(pStmt->exec.pCurrTbData);
65!
408
  }
409

410
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
89!
411

412
  return TSDB_CODE_SUCCESS;
89✔
413
}
414

415
static void stmtFreeTbBuf(void* buf) {
21✔
416
  void* pBuf = *(void**)buf;
21✔
417
  taosMemoryFree(pBuf);
21!
418
}
21✔
419

420
static void stmtFreeTbCols(void* buf) {
7,000✔
421
  SArray* pCols = *(SArray**)buf;
7,000✔
422
  taosArrayDestroy(pCols);
7,000✔
423
}
7,000✔
424

425
static int32_t stmtCleanSQLInfo(STscStmt2* pStmt) {
74✔
426
  STMT_DLOG_E("start to free SQL info");
74!
427

428
  taosMemoryFreeClear(pStmt->db);
74!
429
  taosMemoryFree(pStmt->sql.pBindInfo);
74!
430
  taosMemoryFree(pStmt->sql.queryRes.fields);
74!
431
  taosMemoryFree(pStmt->sql.queryRes.userFields);
74!
432
  taosMemoryFree(pStmt->sql.sqlStr);
74!
433
  qDestroyQuery(pStmt->sql.pQuery);
74✔
434
  taosArrayDestroy(pStmt->sql.nodeList);
74✔
435
  taosHashCleanup(pStmt->sql.pVgHash);
74✔
436
  pStmt->sql.pVgHash = NULL;
74✔
437

438
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
74✔
439
  while (pIter) {
90✔
440
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
16✔
441

442
    qDestroyStmtDataBlock(pCache->pDataCtx);
16✔
443
    qDestroyBoundColInfo(pCache->boundTags);
16✔
444
    taosMemoryFreeClear(pCache->boundTags);
16!
445

446
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
16✔
447
  }
448
  taosHashCleanup(pStmt->sql.pTableCache);
74✔
449
  pStmt->sql.pTableCache = NULL;
74✔
450

451
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
74!
452
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
74!
453

454
  taos_free_result(pStmt->sql.siInfo.pRequest);
74✔
455
  taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
74✔
456
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
74✔
457
  taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
74✔
458
  taosMemoryFree(pStmt->sql.siInfo.pTSchema);
74!
459
  qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
74✔
460
  taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
74✔
461

462
  (void)memset(&pStmt->sql, 0, sizeof(pStmt->sql));
74✔
463
  pStmt->sql.siInfo.tableColsReady = true;
74✔
464

465
  STMT_DLOG_E("end to free SQL info");
74!
466

467
  return TSDB_CODE_SUCCESS;
74✔
468
}
469

470
static int32_t stmtTryAddTableVgroupInfo(STscStmt2* pStmt, int32_t* vgId) {
51✔
471
  if (*vgId >= 0 && taosHashGet(pStmt->sql.pVgHash, (const char*)vgId, sizeof(*vgId))) {
51✔
472
    return TSDB_CODE_SUCCESS;
5✔
473
  }
474

475
  SVgroupInfo      vgInfo = {0};
46✔
476
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
46✔
477
                           .requestId = pStmt->exec.pRequest->requestId,
46✔
478
                           .requestObjRefId = pStmt->exec.pRequest->self,
46✔
479
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
46✔
480

481
  int32_t code = catalogGetTableHashVgroup(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &vgInfo);
46✔
482
  if (TSDB_CODE_SUCCESS != code) {
46!
UNCOV
483
    return code;
×
484
  }
485

486
  code =
487
      taosHashPut(pStmt->sql.pVgHash, (const char*)&vgInfo.vgId, sizeof(vgInfo.vgId), (char*)&vgInfo, sizeof(vgInfo));
46✔
488
  if (TSDB_CODE_SUCCESS != code) {
46!
UNCOV
489
    return code;
×
490
  }
491

492
  *vgId = vgInfo.vgId;
46✔
493

494
  return TSDB_CODE_SUCCESS;
46✔
495
}
496

497
static int32_t stmtRebuildDataBlock(STscStmt2* pStmt, STableDataCxt* pDataBlock, STableDataCxt** newBlock, uint64_t uid,
51✔
498
                                    uint64_t suid, int32_t vgId) {
499
  STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
51!
500
  STMT_ERR_RET(qRebuildStmtDataBlock(newBlock, pDataBlock, uid, suid, vgId, pStmt->sql.autoCreateTbl));
51!
501

502
  STMT_DLOG("tableDataCxt rebuilt, uid:%" PRId64 ", vgId:%d", uid, vgId);
51!
503

504
  return TSDB_CODE_SUCCESS;
51✔
505
}
506

507
static int32_t stmtGetFromCache(STscStmt2* pStmt) {
114✔
508
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx) {
114!
UNCOV
509
    pStmt->bInfo.needParse = false;
×
UNCOV
510
    pStmt->bInfo.inExecCache = false;
×
UNCOV
511
    return TSDB_CODE_SUCCESS;
×
512
  }
513

514
  pStmt->bInfo.needParse = true;
114✔
515
  pStmt->bInfo.inExecCache = false;
114✔
516

517
  STableDataCxt** pCxtInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
114✔
518
  if (pCxtInExec) {
112✔
519
    pStmt->bInfo.needParse = false;
40✔
520
    pStmt->bInfo.inExecCache = true;
40✔
521

522
    pStmt->exec.pCurrBlock = *pCxtInExec;
40✔
523

524
    if (pStmt->sql.autoCreateTbl) {
40✔
525
      tscDebug("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
38!
526
      return TSDB_CODE_SUCCESS;
38✔
527
    }
528
  }
529

530
  if (NULL == pStmt->pCatalog) {
74✔
531
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
20!
532
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
22✔
533
  }
534

535
  if (NULL == pStmt->sql.pTableCache || taosHashGetSize(pStmt->sql.pTableCache) <= 0) {
76!
536
    if (pStmt->bInfo.inExecCache) {
23!
UNCOV
537
      pStmt->bInfo.needParse = false;
×
UNCOV
538
      tscDebug("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
×
UNCOV
539
      return TSDB_CODE_SUCCESS;
×
540
    }
541

542
    tscDebug("no stmt block cache for tb %s", pStmt->bInfo.tbFName);
23!
543
    return TSDB_CODE_SUCCESS;
23✔
544
  }
545

546
  if (pStmt->sql.autoCreateTbl) {
53✔
547
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &pStmt->bInfo.tbSuid, sizeof(pStmt->bInfo.tbSuid));
45✔
548
    if (pCache) {
45!
549
      pStmt->bInfo.needParse = false;
45✔
550
      pStmt->bInfo.tbUid = 0;
45✔
551

552
      STableDataCxt* pNewBlock = NULL;
45✔
553
      STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, 0, pStmt->bInfo.tbSuid, -1));
45!
554

555
      if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
45!
556
                      POINTER_BYTES)) {
UNCOV
557
        STMT_ERR_RET(terrno);
×
558
      }
559

560
      pStmt->exec.pCurrBlock = pNewBlock;
45✔
561

562
      tscDebug("reuse stmt block for tb %s in sqlBlock, suid:0x%" PRIx64, pStmt->bInfo.tbFName, pStmt->bInfo.tbSuid);
45!
563

564
      return TSDB_CODE_SUCCESS;
45✔
565
    }
566

UNCOV
567
    STMT_RET(stmtCleanBindInfo(pStmt));
×
568
  }
569

570
  uint64_t uid, suid;
571
  int32_t  vgId;
572
  int8_t   tableType;
573

574
  STableMeta*      pTableMeta = NULL;
8✔
575
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
8✔
576
                           .requestId = pStmt->exec.pRequest->requestId,
8✔
577
                           .requestObjRefId = pStmt->exec.pRequest->self,
8✔
578
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
8✔
579
  int32_t          code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
8✔
580

581
  pStmt->stat.ctgGetTbMetaNum++;
8✔
582

583
  if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
8!
584
    tscDebug("tb %s not exist", pStmt->bInfo.tbFName);
×
UNCOV
585
    STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
586

UNCOV
587
    STMT_ERR_RET(code);
×
588
  }
589

590
  STMT_ERR_RET(code);
8!
591

592
  uid = pTableMeta->uid;
8✔
593
  suid = pTableMeta->suid;
8✔
594
  tableType = pTableMeta->tableType;
8✔
595
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
8✔
596
  vgId = pTableMeta->vgId;
8✔
597

598
  taosMemoryFree(pTableMeta);
8!
599

600
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
8!
601

602
  if (uid == pStmt->bInfo.tbUid) {
8!
UNCOV
603
    pStmt->bInfo.needParse = false;
×
604

UNCOV
605
    tscDebug("tb %s is current table", pStmt->bInfo.tbFName);
×
606

UNCOV
607
    return TSDB_CODE_SUCCESS;
×
608
  }
609

610
  if (pStmt->bInfo.inExecCache) {
8✔
611
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
2✔
612
    if (NULL == pCache) {
2!
613
      tscError("table [%s, %" PRIx64 ", %" PRIx64 "] found in exec blockHash, but not in sql blockHash",
×
614
               pStmt->bInfo.tbFName, uid, cacheUid);
615

UNCOV
616
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
617
    }
618

619
    pStmt->bInfo.needParse = false;
2✔
620

621
    pStmt->bInfo.tbUid = uid;
2✔
622
    pStmt->bInfo.tbSuid = suid;
2✔
623
    pStmt->bInfo.tbType = tableType;
2✔
624
    pStmt->bInfo.boundTags = pCache->boundTags;
2✔
625
    pStmt->bInfo.tagsCached = true;
2✔
626

627
    tscDebug("tb %s in execBlock list, set to current", pStmt->bInfo.tbFName);
2!
628

629
    return TSDB_CODE_SUCCESS;
2✔
630
  }
631

632
  SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
6✔
633
  if (pCache) {
6!
634
    pStmt->bInfo.needParse = false;
6✔
635

636
    pStmt->bInfo.tbUid = uid;
6✔
637
    pStmt->bInfo.tbSuid = suid;
6✔
638
    pStmt->bInfo.tbType = tableType;
6✔
639
    pStmt->bInfo.boundTags = pCache->boundTags;
6✔
640
    pStmt->bInfo.tagsCached = true;
6✔
641

642
    STableDataCxt* pNewBlock = NULL;
6✔
643
    STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, uid, suid, vgId));
6!
644

645
    if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
6!
646
                    POINTER_BYTES)) {
UNCOV
647
      STMT_ERR_RET(terrno);
×
648
    }
649

650
    pStmt->exec.pCurrBlock = pNewBlock;
6✔
651

652
    tscDebug("tb %s in sqlBlock list, set to current", pStmt->bInfo.tbFName);
6!
653

654
    return TSDB_CODE_SUCCESS;
6✔
655
  }
656

UNCOV
657
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
658

UNCOV
659
  return TSDB_CODE_SUCCESS;
×
660
}
661

662
static int32_t stmtResetStmt(STscStmt2* pStmt) {
2✔
663
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
2!
664

665
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
2✔
666
  if (NULL == pStmt->sql.pTableCache) {
2!
UNCOV
667
    STMT_ERR_RET(terrno);
×
668
  }
669

670
  pStmt->sql.status = STMT_INIT;
2✔
671

672
  return TSDB_CODE_SUCCESS;
2✔
673
}
674

675
static int32_t stmtAsyncOutput(STscStmt2* pStmt, void* param) {
42✔
676
  SStmtQNode* pParam = (SStmtQNode*)param;
42✔
677

678
  if (pParam->restoreTbCols) {
42✔
679
    for (int32_t i = 0; i < pStmt->sql.siInfo.pTableColsIdx; ++i) {
42✔
680
      SArray** p = (SArray**)TARRAY_GET_ELEM(pStmt->sql.siInfo.pTableCols, i);
27✔
681
      *p = taosArrayInit(20, POINTER_BYTES);
27✔
682
      if (*p == NULL) {
27!
UNCOV
683
        STMT_ERR_RET(terrno);
×
684
      }
685
    }
686

687
    atomic_store_8((int8_t*)&pStmt->sql.siInfo.tableColsReady, true);
15✔
688
  } else {
689
    STMT_ERR_RET(qAppendStmtTableOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, &pParam->tblData, pStmt->exec.pCurrBlock,
27!
690
                                        &pStmt->sql.siInfo));
691

692
    // taosMemoryFree(pParam->pTbData);
693

694
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
27✔
695
  }
696
  return TSDB_CODE_SUCCESS;
42✔
697
}
698

699
static void* stmtBindThreadFunc(void* param) {
21✔
700
  setThreadName("stmtBind");
21✔
701

702
  qInfo("stmt bind thread started");
21!
703

704
  STscStmt2* pStmt = (STscStmt2*)param;
21✔
705

706
  while (true) {
61✔
707
    if (atomic_load_8((int8_t*)&pStmt->queue.stopQueue)) {
82✔
708
      break;
21✔
709
    }
710

711
    SStmtQNode* asyncParam = NULL;
61✔
712
    if (!stmtDequeue(pStmt, &asyncParam)) {
61✔
713
      continue;
19✔
714
    }
715

716
    if (stmtAsyncOutput(pStmt, asyncParam) != 0) {
42!
UNCOV
717
      qError("stmt async output failed");
×
718
    }
719
  }
720

721
  qInfo("stmt bind thread stopped");
21!
722

723
  return NULL;
21✔
724
}
725

726
static int32_t stmtStartBindThread(STscStmt2* pStmt) {
21✔
727
  TdThreadAttr thAttr;
728
  if (taosThreadAttrInit(&thAttr) != 0) {
21!
729
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
730
  }
731
  if (taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE) != 0) {
21!
UNCOV
732
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
733
  }
734

735
  if (taosThreadCreate(&pStmt->bindThread, &thAttr, stmtBindThreadFunc, pStmt) != 0) {
21!
UNCOV
736
    terrno = TAOS_SYSTEM_ERROR(errno);
×
UNCOV
737
    STMT_ERR_RET(terrno);
×
738
  }
739

740
  pStmt->bindThreadInUse = true;
21✔
741

742
  (void)taosThreadAttrDestroy(&thAttr);
21✔
743
  return TSDB_CODE_SUCCESS;
21✔
744
}
745

746
static int32_t stmtInitQueue(STscStmt2* pStmt) {
21✔
747
  (void)taosThreadCondInit(&pStmt->queue.waitCond, NULL);
21✔
748
  (void)taosThreadMutexInit(&pStmt->queue.mutex, NULL);
21✔
749
  STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&pStmt->queue.head));
42!
750
  pStmt->queue.tail = pStmt->queue.head;
21✔
751

752
  return TSDB_CODE_SUCCESS;
21✔
753
}
754

755
static int32_t stmtInitTableBuf(STableBufInfo* pTblBuf) {
21✔
756
  pTblBuf->buffUnit = sizeof(SStmtQNode);
21✔
757
  pTblBuf->buffSize = pTblBuf->buffUnit * 1000;
21✔
758
  pTblBuf->pBufList = taosArrayInit(100, POINTER_BYTES);
21✔
759
  if (NULL == pTblBuf->pBufList) {
21!
UNCOV
760
    return terrno;
×
761
  }
762
  void* buff = taosMemoryMalloc(pTblBuf->buffSize);
21!
763
  if (NULL == buff) {
21!
UNCOV
764
    return terrno;
×
765
  }
766

767
  if (taosArrayPush(pTblBuf->pBufList, &buff) == NULL) {
42!
UNCOV
768
    return terrno;
×
769
  }
770

771
  pTblBuf->pCurBuff = buff;
21✔
772
  pTblBuf->buffIdx = 1;
21✔
773
  pTblBuf->buffOffset = 0;
21✔
774

775
  return TSDB_CODE_SUCCESS;
21✔
776
}
777

778
TAOS_STMT2* stmtInit2(STscObj* taos, TAOS_STMT2_OPTION* pOptions) {
73✔
779
  STscObj*   pObj = (STscObj*)taos;
73✔
780
  STscStmt2* pStmt = NULL;
73✔
781
  int32_t    code = 0;
73✔
782

783
  pStmt = taosMemoryCalloc(1, sizeof(STscStmt2));
73!
784
  if (NULL == pStmt) {
73!
UNCOV
785
    return NULL;
×
786
  }
787

788
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
73✔
789
  if (NULL == pStmt->sql.pTableCache) {
73!
UNCOV
790
    taosMemoryFree(pStmt);
×
UNCOV
791
    return NULL;
×
792
  }
793

794
  pStmt->taos = pObj;
73✔
795
  pStmt->bInfo.needParse = true;
73✔
796
  pStmt->sql.status = STMT_INIT;
73✔
797
  pStmt->errCode = TSDB_CODE_SUCCESS;
73✔
798

799
  if (NULL != pOptions) {
73!
800
    (void)memcpy(&pStmt->options, pOptions, sizeof(pStmt->options));
73✔
801
    if (pOptions->singleStbInsert && pOptions->singleTableBindOnce) {
73✔
802
      pStmt->stbInterlaceMode = true;
21✔
803
    }
804

805
    pStmt->reqid = pOptions->reqid;
73✔
806
  }
807

808
  if (pStmt->stbInterlaceMode) {
73✔
809
    pStmt->sql.siInfo.transport = taos->pAppInfo->pTransporter;
21✔
810
    pStmt->sql.siInfo.acctId = taos->acctId;
21✔
811
    pStmt->sql.siInfo.dbname = taos->db;
21✔
812
    pStmt->sql.siInfo.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
21✔
813
    pStmt->sql.siInfo.pTableHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
21✔
814
    if (NULL == pStmt->sql.siInfo.pTableHash) {
21!
UNCOV
815
      (void)stmtClose(pStmt);
×
UNCOV
816
      return NULL;
×
817
    }
818
    pStmt->sql.siInfo.pTableCols = taosArrayInit(STMT_TABLE_COLS_NUM, POINTER_BYTES);
21✔
819
    if (NULL == pStmt->sql.siInfo.pTableCols) {
21!
UNCOV
820
      terrno = terrno;
×
UNCOV
821
      (void)stmtClose(pStmt);
×
UNCOV
822
      return NULL;
×
823
    }
824

825
    code = stmtInitTableBuf(&pStmt->sql.siInfo.tbBuf);
21✔
826
    if (TSDB_CODE_SUCCESS == code) {
21!
827
      code = stmtInitQueue(pStmt);
21✔
828
    }
829
    if (TSDB_CODE_SUCCESS == code) {
21!
830
      code = stmtStartBindThread(pStmt);
21✔
831
    }
832
    if (TSDB_CODE_SUCCESS != code) {
21!
UNCOV
833
      terrno = code;
×
UNCOV
834
      (void)stmtClose(pStmt);
×
UNCOV
835
      return NULL;
×
836
    }
837
  }
838

839
  pStmt->sql.siInfo.tableColsReady = true;
73✔
840
  if (pStmt->options.asyncExecFn) {
73✔
841
    if (tsem_init(&pStmt->asyncQuerySem, 0, 1) != 0) {
4!
UNCOV
842
      terrno = TAOS_SYSTEM_ERROR(errno);
×
UNCOV
843
      (void)stmtClose(pStmt);
×
UNCOV
844
      return NULL;
×
845
    }
846
  }
847
  pStmt->semWaited = false;
73✔
848

849
  STMT_LOG_SEQ(STMT_INIT);
73!
850

851
  tscDebug("stmt:%p initialized", pStmt);
73!
852

853
  return pStmt;
73✔
854
}
855

856
static int stmtSetDbName2(TAOS_STMT2* stmt, const char* dbName) {
41✔
857
  STscStmt2* pStmt = (STscStmt2*)stmt;
41✔
858

859
  STMT_DLOG("start to set dbName: %s", dbName);
41!
860

861
  pStmt->db = taosStrdup(dbName);
41!
862
  (void)strdequote(pStmt->db);
41✔
863
  STMT_ERR_RET(stmtCreateRequest(pStmt));
41!
864

865
  // The SQL statement specifies a database name, overriding the previously specified database
866
  taosMemoryFreeClear(pStmt->exec.pRequest->pDb);
41!
867
  pStmt->exec.pRequest->pDb = taosStrdup(pStmt->db);
41!
868
  if (pStmt->exec.pRequest->pDb == NULL) {
41!
UNCOV
869
    return terrno;
×
870
  }
871
  return TSDB_CODE_SUCCESS;
41✔
872
}
873

874
int stmtPrepare2(TAOS_STMT2* stmt, const char* sql, unsigned long length) {
73✔
875
  STscStmt2* pStmt = (STscStmt2*)stmt;
73✔
876

877
  STMT_DLOG_E("start to prepare");
73!
878

879
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
73✔
880
    return pStmt->errCode;
1✔
881
  }
882

883
  if (pStmt->sql.status >= STMT_PREPARE) {
72✔
884
    STMT_ERR_RET(stmtResetStmt(pStmt));
2!
885
  }
886

887
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
72!
888

889
  if (length <= 0) {
72✔
890
    length = strlen(sql);
63✔
891
  }
892

893
  pStmt->sql.sqlStr = taosStrndup(sql, length);
72!
894
  if (!pStmt->sql.sqlStr) {
72!
UNCOV
895
    return terrno;
×
896
  }
897
  pStmt->sql.sqlLen = length;
72✔
898
  pStmt->sql.stbInterlaceMode = pStmt->stbInterlaceMode;
72✔
899

900
  char* dbName = NULL;
72✔
901
  if (qParseDbName(sql, length, &dbName)) {
72✔
902
    STMT_ERR_RET(stmtSetDbName2(stmt, dbName));
41!
903
    taosMemoryFreeClear(dbName);
41!
904
  }
905

906
  return TSDB_CODE_SUCCESS;
72✔
907
}
908

909
static int32_t stmtInitStbInterlaceTableInfo(STscStmt2* pStmt) {
7✔
910
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
7✔
911
  if (!pSrc) {
7!
UNCOV
912
    return terrno;
×
913
  }
914
  STableDataCxt* pDst = NULL;
7✔
915

916
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
7!
917
  pStmt->sql.siInfo.pDataCtx = pDst;
6✔
918

919
  SArray* pTblCols = NULL;
6✔
920
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
6,369✔
921
    pTblCols = taosArrayInit(20, POINTER_BYTES);
6,350✔
922
    if (NULL == pTblCols) {
6,661!
UNCOV
923
      return terrno;
×
924
    }
925

926
    if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
13,024!
UNCOV
927
      return terrno;
×
928
    }
929
  }
930

931
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
19✔
932

933
  return TSDB_CODE_SUCCESS;
19✔
934
}
935

936
int stmtIsInsert2(TAOS_STMT2* stmt, int* insert) {
323✔
937
  STscStmt2* pStmt = (STscStmt2*)stmt;
323✔
938

939
  STMT_DLOG_E("start is insert");
323!
940

941
  if (pStmt->sql.type) {
322✔
942
    *insert = (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
252✔
943
  } else {
944
    *insert = qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
70✔
945
  }
946

947
  return TSDB_CODE_SUCCESS;
323✔
948
}
949

950
int stmtSetTbName2(TAOS_STMT2* stmt, const char* tbName) {
131✔
951
  STscStmt2* pStmt = (STscStmt2*)stmt;
131✔
952

953
  int64_t startUs = taosGetTimestampUs();
134✔
954

955
  STMT_DLOG("start to set tbName: %s", tbName);
134!
956

957
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
134!
UNCOV
958
    return pStmt->errCode;
×
959
  }
960

961
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
134!
962

963
  int32_t insert = 0;
134✔
964
  STMT_ERR_RET(stmtIsInsert2(stmt, &insert));
134!
965
  if (0 == insert) {
134!
UNCOV
966
    tscError("set tb name not available for none insert statement");
×
UNCOV
967
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
968
  }
969

970
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
134✔
971
    STMT_ERR_RET(stmtCreateRequest(pStmt));
114!
972

973
    STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
114!
974
                              pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
975
    STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
113!
976

977
    STMT_ERR_RET(stmtGetFromCache(pStmt));
114!
978

979
    if (pStmt->bInfo.needParse) {
114✔
980
      tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
23✔
981
      pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
23✔
982

983
      STMT_ERR_RET(stmtParseSql(pStmt));
23!
984
    }
985
  } else {
986
    tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
20✔
987
    pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
20✔
988
    pStmt->exec.pRequest->requestId++;
20✔
989
    pStmt->bInfo.needParse = false;
20✔
990
  }
991

992
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
134✔
993
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
7!
994
  }
995

996
  int64_t startUs2 = taosGetTimestampUs();
134✔
997
  pStmt->stat.setTbNameUs += startUs2 - startUs;
134✔
998

999
  return TSDB_CODE_SUCCESS;
134✔
1000
}
1001

1002
int stmtSetTbTags2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* tags) {
97✔
1003
  STscStmt2* pStmt = (STscStmt2*)stmt;
97✔
1004

1005
  STMT_DLOG_E("start to set tbTags");
97!
1006

1007
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
98!
UNCOV
1008
    return pStmt->errCode;
×
1009
  }
1010

1011
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
98!
1012

1013
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
97!
UNCOV
1014
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
UNCOV
1015
    pStmt->bInfo.needParse = false;
×
1016
  }
1017
  STMT_ERR_RET(stmtCreateRequest(pStmt));
97!
1018

1019
  if (pStmt->bInfo.needParse) {
97!
1020
    STMT_ERR_RET(stmtParseSql(pStmt));
×
1021
  }
1022
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
97!
UNCOV
1023
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1024
  }
1025

1026
  SBoundColInfo* tags_info = (SBoundColInfo*)pStmt->bInfo.boundTags;
97✔
1027
  // if (tags_info->numOfBound <= 0 || tags_info->numOfCols <= 0) {
1028
  //   tscWarn("no tags or cols bound in sql, will not bound tags");
1029
  //   return TSDB_CODE_SUCCESS;
1030
  // }
1031

1032
  STableDataCxt** pDataBlock =
1033
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
97✔
1034
  if (NULL == pDataBlock) {
97!
UNCOV
1035
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
UNCOV
1036
    STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1037
  }
1038

1039
  if (pStmt->bInfo.inExecCache && !pStmt->sql.autoCreateTbl) {
97!
UNCOV
1040
    return TSDB_CODE_SUCCESS;
×
1041
  }
1042

1043
  tscDebug("start to bind stmt tag values");
97!
1044
  STMT_ERR_RET(qBindStmtTagsValue2(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.tbSuid, pStmt->bInfo.stbFName,
97!
1045
                                   pStmt->bInfo.sname.tname, tags, pStmt->exec.pRequest->msgBuf,
1046
                                   pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt));
1047

1048
  return TSDB_CODE_SUCCESS;
98✔
1049
}
1050

UNCOV
1051
static int stmtFetchColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
×
UNCOV
1052
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1053
    return pStmt->errCode;
×
1054
  }
1055

UNCOV
1056
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
UNCOV
1057
    tscError("invalid operation to get query column fileds");
×
1058
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1059
  }
1060

1061
  STableDataCxt** pDataBlock = NULL;
×
1062

UNCOV
1063
  if (pStmt->sql.stbInterlaceMode) {
×
1064
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
×
1065
  } else {
1066
    pDataBlock =
1067
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
×
UNCOV
1068
    if (NULL == pDataBlock) {
×
UNCOV
1069
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
UNCOV
1070
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1071
    }
1072
  }
1073

UNCOV
1074
  STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields));
×
1075

UNCOV
1076
  return TSDB_CODE_SUCCESS;
×
1077
}
1078

1079
static int stmtFetchStbColFields2(STscStmt2* pStmt, int32_t* fieldNum, TAOS_FIELD_ALL** fields) {
22✔
1080
  int32_t    code = 0;
22✔
1081
  int32_t    preCode = pStmt->errCode;
22✔
1082

1083
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
22!
UNCOV
1084
    return pStmt->errCode;
×
1085
  }
1086

1087
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
22!
UNCOV
1088
    tscError("invalid operation to get query column fileds");
×
UNCOV
1089
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1090
  }
1091

1092
  STableDataCxt** pDataBlock = NULL;
22✔
1093

1094
  if (pStmt->sql.stbInterlaceMode) {
22!
UNCOV
1095
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
×
1096
  } else {
1097
    pDataBlock =
1098
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
22✔
1099
    if (NULL == pDataBlock) {
22!
UNCOV
1100
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
UNCOV
1101
      STMT_ERRI_JRET(TSDB_CODE_APP_ERROR);
×
1102
    }
1103
  }
1104

1105
  STMT_ERRI_JRET(qBuildStmtStbColFields(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.preCtbname, fieldNum, fields));
22!
1106
  if (pStmt->bInfo.tbType == TSDB_SUPER_TABLE) {
22✔
1107
    pStmt->bInfo.needParse = true;
15✔
1108
    qDestroyStmtDataBlock(*pDataBlock);
15✔
1109
    if (taosHashRemove(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName)) != 0) {
15!
UNCOV
1110
      tscError("get fileds %s remove exec blockHash fail", pStmt->bInfo.tbFName);
×
UNCOV
1111
      STMT_ERRI_JRET(TSDB_CODE_APP_ERROR);
×
1112
    }
1113
  }
1114

1115
_return:
22✔
1116

1117
  pStmt->errCode = preCode;
22✔
1118

1119
  return code;
22✔
1120
}
1121
/*
1122
SArray* stmtGetFreeCol(STscStmt2* pStmt, int32_t* idx) {
1123
  while (true) {
1124
    if (pStmt->exec.smInfo.pColIdx >= STMT_COL_BUF_SIZE) {
1125
      pStmt->exec.smInfo.pColIdx = 0;
1126
    }
1127

1128
    if ((pStmt->exec.smInfo.pColIdx + 1) == atomic_load_32(&pStmt->exec.smInfo.pColFreeIdx)) {
1129
      taosUsleep(1);
1130
      continue;
1131
    }
1132

1133
    *idx = pStmt->exec.smInfo.pColIdx;
1134
    return pStmt->exec.smInfo.pCols[pStmt->exec.smInfo.pColIdx++];
1135
  }
1136
}
1137
*/
1138
static int32_t stmtAppendTablePostHandle(STscStmt2* pStmt, SStmtQNode* param) {
27✔
1139
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
27✔
1140
    pStmt->sql.siInfo.pVgroupHash =
15✔
1141
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
15✔
1142
  }
1143
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
27✔
1144
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
15✔
1145
  }
1146

1147
  if (NULL == pStmt->sql.siInfo.pRequest) {
27✔
1148
    STMT_ERR_RET(buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false,
7!
1149
                              (SRequestObj**)&pStmt->sql.siInfo.pRequest, pStmt->reqid));
1150

1151
    if (pStmt->reqid != 0) {
7!
UNCOV
1152
      pStmt->reqid++;
×
1153
    }
1154
    pStmt->exec.pRequest->syncQuery = true;
7✔
1155

1156
    pStmt->sql.siInfo.requestId = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->requestId;
7✔
1157
    pStmt->sql.siInfo.requestSelf = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->self;
7✔
1158
  }
1159

1160
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
27✔
1161
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
10✔
1162
    pStmt->sql.siInfo.tbFromHash = true;
6✔
1163
  }
1164

1165
  if (0 == pStmt->sql.siInfo.firstName[0]) {
27✔
1166
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
7✔
1167
  }
1168

1169
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
27✔
1170
  param->next = NULL;
27✔
1171

1172
  (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
27✔
1173

1174
  stmtEnqueue(pStmt, param);
27✔
1175

1176
  return TSDB_CODE_SUCCESS;
27✔
1177
}
1178

1179
static FORCE_INLINE int32_t stmtGetTableColsFromCache(STscStmt2* pStmt, SArray** pTableCols) {
1180
  while (true) {
1181
    if (pStmt->sql.siInfo.pTableColsIdx < taosArrayGetSize(pStmt->sql.siInfo.pTableCols)) {
27!
1182
      *pTableCols = (SArray*)taosArrayGetP(pStmt->sql.siInfo.pTableCols, pStmt->sql.siInfo.pTableColsIdx++);
27✔
1183
      break;
27✔
1184
    } else {
1185
      SArray* pTblCols = NULL;
×
1186
      for (int32_t i = 0; i < 100; i++) {
×
UNCOV
1187
        pTblCols = taosArrayInit(20, POINTER_BYTES);
×
UNCOV
1188
        if (NULL == pTblCols) {
×
1189
          return terrno;
×
1190
        }
1191

UNCOV
1192
        if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
×
UNCOV
1193
          return terrno;
×
1194
        }
1195
      }
1196
    }
1197
  }
1198

1199
  return TSDB_CODE_SUCCESS;
27✔
1200
}
1201

1202
static int32_t stmtCacheBlock(STscStmt2* pStmt) {
114✔
1203
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
114✔
1204
    return TSDB_CODE_SUCCESS;
7✔
1205
  }
1206

1207
  uint64_t uid = pStmt->bInfo.tbUid;
107✔
1208
  uint64_t cacheUid = (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) ? pStmt->bInfo.tbSuid : uid;
107!
1209

1210
  if (taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid))) {
107✔
1211
    return TSDB_CODE_SUCCESS;
91✔
1212
  }
1213

1214
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
15✔
1215
  if (!pSrc) {
16!
UNCOV
1216
    return terrno;
×
1217
  }
1218
  STableDataCxt* pDst = NULL;
16✔
1219

1220
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
16!
1221

1222
  SStmtTableCache cache = {
15✔
1223
      .pDataCtx = pDst,
1224
      .boundTags = pStmt->bInfo.boundTags,
15✔
1225
  };
1226

1227
  if (taosHashPut(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid), &cache, sizeof(cache))) {
15!
UNCOV
1228
    return terrno;
×
1229
  }
1230

1231
  if (pStmt->sql.autoCreateTbl) {
16✔
1232
    pStmt->bInfo.tagsCached = true;
15✔
1233
  } else {
1234
    pStmt->bInfo.boundTags = NULL;
1✔
1235
  }
1236

1237
  return TSDB_CODE_SUCCESS;
16✔
1238
}
1239

1240
static int stmtAddBatch2(TAOS_STMT2* stmt) {
128✔
1241
  STscStmt2* pStmt = (STscStmt2*)stmt;
128✔
1242

1243
  int64_t startUs = taosGetTimestampUs();
128✔
1244

1245
  STMT_DLOG_E("start to add batch");
128!
1246

1247
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
128!
UNCOV
1248
    return pStmt->errCode;
×
1249
  }
1250

1251
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
128!
1252

1253
  if (pStmt->sql.stbInterlaceMode) {
127✔
1254
    int64_t startUs2 = taosGetTimestampUs();
14✔
1255
    pStmt->stat.addBatchUs += startUs2 - startUs;
14✔
1256

1257
    pStmt->sql.siInfo.tableColsReady = false;
14✔
1258

1259
    SStmtQNode* param = NULL;
14✔
1260
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
28!
1261
    param->restoreTbCols = true;
14✔
1262
    param->next = NULL;
14✔
1263

1264
    stmtEnqueue(pStmt, param);
14✔
1265

1266
    return TSDB_CODE_SUCCESS;
15✔
1267
  }
1268

1269
  STMT_ERR_RET(stmtCacheBlock(pStmt));
113!
1270

1271
  return TSDB_CODE_SUCCESS;
114✔
1272
}
1273
/*
1274
static int32_t stmtBackupQueryFields(STscStmt2* pStmt) {
1275
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
1276
  pRes->numOfCols = pStmt->exec.pRequest->body.resInfo.numOfCols;
1277
  pRes->precision = pStmt->exec.pRequest->body.resInfo.precision;
1278

1279
  int32_t size = pRes->numOfCols * sizeof(TAOS_FIELD);
1280
  pRes->fields = taosMemoryMalloc(size);
1281
  pRes->userFields = taosMemoryMalloc(size);
1282
  if (NULL == pRes->fields || NULL == pRes->userFields) {
1283
    STMT_ERR_RET(terrno);
1284
  }
1285
  (void)memcpy(pRes->fields, pStmt->exec.pRequest->body.resInfo.fields, size);
1286
  (void)memcpy(pRes->userFields, pStmt->exec.pRequest->body.resInfo.userFields, size);
1287

1288
  return TSDB_CODE_SUCCESS;
1289
}
1290

1291
static int32_t stmtRestoreQueryFields(STscStmt2* pStmt) {
1292
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
1293
  int32_t            size = pRes->numOfCols * sizeof(TAOS_FIELD);
1294

1295
  pStmt->exec.pRequest->body.resInfo.numOfCols = pRes->numOfCols;
1296
  pStmt->exec.pRequest->body.resInfo.precision = pRes->precision;
1297

1298
  if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
1299
    pStmt->exec.pRequest->body.resInfo.fields = taosMemoryMalloc(size);
1300
    if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
1301
      STMT_ERR_RET(terrno);
1302
    }
1303
    (void)memcpy(pStmt->exec.pRequest->body.resInfo.fields, pRes->fields, size);
1304
  }
1305

1306
  if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
1307
    pStmt->exec.pRequest->body.resInfo.userFields = taosMemoryMalloc(size);
1308
    if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
1309
      STMT_ERR_RET(terrno);
1310
    }
1311
    (void)memcpy(pStmt->exec.pRequest->body.resInfo.userFields, pRes->userFields, size);
1312
  }
1313

1314
  return TSDB_CODE_SUCCESS;
1315
}
1316
*/
1317
int stmtBindBatch2(TAOS_STMT2* stmt, TAOS_STMT2_BIND* bind, int32_t colIdx) {
143✔
1318
  STscStmt2* pStmt = (STscStmt2*)stmt;
143✔
1319
  int32_t    code = 0;
143✔
1320

1321
  int64_t startUs = taosGetTimestampUs();
143✔
1322

1323
  STMT_DLOG("start to bind stmt data, colIdx: %d", colIdx);
143!
1324

1325
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
143!
UNCOV
1326
    return pStmt->errCode;
×
1327
  }
1328

1329
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
143!
1330

1331
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
143!
UNCOV
1332
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
UNCOV
1333
    pStmt->bInfo.needParse = false;
×
1334
  }
1335

1336
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
143!
1337
    taos_free_result(pStmt->exec.pRequest);
1✔
1338
    pStmt->exec.pRequest = NULL;
1✔
1339
  }
1340

1341
  STMT_ERR_RET(stmtCreateRequest(pStmt));
143!
1342

1343
  if (pStmt->bInfo.needParse) {
143✔
1344
    STMT_ERR_RET(stmtParseSql(pStmt));
4!
1345
  }
1346

1347
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
144✔
1348
    STMT_ERR_RET(qStmtBindParams2(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt));
3!
1349

1350
    SParseContext ctx = {.requestId = pStmt->exec.pRequest->requestId,
3✔
1351
                         .acctId = pStmt->taos->acctId,
3✔
1352
                         .db = pStmt->exec.pRequest->pDb,
3✔
1353
                         .topicQuery = false,
1354
                         .pSql = pStmt->sql.sqlStr,
3✔
1355
                         .sqlLen = pStmt->sql.sqlLen,
3✔
1356
                         .pMsg = pStmt->exec.pRequest->msgBuf,
3✔
1357
                         .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1358
                         .pTransporter = pStmt->taos->pAppInfo->pTransporter,
3✔
1359
                         .pStmtCb = NULL,
1360
                         .pUser = pStmt->taos->user};
3✔
1361
    ctx.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
3✔
1362
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &ctx.pCatalog));
3!
1363

1364
    STMT_ERR_RET(qStmtParseQuerySql(&ctx, pStmt->sql.pQuery));
3!
1365

1366
    if (pStmt->sql.pQuery->haveResultSet) {
3!
1367
      STMT_ERR_RET(setResSchemaInfo(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->pResSchema,
3!
1368
                                    pStmt->sql.pQuery->numOfResCols));
1369
      taosMemoryFreeClear(pStmt->sql.pQuery->pResSchema);
3!
1370
      setResPrecision(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->precision);
3✔
1371
    }
1372

1373
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
3✔
1374
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
3✔
1375
    TSWAP(pStmt->exec.pRequest->targetTableList, pStmt->sql.pQuery->pTargetTableList);
3✔
1376

1377
    // if (STMT_TYPE_QUERY == pStmt->sql.queryRes) {
1378
    //   STMT_ERR_RET(stmtRestoreQueryFields(pStmt));
1379
    // }
1380

1381
    // STMT_ERR_RET(stmtBackupQueryFields(pStmt));
1382

1383
    return TSDB_CODE_SUCCESS;
3✔
1384
  }
1385

1386
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
141!
UNCOV
1387
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1388
  }
1389

1390
  STableDataCxt** pDataBlock = NULL;
140✔
1391

1392
  if (pStmt->exec.pCurrBlock) {
140✔
1393
    pDataBlock = &pStmt->exec.pCurrBlock;
117✔
1394
  } else {
1395
    pDataBlock =
1396
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
23✔
1397
    if (NULL == pDataBlock) {
24!
UNCOV
1398
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
UNCOV
1399
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1400
    }
1401
    pStmt->exec.pCurrBlock = *pDataBlock;
24✔
1402
    if (pStmt->sql.stbInterlaceMode) {
24✔
1403
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
7✔
1404
      pStmt->exec.pCurrBlock->pData->aCol = NULL;
7✔
1405
    }
1406
  }
1407

1408
  int64_t startUs2 = taosGetTimestampUs();
141✔
1409
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
141✔
1410

1411
  SStmtQNode* param = NULL;
141✔
1412
  if (pStmt->sql.stbInterlaceMode) {
141✔
1413
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
54!
1414
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
54!
1415
    taosArrayClear(param->tblData.aCol);
27✔
1416

1417
    // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES);
1418

1419
    param->restoreTbCols = false;
27✔
1420
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
27✔
1421
  }
1422

1423
  int64_t startUs3 = taosGetTimestampUs();
141✔
1424
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
141✔
1425

1426
  SArray* pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
141✔
1427

1428
  if (colIdx < 0) {
141✔
1429
    if (pStmt->sql.stbInterlaceMode) {
135✔
1430
      (*pDataBlock)->pData->flags = 0;
27✔
1431
      code = qBindStmtStbColsValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
27✔
1432
                                    pStmt->exec.pRequest->msgBufLen, &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt);
27✔
1433
    } else {
1434
      code =
1435
          qBindStmtColsValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt);
108✔
1436
    }
1437

1438
    if (code) {
136✔
1439
      tscError("qBindStmtColsValue failed, error:%s", tstrerror(code));
1!
1440
      STMT_ERR_RET(code);
1!
1441
    }
1442
  } else {
1443
    if (pStmt->sql.stbInterlaceMode) {
6!
UNCOV
1444
      tscError("bind single column not allowed in stb insert mode");
×
1445
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1446
    }
1447

1448
    if (colIdx != (pStmt->bInfo.sBindLastIdx + 1) && colIdx != 0) {
6!
UNCOV
1449
      tscError("bind column index not in sequence");
×
1450
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1451
    }
1452

1453
    pStmt->bInfo.sBindLastIdx = colIdx;
6✔
1454

1455
    if (0 == colIdx) {
6✔
1456
      pStmt->bInfo.sBindRowNum = bind->num;
3✔
1457
    }
1458

1459
    code = qBindStmtSingleColValue2(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
6✔
1460
                                    pStmt->exec.pRequest->msgBufLen, colIdx, pStmt->bInfo.sBindRowNum, pStmt->taos->optionInfo.charsetCxt);
6✔
1461
    if (code) {
6!
UNCOV
1462
      tscError("qBindStmtSingleColValue failed, error:%s", tstrerror(code));
×
UNCOV
1463
      STMT_ERR_RET(code);
×
1464
    }
1465
  }
1466

1467
  int64_t startUs4 = taosGetTimestampUs();
141✔
1468
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
141✔
1469

1470
  if (pStmt->sql.stbInterlaceMode) {
141✔
1471
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
27!
1472
  } else {
1473
    STMT_ERR_RET(stmtAddBatch2(pStmt));
114!
1474
  }
1475

1476
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
141✔
1477

1478
  return TSDB_CODE_SUCCESS;
141✔
1479
}
1480
/*
1481
int stmtUpdateTableUid(STscStmt2* pStmt, SSubmitRsp* pRsp) {
1482
  tscDebug("stmt start to update tbUid, blockNum: %d", pRsp->nBlocks);
1483

1484
  int32_t code = 0;
1485
  int32_t finalCode = 0;
1486
  size_t  keyLen = 0;
1487
  void*   pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
1488
  while (pIter) {
1489
    STableDataCxt* pBlock = *(STableDataCxt**)pIter;
1490
    char*          key = taosHashGetKey(pIter, &keyLen);
1491

1492
    STableMeta* pMeta = qGetTableMetaInDataBlock(pBlock);
1493
    if (pMeta->uid) {
1494
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1495
      continue;
1496
    }
1497

1498
    SSubmitBlkRsp* blkRsp = NULL;
1499
    int32_t        i = 0;
1500
    for (; i < pRsp->nBlocks; ++i) {
1501
      blkRsp = pRsp->pBlocks + i;
1502
      if (strlen(blkRsp->tblFName) != keyLen) {
1503
        continue;
1504
      }
1505

1506
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
1507
        continue;
1508
      }
1509

1510
      break;
1511
    }
1512

1513
    if (i < pRsp->nBlocks) {
1514
      tscDebug("auto created table %s uid updated from %" PRIx64 " to %" PRIx64, blkRsp->tblFName, pMeta->uid,
1515
               blkRsp->uid);
1516

1517
      pMeta->uid = blkRsp->uid;
1518
      pStmt->bInfo.tbUid = blkRsp->uid;
1519
    } else {
1520
      tscDebug("table %s not found in submit rsp, will update from catalog", pStmt->bInfo.tbFName);
1521
      if (NULL == pStmt->pCatalog) {
1522
        code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog);
1523
        if (code) {
1524
          pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1525
          finalCode = code;
1526
          continue;
1527
        }
1528
      }
1529

1530
      code = stmtCreateRequest(pStmt);
1531
      if (code) {
1532
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1533
        finalCode = code;
1534
        continue;
1535
      }
1536

1537
      STableMeta*      pTableMeta = NULL;
1538
      SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
1539
                               .requestId = pStmt->exec.pRequest->requestId,
1540
                               .requestObjRefId = pStmt->exec.pRequest->self,
1541
                               .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
1542
      code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
1543

1544
      pStmt->stat.ctgGetTbMetaNum++;
1545

1546
      taos_free_result(pStmt->exec.pRequest);
1547
      pStmt->exec.pRequest = NULL;
1548

1549
      if (code || NULL == pTableMeta) {
1550
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1551
        finalCode = code;
1552
        taosMemoryFree(pTableMeta);
1553
        continue;
1554
      }
1555

1556
      pMeta->uid = pTableMeta->uid;
1557
      pStmt->bInfo.tbUid = pTableMeta->uid;
1558
      taosMemoryFree(pTableMeta);
1559
    }
1560

1561
    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1562
  }
1563

1564
  return finalCode;
1565
}
1566
*/
1567
/*
1568
int stmtStaticModeExec(TAOS_STMT* stmt) {
1569
  STscStmt2*   pStmt = (STscStmt2*)stmt;
1570
  int32_t     code = 0;
1571
  SSubmitRsp* pRsp = NULL;
1572
  if (pStmt->sql.staticMode) {
1573
    return TSDB_CODE_TSC_STMT_API_ERROR;
1574
  }
1575

1576
  STMT_DLOG_E("start to exec");
1577

1578
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
1579

1580
  STMT_ERR_RET(qBuildStmtOutputFromTbList(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pTbBlkList,
1581
pStmt->exec.pCurrBlock, pStmt->exec.tbBlkNum));
1582

1583
  launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
1584

1585
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
1586
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
1587
    if (code) {
1588
      pStmt->exec.pRequest->code = code;
1589
    } else {
1590
      tFreeSSubmitRsp(pRsp);
1591
      STMT_ERR_RET(stmtResetStmt(pStmt));
1592
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
1593
    }
1594
  }
1595

1596
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
1597

1598
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
1599
  pStmt->affectedRows += pStmt->exec.affectedRows;
1600

1601
_return:
1602

1603
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
1604

1605
  tFreeSSubmitRsp(pRsp);
1606

1607
  ++pStmt->sql.runTimes;
1608

1609
  STMT_RET(code);
1610
}
1611
*/
1612

1613
static int32_t createParseContext(const SRequestObj* pRequest, SParseContext** pCxt, SSqlCallbackWrapper* pWrapper) {
9✔
1614
  const STscObj* pTscObj = pRequest->pTscObj;
9✔
1615

1616
  *pCxt = taosMemoryCalloc(1, sizeof(SParseContext));
9!
1617
  if (*pCxt == NULL) {
9!
UNCOV
1618
    return terrno;
×
1619
  }
1620

1621
  **pCxt = (SParseContext){.requestId = pRequest->requestId,
9✔
1622
                           .requestRid = pRequest->self,
9✔
1623
                           .acctId = pTscObj->acctId,
9✔
1624
                           .db = pRequest->pDb,
9✔
1625
                           .topicQuery = false,
1626
                           .pSql = pRequest->sqlstr,
9✔
1627
                           .sqlLen = pRequest->sqlLen,
9✔
1628
                           .pMsg = pRequest->msgBuf,
9✔
1629
                           .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1630
                           .pTransporter = pTscObj->pAppInfo->pTransporter,
9✔
1631
                           .pStmtCb = NULL,
1632
                           .pUser = pTscObj->user,
9✔
1633
                           .pEffectiveUser = pRequest->effectiveUser,
9✔
1634
                           .isSuperUser = (0 == strcmp(pTscObj->user, TSDB_DEFAULT_USER)),
9✔
1635
                           .enableSysInfo = pTscObj->sysInfo,
9✔
1636
                           .async = true,
1637
                           .svrVer = pTscObj->sVer,
9✔
1638
                           .nodeOffline = (pTscObj->pAppInfo->onlineDnodes < pTscObj->pAppInfo->totalDnodes),
9✔
1639
                           .allocatorId = pRequest->allocatorRefId,
9✔
1640
                           .parseSqlFp = clientParseSql,
1641
                           .parseSqlParam = pWrapper};
1642
  int8_t biMode = atomic_load_8(&((STscObj*)pTscObj)->biMode);
9✔
1643
  (*pCxt)->biMode = biMode;
9✔
1644
  return TSDB_CODE_SUCCESS;
9✔
1645
}
1646

1647
static void asyncQueryCb(void* userdata, TAOS_RES* res, int code) {
9✔
1648
  STscStmt2*        pStmt = userdata;
9✔
1649
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
9✔
1650

1651
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
9✔
1652
  pStmt->affectedRows += pStmt->exec.affectedRows;
9✔
1653

1654
  fp(pStmt->options.userdata, res, code);
9✔
1655

1656
  while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
9!
UNCOV
1657
    taosUsleep(1);
×
1658
  }
1659
  (void)stmtCleanExecInfo(pStmt, (code ? false : true), false);
9✔
1660
  ++pStmt->sql.runTimes;
9✔
1661

1662
  if (tsem_post(&pStmt->asyncQuerySem) != 0) {
9!
UNCOV
1663
    tscError("failed to post asyncQuerySem");
×
1664
  }
1665
}
9✔
1666

1667
int stmtExec2(TAOS_STMT2* stmt, int* affected_rows) {
79✔
1668
  STscStmt2* pStmt = (STscStmt2*)stmt;
79✔
1669
  int32_t    code = 0;
79✔
1670
  int64_t    startUs = taosGetTimestampUs();
79✔
1671

1672
  STMT_DLOG_E("start to exec");
79!
1673

1674
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
79!
UNCOV
1675
    return pStmt->errCode;
×
1676
  }
1677

1678
  if (pStmt->sql.stbInterlaceMode) {
79✔
1679
    STMT_ERR_RET(stmtAddBatch2(pStmt));
15!
1680
  }
1681

1682
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
79✔
1683

1684
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
78✔
1685
    if (pStmt->sql.stbInterlaceMode) {
75✔
1686
      int64_t startTs = taosGetTimestampUs();
15✔
1687
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
43✔
1688
        taosUsleep(1);
28✔
1689
      }
1690
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
15✔
1691

1692
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
15!
1693
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
15✔
1694
      pStmt->sql.siInfo.pVgroupHash = NULL;
15✔
1695
      pStmt->sql.siInfo.pVgroupList = NULL;
15✔
1696
    } else {
1697
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
60✔
1698
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
60!
1699

1700
      STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
60!
1701

1702
      STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
59!
1703
    }
1704
  }
1705

1706
  SRequestObj*      pRequest = pStmt->exec.pRequest;
78✔
1707
  __taos_async_fn_t fp = pStmt->options.asyncExecFn;
78✔
1708

1709
  if (!fp) {
78✔
1710
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
69✔
1711

1712
    if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
69!
UNCOV
1713
      code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
×
1714
      if (code) {
×
1715
        pStmt->exec.pRequest->code = code;
×
1716
      } else {
UNCOV
1717
        STMT_ERR_RET(stmtResetStmt(pStmt));
×
UNCOV
1718
        STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
×
1719
      }
1720
    }
1721

1722
    STMT_ERR_JRET(pStmt->exec.pRequest->code);
69!
1723

1724
    pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
69✔
1725
    if (affected_rows) {
69✔
1726
      *affected_rows = pStmt->exec.affectedRows;
66✔
1727
    }
1728
    pStmt->affectedRows += pStmt->exec.affectedRows;
69✔
1729

1730
    while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
69!
UNCOV
1731
      taosUsleep(1);
×
1732
    }
1733

1734
    STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
69!
1735

1736
    ++pStmt->sql.runTimes;
69✔
1737
  } else {
1738
    SSqlCallbackWrapper* pWrapper = taosMemoryCalloc(1, sizeof(SSqlCallbackWrapper));
9!
1739
    if (pWrapper == NULL) {
9!
UNCOV
1740
      code = terrno;
×
1741
    } else {
1742
      pWrapper->pRequest = pRequest;
9✔
1743
      pRequest->pWrapper = pWrapper;
9✔
1744
    }
1745
    if (TSDB_CODE_SUCCESS == code) {
9!
1746
      code = createParseContext(pRequest, &pWrapper->pParseCtx, pWrapper);
9✔
1747
    }
1748
    pRequest->syncQuery = false;
9✔
1749
    pRequest->body.queryFp = asyncQueryCb;
9✔
1750
    ((SSyncQueryParam*)(pRequest)->body.interParam)->userParam = pStmt;
9✔
1751

1752
    pStmt->semWaited = false;
9✔
1753
    launchAsyncQuery(pRequest, pStmt->sql.pQuery, NULL, pWrapper);
9✔
1754
  }
1755

1756
_return:
78✔
1757
  pStmt->stat.execUseUs += taosGetTimestampUs() - startUs;
78✔
1758

1759
  STMT_RET(code);
78!
1760
}
1761

1762
int stmtClose2(TAOS_STMT2* stmt) {
72✔
1763
  STscStmt2* pStmt = (STscStmt2*)stmt;
72✔
1764

1765
  STMT_DLOG_E("start to free stmt");
72!
1766

1767
  pStmt->queue.stopQueue = true;
72✔
1768

1769
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
72✔
1770
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
72✔
1771
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
72✔
1772

1773
  if (pStmt->bindThreadInUse) {
72✔
1774
    (void)taosThreadJoin(pStmt->bindThread, NULL);
21✔
1775
    pStmt->bindThreadInUse = false;
21✔
1776
  }
1777

1778
  (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
72✔
1779
  (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
72✔
1780

1781
  if (pStmt->options.asyncExecFn && !pStmt->semWaited) {
72!
1782
    if (tsem_wait(&pStmt->asyncQuerySem) != 0) {
4!
UNCOV
1783
      tscError("failed to wait asyncQuerySem");
×
1784
    }
1785
  }
1786

1787
  STMT_DLOG("stmt %p closed, stbInterlaceMode: %d, statInfo: ctgGetTbMetaNum=>%" PRId64 ", getCacheTbInfo=>%" PRId64
72!
1788
            ", parseSqlNum=>%" PRId64 ", pStmt->stat.bindDataNum=>%" PRId64
1789
            ", settbnameAPI:%u, bindAPI:%u, addbatchAPI:%u, execAPI:%u"
1790
            ", setTbNameUs:%" PRId64 ", bindDataUs:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64 " addBatchUs:%" PRId64
1791
            ", execWaitUs:%" PRId64 ", execUseUs:%" PRId64,
1792
            pStmt, pStmt->sql.stbInterlaceMode, pStmt->stat.ctgGetTbMetaNum, pStmt->stat.getCacheTbInfo,
1793
            pStmt->stat.parseSqlNum, pStmt->stat.bindDataNum, pStmt->seqIds[STMT_SETTBNAME], pStmt->seqIds[STMT_BIND],
1794
            pStmt->seqIds[STMT_ADD_BATCH], pStmt->seqIds[STMT_EXECUTE], pStmt->stat.setTbNameUs,
1795
            pStmt->stat.bindDataUs1, pStmt->stat.bindDataUs2, pStmt->stat.bindDataUs3, pStmt->stat.bindDataUs4,
1796
            pStmt->stat.addBatchUs, pStmt->stat.execWaitUs, pStmt->stat.execUseUs);
1797

1798
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
72!
1799

1800
  if (pStmt->options.asyncExecFn) {
72✔
1801
    if (tsem_destroy(&pStmt->asyncQuerySem) != 0) {
4!
UNCOV
1802
      tscError("failed to destroy asyncQuerySem");
×
1803
    }
1804
  }
1805
  taosMemoryFree(stmt);
72!
1806

1807
  return TSDB_CODE_SUCCESS;
72✔
1808
}
1809

1810
const char* stmtErrstr2(TAOS_STMT2* stmt) {
3✔
1811
  STscStmt2* pStmt = (STscStmt2*)stmt;
3✔
1812

1813
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
3!
1814
    return (char*)tstrerror(terrno);
3✔
1815
  }
1816

UNCOV
1817
  pStmt->exec.pRequest->code = terrno;
×
1818

UNCOV
1819
  return taos_errstr(pStmt->exec.pRequest);
×
1820
}
1821
/*
1822
int stmtAffectedRows(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->affectedRows; }
1823

1824
int stmtAffectedRowsOnce(TAOS_STMT* stmt) { return ((STscStmt2*)stmt)->exec.affectedRows; }
1825
*/
1826

1827
int stmtParseColFields2(TAOS_STMT2* stmt) {
34✔
1828
  int32_t    code = 0;
34✔
1829
  STscStmt2* pStmt = (STscStmt2*)stmt;
34✔
1830
  int32_t    preCode = pStmt->errCode;
34✔
1831

1832
  STMT_DLOG_E("start to get col fields");
34!
1833

1834
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
34!
UNCOV
1835
    return pStmt->errCode;
×
1836
  }
1837

1838
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
34!
UNCOV
1839
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1840
  }
1841

1842
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
34!
1843

1844
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
34!
UNCOV
1845
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
UNCOV
1846
    pStmt->bInfo.needParse = false;
×
1847
  }
1848

1849
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
34!
UNCOV
1850
    taos_free_result(pStmt->exec.pRequest);
×
UNCOV
1851
    pStmt->exec.pRequest = NULL;
×
UNCOV
1852
    STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
1853
  }
1854

1855
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
34!
1856

1857
  if (pStmt->bInfo.needParse) {
34!
1858
    STMT_ERRI_JRET(stmtParseSql(pStmt));
34✔
1859
  }
1860

1861
_return:
22✔
1862

1863
  pStmt->errCode = preCode;
34✔
1864

1865
  return code;
34✔
1866
}
1867

1868
int stmtGetStbColFields2(TAOS_STMT2* stmt, int* nums, TAOS_FIELD_ALL** fields) {
34✔
1869
  int32_t code = stmtParseColFields2(stmt);
34✔
1870
  if (code != TSDB_CODE_SUCCESS) {
34✔
1871
    return code;
12✔
1872
  }
1873

1874
  return stmtFetchStbColFields2(stmt, nums, fields);
22✔
1875
}
1876

1877
int stmtGetParamNum2(TAOS_STMT2* stmt, int* nums) {
9✔
1878
  int32_t    code = 0;
9✔
1879
  STscStmt2* pStmt = (STscStmt2*)stmt;
9✔
1880
  int32_t    preCode = pStmt->errCode;
9✔
1881

1882
  STMT_DLOG_E("start to get param num");
9!
1883

1884
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
9!
UNCOV
1885
    return pStmt->errCode;
×
1886
  }
1887

1888
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
9!
1889

1890
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
9!
UNCOV
1891
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
UNCOV
1892
    pStmt->bInfo.needParse = false;
×
1893
  }
1894

1895
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
9!
UNCOV
1896
    taos_free_result(pStmt->exec.pRequest);
×
UNCOV
1897
    pStmt->exec.pRequest = NULL;
×
1898
  }
1899

1900
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
9!
1901

1902
  if (pStmt->bInfo.needParse) {
9!
1903
    STMT_ERRI_JRET(stmtParseSql(pStmt));
9✔
1904
  }
1905

1906
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
2!
1907
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
2✔
1908
  } else {
1909
    STMT_ERRI_JRET(stmtFetchColFields2(stmt, nums, NULL));
×
1910
  }
1911

UNCOV
1912
_return:
×
1913

1914
  pStmt->errCode = preCode;
9✔
1915

1916
  return code;
9✔
1917
}
1918

1919
TAOS_RES* stmtUseResult2(TAOS_STMT2* stmt) {
3✔
1920
  STscStmt2* pStmt = (STscStmt2*)stmt;
3✔
1921

1922
  STMT_DLOG_E("start to use result");
3!
1923

1924
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
3!
UNCOV
1925
    tscError("useResult only for query statement");
×
UNCOV
1926
    return NULL;
×
1927
  }
1928

1929
  return pStmt->exec.pRequest;
3✔
1930
}
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