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

taosdata / TDengine / #4651

04 Aug 2025 01:33AM UTC coverage: 60.986% (-0.3%) from 61.262%
#4651

push

travis-ci

web-flow
docs(taosx): update Data-In tasks name to TDengine Subscription and TDengine Query (#32433)

Closes [TS-6956](https://jira.taosdata.com:18080/browse/TS-6956)

139941 of 291599 branches covered (47.99%)

Branch coverage included in aggregate %.

211138 of 284076 relevant lines covered (74.32%)

4897793.5 hits per line

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

64.42
/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,264✔
15
    *pBuf = (char*)pTblBuf->pCurBuff + pTblBuf->buffOffset;
10,272✔
16
    pTblBuf->buffOffset += pTblBuf->buffUnit;
10,272✔
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,272✔
41
}
42

43
bool stmtDequeue(STscStmt* pStmt, SStmtQNode** param) {
10,279✔
44
  int i = 0;
10,279✔
45
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
61,765✔
46
    if (i < 10) {
51,507✔
47
      taosUsleep(1);
46,836✔
48
      i++;
46,818✔
49
    } else {
50
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
4,671✔
51
      if (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
4,670!
52
        (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
4,670✔
53
      }
54
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
4,668✔
55
    }
56
  }
57
  if (pStmt->queue.stopQueue) {
10,235✔
58
    return false;
95✔
59
  }
60
  SStmtQNode* orig = pStmt->queue.head;
10,140✔
61
  SStmtQNode* node = pStmt->queue.head->next;
10,140✔
62
  pStmt->queue.head = pStmt->queue.head->next;
10,140✔
63
  *param = node;
10,140✔
64

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

67
  return true;
10,183✔
68
}
69

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

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

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

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

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

97
  return code;
119,102✔
98
}
99

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

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

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

112
  switch (newStatus) {
212,958!
113
    case STMT_PREPARE:
10,980✔
114
      pStmt->errCode = 0;
10,980✔
115
      break;
10,980✔
116
    case STMT_SETTBNAME:
15,655✔
117
      if (STMT_STATUS_EQ(INIT)) {
15,655!
118
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
119
      }
120
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
15,655!
121
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
122
      }
123
      break;
15,655✔
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:
13✔
130
      if (STMT_STATUS_EQ(INIT)) {
13!
131
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
132
      }
133
      break;
13✔
134
    case STMT_BIND:
86,306✔
135
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
86,306!
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;
86,306✔
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,564✔
150
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
84,564!
151
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
152
      }
153
      break;
84,564✔
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,958!
172

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

175
  return TSDB_CODE_SUCCESS;
212,958✔
176
}
177

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

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

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

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

190
  return TSDB_CODE_SUCCESS;
10,086✔
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,904✔
243
                           bool autoCreateTbl, uint8_t tbNameFlag) {
244
  STscStmt* pStmt = (STscStmt*)stmt;
10,904✔
245
  char      tbFName[TSDB_TABLE_FNAME_LEN];
246
  int32_t   code = tNameExtractFullName(tbName, tbFName);
10,904✔
247
  if (code != 0) {
10,915!
248
    return code;
×
249
  }
250

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

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

264
  return TSDB_CODE_SUCCESS;
10,915✔
265
}
266

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

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

273
  return TSDB_CODE_SUCCESS;
10,904✔
274
}
275

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

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

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

289
  return TSDB_CODE_SUCCESS;
10,916✔
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,815✔
305
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
79,815✔
306
    return TSDB_CODE_SUCCESS;
69,909✔
307
  }
308

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

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

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

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

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

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

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

339
  return TSDB_CODE_SUCCESS;
10,017✔
340
}
341

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

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

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

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

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

361
  if (pStmt->sql.pQuery->pRoot && 0 == pStmt->sql.type) {
10,913!
362
    pStmt->sql.type = STMT_TYPE_INSERT;
824✔
363
    pStmt->sql.stbInterlaceMode = false;
824✔
364
  } else if (pStmt->sql.pQuery->pPrepareRoot) {
10,089✔
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,911✔
373
  if (NULL == pSrc || NULL == *pSrc) {
10,921!
374
    return terrno;
×
375
  }
376

377
  STableDataCxt* pTableCtx = *pSrc;
10,921✔
378
  if (pStmt->sql.stbInterlaceMode) {
10,921✔
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,921✔
392
    pStmt->sql.pBindInfo = taosMemoryMalloc(pTableCtx->boundColsInfo.numOfBound * sizeof(*pStmt->sql.pBindInfo));
10,919!
393
    if (NULL == pStmt->sql.pBindInfo) {
10,908!
394
      return terrno;
×
395
    }
396
  }
397

398
  return TSDB_CODE_SUCCESS;
10,910✔
399
}
400

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

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

417
  return TSDB_CODE_SUCCESS;
26,507✔
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,542✔
426
  pTblBuf->pCurBuff = taosArrayGetP(pTblBuf->pBufList, 0);
4,542✔
427
  if (NULL == pTblBuf->pCurBuff) {
4,552✔
428
    tscError("QInfo:%p, failed to get buffer from list", pTblBuf);
1!
429
    return;
×
430
  }
431
  pTblBuf->buffIdx = 1;
4,551✔
432
  pTblBuf->buffOffset = sizeof(*pQueue->head);
4,551✔
433

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

439
int32_t stmtCleanExecInfo(STscStmt* pStmt, bool keepTable, bool deepClean) {
26,390✔
440
  if (pStmt->sql.stbInterlaceMode) {
26,390✔
441
    if (deepClean) {
4,626✔
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,549✔
451
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
4,549✔
452
      tSimpleHashClear(pStmt->sql.siInfo.pTableRowDataHash);
4,551✔
453
    }
454
  } else {
455
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
21,764✔
456
      taos_free_result(pStmt->exec.pRequest);
21,762✔
457
      pStmt->exec.pRequest = NULL;
21,767✔
458
    }
459

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

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

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

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

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

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

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

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

492
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
15,532!
493

494
  return TSDB_CODE_SUCCESS;
15,528✔
495
}
496

497
void stmtFreeTbBuf(void* buf) {
94✔
498
  void* pBuf = *(void**)buf;
94✔
499
  taosMemoryFree(pBuf);
94!
500
}
93✔
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,968✔
508
  STMT_DLOG_E("start to free SQL info");
10,968!
509

510
  taosMemoryFree(pStmt->sql.pBindInfo);
10,968!
511
  taosMemoryFree(pStmt->sql.queryRes.fields);
10,985!
512
  taosMemoryFree(pStmt->sql.queryRes.userFields);
10,982!
513
  taosMemoryFree(pStmt->sql.sqlStr);
10,981!
514
  qDestroyQuery(pStmt->sql.pQuery);
10,982✔
515
  taosArrayDestroy(pStmt->sql.nodeList);
10,985✔
516
  taosHashCleanup(pStmt->sql.pVgHash);
10,978✔
517
  pStmt->sql.pVgHash = NULL;
10,982✔
518

519
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
10,982✔
520
  while (pIter) {
20,999✔
521
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
10,015✔
522

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

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

532
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
10,984!
533
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
10,981!
534

535
  taos_free_result(pStmt->sql.siInfo.pRequest);
10,982✔
536
  taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
10,981✔
537
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
10,985✔
538
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableRowDataHash);
10,977✔
539
  taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
10,983✔
540
  taosMemoryFree(pStmt->sql.siInfo.pTSchema);
10,982!
541
  qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
10,984✔
542
  taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
10,980✔
543

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

547
  STMT_DLOG_E("end to free SQL info");
10,984!
548

549
  return TSDB_CODE_SUCCESS;
10,973✔
550
}
551

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

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

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

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

574
  *vgId = vgInfo.vgId;
9✔
575

576
  return TSDB_CODE_SUCCESS;
9✔
577
}
578

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

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

586
  return TSDB_CODE_SUCCESS;
16✔
587
}
588

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

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

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

606
  return TSDB_CODE_SUCCESS;
7✔
607
}
608

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

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

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

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

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

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

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

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

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

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

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

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

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

666
      return TSDB_CODE_SUCCESS;
8✔
667
    }
668

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

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

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

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

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

689
    STMT_ERR_RET(code);
×
690
  }
691

692
  STMT_ERR_RET(code);
8!
693

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

700
  taosMemoryFree(pTableMeta);
8!
701

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

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

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

709
    return TSDB_CODE_SUCCESS;
×
710
  }
711

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

718
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
719
    }
720

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

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

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

731
    return TSDB_CODE_SUCCESS;
×
732
  }
733

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

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

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

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

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

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

756
    return TSDB_CODE_SUCCESS;
8✔
757
  }
758

759
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
760

761
  return TSDB_CODE_SUCCESS;
×
762
}
763

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

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

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

776
  pStmt->sql.status = STMT_INIT;
9,997✔
777

778
  return TSDB_CODE_SUCCESS;
9,997✔
779
}
780

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

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

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

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

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

805
void* stmtBindThreadFunc(void* param) {
95✔
806
  setThreadName("stmtBind");
95✔
807

808
  qInfo("stmt bind thread started");
95!
809

810
  STscStmt* pStmt = (STscStmt*)param;
95✔
811

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

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

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

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

830
  return NULL;
95✔
831
}
832

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

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

847
  pStmt->bindThreadInUse = true;
95✔
848

849
  (void)taosThreadAttrDestroy(&thAttr);
95✔
850
  return TSDB_CODE_SUCCESS;
95✔
851
}
852

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

859
  return TSDB_CODE_SUCCESS;
95✔
860
}
861

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

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

878
  pTblBuf->pCurBuff = buff;
95✔
879
  pTblBuf->buffIdx = 1;
95✔
880
  pTblBuf->buffOffset = 0;
95✔
881

882
  return TSDB_CODE_SUCCESS;
95✔
883
}
884

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

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

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

901
  pStmt->taos = pObj;
987✔
902
  pStmt->bInfo.needParse = true;
987✔
903
  pStmt->sql.status = STMT_INIT;
987✔
904
  pStmt->reqid = reqid;
987✔
905
  pStmt->errCode = TSDB_CODE_SUCCESS;
987✔
906

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

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

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

950
  pStmt->sql.siInfo.tableColsReady = true;
986✔
951

952
  STMT_LOG_SEQ(STMT_INIT);
986!
953

954
  tscDebug("stmt:%p initialized", pStmt);
986!
955

956
  return pStmt;
987✔
957
}
958

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

962
  STMT_DLOG_E("start to prepare");
10,973!
963

964
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
10,983!
965
    return pStmt->errCode;
×
966
  }
967

968
  if (pStmt->sql.status >= STMT_PREPARE) {
10,983✔
969
    STMT_ERR_RET(stmtResetStmt(pStmt));
9,998!
970
  }
971

972
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
10,978!
973

974
  if (length <= 0) {
10,980✔
975
    length = strlen(sql);
894✔
976
  }
977

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

985
  STMT_ERR_RET(stmtCreateRequest(pStmt));
10,983!
986

987
  int32_t code = 0;
10,975✔
988
  if (qIsUpdateSetSql(sql, strlen(sql), &pStmt->bInfo.sname, pStmt->taos->acctId, pStmt->taos->db,
10,982✔
989
                      pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen, &code)) {
10,975✔
990
    // get table meta
991
    STableMeta* pTableMeta = NULL;
15✔
992
    STMT_ERR_RET(stmtGetTableMeta(pStmt, &pTableMeta));
19✔
993

994
    char* newSql = NULL;
7✔
995

996
    // conver update sql to insert sql
997
    code =
7✔
998
        convertUpdateToInsert(sql, &newSql, pTableMeta, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen);
7✔
999
    taosMemoryFree(pTableMeta);
7!
1000

1001
    if (TSDB_CODE_SUCCESS != code) {
7✔
1002
      STMT_ELOG("convert update sql to insert sql failed, code: 0x%x", code);
4!
1003
      return code;
4✔
1004
    }
1005

1006
    // reset request sql
1007
    if (pStmt->exec.pRequest->sqlstr) {
3!
1008
      taosMemoryFreeClear(pStmt->exec.pRequest->sqlstr);
3!
1009
    }
1010
    pStmt->exec.pRequest->sqlstr = newSql;
3✔
1011
    pStmt->exec.pRequest->sqlLen = strlen(newSql);
3✔
1012

1013
    if (pStmt->sql.sqlStr) {
3!
1014
      taosMemoryFreeClear(pStmt->sql.sqlStr);
3!
1015
    }
1016
    pStmt->sql.sqlStr = taosStrndup(newSql, strlen(newSql));
3!
1017
    pStmt->sql.sqlLen = strlen(newSql);
3✔
1018
  }
1019

1020
  STMT_ERR_RET(code);
10,970✔
1021

1022
  char* dbName = NULL;
10,969✔
1023
  if (qParseDbName(sql, length, &dbName)) {
10,969✔
1024
    STMT_ERR_RET(stmtSetDbName(stmt, dbName));
873!
1025
    taosMemoryFreeClear(dbName);
873!
1026
  }
1027

1028
  return TSDB_CODE_SUCCESS;
10,964✔
1029
}
1030

1031
int32_t stmtInitStbInterlaceTableInfo(STscStmt* pStmt) {
77✔
1032
  STableDataCxt** pSrc = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
77✔
1033
  if (!pSrc) {
77!
1034
    return terrno;
×
1035
  }
1036
  STableDataCxt* pDst = NULL;
77✔
1037

1038
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
77!
1039
  pStmt->sql.siInfo.pDataCtx = pDst;
77✔
1040

1041
  SArray* pTblCols = NULL;
77✔
1042
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
70,021✔
1043
    pTblCols = taosArrayInit(20, POINTER_BYTES);
69,795✔
1044
    if (NULL == pTblCols) {
71,475!
1045
      return terrno;
×
1046
    }
1047

1048
    if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
141,419!
1049
      return terrno;
×
1050
    }
1051
  }
1052

1053
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
226✔
1054

1055
  return TSDB_CODE_SUCCESS;
226✔
1056
}
1057

1058
int stmtSetDbName(TAOS_STMT* stmt, const char* dbName) {
873✔
1059
  STscStmt* pStmt = (STscStmt*)stmt;
873✔
1060

1061
  STMT_DLOG("start to set dbName:%s", dbName);
873!
1062

1063
  STMT_ERR_RET(stmtCreateRequest(pStmt));
873!
1064

1065
  // The SQL statement specifies a database name, overriding the previously specified database
1066
  taosMemoryFreeClear(pStmt->exec.pRequest->pDb);
873!
1067
  pStmt->exec.pRequest->pDb = taosStrdup(dbName);
873!
1068
  if (pStmt->exec.pRequest->pDb == NULL) {
873!
1069
    return terrno;
×
1070
  }
1071
  return TSDB_CODE_SUCCESS;
873✔
1072
}
1073

1074
int stmtSetTbName(TAOS_STMT* stmt, const char* tbName) {
15,646✔
1075
  STscStmt* pStmt = (STscStmt*)stmt;
15,646✔
1076

1077
  int64_t startUs = taosGetTimestampUs();
15,659✔
1078

1079
  STMT_DLOG("start to set tbName:%s", tbName);
15,659!
1080

1081
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
15,664!
1082
    return pStmt->errCode;
×
1083
  }
1084

1085
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
15,664!
1086

1087
  int32_t insert = 0;
15,658✔
1088
  STMT_ERR_RET(stmtIsInsert(stmt, &insert));
15,658!
1089
  if (0 == insert) {
15,656!
1090
    tscError("set tb name not available for none insert statement");
×
1091
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1092
  }
1093

1094
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
15,656✔
1095
    STMT_ERR_RET(stmtCreateRequest(pStmt));
10,131!
1096

1097
    STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
10,127!
1098
                              pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1099
    STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
10,127!
1100

1101
    STMT_ERR_RET(stmtGetFromCache(pStmt));
10,119!
1102

1103
    if (pStmt->bInfo.needParse) {
10,125✔
1104
      tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
10,094✔
1105
      pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
10,094✔
1106

1107
      STMT_ERR_RET(stmtParseSql(pStmt));
10,094!
1108
    }
1109
  } else {
1110
    tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
5,525✔
1111
    pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
5,525✔
1112
    pStmt->exec.pRequest->requestId++;
5,525✔
1113
    pStmt->bInfo.needParse = false;
5,525✔
1114
  }
1115

1116
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
15,638✔
1117
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
77!
1118
  }
1119

1120
  int64_t startUs2 = taosGetTimestampUs();
15,647✔
1121
  pStmt->stat.setTbNameUs += startUs2 - startUs;
15,647✔
1122

1123
  return TSDB_CODE_SUCCESS;
15,647✔
1124
}
1125

1126
int stmtSetTbTags(TAOS_STMT* stmt, TAOS_MULTI_BIND* tags) {
11✔
1127
  STscStmt* pStmt = (STscStmt*)stmt;
11✔
1128

1129
  STMT_DLOG_E("start to set tbTags");
11!
1130

1131
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
11!
1132
    return pStmt->errCode;
×
1133
  }
1134

1135
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
11!
1136

1137
  SBoundColInfo* tags_info = (SBoundColInfo*)pStmt->bInfo.boundTags;
11✔
1138
  if (tags_info->numOfBound <= 0 || tags_info->numOfCols <= 0) {
11!
1139
    tscWarn("no tags bound in sql, will not bound tags");
×
1140
    return TSDB_CODE_SUCCESS;
×
1141
  }
1142

1143
  if (pStmt->bInfo.inExecCache) {
11!
1144
    return TSDB_CODE_SUCCESS;
×
1145
  }
1146

1147
  STableDataCxt** pDataBlock =
1148
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
11✔
1149
  if (NULL == pDataBlock) {
11!
1150
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1151
    STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1152
  }
1153

1154
  tscDebug("start to bind stmt tag values");
11!
1155
  STMT_ERR_RET(qBindStmtTagsValue(*pDataBlock, pStmt->bInfo.boundTags, pStmt->bInfo.tbSuid, pStmt->bInfo.stbFName,
11!
1156
                                  pStmt->bInfo.sname.tname, tags, pStmt->exec.pRequest->msgBuf,
1157
                                  pStmt->exec.pRequest->msgBufLen, pStmt->taos->optionInfo.charsetCxt));
1158

1159
  return TSDB_CODE_SUCCESS;
11✔
1160
}
1161

1162
int stmtFetchTagFields(STscStmt* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
2✔
1163
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
2✔
1164
    return pStmt->errCode;
1✔
1165
  }
1166

1167
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
1!
1168
    tscError("invalid operation to get query tag fileds");
×
1169
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1170
  }
1171

1172
  STableDataCxt** pDataBlock =
1173
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
1✔
1174
  if (NULL == pDataBlock) {
1!
1175
    tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1176
    STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1177
  }
1178

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

1181
  return TSDB_CODE_SUCCESS;
1✔
1182
}
1183

1184
int stmtFetchColFields(STscStmt* pStmt, int32_t* fieldNum, TAOS_FIELD_E** fields) {
8✔
1185
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
8!
1186
    return pStmt->errCode;
×
1187
  }
1188

1189
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
8!
1190
    tscError("invalid operation to get query column fileds");
×
1191
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1192
  }
1193

1194
  STableDataCxt** pDataBlock = NULL;
8✔
1195

1196
  if (pStmt->sql.stbInterlaceMode) {
8!
1197
    pDataBlock = &pStmt->sql.siInfo.pDataCtx;
×
1198
  } else {
1199
    pDataBlock =
1200
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
8✔
1201
    if (NULL == pDataBlock) {
8!
1202
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1203
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1204
    }
1205
  }
1206

1207
  STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields));
8!
1208

1209
  return TSDB_CODE_SUCCESS;
8✔
1210
}
1211

1212
/*
1213
SArray* stmtGetFreeCol(STscStmt* pStmt, int32_t* idx) {
1214
  while (true) {
1215
    if (pStmt->exec.smInfo.pColIdx >= STMT_COL_BUF_SIZE) {
1216
      pStmt->exec.smInfo.pColIdx = 0;
1217
    }
1218

1219
    if ((pStmt->exec.smInfo.pColIdx + 1) == atomic_load_32(&pStmt->exec.smInfo.pColFreeIdx)) {
1220
      taosUsleep(1);
1221
      continue;
1222
    }
1223

1224
    *idx = pStmt->exec.smInfo.pColIdx;
1225
    return pStmt->exec.smInfo.pCols[pStmt->exec.smInfo.pColIdx++];
1226
  }
1227
}
1228
*/
1229

1230
int32_t stmtAppendTablePostHandle(STscStmt* pStmt, SStmtQNode* param) {
5,618✔
1231
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
5,618✔
1232
    pStmt->sql.siInfo.pVgroupHash =
4,551✔
1233
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
4,544✔
1234
  }
1235
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
5,625✔
1236
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
4,552✔
1237
  }
1238

1239
  if (NULL == pStmt->sql.siInfo.pRequest) {
5,627✔
1240
    STMT_ERR_RET(buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false,
77!
1241
                              (SRequestObj**)&pStmt->sql.siInfo.pRequest, pStmt->reqid));
1242

1243
    if (pStmt->reqid != 0) {
77!
1244
      pStmt->reqid++;
×
1245
    }
1246
    pStmt->exec.pRequest->syncQuery = true;
77✔
1247

1248
    pStmt->sql.siInfo.requestId = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->requestId;
77✔
1249
    pStmt->sql.siInfo.requestSelf = ((SRequestObj*)pStmt->sql.siInfo.pRequest)->self;
77✔
1250
  }
1251

1252
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
5,627✔
1253
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
372✔
1254
    pStmt->sql.siInfo.tbFromHash = true;
22✔
1255
  }
1256

1257
  if (0 == pStmt->sql.siInfo.firstName[0]) {
5,627✔
1258
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
77✔
1259
  }
1260

1261
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
5,627✔
1262
  param->next = NULL;
5,627✔
1263

1264
  (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
5,627✔
1265

1266
  stmtEnqueue(pStmt, param);
5,631✔
1267

1268
  return TSDB_CODE_SUCCESS;
5,627✔
1269
}
1270

1271
static FORCE_INLINE int32_t stmtGetTableColsFromCache(STscStmt* pStmt, SArray** pTableCols) {
1272
  while (true) {
1273
    if (pStmt->sql.siInfo.pTableColsIdx < taosArrayGetSize(pStmt->sql.siInfo.pTableCols)) {
5,626!
1274
      *pTableCols = (SArray*)taosArrayGetP(pStmt->sql.siInfo.pTableCols, pStmt->sql.siInfo.pTableColsIdx++);
5,613✔
1275
      break;
5,627✔
1276
    } else {
1277
      SArray* pTblCols = NULL;
×
1278
      for (int32_t i = 0; i < 100; i++) {
×
1279
        pTblCols = taosArrayInit(20, POINTER_BYTES);
×
1280
        if (NULL == pTblCols) {
×
1281
          return terrno;
×
1282
        }
1283

1284
        if (taosArrayPush(pStmt->sql.siInfo.pTableCols, &pTblCols) == NULL) {
×
1285
          return terrno;
×
1286
        }
1287
      }
1288
    }
1289
  }
1290

1291
  return TSDB_CODE_SUCCESS;
5,627✔
1292
}
1293

1294
int stmtBindBatch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind, int32_t colIdx) {
86,187✔
1295
  STscStmt* pStmt = (STscStmt*)stmt;
86,187✔
1296
  int32_t   code = 0;
86,187✔
1297

1298
  int64_t startUs = taosGetTimestampUs();
86,241✔
1299

1300
  STMT_DLOG("start to bind stmt data, colIdx:%d", colIdx);
86,241!
1301

1302
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
86,367!
1303
    return pStmt->errCode;
×
1304
  }
1305

1306
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
86,367!
1307

1308
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
86,225!
1309
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1310
    pStmt->bInfo.needParse = false;
×
1311
  }
1312

1313
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
86,225!
1314
    taos_free_result(pStmt->exec.pRequest);
×
1315
    pStmt->exec.pRequest = NULL;
×
1316
  }
1317

1318
  STMT_ERR_RET(stmtCreateRequest(pStmt));
86,225!
1319

1320
  if (pStmt->bInfo.needParse) {
86,194✔
1321
    STMT_ERR_RET(stmtParseSql(pStmt));
826✔
1322
  }
1323

1324
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
86,197✔
1325
    STMT_ERR_RET(qStmtBindParams(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt));
2!
1326

1327
    SParseContext ctx = {.requestId = pStmt->exec.pRequest->requestId,
2✔
1328
                         .acctId = pStmt->taos->acctId,
2✔
1329
                         .db = pStmt->exec.pRequest->pDb,
2✔
1330
                         .topicQuery = false,
1331
                         .pSql = pStmt->sql.sqlStr,
2✔
1332
                         .sqlLen = pStmt->sql.sqlLen,
2✔
1333
                         .pMsg = pStmt->exec.pRequest->msgBuf,
2✔
1334
                         .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE,
1335
                         .pTransporter = pStmt->taos->pAppInfo->pTransporter,
2✔
1336
                         .pStmtCb = NULL,
1337
                         .pUser = pStmt->taos->user,
2✔
1338
                         .setQueryFp = setQueryRequest};
1339

1340
    ctx.mgmtEpSet = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp);
2✔
1341
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &ctx.pCatalog));
2!
1342

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

1345
    if (pStmt->sql.pQuery->haveResultSet) {
2!
1346
      STMT_ERR_RET(setResSchemaInfo(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->pResSchema,
2!
1347
                                    pStmt->sql.pQuery->numOfResCols, pStmt->sql.pQuery->pResExtSchema, true));
1348
      taosMemoryFreeClear(pStmt->sql.pQuery->pResSchema);
2!
1349
      taosMemoryFreeClear(pStmt->sql.pQuery->pResExtSchema);
2!
1350
      setResPrecision(&pStmt->exec.pRequest->body.resInfo, pStmt->sql.pQuery->precision);
2✔
1351
    }
1352

1353
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
2✔
1354
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
2✔
1355
    TSWAP(pStmt->exec.pRequest->targetTableList, pStmt->sql.pQuery->pTargetTableList);
2✔
1356

1357
    return TSDB_CODE_SUCCESS;
2✔
1358
  }
1359

1360
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
86,195!
1361
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1362
  }
1363

1364
  STableDataCxt** pDataBlock = NULL;
86,149✔
1365

1366
  if (pStmt->exec.pCurrBlock) {
86,149✔
1367
    pDataBlock = &pStmt->exec.pCurrBlock;
75,246✔
1368
  } else {
1369
    pDataBlock =
1370
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
10,903✔
1371
    if (NULL == pDataBlock) {
10,916!
1372
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1373
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1374
    }
1375
    pStmt->exec.pCurrBlock = *pDataBlock;
10,916✔
1376
    if (pStmt->sql.stbInterlaceMode) {
10,916✔
1377
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
77✔
1378
      pStmt->exec.pCurrBlock->pData->aCol = NULL;
77✔
1379
    }
1380
  }
1381

1382
  int64_t startUs2 = taosGetTimestampUs();
86,147✔
1383
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
86,147✔
1384

1385
  SStmtQNode* param = NULL;
86,147✔
1386
  if (pStmt->sql.stbInterlaceMode) {
86,147✔
1387
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
11,243!
1388
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
11,253!
1389
    taosArrayClear(param->tblData.aCol);
5,627✔
1390

1391
    // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES);
1392

1393
    param->restoreTbCols = false;
5,620✔
1394
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
5,620✔
1395
  }
1396

1397
  int64_t startUs3 = taosGetTimestampUs();
86,160✔
1398
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
86,160✔
1399

1400
  SArray* pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
86,160✔
1401

1402
  if (colIdx < 0) {
86,160!
1403
    if (pStmt->sql.stbInterlaceMode) {
86,301✔
1404
      (*pDataBlock)->pData->flags = 0;
5,630✔
1405
      code =
1406
          qBindStmtStbColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
5,630✔
1407
                                &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt);
5,630✔
1408
    } else {
1409
      code = qBindStmtColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
80,671✔
1410
                                pStmt->taos->optionInfo.charsetCxt);
80,671✔
1411
    }
1412

1413
    if (code) {
85,844✔
1414
      tscError("qBindStmtColsValue failed, error:%s", tstrerror(code));
1!
1415
      STMT_ERR_RET(code);
1!
1416
    }
1417
  } else {
1418
    if (pStmt->sql.stbInterlaceMode) {
×
1419
      tscError("bind single column not allowed in stb insert mode");
×
1420
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1421
    }
1422

1423
    if (colIdx != (pStmt->bInfo.sBindLastIdx + 1) && colIdx != 0) {
×
1424
      tscError("bind column index not in sequence");
×
1425
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
1426
    }
1427

1428
    pStmt->bInfo.sBindLastIdx = colIdx;
×
1429

1430
    if (0 == colIdx) {
×
1431
      pStmt->bInfo.sBindRowNum = bind->num;
×
1432
    }
1433

1434
    code =
1435
        qBindStmtSingleColValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
×
1436
                                colIdx, pStmt->bInfo.sBindRowNum, pStmt->taos->optionInfo.charsetCxt);
×
1437
    if (code) {
×
1438
      tscError("qBindStmtSingleColValue failed, error:%s", tstrerror(code));
×
1439
      STMT_ERR_RET(code);
×
1440
    }
1441
  }
1442

1443
  int64_t startUs4 = taosGetTimestampUs();
85,888✔
1444
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
85,888✔
1445

1446
  if (pStmt->sql.stbInterlaceMode) {
85,888✔
1447
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
5,630!
1448
  }
1449

1450
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
86,215✔
1451

1452
  return TSDB_CODE_SUCCESS;
86,215✔
1453
}
1454

1455
int stmtAddBatch(TAOS_STMT* stmt) {
84,456✔
1456
  STscStmt* pStmt = (STscStmt*)stmt;
84,456✔
1457

1458
  int64_t startUs = taosGetTimestampUs();
84,415✔
1459

1460
  STMT_DLOG_E("start to add batch");
84,415!
1461

1462
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
84,373!
1463
    return pStmt->errCode;
×
1464
  }
1465

1466
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
84,373!
1467

1468
  if (pStmt->sql.stbInterlaceMode) {
84,609✔
1469
    int64_t startUs2 = taosGetTimestampUs();
4,552✔
1470
    pStmt->stat.addBatchUs += startUs2 - startUs;
4,552✔
1471

1472
    pStmt->sql.siInfo.tableColsReady = false;
4,552✔
1473

1474
    SStmtQNode* param = NULL;
4,552✔
1475
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
9,103!
1476
    param->restoreTbCols = true;
4,551✔
1477
    param->next = NULL;
4,551✔
1478

1479
    stmtEnqueue(pStmt, param);
4,551✔
1480

1481
    return TSDB_CODE_SUCCESS;
4,555✔
1482
  }
1483

1484
  STMT_ERR_RET(stmtCacheBlock(pStmt));
80,058!
1485

1486
  return TSDB_CODE_SUCCESS;
79,949✔
1487
}
1488
/*
1489
int stmtUpdateTableUid(STscStmt* pStmt, SSubmitRsp* pRsp) {
1490
  tscDebug("stmt start to update tbUid, blockNum:%d", pRsp->nBlocks);
1491

1492
  int32_t code = 0;
1493
  int32_t finalCode = 0;
1494
  size_t  keyLen = 0;
1495
  void*   pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
1496
  while (pIter) {
1497
    STableDataCxt* pBlock = *(STableDataCxt**)pIter;
1498
    char*          key = taosHashGetKey(pIter, &keyLen);
1499

1500
    STableMeta* pMeta = qGetTableMetaInDataBlock(pBlock);
1501
    if (pMeta->uid) {
1502
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1503
      continue;
1504
    }
1505

1506
    SSubmitBlkRsp* blkRsp = NULL;
1507
    int32_t        i = 0;
1508
    for (; i < pRsp->nBlocks; ++i) {
1509
      blkRsp = pRsp->pBlocks + i;
1510
      if (strlen(blkRsp->tblFName) != keyLen) {
1511
        continue;
1512
      }
1513

1514
      if (strncmp(blkRsp->tblFName, key, keyLen)) {
1515
        continue;
1516
      }
1517

1518
      break;
1519
    }
1520

1521
    if (i < pRsp->nBlocks) {
1522
      tscDebug("auto created table %s uid updated from %" PRIx64 " to %" PRIx64, blkRsp->tblFName, pMeta->uid,
1523
               blkRsp->uid);
1524

1525
      pMeta->uid = blkRsp->uid;
1526
      pStmt->bInfo.tbUid = blkRsp->uid;
1527
    } else {
1528
      tscDebug("table %s not found in submit rsp, will update from catalog", pStmt->bInfo.tbFName);
1529
      if (NULL == pStmt->pCatalog) {
1530
        code = catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog);
1531
        if (code) {
1532
          pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1533
          finalCode = code;
1534
          continue;
1535
        }
1536
      }
1537

1538
      code = stmtCreateRequest(pStmt);
1539
      if (code) {
1540
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1541
        finalCode = code;
1542
        continue;
1543
      }
1544

1545
      STableMeta*      pTableMeta = NULL;
1546
      SRequestConnInfo conn = {.pTrans = pStmt->taos->pAppInfo->pTransporter,
1547
                               .requestId = pStmt->exec.pRequest->requestId,
1548
                               .requestObjRefId = pStmt->exec.pRequest->self,
1549
                               .mgmtEps = getEpSet_s(&pStmt->taos->pAppInfo->mgmtEp)};
1550
      code = catalogGetTableMeta(pStmt->pCatalog, &conn, &pStmt->bInfo.sname, &pTableMeta);
1551

1552
      pStmt->stat.ctgGetTbMetaNum++;
1553

1554
      taos_free_result(pStmt->exec.pRequest);
1555
      pStmt->exec.pRequest = NULL;
1556

1557
      if (code || NULL == pTableMeta) {
1558
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1559
        finalCode = code;
1560
        taosMemoryFree(pTableMeta);
1561
        continue;
1562
      }
1563

1564
      pMeta->uid = pTableMeta->uid;
1565
      pStmt->bInfo.tbUid = pTableMeta->uid;
1566
      taosMemoryFree(pTableMeta);
1567
    }
1568

1569
    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1570
  }
1571

1572
  return finalCode;
1573
}
1574
*/
1575

1576
/*
1577
int stmtStaticModeExec(TAOS_STMT* stmt) {
1578
  STscStmt*   pStmt = (STscStmt*)stmt;
1579
  int32_t     code = 0;
1580
  SSubmitRsp* pRsp = NULL;
1581
  if (pStmt->sql.staticMode) {
1582
    return TSDB_CODE_TSC_STMT_API_ERROR;
1583
  }
1584

1585
  STMT_DLOG_E("start to exec");
1586

1587
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
1588

1589
  STMT_ERR_RET(qBuildStmtOutputFromTbList(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pTbBlkList,
1590
pStmt->exec.pCurrBlock, pStmt->exec.tbBlkNum));
1591

1592
  launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
1593

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

1605
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
1606

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

1610
_return:
1611

1612
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
1613

1614
  tFreeSSubmitRsp(pRsp);
1615

1616
  ++pStmt->sql.runTimes;
1617

1618
  STMT_RET(code);
1619
}
1620
*/
1621

1622
int stmtExec(TAOS_STMT* stmt) {
15,413✔
1623
  STscStmt*   pStmt = (STscStmt*)stmt;
15,413✔
1624
  int32_t     code = 0;
15,413✔
1625
  SSubmitRsp* pRsp = NULL;
15,413✔
1626

1627
  int64_t startUs = taosGetTimestampUs();
15,424✔
1628

1629
  STMT_DLOG_E("start to exec");
15,424!
1630

1631
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
15,426!
1632
    return pStmt->errCode;
×
1633
  }
1634

1635
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
15,426✔
1636

1637
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
15,416✔
1638
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
2✔
1639
  } else {
1640
    if (pStmt->sql.stbInterlaceMode) {
15,414✔
1641
      int64_t startTs = taosGetTimestampUs();
4,551✔
1642
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
11,051✔
1643
        taosUsleep(1);
6,502✔
1644
      }
1645
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
4,555✔
1646

1647
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
4,555!
1648
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
4,552✔
1649
      pStmt->sql.siInfo.pVgroupHash = NULL;
4,555✔
1650
      pStmt->sql.siInfo.pVgroupList = NULL;
4,555✔
1651
    } else {
1652
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
10,863✔
1653
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
10,865!
1654

1655
      STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
10,865!
1656

1657
      STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
10,867!
1658
    }
1659

1660
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
15,407✔
1661
  }
1662

1663
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
15,421!
1664
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
×
1665
    if (code) {
×
1666
      pStmt->exec.pRequest->code = code;
×
1667
    } else {
1668
      tFreeSSubmitRsp(pRsp);
×
1669
      STMT_ERR_RET(stmtResetStmt(pStmt));
×
1670
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
×
1671
    }
1672
  }
1673

1674
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
15,426!
1675

1676
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
15,426✔
1677
  pStmt->affectedRows += pStmt->exec.affectedRows;
15,427✔
1678

1679
_return:
15,427✔
1680

1681
  while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
15,427!
1682
    taosUsleep(1);
×
1683
  }
1684

1685
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
15,422!
1686

1687
  tFreeSSubmitRsp(pRsp);
15,421✔
1688

1689
  ++pStmt->sql.runTimes;
15,420✔
1690

1691
  int64_t startUs2 = taosGetTimestampUs();
15,427✔
1692
  pStmt->stat.execUseUs += startUs2 - startUs;
15,427✔
1693

1694
  STMT_RET(code);
15,427!
1695
}
1696

1697
int stmtClose(TAOS_STMT* stmt) {
986✔
1698
  STscStmt* pStmt = (STscStmt*)stmt;
986✔
1699

1700
  STMT_DLOG_E("start to free stmt");
986!
1701

1702
  if (pStmt->bindThreadInUse) {
986✔
1703
    pStmt->queue.stopQueue = true;
95✔
1704

1705
    (void)taosThreadMutexLock(&pStmt->queue.mutex);
95✔
1706
    (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
95✔
1707
    (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
95✔
1708
    (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
95✔
1709

1710
    (void)taosThreadJoin(pStmt->bindThread, NULL);
95✔
1711
    pStmt->bindThreadInUse = false;
95✔
1712

1713
    (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
95✔
1714
    (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
95✔
1715
  }
1716

1717
  STMT_DLOG("stmt %p closed, stbInterlaceMode:%d, statInfo: ctgGetTbMetaNum=>%" PRId64 ", getCacheTbInfo=>%" PRId64
986!
1718
            ", parseSqlNum=>%" PRId64 ", pStmt->stat.bindDataNum=>%" PRId64
1719
            ", settbnameAPI:%u, bindAPI:%u, addbatchAPI:%u, execAPI:%u"
1720
            ", setTbNameUs:%" PRId64 ", bindDataUs:%" PRId64 ",%" PRId64 ",%" PRId64 ",%" PRId64 " addBatchUs:%" PRId64
1721
            ", execWaitUs:%" PRId64 ", execUseUs:%" PRId64,
1722
            pStmt, pStmt->sql.stbInterlaceMode, pStmt->stat.ctgGetTbMetaNum, pStmt->stat.getCacheTbInfo,
1723
            pStmt->stat.parseSqlNum, pStmt->stat.bindDataNum, pStmt->seqIds[STMT_SETTBNAME], pStmt->seqIds[STMT_BIND],
1724
            pStmt->seqIds[STMT_ADD_BATCH], pStmt->seqIds[STMT_EXECUTE], pStmt->stat.setTbNameUs,
1725
            pStmt->stat.bindDataUs1, pStmt->stat.bindDataUs2, pStmt->stat.bindDataUs3, pStmt->stat.bindDataUs4,
1726
            pStmt->stat.addBatchUs, pStmt->stat.execWaitUs, pStmt->stat.execUseUs);
1727

1728
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
986!
1729
  taosMemoryFree(stmt);
984!
1730

1731
  return TSDB_CODE_SUCCESS;
985✔
1732
}
1733

1734
const char* stmtErrstr(TAOS_STMT* stmt) {
8✔
1735
  STscStmt* pStmt = (STscStmt*)stmt;
8✔
1736

1737
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
8!
1738
    return (char*)tstrerror(terrno);
×
1739
  }
1740

1741
  pStmt->exec.pRequest->code = terrno;
8✔
1742

1743
  return taos_errstr(pStmt->exec.pRequest);
8✔
1744
}
1745

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

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

1750
int stmtIsInsert(TAOS_STMT* stmt, int* insert) {
101,858✔
1751
  STscStmt* pStmt = (STscStmt*)stmt;
101,858✔
1752

1753
  STMT_DLOG_E("start is insert");
101,858!
1754

1755
  if (pStmt->sql.type) {
101,948✔
1756
    *insert = (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
91,032!
1757
  } else {
1758
    *insert = qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
10,916✔
1759
  }
1760

1761
  return TSDB_CODE_SUCCESS;
101,953✔
1762
}
1763

1764
int stmtGetTagFields(TAOS_STMT* stmt, int* nums, TAOS_FIELD_E** fields) {
4✔
1765
  int32_t   code = 0;
4✔
1766
  STscStmt* pStmt = (STscStmt*)stmt;
4✔
1767
  int32_t   preCode = pStmt->errCode;
4✔
1768

1769
  STMT_DLOG_E("start to get tag fields");
4!
1770

1771
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
4!
1772
    return pStmt->errCode;
×
1773
  }
1774

1775
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
4!
1776
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1777
  }
1778

1779
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
4!
1780

1781
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
4!
1782
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1783
    pStmt->bInfo.needParse = false;
×
1784
  }
1785

1786
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
4!
1787
    taos_free_result(pStmt->exec.pRequest);
×
1788
    pStmt->exec.pRequest = NULL;
×
1789
  }
1790

1791
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
4!
1792

1793
  if (pStmt->bInfo.needParse) {
4✔
1794
    STMT_ERRI_JRET(stmtParseSql(pStmt));
3✔
1795
  }
1796

1797
  STMT_ERRI_JRET(stmtFetchTagFields(stmt, nums, fields));
2✔
1798

1799
_return:
1✔
1800
  // compatible with previous versions
1801
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && (pStmt->bInfo.tbNameFlag & NO_DATA_USING_CLAUSE) == 0x0) {
4!
1802
    code = TSDB_CODE_TSC_STMT_TBNAME_ERROR;
1✔
1803
  }
1804

1805
  if (code != TSDB_CODE_SUCCESS) {
4✔
1806
    taos_free_result(pStmt->exec.pRequest);
3✔
1807
    pStmt->exec.pRequest = NULL;
3✔
1808
  }
1809
  pStmt->errCode = preCode;
4✔
1810

1811
  return code;
4✔
1812
}
1813

1814
int stmtGetColFields(TAOS_STMT* stmt, int* nums, TAOS_FIELD_E** fields) {
3✔
1815
  int32_t   code = 0;
3✔
1816
  STscStmt* pStmt = (STscStmt*)stmt;
3✔
1817
  int32_t   preCode = pStmt->errCode;
3✔
1818

1819
  STMT_DLOG_E("start to get col fields");
3!
1820

1821
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
3!
1822
    return pStmt->errCode;
×
1823
  }
1824

1825
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
3!
1826
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1827
  }
1828

1829
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
3!
1830

1831
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
3!
1832
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1833
    pStmt->bInfo.needParse = false;
×
1834
  }
1835

1836
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
3!
1837
    taos_free_result(pStmt->exec.pRequest);
×
1838
    pStmt->exec.pRequest = NULL;
×
1839
    STMT_ERR_RET(stmtCreateRequest(pStmt));
×
1840
  }
1841

1842
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
3!
1843

1844
  if (pStmt->bInfo.needParse) {
3✔
1845
    STMT_ERRI_JRET(stmtParseSql(pStmt));
2!
1846
  }
1847

1848
  STMT_ERRI_JRET(stmtFetchColFields(stmt, nums, fields));
3!
1849

1850
_return:
3✔
1851

1852
  if (code != TSDB_CODE_SUCCESS) {
3!
1853
    taos_free_result(pStmt->exec.pRequest);
×
1854
    pStmt->exec.pRequest = NULL;
×
1855
  }
1856

1857
  pStmt->errCode = preCode;
3✔
1858

1859
  return code;
3✔
1860
}
1861

1862
int stmtGetParamNum(TAOS_STMT* stmt, int* nums) {
2✔
1863
  int       code = 0;
2✔
1864
  STscStmt* pStmt = (STscStmt*)stmt;
2✔
1865
  int32_t   preCode = pStmt->errCode;
2✔
1866

1867
  STMT_DLOG_E("start to get param num");
2!
1868

1869
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
2!
1870
    return pStmt->errCode;
×
1871
  }
1872

1873
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
2!
1874

1875
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
2!
1876
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1877
    pStmt->bInfo.needParse = false;
×
1878
  }
1879

1880
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
2!
1881
    taos_free_result(pStmt->exec.pRequest);
×
1882
    pStmt->exec.pRequest = NULL;
×
1883
  }
1884

1885
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
2!
1886

1887
  if (pStmt->bInfo.needParse) {
2✔
1888
    STMT_ERRI_JRET(stmtParseSql(pStmt));
1!
1889
  }
1890

1891
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
1!
1892
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
×
1893
  } else {
1894
    STMT_ERRI_JRET(stmtFetchColFields(stmt, nums, NULL));
1!
1895
  }
1896

1897
_return:
1✔
1898

1899
  if (code != TSDB_CODE_SUCCESS) {
2✔
1900
    taos_free_result(pStmt->exec.pRequest);
1✔
1901
    pStmt->exec.pRequest = NULL;
1✔
1902
  }
1903

1904
  pStmt->errCode = preCode;
2✔
1905

1906
  return code;
2✔
1907
}
1908

1909
int stmtGetParam(TAOS_STMT* stmt, int idx, int* type, int* bytes) {
4✔
1910
  int       code = 0;
4✔
1911
  STscStmt* pStmt = (STscStmt*)stmt;
4✔
1912
  int32_t   preCode = pStmt->errCode;
4✔
1913

1914
  STMT_DLOG_E("start to get param");
4!
1915

1916
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
4!
1917
    return pStmt->errCode;
×
1918
  }
1919

1920
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
4!
1921
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1922
  }
1923

1924
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
4!
1925

1926
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
4!
1927
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1928
    pStmt->bInfo.needParse = false;
×
1929
  }
1930

1931
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
4!
1932
    taos_free_result(pStmt->exec.pRequest);
×
1933
    pStmt->exec.pRequest = NULL;
×
1934
  }
1935

1936
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
4!
1937

1938
  if (pStmt->bInfo.needParse) {
4!
1939
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
1940
  }
1941

1942
  int32_t       nums = 0;
4✔
1943
  TAOS_FIELD_E* pField = NULL;
4✔
1944
  STMT_ERRI_JRET(stmtFetchColFields(stmt, &nums, &pField));
4!
1945
  if (idx >= nums) {
4!
1946
    tscError("idx %d is too big", idx);
×
1947
    STMT_ERRI_JRET(TSDB_CODE_INVALID_PARA);
×
1948
  }
1949

1950
  *type = pField[idx].type;
4✔
1951
  *bytes = calcSchemaBytesFromTypeBytes(pField[idx].type, pField[idx].bytes, true);
4✔
1952

1953
_return:
4✔
1954

1955
  if (code != TSDB_CODE_SUCCESS) {
4!
1956
    taos_free_result(pStmt->exec.pRequest);
×
1957
    pStmt->exec.pRequest = NULL;
×
1958
  }
1959

1960
  taosMemoryFree(pField);
4!
1961
  pStmt->errCode = preCode;
4✔
1962

1963
  return code;
4✔
1964
}
1965

1966
TAOS_RES* stmtUseResult(TAOS_STMT* stmt) {
2✔
1967
  STscStmt* pStmt = (STscStmt*)stmt;
2✔
1968

1969
  STMT_DLOG_E("start to use result");
2!
1970

1971
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
2!
1972
    tscError("useResult only for query statement");
×
1973
    return NULL;
×
1974
  }
1975

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