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

taosdata / TDengine / #4729

12 Sep 2025 02:34AM UTC coverage: 58.085% (-1.0%) from 59.125%
#4729

push

travis-ci

web-flow
docs: optimize taosd config parameters doc better (#32964)

133518 of 292959 branches covered (45.58%)

Branch coverage included in aggregate %.

201933 of 284559 relevant lines covered (70.96%)

5466318.3 hits per line

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

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

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

6
#include "catalog.h"
7
#include "clientStmt.h"
8
#include "parser.h"
9

10
char* gStmtStatusStr[] = {"unknown",     "init", "prepare", "settbname", "settags",
11
                          "fetchFields", "bind", "bindCol", "addBatch",  "exec"};
12

13
static FORCE_INLINE int32_t stmtAllocQNodeFromBuf(STableBufInfo* pTblBuf, void** pBuf) {
14
  if (pTblBuf->buffOffset < pTblBuf->buffSize) {
10,277✔
15
    *pBuf = (char*)pTblBuf->pCurBuff + pTblBuf->buffOffset;
10,278✔
16
    pTblBuf->buffOffset += pTblBuf->buffUnit;
10,278✔
17
  } else if (pTblBuf->buffIdx < taosArrayGetSize(pTblBuf->pBufList)) {
×
18
    pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, pTblBuf->buffIdx++);
×
19
    if (NULL == pTblBuf->pCurBuff) {
×
20
      return TAOS_GET_TERRNO(terrno);
×
21
    }
22
    *pBuf = pTblBuf->pCurBuff;
×
23
    pTblBuf->buffOffset = pTblBuf->buffUnit;
×
24
  } else {
25
    void* buff = taosMemoryMalloc(pTblBuf->buffSize);
×
26
    if (NULL == buff) {
×
27
      return terrno;
×
28
    }
29

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

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

40
  return TSDB_CODE_SUCCESS;
10,278✔
41
}
42

43
bool stmtDequeue(STscStmt* pStmt, SStmtQNode** param) {
10,280✔
44
  int i = 0;
10,280✔
45
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
61,758✔
46
    if (i < 10) {
51,491✔
47
      taosUsleep(1);
46,830✔
48
      i++;
46,817✔
49
    } else {
50
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
4,661✔
51
      if (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
4,661!
52
        (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
4,662✔
53
      }
54
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
4,661✔
55
    }
56
  }
57
  if (pStmt->queue.stopQueue) {
10,202✔
58
    return false;
95✔
59
  }
60
  SStmtQNode* orig = pStmt->queue.head;
10,107✔
61
  SStmtQNode* node = pStmt->queue.head->next;
10,107✔
62
  pStmt->queue.head = pStmt->queue.head->next;
10,107✔
63
  *param = node;
10,107✔
64

65
  (void)atomic_sub_fetch_64((int64_t*)&pStmt->queue.qRemainNum, 1);
10,107✔
66

67
  return true;
10,185✔
68
}
69

70
void stmtEnqueue(STscStmt* pStmt, SStmtQNode* param) {
10,181✔
71
  pStmt->queue.tail->next = param;
10,181✔
72
  pStmt->queue.tail = param;
10,181✔
73

74
  pStmt->stat.bindDataNum++;
10,181✔
75

76
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
10,181✔
77
  (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
10,183✔
78
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
10,185✔
79
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
10,184✔
80
}
10,182✔
81

82
static int32_t stmtCreateRequest(STscStmt* pStmt) {
118,790✔
83
  int32_t code = 0;
118,790✔
84

85
  if (pStmt->exec.pRequest == NULL) {
118,790✔
86
    code = buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false, &pStmt->exec.pRequest,
11,032✔
87
                        pStmt->reqid);
88
    if (pStmt->reqid != 0) {
11,031!
89
      pStmt->reqid++;
×
90
    }
91
    if (TSDB_CODE_SUCCESS == code) {
11,031!
92
      pStmt->exec.pRequest->syncQuery = true;
11,038✔
93
      pStmt->exec.pRequest->stmtBindVersion = 1;
11,038✔
94
    }
95
  }
96

97
  return code;
118,789✔
98
}
99

100
int32_t stmtSwitchStatus(STscStmt* pStmt, STMT_STATUS newStatus) {
209,410✔
101
  int32_t code = 0;
209,410✔
102

103
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
209,410!
104
    STMT_LOG_SEQ(newStatus);
209,581!
105
  }
106

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

112
  switch (newStatus) {
212,743!
113
    case STMT_PREPARE:
11,001✔
114
      pStmt->errCode = 0;
11,001✔
115
      break;
11,001✔
116
    case STMT_SETTBNAME:
15,664✔
117
      if (STMT_STATUS_EQ(INIT)) {
15,664!
118
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
119
      }
120
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
15,664!
121
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
122
      }
123
      break;
15,664✔
124
    case STMT_SETTAGS:
11✔
125
      if (STMT_STATUS_NE(SETTBNAME) && STMT_STATUS_NE(FETCH_FIELDS)) {
11!
126
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
127
      }
128
      break;
11✔
129
    case STMT_FETCH_FIELDS:
19✔
130
      if (STMT_STATUS_EQ(INIT)) {
19!
131
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
132
      }
133
      break;
19✔
134
    case STMT_BIND:
85,950✔
135
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
85,950!
136
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
137
      }
138
      /*
139
            if ((pStmt->sql.type == STMT_TYPE_MULTI_INSERT) && ()) {
140
              code = TSDB_CODE_TSC_STMT_API_ERROR;
141
            }
142
      */
143
      break;
85,950✔
144
    case STMT_BIND_COL:
×
145
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND)) {
×
146
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
147
      }
148
      break;
×
149
    case STMT_ADD_BATCH:
84,669✔
150
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
84,669!
151
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
152
      }
153
      break;
84,669✔
154
    case STMT_EXECUTE:
15,429✔
155
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
15,429✔
156
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
2!
157
            STMT_STATUS_NE(BIND_COL)) {
×
158
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
159
        }
160
      } else {
161
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS)) {
15,427!
162
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
163
        }
164
      }
165
      break;
15,429✔
166
    default:
×
167
      code = TSDB_CODE_APP_ERROR;
×
168
      break;
×
169
  }
170

171
  STMT_ERR_RET(code);
212,743!
172

173
  pStmt->sql.status = newStatus;
212,743✔
174

175
  return TSDB_CODE_SUCCESS;
212,743✔
176
}
177

178
int32_t stmtGetTbName(TAOS_STMT* stmt, char** tbName) {
10,093✔
179
  STscStmt* pStmt = (STscStmt*)stmt;
10,093✔
180

181
  pStmt->sql.type = STMT_TYPE_MULTI_INSERT;
10,093✔
182

183
  if ('\0' == pStmt->bInfo.tbName[0]) {
10,093✔
184
    tscError("no table name set");
4!
185
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_TBNAME_ERROR);
13✔
186
  }
187

188
  *tbName = pStmt->bInfo.tbName;
10,098✔
189

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

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

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

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

213
  return TSDB_CODE_SUCCESS;
214
}
215

216
int32_t stmtRestoreQueryFields(STscStmt* pStmt) {
217
  SStmtQueryResInfo* pRes = &pStmt->sql.queryRes;
218
  int32_t            size = pRes->numOfCols * sizeof(TAOS_FIELD_E);
219

220
  pStmt->exec.pRequest->body.resInfo.numOfCols = pRes->numOfCols;
221
  pStmt->exec.pRequest->body.resInfo.precision = pRes->precision;
222

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

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

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

251
  (void)memcpy(&pStmt->bInfo.sname, tbName, sizeof(*tbName));
10,924✔
252
  tstrncpy(pStmt->bInfo.tbFName, tbFName, TSDB_TABLE_FNAME_LEN);
10,924✔
253
  pStmt->bInfo.tbFName[sizeof(pStmt->bInfo.tbFName) - 1] = 0;
10,924✔
254

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

264
  return TSDB_CODE_SUCCESS;
10,924✔
265
}
266

267
int32_t stmtUpdateExecInfo(TAOS_STMT* stmt, SHashObj* pVgHash, SHashObj* pBlockHash) {
10,916✔
268
  STscStmt* pStmt = (STscStmt*)stmt;
10,916✔
269

270
  pStmt->sql.pVgHash = pVgHash;
10,916✔
271
  pStmt->exec.pBlockHash = pBlockHash;
10,916✔
272

273
  return TSDB_CODE_SUCCESS;
10,916✔
274
}
275

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

281
  STMT_ERR_RET(stmtUpdateBindInfo(stmt, pTableMeta, tags, tbName, sTableName, autoCreateTbl, tbNameFlag));
10,917!
282
  STMT_ERR_RET(stmtUpdateExecInfo(stmt, pVgHash, pBlockHash));
10,929!
283

284
  pStmt->sql.autoCreateTbl = autoCreateTbl;
10,925✔
285
  if (pStmt->sql.autoCreateTbl) {
10,925✔
286
    pStmt->sql.stbInterlaceMode = false;
10,015✔
287
  }
288

289
  return TSDB_CODE_SUCCESS;
10,925✔
290
}
291

292
int32_t stmtGetExecInfo(TAOS_STMT* stmt, SHashObj** pVgHash, SHashObj** pBlockHash) {
×
293
  STscStmt* pStmt = (STscStmt*)stmt;
×
294

295
  *pVgHash = pStmt->sql.pVgHash;
×
296
  pStmt->sql.pVgHash = NULL;
×
297

298
  *pBlockHash = pStmt->exec.pBlockHash;
×
299
  pStmt->exec.pBlockHash = NULL;
×
300

301
  return TSDB_CODE_SUCCESS;
×
302
}
303

304
int32_t stmtCacheBlock(STscStmt* pStmt) {
79,936✔
305
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
79,936✔
306
    return TSDB_CODE_SUCCESS;
69,985✔
307
  }
308

309
  uint64_t uid = pStmt->bInfo.tbUid;
9,951✔
310
  uint64_t cacheUid = (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) ? pStmt->bInfo.tbSuid : uid;
9,951!
311

312
  if (taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid))) {
9,951✔
313
    return TSDB_CODE_SUCCESS;
32✔
314
  }
315

316
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
10,013✔
317
  if (!pSrc) {
10,016!
318
    return terrno;
×
319
  }
320
  STableDataCxt* pDst = NULL;
10,016✔
321

322
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
10,016!
323

324
  SStmtTableCache cache = {
10,010✔
325
      .pDataCtx = pDst,
326
      .boundTags = pStmt->bInfo.boundTags,
10,010✔
327
  };
328

329
  if (taosHashPut(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid), &cache, sizeof(cache))) {
10,010!
330
    return terrno;
×
331
  }
332

333
  if (pStmt->sql.autoCreateTbl) {
10,020✔
334
    pStmt->bInfo.tagsCached = true;
10,019✔
335
  } else {
336
    pStmt->bInfo.boundTags = NULL;
1✔
337
  }
338

339
  return TSDB_CODE_SUCCESS;
10,020✔
340
}
341

342
int32_t stmtParseSql(STscStmt* pStmt) {
10,937✔
343
  pStmt->exec.pCurrBlock = NULL;
10,937✔
344

345
  SStmtCallback stmtCb = {
10,937✔
346
      .pStmt = pStmt,
347
      .getTbNameFn = stmtGetTbName,
348
      .setInfoFn = stmtUpdateInfo,
349
      .getExecInfoFn = stmtGetExecInfo,
350
  };
351

352
  STMT_ERR_RET(stmtCreateRequest(pStmt));
10,937!
353
  pStmt->exec.pRequest->stmtBindVersion = 1;
10,938✔
354

355
  pStmt->stat.parseSqlNum++;
10,938✔
356
  STMT_ERR_RET(parseSql(pStmt->exec.pRequest, false, &pStmt->sql.pQuery, &stmtCb));
10,938✔
357
  pStmt->sql.siInfo.pQuery = pStmt->sql.pQuery;
10,921✔
358

359
  pStmt->bInfo.needParse = false;
10,921✔
360

361
  if (pStmt->sql.pQuery->pRoot && 0 == pStmt->sql.type) {
10,921!
362
    pStmt->sql.type = STMT_TYPE_INSERT;
835✔
363
    pStmt->sql.stbInterlaceMode = false;
835✔
364
  } else if (pStmt->sql.pQuery->pPrepareRoot) {
10,086✔
365
    pStmt->sql.type = STMT_TYPE_QUERY;
2✔
366
    pStmt->sql.stbInterlaceMode = false;
2✔
367

368
    return TSDB_CODE_SUCCESS;
2✔
369
  }
370

371
  STableDataCxt** pSrc =
372
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
10,919✔
373
  if (NULL == pSrc || NULL == *pSrc) {
10,934!
374
    return terrno;
1✔
375
  }
376

377
  STableDataCxt* pTableCtx = *pSrc;
10,933✔
378
  if (pStmt->sql.stbInterlaceMode) {
10,933✔
379
    int16_t lastIdx = -1;
77✔
380

381
    for (int32_t i = 0; i < pTableCtx->boundColsInfo.numOfBound; ++i) {
986✔
382
      if (pTableCtx->boundColsInfo.pColIndex[i] < lastIdx) {
909!
383
        pStmt->sql.stbInterlaceMode = false;
×
384
        break;
×
385
      }
386

387
      lastIdx = pTableCtx->boundColsInfo.pColIndex[i];
909✔
388
    }
389
  }
390

391
  if (NULL == pStmt->sql.pBindInfo) {
10,933✔
392
    pStmt->sql.pBindInfo = taosMemoryMalloc(pTableCtx->boundColsInfo.numOfBound * sizeof(*pStmt->sql.pBindInfo));
10,931!
393
    if (NULL == pStmt->sql.pBindInfo) {
10,923!
394
      return terrno;
×
395
    }
396
  }
397

398
  return TSDB_CODE_SUCCESS;
10,925✔
399
}
400

401
int32_t stmtCleanBindInfo(STscStmt* pStmt) {
26,521✔
402
  pStmt->bInfo.tbUid = 0;
26,521✔
403
  pStmt->bInfo.tbSuid = 0;
26,521✔
404
  pStmt->bInfo.tbVgId = -1;
26,521✔
405
  pStmt->bInfo.tbType = 0;
26,521✔
406
  pStmt->bInfo.needParse = true;
26,521✔
407
  pStmt->bInfo.inExecCache = false;
26,521✔
408

409
  pStmt->bInfo.tbName[0] = 0;
26,521✔
410
  pStmt->bInfo.tbFName[0] = 0;
26,521✔
411
  if (!pStmt->bInfo.tagsCached) {
26,521✔
412
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
6,498✔
413
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
6,495!
414
  }
415
  pStmt->bInfo.stbFName[0] = 0;
26,530✔
416

417
  return TSDB_CODE_SUCCESS;
26,530✔
418
}
419

420
void stmtFreeTableBlkList(STableColsData* pTb) {
×
421
  (void)qResetStmtColumns(pTb->aCol, true);
×
422
  taosArrayDestroy(pTb->aCol);
×
423
}
×
424

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

434
  pQueue->head = pQueue->tail = pTblBuf->pCurBuff;
4,553✔
435
  pQueue->qRemainNum = 0;
4,553✔
436
  pQueue->head->next = NULL;
4,553✔
437
}
438

439
int32_t stmtCleanExecInfo(STscStmt* pStmt, bool keepTable, bool deepClean) {
26,414✔
440
  if (pStmt->sql.stbInterlaceMode) {
26,414✔
441
    if (deepClean) {
4,628✔
442
      taosHashCleanup(pStmt->exec.pBlockHash);
77✔
443
      pStmt->exec.pBlockHash = NULL;
77✔
444

445
      if (NULL != pStmt->exec.pCurrBlock) {
77!
446
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->pData);
77!
447
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
77✔
448
      }
449
    } else {
450
      pStmt->sql.siInfo.pTableColsIdx = 0;
4,551✔
451
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
4,551✔
452
      tSimpleHashClear(pStmt->sql.siInfo.pTableRowDataHash);
4,553✔
453
    }
454
  } else {
455
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
21,786✔
456
      taos_free_result(pStmt->exec.pRequest);
21,784✔
457
      pStmt->exec.pRequest = NULL;
21,790✔
458
    }
459

460
    size_t keyLen = 0;
21,792✔
461
    void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
21,792✔
462
    while (pIter) {
43,560✔
463
      STableDataCxt* pBlocks = *(STableDataCxt**)pIter;
21,757✔
464
      char*          key = taosHashGetKey(pIter, &keyLen);
21,757✔
465
      STableMeta*    pMeta = qGetTableMetaInDataBlock(pBlocks);
21,754✔
466

467
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
21,757✔
468
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
10,884✔
469
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
21,767!
470

471
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
10,865✔
472
        continue;
10,881✔
473
      }
474

475
      qDestroyStmtDataBlock(pBlocks);
10,873✔
476
      STMT_ERR_RET(taosHashRemove(pStmt->exec.pBlockHash, key, keyLen));
10,869!
477

478
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
10,869✔
479
    }
480

481
    if (keepTable) {
21,803✔
482
      return TSDB_CODE_SUCCESS;
10,883✔
483
    }
484

485
    taosHashCleanup(pStmt->exec.pBlockHash);
10,920✔
486
    pStmt->exec.pBlockHash = NULL;
10,916✔
487

488
    tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
10,916✔
489
    taosMemoryFreeClear(pStmt->exec.pCurrTbData);
10,919!
490
  }
491

492
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
15,550✔
493

494
  return TSDB_CODE_SUCCESS;
15,538✔
495
}
496

497
void stmtFreeTbBuf(void* buf) {
95✔
498
  void* pBuf = *(void**)buf;
95✔
499
  taosMemoryFree(pBuf);
95!
500
}
95✔
501

502
void stmtFreeTbCols(void* buf) {
77,000✔
503
  SArray* pCols = *(SArray**)buf;
77,000✔
504
  taosArrayDestroy(pCols);
77,000✔
505
}
77,000✔
506

507
int32_t stmtCleanSQLInfo(STscStmt* pStmt) {
10,983✔
508
  STMT_DLOG_E("start to free SQL info");
10,983!
509

510
  taosMemoryFree(pStmt->sql.pBindInfo);
10,983!
511
  taosMemoryFree(pStmt->sql.queryRes.fields);
10,995!
512
  taosMemoryFree(pStmt->sql.queryRes.userFields);
10,997!
513
  taosMemoryFree(pStmt->sql.sqlStr);
10,997!
514
  qDestroyQuery(pStmt->sql.pQuery);
10,994✔
515
  taosArrayDestroy(pStmt->sql.nodeList);
10,995✔
516
  taosHashCleanup(pStmt->sql.pVgHash);
10,996✔
517
  pStmt->sql.pVgHash = NULL;
10,997✔
518

519
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
10,997✔
520
  while (pIter) {
21,017✔
521
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
10,019✔
522

523
    qDestroyStmtDataBlock(pCache->pDataCtx);
10,019✔
524
    qDestroyBoundColInfo(pCache->boundTags);
10,015✔
525
    taosMemoryFreeClear(pCache->boundTags);
10,004!
526

527
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
10,018✔
528
  }
529
  taosHashCleanup(pStmt->sql.pTableCache);
10,998✔
530
  pStmt->sql.pTableCache = NULL;
10,992✔
531

532
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
10,992!
533
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
10,988!
534

535
  taos_free_result(pStmt->sql.siInfo.pRequest);
10,997✔
536
  taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
10,997✔
537
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
10,988✔
538
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableRowDataHash);
10,986✔
539
  tSimpleHashCleanup(pStmt->sql.predicateCols);
10,994✔
540
  taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
10,994✔
541
  taosMemoryFree(pStmt->sql.siInfo.pTSchema);
10,994!
542
  qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
10,988✔
543
  taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
10,993✔
544

545
  (void)memset(&pStmt->sql, 0, sizeof(pStmt->sql));
10,993✔
546
  pStmt->sql.siInfo.tableColsReady = true;
10,993✔
547

548
  STMT_DLOG_E("end to free SQL info");
10,993!
549

550
  return TSDB_CODE_SUCCESS;
10,989✔
551
}
552

553
int32_t stmtTryAddTableVgroupInfo(STscStmt* pStmt, int32_t* vgId) {
16✔
554
  if (*vgId >= 0 && taosHashGet(pStmt->sql.pVgHash, (const char*)vgId, sizeof(*vgId))) {
16✔
555
    return TSDB_CODE_SUCCESS;
7✔
556
  }
557

558
  SVgroupInfo      vgInfo = {0};
9✔
559
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
9✔
560
                           .requestId = pStmt->exec.pRequest->requestId,
9✔
561
                           .requestObjRefId = pStmt->exec.pRequest->self,
9✔
562
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
9✔
563

564
  int32_t code = catalogGetTableHashVgroup(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &vgInfo);
9✔
565
  if (TSDB_CODE_SUCCESS != code) {
9!
566
    return code;
×
567
  }
568

569
  code =
570
      taosHashPut(pStmt->sql.pVgHash, (const char*)&vgInfo.vgId, sizeof(vgInfo.vgId), (char*)&vgInfo, sizeof(vgInfo));
9✔
571
  if (TSDB_CODE_SUCCESS != code) {
9!
572
    return code;
×
573
  }
574

575
  *vgId = vgInfo.vgId;
9✔
576

577
  return TSDB_CODE_SUCCESS;
9✔
578
}
579

580
int32_t stmtRebuildDataBlock(STscStmt* pStmt, STableDataCxt* pDataBlock, STableDataCxt** newBlock, uint64_t uid,
16✔
581
                             uint64_t suid, int32_t vgId) {
582
  STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
16!
583
  STMT_ERR_RET(qRebuildStmtDataBlock(newBlock, pDataBlock, uid, suid, vgId, pStmt->sql.autoCreateTbl));
16!
584

585
  STMT_DLOG("uid:%" PRId64 ", rebuild table data context, vgId:%d", uid, vgId);
16!
586

587
  return TSDB_CODE_SUCCESS;
16✔
588
}
589

590
int32_t stmtGetTableMeta(STscStmt* pStmt, STableMeta** ppTableMeta) {
12✔
591
  if (NULL == pStmt->pCatalog) {
12✔
592
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
9!
593
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
9✔
594
  }
595

596
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
12✔
597
                           .requestId = pStmt->exec.pRequest->requestId,
12✔
598
                           .requestObjRefId = pStmt->exec.pRequest->self,
12✔
599
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
12✔
600

601
  int32_t code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, ppTableMeta);
12✔
602
  if (TSDB_CODE_SUCCESS != code) {
12✔
603
    STMT_ELOG("get table meta failed, code: 0x%x", code);
2!
604
    return code;
2✔
605
  }
606

607
  return TSDB_CODE_SUCCESS;
10✔
608
}
609

610
int32_t stmtGetFromCache(STscStmt* pStmt) {
10,120✔
611
  if (pStmt->sql.stbInterlaceMode && pStmt->sql.siInfo.pDataCtx) {
10,120!
612
    pStmt->bInfo.needParse = false;
×
613
    pStmt->bInfo.inExecCache = false;
×
614
    return TSDB_CODE_SUCCESS;
×
615
  }
616

617
  pStmt->bInfo.needParse = true;
10,120✔
618
  pStmt->bInfo.inExecCache = false;
10,120✔
619

620
  STableDataCxt** pCxtInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
10,120✔
621
  if (pCxtInExec) {
10,129✔
622
    pStmt->bInfo.needParse = false;
16✔
623
    pStmt->bInfo.inExecCache = true;
16✔
624

625
    pStmt->exec.pCurrBlock = *pCxtInExec;
16✔
626

627
    if (pStmt->sql.autoCreateTbl) {
16!
628
      tscDebug("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
16!
629
      return TSDB_CODE_SUCCESS;
16✔
630
    }
631
  }
632

633
  if (NULL == pStmt->pCatalog) {
10,113✔
634
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
102!
635
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
102✔
636
  }
637

638
  if (NULL == pStmt->sql.pTableCache || taosHashGetSize(pStmt->sql.pTableCache) <= 0) {
10,113✔
639
    if (pStmt->bInfo.inExecCache) {
10,093!
640
      pStmt->bInfo.needParse = false;
×
641
      tscDebug("reuse stmt block for tb %s in execBlock", pStmt->bInfo.tbFName);
×
642
      return TSDB_CODE_SUCCESS;
×
643
    }
644

645
    tscDebug("no stmt block cache for tb %s", pStmt->bInfo.tbFName);
10,093!
646
    return TSDB_CODE_SUCCESS;
10,096✔
647
  }
648

649
  if (pStmt->sql.autoCreateTbl) {
18✔
650
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &pStmt->bInfo.tbSuid, sizeof(pStmt->bInfo.tbSuid));
8✔
651
    if (pCache) {
8!
652
      pStmt->bInfo.needParse = false;
8✔
653
      pStmt->bInfo.tbUid = 0;
8✔
654

655
      STableDataCxt* pNewBlock = NULL;
8✔
656
      STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, 0, pStmt->bInfo.tbSuid, -1));
8!
657

658
      if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
8!
659
                      POINTER_BYTES)) {
660
        STMT_ERR_RET(terrno);
×
661
      }
662

663
      pStmt->exec.pCurrBlock = pNewBlock;
8✔
664

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

667
      return TSDB_CODE_SUCCESS;
8✔
668
    }
669

670
    STMT_RET(stmtCleanBindInfo(pStmt));
×
671
  }
672

673
  uint64_t uid, suid;
674
  int32_t  vgId;
675
  int8_t   tableType;
676

677
  STableMeta*      pTableMeta = NULL;
10✔
678
  SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
10✔
679
                           .requestId = pStmt->exec.pRequest->requestId,
10✔
680
                           .requestObjRefId = pStmt->exec.pRequest->self,
10✔
681
                           .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
10✔
682
  int32_t          code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
8✔
683

684
  pStmt->stat.ctgGetTbMetaNum++;
8✔
685

686
  if (TSDB_CODE_PAR_TABLE_NOT_EXIST == code) {
8!
687
    tscDebug("tb %s not exist", pStmt->bInfo.tbFName);
×
688
    STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
689

690
    STMT_ERR_RET(code);
×
691
  }
692

693
  STMT_ERR_RET(code);
8!
694

695
  uid = pTableMeta->uid;
8✔
696
  suid = pTableMeta->suid;
8✔
697
  tableType = pTableMeta->tableType;
8✔
698
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
8✔
699
  vgId = pTableMeta->vgId;
8✔
700

701
  taosMemoryFree(pTableMeta);
8!
702

703
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
8!
704

705
  if (uid == pStmt->bInfo.tbUid) {
8!
706
    pStmt->bInfo.needParse = false;
×
707

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

710
    return TSDB_CODE_SUCCESS;
×
711
  }
712

713
  if (pStmt->bInfo.inExecCache) {
8!
714
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
×
715
    if (NULL == pCache) {
×
716
      tscError("table [%s, %" PRIx64 ", %" PRIx64 "] found in exec blockHash, but not in sql blockHash",
×
717
               pStmt->bInfo.tbFName, uid, cacheUid);
718

719
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
720
    }
721

722
    pStmt->bInfo.needParse = false;
×
723

724
    pStmt->bInfo.tbUid = uid;
×
725
    pStmt->bInfo.tbSuid = suid;
×
726
    pStmt->bInfo.tbType = tableType;
×
727
    pStmt->bInfo.boundTags = pCache->boundTags;
×
728
    pStmt->bInfo.tagsCached = true;
×
729

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

732
    return TSDB_CODE_SUCCESS;
×
733
  }
734

735
  SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
8✔
736
  if (pCache) {
8!
737
    pStmt->bInfo.needParse = false;
8✔
738

739
    pStmt->bInfo.tbUid = uid;
8✔
740
    pStmt->bInfo.tbSuid = suid;
8✔
741
    pStmt->bInfo.tbType = tableType;
8✔
742
    pStmt->bInfo.boundTags = pCache->boundTags;
8✔
743
    pStmt->bInfo.tagsCached = true;
8✔
744

745
    STableDataCxt* pNewBlock = NULL;
8✔
746
    STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, uid, suid, vgId));
8!
747

748
    if (taosHashPut(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName), &pNewBlock,
8!
749
                    POINTER_BYTES)) {
750
      STMT_ERR_RET(terrno);
×
751
    }
752

753
    pStmt->exec.pCurrBlock = pNewBlock;
8✔
754

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

757
    return TSDB_CODE_SUCCESS;
8✔
758
  }
759

760
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
761

762
  return TSDB_CODE_SUCCESS;
×
763
}
764

765
int32_t stmtResetStmt(STscStmt* pStmt) {
9,991✔
766
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
9,991!
767

768
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
9,992✔
769
  if (NULL == pStmt->sql.pTableCache) {
9,992!
770
    STMT_ERR_RET(terrno);
×
771
  }
772

773
  if (pStmt->sql.siInfo.pTableRowDataHash) {
9,992!
774
    tSimpleHashClear(pStmt->sql.siInfo.pTableRowDataHash);
×
775
  }
776

777
  pStmt->sql.status = STMT_INIT;
9,993✔
778

779
  return TSDB_CODE_SUCCESS;
9,993✔
780
}
781

782
int32_t stmtAsyncOutput(STscStmt* pStmt, void* param) {
10,184✔
783
  SStmtQNode* pParam = (SStmtQNode*)param;
10,184✔
784

785
  if (pParam->restoreTbCols) {
10,184✔
786
    for (int32_t i = 0; i < pStmt->sql.siInfo.pTableColsIdx; ++i) {
10,184✔
787
      SArray** p = (SArray**)TARRAY_GET_ELEM(pStmt->sql.siInfo.pTableCols, i);
5,631✔
788
      *p = taosArrayInit(20, POINTER_BYTES);
5,631✔
789
      if (*p == NULL) {
5,631!
790
        STMT_ERR_RET(terrno);
×
791
      }
792
    }
793

794
    atomic_store_8((int8_t*)&pStmt->sql.siInfo.tableColsReady, true);
4,553✔
795
  } else {
796
    STMT_ERR_RET(qAppendStmtTableOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, &pParam->tblData, pStmt->exec.pCurrBlock,
5,629!
797
                                        &pStmt->sql.siInfo));
798

799
    // taosMemoryFree(pParam->pTbData);
800

801
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
5,630✔
802
  }
803
  return TSDB_CODE_SUCCESS;
10,184✔
804
}
805

806
void* stmtBindThreadFunc(void* param) {
96✔
807
  setThreadName("stmtBind");
96✔
808

809
  qInfo("stmt bind thread started");
96!
810

811
  STscStmt* pStmt = (STscStmt*)param;
96✔
812

813
  while (true) {
10,279✔
814
    if (pStmt->queue.stopQueue) {
10,375✔
815
      break;
95✔
816
    }
817

818
    SStmtQNode* asyncParam = NULL;
10,280✔
819
    if (!stmtDequeue(pStmt, &asyncParam)) {
10,280✔
820
      continue;
95✔
821
    }
822

823
    int ret = stmtAsyncOutput(pStmt, asyncParam);
10,185✔
824
    if (ret != 0) {
10,184!
825
      qError("stmtAsyncOutput failed, reason:%s", tstrerror(ret));
×
826
    }
827
  }
828

829
  qInfo("stmt bind thread stopped");
95!
830

831
  return NULL;
95✔
832
}
833

834
int32_t stmtStartBindThread(STscStmt* pStmt) {
96✔
835
  TdThreadAttr thAttr;
836
  if (taosThreadAttrInit(&thAttr) != 0) {
96!
837
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
838
  }
839
  if (taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE) != 0) {
96!
840
    return TSDB_CODE_TSC_INTERNAL_ERROR;
×
841
  }
842

843
  if (taosThreadCreate(&pStmt->bindThread, &thAttr, stmtBindThreadFunc, pStmt) != 0) {
95!
844
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
845
    STMT_ERR_RET(terrno);
×
846
  }
847

848
  pStmt->bindThreadInUse = true;
96✔
849

850
  (void)taosThreadAttrDestroy(&thAttr);
96✔
851
  return TSDB_CODE_SUCCESS;
96✔
852
}
853

854
int32_t stmtInitQueue(STscStmt* pStmt) {
96✔
855
  (void)taosThreadCondInit(&pStmt->queue.waitCond, NULL);
96✔
856
  (void)taosThreadMutexInit(&pStmt->queue.mutex, NULL);
96✔
857
  STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&pStmt->queue.head));
190!
858
  pStmt->queue.tail = pStmt->queue.head;
95✔
859

860
  return TSDB_CODE_SUCCESS;
95✔
861
}
862

863
int32_t stmtInitTableBuf(STableBufInfo* pTblBuf) {
96✔
864
  pTblBuf->buffUnit = sizeof(SStmtQNode);
96✔
865
  pTblBuf->buffSize = pTblBuf->buffUnit * 1000;
96✔
866
  pTblBuf->pBufList = taosArrayInit(100, POINTER_BYTES);
96✔
867
  if (NULL == pTblBuf->pBufList) {
96!
868
    return terrno;
×
869
  }
870
  void* buff = taosMemoryMalloc(pTblBuf->buffSize);
96!
871
  if (NULL == buff) {
96!
872
    return terrno;
×
873
  }
874

875
  if (taosArrayPush(pTblBuf->pBufList, &buff) == NULL) {
192!
876
    return terrno;
×
877
  }
878

879
  pTblBuf->pCurBuff = buff;
96✔
880
  pTblBuf->buffIdx = 1;
96✔
881
  pTblBuf->buffOffset = 0;
96✔
882

883
  return TSDB_CODE_SUCCESS;
96✔
884
}
885

886
TAOS_STMT* stmtInit(STscObj* taos, int64_t reqid, TAOS_STMT_OPTIONS* pOptions) {
1,006✔
887
  STscObj*  pObj = (STscObj*)taos;
1,006✔
888
  STscStmt* pStmt = NULL;
1,006✔
889
  int32_t   code = 0;
1,006✔
890

891
  pStmt = taosMemoryCalloc(1, sizeof(STscStmt));
1,006!
892
  if (NULL == pStmt) {
1,007!
893
    return NULL;
×
894
  }
895

896
  pStmt->sql.pTableCache = taosHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
1,007✔
897
  if (NULL == pStmt->sql.pTableCache) {
1,007!
898
    taosMemoryFree(pStmt);
×
899
    return NULL;
×
900
  }
901

902
  pStmt->taos = pObj;
1,007✔
903
  pStmt->bInfo.needParse = true;
1,007✔
904
  pStmt->sql.status = STMT_INIT;
1,007✔
905
  pStmt->reqid = reqid;
1,007✔
906
  pStmt->errCode = TSDB_CODE_SUCCESS;
1,007✔
907

908
  if (NULL != pOptions) {
1,007✔
909
    (void)memcpy(&pStmt->options, pOptions, sizeof(pStmt->options));
96✔
910
    if (pOptions->singleStbInsert && pOptions->singleTableBindOnce) {
96!
911
      pStmt->stbInterlaceMode = true;
96✔
912
    }
913
  }
914

915
  if (pStmt->stbInterlaceMode) {
1,007✔
916
    pStmt->sql.siInfo.transport = taos->pAppInfo->pTransporter;
95✔
917
    pStmt->sql.siInfo.acctId = taos->acctId;
95✔
918
    pStmt->sql.siInfo.dbname = taos->db;
95✔
919
    pStmt->sql.siInfo.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
95✔
920
    pStmt->sql.siInfo.pTableHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
96✔
921
    if (NULL == pStmt->sql.siInfo.pTableHash) {
96!
922
      (void)stmtClose(pStmt);
×
923
      return NULL;
×
924
    }
925
    pStmt->sql.siInfo.pTableRowDataHash = tSimpleHashInit(100, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
96✔
926
    if (NULL == pStmt->sql.siInfo.pTableRowDataHash) {
96!
927
      STMT_ELOG("fail to allocate memory for pTableRowDataHash:%s", tstrerror(terrno));
×
928
      (void)stmtClose(pStmt);
×
929
      return NULL;
×
930
    }
931
    pStmt->sql.siInfo.pTableCols = taosArrayInit(STMT_TABLE_COLS_NUM, POINTER_BYTES);
96✔
932
    if (NULL == pStmt->sql.siInfo.pTableCols) {
96!
933
      (void)stmtClose(pStmt);
×
934
      return NULL;
×
935
    }
936

937
    code = stmtInitTableBuf(&pStmt->sql.siInfo.tbBuf);
96✔
938
    if (TSDB_CODE_SUCCESS == code) {
96!
939
      code = stmtInitQueue(pStmt);
96✔
940
    }
941
    if (TSDB_CODE_SUCCESS == code) {
96!
942
      code = stmtStartBindThread(pStmt);
96✔
943
    }
944
    if (TSDB_CODE_SUCCESS != code) {
96!
945
      terrno = code;
×
946
      (void)stmtClose(pStmt);
×
947
      return NULL;
×
948
    }
949
  }
950

951
  pStmt->sql.siInfo.tableColsReady = true;
1,008✔
952

953
  STMT_LOG_SEQ(STMT_INIT);
1,008!
954

955
  tscDebug("stmt:%p initialized", pStmt);
1,008!
956

957
  return pStmt;
1,007✔
958
}
959

960
int stmtPrepare(TAOS_STMT* stmt, const char* sql, unsigned long length) {
10,996✔
961
  STscStmt* pStmt = (STscStmt*)stmt;
10,996✔
962

963
  STMT_DLOG_E("start to prepare");
10,996!
964

965
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
11,009!
966
    return pStmt->errCode;
×
967
  }
968

969
  if (pStmt->sql.status >= STMT_PREPARE) {
11,009✔
970
    STMT_ERR_RET(stmtResetStmt(pStmt));
10,002!
971
  }
972

973
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
11,000!
974

975
  if (length <= 0) {
10,999✔
976
    length = strlen(sql);
917✔
977
  }
978

979
  pStmt->sql.sqlStr = taosStrndup(sql, length);
10,999!
980
  if (!pStmt->sql.sqlStr) {
11,004!
981
    return terrno;
×
982
  }
983
  pStmt->sql.sqlLen = length;
11,004✔
984
  pStmt->sql.stbInterlaceMode = pStmt->stbInterlaceMode;
11,004✔
985

986
  STMT_ERR_RET(stmtCreateRequest(pStmt));
11,004!
987

988
  int32_t code = 0;
11,004✔
989
  if (qIsUpdateSetSql(sql, strlen(sql), &pStmt->bInfo.sname, pStmt->taos->acctId, pStmt->taos->db,
11,009✔
990
                      pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen, &code)) {
11,004✔
991
    // get table meta
992
    STableMeta* pTableMeta = NULL;
23✔
993
    STMT_ERR_RET(stmtGetTableMeta(pStmt, &pTableMeta));
27✔
994

995
    char* newSql = NULL;
10✔
996

997
    // conver update sql to insert sql
998
    pStmt->sql.predicateCols = tSimpleHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
10✔
999
    if (NULL == pStmt->sql.predicateCols) {
10!
1000
      STMT_ELOG("fail to allocate memory for predicateCols:%s", tstrerror(terrno));
×
1001
      return terrno;
×
1002
    }
1003

1004
    code = convertUpdateToInsert(sql, &newSql, pTableMeta, pStmt->sql.predicateCols, pStmt->exec.pRequest->msgBuf,
20✔
1005
                                 pStmt->exec.pRequest->msgBufLen);
10✔
1006
    taosMemoryFree(pTableMeta);
10!
1007

1008
    if (TSDB_CODE_SUCCESS != code) {
10✔
1009
      STMT_ELOG("convert update sql to insert sql failed, code: 0x%x", code);
4!
1010
      return code;
4✔
1011
    }
1012

1013
    // reset request sql
1014
    if (pStmt->exec.pRequest->sqlstr) {
6!
1015
      taosMemoryFreeClear(pStmt->exec.pRequest->sqlstr);
6!
1016
    }
1017
    pStmt->exec.pRequest->sqlstr = newSql;
6✔
1018
    pStmt->exec.pRequest->sqlLen = strlen(newSql);
6✔
1019

1020
    if (pStmt->sql.sqlStr) {
6!
1021
      taosMemoryFreeClear(pStmt->sql.sqlStr);
6!
1022
    }
1023
    pStmt->sql.sqlStr = taosStrndup(newSql, strlen(newSql));
6!
1024
    pStmt->sql.sqlLen = strlen(newSql);
6✔
1025
  }
1026

1027
  STMT_ERR_RET(code);
10,992✔
1028

1029
  char* dbName = NULL;
10,991✔
1030
  if (qParseDbName(sql, length, &dbName)) {
10,991✔
1031
    STMT_ERR_RET(stmtSetDbName(stmt, dbName));
885✔
1032
    taosMemoryFreeClear(dbName);
879!
1033
  }
1034

1035
  return TSDB_CODE_SUCCESS;
10,987✔
1036
}
1037

1038
int32_t stmtInitStbInterlaceTableInfo(STscStmt* pStmt) {
77✔
1039
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
77✔
1040
  if (!pSrc) {
76!
1041
    return terrno;
×
1042
  }
1043
  STableDataCxt* pDst = NULL;
76✔
1044

1045
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
76!
1046
  pStmt->sql.siInfo.pDataCtx = pDst;
77✔
1047

1048
  SArray* pTblCols = NULL;
77✔
1049
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
70,246✔
1050
    pTblCols = taosArrayInit(20, POINTER_BYTES);
70,293✔
1051
    if (NULL == pTblCols) {
71,509!
1052
      return terrno;
×
1053
    }
1054

1055
    if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
141,678!
1056
      return terrno;
×
1057
    }
1058
  }
1059

1060
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
1✔
1061

1062
  return TSDB_CODE_SUCCESS;
1✔
1063
}
1064

1065
int stmtSetDbName(TAOS_STMT* stmt, const char* dbName) {
885✔
1066
  STscStmt* pStmt = (STscStmt*)stmt;
885✔
1067

1068
  STMT_DLOG("start to set dbName:%s", dbName);
885!
1069

1070
  STMT_ERR_RET(stmtCreateRequest(pStmt));
885!
1071

1072
  // The SQL statement specifies a database name, overriding the previously specified database
1073
  taosMemoryFreeClear(pStmt->exec.pRequest->pDb);
885!
1074
  pStmt->exec.pRequest->pDb = taosStrdup(dbName);
885!
1075
  if (pStmt->exec.pRequest->pDb == NULL) {
885!
1076
    return terrno;
×
1077
  }
1078
  return TSDB_CODE_SUCCESS;
885✔
1079
}
1080

1081
int stmtSetTbName(TAOS_STMT* stmt, const char* tbName) {
15,683✔
1082
  STscStmt* pStmt = (STscStmt*)stmt;
15,683✔
1083

1084
  int64_t startUs = taosGetTimestampUs();
15,691✔
1085

1086
  STMT_DLOG("start to set tbName:%s", tbName);
15,691!
1087

1088
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
15,686✔
1089
    return pStmt->errCode;
24✔
1090
  }
1091

1092
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
15,662!
1093

1094
  int32_t insert = 0;
15,665✔
1095
  STMT_ERR_RET(stmtIsInsert(stmt, &insert));
15,665!
1096
  if (0 == insert) {
15,668!
1097
    tscError("set tb name not available for none insert statement");
×
1098
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1099
  }
1100

1101
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
15,668✔
1102
    STMT_ERR_RET(stmtCreateRequest(pStmt));
10,139!
1103

1104
    STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
10,131✔
1105
                              pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1106
    STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
10,124!
1107

1108
    STMT_ERR_RET(stmtGetFromCache(pStmt));
10,127!
1109

1110
    if (pStmt->bInfo.needParse) {
10,127✔
1111
      tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
10,095✔
1112
      pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
10,095✔
1113

1114
      STMT_ERR_RET(stmtParseSql(pStmt));
10,095✔
1115
    }
1116
  } else {
1117
    tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
5,529✔
1118
    pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
5,529✔
1119
    pStmt->exec.pRequest->requestId++;
5,529✔
1120
    pStmt->bInfo.needParse = false;
5,529✔
1121
  }
1122

1123
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
15,645✔
1124
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
77!
1125
  }
1126

1127
  int64_t startUs2 = taosGetTimestampUs();
15,658✔
1128
  pStmt->stat.setTbNameUs += startUs2 - startUs;
15,658✔
1129

1130
  return TSDB_CODE_SUCCESS;
15,658✔
1131
}
1132

1133
int stmtSetTbTags(TAOS_STMT* stmt, TAOS_MULTI_BIND* tags) {
15✔
1134
  STscStmt* pStmt = (STscStmt*)stmt;
15✔
1135

1136
  STMT_DLOG_E("start to set tbTags");
15!
1137

1138
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
15✔
1139
    return pStmt->errCode;
4✔
1140
  }
1141

1142
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
11!
1143

1144
  SBoundColInfo* tags_info = (SBoundColInfo*)pStmt->bInfo.boundTags;
11✔
1145
  if (tags_info->numOfBound <= 0 || tags_info->numOfCols <= 0) {
11!
1146
    tscWarn("no tags bound in sql, will not bound tags");
×
1147
    return TSDB_CODE_SUCCESS;
×
1148
  }
1149

1150
  if (pStmt->bInfo.inExecCache) {
11!
1151
    return TSDB_CODE_SUCCESS;
×
1152
  }
1153

1154
  STableDataCxt** pDataBlock =
1155
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
11✔
1156
  if (NULL == pDataBlock) {
11!
1157
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1158
    STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1159
  }
1160

1161
  tscDebug("start to bind stmt tag values");
11!
1162
  STMT_ERR_RET(qBindStmtTagsValue(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.tbSuid, pStmt->bInfo.stbFName,
11!
1163
                                  pStmt->bInfo.sname.tname, tags, pStmt->exec.pRequest->msgBuf,
1164
                                  pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt));
1165

1166
  return TSDB_CODE_SUCCESS;
11✔
1167
}
1168

1169
int stmtFetchTagFields(STscStmt* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
2✔
1170
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
2✔
1171
    return pStmt->errCode;
1✔
1172
  }
1173

1174
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
1!
1175
    tscError("invalid operation to get query tag fileds");
×
1176
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1177
  }
1178

1179
  STableDataCxt** pDataBlock =
1180
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
1✔
1181
  if (NULL == pDataBlock) {
1!
1182
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1183
    STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1184
  }
1185

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

1188
  return TSDB_CODE_SUCCESS;
1✔
1189
}
1190

1191
int stmtFetchColFields(STscStmt* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
11✔
1192
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
11!
1193
    return pStmt->errCode;
×
1194
  }
1195

1196
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
11!
1197
    tscError("invalid operation to get query column fileds");
×
1198
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1199
  }
1200

1201
  STableDataCxt** pDataBlock = NULL;
11✔
1202

1203
  if (pStmt->sql.stbInterlaceMode) {
11!
1204
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
×
1205
  } else {
1206
    pDataBlock =
1207
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
11✔
1208
    if (NULL == pDataBlock) {
11!
1209
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1210
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1211
    }
1212
  }
1213
  if (pStmt->sql.predicateCols) {
11✔
1214
    STMT_ERR_RET(qBuildUpdateStmtColFields(*pDataBlock, fieldNum, fields, pStmt->sql.predicateCols));
5!
1215
  } else {
1216
    STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields));
6!
1217
  }
1218

1219
  return TSDB_CODE_SUCCESS;
11✔
1220
}
1221

1222
/*
1223
SArray* stmtGetFreeCol(STscStmt* pStmt, int32_t* idx) {
1224
  while (true) {
1225
    if (pStmt->exec.smInfo.pColIdx >= STMT_COL_BUF_SIZE) {
1226
      pStmt->exec.smInfo.pColIdx = 0;
1227
    }
1228

1229
    if ((pStmt->exec.smInfo.pColIdx + 1) == atomic_load_32(&pStmt->exec.smInfo.pColFreeIdx)) {
1230
      taosUsleep(1);
1231
      continue;
1232
    }
1233

1234
    *idx = pStmt->exec.smInfo.pColIdx;
1235
    return pStmt->exec.smInfo.pCols[pStmt->exec.smInfo.pColIdx++];
1236
  }
1237
}
1238
*/
1239

1240
int32_t stmtAppendTablePostHandle(STscStmt* pStmt, SStmtQNode* param) {
5,624✔
1241
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
5,624✔
1242
    pStmt->sql.siInfo.pVgroupHash =
4,551✔
1243
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
4,549✔
1244
  }
1245
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
5,626✔
1246
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
4,552✔
1247
  }
1248

1249
  if (NULL == pStmt->sql.siInfo.pRequest) {
5,629✔
1250
    STMT_ERR_RET(buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false,
77!
1251
                              (SRequestObj**)&pStmt->sql.siInfo.pRequest, pStmt->reqid));
1252

1253
    if (pStmt->reqid != 0) {
77!
1254
      pStmt->reqid++;
×
1255
    }
1256
    pStmt->exec.pRequest->syncQuery = true;
77✔
1257

1258
    pStmt->sql.siInfo.requestId = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->requestId;
77✔
1259
    pStmt->sql.siInfo.requestSelf = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->self;
77✔
1260
  }
1261

1262
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
5,629✔
1263
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
372✔
1264
    pStmt->sql.siInfo.tbFromHash = true;
22✔
1265
  }
1266

1267
  if (0 == pStmt->sql.siInfo.firstName[0]) {
5,629✔
1268
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
77✔
1269
  }
1270

1271
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
5,629✔
1272
  param->next = NULL;
5,629✔
1273

1274
  (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
5,629✔
1275

1276
  stmtEnqueue(pStmt, param);
5,631✔
1277

1278
  return TSDB_CODE_SUCCESS;
5,629✔
1279
}
1280

1281
static FORCE_INLINE int32_t stmtGetTableColsFromCache(STscStmt* pStmt, SArray** pTableCols) {
1282
  while (true) {
1283
    if (pStmt->sql.siInfo.pTableColsIdx < taosArrayGetSize(pStmt->sql.siInfo.pTableCols)) {
5,630!
1284
      *pTableCols = (SArray*)taosArrayGetP(pStmt->sql.siInfo.pTableCols, pStmt->sql.siInfo.pTableColsIdx++);
5,622✔
1285
      break;
5,629✔
1286
    } else {
1287
      SArray* pTblCols = NULL;
×
1288
      for (int32_t i = 0; i < 100; i++) {
×
1289
        pTblCols = taosArrayInit(20, POINTER_BYTES);
×
1290
        if (NULL == pTblCols) {
×
1291
          return terrno;
×
1292
        }
1293

1294
        if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
×
1295
          return terrno;
×
1296
        }
1297
      }
1298
    }
1299
  }
1300

1301
  return TSDB_CODE_SUCCESS;
5,629✔
1302
}
1303

1304
int stmtBindBatch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind, int32_t colIdx) {
86,291✔
1305
  STscStmt* pStmt = (STscStmt*)stmt;
86,291✔
1306
  int32_t   code = 0;
86,291✔
1307

1308
  int64_t startUs = taosGetTimestampUs();
86,313✔
1309

1310
  STMT_DLOG("start to bind stmt data, colIdx:%d", colIdx);
86,313!
1311

1312
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
86,241✔
1313
    return pStmt->errCode;
84✔
1314
  }
1315

1316
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
86,157!
1317

1318
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
85,870!
1319
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1320
    pStmt->bInfo.needParse = false;
×
1321
  }
1322

1323
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
85,870!
1324
    taos_free_result(pStmt->exec.pRequest);
×
1325
    pStmt->exec.pRequest = NULL;
×
1326
  }
1327

1328
  STMT_ERR_RET(stmtCreateRequest(pStmt));
85,870!
1329

1330
  if (pStmt->bInfo.needParse) {
85,805✔
1331
    STMT_ERR_RET(stmtParseSql(pStmt));
836✔
1332
  }
1333

1334
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
85,834✔
1335
    STMT_ERR_RET(qStmtBindParams(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt));
2!
1336

1337
    SParseContext ctx = {.requestId = pStmt->exec.pRequest->requestId,
2✔
1338
                         .acctId = pStmt->taos->acctId,
2✔
1339
                         .db = pStmt->exec.pRequest->pDb,
2✔
1340
                         .topicQuery = false,
1341
                         .pSql = pStmt->sql.sqlStr,
2✔
1342
                         .sqlLen = pStmt->sql.sqlLen,
2✔
1343
                         .pMsg = pStmt->exec.pRequest->msgBuf,
2✔
1344
                         .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1345
                         .pTransporter = pStmt->taos->pAppInfo->pTransporter,
2✔
1346
                         .pStmtCb = NULL,
1347
                         .pUser = pStmt->taos->user,
2✔
1348
                         .setQueryFp = setQueryRequest};
1349

1350
    ctx.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
2✔
1351
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &ctx.pCatalog));
2!
1352

1353
    STMT_ERR_RET(qStmtParseQuerySql(&ctx, pStmt->sql.pQuery));
2!
1354

1355
    if (pStmt->sql.pQuery->haveResultSet) {
2!
1356
      STMT_ERR_RET(setResSchemaInfo(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->pResSchema,
2!
1357
                                    pStmt->sql.pQuery->numOfResCols, pStmt->sql.pQuery->pResExtSchema, true));
1358
      taosMemoryFreeClear(pStmt->sql.pQuery->pResSchema);
2!
1359
      taosMemoryFreeClear(pStmt->sql.pQuery->pResExtSchema);
2!
1360
      setResPrecision(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->precision);
2✔
1361
    }
1362

1363
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
2✔
1364
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
2✔
1365
    TSWAP(pStmt->exec.pRequest->targetTableList, pStmt->sql.pQuery->pTargetTableList);
2✔
1366

1367
    return TSDB_CODE_SUCCESS;
2✔
1368
  }
1369

1370
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
85,832!
1371
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1372
  }
1373

1374
  STableDataCxt** pDataBlock = NULL;
85,796✔
1375

1376
  if (pStmt->exec.pCurrBlock) {
85,796✔
1377
    pDataBlock = &pStmt->exec.pCurrBlock;
74,879✔
1378
  } else {
1379
    pDataBlock =
1380
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
10,917✔
1381
    if (NULL == pDataBlock) {
10,928!
1382
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1383
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1384
    }
1385
    pStmt->exec.pCurrBlock = *pDataBlock;
10,928✔
1386
    if (pStmt->sql.stbInterlaceMode) {
10,928✔
1387
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
77✔
1388
      pStmt->exec.pCurrBlock->pData->aCol = NULL;
77✔
1389
    }
1390
  }
1391

1392
  int64_t startUs2 = taosGetTimestampUs();
85,781✔
1393
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
85,781✔
1394

1395
  SStmtQNode* param = NULL;
85,781✔
1396
  if (pStmt->sql.stbInterlaceMode) {
85,781✔
1397
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
11,260!
1398
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
11,259!
1399
    taosArrayClear(param->tblData.aCol);
5,629✔
1400

1401
    // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES);
1402

1403
    param->restoreTbCols = false;
5,621✔
1404
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
5,621✔
1405
  }
1406

1407
  int64_t startUs3 = taosGetTimestampUs();
85,772✔
1408
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
85,772✔
1409

1410
  SArray* pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
85,772✔
1411

1412
  if (pStmt->sql.predicateCols) {
85,772✔
1413
    STMT_ERR_RET(qBindUpdateStmtColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf,
5!
1414
                                          pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt,
1415
                                          pStmt->sql.predicateCols));
1416
    return TSDB_CODE_SUCCESS;
5✔
1417
  }
1418

1419
  if (colIdx < 0) {
85,767!
1420
    if (pStmt->sql.stbInterlaceMode) {
85,779✔
1421
      (*pDataBlock)->pData->flags = 0;
5,627✔
1422
      code =
1423
          qBindStmtStbColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
5,627✔
1424
                                &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt);
5,627✔
1425
    } else {
1426
      code = qBindStmtColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
80,152✔
1427
                                pStmt->taos->optionInfo.charsetCxt);
80,152✔
1428
    }
1429

1430
    if (code) {
85,942✔
1431
      tscError("qBindStmtColsValue failed, error:%s", tstrerror(code));
1!
1432
      STMT_ERR_RET(code);
1!
1433
    }
1434
  } else {
1435
    if (pStmt->sql.stbInterlaceMode) {
×
1436
      tscError("bind single column not allowed in stb insert mode");
×
1437
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1438
    }
1439

1440
    if (colIdx != (pStmt->bInfo.sBindLastIdx + 1) && colIdx != 0) {
×
1441
      tscError("bind column index not in sequence");
×
1442
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1443
    }
1444

1445
    pStmt->bInfo.sBindLastIdx = colIdx;
×
1446

1447
    if (0 == colIdx) {
×
1448
      pStmt->bInfo.sBindRowNum = bind->num;
×
1449
    }
1450

1451
    code =
1452
        qBindStmtSingleColValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
×
1453
                                colIdx, pStmt->bInfo.sBindRowNum, pStmt->taos->optionInfo.charsetCxt);
×
1454
    if (code) {
×
1455
      tscError("qBindStmtSingleColValue failed, error:%s", tstrerror(code));
×
1456
      STMT_ERR_RET(code);
×
1457
    }
1458
  }
1459

1460
  int64_t startUs4 = taosGetTimestampUs();
86,145✔
1461
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
86,145✔
1462

1463
  if (pStmt->sql.stbInterlaceMode) {
86,145✔
1464
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
5,628!
1465
  }
1466

1467
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
86,261✔
1468

1469
  return TSDB_CODE_SUCCESS;
86,261✔
1470
}
1471

1472
int stmtAddBatch(TAOS_STMT* stmt) {
84,670✔
1473
  STscStmt* pStmt = (STscStmt*)stmt;
84,670✔
1474

1475
  int64_t startUs = taosGetTimestampUs();
84,544✔
1476

1477
  STMT_DLOG_E("start to add batch");
84,544!
1478

1479
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
84,571✔
1480
    return pStmt->errCode;
30✔
1481
  }
1482

1483
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
84,541!
1484

1485
  if (pStmt->sql.stbInterlaceMode) {
84,633✔
1486
    int64_t startUs2 = taosGetTimestampUs();
4,552✔
1487
    pStmt->stat.addBatchUs += startUs2 - startUs;
4,552✔
1488

1489
    pStmt->sql.siInfo.tableColsReady = false;
4,552✔
1490

1491
    SStmtQNode* param = NULL;
4,552✔
1492
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
9,105!
1493
    param->restoreTbCols = true;
4,553✔
1494
    param->next = NULL;
4,553✔
1495

1496
    stmtEnqueue(pStmt, param);
4,553✔
1497

1498
    return TSDB_CODE_SUCCESS;
4,554✔
1499
  }
1500

1501
  STMT_ERR_RET(stmtCacheBlock(pStmt));
80,080!
1502

1503
  return TSDB_CODE_SUCCESS;
80,002✔
1504
}
1505
/*
1506
int stmtUpdateTableUid(STscStmt* pStmt, SSubmitRsp* pRsp) {
1507
  tscDebug("stmt start to update tbUid, blockNum:%d", pRsp->nBlocks);
1508

1509
  int32_t code = 0;
1510
  int32_t finalCode = 0;
1511
  size_t  keyLen = 0;
1512
  void*   pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
1513
  while (pIter) {
1514
    STableDataCxt* pBlock = *(STableDataCxt**)pIter;
1515
    char*          key = taosHashGetKey(pIter, &keyLen);
1516

1517
    STableMeta* pMeta = qGetTableMetaInDataBlock(pBlock);
1518
    if (pMeta->uid) {
1519
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1520
      continue;
1521
    }
1522

1523
    SSubmitBlkRsp* blkRsp = NULL;
1524
    int32_t        i = 0;
1525
    for (; i < pRsp->nBlocks; ++i) {
1526
      blkRsp = pRsp->pBlocks + i;
1527
      if (strlen(blkRsp->tblFName) != keyLen) {
1528
        continue;
1529
      }
1530

1531
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
1532
        continue;
1533
      }
1534

1535
      break;
1536
    }
1537

1538
    if (i < pRsp->nBlocks) {
1539
      tscDebug("auto created table %s uid updated from %" PRIx64 " to %" PRIx64, blkRsp->tblFName, pMeta->uid,
1540
               blkRsp->uid);
1541

1542
      pMeta->uid = blkRsp->uid;
1543
      pStmt->bInfo.tbUid = blkRsp->uid;
1544
    } else {
1545
      tscDebug("table %s not found in submit rsp, will update from catalog", pStmt->bInfo.tbFName);
1546
      if (NULL == pStmt->pCatalog) {
1547
        code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog);
1548
        if (code) {
1549
          pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1550
          finalCode = code;
1551
          continue;
1552
        }
1553
      }
1554

1555
      code = stmtCreateRequest(pStmt);
1556
      if (code) {
1557
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1558
        finalCode = code;
1559
        continue;
1560
      }
1561

1562
      STableMeta*      pTableMeta = NULL;
1563
      SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
1564
                               .requestId = pStmt->exec.pRequest->requestId,
1565
                               .requestObjRefId = pStmt->exec.pRequest->self,
1566
                               .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
1567
      code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
1568

1569
      pStmt->stat.ctgGetTbMetaNum++;
1570

1571
      taos_free_result(pStmt->exec.pRequest);
1572
      pStmt->exec.pRequest = NULL;
1573

1574
      if (code || NULL == pTableMeta) {
1575
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1576
        finalCode = code;
1577
        taosMemoryFree(pTableMeta);
1578
        continue;
1579
      }
1580

1581
      pMeta->uid = pTableMeta->uid;
1582
      pStmt->bInfo.tbUid = pTableMeta->uid;
1583
      taosMemoryFree(pTableMeta);
1584
    }
1585

1586
    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1587
  }
1588

1589
  return finalCode;
1590
}
1591
*/
1592

1593
/*
1594
int stmtStaticModeExec(TAOS_STMT* stmt) {
1595
  STscStmt*   pStmt = (STscStmt*)stmt;
1596
  int32_t     code = 0;
1597
  SSubmitRsp* pRsp = NULL;
1598
  if (pStmt->sql.staticMode) {
1599
    return TSDB_CODE_TSC_STMT_API_ERROR;
1600
  }
1601

1602
  STMT_DLOG_E("start to exec");
1603

1604
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
1605

1606
  STMT_ERR_RET(qBuildStmtOutputFromTbList(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pTbBlkList,
1607
pStmt->exec.pCurrBlock, pStmt->exec.tbBlkNum));
1608

1609
  launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
1610

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

1622
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
1623

1624
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
1625
  pStmt->affectedRows += pStmt->exec.affectedRows;
1626

1627
_return:
1628

1629
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
1630

1631
  tFreeSSubmitRsp(pRsp);
1632

1633
  ++pStmt->sql.runTimes;
1634

1635
  STMT_RET(code);
1636
}
1637
*/
1638

1639
int stmtExec(TAOS_STMT* stmt) {
15,451✔
1640
  STscStmt*   pStmt = (STscStmt*)stmt;
15,451✔
1641
  int32_t     code = 0;
15,451✔
1642
  SSubmitRsp* pRsp = NULL;
15,451✔
1643

1644
  int64_t startUs = taosGetTimestampUs();
15,459✔
1645

1646
  STMT_DLOG_E("start to exec");
15,459!
1647

1648
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
15,455✔
1649
    return pStmt->errCode;
30✔
1650
  }
1651

1652
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
15,425✔
1653

1654
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
15,427✔
1655
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
2✔
1656
  } else {
1657
    if (pStmt->sql.stbInterlaceMode) {
15,425✔
1658
      int64_t startTs = taosGetTimestampUs();
4,552✔
1659
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
11,140✔
1660
        taosUsleep(1);
6,586✔
1661
      }
1662
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
4,555✔
1663

1664
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
4,555!
1665
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
4,553✔
1666
      pStmt->sql.siInfo.pVgroupHash = NULL;
4,555✔
1667
      pStmt->sql.siInfo.pVgroupList = NULL;
4,555✔
1668
    } else {
1669
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
10,872✔
1670
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
10,869!
1671

1672
      STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
10,869!
1673

1674
      STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
10,876!
1675
    }
1676

1677
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
15,424✔
1678
  }
1679

1680
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
15,435!
1681
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
×
1682
    if (code) {
×
1683
      pStmt->exec.pRequest->code = code;
×
1684
    } else {
1685
      tFreeSSubmitRsp(pRsp);
×
1686
      STMT_ERR_RET(stmtResetStmt(pStmt));
×
1687
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
×
1688
    }
1689
  }
1690

1691
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
15,438!
1692

1693
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
15,438✔
1694
  pStmt->affectedRows += pStmt->exec.affectedRows;
15,440✔
1695

1696
_return:
15,440✔
1697

1698
  while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
15,440!
1699
    taosUsleep(1);
×
1700
  }
1701

1702
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
15,439!
1703

1704
  tFreeSSubmitRsp(pRsp);
15,429✔
1705

1706
  ++pStmt->sql.runTimes;
15,426✔
1707

1708
  int64_t startUs2 = taosGetTimestampUs();
15,434✔
1709
  pStmt->stat.execUseUs += startUs2 - startUs;
15,434✔
1710

1711
  STMT_RET(code);
15,434!
1712
}
1713

1714
int stmtClose(TAOS_STMT* stmt) {
996✔
1715
  STscStmt* pStmt = (STscStmt*)stmt;
996✔
1716

1717
  STMT_DLOG_E("start to free stmt");
996!
1718

1719
  if (pStmt->bindThreadInUse) {
996✔
1720
    pStmt->queue.stopQueue = true;
95✔
1721

1722
    (void)taosThreadMutexLock(&pStmt->queue.mutex);
95✔
1723
    (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
95✔
1724
    (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
95✔
1725
    (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
95✔
1726

1727
    (void)taosThreadJoin(pStmt->bindThread, NULL);
95✔
1728
    pStmt->bindThreadInUse = false;
95✔
1729

1730
    (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
95✔
1731
    (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
95✔
1732
  }
1733

1734
  STMT_DLOG("stmt %p closed, stbInterlaceMode:%d, statInfo: ctgGetTbMetaNum=>%" PRId64 ", getCacheTbInfo=>%" PRId64
997!
1735
            ", parseSqlNum=>%" PRId64 ", pStmt->stat.bindDataNum=>%" PRId64
1736
            ", settbnameAPI:%u, bindAPI:%u, addbatchAPI:%u, execAPI:%u"
1737
            ", setTbNameUs:%" PRId64 ", bindDataUs:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64 " addBatchUs:%" PRId64
1738
            ", execWaitUs:%" PRId64 ", execUseUs:%" PRId64,
1739
            pStmt, pStmt->sql.stbInterlaceMode, pStmt->stat.ctgGetTbMetaNum, pStmt->stat.getCacheTbInfo,
1740
            pStmt->stat.parseSqlNum, pStmt->stat.bindDataNum, pStmt->seqIds[STMT_SETTBNAME], pStmt->seqIds[STMT_BIND],
1741
            pStmt->seqIds[STMT_ADD_BATCH], pStmt->seqIds[STMT_EXECUTE], pStmt->stat.setTbNameUs,
1742
            pStmt->stat.bindDataUs1, pStmt->stat.bindDataUs2, pStmt->stat.bindDataUs3, pStmt->stat.bindDataUs4,
1743
            pStmt->stat.addBatchUs, pStmt->stat.execWaitUs, pStmt->stat.execUseUs);
1744

1745
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
997!
1746
  taosMemoryFree(stmt);
997!
1747

1748
  return TSDB_CODE_SUCCESS;
996✔
1749
}
1750

1751
const char* stmtErrstr(TAOS_STMT* stmt) {
189✔
1752
  STscStmt* pStmt = (STscStmt*)stmt;
189✔
1753

1754
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
189!
1755
    return (char*)tstrerror(terrno);
×
1756
  }
1757

1758
  pStmt->exec.pRequest->code = terrno;
189✔
1759

1760
  return taos_errstr(pStmt->exec.pRequest);
189✔
1761
}
1762

1763
int stmtAffectedRows(TAOS_STMT* stmt) { return ((STscStmt*)stmt)->affectedRows; }
×
1764

1765
int stmtAffectedRowsOnce(TAOS_STMT* stmt) { return ((STscStmt*)stmt)->exec.affectedRows; }
56✔
1766

1767
int stmtIsInsert(TAOS_STMT* stmt, int* insert) {
101,884✔
1768
  STscStmt* pStmt = (STscStmt*)stmt;
101,884✔
1769

1770
  STMT_DLOG_E("start is insert");
101,884!
1771

1772
  if (pStmt->sql.type) {
101,962✔
1773
    *insert = (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
91,028!
1774
  } else {
1775
    *insert = qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
10,934✔
1776
  }
1777

1778
  return TSDB_CODE_SUCCESS;
101,965✔
1779
}
1780

1781
int stmtGetTagFields(TAOS_STMT* stmt, int* nums, TAOS_FIELD_E** fields) {
6✔
1782
  int32_t   code = 0;
6✔
1783
  STscStmt* pStmt = (STscStmt*)stmt;
6✔
1784
  int32_t   preCode = pStmt->errCode;
6✔
1785

1786
  STMT_DLOG_E("start to get tag fields");
6!
1787

1788
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
6!
1789
    return pStmt->errCode;
×
1790
  }
1791

1792
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
6!
1793
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1794
  }
1795

1796
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
6!
1797

1798
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
6!
1799
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1800
    pStmt->bInfo.needParse = false;
×
1801
  }
1802

1803
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
6!
1804
    taos_free_result(pStmt->exec.pRequest);
×
1805
    pStmt->exec.pRequest = NULL;
×
1806
  }
1807

1808
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
6!
1809

1810
  if (pStmt->bInfo.needParse) {
6✔
1811
    STMT_ERRI_JRET(stmtParseSql(pStmt));
5✔
1812
  }
1813

1814
  STMT_ERRI_JRET(stmtFetchTagFields(stmt, nums, fields));
2✔
1815

1816
_return:
1✔
1817
  // compatible with previous versions
1818
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && (pStmt->bInfo.tbNameFlag & NO_DATA_USING_CLAUSE) == 0x0) {
6!
1819
    code = TSDB_CODE_TSC_STMT_TBNAME_ERROR;
1✔
1820
  }
1821

1822
  if (code != TSDB_CODE_SUCCESS) {
6✔
1823
    taos_free_result(pStmt->exec.pRequest);
5✔
1824
    pStmt->exec.pRequest = NULL;
5✔
1825
  }
1826
  pStmt->errCode = preCode;
6✔
1827

1828
  return code;
6✔
1829
}
1830

1831
int stmtGetColFields(TAOS_STMT* stmt, int* nums, TAOS_FIELD_E** fields) {
7✔
1832
  int32_t   code = 0;
7✔
1833
  STscStmt* pStmt = (STscStmt*)stmt;
7✔
1834
  int32_t   preCode = pStmt->errCode;
7✔
1835

1836
  STMT_DLOG_E("start to get col fields");
7!
1837

1838
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
7✔
1839
    return pStmt->errCode;
1✔
1840
  }
1841

1842
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
6!
1843
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1844
  }
1845

1846
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
6!
1847

1848
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
6!
1849
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1850
    pStmt->bInfo.needParse = false;
×
1851
  }
1852

1853
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
6!
1854
    taos_free_result(pStmt->exec.pRequest);
×
1855
    pStmt->exec.pRequest = NULL;
×
1856
    STMT_ERR_RET(stmtCreateRequest(pStmt));
×
1857
  }
1858

1859
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
6!
1860

1861
  if (pStmt->bInfo.needParse) {
6✔
1862
    STMT_ERRI_JRET(stmtParseSql(pStmt));
5!
1863
  }
1864

1865
  STMT_ERRI_JRET(stmtFetchColFields(stmt, nums, fields));
6!
1866

1867
_return:
6✔
1868

1869
  if (code != TSDB_CODE_SUCCESS) {
6!
1870
    taos_free_result(pStmt->exec.pRequest);
×
1871
    pStmt->exec.pRequest = NULL;
×
1872
  }
1873

1874
  pStmt->errCode = preCode;
6✔
1875

1876
  return code;
6✔
1877
}
1878

1879
int stmtGetParamNum(TAOS_STMT* stmt, int* nums) {
4✔
1880
  int       code = 0;
4✔
1881
  STscStmt* pStmt = (STscStmt*)stmt;
4✔
1882
  int32_t   preCode = pStmt->errCode;
4✔
1883

1884
  STMT_DLOG_E("start to get param num");
4!
1885

1886
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
4✔
1887
    return pStmt->errCode;
1✔
1888
  }
1889

1890
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
3!
1891

1892
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
3!
1893
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1894
    pStmt->bInfo.needParse = false;
×
1895
  }
1896

1897
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
3!
1898
    taos_free_result(pStmt->exec.pRequest);
×
1899
    pStmt->exec.pRequest = NULL;
×
1900
  }
1901

1902
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
3!
1903

1904
  if (pStmt->bInfo.needParse) {
3✔
1905
    STMT_ERRI_JRET(stmtParseSql(pStmt));
2!
1906
  }
1907

1908
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
1!
1909
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
×
1910
  } else {
1911
    STMT_ERRI_JRET(stmtFetchColFields(stmt, nums, NULL));
1!
1912
  }
1913

1914
_return:
1✔
1915

1916
  if (code != TSDB_CODE_SUCCESS) {
3✔
1917
    taos_free_result(pStmt->exec.pRequest);
2✔
1918
    pStmt->exec.pRequest = NULL;
2✔
1919
  }
1920

1921
  pStmt->errCode = preCode;
3✔
1922

1923
  return code;
3✔
1924
}
1925

1926
int stmtGetParam(TAOS_STMT* stmt, int idx, int* type, int* bytes) {
4✔
1927
  int       code = 0;
4✔
1928
  STscStmt* pStmt = (STscStmt*)stmt;
4✔
1929
  int32_t   preCode = pStmt->errCode;
4✔
1930

1931
  STMT_DLOG_E("start to get param");
4!
1932

1933
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
4!
1934
    return pStmt->errCode;
×
1935
  }
1936

1937
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
4!
1938
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1939
  }
1940

1941
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
4!
1942

1943
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
4!
1944
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1945
    pStmt->bInfo.needParse = false;
×
1946
  }
1947

1948
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
4!
1949
    taos_free_result(pStmt->exec.pRequest);
×
1950
    pStmt->exec.pRequest = NULL;
×
1951
  }
1952

1953
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
4!
1954

1955
  if (pStmt->bInfo.needParse) {
4!
1956
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
1957
  }
1958

1959
  int32_t       nums = 0;
4✔
1960
  TAOS_FIELD_E* pField = NULL;
4✔
1961
  STMT_ERRI_JRET(stmtFetchColFields(stmt, &nums, &pField));
4!
1962
  if (idx >= nums) {
4!
1963
    tscError("idx %d is too big", idx);
×
1964
    STMT_ERRI_JRET(TSDB_CODE_INVALID_PARA);
×
1965
  }
1966

1967
  *type = pField[idx].type;
4✔
1968
  *bytes = calcSchemaBytesFromTypeBytes(pField[idx].type, pField[idx].bytes, true);
4✔
1969

1970
_return:
4✔
1971

1972
  if (code != TSDB_CODE_SUCCESS) {
4!
1973
    taos_free_result(pStmt->exec.pRequest);
×
1974
    pStmt->exec.pRequest = NULL;
×
1975
  }
1976

1977
  taosMemoryFree(pField);
4!
1978
  pStmt->errCode = preCode;
4✔
1979

1980
  return code;
4✔
1981
}
1982

1983
TAOS_RES* stmtUseResult(TAOS_STMT* stmt) {
2✔
1984
  STscStmt* pStmt = (STscStmt*)stmt;
2✔
1985

1986
  STMT_DLOG_E("start to use result");
2!
1987

1988
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
2!
1989
    tscError("useResult only for query statement");
×
1990
    return NULL;
×
1991
  }
1992

1993
  return pStmt->exec.pRequest;
2✔
1994
}
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