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

taosdata / TDengine / #4113

17 May 2025 06:43AM UTC coverage: 62.054% (-0.8%) from 62.857%
#4113

push

travis-ci

web-flow
Merge pull request #31115 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

154737 of 318088 branches covered (48.65%)

Branch coverage included in aggregate %.

175 of 225 new or added lines in 20 files covered. (77.78%)

5853 existing lines in 216 files now uncovered.

239453 of 317147 relevant lines covered (75.5%)

15121865.73 hits per line

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

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

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

6
#include "clientStmt.h"
7

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

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

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

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

38
  return TSDB_CODE_SUCCESS;
68,592✔
39
}
40

41
bool stmtDequeue(STscStmt* pStmt, SStmtQNode** param) {
68,901✔
42
  int i = 0;
68,901✔
43
  while (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
266,143✔
44
    if (i < 10) {
197,250✔
45
      taosUsleep(1);
181,679✔
46
      i++;
181,670✔
47
    } else {
48
      (void)taosThreadMutexLock(&pStmt->queue.mutex);
15,571✔
49
      if (0 == atomic_load_64((int64_t*)&pStmt->queue.qRemainNum)) {
15,573✔
50
        (void)taosThreadCondWait(&pStmt->queue.waitCond, &pStmt->queue.mutex);
15,562✔
51
      }
52
      (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
15,572✔
53
    }
54
  }
55
  if (pStmt->queue.stopQueue) {
68,610✔
56
    return false;
129✔
57
  }
58
  SStmtQNode* orig = pStmt->queue.head;
68,481✔
59
  SStmtQNode* node = pStmt->queue.head->next;
68,481✔
60
  pStmt->queue.head = pStmt->queue.head->next;
68,481✔
61
  *param = node;
68,481✔
62

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

65
  return true;
68,812✔
66
}
67

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

72
  pStmt->stat.bindDataNum++;
68,671✔
73

74
  (void)taosThreadMutexLock(&pStmt->queue.mutex);
68,671✔
75
  (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
68,750✔
76
  (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
68,792✔
77
  (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
68,710✔
78
}
68,764✔
79

80
static int32_t stmtCreateRequest(STscStmt* pStmt) {
1,141,159✔
81
  int32_t code = 0;
1,141,159✔
82

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

95
  return code;
1,141,160✔
96
}
97

98
int32_t stmtSwitchStatus(STscStmt* pStmt, STMT_STATUS newStatus) {
2,238,807✔
99
  int32_t code = 0;
2,238,807✔
100

101
  if (newStatus >= STMT_INIT && newStatus < STMT_MAX) {
2,238,807!
102
    STMT_LOG_SEQ(newStatus);
2,241,061✔
103
  }
104

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

110
  switch (newStatus) {
2,293,842!
111
    case STMT_PREPARE:
20,733✔
112
      pStmt->errCode = 0;
20,733✔
113
      break;
20,733✔
114
    case STMT_SETTBNAME:
90,794✔
115
      if (STMT_STATUS_EQ(INIT)) {
90,794!
116
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
117
      }
118
      if (!pStmt->sql.stbInterlaceMode && (STMT_STATUS_EQ(BIND) || STMT_STATUS_EQ(BIND_COL))) {
90,794!
119
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
120
      }
121
      break;
90,794✔
122
    case STMT_SETTAGS:
1✔
123
      if (STMT_STATUS_NE(SETTBNAME) && STMT_STATUS_NE(FETCH_FIELDS)) {
1!
124
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
125
      }
126
      break;
1✔
UNCOV
127
    case STMT_FETCH_FIELDS:
×
UNCOV
128
      if (STMT_STATUS_EQ(INIT)) {
×
129
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
130
      }
UNCOV
131
      break;
×
132
    case STMT_BIND:
1,084,933✔
133
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND_COL)) {
1,084,933!
134
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
135
      }
136
      /*
137
            if ((pStmt->sql.type == STMT_TYPE_MULTI_INSERT) && ()) {
138
              code = TSDB_CODE_TSC_STMT_API_ERROR;
139
            }
140
      */
141
      break;
1,084,933✔
142
    case STMT_BIND_COL:
×
143
      if (STMT_STATUS_EQ(INIT) || STMT_STATUS_EQ(BIND)) {
×
144
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
145
      }
146
      break;
×
147
    case STMT_ADD_BATCH:
1,038,845✔
148
      if (STMT_STATUS_NE(BIND) && STMT_STATUS_NE(BIND_COL) && STMT_STATUS_NE(FETCH_FIELDS)) {
1,038,845!
149
        code = TSDB_CODE_TSC_STMT_API_ERROR;
×
150
      }
151
      break;
1,038,845✔
152
    case STMT_EXECUTE:
58,536✔
153
      if (STMT_TYPE_QUERY == pStmt->sql.type) {
58,536✔
154
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS) && STMT_STATUS_NE(BIND) &&
3!
155
            STMT_STATUS_NE(BIND_COL)) {
×
156
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
157
        }
158
      } else {
159
        if (STMT_STATUS_NE(ADD_BATCH) && STMT_STATUS_NE(FETCH_FIELDS)) {
58,533!
160
          code = TSDB_CODE_TSC_STMT_API_ERROR;
×
161
        }
162
      }
163
      break;
58,536✔
164
    default:
×
165
      code = TSDB_CODE_APP_ERROR;
×
166
      break;
×
167
  }
168

169
  STMT_ERR_RET(code);
2,293,842!
170

171
  pStmt->sql.status = newStatus;
2,293,842✔
172

173
  return TSDB_CODE_SUCCESS;
2,293,842✔
174
}
175

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

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

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

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

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

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

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

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

211
  return TSDB_CODE_SUCCESS;
212
}
213

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

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

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

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

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

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

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

262
  return TSDB_CODE_SUCCESS;
20,664✔
263
}
264

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

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

271
  return TSDB_CODE_SUCCESS;
20,613✔
272
}
273

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

278
  STMT_ERR_RET(stmtUpdateBindInfo(stmt, pTableMeta, tags, tbName, sTableName, autoCreateTbl, tbNameFlag));
20,637!
279
  STMT_ERR_RET(stmtUpdateExecInfo(stmt, pVgHash, pBlockHash));
20,658!
280

281
  pStmt->sql.autoCreateTbl = autoCreateTbl;
20,657✔
282
  if (pStmt->sql.autoCreateTbl) {
20,657✔
283
    pStmt->sql.stbInterlaceMode = false;
20,010✔
284
  }
285

286
  return TSDB_CODE_SUCCESS;
20,657✔
287
}
288

UNCOV
289
int32_t stmtGetExecInfo(TAOS_STMT* stmt, SHashObj** pVgHash, SHashObj** pBlockHash) {
×
UNCOV
290
  STscStmt* pStmt = (STscStmt*)stmt;
×
291

UNCOV
292
  *pVgHash = pStmt->sql.pVgHash;
×
UNCOV
293
  pStmt->sql.pVgHash = NULL;
×
294

UNCOV
295
  *pBlockHash = pStmt->exec.pBlockHash;
×
UNCOV
296
  pStmt->exec.pBlockHash = NULL;
×
297

UNCOV
298
  return TSDB_CODE_SUCCESS;
×
299
}
300

301
int32_t stmtCacheBlock(STscStmt* pStmt) {
1,020,382✔
302
  if (pStmt->sql.type != STMT_TYPE_MULTI_INSERT) {
1,020,382✔
303
    return TSDB_CODE_SUCCESS;
980,572✔
304
  }
305

306
  uint64_t uid = pStmt->bInfo.tbUid;
39,810✔
307
  uint64_t cacheUid = (TSDB_CHILD_TABLE == pStmt->bInfo.tbType) ? pStmt->bInfo.tbSuid : uid;
39,810!
308

309
  if (taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid))) {
39,810✔
310
    return TSDB_CODE_SUCCESS;
20,016✔
311
  }
312

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

319
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
20,015!
320

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

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

330
  if (pStmt->sql.autoCreateTbl) {
20,014!
331
    pStmt->bInfo.tagsCached = true;
20,014✔
332
  } else {
UNCOV
333
    pStmt->bInfo.boundTags = NULL;
×
334
  }
335

336
  return TSDB_CODE_SUCCESS;
20,014✔
337
}
338

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

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

349
  STMT_ERR_RET(stmtCreateRequest(pStmt));
20,653!
350

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

355
  pStmt->bInfo.needParse = false;
20,660✔
356

357
  if (pStmt->sql.pQuery->pRoot && 0 == pStmt->sql.type) {
20,660!
358
    pStmt->sql.type = STMT_TYPE_INSERT;
565✔
359
    pStmt->sql.stbInterlaceMode = false;
565✔
360
  } else if (pStmt->sql.pQuery->pPrepareRoot) {
20,095✔
361
    pStmt->sql.type = STMT_TYPE_QUERY;
3✔
362
    pStmt->sql.stbInterlaceMode = false;
3✔
363

364
    return TSDB_CODE_SUCCESS;
3✔
365
  }
366

367
  STableDataCxt** pSrc =
368
      (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
20,657✔
369
  if (NULL == pSrc || NULL == *pSrc) {
20,679!
UNCOV
370
    return terrno;
×
371
  }
372

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

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

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

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

394
  return TSDB_CODE_SUCCESS;
20,660✔
395
}
396

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

405
  pStmt->bInfo.tbName[0] = 0;
59,361✔
406
  pStmt->bInfo.tbFName[0] = 0;
59,361✔
407
  if (!pStmt->bInfo.tagsCached) {
59,361✔
408
    qDestroyBoundColInfo(pStmt->bInfo.boundTags);
19,339✔
409
    taosMemoryFreeClear(pStmt->bInfo.boundTags);
19,336!
410
  }
411
  pStmt->bInfo.stbFName[0] = 0;
59,358✔
412

413
  return TSDB_CODE_SUCCESS;
59,358✔
414
}
415

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

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

430
  pQueue->head = pQueue->tail = pTblBuf->pCurBuff;
17,881✔
431
  pQueue->qRemainNum = 0;
17,881✔
432
  pQueue->head->next = NULL;
17,881✔
433
}
434

435
int32_t stmtCleanExecInfo(STscStmt* pStmt, bool keepTable, bool deepClean) {
79,271✔
436
  if (pStmt->sql.stbInterlaceMode) {
79,271✔
437
    if (deepClean) {
17,991✔
438
      taosHashCleanup(pStmt->exec.pBlockHash);
101✔
439
      pStmt->exec.pBlockHash = NULL;
101✔
440

441
      if (NULL != pStmt->exec.pCurrBlock) {
101!
442
        taosMemoryFreeClear(pStmt->exec.pCurrBlock->pData);
101!
443
        qDestroyStmtDataBlock(pStmt->exec.pCurrBlock);
101✔
444
      }
445
    } else {
446
      pStmt->sql.siInfo.pTableColsIdx = 0;
17,890✔
447
      stmtResetQueueTableBuf(&pStmt->sql.siInfo.tbBuf, &pStmt->queue);
17,890✔
448
    }
449
  } else {
450
    if (STMT_TYPE_QUERY != pStmt->sql.type || deepClean) {
61,280✔
451
      taos_free_result(pStmt->exec.pRequest);
61,277✔
452
      pStmt->exec.pRequest = NULL;
61,286✔
453
    }
454

455
    size_t keyLen = 0;
61,289✔
456
    void*  pIter = taosHashIterate(pStmt->exec.pBlockHash, NULL);
61,289✔
457
    while (pIter) {
122,549✔
458
      STableDataCxt* pBlocks = *(STableDataCxt**)pIter;
61,246✔
459
      char*          key = taosHashGetKey(pIter, &keyLen);
61,246✔
460
      STableMeta*    pMeta = qGetTableMetaInDataBlock(pBlocks);
61,241✔
461

462
      if (keepTable && pBlocks == pStmt->exec.pCurrBlock) {
61,242!
463
        TSWAP(pBlocks->pData, pStmt->exec.pCurrTbData);
40,662✔
464
        STMT_ERR_RET(qResetStmtDataBlock(pBlocks, false));
81,323!
465

466
        pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
40,653✔
467
        continue;
40,658✔
468
      }
469

470
      qDestroyStmtDataBlock(pBlocks);
20,580✔
471
      STMT_ERR_RET(taosHashRemove(pStmt->exec.pBlockHash, key, keyLen));
20,584!
472

473
      pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
20,582✔
474
    }
475

476
    if (keepTable) {
61,303✔
477
      return TSDB_CODE_SUCCESS;
40,661✔
478
    }
479

480
    taosHashCleanup(pStmt->exec.pBlockHash);
20,642✔
481
    pStmt->exec.pBlockHash = NULL;
20,644✔
482

483
    tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
20,644✔
484
    taosMemoryFreeClear(pStmt->exec.pCurrTbData);
20,644!
485
  }
486

487
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
38,627!
488

489
  return TSDB_CODE_SUCCESS;
38,620✔
490
}
491

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

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

502
int32_t stmtCleanSQLInfo(STscStmt* pStmt) {
20,720✔
503
  STMT_DLOG_E("start to free SQL info");
20,720✔
504

505
  taosMemoryFree(pStmt->sql.pBindInfo);
20,720!
506
  taosMemoryFree(pStmt->sql.queryRes.fields);
20,741!
507
  taosMemoryFree(pStmt->sql.queryRes.userFields);
20,741!
508
  taosMemoryFree(pStmt->sql.sqlStr);
20,739!
509
  qDestroyQuery(pStmt->sql.pQuery);
20,743✔
510
  taosArrayDestroy(pStmt->sql.nodeList);
20,742✔
511
  taosHashCleanup(pStmt->sql.pVgHash);
20,738✔
512
  pStmt->sql.pVgHash = NULL;
20,744✔
513

514
  void* pIter = taosHashIterate(pStmt->sql.pTableCache, NULL);
20,744✔
515
  while (pIter) {
40,752✔
516
    SStmtTableCache* pCache = (SStmtTableCache*)pIter;
20,012✔
517

518
    qDestroyStmtDataBlock(pCache->pDataCtx);
20,012✔
519
    qDestroyBoundColInfo(pCache->boundTags);
20,017✔
520
    taosMemoryFreeClear(pCache->boundTags);
20,014!
521

522
    pIter = taosHashIterate(pStmt->sql.pTableCache, pIter);
20,014✔
523
  }
524
  taosHashCleanup(pStmt->sql.pTableCache);
20,740✔
525
  pStmt->sql.pTableCache = NULL;
20,744✔
526

527
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, false, true));
20,744!
528
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
20,736!
529

530
  taos_free_result(pStmt->sql.siInfo.pRequest);
20,737✔
531
  taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
20,741✔
532
  tSimpleHashCleanup(pStmt->sql.siInfo.pTableHash);
20,741✔
533
  taosArrayDestroyEx(pStmt->sql.siInfo.tbBuf.pBufList, stmtFreeTbBuf);
20,737✔
534
  taosMemoryFree(pStmt->sql.siInfo.pTSchema);
20,739!
535
  qDestroyStmtDataBlock(pStmt->sql.siInfo.pDataCtx);
20,740✔
536
  taosArrayDestroyEx(pStmt->sql.siInfo.pTableCols, stmtFreeTbCols);
20,742✔
537

538
  (void)memset(&pStmt->sql, 0, sizeof(pStmt->sql));
20,742✔
539
  pStmt->sql.siInfo.tableColsReady = true;
20,742✔
540

541
  STMT_DLOG_E("end to free SQL info");
20,742✔
542

543
  return TSDB_CODE_SUCCESS;
20,739✔
544
}
545

UNCOV
546
int32_t stmtTryAddTableVgroupInfo(STscStmt* pStmt, int32_t* vgId) {
×
UNCOV
547
  if (*vgId >= 0 && taosHashGet(pStmt->sql.pVgHash, (const char*)vgId, sizeof(*vgId))) {
×
UNCOV
548
    return TSDB_CODE_SUCCESS;
×
549
  }
550

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

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

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

UNCOV
568
  *vgId = vgInfo.vgId;
×
569

UNCOV
570
  return TSDB_CODE_SUCCESS;
×
571
}
572

UNCOV
573
int32_t stmtRebuildDataBlock(STscStmt* pStmt, STableDataCxt* pDataBlock, STableDataCxt** newBlock, uint64_t uid,
×
574
                             uint64_t suid, int32_t vgId) {
UNCOV
575
  STMT_ERR_RET(stmtTryAddTableVgroupInfo(pStmt, &vgId));
×
UNCOV
576
  STMT_ERR_RET(qRebuildStmtDataBlock(newBlock, pDataBlock, uid, suid, vgId, pStmt->sql.autoCreateTbl));
×
577

UNCOV
578
  STMT_DLOG("uid:%" PRId64 ", rebuild table data context, vgId:%d", uid, vgId);
×
579

UNCOV
580
  return TSDB_CODE_SUCCESS;
×
581
}
582

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

590
  pStmt->bInfo.needParse = true;
40,085✔
591
  pStmt->bInfo.inExecCache = false;
40,085✔
592

593
  STableDataCxt** pCxtInExec = taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
40,085✔
594
  if (pCxtInExec) {
40,119✔
595
    pStmt->bInfo.needParse = false;
20,016✔
596
    pStmt->bInfo.inExecCache = true;
20,016✔
597

598
    pStmt->exec.pCurrBlock = *pCxtInExec;
20,016✔
599

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

606
  if (NULL == pStmt->pCatalog) {
20,105✔
607
    STMT_ERR_RET(catalogGetHandle(pStmt->taos->pAppInfo->clusterId, &pStmt->pCatalog));
122!
608
    pStmt->sql.siInfo.pCatalog = pStmt->pCatalog;
131✔
609
  }
610

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

618
    tscDebug("no stmt block cache for tb %s", pStmt->bInfo.tbFName);
20,112✔
619
    return TSDB_CODE_SUCCESS;
20,104✔
620
  }
621

622
  if (pStmt->sql.autoCreateTbl) {
23!
UNCOV
623
    SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &pStmt->bInfo.tbSuid, sizeof(pStmt->bInfo.tbSuid));
×
UNCOV
624
    if (pCache) {
×
UNCOV
625
      pStmt->bInfo.needParse = false;
×
UNCOV
626
      pStmt->bInfo.tbUid = 0;
×
627

UNCOV
628
      STableDataCxt* pNewBlock = NULL;
×
UNCOV
629
      STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, 0, pStmt->bInfo.tbSuid, -1));
×
630

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

UNCOV
636
      pStmt->exec.pCurrBlock = pNewBlock;
×
637

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

UNCOV
640
      return TSDB_CODE_SUCCESS;
×
641
    }
642

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

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

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

UNCOV
657
  pStmt->stat.ctgGetTbMetaNum++;
×
658

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

663
    STMT_ERR_RET(code);
×
664
  }
665

UNCOV
666
  STMT_ERR_RET(code);
×
667

UNCOV
668
  uid = pTableMeta->uid;
×
UNCOV
669
  suid = pTableMeta->suid;
×
UNCOV
670
  tableType = pTableMeta->tableType;
×
UNCOV
671
  pStmt->bInfo.tbVgId = pTableMeta->vgId;
×
UNCOV
672
  vgId = pTableMeta->vgId;
×
673

UNCOV
674
  taosMemoryFree(pTableMeta);
×
675

UNCOV
676
  uint64_t cacheUid = (TSDB_CHILD_TABLE == tableType) ? suid : uid;
×
677

UNCOV
678
  if (uid == pStmt->bInfo.tbUid) {
×
679
    pStmt->bInfo.needParse = false;
×
680

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

683
    return TSDB_CODE_SUCCESS;
×
684
  }
685

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

692
      STMT_ERR_RET(TSDB_CODE_APP_ERROR);
×
693
    }
694

695
    pStmt->bInfo.needParse = false;
×
696

697
    pStmt->bInfo.tbUid = uid;
×
698
    pStmt->bInfo.tbSuid = suid;
×
699
    pStmt->bInfo.tbType = tableType;
×
700
    pStmt->bInfo.boundTags = pCache->boundTags;
×
701
    pStmt->bInfo.tagsCached = true;
×
702

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

705
    return TSDB_CODE_SUCCESS;
×
706
  }
707

UNCOV
708
  SStmtTableCache* pCache = taosHashGet(pStmt->sql.pTableCache, &cacheUid, sizeof(cacheUid));
×
UNCOV
709
  if (pCache) {
×
UNCOV
710
    pStmt->bInfo.needParse = false;
×
711

UNCOV
712
    pStmt->bInfo.tbUid = uid;
×
UNCOV
713
    pStmt->bInfo.tbSuid = suid;
×
UNCOV
714
    pStmt->bInfo.tbType = tableType;
×
UNCOV
715
    pStmt->bInfo.boundTags = pCache->boundTags;
×
UNCOV
716
    pStmt->bInfo.tagsCached = true;
×
717

UNCOV
718
    STableDataCxt* pNewBlock = NULL;
×
UNCOV
719
    STMT_ERR_RET(stmtRebuildDataBlock(pStmt, pCache->pDataCtx, &pNewBlock, uid, suid, vgId));
×
720

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

UNCOV
726
    pStmt->exec.pCurrBlock = pNewBlock;
×
727

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

UNCOV
730
    return TSDB_CODE_SUCCESS;
×
731
  }
732

UNCOV
733
  STMT_ERR_RET(stmtCleanBindInfo(pStmt));
×
734

UNCOV
735
  return TSDB_CODE_SUCCESS;
×
736
}
737

738
int32_t stmtResetStmt(STscStmt* pStmt) {
19,966✔
739
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
19,966!
740

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

746
  pStmt->sql.status = STMT_INIT;
19,974✔
747

748
  return TSDB_CODE_SUCCESS;
19,974✔
749
}
750

751
int32_t stmtAsyncOutput(STscStmt* pStmt, void* param) {
68,794✔
752
  SStmtQNode* pParam = (SStmtQNode*)param;
68,794✔
753

754
  if (pParam->restoreTbCols) {
68,794✔
755
    for (int32_t i = 0; i < pStmt->sql.siInfo.pTableColsIdx; ++i) {
68,702✔
756
      SArray** p = (SArray**)TARRAY_GET_ELEM(pStmt->sql.siInfo.pTableCols, i);
50,823✔
757
      *p = taosArrayInit(20, POINTER_BYTES);
50,823✔
758
      if (*p == NULL) {
50,834!
759
        STMT_ERR_RET(terrno);
×
760
      }
761
    }
762

763
    atomic_store_8((int8_t*)&pStmt->sql.siInfo.tableColsReady, true);
17,879✔
764
  } else {
765
    STMT_ERR_RET(qAppendStmtTableOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, &pParam->tblData, pStmt->exec.pCurrBlock,
50,901!
766
                                        &pStmt->sql.siInfo, NULL));
767

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

770
    (void)atomic_sub_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
50,635✔
771
  }
772
  return TSDB_CODE_SUCCESS;
68,799✔
773
}
774

775
void* stmtBindThreadFunc(void* param) {
129✔
776
  setThreadName("stmtBind");
129✔
777

778
  qInfo("stmt bind thread started");
129!
779

780
  STscStmt* pStmt = (STscStmt*)param;
129✔
781

782
  while (true) {
68,915✔
783
    if (pStmt->queue.stopQueue) {
69,044✔
784
      break;
129✔
785
    }
786

787
    SStmtQNode* asyncParam = NULL;
68,915✔
788
    if (!stmtDequeue(pStmt, &asyncParam)) {
68,915✔
789
      continue;
129✔
790
    }
791

792
    int ret = stmtAsyncOutput(pStmt, asyncParam);
68,811✔
793
    if (ret != 0) {
68,784!
794
      qError("stmtAsyncOutput failed, reason:%s", tstrerror(ret));
×
795
    }
796
  }
797

798
  qInfo("stmt bind thread stopped");
129!
799

800
  return NULL;
129✔
801
}
802

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

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

817
  pStmt->bindThreadInUse = true;
129✔
818

819
  (void)taosThreadAttrDestroy(&thAttr);
129✔
820
  return TSDB_CODE_SUCCESS;
129✔
821
}
822

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

829
  return TSDB_CODE_SUCCESS;
129✔
830
}
831

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

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

848
  pTblBuf->pCurBuff = buff;
129✔
849
  pTblBuf->buffIdx = 1;
129✔
850
  pTblBuf->buffOffset = 0;
129✔
851

852
  return TSDB_CODE_SUCCESS;
129✔
853
}
854

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

860
  pStmt = taosMemoryCalloc(1, sizeof(STscStmt));
752!
861
  if (NULL == pStmt) {
755!
862
    return NULL;
×
863
  }
864

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

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

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

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

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

914
  pStmt->sql.siInfo.tableColsReady = true;
755✔
915

916
  STMT_LOG_SEQ(STMT_INIT);
755✔
917

918
  tscDebug("stmt:%p initialized", pStmt);
755✔
919

920
  return pStmt;
756✔
921
}
922

923
int stmtPrepare(TAOS_STMT* stmt, const char* sql, unsigned long length) {
20,719✔
924
  STscStmt* pStmt = (STscStmt*)stmt;
20,719✔
925

926
  STMT_DLOG_E("start to prepare");
20,719✔
927

928
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
20,733!
929
    return pStmt->errCode;
×
930
  }
931

932
  if (pStmt->sql.status >= STMT_PREPARE) {
20,733✔
933
    STMT_ERR_RET(stmtResetStmt(pStmt));
19,981!
934
  }
935

936
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_PREPARE));
20,728!
937

938
  if (length <= 0) {
20,733✔
939
    length = strlen(sql);
617✔
940
  }
941

942
  pStmt->sql.sqlStr = taosStrndup(sql, length);
20,733!
943
  if (!pStmt->sql.sqlStr) {
20,724!
944
    return terrno;
×
945
  }
946
  pStmt->sql.sqlLen = length;
20,724✔
947
  pStmt->sql.stbInterlaceMode = pStmt->stbInterlaceMode;
20,724✔
948

949
  char* dbName = NULL;
20,724✔
950
  if (qParseDbName(sql, length, &dbName)) {
20,724✔
951
    STMT_ERR_RET(stmtSetDbName(stmt, dbName));
616✔
952
    taosMemoryFreeClear(dbName);
605!
953
  }
954

955
  return TSDB_CODE_SUCCESS;
20,721✔
956
}
957

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

965
  STMT_ERR_RET(qCloneStmtDataBlock(&pDst, *pSrc, true));
99!
966
  pStmt->sql.siInfo.pDataCtx = pDst;
91✔
967

968
  SArray* pTblCols = NULL;
91✔
969
  for (int32_t i = 0; i < STMT_TABLE_COLS_NUM; i++) {
76,539!
970
    pTblCols = taosArrayInit(20, POINTER_BYTES);
76,573✔
971
    if (NULL == pTblCols) {
80,991!
972
      return terrno;
×
973
    }
974

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

UNCOV
980
  pStmt->sql.siInfo.boundTags = pStmt->bInfo.boundTags;
×
981

UNCOV
982
  return TSDB_CODE_SUCCESS;
×
983
}
984

985
int stmtSetDbName(TAOS_STMT* stmt, const char* dbName) {
612✔
986
  STscStmt* pStmt = (STscStmt*)stmt;
612✔
987

988
  STMT_DLOG("start to set dbName:%s", dbName);
612!
989

990
  STMT_ERR_RET(stmtCreateRequest(pStmt));
612!
991

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

1001
int stmtSetTbName(TAOS_STMT* stmt, const char* tbName) {
90,823✔
1002
  STscStmt* pStmt = (STscStmt*)stmt;
90,823✔
1003

1004
  int64_t startUs = taosGetTimestampUs();
90,791✔
1005

1006
  STMT_DLOG("start to set tbName:%s", tbName);
90,791✔
1007

1008
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
90,810!
1009
    return pStmt->errCode;
×
1010
  }
1011

1012
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTBNAME));
90,810!
1013

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

1021
  if (!pStmt->sql.stbInterlaceMode || NULL == pStmt->sql.siInfo.pDataCtx) {
90,743✔
1022
    STMT_ERR_RET(stmtCreateRequest(pStmt));
40,114!
1023

1024
    STMT_ERR_RET(qCreateSName(&pStmt->bInfo.sname, tbName, pStmt->taos->acctId, pStmt->exec.pRequest->pDb,
40,114!
1025
                              pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen));
1026
    STMT_ERR_RET(tNameExtractFullName(&pStmt->bInfo.sname, pStmt->bInfo.tbFName));
40,107!
1027

1028
    STMT_ERR_RET(stmtGetFromCache(pStmt));
40,091!
1029

1030
    if (pStmt->bInfo.needParse) {
40,116✔
1031
      tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
20,103✔
1032
      pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
20,103✔
1033

1034
      STMT_ERR_RET(stmtParseSql(pStmt));
20,103✔
1035
    }
1036
  } else {
1037
    tstrncpy(pStmt->bInfo.tbName, tbName, sizeof(pStmt->bInfo.tbName));
50,629✔
1038
    pStmt->bInfo.tbName[sizeof(pStmt->bInfo.tbName) - 1] = 0;
50,629✔
1039
    pStmt->exec.pRequest->requestId++;
50,629✔
1040
    pStmt->bInfo.needParse = false;
50,629✔
1041
  }
1042

1043
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
90,718✔
1044
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
99!
1045
  }
1046

1047
  int64_t startUs2 = taosGetTimestampUs();
90,695✔
1048
  pStmt->stat.setTbNameUs += startUs2 - startUs;
90,695✔
1049

1050
  return TSDB_CODE_SUCCESS;
90,695✔
1051
}
1052

1053
int stmtSetTbTags(TAOS_STMT* stmt, TAOS_MULTI_BIND* tags) {
1✔
1054
  STscStmt* pStmt = (STscStmt*)stmt;
1✔
1055

1056
  STMT_DLOG_E("start to set tbTags");
1!
1057

1058
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
1!
1059
    return pStmt->errCode;
×
1060
  }
1061

1062
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_SETTAGS));
1!
1063

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

1070
  if (pStmt->bInfo.inExecCache) {
1!
1071
    return TSDB_CODE_SUCCESS;
×
1072
  }
1073

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

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

1086
  return TSDB_CODE_SUCCESS;
1✔
1087
}
1088

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

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

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

UNCOV
1106
  STMT_ERR_RET(qBuildStmtTagFields(*pDataBlock, pStmt->bInfo.boundTags, fieldNum, fields));
×
1107

UNCOV
1108
  return TSDB_CODE_SUCCESS;
×
1109
}
1110

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

UNCOV
1116
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1117
    tscError("invalid operation to get query column fileds");
×
1118
    STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1119
  }
1120

UNCOV
1121
  STableDataCxt** pDataBlock = NULL;
×
1122

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

UNCOV
1134
  STMT_ERR_RET(qBuildStmtColFields(*pDataBlock, fieldNum, fields));
×
1135

UNCOV
1136
  return TSDB_CODE_SUCCESS;
×
1137
}
1138

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

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

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

1157
int32_t stmtAppendTablePostHandle(STscStmt* pStmt, SStmtQNode* param) {
50,608✔
1158
  if (NULL == pStmt->sql.siInfo.pVgroupHash) {
50,608✔
1159
    pStmt->sql.siInfo.pVgroupHash =
17,887✔
1160
        taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
17,891✔
1161
  }
1162
  if (NULL == pStmt->sql.siInfo.pVgroupList) {
50,604✔
1163
    pStmt->sql.siInfo.pVgroupList = taosArrayInit(64, POINTER_BYTES);
17,887✔
1164
  }
1165

1166
  if (NULL == pStmt->sql.siInfo.pRequest) {
50,605✔
1167
    STMT_ERR_RET(buildRequest(pStmt->taos->id, pStmt->sql.sqlStr, pStmt->sql.sqlLen, NULL, false,
100!
1168
                              (SRequestObj**)&pStmt->sql.siInfo.pRequest, pStmt->reqid));
1169

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

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

1179
  if (!pStmt->sql.siInfo.tbFromHash && pStmt->sql.siInfo.firstName[0] &&
50,603✔
1180
      0 == strcmp(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName)) {
12,092✔
1181
    pStmt->sql.siInfo.tbFromHash = true;
30✔
1182
  }
1183

1184
  if (0 == pStmt->sql.siInfo.firstName[0]) {
50,603✔
1185
    tstrncpy(pStmt->sql.siInfo.firstName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
100✔
1186
  }
1187

1188
  param->tblData.getFromHash = pStmt->sql.siInfo.tbFromHash;
50,603✔
1189
  param->next = NULL;
50,603✔
1190

1191
  (void)atomic_add_fetch_64(&pStmt->sql.siInfo.tbRemainNum, 1);
50,603✔
1192

1193
  stmtEnqueue(pStmt, param);
50,844✔
1194

1195
  return TSDB_CODE_SUCCESS;
50,863✔
1196
}
1197

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

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

1218
  return TSDB_CODE_SUCCESS;
50,540✔
1219
}
1220

1221
int stmtBindBatch(TAOS_STMT* stmt, TAOS_MULTI_BIND* bind, int32_t colIdx) {
1,083,486✔
1222
  STscStmt* pStmt = (STscStmt*)stmt;
1,083,486✔
1223
  int32_t   code = 0;
1,083,486✔
1224

1225
  int64_t startUs = taosGetTimestampUs();
1,086,307✔
1226

1227
  STMT_DLOG("start to bind stmt data, colIdx:%d", colIdx);
1,086,307✔
1228

1229
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
1,086,607!
1230
    return pStmt->errCode;
×
1231
  }
1232

1233
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_BIND));
1,086,607!
1234

1235
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
1,080,712!
1236
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1237
    pStmt->bInfo.needParse = false;
×
1238
  }
1239

1240
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
1,080,712!
1241
    taos_free_result(pStmt->exec.pRequest);
×
1242
    pStmt->exec.pRequest = NULL;
×
1243
  }
1244

1245
  STMT_ERR_RET(stmtCreateRequest(pStmt));
1,080,712!
1246

1247
  if (pStmt->bInfo.needParse) {
1,079,012✔
1248
    STMT_ERR_RET(stmtParseSql(pStmt));
574!
1249
  }
1250

1251
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
1,079,143✔
1252
    STMT_ERR_RET(qStmtBindParams(pStmt->sql.pQuery, bind, colIdx, pStmt->taos->optionInfo.charsetCxt));
3!
1253

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

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

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

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

1280
    TSWAP(pStmt->exec.pRequest->dbList, pStmt->sql.pQuery->pDbList);
3✔
1281
    TSWAP(pStmt->exec.pRequest->tableList, pStmt->sql.pQuery->pTableList);
3✔
1282
    TSWAP(pStmt->exec.pRequest->targetTableList, pStmt->sql.pQuery->pTargetTableList);
3✔
1283

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

1288
    // STMT_ERR_RET(stmtBackupQueryFields(pStmt));
1289

1290
    return TSDB_CODE_SUCCESS;
3✔
1291
  }
1292

1293
  if (pStmt->sql.stbInterlaceMode && NULL == pStmt->sql.siInfo.pDataCtx) {
1,079,140!
1294
    STMT_ERR_RET(stmtInitStbInterlaceTableInfo(pStmt));
×
1295
  }
1296

1297
  STableDataCxt** pDataBlock = NULL;
1,077,906✔
1298

1299
  if (pStmt->exec.pCurrBlock) {
1,077,906✔
1300
    pDataBlock = &pStmt->exec.pCurrBlock;
1,057,250✔
1301
  } else {
1302
    pDataBlock =
1303
        (STableDataCxt**)taosHashGet(pStmt->exec.pBlockHash, pStmt->bInfo.tbFName, strlen(pStmt->bInfo.tbFName));
20,656✔
1304
    if (NULL == pDataBlock) {
20,684!
1305
      tscError("table %s not found in exec blockHash", pStmt->bInfo.tbFName);
×
1306
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_CACHE_ERROR);
×
1307
    }
1308
    pStmt->exec.pCurrBlock = *pDataBlock;
20,684✔
1309
    if (pStmt->sql.stbInterlaceMode) {
20,684✔
1310
      taosArrayDestroy(pStmt->exec.pCurrBlock->pData->aCol);
101✔
1311
      pStmt->exec.pCurrBlock->pData->aCol = NULL;
100✔
1312
    }
1313
  }
1314

1315
  int64_t startUs2 = taosGetTimestampUs();
1,076,832✔
1316
  pStmt->stat.bindDataUs1 += startUs2 - startUs;
1,076,832✔
1317

1318
  SStmtQNode* param = NULL;
1,076,832✔
1319
  if (pStmt->sql.stbInterlaceMode) {
1,076,832✔
1320
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
101,148!
1321
    STMT_ERR_RET(stmtGetTableColsFromCache(pStmt, &param->tblData.aCol));
101,117!
1322
    taosArrayClear(param->tblData.aCol);
50,540✔
1323

1324
    // param->tblData.aCol = taosArrayInit(20, POINTER_BYTES);
1325

1326
    param->restoreTbCols = false;
50,453✔
1327
    tstrncpy(param->tblData.tbName, pStmt->bInfo.tbName, TSDB_TABLE_NAME_LEN);
50,453✔
1328
  }
1329

1330
  int64_t startUs3 = taosGetTimestampUs();
1,073,803✔
1331
  pStmt->stat.bindDataUs2 += startUs3 - startUs2;
1,073,803✔
1332

1333
  SArray* pCols = pStmt->sql.stbInterlaceMode ? param->tblData.aCol : (*pDataBlock)->pData->aCol;
1,073,803✔
1334

1335
  if (colIdx < 0) {
1,073,803!
1336
    if (pStmt->sql.stbInterlaceMode) {
1,078,169✔
1337
      (*pDataBlock)->pData->flags = 0;
50,521✔
1338
      code =
1339
          qBindStmtStbColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
50,521✔
1340
                                &pStmt->sql.siInfo.pTSchema, pStmt->sql.pBindInfo, pStmt->taos->optionInfo.charsetCxt);
50,521✔
1341
    } else {
1342
      code = qBindStmtColsValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
1,027,648✔
1343
                                pStmt->taos->optionInfo.charsetCxt);
1,027,648✔
1344
    }
1345

1346
    if (code) {
1,074,251!
UNCOV
1347
      tscError("qBindStmtColsValue failed, error:%s", tstrerror(code));
×
UNCOV
1348
      STMT_ERR_RET(code);
×
1349
    }
1350
  } else {
1351
    if (pStmt->sql.stbInterlaceMode) {
×
1352
      tscError("bind single column not allowed in stb insert mode");
×
1353
      STMT_ERR_RET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1354
    }
1355

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

1361
    pStmt->bInfo.sBindLastIdx = colIdx;
×
1362

1363
    if (0 == colIdx) {
×
UNCOV
1364
      pStmt->bInfo.sBindRowNum = bind->num;
×
1365
    }
1366

1367
    code =
1368
        qBindStmtSingleColValue(*pDataBlock, pCols, bind, pStmt->exec.pRequest->msgBuf, pStmt->exec.pRequest->msgBufLen,
×
1369
                                colIdx, pStmt->bInfo.sBindRowNum, pStmt->taos->optionInfo.charsetCxt);
×
UNCOV
1370
    if (code) {
×
1371
      tscError("qBindStmtSingleColValue failed, error:%s", tstrerror(code));
×
1372
      STMT_ERR_RET(code);
×
1373
    }
1374
  }
1375

1376
  int64_t startUs4 = taosGetTimestampUs();
1,073,428✔
1377
  pStmt->stat.bindDataUs3 += startUs4 - startUs3;
1,073,428✔
1378

1379
  if (pStmt->sql.stbInterlaceMode) {
1,073,428✔
1380
    STMT_ERR_RET(stmtAppendTablePostHandle(pStmt, param));
50,623!
1381
  }
1382

1383
  pStmt->stat.bindDataUs4 += taosGetTimestampUs() - startUs4;
1,076,847✔
1384

1385
  return TSDB_CODE_SUCCESS;
1,076,847✔
1386
}
1387

1388
int stmtAddBatch(TAOS_STMT* stmt) {
1,042,241✔
1389
  STscStmt* pStmt = (STscStmt*)stmt;
1,042,241✔
1390

1391
  int64_t startUs = taosGetTimestampUs();
1,038,434✔
1392

1393
  STMT_DLOG_E("start to add batch");
1,038,434✔
1394

1395
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
1,038,293!
1396
    return pStmt->errCode;
×
1397
  }
1398

1399
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_ADD_BATCH));
1,038,293!
1400

1401
  if (pStmt->sql.stbInterlaceMode) {
1,038,533✔
1402
    int64_t startUs2 = taosGetTimestampUs();
17,885✔
1403
    pStmt->stat.addBatchUs += startUs2 - startUs;
17,885✔
1404

1405
    pStmt->sql.siInfo.tableColsReady = false;
17,885✔
1406

1407
    SStmtQNode* param = NULL;
17,885✔
1408
    STMT_ERR_RET(stmtAllocQNodeFromBuf(&pStmt->sql.siInfo.tbBuf, (void**)&param));
35,771!
1409
    param->restoreTbCols = true;
17,886✔
1410
    param->next = NULL;
17,886✔
1411

1412
    stmtEnqueue(pStmt, param);
17,886✔
1413

1414
    return TSDB_CODE_SUCCESS;
17,895✔
1415
  }
1416

1417
  STMT_ERR_RET(stmtCacheBlock(pStmt));
1,020,651!
1418

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

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

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

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

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

1451
      break;
1452
    }
1453

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

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

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

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

1485
      pStmt->stat.ctgGetTbMetaNum++;
1486

1487
      taos_free_result(pStmt->exec.pRequest);
1488
      pStmt->exec.pRequest = NULL;
1489

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

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

1502
    pIter = taosHashIterate(pStmt->exec.pBlockHash, pIter);
1503
  }
1504

1505
  return finalCode;
1506
}
1507
*/
1508

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

1518
  STMT_DLOG_E("start to exec");
1519

1520
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
1521

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

1525
  launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
1526

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

1538
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
1539

1540
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
1541
  pStmt->affectedRows += pStmt->exec.affectedRows;
1542

1543
_return:
1544

1545
  stmtCleanExecInfo(pStmt, (code ? false : true), false);
1546

1547
  tFreeSSubmitRsp(pRsp);
1548

1549
  ++pStmt->sql.runTimes;
1550

1551
  STMT_RET(code);
1552
}
1553
*/
1554

1555
int stmtExec(TAOS_STMT* stmt) {
58,513✔
1556
  STscStmt*   pStmt = (STscStmt*)stmt;
58,513✔
1557
  int32_t     code = 0;
58,513✔
1558
  SSubmitRsp* pRsp = NULL;
58,513✔
1559

1560
  int64_t startUs = taosGetTimestampUs();
58,529✔
1561

1562
  STMT_DLOG_E("start to exec");
58,529✔
1563

1564
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
58,534!
1565
    return pStmt->errCode;
×
1566
  }
1567

1568
  STMT_ERR_RET(stmtSwitchStatus(pStmt, STMT_EXECUTE));
58,534✔
1569

1570
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
58,516✔
1571
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
3✔
1572
  } else {
1573
    if (pStmt->sql.stbInterlaceMode) {
58,513✔
1574
      int64_t startTs = taosGetTimestampUs();
17,892✔
1575
      while (atomic_load_64(&pStmt->sql.siInfo.tbRemainNum)) {
2,276,460✔
1576
        taosUsleep(1);
2,258,045✔
1577
      }
1578
      pStmt->stat.execWaitUs += taosGetTimestampUs() - startTs;
17,884✔
1579

1580
      STMT_ERR_RET(qBuildStmtFinOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->sql.siInfo.pVgroupList));
17,884!
1581
      taosHashCleanup(pStmt->sql.siInfo.pVgroupHash);
17,889✔
1582
      pStmt->sql.siInfo.pVgroupHash = NULL;
17,890✔
1583
      pStmt->sql.siInfo.pVgroupList = NULL;
17,890✔
1584
    } else {
1585
      tDestroySubmitTbData(pStmt->exec.pCurrTbData, TSDB_MSG_FLG_ENCODE);
40,626✔
1586
      taosMemoryFreeClear(pStmt->exec.pCurrTbData);
40,624!
1587

1588
      STMT_ERR_RET(qCloneCurrentTbData(pStmt->exec.pCurrBlock, &pStmt->exec.pCurrTbData));
40,624!
1589

1590
      STMT_ERR_RET(qBuildStmtOutput(pStmt->sql.pQuery, pStmt->sql.pVgHash, pStmt->exec.pBlockHash));
40,638!
1591
    }
1592

1593
    launchQueryImpl(pStmt->exec.pRequest, pStmt->sql.pQuery, true, NULL);
58,523✔
1594
  }
1595

1596
  if (pStmt->exec.pRequest->code && NEED_CLIENT_HANDLE_ERROR(pStmt->exec.pRequest->code)) {
58,549!
1597
    code = refreshMeta(pStmt->exec.pRequest->pTscObj, pStmt->exec.pRequest);
2✔
1598
    if (code) {
×
1599
      pStmt->exec.pRequest->code = code;
×
1600
    } else {
1601
      tFreeSSubmitRsp(pRsp);
×
1602
      STMT_ERR_RET(stmtResetStmt(pStmt));
×
1603
      STMT_ERR_RET(TSDB_CODE_NEED_RETRY);
×
1604
    }
1605
  }
1606

1607
  STMT_ERR_JRET(pStmt->exec.pRequest->code);
58,547✔
1608

1609
  pStmt->exec.affectedRows = taos_affected_rows(pStmt->exec.pRequest);
58,546✔
1610
  pStmt->affectedRows += pStmt->exec.affectedRows;
58,557✔
1611

1612
_return:
58,558✔
1613

1614
  while (0 == atomic_load_8((int8_t*)&pStmt->sql.siInfo.tableColsReady)) {
58,558!
1615
    taosUsleep(1);
×
1616
  }
1617

1618
  STMT_ERR_RET(stmtCleanExecInfo(pStmt, (code ? false : true), false));
58,554!
1619

1620
  tFreeSSubmitRsp(pRsp);
58,547✔
1621

1622
  ++pStmt->sql.runTimes;
58,541✔
1623

1624
  int64_t startUs2 = taosGetTimestampUs();
58,542✔
1625
  pStmt->stat.execUseUs += startUs2 - startUs;
58,542✔
1626

1627
  STMT_RET(code);
58,542✔
1628
}
1629

1630
int stmtClose(TAOS_STMT* stmt) {
755✔
1631
  STscStmt* pStmt = (STscStmt*)stmt;
755✔
1632

1633
  STMT_DLOG_E("start to free stmt");
755✔
1634

1635
  if (pStmt->bindThreadInUse) {
755✔
1636
    pStmt->queue.stopQueue = true;
129✔
1637

1638
    (void)taosThreadMutexLock(&pStmt->queue.mutex);
129✔
1639
    (void)atomic_add_fetch_64(&pStmt->queue.qRemainNum, 1);
129✔
1640
    (void)taosThreadCondSignal(&(pStmt->queue.waitCond));
129✔
1641
    (void)taosThreadMutexUnlock(&pStmt->queue.mutex);
129✔
1642

1643
    (void)taosThreadJoin(pStmt->bindThread, NULL);
129✔
1644
    pStmt->bindThreadInUse = false;
129✔
1645

1646
    (void)taosThreadCondDestroy(&pStmt->queue.waitCond);
129✔
1647
    (void)taosThreadMutexDestroy(&pStmt->queue.mutex);
129✔
1648
  }
1649

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

1661
  STMT_ERR_RET(stmtCleanSQLInfo(pStmt));
756!
1662
  taosMemoryFree(stmt);
755!
1663

1664
  return TSDB_CODE_SUCCESS;
756✔
1665
}
1666

1667
const char* stmtErrstr(TAOS_STMT* stmt) {
6✔
1668
  STscStmt* pStmt = (STscStmt*)stmt;
6✔
1669

1670
  if (stmt == NULL || NULL == pStmt->exec.pRequest) {
6!
1671
    return (char*)tstrerror(terrno);
1✔
1672
  }
1673

1674
  pStmt->exec.pRequest->code = terrno;
5✔
1675

1676
  return taos_errstr(pStmt->exec.pRequest);
5✔
1677
}
1678

1679
int stmtAffectedRows(TAOS_STMT* stmt) { return ((STscStmt*)stmt)->affectedRows; }
3✔
1680

UNCOV
1681
int stmtAffectedRowsOnce(TAOS_STMT* stmt) { return ((STscStmt*)stmt)->exec.affectedRows; }
×
1682

1683
int stmtIsInsert(TAOS_STMT* stmt, int* insert) {
1,179,046✔
1684
  STscStmt* pStmt = (STscStmt*)stmt;
1,179,046✔
1685

1686
  STMT_DLOG_E("start is insert");
1,179,046✔
1687

1688
  if (pStmt->sql.type) {
1,179,438✔
1689
    *insert = (STMT_TYPE_INSERT == pStmt->sql.type || STMT_TYPE_MULTI_INSERT == pStmt->sql.type);
1,158,768!
1690
  } else {
1691
    *insert = qIsInsertValuesSql(pStmt->sql.sqlStr, pStmt->sql.sqlLen);
20,670✔
1692
  }
1693

1694
  return TSDB_CODE_SUCCESS;
1,179,451✔
1695
}
1696

UNCOV
1697
int stmtGetTagFields(TAOS_STMT* stmt, int* nums, TAOS_FIELD_E** fields) {
×
UNCOV
1698
  int32_t   code = 0;
×
UNCOV
1699
  STscStmt* pStmt = (STscStmt*)stmt;
×
UNCOV
1700
  int32_t   preCode = pStmt->errCode;
×
1701

UNCOV
1702
  STMT_DLOG_E("start to get tag fields");
×
1703

UNCOV
1704
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1705
    return pStmt->errCode;
×
1706
  }
1707

UNCOV
1708
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1709
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1710
  }
1711

UNCOV
1712
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
×
1713

UNCOV
1714
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
×
1715
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1716
    pStmt->bInfo.needParse = false;
×
1717
  }
1718

UNCOV
1719
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
×
1720
    taos_free_result(pStmt->exec.pRequest);
×
1721
    pStmt->exec.pRequest = NULL;
×
1722
  }
1723

UNCOV
1724
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
1725

UNCOV
1726
  if (pStmt->bInfo.needParse) {
×
UNCOV
1727
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
1728
  }
1729

UNCOV
1730
  STMT_ERRI_JRET(stmtFetchTagFields(stmt, nums, fields));
×
1731

UNCOV
1732
_return:
×
1733
  // compatible with previous versions
UNCOV
1734
  if (code == TSDB_CODE_PAR_TABLE_NOT_EXIST && (pStmt->bInfo.tbNameFlag & NO_DATA_USING_CLAUSE) == 0x0) {
×
UNCOV
1735
    code = TSDB_CODE_TSC_STMT_TBNAME_ERROR;
×
1736
  }
1737

UNCOV
1738
  if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
1739
    taos_free_result(pStmt->exec.pRequest);
×
UNCOV
1740
    pStmt->exec.pRequest = NULL;
×
1741
  }
UNCOV
1742
  pStmt->errCode = preCode;
×
1743

UNCOV
1744
  return code;
×
1745
}
1746

UNCOV
1747
int stmtGetColFields(TAOS_STMT* stmt, int* nums, TAOS_FIELD_E** fields) {
×
UNCOV
1748
  int32_t   code = 0;
×
UNCOV
1749
  STscStmt* pStmt = (STscStmt*)stmt;
×
UNCOV
1750
  int32_t   preCode = pStmt->errCode;
×
1751

UNCOV
1752
  STMT_DLOG_E("start to get col fields");
×
1753

UNCOV
1754
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1755
    return pStmt->errCode;
×
1756
  }
1757

UNCOV
1758
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1759
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1760
  }
1761

UNCOV
1762
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
×
1763

UNCOV
1764
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
×
1765
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1766
    pStmt->bInfo.needParse = false;
×
1767
  }
1768

UNCOV
1769
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
×
1770
    taos_free_result(pStmt->exec.pRequest);
×
1771
    pStmt->exec.pRequest = NULL;
×
1772
    STMT_ERR_RET(stmtCreateRequest(pStmt));
×
1773
  }
1774

UNCOV
1775
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
1776

UNCOV
1777
  if (pStmt->bInfo.needParse) {
×
UNCOV
1778
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
1779
  }
1780

UNCOV
1781
  STMT_ERRI_JRET(stmtFetchColFields(stmt, nums, fields));
×
1782

UNCOV
1783
_return:
×
1784

UNCOV
1785
  if (code != TSDB_CODE_SUCCESS) {
×
1786
    taos_free_result(pStmt->exec.pRequest);
×
1787
    pStmt->exec.pRequest = NULL;
×
1788
  }
1789

UNCOV
1790
  pStmt->errCode = preCode;
×
1791

UNCOV
1792
  return code;
×
1793
}
1794

UNCOV
1795
int stmtGetParamNum(TAOS_STMT* stmt, int* nums) {
×
UNCOV
1796
  int       code = 0;
×
UNCOV
1797
  STscStmt* pStmt = (STscStmt*)stmt;
×
UNCOV
1798
  int32_t   preCode = pStmt->errCode;
×
1799

UNCOV
1800
  STMT_DLOG_E("start to get param num");
×
1801

UNCOV
1802
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1803
    return pStmt->errCode;
×
1804
  }
1805

UNCOV
1806
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
×
1807

UNCOV
1808
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
×
1809
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1810
    pStmt->bInfo.needParse = false;
×
1811
  }
1812

UNCOV
1813
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
×
1814
    taos_free_result(pStmt->exec.pRequest);
×
1815
    pStmt->exec.pRequest = NULL;
×
1816
  }
1817

UNCOV
1818
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
1819

UNCOV
1820
  if (pStmt->bInfo.needParse) {
×
UNCOV
1821
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
1822
  }
1823

UNCOV
1824
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1825
    *nums = taosArrayGetSize(pStmt->sql.pQuery->pPlaceholderValues);
×
1826
  } else {
UNCOV
1827
    STMT_ERRI_JRET(stmtFetchColFields(stmt, nums, NULL));
×
1828
  }
1829

UNCOV
1830
_return:
×
1831

UNCOV
1832
  if (code != TSDB_CODE_SUCCESS) {
×
UNCOV
1833
    taos_free_result(pStmt->exec.pRequest);
×
UNCOV
1834
    pStmt->exec.pRequest = NULL;
×
1835
  }
1836

UNCOV
1837
  pStmt->errCode = preCode;
×
1838

UNCOV
1839
  return code;
×
1840
}
1841

UNCOV
1842
int stmtGetParam(TAOS_STMT* stmt, int idx, int* type, int* bytes) {
×
UNCOV
1843
  int       code = 0;
×
UNCOV
1844
  STscStmt* pStmt = (STscStmt*)stmt;
×
UNCOV
1845
  int32_t   preCode = pStmt->errCode;
×
1846

UNCOV
1847
  STMT_DLOG_E("start to get param");
×
1848

UNCOV
1849
  if (pStmt->errCode != TSDB_CODE_SUCCESS) {
×
1850
    return pStmt->errCode;
×
1851
  }
1852

UNCOV
1853
  if (STMT_TYPE_QUERY == pStmt->sql.type) {
×
1854
    STMT_ERRI_JRET(TSDB_CODE_TSC_STMT_API_ERROR);
×
1855
  }
1856

UNCOV
1857
  STMT_ERRI_JRET(stmtSwitchStatus(pStmt, STMT_FETCH_FIELDS));
×
1858

UNCOV
1859
  if (pStmt->bInfo.needParse && pStmt->sql.runTimes && pStmt->sql.type > 0 &&
×
1860
      STMT_TYPE_MULTI_INSERT != pStmt->sql.type) {
×
1861
    pStmt->bInfo.needParse = false;
×
1862
  }
1863

UNCOV
1864
  if (pStmt->exec.pRequest && STMT_TYPE_QUERY == pStmt->sql.type && pStmt->sql.runTimes) {
×
1865
    taos_free_result(pStmt->exec.pRequest);
×
1866
    pStmt->exec.pRequest = NULL;
×
1867
  }
1868

UNCOV
1869
  STMT_ERRI_JRET(stmtCreateRequest(pStmt));
×
1870

UNCOV
1871
  if (pStmt->bInfo.needParse) {
×
1872
    STMT_ERRI_JRET(stmtParseSql(pStmt));
×
1873
  }
1874

UNCOV
1875
  int32_t       nums = 0;
×
UNCOV
1876
  TAOS_FIELD_E* pField = NULL;
×
UNCOV
1877
  STMT_ERRI_JRET(stmtFetchColFields(stmt, &nums, &pField));
×
UNCOV
1878
  if (idx >= nums) {
×
1879
    tscError("idx %d is too big", idx);
×
1880
    STMT_ERRI_JRET(TSDB_CODE_INVALID_PARA);
×
1881
  }
1882

UNCOV
1883
  *type = pField[idx].type;
×
UNCOV
1884
  *bytes = calcSchemaBytesFromTypeBytes(pField[idx].type, pField[idx].bytes, true);
×
1885

UNCOV
1886
_return:
×
1887

UNCOV
1888
  if (code != TSDB_CODE_SUCCESS) {
×
1889
    taos_free_result(pStmt->exec.pRequest);
×
1890
    pStmt->exec.pRequest = NULL;
×
1891
  }
1892

UNCOV
1893
  taosMemoryFree(pField);
×
UNCOV
1894
  pStmt->errCode = preCode;
×
1895

UNCOV
1896
  return code;
×
1897
}
1898

1899
TAOS_RES* stmtUseResult(TAOS_STMT* stmt) {
3✔
1900
  STscStmt* pStmt = (STscStmt*)stmt;
3✔
1901

1902
  STMT_DLOG_E("start to use result");
3✔
1903

1904
  if (STMT_TYPE_QUERY != pStmt->sql.type) {
3!
1905
    tscError("useResult only for query statement");
×
1906
    return NULL;
×
1907
  }
1908

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