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

taosdata / TDengine / #4506

15 Jul 2025 12:33AM UTC coverage: 62.026% (-0.7%) from 62.706%
#4506

push

travis-ci

web-flow
docs: update stream docs (#31874)

155391 of 320094 branches covered (48.55%)

Branch coverage included in aggregate %.

240721 of 318525 relevant lines covered (75.57%)

6529048.03 hits per line

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

67.56
/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) {
10,355✔
13
    *pBuf = (char*)pTblBuf->pCurBuff + pTblBuf->buffOffset;
10,360✔
14
    pTblBuf->buffOffset += pTblBuf->buffUnit;
10,360✔
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(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;
10,360✔
39
}
40

41
bool stmtDequeue(STscStmt* pStmt, SStmtQNode** param) {
10,362✔
42
  int i = 0;
10,362✔
43
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
58,393✔
44
    if (i < 10) {
48,026✔
45
      taosUsleep(1);
45,335✔
46
      i++;
45,338✔
47
    } else {
48
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
2,691✔
49
      if (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
2,693✔
50
        (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
2,685✔
51
      }
52
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
2,693✔
53
    }
54
  }
55
  if (pStmt->queue.stopQueue) {
10,229✔
56
    return false;
100✔
57
  }
58
  SStmtQNode* orig = pStmt->queue.head;
10,129✔
59
  SStmtQNode* node = pStmt->queue.head->next;
10,129✔
60
  pStmt->queue.head = pStmt->queue.head->next;
10,129✔
61
  *param = node;
10,129✔
62

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

65
  return true;
10,262✔
66
}
67

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

72
  pStmt->stat.bindDataNum++;
10,253✔
73

74
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
10,253✔
75
  (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
10,259✔
76
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
10,269✔
77
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
10,267✔
78
}
10,266✔
79

80
static int32_t stmtCreateRequest(STscStmt* pStmt) {
107,592✔
81
  int32_t code = 0;
107,592✔
82

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

95
  return code;
107,598✔
96
}
97

98
int32_t stmtSwitchStatus(STscStmt* pStmt, STMT_STATUS newStatus) {
202,575✔
99
  int32_t code = 0;
202,575✔
100

101
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
202,575!
102
    STMT_LOG_SEQ(newStatus);
202,652✔
103
  }
104

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

110
  switch (newStatus) {
202,666!
111
    case STMT_PREPARE:
20,119✔
112
      pStmt->errCode = 0;
20,119✔
113
      break;
20,119✔
114
    case STMT_SETTBNAME:
45,746✔
115
      if (STMT_STATUS_EQ(INIT)) {
45,746!
116
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
117
      }
118
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
45,746!
119
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
120
      }
121
      break;
45,746✔
122
    case STMT_SETTAGS:
15✔
123
      if (STMT_STATUS_NE(SETTBNAME) && STMT_STATUS_NE(FETCH_FIELDS)) {
15!
124
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
125
      }
126
      break;
15✔
127
    case STMT_FETCH_FIELDS:
1,064✔
128
      if (STMT_STATUS_EQ(INIT)) {
1,064!
129
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
130
      }
131
      break;
1,064✔
132
    case STMT_BIND:
46,342✔
133
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
46,342!
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;
46,342✔
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:
44,764✔
148
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
44,764!
149
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
150
      }
151
      break;
44,764✔
152
    case STMT_EXECUTE:
44,616✔
153
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
44,616✔
154
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
17!
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)) {
44,599!
160
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
161
        }
162
      }
163
      break;
44,616✔
164
    default:
×
165
      code = TSDB_CODE_APP_ERROR;
×
166
      break;
×
167
  }
168

169
  STMT_ERR_RET(code);
202,666!
170

171
  pStmt->sql.status = newStatus;
202,666✔
172

173
  return TSDB_CODE_SUCCESS;
202,666✔
174
}
175

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

179
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
20,090✔
180

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

186
  *tbName = pStmt->bInfo.tbName;
20,088✔
187

188
  return TSDB_CODE_SUCCESS;
20,088✔
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_E);
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,
20,068✔
241
                           bool autoCreateTbl, uint8_t tbNameFlag) {
242
  STscStmt* pStmt = (STscStmt*)stmt;
20,068✔
243
  char      tbFName[TSDB_TABLE_FNAME_LEN];
244
  int32_t   code = tNameExtractFullName(tbName, tbFName);
20,068✔
245
  if (code != 0) {
20,112!
246
    return code;
×
247
  }
248

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

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

262
  return TSDB_CODE_SUCCESS;
20,112✔
263
}
264

265
int32_t stmtUpdateExecInfo(TAOS_STMT* stmt, SHashObj* pVgHash, SHashObj* pBlockHash) {
20,060✔
266
  STscStmt* pStmt = (STscStmt*)stmt;
20,060✔
267

268
  pStmt->sql.pVgHash = pVgHash;
20,060✔
269
  pStmt->exec.pBlockHash = pBlockHash;
20,060✔
270

271
  return TSDB_CODE_SUCCESS;
20,060✔
272
}
273

274
int32_t stmtUpdateInfo(TAOS_STMT* stmt, STableMeta* pTableMeta, void* tags, SArray* cols, SName* tbName,
20,070✔
275
                       bool autoCreateTbl, SHashObj* pVgHash, SHashObj* pBlockHash, const char* sTableName,
276
                       uint8_t tbNameFlag) {
277
  STscStmt* pStmt = (STscStmt*)stmt;
20,070✔
278

279
  STMT_ERR_RET(stmtUpdateBindInfo(stmt, pTableMeta, tags, tbName, sTableName, autoCreateTbl, tbNameFlag));
20,070!
280
  STMT_ERR_RET(stmtUpdateExecInfo(stmt, pVgHash, pBlockHash));
20,098!
281

282
  pStmt->sql.autoCreateTbl = autoCreateTbl;
20,101✔
283
  if (pStmt->sql.autoCreateTbl) {
20,101✔
284
    pStmt->sql.stbInterlaceMode = false;
20,008✔
285
  }
286

287
  return TSDB_CODE_SUCCESS;
20,101✔
288
}
289

290
int32_t stmtGetExecInfo(TAOS_STMT* stmt, SHashObj** pVgHash, SHashObj** pBlockHash) {
18✔
291
  STscStmt* pStmt = (STscStmt*)stmt;
18✔
292

293
  *pVgHash = pStmt->sql.pVgHash;
18✔
294
  pStmt->sql.pVgHash = NULL;
18✔
295

296
  *pBlockHash = pStmt->exec.pBlockHash;
18✔
297
  pStmt->exec.pBlockHash = NULL;
18✔
298

299
  return TSDB_CODE_SUCCESS;
18✔
300
}
301

302
int32_t stmtCacheBlock(STscStmt* pStmt) {
40,218✔
303
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
40,218✔
304
    return TSDB_CODE_SUCCESS;
30✔
305
  }
306

307
  uint64_t uid = pStmt->bInfo.tbUid;
40,188✔
308
  uint64_t cacheUid = (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) ? pStmt->bInfo.tbSuid : uid;
40,188✔
309

310
  if (taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid))) {
40,188✔
311
    return TSDB_CODE_SUCCESS;
20,162✔
312
  }
313

314
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
20,039✔
315
  if (!pSrc) {
20,039!
316
    return terrno;
×
317
  }
318
  STableDataCxt* pDst = NULL;
20,039✔
319

320
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
20,039!
321

322
  SStmtTableCache cache = {
20,034✔
323
      .pDataCtx = pDst,
324
      .boundTags = pStmt->bInfo.boundTags,
20,034✔
325
  };
326

327
  if (taosHashPut(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid), &cache, sizeof(cache))) {
20,034✔
328
    return terrno;
2✔
329
  }
330

331
  if (pStmt->sql.autoCreateTbl) {
20,039✔
332
    pStmt->bInfo.tagsCached = true;
20,018✔
333
  } else {
334
    pStmt->bInfo.boundTags = NULL;
21✔
335
  }
336

337
  return TSDB_CODE_SUCCESS;
20,039✔
338
}
339

340
int32_t stmtParseSql(STscStmt* pStmt) {
20,104✔
341
  pStmt->exec.pCurrBlock = NULL;
20,104✔
342

343
  SStmtCallback stmtCb = {
20,104✔
344
      .pStmt = pStmt,
345
      .getTbNameFn = stmtGetTbName,
346
      .setInfoFn = stmtUpdateInfo,
347
      .getExecInfoFn = stmtGetExecInfo,
348
  };
349

350
  STMT_ERR_RET(stmtCreateRequest(pStmt));
20,104!
351

352
  pStmt->stat.parseSqlNum++;
20,115✔
353
  STMT_ERR_RET(parseSql(pStmt->exec.pRequest, false, &pStmt->sql.pQuery, &stmtCb));
20,115✔
354
  pStmt->sql.siInfo.pQuery = pStmt->sql.pQuery;
20,100✔
355

356
  pStmt->bInfo.needParse = false;
20,100✔
357

358
  if (pStmt->sql.pQuery->pRoot && 0 == pStmt->sql.type) {
20,100!
359
    pStmt->sql.type = STMT_TYPE_INSERT;
6✔
360
    pStmt->sql.stbInterlaceMode = false;
6✔
361
  } else if (pStmt->sql.pQuery->pPrepareRoot) {
20,094✔
362
    pStmt->sql.type = STMT_TYPE_QUERY;
17✔
363
    pStmt->sql.stbInterlaceMode = false;
17✔
364

365
    return TSDB_CODE_SUCCESS;
17✔
366
  }
367

368
  STableDataCxt** pSrc =
369
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
20,083✔
370
  if (NULL == pSrc || NULL == *pSrc) {
20,118!
371
    return terrno;
1✔
372
  }
373

374
  STableDataCxt* pTableCtx = *pSrc;
20,117✔
375
  if (pStmt->sql.stbInterlaceMode) {
20,117✔
376
    int16_t lastIdx = -1;
72✔
377

378
    for (int32_t i = 0; i < pTableCtx->boundColsInfo.numOfBound; ++i) {
643✔
379
      if (pTableCtx->boundColsInfo.pColIndex[i] < lastIdx) {
571!
380
        pStmt->sql.stbInterlaceMode = false;
×
381
        break;
×
382
      }
383

384
      lastIdx = pTableCtx->boundColsInfo.pColIndex[i];
571✔
385
    }
386
  }
387

388
  if (NULL == pStmt->sql.pBindInfo) {
20,117✔
389
    pStmt->sql.pBindInfo = taosMemoryMalloc(pTableCtx->boundColsInfo.numOfBound * sizeof(*pStmt->sql.pBindInfo));
20,095!
390
    if (NULL == pStmt->sql.pBindInfo) {
20,074!
391
      return terrno;
×
392
    }
393
  }
394

395
  return TSDB_CODE_SUCCESS;
20,096✔
396
}
397

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

406
  pStmt->bInfo.tbName[0] = 0;
44,805✔
407
  pStmt->bInfo.tbFName[0] = 0;
44,805✔
408
  if (!pStmt->bInfo.tagsCached) {
44,805✔
409
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
4,771✔
410
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
4,771!
411
  }
412
  pStmt->bInfo.stbFName[0] = 0;
44,809✔
413

414
  return TSDB_CODE_SUCCESS;
44,809✔
415
}
416

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

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

431
  pQueue->head = pQueue->tail = pTblBuf->pCurBuff;
4,538✔
432
  pQueue->qRemainNum = 0;
4,538✔
433
  pQueue->head->next = NULL;
4,538✔
434
}
435

436
int32_t stmtCleanExecInfo(STscStmt* pStmt, bool keepTable, bool deepClean) {
64,723✔
437
  if (pStmt->sql.stbInterlaceMode) {
64,723✔
438
    if (deepClean) {
4,616✔
439
      taosHashCleanup(pStmt->exec.pBlockHash);
72✔
440
      pStmt->exec.pBlockHash = NULL;
72✔
441

442
      if (NULL != pStmt->exec.pCurrBlock) {
72!
443
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->pData);
72!
444
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
72✔
445
      }
446
    } else {
447
      pStmt->sql.siInfo.pTableColsIdx = 0;
4,544✔
448
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
4,544✔
449
      tSimpleHashClear(pStmt->sql.siInfo.pTableRowDataHash);
4,540✔
450
    }
451
  } else {
452
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
60,107✔
453
      taos_free_result(pStmt->exec.pRequest);
60,090✔
454
      pStmt->exec.pRequest = NULL;
60,102✔
455
    }
456

457
    size_t keyLen = 0;
60,119✔
458
    void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
60,119✔
459
    while (pIter) {
120,254✔
460
      STableDataCxt* pBlocks = *(STableDataCxt**)pIter;
60,126✔
461
      char*          key = taosHashGetKey(pIter, &keyLen);
60,126✔
462
      STableMeta*    pMeta = qGetTableMetaInDataBlock(pBlocks);
60,123✔
463

464
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
60,123✔
465
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
40,062✔
466
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
80,138!
467

468
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
40,052✔
469
        continue;
40,059✔
470
      }
471

472
      qDestroyStmtDataBlock(pBlocks);
20,061✔
473
      STMT_ERR_RET(taosHashRemove(pStmt->exec.pBlockHash, key, keyLen));
20,065!
474

475
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
20,061✔
476
    }
477

478
    if (keepTable) {
60,128✔
479
      return TSDB_CODE_SUCCESS;
40,076✔
480
    }
481

482
    taosHashCleanup(pStmt->exec.pBlockHash);
20,052✔
483
    pStmt->exec.pBlockHash = NULL;
20,057✔
484

485
    tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
20,057✔
486
    taosMemoryFreeClear(pStmt->exec.pCurrTbData);
20,059!
487
  }
488

489
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
24,672!
490

491
  return TSDB_CODE_SUCCESS;
24,666✔
492
}
493

494
void stmtFreeTbBuf(void* buf) {
100✔
495
  void* pBuf = *(void**)buf;
100✔
496
  taosMemoryFree(pBuf);
100!
497
}
100✔
498

499
void stmtFreeTbCols(void* buf) {
72,000✔
500
  SArray* pCols = *(SArray**)buf;
72,000✔
501
  taosArrayDestroy(pCols);
72,000✔
502
}
72,000✔
503

504
int32_t stmtCleanSQLInfo(STscStmt* pStmt) {
20,105✔
505
  STMT_DLOG_E("start to free SQL info");
20,105✔
506

507
  taosMemoryFree(pStmt->sql.pBindInfo);
20,105!
508
  taosMemoryFree(pStmt->sql.queryRes.fields);
20,127!
509
  taosMemoryFree(pStmt->sql.queryRes.userFields);
20,128!
510
  taosMemoryFree(pStmt->sql.sqlStr);
20,127!
511
  qDestroyQuery(pStmt->sql.pQuery);
20,127✔
512
  taosArrayDestroy(pStmt->sql.nodeList);
20,126✔
513
  taosHashCleanup(pStmt->sql.pVgHash);
20,123✔
514
  pStmt->sql.pVgHash = NULL;
20,129✔
515

516
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
20,129✔
517
  while (pIter) {
40,166✔
518
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
20,039✔
519

520
    qDestroyStmtDataBlock(pCache->pDataCtx);
20,039✔
521
    qDestroyBoundColInfo(pCache->boundTags);
20,042✔
522
    taosMemoryFreeClear(pCache->boundTags);
20,039!
523

524
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
20,041✔
525
  }
526
  taosHashCleanup(pStmt->sql.pTableCache);
20,127✔
527
  pStmt->sql.pTableCache = NULL;
20,129✔
528

529
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
20,129!
530
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
20,126!
531

532
  taos_free_result(pStmt->sql.siInfo.pRequest);
20,122✔
533
  taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
20,128✔
534
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
20,128✔
535
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableRowDataHash);
20,121✔
536
  taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
20,127✔
537
  taosMemoryFree(pStmt->sql.siInfo.pTSchema);
20,126!
538
  qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
20,121✔
539
  taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
20,123✔
540

541
  (void)memset(&pStmt->sql, 0, sizeof(pStmt->sql));
20,118✔
542
  pStmt->sql.siInfo.tableColsReady = true;
20,118✔
543

544
  STMT_DLOG_E("end to free SQL info");
20,118✔
545

546
  return TSDB_CODE_SUCCESS;
20,116✔
547
}
548

549
int32_t stmtTryAddTableVgroupInfo(STscStmt* pStmt, int32_t* vgId) {
17✔
550
  if (*vgId >= 0 && taosHashGet(pStmt->sql.pVgHash, (const char*)vgId, sizeof(*vgId))) {
17✔
551
    return TSDB_CODE_SUCCESS;
7✔
552
  }
553

554
  SVgroupInfo      vgInfo = {0};
10✔
555
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
10✔
556
                           .requestId = pStmt->exec.pRequest->requestId,
10✔
557
                           .requestObjRefId = pStmt->exec.pRequest->self,
10✔
558
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
10✔
559

560
  int32_t code = catalogGetTableHashVgroup(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &vgInfo);
10✔
561
  if (TSDB_CODE_SUCCESS != code) {
10!
562
    return code;
×
563
  }
564

565
  code =
566
      taosHashPut(pStmt->sql.pVgHash, (const char*)&vgInfo.vgId, sizeof(vgInfo.vgId), (char*)&vgInfo, sizeof(vgInfo));
10✔
567
  if (TSDB_CODE_SUCCESS != code) {
10!
568
    return code;
×
569
  }
570

571
  *vgId = vgInfo.vgId;
10✔
572

573
  return TSDB_CODE_SUCCESS;
10✔
574
}
575

576
int32_t stmtRebuildDataBlock(STscStmt* pStmt, STableDataCxt* pDataBlock, STableDataCxt** newBlock, uint64_t uid,
17✔
577
                             uint64_t suid, int32_t vgId) {
578
  STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
17!
579
  STMT_ERR_RET(qRebuildStmtDataBlock(newBlock, pDataBlock, uid, suid, vgId, pStmt->sql.autoCreateTbl));
17!
580

581
  STMT_DLOG("uid:%" PRId64 ", rebuild table data context, vgId:%d", uid, vgId);
17!
582

583
  return TSDB_CODE_SUCCESS;
17✔
584
}
585

586
int32_t stmtGetFromCache(STscStmt* pStmt) {
40,084✔
587
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx) {
40,084!
588
    pStmt->bInfo.needParse = false;
×
589
    pStmt->bInfo.inExecCache = false;
×
590
    return TSDB_CODE_SUCCESS;
×
591
  }
592

593
  pStmt->bInfo.needParse = true;
40,084✔
594
  pStmt->bInfo.inExecCache = false;
40,084✔
595

596
  STableDataCxt** pCxtInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
40,084✔
597
  if (pCxtInExec) {
40,130✔
598
    pStmt->bInfo.needParse = false;
20,015✔
599
    pStmt->bInfo.inExecCache = true;
20,015✔
600

601
    pStmt->exec.pCurrBlock = *pCxtInExec;
20,015✔
602

603
    if (pStmt->sql.autoCreateTbl) {
20,015✔
604
      tscDebug("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
20,014!
605
      return TSDB_CODE_SUCCESS;
20,015✔
606
    }
607
  }
608

609
  if (NULL == pStmt->pCatalog) {
20,116✔
610
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
107!
611
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
110✔
612
  }
613

614
  if (NULL == pStmt->sql.pTableCache || taosHashGetSize(pStmt->sql.pTableCache) <= 0) {
20,119✔
615
    if (pStmt->bInfo.inExecCache) {
20,077!
616
      pStmt->bInfo.needParse = false;
×
617
      tscDebug("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
×
618
      return TSDB_CODE_SUCCESS;
×
619
    }
620

621
    tscDebug("no stmt block cache for tb %s", pStmt->bInfo.tbFName);
20,077✔
622
    return TSDB_CODE_SUCCESS;
20,077✔
623
  }
624

625
  if (pStmt->sql.autoCreateTbl) {
62✔
626
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &pStmt->bInfo.tbSuid, sizeof(pStmt->bInfo.tbSuid));
9✔
627
    if (pCache) {
9!
628
      pStmt->bInfo.needParse = false;
9✔
629
      pStmt->bInfo.tbUid = 0;
9✔
630

631
      STableDataCxt* pNewBlock = NULL;
9✔
632
      STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, 0, pStmt->bInfo.tbSuid, -1));
9!
633

634
      if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
9!
635
                      POINTER_BYTES)) {
636
        STMT_ERR_RET(terrno);
×
637
      }
638

639
      pStmt->exec.pCurrBlock = pNewBlock;
9✔
640

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

643
      return TSDB_CODE_SUCCESS;
9✔
644
    }
645

646
    STMT_RET(stmtCleanBindInfo(pStmt));
×
647
  }
648

649
  uint64_t uid, suid;
650
  int32_t  vgId;
651
  int8_t   tableType;
652

653
  STableMeta*      pTableMeta = NULL;
53✔
654
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
53✔
655
                           .requestId = pStmt->exec.pRequest->requestId,
53✔
656
                           .requestObjRefId = pStmt->exec.pRequest->self,
53✔
657
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
53✔
658
  int32_t          code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
26✔
659

660
  pStmt->stat.ctgGetTbMetaNum++;
26✔
661

662
  if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
26!
663
    tscDebug("tb %s not exist", pStmt->bInfo.tbFName);
×
664
    STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
665

666
    STMT_ERR_RET(code);
×
667
  }
668

669
  STMT_ERR_RET(code);
26!
670

671
  uid = pTableMeta->uid;
26✔
672
  suid = pTableMeta->suid;
26✔
673
  tableType = pTableMeta->tableType;
26✔
674
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
26✔
675
  vgId = pTableMeta->vgId;
26✔
676

677
  taosMemoryFree(pTableMeta);
26!
678

679
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
26✔
680

681
  if (uid == pStmt->bInfo.tbUid) {
26!
682
    pStmt->bInfo.needParse = false;
×
683

684
    tscDebug("tb %s is current table", pStmt->bInfo.tbFName);
×
685

686
    return TSDB_CODE_SUCCESS;
×
687
  }
688

689
  if (pStmt->bInfo.inExecCache) {
26!
690
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
×
691
    if (NULL == pCache) {
×
692
      tscError("table [%s, %" PRIx64 ", %" PRIx64 "] found in exec blockHash, but not in sql blockHash",
×
693
               pStmt->bInfo.tbFName, uid, cacheUid);
694

695
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
696
    }
697

698
    pStmt->bInfo.needParse = false;
×
699

700
    pStmt->bInfo.tbUid = uid;
×
701
    pStmt->bInfo.tbSuid = suid;
×
702
    pStmt->bInfo.tbType = tableType;
×
703
    pStmt->bInfo.boundTags = pCache->boundTags;
×
704
    pStmt->bInfo.tagsCached = true;
×
705

706
    tscDebug("tb %s in execBlock list, set to current", pStmt->bInfo.tbFName);
×
707

708
    return TSDB_CODE_SUCCESS;
×
709
  }
710

711
  SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
26✔
712
  if (pCache) {
26✔
713
    pStmt->bInfo.needParse = false;
8✔
714

715
    pStmt->bInfo.tbUid = uid;
8✔
716
    pStmt->bInfo.tbSuid = suid;
8✔
717
    pStmt->bInfo.tbType = tableType;
8✔
718
    pStmt->bInfo.boundTags = pCache->boundTags;
8✔
719
    pStmt->bInfo.tagsCached = true;
8✔
720

721
    STableDataCxt* pNewBlock = NULL;
8✔
722
    STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, uid, suid, vgId));
8!
723

724
    if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
8!
725
                    POINTER_BYTES)) {
726
      STMT_ERR_RET(terrno);
×
727
    }
728

729
    pStmt->exec.pCurrBlock = pNewBlock;
8✔
730

731
    tscDebug("tb %s in sqlBlock list, set to current", pStmt->bInfo.tbFName);
8!
732

733
    return TSDB_CODE_SUCCESS;
8✔
734
  }
735

736
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
18!
737

738
  return TSDB_CODE_SUCCESS;
18✔
739
}
740

741
int32_t stmtResetStmt(STscStmt* pStmt) {
19,964✔
742
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
19,964!
743

744
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
19,976✔
745
  if (NULL == pStmt->sql.pTableCache) {
19,974!
746
    STMT_ERR_RET(terrno);
×
747
  }
748

749
  if (pStmt->sql.siInfo.pTableRowDataHash) {
19,974!
750
    tSimpleHashClear(pStmt->sql.siInfo.pTableRowDataHash);
×
751
  }
752

753
  pStmt->sql.status = STMT_INIT;
19,977✔
754

755
  return TSDB_CODE_SUCCESS;
19,977✔
756
}
757

758
int32_t stmtAsyncOutput(STscStmt* pStmt, void* param) {
10,261✔
759
  SStmtQNode* pParam = (SStmtQNode*)param;
10,261✔
760

761
  if (pParam->restoreTbCols) {
10,261✔
762
    for (int32_t i = 0; i < pStmt->sql.siInfo.pTableColsIdx; ++i) {
10,258✔
763
      SArray** p = (SArray**)TARRAY_GET_ELEM(pStmt->sql.siInfo.pTableCols, i);
5,724✔
764
      *p = taosArrayInit(20, POINTER_BYTES);
5,724✔
765
      if (*p == NULL) {
5,714!
766
        STMT_ERR_RET(terrno);
×
767
      }
768
    }
769

770
    atomic_store_8((int8_t*)&pStmt->sql.siInfo.tableColsReady, true);
4,534✔
771
  } else {
772
    STMT_ERR_RET(qAppendStmtTableOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, &pParam->tblData, pStmt->exec.pCurrBlock,
5,713!
773
                                        &pStmt->sql.siInfo, NULL));
774

775
    // taosMemoryFree(pParam->pTbData);
776

777
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
5,715✔
778
  }
779
  return TSDB_CODE_SUCCESS;
10,271✔
780
}
781

782
void* stmtBindThreadFunc(void* param) {
100✔
783
  setThreadName("stmtBind");
100✔
784

785
  qInfo("stmt bind thread started");
100!
786

787
  STscStmt* pStmt = (STscStmt*)param;
100✔
788

789
  while (true) {
10,368✔
790
    if (pStmt->queue.stopQueue) {
10,468✔
791
      break;
100✔
792
    }
793

794
    SStmtQNode* asyncParam = NULL;
10,368✔
795
    if (!stmtDequeue(pStmt, &asyncParam)) {
10,368✔
796
      continue;
100✔
797
    }
798

799
    int ret = stmtAsyncOutput(pStmt, asyncParam);
10,263✔
800
    if (ret != 0) {
10,269!
801
      qError("stmtAsyncOutput failed, reason:%s", tstrerror(ret));
×
802
    }
803
  }
804

805
  qInfo("stmt bind thread stopped");
100!
806

807
  return NULL;
100✔
808
}
809

810
int32_t stmtStartBindThread(STscStmt* pStmt) {
100✔
811
  TdThreadAttr thAttr;
812
  if (taosThreadAttrInit(&thAttr) != 0) {
100!
813
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
814
  }
815
  if (taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE) != 0) {
100!
816
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
817
  }
818

819
  if (taosThreadCreate(&pStmt->bindThread, &thAttr, stmtBindThreadFunc, pStmt) != 0) {
100!
820
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
821
    STMT_ERR_RET(terrno);
×
822
  }
823

824
  pStmt->bindThreadInUse = true;
100✔
825

826
  (void)taosThreadAttrDestroy(&thAttr);
100✔
827
  return TSDB_CODE_SUCCESS;
100✔
828
}
829

830
int32_t stmtInitQueue(STscStmt* pStmt) {
100✔
831
  (void)taosThreadCondInit(&pStmt->queue.waitCond, NULL);
100✔
832
  (void)taosThreadMutexInit(&pStmt->queue.mutex, NULL);
100✔
833
  STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&pStmt->queue.head));
200!
834
  pStmt->queue.tail = pStmt->queue.head;
100✔
835

836
  return TSDB_CODE_SUCCESS;
100✔
837
}
838

839
int32_t stmtInitTableBuf(STableBufInfo* pTblBuf) {
100✔
840
  pTblBuf->buffUnit = sizeof(SStmtQNode);
100✔
841
  pTblBuf->buffSize = pTblBuf->buffUnit * 1000;
100✔
842
  pTblBuf->pBufList = taosArrayInit(100, POINTER_BYTES);
100✔
843
  if (NULL == pTblBuf->pBufList) {
100!
844
    return terrno;
×
845
  }
846
  void* buff = taosMemoryMalloc(pTblBuf->buffSize);
100!
847
  if (NULL == buff) {
100!
848
    return terrno;
×
849
  }
850

851
  if (taosArrayPush(pTblBuf->pBufList, &buff) == NULL) {
200!
852
    return terrno;
×
853
  }
854

855
  pTblBuf->pCurBuff = buff;
100✔
856
  pTblBuf->buffIdx = 1;
100✔
857
  pTblBuf->buffOffset = 0;
100✔
858

859
  return TSDB_CODE_SUCCESS;
100✔
860
}
861

862
TAOS_STMT* stmtInit(STscObj* taos, int64_t reqid, TAOS_STMT_OPTIONS* pOptions) {
140✔
863
  STscObj*  pObj = (STscObj*)taos;
140✔
864
  STscStmt* pStmt = NULL;
140✔
865
  int32_t   code = 0;
140✔
866

867
  pStmt = taosMemoryCalloc(1, sizeof(STscStmt));
140!
868
  if (NULL == pStmt) {
142!
869
    return NULL;
×
870
  }
871

872
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
142✔
873
  if (NULL == pStmt->sql.pTableCache) {
142!
874
    taosMemoryFree(pStmt);
×
875
    return NULL;
×
876
  }
877

878
  pStmt->taos = pObj;
142✔
879
  pStmt->bInfo.needParse = true;
142✔
880
  pStmt->sql.status = STMT_INIT;
142✔
881
  pStmt->reqid = reqid;
142✔
882
  pStmt->errCode = TSDB_CODE_SUCCESS;
142✔
883

884
  if (NULL != pOptions) {
142✔
885
    (void)memcpy(&pStmt->options, pOptions, sizeof(pStmt->options));
100✔
886
    if (pOptions->singleStbInsert && pOptions->singleTableBindOnce) {
100!
887
      pStmt->stbInterlaceMode = true;
100✔
888
    }
889
  }
890

891
  if (pStmt->stbInterlaceMode) {
142✔
892
    pStmt->sql.siInfo.transport = taos->pAppInfo->pTransporter;
100✔
893
    pStmt->sql.siInfo.acctId = taos->acctId;
100✔
894
    pStmt->sql.siInfo.dbname = taos->db;
100✔
895
    pStmt->sql.siInfo.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
100✔
896
    pStmt->sql.siInfo.pTableHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
100✔
897
    if (NULL == pStmt->sql.siInfo.pTableHash) {
100!
898
      (void)stmtClose(pStmt);
×
899
      return NULL;
×
900
    }
901
    pStmt->sql.siInfo.pTableRowDataHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
100✔
902
    if (NULL == pStmt->sql.siInfo.pTableRowDataHash) {
100!
903
      STMT_ELOG("fail to allocate memory for pTableRowDataHash:%s", tstrerror(terrno));
×
904
      (void)stmtClose(pStmt);
×
905
      return NULL;
×
906
    }
907
    pStmt->sql.siInfo.pTableCols = taosArrayInit(STMT_TABLE_COLS_NUM, POINTER_BYTES);
100✔
908
    if (NULL == pStmt->sql.siInfo.pTableCols) {
100!
909
      (void)stmtClose(pStmt);
×
910
      return NULL;
×
911
    }
912

913
    code = stmtInitTableBuf(&pStmt->sql.siInfo.tbBuf);
100✔
914
    if (TSDB_CODE_SUCCESS == code) {
100!
915
      code = stmtInitQueue(pStmt);
100✔
916
    }
917
    if (TSDB_CODE_SUCCESS == code) {
100!
918
      code = stmtStartBindThread(pStmt);
100✔
919
    }
920
    if (TSDB_CODE_SUCCESS != code) {
100!
921
      terrno = code;
×
922
      (void)stmtClose(pStmt);
×
923
      return NULL;
×
924
    }
925
  }
926

927
  pStmt->sql.siInfo.tableColsReady = true;
142✔
928

929
  STMT_LOG_SEQ(STMT_INIT);
142✔
930

931
  tscDebug("stmt:%p initialized", pStmt);
142✔
932

933
  return pStmt;
142✔
934
}
935

936
int stmtPrepare(TAOS_STMT* stmt, const char* sql, unsigned long length) {
20,108✔
937
  STscStmt* pStmt = (STscStmt*)stmt;
20,108✔
938

939
  STMT_DLOG_E("start to prepare");
20,108✔
940

941
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
20,122!
942
    return pStmt->errCode;
×
943
  }
944

945
  if (pStmt->sql.status >= STMT_PREPARE) {
20,122✔
946
    STMT_ERR_RET(stmtResetStmt(pStmt));
19,979!
947
  }
948

949
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
20,116!
950

951
  if (length <= 0) {
20,119✔
952
    length = strlen(sql);
20✔
953
  }
954

955
  pStmt->sql.sqlStr = taosStrndup(sql, length);
20,119!
956
  if (!pStmt->sql.sqlStr) {
20,114!
957
    return terrno;
×
958
  }
959
  pStmt->sql.sqlLen = length;
20,114✔
960
  pStmt->sql.stbInterlaceMode = pStmt->stbInterlaceMode;
20,114✔
961

962
  char* dbName = NULL;
20,114✔
963
  if (qParseDbName(sql, length, &dbName)) {
20,114✔
964
    STMT_ERR_RET(stmtSetDbName(stmt, dbName));
3!
965
    taosMemoryFreeClear(dbName);
3!
966
  }
967

968
  return TSDB_CODE_SUCCESS;
20,097✔
969
}
970

971
int32_t stmtInitStbInterlaceTableInfo(STscStmt* pStmt) {
68✔
972
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
68✔
973
  if (!pSrc) {
70!
974
    return terrno;
×
975
  }
976
  STableDataCxt* pDst = NULL;
70✔
977

978
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
70!
979
  pStmt->sql.siInfo.pDataCtx = pDst;
63✔
980

981
  SArray* pTblCols = NULL;
63✔
982
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
57,705✔
983
    pTblCols = taosArrayInit(20, POINTER_BYTES);
57,289✔
984
    if (NULL == pTblCols) {
60,552!
985
      return terrno;
×
986
    }
987

988
    if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
118,194!
989
      return terrno;
×
990
    }
991
  }
992

993
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
416✔
994

995
  return TSDB_CODE_SUCCESS;
416✔
996
}
997

998
int stmtSetDbName(TAOS_STMT* stmt, const char* dbName) {
3✔
999
  STscStmt* pStmt = (STscStmt*)stmt;
3✔
1000

1001
  STMT_DLOG("start to set dbName:%s", dbName);
3!
1002

1003
  STMT_ERR_RET(stmtCreateRequest(pStmt));
3!
1004

1005
  // The SQL statement specifies a database name, overriding the previously specified database
1006
  taosMemoryFreeClear(pStmt->exec.pRequest->pDb);
3!
1007
  pStmt->exec.pRequest->pDb = taosStrdup(dbName);
3!
1008
  if (pStmt->exec.pRequest->pDb == NULL) {
3!
1009
    return terrno;
×
1010
  }
1011
  return TSDB_CODE_SUCCESS;
3✔
1012
}
1013

1014
int stmtSetTbName(TAOS_STMT* stmt, const char* tbName) {
45,718✔
1015
  STscStmt* pStmt = (STscStmt*)stmt;
45,718✔
1016

1017
  int64_t startUs = taosGetTimestampUs();
45,752✔
1018

1019
  STMT_DLOG("start to set tbName:%s", tbName);
45,752✔
1020

1021
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
45,756!
1022
    return pStmt->errCode;
×
1023
  }
1024

1025
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
45,756!
1026

1027
  int32_t insert = 0;
45,738✔
1028
  STMT_ERR_RET(stmtIsInsert(stmt, &insert));
45,738!
1029
  if (0 == insert) {
45,748!
1030
    tscError("set tb name not available for none insert statement");
×
1031
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1032
  }
1033

1034
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
45,748✔
1035
    STMT_ERR_RET(stmtCreateRequest(pStmt));
40,123!
1036

1037
    STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
40,130!
1038
                              pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1039
    STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
40,140!
1040

1041
    STMT_ERR_RET(stmtGetFromCache(pStmt));
40,107!
1042

1043
    if (pStmt->bInfo.needParse) {
40,126✔
1044
      tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
20,107✔
1045
      pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
20,107✔
1046

1047
      STMT_ERR_RET(stmtParseSql(pStmt));
20,107✔
1048
    }
1049
  } else {
1050
    tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
5,625✔
1051
    pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
5,625✔
1052
    pStmt->exec.pRequest->requestId++;
5,625✔
1053
    pStmt->bInfo.needParse = false;
5,625✔
1054
  }
1055

1056
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
45,712✔
1057
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
71!
1058
  }
1059

1060
  int64_t startUs2 = taosGetTimestampUs();
45,755✔
1061
  pStmt->stat.setTbNameUs += startUs2 - startUs;
45,755✔
1062

1063
  return TSDB_CODE_SUCCESS;
45,755✔
1064
}
1065

1066
int stmtSetTbTags(TAOS_STMT* stmt, TAOS_MULTI_BIND* tags) {
15✔
1067
  STscStmt* pStmt = (STscStmt*)stmt;
15✔
1068

1069
  STMT_DLOG_E("start to set tbTags");
15✔
1070

1071
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
15!
1072
    return pStmt->errCode;
×
1073
  }
1074

1075
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
15!
1076

1077
  SBoundColInfo* tags_info = (SBoundColInfo*)pStmt->bInfo.boundTags;
15✔
1078
  if (tags_info->numOfBound <= 0 || tags_info->numOfCols <= 0) {
15!
1079
    tscWarn("no tags bound in sql, will not bound tags");
×
1080
    return TSDB_CODE_SUCCESS;
×
1081
  }
1082

1083
  if (pStmt->bInfo.inExecCache) {
15!
1084
    return TSDB_CODE_SUCCESS;
×
1085
  }
1086

1087
  STableDataCxt** pDataBlock =
1088
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
15✔
1089
  if (NULL == pDataBlock) {
15!
1090
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1091
    STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1092
  }
1093

1094
  tscDebug("start to bind stmt tag values");
15✔
1095
  STMT_ERR_RET(qBindStmtTagsValue(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.tbSuid, pStmt->bInfo.stbFName,
15!
1096
                                  pStmt->bInfo.sname.tname, tags, pStmt->exec.pRequest->msgBuf,
1097
                                  pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt));
1098

1099
  return TSDB_CODE_SUCCESS;
15✔
1100
}
1101

1102
int stmtFetchTagFields(STscStmt* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
2✔
1103
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
2✔
1104
    return pStmt->errCode;
1✔
1105
  }
1106

1107
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
1!
1108
    tscError("invalid operation to get query tag fileds");
×
1109
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1110
  }
1111

1112
  STableDataCxt** pDataBlock =
1113
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
1✔
1114
  if (NULL == pDataBlock) {
1!
1115
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1116
    STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1117
  }
1118

1119
  STMT_ERR_RET(qBuildStmtTagFields(*pDataBlock, pStmt->bInfo.boundTags, fieldNum, fields));
1!
1120

1121
  return TSDB_CODE_SUCCESS;
1✔
1122
}
1123

1124
int stmtFetchColFields(STscStmt* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
1,059✔
1125
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
1,059!
1126
    return pStmt->errCode;
×
1127
  }
1128

1129
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
1,059!
1130
    tscError("invalid operation to get query column fileds");
×
1131
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1132
  }
1133

1134
  STableDataCxt** pDataBlock = NULL;
1,059✔
1135

1136
  if (pStmt->sql.stbInterlaceMode) {
1,059!
1137
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
×
1138
  } else {
1139
    pDataBlock =
1140
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
1,059✔
1141
    if (NULL == pDataBlock) {
1,059!
1142
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1143
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1144
    }
1145
  }
1146

1147
  STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields));
1,059!
1148

1149
  return TSDB_CODE_SUCCESS;
1,059✔
1150
}
1151

1152
/*
1153
SArray* stmtGetFreeCol(STscStmt* pStmt, int32_t* idx) {
1154
  while (true) {
1155
    if (pStmt->exec.smInfo.pColIdx >= STMT_COL_BUF_SIZE) {
1156
      pStmt->exec.smInfo.pColIdx = 0;
1157
    }
1158

1159
    if ((pStmt->exec.smInfo.pColIdx + 1) == atomic_load_32(&pStmt->exec.smInfo.pColFreeIdx)) {
1160
      taosUsleep(1);
1161
      continue;
1162
    }
1163

1164
    *idx = pStmt->exec.smInfo.pColIdx;
1165
    return pStmt->exec.smInfo.pCols[pStmt->exec.smInfo.pColIdx++];
1166
  }
1167
}
1168
*/
1169

1170
int32_t stmtAppendTablePostHandle(STscStmt* pStmt, SStmtQNode* param) {
5,711✔
1171
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
5,711✔
1172
    pStmt->sql.siInfo.pVgroupHash =
4,543✔
1173
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
4,541✔
1174
  }
1175
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
5,713✔
1176
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
4,544✔
1177
  }
1178

1179
  if (NULL == pStmt->sql.siInfo.pRequest) {
5,708✔
1180
    STMT_ERR_RET(buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false,
71!
1181
                              (SRequestObj**)&pStmt->sql.siInfo.pRequest, pStmt->reqid));
1182

1183
    if (pStmt->reqid != 0) {
70!
1184
      pStmt->reqid++;
×
1185
    }
1186
    pStmt->exec.pRequest->syncQuery = true;
70✔
1187

1188
    pStmt->sql.siInfo.requestId = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->requestId;
70✔
1189
    pStmt->sql.siInfo.requestSelf = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->self;
70✔
1190
  }
1191

1192
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
5,707✔
1193
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
176✔
1194
    pStmt->sql.siInfo.tbFromHash = true;
24✔
1195
  }
1196

1197
  if (0 == pStmt->sql.siInfo.firstName[0]) {
5,707✔
1198
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
71✔
1199
  }
1200

1201
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
5,707✔
1202
  param->next = NULL;
5,707✔
1203

1204
  (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
5,707✔
1205

1206
  stmtEnqueue(pStmt, param);
5,724✔
1207

1208
  return TSDB_CODE_SUCCESS;
5,717✔
1209
}
1210

1211
static FORCE_INLINE int32_t stmtGetTableColsFromCache(STscStmt* pStmt, SArray** pTableCols) {
1212
  while (true) {
1213
    if (pStmt->sql.siInfo.pTableColsIdx < taosArrayGetSize(pStmt->sql.siInfo.pTableCols)) {
5,716!
1214
      *pTableCols = (SArray*)taosArrayGetP(pStmt->sql.siInfo.pTableCols, pStmt->sql.siInfo.pTableColsIdx++);
5,707✔
1215
      break;
5,716✔
1216
    } else {
1217
      SArray* pTblCols = NULL;
×
1218
      for (int32_t i = 0; i < 100; i++) {
×
1219
        pTblCols = taosArrayInit(20, POINTER_BYTES);
×
1220
        if (NULL == pTblCols) {
×
1221
          return terrno;
×
1222
        }
1223

1224
        if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
×
1225
          return terrno;
×
1226
        }
1227
      }
1228
    }
1229
  }
1230

1231
  return TSDB_CODE_SUCCESS;
5,716✔
1232
}
1233

1234
int stmtBindBatch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind, int32_t colIdx) {
46,325✔
1235
  STscStmt* pStmt = (STscStmt*)stmt;
46,325✔
1236
  int32_t   code = 0;
46,325✔
1237

1238
  int64_t startUs = taosGetTimestampUs();
46,343✔
1239

1240
  STMT_DLOG("start to bind stmt data, colIdx:%d", colIdx);
46,343✔
1241

1242
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
46,354!
1243
    return pStmt->errCode;
×
1244
  }
1245

1246
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
46,354!
1247

1248
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
46,344!
1249
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1250
    pStmt->bInfo.needParse = false;
×
1251
  }
1252

1253
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
46,344!
1254
    taos_free_result(pStmt->exec.pRequest);
×
1255
    pStmt->exec.pRequest = NULL;
×
1256
  }
1257

1258
  STMT_ERR_RET(stmtCreateRequest(pStmt));
46,344!
1259

1260
  if (pStmt->bInfo.needParse) {
46,331✔
1261
    STMT_ERR_RET(stmtParseSql(pStmt));
24✔
1262
  }
1263

1264
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
46,316✔
1265
    STMT_ERR_RET(qStmtBindParams(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt));
17!
1266

1267
    SParseContext ctx = {.requestId = pStmt->exec.pRequest->requestId,
17✔
1268
                         .acctId = pStmt->taos->acctId,
17✔
1269
                         .db = pStmt->exec.pRequest->pDb,
17✔
1270
                         .topicQuery = false,
1271
                         .pSql = pStmt->sql.sqlStr,
17✔
1272
                         .sqlLen = pStmt->sql.sqlLen,
17✔
1273
                         .pMsg = pStmt->exec.pRequest->msgBuf,
17✔
1274
                         .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1275
                         .pTransporter = pStmt->taos->pAppInfo->pTransporter,
17✔
1276
                         .pStmtCb = NULL,
1277
                         .pUser = pStmt->taos->user,
17✔
1278
                         .setQueryFp = setQueryRequest};
1279

1280
    ctx.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
17✔
1281
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &ctx.pCatalog));
17!
1282

1283
    STMT_ERR_RET(qStmtParseQuerySql(&ctx, pStmt->sql.pQuery));
17!
1284

1285
    if (pStmt->sql.pQuery->haveResultSet) {
17!
1286
      STMT_ERR_RET(setResSchemaInfo(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->pResSchema,
17!
1287
                                    pStmt->sql.pQuery->numOfResCols, pStmt->sql.pQuery->pResExtSchema, true));
1288
      taosMemoryFreeClear(pStmt->sql.pQuery->pResSchema);
17!
1289
      taosMemoryFreeClear(pStmt->sql.pQuery->pResExtSchema);
17!
1290
      setResPrecision(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->precision);
17✔
1291
    }
1292

1293
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
17✔
1294
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
17✔
1295
    TSWAP(pStmt->exec.pRequest->targetTableList, pStmt->sql.pQuery->pTargetTableList);
17✔
1296

1297
    // if (STMT_TYPE_QUERY == pStmt->sql.queryRes) {
1298
    //   STMT_ERR_RET(stmtRestoreQueryFields(pStmt));
1299
    // }
1300

1301
    // STMT_ERR_RET(stmtBackupQueryFields(pStmt));
1302

1303
    return TSDB_CODE_SUCCESS;
17✔
1304
  }
1305

1306
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
46,299!
1307
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1308
  }
1309

1310
  STableDataCxt** pDataBlock = NULL;
46,306✔
1311

1312
  if (pStmt->exec.pCurrBlock) {
46,306✔
1313
    pDataBlock = &pStmt->exec.pCurrBlock;
26,224✔
1314
  } else {
1315
    pDataBlock =
1316
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
20,082✔
1317
    if (NULL == pDataBlock) {
20,116!
1318
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1319
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1320
    }
1321
    pStmt->exec.pCurrBlock = *pDataBlock;
20,116✔
1322
    if (pStmt->sql.stbInterlaceMode) {
20,116✔
1323
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
72✔
1324
      pStmt->exec.pCurrBlock->pData->aCol = NULL;
72✔
1325
    }
1326
  }
1327

1328
  int64_t startUs2 = taosGetTimestampUs();
46,341✔
1329
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
46,341✔
1330

1331
  SStmtQNode* param = NULL;
46,341✔
1332
  if (pStmt->sql.stbInterlaceMode) {
46,341✔
1333
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
11,426!
1334
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
11,432!
1335
    taosArrayClear(param->tblData.aCol);
5,716✔
1336

1337
    // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES);
1338

1339
    param->restoreTbCols = false;
5,702✔
1340
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
5,702✔
1341
  }
1342

1343
  int64_t startUs3 = taosGetTimestampUs();
46,318✔
1344
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
46,318✔
1345

1346
  SArray* pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
46,318✔
1347

1348
  if (colIdx < 0) {
46,318✔
1349
    if (pStmt->sql.stbInterlaceMode) {
45,888✔
1350
      (*pDataBlock)->pData->flags = 0;
5,713✔
1351
      code =
1352
          qBindStmtStbColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
5,713✔
1353
                                &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt);
5,713✔
1354
    } else {
1355
      code = qBindStmtColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
40,175✔
1356
                                pStmt->taos->optionInfo.charsetCxt);
40,175✔
1357
    }
1358

1359
    if (code) {
45,884✔
1360
      tscError("qBindStmtColsValue failed, error:%s", tstrerror(code));
1!
1361
      STMT_ERR_RET(code);
1!
1362
    }
1363
  } else {
1364
    if (pStmt->sql.stbInterlaceMode) {
430!
1365
      tscError("bind single column not allowed in stb insert mode");
×
1366
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1367
    }
1368

1369
    if (colIdx != (pStmt->bInfo.sBindLastIdx + 1) && colIdx != 0) {
430!
1370
      tscError("bind column index not in sequence");
×
1371
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1372
    }
1373

1374
    pStmt->bInfo.sBindLastIdx = colIdx;
430✔
1375

1376
    if (0 == colIdx) {
430✔
1377
      pStmt->bInfo.sBindRowNum = bind->num;
75✔
1378
    }
1379

1380
    code =
1381
        qBindStmtSingleColValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
430✔
1382
                                colIdx, pStmt->bInfo.sBindRowNum, pStmt->taos->optionInfo.charsetCxt);
430✔
1383
    if (code) {
450!
1384
      tscError("qBindStmtSingleColValue failed, error:%s", tstrerror(code));
×
1385
      STMT_ERR_RET(code);
×
1386
    }
1387
  }
1388

1389
  int64_t startUs4 = taosGetTimestampUs();
46,320✔
1390
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
46,320✔
1391

1392
  if (pStmt->sql.stbInterlaceMode) {
46,320✔
1393
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
5,713!
1394
  }
1395

1396
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
46,349✔
1397

1398
  return TSDB_CODE_SUCCESS;
46,349✔
1399
}
1400

1401
int stmtAddBatch(TAOS_STMT* stmt) {
44,757✔
1402
  STscStmt* pStmt = (STscStmt*)stmt;
44,757✔
1403

1404
  int64_t startUs = taosGetTimestampUs();
44,769✔
1405

1406
  STMT_DLOG_E("start to add batch");
44,769✔
1407

1408
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
44,757!
1409
    return pStmt->errCode;
×
1410
  }
1411

1412
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
44,757✔
1413

1414
  if (pStmt->sql.stbInterlaceMode) {
44,765✔
1415
    int64_t startUs2 = taosGetTimestampUs();
4,545✔
1416
    pStmt->stat.addBatchUs += startUs2 - startUs;
4,545✔
1417

1418
    pStmt->sql.siInfo.tableColsReady = false;
4,545✔
1419

1420
    SStmtQNode* param = NULL;
4,545✔
1421
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
9,089!
1422
    param->restoreTbCols = true;
4,544✔
1423
    param->next = NULL;
4,544✔
1424

1425
    stmtEnqueue(pStmt, param);
4,544✔
1426

1427
    return TSDB_CODE_SUCCESS;
4,546✔
1428
  }
1429

1430
  STMT_ERR_RET(stmtCacheBlock(pStmt));
40,220!
1431

1432
  return TSDB_CODE_SUCCESS;
40,229✔
1433
}
1434
/*
1435
int stmtUpdateTableUid(STscStmt* pStmt, SSubmitRsp* pRsp) {
1436
  tscDebug("stmt start to update tbUid, blockNum:%d", pRsp->nBlocks);
1437

1438
  int32_t code = 0;
1439
  int32_t finalCode = 0;
1440
  size_t  keyLen = 0;
1441
  void*   pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
1442
  while (pIter) {
1443
    STableDataCxt* pBlock = *(STableDataCxt**)pIter;
1444
    char*          key = taosHashGetKey(pIter, &keyLen);
1445

1446
    STableMeta* pMeta = qGetTableMetaInDataBlock(pBlock);
1447
    if (pMeta->uid) {
1448
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1449
      continue;
1450
    }
1451

1452
    SSubmitBlkRsp* blkRsp = NULL;
1453
    int32_t        i = 0;
1454
    for (; i < pRsp->nBlocks; ++i) {
1455
      blkRsp = pRsp->pBlocks + i;
1456
      if (strlen(blkRsp->tblFName) != keyLen) {
1457
        continue;
1458
      }
1459

1460
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
1461
        continue;
1462
      }
1463

1464
      break;
1465
    }
1466

1467
    if (i < pRsp->nBlocks) {
1468
      tscDebug("auto created table %s uid updated from %" PRIx64 " to %" PRIx64, blkRsp->tblFName, pMeta->uid,
1469
               blkRsp->uid);
1470

1471
      pMeta->uid = blkRsp->uid;
1472
      pStmt->bInfo.tbUid = blkRsp->uid;
1473
    } else {
1474
      tscDebug("table %s not found in submit rsp, will update from catalog", pStmt->bInfo.tbFName);
1475
      if (NULL == pStmt->pCatalog) {
1476
        code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog);
1477
        if (code) {
1478
          pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1479
          finalCode = code;
1480
          continue;
1481
        }
1482
      }
1483

1484
      code = stmtCreateRequest(pStmt);
1485
      if (code) {
1486
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1487
        finalCode = code;
1488
        continue;
1489
      }
1490

1491
      STableMeta*      pTableMeta = NULL;
1492
      SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
1493
                               .requestId = pStmt->exec.pRequest->requestId,
1494
                               .requestObjRefId = pStmt->exec.pRequest->self,
1495
                               .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
1496
      code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
1497

1498
      pStmt->stat.ctgGetTbMetaNum++;
1499

1500
      taos_free_result(pStmt->exec.pRequest);
1501
      pStmt->exec.pRequest = NULL;
1502

1503
      if (code || NULL == pTableMeta) {
1504
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1505
        finalCode = code;
1506
        taosMemoryFree(pTableMeta);
1507
        continue;
1508
      }
1509

1510
      pMeta->uid = pTableMeta->uid;
1511
      pStmt->bInfo.tbUid = pTableMeta->uid;
1512
      taosMemoryFree(pTableMeta);
1513
    }
1514

1515
    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1516
  }
1517

1518
  return finalCode;
1519
}
1520
*/
1521

1522
/*
1523
int stmtStaticModeExec(TAOS_STMT* stmt) {
1524
  STscStmt*   pStmt = (STscStmt*)stmt;
1525
  int32_t     code = 0;
1526
  SSubmitRsp* pRsp = NULL;
1527
  if (pStmt->sql.staticMode) {
1528
    return TSDB_CODE_TSC_STMT_API_ERROR;
1529
  }
1530

1531
  STMT_DLOG_E("start to exec");
1532

1533
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
1534

1535
  STMT_ERR_RET(qBuildStmtOutputFromTbList(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pTbBlkList,
1536
pStmt->exec.pCurrBlock, pStmt->exec.tbBlkNum));
1537

1538
  launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
1539

1540
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
1541
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
1542
    if (code) {
1543
      pStmt->exec.pRequest->code = code;
1544
    } else {
1545
      tFreeSSubmitRsp(pRsp);
1546
      STMT_ERR_RET(stmtResetStmt(pStmt));
1547
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
1548
    }
1549
  }
1550

1551
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
1552

1553
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
1554
  pStmt->affectedRows += pStmt->exec.affectedRows;
1555

1556
_return:
1557

1558
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
1559

1560
  tFreeSSubmitRsp(pRsp);
1561

1562
  ++pStmt->sql.runTimes;
1563

1564
  STMT_RET(code);
1565
}
1566
*/
1567

1568
int stmtExec(TAOS_STMT* stmt) {
44,582✔
1569
  STscStmt*   pStmt = (STscStmt*)stmt;
44,582✔
1570
  int32_t     code = 0;
44,582✔
1571
  SSubmitRsp* pRsp = NULL;
44,582✔
1572

1573
  int64_t startUs = taosGetTimestampUs();
44,610✔
1574

1575
  STMT_DLOG_E("start to exec");
44,610✔
1576

1577
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
44,615!
1578
    return pStmt->errCode;
×
1579
  }
1580

1581
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
44,615✔
1582

1583
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
44,581✔
1584
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
17✔
1585
  } else {
1586
    if (pStmt->sql.stbInterlaceMode) {
44,564✔
1587
      int64_t startTs = taosGetTimestampUs();
4,545✔
1588
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
10,751✔
1589
        taosUsleep(1);
6,206✔
1590
      }
1591
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
4,546✔
1592

1593
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
4,546!
1594
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
4,547✔
1595
      pStmt->sql.siInfo.pVgroupHash = NULL;
4,545✔
1596
      pStmt->sql.siInfo.pVgroupList = NULL;
4,545✔
1597
    } else {
1598
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
40,019✔
1599
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
40,033!
1600

1601
      STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
40,033!
1602

1603
      STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
40,053!
1604
    }
1605

1606
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
44,562✔
1607
  }
1608

1609
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
44,615!
1610
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
×
1611
    if (code) {
×
1612
      pStmt->exec.pRequest->code = code;
×
1613
    } else {
1614
      tFreeSSubmitRsp(pRsp);
×
1615
      STMT_ERR_RET(stmtResetStmt(pStmt));
×
1616
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
×
1617
    }
1618
  }
1619

1620
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
44,615✔
1621

1622
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
44,614✔
1623
  pStmt->affectedRows += pStmt->exec.affectedRows;
44,622✔
1624

1625
_return:
44,623✔
1626

1627
  while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
44,623!
1628
    taosUsleep(1);
×
1629
  }
1630

1631
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
44,616!
1632

1633
  tFreeSSubmitRsp(pRsp);
44,618✔
1634

1635
  ++pStmt->sql.runTimes;
44,612✔
1636

1637
  int64_t startUs2 = taosGetTimestampUs();
44,621✔
1638
  pStmt->stat.execUseUs += startUs2 - startUs;
44,621✔
1639

1640
  STMT_RET(code);
44,621✔
1641
}
1642

1643
int stmtClose(TAOS_STMT* stmt) {
140✔
1644
  STscStmt* pStmt = (STscStmt*)stmt;
140✔
1645

1646
  STMT_DLOG_E("start to free stmt");
140✔
1647

1648
  if (pStmt->bindThreadInUse) {
140✔
1649
    pStmt->queue.stopQueue = true;
100✔
1650

1651
    (void)taosThreadMutexLock(&pStmt->queue.mutex);
100✔
1652
    (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
100✔
1653
    (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
100✔
1654
    (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
100✔
1655

1656
    (void)taosThreadJoin(pStmt->bindThread, NULL);
100✔
1657
    pStmt->bindThreadInUse = false;
100✔
1658

1659
    (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
100✔
1660
    (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
100✔
1661
  }
1662

1663
  STMT_DLOG("stmt %p closed, stbInterlaceMode:%d, statInfo: ctgGetTbMetaNum=>%" PRId64 ", getCacheTbInfo=>%" PRId64
140✔
1664
            ", parseSqlNum=>%" PRId64 ", pStmt->stat.bindDataNum=>%" PRId64
1665
            ", settbnameAPI:%u, bindAPI:%u, addbatchAPI:%u, execAPI:%u"
1666
            ", setTbNameUs:%" PRId64 ", bindDataUs:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64 " addBatchUs:%" PRId64
1667
            ", execWaitUs:%" PRId64 ", execUseUs:%" PRId64,
1668
            pStmt, pStmt->sql.stbInterlaceMode, pStmt->stat.ctgGetTbMetaNum, pStmt->stat.getCacheTbInfo,
1669
            pStmt->stat.parseSqlNum, pStmt->stat.bindDataNum, pStmt->seqIds[STMT_SETTBNAME], pStmt->seqIds[STMT_BIND],
1670
            pStmt->seqIds[STMT_ADD_BATCH], pStmt->seqIds[STMT_EXECUTE], pStmt->stat.setTbNameUs,
1671
            pStmt->stat.bindDataUs1, pStmt->stat.bindDataUs2, pStmt->stat.bindDataUs3, pStmt->stat.bindDataUs4,
1672
            pStmt->stat.addBatchUs, pStmt->stat.execWaitUs, pStmt->stat.execUseUs);
1673

1674
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
140!
1675
  taosMemoryFree(stmt);
140!
1676

1677
  return TSDB_CODE_SUCCESS;
140✔
1678
}
1679

1680
const char* stmtErrstr(TAOS_STMT* stmt) {
4✔
1681
  STscStmt* pStmt = (STscStmt*)stmt;
4✔
1682

1683
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
4!
1684
    return (char*)tstrerror(terrno);
1✔
1685
  }
1686

1687
  pStmt->exec.pRequest->code = terrno;
3✔
1688

1689
  return taos_errstr(pStmt->exec.pRequest);
3✔
1690
}
1691

1692
int stmtAffectedRows(TAOS_STMT* stmt) { return ((STscStmt*)stmt)->affectedRows; }
8✔
1693

1694
int stmtAffectedRowsOnce(TAOS_STMT* stmt) { return ((STscStmt*)stmt)->exec.affectedRows; }
28✔
1695

1696
int stmtIsInsert(TAOS_STMT* stmt, int* insert) {
91,936✔
1697
  STscStmt* pStmt = (STscStmt*)stmt;
91,936✔
1698

1699
  STMT_DLOG_E("start is insert");
91,936✔
1700

1701
  if (pStmt->sql.type) {
91,984✔
1702
    *insert = (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
71,902!
1703
  } else {
1704
    *insert = qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
20,082✔
1705
  }
1706

1707
  return TSDB_CODE_SUCCESS;
92,006✔
1708
}
1709

1710
int stmtGetTagFields(TAOS_STMT* stmt, int* nums, TAOS_FIELD_E** fields) {
4✔
1711
  int32_t   code = 0;
4✔
1712
  STscStmt* pStmt = (STscStmt*)stmt;
4✔
1713
  int32_t   preCode = pStmt->errCode;
4✔
1714

1715
  STMT_DLOG_E("start to get tag fields");
4!
1716

1717
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
4!
1718
    return pStmt->errCode;
×
1719
  }
1720

1721
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
4!
1722
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1723
  }
1724

1725
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
4!
1726

1727
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
4!
1728
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1729
    pStmt->bInfo.needParse = false;
×
1730
  }
1731

1732
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
4!
1733
    taos_free_result(pStmt->exec.pRequest);
×
1734
    pStmt->exec.pRequest = NULL;
×
1735
  }
1736

1737
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
4!
1738

1739
  if (pStmt->bInfo.needParse) {
4✔
1740
    STMT_ERRI_JRET(stmtParseSql(pStmt));
3✔
1741
  }
1742

1743
  STMT_ERRI_JRET(stmtFetchTagFields(stmt, nums, fields));
2✔
1744

1745
_return:
1✔
1746
  // compatible with previous versions
1747
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && (pStmt->bInfo.tbNameFlag & NO_DATA_USING_CLAUSE) == 0x0) {
4!
1748
    code = TSDB_CODE_TSC_STMT_TBNAME_ERROR;
1✔
1749
  }
1750

1751
  if (code != TSDB_CODE_SUCCESS) {
4✔
1752
    taos_free_result(pStmt->exec.pRequest);
3✔
1753
    pStmt->exec.pRequest = NULL;
3✔
1754
  }
1755
  pStmt->errCode = preCode;
4✔
1756

1757
  return code;
4✔
1758
}
1759

1760
int stmtGetColFields(TAOS_STMT* stmt, int* nums, TAOS_FIELD_E** fields) {
152✔
1761
  int32_t   code = 0;
152✔
1762
  STscStmt* pStmt = (STscStmt*)stmt;
152✔
1763
  int32_t   preCode = pStmt->errCode;
152✔
1764

1765
  STMT_DLOG_E("start to get col fields");
152!
1766

1767
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
152!
1768
    return pStmt->errCode;
×
1769
  }
1770

1771
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
152!
1772
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1773
  }
1774

1775
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
152!
1776

1777
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
152!
1778
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1779
    pStmt->bInfo.needParse = false;
×
1780
  }
1781

1782
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
152!
1783
    taos_free_result(pStmt->exec.pRequest);
×
1784
    pStmt->exec.pRequest = NULL;
×
1785
    STMT_ERR_RET(stmtCreateRequest(pStmt));
×
1786
  }
1787

1788
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
152!
1789

1790
  if (pStmt->bInfo.needParse) {
152✔
1791
    STMT_ERRI_JRET(stmtParseSql(pStmt));
1!
1792
  }
1793

1794
  STMT_ERRI_JRET(stmtFetchColFields(stmt, nums, fields));
152!
1795

1796
_return:
152✔
1797

1798
  if (code != TSDB_CODE_SUCCESS) {
152!
1799
    taos_free_result(pStmt->exec.pRequest);
×
1800
    pStmt->exec.pRequest = NULL;
×
1801
  }
1802

1803
  pStmt->errCode = preCode;
152✔
1804

1805
  return code;
152✔
1806
}
1807

1808
int stmtGetParamNum(TAOS_STMT* stmt, int* nums) {
2✔
1809
  int       code = 0;
2✔
1810
  STscStmt* pStmt = (STscStmt*)stmt;
2✔
1811
  int32_t   preCode = pStmt->errCode;
2✔
1812

1813
  STMT_DLOG_E("start to get param num");
2!
1814

1815
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
2!
1816
    return pStmt->errCode;
×
1817
  }
1818

1819
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
2!
1820

1821
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
2!
1822
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1823
    pStmt->bInfo.needParse = false;
×
1824
  }
1825

1826
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
2!
1827
    taos_free_result(pStmt->exec.pRequest);
×
1828
    pStmt->exec.pRequest = NULL;
×
1829
  }
1830

1831
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
2!
1832

1833
  if (pStmt->bInfo.needParse) {
2✔
1834
    STMT_ERRI_JRET(stmtParseSql(pStmt));
1!
1835
  }
1836

1837
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
1!
1838
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
×
1839
  } else {
1840
    STMT_ERRI_JRET(stmtFetchColFields(stmt, nums, NULL));
1!
1841
  }
1842

1843
_return:
1✔
1844

1845
  if (code != TSDB_CODE_SUCCESS) {
2✔
1846
    taos_free_result(pStmt->exec.pRequest);
1✔
1847
    pStmt->exec.pRequest = NULL;
1✔
1848
  }
1849

1850
  pStmt->errCode = preCode;
2✔
1851

1852
  return code;
2✔
1853
}
1854

1855
int stmtGetParam(TAOS_STMT* stmt, int idx, int* type, int* bytes) {
906✔
1856
  int       code = 0;
906✔
1857
  STscStmt* pStmt = (STscStmt*)stmt;
906✔
1858
  int32_t   preCode = pStmt->errCode;
906✔
1859

1860
  STMT_DLOG_E("start to get param");
906!
1861

1862
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
906!
1863
    return pStmt->errCode;
×
1864
  }
1865

1866
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
906!
1867
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1868
  }
1869

1870
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
906!
1871

1872
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
906!
1873
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1874
    pStmt->bInfo.needParse = false;
×
1875
  }
1876

1877
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
906!
1878
    taos_free_result(pStmt->exec.pRequest);
×
1879
    pStmt->exec.pRequest = NULL;
×
1880
  }
1881

1882
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
906!
1883

1884
  if (pStmt->bInfo.needParse) {
906!
1885
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
1886
  }
1887

1888
  int32_t       nums = 0;
906✔
1889
  TAOS_FIELD_E* pField = NULL;
906✔
1890
  STMT_ERRI_JRET(stmtFetchColFields(stmt, &nums, &pField));
906!
1891
  if (idx >= nums) {
906!
1892
    tscError("idx %d is too big", idx);
×
1893
    STMT_ERRI_JRET(TSDB_CODE_INVALID_PARA);
×
1894
  }
1895

1896
  *type = pField[idx].type;
906✔
1897
  *bytes = calcSchemaBytesFromTypeBytes(pField[idx].type, pField[idx].bytes, true);
906✔
1898

1899
_return:
906✔
1900

1901
  if (code != TSDB_CODE_SUCCESS) {
906!
1902
    taos_free_result(pStmt->exec.pRequest);
×
1903
    pStmt->exec.pRequest = NULL;
×
1904
  }
1905

1906
  taosMemoryFree(pField);
906!
1907
  pStmt->errCode = preCode;
906✔
1908

1909
  return code;
906✔
1910
}
1911

1912
TAOS_RES* stmtUseResult(TAOS_STMT* stmt) {
17✔
1913
  STscStmt* pStmt = (STscStmt*)stmt;
17✔
1914

1915
  STMT_DLOG_E("start to use result");
17✔
1916

1917
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
17!
1918
    tscError("useResult only for query statement");
×
1919
    return NULL;
×
1920
  }
1921

1922
  return pStmt->exec.pRequest;
17✔
1923
}
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