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

taosdata / TDengine / #3632

08 Mar 2025 06:17AM UTC coverage: 60.719% (+0.05%) from 60.671%
#3632

push

travis-ci

web-flow
Merge pull request #29999 from taosdata/enh/TS-5089

feat: taosBenchmark supports exporting to CSV files

141890 of 300701 branches covered (47.19%)

Branch coverage included in aggregate %.

599 of 766 new or added lines in 3 files covered. (78.2%)

1025 existing lines in 124 files now uncovered.

223757 of 301490 relevant lines covered (74.22%)

17284906.68 hits per line

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

66.46
/source/client/src/clientStmt.c
1

2
#include "clientInt.h"
3
#include "clientLog.h"
4
#include "tdef.h"
5

6
#include "clientStmt.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) {
41,518✔
13
    *pBuf = (char*)pTblBuf->pCurBuff + pTblBuf->buffOffset;
41,517✔
14
    pTblBuf->buffOffset += pTblBuf->buffUnit;
41,517✔
15
  } else if (pTblBuf->buffIdx < taosArrayGetSize(pTblBuf->pBufList)) {
1!
16
    pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, pTblBuf->buffIdx++);
×
17
    if (NULL == pTblBuf->pCurBuff) {
×
18
      return TAOS_GET_TERRNO(terrno);
×
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;
41,517✔
39
}
40

41
bool stmtDequeue(STscStmt* pStmt, SStmtQNode** param) {
41,685✔
42
  int i = 0;
41,685✔
43
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
69,962✔
44
    if (i < 10) {
28,305✔
45
      taosUsleep(1);
26,239✔
46
      i++;
26,212✔
47
    } else {
48
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
2,066✔
49
      if (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
2,066!
50
        (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
2,066✔
51
      }
52
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
2,065✔
53
    }
54
  }
55
  if (pStmt->queue.stopQueue) {
41,516✔
56
    return false;
54✔
57
  }
58
  SStmtQNode* orig = pStmt->queue.head;
41,462✔
59
  SStmtQNode* node = pStmt->queue.head->next;
41,462✔
60
  pStmt->queue.head = pStmt->queue.head->next;
41,462✔
61
  *param = node;
41,462✔
62

63
  (void)atomic_sub_fetch_64((int64_t*)&pStmt->queue.qRemainNum, 1);
41,462✔
64

65
  return true;
41,624✔
66
}
67

68
void stmtEnqueue(STscStmt* pStmt, SStmtQNode* param) {
41,598✔
69
  pStmt->queue.tail->next = param;
41,598✔
70
  pStmt->queue.tail = param;
41,598✔
71

72
  pStmt->stat.bindDataNum++;
41,598✔
73

74
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
41,598✔
75
  (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
41,619✔
76
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
41,606✔
77
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
41,585✔
78
}
41,611✔
79

80
static int32_t stmtCreateRequest(STscStmt* pStmt) {
5,004,619✔
81
  int32_t code = 0;
5,004,619✔
82

83
  if (pStmt->exec.pRequest == NULL) {
5,004,619✔
84
    code = buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false, &pStmt->exec.pRequest,
113,614✔
85
                        pStmt->reqid);
86
    if (pStmt->reqid != 0) {
113,614!
87
      pStmt->reqid++;
×
88
    }
89
    if (TSDB_CODE_SUCCESS == code) {
113,614!
90
      pStmt->exec.pRequest->syncQuery = true;
113,614✔
91
      pStmt->exec.pRequest->isStmtBind = true;
113,614✔
92
    }
93
  }
94

95
  return code;
5,004,619✔
96
}
97

98
int32_t stmtSwitchStatus(STscStmt* pStmt, STMT_STATUS newStatus) {
6,400,298✔
99
  int32_t code = 0;
6,400,298✔
100

101
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
6,400,298!
102
    STMT_LOG_SEQ(newStatus);
6,403,214✔
103
  }
104

105
  if (pStmt->errCode && newStatus != STMT_PREPARE) {
6,452,929!
106
    STMT_DLOG("stmt already failed with err:%s", tstrerror(pStmt->errCode));
×
107
    return pStmt->errCode;
×
108
  }
109

110
  switch (newStatus) {
6,452,929!
111
    case STMT_PREPARE:
1,133✔
112
      pStmt->errCode = 0;
1,133✔
113
      break;
1,133✔
114
    case STMT_SETTBNAME:
126,818✔
115
      if (STMT_STATUS_EQ(INIT)) {
126,818!
116
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
117
      }
118
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
126,818!
119
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
120
      }
121
      break;
126,818✔
122
    case STMT_SETTAGS:
40,122✔
123
      if (STMT_STATUS_NE(SETTBNAME) && STMT_STATUS_NE(FETCH_FIELDS)) {
40,122!
124
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
125
      }
126
      break;
40,122✔
127
    case STMT_FETCH_FIELDS:
2,614,580✔
128
      if (STMT_STATUS_EQ(INIT)) {
2,614,580!
129
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
130
      }
131
      break;
2,614,580✔
132
    case STMT_BIND:
2,299,415✔
133
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
2,299,415!
134
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
135
      }
136
      /*
137
            if ((pStmt->sql.type == STMT_TYPE_MULTI_INSERT) && ()) {
138
              code = TSDB_CODE_TSC_STMT_API_ERROR;
139
            }
140
      */
141
      break;
2,299,415✔
142
    case STMT_BIND_COL:
×
143
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND)) {
×
144
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
145
      }
146
      break;
×
147
    case STMT_ADD_BATCH:
1,255,396✔
148
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
1,255,396!
149
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
150
      }
151
      break;
1,255,396✔
152
    case STMT_EXECUTE:
115,465✔
153
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
115,465✔
154
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
21,801!
155
            STMT_STATUS_NE(BIND_COL)) {
×
156
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
157
        }
158
      } else {
159
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS)) {
93,664!
160
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
161
        }
162
      }
163
      break;
115,465✔
164
    default:
×
165
      code = TSDB_CODE_APP_ERROR;
×
166
      break;
×
167
  }
168

169
  STMT_ERR_RET(code);
6,452,929!
170

171
  pStmt->sql.status = newStatus;
6,452,929✔
172

173
  return TSDB_CODE_SUCCESS;
6,452,929✔
174
}
175

176
int32_t stmtGetTbName(TAOS_STMT* stmt, char** tbName) {
3,917✔
177
  STscStmt* pStmt = (STscStmt*)stmt;
3,917✔
178

179
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
3,917✔
180

181
  if ('\0' == pStmt->bInfo.tbName[0]) {
3,917!
182
    tscError("no table name set");
×
183
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_TBNAME_ERROR);
×
184
  }
185

186
  *tbName = pStmt->bInfo.tbName;
3,916✔
187

188
  return TSDB_CODE_SUCCESS;
3,916✔
189
}
190
/*
191
int32_t stmtBackupQueryFields(STscStmt* pStmt) {
192
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
193
  pRes->numOfCols = pStmt->exec.pRequest->body.resInfo.numOfCols;
194
  pRes->precision = pStmt->exec.pRequest->body.resInfo.precision;
195

196
  int32_t size = pRes->numOfCols * sizeof(TAOS_FIELD);
197
  pRes->fields = taosMemoryMalloc(size);
198
  if (pRes->fields == NULL) {
199
    STMT_ERR_RET(terrno);
200
  }
201

202
  pRes->userFields = taosMemoryMalloc(size);
203
  if (pRes->userFields == NULL) {
204
    taosMemoryFreeClear(pRes->fields);
205
    STMT_ERR_RET(terrno);
206
  }
207

208
  (void)memcpy(pRes->fields, pStmt->exec.pRequest->body.resInfo.fields, size);
209
  (void)memcpy(pRes->userFields, pStmt->exec.pRequest->body.resInfo.userFields, size);
210

211
  return TSDB_CODE_SUCCESS;
212
}
213

214
int32_t stmtRestoreQueryFields(STscStmt* pStmt) {
215
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
216
  int32_t            size = pRes->numOfCols * sizeof(TAOS_FIELD);
217

218
  pStmt->exec.pRequest->body.resInfo.numOfCols = pRes->numOfCols;
219
  pStmt->exec.pRequest->body.resInfo.precision = pRes->precision;
220

221
  if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
222
    pStmt->exec.pRequest->body.resInfo.fields = taosMemoryMalloc(size);
223
    if (NULL == pStmt->exec.pRequest->body.resInfo.fields) {
224
      STMT_ERR_RET(terrno);
225
    }
226
    (void)memcpy(pStmt->exec.pRequest->body.resInfo.fields, pRes->fields, size);
227
  }
228

229
  if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
230
    pStmt->exec.pRequest->body.resInfo.userFields = taosMemoryMalloc(size);
231
    if (NULL == pStmt->exec.pRequest->body.resInfo.userFields) {
232
      STMT_ERR_RET(terrno);
233
    }
234
    (void)memcpy(pStmt->exec.pRequest->body.resInfo.userFields, pRes->userFields, size);
235
  }
236

237
  return TSDB_CODE_SUCCESS;
238
}
239
*/
240
int32_t stmtUpdateBindInfo(TAOS_STMT* stmt, STableMeta* pTableMeta, void* tags, SName* tbName, const char* sTableName,
3,914✔
241
                           bool autoCreateTbl) {
242
  STscStmt* pStmt = (STscStmt*)stmt;
3,914✔
243
  char      tbFName[TSDB_TABLE_FNAME_LEN];
244
  int32_t   code = tNameExtractFullName(tbName, tbFName);
3,914✔
245
  if (code != 0) {
3,917!
246
    return code;
×
247
  }
248

249
  (void)memcpy(&pStmt->bInfo.sname, tbName, sizeof(*tbName));
3,917✔
250
  tstrncpy(pStmt->bInfo.tbFName, tbFName, TSDB_TABLE_FNAME_LEN);
3,917✔
251
  pStmt->bInfo.tbFName[sizeof(pStmt->bInfo.tbFName) - 1] = 0;
3,917✔
252

253
  pStmt->bInfo.tbUid = autoCreateTbl ? 0 : pTableMeta->uid;
3,917✔
254
  pStmt->bInfo.tbSuid = pTableMeta->suid;
3,917✔
255
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
3,917✔
256
  pStmt->bInfo.tbType = pTableMeta->tableType;
3,917✔
257
  pStmt->bInfo.boundTags = tags;
3,917✔
258
  pStmt->bInfo.tagsCached = false;
3,917✔
259
  tstrncpy(pStmt->bInfo.stbFName, sTableName, sizeof(pStmt->bInfo.stbFName));
3,917✔
260

261
  return TSDB_CODE_SUCCESS;
3,917✔
262
}
263

264
int32_t stmtUpdateExecInfo(TAOS_STMT* stmt, SHashObj* pVgHash, SHashObj* pBlockHash) {
3,913✔
265
  STscStmt* pStmt = (STscStmt*)stmt;
3,913✔
266

267
  pStmt->sql.pVgHash = pVgHash;
3,913✔
268
  pStmt->exec.pBlockHash = pBlockHash;
3,913✔
269

270
  return TSDB_CODE_SUCCESS;
3,913✔
271
}
272

273
int32_t stmtUpdateInfo(TAOS_STMT* stmt, STableMeta* pTableMeta, void* tags, SName* tbName, bool autoCreateTbl,
3,916✔
274
                       SHashObj* pVgHash, SHashObj* pBlockHash, const char* sTableName, bool preCtbname) {
275
  STscStmt* pStmt = (STscStmt*)stmt;
3,916✔
276

277
  STMT_ERR_RET(stmtUpdateBindInfo(stmt, pTableMeta, tags, tbName, sTableName, autoCreateTbl));
3,916!
278
  STMT_ERR_RET(stmtUpdateExecInfo(stmt, pVgHash, pBlockHash));
3,915!
279

280
  pStmt->sql.autoCreateTbl = autoCreateTbl;
3,913✔
281
  if (pStmt->sql.autoCreateTbl) {
3,913✔
282
    pStmt->sql.stbInterlaceMode = false;
198✔
283
  }
284

285
  return TSDB_CODE_SUCCESS;
3,913✔
286
}
287

288
int32_t stmtGetExecInfo(TAOS_STMT* stmt, SHashObj** pVgHash, SHashObj** pBlockHash) {
2,997✔
289
  STscStmt* pStmt = (STscStmt*)stmt;
2,997✔
290

291
  *pVgHash = pStmt->sql.pVgHash;
2,997✔
292
  pStmt->sql.pVgHash = NULL;
2,997✔
293

294
  *pBlockHash = pStmt->exec.pBlockHash;
2,997✔
295
  pStmt->exec.pBlockHash = NULL;
2,997✔
296

297
  return TSDB_CODE_SUCCESS;
2,997✔
298
}
299

300
int32_t stmtCacheBlock(STscStmt* pStmt) {
1,250,531✔
301
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
1,250,531✔
302
    return TSDB_CODE_SUCCESS;
11,138✔
303
  }
304

305
  uint64_t uid = pStmt->bInfo.tbUid;
1,239,393✔
306
  uint64_t cacheUid = (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) ? pStmt->bInfo.tbSuid : uid;
1,239,393✔
307

308
  if (taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid))) {
1,239,393✔
309
    return TSDB_CODE_SUCCESS;
1,237,311✔
310
  }
311

312
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
3,476✔
313
  if (!pSrc) {
3,846!
314
    return terrno;
×
315
  }
316
  STableDataCxt* pDst = NULL;
3,846✔
317

318
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
3,846!
319

320
  SStmtTableCache cache = {
3,846✔
321
      .pDataCtx = pDst,
322
      .boundTags = pStmt->bInfo.boundTags,
3,846✔
323
  };
324

325
  if (taosHashPut(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid), &cache, sizeof(cache))) {
3,846!
326
    return terrno;
×
327
  }
328

329
  if (pStmt->sql.autoCreateTbl) {
3,846✔
330
    pStmt->bInfo.tagsCached = true;
198✔
331
  } else {
332
    pStmt->bInfo.boundTags = NULL;
3,648✔
333
  }
334

335
  return TSDB_CODE_SUCCESS;
3,846✔
336
}
337

338
int32_t stmtParseSql(STscStmt* pStmt) {
4,128✔
339
  pStmt->exec.pCurrBlock = NULL;
4,128✔
340

341
  SStmtCallback stmtCb = {
4,128✔
342
      .pStmt = pStmt,
343
      .getTbNameFn = stmtGetTbName,
344
      .setInfoFn = stmtUpdateInfo,
345
      .getExecInfoFn = stmtGetExecInfo,
346
  };
347

348
  STMT_ERR_RET(stmtCreateRequest(pStmt));
4,128!
349

350
  pStmt->stat.parseSqlNum++;
4,126✔
351
  STMT_ERR_RET(parseSql(pStmt->exec.pRequest, false, &pStmt->sql.pQuery, &stmtCb));
4,126✔
352
  pStmt->sql.siInfo.pQuery = pStmt->sql.pQuery;
4,117✔
353

354
  pStmt->bInfo.needParse = false;
4,117✔
355

356
  if (pStmt->sql.pQuery->pRoot && 0 == pStmt->sql.type) {
4,117✔
357
    pStmt->sql.type = STMT_TYPE_INSERT;
10✔
358
    pStmt->sql.stbInterlaceMode = false;
10✔
359
  } else if (pStmt->sql.pQuery->pPrepareRoot) {
4,107✔
360
    pStmt->sql.type = STMT_TYPE_QUERY;
201✔
361
    pStmt->sql.stbInterlaceMode = false;
201✔
362

363
    return TSDB_CODE_SUCCESS;
201✔
364
  }
365

366
  STableDataCxt** pSrc =
367
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
3,916✔
368
  if (NULL == pSrc || NULL == *pSrc) {
3,918!
369
    return terrno;
×
370
  }
371

372
  STableDataCxt* pTableCtx = *pSrc;
3,918✔
373
  if (pStmt->sql.stbInterlaceMode) {
3,918✔
374
    int16_t lastIdx = -1;
53✔
375

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

382
      lastIdx = pTableCtx->boundColsInfo.pColIndex[i];
445✔
383
    }
384
  }
385

386
  if (NULL == pStmt->sql.pBindInfo) {
3,918✔
387
    pStmt->sql.pBindInfo = taosMemoryMalloc(pTableCtx->boundColsInfo.numOfBound * sizeof(*pStmt->sql.pBindInfo));
921!
388
    if (NULL == pStmt->sql.pBindInfo) {
921!
389
      return terrno;
×
390
    }
391
  }
392

393
  return TSDB_CODE_SUCCESS;
3,918✔
394
}
395

396
int32_t stmtCleanBindInfo(STscStmt* pStmt) {
7,367✔
397
  pStmt->bInfo.tbUid = 0;
7,367✔
398
  pStmt->bInfo.tbSuid = 0;
7,367✔
399
  pStmt->bInfo.tbVgId = -1;
7,367✔
400
  pStmt->bInfo.tbType = 0;
7,367✔
401
  pStmt->bInfo.needParse = true;
7,367✔
402
  pStmt->bInfo.inExecCache = false;
7,367✔
403

404
  pStmt->bInfo.tbName[0] = 0;
7,367✔
405
  pStmt->bInfo.tbFName[0] = 0;
7,367✔
406
  if (!pStmt->bInfo.tagsCached) {
7,367✔
407
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
6,851✔
408
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
6,851!
409
  }
410
  pStmt->bInfo.stbFName[0] = 0;
7,367✔
411

412
  return TSDB_CODE_SUCCESS;
7,367✔
413
}
414

415
void stmtFreeTableBlkList(STableColsData* pTb) {
×
416
  (void)qResetStmtColumns(pTb->aCol, true);
×
417
  taosArrayDestroy(pTb->aCol);
×
418
}
×
419

420
void stmtResetQueueTableBuf(STableBufInfo* pTblBuf, SStmtQueue* pQueue) {
2,104✔
421
  pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, 0);
2,104✔
422
  if (NULL == pTblBuf->pCurBuff) {
2,105!
423
    tscError("QInfo:%p, failed to get buffer from list", pTblBuf);
×
424
    return;
×
425
  }
426
  pTblBuf->buffIdx = 1;
2,105✔
427
  pTblBuf->buffOffset = sizeof(*pQueue->head);
2,105✔
428

429
  pQueue->head = pQueue->tail = pTblBuf->pCurBuff;
2,105✔
430
  pQueue->qRemainNum = 0;
2,105✔
431
  pQueue->head->next = NULL;
2,105✔
432
}
433

434
int32_t stmtCleanExecInfo(STscStmt* pStmt, bool keepTable, bool deepClean) {
116,598✔
435
  if (pStmt->sql.stbInterlaceMode) {
116,598✔
436
    if (deepClean) {
2,157✔
437
      taosHashCleanup(pStmt->exec.pBlockHash);
52✔
438
      pStmt->exec.pBlockHash = NULL;
52✔
439

440
      if (NULL != pStmt->exec.pCurrBlock) {
52!
441
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->pData);
52!
442
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
52✔
443
      }
444
    } else {
445
      pStmt->sql.siInfo.pTableColsIdx = 0;
2,105✔
446
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
2,105✔
447
    }
448
  } else {
449
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
114,441✔
450
      taos_free_result(pStmt->exec.pRequest);
92,640✔
451
      pStmt->exec.pRequest = NULL;
92,641✔
452
    }
453

454
    size_t keyLen = 0;
114,442✔
455
    void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
114,442✔
456
    while (pIter) {
265,738✔
457
      STableDataCxt* pBlocks = *(STableDataCxt**)pIter;
151,296✔
458
      char*          key = taosHashGetKey(pIter, &keyLen);
151,296✔
459
      STableMeta*    pMeta = qGetTableMetaInDataBlock(pBlocks);
151,296✔
460

461
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
151,296✔
462
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
91,560✔
463
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
204,921!
464

465
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
91,560✔
466
        continue;
91,560✔
467
      }
468

469
      qDestroyStmtDataBlock(pBlocks);
59,736✔
470
      STMT_ERR_RET(taosHashRemove(pStmt->exec.pBlockHash, key, keyLen));
59,736!
471

472
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
59,736✔
473
    }
474

475
    if (keepTable) {
114,442✔
476
      return TSDB_CODE_SUCCESS;
113,361✔
477
    }
478

479
    taosHashCleanup(pStmt->exec.pBlockHash);
1,081✔
480
    pStmt->exec.pBlockHash = NULL;
1,081✔
481

482
    tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
1,081✔
483
    taosMemoryFreeClear(pStmt->exec.pCurrTbData);
1,081!
484
  }
485

486
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
3,238!
487

488
  return TSDB_CODE_SUCCESS;
3,237✔
489
}
490

491
void stmtFreeTbBuf(void* buf) {
54✔
492
  void* pBuf = *(void**)buf;
54✔
493
  taosMemoryFree(pBuf);
54!
494
}
54✔
495

496
void stmtFreeTbCols(void* buf) {
52,000✔
497
  SArray* pCols = *(SArray**)buf;
52,000✔
498
  taosArrayDestroy(pCols);
52,000✔
499
}
52,000✔
500

501
int32_t stmtCleanSQLInfo(STscStmt* pStmt) {
1,133✔
502
  STMT_DLOG_E("start to free SQL info");
1,133✔
503

504
  taosMemoryFree(pStmt->sql.pBindInfo);
1,133!
505
  taosMemoryFree(pStmt->sql.queryRes.fields);
1,133!
506
  taosMemoryFree(pStmt->sql.queryRes.userFields);
1,133!
507
  taosMemoryFree(pStmt->sql.sqlStr);
1,133!
508
  qDestroyQuery(pStmt->sql.pQuery);
1,133✔
509
  taosArrayDestroy(pStmt->sql.nodeList);
1,133✔
510
  taosHashCleanup(pStmt->sql.pVgHash);
1,133✔
511
  pStmt->sql.pVgHash = NULL;
1,133✔
512

513
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
1,133✔
514
  while (pIter) {
4,979✔
515
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
3,846✔
516

517
    qDestroyStmtDataBlock(pCache->pDataCtx);
3,846✔
518
    qDestroyBoundColInfo(pCache->boundTags);
3,846✔
519
    taosMemoryFreeClear(pCache->boundTags);
3,846!
520

521
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
3,845✔
522
  }
523
  taosHashCleanup(pStmt->sql.pTableCache);
1,133✔
524
  pStmt->sql.pTableCache = NULL;
1,133✔
525

526
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
1,133!
527
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
1,133!
528

529
  taos_free_result(pStmt->sql.siInfo.pRequest);
1,133✔
530
  taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
1,133✔
531
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
1,133✔
532
  taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
1,133✔
533
  taosMemoryFree(pStmt->sql.siInfo.pTSchema);
1,133!
534
  qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
1,133✔
535
  taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
1,133✔
536

537
  (void)memset(&pStmt->sql, 0, sizeof(pStmt->sql));
1,133✔
538
  pStmt->sql.siInfo.tableColsReady = true;
1,133✔
539

540
  STMT_DLOG_E("end to free SQL info");
1,133✔
541

542
  return TSDB_CODE_SUCCESS;
1,133✔
543
}
544

545
int32_t stmtTryAddTableVgroupInfo(STscStmt* pStmt, int32_t* vgId) {
55,870✔
546
  if (*vgId >= 0 && taosHashGet(pStmt->sql.pVgHash, (const char*)vgId, sizeof(*vgId))) {
55,870!
547
    return TSDB_CODE_SUCCESS;
16,200✔
548
  }
549

550
  SVgroupInfo      vgInfo = {0};
39,670✔
551
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
39,670✔
552
                           .requestId = pStmt->exec.pRequest->requestId,
39,670✔
553
                           .requestObjRefId = pStmt->exec.pRequest->self,
39,670✔
554
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
39,670✔
555

556
  int32_t code = catalogGetTableHashVgroup(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &vgInfo);
39,670✔
557
  if (TSDB_CODE_SUCCESS != code) {
39,670!
558
    return code;
×
559
  }
560

561
  code =
562
      taosHashPut(pStmt->sql.pVgHash, (const char*)&vgInfo.vgId, sizeof(vgInfo.vgId), (char*)&vgInfo, sizeof(vgInfo));
39,670✔
563
  if (TSDB_CODE_SUCCESS != code) {
39,670!
564
    return code;
×
565
  }
566

567
  *vgId = vgInfo.vgId;
39,670✔
568

569
  return TSDB_CODE_SUCCESS;
39,670✔
570
}
571

572
int32_t stmtRebuildDataBlock(STscStmt* pStmt, STableDataCxt* pDataBlock, STableDataCxt** newBlock, uint64_t uid,
55,870✔
573
                             uint64_t suid, int32_t vgId) {
574
  STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
55,870!
575
  STMT_ERR_RET(qRebuildStmtDataBlock(newBlock, pDataBlock, uid, suid, vgId, pStmt->sql.autoCreateTbl));
55,870!
576

577
  STMT_DLOG("uid:%" PRId64 ", rebuild table data context, vgId:%d", uid, vgId);
55,870!
578

579
  return TSDB_CODE_SUCCESS;
55,870✔
580
}
581

582
int32_t stmtGetFromCache(STscStmt* pStmt) {
92,773✔
583
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx) {
92,773!
584
    pStmt->bInfo.needParse = false;
×
585
    pStmt->bInfo.inExecCache = false;
×
586
    return TSDB_CODE_SUCCESS;
×
587
  }
588

589
  pStmt->bInfo.needParse = true;
92,773✔
590
  pStmt->bInfo.inExecCache = false;
92,773✔
591

592
  STableDataCxt** pCxtInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
92,773✔
593
  if (pCxtInExec) {
92,772✔
594
    pStmt->bInfo.needParse = false;
32,986✔
595
    pStmt->bInfo.inExecCache = true;
32,986✔
596

597
    pStmt->exec.pCurrBlock = *pCxtInExec;
32,986✔
598

599
    if (pStmt->sql.autoCreateTbl) {
32,986✔
600
      tscDebug("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
286!
601
      return TSDB_CODE_SUCCESS;
286✔
602
    }
603
  }
604

605
  if (NULL == pStmt->pCatalog) {
92,486✔
606
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
642✔
607
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
641✔
608
  }
609

610
  if (NULL == pStmt->sql.pTableCache || taosHashGetSize(pStmt->sql.pTableCache) <= 0) {
92,485!
611
    if (pStmt->bInfo.inExecCache) {
950✔
612
      pStmt->bInfo.needParse = false;
30✔
613
      tscDebug("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
30!
614
      return TSDB_CODE_SUCCESS;
30✔
615
    }
616

617
    tscDebug("no stmt block cache for tb %s", pStmt->bInfo.tbFName);
920!
618
    return TSDB_CODE_SUCCESS;
920✔
619
  }
620

621
  if (pStmt->sql.autoCreateTbl) {
91,537✔
622
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &pStmt->bInfo.tbSuid, sizeof(pStmt->bInfo.tbSuid));
39,670✔
623
    if (pCache) {
39,670!
624
      pStmt->bInfo.needParse = false;
39,670✔
625
      pStmt->bInfo.tbUid = 0;
39,670✔
626

627
      STableDataCxt* pNewBlock = NULL;
39,670✔
628
      STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, 0, pStmt->bInfo.tbSuid, -1));
39,670!
629

630
      if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
39,670!
631
                      POINTER_BYTES)) {
632
        STMT_ERR_RET(terrno);
×
633
      }
634

635
      pStmt->exec.pCurrBlock = pNewBlock;
39,670✔
636

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

639
      return TSDB_CODE_SUCCESS;
39,670✔
640
    }
641

642
    STMT_RET(stmtCleanBindInfo(pStmt));
×
643
  }
644

645
  uint64_t uid, suid;
646
  int32_t  vgId;
647
  int8_t   tableType;
648

649
  STableMeta*      pTableMeta = NULL;
51,867✔
650
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
51,867✔
651
                           .requestId = pStmt->exec.pRequest->requestId,
51,867✔
652
                           .requestObjRefId = pStmt->exec.pRequest->self,
51,867✔
653
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
51,867✔
654
  int32_t          code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
51,867✔
655

656
  pStmt->stat.ctgGetTbMetaNum++;
51,867✔
657

658
  if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
51,867!
659
    tscDebug("tb %s not exist", pStmt->bInfo.tbFName);
×
660
    STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
661

662
    STMT_ERR_RET(code);
×
663
  }
664

665
  STMT_ERR_RET(code);
51,867!
666

667
  uid = pTableMeta->uid;
51,867✔
668
  suid = pTableMeta->suid;
51,867✔
669
  tableType = pTableMeta->tableType;
51,867✔
670
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
51,867✔
671
  vgId = pTableMeta->vgId;
51,867✔
672

673
  taosMemoryFree(pTableMeta);
51,867!
674

675
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
51,867!
676

677
  if (uid == pStmt->bInfo.tbUid) {
51,867✔
678
    pStmt->bInfo.needParse = false;
16,470✔
679

680
    tscDebug("tb %s is current table", pStmt->bInfo.tbFName);
16,470!
681

682
    return TSDB_CODE_SUCCESS;
16,470✔
683
  }
684

685
  if (pStmt->bInfo.inExecCache) {
35,397✔
686
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
16,200✔
687
    if (NULL == pCache) {
16,200!
688
      tscError("table [%s, %" PRIx64 ", %" PRIx64 "] found in exec blockHash, but not in sql blockHash",
×
689
               pStmt->bInfo.tbFName, uid, cacheUid);
690

691
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
692
    }
693

694
    pStmt->bInfo.needParse = false;
16,200✔
695

696
    pStmt->bInfo.tbUid = uid;
16,200✔
697
    pStmt->bInfo.tbSuid = suid;
16,200✔
698
    pStmt->bInfo.tbType = tableType;
16,200✔
699
    pStmt->bInfo.boundTags = pCache->boundTags;
16,200✔
700
    pStmt->bInfo.tagsCached = true;
16,200✔
701

702
    tscDebug("tb %s in execBlock list, set to current", pStmt->bInfo.tbFName);
16,200!
703

704
    return TSDB_CODE_SUCCESS;
16,200✔
705
  }
706

707
  SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
19,197✔
708
  if (pCache) {
19,197✔
709
    pStmt->bInfo.needParse = false;
16,200✔
710

711
    pStmt->bInfo.tbUid = uid;
16,200✔
712
    pStmt->bInfo.tbSuid = suid;
16,200✔
713
    pStmt->bInfo.tbType = tableType;
16,200✔
714
    pStmt->bInfo.boundTags = pCache->boundTags;
16,200✔
715
    pStmt->bInfo.tagsCached = true;
16,200✔
716

717
    STableDataCxt* pNewBlock = NULL;
16,200✔
718
    STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, uid, suid, vgId));
16,200!
719

720
    if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
16,200!
721
                    POINTER_BYTES)) {
722
      STMT_ERR_RET(terrno);
×
723
    }
724

725
    pStmt->exec.pCurrBlock = pNewBlock;
16,200✔
726

727
    tscDebug("tb %s in sqlBlock list, set to current", pStmt->bInfo.tbFName);
16,200!
728

729
    return TSDB_CODE_SUCCESS;
16,200✔
730
  }
731

732
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
2,997!
733

734
  return TSDB_CODE_SUCCESS;
2,997✔
735
}
736

737
int32_t stmtResetStmt(STscStmt* pStmt) {
457✔
738
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
457!
739

740
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
457✔
741
  if (NULL == pStmt->sql.pTableCache) {
457!
742
    STMT_ERR_RET(terrno);
×
743
  }
744

745
  pStmt->sql.status = STMT_INIT;
457✔
746

747
  return TSDB_CODE_SUCCESS;
457✔
748
}
749

750
int32_t stmtAsyncOutput(STscStmt* pStmt, void* param) {
41,622✔
751
  SStmtQNode* pParam = (SStmtQNode*)param;
41,622✔
752

753
  if (pParam->restoreTbCols) {
41,622✔
754
    for (int32_t i = 0; i < pStmt->sql.siInfo.pTableColsIdx; ++i) {
41,535✔
755
      SArray** p = (SArray**)TARRAY_GET_ELEM(pStmt->sql.siInfo.pTableCols, i);
39,431✔
756
      *p = taosArrayInit(20, POINTER_BYTES);
39,431✔
757
      if (*p == NULL) {
39,431!
758
        STMT_ERR_RET(terrno);
×
759
      }
760
    }
761

762
    atomic_store_8((int8_t*)&pStmt->sql.siInfo.tableColsReady, true);
2,104✔
763
  } else {
764
    STMT_ERR_RET(qAppendStmtTableOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, &pParam->tblData, pStmt->exec.pCurrBlock,
39,517!
765
                                        &pStmt->sql.siInfo));
766

767
    // taosMemoryFree(pParam->pTbData);
768

769
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
39,458✔
770
  }
771
  return TSDB_CODE_SUCCESS;
41,636✔
772
}
773

774
void* stmtBindThreadFunc(void* param) {
54✔
775
  setThreadName("stmtBind");
54✔
776

777
  qInfo("stmt bind thread started");
54!
778

779
  STscStmt* pStmt = (STscStmt*)param;
54✔
780

781
  while (true) {
41,689✔
782
    if (pStmt->queue.stopQueue) {
41,743✔
783
      break;
54✔
784
    }
785

786
    SStmtQNode* asyncParam = NULL;
41,689✔
787
    if (!stmtDequeue(pStmt, &asyncParam)) {
41,689✔
788
      continue;
54✔
789
    }
790

791
    int ret = stmtAsyncOutput(pStmt, asyncParam);
41,627✔
792
    if (ret != 0) {
41,635!
793
      qError("stmtAsyncOutput failed, reason:%s", tstrerror(ret));
×
794
    }
795
  }
796

797
  qInfo("stmt bind thread stopped");
54!
798

799
  return NULL;
54✔
800
}
801

802
int32_t stmtStartBindThread(STscStmt* pStmt) {
54✔
803
  TdThreadAttr thAttr;
804
  if (taosThreadAttrInit(&thAttr) != 0) {
54!
805
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
806
  }
807
  if (taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE) != 0) {
54!
808
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
809
  }
810

811
  if (taosThreadCreate(&pStmt->bindThread, &thAttr, stmtBindThreadFunc, pStmt) != 0) {
54!
812
    terrno = TAOS_SYSTEM_ERROR(errno);
×
813
    STMT_ERR_RET(terrno);
×
814
  }
815

816
  pStmt->bindThreadInUse = true;
54✔
817

818
  (void)taosThreadAttrDestroy(&thAttr);
54✔
819
  return TSDB_CODE_SUCCESS;
54✔
820
}
821

822
int32_t stmtInitQueue(STscStmt* pStmt) {
54✔
823
  (void)taosThreadCondInit(&pStmt->queue.waitCond, NULL);
54✔
824
  (void)taosThreadMutexInit(&pStmt->queue.mutex, NULL);
54✔
825
  STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&pStmt->queue.head));
108!
826
  pStmt->queue.tail = pStmt->queue.head;
54✔
827

828
  return TSDB_CODE_SUCCESS;
54✔
829
}
830

831
int32_t stmtInitTableBuf(STableBufInfo* pTblBuf) {
54✔
832
  pTblBuf->buffUnit = sizeof(SStmtQNode);
54✔
833
  pTblBuf->buffSize = pTblBuf->buffUnit * 1000;
54✔
834
  pTblBuf->pBufList = taosArrayInit(100, POINTER_BYTES);
54✔
835
  if (NULL == pTblBuf->pBufList) {
54!
836
    return terrno;
×
837
  }
838
  void* buff = taosMemoryMalloc(pTblBuf->buffSize);
54!
839
  if (NULL == buff) {
54!
840
    return terrno;
×
841
  }
842

843
  if (taosArrayPush(pTblBuf->pBufList, &buff) == NULL) {
108!
844
    return terrno;
×
845
  }
846

847
  pTblBuf->pCurBuff = buff;
54✔
848
  pTblBuf->buffIdx = 1;
54✔
849
  pTblBuf->buffOffset = 0;
54✔
850

851
  return TSDB_CODE_SUCCESS;
54✔
852
}
853

854
TAOS_STMT* stmtInit(STscObj* taos, int64_t reqid, TAOS_STMT_OPTIONS* pOptions) {
675✔
855
  STscObj*  pObj = (STscObj*)taos;
675✔
856
  STscStmt* pStmt = NULL;
675✔
857
  int32_t   code = 0;
675✔
858

859
  pStmt = taosMemoryCalloc(1, sizeof(STscStmt));
675!
860
  if (NULL == pStmt) {
676!
861
    return NULL;
×
862
  }
863

864
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
676✔
865
  if (NULL == pStmt->sql.pTableCache) {
676!
866
    taosMemoryFree(pStmt);
×
867
    return NULL;
×
868
  }
869

870
  pStmt->taos = pObj;
676✔
871
  pStmt->bInfo.needParse = true;
676✔
872
  pStmt->sql.status = STMT_INIT;
676✔
873
  pStmt->reqid = reqid;
676✔
874
  pStmt->errCode = TSDB_CODE_SUCCESS;
676✔
875

876
  if (NULL != pOptions) {
676✔
877
    (void)memcpy(&pStmt->options, pOptions, sizeof(pStmt->options));
54✔
878
    if (pOptions->singleStbInsert && pOptions->singleTableBindOnce) {
54!
879
      pStmt->stbInterlaceMode = true;
54✔
880
    }
881
  }
882

883
  if (pStmt->stbInterlaceMode) {
676✔
884
    pStmt->sql.siInfo.transport = taos->pAppInfo->pTransporter;
54✔
885
    pStmt->sql.siInfo.acctId = taos->acctId;
54✔
886
    pStmt->sql.siInfo.dbname = taos->db;
54✔
887
    pStmt->sql.siInfo.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
54✔
888
    pStmt->sql.siInfo.pTableHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
54✔
889
    if (NULL == pStmt->sql.siInfo.pTableHash) {
54!
890
      (void)stmtClose(pStmt);
×
891
      return NULL;
×
892
    }
893
    pStmt->sql.siInfo.pTableCols = taosArrayInit(STMT_TABLE_COLS_NUM, POINTER_BYTES);
54✔
894
    if (NULL == pStmt->sql.siInfo.pTableCols) {
54!
895
      (void)stmtClose(pStmt);
×
896
      return NULL;
×
897
    }
898

899
    code = stmtInitTableBuf(&pStmt->sql.siInfo.tbBuf);
54✔
900
    if (TSDB_CODE_SUCCESS == code) {
54!
901
      code = stmtInitQueue(pStmt);
54✔
902
    }
903
    if (TSDB_CODE_SUCCESS == code) {
54!
904
      code = stmtStartBindThread(pStmt);
54✔
905
    }
906
    if (TSDB_CODE_SUCCESS != code) {
54!
907
      terrno = code;
×
908
      (void)stmtClose(pStmt);
×
909
      return NULL;
×
910
    }
911
  }
912

913
  pStmt->sql.siInfo.tableColsReady = true;
676✔
914

915
  STMT_LOG_SEQ(STMT_INIT);
676✔
916

917
  tscDebug("stmt:%p initialized", pStmt);
676✔
918

919
  return pStmt;
676✔
920
}
921

922
int stmtPrepare(TAOS_STMT* stmt, const char* sql, unsigned long length) {
1,133✔
923
  STscStmt* pStmt = (STscStmt*)stmt;
1,133✔
924

925
  STMT_DLOG_E("start to prepare");
1,133✔
926

927
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
1,133!
928
    return pStmt->errCode;
×
929
  }
930

931
  if (pStmt->sql.status >= STMT_PREPARE) {
1,133✔
932
    STMT_ERR_RET(stmtResetStmt(pStmt));
457!
933
  }
934

935
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
1,133!
936

937
  if (length <= 0) {
1,133✔
938
    length = strlen(sql);
1,031✔
939
  }
940

941
  pStmt->sql.sqlStr = taosStrndup(sql, length);
1,133!
942
  if (!pStmt->sql.sqlStr) {
1,132!
943
    return terrno;
×
944
  }
945
  pStmt->sql.sqlLen = length;
1,132✔
946
  pStmt->sql.stbInterlaceMode = pStmt->stbInterlaceMode;
1,132✔
947

948
  char* dbName = NULL;
1,132✔
949
  if (qParseDbName(sql, length, &dbName)) {
1,132!
950
    STMT_ERR_RET(stmtSetDbName(stmt, dbName));
×
951
    taosMemoryFreeClear(dbName);
×
952
  }
953

954
  return TSDB_CODE_SUCCESS;
1,133✔
955
}
956

957
int32_t stmtInitStbInterlaceTableInfo(STscStmt* pStmt) {
51✔
958
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
51✔
959
  if (!pSrc) {
52!
960
    return terrno;
×
961
  }
962
  STableDataCxt* pDst = NULL;
52✔
963

964
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
52!
965
  pStmt->sql.siInfo.pDataCtx = pDst;
51✔
966

967
  SArray* pTblCols = NULL;
51✔
968
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
43,901✔
969
    pTblCols = taosArrayInit(20, POINTER_BYTES);
43,758✔
970
    if (NULL == pTblCols) {
46,576!
971
      return terrno;
×
972
    }
973

974
    if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
90,426!
975
      return terrno;
×
976
    }
977
  }
978

979
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
143✔
980

981
  return TSDB_CODE_SUCCESS;
143✔
982
}
983

984
int stmtSetDbName(TAOS_STMT* stmt, const char* dbName) {
×
985
  STscStmt* pStmt = (STscStmt*)stmt;
×
986

987
  STMT_DLOG("start to set dbName:%s", dbName);
×
988

989
  STMT_ERR_RET(stmtCreateRequest(pStmt));
×
990

991
  // The SQL statement specifies a database name, overriding the previously specified database
992
  taosMemoryFreeClear(pStmt->exec.pRequest->pDb);
×
993
  pStmt->exec.pRequest->pDb = taosStrdup(dbName);
×
994
  if (pStmt->exec.pRequest->pDb == NULL) {
×
995
    return terrno;
×
996
  }
997
  return TSDB_CODE_SUCCESS;
×
998
}
999

1000
int stmtSetTbName(TAOS_STMT* stmt, const char* tbName) {
126,783✔
1001
  STscStmt* pStmt = (STscStmt*)stmt;
126,783✔
1002

1003
  int64_t startUs = taosGetTimestampUs();
126,816✔
1004

1005
  STMT_DLOG("start to set tbName:%s", tbName);
126,816!
1006

1007
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
126,816!
1008
    return pStmt->errCode;
×
1009
  }
1010

1011
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
126,816!
1012

1013
  int32_t insert = 0;
126,753✔
1014
  STMT_ERR_RET(stmtIsInsert(stmt, &insert));
126,753!
1015
  if (0 == insert) {
126,728!
1016
    tscError("set tb name not available for none insert statement");
×
1017
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1018
  }
1019

1020
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
126,728✔
1021
    STMT_ERR_RET(stmtCreateRequest(pStmt));
92,763!
1022

1023
    STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
92,774!
1024
                              pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1025
    STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
92,774!
1026

1027
    STMT_ERR_RET(stmtGetFromCache(pStmt));
92,774!
1028

1029
    if (pStmt->bInfo.needParse) {
92,771✔
1030
      tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
3,915✔
1031
      pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
3,915✔
1032

1033
      STMT_ERR_RET(stmtParseSql(pStmt));
3,915✔
1034
    }
1035
  } else {
1036
    tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
33,965✔
1037
    pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
33,965✔
1038
    pStmt->exec.pRequest->requestId++;
33,965✔
1039
    pStmt->bInfo.needParse = false;
33,965✔
1040
  }
1041

1042
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
126,728✔
1043
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
52!
1044
  }
1045

1046
  int64_t startUs2 = taosGetTimestampUs();
126,763✔
1047
  pStmt->stat.setTbNameUs += startUs2 - startUs;
126,763✔
1048

1049
  return TSDB_CODE_SUCCESS;
126,763✔
1050
}
1051

1052
int stmtSetTbTags(TAOS_STMT* stmt, TAOS_MULTI_BIND* tags) {
40,122✔
1053
  STscStmt* pStmt = (STscStmt*)stmt;
40,122✔
1054

1055
  STMT_DLOG_E("start to set tbTags");
40,122!
1056

1057
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
40,122!
1058
    return pStmt->errCode;
×
1059
  }
1060

1061
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
40,122!
1062

1063
  SBoundColInfo* tags_info = (SBoundColInfo*)pStmt->bInfo.boundTags;
40,122✔
1064
  if (tags_info->numOfBound <= 0 || tags_info->numOfCols <= 0) {
40,122!
1065
    tscWarn("no tags bound in sql, will not bound tags");
×
1066
    return TSDB_CODE_SUCCESS;
×
1067
  }
1068

1069
  if (pStmt->bInfo.inExecCache) {
40,122✔
1070
    return TSDB_CODE_SUCCESS;
270✔
1071
  }
1072

1073
  STableDataCxt** pDataBlock =
1074
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
39,852✔
1075
  if (NULL == pDataBlock) {
39,852!
1076
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1077
    STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1078
  }
1079

1080
  tscDebug("start to bind stmt tag values");
39,852!
1081
  STMT_ERR_RET(qBindStmtTagsValue(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.tbSuid, pStmt->bInfo.stbFName,
39,852!
1082
                                  pStmt->bInfo.sname.tname, tags, pStmt->exec.pRequest->msgBuf,
1083
                                  pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt));
1084

1085
  return TSDB_CODE_SUCCESS;
39,852✔
1086
}
1087

1088
int stmtFetchTagFields(STscStmt* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
20,148✔
1089
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
20,148!
1090
    return pStmt->errCode;
×
1091
  }
1092

1093
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
20,148!
1094
    tscError("invalid operation to get query tag fileds");
×
1095
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1096
  }
1097

1098
  STableDataCxt** pDataBlock =
1099
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
20,148✔
1100
  if (NULL == pDataBlock) {
20,148!
1101
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1102
    STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1103
  }
1104

1105
  STMT_ERR_RET(qBuildStmtTagFields(*pDataBlock, pStmt->bInfo.boundTags, fieldNum, fields));
20,148!
1106

1107
  return TSDB_CODE_SUCCESS;
20,148✔
1108
}
1109

1110
int stmtFetchColFields(STscStmt* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
2,594,232✔
1111
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
2,594,232!
1112
    return pStmt->errCode;
×
1113
  }
1114

1115
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
2,594,232!
1116
    tscError("invalid operation to get query column fileds");
×
1117
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1118
  }
1119

1120
  STableDataCxt** pDataBlock = NULL;
2,594,232✔
1121

1122
  if (pStmt->sql.stbInterlaceMode) {
2,594,232✔
1123
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
81,760✔
1124
  } else {
1125
    pDataBlock =
1126
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
2,512,472✔
1127
    if (NULL == pDataBlock) {
2,512,472!
1128
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1129
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1130
    }
1131
  }
1132

1133
  STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields));
2,594,232!
1134

1135
  return TSDB_CODE_SUCCESS;
2,594,232✔
1136
}
1137

1138
/*
1139
SArray* stmtGetFreeCol(STscStmt* pStmt, int32_t* idx) {
1140
  while (true) {
1141
    if (pStmt->exec.smInfo.pColIdx >= STMT_COL_BUF_SIZE) {
1142
      pStmt->exec.smInfo.pColIdx = 0;
1143
    }
1144

1145
    if ((pStmt->exec.smInfo.pColIdx + 1) == atomic_load_32(&pStmt->exec.smInfo.pColFreeIdx)) {
1146
      taosUsleep(1);
1147
      continue;
1148
    }
1149

1150
    *idx = pStmt->exec.smInfo.pColIdx;
1151
    return pStmt->exec.smInfo.pCols[pStmt->exec.smInfo.pColIdx++];
1152
  }
1153
}
1154
*/
1155

1156
int32_t stmtAppendTablePostHandle(STscStmt* pStmt, SStmtQNode* param) {
39,445✔
1157
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
39,445✔
1158
    pStmt->sql.siInfo.pVgroupHash =
2,103✔
1159
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
2,104✔
1160
  }
1161
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
39,444✔
1162
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
2,103✔
1163
  }
1164

1165
  if (NULL == pStmt->sql.siInfo.pRequest) {
39,439✔
1166
    STMT_ERR_RET(buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false,
52!
1167
                              (SRequestObj**)&pStmt->sql.siInfo.pRequest, pStmt->reqid));
1168

1169
    if (pStmt->reqid != 0) {
52!
1170
      pStmt->reqid++;
×
1171
    }
1172
    pStmt->exec.pRequest->syncQuery = true;
52✔
1173

1174
    pStmt->sql.siInfo.requestId = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->requestId;
52✔
1175
    pStmt->sql.siInfo.requestSelf = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->self;
52✔
1176
  }
1177

1178
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
39,439✔
1179
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
269✔
1180
    pStmt->sql.siInfo.tbFromHash = true;
30✔
1181
  }
1182

1183
  if (0 == pStmt->sql.siInfo.firstName[0]) {
39,439✔
1184
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
52✔
1185
  }
1186

1187
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
39,439✔
1188
  param->next = NULL;
39,439✔
1189

1190
  (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
39,439✔
1191

1192
  stmtEnqueue(pStmt, param);
39,511✔
1193

1194
  return TSDB_CODE_SUCCESS;
39,512✔
1195
}
1196

1197
static FORCE_INLINE int32_t stmtGetTableColsFromCache(STscStmt* pStmt, SArray** pTableCols) {
1198
  while (true) {
1199
    if (pStmt->sql.siInfo.pTableColsIdx < taosArrayGetSize(pStmt->sql.siInfo.pTableCols)) {
39,358!
1200
      *pTableCols = (SArray*)taosArrayGetP(pStmt->sql.siInfo.pTableCols, pStmt->sql.siInfo.pTableColsIdx++);
39,345✔
1201
      break;
39,383✔
1202
    } else {
1203
      SArray* pTblCols = NULL;
×
1204
      for (int32_t i = 0; i < 100; i++) {
×
1205
        pTblCols = taosArrayInit(20, POINTER_BYTES);
×
1206
        if (NULL == pTblCols) {
×
1207
          return terrno;
×
1208
        }
1209

1210
        if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
×
1211
          return terrno;
×
1212
        }
1213
      }
1214
    }
1215
  }
1216

1217
  return TSDB_CODE_SUCCESS;
39,383✔
1218
}
1219

1220
int stmtBindBatch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind, int32_t colIdx) {
2,301,009✔
1221
  STscStmt* pStmt = (STscStmt*)stmt;
2,301,009✔
1222
  int32_t   code = 0;
2,301,009✔
1223

1224
  int64_t startUs = taosGetTimestampUs();
2,298,011✔
1225

1226
  STMT_DLOG("start to bind stmt data, colIdx:%d", colIdx);
2,298,011✔
1227

1228
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
2,299,119✔
1229
    return pStmt->errCode;
10✔
1230
  }
1231

1232
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
2,299,109!
1233

1234
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
2,293,758!
1235
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1236
    pStmt->bInfo.needParse = false;
×
1237
  }
1238

1239
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
2,293,758!
1240
    taos_free_result(pStmt->exec.pRequest);
21,600✔
1241
    pStmt->exec.pRequest = NULL;
21,600✔
1242
  }
1243

1244
  STMT_ERR_RET(stmtCreateRequest(pStmt));
2,293,758!
1245

1246
  if (pStmt->bInfo.needParse) {
2,292,020✔
1247
    STMT_ERR_RET(stmtParseSql(pStmt));
183!
1248
  }
1249

1250
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
2,292,338✔
1251
    STMT_ERR_RET(qStmtBindParams(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt));
21,801!
1252

1253
    SParseContext ctx = {.requestId = pStmt->exec.pRequest->requestId,
21,801✔
1254
                         .acctId = pStmt->taos->acctId,
21,801✔
1255
                         .db = pStmt->exec.pRequest->pDb,
21,801✔
1256
                         .topicQuery = false,
1257
                         .pSql = pStmt->sql.sqlStr,
21,801✔
1258
                         .sqlLen = pStmt->sql.sqlLen,
21,801✔
1259
                         .pMsg = pStmt->exec.pRequest->msgBuf,
21,801✔
1260
                         .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1261
                         .pTransporter = pStmt->taos->pAppInfo->pTransporter,
21,801✔
1262
                         .pStmtCb = NULL,
1263
                         .pUser = pStmt->taos->user,
21,801✔
1264
                         .setQueryFp = setQueryRequest};
1265

1266
    ctx.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
21,801✔
1267
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &ctx.pCatalog));
21,801!
1268

1269
    STMT_ERR_RET(qStmtParseQuerySql(&ctx, pStmt->sql.pQuery));
21,801!
1270

1271
    if (pStmt->sql.pQuery->haveResultSet) {
21,801!
1272
      STMT_ERR_RET(setResSchemaInfo(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->pResSchema,
21,801!
1273
                                    pStmt->sql.pQuery->numOfResCols));
1274
      taosMemoryFreeClear(pStmt->sql.pQuery->pResSchema);
21,801!
1275
      setResPrecision(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->precision);
21,801✔
1276
    }
1277

1278
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
21,801✔
1279
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
21,801✔
1280
    TSWAP(pStmt->exec.pRequest->targetTableList, pStmt->sql.pQuery->pTargetTableList);
21,801✔
1281

1282
    // if (STMT_TYPE_QUERY == pStmt->sql.queryRes) {
1283
    //   STMT_ERR_RET(stmtRestoreQueryFields(pStmt));
1284
    // }
1285

1286
    // STMT_ERR_RET(stmtBackupQueryFields(pStmt));
1287

1288
    return TSDB_CODE_SUCCESS;
21,801✔
1289
  }
1290

1291
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
2,270,537!
1292
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1293
  }
1294

1295
  STableDataCxt** pDataBlock = NULL;
2,268,317✔
1296

1297
  if (pStmt->exec.pCurrBlock) {
2,268,317✔
1298
    pDataBlock = &pStmt->exec.pCurrBlock;
2,264,702✔
1299
  } else {
1300
    pDataBlock =
1301
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
3,615✔
1302
    if (NULL == pDataBlock) {
3,615!
1303
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1304
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1305
    }
1306
    pStmt->exec.pCurrBlock = *pDataBlock;
3,615✔
1307
    if (pStmt->sql.stbInterlaceMode) {
3,615✔
1308
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
52✔
1309
      pStmt->exec.pCurrBlock->pData->aCol = NULL;
52✔
1310
    }
1311
  }
1312

1313
  int64_t startUs2 = taosGetTimestampUs();
2,267,210✔
1314
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
2,267,210✔
1315

1316
  SStmtQNode* param = NULL;
2,267,210✔
1317
  if (pStmt->sql.stbInterlaceMode) {
2,267,210✔
1318
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
78,717!
1319
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
78,741!
1320
    taosArrayClear(param->tblData.aCol);
39,383✔
1321

1322
    // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES);
1323

1324
    param->restoreTbCols = false;
39,354✔
1325
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
39,354✔
1326
  }
1327

1328
  int64_t startUs3 = taosGetTimestampUs();
2,266,167✔
1329
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
2,266,167✔
1330

1331
  SArray* pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
2,266,167✔
1332

1333
  if (colIdx < 0) {
2,266,167✔
1334
    if (pStmt->sql.stbInterlaceMode) {
1,167,053✔
1335
      (*pDataBlock)->pData->flags = 0;
39,368✔
1336
      code =
1337
          qBindStmtStbColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
39,368✔
1338
                                &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt);
39,368✔
1339
    } else {
1340
      code = qBindStmtColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
1,127,685✔
1341
                                pStmt->taos->optionInfo.charsetCxt);
1,127,685✔
1342
    }
1343

1344
    if (code) {
1,151,315✔
1345
      tscError("qBindStmtColsValue failed, error:%s", tstrerror(code));
10!
1346
      STMT_ERR_RET(code);
10!
1347
    }
1348
  } else {
1349
    if (pStmt->sql.stbInterlaceMode) {
1,099,114!
1350
      tscError("bind single column not allowed in stb insert mode");
×
1351
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1352
    }
1353

1354
    if (colIdx != (pStmt->bInfo.sBindLastIdx + 1) && colIdx != 0) {
1,099,114!
1355
      tscError("bind column index not in sequence");
×
1356
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1357
    }
1358

1359
    pStmt->bInfo.sBindLastIdx = colIdx;
1,099,114✔
1360

1361
    if (0 == colIdx) {
1,099,114✔
1362
      pStmt->bInfo.sBindRowNum = bind->num;
128,651✔
1363
    }
1364

1365
    code =
1366
        qBindStmtSingleColValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
1,099,114✔
1367
                                colIdx, pStmt->bInfo.sBindRowNum, pStmt->taos->optionInfo.charsetCxt);
1,099,114✔
1368
    if (code) {
1,102,302!
1369
      tscError("qBindStmtSingleColValue failed, error:%s", tstrerror(code));
×
1370
      STMT_ERR_RET(code);
×
1371
    }
1372
  }
1373

1374
  int64_t startUs4 = taosGetTimestampUs();
2,254,335✔
1375
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
2,254,335✔
1376

1377
  if (pStmt->sql.stbInterlaceMode) {
2,254,335✔
1378
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
39,461!
1379
  }
1380

1381
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
2,259,001✔
1382

1383
  return TSDB_CODE_SUCCESS;
2,259,001✔
1384
}
1385

1386
int stmtAddBatch(TAOS_STMT* stmt) {
1,259,666✔
1387
  STscStmt* pStmt = (STscStmt*)stmt;
1,259,666✔
1388

1389
  int64_t startUs = taosGetTimestampUs();
1,256,032✔
1390

1391
  STMT_DLOG_E("start to add batch");
1,256,032✔
1392

1393
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
1,255,196✔
1394
    return pStmt->errCode;
10✔
1395
  }
1396

1397
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
1,255,186!
1398

1399
  if (pStmt->sql.stbInterlaceMode) {
1,253,832✔
1400
    int64_t startUs2 = taosGetTimestampUs();
2,105✔
1401
    pStmt->stat.addBatchUs += startUs2 - startUs;
2,105✔
1402

1403
    pStmt->sql.siInfo.tableColsReady = false;
2,105✔
1404

1405
    SStmtQNode* param = NULL;
2,105✔
1406
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
4,210!
1407
    param->restoreTbCols = true;
2,105✔
1408
    param->next = NULL;
2,105✔
1409

1410
    stmtEnqueue(pStmt, param);
2,105✔
1411

1412
    return TSDB_CODE_SUCCESS;
2,105✔
1413
  }
1414

1415
  STMT_ERR_RET(stmtCacheBlock(pStmt));
1,251,727!
1416

1417
  return TSDB_CODE_SUCCESS;
1,251,551✔
1418
}
1419
/*
1420
int stmtUpdateTableUid(STscStmt* pStmt, SSubmitRsp* pRsp) {
1421
  tscDebug("stmt start to update tbUid, blockNum:%d", pRsp->nBlocks);
1422

1423
  int32_t code = 0;
1424
  int32_t finalCode = 0;
1425
  size_t  keyLen = 0;
1426
  void*   pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
1427
  while (pIter) {
1428
    STableDataCxt* pBlock = *(STableDataCxt**)pIter;
1429
    char*          key = taosHashGetKey(pIter, &keyLen);
1430

1431
    STableMeta* pMeta = qGetTableMetaInDataBlock(pBlock);
1432
    if (pMeta->uid) {
1433
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1434
      continue;
1435
    }
1436

1437
    SSubmitBlkRsp* blkRsp = NULL;
1438
    int32_t        i = 0;
1439
    for (; i < pRsp->nBlocks; ++i) {
1440
      blkRsp = pRsp->pBlocks + i;
1441
      if (strlen(blkRsp->tblFName) != keyLen) {
1442
        continue;
1443
      }
1444

1445
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
1446
        continue;
1447
      }
1448

1449
      break;
1450
    }
1451

1452
    if (i < pRsp->nBlocks) {
1453
      tscDebug("auto created table %s uid updated from %" PRIx64 " to %" PRIx64, blkRsp->tblFName, pMeta->uid,
1454
               blkRsp->uid);
1455

1456
      pMeta->uid = blkRsp->uid;
1457
      pStmt->bInfo.tbUid = blkRsp->uid;
1458
    } else {
1459
      tscDebug("table %s not found in submit rsp, will update from catalog", pStmt->bInfo.tbFName);
1460
      if (NULL == pStmt->pCatalog) {
1461
        code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog);
1462
        if (code) {
1463
          pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1464
          finalCode = code;
1465
          continue;
1466
        }
1467
      }
1468

1469
      code = stmtCreateRequest(pStmt);
1470
      if (code) {
1471
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1472
        finalCode = code;
1473
        continue;
1474
      }
1475

1476
      STableMeta*      pTableMeta = NULL;
1477
      SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
1478
                               .requestId = pStmt->exec.pRequest->requestId,
1479
                               .requestObjRefId = pStmt->exec.pRequest->self,
1480
                               .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
1481
      code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
1482

1483
      pStmt->stat.ctgGetTbMetaNum++;
1484

1485
      taos_free_result(pStmt->exec.pRequest);
1486
      pStmt->exec.pRequest = NULL;
1487

1488
      if (code || NULL == pTableMeta) {
1489
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1490
        finalCode = code;
1491
        taosMemoryFree(pTableMeta);
1492
        continue;
1493
      }
1494

1495
      pMeta->uid = pTableMeta->uid;
1496
      pStmt->bInfo.tbUid = pTableMeta->uid;
1497
      taosMemoryFree(pTableMeta);
1498
    }
1499

1500
    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1501
  }
1502

1503
  return finalCode;
1504
}
1505
*/
1506

1507
/*
1508
int stmtStaticModeExec(TAOS_STMT* stmt) {
1509
  STscStmt*   pStmt = (STscStmt*)stmt;
1510
  int32_t     code = 0;
1511
  SSubmitRsp* pRsp = NULL;
1512
  if (pStmt->sql.staticMode) {
1513
    return TSDB_CODE_TSC_STMT_API_ERROR;
1514
  }
1515

1516
  STMT_DLOG_E("start to exec");
1517

1518
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
1519

1520
  STMT_ERR_RET(qBuildStmtOutputFromTbList(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pTbBlkList,
1521
pStmt->exec.pCurrBlock, pStmt->exec.tbBlkNum));
1522

1523
  launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
1524

1525
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
1526
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
1527
    if (code) {
1528
      pStmt->exec.pRequest->code = code;
1529
    } else {
1530
      tFreeSSubmitRsp(pRsp);
1531
      STMT_ERR_RET(stmtResetStmt(pStmt));
1532
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
1533
    }
1534
  }
1535

1536
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
1537

1538
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
1539
  pStmt->affectedRows += pStmt->exec.affectedRows;
1540

1541
_return:
1542

1543
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
1544

1545
  tFreeSSubmitRsp(pRsp);
1546

1547
  ++pStmt->sql.runTimes;
1548

1549
  STMT_RET(code);
1550
}
1551
*/
1552

1553
int stmtExec(TAOS_STMT* stmt) {
115,476✔
1554
  STscStmt*   pStmt = (STscStmt*)stmt;
115,476✔
1555
  int32_t     code = 0;
115,476✔
1556
  SSubmitRsp* pRsp = NULL;
115,476✔
1557

1558
  int64_t startUs = taosGetTimestampUs();
115,475✔
1559

1560
  STMT_DLOG_E("start to exec");
115,475✔
1561

1562
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
115,475✔
1563
    return pStmt->errCode;
10✔
1564
  }
1565

1566
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
115,465!
1567

1568
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
115,465✔
1569
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
21,801✔
1570
  } else {
1571
    if (pStmt->sql.stbInterlaceMode) {
93,664✔
1572
      int64_t startTs = taosGetTimestampUs();
2,105✔
1573
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
4,273✔
1574
        taosUsleep(1);
2,168✔
1575
      }
1576
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
2,105✔
1577

1578
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
2,105!
1579
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
2,104✔
1580
      pStmt->sql.siInfo.pVgroupHash = NULL;
2,105✔
1581
      pStmt->sql.siInfo.pVgroupList = NULL;
2,105✔
1582
    } else {
1583
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
91,559✔
1584
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
91,560!
1585

1586
      STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
91,560!
1587

1588
      STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
91,560!
1589
    }
1590

1591
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
93,664✔
1592
  }
1593

1594
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
115,465!
1595
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
×
1596
    if (code) {
×
1597
      pStmt->exec.pRequest->code = code;
×
1598
    } else {
1599
      tFreeSSubmitRsp(pRsp);
×
1600
      STMT_ERR_RET(stmtResetStmt(pStmt));
×
1601
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
×
1602
    }
1603
  }
1604

1605
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
115,466!
1606

1607
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
115,466✔
1608
  pStmt->affectedRows += pStmt->exec.affectedRows;
115,466✔
1609

1610
_return:
115,466✔
1611

1612
  while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
115,466!
1613
    taosUsleep(1);
×
1614
  }
1615

1616
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
115,466!
1617

1618
  tFreeSSubmitRsp(pRsp);
115,465✔
1619

1620
  ++pStmt->sql.runTimes;
115,465✔
1621

1622
  int64_t startUs2 = taosGetTimestampUs();
115,466✔
1623
  pStmt->stat.execUseUs += startUs2 - startUs;
115,466✔
1624

1625
  STMT_RET(code);
115,466!
1626
}
1627

1628
int stmtClose(TAOS_STMT* stmt) {
676✔
1629
  STscStmt* pStmt = (STscStmt*)stmt;
676✔
1630

1631
  STMT_DLOG_E("start to free stmt");
676✔
1632

1633
  pStmt->queue.stopQueue = true;
676✔
1634
  
1635
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
676✔
1636
  (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
676✔
1637
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
676✔
1638
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
676✔
1639

1640
  if (pStmt->bindThreadInUse) {
676✔
1641
    (void)taosThreadJoin(pStmt->bindThread, NULL);
54✔
1642
    pStmt->bindThreadInUse = false;
54✔
1643
  }
1644

1645
  (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
676✔
1646
  (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
676✔
1647

1648
  STMT_DLOG("stmt %p closed, stbInterlaceMode:%d, statInfo: ctgGetTbMetaNum=>%" PRId64 ", getCacheTbInfo=>%" PRId64
675✔
1649
            ", parseSqlNum=>%" PRId64 ", pStmt->stat.bindDataNum=>%" PRId64
1650
            ", settbnameAPI:%u, bindAPI:%u, addbatchAPI:%u, execAPI:%u"
1651
            ", setTbNameUs:%" PRId64 ", bindDataUs:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64 " addBatchUs:%" PRId64
1652
            ", execWaitUs:%" PRId64 ", execUseUs:%" PRId64,
1653
            pStmt, pStmt->sql.stbInterlaceMode, pStmt->stat.ctgGetTbMetaNum, pStmt->stat.getCacheTbInfo,
1654
            pStmt->stat.parseSqlNum, pStmt->stat.bindDataNum, pStmt->seqIds[STMT_SETTBNAME], pStmt->seqIds[STMT_BIND],
1655
            pStmt->seqIds[STMT_ADD_BATCH], pStmt->seqIds[STMT_EXECUTE], pStmt->stat.setTbNameUs,
1656
            pStmt->stat.bindDataUs1, pStmt->stat.bindDataUs2, pStmt->stat.bindDataUs3, pStmt->stat.bindDataUs4,
1657
            pStmt->stat.addBatchUs, pStmt->stat.execWaitUs, pStmt->stat.execUseUs);
1658

1659
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
675!
1660
  taosMemoryFree(stmt);
676!
1661

1662
  return TSDB_CODE_SUCCESS;
676✔
1663
}
1664

1665
const char* stmtErrstr(TAOS_STMT* stmt) {
1✔
1666
  STscStmt* pStmt = (STscStmt*)stmt;
1✔
1667

1668
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
1!
UNCOV
1669
    return (char*)tstrerror(terrno);
×
1670
  }
1671

1672
  pStmt->exec.pRequest->code = terrno;
1✔
1673

1674
  return taos_errstr(pStmt->exec.pRequest);
1✔
1675
}
1676

1677
int stmtAffectedRows(TAOS_STMT* stmt) { return ((STscStmt*)stmt)->affectedRows; }
296✔
1678

1679
int stmtAffectedRowsOnce(TAOS_STMT* stmt) { return ((STscStmt*)stmt)->exec.affectedRows; }
×
1680

1681
int stmtIsInsert(TAOS_STMT* stmt, int* insert) {
2,440,425✔
1682
  STscStmt* pStmt = (STscStmt*)stmt;
2,440,425✔
1683

1684
  STMT_DLOG_E("start is insert");
2,440,425✔
1685

1686
  if (pStmt->sql.type) {
2,441,156✔
1687
    *insert = (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
2,439,402!
1688
  } else {
1689
    *insert = qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
1,754✔
1690
  }
1691

1692
  return TSDB_CODE_SUCCESS;
2,441,156✔
1693
}
1694

1695
int stmtGetTagFields(TAOS_STMT* stmt, int* nums, TAOS_FIELD_E** fields) {
20,148✔
1696
  int32_t   code = 0;
20,148✔
1697
  STscStmt* pStmt = (STscStmt*)stmt;
20,148✔
1698
  int32_t   preCode = pStmt->errCode;
20,148✔
1699

1700
  STMT_DLOG_E("start to get tag fields");
20,148!
1701

1702
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
20,148!
1703
    return pStmt->errCode;
×
1704
  }
1705

1706
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
20,148!
1707
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1708
  }
1709

1710
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
20,148!
1711

1712
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
20,148!
1713
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1714
    pStmt->bInfo.needParse = false;
×
1715
  }
1716

1717
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
20,148!
1718
    taos_free_result(pStmt->exec.pRequest);
×
1719
    pStmt->exec.pRequest = NULL;
×
1720
  }
1721

1722
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
20,148!
1723

1724
  if (pStmt->bInfo.needParse) {
20,148!
1725
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
1726
  }
1727

1728
  STMT_ERRI_JRET(stmtFetchTagFields(stmt, nums, fields));
20,148!
1729

1730
_return:
20,148✔
1731

1732
  pStmt->errCode = preCode;
20,148✔
1733

1734
  return code;
20,148✔
1735
}
1736

1737
int stmtGetColFields(TAOS_STMT* stmt, int* nums, TAOS_FIELD_E** fields) {
268,567✔
1738
  int32_t   code = 0;
268,567✔
1739
  STscStmt* pStmt = (STscStmt*)stmt;
268,567✔
1740
  int32_t   preCode = pStmt->errCode;
268,567✔
1741

1742
  STMT_DLOG_E("start to get col fields");
268,567!
1743

1744
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
268,567!
1745
    return pStmt->errCode;
×
1746
  }
1747

1748
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
268,567!
1749
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1750
  }
1751

1752
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
268,567!
1753

1754
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
268,567!
1755
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1756
    pStmt->bInfo.needParse = false;
×
1757
  }
1758

1759
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
268,567!
1760
    taos_free_result(pStmt->exec.pRequest);
×
1761
    pStmt->exec.pRequest = NULL;
×
1762
    STMT_ERR_RET(stmtCreateRequest(pStmt));
×
1763
  }
1764

1765
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
268,567!
1766

1767
  if (pStmt->bInfo.needParse) {
268,567✔
1768
    STMT_ERRI_JRET(stmtParseSql(pStmt));
8!
1769
  }
1770

1771
  STMT_ERRI_JRET(stmtFetchColFields(stmt, nums, fields));
268,567!
1772

1773
_return:
268,567✔
1774

1775
  pStmt->errCode = preCode;
268,567✔
1776

1777
  return code;
268,567✔
1778
}
1779

1780
int stmtGetParamNum(TAOS_STMT* stmt, int* nums) {
1,331✔
1781
  int       code = 0;
1,331✔
1782
  STscStmt* pStmt = (STscStmt*)stmt;
1,331✔
1783
  int32_t   preCode = pStmt->errCode;
1,331✔
1784

1785
  STMT_DLOG_E("start to get param num");
1,331!
1786

1787
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
1,331!
1788
    return pStmt->errCode;
×
1789
  }
1790

1791
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
1,331!
1792

1793
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
1,331!
1794
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1795
    pStmt->bInfo.needParse = false;
×
1796
  }
1797

1798
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
1,331!
1799
    taos_free_result(pStmt->exec.pRequest);
180✔
1800
    pStmt->exec.pRequest = NULL;
180✔
1801
  }
1802

1803
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
1,331!
1804

1805
  if (pStmt->bInfo.needParse) {
1,331✔
1806
    STMT_ERRI_JRET(stmtParseSql(pStmt));
21!
1807
  }
1808

1809
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
1,331✔
1810
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
200✔
1811
  } else {
1812
    STMT_ERRI_JRET(stmtFetchColFields(stmt, nums, NULL));
1,131!
1813
  }
1814

1815
_return:
1,131✔
1816

1817
  pStmt->errCode = preCode;
1,331✔
1818

1819
  return code;
1,331✔
1820
}
1821

1822
int stmtGetParam(TAOS_STMT* stmt, int idx, int* type, int* bytes) {
2,324,534✔
1823
  int       code = 0;
2,324,534✔
1824
  STscStmt* pStmt = (STscStmt*)stmt;
2,324,534✔
1825
  int32_t   preCode = pStmt->errCode;
2,324,534✔
1826

1827
  STMT_DLOG_E("start to get param");
2,324,534!
1828

1829
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
2,324,534!
1830
    return pStmt->errCode;
×
1831
  }
1832

1833
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
2,324,534!
1834
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1835
  }
1836

1837
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
2,324,534!
1838

1839
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
2,324,534!
1840
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1841
    pStmt->bInfo.needParse = false;
×
1842
  }
1843

1844
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
2,324,534!
1845
    taos_free_result(pStmt->exec.pRequest);
×
1846
    pStmt->exec.pRequest = NULL;
×
1847
  }
1848

1849
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
2,324,534!
1850

1851
  if (pStmt->bInfo.needParse) {
2,324,534!
1852
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
1853
  }
1854

1855
  int32_t       nums = 0;
2,324,534✔
1856
  TAOS_FIELD_E* pField = NULL;
2,324,534✔
1857
  STMT_ERRI_JRET(stmtFetchColFields(stmt, &nums, &pField));
2,324,534!
1858
  if (idx >= nums) {
2,324,534!
1859
    tscError("idx %d is too big", idx);
×
1860
    STMT_ERRI_JRET(TSDB_CODE_INVALID_PARA);
×
1861
  }
1862

1863
  *type = pField[idx].type;
2,324,534✔
1864
  *bytes = pField[idx].bytes;
2,324,534✔
1865

1866
_return:
2,324,534✔
1867

1868
  taosMemoryFree(pField);
2,324,534!
1869
  pStmt->errCode = preCode;
2,324,534✔
1870

1871
  return code;
2,324,534✔
1872
}
1873

1874
TAOS_RES* stmtUseResult(TAOS_STMT* stmt) {
21,801✔
1875
  STscStmt* pStmt = (STscStmt*)stmt;
21,801✔
1876

1877
  STMT_DLOG_E("start to use result");
21,801!
1878

1879
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
21,801!
1880
    tscError("useResult only for query statement");
×
1881
    return NULL;
×
1882
  }
1883

1884
  return pStmt->exec.pRequest;
21,801✔
1885
}
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